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 *);
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 void 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(adapter->req_tx_queues,
365 sizeof(struct ibmvnic_tx_queue_stats),
367 if (!adapter->tx_stats_buffers)
370 adapter->rx_stats_buffers =
371 kcalloc(adapter->req_rx_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 = 0;
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;
557 adapter->num_active_rx_pools = rxadd_subcrqs;
562 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
564 struct ibmvnic_tx_pool *tx_pool;
568 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
569 for (i = 0; i < tx_scrqs; i++) {
570 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
572 tx_pool = &adapter->tx_pool[i];
574 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
578 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
582 memset(tx_pool->tx_buff, 0,
583 adapter->req_tx_entries_per_subcrq *
584 sizeof(struct ibmvnic_tx_buff));
586 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
587 tx_pool->free_map[j] = j;
589 tx_pool->consumer_index = 0;
590 tx_pool->producer_index = 0;
591 tx_pool->tso_index = 0;
597 static void release_vpd_data(struct ibmvnic_adapter *adapter)
602 kfree(adapter->vpd->buff);
608 static void release_tx_pools(struct ibmvnic_adapter *adapter)
610 struct ibmvnic_tx_pool *tx_pool;
613 if (!adapter->tx_pool)
616 for (i = 0; i < adapter->num_active_tx_pools; i++) {
617 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
618 tx_pool = &adapter->tx_pool[i];
619 kfree(tx_pool->tx_buff);
620 free_long_term_buff(adapter, &tx_pool->long_term_buff);
621 free_long_term_buff(adapter, &tx_pool->tso_ltb);
622 kfree(tx_pool->free_map);
625 kfree(adapter->tx_pool);
626 adapter->tx_pool = NULL;
627 adapter->num_active_tx_pools = 0;
630 static int init_tx_pools(struct net_device *netdev)
632 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
633 struct device *dev = &adapter->vdev->dev;
634 struct ibmvnic_tx_pool *tx_pool;
638 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
639 adapter->tx_pool = kcalloc(tx_subcrqs,
640 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
641 if (!adapter->tx_pool)
644 adapter->num_active_tx_pools = 0;
646 for (i = 0; i < tx_subcrqs; i++) {
647 tx_pool = &adapter->tx_pool[i];
649 netdev_dbg(adapter->netdev,
650 "Initializing tx_pool[%d], %lld buffs\n",
651 i, adapter->req_tx_entries_per_subcrq);
653 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
654 sizeof(struct ibmvnic_tx_buff),
656 if (!tx_pool->tx_buff) {
657 dev_err(dev, "tx pool buffer allocation failed\n");
658 release_tx_pools(adapter);
662 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
663 adapter->req_tx_entries_per_subcrq *
665 release_tx_pools(adapter);
670 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
672 IBMVNIC_TSO_BUF_SZ)) {
673 release_tx_pools(adapter);
677 tx_pool->tso_index = 0;
679 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
680 sizeof(int), GFP_KERNEL);
681 if (!tx_pool->free_map) {
682 release_tx_pools(adapter);
686 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
687 tx_pool->free_map[j] = j;
689 tx_pool->consumer_index = 0;
690 tx_pool->producer_index = 0;
693 adapter->num_active_tx_pools = tx_subcrqs;
698 static void release_error_buffers(struct ibmvnic_adapter *adapter)
700 struct device *dev = &adapter->vdev->dev;
701 struct ibmvnic_error_buff *error_buff, *tmp;
704 spin_lock_irqsave(&adapter->error_list_lock, flags);
705 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
706 list_del(&error_buff->list);
707 dma_unmap_single(dev, error_buff->dma, error_buff->len,
709 kfree(error_buff->buff);
712 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
715 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
719 if (adapter->napi_enabled)
722 for (i = 0; i < adapter->req_rx_queues; i++)
723 napi_enable(&adapter->napi[i]);
725 adapter->napi_enabled = true;
728 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
732 if (!adapter->napi_enabled)
735 for (i = 0; i < adapter->req_rx_queues; i++) {
736 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
737 napi_disable(&adapter->napi[i]);
740 adapter->napi_enabled = false;
743 static int ibmvnic_login(struct net_device *netdev)
745 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
746 unsigned long timeout = msecs_to_jiffies(30000);
747 struct device *dev = &adapter->vdev->dev;
751 if (adapter->renegotiate) {
752 adapter->renegotiate = false;
753 release_sub_crqs(adapter);
755 reinit_completion(&adapter->init_done);
756 send_cap_queries(adapter);
757 if (!wait_for_completion_timeout(&adapter->init_done,
759 dev_err(dev, "Capabilities query timeout\n");
762 rc = init_sub_crqs(adapter);
765 "Initialization of SCRQ's failed\n");
768 rc = init_sub_crq_irqs(adapter);
771 "Initialization of SCRQ's irqs failed\n");
776 reinit_completion(&adapter->init_done);
778 if (!wait_for_completion_timeout(&adapter->init_done,
780 dev_err(dev, "Login timeout\n");
783 } while (adapter->renegotiate);
785 /* handle pending MAC address changes after successful login */
786 if (adapter->mac_change_pending) {
787 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
788 adapter->mac_change_pending = false;
794 static void release_login_buffer(struct ibmvnic_adapter *adapter)
796 kfree(adapter->login_buf);
797 adapter->login_buf = NULL;
800 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
802 kfree(adapter->login_rsp_buf);
803 adapter->login_rsp_buf = NULL;
806 static void release_resources(struct ibmvnic_adapter *adapter)
810 release_vpd_data(adapter);
812 release_tx_pools(adapter);
813 release_rx_pools(adapter);
815 release_stats_token(adapter);
816 release_stats_buffers(adapter);
817 release_error_buffers(adapter);
820 for (i = 0; i < adapter->req_rx_queues; i++) {
821 if (&adapter->napi[i]) {
822 netdev_dbg(adapter->netdev,
823 "Releasing napi[%d]\n", i);
824 netif_napi_del(&adapter->napi[i]);
829 release_login_rsp_buffer(adapter);
832 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
834 struct net_device *netdev = adapter->netdev;
835 unsigned long timeout = msecs_to_jiffies(30000);
836 union ibmvnic_crq crq;
840 netdev_dbg(netdev, "setting link state %d\n", link_state);
842 memset(&crq, 0, sizeof(crq));
843 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
844 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
845 crq.logical_link_state.link_state = link_state;
850 reinit_completion(&adapter->init_done);
851 rc = ibmvnic_send_crq(adapter, &crq);
853 netdev_err(netdev, "Failed to set link state\n");
857 if (!wait_for_completion_timeout(&adapter->init_done,
859 netdev_err(netdev, "timeout setting link state\n");
863 if (adapter->init_done_rc == 1) {
864 /* Partuial success, delay and re-send */
873 static int set_real_num_queues(struct net_device *netdev)
875 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
878 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
879 adapter->req_tx_queues, adapter->req_rx_queues);
881 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
883 netdev_err(netdev, "failed to set the number of tx queues\n");
887 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
889 netdev_err(netdev, "failed to set the number of rx queues\n");
894 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
896 struct device *dev = &adapter->vdev->dev;
897 union ibmvnic_crq crq;
900 if (adapter->vpd->buff)
901 len = adapter->vpd->len;
903 init_completion(&adapter->fw_done);
904 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
905 crq.get_vpd_size.cmd = GET_VPD_SIZE;
906 ibmvnic_send_crq(adapter, &crq);
907 wait_for_completion(&adapter->fw_done);
909 if (!adapter->vpd->len)
912 if (!adapter->vpd->buff)
913 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
914 else if (adapter->vpd->len != len)
916 krealloc(adapter->vpd->buff,
917 adapter->vpd->len, GFP_KERNEL);
919 if (!adapter->vpd->buff) {
920 dev_err(dev, "Could allocate VPD buffer\n");
924 adapter->vpd->dma_addr =
925 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
927 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
928 dev_err(dev, "Could not map VPD buffer\n");
929 kfree(adapter->vpd->buff);
930 adapter->vpd->buff = NULL;
934 reinit_completion(&adapter->fw_done);
935 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
936 crq.get_vpd.cmd = GET_VPD;
937 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
938 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
939 ibmvnic_send_crq(adapter, &crq);
940 wait_for_completion(&adapter->fw_done);
945 static int init_resources(struct ibmvnic_adapter *adapter)
947 struct net_device *netdev = adapter->netdev;
950 rc = set_real_num_queues(netdev);
954 rc = init_stats_buffers(adapter);
958 rc = init_stats_token(adapter);
962 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
966 /* Vital Product Data (VPD) */
967 rc = ibmvnic_get_vpd(adapter);
969 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
974 adapter->napi = kcalloc(adapter->req_rx_queues,
975 sizeof(struct napi_struct), GFP_KERNEL);
979 for (i = 0; i < adapter->req_rx_queues; i++) {
980 netdev_dbg(netdev, "Adding napi[%d]\n", i);
981 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
985 send_map_query(adapter);
987 rc = init_rx_pools(netdev);
991 rc = init_tx_pools(netdev);
995 static int __ibmvnic_open(struct net_device *netdev)
997 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
998 enum vnic_state prev_state = adapter->state;
1001 adapter->state = VNIC_OPENING;
1002 replenish_pools(adapter);
1003 ibmvnic_napi_enable(adapter);
1005 /* We're ready to receive frames, enable the sub-crq interrupts and
1006 * set the logical link state to up
1008 for (i = 0; i < adapter->req_rx_queues; i++) {
1009 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1010 if (prev_state == VNIC_CLOSED)
1011 enable_irq(adapter->rx_scrq[i]->irq);
1013 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1016 for (i = 0; i < adapter->req_tx_queues; i++) {
1017 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1018 if (prev_state == VNIC_CLOSED)
1019 enable_irq(adapter->tx_scrq[i]->irq);
1021 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1024 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1026 for (i = 0; i < adapter->req_rx_queues; i++)
1027 napi_disable(&adapter->napi[i]);
1028 release_resources(adapter);
1032 netif_tx_start_all_queues(netdev);
1034 if (prev_state == VNIC_CLOSED) {
1035 for (i = 0; i < adapter->req_rx_queues; i++)
1036 napi_schedule(&adapter->napi[i]);
1039 adapter->state = VNIC_OPEN;
1043 static int ibmvnic_open(struct net_device *netdev)
1045 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1048 mutex_lock(&adapter->reset_lock);
1050 if (adapter->state != VNIC_CLOSED) {
1051 rc = ibmvnic_login(netdev);
1053 mutex_unlock(&adapter->reset_lock);
1057 rc = init_resources(adapter);
1059 netdev_err(netdev, "failed to initialize resources\n");
1060 release_resources(adapter);
1061 mutex_unlock(&adapter->reset_lock);
1066 rc = __ibmvnic_open(netdev);
1067 netif_carrier_on(netdev);
1069 mutex_unlock(&adapter->reset_lock);
1074 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1076 struct ibmvnic_tx_pool *tx_pool;
1081 if (!adapter->tx_pool)
1084 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1085 tx_entries = adapter->req_tx_entries_per_subcrq;
1087 /* Free any remaining skbs in the tx buffer pools */
1088 for (i = 0; i < tx_scrqs; i++) {
1089 tx_pool = &adapter->tx_pool[i];
1093 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1094 for (j = 0; j < tx_entries; j++) {
1095 if (tx_pool->tx_buff[j].skb) {
1096 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1097 tx_pool->tx_buff[j].skb = NULL;
1103 static int __ibmvnic_close(struct net_device *netdev)
1105 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1109 adapter->state = VNIC_CLOSING;
1111 /* ensure that transmissions are stopped if called by do_reset */
1112 if (adapter->resetting)
1113 netif_tx_disable(netdev);
1115 netif_tx_stop_all_queues(netdev);
1117 ibmvnic_napi_disable(adapter);
1119 if (adapter->tx_scrq) {
1120 for (i = 0; i < adapter->req_tx_queues; i++)
1121 if (adapter->tx_scrq[i]->irq) {
1122 netdev_dbg(adapter->netdev,
1123 "Disabling tx_scrq[%d] irq\n", i);
1124 disable_irq(adapter->tx_scrq[i]->irq);
1128 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1132 if (adapter->rx_scrq) {
1133 for (i = 0; i < adapter->req_rx_queues; i++) {
1136 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1144 if (adapter->rx_scrq[i]->irq) {
1145 netdev_dbg(adapter->netdev,
1146 "Disabling rx_scrq[%d] irq\n", i);
1147 disable_irq(adapter->rx_scrq[i]->irq);
1152 clean_tx_pools(adapter);
1153 adapter->state = VNIC_CLOSED;
1157 static int ibmvnic_close(struct net_device *netdev)
1159 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1162 mutex_lock(&adapter->reset_lock);
1163 rc = __ibmvnic_close(netdev);
1164 mutex_unlock(&adapter->reset_lock);
1170 * build_hdr_data - creates L2/L3/L4 header data buffer
1171 * @hdr_field - bitfield determining needed headers
1172 * @skb - socket buffer
1173 * @hdr_len - array of header lengths
1174 * @tot_len - total length of data
1176 * Reads hdr_field to determine which headers are needed by firmware.
1177 * Builds a buffer containing these headers. Saves individual header
1178 * lengths and total buffer length to be used to build descriptors.
1180 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1181 int *hdr_len, u8 *hdr_data)
1186 hdr_len[0] = sizeof(struct ethhdr);
1188 if (skb->protocol == htons(ETH_P_IP)) {
1189 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1190 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1191 hdr_len[2] = tcp_hdrlen(skb);
1192 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1193 hdr_len[2] = sizeof(struct udphdr);
1194 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1195 hdr_len[1] = sizeof(struct ipv6hdr);
1196 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1197 hdr_len[2] = tcp_hdrlen(skb);
1198 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1199 hdr_len[2] = sizeof(struct udphdr);
1200 } else if (skb->protocol == htons(ETH_P_ARP)) {
1201 hdr_len[1] = arp_hdr_len(skb->dev);
1205 memset(hdr_data, 0, 120);
1206 if ((hdr_field >> 6) & 1) {
1207 hdr = skb_mac_header(skb);
1208 memcpy(hdr_data, hdr, hdr_len[0]);
1212 if ((hdr_field >> 5) & 1) {
1213 hdr = skb_network_header(skb);
1214 memcpy(hdr_data + len, hdr, hdr_len[1]);
1218 if ((hdr_field >> 4) & 1) {
1219 hdr = skb_transport_header(skb);
1220 memcpy(hdr_data + len, hdr, hdr_len[2]);
1227 * create_hdr_descs - create header and header extension descriptors
1228 * @hdr_field - bitfield determining needed headers
1229 * @data - buffer containing header data
1230 * @len - length of data buffer
1231 * @hdr_len - array of individual header lengths
1232 * @scrq_arr - descriptor array
1234 * Creates header and, if needed, header extension descriptors and
1235 * places them in a descriptor array, scrq_arr
1238 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1239 union sub_crq *scrq_arr)
1241 union sub_crq hdr_desc;
1247 while (tmp_len > 0) {
1248 cur = hdr_data + len - tmp_len;
1250 memset(&hdr_desc, 0, sizeof(hdr_desc));
1251 if (cur != hdr_data) {
1252 data = hdr_desc.hdr_ext.data;
1253 tmp = tmp_len > 29 ? 29 : tmp_len;
1254 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1255 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1256 hdr_desc.hdr_ext.len = tmp;
1258 data = hdr_desc.hdr.data;
1259 tmp = tmp_len > 24 ? 24 : tmp_len;
1260 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1261 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1262 hdr_desc.hdr.len = tmp;
1263 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1264 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1265 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1266 hdr_desc.hdr.flag = hdr_field << 1;
1268 memcpy(data, cur, tmp);
1270 *scrq_arr = hdr_desc;
1279 * build_hdr_descs_arr - build a header descriptor array
1280 * @skb - socket buffer
1281 * @num_entries - number of descriptors to be sent
1282 * @subcrq - first TX descriptor
1283 * @hdr_field - bit field determining which headers will be sent
1285 * This function will build a TX descriptor array with applicable
1286 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1289 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1290 int *num_entries, u8 hdr_field)
1292 int hdr_len[3] = {0, 0, 0};
1294 u8 *hdr_data = txbuff->hdr_data;
1296 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1298 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1299 txbuff->indir_arr + 1);
1302 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1304 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1305 int queue_num = skb_get_queue_mapping(skb);
1306 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1307 struct device *dev = &adapter->vdev->dev;
1308 struct ibmvnic_tx_buff *tx_buff = NULL;
1309 struct ibmvnic_sub_crq_queue *tx_scrq;
1310 struct ibmvnic_tx_pool *tx_pool;
1311 unsigned int tx_send_failed = 0;
1312 unsigned int tx_map_failed = 0;
1313 unsigned int tx_dropped = 0;
1314 unsigned int tx_packets = 0;
1315 unsigned int tx_bytes = 0;
1316 dma_addr_t data_dma_addr;
1317 struct netdev_queue *txq;
1318 unsigned long lpar_rc;
1319 union sub_crq tx_crq;
1320 unsigned int offset;
1321 int num_entries = 1;
1328 if (adapter->resetting) {
1329 if (!netif_subqueue_stopped(netdev, skb))
1330 netif_stop_subqueue(netdev, queue_num);
1331 dev_kfree_skb_any(skb);
1339 tx_pool = &adapter->tx_pool[queue_num];
1340 tx_scrq = adapter->tx_scrq[queue_num];
1341 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1342 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1343 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1345 index = tx_pool->free_map[tx_pool->consumer_index];
1347 if (skb_is_gso(skb)) {
1348 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1349 dst = tx_pool->tso_ltb.buff + offset;
1350 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1351 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1352 tx_pool->tso_index++;
1353 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1354 tx_pool->tso_index = 0;
1356 offset = index * adapter->req_mtu;
1357 dst = tx_pool->long_term_buff.buff + offset;
1358 memset(dst, 0, adapter->req_mtu);
1359 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1362 if (skb_shinfo(skb)->nr_frags) {
1366 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1367 cur = skb_headlen(skb);
1369 /* Copy the frags */
1370 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1371 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1374 page_address(skb_frag_page(frag)) +
1375 frag->page_offset, skb_frag_size(frag));
1376 cur += skb_frag_size(frag);
1379 skb_copy_from_linear_data(skb, dst, skb->len);
1382 tx_pool->consumer_index =
1383 (tx_pool->consumer_index + 1) %
1384 adapter->req_tx_entries_per_subcrq;
1386 tx_buff = &tx_pool->tx_buff[index];
1388 tx_buff->data_dma[0] = data_dma_addr;
1389 tx_buff->data_len[0] = skb->len;
1390 tx_buff->index = index;
1391 tx_buff->pool_index = queue_num;
1392 tx_buff->last_frag = true;
1394 memset(&tx_crq, 0, sizeof(tx_crq));
1395 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1396 tx_crq.v1.type = IBMVNIC_TX_DESC;
1397 tx_crq.v1.n_crq_elem = 1;
1398 tx_crq.v1.n_sge = 1;
1399 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1400 tx_crq.v1.correlator = cpu_to_be32(index);
1401 if (skb_is_gso(skb))
1402 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1404 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1405 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1406 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1408 if (adapter->vlan_header_insertion) {
1409 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1410 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1413 if (skb->protocol == htons(ETH_P_IP)) {
1414 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1415 proto = ip_hdr(skb)->protocol;
1416 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1417 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1418 proto = ipv6_hdr(skb)->nexthdr;
1421 if (proto == IPPROTO_TCP)
1422 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1423 else if (proto == IPPROTO_UDP)
1424 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1426 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1427 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1430 if (skb_is_gso(skb)) {
1431 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1432 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1435 /* determine if l2/3/4 headers are sent to firmware */
1436 if ((*hdrs >> 7) & 1) {
1437 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1438 tx_crq.v1.n_crq_elem = num_entries;
1439 tx_buff->indir_arr[0] = tx_crq;
1440 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1441 sizeof(tx_buff->indir_arr),
1443 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1444 dev_kfree_skb_any(skb);
1445 tx_buff->skb = NULL;
1446 if (!firmware_has_feature(FW_FEATURE_CMO))
1447 dev_err(dev, "tx: unable to map descriptor array\n");
1453 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1454 (u64)tx_buff->indir_dma,
1457 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1460 if (lpar_rc != H_SUCCESS) {
1461 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1463 if (tx_pool->consumer_index == 0)
1464 tx_pool->consumer_index =
1465 adapter->req_tx_entries_per_subcrq - 1;
1467 tx_pool->consumer_index--;
1469 dev_kfree_skb_any(skb);
1470 tx_buff->skb = NULL;
1472 if (lpar_rc == H_CLOSED) {
1473 /* Disable TX and report carrier off if queue is closed.
1474 * Firmware guarantees that a signal will be sent to the
1475 * driver, triggering a reset or some other action.
1477 netif_tx_stop_all_queues(netdev);
1478 netif_carrier_off(netdev);
1487 if (atomic_inc_return(&tx_scrq->used)
1488 >= adapter->req_tx_entries_per_subcrq) {
1489 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1490 netif_stop_subqueue(netdev, queue_num);
1494 tx_bytes += skb->len;
1495 txq->trans_start = jiffies;
1499 netdev->stats.tx_dropped += tx_dropped;
1500 netdev->stats.tx_bytes += tx_bytes;
1501 netdev->stats.tx_packets += tx_packets;
1502 adapter->tx_send_failed += tx_send_failed;
1503 adapter->tx_map_failed += tx_map_failed;
1504 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1505 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1506 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1511 static void ibmvnic_set_multi(struct net_device *netdev)
1513 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1514 struct netdev_hw_addr *ha;
1515 union ibmvnic_crq crq;
1517 memset(&crq, 0, sizeof(crq));
1518 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1519 crq.request_capability.cmd = REQUEST_CAPABILITY;
1521 if (netdev->flags & IFF_PROMISC) {
1522 if (!adapter->promisc_supported)
1525 if (netdev->flags & IFF_ALLMULTI) {
1526 /* Accept all multicast */
1527 memset(&crq, 0, sizeof(crq));
1528 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1529 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1530 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1531 ibmvnic_send_crq(adapter, &crq);
1532 } else if (netdev_mc_empty(netdev)) {
1533 /* Reject all multicast */
1534 memset(&crq, 0, sizeof(crq));
1535 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1536 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1537 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1538 ibmvnic_send_crq(adapter, &crq);
1540 /* Accept one or more multicast(s) */
1541 netdev_for_each_mc_addr(ha, netdev) {
1542 memset(&crq, 0, sizeof(crq));
1543 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1544 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1545 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1546 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1548 ibmvnic_send_crq(adapter, &crq);
1554 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1556 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1557 struct sockaddr *addr = p;
1558 union ibmvnic_crq crq;
1560 if (!is_valid_ether_addr(addr->sa_data))
1561 return -EADDRNOTAVAIL;
1563 memset(&crq, 0, sizeof(crq));
1564 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1565 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1566 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1568 init_completion(&adapter->fw_done);
1569 ibmvnic_send_crq(adapter, &crq);
1570 wait_for_completion(&adapter->fw_done);
1571 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1572 return adapter->fw_done_rc ? -EIO : 0;
1575 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1577 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1578 struct sockaddr *addr = p;
1581 if (adapter->state == VNIC_PROBED) {
1582 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1583 adapter->mac_change_pending = true;
1587 rc = __ibmvnic_set_mac(netdev, addr);
1593 * do_reset returns zero if we are able to keep processing reset events, or
1594 * non-zero if we hit a fatal error and must halt.
1596 static int do_reset(struct ibmvnic_adapter *adapter,
1597 struct ibmvnic_rwi *rwi, u32 reset_state)
1599 u64 old_num_rx_queues, old_num_tx_queues;
1600 struct net_device *netdev = adapter->netdev;
1603 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1606 netif_carrier_off(netdev);
1607 adapter->reset_reason = rwi->reset_reason;
1609 old_num_rx_queues = adapter->req_rx_queues;
1610 old_num_tx_queues = adapter->req_tx_queues;
1612 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1613 rc = ibmvnic_reenable_crq_queue(adapter);
1618 rc = __ibmvnic_close(netdev);
1622 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1623 adapter->wait_for_reset) {
1624 release_resources(adapter);
1625 release_sub_crqs(adapter);
1626 release_crq_queue(adapter);
1629 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1630 /* remove the closed state so when we call open it appears
1631 * we are coming from the probed state.
1633 adapter->state = VNIC_PROBED;
1635 rc = ibmvnic_init(adapter);
1637 return IBMVNIC_INIT_FAILED;
1639 /* If the adapter was in PROBE state prior to the reset,
1642 if (reset_state == VNIC_PROBED)
1645 rc = ibmvnic_login(netdev);
1647 adapter->state = VNIC_PROBED;
1651 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1652 adapter->wait_for_reset) {
1653 rc = init_resources(adapter);
1656 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1657 adapter->req_tx_queues != old_num_tx_queues) {
1658 adapter->map_id = 1;
1659 release_rx_pools(adapter);
1660 release_tx_pools(adapter);
1661 init_rx_pools(netdev);
1662 init_tx_pools(netdev);
1664 rc = reset_tx_pools(adapter);
1668 rc = reset_rx_pools(adapter);
1672 if (reset_state == VNIC_CLOSED)
1677 rc = __ibmvnic_open(netdev);
1679 if (list_empty(&adapter->rwi_list))
1680 adapter->state = VNIC_CLOSED;
1682 adapter->state = reset_state;
1688 for (i = 0; i < adapter->req_rx_queues; i++)
1689 napi_schedule(&adapter->napi[i]);
1691 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1692 netdev_notify_peers(netdev);
1694 netif_carrier_on(netdev);
1699 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1701 struct ibmvnic_rwi *rwi;
1703 mutex_lock(&adapter->rwi_lock);
1705 if (!list_empty(&adapter->rwi_list)) {
1706 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1708 list_del(&rwi->list);
1713 mutex_unlock(&adapter->rwi_lock);
1717 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1719 struct ibmvnic_rwi *rwi;
1721 rwi = get_next_rwi(adapter);
1724 rwi = get_next_rwi(adapter);
1728 static void __ibmvnic_reset(struct work_struct *work)
1730 struct ibmvnic_rwi *rwi;
1731 struct ibmvnic_adapter *adapter;
1732 struct net_device *netdev;
1736 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1737 netdev = adapter->netdev;
1739 mutex_lock(&adapter->reset_lock);
1740 adapter->resetting = true;
1741 reset_state = adapter->state;
1743 rwi = get_next_rwi(adapter);
1745 rc = do_reset(adapter, rwi, reset_state);
1747 if (rc && rc != IBMVNIC_INIT_FAILED)
1750 rwi = get_next_rwi(adapter);
1753 if (adapter->wait_for_reset) {
1754 adapter->wait_for_reset = false;
1755 adapter->reset_done_rc = rc;
1756 complete(&adapter->reset_done);
1760 netdev_dbg(adapter->netdev, "Reset failed\n");
1761 free_all_rwi(adapter);
1762 mutex_unlock(&adapter->reset_lock);
1766 adapter->resetting = false;
1767 mutex_unlock(&adapter->reset_lock);
1770 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1771 enum ibmvnic_reset_reason reason)
1773 struct ibmvnic_rwi *rwi, *tmp;
1774 struct net_device *netdev = adapter->netdev;
1775 struct list_head *entry;
1777 if (adapter->state == VNIC_REMOVING ||
1778 adapter->state == VNIC_REMOVED) {
1779 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1783 if (adapter->state == VNIC_PROBING) {
1784 netdev_warn(netdev, "Adapter reset during probe\n");
1785 adapter->init_done_rc = EAGAIN;
1789 mutex_lock(&adapter->rwi_lock);
1791 list_for_each(entry, &adapter->rwi_list) {
1792 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1793 if (tmp->reset_reason == reason) {
1794 netdev_dbg(netdev, "Skipping matching reset\n");
1795 mutex_unlock(&adapter->rwi_lock);
1800 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1802 mutex_unlock(&adapter->rwi_lock);
1803 ibmvnic_close(netdev);
1807 rwi->reset_reason = reason;
1808 list_add_tail(&rwi->list, &adapter->rwi_list);
1809 mutex_unlock(&adapter->rwi_lock);
1811 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1812 schedule_work(&adapter->ibmvnic_reset);
1815 static void ibmvnic_tx_timeout(struct net_device *dev)
1817 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1819 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1822 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1823 struct ibmvnic_rx_buff *rx_buff)
1825 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1827 rx_buff->skb = NULL;
1829 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1830 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1832 atomic_dec(&pool->available);
1835 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1837 struct net_device *netdev = napi->dev;
1838 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1839 int scrq_num = (int)(napi - adapter->napi);
1840 int frames_processed = 0;
1843 while (frames_processed < budget) {
1844 struct sk_buff *skb;
1845 struct ibmvnic_rx_buff *rx_buff;
1846 union sub_crq *next;
1851 if (unlikely(adapter->resetting &&
1852 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1853 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1854 napi_complete_done(napi, frames_processed);
1855 return frames_processed;
1858 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1860 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1862 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1863 rx_comp.correlator);
1864 /* do error checking */
1865 if (next->rx_comp.rc) {
1866 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1867 be16_to_cpu(next->rx_comp.rc));
1868 /* free the entry */
1869 next->rx_comp.first = 0;
1870 remove_buff_from_pool(adapter, rx_buff);
1874 length = be32_to_cpu(next->rx_comp.len);
1875 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1876 flags = next->rx_comp.flags;
1878 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1881 /* VLAN Header has been stripped by the system firmware and
1882 * needs to be inserted by the driver
1884 if (adapter->rx_vlan_header_insertion &&
1885 (flags & IBMVNIC_VLAN_STRIPPED))
1886 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1887 ntohs(next->rx_comp.vlan_tci));
1889 /* free the entry */
1890 next->rx_comp.first = 0;
1891 remove_buff_from_pool(adapter, rx_buff);
1893 skb_put(skb, length);
1894 skb->protocol = eth_type_trans(skb, netdev);
1895 skb_record_rx_queue(skb, scrq_num);
1897 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1898 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1899 skb->ip_summed = CHECKSUM_UNNECESSARY;
1903 napi_gro_receive(napi, skb); /* send it up */
1904 netdev->stats.rx_packets++;
1905 netdev->stats.rx_bytes += length;
1906 adapter->rx_stats_buffers[scrq_num].packets++;
1907 adapter->rx_stats_buffers[scrq_num].bytes += length;
1911 if (adapter->state != VNIC_CLOSING)
1912 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1914 if (frames_processed < budget) {
1915 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1916 napi_complete_done(napi, frames_processed);
1917 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1918 napi_reschedule(napi)) {
1919 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1923 return frames_processed;
1926 #ifdef CONFIG_NET_POLL_CONTROLLER
1927 static void ibmvnic_netpoll_controller(struct net_device *dev)
1929 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1932 replenish_pools(netdev_priv(dev));
1933 for (i = 0; i < adapter->req_rx_queues; i++)
1934 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1935 adapter->rx_scrq[i]);
1939 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1941 adapter->fallback.mtu = adapter->req_mtu;
1942 adapter->fallback.rx_queues = adapter->req_rx_queues;
1943 adapter->fallback.tx_queues = adapter->req_tx_queues;
1944 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1945 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1947 init_completion(&adapter->reset_done);
1948 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1949 adapter->wait_for_reset = true;
1950 wait_for_completion(&adapter->reset_done);
1952 if (adapter->reset_done_rc) {
1953 adapter->desired.mtu = adapter->fallback.mtu;
1954 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1955 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1956 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1957 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1959 init_completion(&adapter->reset_done);
1960 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1961 wait_for_completion(&adapter->reset_done);
1963 adapter->wait_for_reset = false;
1965 return adapter->reset_done_rc;
1968 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1970 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1972 adapter->desired.mtu = new_mtu + ETH_HLEN;
1974 return wait_for_reset(adapter);
1977 static const struct net_device_ops ibmvnic_netdev_ops = {
1978 .ndo_open = ibmvnic_open,
1979 .ndo_stop = ibmvnic_close,
1980 .ndo_start_xmit = ibmvnic_xmit,
1981 .ndo_set_rx_mode = ibmvnic_set_multi,
1982 .ndo_set_mac_address = ibmvnic_set_mac,
1983 .ndo_validate_addr = eth_validate_addr,
1984 .ndo_tx_timeout = ibmvnic_tx_timeout,
1985 #ifdef CONFIG_NET_POLL_CONTROLLER
1986 .ndo_poll_controller = ibmvnic_netpoll_controller,
1988 .ndo_change_mtu = ibmvnic_change_mtu,
1991 /* ethtool functions */
1993 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1994 struct ethtool_link_ksettings *cmd)
1996 u32 supported, advertising;
1998 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2000 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2002 cmd->base.speed = SPEED_1000;
2003 cmd->base.duplex = DUPLEX_FULL;
2004 cmd->base.port = PORT_FIBRE;
2005 cmd->base.phy_address = 0;
2006 cmd->base.autoneg = AUTONEG_ENABLE;
2008 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2010 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2016 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2017 struct ethtool_drvinfo *info)
2019 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2021 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2022 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2023 strlcpy(info->fw_version, adapter->fw_version,
2024 sizeof(info->fw_version));
2027 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2029 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2031 return adapter->msg_enable;
2034 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2036 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2038 adapter->msg_enable = data;
2041 static u32 ibmvnic_get_link(struct net_device *netdev)
2043 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2045 /* Don't need to send a query because we request a logical link up at
2046 * init and then we wait for link state indications
2048 return adapter->logical_link_state;
2051 static void ibmvnic_get_ringparam(struct net_device *netdev,
2052 struct ethtool_ringparam *ring)
2054 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2056 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2057 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2058 ring->rx_mini_max_pending = 0;
2059 ring->rx_jumbo_max_pending = 0;
2060 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2061 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2062 ring->rx_mini_pending = 0;
2063 ring->rx_jumbo_pending = 0;
2066 static int ibmvnic_set_ringparam(struct net_device *netdev,
2067 struct ethtool_ringparam *ring)
2069 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2071 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2072 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2073 netdev_err(netdev, "Invalid request.\n");
2074 netdev_err(netdev, "Max tx buffers = %llu\n",
2075 adapter->max_rx_add_entries_per_subcrq);
2076 netdev_err(netdev, "Max rx buffers = %llu\n",
2077 adapter->max_tx_entries_per_subcrq);
2081 adapter->desired.rx_entries = ring->rx_pending;
2082 adapter->desired.tx_entries = ring->tx_pending;
2084 return wait_for_reset(adapter);
2087 static void ibmvnic_get_channels(struct net_device *netdev,
2088 struct ethtool_channels *channels)
2090 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2092 channels->max_rx = adapter->max_rx_queues;
2093 channels->max_tx = adapter->max_tx_queues;
2094 channels->max_other = 0;
2095 channels->max_combined = 0;
2096 channels->rx_count = adapter->req_rx_queues;
2097 channels->tx_count = adapter->req_tx_queues;
2098 channels->other_count = 0;
2099 channels->combined_count = 0;
2102 static int ibmvnic_set_channels(struct net_device *netdev,
2103 struct ethtool_channels *channels)
2105 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2107 adapter->desired.rx_queues = channels->rx_count;
2108 adapter->desired.tx_queues = channels->tx_count;
2110 return wait_for_reset(adapter);
2113 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2115 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2118 if (stringset != ETH_SS_STATS)
2121 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2122 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2124 for (i = 0; i < adapter->req_tx_queues; i++) {
2125 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2126 data += ETH_GSTRING_LEN;
2128 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2129 data += ETH_GSTRING_LEN;
2131 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2132 data += ETH_GSTRING_LEN;
2135 for (i = 0; i < adapter->req_rx_queues; i++) {
2136 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2137 data += ETH_GSTRING_LEN;
2139 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2140 data += ETH_GSTRING_LEN;
2142 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2143 data += ETH_GSTRING_LEN;
2147 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2149 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2153 return ARRAY_SIZE(ibmvnic_stats) +
2154 adapter->req_tx_queues * NUM_TX_STATS +
2155 adapter->req_rx_queues * NUM_RX_STATS;
2161 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2162 struct ethtool_stats *stats, u64 *data)
2164 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2165 union ibmvnic_crq crq;
2168 memset(&crq, 0, sizeof(crq));
2169 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2170 crq.request_statistics.cmd = REQUEST_STATISTICS;
2171 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2172 crq.request_statistics.len =
2173 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2175 /* Wait for data to be written */
2176 init_completion(&adapter->stats_done);
2177 ibmvnic_send_crq(adapter, &crq);
2178 wait_for_completion(&adapter->stats_done);
2180 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2181 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2182 ibmvnic_stats[i].offset));
2184 for (j = 0; j < adapter->req_tx_queues; j++) {
2185 data[i] = adapter->tx_stats_buffers[j].packets;
2187 data[i] = adapter->tx_stats_buffers[j].bytes;
2189 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2193 for (j = 0; j < adapter->req_rx_queues; j++) {
2194 data[i] = adapter->rx_stats_buffers[j].packets;
2196 data[i] = adapter->rx_stats_buffers[j].bytes;
2198 data[i] = adapter->rx_stats_buffers[j].interrupts;
2203 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2204 .get_drvinfo = ibmvnic_get_drvinfo,
2205 .get_msglevel = ibmvnic_get_msglevel,
2206 .set_msglevel = ibmvnic_set_msglevel,
2207 .get_link = ibmvnic_get_link,
2208 .get_ringparam = ibmvnic_get_ringparam,
2209 .set_ringparam = ibmvnic_set_ringparam,
2210 .get_channels = ibmvnic_get_channels,
2211 .set_channels = ibmvnic_set_channels,
2212 .get_strings = ibmvnic_get_strings,
2213 .get_sset_count = ibmvnic_get_sset_count,
2214 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2215 .get_link_ksettings = ibmvnic_get_link_ksettings,
2218 /* Routines for managing CRQs/sCRQs */
2220 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2221 struct ibmvnic_sub_crq_queue *scrq)
2226 free_irq(scrq->irq, scrq);
2227 irq_dispose_mapping(scrq->irq);
2231 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2234 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2235 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2239 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2243 for (i = 0; i < adapter->req_tx_queues; i++) {
2244 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2245 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2250 for (i = 0; i < adapter->req_rx_queues; i++) {
2251 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2252 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2260 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2261 struct ibmvnic_sub_crq_queue *scrq)
2263 struct device *dev = &adapter->vdev->dev;
2266 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2268 /* Close the sub-crqs */
2270 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2271 adapter->vdev->unit_address,
2273 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2276 netdev_err(adapter->netdev,
2277 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2281 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2283 free_pages((unsigned long)scrq->msgs, 2);
2287 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2290 struct device *dev = &adapter->vdev->dev;
2291 struct ibmvnic_sub_crq_queue *scrq;
2294 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2299 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2301 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2302 goto zero_page_failed;
2305 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2307 if (dma_mapping_error(dev, scrq->msg_token)) {
2308 dev_warn(dev, "Couldn't map crq queue messages page\n");
2312 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2313 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2315 if (rc == H_RESOURCE)
2316 rc = ibmvnic_reset_crq(adapter);
2318 if (rc == H_CLOSED) {
2319 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2321 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2325 scrq->adapter = adapter;
2326 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2327 spin_lock_init(&scrq->lock);
2329 netdev_dbg(adapter->netdev,
2330 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2331 scrq->crq_num, scrq->hw_irq, scrq->irq);
2336 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2339 free_pages((unsigned long)scrq->msgs, 2);
2346 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2350 if (adapter->tx_scrq) {
2351 for (i = 0; i < adapter->req_tx_queues; i++) {
2352 if (!adapter->tx_scrq[i])
2355 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2357 if (adapter->tx_scrq[i]->irq) {
2358 free_irq(adapter->tx_scrq[i]->irq,
2359 adapter->tx_scrq[i]);
2360 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2361 adapter->tx_scrq[i]->irq = 0;
2364 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2367 kfree(adapter->tx_scrq);
2368 adapter->tx_scrq = NULL;
2371 if (adapter->rx_scrq) {
2372 for (i = 0; i < adapter->req_rx_queues; i++) {
2373 if (!adapter->rx_scrq[i])
2376 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2378 if (adapter->rx_scrq[i]->irq) {
2379 free_irq(adapter->rx_scrq[i]->irq,
2380 adapter->rx_scrq[i]);
2381 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2382 adapter->rx_scrq[i]->irq = 0;
2385 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2388 kfree(adapter->rx_scrq);
2389 adapter->rx_scrq = NULL;
2393 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2394 struct ibmvnic_sub_crq_queue *scrq)
2396 struct device *dev = &adapter->vdev->dev;
2399 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2400 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2402 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2407 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2408 struct ibmvnic_sub_crq_queue *scrq)
2410 struct device *dev = &adapter->vdev->dev;
2413 if (scrq->hw_irq > 0x100000000ULL) {
2414 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2418 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2419 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2421 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2426 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2427 struct ibmvnic_sub_crq_queue *scrq)
2429 struct device *dev = &adapter->vdev->dev;
2430 struct ibmvnic_tx_buff *txbuff;
2431 union sub_crq *next;
2437 while (pending_scrq(adapter, scrq)) {
2438 unsigned int pool = scrq->pool_index;
2440 next = ibmvnic_next_scrq(adapter, scrq);
2441 for (i = 0; i < next->tx_comp.num_comps; i++) {
2442 if (next->tx_comp.rcs[i]) {
2443 dev_err(dev, "tx error %x\n",
2444 next->tx_comp.rcs[i]);
2447 index = be32_to_cpu(next->tx_comp.correlators[i]);
2448 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2450 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2451 if (!txbuff->data_dma[j])
2454 txbuff->data_dma[j] = 0;
2456 /* if sub_crq was sent indirectly */
2457 first = txbuff->indir_arr[0].generic.first;
2458 if (first == IBMVNIC_CRQ_CMD) {
2459 dma_unmap_single(dev, txbuff->indir_dma,
2460 sizeof(txbuff->indir_arr),
2464 if (txbuff->last_frag) {
2465 dev_kfree_skb_any(txbuff->skb);
2469 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2470 producer_index] = index;
2471 adapter->tx_pool[pool].producer_index =
2472 (adapter->tx_pool[pool].producer_index + 1) %
2473 adapter->req_tx_entries_per_subcrq;
2475 /* remove tx_comp scrq*/
2476 next->tx_comp.first = 0;
2478 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2479 (adapter->req_tx_entries_per_subcrq / 2) &&
2480 __netif_subqueue_stopped(adapter->netdev,
2481 scrq->pool_index)) {
2482 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2483 netdev_info(adapter->netdev, "Started queue %d\n",
2488 enable_scrq_irq(adapter, scrq);
2490 if (pending_scrq(adapter, scrq)) {
2491 disable_scrq_irq(adapter, scrq);
2498 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2500 struct ibmvnic_sub_crq_queue *scrq = instance;
2501 struct ibmvnic_adapter *adapter = scrq->adapter;
2503 disable_scrq_irq(adapter, scrq);
2504 ibmvnic_complete_tx(adapter, scrq);
2509 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2511 struct ibmvnic_sub_crq_queue *scrq = instance;
2512 struct ibmvnic_adapter *adapter = scrq->adapter;
2514 /* When booting a kdump kernel we can hit pending interrupts
2515 * prior to completing driver initialization.
2517 if (unlikely(adapter->state != VNIC_OPEN))
2520 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2522 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2523 disable_scrq_irq(adapter, scrq);
2524 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2530 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2532 struct device *dev = &adapter->vdev->dev;
2533 struct ibmvnic_sub_crq_queue *scrq;
2537 for (i = 0; i < adapter->req_tx_queues; i++) {
2538 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2540 scrq = adapter->tx_scrq[i];
2541 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2545 dev_err(dev, "Error mapping irq\n");
2546 goto req_tx_irq_failed;
2549 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2550 0, "ibmvnic_tx", scrq);
2553 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2555 irq_dispose_mapping(scrq->irq);
2556 goto req_rx_irq_failed;
2560 for (i = 0; i < adapter->req_rx_queues; i++) {
2561 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2563 scrq = adapter->rx_scrq[i];
2564 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2567 dev_err(dev, "Error mapping irq\n");
2568 goto req_rx_irq_failed;
2570 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2571 0, "ibmvnic_rx", scrq);
2573 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2575 irq_dispose_mapping(scrq->irq);
2576 goto req_rx_irq_failed;
2582 for (j = 0; j < i; j++) {
2583 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2584 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2586 i = adapter->req_tx_queues;
2588 for (j = 0; j < i; j++) {
2589 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2590 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2592 release_sub_crqs(adapter);
2596 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2598 struct device *dev = &adapter->vdev->dev;
2599 struct ibmvnic_sub_crq_queue **allqueues;
2600 int registered_queues = 0;
2605 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2607 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2611 for (i = 0; i < total_queues; i++) {
2612 allqueues[i] = init_sub_crq_queue(adapter);
2613 if (!allqueues[i]) {
2614 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2617 registered_queues++;
2620 /* Make sure we were able to register the minimum number of queues */
2621 if (registered_queues <
2622 adapter->min_tx_queues + adapter->min_rx_queues) {
2623 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2627 /* Distribute the failed allocated queues*/
2628 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2629 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2632 if (adapter->req_rx_queues > adapter->min_rx_queues)
2633 adapter->req_rx_queues--;
2638 if (adapter->req_tx_queues > adapter->min_tx_queues)
2639 adapter->req_tx_queues--;
2646 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2647 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2648 if (!adapter->tx_scrq)
2651 for (i = 0; i < adapter->req_tx_queues; i++) {
2652 adapter->tx_scrq[i] = allqueues[i];
2653 adapter->tx_scrq[i]->pool_index = i;
2656 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2657 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2658 if (!adapter->rx_scrq)
2661 for (i = 0; i < adapter->req_rx_queues; i++) {
2662 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2663 adapter->rx_scrq[i]->scrq_num = i;
2670 kfree(adapter->tx_scrq);
2671 adapter->tx_scrq = NULL;
2673 for (i = 0; i < registered_queues; i++)
2674 release_sub_crq_queue(adapter, allqueues[i]);
2679 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2681 struct device *dev = &adapter->vdev->dev;
2682 union ibmvnic_crq crq;
2686 /* Sub-CRQ entries are 32 byte long */
2687 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2689 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2690 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2691 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2695 if (adapter->desired.mtu)
2696 adapter->req_mtu = adapter->desired.mtu;
2698 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2700 if (!adapter->desired.tx_entries)
2701 adapter->desired.tx_entries =
2702 adapter->max_tx_entries_per_subcrq;
2703 if (!adapter->desired.rx_entries)
2704 adapter->desired.rx_entries =
2705 adapter->max_rx_add_entries_per_subcrq;
2707 max_entries = IBMVNIC_MAX_LTB_SIZE /
2708 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2710 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2711 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2712 adapter->desired.tx_entries = max_entries;
2715 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2716 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2717 adapter->desired.rx_entries = max_entries;
2720 if (adapter->desired.tx_entries)
2721 adapter->req_tx_entries_per_subcrq =
2722 adapter->desired.tx_entries;
2724 adapter->req_tx_entries_per_subcrq =
2725 adapter->max_tx_entries_per_subcrq;
2727 if (adapter->desired.rx_entries)
2728 adapter->req_rx_add_entries_per_subcrq =
2729 adapter->desired.rx_entries;
2731 adapter->req_rx_add_entries_per_subcrq =
2732 adapter->max_rx_add_entries_per_subcrq;
2734 if (adapter->desired.tx_queues)
2735 adapter->req_tx_queues =
2736 adapter->desired.tx_queues;
2738 adapter->req_tx_queues =
2739 adapter->opt_tx_comp_sub_queues;
2741 if (adapter->desired.rx_queues)
2742 adapter->req_rx_queues =
2743 adapter->desired.rx_queues;
2745 adapter->req_rx_queues =
2746 adapter->opt_rx_comp_queues;
2748 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2751 memset(&crq, 0, sizeof(crq));
2752 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2753 crq.request_capability.cmd = REQUEST_CAPABILITY;
2755 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2756 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2757 atomic_inc(&adapter->running_cap_crqs);
2758 ibmvnic_send_crq(adapter, &crq);
2760 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2761 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2762 atomic_inc(&adapter->running_cap_crqs);
2763 ibmvnic_send_crq(adapter, &crq);
2765 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2766 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2767 atomic_inc(&adapter->running_cap_crqs);
2768 ibmvnic_send_crq(adapter, &crq);
2770 crq.request_capability.capability =
2771 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2772 crq.request_capability.number =
2773 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2774 atomic_inc(&adapter->running_cap_crqs);
2775 ibmvnic_send_crq(adapter, &crq);
2777 crq.request_capability.capability =
2778 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2779 crq.request_capability.number =
2780 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2781 atomic_inc(&adapter->running_cap_crqs);
2782 ibmvnic_send_crq(adapter, &crq);
2784 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2785 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2786 atomic_inc(&adapter->running_cap_crqs);
2787 ibmvnic_send_crq(adapter, &crq);
2789 if (adapter->netdev->flags & IFF_PROMISC) {
2790 if (adapter->promisc_supported) {
2791 crq.request_capability.capability =
2792 cpu_to_be16(PROMISC_REQUESTED);
2793 crq.request_capability.number = cpu_to_be64(1);
2794 atomic_inc(&adapter->running_cap_crqs);
2795 ibmvnic_send_crq(adapter, &crq);
2798 crq.request_capability.capability =
2799 cpu_to_be16(PROMISC_REQUESTED);
2800 crq.request_capability.number = cpu_to_be64(0);
2801 atomic_inc(&adapter->running_cap_crqs);
2802 ibmvnic_send_crq(adapter, &crq);
2806 static int pending_scrq(struct ibmvnic_adapter *adapter,
2807 struct ibmvnic_sub_crq_queue *scrq)
2809 union sub_crq *entry = &scrq->msgs[scrq->cur];
2811 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2817 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2818 struct ibmvnic_sub_crq_queue *scrq)
2820 union sub_crq *entry;
2821 unsigned long flags;
2823 spin_lock_irqsave(&scrq->lock, flags);
2824 entry = &scrq->msgs[scrq->cur];
2825 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2826 if (++scrq->cur == scrq->size)
2831 spin_unlock_irqrestore(&scrq->lock, flags);
2836 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2838 struct ibmvnic_crq_queue *queue = &adapter->crq;
2839 union ibmvnic_crq *crq;
2841 crq = &queue->msgs[queue->cur];
2842 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2843 if (++queue->cur == queue->size)
2852 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2853 union sub_crq *sub_crq)
2855 unsigned int ua = adapter->vdev->unit_address;
2856 struct device *dev = &adapter->vdev->dev;
2857 u64 *u64_crq = (u64 *)sub_crq;
2860 netdev_dbg(adapter->netdev,
2861 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2862 (unsigned long int)cpu_to_be64(remote_handle),
2863 (unsigned long int)cpu_to_be64(u64_crq[0]),
2864 (unsigned long int)cpu_to_be64(u64_crq[1]),
2865 (unsigned long int)cpu_to_be64(u64_crq[2]),
2866 (unsigned long int)cpu_to_be64(u64_crq[3]));
2868 /* Make sure the hypervisor sees the complete request */
2871 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2872 cpu_to_be64(remote_handle),
2873 cpu_to_be64(u64_crq[0]),
2874 cpu_to_be64(u64_crq[1]),
2875 cpu_to_be64(u64_crq[2]),
2876 cpu_to_be64(u64_crq[3]));
2880 dev_warn(dev, "CRQ Queue closed\n");
2881 dev_err(dev, "Send error (rc=%d)\n", rc);
2887 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2888 u64 remote_handle, u64 ioba, u64 num_entries)
2890 unsigned int ua = adapter->vdev->unit_address;
2891 struct device *dev = &adapter->vdev->dev;
2894 /* Make sure the hypervisor sees the complete request */
2896 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2897 cpu_to_be64(remote_handle),
2902 dev_warn(dev, "CRQ Queue closed\n");
2903 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2909 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2910 union ibmvnic_crq *crq)
2912 unsigned int ua = adapter->vdev->unit_address;
2913 struct device *dev = &adapter->vdev->dev;
2914 u64 *u64_crq = (u64 *)crq;
2917 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2918 (unsigned long int)cpu_to_be64(u64_crq[0]),
2919 (unsigned long int)cpu_to_be64(u64_crq[1]));
2921 /* Make sure the hypervisor sees the complete request */
2924 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2925 cpu_to_be64(u64_crq[0]),
2926 cpu_to_be64(u64_crq[1]));
2929 if (rc == H_CLOSED) {
2930 dev_warn(dev, "CRQ Queue closed\n");
2931 if (adapter->resetting)
2932 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2935 dev_warn(dev, "Send error (rc=%d)\n", rc);
2941 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2943 union ibmvnic_crq crq;
2945 memset(&crq, 0, sizeof(crq));
2946 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2947 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2948 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2950 return ibmvnic_send_crq(adapter, &crq);
2953 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2955 union ibmvnic_crq crq;
2957 memset(&crq, 0, sizeof(crq));
2958 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2959 crq.version_exchange.cmd = VERSION_EXCHANGE;
2960 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2962 return ibmvnic_send_crq(adapter, &crq);
2965 struct vnic_login_client_data {
2971 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2975 /* Calculate the amount of buffer space needed for the
2976 * vnic client data in the login buffer. There are four entries,
2977 * OS name, LPAR name, device name, and a null last entry.
2979 len = 4 * sizeof(struct vnic_login_client_data);
2980 len += 6; /* "Linux" plus NULL */
2981 len += strlen(utsname()->nodename) + 1;
2982 len += strlen(adapter->netdev->name) + 1;
2987 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2988 struct vnic_login_client_data *vlcd)
2990 const char *os_name = "Linux";
2993 /* Type 1 - LPAR OS */
2995 len = strlen(os_name) + 1;
2996 vlcd->len = cpu_to_be16(len);
2997 strncpy(&vlcd->name, os_name, len);
2998 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3000 /* Type 2 - LPAR name */
3002 len = strlen(utsname()->nodename) + 1;
3003 vlcd->len = cpu_to_be16(len);
3004 strncpy(&vlcd->name, utsname()->nodename, len);
3005 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3007 /* Type 3 - device name */
3009 len = strlen(adapter->netdev->name) + 1;
3010 vlcd->len = cpu_to_be16(len);
3011 strncpy(&vlcd->name, adapter->netdev->name, len);
3014 static void send_login(struct ibmvnic_adapter *adapter)
3016 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3017 struct ibmvnic_login_buffer *login_buffer;
3018 struct device *dev = &adapter->vdev->dev;
3019 dma_addr_t rsp_buffer_token;
3020 dma_addr_t buffer_token;
3021 size_t rsp_buffer_size;
3022 union ibmvnic_crq crq;
3026 int client_data_len;
3027 struct vnic_login_client_data *vlcd;
3030 release_login_rsp_buffer(adapter);
3031 client_data_len = vnic_client_data_len(adapter);
3034 sizeof(struct ibmvnic_login_buffer) +
3035 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3038 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3040 goto buf_alloc_failed;
3042 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3044 if (dma_mapping_error(dev, buffer_token)) {
3045 dev_err(dev, "Couldn't map login buffer\n");
3046 goto buf_map_failed;
3049 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3050 sizeof(u64) * adapter->req_tx_queues +
3051 sizeof(u64) * adapter->req_rx_queues +
3052 sizeof(u64) * adapter->req_rx_queues +
3053 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3055 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3056 if (!login_rsp_buffer)
3057 goto buf_rsp_alloc_failed;
3059 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3060 rsp_buffer_size, DMA_FROM_DEVICE);
3061 if (dma_mapping_error(dev, rsp_buffer_token)) {
3062 dev_err(dev, "Couldn't map login rsp buffer\n");
3063 goto buf_rsp_map_failed;
3066 adapter->login_buf = login_buffer;
3067 adapter->login_buf_token = buffer_token;
3068 adapter->login_buf_sz = buffer_size;
3069 adapter->login_rsp_buf = login_rsp_buffer;
3070 adapter->login_rsp_buf_token = rsp_buffer_token;
3071 adapter->login_rsp_buf_sz = rsp_buffer_size;
3073 login_buffer->len = cpu_to_be32(buffer_size);
3074 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3075 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3076 login_buffer->off_txcomp_subcrqs =
3077 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3078 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3079 login_buffer->off_rxcomp_subcrqs =
3080 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3081 sizeof(u64) * adapter->req_tx_queues);
3082 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3083 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3085 tx_list_p = (__be64 *)((char *)login_buffer +
3086 sizeof(struct ibmvnic_login_buffer));
3087 rx_list_p = (__be64 *)((char *)login_buffer +
3088 sizeof(struct ibmvnic_login_buffer) +
3089 sizeof(u64) * adapter->req_tx_queues);
3091 for (i = 0; i < adapter->req_tx_queues; i++) {
3092 if (adapter->tx_scrq[i]) {
3093 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3098 for (i = 0; i < adapter->req_rx_queues; i++) {
3099 if (adapter->rx_scrq[i]) {
3100 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3105 /* Insert vNIC login client data */
3106 vlcd = (struct vnic_login_client_data *)
3107 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3108 login_buffer->client_data_offset =
3109 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3110 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3112 vnic_add_client_data(adapter, vlcd);
3114 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3115 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3116 netdev_dbg(adapter->netdev, "%016lx\n",
3117 ((unsigned long int *)(adapter->login_buf))[i]);
3120 memset(&crq, 0, sizeof(crq));
3121 crq.login.first = IBMVNIC_CRQ_CMD;
3122 crq.login.cmd = LOGIN;
3123 crq.login.ioba = cpu_to_be32(buffer_token);
3124 crq.login.len = cpu_to_be32(buffer_size);
3125 ibmvnic_send_crq(adapter, &crq);
3130 kfree(login_rsp_buffer);
3131 buf_rsp_alloc_failed:
3132 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3134 kfree(login_buffer);
3139 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3142 union ibmvnic_crq crq;
3144 memset(&crq, 0, sizeof(crq));
3145 crq.request_map.first = IBMVNIC_CRQ_CMD;
3146 crq.request_map.cmd = REQUEST_MAP;
3147 crq.request_map.map_id = map_id;
3148 crq.request_map.ioba = cpu_to_be32(addr);
3149 crq.request_map.len = cpu_to_be32(len);
3150 ibmvnic_send_crq(adapter, &crq);
3153 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3155 union ibmvnic_crq crq;
3157 memset(&crq, 0, sizeof(crq));
3158 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3159 crq.request_unmap.cmd = REQUEST_UNMAP;
3160 crq.request_unmap.map_id = map_id;
3161 ibmvnic_send_crq(adapter, &crq);
3164 static void send_map_query(struct ibmvnic_adapter *adapter)
3166 union ibmvnic_crq crq;
3168 memset(&crq, 0, sizeof(crq));
3169 crq.query_map.first = IBMVNIC_CRQ_CMD;
3170 crq.query_map.cmd = QUERY_MAP;
3171 ibmvnic_send_crq(adapter, &crq);
3174 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3175 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3177 union ibmvnic_crq crq;
3179 atomic_set(&adapter->running_cap_crqs, 0);
3180 memset(&crq, 0, sizeof(crq));
3181 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3182 crq.query_capability.cmd = QUERY_CAPABILITY;
3184 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3185 atomic_inc(&adapter->running_cap_crqs);
3186 ibmvnic_send_crq(adapter, &crq);
3188 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3189 atomic_inc(&adapter->running_cap_crqs);
3190 ibmvnic_send_crq(adapter, &crq);
3192 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3193 atomic_inc(&adapter->running_cap_crqs);
3194 ibmvnic_send_crq(adapter, &crq);
3196 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3197 atomic_inc(&adapter->running_cap_crqs);
3198 ibmvnic_send_crq(adapter, &crq);
3200 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3201 atomic_inc(&adapter->running_cap_crqs);
3202 ibmvnic_send_crq(adapter, &crq);
3204 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3205 atomic_inc(&adapter->running_cap_crqs);
3206 ibmvnic_send_crq(adapter, &crq);
3208 crq.query_capability.capability =
3209 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3210 atomic_inc(&adapter->running_cap_crqs);
3211 ibmvnic_send_crq(adapter, &crq);
3213 crq.query_capability.capability =
3214 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3215 atomic_inc(&adapter->running_cap_crqs);
3216 ibmvnic_send_crq(adapter, &crq);
3218 crq.query_capability.capability =
3219 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3220 atomic_inc(&adapter->running_cap_crqs);
3221 ibmvnic_send_crq(adapter, &crq);
3223 crq.query_capability.capability =
3224 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3225 atomic_inc(&adapter->running_cap_crqs);
3226 ibmvnic_send_crq(adapter, &crq);
3228 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3229 atomic_inc(&adapter->running_cap_crqs);
3230 ibmvnic_send_crq(adapter, &crq);
3232 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3233 atomic_inc(&adapter->running_cap_crqs);
3234 ibmvnic_send_crq(adapter, &crq);
3236 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3237 atomic_inc(&adapter->running_cap_crqs);
3238 ibmvnic_send_crq(adapter, &crq);
3240 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3241 atomic_inc(&adapter->running_cap_crqs);
3242 ibmvnic_send_crq(adapter, &crq);
3244 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3245 atomic_inc(&adapter->running_cap_crqs);
3246 ibmvnic_send_crq(adapter, &crq);
3248 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3249 atomic_inc(&adapter->running_cap_crqs);
3250 ibmvnic_send_crq(adapter, &crq);
3252 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3253 atomic_inc(&adapter->running_cap_crqs);
3254 ibmvnic_send_crq(adapter, &crq);
3256 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3257 atomic_inc(&adapter->running_cap_crqs);
3258 ibmvnic_send_crq(adapter, &crq);
3260 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3261 atomic_inc(&adapter->running_cap_crqs);
3262 ibmvnic_send_crq(adapter, &crq);
3264 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3265 atomic_inc(&adapter->running_cap_crqs);
3266 ibmvnic_send_crq(adapter, &crq);
3268 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3269 atomic_inc(&adapter->running_cap_crqs);
3270 ibmvnic_send_crq(adapter, &crq);
3272 crq.query_capability.capability =
3273 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3274 atomic_inc(&adapter->running_cap_crqs);
3275 ibmvnic_send_crq(adapter, &crq);
3277 crq.query_capability.capability =
3278 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3279 atomic_inc(&adapter->running_cap_crqs);
3280 ibmvnic_send_crq(adapter, &crq);
3282 crq.query_capability.capability =
3283 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3284 atomic_inc(&adapter->running_cap_crqs);
3285 ibmvnic_send_crq(adapter, &crq);
3287 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3288 atomic_inc(&adapter->running_cap_crqs);
3289 ibmvnic_send_crq(adapter, &crq);
3292 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3293 struct ibmvnic_adapter *adapter)
3295 struct device *dev = &adapter->vdev->dev;
3297 if (crq->get_vpd_size_rsp.rc.code) {
3298 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3299 crq->get_vpd_size_rsp.rc.code);
3300 complete(&adapter->fw_done);
3304 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3305 complete(&adapter->fw_done);
3308 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3309 struct ibmvnic_adapter *adapter)
3311 struct device *dev = &adapter->vdev->dev;
3312 unsigned char *substr = NULL;
3313 u8 fw_level_len = 0;
3315 memset(adapter->fw_version, 0, 32);
3317 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3320 if (crq->get_vpd_rsp.rc.code) {
3321 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3322 crq->get_vpd_rsp.rc.code);
3326 /* get the position of the firmware version info
3327 * located after the ASCII 'RM' substring in the buffer
3329 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3331 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3335 /* get length of firmware level ASCII substring */
3336 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3337 fw_level_len = *(substr + 2);
3339 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3343 /* copy firmware version string from vpd into adapter */
3344 if ((substr + 3 + fw_level_len) <
3345 (adapter->vpd->buff + adapter->vpd->len)) {
3346 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3348 dev_info(dev, "FW substr extrapolated VPD buff\n");
3352 if (adapter->fw_version[0] == '\0')
3353 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3354 complete(&adapter->fw_done);
3357 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3359 struct device *dev = &adapter->vdev->dev;
3360 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3361 union ibmvnic_crq crq;
3364 dma_unmap_single(dev, adapter->ip_offload_tok,
3365 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3367 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3368 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3369 netdev_dbg(adapter->netdev, "%016lx\n",
3370 ((unsigned long int *)(buf))[i]);
3372 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3373 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3374 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3375 buf->tcp_ipv4_chksum);
3376 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3377 buf->tcp_ipv6_chksum);
3378 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3379 buf->udp_ipv4_chksum);
3380 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3381 buf->udp_ipv6_chksum);
3382 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3383 buf->large_tx_ipv4);
3384 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3385 buf->large_tx_ipv6);
3386 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3387 buf->large_rx_ipv4);
3388 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3389 buf->large_rx_ipv6);
3390 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3391 buf->max_ipv4_header_size);
3392 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3393 buf->max_ipv6_header_size);
3394 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3395 buf->max_tcp_header_size);
3396 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3397 buf->max_udp_header_size);
3398 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3399 buf->max_large_tx_size);
3400 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3401 buf->max_large_rx_size);
3402 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3403 buf->ipv6_extension_header);
3404 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3405 buf->tcp_pseudosum_req);
3406 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3407 buf->num_ipv6_ext_headers);
3408 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3409 buf->off_ipv6_ext_headers);
3411 adapter->ip_offload_ctrl_tok =
3412 dma_map_single(dev, &adapter->ip_offload_ctrl,
3413 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3415 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3416 dev_err(dev, "Couldn't map ip offload control buffer\n");
3420 adapter->ip_offload_ctrl.len =
3421 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3422 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3423 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3424 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3425 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3426 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3427 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3428 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3429 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3430 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3432 /* large_rx disabled for now, additional features needed */
3433 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3434 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3436 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3438 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3439 adapter->netdev->features |= NETIF_F_IP_CSUM;
3441 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3442 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3444 if ((adapter->netdev->features &
3445 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3446 adapter->netdev->features |= NETIF_F_RXCSUM;
3448 if (buf->large_tx_ipv4)
3449 adapter->netdev->features |= NETIF_F_TSO;
3450 if (buf->large_tx_ipv6)
3451 adapter->netdev->features |= NETIF_F_TSO6;
3453 adapter->netdev->hw_features |= adapter->netdev->features;
3455 memset(&crq, 0, sizeof(crq));
3456 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3457 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3458 crq.control_ip_offload.len =
3459 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3460 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3461 ibmvnic_send_crq(adapter, &crq);
3464 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3465 struct ibmvnic_adapter *adapter)
3467 struct device *dev = &adapter->vdev->dev;
3468 struct ibmvnic_error_buff *error_buff, *tmp;
3469 unsigned long flags;
3473 if (!crq->request_error_rsp.rc.code) {
3474 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3475 crq->request_error_rsp.rc.code);
3479 spin_lock_irqsave(&adapter->error_list_lock, flags);
3480 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3481 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3483 list_del(&error_buff->list);
3486 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3489 dev_err(dev, "Couldn't find error id %x\n",
3490 be32_to_cpu(crq->request_error_rsp.error_id));
3494 dev_err(dev, "Detailed info for error id %x:",
3495 be32_to_cpu(crq->request_error_rsp.error_id));
3497 for (i = 0; i < error_buff->len; i++) {
3498 pr_cont("%02x", (int)error_buff->buff[i]);
3504 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3506 kfree(error_buff->buff);
3510 static void request_error_information(struct ibmvnic_adapter *adapter,
3511 union ibmvnic_crq *err_crq)
3513 struct device *dev = &adapter->vdev->dev;
3514 struct net_device *netdev = adapter->netdev;
3515 struct ibmvnic_error_buff *error_buff;
3516 unsigned long timeout = msecs_to_jiffies(30000);
3517 union ibmvnic_crq crq;
3518 unsigned long flags;
3521 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3525 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3526 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3527 if (!error_buff->buff) {
3532 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3534 if (dma_mapping_error(dev, error_buff->dma)) {
3535 netdev_err(netdev, "Couldn't map error buffer\n");
3536 kfree(error_buff->buff);
3541 error_buff->len = detail_len;
3542 error_buff->error_id = err_crq->error_indication.error_id;
3544 spin_lock_irqsave(&adapter->error_list_lock, flags);
3545 list_add_tail(&error_buff->list, &adapter->errors);
3546 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3548 memset(&crq, 0, sizeof(crq));
3549 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3550 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3551 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3552 crq.request_error_info.len = cpu_to_be32(detail_len);
3553 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3555 rc = ibmvnic_send_crq(adapter, &crq);
3557 netdev_err(netdev, "failed to request error information\n");
3561 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3562 netdev_err(netdev, "timeout waiting for error information\n");
3569 spin_lock_irqsave(&adapter->error_list_lock, flags);
3570 list_del(&error_buff->list);
3571 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3573 kfree(error_buff->buff);
3577 static void handle_error_indication(union ibmvnic_crq *crq,
3578 struct ibmvnic_adapter *adapter)
3580 struct device *dev = &adapter->vdev->dev;
3582 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3583 crq->error_indication.flags
3584 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3585 be32_to_cpu(crq->error_indication.error_id),
3586 be16_to_cpu(crq->error_indication.error_cause));
3588 if (be32_to_cpu(crq->error_indication.error_id))
3589 request_error_information(adapter, crq);
3591 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3592 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3594 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3597 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3598 struct ibmvnic_adapter *adapter)
3600 struct net_device *netdev = adapter->netdev;
3601 struct device *dev = &adapter->vdev->dev;
3604 rc = crq->change_mac_addr_rsp.rc.code;
3606 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3609 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3612 complete(&adapter->fw_done);
3616 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3617 struct ibmvnic_adapter *adapter)
3619 struct device *dev = &adapter->vdev->dev;
3623 atomic_dec(&adapter->running_cap_crqs);
3624 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3626 req_value = &adapter->req_tx_queues;
3630 req_value = &adapter->req_rx_queues;
3633 case REQ_RX_ADD_QUEUES:
3634 req_value = &adapter->req_rx_add_queues;
3637 case REQ_TX_ENTRIES_PER_SUBCRQ:
3638 req_value = &adapter->req_tx_entries_per_subcrq;
3639 name = "tx_entries_per_subcrq";
3641 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3642 req_value = &adapter->req_rx_add_entries_per_subcrq;
3643 name = "rx_add_entries_per_subcrq";
3646 req_value = &adapter->req_mtu;
3649 case PROMISC_REQUESTED:
3650 req_value = &adapter->promisc;
3654 dev_err(dev, "Got invalid cap request rsp %d\n",
3655 crq->request_capability.capability);
3659 switch (crq->request_capability_rsp.rc.code) {
3662 case PARTIALSUCCESS:
3663 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3665 (long int)be64_to_cpu(crq->request_capability_rsp.
3668 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3670 pr_err("mtu of %llu is not supported. Reverting.\n",
3672 *req_value = adapter->fallback.mtu;
3675 be64_to_cpu(crq->request_capability_rsp.number);
3678 ibmvnic_send_req_caps(adapter, 1);
3681 dev_err(dev, "Error %d in request cap rsp\n",
3682 crq->request_capability_rsp.rc.code);
3686 /* Done receiving requested capabilities, query IP offload support */
3687 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3688 union ibmvnic_crq newcrq;
3689 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3690 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3691 &adapter->ip_offload_buf;
3693 adapter->wait_capability = false;
3694 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3698 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3699 if (!firmware_has_feature(FW_FEATURE_CMO))
3700 dev_err(dev, "Couldn't map offload buffer\n");
3704 memset(&newcrq, 0, sizeof(newcrq));
3705 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3706 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3707 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3708 newcrq.query_ip_offload.ioba =
3709 cpu_to_be32(adapter->ip_offload_tok);
3711 ibmvnic_send_crq(adapter, &newcrq);
3715 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3716 struct ibmvnic_adapter *adapter)
3718 struct device *dev = &adapter->vdev->dev;
3719 struct net_device *netdev = adapter->netdev;
3720 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3721 struct ibmvnic_login_buffer *login = adapter->login_buf;
3724 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3726 release_login_buffer(adapter);
3727 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3728 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3730 /* If the number of queues requested can't be allocated by the
3731 * server, the login response will return with code 1. We will need
3732 * to resend the login buffer with fewer queues requested.
3734 if (login_rsp_crq->generic.rc.code) {
3735 adapter->renegotiate = true;
3736 complete(&adapter->init_done);
3740 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3742 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3743 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3744 netdev_dbg(adapter->netdev, "%016lx\n",
3745 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3749 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3750 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3751 adapter->req_rx_add_queues !=
3752 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3753 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3754 ibmvnic_remove(adapter->vdev);
3757 complete(&adapter->init_done);
3762 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3763 struct ibmvnic_adapter *adapter)
3765 struct device *dev = &adapter->vdev->dev;
3768 rc = crq->request_unmap_rsp.rc.code;
3770 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3773 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3774 struct ibmvnic_adapter *adapter)
3776 struct net_device *netdev = adapter->netdev;
3777 struct device *dev = &adapter->vdev->dev;
3780 rc = crq->query_map_rsp.rc.code;
3782 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3785 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3786 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3787 crq->query_map_rsp.free_pages);
3790 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3791 struct ibmvnic_adapter *adapter)
3793 struct net_device *netdev = adapter->netdev;
3794 struct device *dev = &adapter->vdev->dev;
3797 atomic_dec(&adapter->running_cap_crqs);
3798 netdev_dbg(netdev, "Outstanding queries: %d\n",
3799 atomic_read(&adapter->running_cap_crqs));
3800 rc = crq->query_capability.rc.code;
3802 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3806 switch (be16_to_cpu(crq->query_capability.capability)) {
3808 adapter->min_tx_queues =
3809 be64_to_cpu(crq->query_capability.number);
3810 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3811 adapter->min_tx_queues);
3814 adapter->min_rx_queues =
3815 be64_to_cpu(crq->query_capability.number);
3816 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3817 adapter->min_rx_queues);
3819 case MIN_RX_ADD_QUEUES:
3820 adapter->min_rx_add_queues =
3821 be64_to_cpu(crq->query_capability.number);
3822 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3823 adapter->min_rx_add_queues);
3826 adapter->max_tx_queues =
3827 be64_to_cpu(crq->query_capability.number);
3828 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3829 adapter->max_tx_queues);
3832 adapter->max_rx_queues =
3833 be64_to_cpu(crq->query_capability.number);
3834 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3835 adapter->max_rx_queues);
3837 case MAX_RX_ADD_QUEUES:
3838 adapter->max_rx_add_queues =
3839 be64_to_cpu(crq->query_capability.number);
3840 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3841 adapter->max_rx_add_queues);
3843 case MIN_TX_ENTRIES_PER_SUBCRQ:
3844 adapter->min_tx_entries_per_subcrq =
3845 be64_to_cpu(crq->query_capability.number);
3846 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3847 adapter->min_tx_entries_per_subcrq);
3849 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3850 adapter->min_rx_add_entries_per_subcrq =
3851 be64_to_cpu(crq->query_capability.number);
3852 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3853 adapter->min_rx_add_entries_per_subcrq);
3855 case MAX_TX_ENTRIES_PER_SUBCRQ:
3856 adapter->max_tx_entries_per_subcrq =
3857 be64_to_cpu(crq->query_capability.number);
3858 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3859 adapter->max_tx_entries_per_subcrq);
3861 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3862 adapter->max_rx_add_entries_per_subcrq =
3863 be64_to_cpu(crq->query_capability.number);
3864 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3865 adapter->max_rx_add_entries_per_subcrq);
3867 case TCP_IP_OFFLOAD:
3868 adapter->tcp_ip_offload =
3869 be64_to_cpu(crq->query_capability.number);
3870 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3871 adapter->tcp_ip_offload);
3873 case PROMISC_SUPPORTED:
3874 adapter->promisc_supported =
3875 be64_to_cpu(crq->query_capability.number);
3876 netdev_dbg(netdev, "promisc_supported = %lld\n",
3877 adapter->promisc_supported);
3880 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3881 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3882 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3885 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3886 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3887 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3889 case MAX_MULTICAST_FILTERS:
3890 adapter->max_multicast_filters =
3891 be64_to_cpu(crq->query_capability.number);
3892 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3893 adapter->max_multicast_filters);
3895 case VLAN_HEADER_INSERTION:
3896 adapter->vlan_header_insertion =
3897 be64_to_cpu(crq->query_capability.number);
3898 if (adapter->vlan_header_insertion)
3899 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3900 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3901 adapter->vlan_header_insertion);
3903 case RX_VLAN_HEADER_INSERTION:
3904 adapter->rx_vlan_header_insertion =
3905 be64_to_cpu(crq->query_capability.number);
3906 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3907 adapter->rx_vlan_header_insertion);
3909 case MAX_TX_SG_ENTRIES:
3910 adapter->max_tx_sg_entries =
3911 be64_to_cpu(crq->query_capability.number);
3912 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3913 adapter->max_tx_sg_entries);
3915 case RX_SG_SUPPORTED:
3916 adapter->rx_sg_supported =
3917 be64_to_cpu(crq->query_capability.number);
3918 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3919 adapter->rx_sg_supported);
3921 case OPT_TX_COMP_SUB_QUEUES:
3922 adapter->opt_tx_comp_sub_queues =
3923 be64_to_cpu(crq->query_capability.number);
3924 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3925 adapter->opt_tx_comp_sub_queues);
3927 case OPT_RX_COMP_QUEUES:
3928 adapter->opt_rx_comp_queues =
3929 be64_to_cpu(crq->query_capability.number);
3930 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3931 adapter->opt_rx_comp_queues);
3933 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3934 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3935 be64_to_cpu(crq->query_capability.number);
3936 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3937 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3939 case OPT_TX_ENTRIES_PER_SUBCRQ:
3940 adapter->opt_tx_entries_per_subcrq =
3941 be64_to_cpu(crq->query_capability.number);
3942 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3943 adapter->opt_tx_entries_per_subcrq);
3945 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3946 adapter->opt_rxba_entries_per_subcrq =
3947 be64_to_cpu(crq->query_capability.number);
3948 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3949 adapter->opt_rxba_entries_per_subcrq);
3951 case TX_RX_DESC_REQ:
3952 adapter->tx_rx_desc_req = crq->query_capability.number;
3953 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3954 adapter->tx_rx_desc_req);
3958 netdev_err(netdev, "Got invalid cap rsp %d\n",
3959 crq->query_capability.capability);
3963 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3964 adapter->wait_capability = false;
3965 ibmvnic_send_req_caps(adapter, 0);
3969 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3970 struct ibmvnic_adapter *adapter)
3972 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3973 struct net_device *netdev = adapter->netdev;
3974 struct device *dev = &adapter->vdev->dev;
3975 u64 *u64_crq = (u64 *)crq;
3978 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3979 (unsigned long int)cpu_to_be64(u64_crq[0]),
3980 (unsigned long int)cpu_to_be64(u64_crq[1]));
3981 switch (gen_crq->first) {
3982 case IBMVNIC_CRQ_INIT_RSP:
3983 switch (gen_crq->cmd) {
3984 case IBMVNIC_CRQ_INIT:
3985 dev_info(dev, "Partner initialized\n");
3986 adapter->from_passive_init = true;
3987 complete(&adapter->init_done);
3989 case IBMVNIC_CRQ_INIT_COMPLETE:
3990 dev_info(dev, "Partner initialization complete\n");
3991 send_version_xchg(adapter);
3994 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3997 case IBMVNIC_CRQ_XPORT_EVENT:
3998 netif_carrier_off(netdev);
3999 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4000 dev_info(dev, "Migrated, re-enabling adapter\n");
4001 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4002 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4003 dev_info(dev, "Backing device failover detected\n");
4004 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4006 /* The adapter lost the connection */
4007 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4009 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4012 case IBMVNIC_CRQ_CMD_RSP:
4015 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4020 switch (gen_crq->cmd) {
4021 case VERSION_EXCHANGE_RSP:
4022 rc = crq->version_exchange_rsp.rc.code;
4024 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4027 dev_info(dev, "Partner protocol version is %d\n",
4028 crq->version_exchange_rsp.version);
4029 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4032 be16_to_cpu(crq->version_exchange_rsp.version);
4033 send_cap_queries(adapter);
4035 case QUERY_CAPABILITY_RSP:
4036 handle_query_cap_rsp(crq, adapter);
4039 handle_query_map_rsp(crq, adapter);
4041 case REQUEST_MAP_RSP:
4042 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4043 complete(&adapter->fw_done);
4045 case REQUEST_UNMAP_RSP:
4046 handle_request_unmap_rsp(crq, adapter);
4048 case REQUEST_CAPABILITY_RSP:
4049 handle_request_cap_rsp(crq, adapter);
4052 netdev_dbg(netdev, "Got Login Response\n");
4053 handle_login_rsp(crq, adapter);
4055 case LOGICAL_LINK_STATE_RSP:
4057 "Got Logical Link State Response, state: %d rc: %d\n",
4058 crq->logical_link_state_rsp.link_state,
4059 crq->logical_link_state_rsp.rc.code);
4060 adapter->logical_link_state =
4061 crq->logical_link_state_rsp.link_state;
4062 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4063 complete(&adapter->init_done);
4065 case LINK_STATE_INDICATION:
4066 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4067 adapter->phys_link_state =
4068 crq->link_state_indication.phys_link_state;
4069 adapter->logical_link_state =
4070 crq->link_state_indication.logical_link_state;
4072 case CHANGE_MAC_ADDR_RSP:
4073 netdev_dbg(netdev, "Got MAC address change Response\n");
4074 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4076 case ERROR_INDICATION:
4077 netdev_dbg(netdev, "Got Error Indication\n");
4078 handle_error_indication(crq, adapter);
4080 case REQUEST_ERROR_RSP:
4081 netdev_dbg(netdev, "Got Error Detail Response\n");
4082 handle_error_info_rsp(crq, adapter);
4084 case REQUEST_STATISTICS_RSP:
4085 netdev_dbg(netdev, "Got Statistics Response\n");
4086 complete(&adapter->stats_done);
4088 case QUERY_IP_OFFLOAD_RSP:
4089 netdev_dbg(netdev, "Got Query IP offload Response\n");
4090 handle_query_ip_offload_rsp(adapter);
4092 case MULTICAST_CTRL_RSP:
4093 netdev_dbg(netdev, "Got multicast control Response\n");
4095 case CONTROL_IP_OFFLOAD_RSP:
4096 netdev_dbg(netdev, "Got Control IP offload Response\n");
4097 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4098 sizeof(adapter->ip_offload_ctrl),
4100 complete(&adapter->init_done);
4102 case COLLECT_FW_TRACE_RSP:
4103 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4104 complete(&adapter->fw_done);
4106 case GET_VPD_SIZE_RSP:
4107 handle_vpd_size_rsp(crq, adapter);
4110 handle_vpd_rsp(crq, adapter);
4113 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4118 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4120 struct ibmvnic_adapter *adapter = instance;
4122 tasklet_schedule(&adapter->tasklet);
4126 static void ibmvnic_tasklet(void *data)
4128 struct ibmvnic_adapter *adapter = data;
4129 struct ibmvnic_crq_queue *queue = &adapter->crq;
4130 union ibmvnic_crq *crq;
4131 unsigned long flags;
4134 spin_lock_irqsave(&queue->lock, flags);
4136 /* Pull all the valid messages off the CRQ */
4137 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4138 ibmvnic_handle_crq(crq, adapter);
4139 crq->generic.first = 0;
4142 /* remain in tasklet until all
4143 * capabilities responses are received
4145 if (!adapter->wait_capability)
4148 /* if capabilities CRQ's were sent in this tasklet, the following
4149 * tasklet must wait until all responses are received
4151 if (atomic_read(&adapter->running_cap_crqs) != 0)
4152 adapter->wait_capability = true;
4153 spin_unlock_irqrestore(&queue->lock, flags);
4156 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4158 struct vio_dev *vdev = adapter->vdev;
4162 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4163 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4166 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4171 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4173 struct ibmvnic_crq_queue *crq = &adapter->crq;
4174 struct device *dev = &adapter->vdev->dev;
4175 struct vio_dev *vdev = adapter->vdev;
4180 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4181 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4183 /* Clean out the queue */
4184 memset(crq->msgs, 0, PAGE_SIZE);
4187 /* And re-open it again */
4188 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4189 crq->msg_token, PAGE_SIZE);
4192 /* Adapter is good, but other end is not ready */
4193 dev_warn(dev, "Partner adapter not ready\n");
4195 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4200 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4202 struct ibmvnic_crq_queue *crq = &adapter->crq;
4203 struct vio_dev *vdev = adapter->vdev;
4209 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4210 free_irq(vdev->irq, adapter);
4211 tasklet_kill(&adapter->tasklet);
4213 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4214 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4216 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4218 free_page((unsigned long)crq->msgs);
4222 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4224 struct ibmvnic_crq_queue *crq = &adapter->crq;
4225 struct device *dev = &adapter->vdev->dev;
4226 struct vio_dev *vdev = adapter->vdev;
4227 int rc, retrc = -ENOMEM;
4232 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4233 /* Should we allocate more than one page? */
4238 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4239 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4241 if (dma_mapping_error(dev, crq->msg_token))
4244 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4245 crq->msg_token, PAGE_SIZE);
4247 if (rc == H_RESOURCE)
4248 /* maybe kexecing and resource is busy. try a reset */
4249 rc = ibmvnic_reset_crq(adapter);
4252 if (rc == H_CLOSED) {
4253 dev_warn(dev, "Partner adapter not ready\n");
4255 dev_warn(dev, "Error %d opening adapter\n", rc);
4256 goto reg_crq_failed;
4261 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4262 (unsigned long)adapter);
4264 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4265 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4268 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4270 goto req_irq_failed;
4273 rc = vio_enable_interrupts(vdev);
4275 dev_err(dev, "Error %d enabling interrupts\n", rc);
4276 goto req_irq_failed;
4280 spin_lock_init(&crq->lock);
4285 tasklet_kill(&adapter->tasklet);
4287 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4288 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4290 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4292 free_page((unsigned long)crq->msgs);
4297 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4299 struct device *dev = &adapter->vdev->dev;
4300 unsigned long timeout = msecs_to_jiffies(30000);
4303 if (adapter->resetting && !adapter->wait_for_reset) {
4304 rc = ibmvnic_reset_crq(adapter);
4306 rc = vio_enable_interrupts(adapter->vdev);
4308 rc = init_crq_queue(adapter);
4312 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4316 adapter->from_passive_init = false;
4318 init_completion(&adapter->init_done);
4319 adapter->init_done_rc = 0;
4320 ibmvnic_send_crq_init(adapter);
4321 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4322 dev_err(dev, "Initialization sequence timed out\n");
4326 if (adapter->init_done_rc) {
4327 release_crq_queue(adapter);
4328 return adapter->init_done_rc;
4331 if (adapter->from_passive_init) {
4332 adapter->state = VNIC_OPEN;
4333 adapter->from_passive_init = false;
4337 if (adapter->resetting && !adapter->wait_for_reset)
4338 rc = reset_sub_crq_queues(adapter);
4340 rc = init_sub_crqs(adapter);
4342 dev_err(dev, "Initialization of sub crqs failed\n");
4343 release_crq_queue(adapter);
4347 rc = init_sub_crq_irqs(adapter);
4349 dev_err(dev, "Failed to initialize sub crq irqs\n");
4350 release_crq_queue(adapter);
4356 static struct device_attribute dev_attr_failover;
4358 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4360 struct ibmvnic_adapter *adapter;
4361 struct net_device *netdev;
4362 unsigned char *mac_addr_p;
4365 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4368 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4369 VETH_MAC_ADDR, NULL);
4372 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4373 __FILE__, __LINE__);
4377 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4378 IBMVNIC_MAX_QUEUES);
4382 adapter = netdev_priv(netdev);
4383 adapter->state = VNIC_PROBING;
4384 dev_set_drvdata(&dev->dev, netdev);
4385 adapter->vdev = dev;
4386 adapter->netdev = netdev;
4388 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4389 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4390 netdev->irq = dev->irq;
4391 netdev->netdev_ops = &ibmvnic_netdev_ops;
4392 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4393 SET_NETDEV_DEV(netdev, &dev->dev);
4395 spin_lock_init(&adapter->stats_lock);
4397 INIT_LIST_HEAD(&adapter->errors);
4398 spin_lock_init(&adapter->error_list_lock);
4400 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4401 INIT_LIST_HEAD(&adapter->rwi_list);
4402 mutex_init(&adapter->reset_lock);
4403 mutex_init(&adapter->rwi_lock);
4404 adapter->resetting = false;
4406 adapter->mac_change_pending = false;
4409 rc = ibmvnic_init(adapter);
4410 if (rc && rc != EAGAIN)
4411 goto ibmvnic_init_fail;
4412 } while (rc == EAGAIN);
4414 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4415 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4416 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4418 rc = device_create_file(&dev->dev, &dev_attr_failover);
4420 goto ibmvnic_init_fail;
4422 netif_carrier_off(netdev);
4423 rc = register_netdev(netdev);
4425 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4426 goto ibmvnic_register_fail;
4428 dev_info(&dev->dev, "ibmvnic registered\n");
4430 adapter->state = VNIC_PROBED;
4432 adapter->wait_for_reset = false;
4436 ibmvnic_register_fail:
4437 device_remove_file(&dev->dev, &dev_attr_failover);
4440 release_sub_crqs(adapter);
4441 release_crq_queue(adapter);
4442 free_netdev(netdev);
4447 static int ibmvnic_remove(struct vio_dev *dev)
4449 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4450 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4452 adapter->state = VNIC_REMOVING;
4453 unregister_netdev(netdev);
4454 mutex_lock(&adapter->reset_lock);
4456 release_resources(adapter);
4457 release_sub_crqs(adapter);
4458 release_crq_queue(adapter);
4460 adapter->state = VNIC_REMOVED;
4462 mutex_unlock(&adapter->reset_lock);
4463 device_remove_file(&dev->dev, &dev_attr_failover);
4464 free_netdev(netdev);
4465 dev_set_drvdata(&dev->dev, NULL);
4470 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4471 const char *buf, size_t count)
4473 struct net_device *netdev = dev_get_drvdata(dev);
4474 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4475 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4476 __be64 session_token;
4479 if (!sysfs_streq(buf, "1"))
4482 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4483 H_GET_SESSION_TOKEN, 0, 0, 0);
4485 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4490 session_token = (__be64)retbuf[0];
4491 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4492 be64_to_cpu(session_token));
4493 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4494 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4496 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4504 static DEVICE_ATTR_WO(failover);
4506 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4508 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4509 struct ibmvnic_adapter *adapter;
4510 struct iommu_table *tbl;
4511 unsigned long ret = 0;
4514 tbl = get_iommu_table_base(&vdev->dev);
4516 /* netdev inits at probe time along with the structures we need below*/
4518 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4520 adapter = netdev_priv(netdev);
4522 ret += PAGE_SIZE; /* the crq message queue */
4523 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4525 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4526 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4528 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4530 ret += adapter->rx_pool[i].size *
4531 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4536 static int ibmvnic_resume(struct device *dev)
4538 struct net_device *netdev = dev_get_drvdata(dev);
4539 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4541 if (adapter->state != VNIC_OPEN)
4544 tasklet_schedule(&adapter->tasklet);
4549 static const struct vio_device_id ibmvnic_device_table[] = {
4550 {"network", "IBM,vnic"},
4553 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4555 static const struct dev_pm_ops ibmvnic_pm_ops = {
4556 .resume = ibmvnic_resume
4559 static struct vio_driver ibmvnic_driver = {
4560 .id_table = ibmvnic_device_table,
4561 .probe = ibmvnic_probe,
4562 .remove = ibmvnic_remove,
4563 .get_desired_dma = ibmvnic_get_desired_dma,
4564 .name = ibmvnic_driver_name,
4565 .pm = &ibmvnic_pm_ops,
4568 /* module functions */
4569 static int __init ibmvnic_module_init(void)
4571 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4572 IBMVNIC_DRIVER_VERSION);
4574 return vio_register_driver(&ibmvnic_driver);
4577 static void __exit ibmvnic_module_exit(void)
4579 vio_unregister_driver(&ibmvnic_driver);
4582 module_init(ibmvnic_module_init);
4583 module_exit(ibmvnic_module_exit);