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);
359 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
361 adapter->tx_stats_buffers =
362 kcalloc(adapter->req_tx_queues,
363 sizeof(struct ibmvnic_tx_queue_stats),
365 if (!adapter->tx_stats_buffers)
368 adapter->rx_stats_buffers =
369 kcalloc(adapter->req_rx_queues,
370 sizeof(struct ibmvnic_rx_queue_stats),
372 if (!adapter->rx_stats_buffers)
378 static void release_stats_token(struct ibmvnic_adapter *adapter)
380 struct device *dev = &adapter->vdev->dev;
382 if (!adapter->stats_token)
385 dma_unmap_single(dev, adapter->stats_token,
386 sizeof(struct ibmvnic_statistics),
388 adapter->stats_token = 0;
391 static int init_stats_token(struct ibmvnic_adapter *adapter)
393 struct device *dev = &adapter->vdev->dev;
396 stok = dma_map_single(dev, &adapter->stats,
397 sizeof(struct ibmvnic_statistics),
399 if (dma_mapping_error(dev, stok)) {
400 dev_err(dev, "Couldn't map stats buffer\n");
404 adapter->stats_token = stok;
405 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
409 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
411 struct ibmvnic_rx_pool *rx_pool;
415 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
416 for (i = 0; i < rx_scrqs; i++) {
417 rx_pool = &adapter->rx_pool[i];
419 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
421 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
425 for (j = 0; j < rx_pool->size; j++)
426 rx_pool->free_map[j] = j;
428 memset(rx_pool->rx_buff, 0,
429 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
431 atomic_set(&rx_pool->available, 0);
432 rx_pool->next_alloc = 0;
433 rx_pool->next_free = 0;
440 static void release_rx_pools(struct ibmvnic_adapter *adapter)
442 struct ibmvnic_rx_pool *rx_pool;
446 if (!adapter->rx_pool)
449 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
450 for (i = 0; i < rx_scrqs; i++) {
451 rx_pool = &adapter->rx_pool[i];
453 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
455 kfree(rx_pool->free_map);
456 free_long_term_buff(adapter, &rx_pool->long_term_buff);
458 if (!rx_pool->rx_buff)
461 for (j = 0; j < rx_pool->size; j++) {
462 if (rx_pool->rx_buff[j].skb) {
463 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
464 rx_pool->rx_buff[i].skb = NULL;
468 kfree(rx_pool->rx_buff);
471 kfree(adapter->rx_pool);
472 adapter->rx_pool = NULL;
475 static int init_rx_pools(struct net_device *netdev)
477 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
478 struct device *dev = &adapter->vdev->dev;
479 struct ibmvnic_rx_pool *rx_pool;
485 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
486 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
487 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
489 adapter->rx_pool = kcalloc(rxadd_subcrqs,
490 sizeof(struct ibmvnic_rx_pool),
492 if (!adapter->rx_pool) {
493 dev_err(dev, "Failed to allocate rx pools\n");
497 for (i = 0; i < rxadd_subcrqs; i++) {
498 rx_pool = &adapter->rx_pool[i];
500 netdev_dbg(adapter->netdev,
501 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
502 i, adapter->req_rx_add_entries_per_subcrq,
503 be64_to_cpu(size_array[i]));
505 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
507 rx_pool->buff_size = be64_to_cpu(size_array[i]);
510 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
512 if (!rx_pool->free_map) {
513 release_rx_pools(adapter);
517 rx_pool->rx_buff = kcalloc(rx_pool->size,
518 sizeof(struct ibmvnic_rx_buff),
520 if (!rx_pool->rx_buff) {
521 dev_err(dev, "Couldn't alloc rx buffers\n");
522 release_rx_pools(adapter);
526 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
527 rx_pool->size * rx_pool->buff_size)) {
528 release_rx_pools(adapter);
532 for (j = 0; j < rx_pool->size; ++j)
533 rx_pool->free_map[j] = j;
535 atomic_set(&rx_pool->available, 0);
536 rx_pool->next_alloc = 0;
537 rx_pool->next_free = 0;
543 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
545 struct ibmvnic_tx_pool *tx_pool;
549 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
550 for (i = 0; i < tx_scrqs; i++) {
551 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
553 tx_pool = &adapter->tx_pool[i];
555 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
559 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
563 memset(tx_pool->tx_buff, 0,
564 adapter->req_tx_entries_per_subcrq *
565 sizeof(struct ibmvnic_tx_buff));
567 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
568 tx_pool->free_map[j] = j;
570 tx_pool->consumer_index = 0;
571 tx_pool->producer_index = 0;
572 tx_pool->tso_index = 0;
578 static void release_vpd_data(struct ibmvnic_adapter *adapter)
583 kfree(adapter->vpd->buff);
587 static void release_tx_pools(struct ibmvnic_adapter *adapter)
589 struct ibmvnic_tx_pool *tx_pool;
592 if (!adapter->tx_pool)
595 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
596 for (i = 0; i < tx_scrqs; i++) {
597 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
598 tx_pool = &adapter->tx_pool[i];
599 kfree(tx_pool->tx_buff);
600 free_long_term_buff(adapter, &tx_pool->long_term_buff);
601 free_long_term_buff(adapter, &tx_pool->tso_ltb);
602 kfree(tx_pool->free_map);
605 kfree(adapter->tx_pool);
606 adapter->tx_pool = NULL;
609 static int init_tx_pools(struct net_device *netdev)
611 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
612 struct device *dev = &adapter->vdev->dev;
613 struct ibmvnic_tx_pool *tx_pool;
617 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
618 adapter->tx_pool = kcalloc(tx_subcrqs,
619 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
620 if (!adapter->tx_pool)
623 for (i = 0; i < tx_subcrqs; i++) {
624 tx_pool = &adapter->tx_pool[i];
626 netdev_dbg(adapter->netdev,
627 "Initializing tx_pool[%d], %lld buffs\n",
628 i, adapter->req_tx_entries_per_subcrq);
630 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
631 sizeof(struct ibmvnic_tx_buff),
633 if (!tx_pool->tx_buff) {
634 dev_err(dev, "tx pool buffer allocation failed\n");
635 release_tx_pools(adapter);
639 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
640 adapter->req_tx_entries_per_subcrq *
642 release_tx_pools(adapter);
647 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
649 IBMVNIC_TSO_BUF_SZ)) {
650 release_tx_pools(adapter);
654 tx_pool->tso_index = 0;
656 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
657 sizeof(int), GFP_KERNEL);
658 if (!tx_pool->free_map) {
659 release_tx_pools(adapter);
663 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
664 tx_pool->free_map[j] = j;
666 tx_pool->consumer_index = 0;
667 tx_pool->producer_index = 0;
673 static void release_error_buffers(struct ibmvnic_adapter *adapter)
675 struct device *dev = &adapter->vdev->dev;
676 struct ibmvnic_error_buff *error_buff, *tmp;
679 spin_lock_irqsave(&adapter->error_list_lock, flags);
680 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
681 list_del(&error_buff->list);
682 dma_unmap_single(dev, error_buff->dma, error_buff->len,
684 kfree(error_buff->buff);
687 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
690 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
694 if (adapter->napi_enabled)
697 for (i = 0; i < adapter->req_rx_queues; i++)
698 napi_enable(&adapter->napi[i]);
700 adapter->napi_enabled = true;
703 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
707 if (!adapter->napi_enabled)
710 for (i = 0; i < adapter->req_rx_queues; i++) {
711 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
712 napi_disable(&adapter->napi[i]);
715 adapter->napi_enabled = false;
718 static int ibmvnic_login(struct net_device *netdev)
720 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
721 unsigned long timeout = msecs_to_jiffies(30000);
722 struct device *dev = &adapter->vdev->dev;
726 if (adapter->renegotiate) {
727 adapter->renegotiate = false;
728 release_sub_crqs(adapter);
730 reinit_completion(&adapter->init_done);
731 send_cap_queries(adapter);
732 if (!wait_for_completion_timeout(&adapter->init_done,
734 dev_err(dev, "Capabilities query timeout\n");
737 rc = init_sub_crqs(adapter);
740 "Initialization of SCRQ's failed\n");
743 rc = init_sub_crq_irqs(adapter);
746 "Initialization of SCRQ's irqs failed\n");
751 reinit_completion(&adapter->init_done);
753 if (!wait_for_completion_timeout(&adapter->init_done,
755 dev_err(dev, "Login timeout\n");
758 } while (adapter->renegotiate);
763 static void release_resources(struct ibmvnic_adapter *adapter)
767 release_vpd_data(adapter);
769 release_tx_pools(adapter);
770 release_rx_pools(adapter);
772 release_stats_token(adapter);
773 release_stats_buffers(adapter);
774 release_error_buffers(adapter);
777 for (i = 0; i < adapter->req_rx_queues; i++) {
778 if (&adapter->napi[i]) {
779 netdev_dbg(adapter->netdev,
780 "Releasing napi[%d]\n", i);
781 netif_napi_del(&adapter->napi[i]);
787 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
789 struct net_device *netdev = adapter->netdev;
790 unsigned long timeout = msecs_to_jiffies(30000);
791 union ibmvnic_crq crq;
795 netdev_dbg(netdev, "setting link state %d\n", link_state);
797 memset(&crq, 0, sizeof(crq));
798 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
799 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
800 crq.logical_link_state.link_state = link_state;
805 reinit_completion(&adapter->init_done);
806 rc = ibmvnic_send_crq(adapter, &crq);
808 netdev_err(netdev, "Failed to set link state\n");
812 if (!wait_for_completion_timeout(&adapter->init_done,
814 netdev_err(netdev, "timeout setting link state\n");
818 if (adapter->init_done_rc == 1) {
819 /* Partuial success, delay and re-send */
828 static int set_real_num_queues(struct net_device *netdev)
830 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
833 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
834 adapter->req_tx_queues, adapter->req_rx_queues);
836 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
838 netdev_err(netdev, "failed to set the number of tx queues\n");
842 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
844 netdev_err(netdev, "failed to set the number of rx queues\n");
849 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
851 struct device *dev = &adapter->vdev->dev;
852 union ibmvnic_crq crq;
855 if (adapter->vpd->buff)
856 len = adapter->vpd->len;
858 reinit_completion(&adapter->fw_done);
859 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
860 crq.get_vpd_size.cmd = GET_VPD_SIZE;
861 ibmvnic_send_crq(adapter, &crq);
862 wait_for_completion(&adapter->fw_done);
864 if (!adapter->vpd->len)
867 if (!adapter->vpd->buff)
868 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
869 else if (adapter->vpd->len != len)
871 krealloc(adapter->vpd->buff,
872 adapter->vpd->len, GFP_KERNEL);
874 if (!adapter->vpd->buff) {
875 dev_err(dev, "Could allocate VPD buffer\n");
879 adapter->vpd->dma_addr =
880 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
882 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
883 dev_err(dev, "Could not map VPD buffer\n");
884 kfree(adapter->vpd->buff);
888 reinit_completion(&adapter->fw_done);
889 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
890 crq.get_vpd.cmd = GET_VPD;
891 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
892 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
893 ibmvnic_send_crq(adapter, &crq);
894 wait_for_completion(&adapter->fw_done);
899 static int init_resources(struct ibmvnic_adapter *adapter)
901 struct net_device *netdev = adapter->netdev;
904 rc = set_real_num_queues(netdev);
908 rc = init_stats_buffers(adapter);
912 rc = init_stats_token(adapter);
916 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
921 adapter->napi = kcalloc(adapter->req_rx_queues,
922 sizeof(struct napi_struct), GFP_KERNEL);
926 for (i = 0; i < adapter->req_rx_queues; i++) {
927 netdev_dbg(netdev, "Adding napi[%d]\n", i);
928 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
932 send_map_query(adapter);
934 rc = init_rx_pools(netdev);
938 rc = init_tx_pools(netdev);
942 static int __ibmvnic_open(struct net_device *netdev)
944 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
945 enum vnic_state prev_state = adapter->state;
948 adapter->state = VNIC_OPENING;
949 replenish_pools(adapter);
950 ibmvnic_napi_enable(adapter);
952 /* We're ready to receive frames, enable the sub-crq interrupts and
953 * set the logical link state to up
955 for (i = 0; i < adapter->req_rx_queues; i++) {
956 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
957 if (prev_state == VNIC_CLOSED)
958 enable_irq(adapter->rx_scrq[i]->irq);
960 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
963 for (i = 0; i < adapter->req_tx_queues; i++) {
964 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
965 if (prev_state == VNIC_CLOSED)
966 enable_irq(adapter->tx_scrq[i]->irq);
968 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
971 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
973 for (i = 0; i < adapter->req_rx_queues; i++)
974 napi_disable(&adapter->napi[i]);
975 release_resources(adapter);
979 netif_tx_start_all_queues(netdev);
981 if (prev_state == VNIC_CLOSED) {
982 for (i = 0; i < adapter->req_rx_queues; i++)
983 napi_schedule(&adapter->napi[i]);
986 adapter->state = VNIC_OPEN;
990 static int ibmvnic_open(struct net_device *netdev)
992 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
995 mutex_lock(&adapter->reset_lock);
997 if (adapter->mac_change_pending) {
998 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
999 adapter->mac_change_pending = false;
1002 if (adapter->state != VNIC_CLOSED) {
1003 rc = ibmvnic_login(netdev);
1005 mutex_unlock(&adapter->reset_lock);
1009 rc = init_resources(adapter);
1011 netdev_err(netdev, "failed to initialize resources\n");
1012 release_resources(adapter);
1013 mutex_unlock(&adapter->reset_lock);
1018 rc = __ibmvnic_open(netdev);
1019 netif_carrier_on(netdev);
1021 /* Vital Product Data (VPD) */
1022 vpd = ibmvnic_get_vpd(adapter);
1024 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1026 mutex_unlock(&adapter->reset_lock);
1031 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1033 struct ibmvnic_tx_pool *tx_pool;
1038 if (!adapter->tx_pool)
1041 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1042 tx_entries = adapter->req_tx_entries_per_subcrq;
1044 /* Free any remaining skbs in the tx buffer pools */
1045 for (i = 0; i < tx_scrqs; i++) {
1046 tx_pool = &adapter->tx_pool[i];
1050 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1051 for (j = 0; j < tx_entries; j++) {
1052 if (tx_pool->tx_buff[j].skb) {
1053 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1054 tx_pool->tx_buff[j].skb = NULL;
1060 static int __ibmvnic_close(struct net_device *netdev)
1062 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1066 adapter->state = VNIC_CLOSING;
1068 /* ensure that transmissions are stopped if called by do_reset */
1069 if (adapter->resetting)
1070 netif_tx_disable(netdev);
1072 netif_tx_stop_all_queues(netdev);
1074 ibmvnic_napi_disable(adapter);
1076 if (adapter->tx_scrq) {
1077 for (i = 0; i < adapter->req_tx_queues; i++)
1078 if (adapter->tx_scrq[i]->irq) {
1079 netdev_dbg(adapter->netdev,
1080 "Disabling tx_scrq[%d] irq\n", i);
1081 disable_irq(adapter->tx_scrq[i]->irq);
1085 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1089 if (adapter->rx_scrq) {
1090 for (i = 0; i < adapter->req_rx_queues; i++) {
1093 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1101 if (adapter->rx_scrq[i]->irq) {
1102 netdev_dbg(adapter->netdev,
1103 "Disabling rx_scrq[%d] irq\n", i);
1104 disable_irq(adapter->rx_scrq[i]->irq);
1109 clean_tx_pools(adapter);
1110 adapter->state = VNIC_CLOSED;
1114 static int ibmvnic_close(struct net_device *netdev)
1116 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1119 mutex_lock(&adapter->reset_lock);
1120 rc = __ibmvnic_close(netdev);
1121 mutex_unlock(&adapter->reset_lock);
1127 * build_hdr_data - creates L2/L3/L4 header data buffer
1128 * @hdr_field - bitfield determining needed headers
1129 * @skb - socket buffer
1130 * @hdr_len - array of header lengths
1131 * @tot_len - total length of data
1133 * Reads hdr_field to determine which headers are needed by firmware.
1134 * Builds a buffer containing these headers. Saves individual header
1135 * lengths and total buffer length to be used to build descriptors.
1137 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1138 int *hdr_len, u8 *hdr_data)
1143 hdr_len[0] = sizeof(struct ethhdr);
1145 if (skb->protocol == htons(ETH_P_IP)) {
1146 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1147 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1148 hdr_len[2] = tcp_hdrlen(skb);
1149 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1150 hdr_len[2] = sizeof(struct udphdr);
1151 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1152 hdr_len[1] = sizeof(struct ipv6hdr);
1153 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1154 hdr_len[2] = tcp_hdrlen(skb);
1155 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1156 hdr_len[2] = sizeof(struct udphdr);
1157 } else if (skb->protocol == htons(ETH_P_ARP)) {
1158 hdr_len[1] = arp_hdr_len(skb->dev);
1162 memset(hdr_data, 0, 120);
1163 if ((hdr_field >> 6) & 1) {
1164 hdr = skb_mac_header(skb);
1165 memcpy(hdr_data, hdr, hdr_len[0]);
1169 if ((hdr_field >> 5) & 1) {
1170 hdr = skb_network_header(skb);
1171 memcpy(hdr_data + len, hdr, hdr_len[1]);
1175 if ((hdr_field >> 4) & 1) {
1176 hdr = skb_transport_header(skb);
1177 memcpy(hdr_data + len, hdr, hdr_len[2]);
1184 * create_hdr_descs - create header and header extension descriptors
1185 * @hdr_field - bitfield determining needed headers
1186 * @data - buffer containing header data
1187 * @len - length of data buffer
1188 * @hdr_len - array of individual header lengths
1189 * @scrq_arr - descriptor array
1191 * Creates header and, if needed, header extension descriptors and
1192 * places them in a descriptor array, scrq_arr
1195 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1196 union sub_crq *scrq_arr)
1198 union sub_crq hdr_desc;
1204 while (tmp_len > 0) {
1205 cur = hdr_data + len - tmp_len;
1207 memset(&hdr_desc, 0, sizeof(hdr_desc));
1208 if (cur != hdr_data) {
1209 data = hdr_desc.hdr_ext.data;
1210 tmp = tmp_len > 29 ? 29 : tmp_len;
1211 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1212 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1213 hdr_desc.hdr_ext.len = tmp;
1215 data = hdr_desc.hdr.data;
1216 tmp = tmp_len > 24 ? 24 : tmp_len;
1217 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1218 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1219 hdr_desc.hdr.len = tmp;
1220 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1221 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1222 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1223 hdr_desc.hdr.flag = hdr_field << 1;
1225 memcpy(data, cur, tmp);
1227 *scrq_arr = hdr_desc;
1236 * build_hdr_descs_arr - build a header descriptor array
1237 * @skb - socket buffer
1238 * @num_entries - number of descriptors to be sent
1239 * @subcrq - first TX descriptor
1240 * @hdr_field - bit field determining which headers will be sent
1242 * This function will build a TX descriptor array with applicable
1243 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1246 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1247 int *num_entries, u8 hdr_field)
1249 int hdr_len[3] = {0, 0, 0};
1251 u8 *hdr_data = txbuff->hdr_data;
1253 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1255 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1256 txbuff->indir_arr + 1);
1259 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1261 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1262 int queue_num = skb_get_queue_mapping(skb);
1263 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1264 struct device *dev = &adapter->vdev->dev;
1265 struct ibmvnic_tx_buff *tx_buff = NULL;
1266 struct ibmvnic_sub_crq_queue *tx_scrq;
1267 struct ibmvnic_tx_pool *tx_pool;
1268 unsigned int tx_send_failed = 0;
1269 unsigned int tx_map_failed = 0;
1270 unsigned int tx_dropped = 0;
1271 unsigned int tx_packets = 0;
1272 unsigned int tx_bytes = 0;
1273 dma_addr_t data_dma_addr;
1274 struct netdev_queue *txq;
1275 unsigned long lpar_rc;
1276 union sub_crq tx_crq;
1277 unsigned int offset;
1278 int num_entries = 1;
1284 if (adapter->resetting) {
1285 if (!netif_subqueue_stopped(netdev, skb))
1286 netif_stop_subqueue(netdev, queue_num);
1287 dev_kfree_skb_any(skb);
1295 tx_pool = &adapter->tx_pool[queue_num];
1296 tx_scrq = adapter->tx_scrq[queue_num];
1297 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1298 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1299 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1301 index = tx_pool->free_map[tx_pool->consumer_index];
1303 if (skb_is_gso(skb)) {
1304 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1305 dst = tx_pool->tso_ltb.buff + offset;
1306 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1307 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1308 tx_pool->tso_index++;
1309 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1310 tx_pool->tso_index = 0;
1312 offset = index * adapter->req_mtu;
1313 dst = tx_pool->long_term_buff.buff + offset;
1314 memset(dst, 0, adapter->req_mtu);
1315 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1318 if (skb_shinfo(skb)->nr_frags) {
1322 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1323 cur = skb_headlen(skb);
1325 /* Copy the frags */
1326 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1327 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1330 page_address(skb_frag_page(frag)) +
1331 frag->page_offset, skb_frag_size(frag));
1332 cur += skb_frag_size(frag);
1335 skb_copy_from_linear_data(skb, dst, skb->len);
1338 tx_pool->consumer_index =
1339 (tx_pool->consumer_index + 1) %
1340 adapter->req_tx_entries_per_subcrq;
1342 tx_buff = &tx_pool->tx_buff[index];
1344 tx_buff->data_dma[0] = data_dma_addr;
1345 tx_buff->data_len[0] = skb->len;
1346 tx_buff->index = index;
1347 tx_buff->pool_index = queue_num;
1348 tx_buff->last_frag = true;
1350 memset(&tx_crq, 0, sizeof(tx_crq));
1351 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1352 tx_crq.v1.type = IBMVNIC_TX_DESC;
1353 tx_crq.v1.n_crq_elem = 1;
1354 tx_crq.v1.n_sge = 1;
1355 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1356 tx_crq.v1.correlator = cpu_to_be32(index);
1357 if (skb_is_gso(skb))
1358 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1360 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1361 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1362 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1364 if (adapter->vlan_header_insertion) {
1365 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1366 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1369 if (skb->protocol == htons(ETH_P_IP)) {
1370 if (ip_hdr(skb)->version == 4)
1371 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1372 else if (ip_hdr(skb)->version == 6)
1373 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1375 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1376 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1377 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1378 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1381 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1382 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1385 if (skb_is_gso(skb)) {
1386 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1387 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1390 /* determine if l2/3/4 headers are sent to firmware */
1391 if ((*hdrs >> 7) & 1 &&
1392 (skb->protocol == htons(ETH_P_IP) ||
1393 skb->protocol == htons(ETH_P_IPV6) ||
1394 skb->protocol == htons(ETH_P_ARP))) {
1395 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1396 tx_crq.v1.n_crq_elem = num_entries;
1397 tx_buff->indir_arr[0] = tx_crq;
1398 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1399 sizeof(tx_buff->indir_arr),
1401 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1402 dev_kfree_skb_any(skb);
1403 tx_buff->skb = NULL;
1404 if (!firmware_has_feature(FW_FEATURE_CMO))
1405 dev_err(dev, "tx: unable to map descriptor array\n");
1411 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1412 (u64)tx_buff->indir_dma,
1415 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1418 if (lpar_rc != H_SUCCESS) {
1419 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1421 if (tx_pool->consumer_index == 0)
1422 tx_pool->consumer_index =
1423 adapter->req_tx_entries_per_subcrq - 1;
1425 tx_pool->consumer_index--;
1427 dev_kfree_skb_any(skb);
1428 tx_buff->skb = NULL;
1430 if (lpar_rc == H_CLOSED) {
1431 /* Disable TX and report carrier off if queue is closed.
1432 * Firmware guarantees that a signal will be sent to the
1433 * driver, triggering a reset or some other action.
1435 netif_tx_stop_all_queues(netdev);
1436 netif_carrier_off(netdev);
1445 if (atomic_inc_return(&tx_scrq->used)
1446 >= adapter->req_tx_entries_per_subcrq) {
1447 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1448 netif_stop_subqueue(netdev, queue_num);
1452 tx_bytes += skb->len;
1453 txq->trans_start = jiffies;
1457 netdev->stats.tx_dropped += tx_dropped;
1458 netdev->stats.tx_bytes += tx_bytes;
1459 netdev->stats.tx_packets += tx_packets;
1460 adapter->tx_send_failed += tx_send_failed;
1461 adapter->tx_map_failed += tx_map_failed;
1462 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1463 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1464 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1469 static void ibmvnic_set_multi(struct net_device *netdev)
1471 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1472 struct netdev_hw_addr *ha;
1473 union ibmvnic_crq crq;
1475 memset(&crq, 0, sizeof(crq));
1476 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1477 crq.request_capability.cmd = REQUEST_CAPABILITY;
1479 if (netdev->flags & IFF_PROMISC) {
1480 if (!adapter->promisc_supported)
1483 if (netdev->flags & IFF_ALLMULTI) {
1484 /* Accept all multicast */
1485 memset(&crq, 0, sizeof(crq));
1486 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1487 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1488 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1489 ibmvnic_send_crq(adapter, &crq);
1490 } else if (netdev_mc_empty(netdev)) {
1491 /* Reject all multicast */
1492 memset(&crq, 0, sizeof(crq));
1493 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1494 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1495 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1496 ibmvnic_send_crq(adapter, &crq);
1498 /* Accept one or more multicast(s) */
1499 netdev_for_each_mc_addr(ha, netdev) {
1500 memset(&crq, 0, sizeof(crq));
1501 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1502 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1503 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1504 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1506 ibmvnic_send_crq(adapter, &crq);
1512 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1514 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1515 struct sockaddr *addr = p;
1516 union ibmvnic_crq crq;
1518 if (!is_valid_ether_addr(addr->sa_data))
1519 return -EADDRNOTAVAIL;
1521 memset(&crq, 0, sizeof(crq));
1522 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1523 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1524 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1525 ibmvnic_send_crq(adapter, &crq);
1526 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1530 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1532 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1533 struct sockaddr *addr = p;
1535 if (adapter->state != VNIC_OPEN) {
1536 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1537 adapter->mac_change_pending = true;
1541 __ibmvnic_set_mac(netdev, addr);
1547 * do_reset returns zero if we are able to keep processing reset events, or
1548 * non-zero if we hit a fatal error and must halt.
1550 static int do_reset(struct ibmvnic_adapter *adapter,
1551 struct ibmvnic_rwi *rwi, u32 reset_state)
1553 struct net_device *netdev = adapter->netdev;
1556 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1559 netif_carrier_off(netdev);
1560 adapter->reset_reason = rwi->reset_reason;
1562 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1563 rc = ibmvnic_reenable_crq_queue(adapter);
1568 rc = __ibmvnic_close(netdev);
1572 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1573 adapter->wait_for_reset) {
1574 release_resources(adapter);
1575 release_sub_crqs(adapter);
1576 release_crq_queue(adapter);
1579 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1580 /* remove the closed state so when we call open it appears
1581 * we are coming from the probed state.
1583 adapter->state = VNIC_PROBED;
1585 rc = ibmvnic_init(adapter);
1587 return IBMVNIC_INIT_FAILED;
1589 /* If the adapter was in PROBE state prior to the reset,
1592 if (reset_state == VNIC_PROBED)
1595 rc = ibmvnic_login(netdev);
1597 adapter->state = VNIC_PROBED;
1601 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1602 adapter->wait_for_reset) {
1603 rc = init_resources(adapter);
1607 rc = reset_tx_pools(adapter);
1611 rc = reset_rx_pools(adapter);
1615 if (reset_state == VNIC_CLOSED)
1620 rc = __ibmvnic_open(netdev);
1622 if (list_empty(&adapter->rwi_list))
1623 adapter->state = VNIC_CLOSED;
1625 adapter->state = reset_state;
1630 netif_carrier_on(netdev);
1633 for (i = 0; i < adapter->req_rx_queues; i++)
1634 napi_schedule(&adapter->napi[i]);
1636 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1637 netdev_notify_peers(netdev);
1642 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1644 struct ibmvnic_rwi *rwi;
1646 mutex_lock(&adapter->rwi_lock);
1648 if (!list_empty(&adapter->rwi_list)) {
1649 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1651 list_del(&rwi->list);
1656 mutex_unlock(&adapter->rwi_lock);
1660 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1662 struct ibmvnic_rwi *rwi;
1664 rwi = get_next_rwi(adapter);
1667 rwi = get_next_rwi(adapter);
1671 static void __ibmvnic_reset(struct work_struct *work)
1673 struct ibmvnic_rwi *rwi;
1674 struct ibmvnic_adapter *adapter;
1675 struct net_device *netdev;
1679 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1680 netdev = adapter->netdev;
1682 mutex_lock(&adapter->reset_lock);
1683 adapter->resetting = true;
1684 reset_state = adapter->state;
1686 rwi = get_next_rwi(adapter);
1688 rc = do_reset(adapter, rwi, reset_state);
1690 if (rc && rc != IBMVNIC_INIT_FAILED)
1693 rwi = get_next_rwi(adapter);
1696 if (adapter->wait_for_reset) {
1697 adapter->wait_for_reset = false;
1698 adapter->reset_done_rc = rc;
1699 complete(&adapter->reset_done);
1703 netdev_dbg(adapter->netdev, "Reset failed\n");
1704 free_all_rwi(adapter);
1705 mutex_unlock(&adapter->reset_lock);
1709 adapter->resetting = false;
1710 mutex_unlock(&adapter->reset_lock);
1713 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1714 enum ibmvnic_reset_reason reason)
1716 struct ibmvnic_rwi *rwi, *tmp;
1717 struct net_device *netdev = adapter->netdev;
1718 struct list_head *entry;
1720 if (adapter->state == VNIC_REMOVING ||
1721 adapter->state == VNIC_REMOVED) {
1722 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1726 if (adapter->state == VNIC_PROBING) {
1727 netdev_warn(netdev, "Adapter reset during probe\n");
1728 adapter->init_done_rc = EAGAIN;
1732 mutex_lock(&adapter->rwi_lock);
1734 list_for_each(entry, &adapter->rwi_list) {
1735 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1736 if (tmp->reset_reason == reason) {
1737 netdev_dbg(netdev, "Skipping matching reset\n");
1738 mutex_unlock(&adapter->rwi_lock);
1743 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1745 mutex_unlock(&adapter->rwi_lock);
1746 ibmvnic_close(netdev);
1750 rwi->reset_reason = reason;
1751 list_add_tail(&rwi->list, &adapter->rwi_list);
1752 mutex_unlock(&adapter->rwi_lock);
1754 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1755 schedule_work(&adapter->ibmvnic_reset);
1758 static void ibmvnic_tx_timeout(struct net_device *dev)
1760 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1762 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1765 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1766 struct ibmvnic_rx_buff *rx_buff)
1768 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1770 rx_buff->skb = NULL;
1772 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1773 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1775 atomic_dec(&pool->available);
1778 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1780 struct net_device *netdev = napi->dev;
1781 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1782 int scrq_num = (int)(napi - adapter->napi);
1783 int frames_processed = 0;
1786 while (frames_processed < budget) {
1787 struct sk_buff *skb;
1788 struct ibmvnic_rx_buff *rx_buff;
1789 union sub_crq *next;
1794 if (unlikely(adapter->resetting)) {
1795 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1796 napi_complete_done(napi, frames_processed);
1797 return frames_processed;
1800 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1802 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1804 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1805 rx_comp.correlator);
1806 /* do error checking */
1807 if (next->rx_comp.rc) {
1808 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1809 be16_to_cpu(next->rx_comp.rc));
1810 /* free the entry */
1811 next->rx_comp.first = 0;
1812 remove_buff_from_pool(adapter, rx_buff);
1816 length = be32_to_cpu(next->rx_comp.len);
1817 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1818 flags = next->rx_comp.flags;
1820 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1823 /* VLAN Header has been stripped by the system firmware and
1824 * needs to be inserted by the driver
1826 if (adapter->rx_vlan_header_insertion &&
1827 (flags & IBMVNIC_VLAN_STRIPPED))
1828 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1829 ntohs(next->rx_comp.vlan_tci));
1831 /* free the entry */
1832 next->rx_comp.first = 0;
1833 remove_buff_from_pool(adapter, rx_buff);
1835 skb_put(skb, length);
1836 skb->protocol = eth_type_trans(skb, netdev);
1837 skb_record_rx_queue(skb, scrq_num);
1839 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1840 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1841 skb->ip_summed = CHECKSUM_UNNECESSARY;
1845 napi_gro_receive(napi, skb); /* send it up */
1846 netdev->stats.rx_packets++;
1847 netdev->stats.rx_bytes += length;
1848 adapter->rx_stats_buffers[scrq_num].packets++;
1849 adapter->rx_stats_buffers[scrq_num].bytes += length;
1853 if (adapter->state != VNIC_CLOSING)
1854 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1856 if (frames_processed < budget) {
1857 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1858 napi_complete_done(napi, frames_processed);
1859 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1860 napi_reschedule(napi)) {
1861 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1865 return frames_processed;
1868 #ifdef CONFIG_NET_POLL_CONTROLLER
1869 static void ibmvnic_netpoll_controller(struct net_device *dev)
1871 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1874 replenish_pools(netdev_priv(dev));
1875 for (i = 0; i < adapter->req_rx_queues; i++)
1876 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1877 adapter->rx_scrq[i]);
1881 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1883 adapter->fallback.mtu = adapter->req_mtu;
1884 adapter->fallback.rx_queues = adapter->req_rx_queues;
1885 adapter->fallback.tx_queues = adapter->req_tx_queues;
1886 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1887 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1889 init_completion(&adapter->reset_done);
1890 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1891 adapter->wait_for_reset = true;
1892 wait_for_completion(&adapter->reset_done);
1894 if (adapter->reset_done_rc) {
1895 adapter->desired.mtu = adapter->fallback.mtu;
1896 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1897 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1898 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1899 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1901 init_completion(&adapter->reset_done);
1902 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1903 wait_for_completion(&adapter->reset_done);
1905 adapter->wait_for_reset = false;
1907 return adapter->reset_done_rc;
1910 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1912 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1914 adapter->desired.mtu = new_mtu + ETH_HLEN;
1916 return wait_for_reset(adapter);
1919 static const struct net_device_ops ibmvnic_netdev_ops = {
1920 .ndo_open = ibmvnic_open,
1921 .ndo_stop = ibmvnic_close,
1922 .ndo_start_xmit = ibmvnic_xmit,
1923 .ndo_set_rx_mode = ibmvnic_set_multi,
1924 .ndo_set_mac_address = ibmvnic_set_mac,
1925 .ndo_validate_addr = eth_validate_addr,
1926 .ndo_tx_timeout = ibmvnic_tx_timeout,
1927 #ifdef CONFIG_NET_POLL_CONTROLLER
1928 .ndo_poll_controller = ibmvnic_netpoll_controller,
1930 .ndo_change_mtu = ibmvnic_change_mtu,
1933 /* ethtool functions */
1935 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1936 struct ethtool_link_ksettings *cmd)
1938 u32 supported, advertising;
1940 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1942 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1944 cmd->base.speed = SPEED_1000;
1945 cmd->base.duplex = DUPLEX_FULL;
1946 cmd->base.port = PORT_FIBRE;
1947 cmd->base.phy_address = 0;
1948 cmd->base.autoneg = AUTONEG_ENABLE;
1950 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1952 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1958 static void ibmvnic_get_drvinfo(struct net_device *netdev,
1959 struct ethtool_drvinfo *info)
1961 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1963 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1964 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1965 strlcpy(info->fw_version, adapter->fw_version,
1966 sizeof(info->fw_version));
1969 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1971 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1973 return adapter->msg_enable;
1976 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1978 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1980 adapter->msg_enable = data;
1983 static u32 ibmvnic_get_link(struct net_device *netdev)
1985 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1987 /* Don't need to send a query because we request a logical link up at
1988 * init and then we wait for link state indications
1990 return adapter->logical_link_state;
1993 static void ibmvnic_get_ringparam(struct net_device *netdev,
1994 struct ethtool_ringparam *ring)
1996 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1998 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1999 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2000 ring->rx_mini_max_pending = 0;
2001 ring->rx_jumbo_max_pending = 0;
2002 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2003 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2004 ring->rx_mini_pending = 0;
2005 ring->rx_jumbo_pending = 0;
2008 static int ibmvnic_set_ringparam(struct net_device *netdev,
2009 struct ethtool_ringparam *ring)
2011 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2013 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2014 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2015 netdev_err(netdev, "Invalid request.\n");
2016 netdev_err(netdev, "Max tx buffers = %llu\n",
2017 adapter->max_rx_add_entries_per_subcrq);
2018 netdev_err(netdev, "Max rx buffers = %llu\n",
2019 adapter->max_tx_entries_per_subcrq);
2023 adapter->desired.rx_entries = ring->rx_pending;
2024 adapter->desired.tx_entries = ring->tx_pending;
2026 return wait_for_reset(adapter);
2029 static void ibmvnic_get_channels(struct net_device *netdev,
2030 struct ethtool_channels *channels)
2032 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2034 channels->max_rx = adapter->max_rx_queues;
2035 channels->max_tx = adapter->max_tx_queues;
2036 channels->max_other = 0;
2037 channels->max_combined = 0;
2038 channels->rx_count = adapter->req_rx_queues;
2039 channels->tx_count = adapter->req_tx_queues;
2040 channels->other_count = 0;
2041 channels->combined_count = 0;
2044 static int ibmvnic_set_channels(struct net_device *netdev,
2045 struct ethtool_channels *channels)
2047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2049 adapter->desired.rx_queues = channels->rx_count;
2050 adapter->desired.tx_queues = channels->tx_count;
2052 return wait_for_reset(adapter);
2055 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2057 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2060 if (stringset != ETH_SS_STATS)
2063 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2064 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2066 for (i = 0; i < adapter->req_tx_queues; i++) {
2067 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2068 data += ETH_GSTRING_LEN;
2070 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2071 data += ETH_GSTRING_LEN;
2073 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2074 data += ETH_GSTRING_LEN;
2077 for (i = 0; i < adapter->req_rx_queues; i++) {
2078 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2079 data += ETH_GSTRING_LEN;
2081 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2082 data += ETH_GSTRING_LEN;
2084 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2085 data += ETH_GSTRING_LEN;
2089 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2091 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2095 return ARRAY_SIZE(ibmvnic_stats) +
2096 adapter->req_tx_queues * NUM_TX_STATS +
2097 adapter->req_rx_queues * NUM_RX_STATS;
2103 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2104 struct ethtool_stats *stats, u64 *data)
2106 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2107 union ibmvnic_crq crq;
2110 memset(&crq, 0, sizeof(crq));
2111 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2112 crq.request_statistics.cmd = REQUEST_STATISTICS;
2113 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2114 crq.request_statistics.len =
2115 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2117 /* Wait for data to be written */
2118 init_completion(&adapter->stats_done);
2119 ibmvnic_send_crq(adapter, &crq);
2120 wait_for_completion(&adapter->stats_done);
2122 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2123 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2124 ibmvnic_stats[i].offset));
2126 for (j = 0; j < adapter->req_tx_queues; j++) {
2127 data[i] = adapter->tx_stats_buffers[j].packets;
2129 data[i] = adapter->tx_stats_buffers[j].bytes;
2131 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2135 for (j = 0; j < adapter->req_rx_queues; j++) {
2136 data[i] = adapter->rx_stats_buffers[j].packets;
2138 data[i] = adapter->rx_stats_buffers[j].bytes;
2140 data[i] = adapter->rx_stats_buffers[j].interrupts;
2145 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2146 .get_drvinfo = ibmvnic_get_drvinfo,
2147 .get_msglevel = ibmvnic_get_msglevel,
2148 .set_msglevel = ibmvnic_set_msglevel,
2149 .get_link = ibmvnic_get_link,
2150 .get_ringparam = ibmvnic_get_ringparam,
2151 .set_ringparam = ibmvnic_set_ringparam,
2152 .get_channels = ibmvnic_get_channels,
2153 .set_channels = ibmvnic_set_channels,
2154 .get_strings = ibmvnic_get_strings,
2155 .get_sset_count = ibmvnic_get_sset_count,
2156 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2157 .get_link_ksettings = ibmvnic_get_link_ksettings,
2160 /* Routines for managing CRQs/sCRQs */
2162 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2163 struct ibmvnic_sub_crq_queue *scrq)
2168 free_irq(scrq->irq, scrq);
2169 irq_dispose_mapping(scrq->irq);
2173 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2176 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2177 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2181 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2185 for (i = 0; i < adapter->req_tx_queues; i++) {
2186 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2187 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2192 for (i = 0; i < adapter->req_rx_queues; i++) {
2193 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2194 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2202 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2203 struct ibmvnic_sub_crq_queue *scrq)
2205 struct device *dev = &adapter->vdev->dev;
2208 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2210 /* Close the sub-crqs */
2212 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2213 adapter->vdev->unit_address,
2215 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2218 netdev_err(adapter->netdev,
2219 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2223 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2225 free_pages((unsigned long)scrq->msgs, 2);
2229 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2232 struct device *dev = &adapter->vdev->dev;
2233 struct ibmvnic_sub_crq_queue *scrq;
2236 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2241 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2243 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2244 goto zero_page_failed;
2247 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2249 if (dma_mapping_error(dev, scrq->msg_token)) {
2250 dev_warn(dev, "Couldn't map crq queue messages page\n");
2254 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2255 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2257 if (rc == H_RESOURCE)
2258 rc = ibmvnic_reset_crq(adapter);
2260 if (rc == H_CLOSED) {
2261 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2263 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2267 scrq->adapter = adapter;
2268 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2269 spin_lock_init(&scrq->lock);
2271 netdev_dbg(adapter->netdev,
2272 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2273 scrq->crq_num, scrq->hw_irq, scrq->irq);
2278 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2281 free_pages((unsigned long)scrq->msgs, 2);
2288 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2292 if (adapter->tx_scrq) {
2293 for (i = 0; i < adapter->req_tx_queues; i++) {
2294 if (!adapter->tx_scrq[i])
2297 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2299 if (adapter->tx_scrq[i]->irq) {
2300 free_irq(adapter->tx_scrq[i]->irq,
2301 adapter->tx_scrq[i]);
2302 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2303 adapter->tx_scrq[i]->irq = 0;
2306 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2309 kfree(adapter->tx_scrq);
2310 adapter->tx_scrq = NULL;
2313 if (adapter->rx_scrq) {
2314 for (i = 0; i < adapter->req_rx_queues; i++) {
2315 if (!adapter->rx_scrq[i])
2318 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2320 if (adapter->rx_scrq[i]->irq) {
2321 free_irq(adapter->rx_scrq[i]->irq,
2322 adapter->rx_scrq[i]);
2323 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2324 adapter->rx_scrq[i]->irq = 0;
2327 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2330 kfree(adapter->rx_scrq);
2331 adapter->rx_scrq = NULL;
2335 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2336 struct ibmvnic_sub_crq_queue *scrq)
2338 struct device *dev = &adapter->vdev->dev;
2341 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2342 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2344 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2349 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2350 struct ibmvnic_sub_crq_queue *scrq)
2352 struct device *dev = &adapter->vdev->dev;
2355 if (scrq->hw_irq > 0x100000000ULL) {
2356 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2360 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2361 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2363 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2368 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2369 struct ibmvnic_sub_crq_queue *scrq)
2371 struct device *dev = &adapter->vdev->dev;
2372 struct ibmvnic_tx_buff *txbuff;
2373 union sub_crq *next;
2379 while (pending_scrq(adapter, scrq)) {
2380 unsigned int pool = scrq->pool_index;
2382 next = ibmvnic_next_scrq(adapter, scrq);
2383 for (i = 0; i < next->tx_comp.num_comps; i++) {
2384 if (next->tx_comp.rcs[i]) {
2385 dev_err(dev, "tx error %x\n",
2386 next->tx_comp.rcs[i]);
2389 index = be32_to_cpu(next->tx_comp.correlators[i]);
2390 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2392 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2393 if (!txbuff->data_dma[j])
2396 txbuff->data_dma[j] = 0;
2398 /* if sub_crq was sent indirectly */
2399 first = txbuff->indir_arr[0].generic.first;
2400 if (first == IBMVNIC_CRQ_CMD) {
2401 dma_unmap_single(dev, txbuff->indir_dma,
2402 sizeof(txbuff->indir_arr),
2406 if (txbuff->last_frag) {
2407 dev_kfree_skb_any(txbuff->skb);
2411 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2412 producer_index] = index;
2413 adapter->tx_pool[pool].producer_index =
2414 (adapter->tx_pool[pool].producer_index + 1) %
2415 adapter->req_tx_entries_per_subcrq;
2417 /* remove tx_comp scrq*/
2418 next->tx_comp.first = 0;
2420 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2421 (adapter->req_tx_entries_per_subcrq / 2) &&
2422 __netif_subqueue_stopped(adapter->netdev,
2423 scrq->pool_index)) {
2424 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2425 netdev_info(adapter->netdev, "Started queue %d\n",
2430 enable_scrq_irq(adapter, scrq);
2432 if (pending_scrq(adapter, scrq)) {
2433 disable_scrq_irq(adapter, scrq);
2440 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2442 struct ibmvnic_sub_crq_queue *scrq = instance;
2443 struct ibmvnic_adapter *adapter = scrq->adapter;
2445 disable_scrq_irq(adapter, scrq);
2446 ibmvnic_complete_tx(adapter, scrq);
2451 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2453 struct ibmvnic_sub_crq_queue *scrq = instance;
2454 struct ibmvnic_adapter *adapter = scrq->adapter;
2456 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2458 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2459 disable_scrq_irq(adapter, scrq);
2460 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2466 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2468 struct device *dev = &adapter->vdev->dev;
2469 struct ibmvnic_sub_crq_queue *scrq;
2473 for (i = 0; i < adapter->req_tx_queues; i++) {
2474 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2476 scrq = adapter->tx_scrq[i];
2477 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2481 dev_err(dev, "Error mapping irq\n");
2482 goto req_tx_irq_failed;
2485 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2486 0, "ibmvnic_tx", scrq);
2489 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2491 irq_dispose_mapping(scrq->irq);
2492 goto req_rx_irq_failed;
2496 for (i = 0; i < adapter->req_rx_queues; i++) {
2497 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2499 scrq = adapter->rx_scrq[i];
2500 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2503 dev_err(dev, "Error mapping irq\n");
2504 goto req_rx_irq_failed;
2506 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2507 0, "ibmvnic_rx", scrq);
2509 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2511 irq_dispose_mapping(scrq->irq);
2512 goto req_rx_irq_failed;
2518 for (j = 0; j < i; j++) {
2519 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2520 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2522 i = adapter->req_tx_queues;
2524 for (j = 0; j < i; j++) {
2525 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2526 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2528 release_sub_crqs(adapter);
2532 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2534 struct device *dev = &adapter->vdev->dev;
2535 struct ibmvnic_sub_crq_queue **allqueues;
2536 int registered_queues = 0;
2541 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2543 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2547 for (i = 0; i < total_queues; i++) {
2548 allqueues[i] = init_sub_crq_queue(adapter);
2549 if (!allqueues[i]) {
2550 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2553 registered_queues++;
2556 /* Make sure we were able to register the minimum number of queues */
2557 if (registered_queues <
2558 adapter->min_tx_queues + adapter->min_rx_queues) {
2559 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2563 /* Distribute the failed allocated queues*/
2564 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2565 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2568 if (adapter->req_rx_queues > adapter->min_rx_queues)
2569 adapter->req_rx_queues--;
2574 if (adapter->req_tx_queues > adapter->min_tx_queues)
2575 adapter->req_tx_queues--;
2582 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2583 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2584 if (!adapter->tx_scrq)
2587 for (i = 0; i < adapter->req_tx_queues; i++) {
2588 adapter->tx_scrq[i] = allqueues[i];
2589 adapter->tx_scrq[i]->pool_index = i;
2592 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2593 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2594 if (!adapter->rx_scrq)
2597 for (i = 0; i < adapter->req_rx_queues; i++) {
2598 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2599 adapter->rx_scrq[i]->scrq_num = i;
2606 kfree(adapter->tx_scrq);
2607 adapter->tx_scrq = NULL;
2609 for (i = 0; i < registered_queues; i++)
2610 release_sub_crq_queue(adapter, allqueues[i]);
2615 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2617 struct device *dev = &adapter->vdev->dev;
2618 union ibmvnic_crq crq;
2622 /* Sub-CRQ entries are 32 byte long */
2623 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2625 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2626 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2627 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2631 if (adapter->desired.mtu)
2632 adapter->req_mtu = adapter->desired.mtu;
2634 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2636 if (!adapter->desired.tx_entries)
2637 adapter->desired.tx_entries =
2638 adapter->max_tx_entries_per_subcrq;
2639 if (!adapter->desired.rx_entries)
2640 adapter->desired.rx_entries =
2641 adapter->max_rx_add_entries_per_subcrq;
2643 max_entries = IBMVNIC_MAX_LTB_SIZE /
2644 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2646 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2647 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2648 adapter->desired.tx_entries = max_entries;
2651 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2652 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2653 adapter->desired.rx_entries = max_entries;
2656 if (adapter->desired.tx_entries)
2657 adapter->req_tx_entries_per_subcrq =
2658 adapter->desired.tx_entries;
2660 adapter->req_tx_entries_per_subcrq =
2661 adapter->max_tx_entries_per_subcrq;
2663 if (adapter->desired.rx_entries)
2664 adapter->req_rx_add_entries_per_subcrq =
2665 adapter->desired.rx_entries;
2667 adapter->req_rx_add_entries_per_subcrq =
2668 adapter->max_rx_add_entries_per_subcrq;
2670 if (adapter->desired.tx_queues)
2671 adapter->req_tx_queues =
2672 adapter->desired.tx_queues;
2674 adapter->req_tx_queues =
2675 adapter->opt_tx_comp_sub_queues;
2677 if (adapter->desired.rx_queues)
2678 adapter->req_rx_queues =
2679 adapter->desired.rx_queues;
2681 adapter->req_rx_queues =
2682 adapter->opt_rx_comp_queues;
2684 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2687 memset(&crq, 0, sizeof(crq));
2688 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2689 crq.request_capability.cmd = REQUEST_CAPABILITY;
2691 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2692 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2693 atomic_inc(&adapter->running_cap_crqs);
2694 ibmvnic_send_crq(adapter, &crq);
2696 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2697 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2698 atomic_inc(&adapter->running_cap_crqs);
2699 ibmvnic_send_crq(adapter, &crq);
2701 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2702 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2703 atomic_inc(&adapter->running_cap_crqs);
2704 ibmvnic_send_crq(adapter, &crq);
2706 crq.request_capability.capability =
2707 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2708 crq.request_capability.number =
2709 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2710 atomic_inc(&adapter->running_cap_crqs);
2711 ibmvnic_send_crq(adapter, &crq);
2713 crq.request_capability.capability =
2714 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2715 crq.request_capability.number =
2716 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2717 atomic_inc(&adapter->running_cap_crqs);
2718 ibmvnic_send_crq(adapter, &crq);
2720 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2721 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2722 atomic_inc(&adapter->running_cap_crqs);
2723 ibmvnic_send_crq(adapter, &crq);
2725 if (adapter->netdev->flags & IFF_PROMISC) {
2726 if (adapter->promisc_supported) {
2727 crq.request_capability.capability =
2728 cpu_to_be16(PROMISC_REQUESTED);
2729 crq.request_capability.number = cpu_to_be64(1);
2730 atomic_inc(&adapter->running_cap_crqs);
2731 ibmvnic_send_crq(adapter, &crq);
2734 crq.request_capability.capability =
2735 cpu_to_be16(PROMISC_REQUESTED);
2736 crq.request_capability.number = cpu_to_be64(0);
2737 atomic_inc(&adapter->running_cap_crqs);
2738 ibmvnic_send_crq(adapter, &crq);
2742 static int pending_scrq(struct ibmvnic_adapter *adapter,
2743 struct ibmvnic_sub_crq_queue *scrq)
2745 union sub_crq *entry = &scrq->msgs[scrq->cur];
2747 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2753 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2754 struct ibmvnic_sub_crq_queue *scrq)
2756 union sub_crq *entry;
2757 unsigned long flags;
2759 spin_lock_irqsave(&scrq->lock, flags);
2760 entry = &scrq->msgs[scrq->cur];
2761 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2762 if (++scrq->cur == scrq->size)
2767 spin_unlock_irqrestore(&scrq->lock, flags);
2772 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2774 struct ibmvnic_crq_queue *queue = &adapter->crq;
2775 union ibmvnic_crq *crq;
2777 crq = &queue->msgs[queue->cur];
2778 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2779 if (++queue->cur == queue->size)
2788 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2789 union sub_crq *sub_crq)
2791 unsigned int ua = adapter->vdev->unit_address;
2792 struct device *dev = &adapter->vdev->dev;
2793 u64 *u64_crq = (u64 *)sub_crq;
2796 netdev_dbg(adapter->netdev,
2797 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2798 (unsigned long int)cpu_to_be64(remote_handle),
2799 (unsigned long int)cpu_to_be64(u64_crq[0]),
2800 (unsigned long int)cpu_to_be64(u64_crq[1]),
2801 (unsigned long int)cpu_to_be64(u64_crq[2]),
2802 (unsigned long int)cpu_to_be64(u64_crq[3]));
2804 /* Make sure the hypervisor sees the complete request */
2807 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2808 cpu_to_be64(remote_handle),
2809 cpu_to_be64(u64_crq[0]),
2810 cpu_to_be64(u64_crq[1]),
2811 cpu_to_be64(u64_crq[2]),
2812 cpu_to_be64(u64_crq[3]));
2816 dev_warn(dev, "CRQ Queue closed\n");
2817 dev_err(dev, "Send error (rc=%d)\n", rc);
2823 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2824 u64 remote_handle, u64 ioba, u64 num_entries)
2826 unsigned int ua = adapter->vdev->unit_address;
2827 struct device *dev = &adapter->vdev->dev;
2830 /* Make sure the hypervisor sees the complete request */
2832 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2833 cpu_to_be64(remote_handle),
2838 dev_warn(dev, "CRQ Queue closed\n");
2839 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2845 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2846 union ibmvnic_crq *crq)
2848 unsigned int ua = adapter->vdev->unit_address;
2849 struct device *dev = &adapter->vdev->dev;
2850 u64 *u64_crq = (u64 *)crq;
2853 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2854 (unsigned long int)cpu_to_be64(u64_crq[0]),
2855 (unsigned long int)cpu_to_be64(u64_crq[1]));
2857 /* Make sure the hypervisor sees the complete request */
2860 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2861 cpu_to_be64(u64_crq[0]),
2862 cpu_to_be64(u64_crq[1]));
2866 dev_warn(dev, "CRQ Queue closed\n");
2867 dev_warn(dev, "Send error (rc=%d)\n", rc);
2873 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2875 union ibmvnic_crq crq;
2877 memset(&crq, 0, sizeof(crq));
2878 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2879 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2880 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2882 return ibmvnic_send_crq(adapter, &crq);
2885 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2887 union ibmvnic_crq crq;
2889 memset(&crq, 0, sizeof(crq));
2890 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2891 crq.version_exchange.cmd = VERSION_EXCHANGE;
2892 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2894 return ibmvnic_send_crq(adapter, &crq);
2897 struct vnic_login_client_data {
2903 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2907 /* Calculate the amount of buffer space needed for the
2908 * vnic client data in the login buffer. There are four entries,
2909 * OS name, LPAR name, device name, and a null last entry.
2911 len = 4 * sizeof(struct vnic_login_client_data);
2912 len += 6; /* "Linux" plus NULL */
2913 len += strlen(utsname()->nodename) + 1;
2914 len += strlen(adapter->netdev->name) + 1;
2919 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2920 struct vnic_login_client_data *vlcd)
2922 const char *os_name = "Linux";
2925 /* Type 1 - LPAR OS */
2927 len = strlen(os_name) + 1;
2928 vlcd->len = cpu_to_be16(len);
2929 strncpy(&vlcd->name, os_name, len);
2930 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2932 /* Type 2 - LPAR name */
2934 len = strlen(utsname()->nodename) + 1;
2935 vlcd->len = cpu_to_be16(len);
2936 strncpy(&vlcd->name, utsname()->nodename, len);
2937 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2939 /* Type 3 - device name */
2941 len = strlen(adapter->netdev->name) + 1;
2942 vlcd->len = cpu_to_be16(len);
2943 strncpy(&vlcd->name, adapter->netdev->name, len);
2946 static void send_login(struct ibmvnic_adapter *adapter)
2948 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2949 struct ibmvnic_login_buffer *login_buffer;
2950 struct device *dev = &adapter->vdev->dev;
2951 dma_addr_t rsp_buffer_token;
2952 dma_addr_t buffer_token;
2953 size_t rsp_buffer_size;
2954 union ibmvnic_crq crq;
2958 int client_data_len;
2959 struct vnic_login_client_data *vlcd;
2962 client_data_len = vnic_client_data_len(adapter);
2965 sizeof(struct ibmvnic_login_buffer) +
2966 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
2969 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
2971 goto buf_alloc_failed;
2973 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2975 if (dma_mapping_error(dev, buffer_token)) {
2976 dev_err(dev, "Couldn't map login buffer\n");
2977 goto buf_map_failed;
2980 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2981 sizeof(u64) * adapter->req_tx_queues +
2982 sizeof(u64) * adapter->req_rx_queues +
2983 sizeof(u64) * adapter->req_rx_queues +
2984 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
2986 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2987 if (!login_rsp_buffer)
2988 goto buf_rsp_alloc_failed;
2990 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2991 rsp_buffer_size, DMA_FROM_DEVICE);
2992 if (dma_mapping_error(dev, rsp_buffer_token)) {
2993 dev_err(dev, "Couldn't map login rsp buffer\n");
2994 goto buf_rsp_map_failed;
2997 adapter->login_buf = login_buffer;
2998 adapter->login_buf_token = buffer_token;
2999 adapter->login_buf_sz = buffer_size;
3000 adapter->login_rsp_buf = login_rsp_buffer;
3001 adapter->login_rsp_buf_token = rsp_buffer_token;
3002 adapter->login_rsp_buf_sz = rsp_buffer_size;
3004 login_buffer->len = cpu_to_be32(buffer_size);
3005 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3006 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3007 login_buffer->off_txcomp_subcrqs =
3008 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3009 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3010 login_buffer->off_rxcomp_subcrqs =
3011 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3012 sizeof(u64) * adapter->req_tx_queues);
3013 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3014 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3016 tx_list_p = (__be64 *)((char *)login_buffer +
3017 sizeof(struct ibmvnic_login_buffer));
3018 rx_list_p = (__be64 *)((char *)login_buffer +
3019 sizeof(struct ibmvnic_login_buffer) +
3020 sizeof(u64) * adapter->req_tx_queues);
3022 for (i = 0; i < adapter->req_tx_queues; i++) {
3023 if (adapter->tx_scrq[i]) {
3024 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3029 for (i = 0; i < adapter->req_rx_queues; i++) {
3030 if (adapter->rx_scrq[i]) {
3031 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3036 /* Insert vNIC login client data */
3037 vlcd = (struct vnic_login_client_data *)
3038 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3039 login_buffer->client_data_offset =
3040 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3041 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3043 vnic_add_client_data(adapter, vlcd);
3045 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3046 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3047 netdev_dbg(adapter->netdev, "%016lx\n",
3048 ((unsigned long int *)(adapter->login_buf))[i]);
3051 memset(&crq, 0, sizeof(crq));
3052 crq.login.first = IBMVNIC_CRQ_CMD;
3053 crq.login.cmd = LOGIN;
3054 crq.login.ioba = cpu_to_be32(buffer_token);
3055 crq.login.len = cpu_to_be32(buffer_size);
3056 ibmvnic_send_crq(adapter, &crq);
3061 kfree(login_rsp_buffer);
3062 buf_rsp_alloc_failed:
3063 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3065 kfree(login_buffer);
3070 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3073 union ibmvnic_crq crq;
3075 memset(&crq, 0, sizeof(crq));
3076 crq.request_map.first = IBMVNIC_CRQ_CMD;
3077 crq.request_map.cmd = REQUEST_MAP;
3078 crq.request_map.map_id = map_id;
3079 crq.request_map.ioba = cpu_to_be32(addr);
3080 crq.request_map.len = cpu_to_be32(len);
3081 ibmvnic_send_crq(adapter, &crq);
3084 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3086 union ibmvnic_crq crq;
3088 memset(&crq, 0, sizeof(crq));
3089 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3090 crq.request_unmap.cmd = REQUEST_UNMAP;
3091 crq.request_unmap.map_id = map_id;
3092 ibmvnic_send_crq(adapter, &crq);
3095 static void send_map_query(struct ibmvnic_adapter *adapter)
3097 union ibmvnic_crq crq;
3099 memset(&crq, 0, sizeof(crq));
3100 crq.query_map.first = IBMVNIC_CRQ_CMD;
3101 crq.query_map.cmd = QUERY_MAP;
3102 ibmvnic_send_crq(adapter, &crq);
3105 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3106 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3108 union ibmvnic_crq crq;
3110 atomic_set(&adapter->running_cap_crqs, 0);
3111 memset(&crq, 0, sizeof(crq));
3112 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3113 crq.query_capability.cmd = QUERY_CAPABILITY;
3115 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3116 atomic_inc(&adapter->running_cap_crqs);
3117 ibmvnic_send_crq(adapter, &crq);
3119 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3120 atomic_inc(&adapter->running_cap_crqs);
3121 ibmvnic_send_crq(adapter, &crq);
3123 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3124 atomic_inc(&adapter->running_cap_crqs);
3125 ibmvnic_send_crq(adapter, &crq);
3127 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3128 atomic_inc(&adapter->running_cap_crqs);
3129 ibmvnic_send_crq(adapter, &crq);
3131 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3132 atomic_inc(&adapter->running_cap_crqs);
3133 ibmvnic_send_crq(adapter, &crq);
3135 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3136 atomic_inc(&adapter->running_cap_crqs);
3137 ibmvnic_send_crq(adapter, &crq);
3139 crq.query_capability.capability =
3140 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3141 atomic_inc(&adapter->running_cap_crqs);
3142 ibmvnic_send_crq(adapter, &crq);
3144 crq.query_capability.capability =
3145 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3146 atomic_inc(&adapter->running_cap_crqs);
3147 ibmvnic_send_crq(adapter, &crq);
3149 crq.query_capability.capability =
3150 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3151 atomic_inc(&adapter->running_cap_crqs);
3152 ibmvnic_send_crq(adapter, &crq);
3154 crq.query_capability.capability =
3155 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3156 atomic_inc(&adapter->running_cap_crqs);
3157 ibmvnic_send_crq(adapter, &crq);
3159 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3160 atomic_inc(&adapter->running_cap_crqs);
3161 ibmvnic_send_crq(adapter, &crq);
3163 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3164 atomic_inc(&adapter->running_cap_crqs);
3165 ibmvnic_send_crq(adapter, &crq);
3167 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3168 atomic_inc(&adapter->running_cap_crqs);
3169 ibmvnic_send_crq(adapter, &crq);
3171 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3172 atomic_inc(&adapter->running_cap_crqs);
3173 ibmvnic_send_crq(adapter, &crq);
3175 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3176 atomic_inc(&adapter->running_cap_crqs);
3177 ibmvnic_send_crq(adapter, &crq);
3179 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3180 atomic_inc(&adapter->running_cap_crqs);
3181 ibmvnic_send_crq(adapter, &crq);
3183 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3184 atomic_inc(&adapter->running_cap_crqs);
3185 ibmvnic_send_crq(adapter, &crq);
3187 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3188 atomic_inc(&adapter->running_cap_crqs);
3189 ibmvnic_send_crq(adapter, &crq);
3191 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3192 atomic_inc(&adapter->running_cap_crqs);
3193 ibmvnic_send_crq(adapter, &crq);
3195 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3196 atomic_inc(&adapter->running_cap_crqs);
3197 ibmvnic_send_crq(adapter, &crq);
3199 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3200 atomic_inc(&adapter->running_cap_crqs);
3201 ibmvnic_send_crq(adapter, &crq);
3203 crq.query_capability.capability =
3204 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3205 atomic_inc(&adapter->running_cap_crqs);
3206 ibmvnic_send_crq(adapter, &crq);
3208 crq.query_capability.capability =
3209 cpu_to_be16(OPT_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(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3215 atomic_inc(&adapter->running_cap_crqs);
3216 ibmvnic_send_crq(adapter, &crq);
3218 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3219 atomic_inc(&adapter->running_cap_crqs);
3220 ibmvnic_send_crq(adapter, &crq);
3223 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3224 struct ibmvnic_adapter *adapter)
3226 struct device *dev = &adapter->vdev->dev;
3228 if (crq->get_vpd_size_rsp.rc.code) {
3229 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3230 crq->get_vpd_size_rsp.rc.code);
3231 complete(&adapter->fw_done);
3235 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3236 complete(&adapter->fw_done);
3239 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3240 struct ibmvnic_adapter *adapter)
3242 struct device *dev = &adapter->vdev->dev;
3243 unsigned char *substr = NULL, *ptr = NULL;
3244 u8 fw_level_len = 0;
3246 memset(adapter->fw_version, 0, 32);
3248 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3251 if (crq->get_vpd_rsp.rc.code) {
3252 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3253 crq->get_vpd_rsp.rc.code);
3257 /* get the position of the firmware version info
3258 * located after the ASCII 'RM' substring in the buffer
3260 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3262 dev_info(dev, "No FW level provided by VPD\n");
3266 /* get length of firmware level ASCII substring */
3267 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3268 fw_level_len = *(substr + 2);
3270 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3274 /* copy firmware version string from vpd into adapter */
3275 if ((substr + 3 + fw_level_len) <
3276 (adapter->vpd->buff + adapter->vpd->len)) {
3277 ptr = strncpy((char *)adapter->fw_version,
3278 substr + 3, fw_level_len);
3281 dev_err(dev, "Failed to isolate FW level string\n");
3283 dev_info(dev, "FW substr extrapolated VPD buff\n");
3287 complete(&adapter->fw_done);
3290 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3292 struct device *dev = &adapter->vdev->dev;
3293 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3294 union ibmvnic_crq crq;
3297 dma_unmap_single(dev, adapter->ip_offload_tok,
3298 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3300 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3301 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3302 netdev_dbg(adapter->netdev, "%016lx\n",
3303 ((unsigned long int *)(buf))[i]);
3305 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3306 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3307 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3308 buf->tcp_ipv4_chksum);
3309 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3310 buf->tcp_ipv6_chksum);
3311 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3312 buf->udp_ipv4_chksum);
3313 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3314 buf->udp_ipv6_chksum);
3315 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3316 buf->large_tx_ipv4);
3317 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3318 buf->large_tx_ipv6);
3319 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3320 buf->large_rx_ipv4);
3321 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3322 buf->large_rx_ipv6);
3323 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3324 buf->max_ipv4_header_size);
3325 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3326 buf->max_ipv6_header_size);
3327 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3328 buf->max_tcp_header_size);
3329 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3330 buf->max_udp_header_size);
3331 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3332 buf->max_large_tx_size);
3333 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3334 buf->max_large_rx_size);
3335 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3336 buf->ipv6_extension_header);
3337 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3338 buf->tcp_pseudosum_req);
3339 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3340 buf->num_ipv6_ext_headers);
3341 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3342 buf->off_ipv6_ext_headers);
3344 adapter->ip_offload_ctrl_tok =
3345 dma_map_single(dev, &adapter->ip_offload_ctrl,
3346 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3348 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3349 dev_err(dev, "Couldn't map ip offload control buffer\n");
3353 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3354 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3355 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3356 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3357 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3358 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3359 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3361 /* large_rx disabled for now, additional features needed */
3362 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3363 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3365 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3367 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3368 adapter->netdev->features |= NETIF_F_IP_CSUM;
3370 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3371 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3373 if ((adapter->netdev->features &
3374 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3375 adapter->netdev->features |= NETIF_F_RXCSUM;
3377 if (buf->large_tx_ipv4)
3378 adapter->netdev->features |= NETIF_F_TSO;
3379 if (buf->large_tx_ipv6)
3380 adapter->netdev->features |= NETIF_F_TSO6;
3382 adapter->netdev->hw_features |= adapter->netdev->features;
3384 memset(&crq, 0, sizeof(crq));
3385 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3386 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3387 crq.control_ip_offload.len =
3388 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3389 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3390 ibmvnic_send_crq(adapter, &crq);
3393 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3394 struct ibmvnic_adapter *adapter)
3396 struct device *dev = &adapter->vdev->dev;
3397 struct ibmvnic_error_buff *error_buff, *tmp;
3398 unsigned long flags;
3402 if (!crq->request_error_rsp.rc.code) {
3403 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3404 crq->request_error_rsp.rc.code);
3408 spin_lock_irqsave(&adapter->error_list_lock, flags);
3409 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3410 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3412 list_del(&error_buff->list);
3415 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3418 dev_err(dev, "Couldn't find error id %x\n",
3419 be32_to_cpu(crq->request_error_rsp.error_id));
3423 dev_err(dev, "Detailed info for error id %x:",
3424 be32_to_cpu(crq->request_error_rsp.error_id));
3426 for (i = 0; i < error_buff->len; i++) {
3427 pr_cont("%02x", (int)error_buff->buff[i]);
3433 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3435 kfree(error_buff->buff);
3439 static void request_error_information(struct ibmvnic_adapter *adapter,
3440 union ibmvnic_crq *err_crq)
3442 struct device *dev = &adapter->vdev->dev;
3443 struct net_device *netdev = adapter->netdev;
3444 struct ibmvnic_error_buff *error_buff;
3445 unsigned long timeout = msecs_to_jiffies(30000);
3446 union ibmvnic_crq crq;
3447 unsigned long flags;
3450 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3454 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3455 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3456 if (!error_buff->buff) {
3461 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3463 if (dma_mapping_error(dev, error_buff->dma)) {
3464 netdev_err(netdev, "Couldn't map error buffer\n");
3465 kfree(error_buff->buff);
3470 error_buff->len = detail_len;
3471 error_buff->error_id = err_crq->error_indication.error_id;
3473 spin_lock_irqsave(&adapter->error_list_lock, flags);
3474 list_add_tail(&error_buff->list, &adapter->errors);
3475 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3477 memset(&crq, 0, sizeof(crq));
3478 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3479 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3480 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3481 crq.request_error_info.len = cpu_to_be32(detail_len);
3482 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3484 rc = ibmvnic_send_crq(adapter, &crq);
3486 netdev_err(netdev, "failed to request error information\n");
3490 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3491 netdev_err(netdev, "timeout waiting for error information\n");
3498 spin_lock_irqsave(&adapter->error_list_lock, flags);
3499 list_del(&error_buff->list);
3500 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3502 kfree(error_buff->buff);
3506 static void handle_error_indication(union ibmvnic_crq *crq,
3507 struct ibmvnic_adapter *adapter)
3509 struct device *dev = &adapter->vdev->dev;
3511 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3512 crq->error_indication.flags
3513 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3514 be32_to_cpu(crq->error_indication.error_id),
3515 be16_to_cpu(crq->error_indication.error_cause));
3517 if (be32_to_cpu(crq->error_indication.error_id))
3518 request_error_information(adapter, crq);
3520 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3521 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3523 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3526 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3527 struct ibmvnic_adapter *adapter)
3529 struct net_device *netdev = adapter->netdev;
3530 struct device *dev = &adapter->vdev->dev;
3533 rc = crq->change_mac_addr_rsp.rc.code;
3535 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3538 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3542 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3543 struct ibmvnic_adapter *adapter)
3545 struct device *dev = &adapter->vdev->dev;
3549 atomic_dec(&adapter->running_cap_crqs);
3550 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3552 req_value = &adapter->req_tx_queues;
3556 req_value = &adapter->req_rx_queues;
3559 case REQ_RX_ADD_QUEUES:
3560 req_value = &adapter->req_rx_add_queues;
3563 case REQ_TX_ENTRIES_PER_SUBCRQ:
3564 req_value = &adapter->req_tx_entries_per_subcrq;
3565 name = "tx_entries_per_subcrq";
3567 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3568 req_value = &adapter->req_rx_add_entries_per_subcrq;
3569 name = "rx_add_entries_per_subcrq";
3572 req_value = &adapter->req_mtu;
3575 case PROMISC_REQUESTED:
3576 req_value = &adapter->promisc;
3580 dev_err(dev, "Got invalid cap request rsp %d\n",
3581 crq->request_capability.capability);
3585 switch (crq->request_capability_rsp.rc.code) {
3588 case PARTIALSUCCESS:
3589 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3591 (long int)be64_to_cpu(crq->request_capability_rsp.
3593 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
3594 ibmvnic_send_req_caps(adapter, 1);
3597 dev_err(dev, "Error %d in request cap rsp\n",
3598 crq->request_capability_rsp.rc.code);
3602 /* Done receiving requested capabilities, query IP offload support */
3603 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3604 union ibmvnic_crq newcrq;
3605 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3606 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3607 &adapter->ip_offload_buf;
3609 adapter->wait_capability = false;
3610 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3614 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3615 if (!firmware_has_feature(FW_FEATURE_CMO))
3616 dev_err(dev, "Couldn't map offload buffer\n");
3620 memset(&newcrq, 0, sizeof(newcrq));
3621 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3622 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3623 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3624 newcrq.query_ip_offload.ioba =
3625 cpu_to_be32(adapter->ip_offload_tok);
3627 ibmvnic_send_crq(adapter, &newcrq);
3631 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3632 struct ibmvnic_adapter *adapter)
3634 struct device *dev = &adapter->vdev->dev;
3635 struct net_device *netdev = adapter->netdev;
3636 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3637 struct ibmvnic_login_buffer *login = adapter->login_buf;
3640 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3642 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3643 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3645 /* If the number of queues requested can't be allocated by the
3646 * server, the login response will return with code 1. We will need
3647 * to resend the login buffer with fewer queues requested.
3649 if (login_rsp_crq->generic.rc.code) {
3650 adapter->renegotiate = true;
3651 complete(&adapter->init_done);
3655 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3657 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3658 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3659 netdev_dbg(adapter->netdev, "%016lx\n",
3660 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3664 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3665 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3666 adapter->req_rx_add_queues !=
3667 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3668 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3669 ibmvnic_remove(adapter->vdev);
3672 complete(&adapter->init_done);
3677 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3678 struct ibmvnic_adapter *adapter)
3680 struct device *dev = &adapter->vdev->dev;
3683 rc = crq->request_unmap_rsp.rc.code;
3685 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3688 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3689 struct ibmvnic_adapter *adapter)
3691 struct net_device *netdev = adapter->netdev;
3692 struct device *dev = &adapter->vdev->dev;
3695 rc = crq->query_map_rsp.rc.code;
3697 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3700 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3701 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3702 crq->query_map_rsp.free_pages);
3705 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3706 struct ibmvnic_adapter *adapter)
3708 struct net_device *netdev = adapter->netdev;
3709 struct device *dev = &adapter->vdev->dev;
3712 atomic_dec(&adapter->running_cap_crqs);
3713 netdev_dbg(netdev, "Outstanding queries: %d\n",
3714 atomic_read(&adapter->running_cap_crqs));
3715 rc = crq->query_capability.rc.code;
3717 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3721 switch (be16_to_cpu(crq->query_capability.capability)) {
3723 adapter->min_tx_queues =
3724 be64_to_cpu(crq->query_capability.number);
3725 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3726 adapter->min_tx_queues);
3729 adapter->min_rx_queues =
3730 be64_to_cpu(crq->query_capability.number);
3731 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3732 adapter->min_rx_queues);
3734 case MIN_RX_ADD_QUEUES:
3735 adapter->min_rx_add_queues =
3736 be64_to_cpu(crq->query_capability.number);
3737 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3738 adapter->min_rx_add_queues);
3741 adapter->max_tx_queues =
3742 be64_to_cpu(crq->query_capability.number);
3743 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3744 adapter->max_tx_queues);
3747 adapter->max_rx_queues =
3748 be64_to_cpu(crq->query_capability.number);
3749 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3750 adapter->max_rx_queues);
3752 case MAX_RX_ADD_QUEUES:
3753 adapter->max_rx_add_queues =
3754 be64_to_cpu(crq->query_capability.number);
3755 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3756 adapter->max_rx_add_queues);
3758 case MIN_TX_ENTRIES_PER_SUBCRQ:
3759 adapter->min_tx_entries_per_subcrq =
3760 be64_to_cpu(crq->query_capability.number);
3761 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3762 adapter->min_tx_entries_per_subcrq);
3764 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3765 adapter->min_rx_add_entries_per_subcrq =
3766 be64_to_cpu(crq->query_capability.number);
3767 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3768 adapter->min_rx_add_entries_per_subcrq);
3770 case MAX_TX_ENTRIES_PER_SUBCRQ:
3771 adapter->max_tx_entries_per_subcrq =
3772 be64_to_cpu(crq->query_capability.number);
3773 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3774 adapter->max_tx_entries_per_subcrq);
3776 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3777 adapter->max_rx_add_entries_per_subcrq =
3778 be64_to_cpu(crq->query_capability.number);
3779 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3780 adapter->max_rx_add_entries_per_subcrq);
3782 case TCP_IP_OFFLOAD:
3783 adapter->tcp_ip_offload =
3784 be64_to_cpu(crq->query_capability.number);
3785 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3786 adapter->tcp_ip_offload);
3788 case PROMISC_SUPPORTED:
3789 adapter->promisc_supported =
3790 be64_to_cpu(crq->query_capability.number);
3791 netdev_dbg(netdev, "promisc_supported = %lld\n",
3792 adapter->promisc_supported);
3795 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3796 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3797 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3800 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3801 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3802 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3804 case MAX_MULTICAST_FILTERS:
3805 adapter->max_multicast_filters =
3806 be64_to_cpu(crq->query_capability.number);
3807 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3808 adapter->max_multicast_filters);
3810 case VLAN_HEADER_INSERTION:
3811 adapter->vlan_header_insertion =
3812 be64_to_cpu(crq->query_capability.number);
3813 if (adapter->vlan_header_insertion)
3814 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3815 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3816 adapter->vlan_header_insertion);
3818 case RX_VLAN_HEADER_INSERTION:
3819 adapter->rx_vlan_header_insertion =
3820 be64_to_cpu(crq->query_capability.number);
3821 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3822 adapter->rx_vlan_header_insertion);
3824 case MAX_TX_SG_ENTRIES:
3825 adapter->max_tx_sg_entries =
3826 be64_to_cpu(crq->query_capability.number);
3827 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3828 adapter->max_tx_sg_entries);
3830 case RX_SG_SUPPORTED:
3831 adapter->rx_sg_supported =
3832 be64_to_cpu(crq->query_capability.number);
3833 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3834 adapter->rx_sg_supported);
3836 case OPT_TX_COMP_SUB_QUEUES:
3837 adapter->opt_tx_comp_sub_queues =
3838 be64_to_cpu(crq->query_capability.number);
3839 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3840 adapter->opt_tx_comp_sub_queues);
3842 case OPT_RX_COMP_QUEUES:
3843 adapter->opt_rx_comp_queues =
3844 be64_to_cpu(crq->query_capability.number);
3845 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3846 adapter->opt_rx_comp_queues);
3848 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3849 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3850 be64_to_cpu(crq->query_capability.number);
3851 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3852 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3854 case OPT_TX_ENTRIES_PER_SUBCRQ:
3855 adapter->opt_tx_entries_per_subcrq =
3856 be64_to_cpu(crq->query_capability.number);
3857 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3858 adapter->opt_tx_entries_per_subcrq);
3860 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3861 adapter->opt_rxba_entries_per_subcrq =
3862 be64_to_cpu(crq->query_capability.number);
3863 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3864 adapter->opt_rxba_entries_per_subcrq);
3866 case TX_RX_DESC_REQ:
3867 adapter->tx_rx_desc_req = crq->query_capability.number;
3868 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3869 adapter->tx_rx_desc_req);
3873 netdev_err(netdev, "Got invalid cap rsp %d\n",
3874 crq->query_capability.capability);
3878 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3879 adapter->wait_capability = false;
3880 ibmvnic_send_req_caps(adapter, 0);
3884 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3885 struct ibmvnic_adapter *adapter)
3887 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3888 struct net_device *netdev = adapter->netdev;
3889 struct device *dev = &adapter->vdev->dev;
3890 u64 *u64_crq = (u64 *)crq;
3893 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3894 (unsigned long int)cpu_to_be64(u64_crq[0]),
3895 (unsigned long int)cpu_to_be64(u64_crq[1]));
3896 switch (gen_crq->first) {
3897 case IBMVNIC_CRQ_INIT_RSP:
3898 switch (gen_crq->cmd) {
3899 case IBMVNIC_CRQ_INIT:
3900 dev_info(dev, "Partner initialized\n");
3901 adapter->from_passive_init = true;
3902 complete(&adapter->init_done);
3904 case IBMVNIC_CRQ_INIT_COMPLETE:
3905 dev_info(dev, "Partner initialization complete\n");
3906 send_version_xchg(adapter);
3909 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3912 case IBMVNIC_CRQ_XPORT_EVENT:
3913 netif_carrier_off(netdev);
3914 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3915 dev_info(dev, "Migrated, re-enabling adapter\n");
3916 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
3917 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3918 dev_info(dev, "Backing device failover detected\n");
3919 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
3921 /* The adapter lost the connection */
3922 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3924 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3927 case IBMVNIC_CRQ_CMD_RSP:
3930 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3935 switch (gen_crq->cmd) {
3936 case VERSION_EXCHANGE_RSP:
3937 rc = crq->version_exchange_rsp.rc.code;
3939 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3942 dev_info(dev, "Partner protocol version is %d\n",
3943 crq->version_exchange_rsp.version);
3944 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3947 be16_to_cpu(crq->version_exchange_rsp.version);
3948 send_cap_queries(adapter);
3950 case QUERY_CAPABILITY_RSP:
3951 handle_query_cap_rsp(crq, adapter);
3954 handle_query_map_rsp(crq, adapter);
3956 case REQUEST_MAP_RSP:
3957 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3958 complete(&adapter->fw_done);
3960 case REQUEST_UNMAP_RSP:
3961 handle_request_unmap_rsp(crq, adapter);
3963 case REQUEST_CAPABILITY_RSP:
3964 handle_request_cap_rsp(crq, adapter);
3967 netdev_dbg(netdev, "Got Login Response\n");
3968 handle_login_rsp(crq, adapter);
3970 case LOGICAL_LINK_STATE_RSP:
3972 "Got Logical Link State Response, state: %d rc: %d\n",
3973 crq->logical_link_state_rsp.link_state,
3974 crq->logical_link_state_rsp.rc.code);
3975 adapter->logical_link_state =
3976 crq->logical_link_state_rsp.link_state;
3977 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3978 complete(&adapter->init_done);
3980 case LINK_STATE_INDICATION:
3981 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3982 adapter->phys_link_state =
3983 crq->link_state_indication.phys_link_state;
3984 adapter->logical_link_state =
3985 crq->link_state_indication.logical_link_state;
3987 case CHANGE_MAC_ADDR_RSP:
3988 netdev_dbg(netdev, "Got MAC address change Response\n");
3989 handle_change_mac_rsp(crq, adapter);
3991 case ERROR_INDICATION:
3992 netdev_dbg(netdev, "Got Error Indication\n");
3993 handle_error_indication(crq, adapter);
3995 case REQUEST_ERROR_RSP:
3996 netdev_dbg(netdev, "Got Error Detail Response\n");
3997 handle_error_info_rsp(crq, adapter);
3999 case REQUEST_STATISTICS_RSP:
4000 netdev_dbg(netdev, "Got Statistics Response\n");
4001 complete(&adapter->stats_done);
4003 case QUERY_IP_OFFLOAD_RSP:
4004 netdev_dbg(netdev, "Got Query IP offload Response\n");
4005 handle_query_ip_offload_rsp(adapter);
4007 case MULTICAST_CTRL_RSP:
4008 netdev_dbg(netdev, "Got multicast control Response\n");
4010 case CONTROL_IP_OFFLOAD_RSP:
4011 netdev_dbg(netdev, "Got Control IP offload Response\n");
4012 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4013 sizeof(adapter->ip_offload_ctrl),
4015 complete(&adapter->init_done);
4017 case COLLECT_FW_TRACE_RSP:
4018 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4019 complete(&adapter->fw_done);
4021 case GET_VPD_SIZE_RSP:
4022 handle_vpd_size_rsp(crq, adapter);
4025 handle_vpd_rsp(crq, adapter);
4028 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4033 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4035 struct ibmvnic_adapter *adapter = instance;
4037 tasklet_schedule(&adapter->tasklet);
4041 static void ibmvnic_tasklet(void *data)
4043 struct ibmvnic_adapter *adapter = data;
4044 struct ibmvnic_crq_queue *queue = &adapter->crq;
4045 union ibmvnic_crq *crq;
4046 unsigned long flags;
4049 spin_lock_irqsave(&queue->lock, flags);
4051 /* Pull all the valid messages off the CRQ */
4052 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4053 ibmvnic_handle_crq(crq, adapter);
4054 crq->generic.first = 0;
4057 /* remain in tasklet until all
4058 * capabilities responses are received
4060 if (!adapter->wait_capability)
4063 /* if capabilities CRQ's were sent in this tasklet, the following
4064 * tasklet must wait until all responses are received
4066 if (atomic_read(&adapter->running_cap_crqs) != 0)
4067 adapter->wait_capability = true;
4068 spin_unlock_irqrestore(&queue->lock, flags);
4071 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4073 struct vio_dev *vdev = adapter->vdev;
4077 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4078 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4081 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4086 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4088 struct ibmvnic_crq_queue *crq = &adapter->crq;
4089 struct device *dev = &adapter->vdev->dev;
4090 struct vio_dev *vdev = adapter->vdev;
4095 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4096 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4098 /* Clean out the queue */
4099 memset(crq->msgs, 0, PAGE_SIZE);
4102 /* And re-open it again */
4103 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4104 crq->msg_token, PAGE_SIZE);
4107 /* Adapter is good, but other end is not ready */
4108 dev_warn(dev, "Partner adapter not ready\n");
4110 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4115 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4117 struct ibmvnic_crq_queue *crq = &adapter->crq;
4118 struct vio_dev *vdev = adapter->vdev;
4124 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4125 free_irq(vdev->irq, adapter);
4126 tasklet_kill(&adapter->tasklet);
4128 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4129 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4131 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4133 free_page((unsigned long)crq->msgs);
4137 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4139 struct ibmvnic_crq_queue *crq = &adapter->crq;
4140 struct device *dev = &adapter->vdev->dev;
4141 struct vio_dev *vdev = adapter->vdev;
4142 int rc, retrc = -ENOMEM;
4147 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4148 /* Should we allocate more than one page? */
4153 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4154 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4156 if (dma_mapping_error(dev, crq->msg_token))
4159 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4160 crq->msg_token, PAGE_SIZE);
4162 if (rc == H_RESOURCE)
4163 /* maybe kexecing and resource is busy. try a reset */
4164 rc = ibmvnic_reset_crq(adapter);
4167 if (rc == H_CLOSED) {
4168 dev_warn(dev, "Partner adapter not ready\n");
4170 dev_warn(dev, "Error %d opening adapter\n", rc);
4171 goto reg_crq_failed;
4176 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4177 (unsigned long)adapter);
4179 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4180 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4183 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4185 goto req_irq_failed;
4188 rc = vio_enable_interrupts(vdev);
4190 dev_err(dev, "Error %d enabling interrupts\n", rc);
4191 goto req_irq_failed;
4195 spin_lock_init(&crq->lock);
4200 tasklet_kill(&adapter->tasklet);
4202 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4203 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4205 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4207 free_page((unsigned long)crq->msgs);
4212 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4214 struct device *dev = &adapter->vdev->dev;
4215 unsigned long timeout = msecs_to_jiffies(30000);
4218 if (adapter->resetting && !adapter->wait_for_reset) {
4219 rc = ibmvnic_reset_crq(adapter);
4221 rc = vio_enable_interrupts(adapter->vdev);
4223 rc = init_crq_queue(adapter);
4227 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4231 adapter->from_passive_init = false;
4233 init_completion(&adapter->init_done);
4234 adapter->init_done_rc = 0;
4235 ibmvnic_send_crq_init(adapter);
4236 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4237 dev_err(dev, "Initialization sequence timed out\n");
4241 if (adapter->init_done_rc) {
4242 release_crq_queue(adapter);
4243 return adapter->init_done_rc;
4246 if (adapter->from_passive_init) {
4247 adapter->state = VNIC_OPEN;
4248 adapter->from_passive_init = false;
4252 if (adapter->resetting && !adapter->wait_for_reset)
4253 rc = reset_sub_crq_queues(adapter);
4255 rc = init_sub_crqs(adapter);
4257 dev_err(dev, "Initialization of sub crqs failed\n");
4258 release_crq_queue(adapter);
4262 rc = init_sub_crq_irqs(adapter);
4264 dev_err(dev, "Failed to initialize sub crq irqs\n");
4265 release_crq_queue(adapter);
4271 static struct device_attribute dev_attr_failover;
4273 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4275 struct ibmvnic_adapter *adapter;
4276 struct net_device *netdev;
4277 unsigned char *mac_addr_p;
4280 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4283 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4284 VETH_MAC_ADDR, NULL);
4287 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4288 __FILE__, __LINE__);
4292 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4293 IBMVNIC_MAX_QUEUES);
4297 adapter = netdev_priv(netdev);
4298 adapter->state = VNIC_PROBING;
4299 dev_set_drvdata(&dev->dev, netdev);
4300 adapter->vdev = dev;
4301 adapter->netdev = netdev;
4303 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4304 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4305 netdev->irq = dev->irq;
4306 netdev->netdev_ops = &ibmvnic_netdev_ops;
4307 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4308 SET_NETDEV_DEV(netdev, &dev->dev);
4310 spin_lock_init(&adapter->stats_lock);
4312 INIT_LIST_HEAD(&adapter->errors);
4313 spin_lock_init(&adapter->error_list_lock);
4315 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4316 INIT_LIST_HEAD(&adapter->rwi_list);
4317 mutex_init(&adapter->reset_lock);
4318 mutex_init(&adapter->rwi_lock);
4319 adapter->resetting = false;
4321 adapter->mac_change_pending = false;
4324 rc = ibmvnic_init(adapter);
4325 if (rc && rc != EAGAIN)
4326 goto ibmvnic_init_fail;
4327 } while (rc == EAGAIN);
4329 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4330 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4331 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4333 rc = device_create_file(&dev->dev, &dev_attr_failover);
4335 goto ibmvnic_init_fail;
4337 netif_carrier_off(netdev);
4338 rc = register_netdev(netdev);
4340 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4341 goto ibmvnic_register_fail;
4343 dev_info(&dev->dev, "ibmvnic registered\n");
4345 adapter->state = VNIC_PROBED;
4347 adapter->wait_for_reset = false;
4351 ibmvnic_register_fail:
4352 device_remove_file(&dev->dev, &dev_attr_failover);
4355 release_sub_crqs(adapter);
4356 release_crq_queue(adapter);
4357 free_netdev(netdev);
4362 static int ibmvnic_remove(struct vio_dev *dev)
4364 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4365 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4367 adapter->state = VNIC_REMOVING;
4368 unregister_netdev(netdev);
4369 mutex_lock(&adapter->reset_lock);
4371 release_resources(adapter);
4372 release_sub_crqs(adapter);
4373 release_crq_queue(adapter);
4375 adapter->state = VNIC_REMOVED;
4377 mutex_unlock(&adapter->reset_lock);
4378 device_remove_file(&dev->dev, &dev_attr_failover);
4379 free_netdev(netdev);
4380 dev_set_drvdata(&dev->dev, NULL);
4385 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4386 const char *buf, size_t count)
4388 struct net_device *netdev = dev_get_drvdata(dev);
4389 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4390 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4391 __be64 session_token;
4394 if (!sysfs_streq(buf, "1"))
4397 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4398 H_GET_SESSION_TOKEN, 0, 0, 0);
4400 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4405 session_token = (__be64)retbuf[0];
4406 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4407 be64_to_cpu(session_token));
4408 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4409 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4411 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4419 static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4421 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4423 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4424 struct ibmvnic_adapter *adapter;
4425 struct iommu_table *tbl;
4426 unsigned long ret = 0;
4429 tbl = get_iommu_table_base(&vdev->dev);
4431 /* netdev inits at probe time along with the structures we need below*/
4433 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4435 adapter = netdev_priv(netdev);
4437 ret += PAGE_SIZE; /* the crq message queue */
4438 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4440 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4441 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4443 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4445 ret += adapter->rx_pool[i].size *
4446 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4451 static int ibmvnic_resume(struct device *dev)
4453 struct net_device *netdev = dev_get_drvdata(dev);
4454 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4456 if (adapter->state != VNIC_OPEN)
4459 tasklet_schedule(&adapter->tasklet);
4464 static const struct vio_device_id ibmvnic_device_table[] = {
4465 {"network", "IBM,vnic"},
4468 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4470 static const struct dev_pm_ops ibmvnic_pm_ops = {
4471 .resume = ibmvnic_resume
4474 static struct vio_driver ibmvnic_driver = {
4475 .id_table = ibmvnic_device_table,
4476 .probe = ibmvnic_probe,
4477 .remove = ibmvnic_remove,
4478 .get_desired_dma = ibmvnic_get_desired_dma,
4479 .name = ibmvnic_driver_name,
4480 .pm = &ibmvnic_pm_ops,
4483 /* module functions */
4484 static int __init ibmvnic_module_init(void)
4486 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4487 IBMVNIC_DRIVER_VERSION);
4489 return vio_register_driver(&ibmvnic_driver);
4492 static void __exit ibmvnic_module_exit(void)
4494 vio_unregister_driver(&ibmvnic_driver);
4497 module_init(ibmvnic_module_init);
4498 module_exit(ibmvnic_module_exit);