1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**************************************************************************/
4 /* IBM System i and System p Virtual NIC Device Driver */
5 /* Copyright (C) 2014 IBM Corp. */
6 /* Santiago Leon (santi_leon@yahoo.com) */
7 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
8 /* John Allen (jallen@linux.vnet.ibm.com) */
11 /* This module contains the implementation of a virtual ethernet device */
12 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
13 /* option of the RS/6000 Platform Architecture to interface with virtual */
14 /* ethernet NICs that are presented to the partition by the hypervisor. */
16 /* Messages are passed between the VNIC driver and the VNIC server using */
17 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
18 /* issue and receive commands that initiate communication with the server */
19 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
20 /* are used by the driver to notify the server that a packet is */
21 /* ready for transmission or that a buffer has been added to receive a */
22 /* packet. Subsequently, sCRQs are used by the server to notify the */
23 /* driver that a packet transmission has been completed or that a packet */
24 /* has been received and placed in a waiting buffer. */
26 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
27 /* which skbs are DMA mapped and immediately unmapped when the transmit */
28 /* or receive has been completed, the VNIC driver is required to use */
29 /* "long term mapping". This entails that large, continuous DMA mapped */
30 /* buffers are allocated on driver initialization and these buffers are */
31 /* then continuously reused to pass skbs to and from the VNIC server. */
33 /**************************************************************************/
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/types.h>
38 #include <linux/errno.h>
39 #include <linux/completion.h>
40 #include <linux/ioport.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/kernel.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
46 #include <linux/init.h>
47 #include <linux/delay.h>
49 #include <linux/ethtool.h>
50 #include <linux/proc_fs.h>
51 #include <linux/if_arp.h>
54 #include <linux/ipv6.h>
55 #include <linux/irq.h>
56 #include <linux/kthread.h>
57 #include <linux/seq_file.h>
58 #include <linux/interrupt.h>
59 #include <net/net_namespace.h>
60 #include <asm/hvcall.h>
61 #include <linux/atomic.h>
63 #include <asm/iommu.h>
64 #include <linux/uaccess.h>
65 #include <asm/firmware.h>
66 #include <linux/workqueue.h>
67 #include <linux/if_vlan.h>
68 #include <linux/utsname.h>
72 static const char ibmvnic_driver_name[] = "ibmvnic";
73 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
75 MODULE_AUTHOR("Santiago Leon");
76 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
77 MODULE_LICENSE("GPL");
78 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
80 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
81 static int ibmvnic_remove(struct vio_dev *);
82 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
83 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
84 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
85 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
86 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
87 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
88 union sub_crq *sub_crq);
89 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
90 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
91 static int enable_scrq_irq(struct ibmvnic_adapter *,
92 struct ibmvnic_sub_crq_queue *);
93 static int disable_scrq_irq(struct ibmvnic_adapter *,
94 struct ibmvnic_sub_crq_queue *);
95 static int pending_scrq(struct ibmvnic_adapter *,
96 struct ibmvnic_sub_crq_queue *);
97 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
98 struct ibmvnic_sub_crq_queue *);
99 static int ibmvnic_poll(struct napi_struct *napi, int data);
100 static void send_query_map(struct ibmvnic_adapter *adapter);
101 static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
102 static int send_request_unmap(struct ibmvnic_adapter *, u8);
103 static int send_login(struct ibmvnic_adapter *adapter);
104 static void send_query_cap(struct ibmvnic_adapter *adapter);
105 static int init_sub_crqs(struct ibmvnic_adapter *);
106 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
107 static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
108 static void release_crq_queue(struct ibmvnic_adapter *);
109 static int __ibmvnic_set_mac(struct net_device *, u8 *);
110 static int init_crq_queue(struct ibmvnic_adapter *adapter);
111 static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
113 struct ibmvnic_stat {
114 char name[ETH_GSTRING_LEN];
118 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
119 offsetof(struct ibmvnic_statistics, stat))
120 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
122 static const struct ibmvnic_stat ibmvnic_stats[] = {
123 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
124 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
125 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
126 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
127 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
128 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
129 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
130 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
131 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
132 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
133 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
134 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
135 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
136 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
137 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
138 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
139 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
140 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
141 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
142 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
143 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
144 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
147 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
148 unsigned long length, unsigned long *number,
151 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
154 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
162 * ibmvnic_wait_for_completion - Check device state and wait for completion
163 * @adapter: private device data
164 * @comp_done: completion structure to wait for
165 * @timeout: time to wait in milliseconds
167 * Wait for a completion signal or until the timeout limit is reached
168 * while checking that the device is still active.
170 static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
171 struct completion *comp_done,
172 unsigned long timeout)
174 struct net_device *netdev;
175 unsigned long div_timeout;
178 netdev = adapter->netdev;
180 div_timeout = msecs_to_jiffies(timeout / retry);
182 if (!adapter->crq.active) {
183 netdev_err(netdev, "Device down!\n");
188 if (wait_for_completion_timeout(comp_done, div_timeout))
191 netdev_err(netdev, "Operation timed out.\n");
195 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
196 struct ibmvnic_long_term_buff *ltb, int size)
198 struct device *dev = &adapter->vdev->dev;
202 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
206 dev_err(dev, "Couldn't alloc long term buffer\n");
209 ltb->map_id = adapter->map_id;
212 mutex_lock(&adapter->fw_lock);
213 adapter->fw_done_rc = 0;
214 reinit_completion(&adapter->fw_done);
215 rc = send_request_map(adapter, ltb->addr,
216 ltb->size, ltb->map_id);
218 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
219 mutex_unlock(&adapter->fw_lock);
223 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
226 "Long term map request aborted or timed out,rc = %d\n",
228 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
229 mutex_unlock(&adapter->fw_lock);
233 if (adapter->fw_done_rc) {
234 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
235 adapter->fw_done_rc);
236 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
237 mutex_unlock(&adapter->fw_lock);
240 mutex_unlock(&adapter->fw_lock);
244 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
245 struct ibmvnic_long_term_buff *ltb)
247 struct device *dev = &adapter->vdev->dev;
252 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
253 adapter->reset_reason != VNIC_RESET_MOBILITY)
254 send_request_unmap(adapter, ltb->map_id);
255 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
258 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
259 struct ibmvnic_long_term_buff *ltb)
261 struct device *dev = &adapter->vdev->dev;
264 memset(ltb->buff, 0, ltb->size);
266 mutex_lock(&adapter->fw_lock);
267 adapter->fw_done_rc = 0;
269 reinit_completion(&adapter->fw_done);
270 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
272 mutex_unlock(&adapter->fw_lock);
276 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
279 "Reset failed, long term map request timed out or aborted\n");
280 mutex_unlock(&adapter->fw_lock);
284 if (adapter->fw_done_rc) {
286 "Reset failed, attempting to free and reallocate buffer\n");
287 free_long_term_buff(adapter, ltb);
288 mutex_unlock(&adapter->fw_lock);
289 return alloc_long_term_buff(adapter, ltb, ltb->size);
291 mutex_unlock(&adapter->fw_lock);
295 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
299 for (i = 0; i < adapter->num_active_rx_pools; i++)
300 adapter->rx_pool[i].active = 0;
303 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
304 struct ibmvnic_rx_pool *pool)
306 int count = pool->size - atomic_read(&pool->available);
307 u64 handle = adapter->rx_scrq[pool->index]->handle;
308 struct device *dev = &adapter->vdev->dev;
309 int buffers_added = 0;
310 unsigned long lpar_rc;
311 union sub_crq sub_crq;
323 for (i = 0; i < count; ++i) {
324 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
326 dev_err(dev, "Couldn't replenish rx buff\n");
327 adapter->replenish_no_mem++;
331 index = pool->free_map[pool->next_free];
333 if (pool->rx_buff[index].skb)
334 dev_err(dev, "Inconsistent free_map!\n");
336 /* Copy the skb to the long term mapped DMA buffer */
337 offset = index * pool->buff_size;
338 dst = pool->long_term_buff.buff + offset;
339 memset(dst, 0, pool->buff_size);
340 dma_addr = pool->long_term_buff.addr + offset;
341 pool->rx_buff[index].data = dst;
343 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
344 pool->rx_buff[index].dma = dma_addr;
345 pool->rx_buff[index].skb = skb;
346 pool->rx_buff[index].pool_index = pool->index;
347 pool->rx_buff[index].size = pool->buff_size;
349 memset(&sub_crq, 0, sizeof(sub_crq));
350 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
351 sub_crq.rx_add.correlator =
352 cpu_to_be64((u64)&pool->rx_buff[index]);
353 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
354 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
356 /* The length field of the sCRQ is defined to be 24 bits so the
357 * buffer size needs to be left shifted by a byte before it is
358 * converted to big endian to prevent the last byte from being
361 #ifdef __LITTLE_ENDIAN__
364 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
366 lpar_rc = send_subcrq(adapter, handle, &sub_crq);
367 if (lpar_rc != H_SUCCESS)
371 adapter->replenish_add_buff_success++;
372 pool->next_free = (pool->next_free + 1) % pool->size;
374 atomic_add(buffers_added, &pool->available);
378 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
379 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
380 pool->free_map[pool->next_free] = index;
381 pool->rx_buff[index].skb = NULL;
383 dev_kfree_skb_any(skb);
384 adapter->replenish_add_buff_failure++;
385 atomic_add(buffers_added, &pool->available);
387 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
388 /* Disable buffer pool replenishment and report carrier off if
389 * queue is closed or pending failover.
390 * Firmware guarantees that a signal will be sent to the
391 * driver, triggering a reset.
393 deactivate_rx_pools(adapter);
394 netif_carrier_off(adapter->netdev);
398 static void replenish_pools(struct ibmvnic_adapter *adapter)
402 adapter->replenish_task_cycles++;
403 for (i = 0; i < adapter->num_active_rx_pools; i++) {
404 if (adapter->rx_pool[i].active)
405 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
409 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
411 kfree(adapter->tx_stats_buffers);
412 kfree(adapter->rx_stats_buffers);
413 adapter->tx_stats_buffers = NULL;
414 adapter->rx_stats_buffers = NULL;
417 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
419 adapter->tx_stats_buffers =
420 kcalloc(IBMVNIC_MAX_QUEUES,
421 sizeof(struct ibmvnic_tx_queue_stats),
423 if (!adapter->tx_stats_buffers)
426 adapter->rx_stats_buffers =
427 kcalloc(IBMVNIC_MAX_QUEUES,
428 sizeof(struct ibmvnic_rx_queue_stats),
430 if (!adapter->rx_stats_buffers)
436 static void release_stats_token(struct ibmvnic_adapter *adapter)
438 struct device *dev = &adapter->vdev->dev;
440 if (!adapter->stats_token)
443 dma_unmap_single(dev, adapter->stats_token,
444 sizeof(struct ibmvnic_statistics),
446 adapter->stats_token = 0;
449 static int init_stats_token(struct ibmvnic_adapter *adapter)
451 struct device *dev = &adapter->vdev->dev;
454 stok = dma_map_single(dev, &adapter->stats,
455 sizeof(struct ibmvnic_statistics),
457 if (dma_mapping_error(dev, stok)) {
458 dev_err(dev, "Couldn't map stats buffer\n");
462 adapter->stats_token = stok;
463 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
467 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
469 struct ibmvnic_rx_pool *rx_pool;
474 if (!adapter->rx_pool)
477 buff_size = adapter->cur_rx_buf_sz;
478 rx_scrqs = adapter->num_active_rx_pools;
479 for (i = 0; i < rx_scrqs; i++) {
480 rx_pool = &adapter->rx_pool[i];
482 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
484 if (rx_pool->buff_size != buff_size) {
485 free_long_term_buff(adapter, &rx_pool->long_term_buff);
486 rx_pool->buff_size = buff_size;
487 rc = alloc_long_term_buff(adapter,
488 &rx_pool->long_term_buff,
492 rc = reset_long_term_buff(adapter,
493 &rx_pool->long_term_buff);
499 for (j = 0; j < rx_pool->size; j++)
500 rx_pool->free_map[j] = j;
502 memset(rx_pool->rx_buff, 0,
503 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
505 atomic_set(&rx_pool->available, 0);
506 rx_pool->next_alloc = 0;
507 rx_pool->next_free = 0;
514 static void release_rx_pools(struct ibmvnic_adapter *adapter)
516 struct ibmvnic_rx_pool *rx_pool;
519 if (!adapter->rx_pool)
522 for (i = 0; i < adapter->num_active_rx_pools; i++) {
523 rx_pool = &adapter->rx_pool[i];
525 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
527 kfree(rx_pool->free_map);
528 free_long_term_buff(adapter, &rx_pool->long_term_buff);
530 if (!rx_pool->rx_buff)
533 for (j = 0; j < rx_pool->size; j++) {
534 if (rx_pool->rx_buff[j].skb) {
535 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
536 rx_pool->rx_buff[j].skb = NULL;
540 kfree(rx_pool->rx_buff);
543 kfree(adapter->rx_pool);
544 adapter->rx_pool = NULL;
545 adapter->num_active_rx_pools = 0;
548 static int init_rx_pools(struct net_device *netdev)
550 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
551 struct device *dev = &adapter->vdev->dev;
552 struct ibmvnic_rx_pool *rx_pool;
557 rxadd_subcrqs = adapter->num_active_rx_scrqs;
558 buff_size = adapter->cur_rx_buf_sz;
560 adapter->rx_pool = kcalloc(rxadd_subcrqs,
561 sizeof(struct ibmvnic_rx_pool),
563 if (!adapter->rx_pool) {
564 dev_err(dev, "Failed to allocate rx pools\n");
568 adapter->num_active_rx_pools = rxadd_subcrqs;
570 for (i = 0; i < rxadd_subcrqs; i++) {
571 rx_pool = &adapter->rx_pool[i];
573 netdev_dbg(adapter->netdev,
574 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
575 i, adapter->req_rx_add_entries_per_subcrq,
578 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
580 rx_pool->buff_size = buff_size;
583 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
585 if (!rx_pool->free_map) {
586 release_rx_pools(adapter);
590 rx_pool->rx_buff = kcalloc(rx_pool->size,
591 sizeof(struct ibmvnic_rx_buff),
593 if (!rx_pool->rx_buff) {
594 dev_err(dev, "Couldn't alloc rx buffers\n");
595 release_rx_pools(adapter);
599 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
600 rx_pool->size * rx_pool->buff_size)) {
601 release_rx_pools(adapter);
605 for (j = 0; j < rx_pool->size; ++j)
606 rx_pool->free_map[j] = j;
608 atomic_set(&rx_pool->available, 0);
609 rx_pool->next_alloc = 0;
610 rx_pool->next_free = 0;
616 static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
617 struct ibmvnic_tx_pool *tx_pool)
621 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
625 memset(tx_pool->tx_buff, 0,
626 tx_pool->num_buffers *
627 sizeof(struct ibmvnic_tx_buff));
629 for (i = 0; i < tx_pool->num_buffers; i++)
630 tx_pool->free_map[i] = i;
632 tx_pool->consumer_index = 0;
633 tx_pool->producer_index = 0;
638 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
643 if (!adapter->tx_pool)
646 tx_scrqs = adapter->num_active_tx_pools;
647 for (i = 0; i < tx_scrqs; i++) {
648 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
651 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
659 static void release_vpd_data(struct ibmvnic_adapter *adapter)
664 kfree(adapter->vpd->buff);
670 static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
671 struct ibmvnic_tx_pool *tx_pool)
673 kfree(tx_pool->tx_buff);
674 kfree(tx_pool->free_map);
675 free_long_term_buff(adapter, &tx_pool->long_term_buff);
678 static void release_tx_pools(struct ibmvnic_adapter *adapter)
682 if (!adapter->tx_pool)
685 for (i = 0; i < adapter->num_active_tx_pools; i++) {
686 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
687 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
690 kfree(adapter->tx_pool);
691 adapter->tx_pool = NULL;
692 kfree(adapter->tso_pool);
693 adapter->tso_pool = NULL;
694 adapter->num_active_tx_pools = 0;
697 static int init_one_tx_pool(struct net_device *netdev,
698 struct ibmvnic_tx_pool *tx_pool,
699 int num_entries, int buf_size)
701 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
704 tx_pool->tx_buff = kcalloc(num_entries,
705 sizeof(struct ibmvnic_tx_buff),
707 if (!tx_pool->tx_buff)
710 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
711 num_entries * buf_size))
714 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
715 if (!tx_pool->free_map)
718 for (i = 0; i < num_entries; i++)
719 tx_pool->free_map[i] = i;
721 tx_pool->consumer_index = 0;
722 tx_pool->producer_index = 0;
723 tx_pool->num_buffers = num_entries;
724 tx_pool->buf_size = buf_size;
729 static int init_tx_pools(struct net_device *netdev)
731 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
735 tx_subcrqs = adapter->num_active_tx_scrqs;
736 adapter->tx_pool = kcalloc(tx_subcrqs,
737 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
738 if (!adapter->tx_pool)
741 adapter->tso_pool = kcalloc(tx_subcrqs,
742 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
743 if (!adapter->tso_pool)
746 adapter->num_active_tx_pools = tx_subcrqs;
748 for (i = 0; i < tx_subcrqs; i++) {
749 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
750 adapter->req_tx_entries_per_subcrq,
751 adapter->req_mtu + VLAN_HLEN);
753 release_tx_pools(adapter);
757 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
761 release_tx_pools(adapter);
769 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
773 if (adapter->napi_enabled)
776 for (i = 0; i < adapter->req_rx_queues; i++)
777 napi_enable(&adapter->napi[i]);
779 adapter->napi_enabled = true;
782 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
786 if (!adapter->napi_enabled)
789 for (i = 0; i < adapter->req_rx_queues; i++) {
790 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
791 napi_disable(&adapter->napi[i]);
794 adapter->napi_enabled = false;
797 static int init_napi(struct ibmvnic_adapter *adapter)
801 adapter->napi = kcalloc(adapter->req_rx_queues,
802 sizeof(struct napi_struct), GFP_KERNEL);
806 for (i = 0; i < adapter->req_rx_queues; i++) {
807 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
808 netif_napi_add(adapter->netdev, &adapter->napi[i],
809 ibmvnic_poll, NAPI_POLL_WEIGHT);
812 adapter->num_active_rx_napi = adapter->req_rx_queues;
816 static void release_napi(struct ibmvnic_adapter *adapter)
823 for (i = 0; i < adapter->num_active_rx_napi; i++) {
824 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
825 netif_napi_del(&adapter->napi[i]);
828 kfree(adapter->napi);
829 adapter->napi = NULL;
830 adapter->num_active_rx_napi = 0;
831 adapter->napi_enabled = false;
834 static int ibmvnic_login(struct net_device *netdev)
836 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
837 unsigned long timeout = msecs_to_jiffies(30000);
845 if (retry_count > retries) {
846 netdev_warn(netdev, "Login attempts exceeded\n");
850 adapter->init_done_rc = 0;
851 reinit_completion(&adapter->init_done);
852 rc = send_login(adapter);
854 netdev_warn(netdev, "Unable to login\n");
858 if (!wait_for_completion_timeout(&adapter->init_done,
860 netdev_warn(netdev, "Login timed out, retrying...\n");
862 adapter->init_done_rc = 0;
867 if (adapter->init_done_rc == ABORTED) {
868 netdev_warn(netdev, "Login aborted, retrying...\n");
870 adapter->init_done_rc = 0;
872 /* FW or device may be busy, so
873 * wait a bit before retrying login
876 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
878 release_sub_crqs(adapter, 1);
882 "Received partial success, retrying...\n");
883 adapter->init_done_rc = 0;
884 reinit_completion(&adapter->init_done);
885 send_query_cap(adapter);
886 if (!wait_for_completion_timeout(&adapter->init_done,
889 "Capabilities query timed out\n");
893 rc = init_sub_crqs(adapter);
896 "SCRQ initialization failed\n");
900 rc = init_sub_crq_irqs(adapter);
903 "SCRQ irq initialization failed\n");
906 } else if (adapter->init_done_rc) {
907 netdev_warn(netdev, "Adapter login failed\n");
912 __ibmvnic_set_mac(netdev, adapter->mac_addr);
917 static void release_login_buffer(struct ibmvnic_adapter *adapter)
919 kfree(adapter->login_buf);
920 adapter->login_buf = NULL;
923 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
925 kfree(adapter->login_rsp_buf);
926 adapter->login_rsp_buf = NULL;
929 static void release_resources(struct ibmvnic_adapter *adapter)
931 release_vpd_data(adapter);
933 release_tx_pools(adapter);
934 release_rx_pools(adapter);
936 release_napi(adapter);
937 release_login_rsp_buffer(adapter);
940 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
942 struct net_device *netdev = adapter->netdev;
943 unsigned long timeout = msecs_to_jiffies(30000);
944 union ibmvnic_crq crq;
948 netdev_dbg(netdev, "setting link state %d\n", link_state);
950 memset(&crq, 0, sizeof(crq));
951 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
952 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
953 crq.logical_link_state.link_state = link_state;
958 reinit_completion(&adapter->init_done);
959 rc = ibmvnic_send_crq(adapter, &crq);
961 netdev_err(netdev, "Failed to set link state\n");
965 if (!wait_for_completion_timeout(&adapter->init_done,
967 netdev_err(netdev, "timeout setting link state\n");
971 if (adapter->init_done_rc == PARTIALSUCCESS) {
972 /* Partuial success, delay and re-send */
975 } else if (adapter->init_done_rc) {
976 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
977 adapter->init_done_rc);
978 return adapter->init_done_rc;
985 static int set_real_num_queues(struct net_device *netdev)
987 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
990 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
991 adapter->req_tx_queues, adapter->req_rx_queues);
993 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
995 netdev_err(netdev, "failed to set the number of tx queues\n");
999 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1001 netdev_err(netdev, "failed to set the number of rx queues\n");
1006 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1008 struct device *dev = &adapter->vdev->dev;
1009 union ibmvnic_crq crq;
1013 if (adapter->vpd->buff)
1014 len = adapter->vpd->len;
1016 mutex_lock(&adapter->fw_lock);
1017 adapter->fw_done_rc = 0;
1018 reinit_completion(&adapter->fw_done);
1020 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1021 crq.get_vpd_size.cmd = GET_VPD_SIZE;
1022 rc = ibmvnic_send_crq(adapter, &crq);
1024 mutex_unlock(&adapter->fw_lock);
1028 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1030 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
1031 mutex_unlock(&adapter->fw_lock);
1034 mutex_unlock(&adapter->fw_lock);
1036 if (!adapter->vpd->len)
1039 if (!adapter->vpd->buff)
1040 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1041 else if (adapter->vpd->len != len)
1042 adapter->vpd->buff =
1043 krealloc(adapter->vpd->buff,
1044 adapter->vpd->len, GFP_KERNEL);
1046 if (!adapter->vpd->buff) {
1047 dev_err(dev, "Could allocate VPD buffer\n");
1051 adapter->vpd->dma_addr =
1052 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1054 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
1055 dev_err(dev, "Could not map VPD buffer\n");
1056 kfree(adapter->vpd->buff);
1057 adapter->vpd->buff = NULL;
1061 mutex_lock(&adapter->fw_lock);
1062 adapter->fw_done_rc = 0;
1063 reinit_completion(&adapter->fw_done);
1065 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1066 crq.get_vpd.cmd = GET_VPD;
1067 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1068 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1069 rc = ibmvnic_send_crq(adapter, &crq);
1071 kfree(adapter->vpd->buff);
1072 adapter->vpd->buff = NULL;
1073 mutex_unlock(&adapter->fw_lock);
1077 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1079 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1080 kfree(adapter->vpd->buff);
1081 adapter->vpd->buff = NULL;
1082 mutex_unlock(&adapter->fw_lock);
1086 mutex_unlock(&adapter->fw_lock);
1090 static int init_resources(struct ibmvnic_adapter *adapter)
1092 struct net_device *netdev = adapter->netdev;
1095 rc = set_real_num_queues(netdev);
1099 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1103 /* Vital Product Data (VPD) */
1104 rc = ibmvnic_get_vpd(adapter);
1106 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1110 adapter->map_id = 1;
1112 rc = init_napi(adapter);
1116 send_query_map(adapter);
1118 rc = init_rx_pools(netdev);
1122 rc = init_tx_pools(netdev);
1126 static int __ibmvnic_open(struct net_device *netdev)
1128 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1129 enum vnic_state prev_state = adapter->state;
1132 adapter->state = VNIC_OPENING;
1133 replenish_pools(adapter);
1134 ibmvnic_napi_enable(adapter);
1136 /* We're ready to receive frames, enable the sub-crq interrupts and
1137 * set the logical link state to up
1139 for (i = 0; i < adapter->req_rx_queues; i++) {
1140 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1141 if (prev_state == VNIC_CLOSED)
1142 enable_irq(adapter->rx_scrq[i]->irq);
1143 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1146 for (i = 0; i < adapter->req_tx_queues; i++) {
1147 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1148 if (prev_state == VNIC_CLOSED)
1149 enable_irq(adapter->tx_scrq[i]->irq);
1150 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1153 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1155 for (i = 0; i < adapter->req_rx_queues; i++)
1156 napi_disable(&adapter->napi[i]);
1157 release_resources(adapter);
1161 netif_tx_start_all_queues(netdev);
1163 if (prev_state == VNIC_CLOSED) {
1164 for (i = 0; i < adapter->req_rx_queues; i++)
1165 napi_schedule(&adapter->napi[i]);
1168 adapter->state = VNIC_OPEN;
1172 static int ibmvnic_open(struct net_device *netdev)
1174 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1177 /* If device failover is pending, just set device state and return.
1178 * Device operation will be handled by reset routine.
1180 if (adapter->failover_pending) {
1181 adapter->state = VNIC_OPEN;
1185 if (adapter->state != VNIC_CLOSED) {
1186 rc = ibmvnic_login(netdev);
1190 rc = init_resources(adapter);
1192 netdev_err(netdev, "failed to initialize resources\n");
1193 release_resources(adapter);
1198 rc = __ibmvnic_open(netdev);
1203 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1205 struct ibmvnic_rx_pool *rx_pool;
1206 struct ibmvnic_rx_buff *rx_buff;
1211 if (!adapter->rx_pool)
1214 rx_scrqs = adapter->num_active_rx_pools;
1215 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1217 /* Free any remaining skbs in the rx buffer pools */
1218 for (i = 0; i < rx_scrqs; i++) {
1219 rx_pool = &adapter->rx_pool[i];
1220 if (!rx_pool || !rx_pool->rx_buff)
1223 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1224 for (j = 0; j < rx_entries; j++) {
1225 rx_buff = &rx_pool->rx_buff[j];
1226 if (rx_buff && rx_buff->skb) {
1227 dev_kfree_skb_any(rx_buff->skb);
1228 rx_buff->skb = NULL;
1234 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1235 struct ibmvnic_tx_pool *tx_pool)
1237 struct ibmvnic_tx_buff *tx_buff;
1241 if (!tx_pool || !tx_pool->tx_buff)
1244 tx_entries = tx_pool->num_buffers;
1246 for (i = 0; i < tx_entries; i++) {
1247 tx_buff = &tx_pool->tx_buff[i];
1248 if (tx_buff && tx_buff->skb) {
1249 dev_kfree_skb_any(tx_buff->skb);
1250 tx_buff->skb = NULL;
1255 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1260 if (!adapter->tx_pool || !adapter->tso_pool)
1263 tx_scrqs = adapter->num_active_tx_pools;
1265 /* Free any remaining skbs in the tx buffer pools */
1266 for (i = 0; i < tx_scrqs; i++) {
1267 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1268 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1269 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1273 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1275 struct net_device *netdev = adapter->netdev;
1278 if (adapter->tx_scrq) {
1279 for (i = 0; i < adapter->req_tx_queues; i++)
1280 if (adapter->tx_scrq[i]->irq) {
1282 "Disabling tx_scrq[%d] irq\n", i);
1283 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1284 disable_irq(adapter->tx_scrq[i]->irq);
1288 if (adapter->rx_scrq) {
1289 for (i = 0; i < adapter->req_rx_queues; i++) {
1290 if (adapter->rx_scrq[i]->irq) {
1292 "Disabling rx_scrq[%d] irq\n", i);
1293 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1294 disable_irq(adapter->rx_scrq[i]->irq);
1300 static void ibmvnic_cleanup(struct net_device *netdev)
1302 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1304 /* ensure that transmissions are stopped if called by do_reset */
1305 if (test_bit(0, &adapter->resetting))
1306 netif_tx_disable(netdev);
1308 netif_tx_stop_all_queues(netdev);
1310 ibmvnic_napi_disable(adapter);
1311 ibmvnic_disable_irqs(adapter);
1313 clean_rx_pools(adapter);
1314 clean_tx_pools(adapter);
1317 static int __ibmvnic_close(struct net_device *netdev)
1319 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1322 adapter->state = VNIC_CLOSING;
1323 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1326 adapter->state = VNIC_CLOSED;
1330 static int ibmvnic_close(struct net_device *netdev)
1332 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1335 /* If device failover is pending, just set device state and return.
1336 * Device operation will be handled by reset routine.
1338 if (adapter->failover_pending) {
1339 adapter->state = VNIC_CLOSED;
1343 rc = __ibmvnic_close(netdev);
1344 ibmvnic_cleanup(netdev);
1350 * build_hdr_data - creates L2/L3/L4 header data buffer
1351 * @hdr_field - bitfield determining needed headers
1352 * @skb - socket buffer
1353 * @hdr_len - array of header lengths
1354 * @tot_len - total length of data
1356 * Reads hdr_field to determine which headers are needed by firmware.
1357 * Builds a buffer containing these headers. Saves individual header
1358 * lengths and total buffer length to be used to build descriptors.
1360 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1361 int *hdr_len, u8 *hdr_data)
1366 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1367 hdr_len[0] = sizeof(struct vlan_ethhdr);
1369 hdr_len[0] = sizeof(struct ethhdr);
1371 if (skb->protocol == htons(ETH_P_IP)) {
1372 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1373 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1374 hdr_len[2] = tcp_hdrlen(skb);
1375 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1376 hdr_len[2] = sizeof(struct udphdr);
1377 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1378 hdr_len[1] = sizeof(struct ipv6hdr);
1379 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1380 hdr_len[2] = tcp_hdrlen(skb);
1381 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1382 hdr_len[2] = sizeof(struct udphdr);
1383 } else if (skb->protocol == htons(ETH_P_ARP)) {
1384 hdr_len[1] = arp_hdr_len(skb->dev);
1388 memset(hdr_data, 0, 120);
1389 if ((hdr_field >> 6) & 1) {
1390 hdr = skb_mac_header(skb);
1391 memcpy(hdr_data, hdr, hdr_len[0]);
1395 if ((hdr_field >> 5) & 1) {
1396 hdr = skb_network_header(skb);
1397 memcpy(hdr_data + len, hdr, hdr_len[1]);
1401 if ((hdr_field >> 4) & 1) {
1402 hdr = skb_transport_header(skb);
1403 memcpy(hdr_data + len, hdr, hdr_len[2]);
1410 * create_hdr_descs - create header and header extension descriptors
1411 * @hdr_field - bitfield determining needed headers
1412 * @data - buffer containing header data
1413 * @len - length of data buffer
1414 * @hdr_len - array of individual header lengths
1415 * @scrq_arr - descriptor array
1417 * Creates header and, if needed, header extension descriptors and
1418 * places them in a descriptor array, scrq_arr
1421 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1422 union sub_crq *scrq_arr)
1424 union sub_crq hdr_desc;
1430 while (tmp_len > 0) {
1431 cur = hdr_data + len - tmp_len;
1433 memset(&hdr_desc, 0, sizeof(hdr_desc));
1434 if (cur != hdr_data) {
1435 data = hdr_desc.hdr_ext.data;
1436 tmp = tmp_len > 29 ? 29 : tmp_len;
1437 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1438 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1439 hdr_desc.hdr_ext.len = tmp;
1441 data = hdr_desc.hdr.data;
1442 tmp = tmp_len > 24 ? 24 : tmp_len;
1443 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1444 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1445 hdr_desc.hdr.len = tmp;
1446 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1447 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1448 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1449 hdr_desc.hdr.flag = hdr_field << 1;
1451 memcpy(data, cur, tmp);
1453 *scrq_arr = hdr_desc;
1462 * build_hdr_descs_arr - build a header descriptor array
1463 * @skb - socket buffer
1464 * @num_entries - number of descriptors to be sent
1465 * @subcrq - first TX descriptor
1466 * @hdr_field - bit field determining which headers will be sent
1468 * This function will build a TX descriptor array with applicable
1469 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1472 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1473 int *num_entries, u8 hdr_field)
1475 int hdr_len[3] = {0, 0, 0};
1477 u8 *hdr_data = txbuff->hdr_data;
1479 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1481 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1482 txbuff->indir_arr + 1);
1485 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1486 struct net_device *netdev)
1488 /* For some backing devices, mishandling of small packets
1489 * can result in a loss of connection or TX stall. Device
1490 * architects recommend that no packet should be smaller
1491 * than the minimum MTU value provided to the driver, so
1492 * pad any packets to that length
1494 if (skb->len < netdev->min_mtu)
1495 return skb_put_padto(skb, netdev->min_mtu);
1500 static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1502 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1503 int queue_num = skb_get_queue_mapping(skb);
1504 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1505 struct device *dev = &adapter->vdev->dev;
1506 struct ibmvnic_tx_buff *tx_buff = NULL;
1507 struct ibmvnic_sub_crq_queue *tx_scrq;
1508 struct ibmvnic_tx_pool *tx_pool;
1509 unsigned int tx_send_failed = 0;
1510 unsigned int tx_map_failed = 0;
1511 unsigned int tx_dropped = 0;
1512 unsigned int tx_packets = 0;
1513 unsigned int tx_bytes = 0;
1514 dma_addr_t data_dma_addr;
1515 struct netdev_queue *txq;
1516 unsigned long lpar_rc;
1517 union sub_crq tx_crq;
1518 unsigned int offset;
1519 int num_entries = 1;
1524 netdev_tx_t ret = NETDEV_TX_OK;
1526 if (test_bit(0, &adapter->resetting)) {
1527 if (!netif_subqueue_stopped(netdev, skb))
1528 netif_stop_subqueue(netdev, queue_num);
1529 dev_kfree_skb_any(skb);
1537 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1543 if (skb_is_gso(skb))
1544 tx_pool = &adapter->tso_pool[queue_num];
1546 tx_pool = &adapter->tx_pool[queue_num];
1548 tx_scrq = adapter->tx_scrq[queue_num];
1549 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1550 handle = tx_scrq->handle;
1552 index = tx_pool->free_map[tx_pool->consumer_index];
1554 if (index == IBMVNIC_INVALID_MAP) {
1555 dev_kfree_skb_any(skb);
1562 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1564 offset = index * tx_pool->buf_size;
1565 dst = tx_pool->long_term_buff.buff + offset;
1566 memset(dst, 0, tx_pool->buf_size);
1567 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1569 if (skb_shinfo(skb)->nr_frags) {
1573 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1574 cur = skb_headlen(skb);
1576 /* Copy the frags */
1577 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1578 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1581 page_address(skb_frag_page(frag)) +
1582 skb_frag_off(frag), skb_frag_size(frag));
1583 cur += skb_frag_size(frag);
1586 skb_copy_from_linear_data(skb, dst, skb->len);
1589 tx_pool->consumer_index =
1590 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1592 tx_buff = &tx_pool->tx_buff[index];
1594 tx_buff->data_dma[0] = data_dma_addr;
1595 tx_buff->data_len[0] = skb->len;
1596 tx_buff->index = index;
1597 tx_buff->pool_index = queue_num;
1598 tx_buff->last_frag = true;
1600 memset(&tx_crq, 0, sizeof(tx_crq));
1601 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1602 tx_crq.v1.type = IBMVNIC_TX_DESC;
1603 tx_crq.v1.n_crq_elem = 1;
1604 tx_crq.v1.n_sge = 1;
1605 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1607 if (skb_is_gso(skb))
1608 tx_crq.v1.correlator =
1609 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1611 tx_crq.v1.correlator = cpu_to_be32(index);
1612 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1613 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1614 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1616 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
1617 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1618 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1621 if (skb->protocol == htons(ETH_P_IP)) {
1622 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1623 proto = ip_hdr(skb)->protocol;
1624 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1625 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1626 proto = ipv6_hdr(skb)->nexthdr;
1629 if (proto == IPPROTO_TCP)
1630 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1631 else if (proto == IPPROTO_UDP)
1632 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1634 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1635 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1638 if (skb_is_gso(skb)) {
1639 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1640 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1643 /* determine if l2/3/4 headers are sent to firmware */
1644 if ((*hdrs >> 7) & 1) {
1645 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1646 tx_crq.v1.n_crq_elem = num_entries;
1647 tx_buff->num_entries = num_entries;
1648 tx_buff->indir_arr[0] = tx_crq;
1649 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1650 sizeof(tx_buff->indir_arr),
1652 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1653 dev_kfree_skb_any(skb);
1654 tx_buff->skb = NULL;
1655 if (!firmware_has_feature(FW_FEATURE_CMO))
1656 dev_err(dev, "tx: unable to map descriptor array\n");
1662 lpar_rc = send_subcrq_indirect(adapter, handle,
1663 (u64)tx_buff->indir_dma,
1665 dma_unmap_single(dev, tx_buff->indir_dma,
1666 sizeof(tx_buff->indir_arr), DMA_TO_DEVICE);
1668 tx_buff->num_entries = num_entries;
1669 lpar_rc = send_subcrq(adapter, handle,
1672 if (lpar_rc != H_SUCCESS) {
1673 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1674 dev_err_ratelimited(dev, "tx: send failed\n");
1675 dev_kfree_skb_any(skb);
1676 tx_buff->skb = NULL;
1678 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1679 /* Disable TX and report carrier off if queue is closed
1680 * or pending failover.
1681 * Firmware guarantees that a signal will be sent to the
1682 * driver, triggering a reset or some other action.
1684 netif_tx_stop_all_queues(netdev);
1685 netif_carrier_off(netdev);
1694 if (atomic_add_return(num_entries, &tx_scrq->used)
1695 >= adapter->req_tx_entries_per_subcrq) {
1696 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1697 netif_stop_subqueue(netdev, queue_num);
1701 tx_bytes += skb->len;
1702 txq->trans_start = jiffies;
1707 /* roll back consumer index and map array*/
1708 if (tx_pool->consumer_index == 0)
1709 tx_pool->consumer_index =
1710 tx_pool->num_buffers - 1;
1712 tx_pool->consumer_index--;
1713 tx_pool->free_map[tx_pool->consumer_index] = index;
1715 netdev->stats.tx_dropped += tx_dropped;
1716 netdev->stats.tx_bytes += tx_bytes;
1717 netdev->stats.tx_packets += tx_packets;
1718 adapter->tx_send_failed += tx_send_failed;
1719 adapter->tx_map_failed += tx_map_failed;
1720 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1721 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1722 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1727 static void ibmvnic_set_multi(struct net_device *netdev)
1729 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1730 struct netdev_hw_addr *ha;
1731 union ibmvnic_crq crq;
1733 memset(&crq, 0, sizeof(crq));
1734 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1735 crq.request_capability.cmd = REQUEST_CAPABILITY;
1737 if (netdev->flags & IFF_PROMISC) {
1738 if (!adapter->promisc_supported)
1741 if (netdev->flags & IFF_ALLMULTI) {
1742 /* Accept all multicast */
1743 memset(&crq, 0, sizeof(crq));
1744 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1745 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1746 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1747 ibmvnic_send_crq(adapter, &crq);
1748 } else if (netdev_mc_empty(netdev)) {
1749 /* Reject all multicast */
1750 memset(&crq, 0, sizeof(crq));
1751 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1752 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1753 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1754 ibmvnic_send_crq(adapter, &crq);
1756 /* Accept one or more multicast(s) */
1757 netdev_for_each_mc_addr(ha, netdev) {
1758 memset(&crq, 0, sizeof(crq));
1759 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1760 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1761 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1762 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1764 ibmvnic_send_crq(adapter, &crq);
1770 static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
1772 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1773 union ibmvnic_crq crq;
1776 if (!is_valid_ether_addr(dev_addr)) {
1777 rc = -EADDRNOTAVAIL;
1781 memset(&crq, 0, sizeof(crq));
1782 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1783 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1784 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
1786 mutex_lock(&adapter->fw_lock);
1787 adapter->fw_done_rc = 0;
1788 reinit_completion(&adapter->fw_done);
1790 rc = ibmvnic_send_crq(adapter, &crq);
1793 mutex_unlock(&adapter->fw_lock);
1797 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1798 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1799 if (rc || adapter->fw_done_rc) {
1801 mutex_unlock(&adapter->fw_lock);
1804 mutex_unlock(&adapter->fw_lock);
1807 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1811 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1813 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1814 struct sockaddr *addr = p;
1818 ether_addr_copy(adapter->mac_addr, addr->sa_data);
1819 if (adapter->state != VNIC_PROBED)
1820 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
1826 * do_change_param_reset returns zero if we are able to keep processing reset
1827 * events, or non-zero if we hit a fatal error and must halt.
1829 static int do_change_param_reset(struct ibmvnic_adapter *adapter,
1830 struct ibmvnic_rwi *rwi,
1833 struct net_device *netdev = adapter->netdev;
1836 netdev_dbg(adapter->netdev, "Change param resetting driver (%d)\n",
1839 netif_carrier_off(netdev);
1840 adapter->reset_reason = rwi->reset_reason;
1842 ibmvnic_cleanup(netdev);
1844 if (reset_state == VNIC_OPEN) {
1845 rc = __ibmvnic_close(netdev);
1850 release_resources(adapter);
1851 release_sub_crqs(adapter, 1);
1852 release_crq_queue(adapter);
1854 adapter->state = VNIC_PROBED;
1856 rc = init_crq_queue(adapter);
1859 netdev_err(adapter->netdev,
1860 "Couldn't initialize crq. rc=%d\n", rc);
1864 rc = ibmvnic_reset_init(adapter, true);
1866 return IBMVNIC_INIT_FAILED;
1868 /* If the adapter was in PROBE state prior to the reset,
1871 if (reset_state == VNIC_PROBED)
1874 rc = ibmvnic_login(netdev);
1876 adapter->state = reset_state;
1880 rc = init_resources(adapter);
1884 ibmvnic_disable_irqs(adapter);
1886 adapter->state = VNIC_CLOSED;
1888 if (reset_state == VNIC_CLOSED)
1891 rc = __ibmvnic_open(netdev);
1893 return IBMVNIC_OPEN_FAILED;
1895 /* refresh device's multicast list */
1896 ibmvnic_set_multi(netdev);
1899 for (i = 0; i < adapter->req_rx_queues; i++)
1900 napi_schedule(&adapter->napi[i]);
1906 * do_reset returns zero if we are able to keep processing reset events, or
1907 * non-zero if we hit a fatal error and must halt.
1909 static int do_reset(struct ibmvnic_adapter *adapter,
1910 struct ibmvnic_rwi *rwi, u32 reset_state)
1912 u64 old_num_rx_queues, old_num_tx_queues;
1913 u64 old_num_rx_slots, old_num_tx_slots;
1914 struct net_device *netdev = adapter->netdev;
1917 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1922 netif_carrier_off(netdev);
1923 adapter->reset_reason = rwi->reset_reason;
1925 old_num_rx_queues = adapter->req_rx_queues;
1926 old_num_tx_queues = adapter->req_tx_queues;
1927 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1928 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
1930 ibmvnic_cleanup(netdev);
1932 if (reset_state == VNIC_OPEN &&
1933 adapter->reset_reason != VNIC_RESET_MOBILITY &&
1934 adapter->reset_reason != VNIC_RESET_FAILOVER) {
1935 adapter->state = VNIC_CLOSING;
1937 /* Release the RTNL lock before link state change and
1938 * re-acquire after the link state change to allow
1939 * linkwatch_event to grab the RTNL lock and run during
1943 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1948 if (adapter->state != VNIC_CLOSING) {
1953 adapter->state = VNIC_CLOSED;
1956 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1957 /* remove the closed state so when we call open it appears
1958 * we are coming from the probed state.
1960 adapter->state = VNIC_PROBED;
1962 if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1963 rc = ibmvnic_reenable_crq_queue(adapter);
1964 release_sub_crqs(adapter, 1);
1966 rc = ibmvnic_reset_crq(adapter);
1967 if (rc == H_CLOSED || rc == H_SUCCESS) {
1968 rc = vio_enable_interrupts(adapter->vdev);
1970 netdev_err(adapter->netdev,
1971 "Reset failed to enable interrupts. rc=%d\n",
1977 netdev_err(adapter->netdev,
1978 "Reset couldn't initialize crq. rc=%d\n", rc);
1982 rc = ibmvnic_reset_init(adapter, true);
1984 rc = IBMVNIC_INIT_FAILED;
1988 /* If the adapter was in PROBE state prior to the reset,
1991 if (reset_state == VNIC_PROBED) {
1996 rc = ibmvnic_login(netdev);
1998 adapter->state = reset_state;
2002 if (adapter->req_rx_queues != old_num_rx_queues ||
2003 adapter->req_tx_queues != old_num_tx_queues ||
2004 adapter->req_rx_add_entries_per_subcrq !=
2006 adapter->req_tx_entries_per_subcrq !=
2008 !adapter->rx_pool ||
2009 !adapter->tso_pool ||
2010 !adapter->tx_pool) {
2011 release_rx_pools(adapter);
2012 release_tx_pools(adapter);
2013 release_napi(adapter);
2014 release_vpd_data(adapter);
2016 rc = init_resources(adapter);
2021 rc = reset_tx_pools(adapter);
2023 netdev_dbg(adapter->netdev, "reset tx pools failed (%d)\n",
2028 rc = reset_rx_pools(adapter);
2030 netdev_dbg(adapter->netdev, "reset rx pools failed (%d)\n",
2035 ibmvnic_disable_irqs(adapter);
2037 adapter->state = VNIC_CLOSED;
2039 if (reset_state == VNIC_CLOSED) {
2044 rc = __ibmvnic_open(netdev);
2046 rc = IBMVNIC_OPEN_FAILED;
2050 /* refresh device's multicast list */
2051 ibmvnic_set_multi(netdev);
2054 for (i = 0; i < adapter->req_rx_queues; i++)
2055 napi_schedule(&adapter->napi[i]);
2057 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
2058 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
2068 static int do_hard_reset(struct ibmvnic_adapter *adapter,
2069 struct ibmvnic_rwi *rwi, u32 reset_state)
2071 struct net_device *netdev = adapter->netdev;
2074 netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
2077 netif_carrier_off(netdev);
2078 adapter->reset_reason = rwi->reset_reason;
2080 ibmvnic_cleanup(netdev);
2081 release_resources(adapter);
2082 release_sub_crqs(adapter, 0);
2083 release_crq_queue(adapter);
2085 /* remove the closed state so when we call open it appears
2086 * we are coming from the probed state.
2088 adapter->state = VNIC_PROBED;
2090 reinit_completion(&adapter->init_done);
2091 rc = init_crq_queue(adapter);
2093 netdev_err(adapter->netdev,
2094 "Couldn't initialize crq. rc=%d\n", rc);
2098 rc = ibmvnic_reset_init(adapter, false);
2102 /* If the adapter was in PROBE state prior to the reset,
2105 if (reset_state == VNIC_PROBED)
2108 rc = ibmvnic_login(netdev);
2110 adapter->state = VNIC_PROBED;
2114 rc = init_resources(adapter);
2118 ibmvnic_disable_irqs(adapter);
2119 adapter->state = VNIC_CLOSED;
2121 if (reset_state == VNIC_CLOSED)
2124 rc = __ibmvnic_open(netdev);
2126 return IBMVNIC_OPEN_FAILED;
2131 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2133 struct ibmvnic_rwi *rwi;
2134 unsigned long flags;
2136 spin_lock_irqsave(&adapter->rwi_lock, flags);
2138 if (!list_empty(&adapter->rwi_list)) {
2139 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2141 list_del(&rwi->list);
2146 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2150 static void free_all_rwi(struct ibmvnic_adapter *adapter)
2152 struct ibmvnic_rwi *rwi;
2154 rwi = get_next_rwi(adapter);
2157 rwi = get_next_rwi(adapter);
2161 static void __ibmvnic_reset(struct work_struct *work)
2163 struct ibmvnic_rwi *rwi;
2164 struct ibmvnic_adapter *adapter;
2165 bool saved_state = false;
2166 unsigned long flags;
2170 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
2172 if (test_and_set_bit_lock(0, &adapter->resetting)) {
2173 schedule_delayed_work(&adapter->ibmvnic_delayed_reset,
2174 IBMVNIC_RESET_DELAY);
2178 rwi = get_next_rwi(adapter);
2180 spin_lock_irqsave(&adapter->state_lock, flags);
2182 if (adapter->state == VNIC_REMOVING ||
2183 adapter->state == VNIC_REMOVED) {
2184 spin_unlock_irqrestore(&adapter->state_lock, flags);
2191 reset_state = adapter->state;
2192 adapter->state = VNIC_RESETTING;
2195 spin_unlock_irqrestore(&adapter->state_lock, flags);
2197 if (rwi->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2198 /* CHANGE_PARAM requestor holds rtnl_lock */
2199 rc = do_change_param_reset(adapter, rwi, reset_state);
2200 } else if (adapter->force_reset_recovery) {
2201 /* Transport event occurred during previous reset */
2202 if (adapter->wait_for_reset) {
2203 /* Previous was CHANGE_PARAM; caller locked */
2204 adapter->force_reset_recovery = false;
2205 rc = do_hard_reset(adapter, rwi, reset_state);
2208 adapter->force_reset_recovery = false;
2209 rc = do_hard_reset(adapter, rwi, reset_state);
2212 } else if (!(rwi->reset_reason == VNIC_RESET_FATAL &&
2213 adapter->from_passive_init)) {
2214 rc = do_reset(adapter, rwi, reset_state);
2217 if (rc == IBMVNIC_OPEN_FAILED) {
2218 if (list_empty(&adapter->rwi_list))
2219 adapter->state = VNIC_CLOSED;
2221 adapter->state = reset_state;
2223 } else if (rc && rc != IBMVNIC_INIT_FAILED &&
2224 !adapter->force_reset_recovery)
2227 rwi = get_next_rwi(adapter);
2229 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2230 rwi->reset_reason == VNIC_RESET_MOBILITY))
2231 adapter->force_reset_recovery = true;
2234 if (adapter->wait_for_reset) {
2235 adapter->reset_done_rc = rc;
2236 complete(&adapter->reset_done);
2240 netdev_dbg(adapter->netdev, "Reset failed\n");
2241 free_all_rwi(adapter);
2244 clear_bit_unlock(0, &adapter->resetting);
2247 static void __ibmvnic_delayed_reset(struct work_struct *work)
2249 struct ibmvnic_adapter *adapter;
2251 adapter = container_of(work, struct ibmvnic_adapter,
2252 ibmvnic_delayed_reset.work);
2253 __ibmvnic_reset(&adapter->ibmvnic_reset);
2256 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2257 enum ibmvnic_reset_reason reason)
2259 struct list_head *entry, *tmp_entry;
2260 struct ibmvnic_rwi *rwi, *tmp;
2261 struct net_device *netdev = adapter->netdev;
2262 unsigned long flags;
2265 if (adapter->state == VNIC_REMOVING ||
2266 adapter->state == VNIC_REMOVED ||
2267 adapter->failover_pending) {
2269 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
2273 if (adapter->state == VNIC_PROBING) {
2274 netdev_warn(netdev, "Adapter reset during probe\n");
2275 ret = adapter->init_done_rc = EAGAIN;
2279 spin_lock_irqsave(&adapter->rwi_lock, flags);
2281 list_for_each(entry, &adapter->rwi_list) {
2282 tmp = list_entry(entry, struct ibmvnic_rwi, list);
2283 if (tmp->reset_reason == reason) {
2284 netdev_dbg(netdev, "Skipping matching reset\n");
2285 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2291 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
2293 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2294 ibmvnic_close(netdev);
2298 /* if we just received a transport event,
2299 * flush reset queue and process this reset
2301 if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2302 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2305 rwi->reset_reason = reason;
2306 list_add_tail(&rwi->list, &adapter->rwi_list);
2307 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2308 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
2309 schedule_work(&adapter->ibmvnic_reset);
2316 static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
2318 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2320 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2323 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2324 struct ibmvnic_rx_buff *rx_buff)
2326 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2328 rx_buff->skb = NULL;
2330 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2331 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2333 atomic_dec(&pool->available);
2336 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2338 struct net_device *netdev = napi->dev;
2339 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2340 int scrq_num = (int)(napi - adapter->napi);
2341 int frames_processed = 0;
2344 while (frames_processed < budget) {
2345 struct sk_buff *skb;
2346 struct ibmvnic_rx_buff *rx_buff;
2347 union sub_crq *next;
2352 if (unlikely(test_bit(0, &adapter->resetting) &&
2353 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2354 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2355 napi_complete_done(napi, frames_processed);
2356 return frames_processed;
2359 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2361 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2363 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2364 rx_comp.correlator);
2365 /* do error checking */
2366 if (next->rx_comp.rc) {
2367 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2368 be16_to_cpu(next->rx_comp.rc));
2369 /* free the entry */
2370 next->rx_comp.first = 0;
2371 dev_kfree_skb_any(rx_buff->skb);
2372 remove_buff_from_pool(adapter, rx_buff);
2374 } else if (!rx_buff->skb) {
2375 /* free the entry */
2376 next->rx_comp.first = 0;
2377 remove_buff_from_pool(adapter, rx_buff);
2381 length = be32_to_cpu(next->rx_comp.len);
2382 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2383 flags = next->rx_comp.flags;
2385 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2388 /* VLAN Header has been stripped by the system firmware and
2389 * needs to be inserted by the driver
2391 if (adapter->rx_vlan_header_insertion &&
2392 (flags & IBMVNIC_VLAN_STRIPPED))
2393 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2394 ntohs(next->rx_comp.vlan_tci));
2396 /* free the entry */
2397 next->rx_comp.first = 0;
2398 remove_buff_from_pool(adapter, rx_buff);
2400 skb_put(skb, length);
2401 skb->protocol = eth_type_trans(skb, netdev);
2402 skb_record_rx_queue(skb, scrq_num);
2404 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2405 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2406 skb->ip_summed = CHECKSUM_UNNECESSARY;
2410 napi_gro_receive(napi, skb); /* send it up */
2411 netdev->stats.rx_packets++;
2412 netdev->stats.rx_bytes += length;
2413 adapter->rx_stats_buffers[scrq_num].packets++;
2414 adapter->rx_stats_buffers[scrq_num].bytes += length;
2418 if (adapter->state != VNIC_CLOSING)
2419 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2421 if (frames_processed < budget) {
2422 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2423 napi_complete_done(napi, frames_processed);
2424 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2425 napi_reschedule(napi)) {
2426 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2430 return frames_processed;
2433 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2437 adapter->fallback.mtu = adapter->req_mtu;
2438 adapter->fallback.rx_queues = adapter->req_rx_queues;
2439 adapter->fallback.tx_queues = adapter->req_tx_queues;
2440 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2441 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2443 reinit_completion(&adapter->reset_done);
2444 adapter->wait_for_reset = true;
2445 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2451 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2458 if (adapter->reset_done_rc) {
2460 adapter->desired.mtu = adapter->fallback.mtu;
2461 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2462 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2463 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2464 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2466 reinit_completion(&adapter->reset_done);
2467 adapter->wait_for_reset = true;
2468 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2473 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2481 adapter->wait_for_reset = false;
2486 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2488 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2490 adapter->desired.mtu = new_mtu + ETH_HLEN;
2492 return wait_for_reset(adapter);
2495 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2496 struct net_device *dev,
2497 netdev_features_t features)
2499 /* Some backing hardware adapters can not
2500 * handle packets with a MSS less than 224
2501 * or with only one segment.
2503 if (skb_is_gso(skb)) {
2504 if (skb_shinfo(skb)->gso_size < 224 ||
2505 skb_shinfo(skb)->gso_segs == 1)
2506 features &= ~NETIF_F_GSO_MASK;
2512 static const struct net_device_ops ibmvnic_netdev_ops = {
2513 .ndo_open = ibmvnic_open,
2514 .ndo_stop = ibmvnic_close,
2515 .ndo_start_xmit = ibmvnic_xmit,
2516 .ndo_set_rx_mode = ibmvnic_set_multi,
2517 .ndo_set_mac_address = ibmvnic_set_mac,
2518 .ndo_validate_addr = eth_validate_addr,
2519 .ndo_tx_timeout = ibmvnic_tx_timeout,
2520 .ndo_change_mtu = ibmvnic_change_mtu,
2521 .ndo_features_check = ibmvnic_features_check,
2524 /* ethtool functions */
2526 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2527 struct ethtool_link_ksettings *cmd)
2529 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2532 rc = send_query_phys_parms(adapter);
2534 adapter->speed = SPEED_UNKNOWN;
2535 adapter->duplex = DUPLEX_UNKNOWN;
2537 cmd->base.speed = adapter->speed;
2538 cmd->base.duplex = adapter->duplex;
2539 cmd->base.port = PORT_FIBRE;
2540 cmd->base.phy_address = 0;
2541 cmd->base.autoneg = AUTONEG_ENABLE;
2546 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2547 struct ethtool_drvinfo *info)
2549 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2551 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2552 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2553 strlcpy(info->fw_version, adapter->fw_version,
2554 sizeof(info->fw_version));
2557 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2559 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2561 return adapter->msg_enable;
2564 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2566 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2568 adapter->msg_enable = data;
2571 static u32 ibmvnic_get_link(struct net_device *netdev)
2573 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2575 /* Don't need to send a query because we request a logical link up at
2576 * init and then we wait for link state indications
2578 return adapter->logical_link_state;
2581 static void ibmvnic_get_ringparam(struct net_device *netdev,
2582 struct ethtool_ringparam *ring)
2584 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2586 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2587 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2588 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2590 ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2591 ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2593 ring->rx_mini_max_pending = 0;
2594 ring->rx_jumbo_max_pending = 0;
2595 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2596 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2597 ring->rx_mini_pending = 0;
2598 ring->rx_jumbo_pending = 0;
2601 static int ibmvnic_set_ringparam(struct net_device *netdev,
2602 struct ethtool_ringparam *ring)
2604 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2608 adapter->desired.rx_entries = ring->rx_pending;
2609 adapter->desired.tx_entries = ring->tx_pending;
2611 ret = wait_for_reset(adapter);
2614 (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2615 adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2617 "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2618 ring->rx_pending, ring->tx_pending,
2619 adapter->req_rx_add_entries_per_subcrq,
2620 adapter->req_tx_entries_per_subcrq);
2624 static void ibmvnic_get_channels(struct net_device *netdev,
2625 struct ethtool_channels *channels)
2627 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2629 if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2630 channels->max_rx = adapter->max_rx_queues;
2631 channels->max_tx = adapter->max_tx_queues;
2633 channels->max_rx = IBMVNIC_MAX_QUEUES;
2634 channels->max_tx = IBMVNIC_MAX_QUEUES;
2637 channels->max_other = 0;
2638 channels->max_combined = 0;
2639 channels->rx_count = adapter->req_rx_queues;
2640 channels->tx_count = adapter->req_tx_queues;
2641 channels->other_count = 0;
2642 channels->combined_count = 0;
2645 static int ibmvnic_set_channels(struct net_device *netdev,
2646 struct ethtool_channels *channels)
2648 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2652 adapter->desired.rx_queues = channels->rx_count;
2653 adapter->desired.tx_queues = channels->tx_count;
2655 ret = wait_for_reset(adapter);
2658 (adapter->req_rx_queues != channels->rx_count ||
2659 adapter->req_tx_queues != channels->tx_count))
2661 "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2662 channels->rx_count, channels->tx_count,
2663 adapter->req_rx_queues, adapter->req_tx_queues);
2668 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2670 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2673 switch (stringset) {
2675 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2676 i++, data += ETH_GSTRING_LEN)
2677 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2679 for (i = 0; i < adapter->req_tx_queues; i++) {
2680 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2681 data += ETH_GSTRING_LEN;
2683 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2684 data += ETH_GSTRING_LEN;
2686 snprintf(data, ETH_GSTRING_LEN,
2687 "tx%d_dropped_packets", i);
2688 data += ETH_GSTRING_LEN;
2691 for (i = 0; i < adapter->req_rx_queues; i++) {
2692 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2693 data += ETH_GSTRING_LEN;
2695 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2696 data += ETH_GSTRING_LEN;
2698 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2699 data += ETH_GSTRING_LEN;
2703 case ETH_SS_PRIV_FLAGS:
2704 for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2705 strcpy(data + i * ETH_GSTRING_LEN,
2706 ibmvnic_priv_flags[i]);
2713 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2715 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2719 return ARRAY_SIZE(ibmvnic_stats) +
2720 adapter->req_tx_queues * NUM_TX_STATS +
2721 adapter->req_rx_queues * NUM_RX_STATS;
2722 case ETH_SS_PRIV_FLAGS:
2723 return ARRAY_SIZE(ibmvnic_priv_flags);
2729 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2730 struct ethtool_stats *stats, u64 *data)
2732 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2733 union ibmvnic_crq crq;
2737 memset(&crq, 0, sizeof(crq));
2738 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2739 crq.request_statistics.cmd = REQUEST_STATISTICS;
2740 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2741 crq.request_statistics.len =
2742 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2744 /* Wait for data to be written */
2745 reinit_completion(&adapter->stats_done);
2746 rc = ibmvnic_send_crq(adapter, &crq);
2749 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
2753 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2754 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2755 ibmvnic_stats[i].offset));
2757 for (j = 0; j < adapter->req_tx_queues; j++) {
2758 data[i] = adapter->tx_stats_buffers[j].packets;
2760 data[i] = adapter->tx_stats_buffers[j].bytes;
2762 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2766 for (j = 0; j < adapter->req_rx_queues; j++) {
2767 data[i] = adapter->rx_stats_buffers[j].packets;
2769 data[i] = adapter->rx_stats_buffers[j].bytes;
2771 data[i] = adapter->rx_stats_buffers[j].interrupts;
2776 static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2778 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2780 return adapter->priv_flags;
2783 static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2785 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2786 bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2789 adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2791 adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2795 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2796 .get_drvinfo = ibmvnic_get_drvinfo,
2797 .get_msglevel = ibmvnic_get_msglevel,
2798 .set_msglevel = ibmvnic_set_msglevel,
2799 .get_link = ibmvnic_get_link,
2800 .get_ringparam = ibmvnic_get_ringparam,
2801 .set_ringparam = ibmvnic_set_ringparam,
2802 .get_channels = ibmvnic_get_channels,
2803 .set_channels = ibmvnic_set_channels,
2804 .get_strings = ibmvnic_get_strings,
2805 .get_sset_count = ibmvnic_get_sset_count,
2806 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2807 .get_link_ksettings = ibmvnic_get_link_ksettings,
2808 .get_priv_flags = ibmvnic_get_priv_flags,
2809 .set_priv_flags = ibmvnic_set_priv_flags,
2812 /* Routines for managing CRQs/sCRQs */
2814 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2815 struct ibmvnic_sub_crq_queue *scrq)
2820 free_irq(scrq->irq, scrq);
2821 irq_dispose_mapping(scrq->irq);
2825 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2826 atomic_set(&scrq->used, 0);
2829 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2830 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2834 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2838 for (i = 0; i < adapter->req_tx_queues; i++) {
2839 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2840 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2845 for (i = 0; i < adapter->req_rx_queues; i++) {
2846 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2847 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2855 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2856 struct ibmvnic_sub_crq_queue *scrq,
2859 struct device *dev = &adapter->vdev->dev;
2862 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2865 /* Close the sub-crqs */
2867 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2868 adapter->vdev->unit_address,
2870 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2873 netdev_err(adapter->netdev,
2874 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2879 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2881 free_pages((unsigned long)scrq->msgs, 2);
2885 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2888 struct device *dev = &adapter->vdev->dev;
2889 struct ibmvnic_sub_crq_queue *scrq;
2892 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2897 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2899 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2900 goto zero_page_failed;
2903 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2905 if (dma_mapping_error(dev, scrq->msg_token)) {
2906 dev_warn(dev, "Couldn't map crq queue messages page\n");
2910 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2911 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2913 if (rc == H_RESOURCE)
2914 rc = ibmvnic_reset_crq(adapter);
2916 if (rc == H_CLOSED) {
2917 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2919 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2923 scrq->adapter = adapter;
2924 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2925 spin_lock_init(&scrq->lock);
2927 netdev_dbg(adapter->netdev,
2928 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2929 scrq->crq_num, scrq->hw_irq, scrq->irq);
2934 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2937 free_pages((unsigned long)scrq->msgs, 2);
2944 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2948 if (adapter->tx_scrq) {
2949 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2950 if (!adapter->tx_scrq[i])
2953 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2955 if (adapter->tx_scrq[i]->irq) {
2956 free_irq(adapter->tx_scrq[i]->irq,
2957 adapter->tx_scrq[i]);
2958 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2959 adapter->tx_scrq[i]->irq = 0;
2962 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2966 kfree(adapter->tx_scrq);
2967 adapter->tx_scrq = NULL;
2968 adapter->num_active_tx_scrqs = 0;
2971 if (adapter->rx_scrq) {
2972 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2973 if (!adapter->rx_scrq[i])
2976 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2978 if (adapter->rx_scrq[i]->irq) {
2979 free_irq(adapter->rx_scrq[i]->irq,
2980 adapter->rx_scrq[i]);
2981 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2982 adapter->rx_scrq[i]->irq = 0;
2985 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2989 kfree(adapter->rx_scrq);
2990 adapter->rx_scrq = NULL;
2991 adapter->num_active_rx_scrqs = 0;
2995 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2996 struct ibmvnic_sub_crq_queue *scrq)
2998 struct device *dev = &adapter->vdev->dev;
3001 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3002 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3004 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3009 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3010 struct ibmvnic_sub_crq_queue *scrq)
3012 struct device *dev = &adapter->vdev->dev;
3015 if (scrq->hw_irq > 0x100000000ULL) {
3016 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3020 if (test_bit(0, &adapter->resetting) &&
3021 adapter->reset_reason == VNIC_RESET_MOBILITY) {
3022 u64 val = (0xff000000) | scrq->hw_irq;
3024 rc = plpar_hcall_norets(H_EOI, val);
3025 /* H_EOI would fail with rc = H_FUNCTION when running
3026 * in XIVE mode which is expected, but not an error.
3028 if (rc && (rc != H_FUNCTION))
3029 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3033 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3034 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3036 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3041 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3042 struct ibmvnic_sub_crq_queue *scrq)
3044 struct device *dev = &adapter->vdev->dev;
3045 struct ibmvnic_tx_pool *tx_pool;
3046 struct ibmvnic_tx_buff *txbuff;
3047 union sub_crq *next;
3052 while (pending_scrq(adapter, scrq)) {
3053 unsigned int pool = scrq->pool_index;
3054 int num_entries = 0;
3056 next = ibmvnic_next_scrq(adapter, scrq);
3057 for (i = 0; i < next->tx_comp.num_comps; i++) {
3058 if (next->tx_comp.rcs[i]) {
3059 dev_err(dev, "tx error %x\n",
3060 next->tx_comp.rcs[i]);
3063 index = be32_to_cpu(next->tx_comp.correlators[i]);
3064 if (index & IBMVNIC_TSO_POOL_MASK) {
3065 tx_pool = &adapter->tso_pool[pool];
3066 index &= ~IBMVNIC_TSO_POOL_MASK;
3068 tx_pool = &adapter->tx_pool[pool];
3071 txbuff = &tx_pool->tx_buff[index];
3073 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
3074 if (!txbuff->data_dma[j])
3077 txbuff->data_dma[j] = 0;
3080 if (txbuff->last_frag) {
3081 dev_kfree_skb_any(txbuff->skb);
3085 num_entries += txbuff->num_entries;
3087 tx_pool->free_map[tx_pool->producer_index] = index;
3088 tx_pool->producer_index =
3089 (tx_pool->producer_index + 1) %
3090 tx_pool->num_buffers;
3092 /* remove tx_comp scrq*/
3093 next->tx_comp.first = 0;
3095 if (atomic_sub_return(num_entries, &scrq->used) <=
3096 (adapter->req_tx_entries_per_subcrq / 2) &&
3097 __netif_subqueue_stopped(adapter->netdev,
3098 scrq->pool_index)) {
3099 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
3100 netdev_dbg(adapter->netdev, "Started queue %d\n",
3105 enable_scrq_irq(adapter, scrq);
3107 if (pending_scrq(adapter, scrq)) {
3108 disable_scrq_irq(adapter, scrq);
3115 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3117 struct ibmvnic_sub_crq_queue *scrq = instance;
3118 struct ibmvnic_adapter *adapter = scrq->adapter;
3120 disable_scrq_irq(adapter, scrq);
3121 ibmvnic_complete_tx(adapter, scrq);
3126 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3128 struct ibmvnic_sub_crq_queue *scrq = instance;
3129 struct ibmvnic_adapter *adapter = scrq->adapter;
3131 /* When booting a kdump kernel we can hit pending interrupts
3132 * prior to completing driver initialization.
3134 if (unlikely(adapter->state != VNIC_OPEN))
3137 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3139 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3140 disable_scrq_irq(adapter, scrq);
3141 __napi_schedule(&adapter->napi[scrq->scrq_num]);
3147 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3149 struct device *dev = &adapter->vdev->dev;
3150 struct ibmvnic_sub_crq_queue *scrq;
3154 for (i = 0; i < adapter->req_tx_queues; i++) {
3155 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3157 scrq = adapter->tx_scrq[i];
3158 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3162 dev_err(dev, "Error mapping irq\n");
3163 goto req_tx_irq_failed;
3166 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3167 adapter->vdev->unit_address, i);
3168 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
3169 0, scrq->name, scrq);
3172 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3174 irq_dispose_mapping(scrq->irq);
3175 goto req_tx_irq_failed;
3179 for (i = 0; i < adapter->req_rx_queues; i++) {
3180 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3182 scrq = adapter->rx_scrq[i];
3183 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3186 dev_err(dev, "Error mapping irq\n");
3187 goto req_rx_irq_failed;
3189 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3190 adapter->vdev->unit_address, i);
3191 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
3192 0, scrq->name, scrq);
3194 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3196 irq_dispose_mapping(scrq->irq);
3197 goto req_rx_irq_failed;
3203 for (j = 0; j < i; j++) {
3204 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3205 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
3207 i = adapter->req_tx_queues;
3209 for (j = 0; j < i; j++) {
3210 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
3211 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
3213 release_sub_crqs(adapter, 1);
3217 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
3219 struct device *dev = &adapter->vdev->dev;
3220 struct ibmvnic_sub_crq_queue **allqueues;
3221 int registered_queues = 0;
3226 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3228 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
3232 for (i = 0; i < total_queues; i++) {
3233 allqueues[i] = init_sub_crq_queue(adapter);
3234 if (!allqueues[i]) {
3235 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3238 registered_queues++;
3241 /* Make sure we were able to register the minimum number of queues */
3242 if (registered_queues <
3243 adapter->min_tx_queues + adapter->min_rx_queues) {
3244 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
3248 /* Distribute the failed allocated queues*/
3249 for (i = 0; i < total_queues - registered_queues + more ; i++) {
3250 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3253 if (adapter->req_rx_queues > adapter->min_rx_queues)
3254 adapter->req_rx_queues--;
3259 if (adapter->req_tx_queues > adapter->min_tx_queues)
3260 adapter->req_tx_queues--;
3267 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3268 sizeof(*adapter->tx_scrq), GFP_KERNEL);
3269 if (!adapter->tx_scrq)
3272 for (i = 0; i < adapter->req_tx_queues; i++) {
3273 adapter->tx_scrq[i] = allqueues[i];
3274 adapter->tx_scrq[i]->pool_index = i;
3275 adapter->num_active_tx_scrqs++;
3278 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3279 sizeof(*adapter->rx_scrq), GFP_KERNEL);
3280 if (!adapter->rx_scrq)
3283 for (i = 0; i < adapter->req_rx_queues; i++) {
3284 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3285 adapter->rx_scrq[i]->scrq_num = i;
3286 adapter->num_active_rx_scrqs++;
3293 kfree(adapter->tx_scrq);
3294 adapter->tx_scrq = NULL;
3296 for (i = 0; i < registered_queues; i++)
3297 release_sub_crq_queue(adapter, allqueues[i], 1);
3302 static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
3304 struct device *dev = &adapter->vdev->dev;
3305 union ibmvnic_crq crq;
3309 /* Sub-CRQ entries are 32 byte long */
3310 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3312 if (adapter->min_tx_entries_per_subcrq > entries_page ||
3313 adapter->min_rx_add_entries_per_subcrq > entries_page) {
3314 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3318 if (adapter->desired.mtu)
3319 adapter->req_mtu = adapter->desired.mtu;
3321 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
3323 if (!adapter->desired.tx_entries)
3324 adapter->desired.tx_entries =
3325 adapter->max_tx_entries_per_subcrq;
3326 if (!adapter->desired.rx_entries)
3327 adapter->desired.rx_entries =
3328 adapter->max_rx_add_entries_per_subcrq;
3330 max_entries = IBMVNIC_MAX_LTB_SIZE /
3331 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3333 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3334 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3335 adapter->desired.tx_entries = max_entries;
3338 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3339 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3340 adapter->desired.rx_entries = max_entries;
3343 if (adapter->desired.tx_entries)
3344 adapter->req_tx_entries_per_subcrq =
3345 adapter->desired.tx_entries;
3347 adapter->req_tx_entries_per_subcrq =
3348 adapter->max_tx_entries_per_subcrq;
3350 if (adapter->desired.rx_entries)
3351 adapter->req_rx_add_entries_per_subcrq =
3352 adapter->desired.rx_entries;
3354 adapter->req_rx_add_entries_per_subcrq =
3355 adapter->max_rx_add_entries_per_subcrq;
3357 if (adapter->desired.tx_queues)
3358 adapter->req_tx_queues =
3359 adapter->desired.tx_queues;
3361 adapter->req_tx_queues =
3362 adapter->opt_tx_comp_sub_queues;
3364 if (adapter->desired.rx_queues)
3365 adapter->req_rx_queues =
3366 adapter->desired.rx_queues;
3368 adapter->req_rx_queues =
3369 adapter->opt_rx_comp_queues;
3371 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3374 memset(&crq, 0, sizeof(crq));
3375 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3376 crq.request_capability.cmd = REQUEST_CAPABILITY;
3378 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3379 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3380 atomic_inc(&adapter->running_cap_crqs);
3381 ibmvnic_send_crq(adapter, &crq);
3383 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3384 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3385 atomic_inc(&adapter->running_cap_crqs);
3386 ibmvnic_send_crq(adapter, &crq);
3388 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3389 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3390 atomic_inc(&adapter->running_cap_crqs);
3391 ibmvnic_send_crq(adapter, &crq);
3393 crq.request_capability.capability =
3394 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3395 crq.request_capability.number =
3396 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3397 atomic_inc(&adapter->running_cap_crqs);
3398 ibmvnic_send_crq(adapter, &crq);
3400 crq.request_capability.capability =
3401 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3402 crq.request_capability.number =
3403 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3404 atomic_inc(&adapter->running_cap_crqs);
3405 ibmvnic_send_crq(adapter, &crq);
3407 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3408 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3409 atomic_inc(&adapter->running_cap_crqs);
3410 ibmvnic_send_crq(adapter, &crq);
3412 if (adapter->netdev->flags & IFF_PROMISC) {
3413 if (adapter->promisc_supported) {
3414 crq.request_capability.capability =
3415 cpu_to_be16(PROMISC_REQUESTED);
3416 crq.request_capability.number = cpu_to_be64(1);
3417 atomic_inc(&adapter->running_cap_crqs);
3418 ibmvnic_send_crq(adapter, &crq);
3421 crq.request_capability.capability =
3422 cpu_to_be16(PROMISC_REQUESTED);
3423 crq.request_capability.number = cpu_to_be64(0);
3424 atomic_inc(&adapter->running_cap_crqs);
3425 ibmvnic_send_crq(adapter, &crq);
3429 static int pending_scrq(struct ibmvnic_adapter *adapter,
3430 struct ibmvnic_sub_crq_queue *scrq)
3432 union sub_crq *entry = &scrq->msgs[scrq->cur];
3434 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3440 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3441 struct ibmvnic_sub_crq_queue *scrq)
3443 union sub_crq *entry;
3444 unsigned long flags;
3446 spin_lock_irqsave(&scrq->lock, flags);
3447 entry = &scrq->msgs[scrq->cur];
3448 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3449 if (++scrq->cur == scrq->size)
3454 spin_unlock_irqrestore(&scrq->lock, flags);
3459 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3461 struct ibmvnic_crq_queue *queue = &adapter->crq;
3462 union ibmvnic_crq *crq;
3464 crq = &queue->msgs[queue->cur];
3465 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3466 if (++queue->cur == queue->size)
3475 static void print_subcrq_error(struct device *dev, int rc, const char *func)
3479 dev_warn_ratelimited(dev,
3480 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3484 dev_warn_ratelimited(dev,
3485 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3489 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3494 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3495 union sub_crq *sub_crq)
3497 unsigned int ua = adapter->vdev->unit_address;
3498 struct device *dev = &adapter->vdev->dev;
3499 u64 *u64_crq = (u64 *)sub_crq;
3502 netdev_dbg(adapter->netdev,
3503 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3504 (unsigned long int)cpu_to_be64(remote_handle),
3505 (unsigned long int)cpu_to_be64(u64_crq[0]),
3506 (unsigned long int)cpu_to_be64(u64_crq[1]),
3507 (unsigned long int)cpu_to_be64(u64_crq[2]),
3508 (unsigned long int)cpu_to_be64(u64_crq[3]));
3510 /* Make sure the hypervisor sees the complete request */
3513 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3514 cpu_to_be64(remote_handle),
3515 cpu_to_be64(u64_crq[0]),
3516 cpu_to_be64(u64_crq[1]),
3517 cpu_to_be64(u64_crq[2]),
3518 cpu_to_be64(u64_crq[3]));
3521 print_subcrq_error(dev, rc, __func__);
3526 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3527 u64 remote_handle, u64 ioba, u64 num_entries)
3529 unsigned int ua = adapter->vdev->unit_address;
3530 struct device *dev = &adapter->vdev->dev;
3533 /* Make sure the hypervisor sees the complete request */
3535 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3536 cpu_to_be64(remote_handle),
3540 print_subcrq_error(dev, rc, __func__);
3545 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3546 union ibmvnic_crq *crq)
3548 unsigned int ua = adapter->vdev->unit_address;
3549 struct device *dev = &adapter->vdev->dev;
3550 u64 *u64_crq = (u64 *)crq;
3553 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3554 (unsigned long int)cpu_to_be64(u64_crq[0]),
3555 (unsigned long int)cpu_to_be64(u64_crq[1]));
3557 if (!adapter->crq.active &&
3558 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3559 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3563 /* Make sure the hypervisor sees the complete request */
3566 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3567 cpu_to_be64(u64_crq[0]),
3568 cpu_to_be64(u64_crq[1]));
3571 if (rc == H_CLOSED) {
3572 dev_warn(dev, "CRQ Queue closed\n");
3573 /* do not reset, report the fail, wait for passive init from server */
3576 dev_warn(dev, "Send error (rc=%d)\n", rc);
3582 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3584 struct device *dev = &adapter->vdev->dev;
3585 union ibmvnic_crq crq;
3589 memset(&crq, 0, sizeof(crq));
3590 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3591 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3592 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3595 rc = ibmvnic_send_crq(adapter, &crq);
3601 } while (retries > 0);
3604 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
3611 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3613 union ibmvnic_crq crq;
3615 memset(&crq, 0, sizeof(crq));
3616 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3617 crq.version_exchange.cmd = VERSION_EXCHANGE;
3618 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3620 return ibmvnic_send_crq(adapter, &crq);
3623 struct vnic_login_client_data {
3629 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3633 /* Calculate the amount of buffer space needed for the
3634 * vnic client data in the login buffer. There are four entries,
3635 * OS name, LPAR name, device name, and a null last entry.
3637 len = 4 * sizeof(struct vnic_login_client_data);
3638 len += 6; /* "Linux" plus NULL */
3639 len += strlen(utsname()->nodename) + 1;
3640 len += strlen(adapter->netdev->name) + 1;
3645 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3646 struct vnic_login_client_data *vlcd)
3648 const char *os_name = "Linux";
3651 /* Type 1 - LPAR OS */
3653 len = strlen(os_name) + 1;
3654 vlcd->len = cpu_to_be16(len);
3655 strncpy(vlcd->name, os_name, len);
3656 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3658 /* Type 2 - LPAR name */
3660 len = strlen(utsname()->nodename) + 1;
3661 vlcd->len = cpu_to_be16(len);
3662 strncpy(vlcd->name, utsname()->nodename, len);
3663 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3665 /* Type 3 - device name */
3667 len = strlen(adapter->netdev->name) + 1;
3668 vlcd->len = cpu_to_be16(len);
3669 strncpy(vlcd->name, adapter->netdev->name, len);
3672 static int send_login(struct ibmvnic_adapter *adapter)
3674 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3675 struct ibmvnic_login_buffer *login_buffer;
3676 struct device *dev = &adapter->vdev->dev;
3677 dma_addr_t rsp_buffer_token;
3678 dma_addr_t buffer_token;
3679 size_t rsp_buffer_size;
3680 union ibmvnic_crq crq;
3684 int client_data_len;
3685 struct vnic_login_client_data *vlcd;
3688 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3689 netdev_err(adapter->netdev,
3690 "RX or TX queues are not allocated, device login failed\n");
3694 release_login_rsp_buffer(adapter);
3695 client_data_len = vnic_client_data_len(adapter);
3698 sizeof(struct ibmvnic_login_buffer) +
3699 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3702 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3704 goto buf_alloc_failed;
3706 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3708 if (dma_mapping_error(dev, buffer_token)) {
3709 dev_err(dev, "Couldn't map login buffer\n");
3710 goto buf_map_failed;
3713 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3714 sizeof(u64) * adapter->req_tx_queues +
3715 sizeof(u64) * adapter->req_rx_queues +
3716 sizeof(u64) * adapter->req_rx_queues +
3717 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3719 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3720 if (!login_rsp_buffer)
3721 goto buf_rsp_alloc_failed;
3723 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3724 rsp_buffer_size, DMA_FROM_DEVICE);
3725 if (dma_mapping_error(dev, rsp_buffer_token)) {
3726 dev_err(dev, "Couldn't map login rsp buffer\n");
3727 goto buf_rsp_map_failed;
3730 adapter->login_buf = login_buffer;
3731 adapter->login_buf_token = buffer_token;
3732 adapter->login_buf_sz = buffer_size;
3733 adapter->login_rsp_buf = login_rsp_buffer;
3734 adapter->login_rsp_buf_token = rsp_buffer_token;
3735 adapter->login_rsp_buf_sz = rsp_buffer_size;
3737 login_buffer->len = cpu_to_be32(buffer_size);
3738 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3739 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3740 login_buffer->off_txcomp_subcrqs =
3741 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3742 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3743 login_buffer->off_rxcomp_subcrqs =
3744 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3745 sizeof(u64) * adapter->req_tx_queues);
3746 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3747 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3749 tx_list_p = (__be64 *)((char *)login_buffer +
3750 sizeof(struct ibmvnic_login_buffer));
3751 rx_list_p = (__be64 *)((char *)login_buffer +
3752 sizeof(struct ibmvnic_login_buffer) +
3753 sizeof(u64) * adapter->req_tx_queues);
3755 for (i = 0; i < adapter->req_tx_queues; i++) {
3756 if (adapter->tx_scrq[i]) {
3757 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3762 for (i = 0; i < adapter->req_rx_queues; i++) {
3763 if (adapter->rx_scrq[i]) {
3764 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3769 /* Insert vNIC login client data */
3770 vlcd = (struct vnic_login_client_data *)
3771 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3772 login_buffer->client_data_offset =
3773 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3774 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3776 vnic_add_client_data(adapter, vlcd);
3778 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3779 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3780 netdev_dbg(adapter->netdev, "%016lx\n",
3781 ((unsigned long int *)(adapter->login_buf))[i]);
3784 memset(&crq, 0, sizeof(crq));
3785 crq.login.first = IBMVNIC_CRQ_CMD;
3786 crq.login.cmd = LOGIN;
3787 crq.login.ioba = cpu_to_be32(buffer_token);
3788 crq.login.len = cpu_to_be32(buffer_size);
3789 ibmvnic_send_crq(adapter, &crq);
3794 kfree(login_rsp_buffer);
3795 buf_rsp_alloc_failed:
3796 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3798 kfree(login_buffer);
3803 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3806 union ibmvnic_crq crq;
3808 memset(&crq, 0, sizeof(crq));
3809 crq.request_map.first = IBMVNIC_CRQ_CMD;
3810 crq.request_map.cmd = REQUEST_MAP;
3811 crq.request_map.map_id = map_id;
3812 crq.request_map.ioba = cpu_to_be32(addr);
3813 crq.request_map.len = cpu_to_be32(len);
3814 return ibmvnic_send_crq(adapter, &crq);
3817 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3819 union ibmvnic_crq crq;
3821 memset(&crq, 0, sizeof(crq));
3822 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3823 crq.request_unmap.cmd = REQUEST_UNMAP;
3824 crq.request_unmap.map_id = map_id;
3825 return ibmvnic_send_crq(adapter, &crq);
3828 static void send_query_map(struct ibmvnic_adapter *adapter)
3830 union ibmvnic_crq crq;
3832 memset(&crq, 0, sizeof(crq));
3833 crq.query_map.first = IBMVNIC_CRQ_CMD;
3834 crq.query_map.cmd = QUERY_MAP;
3835 ibmvnic_send_crq(adapter, &crq);
3838 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3839 static void send_query_cap(struct ibmvnic_adapter *adapter)
3841 union ibmvnic_crq crq;
3843 atomic_set(&adapter->running_cap_crqs, 0);
3844 memset(&crq, 0, sizeof(crq));
3845 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3846 crq.query_capability.cmd = QUERY_CAPABILITY;
3848 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3849 atomic_inc(&adapter->running_cap_crqs);
3850 ibmvnic_send_crq(adapter, &crq);
3852 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3853 atomic_inc(&adapter->running_cap_crqs);
3854 ibmvnic_send_crq(adapter, &crq);
3856 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3857 atomic_inc(&adapter->running_cap_crqs);
3858 ibmvnic_send_crq(adapter, &crq);
3860 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3861 atomic_inc(&adapter->running_cap_crqs);
3862 ibmvnic_send_crq(adapter, &crq);
3864 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3865 atomic_inc(&adapter->running_cap_crqs);
3866 ibmvnic_send_crq(adapter, &crq);
3868 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3869 atomic_inc(&adapter->running_cap_crqs);
3870 ibmvnic_send_crq(adapter, &crq);
3872 crq.query_capability.capability =
3873 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3874 atomic_inc(&adapter->running_cap_crqs);
3875 ibmvnic_send_crq(adapter, &crq);
3877 crq.query_capability.capability =
3878 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3879 atomic_inc(&adapter->running_cap_crqs);
3880 ibmvnic_send_crq(adapter, &crq);
3882 crq.query_capability.capability =
3883 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3884 atomic_inc(&adapter->running_cap_crqs);
3885 ibmvnic_send_crq(adapter, &crq);
3887 crq.query_capability.capability =
3888 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3889 atomic_inc(&adapter->running_cap_crqs);
3890 ibmvnic_send_crq(adapter, &crq);
3892 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3893 atomic_inc(&adapter->running_cap_crqs);
3894 ibmvnic_send_crq(adapter, &crq);
3896 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3897 atomic_inc(&adapter->running_cap_crqs);
3898 ibmvnic_send_crq(adapter, &crq);
3900 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3901 atomic_inc(&adapter->running_cap_crqs);
3902 ibmvnic_send_crq(adapter, &crq);
3904 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3905 atomic_inc(&adapter->running_cap_crqs);
3906 ibmvnic_send_crq(adapter, &crq);
3908 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3909 atomic_inc(&adapter->running_cap_crqs);
3910 ibmvnic_send_crq(adapter, &crq);
3912 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3913 atomic_inc(&adapter->running_cap_crqs);
3914 ibmvnic_send_crq(adapter, &crq);
3916 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3917 atomic_inc(&adapter->running_cap_crqs);
3918 ibmvnic_send_crq(adapter, &crq);
3920 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3921 atomic_inc(&adapter->running_cap_crqs);
3922 ibmvnic_send_crq(adapter, &crq);
3924 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3925 atomic_inc(&adapter->running_cap_crqs);
3926 ibmvnic_send_crq(adapter, &crq);
3928 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3929 atomic_inc(&adapter->running_cap_crqs);
3930 ibmvnic_send_crq(adapter, &crq);
3932 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3933 atomic_inc(&adapter->running_cap_crqs);
3934 ibmvnic_send_crq(adapter, &crq);
3936 crq.query_capability.capability =
3937 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3938 atomic_inc(&adapter->running_cap_crqs);
3939 ibmvnic_send_crq(adapter, &crq);
3941 crq.query_capability.capability =
3942 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3943 atomic_inc(&adapter->running_cap_crqs);
3944 ibmvnic_send_crq(adapter, &crq);
3946 crq.query_capability.capability =
3947 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3948 atomic_inc(&adapter->running_cap_crqs);
3949 ibmvnic_send_crq(adapter, &crq);
3951 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3952 atomic_inc(&adapter->running_cap_crqs);
3953 ibmvnic_send_crq(adapter, &crq);
3956 static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
3958 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3959 struct device *dev = &adapter->vdev->dev;
3960 union ibmvnic_crq crq;
3962 adapter->ip_offload_tok =
3964 &adapter->ip_offload_buf,
3968 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3969 if (!firmware_has_feature(FW_FEATURE_CMO))
3970 dev_err(dev, "Couldn't map offload buffer\n");
3974 memset(&crq, 0, sizeof(crq));
3975 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3976 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3977 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
3978 crq.query_ip_offload.ioba =
3979 cpu_to_be32(adapter->ip_offload_tok);
3981 ibmvnic_send_crq(adapter, &crq);
3984 static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
3986 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
3987 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3988 struct device *dev = &adapter->vdev->dev;
3989 netdev_features_t old_hw_features = 0;
3990 union ibmvnic_crq crq;
3992 adapter->ip_offload_ctrl_tok =
3995 sizeof(adapter->ip_offload_ctrl),
3998 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3999 dev_err(dev, "Couldn't map ip offload control buffer\n");
4003 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4004 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4005 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4006 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4007 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4008 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4009 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4010 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4011 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4012 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4014 /* large_rx disabled for now, additional features needed */
4015 ctrl_buf->large_rx_ipv4 = 0;
4016 ctrl_buf->large_rx_ipv6 = 0;
4018 if (adapter->state != VNIC_PROBING) {
4019 old_hw_features = adapter->netdev->hw_features;
4020 adapter->netdev->hw_features = 0;
4023 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4025 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4026 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4028 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4029 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4031 if ((adapter->netdev->features &
4032 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4033 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4035 if (buf->large_tx_ipv4)
4036 adapter->netdev->hw_features |= NETIF_F_TSO;
4037 if (buf->large_tx_ipv6)
4038 adapter->netdev->hw_features |= NETIF_F_TSO6;
4040 if (adapter->state == VNIC_PROBING) {
4041 adapter->netdev->features |= adapter->netdev->hw_features;
4042 } else if (old_hw_features != adapter->netdev->hw_features) {
4043 netdev_features_t tmp = 0;
4045 /* disable features no longer supported */
4046 adapter->netdev->features &= adapter->netdev->hw_features;
4047 /* turn on features now supported if previously enabled */
4048 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4049 adapter->netdev->hw_features;
4050 adapter->netdev->features |=
4051 tmp & adapter->netdev->wanted_features;
4054 memset(&crq, 0, sizeof(crq));
4055 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4056 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4057 crq.control_ip_offload.len =
4058 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4059 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4060 ibmvnic_send_crq(adapter, &crq);
4063 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4064 struct ibmvnic_adapter *adapter)
4066 struct device *dev = &adapter->vdev->dev;
4068 if (crq->get_vpd_size_rsp.rc.code) {
4069 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4070 crq->get_vpd_size_rsp.rc.code);
4071 complete(&adapter->fw_done);
4075 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4076 complete(&adapter->fw_done);
4079 static void handle_vpd_rsp(union ibmvnic_crq *crq,
4080 struct ibmvnic_adapter *adapter)
4082 struct device *dev = &adapter->vdev->dev;
4083 unsigned char *substr = NULL;
4084 u8 fw_level_len = 0;
4086 memset(adapter->fw_version, 0, 32);
4088 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4091 if (crq->get_vpd_rsp.rc.code) {
4092 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4093 crq->get_vpd_rsp.rc.code);
4097 /* get the position of the firmware version info
4098 * located after the ASCII 'RM' substring in the buffer
4100 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4102 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
4106 /* get length of firmware level ASCII substring */
4107 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4108 fw_level_len = *(substr + 2);
4110 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4114 /* copy firmware version string from vpd into adapter */
4115 if ((substr + 3 + fw_level_len) <
4116 (adapter->vpd->buff + adapter->vpd->len)) {
4117 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
4119 dev_info(dev, "FW substr extrapolated VPD buff\n");
4123 if (adapter->fw_version[0] == '\0')
4124 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
4125 complete(&adapter->fw_done);
4128 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4130 struct device *dev = &adapter->vdev->dev;
4131 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4134 dma_unmap_single(dev, adapter->ip_offload_tok,
4135 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4137 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4138 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4139 netdev_dbg(adapter->netdev, "%016lx\n",
4140 ((unsigned long int *)(buf))[i]);
4142 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4143 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4144 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4145 buf->tcp_ipv4_chksum);
4146 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4147 buf->tcp_ipv6_chksum);
4148 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4149 buf->udp_ipv4_chksum);
4150 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4151 buf->udp_ipv6_chksum);
4152 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4153 buf->large_tx_ipv4);
4154 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4155 buf->large_tx_ipv6);
4156 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4157 buf->large_rx_ipv4);
4158 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4159 buf->large_rx_ipv6);
4160 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4161 buf->max_ipv4_header_size);
4162 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4163 buf->max_ipv6_header_size);
4164 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4165 buf->max_tcp_header_size);
4166 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4167 buf->max_udp_header_size);
4168 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4169 buf->max_large_tx_size);
4170 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4171 buf->max_large_rx_size);
4172 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4173 buf->ipv6_extension_header);
4174 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4175 buf->tcp_pseudosum_req);
4176 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4177 buf->num_ipv6_ext_headers);
4178 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4179 buf->off_ipv6_ext_headers);
4181 send_control_ip_offload(adapter);
4184 static const char *ibmvnic_fw_err_cause(u16 cause)
4187 case ADAPTER_PROBLEM:
4188 return "adapter problem";
4190 return "bus problem";
4192 return "firmware problem";
4194 return "device driver problem";
4196 return "EEH recovery";
4198 return "firmware updated";
4200 return "low Memory";
4206 static void handle_error_indication(union ibmvnic_crq *crq,
4207 struct ibmvnic_adapter *adapter)
4209 struct device *dev = &adapter->vdev->dev;
4212 cause = be16_to_cpu(crq->error_indication.error_cause);
4214 dev_warn_ratelimited(dev,
4215 "Firmware reports %serror, cause: %s. Starting recovery...\n",
4216 crq->error_indication.flags
4217 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4218 ibmvnic_fw_err_cause(cause));
4220 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4221 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4223 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
4226 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4227 struct ibmvnic_adapter *adapter)
4229 struct net_device *netdev = adapter->netdev;
4230 struct device *dev = &adapter->vdev->dev;
4233 rc = crq->change_mac_addr_rsp.rc.code;
4235 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
4238 /* crq->change_mac_addr.mac_addr is the requested one
4239 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4241 ether_addr_copy(netdev->dev_addr,
4242 &crq->change_mac_addr_rsp.mac_addr[0]);
4243 ether_addr_copy(adapter->mac_addr,
4244 &crq->change_mac_addr_rsp.mac_addr[0]);
4246 complete(&adapter->fw_done);
4250 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4251 struct ibmvnic_adapter *adapter)
4253 struct device *dev = &adapter->vdev->dev;
4257 atomic_dec(&adapter->running_cap_crqs);
4258 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4260 req_value = &adapter->req_tx_queues;
4264 req_value = &adapter->req_rx_queues;
4267 case REQ_RX_ADD_QUEUES:
4268 req_value = &adapter->req_rx_add_queues;
4271 case REQ_TX_ENTRIES_PER_SUBCRQ:
4272 req_value = &adapter->req_tx_entries_per_subcrq;
4273 name = "tx_entries_per_subcrq";
4275 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4276 req_value = &adapter->req_rx_add_entries_per_subcrq;
4277 name = "rx_add_entries_per_subcrq";
4280 req_value = &adapter->req_mtu;
4283 case PROMISC_REQUESTED:
4284 req_value = &adapter->promisc;
4288 dev_err(dev, "Got invalid cap request rsp %d\n",
4289 crq->request_capability.capability);
4293 switch (crq->request_capability_rsp.rc.code) {
4296 case PARTIALSUCCESS:
4297 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4299 (long int)be64_to_cpu(crq->request_capability_rsp.
4302 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4304 pr_err("mtu of %llu is not supported. Reverting.\n",
4306 *req_value = adapter->fallback.mtu;
4309 be64_to_cpu(crq->request_capability_rsp.number);
4312 send_request_cap(adapter, 1);
4315 dev_err(dev, "Error %d in request cap rsp\n",
4316 crq->request_capability_rsp.rc.code);
4320 /* Done receiving requested capabilities, query IP offload support */
4321 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4322 adapter->wait_capability = false;
4323 send_query_ip_offload(adapter);
4327 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4328 struct ibmvnic_adapter *adapter)
4330 struct device *dev = &adapter->vdev->dev;
4331 struct net_device *netdev = adapter->netdev;
4332 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4333 struct ibmvnic_login_buffer *login = adapter->login_buf;
4334 u64 *tx_handle_array;
4335 u64 *rx_handle_array;
4341 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
4343 dma_unmap_single(dev, adapter->login_rsp_buf_token,
4344 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
4346 /* If the number of queues requested can't be allocated by the
4347 * server, the login response will return with code 1. We will need
4348 * to resend the login buffer with fewer queues requested.
4350 if (login_rsp_crq->generic.rc.code) {
4351 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4352 complete(&adapter->init_done);
4356 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4358 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4359 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4360 netdev_dbg(adapter->netdev, "%016lx\n",
4361 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4365 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4366 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4367 adapter->req_rx_add_queues !=
4368 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4369 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4370 ibmvnic_remove(adapter->vdev);
4373 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4374 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4375 /* variable buffer sizes are not supported, so just read the
4378 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
4380 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4381 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4383 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4384 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4385 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4386 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4388 for (i = 0; i < num_tx_pools; i++)
4389 adapter->tx_scrq[i]->handle = tx_handle_array[i];
4391 for (i = 0; i < num_rx_pools; i++)
4392 adapter->rx_scrq[i]->handle = rx_handle_array[i];
4394 adapter->num_active_tx_scrqs = num_tx_pools;
4395 adapter->num_active_rx_scrqs = num_rx_pools;
4396 release_login_rsp_buffer(adapter);
4397 release_login_buffer(adapter);
4398 complete(&adapter->init_done);
4403 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4404 struct ibmvnic_adapter *adapter)
4406 struct device *dev = &adapter->vdev->dev;
4409 rc = crq->request_unmap_rsp.rc.code;
4411 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4414 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4415 struct ibmvnic_adapter *adapter)
4417 struct net_device *netdev = adapter->netdev;
4418 struct device *dev = &adapter->vdev->dev;
4421 rc = crq->query_map_rsp.rc.code;
4423 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4426 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4427 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4428 crq->query_map_rsp.free_pages);
4431 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4432 struct ibmvnic_adapter *adapter)
4434 struct net_device *netdev = adapter->netdev;
4435 struct device *dev = &adapter->vdev->dev;
4438 atomic_dec(&adapter->running_cap_crqs);
4439 netdev_dbg(netdev, "Outstanding queries: %d\n",
4440 atomic_read(&adapter->running_cap_crqs));
4441 rc = crq->query_capability.rc.code;
4443 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4447 switch (be16_to_cpu(crq->query_capability.capability)) {
4449 adapter->min_tx_queues =
4450 be64_to_cpu(crq->query_capability.number);
4451 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4452 adapter->min_tx_queues);
4455 adapter->min_rx_queues =
4456 be64_to_cpu(crq->query_capability.number);
4457 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4458 adapter->min_rx_queues);
4460 case MIN_RX_ADD_QUEUES:
4461 adapter->min_rx_add_queues =
4462 be64_to_cpu(crq->query_capability.number);
4463 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4464 adapter->min_rx_add_queues);
4467 adapter->max_tx_queues =
4468 be64_to_cpu(crq->query_capability.number);
4469 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4470 adapter->max_tx_queues);
4473 adapter->max_rx_queues =
4474 be64_to_cpu(crq->query_capability.number);
4475 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4476 adapter->max_rx_queues);
4478 case MAX_RX_ADD_QUEUES:
4479 adapter->max_rx_add_queues =
4480 be64_to_cpu(crq->query_capability.number);
4481 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4482 adapter->max_rx_add_queues);
4484 case MIN_TX_ENTRIES_PER_SUBCRQ:
4485 adapter->min_tx_entries_per_subcrq =
4486 be64_to_cpu(crq->query_capability.number);
4487 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4488 adapter->min_tx_entries_per_subcrq);
4490 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4491 adapter->min_rx_add_entries_per_subcrq =
4492 be64_to_cpu(crq->query_capability.number);
4493 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4494 adapter->min_rx_add_entries_per_subcrq);
4496 case MAX_TX_ENTRIES_PER_SUBCRQ:
4497 adapter->max_tx_entries_per_subcrq =
4498 be64_to_cpu(crq->query_capability.number);
4499 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4500 adapter->max_tx_entries_per_subcrq);
4502 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4503 adapter->max_rx_add_entries_per_subcrq =
4504 be64_to_cpu(crq->query_capability.number);
4505 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4506 adapter->max_rx_add_entries_per_subcrq);
4508 case TCP_IP_OFFLOAD:
4509 adapter->tcp_ip_offload =
4510 be64_to_cpu(crq->query_capability.number);
4511 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4512 adapter->tcp_ip_offload);
4514 case PROMISC_SUPPORTED:
4515 adapter->promisc_supported =
4516 be64_to_cpu(crq->query_capability.number);
4517 netdev_dbg(netdev, "promisc_supported = %lld\n",
4518 adapter->promisc_supported);
4521 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4522 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4523 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4526 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4527 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4528 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4530 case MAX_MULTICAST_FILTERS:
4531 adapter->max_multicast_filters =
4532 be64_to_cpu(crq->query_capability.number);
4533 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4534 adapter->max_multicast_filters);
4536 case VLAN_HEADER_INSERTION:
4537 adapter->vlan_header_insertion =
4538 be64_to_cpu(crq->query_capability.number);
4539 if (adapter->vlan_header_insertion)
4540 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4541 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4542 adapter->vlan_header_insertion);
4544 case RX_VLAN_HEADER_INSERTION:
4545 adapter->rx_vlan_header_insertion =
4546 be64_to_cpu(crq->query_capability.number);
4547 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4548 adapter->rx_vlan_header_insertion);
4550 case MAX_TX_SG_ENTRIES:
4551 adapter->max_tx_sg_entries =
4552 be64_to_cpu(crq->query_capability.number);
4553 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4554 adapter->max_tx_sg_entries);
4556 case RX_SG_SUPPORTED:
4557 adapter->rx_sg_supported =
4558 be64_to_cpu(crq->query_capability.number);
4559 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4560 adapter->rx_sg_supported);
4562 case OPT_TX_COMP_SUB_QUEUES:
4563 adapter->opt_tx_comp_sub_queues =
4564 be64_to_cpu(crq->query_capability.number);
4565 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4566 adapter->opt_tx_comp_sub_queues);
4568 case OPT_RX_COMP_QUEUES:
4569 adapter->opt_rx_comp_queues =
4570 be64_to_cpu(crq->query_capability.number);
4571 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4572 adapter->opt_rx_comp_queues);
4574 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4575 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4576 be64_to_cpu(crq->query_capability.number);
4577 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4578 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4580 case OPT_TX_ENTRIES_PER_SUBCRQ:
4581 adapter->opt_tx_entries_per_subcrq =
4582 be64_to_cpu(crq->query_capability.number);
4583 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4584 adapter->opt_tx_entries_per_subcrq);
4586 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4587 adapter->opt_rxba_entries_per_subcrq =
4588 be64_to_cpu(crq->query_capability.number);
4589 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4590 adapter->opt_rxba_entries_per_subcrq);
4592 case TX_RX_DESC_REQ:
4593 adapter->tx_rx_desc_req = crq->query_capability.number;
4594 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4595 adapter->tx_rx_desc_req);
4599 netdev_err(netdev, "Got invalid cap rsp %d\n",
4600 crq->query_capability.capability);
4604 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4605 adapter->wait_capability = false;
4606 send_request_cap(adapter, 0);
4610 static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4612 union ibmvnic_crq crq;
4615 memset(&crq, 0, sizeof(crq));
4616 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4617 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
4619 mutex_lock(&adapter->fw_lock);
4620 adapter->fw_done_rc = 0;
4621 reinit_completion(&adapter->fw_done);
4623 rc = ibmvnic_send_crq(adapter, &crq);
4625 mutex_unlock(&adapter->fw_lock);
4629 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
4631 mutex_unlock(&adapter->fw_lock);
4635 mutex_unlock(&adapter->fw_lock);
4636 return adapter->fw_done_rc ? -EIO : 0;
4639 static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4640 struct ibmvnic_adapter *adapter)
4642 struct net_device *netdev = adapter->netdev;
4644 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
4646 rc = crq->query_phys_parms_rsp.rc.code;
4648 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4652 case IBMVNIC_10MBPS:
4653 adapter->speed = SPEED_10;
4655 case IBMVNIC_100MBPS:
4656 adapter->speed = SPEED_100;
4659 adapter->speed = SPEED_1000;
4661 case IBMVNIC_10GBPS:
4662 adapter->speed = SPEED_10000;
4664 case IBMVNIC_25GBPS:
4665 adapter->speed = SPEED_25000;
4667 case IBMVNIC_40GBPS:
4668 adapter->speed = SPEED_40000;
4670 case IBMVNIC_50GBPS:
4671 adapter->speed = SPEED_50000;
4673 case IBMVNIC_100GBPS:
4674 adapter->speed = SPEED_100000;
4676 case IBMVNIC_200GBPS:
4677 adapter->speed = SPEED_200000;
4680 if (netif_carrier_ok(netdev))
4681 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
4682 adapter->speed = SPEED_UNKNOWN;
4684 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4685 adapter->duplex = DUPLEX_FULL;
4686 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4687 adapter->duplex = DUPLEX_HALF;
4689 adapter->duplex = DUPLEX_UNKNOWN;
4694 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4695 struct ibmvnic_adapter *adapter)
4697 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4698 struct net_device *netdev = adapter->netdev;
4699 struct device *dev = &adapter->vdev->dev;
4700 u64 *u64_crq = (u64 *)crq;
4703 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4704 (unsigned long int)cpu_to_be64(u64_crq[0]),
4705 (unsigned long int)cpu_to_be64(u64_crq[1]));
4706 switch (gen_crq->first) {
4707 case IBMVNIC_CRQ_INIT_RSP:
4708 switch (gen_crq->cmd) {
4709 case IBMVNIC_CRQ_INIT:
4710 dev_info(dev, "Partner initialized\n");
4711 adapter->from_passive_init = true;
4712 adapter->failover_pending = false;
4713 if (!completion_done(&adapter->init_done)) {
4714 complete(&adapter->init_done);
4715 adapter->init_done_rc = -EIO;
4717 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4719 case IBMVNIC_CRQ_INIT_COMPLETE:
4720 dev_info(dev, "Partner initialization complete\n");
4721 adapter->crq.active = true;
4722 send_version_xchg(adapter);
4725 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4728 case IBMVNIC_CRQ_XPORT_EVENT:
4729 netif_carrier_off(netdev);
4730 adapter->crq.active = false;
4731 /* terminate any thread waiting for a response
4734 if (!completion_done(&adapter->fw_done)) {
4735 adapter->fw_done_rc = -EIO;
4736 complete(&adapter->fw_done);
4738 if (!completion_done(&adapter->stats_done))
4739 complete(&adapter->stats_done);
4740 if (test_bit(0, &adapter->resetting))
4741 adapter->force_reset_recovery = true;
4742 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4743 dev_info(dev, "Migrated, re-enabling adapter\n");
4744 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4745 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4746 dev_info(dev, "Backing device failover detected\n");
4747 adapter->failover_pending = true;
4749 /* The adapter lost the connection */
4750 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4752 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4755 case IBMVNIC_CRQ_CMD_RSP:
4758 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4763 switch (gen_crq->cmd) {
4764 case VERSION_EXCHANGE_RSP:
4765 rc = crq->version_exchange_rsp.rc.code;
4767 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4771 be16_to_cpu(crq->version_exchange_rsp.version);
4772 dev_info(dev, "Partner protocol version is %d\n",
4774 send_query_cap(adapter);
4776 case QUERY_CAPABILITY_RSP:
4777 handle_query_cap_rsp(crq, adapter);
4780 handle_query_map_rsp(crq, adapter);
4782 case REQUEST_MAP_RSP:
4783 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4784 complete(&adapter->fw_done);
4786 case REQUEST_UNMAP_RSP:
4787 handle_request_unmap_rsp(crq, adapter);
4789 case REQUEST_CAPABILITY_RSP:
4790 handle_request_cap_rsp(crq, adapter);
4793 netdev_dbg(netdev, "Got Login Response\n");
4794 handle_login_rsp(crq, adapter);
4796 case LOGICAL_LINK_STATE_RSP:
4798 "Got Logical Link State Response, state: %d rc: %d\n",
4799 crq->logical_link_state_rsp.link_state,
4800 crq->logical_link_state_rsp.rc.code);
4801 adapter->logical_link_state =
4802 crq->logical_link_state_rsp.link_state;
4803 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4804 complete(&adapter->init_done);
4806 case LINK_STATE_INDICATION:
4807 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4808 adapter->phys_link_state =
4809 crq->link_state_indication.phys_link_state;
4810 adapter->logical_link_state =
4811 crq->link_state_indication.logical_link_state;
4812 if (adapter->phys_link_state && adapter->logical_link_state)
4813 netif_carrier_on(netdev);
4815 netif_carrier_off(netdev);
4817 case CHANGE_MAC_ADDR_RSP:
4818 netdev_dbg(netdev, "Got MAC address change Response\n");
4819 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4821 case ERROR_INDICATION:
4822 netdev_dbg(netdev, "Got Error Indication\n");
4823 handle_error_indication(crq, adapter);
4825 case REQUEST_STATISTICS_RSP:
4826 netdev_dbg(netdev, "Got Statistics Response\n");
4827 complete(&adapter->stats_done);
4829 case QUERY_IP_OFFLOAD_RSP:
4830 netdev_dbg(netdev, "Got Query IP offload Response\n");
4831 handle_query_ip_offload_rsp(adapter);
4833 case MULTICAST_CTRL_RSP:
4834 netdev_dbg(netdev, "Got multicast control Response\n");
4836 case CONTROL_IP_OFFLOAD_RSP:
4837 netdev_dbg(netdev, "Got Control IP offload Response\n");
4838 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4839 sizeof(adapter->ip_offload_ctrl),
4841 complete(&adapter->init_done);
4843 case COLLECT_FW_TRACE_RSP:
4844 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4845 complete(&adapter->fw_done);
4847 case GET_VPD_SIZE_RSP:
4848 handle_vpd_size_rsp(crq, adapter);
4851 handle_vpd_rsp(crq, adapter);
4853 case QUERY_PHYS_PARMS_RSP:
4854 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4855 complete(&adapter->fw_done);
4858 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4863 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4865 struct ibmvnic_adapter *adapter = instance;
4867 tasklet_schedule(&adapter->tasklet);
4871 static void ibmvnic_tasklet(struct tasklet_struct *t)
4873 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
4874 struct ibmvnic_crq_queue *queue = &adapter->crq;
4875 union ibmvnic_crq *crq;
4876 unsigned long flags;
4879 spin_lock_irqsave(&queue->lock, flags);
4881 /* Pull all the valid messages off the CRQ */
4882 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4883 ibmvnic_handle_crq(crq, adapter);
4884 crq->generic.first = 0;
4887 /* remain in tasklet until all
4888 * capabilities responses are received
4890 if (!adapter->wait_capability)
4893 /* if capabilities CRQ's were sent in this tasklet, the following
4894 * tasklet must wait until all responses are received
4896 if (atomic_read(&adapter->running_cap_crqs) != 0)
4897 adapter->wait_capability = true;
4898 spin_unlock_irqrestore(&queue->lock, flags);
4901 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4903 struct vio_dev *vdev = adapter->vdev;
4907 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4908 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4911 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4916 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4918 struct ibmvnic_crq_queue *crq = &adapter->crq;
4919 struct device *dev = &adapter->vdev->dev;
4920 struct vio_dev *vdev = adapter->vdev;
4925 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4926 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4928 /* Clean out the queue */
4929 memset(crq->msgs, 0, PAGE_SIZE);
4931 crq->active = false;
4933 /* And re-open it again */
4934 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4935 crq->msg_token, PAGE_SIZE);
4938 /* Adapter is good, but other end is not ready */
4939 dev_warn(dev, "Partner adapter not ready\n");
4941 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4946 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4948 struct ibmvnic_crq_queue *crq = &adapter->crq;
4949 struct vio_dev *vdev = adapter->vdev;
4955 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4956 free_irq(vdev->irq, adapter);
4957 tasklet_kill(&adapter->tasklet);
4959 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4960 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4962 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4964 free_page((unsigned long)crq->msgs);
4966 crq->active = false;
4969 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4971 struct ibmvnic_crq_queue *crq = &adapter->crq;
4972 struct device *dev = &adapter->vdev->dev;
4973 struct vio_dev *vdev = adapter->vdev;
4974 int rc, retrc = -ENOMEM;
4979 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4980 /* Should we allocate more than one page? */
4985 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4986 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4988 if (dma_mapping_error(dev, crq->msg_token))
4991 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4992 crq->msg_token, PAGE_SIZE);
4994 if (rc == H_RESOURCE)
4995 /* maybe kexecing and resource is busy. try a reset */
4996 rc = ibmvnic_reset_crq(adapter);
4999 if (rc == H_CLOSED) {
5000 dev_warn(dev, "Partner adapter not ready\n");
5002 dev_warn(dev, "Error %d opening adapter\n", rc);
5003 goto reg_crq_failed;
5008 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
5010 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
5011 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5012 adapter->vdev->unit_address);
5013 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
5015 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5017 goto req_irq_failed;
5020 rc = vio_enable_interrupts(vdev);
5022 dev_err(dev, "Error %d enabling interrupts\n", rc);
5023 goto req_irq_failed;
5027 spin_lock_init(&crq->lock);
5032 tasklet_kill(&adapter->tasklet);
5034 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5035 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5037 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5039 free_page((unsigned long)crq->msgs);
5044 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
5046 struct device *dev = &adapter->vdev->dev;
5047 unsigned long timeout = msecs_to_jiffies(30000);
5048 u64 old_num_rx_queues, old_num_tx_queues;
5051 adapter->from_passive_init = false;
5054 old_num_rx_queues = adapter->req_rx_queues;
5055 old_num_tx_queues = adapter->req_tx_queues;
5056 reinit_completion(&adapter->init_done);
5059 adapter->init_done_rc = 0;
5060 rc = ibmvnic_send_crq_init(adapter);
5062 dev_err(dev, "Send crq init failed with error %d\n", rc);
5066 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5067 dev_err(dev, "Initialization sequence timed out\n");
5071 if (adapter->init_done_rc) {
5072 release_crq_queue(adapter);
5073 return adapter->init_done_rc;
5076 if (adapter->from_passive_init) {
5077 adapter->state = VNIC_OPEN;
5078 adapter->from_passive_init = false;
5083 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
5084 adapter->reset_reason != VNIC_RESET_MOBILITY) {
5085 if (adapter->req_rx_queues != old_num_rx_queues ||
5086 adapter->req_tx_queues != old_num_tx_queues) {
5087 release_sub_crqs(adapter, 0);
5088 rc = init_sub_crqs(adapter);
5090 rc = reset_sub_crq_queues(adapter);
5093 rc = init_sub_crqs(adapter);
5097 dev_err(dev, "Initialization of sub crqs failed\n");
5098 release_crq_queue(adapter);
5102 rc = init_sub_crq_irqs(adapter);
5104 dev_err(dev, "Failed to initialize sub crq irqs\n");
5105 release_crq_queue(adapter);
5111 static struct device_attribute dev_attr_failover;
5113 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5115 struct ibmvnic_adapter *adapter;
5116 struct net_device *netdev;
5117 unsigned char *mac_addr_p;
5120 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5123 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5124 VETH_MAC_ADDR, NULL);
5127 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5128 __FILE__, __LINE__);
5132 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
5133 IBMVNIC_MAX_QUEUES);
5137 adapter = netdev_priv(netdev);
5138 adapter->state = VNIC_PROBING;
5139 dev_set_drvdata(&dev->dev, netdev);
5140 adapter->vdev = dev;
5141 adapter->netdev = netdev;
5143 ether_addr_copy(adapter->mac_addr, mac_addr_p);
5144 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
5145 netdev->irq = dev->irq;
5146 netdev->netdev_ops = &ibmvnic_netdev_ops;
5147 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5148 SET_NETDEV_DEV(netdev, &dev->dev);
5150 spin_lock_init(&adapter->stats_lock);
5152 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
5153 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5154 __ibmvnic_delayed_reset);
5155 INIT_LIST_HEAD(&adapter->rwi_list);
5156 spin_lock_init(&adapter->rwi_lock);
5157 spin_lock_init(&adapter->state_lock);
5158 mutex_init(&adapter->fw_lock);
5159 init_completion(&adapter->init_done);
5160 init_completion(&adapter->fw_done);
5161 init_completion(&adapter->reset_done);
5162 init_completion(&adapter->stats_done);
5163 clear_bit(0, &adapter->resetting);
5166 rc = init_crq_queue(adapter);
5168 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5170 goto ibmvnic_init_fail;
5173 rc = ibmvnic_reset_init(adapter, false);
5174 if (rc && rc != EAGAIN)
5175 goto ibmvnic_init_fail;
5176 } while (rc == EAGAIN);
5178 rc = init_stats_buffers(adapter);
5180 goto ibmvnic_init_fail;
5182 rc = init_stats_token(adapter);
5184 goto ibmvnic_stats_fail;
5186 netdev->mtu = adapter->req_mtu - ETH_HLEN;
5187 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5188 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5190 rc = device_create_file(&dev->dev, &dev_attr_failover);
5192 goto ibmvnic_dev_file_err;
5194 netif_carrier_off(netdev);
5195 rc = register_netdev(netdev);
5197 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
5198 goto ibmvnic_register_fail;
5200 dev_info(&dev->dev, "ibmvnic registered\n");
5202 adapter->state = VNIC_PROBED;
5204 adapter->wait_for_reset = false;
5208 ibmvnic_register_fail:
5209 device_remove_file(&dev->dev, &dev_attr_failover);
5211 ibmvnic_dev_file_err:
5212 release_stats_token(adapter);
5215 release_stats_buffers(adapter);
5218 release_sub_crqs(adapter, 1);
5219 release_crq_queue(adapter);
5220 mutex_destroy(&adapter->fw_lock);
5221 free_netdev(netdev);
5226 static int ibmvnic_remove(struct vio_dev *dev)
5228 struct net_device *netdev = dev_get_drvdata(&dev->dev);
5229 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5230 unsigned long flags;
5232 spin_lock_irqsave(&adapter->state_lock, flags);
5233 if (adapter->state == VNIC_RESETTING) {
5234 spin_unlock_irqrestore(&adapter->state_lock, flags);
5238 adapter->state = VNIC_REMOVING;
5239 spin_unlock_irqrestore(&adapter->state_lock, flags);
5241 flush_work(&adapter->ibmvnic_reset);
5242 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5245 unregister_netdevice(netdev);
5247 release_resources(adapter);
5248 release_sub_crqs(adapter, 1);
5249 release_crq_queue(adapter);
5251 release_stats_token(adapter);
5252 release_stats_buffers(adapter);
5254 adapter->state = VNIC_REMOVED;
5257 mutex_destroy(&adapter->fw_lock);
5258 device_remove_file(&dev->dev, &dev_attr_failover);
5259 free_netdev(netdev);
5260 dev_set_drvdata(&dev->dev, NULL);
5265 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5266 const char *buf, size_t count)
5268 struct net_device *netdev = dev_get_drvdata(dev);
5269 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5270 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5271 __be64 session_token;
5274 if (!sysfs_streq(buf, "1"))
5277 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5278 H_GET_SESSION_TOKEN, 0, 0, 0);
5280 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5285 session_token = (__be64)retbuf[0];
5286 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5287 be64_to_cpu(session_token));
5288 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5289 H_SESSION_ERR_DETECTED, session_token, 0, 0);
5291 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
5299 static DEVICE_ATTR_WO(failover);
5301 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5303 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5304 struct ibmvnic_adapter *adapter;
5305 struct iommu_table *tbl;
5306 unsigned long ret = 0;
5309 tbl = get_iommu_table_base(&vdev->dev);
5311 /* netdev inits at probe time along with the structures we need below*/
5313 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5315 adapter = netdev_priv(netdev);
5317 ret += PAGE_SIZE; /* the crq message queue */
5318 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5320 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5321 ret += 4 * PAGE_SIZE; /* the scrq message queue */
5323 for (i = 0; i < adapter->num_active_rx_pools; i++)
5324 ret += adapter->rx_pool[i].size *
5325 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5330 static int ibmvnic_resume(struct device *dev)
5332 struct net_device *netdev = dev_get_drvdata(dev);
5333 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5335 if (adapter->state != VNIC_OPEN)
5338 tasklet_schedule(&adapter->tasklet);
5343 static const struct vio_device_id ibmvnic_device_table[] = {
5344 {"network", "IBM,vnic"},
5347 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5349 static const struct dev_pm_ops ibmvnic_pm_ops = {
5350 .resume = ibmvnic_resume
5353 static struct vio_driver ibmvnic_driver = {
5354 .id_table = ibmvnic_device_table,
5355 .probe = ibmvnic_probe,
5356 .remove = ibmvnic_remove,
5357 .get_desired_dma = ibmvnic_get_desired_dma,
5358 .name = ibmvnic_driver_name,
5359 .pm = &ibmvnic_pm_ops,
5362 /* module functions */
5363 static int __init ibmvnic_module_init(void)
5365 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5366 IBMVNIC_DRIVER_VERSION);
5368 return vio_register_driver(&ibmvnic_driver);
5371 static void __exit ibmvnic_module_exit(void)
5373 vio_unregister_driver(&ibmvnic_driver);
5376 module_init(ibmvnic_module_init);
5377 module_exit(ibmvnic_module_exit);