1 /**************************************************************************/
3 /* IBM System i and System p Virtual NIC Device Driver */
4 /* Copyright (C) 2014 IBM Corp. */
5 /* Santiago Leon (santi_leon@yahoo.com) */
6 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7 /* John Allen (jallen@linux.vnet.ibm.com) */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2 of the License, or */
12 /* (at your option) any later version. */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program. */
22 /* This module contains the implementation of a virtual ethernet device */
23 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24 /* option of the RS/6000 Platform Architecture to interface with virtual */
25 /* ethernet NICs that are presented to the partition by the hypervisor. */
27 /* Messages are passed between the VNIC driver and the VNIC server using */
28 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29 /* issue and receive commands that initiate communication with the server */
30 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31 /* are used by the driver to notify the server that a packet is */
32 /* ready for transmission or that a buffer has been added to receive a */
33 /* packet. Subsequently, sCRQs are used by the server to notify the */
34 /* driver that a packet transmission has been completed or that a packet */
35 /* has been received and placed in a waiting buffer. */
37 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38 /* which skbs are DMA mapped and immediately unmapped when the transmit */
39 /* or receive has been completed, the VNIC driver is required to use */
40 /* "long term mapping". This entails that large, continuous DMA mapped */
41 /* buffers are allocated on driver initialization and these buffers are */
42 /* then continuously reused to pass skbs to and from the VNIC server. */
44 /**************************************************************************/
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/types.h>
49 #include <linux/errno.h>
50 #include <linux/completion.h>
51 #include <linux/ioport.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/kernel.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
60 #include <linux/ethtool.h>
61 #include <linux/proc_fs.h>
62 #include <linux/if_arp.h>
65 #include <linux/ipv6.h>
66 #include <linux/irq.h>
67 #include <linux/kthread.h>
68 #include <linux/seq_file.h>
69 #include <linux/interrupt.h>
70 #include <net/net_namespace.h>
71 #include <asm/hvcall.h>
72 #include <linux/atomic.h>
74 #include <asm/iommu.h>
75 #include <linux/uaccess.h>
76 #include <asm/firmware.h>
77 #include <linux/workqueue.h>
78 #include <linux/if_vlan.h>
79 #include <linux/utsname.h>
83 static const char ibmvnic_driver_name[] = "ibmvnic";
84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
86 MODULE_AUTHOR("Santiago Leon");
87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92 static int ibmvnic_remove(struct vio_dev *);
93 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102 static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104 static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106 static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110 static int ibmvnic_poll(struct napi_struct *napi, int data);
111 static void send_map_query(struct ibmvnic_adapter *adapter);
112 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static void send_request_unmap(struct ibmvnic_adapter *, u8);
114 static int send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
122 struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
131 static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
173 struct device *dev = &adapter->vdev->dev;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
180 dev_err(dev, "Couldn't alloc long term buffer\n");
183 ltb->map_id = adapter->map_id;
186 init_completion(&adapter->fw_done);
187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
189 wait_for_completion(&adapter->fw_done);
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
202 struct device *dev = &adapter->vdev->dev;
207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
209 send_request_unmap(adapter, ltb->map_id);
210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
216 memset(ltb->buff, 0, ltb->size);
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
237 adapter->rx_pool[i].active = 0;
240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
272 index = pool->free_map[pool->next_free];
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
302 #ifdef __LITTLE_ENDIAN__
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
309 if (lpar_rc != H_SUCCESS)
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
316 atomic_add(buffers_added, &pool->available);
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
361 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
363 adapter->tx_stats_buffers =
364 kcalloc(IBMVNIC_MAX_QUEUES,
365 sizeof(struct ibmvnic_tx_queue_stats),
367 if (!adapter->tx_stats_buffers)
370 adapter->rx_stats_buffers =
371 kcalloc(IBMVNIC_MAX_QUEUES,
372 sizeof(struct ibmvnic_rx_queue_stats),
374 if (!adapter->rx_stats_buffers)
380 static void release_stats_token(struct ibmvnic_adapter *adapter)
382 struct device *dev = &adapter->vdev->dev;
384 if (!adapter->stats_token)
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
390 adapter->stats_token = 0;
393 static int init_stats_token(struct ibmvnic_adapter *adapter)
395 struct device *dev = &adapter->vdev->dev;
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
406 adapter->stats_token = stok;
407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
411 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
413 struct ibmvnic_rx_pool *rx_pool;
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
456 static void release_rx_pools(struct ibmvnic_adapter *adapter)
458 struct ibmvnic_rx_pool *rx_pool;
461 if (!adapter->rx_pool)
464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
465 rx_pool = &adapter->rx_pool[i];
467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
472 if (!rx_pool->rx_buff)
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
482 kfree(rx_pool->rx_buff);
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
487 adapter->num_active_rx_pools = 0;
490 static int init_rx_pools(struct net_device *netdev)
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
512 adapter->num_active_rx_pools = rxadd_subcrqs;
514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
517 netdev_dbg(adapter->netdev,
518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
560 static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
561 struct ibmvnic_tx_pool *tx_pool)
565 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
569 memset(tx_pool->tx_buff, 0,
570 tx_pool->num_buffers *
571 sizeof(struct ibmvnic_tx_buff));
573 for (i = 0; i < tx_pool->num_buffers; i++)
574 tx_pool->free_map[i] = i;
576 tx_pool->consumer_index = 0;
577 tx_pool->producer_index = 0;
582 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
587 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
588 for (i = 0; i < tx_scrqs; i++) {
589 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
592 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
600 static void release_vpd_data(struct ibmvnic_adapter *adapter)
605 kfree(adapter->vpd->buff);
611 static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
612 struct ibmvnic_tx_pool *tx_pool)
614 kfree(tx_pool->tx_buff);
615 kfree(tx_pool->free_map);
616 free_long_term_buff(adapter, &tx_pool->long_term_buff);
619 static void release_tx_pools(struct ibmvnic_adapter *adapter)
623 if (!adapter->tx_pool)
626 for (i = 0; i < adapter->num_active_tx_pools; i++) {
627 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
628 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
631 kfree(adapter->tx_pool);
632 adapter->tx_pool = NULL;
633 kfree(adapter->tso_pool);
634 adapter->tso_pool = NULL;
635 adapter->num_active_tx_pools = 0;
638 static int init_tx_pools(struct net_device *netdev)
640 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
641 struct device *dev = &adapter->vdev->dev;
642 struct ibmvnic_tx_pool *tx_pool;
646 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
647 adapter->tx_pool = kcalloc(tx_subcrqs,
648 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
649 if (!adapter->tx_pool)
652 adapter->num_active_tx_pools = tx_subcrqs;
654 for (i = 0; i < tx_subcrqs; i++) {
655 tx_pool = &adapter->tx_pool[i];
657 netdev_dbg(adapter->netdev,
658 "Initializing tx_pool[%d], %lld buffs\n",
659 i, adapter->req_tx_entries_per_subcrq);
661 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
662 sizeof(struct ibmvnic_tx_buff),
664 if (!tx_pool->tx_buff) {
665 dev_err(dev, "tx pool buffer allocation failed\n");
666 release_tx_pools(adapter);
670 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
671 adapter->req_tx_entries_per_subcrq *
672 (adapter->req_mtu + VLAN_HLEN))) {
673 release_tx_pools(adapter);
678 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
680 IBMVNIC_TSO_BUF_SZ)) {
681 release_tx_pools(adapter);
685 tx_pool->tso_index = 0;
687 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
688 sizeof(int), GFP_KERNEL);
689 if (!tx_pool->free_map) {
690 release_tx_pools(adapter);
694 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
695 tx_pool->free_map[j] = j;
697 tx_pool->consumer_index = 0;
698 tx_pool->producer_index = 0;
704 static void release_error_buffers(struct ibmvnic_adapter *adapter)
706 struct device *dev = &adapter->vdev->dev;
707 struct ibmvnic_error_buff *error_buff, *tmp;
710 spin_lock_irqsave(&adapter->error_list_lock, flags);
711 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
712 list_del(&error_buff->list);
713 dma_unmap_single(dev, error_buff->dma, error_buff->len,
715 kfree(error_buff->buff);
718 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
721 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
725 if (adapter->napi_enabled)
728 for (i = 0; i < adapter->req_rx_queues; i++)
729 napi_enable(&adapter->napi[i]);
731 adapter->napi_enabled = true;
734 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
738 if (!adapter->napi_enabled)
741 for (i = 0; i < adapter->req_rx_queues; i++) {
742 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
743 napi_disable(&adapter->napi[i]);
746 adapter->napi_enabled = false;
749 static int init_napi(struct ibmvnic_adapter *adapter)
753 adapter->napi = kcalloc(adapter->req_rx_queues,
754 sizeof(struct napi_struct), GFP_KERNEL);
758 for (i = 0; i < adapter->req_rx_queues; i++) {
759 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
760 netif_napi_add(adapter->netdev, &adapter->napi[i],
761 ibmvnic_poll, NAPI_POLL_WEIGHT);
764 adapter->num_active_rx_napi = adapter->req_rx_queues;
768 static void release_napi(struct ibmvnic_adapter *adapter)
775 for (i = 0; i < adapter->num_active_rx_napi; i++) {
776 if (&adapter->napi[i]) {
777 netdev_dbg(adapter->netdev,
778 "Releasing napi[%d]\n", i);
779 netif_napi_del(&adapter->napi[i]);
783 kfree(adapter->napi);
784 adapter->napi = NULL;
785 adapter->num_active_rx_napi = 0;
788 static int ibmvnic_login(struct net_device *netdev)
790 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
791 unsigned long timeout = msecs_to_jiffies(30000);
792 struct device *dev = &adapter->vdev->dev;
796 if (adapter->renegotiate) {
797 adapter->renegotiate = false;
798 release_sub_crqs(adapter, 1);
800 reinit_completion(&adapter->init_done);
801 send_cap_queries(adapter);
802 if (!wait_for_completion_timeout(&adapter->init_done,
804 dev_err(dev, "Capabilities query timeout\n");
807 rc = init_sub_crqs(adapter);
810 "Initialization of SCRQ's failed\n");
813 rc = init_sub_crq_irqs(adapter);
816 "Initialization of SCRQ's irqs failed\n");
821 reinit_completion(&adapter->init_done);
822 rc = send_login(adapter);
824 dev_err(dev, "Unable to attempt device login\n");
826 } else if (!wait_for_completion_timeout(&adapter->init_done,
828 dev_err(dev, "Login timeout\n");
831 } while (adapter->renegotiate);
833 /* handle pending MAC address changes after successful login */
834 if (adapter->mac_change_pending) {
835 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
836 adapter->mac_change_pending = false;
842 static void release_login_buffer(struct ibmvnic_adapter *adapter)
844 kfree(adapter->login_buf);
845 adapter->login_buf = NULL;
848 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
850 kfree(adapter->login_rsp_buf);
851 adapter->login_rsp_buf = NULL;
854 static void release_resources(struct ibmvnic_adapter *adapter)
856 release_vpd_data(adapter);
858 release_tx_pools(adapter);
859 release_rx_pools(adapter);
861 release_error_buffers(adapter);
862 release_napi(adapter);
863 release_login_rsp_buffer(adapter);
866 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
868 struct net_device *netdev = adapter->netdev;
869 unsigned long timeout = msecs_to_jiffies(30000);
870 union ibmvnic_crq crq;
874 netdev_dbg(netdev, "setting link state %d\n", link_state);
876 memset(&crq, 0, sizeof(crq));
877 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
878 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
879 crq.logical_link_state.link_state = link_state;
884 reinit_completion(&adapter->init_done);
885 rc = ibmvnic_send_crq(adapter, &crq);
887 netdev_err(netdev, "Failed to set link state\n");
891 if (!wait_for_completion_timeout(&adapter->init_done,
893 netdev_err(netdev, "timeout setting link state\n");
897 if (adapter->init_done_rc == 1) {
898 /* Partuial success, delay and re-send */
907 static int set_real_num_queues(struct net_device *netdev)
909 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
912 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
913 adapter->req_tx_queues, adapter->req_rx_queues);
915 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
917 netdev_err(netdev, "failed to set the number of tx queues\n");
921 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
923 netdev_err(netdev, "failed to set the number of rx queues\n");
928 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
930 struct device *dev = &adapter->vdev->dev;
931 union ibmvnic_crq crq;
934 if (adapter->vpd->buff)
935 len = adapter->vpd->len;
937 init_completion(&adapter->fw_done);
938 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
939 crq.get_vpd_size.cmd = GET_VPD_SIZE;
940 ibmvnic_send_crq(adapter, &crq);
941 wait_for_completion(&adapter->fw_done);
943 if (!adapter->vpd->len)
946 if (!adapter->vpd->buff)
947 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
948 else if (adapter->vpd->len != len)
950 krealloc(adapter->vpd->buff,
951 adapter->vpd->len, GFP_KERNEL);
953 if (!adapter->vpd->buff) {
954 dev_err(dev, "Could allocate VPD buffer\n");
958 adapter->vpd->dma_addr =
959 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
961 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
962 dev_err(dev, "Could not map VPD buffer\n");
963 kfree(adapter->vpd->buff);
964 adapter->vpd->buff = NULL;
968 reinit_completion(&adapter->fw_done);
969 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
970 crq.get_vpd.cmd = GET_VPD;
971 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
972 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
973 ibmvnic_send_crq(adapter, &crq);
974 wait_for_completion(&adapter->fw_done);
979 static int init_resources(struct ibmvnic_adapter *adapter)
981 struct net_device *netdev = adapter->netdev;
984 rc = set_real_num_queues(netdev);
988 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
992 /* Vital Product Data (VPD) */
993 rc = ibmvnic_get_vpd(adapter);
995 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1001 rc = init_napi(adapter);
1005 send_map_query(adapter);
1007 rc = init_rx_pools(netdev);
1011 rc = init_tx_pools(netdev);
1015 static int __ibmvnic_open(struct net_device *netdev)
1017 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1018 enum vnic_state prev_state = adapter->state;
1021 adapter->state = VNIC_OPENING;
1022 replenish_pools(adapter);
1023 ibmvnic_napi_enable(adapter);
1025 /* We're ready to receive frames, enable the sub-crq interrupts and
1026 * set the logical link state to up
1028 for (i = 0; i < adapter->req_rx_queues; i++) {
1029 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1030 if (prev_state == VNIC_CLOSED)
1031 enable_irq(adapter->rx_scrq[i]->irq);
1033 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1036 for (i = 0; i < adapter->req_tx_queues; i++) {
1037 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1038 if (prev_state == VNIC_CLOSED)
1039 enable_irq(adapter->tx_scrq[i]->irq);
1041 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1044 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1046 for (i = 0; i < adapter->req_rx_queues; i++)
1047 napi_disable(&adapter->napi[i]);
1048 release_resources(adapter);
1052 netif_tx_start_all_queues(netdev);
1054 if (prev_state == VNIC_CLOSED) {
1055 for (i = 0; i < adapter->req_rx_queues; i++)
1056 napi_schedule(&adapter->napi[i]);
1059 adapter->state = VNIC_OPEN;
1063 static int ibmvnic_open(struct net_device *netdev)
1065 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1068 mutex_lock(&adapter->reset_lock);
1070 if (adapter->state != VNIC_CLOSED) {
1071 rc = ibmvnic_login(netdev);
1073 mutex_unlock(&adapter->reset_lock);
1077 rc = init_resources(adapter);
1079 netdev_err(netdev, "failed to initialize resources\n");
1080 release_resources(adapter);
1081 mutex_unlock(&adapter->reset_lock);
1086 rc = __ibmvnic_open(netdev);
1087 netif_carrier_on(netdev);
1089 mutex_unlock(&adapter->reset_lock);
1094 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1096 struct ibmvnic_rx_pool *rx_pool;
1097 struct ibmvnic_rx_buff *rx_buff;
1102 if (!adapter->rx_pool)
1105 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1106 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1108 /* Free any remaining skbs in the rx buffer pools */
1109 for (i = 0; i < rx_scrqs; i++) {
1110 rx_pool = &adapter->rx_pool[i];
1111 if (!rx_pool || !rx_pool->rx_buff)
1114 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1115 for (j = 0; j < rx_entries; j++) {
1116 rx_buff = &rx_pool->rx_buff[j];
1117 if (rx_buff && rx_buff->skb) {
1118 dev_kfree_skb_any(rx_buff->skb);
1119 rx_buff->skb = NULL;
1125 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1127 struct ibmvnic_tx_pool *tx_pool;
1128 struct ibmvnic_tx_buff *tx_buff;
1133 if (!adapter->tx_pool)
1136 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1137 tx_entries = adapter->req_tx_entries_per_subcrq;
1139 /* Free any remaining skbs in the tx buffer pools */
1140 for (i = 0; i < tx_scrqs; i++) {
1141 tx_pool = &adapter->tx_pool[i];
1142 if (!tx_pool && !tx_pool->tx_buff)
1145 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1146 for (j = 0; j < tx_entries; j++) {
1147 tx_buff = &tx_pool->tx_buff[j];
1148 if (tx_buff && tx_buff->skb) {
1149 dev_kfree_skb_any(tx_buff->skb);
1150 tx_buff->skb = NULL;
1156 static void ibmvnic_cleanup(struct net_device *netdev)
1158 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1161 /* ensure that transmissions are stopped if called by do_reset */
1162 if (adapter->resetting)
1163 netif_tx_disable(netdev);
1165 netif_tx_stop_all_queues(netdev);
1167 ibmvnic_napi_disable(adapter);
1169 if (adapter->tx_scrq) {
1170 for (i = 0; i < adapter->req_tx_queues; i++)
1171 if (adapter->tx_scrq[i]->irq) {
1173 "Disabling tx_scrq[%d] irq\n", i);
1174 disable_irq(adapter->tx_scrq[i]->irq);
1178 if (adapter->rx_scrq) {
1179 for (i = 0; i < adapter->req_rx_queues; i++) {
1180 if (adapter->rx_scrq[i]->irq) {
1182 "Disabling rx_scrq[%d] irq\n", i);
1183 disable_irq(adapter->rx_scrq[i]->irq);
1187 clean_rx_pools(adapter);
1188 clean_tx_pools(adapter);
1191 static int __ibmvnic_close(struct net_device *netdev)
1193 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1196 adapter->state = VNIC_CLOSING;
1197 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1200 ibmvnic_cleanup(netdev);
1201 adapter->state = VNIC_CLOSED;
1205 static int ibmvnic_close(struct net_device *netdev)
1207 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1210 mutex_lock(&adapter->reset_lock);
1211 rc = __ibmvnic_close(netdev);
1212 mutex_unlock(&adapter->reset_lock);
1218 * build_hdr_data - creates L2/L3/L4 header data buffer
1219 * @hdr_field - bitfield determining needed headers
1220 * @skb - socket buffer
1221 * @hdr_len - array of header lengths
1222 * @tot_len - total length of data
1224 * Reads hdr_field to determine which headers are needed by firmware.
1225 * Builds a buffer containing these headers. Saves individual header
1226 * lengths and total buffer length to be used to build descriptors.
1228 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1229 int *hdr_len, u8 *hdr_data)
1234 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1235 hdr_len[0] = sizeof(struct vlan_ethhdr);
1237 hdr_len[0] = sizeof(struct ethhdr);
1239 if (skb->protocol == htons(ETH_P_IP)) {
1240 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1241 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1242 hdr_len[2] = tcp_hdrlen(skb);
1243 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1244 hdr_len[2] = sizeof(struct udphdr);
1245 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1246 hdr_len[1] = sizeof(struct ipv6hdr);
1247 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1248 hdr_len[2] = tcp_hdrlen(skb);
1249 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1250 hdr_len[2] = sizeof(struct udphdr);
1251 } else if (skb->protocol == htons(ETH_P_ARP)) {
1252 hdr_len[1] = arp_hdr_len(skb->dev);
1256 memset(hdr_data, 0, 120);
1257 if ((hdr_field >> 6) & 1) {
1258 hdr = skb_mac_header(skb);
1259 memcpy(hdr_data, hdr, hdr_len[0]);
1263 if ((hdr_field >> 5) & 1) {
1264 hdr = skb_network_header(skb);
1265 memcpy(hdr_data + len, hdr, hdr_len[1]);
1269 if ((hdr_field >> 4) & 1) {
1270 hdr = skb_transport_header(skb);
1271 memcpy(hdr_data + len, hdr, hdr_len[2]);
1278 * create_hdr_descs - create header and header extension descriptors
1279 * @hdr_field - bitfield determining needed headers
1280 * @data - buffer containing header data
1281 * @len - length of data buffer
1282 * @hdr_len - array of individual header lengths
1283 * @scrq_arr - descriptor array
1285 * Creates header and, if needed, header extension descriptors and
1286 * places them in a descriptor array, scrq_arr
1289 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1290 union sub_crq *scrq_arr)
1292 union sub_crq hdr_desc;
1298 while (tmp_len > 0) {
1299 cur = hdr_data + len - tmp_len;
1301 memset(&hdr_desc, 0, sizeof(hdr_desc));
1302 if (cur != hdr_data) {
1303 data = hdr_desc.hdr_ext.data;
1304 tmp = tmp_len > 29 ? 29 : tmp_len;
1305 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1306 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1307 hdr_desc.hdr_ext.len = tmp;
1309 data = hdr_desc.hdr.data;
1310 tmp = tmp_len > 24 ? 24 : tmp_len;
1311 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1312 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1313 hdr_desc.hdr.len = tmp;
1314 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1315 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1316 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1317 hdr_desc.hdr.flag = hdr_field << 1;
1319 memcpy(data, cur, tmp);
1321 *scrq_arr = hdr_desc;
1330 * build_hdr_descs_arr - build a header descriptor array
1331 * @skb - socket buffer
1332 * @num_entries - number of descriptors to be sent
1333 * @subcrq - first TX descriptor
1334 * @hdr_field - bit field determining which headers will be sent
1336 * This function will build a TX descriptor array with applicable
1337 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1340 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1341 int *num_entries, u8 hdr_field)
1343 int hdr_len[3] = {0, 0, 0};
1345 u8 *hdr_data = txbuff->hdr_data;
1347 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1349 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1350 txbuff->indir_arr + 1);
1353 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1354 struct net_device *netdev)
1356 /* For some backing devices, mishandling of small packets
1357 * can result in a loss of connection or TX stall. Device
1358 * architects recommend that no packet should be smaller
1359 * than the minimum MTU value provided to the driver, so
1360 * pad any packets to that length
1362 if (skb->len < netdev->min_mtu)
1363 return skb_put_padto(skb, netdev->min_mtu);
1368 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1370 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1371 int queue_num = skb_get_queue_mapping(skb);
1372 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1373 struct device *dev = &adapter->vdev->dev;
1374 struct ibmvnic_tx_buff *tx_buff = NULL;
1375 struct ibmvnic_sub_crq_queue *tx_scrq;
1376 struct ibmvnic_tx_pool *tx_pool;
1377 unsigned int tx_send_failed = 0;
1378 unsigned int tx_map_failed = 0;
1379 unsigned int tx_dropped = 0;
1380 unsigned int tx_packets = 0;
1381 unsigned int tx_bytes = 0;
1382 dma_addr_t data_dma_addr;
1383 struct netdev_queue *txq;
1384 unsigned long lpar_rc;
1385 union sub_crq tx_crq;
1386 unsigned int offset;
1387 int num_entries = 1;
1394 if (adapter->resetting) {
1395 if (!netif_subqueue_stopped(netdev, skb))
1396 netif_stop_subqueue(netdev, queue_num);
1397 dev_kfree_skb_any(skb);
1405 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1412 tx_pool = &adapter->tx_pool[queue_num];
1413 tx_scrq = adapter->tx_scrq[queue_num];
1414 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1415 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1416 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1418 index = tx_pool->free_map[tx_pool->consumer_index];
1420 if (skb_is_gso(skb)) {
1421 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1422 dst = tx_pool->tso_ltb.buff + offset;
1423 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1424 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1425 tx_pool->tso_index++;
1426 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1427 tx_pool->tso_index = 0;
1429 offset = index * (adapter->req_mtu + VLAN_HLEN);
1430 dst = tx_pool->long_term_buff.buff + offset;
1431 memset(dst, 0, adapter->req_mtu + VLAN_HLEN);
1432 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1435 if (skb_shinfo(skb)->nr_frags) {
1439 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1440 cur = skb_headlen(skb);
1442 /* Copy the frags */
1443 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1444 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1447 page_address(skb_frag_page(frag)) +
1448 frag->page_offset, skb_frag_size(frag));
1449 cur += skb_frag_size(frag);
1452 skb_copy_from_linear_data(skb, dst, skb->len);
1455 tx_pool->consumer_index =
1456 (tx_pool->consumer_index + 1) %
1457 adapter->req_tx_entries_per_subcrq;
1459 tx_buff = &tx_pool->tx_buff[index];
1461 tx_buff->data_dma[0] = data_dma_addr;
1462 tx_buff->data_len[0] = skb->len;
1463 tx_buff->index = index;
1464 tx_buff->pool_index = queue_num;
1465 tx_buff->last_frag = true;
1467 memset(&tx_crq, 0, sizeof(tx_crq));
1468 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1469 tx_crq.v1.type = IBMVNIC_TX_DESC;
1470 tx_crq.v1.n_crq_elem = 1;
1471 tx_crq.v1.n_sge = 1;
1472 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1473 tx_crq.v1.correlator = cpu_to_be32(index);
1474 if (skb_is_gso(skb))
1475 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1477 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1478 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1479 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1481 if (adapter->vlan_header_insertion) {
1482 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1483 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1486 if (skb->protocol == htons(ETH_P_IP)) {
1487 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1488 proto = ip_hdr(skb)->protocol;
1489 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1490 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1491 proto = ipv6_hdr(skb)->nexthdr;
1494 if (proto == IPPROTO_TCP)
1495 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1496 else if (proto == IPPROTO_UDP)
1497 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1499 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1500 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1503 if (skb_is_gso(skb)) {
1504 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1505 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1508 /* determine if l2/3/4 headers are sent to firmware */
1509 if ((*hdrs >> 7) & 1) {
1510 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1511 tx_crq.v1.n_crq_elem = num_entries;
1512 tx_buff->num_entries = num_entries;
1513 tx_buff->indir_arr[0] = tx_crq;
1514 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1515 sizeof(tx_buff->indir_arr),
1517 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1518 dev_kfree_skb_any(skb);
1519 tx_buff->skb = NULL;
1520 if (!firmware_has_feature(FW_FEATURE_CMO))
1521 dev_err(dev, "tx: unable to map descriptor array\n");
1527 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1528 (u64)tx_buff->indir_dma,
1531 tx_buff->num_entries = num_entries;
1532 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1535 if (lpar_rc != H_SUCCESS) {
1536 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1538 if (tx_pool->consumer_index == 0)
1539 tx_pool->consumer_index =
1540 adapter->req_tx_entries_per_subcrq - 1;
1542 tx_pool->consumer_index--;
1544 dev_kfree_skb_any(skb);
1545 tx_buff->skb = NULL;
1547 if (lpar_rc == H_CLOSED) {
1548 /* Disable TX and report carrier off if queue is closed.
1549 * Firmware guarantees that a signal will be sent to the
1550 * driver, triggering a reset or some other action.
1552 netif_tx_stop_all_queues(netdev);
1553 netif_carrier_off(netdev);
1562 if (atomic_add_return(num_entries, &tx_scrq->used)
1563 >= adapter->req_tx_entries_per_subcrq) {
1564 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1565 netif_stop_subqueue(netdev, queue_num);
1569 tx_bytes += skb->len;
1570 txq->trans_start = jiffies;
1574 netdev->stats.tx_dropped += tx_dropped;
1575 netdev->stats.tx_bytes += tx_bytes;
1576 netdev->stats.tx_packets += tx_packets;
1577 adapter->tx_send_failed += tx_send_failed;
1578 adapter->tx_map_failed += tx_map_failed;
1579 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1580 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1581 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1586 static void ibmvnic_set_multi(struct net_device *netdev)
1588 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1589 struct netdev_hw_addr *ha;
1590 union ibmvnic_crq crq;
1592 memset(&crq, 0, sizeof(crq));
1593 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1594 crq.request_capability.cmd = REQUEST_CAPABILITY;
1596 if (netdev->flags & IFF_PROMISC) {
1597 if (!adapter->promisc_supported)
1600 if (netdev->flags & IFF_ALLMULTI) {
1601 /* Accept all multicast */
1602 memset(&crq, 0, sizeof(crq));
1603 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1604 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1605 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1606 ibmvnic_send_crq(adapter, &crq);
1607 } else if (netdev_mc_empty(netdev)) {
1608 /* Reject all multicast */
1609 memset(&crq, 0, sizeof(crq));
1610 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1611 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1612 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1613 ibmvnic_send_crq(adapter, &crq);
1615 /* Accept one or more multicast(s) */
1616 netdev_for_each_mc_addr(ha, netdev) {
1617 memset(&crq, 0, sizeof(crq));
1618 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1619 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1620 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1621 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1623 ibmvnic_send_crq(adapter, &crq);
1629 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1631 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1632 struct sockaddr *addr = p;
1633 union ibmvnic_crq crq;
1635 if (!is_valid_ether_addr(addr->sa_data))
1636 return -EADDRNOTAVAIL;
1638 memset(&crq, 0, sizeof(crq));
1639 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1640 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1641 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1643 init_completion(&adapter->fw_done);
1644 ibmvnic_send_crq(adapter, &crq);
1645 wait_for_completion(&adapter->fw_done);
1646 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1647 return adapter->fw_done_rc ? -EIO : 0;
1650 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1652 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1653 struct sockaddr *addr = p;
1656 if (adapter->state == VNIC_PROBED) {
1657 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1658 adapter->mac_change_pending = true;
1662 rc = __ibmvnic_set_mac(netdev, addr);
1668 * do_reset returns zero if we are able to keep processing reset events, or
1669 * non-zero if we hit a fatal error and must halt.
1671 static int do_reset(struct ibmvnic_adapter *adapter,
1672 struct ibmvnic_rwi *rwi, u32 reset_state)
1674 u64 old_num_rx_queues, old_num_tx_queues;
1675 struct net_device *netdev = adapter->netdev;
1678 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1681 netif_carrier_off(netdev);
1682 adapter->reset_reason = rwi->reset_reason;
1684 old_num_rx_queues = adapter->req_rx_queues;
1685 old_num_tx_queues = adapter->req_tx_queues;
1687 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1688 rc = ibmvnic_reenable_crq_queue(adapter);
1691 ibmvnic_cleanup(netdev);
1692 } else if (rwi->reset_reason == VNIC_RESET_FAILOVER) {
1693 ibmvnic_cleanup(netdev);
1695 rc = __ibmvnic_close(netdev);
1700 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1701 adapter->wait_for_reset) {
1702 release_resources(adapter);
1703 release_sub_crqs(adapter, 1);
1704 release_crq_queue(adapter);
1707 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1708 /* remove the closed state so when we call open it appears
1709 * we are coming from the probed state.
1711 adapter->state = VNIC_PROBED;
1713 rc = ibmvnic_init(adapter);
1715 return IBMVNIC_INIT_FAILED;
1717 /* If the adapter was in PROBE state prior to the reset,
1720 if (reset_state == VNIC_PROBED)
1723 rc = ibmvnic_login(netdev);
1725 adapter->state = VNIC_PROBED;
1729 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1730 adapter->wait_for_reset) {
1731 rc = init_resources(adapter);
1734 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1735 adapter->req_tx_queues != old_num_tx_queues) {
1736 adapter->map_id = 1;
1737 release_rx_pools(adapter);
1738 release_tx_pools(adapter);
1739 init_rx_pools(netdev);
1740 init_tx_pools(netdev);
1742 release_napi(adapter);
1745 rc = reset_tx_pools(adapter);
1749 rc = reset_rx_pools(adapter);
1755 adapter->state = VNIC_CLOSED;
1757 if (reset_state == VNIC_CLOSED)
1760 rc = __ibmvnic_open(netdev);
1762 if (list_empty(&adapter->rwi_list))
1763 adapter->state = VNIC_CLOSED;
1765 adapter->state = reset_state;
1771 for (i = 0; i < adapter->req_rx_queues; i++)
1772 napi_schedule(&adapter->napi[i]);
1774 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1775 netdev_notify_peers(netdev);
1777 netif_carrier_on(netdev);
1782 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1784 struct ibmvnic_rwi *rwi;
1786 mutex_lock(&adapter->rwi_lock);
1788 if (!list_empty(&adapter->rwi_list)) {
1789 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1791 list_del(&rwi->list);
1796 mutex_unlock(&adapter->rwi_lock);
1800 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1802 struct ibmvnic_rwi *rwi;
1804 rwi = get_next_rwi(adapter);
1807 rwi = get_next_rwi(adapter);
1811 static void __ibmvnic_reset(struct work_struct *work)
1813 struct ibmvnic_rwi *rwi;
1814 struct ibmvnic_adapter *adapter;
1815 struct net_device *netdev;
1819 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1820 netdev = adapter->netdev;
1822 mutex_lock(&adapter->reset_lock);
1823 adapter->resetting = true;
1824 reset_state = adapter->state;
1826 rwi = get_next_rwi(adapter);
1828 rc = do_reset(adapter, rwi, reset_state);
1830 if (rc && rc != IBMVNIC_INIT_FAILED)
1833 rwi = get_next_rwi(adapter);
1836 if (adapter->wait_for_reset) {
1837 adapter->wait_for_reset = false;
1838 adapter->reset_done_rc = rc;
1839 complete(&adapter->reset_done);
1843 netdev_dbg(adapter->netdev, "Reset failed\n");
1844 free_all_rwi(adapter);
1845 mutex_unlock(&adapter->reset_lock);
1849 adapter->resetting = false;
1850 mutex_unlock(&adapter->reset_lock);
1853 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1854 enum ibmvnic_reset_reason reason)
1856 struct ibmvnic_rwi *rwi, *tmp;
1857 struct net_device *netdev = adapter->netdev;
1858 struct list_head *entry;
1860 if (adapter->state == VNIC_REMOVING ||
1861 adapter->state == VNIC_REMOVED) {
1862 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1866 if (adapter->state == VNIC_PROBING) {
1867 netdev_warn(netdev, "Adapter reset during probe\n");
1868 adapter->init_done_rc = EAGAIN;
1872 mutex_lock(&adapter->rwi_lock);
1874 list_for_each(entry, &adapter->rwi_list) {
1875 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1876 if (tmp->reset_reason == reason) {
1877 netdev_dbg(netdev, "Skipping matching reset\n");
1878 mutex_unlock(&adapter->rwi_lock);
1883 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1885 mutex_unlock(&adapter->rwi_lock);
1886 ibmvnic_close(netdev);
1890 rwi->reset_reason = reason;
1891 list_add_tail(&rwi->list, &adapter->rwi_list);
1892 mutex_unlock(&adapter->rwi_lock);
1894 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1895 schedule_work(&adapter->ibmvnic_reset);
1898 static void ibmvnic_tx_timeout(struct net_device *dev)
1900 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1902 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1905 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1906 struct ibmvnic_rx_buff *rx_buff)
1908 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1910 rx_buff->skb = NULL;
1912 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1913 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1915 atomic_dec(&pool->available);
1918 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1920 struct net_device *netdev = napi->dev;
1921 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1922 int scrq_num = (int)(napi - adapter->napi);
1923 int frames_processed = 0;
1926 while (frames_processed < budget) {
1927 struct sk_buff *skb;
1928 struct ibmvnic_rx_buff *rx_buff;
1929 union sub_crq *next;
1934 if (unlikely(adapter->resetting &&
1935 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1936 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1937 napi_complete_done(napi, frames_processed);
1938 return frames_processed;
1941 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1943 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1945 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1946 rx_comp.correlator);
1947 /* do error checking */
1948 if (next->rx_comp.rc) {
1949 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1950 be16_to_cpu(next->rx_comp.rc));
1951 /* free the entry */
1952 next->rx_comp.first = 0;
1953 dev_kfree_skb_any(rx_buff->skb);
1954 remove_buff_from_pool(adapter, rx_buff);
1956 } else if (!rx_buff->skb) {
1957 /* free the entry */
1958 next->rx_comp.first = 0;
1959 remove_buff_from_pool(adapter, rx_buff);
1963 length = be32_to_cpu(next->rx_comp.len);
1964 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1965 flags = next->rx_comp.flags;
1967 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1970 /* VLAN Header has been stripped by the system firmware and
1971 * needs to be inserted by the driver
1973 if (adapter->rx_vlan_header_insertion &&
1974 (flags & IBMVNIC_VLAN_STRIPPED))
1975 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1976 ntohs(next->rx_comp.vlan_tci));
1978 /* free the entry */
1979 next->rx_comp.first = 0;
1980 remove_buff_from_pool(adapter, rx_buff);
1982 skb_put(skb, length);
1983 skb->protocol = eth_type_trans(skb, netdev);
1984 skb_record_rx_queue(skb, scrq_num);
1986 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1987 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1988 skb->ip_summed = CHECKSUM_UNNECESSARY;
1992 napi_gro_receive(napi, skb); /* send it up */
1993 netdev->stats.rx_packets++;
1994 netdev->stats.rx_bytes += length;
1995 adapter->rx_stats_buffers[scrq_num].packets++;
1996 adapter->rx_stats_buffers[scrq_num].bytes += length;
2000 if (adapter->state != VNIC_CLOSING)
2001 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2003 if (frames_processed < budget) {
2004 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2005 napi_complete_done(napi, frames_processed);
2006 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2007 napi_reschedule(napi)) {
2008 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2012 return frames_processed;
2015 #ifdef CONFIG_NET_POLL_CONTROLLER
2016 static void ibmvnic_netpoll_controller(struct net_device *dev)
2018 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2021 replenish_pools(netdev_priv(dev));
2022 for (i = 0; i < adapter->req_rx_queues; i++)
2023 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2024 adapter->rx_scrq[i]);
2028 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2030 adapter->fallback.mtu = adapter->req_mtu;
2031 adapter->fallback.rx_queues = adapter->req_rx_queues;
2032 adapter->fallback.tx_queues = adapter->req_tx_queues;
2033 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2034 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2036 init_completion(&adapter->reset_done);
2037 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2038 adapter->wait_for_reset = true;
2039 wait_for_completion(&adapter->reset_done);
2041 if (adapter->reset_done_rc) {
2042 adapter->desired.mtu = adapter->fallback.mtu;
2043 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2044 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2045 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2046 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2048 init_completion(&adapter->reset_done);
2049 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2050 wait_for_completion(&adapter->reset_done);
2052 adapter->wait_for_reset = false;
2054 return adapter->reset_done_rc;
2057 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2059 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2061 adapter->desired.mtu = new_mtu + ETH_HLEN;
2063 return wait_for_reset(adapter);
2066 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2067 struct net_device *dev,
2068 netdev_features_t features)
2070 /* Some backing hardware adapters can not
2071 * handle packets with a MSS less than 224
2072 * or with only one segment.
2074 if (skb_is_gso(skb)) {
2075 if (skb_shinfo(skb)->gso_size < 224 ||
2076 skb_shinfo(skb)->gso_segs == 1)
2077 features &= ~NETIF_F_GSO_MASK;
2083 static const struct net_device_ops ibmvnic_netdev_ops = {
2084 .ndo_open = ibmvnic_open,
2085 .ndo_stop = ibmvnic_close,
2086 .ndo_start_xmit = ibmvnic_xmit,
2087 .ndo_set_rx_mode = ibmvnic_set_multi,
2088 .ndo_set_mac_address = ibmvnic_set_mac,
2089 .ndo_validate_addr = eth_validate_addr,
2090 .ndo_tx_timeout = ibmvnic_tx_timeout,
2091 #ifdef CONFIG_NET_POLL_CONTROLLER
2092 .ndo_poll_controller = ibmvnic_netpoll_controller,
2094 .ndo_change_mtu = ibmvnic_change_mtu,
2095 .ndo_features_check = ibmvnic_features_check,
2098 /* ethtool functions */
2100 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2101 struct ethtool_link_ksettings *cmd)
2103 u32 supported, advertising;
2105 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2107 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2109 cmd->base.speed = SPEED_1000;
2110 cmd->base.duplex = DUPLEX_FULL;
2111 cmd->base.port = PORT_FIBRE;
2112 cmd->base.phy_address = 0;
2113 cmd->base.autoneg = AUTONEG_ENABLE;
2115 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2117 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2123 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2124 struct ethtool_drvinfo *info)
2126 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2128 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2129 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2130 strlcpy(info->fw_version, adapter->fw_version,
2131 sizeof(info->fw_version));
2134 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2136 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2138 return adapter->msg_enable;
2141 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2143 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2145 adapter->msg_enable = data;
2148 static u32 ibmvnic_get_link(struct net_device *netdev)
2150 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2152 /* Don't need to send a query because we request a logical link up at
2153 * init and then we wait for link state indications
2155 return adapter->logical_link_state;
2158 static void ibmvnic_get_ringparam(struct net_device *netdev,
2159 struct ethtool_ringparam *ring)
2161 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2163 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2164 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2165 ring->rx_mini_max_pending = 0;
2166 ring->rx_jumbo_max_pending = 0;
2167 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2168 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2169 ring->rx_mini_pending = 0;
2170 ring->rx_jumbo_pending = 0;
2173 static int ibmvnic_set_ringparam(struct net_device *netdev,
2174 struct ethtool_ringparam *ring)
2176 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2178 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2179 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2180 netdev_err(netdev, "Invalid request.\n");
2181 netdev_err(netdev, "Max tx buffers = %llu\n",
2182 adapter->max_rx_add_entries_per_subcrq);
2183 netdev_err(netdev, "Max rx buffers = %llu\n",
2184 adapter->max_tx_entries_per_subcrq);
2188 adapter->desired.rx_entries = ring->rx_pending;
2189 adapter->desired.tx_entries = ring->tx_pending;
2191 return wait_for_reset(adapter);
2194 static void ibmvnic_get_channels(struct net_device *netdev,
2195 struct ethtool_channels *channels)
2197 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2199 channels->max_rx = adapter->max_rx_queues;
2200 channels->max_tx = adapter->max_tx_queues;
2201 channels->max_other = 0;
2202 channels->max_combined = 0;
2203 channels->rx_count = adapter->req_rx_queues;
2204 channels->tx_count = adapter->req_tx_queues;
2205 channels->other_count = 0;
2206 channels->combined_count = 0;
2209 static int ibmvnic_set_channels(struct net_device *netdev,
2210 struct ethtool_channels *channels)
2212 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2214 adapter->desired.rx_queues = channels->rx_count;
2215 adapter->desired.tx_queues = channels->tx_count;
2217 return wait_for_reset(adapter);
2220 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2222 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2225 if (stringset != ETH_SS_STATS)
2228 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2229 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2231 for (i = 0; i < adapter->req_tx_queues; i++) {
2232 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2233 data += ETH_GSTRING_LEN;
2235 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2236 data += ETH_GSTRING_LEN;
2238 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2239 data += ETH_GSTRING_LEN;
2242 for (i = 0; i < adapter->req_rx_queues; i++) {
2243 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2244 data += ETH_GSTRING_LEN;
2246 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2247 data += ETH_GSTRING_LEN;
2249 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2250 data += ETH_GSTRING_LEN;
2254 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2256 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2260 return ARRAY_SIZE(ibmvnic_stats) +
2261 adapter->req_tx_queues * NUM_TX_STATS +
2262 adapter->req_rx_queues * NUM_RX_STATS;
2268 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2269 struct ethtool_stats *stats, u64 *data)
2271 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2272 union ibmvnic_crq crq;
2275 memset(&crq, 0, sizeof(crq));
2276 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2277 crq.request_statistics.cmd = REQUEST_STATISTICS;
2278 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2279 crq.request_statistics.len =
2280 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2282 /* Wait for data to be written */
2283 init_completion(&adapter->stats_done);
2284 ibmvnic_send_crq(adapter, &crq);
2285 wait_for_completion(&adapter->stats_done);
2287 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2288 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2289 ibmvnic_stats[i].offset));
2291 for (j = 0; j < adapter->req_tx_queues; j++) {
2292 data[i] = adapter->tx_stats_buffers[j].packets;
2294 data[i] = adapter->tx_stats_buffers[j].bytes;
2296 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2300 for (j = 0; j < adapter->req_rx_queues; j++) {
2301 data[i] = adapter->rx_stats_buffers[j].packets;
2303 data[i] = adapter->rx_stats_buffers[j].bytes;
2305 data[i] = adapter->rx_stats_buffers[j].interrupts;
2310 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2311 .get_drvinfo = ibmvnic_get_drvinfo,
2312 .get_msglevel = ibmvnic_get_msglevel,
2313 .set_msglevel = ibmvnic_set_msglevel,
2314 .get_link = ibmvnic_get_link,
2315 .get_ringparam = ibmvnic_get_ringparam,
2316 .set_ringparam = ibmvnic_set_ringparam,
2317 .get_channels = ibmvnic_get_channels,
2318 .set_channels = ibmvnic_set_channels,
2319 .get_strings = ibmvnic_get_strings,
2320 .get_sset_count = ibmvnic_get_sset_count,
2321 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2322 .get_link_ksettings = ibmvnic_get_link_ksettings,
2325 /* Routines for managing CRQs/sCRQs */
2327 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2328 struct ibmvnic_sub_crq_queue *scrq)
2333 free_irq(scrq->irq, scrq);
2334 irq_dispose_mapping(scrq->irq);
2338 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2341 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2342 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2346 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2350 for (i = 0; i < adapter->req_tx_queues; i++) {
2351 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2352 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2357 for (i = 0; i < adapter->req_rx_queues; i++) {
2358 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2359 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2367 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2368 struct ibmvnic_sub_crq_queue *scrq,
2371 struct device *dev = &adapter->vdev->dev;
2374 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2377 /* Close the sub-crqs */
2379 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2380 adapter->vdev->unit_address,
2382 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2385 netdev_err(adapter->netdev,
2386 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2391 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2393 free_pages((unsigned long)scrq->msgs, 2);
2397 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2400 struct device *dev = &adapter->vdev->dev;
2401 struct ibmvnic_sub_crq_queue *scrq;
2404 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2409 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2411 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2412 goto zero_page_failed;
2415 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2417 if (dma_mapping_error(dev, scrq->msg_token)) {
2418 dev_warn(dev, "Couldn't map crq queue messages page\n");
2422 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2423 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2425 if (rc == H_RESOURCE)
2426 rc = ibmvnic_reset_crq(adapter);
2428 if (rc == H_CLOSED) {
2429 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2431 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2435 scrq->adapter = adapter;
2436 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2437 spin_lock_init(&scrq->lock);
2439 netdev_dbg(adapter->netdev,
2440 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2441 scrq->crq_num, scrq->hw_irq, scrq->irq);
2446 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2449 free_pages((unsigned long)scrq->msgs, 2);
2456 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2460 if (adapter->tx_scrq) {
2461 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2462 if (!adapter->tx_scrq[i])
2465 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2467 if (adapter->tx_scrq[i]->irq) {
2468 free_irq(adapter->tx_scrq[i]->irq,
2469 adapter->tx_scrq[i]);
2470 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2471 adapter->tx_scrq[i]->irq = 0;
2474 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2478 kfree(adapter->tx_scrq);
2479 adapter->tx_scrq = NULL;
2480 adapter->num_active_tx_scrqs = 0;
2483 if (adapter->rx_scrq) {
2484 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2485 if (!adapter->rx_scrq[i])
2488 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2490 if (adapter->rx_scrq[i]->irq) {
2491 free_irq(adapter->rx_scrq[i]->irq,
2492 adapter->rx_scrq[i]);
2493 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2494 adapter->rx_scrq[i]->irq = 0;
2497 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2501 kfree(adapter->rx_scrq);
2502 adapter->rx_scrq = NULL;
2503 adapter->num_active_rx_scrqs = 0;
2507 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2508 struct ibmvnic_sub_crq_queue *scrq)
2510 struct device *dev = &adapter->vdev->dev;
2513 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2514 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2516 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2521 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2522 struct ibmvnic_sub_crq_queue *scrq)
2524 struct device *dev = &adapter->vdev->dev;
2527 if (scrq->hw_irq > 0x100000000ULL) {
2528 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2532 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2533 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2535 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2540 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2541 struct ibmvnic_sub_crq_queue *scrq)
2543 struct device *dev = &adapter->vdev->dev;
2544 struct ibmvnic_tx_buff *txbuff;
2545 union sub_crq *next;
2551 while (pending_scrq(adapter, scrq)) {
2552 unsigned int pool = scrq->pool_index;
2553 int num_entries = 0;
2555 next = ibmvnic_next_scrq(adapter, scrq);
2556 for (i = 0; i < next->tx_comp.num_comps; i++) {
2557 if (next->tx_comp.rcs[i]) {
2558 dev_err(dev, "tx error %x\n",
2559 next->tx_comp.rcs[i]);
2562 index = be32_to_cpu(next->tx_comp.correlators[i]);
2563 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2565 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2566 if (!txbuff->data_dma[j])
2569 txbuff->data_dma[j] = 0;
2571 /* if sub_crq was sent indirectly */
2572 first = txbuff->indir_arr[0].generic.first;
2573 if (first == IBMVNIC_CRQ_CMD) {
2574 dma_unmap_single(dev, txbuff->indir_dma,
2575 sizeof(txbuff->indir_arr),
2579 if (txbuff->last_frag) {
2580 dev_kfree_skb_any(txbuff->skb);
2584 num_entries += txbuff->num_entries;
2586 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2587 producer_index] = index;
2588 adapter->tx_pool[pool].producer_index =
2589 (adapter->tx_pool[pool].producer_index + 1) %
2590 adapter->req_tx_entries_per_subcrq;
2592 /* remove tx_comp scrq*/
2593 next->tx_comp.first = 0;
2595 if (atomic_sub_return(num_entries, &scrq->used) <=
2596 (adapter->req_tx_entries_per_subcrq / 2) &&
2597 __netif_subqueue_stopped(adapter->netdev,
2598 scrq->pool_index)) {
2599 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2600 netdev_dbg(adapter->netdev, "Started queue %d\n",
2605 enable_scrq_irq(adapter, scrq);
2607 if (pending_scrq(adapter, scrq)) {
2608 disable_scrq_irq(adapter, scrq);
2615 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2617 struct ibmvnic_sub_crq_queue *scrq = instance;
2618 struct ibmvnic_adapter *adapter = scrq->adapter;
2620 disable_scrq_irq(adapter, scrq);
2621 ibmvnic_complete_tx(adapter, scrq);
2626 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2628 struct ibmvnic_sub_crq_queue *scrq = instance;
2629 struct ibmvnic_adapter *adapter = scrq->adapter;
2631 /* When booting a kdump kernel we can hit pending interrupts
2632 * prior to completing driver initialization.
2634 if (unlikely(adapter->state != VNIC_OPEN))
2637 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2639 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2640 disable_scrq_irq(adapter, scrq);
2641 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2647 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2649 struct device *dev = &adapter->vdev->dev;
2650 struct ibmvnic_sub_crq_queue *scrq;
2654 for (i = 0; i < adapter->req_tx_queues; i++) {
2655 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2657 scrq = adapter->tx_scrq[i];
2658 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2662 dev_err(dev, "Error mapping irq\n");
2663 goto req_tx_irq_failed;
2666 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2667 0, "ibmvnic_tx", scrq);
2670 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2672 irq_dispose_mapping(scrq->irq);
2673 goto req_tx_irq_failed;
2677 for (i = 0; i < adapter->req_rx_queues; i++) {
2678 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2680 scrq = adapter->rx_scrq[i];
2681 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2684 dev_err(dev, "Error mapping irq\n");
2685 goto req_rx_irq_failed;
2687 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2688 0, "ibmvnic_rx", scrq);
2690 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2692 irq_dispose_mapping(scrq->irq);
2693 goto req_rx_irq_failed;
2699 for (j = 0; j < i; j++) {
2700 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2701 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2703 i = adapter->req_tx_queues;
2705 for (j = 0; j < i; j++) {
2706 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2707 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2709 release_sub_crqs(adapter, 1);
2713 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2715 struct device *dev = &adapter->vdev->dev;
2716 struct ibmvnic_sub_crq_queue **allqueues;
2717 int registered_queues = 0;
2722 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2724 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2728 for (i = 0; i < total_queues; i++) {
2729 allqueues[i] = init_sub_crq_queue(adapter);
2730 if (!allqueues[i]) {
2731 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2734 registered_queues++;
2737 /* Make sure we were able to register the minimum number of queues */
2738 if (registered_queues <
2739 adapter->min_tx_queues + adapter->min_rx_queues) {
2740 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2744 /* Distribute the failed allocated queues*/
2745 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2746 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2749 if (adapter->req_rx_queues > adapter->min_rx_queues)
2750 adapter->req_rx_queues--;
2755 if (adapter->req_tx_queues > adapter->min_tx_queues)
2756 adapter->req_tx_queues--;
2763 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2764 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2765 if (!adapter->tx_scrq)
2768 for (i = 0; i < adapter->req_tx_queues; i++) {
2769 adapter->tx_scrq[i] = allqueues[i];
2770 adapter->tx_scrq[i]->pool_index = i;
2771 adapter->num_active_tx_scrqs++;
2774 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2775 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2776 if (!adapter->rx_scrq)
2779 for (i = 0; i < adapter->req_rx_queues; i++) {
2780 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2781 adapter->rx_scrq[i]->scrq_num = i;
2782 adapter->num_active_rx_scrqs++;
2789 kfree(adapter->tx_scrq);
2790 adapter->tx_scrq = NULL;
2792 for (i = 0; i < registered_queues; i++)
2793 release_sub_crq_queue(adapter, allqueues[i], 1);
2798 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2800 struct device *dev = &adapter->vdev->dev;
2801 union ibmvnic_crq crq;
2805 /* Sub-CRQ entries are 32 byte long */
2806 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2808 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2809 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2810 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2814 if (adapter->desired.mtu)
2815 adapter->req_mtu = adapter->desired.mtu;
2817 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2819 if (!adapter->desired.tx_entries)
2820 adapter->desired.tx_entries =
2821 adapter->max_tx_entries_per_subcrq;
2822 if (!adapter->desired.rx_entries)
2823 adapter->desired.rx_entries =
2824 adapter->max_rx_add_entries_per_subcrq;
2826 max_entries = IBMVNIC_MAX_LTB_SIZE /
2827 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2829 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2830 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2831 adapter->desired.tx_entries = max_entries;
2834 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2835 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2836 adapter->desired.rx_entries = max_entries;
2839 if (adapter->desired.tx_entries)
2840 adapter->req_tx_entries_per_subcrq =
2841 adapter->desired.tx_entries;
2843 adapter->req_tx_entries_per_subcrq =
2844 adapter->max_tx_entries_per_subcrq;
2846 if (adapter->desired.rx_entries)
2847 adapter->req_rx_add_entries_per_subcrq =
2848 adapter->desired.rx_entries;
2850 adapter->req_rx_add_entries_per_subcrq =
2851 adapter->max_rx_add_entries_per_subcrq;
2853 if (adapter->desired.tx_queues)
2854 adapter->req_tx_queues =
2855 adapter->desired.tx_queues;
2857 adapter->req_tx_queues =
2858 adapter->opt_tx_comp_sub_queues;
2860 if (adapter->desired.rx_queues)
2861 adapter->req_rx_queues =
2862 adapter->desired.rx_queues;
2864 adapter->req_rx_queues =
2865 adapter->opt_rx_comp_queues;
2867 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2870 memset(&crq, 0, sizeof(crq));
2871 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2872 crq.request_capability.cmd = REQUEST_CAPABILITY;
2874 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2875 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2876 atomic_inc(&adapter->running_cap_crqs);
2877 ibmvnic_send_crq(adapter, &crq);
2879 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2880 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2881 atomic_inc(&adapter->running_cap_crqs);
2882 ibmvnic_send_crq(adapter, &crq);
2884 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2885 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2886 atomic_inc(&adapter->running_cap_crqs);
2887 ibmvnic_send_crq(adapter, &crq);
2889 crq.request_capability.capability =
2890 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2891 crq.request_capability.number =
2892 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2893 atomic_inc(&adapter->running_cap_crqs);
2894 ibmvnic_send_crq(adapter, &crq);
2896 crq.request_capability.capability =
2897 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2898 crq.request_capability.number =
2899 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2900 atomic_inc(&adapter->running_cap_crqs);
2901 ibmvnic_send_crq(adapter, &crq);
2903 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2904 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2905 atomic_inc(&adapter->running_cap_crqs);
2906 ibmvnic_send_crq(adapter, &crq);
2908 if (adapter->netdev->flags & IFF_PROMISC) {
2909 if (adapter->promisc_supported) {
2910 crq.request_capability.capability =
2911 cpu_to_be16(PROMISC_REQUESTED);
2912 crq.request_capability.number = cpu_to_be64(1);
2913 atomic_inc(&adapter->running_cap_crqs);
2914 ibmvnic_send_crq(adapter, &crq);
2917 crq.request_capability.capability =
2918 cpu_to_be16(PROMISC_REQUESTED);
2919 crq.request_capability.number = cpu_to_be64(0);
2920 atomic_inc(&adapter->running_cap_crqs);
2921 ibmvnic_send_crq(adapter, &crq);
2925 static int pending_scrq(struct ibmvnic_adapter *adapter,
2926 struct ibmvnic_sub_crq_queue *scrq)
2928 union sub_crq *entry = &scrq->msgs[scrq->cur];
2930 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2936 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2937 struct ibmvnic_sub_crq_queue *scrq)
2939 union sub_crq *entry;
2940 unsigned long flags;
2942 spin_lock_irqsave(&scrq->lock, flags);
2943 entry = &scrq->msgs[scrq->cur];
2944 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2945 if (++scrq->cur == scrq->size)
2950 spin_unlock_irqrestore(&scrq->lock, flags);
2955 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2957 struct ibmvnic_crq_queue *queue = &adapter->crq;
2958 union ibmvnic_crq *crq;
2960 crq = &queue->msgs[queue->cur];
2961 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2962 if (++queue->cur == queue->size)
2971 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2972 union sub_crq *sub_crq)
2974 unsigned int ua = adapter->vdev->unit_address;
2975 struct device *dev = &adapter->vdev->dev;
2976 u64 *u64_crq = (u64 *)sub_crq;
2979 netdev_dbg(adapter->netdev,
2980 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2981 (unsigned long int)cpu_to_be64(remote_handle),
2982 (unsigned long int)cpu_to_be64(u64_crq[0]),
2983 (unsigned long int)cpu_to_be64(u64_crq[1]),
2984 (unsigned long int)cpu_to_be64(u64_crq[2]),
2985 (unsigned long int)cpu_to_be64(u64_crq[3]));
2987 /* Make sure the hypervisor sees the complete request */
2990 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2991 cpu_to_be64(remote_handle),
2992 cpu_to_be64(u64_crq[0]),
2993 cpu_to_be64(u64_crq[1]),
2994 cpu_to_be64(u64_crq[2]),
2995 cpu_to_be64(u64_crq[3]));
2999 dev_warn(dev, "CRQ Queue closed\n");
3000 dev_err(dev, "Send error (rc=%d)\n", rc);
3006 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3007 u64 remote_handle, u64 ioba, u64 num_entries)
3009 unsigned int ua = adapter->vdev->unit_address;
3010 struct device *dev = &adapter->vdev->dev;
3013 /* Make sure the hypervisor sees the complete request */
3015 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3016 cpu_to_be64(remote_handle),
3021 dev_warn(dev, "CRQ Queue closed\n");
3022 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3028 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3029 union ibmvnic_crq *crq)
3031 unsigned int ua = adapter->vdev->unit_address;
3032 struct device *dev = &adapter->vdev->dev;
3033 u64 *u64_crq = (u64 *)crq;
3036 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3037 (unsigned long int)cpu_to_be64(u64_crq[0]),
3038 (unsigned long int)cpu_to_be64(u64_crq[1]));
3040 /* Make sure the hypervisor sees the complete request */
3043 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3044 cpu_to_be64(u64_crq[0]),
3045 cpu_to_be64(u64_crq[1]));
3048 if (rc == H_CLOSED) {
3049 dev_warn(dev, "CRQ Queue closed\n");
3050 if (adapter->resetting)
3051 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3054 dev_warn(dev, "Send error (rc=%d)\n", rc);
3060 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3062 union ibmvnic_crq crq;
3064 memset(&crq, 0, sizeof(crq));
3065 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3066 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3067 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3069 return ibmvnic_send_crq(adapter, &crq);
3072 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3074 union ibmvnic_crq crq;
3076 memset(&crq, 0, sizeof(crq));
3077 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3078 crq.version_exchange.cmd = VERSION_EXCHANGE;
3079 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3081 return ibmvnic_send_crq(adapter, &crq);
3084 struct vnic_login_client_data {
3090 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3094 /* Calculate the amount of buffer space needed for the
3095 * vnic client data in the login buffer. There are four entries,
3096 * OS name, LPAR name, device name, and a null last entry.
3098 len = 4 * sizeof(struct vnic_login_client_data);
3099 len += 6; /* "Linux" plus NULL */
3100 len += strlen(utsname()->nodename) + 1;
3101 len += strlen(adapter->netdev->name) + 1;
3106 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3107 struct vnic_login_client_data *vlcd)
3109 const char *os_name = "Linux";
3112 /* Type 1 - LPAR OS */
3114 len = strlen(os_name) + 1;
3115 vlcd->len = cpu_to_be16(len);
3116 strncpy(&vlcd->name, os_name, len);
3117 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3119 /* Type 2 - LPAR name */
3121 len = strlen(utsname()->nodename) + 1;
3122 vlcd->len = cpu_to_be16(len);
3123 strncpy(&vlcd->name, utsname()->nodename, len);
3124 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3126 /* Type 3 - device name */
3128 len = strlen(adapter->netdev->name) + 1;
3129 vlcd->len = cpu_to_be16(len);
3130 strncpy(&vlcd->name, adapter->netdev->name, len);
3133 static int send_login(struct ibmvnic_adapter *adapter)
3135 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3136 struct ibmvnic_login_buffer *login_buffer;
3137 struct device *dev = &adapter->vdev->dev;
3138 dma_addr_t rsp_buffer_token;
3139 dma_addr_t buffer_token;
3140 size_t rsp_buffer_size;
3141 union ibmvnic_crq crq;
3145 int client_data_len;
3146 struct vnic_login_client_data *vlcd;
3149 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3150 netdev_err(adapter->netdev,
3151 "RX or TX queues are not allocated, device login failed\n");
3155 release_login_rsp_buffer(adapter);
3156 client_data_len = vnic_client_data_len(adapter);
3159 sizeof(struct ibmvnic_login_buffer) +
3160 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3163 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3165 goto buf_alloc_failed;
3167 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3169 if (dma_mapping_error(dev, buffer_token)) {
3170 dev_err(dev, "Couldn't map login buffer\n");
3171 goto buf_map_failed;
3174 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3175 sizeof(u64) * adapter->req_tx_queues +
3176 sizeof(u64) * adapter->req_rx_queues +
3177 sizeof(u64) * adapter->req_rx_queues +
3178 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3180 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3181 if (!login_rsp_buffer)
3182 goto buf_rsp_alloc_failed;
3184 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3185 rsp_buffer_size, DMA_FROM_DEVICE);
3186 if (dma_mapping_error(dev, rsp_buffer_token)) {
3187 dev_err(dev, "Couldn't map login rsp buffer\n");
3188 goto buf_rsp_map_failed;
3191 adapter->login_buf = login_buffer;
3192 adapter->login_buf_token = buffer_token;
3193 adapter->login_buf_sz = buffer_size;
3194 adapter->login_rsp_buf = login_rsp_buffer;
3195 adapter->login_rsp_buf_token = rsp_buffer_token;
3196 adapter->login_rsp_buf_sz = rsp_buffer_size;
3198 login_buffer->len = cpu_to_be32(buffer_size);
3199 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3200 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3201 login_buffer->off_txcomp_subcrqs =
3202 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3203 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3204 login_buffer->off_rxcomp_subcrqs =
3205 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3206 sizeof(u64) * adapter->req_tx_queues);
3207 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3208 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3210 tx_list_p = (__be64 *)((char *)login_buffer +
3211 sizeof(struct ibmvnic_login_buffer));
3212 rx_list_p = (__be64 *)((char *)login_buffer +
3213 sizeof(struct ibmvnic_login_buffer) +
3214 sizeof(u64) * adapter->req_tx_queues);
3216 for (i = 0; i < adapter->req_tx_queues; i++) {
3217 if (adapter->tx_scrq[i]) {
3218 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3223 for (i = 0; i < adapter->req_rx_queues; i++) {
3224 if (adapter->rx_scrq[i]) {
3225 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3230 /* Insert vNIC login client data */
3231 vlcd = (struct vnic_login_client_data *)
3232 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3233 login_buffer->client_data_offset =
3234 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3235 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3237 vnic_add_client_data(adapter, vlcd);
3239 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3240 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3241 netdev_dbg(adapter->netdev, "%016lx\n",
3242 ((unsigned long int *)(adapter->login_buf))[i]);
3245 memset(&crq, 0, sizeof(crq));
3246 crq.login.first = IBMVNIC_CRQ_CMD;
3247 crq.login.cmd = LOGIN;
3248 crq.login.ioba = cpu_to_be32(buffer_token);
3249 crq.login.len = cpu_to_be32(buffer_size);
3250 ibmvnic_send_crq(adapter, &crq);
3255 kfree(login_rsp_buffer);
3256 buf_rsp_alloc_failed:
3257 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3259 kfree(login_buffer);
3264 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3267 union ibmvnic_crq crq;
3269 memset(&crq, 0, sizeof(crq));
3270 crq.request_map.first = IBMVNIC_CRQ_CMD;
3271 crq.request_map.cmd = REQUEST_MAP;
3272 crq.request_map.map_id = map_id;
3273 crq.request_map.ioba = cpu_to_be32(addr);
3274 crq.request_map.len = cpu_to_be32(len);
3275 ibmvnic_send_crq(adapter, &crq);
3278 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3280 union ibmvnic_crq crq;
3282 memset(&crq, 0, sizeof(crq));
3283 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3284 crq.request_unmap.cmd = REQUEST_UNMAP;
3285 crq.request_unmap.map_id = map_id;
3286 ibmvnic_send_crq(adapter, &crq);
3289 static void send_map_query(struct ibmvnic_adapter *adapter)
3291 union ibmvnic_crq crq;
3293 memset(&crq, 0, sizeof(crq));
3294 crq.query_map.first = IBMVNIC_CRQ_CMD;
3295 crq.query_map.cmd = QUERY_MAP;
3296 ibmvnic_send_crq(adapter, &crq);
3299 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3300 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3302 union ibmvnic_crq crq;
3304 atomic_set(&adapter->running_cap_crqs, 0);
3305 memset(&crq, 0, sizeof(crq));
3306 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3307 crq.query_capability.cmd = QUERY_CAPABILITY;
3309 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3310 atomic_inc(&adapter->running_cap_crqs);
3311 ibmvnic_send_crq(adapter, &crq);
3313 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3314 atomic_inc(&adapter->running_cap_crqs);
3315 ibmvnic_send_crq(adapter, &crq);
3317 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3318 atomic_inc(&adapter->running_cap_crqs);
3319 ibmvnic_send_crq(adapter, &crq);
3321 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3322 atomic_inc(&adapter->running_cap_crqs);
3323 ibmvnic_send_crq(adapter, &crq);
3325 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3326 atomic_inc(&adapter->running_cap_crqs);
3327 ibmvnic_send_crq(adapter, &crq);
3329 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3330 atomic_inc(&adapter->running_cap_crqs);
3331 ibmvnic_send_crq(adapter, &crq);
3333 crq.query_capability.capability =
3334 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3335 atomic_inc(&adapter->running_cap_crqs);
3336 ibmvnic_send_crq(adapter, &crq);
3338 crq.query_capability.capability =
3339 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3340 atomic_inc(&adapter->running_cap_crqs);
3341 ibmvnic_send_crq(adapter, &crq);
3343 crq.query_capability.capability =
3344 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3345 atomic_inc(&adapter->running_cap_crqs);
3346 ibmvnic_send_crq(adapter, &crq);
3348 crq.query_capability.capability =
3349 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3350 atomic_inc(&adapter->running_cap_crqs);
3351 ibmvnic_send_crq(adapter, &crq);
3353 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3354 atomic_inc(&adapter->running_cap_crqs);
3355 ibmvnic_send_crq(adapter, &crq);
3357 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3358 atomic_inc(&adapter->running_cap_crqs);
3359 ibmvnic_send_crq(adapter, &crq);
3361 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3362 atomic_inc(&adapter->running_cap_crqs);
3363 ibmvnic_send_crq(adapter, &crq);
3365 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3366 atomic_inc(&adapter->running_cap_crqs);
3367 ibmvnic_send_crq(adapter, &crq);
3369 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3370 atomic_inc(&adapter->running_cap_crqs);
3371 ibmvnic_send_crq(adapter, &crq);
3373 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3374 atomic_inc(&adapter->running_cap_crqs);
3375 ibmvnic_send_crq(adapter, &crq);
3377 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3378 atomic_inc(&adapter->running_cap_crqs);
3379 ibmvnic_send_crq(adapter, &crq);
3381 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3382 atomic_inc(&adapter->running_cap_crqs);
3383 ibmvnic_send_crq(adapter, &crq);
3385 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3386 atomic_inc(&adapter->running_cap_crqs);
3387 ibmvnic_send_crq(adapter, &crq);
3389 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3390 atomic_inc(&adapter->running_cap_crqs);
3391 ibmvnic_send_crq(adapter, &crq);
3393 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3394 atomic_inc(&adapter->running_cap_crqs);
3395 ibmvnic_send_crq(adapter, &crq);
3397 crq.query_capability.capability =
3398 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3399 atomic_inc(&adapter->running_cap_crqs);
3400 ibmvnic_send_crq(adapter, &crq);
3402 crq.query_capability.capability =
3403 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3404 atomic_inc(&adapter->running_cap_crqs);
3405 ibmvnic_send_crq(adapter, &crq);
3407 crq.query_capability.capability =
3408 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3409 atomic_inc(&adapter->running_cap_crqs);
3410 ibmvnic_send_crq(adapter, &crq);
3412 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3413 atomic_inc(&adapter->running_cap_crqs);
3414 ibmvnic_send_crq(adapter, &crq);
3417 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3418 struct ibmvnic_adapter *adapter)
3420 struct device *dev = &adapter->vdev->dev;
3422 if (crq->get_vpd_size_rsp.rc.code) {
3423 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3424 crq->get_vpd_size_rsp.rc.code);
3425 complete(&adapter->fw_done);
3429 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3430 complete(&adapter->fw_done);
3433 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3434 struct ibmvnic_adapter *adapter)
3436 struct device *dev = &adapter->vdev->dev;
3437 unsigned char *substr = NULL;
3438 u8 fw_level_len = 0;
3440 memset(adapter->fw_version, 0, 32);
3442 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3445 if (crq->get_vpd_rsp.rc.code) {
3446 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3447 crq->get_vpd_rsp.rc.code);
3451 /* get the position of the firmware version info
3452 * located after the ASCII 'RM' substring in the buffer
3454 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3456 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3460 /* get length of firmware level ASCII substring */
3461 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3462 fw_level_len = *(substr + 2);
3464 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3468 /* copy firmware version string from vpd into adapter */
3469 if ((substr + 3 + fw_level_len) <
3470 (adapter->vpd->buff + adapter->vpd->len)) {
3471 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3473 dev_info(dev, "FW substr extrapolated VPD buff\n");
3477 if (adapter->fw_version[0] == '\0')
3478 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3479 complete(&adapter->fw_done);
3482 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3484 struct device *dev = &adapter->vdev->dev;
3485 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3486 union ibmvnic_crq crq;
3489 dma_unmap_single(dev, adapter->ip_offload_tok,
3490 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3492 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3493 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3494 netdev_dbg(adapter->netdev, "%016lx\n",
3495 ((unsigned long int *)(buf))[i]);
3497 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3498 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3499 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3500 buf->tcp_ipv4_chksum);
3501 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3502 buf->tcp_ipv6_chksum);
3503 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3504 buf->udp_ipv4_chksum);
3505 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3506 buf->udp_ipv6_chksum);
3507 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3508 buf->large_tx_ipv4);
3509 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3510 buf->large_tx_ipv6);
3511 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3512 buf->large_rx_ipv4);
3513 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3514 buf->large_rx_ipv6);
3515 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3516 buf->max_ipv4_header_size);
3517 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3518 buf->max_ipv6_header_size);
3519 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3520 buf->max_tcp_header_size);
3521 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3522 buf->max_udp_header_size);
3523 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3524 buf->max_large_tx_size);
3525 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3526 buf->max_large_rx_size);
3527 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3528 buf->ipv6_extension_header);
3529 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3530 buf->tcp_pseudosum_req);
3531 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3532 buf->num_ipv6_ext_headers);
3533 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3534 buf->off_ipv6_ext_headers);
3536 adapter->ip_offload_ctrl_tok =
3537 dma_map_single(dev, &adapter->ip_offload_ctrl,
3538 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3540 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3541 dev_err(dev, "Couldn't map ip offload control buffer\n");
3545 adapter->ip_offload_ctrl.len =
3546 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3547 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3548 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3549 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3550 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3551 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3552 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3553 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3554 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3555 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3557 /* large_rx disabled for now, additional features needed */
3558 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3559 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3561 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3563 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3564 adapter->netdev->features |= NETIF_F_IP_CSUM;
3566 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3567 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3569 if ((adapter->netdev->features &
3570 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3571 adapter->netdev->features |= NETIF_F_RXCSUM;
3573 if (buf->large_tx_ipv4)
3574 adapter->netdev->features |= NETIF_F_TSO;
3575 if (buf->large_tx_ipv6)
3576 adapter->netdev->features |= NETIF_F_TSO6;
3578 adapter->netdev->hw_features |= adapter->netdev->features;
3580 memset(&crq, 0, sizeof(crq));
3581 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3582 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3583 crq.control_ip_offload.len =
3584 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3585 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3586 ibmvnic_send_crq(adapter, &crq);
3589 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3590 struct ibmvnic_adapter *adapter)
3592 struct device *dev = &adapter->vdev->dev;
3593 struct ibmvnic_error_buff *error_buff, *tmp;
3594 unsigned long flags;
3598 if (!crq->request_error_rsp.rc.code) {
3599 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3600 crq->request_error_rsp.rc.code);
3604 spin_lock_irqsave(&adapter->error_list_lock, flags);
3605 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3606 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3608 list_del(&error_buff->list);
3611 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3614 dev_err(dev, "Couldn't find error id %x\n",
3615 be32_to_cpu(crq->request_error_rsp.error_id));
3619 dev_err(dev, "Detailed info for error id %x:",
3620 be32_to_cpu(crq->request_error_rsp.error_id));
3622 for (i = 0; i < error_buff->len; i++) {
3623 pr_cont("%02x", (int)error_buff->buff[i]);
3629 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3631 kfree(error_buff->buff);
3635 static void request_error_information(struct ibmvnic_adapter *adapter,
3636 union ibmvnic_crq *err_crq)
3638 struct device *dev = &adapter->vdev->dev;
3639 struct net_device *netdev = adapter->netdev;
3640 struct ibmvnic_error_buff *error_buff;
3641 unsigned long timeout = msecs_to_jiffies(30000);
3642 union ibmvnic_crq crq;
3643 unsigned long flags;
3646 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3650 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3651 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3652 if (!error_buff->buff) {
3657 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3659 if (dma_mapping_error(dev, error_buff->dma)) {
3660 netdev_err(netdev, "Couldn't map error buffer\n");
3661 kfree(error_buff->buff);
3666 error_buff->len = detail_len;
3667 error_buff->error_id = err_crq->error_indication.error_id;
3669 spin_lock_irqsave(&adapter->error_list_lock, flags);
3670 list_add_tail(&error_buff->list, &adapter->errors);
3671 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3673 memset(&crq, 0, sizeof(crq));
3674 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3675 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3676 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3677 crq.request_error_info.len = cpu_to_be32(detail_len);
3678 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3680 rc = ibmvnic_send_crq(adapter, &crq);
3682 netdev_err(netdev, "failed to request error information\n");
3686 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3687 netdev_err(netdev, "timeout waiting for error information\n");
3694 spin_lock_irqsave(&adapter->error_list_lock, flags);
3695 list_del(&error_buff->list);
3696 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3698 kfree(error_buff->buff);
3702 static void handle_error_indication(union ibmvnic_crq *crq,
3703 struct ibmvnic_adapter *adapter)
3705 struct device *dev = &adapter->vdev->dev;
3707 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3708 crq->error_indication.flags
3709 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3710 be32_to_cpu(crq->error_indication.error_id),
3711 be16_to_cpu(crq->error_indication.error_cause));
3713 if (be32_to_cpu(crq->error_indication.error_id))
3714 request_error_information(adapter, crq);
3716 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3717 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3719 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3722 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3723 struct ibmvnic_adapter *adapter)
3725 struct net_device *netdev = adapter->netdev;
3726 struct device *dev = &adapter->vdev->dev;
3729 rc = crq->change_mac_addr_rsp.rc.code;
3731 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3734 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3737 complete(&adapter->fw_done);
3741 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3742 struct ibmvnic_adapter *adapter)
3744 struct device *dev = &adapter->vdev->dev;
3748 atomic_dec(&adapter->running_cap_crqs);
3749 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3751 req_value = &adapter->req_tx_queues;
3755 req_value = &adapter->req_rx_queues;
3758 case REQ_RX_ADD_QUEUES:
3759 req_value = &adapter->req_rx_add_queues;
3762 case REQ_TX_ENTRIES_PER_SUBCRQ:
3763 req_value = &adapter->req_tx_entries_per_subcrq;
3764 name = "tx_entries_per_subcrq";
3766 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3767 req_value = &adapter->req_rx_add_entries_per_subcrq;
3768 name = "rx_add_entries_per_subcrq";
3771 req_value = &adapter->req_mtu;
3774 case PROMISC_REQUESTED:
3775 req_value = &adapter->promisc;
3779 dev_err(dev, "Got invalid cap request rsp %d\n",
3780 crq->request_capability.capability);
3784 switch (crq->request_capability_rsp.rc.code) {
3787 case PARTIALSUCCESS:
3788 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3790 (long int)be64_to_cpu(crq->request_capability_rsp.
3793 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3795 pr_err("mtu of %llu is not supported. Reverting.\n",
3797 *req_value = adapter->fallback.mtu;
3800 be64_to_cpu(crq->request_capability_rsp.number);
3803 ibmvnic_send_req_caps(adapter, 1);
3806 dev_err(dev, "Error %d in request cap rsp\n",
3807 crq->request_capability_rsp.rc.code);
3811 /* Done receiving requested capabilities, query IP offload support */
3812 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3813 union ibmvnic_crq newcrq;
3814 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3815 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3816 &adapter->ip_offload_buf;
3818 adapter->wait_capability = false;
3819 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3823 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3824 if (!firmware_has_feature(FW_FEATURE_CMO))
3825 dev_err(dev, "Couldn't map offload buffer\n");
3829 memset(&newcrq, 0, sizeof(newcrq));
3830 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3831 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3832 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3833 newcrq.query_ip_offload.ioba =
3834 cpu_to_be32(adapter->ip_offload_tok);
3836 ibmvnic_send_crq(adapter, &newcrq);
3840 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3841 struct ibmvnic_adapter *adapter)
3843 struct device *dev = &adapter->vdev->dev;
3844 struct net_device *netdev = adapter->netdev;
3845 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3846 struct ibmvnic_login_buffer *login = adapter->login_buf;
3849 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3851 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3852 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3854 /* If the number of queues requested can't be allocated by the
3855 * server, the login response will return with code 1. We will need
3856 * to resend the login buffer with fewer queues requested.
3858 if (login_rsp_crq->generic.rc.code) {
3859 adapter->renegotiate = true;
3860 complete(&adapter->init_done);
3864 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3866 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3867 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3868 netdev_dbg(adapter->netdev, "%016lx\n",
3869 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3873 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3874 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3875 adapter->req_rx_add_queues !=
3876 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3877 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3878 ibmvnic_remove(adapter->vdev);
3881 release_login_buffer(adapter);
3882 complete(&adapter->init_done);
3887 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3888 struct ibmvnic_adapter *adapter)
3890 struct device *dev = &adapter->vdev->dev;
3893 rc = crq->request_unmap_rsp.rc.code;
3895 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3898 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3899 struct ibmvnic_adapter *adapter)
3901 struct net_device *netdev = adapter->netdev;
3902 struct device *dev = &adapter->vdev->dev;
3905 rc = crq->query_map_rsp.rc.code;
3907 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3910 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3911 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3912 crq->query_map_rsp.free_pages);
3915 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3916 struct ibmvnic_adapter *adapter)
3918 struct net_device *netdev = adapter->netdev;
3919 struct device *dev = &adapter->vdev->dev;
3922 atomic_dec(&adapter->running_cap_crqs);
3923 netdev_dbg(netdev, "Outstanding queries: %d\n",
3924 atomic_read(&adapter->running_cap_crqs));
3925 rc = crq->query_capability.rc.code;
3927 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3931 switch (be16_to_cpu(crq->query_capability.capability)) {
3933 adapter->min_tx_queues =
3934 be64_to_cpu(crq->query_capability.number);
3935 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3936 adapter->min_tx_queues);
3939 adapter->min_rx_queues =
3940 be64_to_cpu(crq->query_capability.number);
3941 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3942 adapter->min_rx_queues);
3944 case MIN_RX_ADD_QUEUES:
3945 adapter->min_rx_add_queues =
3946 be64_to_cpu(crq->query_capability.number);
3947 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3948 adapter->min_rx_add_queues);
3951 adapter->max_tx_queues =
3952 be64_to_cpu(crq->query_capability.number);
3953 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3954 adapter->max_tx_queues);
3957 adapter->max_rx_queues =
3958 be64_to_cpu(crq->query_capability.number);
3959 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3960 adapter->max_rx_queues);
3962 case MAX_RX_ADD_QUEUES:
3963 adapter->max_rx_add_queues =
3964 be64_to_cpu(crq->query_capability.number);
3965 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3966 adapter->max_rx_add_queues);
3968 case MIN_TX_ENTRIES_PER_SUBCRQ:
3969 adapter->min_tx_entries_per_subcrq =
3970 be64_to_cpu(crq->query_capability.number);
3971 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3972 adapter->min_tx_entries_per_subcrq);
3974 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3975 adapter->min_rx_add_entries_per_subcrq =
3976 be64_to_cpu(crq->query_capability.number);
3977 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3978 adapter->min_rx_add_entries_per_subcrq);
3980 case MAX_TX_ENTRIES_PER_SUBCRQ:
3981 adapter->max_tx_entries_per_subcrq =
3982 be64_to_cpu(crq->query_capability.number);
3983 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3984 adapter->max_tx_entries_per_subcrq);
3986 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3987 adapter->max_rx_add_entries_per_subcrq =
3988 be64_to_cpu(crq->query_capability.number);
3989 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3990 adapter->max_rx_add_entries_per_subcrq);
3992 case TCP_IP_OFFLOAD:
3993 adapter->tcp_ip_offload =
3994 be64_to_cpu(crq->query_capability.number);
3995 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3996 adapter->tcp_ip_offload);
3998 case PROMISC_SUPPORTED:
3999 adapter->promisc_supported =
4000 be64_to_cpu(crq->query_capability.number);
4001 netdev_dbg(netdev, "promisc_supported = %lld\n",
4002 adapter->promisc_supported);
4005 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4006 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4007 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4010 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4011 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4012 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4014 case MAX_MULTICAST_FILTERS:
4015 adapter->max_multicast_filters =
4016 be64_to_cpu(crq->query_capability.number);
4017 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4018 adapter->max_multicast_filters);
4020 case VLAN_HEADER_INSERTION:
4021 adapter->vlan_header_insertion =
4022 be64_to_cpu(crq->query_capability.number);
4023 if (adapter->vlan_header_insertion)
4024 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4025 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4026 adapter->vlan_header_insertion);
4028 case RX_VLAN_HEADER_INSERTION:
4029 adapter->rx_vlan_header_insertion =
4030 be64_to_cpu(crq->query_capability.number);
4031 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4032 adapter->rx_vlan_header_insertion);
4034 case MAX_TX_SG_ENTRIES:
4035 adapter->max_tx_sg_entries =
4036 be64_to_cpu(crq->query_capability.number);
4037 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4038 adapter->max_tx_sg_entries);
4040 case RX_SG_SUPPORTED:
4041 adapter->rx_sg_supported =
4042 be64_to_cpu(crq->query_capability.number);
4043 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4044 adapter->rx_sg_supported);
4046 case OPT_TX_COMP_SUB_QUEUES:
4047 adapter->opt_tx_comp_sub_queues =
4048 be64_to_cpu(crq->query_capability.number);
4049 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4050 adapter->opt_tx_comp_sub_queues);
4052 case OPT_RX_COMP_QUEUES:
4053 adapter->opt_rx_comp_queues =
4054 be64_to_cpu(crq->query_capability.number);
4055 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4056 adapter->opt_rx_comp_queues);
4058 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4059 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4060 be64_to_cpu(crq->query_capability.number);
4061 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4062 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4064 case OPT_TX_ENTRIES_PER_SUBCRQ:
4065 adapter->opt_tx_entries_per_subcrq =
4066 be64_to_cpu(crq->query_capability.number);
4067 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4068 adapter->opt_tx_entries_per_subcrq);
4070 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4071 adapter->opt_rxba_entries_per_subcrq =
4072 be64_to_cpu(crq->query_capability.number);
4073 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4074 adapter->opt_rxba_entries_per_subcrq);
4076 case TX_RX_DESC_REQ:
4077 adapter->tx_rx_desc_req = crq->query_capability.number;
4078 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4079 adapter->tx_rx_desc_req);
4083 netdev_err(netdev, "Got invalid cap rsp %d\n",
4084 crq->query_capability.capability);
4088 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4089 adapter->wait_capability = false;
4090 ibmvnic_send_req_caps(adapter, 0);
4094 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4095 struct ibmvnic_adapter *adapter)
4097 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4098 struct net_device *netdev = adapter->netdev;
4099 struct device *dev = &adapter->vdev->dev;
4100 u64 *u64_crq = (u64 *)crq;
4103 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4104 (unsigned long int)cpu_to_be64(u64_crq[0]),
4105 (unsigned long int)cpu_to_be64(u64_crq[1]));
4106 switch (gen_crq->first) {
4107 case IBMVNIC_CRQ_INIT_RSP:
4108 switch (gen_crq->cmd) {
4109 case IBMVNIC_CRQ_INIT:
4110 dev_info(dev, "Partner initialized\n");
4111 adapter->from_passive_init = true;
4112 complete(&adapter->init_done);
4114 case IBMVNIC_CRQ_INIT_COMPLETE:
4115 dev_info(dev, "Partner initialization complete\n");
4116 send_version_xchg(adapter);
4119 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4122 case IBMVNIC_CRQ_XPORT_EVENT:
4123 netif_carrier_off(netdev);
4124 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4125 dev_info(dev, "Migrated, re-enabling adapter\n");
4126 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4127 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4128 dev_info(dev, "Backing device failover detected\n");
4129 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4131 /* The adapter lost the connection */
4132 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4134 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4137 case IBMVNIC_CRQ_CMD_RSP:
4140 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4145 switch (gen_crq->cmd) {
4146 case VERSION_EXCHANGE_RSP:
4147 rc = crq->version_exchange_rsp.rc.code;
4149 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4152 dev_info(dev, "Partner protocol version is %d\n",
4153 crq->version_exchange_rsp.version);
4154 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4157 be16_to_cpu(crq->version_exchange_rsp.version);
4158 send_cap_queries(adapter);
4160 case QUERY_CAPABILITY_RSP:
4161 handle_query_cap_rsp(crq, adapter);
4164 handle_query_map_rsp(crq, adapter);
4166 case REQUEST_MAP_RSP:
4167 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4168 complete(&adapter->fw_done);
4170 case REQUEST_UNMAP_RSP:
4171 handle_request_unmap_rsp(crq, adapter);
4173 case REQUEST_CAPABILITY_RSP:
4174 handle_request_cap_rsp(crq, adapter);
4177 netdev_dbg(netdev, "Got Login Response\n");
4178 handle_login_rsp(crq, adapter);
4180 case LOGICAL_LINK_STATE_RSP:
4182 "Got Logical Link State Response, state: %d rc: %d\n",
4183 crq->logical_link_state_rsp.link_state,
4184 crq->logical_link_state_rsp.rc.code);
4185 adapter->logical_link_state =
4186 crq->logical_link_state_rsp.link_state;
4187 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4188 complete(&adapter->init_done);
4190 case LINK_STATE_INDICATION:
4191 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4192 adapter->phys_link_state =
4193 crq->link_state_indication.phys_link_state;
4194 adapter->logical_link_state =
4195 crq->link_state_indication.logical_link_state;
4197 case CHANGE_MAC_ADDR_RSP:
4198 netdev_dbg(netdev, "Got MAC address change Response\n");
4199 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4201 case ERROR_INDICATION:
4202 netdev_dbg(netdev, "Got Error Indication\n");
4203 handle_error_indication(crq, adapter);
4205 case REQUEST_ERROR_RSP:
4206 netdev_dbg(netdev, "Got Error Detail Response\n");
4207 handle_error_info_rsp(crq, adapter);
4209 case REQUEST_STATISTICS_RSP:
4210 netdev_dbg(netdev, "Got Statistics Response\n");
4211 complete(&adapter->stats_done);
4213 case QUERY_IP_OFFLOAD_RSP:
4214 netdev_dbg(netdev, "Got Query IP offload Response\n");
4215 handle_query_ip_offload_rsp(adapter);
4217 case MULTICAST_CTRL_RSP:
4218 netdev_dbg(netdev, "Got multicast control Response\n");
4220 case CONTROL_IP_OFFLOAD_RSP:
4221 netdev_dbg(netdev, "Got Control IP offload Response\n");
4222 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4223 sizeof(adapter->ip_offload_ctrl),
4225 complete(&adapter->init_done);
4227 case COLLECT_FW_TRACE_RSP:
4228 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4229 complete(&adapter->fw_done);
4231 case GET_VPD_SIZE_RSP:
4232 handle_vpd_size_rsp(crq, adapter);
4235 handle_vpd_rsp(crq, adapter);
4238 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4243 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4245 struct ibmvnic_adapter *adapter = instance;
4247 tasklet_schedule(&adapter->tasklet);
4251 static void ibmvnic_tasklet(void *data)
4253 struct ibmvnic_adapter *adapter = data;
4254 struct ibmvnic_crq_queue *queue = &adapter->crq;
4255 union ibmvnic_crq *crq;
4256 unsigned long flags;
4259 spin_lock_irqsave(&queue->lock, flags);
4261 /* Pull all the valid messages off the CRQ */
4262 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4263 ibmvnic_handle_crq(crq, adapter);
4264 crq->generic.first = 0;
4267 /* remain in tasklet until all
4268 * capabilities responses are received
4270 if (!adapter->wait_capability)
4273 /* if capabilities CRQ's were sent in this tasklet, the following
4274 * tasklet must wait until all responses are received
4276 if (atomic_read(&adapter->running_cap_crqs) != 0)
4277 adapter->wait_capability = true;
4278 spin_unlock_irqrestore(&queue->lock, flags);
4281 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4283 struct vio_dev *vdev = adapter->vdev;
4287 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4288 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4291 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4296 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4298 struct ibmvnic_crq_queue *crq = &adapter->crq;
4299 struct device *dev = &adapter->vdev->dev;
4300 struct vio_dev *vdev = adapter->vdev;
4305 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4306 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4308 /* Clean out the queue */
4309 memset(crq->msgs, 0, PAGE_SIZE);
4312 /* And re-open it again */
4313 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4314 crq->msg_token, PAGE_SIZE);
4317 /* Adapter is good, but other end is not ready */
4318 dev_warn(dev, "Partner adapter not ready\n");
4320 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4325 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4327 struct ibmvnic_crq_queue *crq = &adapter->crq;
4328 struct vio_dev *vdev = adapter->vdev;
4334 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4335 free_irq(vdev->irq, adapter);
4336 tasklet_kill(&adapter->tasklet);
4338 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4339 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4341 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4343 free_page((unsigned long)crq->msgs);
4347 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4349 struct ibmvnic_crq_queue *crq = &adapter->crq;
4350 struct device *dev = &adapter->vdev->dev;
4351 struct vio_dev *vdev = adapter->vdev;
4352 int rc, retrc = -ENOMEM;
4357 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4358 /* Should we allocate more than one page? */
4363 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4364 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4366 if (dma_mapping_error(dev, crq->msg_token))
4369 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4370 crq->msg_token, PAGE_SIZE);
4372 if (rc == H_RESOURCE)
4373 /* maybe kexecing and resource is busy. try a reset */
4374 rc = ibmvnic_reset_crq(adapter);
4377 if (rc == H_CLOSED) {
4378 dev_warn(dev, "Partner adapter not ready\n");
4380 dev_warn(dev, "Error %d opening adapter\n", rc);
4381 goto reg_crq_failed;
4386 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4387 (unsigned long)adapter);
4389 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4390 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4393 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4395 goto req_irq_failed;
4398 rc = vio_enable_interrupts(vdev);
4400 dev_err(dev, "Error %d enabling interrupts\n", rc);
4401 goto req_irq_failed;
4405 spin_lock_init(&crq->lock);
4410 tasklet_kill(&adapter->tasklet);
4412 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4413 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4415 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4417 free_page((unsigned long)crq->msgs);
4422 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4424 struct device *dev = &adapter->vdev->dev;
4425 unsigned long timeout = msecs_to_jiffies(30000);
4426 u64 old_num_rx_queues, old_num_tx_queues;
4429 if (adapter->resetting && !adapter->wait_for_reset) {
4430 rc = ibmvnic_reset_crq(adapter);
4432 rc = vio_enable_interrupts(adapter->vdev);
4434 rc = init_crq_queue(adapter);
4438 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4442 adapter->from_passive_init = false;
4444 old_num_rx_queues = adapter->req_rx_queues;
4445 old_num_tx_queues = adapter->req_tx_queues;
4447 init_completion(&adapter->init_done);
4448 adapter->init_done_rc = 0;
4449 ibmvnic_send_crq_init(adapter);
4450 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4451 dev_err(dev, "Initialization sequence timed out\n");
4455 if (adapter->init_done_rc) {
4456 release_crq_queue(adapter);
4457 return adapter->init_done_rc;
4460 if (adapter->from_passive_init) {
4461 adapter->state = VNIC_OPEN;
4462 adapter->from_passive_init = false;
4466 if (adapter->resetting && !adapter->wait_for_reset) {
4467 if (adapter->req_rx_queues != old_num_rx_queues ||
4468 adapter->req_tx_queues != old_num_tx_queues) {
4469 release_sub_crqs(adapter, 0);
4470 rc = init_sub_crqs(adapter);
4472 rc = reset_sub_crq_queues(adapter);
4475 rc = init_sub_crqs(adapter);
4479 dev_err(dev, "Initialization of sub crqs failed\n");
4480 release_crq_queue(adapter);
4484 rc = init_sub_crq_irqs(adapter);
4486 dev_err(dev, "Failed to initialize sub crq irqs\n");
4487 release_crq_queue(adapter);
4490 rc = init_stats_buffers(adapter);
4494 rc = init_stats_token(adapter);
4501 static struct device_attribute dev_attr_failover;
4503 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4505 struct ibmvnic_adapter *adapter;
4506 struct net_device *netdev;
4507 unsigned char *mac_addr_p;
4510 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4513 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4514 VETH_MAC_ADDR, NULL);
4517 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4518 __FILE__, __LINE__);
4522 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4523 IBMVNIC_MAX_QUEUES);
4527 adapter = netdev_priv(netdev);
4528 adapter->state = VNIC_PROBING;
4529 dev_set_drvdata(&dev->dev, netdev);
4530 adapter->vdev = dev;
4531 adapter->netdev = netdev;
4533 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4534 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4535 netdev->irq = dev->irq;
4536 netdev->netdev_ops = &ibmvnic_netdev_ops;
4537 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4538 SET_NETDEV_DEV(netdev, &dev->dev);
4540 spin_lock_init(&adapter->stats_lock);
4542 INIT_LIST_HEAD(&adapter->errors);
4543 spin_lock_init(&adapter->error_list_lock);
4545 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4546 INIT_LIST_HEAD(&adapter->rwi_list);
4547 mutex_init(&adapter->reset_lock);
4548 mutex_init(&adapter->rwi_lock);
4549 adapter->resetting = false;
4551 adapter->mac_change_pending = false;
4554 rc = ibmvnic_init(adapter);
4555 if (rc && rc != EAGAIN)
4556 goto ibmvnic_init_fail;
4557 } while (rc == EAGAIN);
4559 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4560 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4561 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4563 rc = device_create_file(&dev->dev, &dev_attr_failover);
4565 goto ibmvnic_init_fail;
4567 netif_carrier_off(netdev);
4568 rc = register_netdev(netdev);
4570 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4571 goto ibmvnic_register_fail;
4573 dev_info(&dev->dev, "ibmvnic registered\n");
4575 adapter->state = VNIC_PROBED;
4577 adapter->wait_for_reset = false;
4581 ibmvnic_register_fail:
4582 device_remove_file(&dev->dev, &dev_attr_failover);
4585 release_sub_crqs(adapter, 1);
4586 release_crq_queue(adapter);
4587 free_netdev(netdev);
4592 static int ibmvnic_remove(struct vio_dev *dev)
4594 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4595 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4597 adapter->state = VNIC_REMOVING;
4598 unregister_netdev(netdev);
4599 mutex_lock(&adapter->reset_lock);
4601 release_resources(adapter);
4602 release_sub_crqs(adapter, 1);
4603 release_crq_queue(adapter);
4605 release_stats_token(adapter);
4606 release_stats_buffers(adapter);
4608 adapter->state = VNIC_REMOVED;
4610 mutex_unlock(&adapter->reset_lock);
4611 device_remove_file(&dev->dev, &dev_attr_failover);
4612 free_netdev(netdev);
4613 dev_set_drvdata(&dev->dev, NULL);
4618 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4619 const char *buf, size_t count)
4621 struct net_device *netdev = dev_get_drvdata(dev);
4622 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4623 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4624 __be64 session_token;
4627 if (!sysfs_streq(buf, "1"))
4630 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4631 H_GET_SESSION_TOKEN, 0, 0, 0);
4633 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4638 session_token = (__be64)retbuf[0];
4639 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4640 be64_to_cpu(session_token));
4641 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4642 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4644 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4652 static DEVICE_ATTR_WO(failover);
4654 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4656 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4657 struct ibmvnic_adapter *adapter;
4658 struct iommu_table *tbl;
4659 unsigned long ret = 0;
4662 tbl = get_iommu_table_base(&vdev->dev);
4664 /* netdev inits at probe time along with the structures we need below*/
4666 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4668 adapter = netdev_priv(netdev);
4670 ret += PAGE_SIZE; /* the crq message queue */
4671 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4673 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4674 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4676 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4678 ret += adapter->rx_pool[i].size *
4679 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4684 static int ibmvnic_resume(struct device *dev)
4686 struct net_device *netdev = dev_get_drvdata(dev);
4687 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4689 if (adapter->state != VNIC_OPEN)
4692 tasklet_schedule(&adapter->tasklet);
4697 static const struct vio_device_id ibmvnic_device_table[] = {
4698 {"network", "IBM,vnic"},
4701 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4703 static const struct dev_pm_ops ibmvnic_pm_ops = {
4704 .resume = ibmvnic_resume
4707 static struct vio_driver ibmvnic_driver = {
4708 .id_table = ibmvnic_device_table,
4709 .probe = ibmvnic_probe,
4710 .remove = ibmvnic_remove,
4711 .get_desired_dma = ibmvnic_get_desired_dma,
4712 .name = ibmvnic_driver_name,
4713 .pm = &ibmvnic_pm_ops,
4716 /* module functions */
4717 static int __init ibmvnic_module_init(void)
4719 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4720 IBMVNIC_DRIVER_VERSION);
4722 return vio_register_driver(&ibmvnic_driver);
4725 static void __exit ibmvnic_module_exit(void)
4727 vio_unregister_driver(&ibmvnic_driver);
4730 module_init(ibmvnic_module_init);
4731 module_exit(ibmvnic_module_exit);