1 /**************************************************************************/
3 /* IBM System i and System p Virtual NIC Device Driver */
4 /* Copyright (C) 2014 IBM Corp. */
5 /* Santiago Leon (santi_leon@yahoo.com) */
6 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7 /* John Allen (jallen@linux.vnet.ibm.com) */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2 of the License, or */
12 /* (at your option) any later version. */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program. */
22 /* This module contains the implementation of a virtual ethernet device */
23 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24 /* option of the RS/6000 Platform Architecture to interface with virtual */
25 /* ethernet NICs that are presented to the partition by the hypervisor. */
27 /* Messages are passed between the VNIC driver and the VNIC server using */
28 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29 /* issue and receive commands that initiate communication with the server */
30 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31 /* are used by the driver to notify the server that a packet is */
32 /* ready for transmission or that a buffer has been added to receive a */
33 /* packet. Subsequently, sCRQs are used by the server to notify the */
34 /* driver that a packet transmission has been completed or that a packet */
35 /* has been received and placed in a waiting buffer. */
37 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38 /* which skbs are DMA mapped and immediately unmapped when the transmit */
39 /* or receive has been completed, the VNIC driver is required to use */
40 /* "long term mapping". This entails that large, continuous DMA mapped */
41 /* buffers are allocated on driver initialization and these buffers are */
42 /* then continuously reused to pass skbs to and from the VNIC server. */
44 /**************************************************************************/
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/types.h>
49 #include <linux/errno.h>
50 #include <linux/completion.h>
51 #include <linux/ioport.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/kernel.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
60 #include <linux/ethtool.h>
61 #include <linux/proc_fs.h>
62 #include <linux/if_arp.h>
65 #include <linux/ipv6.h>
66 #include <linux/irq.h>
67 #include <linux/kthread.h>
68 #include <linux/seq_file.h>
69 #include <linux/interrupt.h>
70 #include <net/net_namespace.h>
71 #include <asm/hvcall.h>
72 #include <linux/atomic.h>
74 #include <asm/iommu.h>
75 #include <linux/uaccess.h>
76 #include <asm/firmware.h>
77 #include <linux/workqueue.h>
78 #include <linux/if_vlan.h>
79 #include <linux/utsname.h>
83 static const char ibmvnic_driver_name[] = "ibmvnic";
84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
86 MODULE_AUTHOR("Santiago Leon");
87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92 static int ibmvnic_remove(struct vio_dev *);
93 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102 static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104 static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106 static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110 static int ibmvnic_poll(struct napi_struct *napi, int data);
111 static void send_map_query(struct ibmvnic_adapter *adapter);
112 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static void send_request_unmap(struct ibmvnic_adapter *, u8);
114 static int send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
121 static int init_crq_queue(struct ibmvnic_adapter *adapter);
123 struct ibmvnic_stat {
124 char name[ETH_GSTRING_LEN];
128 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
129 offsetof(struct ibmvnic_statistics, stat))
130 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
132 static const struct ibmvnic_stat ibmvnic_stats[] = {
133 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
134 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
135 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
136 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
137 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
138 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
139 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
140 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
141 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
142 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
143 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
144 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
145 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
146 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
147 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
148 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
149 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
150 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
151 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
152 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
153 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
154 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
157 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
158 unsigned long length, unsigned long *number,
161 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
164 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
171 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
172 struct ibmvnic_long_term_buff *ltb, int size)
174 struct device *dev = &adapter->vdev->dev;
177 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
181 dev_err(dev, "Couldn't alloc long term buffer\n");
184 ltb->map_id = adapter->map_id;
187 init_completion(&adapter->fw_done);
188 send_request_map(adapter, ltb->addr,
189 ltb->size, ltb->map_id);
190 wait_for_completion(&adapter->fw_done);
192 if (adapter->fw_done_rc) {
193 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
194 adapter->fw_done_rc);
195 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
201 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
202 struct ibmvnic_long_term_buff *ltb)
204 struct device *dev = &adapter->vdev->dev;
209 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
210 adapter->reset_reason != VNIC_RESET_MOBILITY)
211 send_request_unmap(adapter, ltb->map_id);
212 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
215 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
216 struct ibmvnic_long_term_buff *ltb)
218 memset(ltb->buff, 0, ltb->size);
220 init_completion(&adapter->fw_done);
221 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
222 wait_for_completion(&adapter->fw_done);
224 if (adapter->fw_done_rc) {
225 dev_info(&adapter->vdev->dev,
226 "Reset failed, attempting to free and reallocate buffer\n");
227 free_long_term_buff(adapter, ltb);
228 return alloc_long_term_buff(adapter, ltb, ltb->size);
233 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
237 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
239 adapter->rx_pool[i].active = 0;
242 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
243 struct ibmvnic_rx_pool *pool)
245 int count = pool->size - atomic_read(&pool->available);
246 struct device *dev = &adapter->vdev->dev;
247 int buffers_added = 0;
248 unsigned long lpar_rc;
249 union sub_crq sub_crq;
262 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
263 be32_to_cpu(adapter->login_rsp_buf->
266 for (i = 0; i < count; ++i) {
267 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
269 dev_err(dev, "Couldn't replenish rx buff\n");
270 adapter->replenish_no_mem++;
274 index = pool->free_map[pool->next_free];
276 if (pool->rx_buff[index].skb)
277 dev_err(dev, "Inconsistent free_map!\n");
279 /* Copy the skb to the long term mapped DMA buffer */
280 offset = index * pool->buff_size;
281 dst = pool->long_term_buff.buff + offset;
282 memset(dst, 0, pool->buff_size);
283 dma_addr = pool->long_term_buff.addr + offset;
284 pool->rx_buff[index].data = dst;
286 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
287 pool->rx_buff[index].dma = dma_addr;
288 pool->rx_buff[index].skb = skb;
289 pool->rx_buff[index].pool_index = pool->index;
290 pool->rx_buff[index].size = pool->buff_size;
292 memset(&sub_crq, 0, sizeof(sub_crq));
293 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
294 sub_crq.rx_add.correlator =
295 cpu_to_be64((u64)&pool->rx_buff[index]);
296 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
297 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
299 /* The length field of the sCRQ is defined to be 24 bits so the
300 * buffer size needs to be left shifted by a byte before it is
301 * converted to big endian to prevent the last byte from being
304 #ifdef __LITTLE_ENDIAN__
307 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
309 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
311 if (lpar_rc != H_SUCCESS)
315 adapter->replenish_add_buff_success++;
316 pool->next_free = (pool->next_free + 1) % pool->size;
318 atomic_add(buffers_added, &pool->available);
322 dev_info(dev, "replenish pools failure\n");
323 pool->free_map[pool->next_free] = index;
324 pool->rx_buff[index].skb = NULL;
326 dev_kfree_skb_any(skb);
327 adapter->replenish_add_buff_failure++;
328 atomic_add(buffers_added, &pool->available);
330 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
331 /* Disable buffer pool replenishment and report carrier off if
332 * queue is closed or pending failover.
333 * Firmware guarantees that a signal will be sent to the
334 * driver, triggering a reset.
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
361 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
363 adapter->tx_stats_buffers =
364 kcalloc(IBMVNIC_MAX_QUEUES,
365 sizeof(struct ibmvnic_tx_queue_stats),
367 if (!adapter->tx_stats_buffers)
370 adapter->rx_stats_buffers =
371 kcalloc(IBMVNIC_MAX_QUEUES,
372 sizeof(struct ibmvnic_rx_queue_stats),
374 if (!adapter->rx_stats_buffers)
380 static void release_stats_token(struct ibmvnic_adapter *adapter)
382 struct device *dev = &adapter->vdev->dev;
384 if (!adapter->stats_token)
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
390 adapter->stats_token = 0;
393 static int init_stats_token(struct ibmvnic_adapter *adapter)
395 struct device *dev = &adapter->vdev->dev;
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
406 adapter->stats_token = stok;
407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
411 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
413 struct ibmvnic_rx_pool *rx_pool;
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
456 static void release_rx_pools(struct ibmvnic_adapter *adapter)
458 struct ibmvnic_rx_pool *rx_pool;
461 if (!adapter->rx_pool)
464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
465 rx_pool = &adapter->rx_pool[i];
467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
472 if (!rx_pool->rx_buff)
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
482 kfree(rx_pool->rx_buff);
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
487 adapter->num_active_rx_pools = 0;
490 static int init_rx_pools(struct net_device *netdev)
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
512 adapter->num_active_rx_pools = rxadd_subcrqs;
514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
517 netdev_dbg(adapter->netdev,
518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
560 static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
561 struct ibmvnic_tx_pool *tx_pool)
565 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
569 memset(tx_pool->tx_buff, 0,
570 tx_pool->num_buffers *
571 sizeof(struct ibmvnic_tx_buff));
573 for (i = 0; i < tx_pool->num_buffers; i++)
574 tx_pool->free_map[i] = i;
576 tx_pool->consumer_index = 0;
577 tx_pool->producer_index = 0;
582 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
587 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
588 for (i = 0; i < tx_scrqs; i++) {
589 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
592 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
600 static void release_vpd_data(struct ibmvnic_adapter *adapter)
605 kfree(adapter->vpd->buff);
611 static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
612 struct ibmvnic_tx_pool *tx_pool)
614 kfree(tx_pool->tx_buff);
615 kfree(tx_pool->free_map);
616 free_long_term_buff(adapter, &tx_pool->long_term_buff);
619 static void release_tx_pools(struct ibmvnic_adapter *adapter)
623 if (!adapter->tx_pool)
626 for (i = 0; i < adapter->num_active_tx_pools; i++) {
627 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
628 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
631 kfree(adapter->tx_pool);
632 adapter->tx_pool = NULL;
633 kfree(adapter->tso_pool);
634 adapter->tso_pool = NULL;
635 adapter->num_active_tx_pools = 0;
638 static int init_one_tx_pool(struct net_device *netdev,
639 struct ibmvnic_tx_pool *tx_pool,
640 int num_entries, int buf_size)
642 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
645 tx_pool->tx_buff = kcalloc(num_entries,
646 sizeof(struct ibmvnic_tx_buff),
648 if (!tx_pool->tx_buff)
651 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
652 num_entries * buf_size))
655 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
656 if (!tx_pool->free_map)
659 for (i = 0; i < num_entries; i++)
660 tx_pool->free_map[i] = i;
662 tx_pool->consumer_index = 0;
663 tx_pool->producer_index = 0;
664 tx_pool->num_buffers = num_entries;
665 tx_pool->buf_size = buf_size;
670 static int init_tx_pools(struct net_device *netdev)
672 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
676 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
677 adapter->tx_pool = kcalloc(tx_subcrqs,
678 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
679 if (!adapter->tx_pool)
682 adapter->tso_pool = kcalloc(tx_subcrqs,
683 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
684 if (!adapter->tso_pool)
687 adapter->num_active_tx_pools = tx_subcrqs;
689 for (i = 0; i < tx_subcrqs; i++) {
690 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
691 adapter->req_tx_entries_per_subcrq,
692 adapter->req_mtu + VLAN_HLEN);
694 release_tx_pools(adapter);
698 init_one_tx_pool(netdev, &adapter->tso_pool[i],
702 release_tx_pools(adapter);
710 static void release_error_buffers(struct ibmvnic_adapter *adapter)
712 struct device *dev = &adapter->vdev->dev;
713 struct ibmvnic_error_buff *error_buff, *tmp;
716 spin_lock_irqsave(&adapter->error_list_lock, flags);
717 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
718 list_del(&error_buff->list);
719 dma_unmap_single(dev, error_buff->dma, error_buff->len,
721 kfree(error_buff->buff);
724 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
727 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
731 if (adapter->napi_enabled)
734 for (i = 0; i < adapter->req_rx_queues; i++)
735 napi_enable(&adapter->napi[i]);
737 adapter->napi_enabled = true;
740 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
744 if (!adapter->napi_enabled)
747 for (i = 0; i < adapter->req_rx_queues; i++) {
748 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
749 napi_disable(&adapter->napi[i]);
752 adapter->napi_enabled = false;
755 static int init_napi(struct ibmvnic_adapter *adapter)
759 adapter->napi = kcalloc(adapter->req_rx_queues,
760 sizeof(struct napi_struct), GFP_KERNEL);
764 for (i = 0; i < adapter->req_rx_queues; i++) {
765 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
766 netif_napi_add(adapter->netdev, &adapter->napi[i],
767 ibmvnic_poll, NAPI_POLL_WEIGHT);
770 adapter->num_active_rx_napi = adapter->req_rx_queues;
774 static void release_napi(struct ibmvnic_adapter *adapter)
781 for (i = 0; i < adapter->num_active_rx_napi; i++) {
782 if (&adapter->napi[i]) {
783 netdev_dbg(adapter->netdev,
784 "Releasing napi[%d]\n", i);
785 netif_napi_del(&adapter->napi[i]);
789 kfree(adapter->napi);
790 adapter->napi = NULL;
791 adapter->num_active_rx_napi = 0;
792 adapter->napi_enabled = false;
795 static int ibmvnic_login(struct net_device *netdev)
797 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
798 unsigned long timeout = msecs_to_jiffies(30000);
803 if (retry_count > IBMVNIC_MAX_QUEUES) {
804 netdev_warn(netdev, "Login attempts exceeded\n");
808 adapter->init_done_rc = 0;
809 reinit_completion(&adapter->init_done);
810 rc = send_login(adapter);
812 netdev_warn(netdev, "Unable to login\n");
816 if (!wait_for_completion_timeout(&adapter->init_done,
818 netdev_warn(netdev, "Login timed out\n");
822 if (adapter->init_done_rc == PARTIALSUCCESS) {
824 release_sub_crqs(adapter, 1);
826 adapter->init_done_rc = 0;
827 reinit_completion(&adapter->init_done);
828 send_cap_queries(adapter);
829 if (!wait_for_completion_timeout(&adapter->init_done,
832 "Capabilities query timed out\n");
836 rc = init_sub_crqs(adapter);
839 "SCRQ initialization failed\n");
843 rc = init_sub_crq_irqs(adapter);
846 "SCRQ irq initialization failed\n");
849 } else if (adapter->init_done_rc) {
850 netdev_warn(netdev, "Adapter login failed\n");
853 } while (adapter->init_done_rc == PARTIALSUCCESS);
855 /* handle pending MAC address changes after successful login */
856 if (adapter->mac_change_pending) {
857 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
858 adapter->mac_change_pending = false;
864 static void release_login_buffer(struct ibmvnic_adapter *adapter)
866 kfree(adapter->login_buf);
867 adapter->login_buf = NULL;
870 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
872 kfree(adapter->login_rsp_buf);
873 adapter->login_rsp_buf = NULL;
876 static void release_resources(struct ibmvnic_adapter *adapter)
878 release_vpd_data(adapter);
880 release_tx_pools(adapter);
881 release_rx_pools(adapter);
883 release_error_buffers(adapter);
884 release_napi(adapter);
885 release_login_rsp_buffer(adapter);
888 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
890 struct net_device *netdev = adapter->netdev;
891 unsigned long timeout = msecs_to_jiffies(30000);
892 union ibmvnic_crq crq;
896 netdev_dbg(netdev, "setting link state %d\n", link_state);
898 memset(&crq, 0, sizeof(crq));
899 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
900 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
901 crq.logical_link_state.link_state = link_state;
906 reinit_completion(&adapter->init_done);
907 rc = ibmvnic_send_crq(adapter, &crq);
909 netdev_err(netdev, "Failed to set link state\n");
913 if (!wait_for_completion_timeout(&adapter->init_done,
915 netdev_err(netdev, "timeout setting link state\n");
919 if (adapter->init_done_rc == 1) {
920 /* Partuial success, delay and re-send */
929 static int set_real_num_queues(struct net_device *netdev)
931 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
934 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
935 adapter->req_tx_queues, adapter->req_rx_queues);
937 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
939 netdev_err(netdev, "failed to set the number of tx queues\n");
943 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
945 netdev_err(netdev, "failed to set the number of rx queues\n");
950 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
952 struct device *dev = &adapter->vdev->dev;
953 union ibmvnic_crq crq;
956 if (adapter->vpd->buff)
957 len = adapter->vpd->len;
959 init_completion(&adapter->fw_done);
960 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
961 crq.get_vpd_size.cmd = GET_VPD_SIZE;
962 ibmvnic_send_crq(adapter, &crq);
963 wait_for_completion(&adapter->fw_done);
965 if (!adapter->vpd->len)
968 if (!adapter->vpd->buff)
969 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
970 else if (adapter->vpd->len != len)
972 krealloc(adapter->vpd->buff,
973 adapter->vpd->len, GFP_KERNEL);
975 if (!adapter->vpd->buff) {
976 dev_err(dev, "Could allocate VPD buffer\n");
980 adapter->vpd->dma_addr =
981 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
983 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
984 dev_err(dev, "Could not map VPD buffer\n");
985 kfree(adapter->vpd->buff);
986 adapter->vpd->buff = NULL;
990 reinit_completion(&adapter->fw_done);
991 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
992 crq.get_vpd.cmd = GET_VPD;
993 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
994 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
995 ibmvnic_send_crq(adapter, &crq);
996 wait_for_completion(&adapter->fw_done);
1001 static int init_resources(struct ibmvnic_adapter *adapter)
1003 struct net_device *netdev = adapter->netdev;
1006 rc = set_real_num_queues(netdev);
1010 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1014 /* Vital Product Data (VPD) */
1015 rc = ibmvnic_get_vpd(adapter);
1017 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1021 adapter->map_id = 1;
1023 rc = init_napi(adapter);
1027 send_map_query(adapter);
1029 rc = init_rx_pools(netdev);
1033 rc = init_tx_pools(netdev);
1037 static int __ibmvnic_open(struct net_device *netdev)
1039 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1040 enum vnic_state prev_state = adapter->state;
1043 adapter->state = VNIC_OPENING;
1044 replenish_pools(adapter);
1045 ibmvnic_napi_enable(adapter);
1047 /* We're ready to receive frames, enable the sub-crq interrupts and
1048 * set the logical link state to up
1050 for (i = 0; i < adapter->req_rx_queues; i++) {
1051 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1052 if (prev_state == VNIC_CLOSED)
1053 enable_irq(adapter->rx_scrq[i]->irq);
1054 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1057 for (i = 0; i < adapter->req_tx_queues; i++) {
1058 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1059 if (prev_state == VNIC_CLOSED)
1060 enable_irq(adapter->tx_scrq[i]->irq);
1061 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1064 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1066 for (i = 0; i < adapter->req_rx_queues; i++)
1067 napi_disable(&adapter->napi[i]);
1068 release_resources(adapter);
1072 netif_tx_start_all_queues(netdev);
1074 if (prev_state == VNIC_CLOSED) {
1075 for (i = 0; i < adapter->req_rx_queues; i++)
1076 napi_schedule(&adapter->napi[i]);
1079 adapter->state = VNIC_OPEN;
1083 static int ibmvnic_open(struct net_device *netdev)
1085 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1088 /* If device failover is pending, just set device state and return.
1089 * Device operation will be handled by reset routine.
1091 if (adapter->failover_pending) {
1092 adapter->state = VNIC_OPEN;
1096 mutex_lock(&adapter->reset_lock);
1098 if (adapter->state != VNIC_CLOSED) {
1099 rc = ibmvnic_login(netdev);
1101 mutex_unlock(&adapter->reset_lock);
1105 rc = init_resources(adapter);
1107 netdev_err(netdev, "failed to initialize resources\n");
1108 release_resources(adapter);
1109 mutex_unlock(&adapter->reset_lock);
1114 rc = __ibmvnic_open(netdev);
1115 netif_carrier_on(netdev);
1117 mutex_unlock(&adapter->reset_lock);
1122 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1124 struct ibmvnic_rx_pool *rx_pool;
1125 struct ibmvnic_rx_buff *rx_buff;
1130 if (!adapter->rx_pool)
1133 rx_scrqs = adapter->num_active_rx_pools;
1134 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1136 /* Free any remaining skbs in the rx buffer pools */
1137 for (i = 0; i < rx_scrqs; i++) {
1138 rx_pool = &adapter->rx_pool[i];
1139 if (!rx_pool || !rx_pool->rx_buff)
1142 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1143 for (j = 0; j < rx_entries; j++) {
1144 rx_buff = &rx_pool->rx_buff[j];
1145 if (rx_buff && rx_buff->skb) {
1146 dev_kfree_skb_any(rx_buff->skb);
1147 rx_buff->skb = NULL;
1153 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1154 struct ibmvnic_tx_pool *tx_pool)
1156 struct ibmvnic_tx_buff *tx_buff;
1160 if (!tx_pool || !tx_pool->tx_buff)
1163 tx_entries = tx_pool->num_buffers;
1165 for (i = 0; i < tx_entries; i++) {
1166 tx_buff = &tx_pool->tx_buff[i];
1167 if (tx_buff && tx_buff->skb) {
1168 dev_kfree_skb_any(tx_buff->skb);
1169 tx_buff->skb = NULL;
1174 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1179 if (!adapter->tx_pool || !adapter->tso_pool)
1182 tx_scrqs = adapter->num_active_tx_pools;
1184 /* Free any remaining skbs in the tx buffer pools */
1185 for (i = 0; i < tx_scrqs; i++) {
1186 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1187 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1188 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1192 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1194 struct net_device *netdev = adapter->netdev;
1197 if (adapter->tx_scrq) {
1198 for (i = 0; i < adapter->req_tx_queues; i++)
1199 if (adapter->tx_scrq[i]->irq) {
1201 "Disabling tx_scrq[%d] irq\n", i);
1202 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1203 disable_irq(adapter->tx_scrq[i]->irq);
1207 if (adapter->rx_scrq) {
1208 for (i = 0; i < adapter->req_rx_queues; i++) {
1209 if (adapter->rx_scrq[i]->irq) {
1211 "Disabling rx_scrq[%d] irq\n", i);
1212 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1213 disable_irq(adapter->rx_scrq[i]->irq);
1219 static void ibmvnic_cleanup(struct net_device *netdev)
1221 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1223 /* ensure that transmissions are stopped if called by do_reset */
1224 if (adapter->resetting)
1225 netif_tx_disable(netdev);
1227 netif_tx_stop_all_queues(netdev);
1229 ibmvnic_napi_disable(adapter);
1230 ibmvnic_disable_irqs(adapter);
1232 clean_rx_pools(adapter);
1233 clean_tx_pools(adapter);
1236 static int __ibmvnic_close(struct net_device *netdev)
1238 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1241 adapter->state = VNIC_CLOSING;
1242 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1245 adapter->state = VNIC_CLOSED;
1249 static int ibmvnic_close(struct net_device *netdev)
1251 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1254 /* If device failover is pending, just set device state and return.
1255 * Device operation will be handled by reset routine.
1257 if (adapter->failover_pending) {
1258 adapter->state = VNIC_CLOSED;
1262 mutex_lock(&adapter->reset_lock);
1263 rc = __ibmvnic_close(netdev);
1264 ibmvnic_cleanup(netdev);
1265 mutex_unlock(&adapter->reset_lock);
1271 * build_hdr_data - creates L2/L3/L4 header data buffer
1272 * @hdr_field - bitfield determining needed headers
1273 * @skb - socket buffer
1274 * @hdr_len - array of header lengths
1275 * @tot_len - total length of data
1277 * Reads hdr_field to determine which headers are needed by firmware.
1278 * Builds a buffer containing these headers. Saves individual header
1279 * lengths and total buffer length to be used to build descriptors.
1281 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1282 int *hdr_len, u8 *hdr_data)
1287 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1288 hdr_len[0] = sizeof(struct vlan_ethhdr);
1290 hdr_len[0] = sizeof(struct ethhdr);
1292 if (skb->protocol == htons(ETH_P_IP)) {
1293 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1294 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1295 hdr_len[2] = tcp_hdrlen(skb);
1296 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1297 hdr_len[2] = sizeof(struct udphdr);
1298 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1299 hdr_len[1] = sizeof(struct ipv6hdr);
1300 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1301 hdr_len[2] = tcp_hdrlen(skb);
1302 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1303 hdr_len[2] = sizeof(struct udphdr);
1304 } else if (skb->protocol == htons(ETH_P_ARP)) {
1305 hdr_len[1] = arp_hdr_len(skb->dev);
1309 memset(hdr_data, 0, 120);
1310 if ((hdr_field >> 6) & 1) {
1311 hdr = skb_mac_header(skb);
1312 memcpy(hdr_data, hdr, hdr_len[0]);
1316 if ((hdr_field >> 5) & 1) {
1317 hdr = skb_network_header(skb);
1318 memcpy(hdr_data + len, hdr, hdr_len[1]);
1322 if ((hdr_field >> 4) & 1) {
1323 hdr = skb_transport_header(skb);
1324 memcpy(hdr_data + len, hdr, hdr_len[2]);
1331 * create_hdr_descs - create header and header extension descriptors
1332 * @hdr_field - bitfield determining needed headers
1333 * @data - buffer containing header data
1334 * @len - length of data buffer
1335 * @hdr_len - array of individual header lengths
1336 * @scrq_arr - descriptor array
1338 * Creates header and, if needed, header extension descriptors and
1339 * places them in a descriptor array, scrq_arr
1342 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1343 union sub_crq *scrq_arr)
1345 union sub_crq hdr_desc;
1351 while (tmp_len > 0) {
1352 cur = hdr_data + len - tmp_len;
1354 memset(&hdr_desc, 0, sizeof(hdr_desc));
1355 if (cur != hdr_data) {
1356 data = hdr_desc.hdr_ext.data;
1357 tmp = tmp_len > 29 ? 29 : tmp_len;
1358 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1359 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1360 hdr_desc.hdr_ext.len = tmp;
1362 data = hdr_desc.hdr.data;
1363 tmp = tmp_len > 24 ? 24 : tmp_len;
1364 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1365 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1366 hdr_desc.hdr.len = tmp;
1367 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1368 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1369 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1370 hdr_desc.hdr.flag = hdr_field << 1;
1372 memcpy(data, cur, tmp);
1374 *scrq_arr = hdr_desc;
1383 * build_hdr_descs_arr - build a header descriptor array
1384 * @skb - socket buffer
1385 * @num_entries - number of descriptors to be sent
1386 * @subcrq - first TX descriptor
1387 * @hdr_field - bit field determining which headers will be sent
1389 * This function will build a TX descriptor array with applicable
1390 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1393 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1394 int *num_entries, u8 hdr_field)
1396 int hdr_len[3] = {0, 0, 0};
1398 u8 *hdr_data = txbuff->hdr_data;
1400 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1402 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1403 txbuff->indir_arr + 1);
1406 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1407 struct net_device *netdev)
1409 /* For some backing devices, mishandling of small packets
1410 * can result in a loss of connection or TX stall. Device
1411 * architects recommend that no packet should be smaller
1412 * than the minimum MTU value provided to the driver, so
1413 * pad any packets to that length
1415 if (skb->len < netdev->min_mtu)
1416 return skb_put_padto(skb, netdev->min_mtu);
1421 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1423 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1424 int queue_num = skb_get_queue_mapping(skb);
1425 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1426 struct device *dev = &adapter->vdev->dev;
1427 struct ibmvnic_tx_buff *tx_buff = NULL;
1428 struct ibmvnic_sub_crq_queue *tx_scrq;
1429 struct ibmvnic_tx_pool *tx_pool;
1430 unsigned int tx_send_failed = 0;
1431 unsigned int tx_map_failed = 0;
1432 unsigned int tx_dropped = 0;
1433 unsigned int tx_packets = 0;
1434 unsigned int tx_bytes = 0;
1435 dma_addr_t data_dma_addr;
1436 struct netdev_queue *txq;
1437 unsigned long lpar_rc;
1438 union sub_crq tx_crq;
1439 unsigned int offset;
1440 int num_entries = 1;
1447 if (adapter->resetting) {
1448 if (!netif_subqueue_stopped(netdev, skb))
1449 netif_stop_subqueue(netdev, queue_num);
1450 dev_kfree_skb_any(skb);
1458 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1464 if (skb_is_gso(skb))
1465 tx_pool = &adapter->tso_pool[queue_num];
1467 tx_pool = &adapter->tx_pool[queue_num];
1469 tx_scrq = adapter->tx_scrq[queue_num];
1470 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1471 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1472 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1474 index = tx_pool->free_map[tx_pool->consumer_index];
1476 if (index == IBMVNIC_INVALID_MAP) {
1477 dev_kfree_skb_any(skb);
1484 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1486 offset = index * tx_pool->buf_size;
1487 dst = tx_pool->long_term_buff.buff + offset;
1488 memset(dst, 0, tx_pool->buf_size);
1489 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1491 if (skb_shinfo(skb)->nr_frags) {
1495 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1496 cur = skb_headlen(skb);
1498 /* Copy the frags */
1499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1500 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1503 page_address(skb_frag_page(frag)) +
1504 frag->page_offset, skb_frag_size(frag));
1505 cur += skb_frag_size(frag);
1508 skb_copy_from_linear_data(skb, dst, skb->len);
1511 tx_pool->consumer_index =
1512 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1514 tx_buff = &tx_pool->tx_buff[index];
1516 tx_buff->data_dma[0] = data_dma_addr;
1517 tx_buff->data_len[0] = skb->len;
1518 tx_buff->index = index;
1519 tx_buff->pool_index = queue_num;
1520 tx_buff->last_frag = true;
1522 memset(&tx_crq, 0, sizeof(tx_crq));
1523 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1524 tx_crq.v1.type = IBMVNIC_TX_DESC;
1525 tx_crq.v1.n_crq_elem = 1;
1526 tx_crq.v1.n_sge = 1;
1527 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1529 if (skb_is_gso(skb))
1530 tx_crq.v1.correlator =
1531 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1533 tx_crq.v1.correlator = cpu_to_be32(index);
1534 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1535 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1536 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1538 if (adapter->vlan_header_insertion) {
1539 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1540 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1543 if (skb->protocol == htons(ETH_P_IP)) {
1544 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1545 proto = ip_hdr(skb)->protocol;
1546 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1547 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1548 proto = ipv6_hdr(skb)->nexthdr;
1551 if (proto == IPPROTO_TCP)
1552 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1553 else if (proto == IPPROTO_UDP)
1554 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1556 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1557 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1560 if (skb_is_gso(skb)) {
1561 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1562 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1565 /* determine if l2/3/4 headers are sent to firmware */
1566 if ((*hdrs >> 7) & 1) {
1567 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1568 tx_crq.v1.n_crq_elem = num_entries;
1569 tx_buff->num_entries = num_entries;
1570 tx_buff->indir_arr[0] = tx_crq;
1571 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1572 sizeof(tx_buff->indir_arr),
1574 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1575 dev_kfree_skb_any(skb);
1576 tx_buff->skb = NULL;
1577 if (!firmware_has_feature(FW_FEATURE_CMO))
1578 dev_err(dev, "tx: unable to map descriptor array\n");
1584 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1585 (u64)tx_buff->indir_dma,
1588 tx_buff->num_entries = num_entries;
1589 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1592 if (lpar_rc != H_SUCCESS) {
1593 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1594 dev_kfree_skb_any(skb);
1595 tx_buff->skb = NULL;
1597 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1598 /* Disable TX and report carrier off if queue is closed
1599 * or pending failover.
1600 * Firmware guarantees that a signal will be sent to the
1601 * driver, triggering a reset or some other action.
1603 netif_tx_stop_all_queues(netdev);
1604 netif_carrier_off(netdev);
1613 if (atomic_add_return(num_entries, &tx_scrq->used)
1614 >= adapter->req_tx_entries_per_subcrq) {
1615 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1616 netif_stop_subqueue(netdev, queue_num);
1620 tx_bytes += skb->len;
1621 txq->trans_start = jiffies;
1626 /* roll back consumer index and map array*/
1627 if (tx_pool->consumer_index == 0)
1628 tx_pool->consumer_index =
1629 tx_pool->num_buffers - 1;
1631 tx_pool->consumer_index--;
1632 tx_pool->free_map[tx_pool->consumer_index] = index;
1634 netdev->stats.tx_dropped += tx_dropped;
1635 netdev->stats.tx_bytes += tx_bytes;
1636 netdev->stats.tx_packets += tx_packets;
1637 adapter->tx_send_failed += tx_send_failed;
1638 adapter->tx_map_failed += tx_map_failed;
1639 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1640 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1641 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1646 static void ibmvnic_set_multi(struct net_device *netdev)
1648 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1649 struct netdev_hw_addr *ha;
1650 union ibmvnic_crq crq;
1652 memset(&crq, 0, sizeof(crq));
1653 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1654 crq.request_capability.cmd = REQUEST_CAPABILITY;
1656 if (netdev->flags & IFF_PROMISC) {
1657 if (!adapter->promisc_supported)
1660 if (netdev->flags & IFF_ALLMULTI) {
1661 /* Accept all multicast */
1662 memset(&crq, 0, sizeof(crq));
1663 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1664 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1665 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1666 ibmvnic_send_crq(adapter, &crq);
1667 } else if (netdev_mc_empty(netdev)) {
1668 /* Reject all multicast */
1669 memset(&crq, 0, sizeof(crq));
1670 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1671 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1672 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1673 ibmvnic_send_crq(adapter, &crq);
1675 /* Accept one or more multicast(s) */
1676 netdev_for_each_mc_addr(ha, netdev) {
1677 memset(&crq, 0, sizeof(crq));
1678 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1679 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1680 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1681 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1683 ibmvnic_send_crq(adapter, &crq);
1689 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1691 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1692 struct sockaddr *addr = p;
1693 union ibmvnic_crq crq;
1695 if (!is_valid_ether_addr(addr->sa_data))
1696 return -EADDRNOTAVAIL;
1698 memset(&crq, 0, sizeof(crq));
1699 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1700 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1701 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1703 init_completion(&adapter->fw_done);
1704 ibmvnic_send_crq(adapter, &crq);
1705 wait_for_completion(&adapter->fw_done);
1706 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1707 return adapter->fw_done_rc ? -EIO : 0;
1710 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1712 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1713 struct sockaddr *addr = p;
1716 if (adapter->state == VNIC_PROBED) {
1717 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1718 adapter->mac_change_pending = true;
1722 rc = __ibmvnic_set_mac(netdev, addr);
1728 * do_reset returns zero if we are able to keep processing reset events, or
1729 * non-zero if we hit a fatal error and must halt.
1731 static int do_reset(struct ibmvnic_adapter *adapter,
1732 struct ibmvnic_rwi *rwi, u32 reset_state)
1734 u64 old_num_rx_queues, old_num_tx_queues;
1735 struct net_device *netdev = adapter->netdev;
1738 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1741 netif_carrier_off(netdev);
1742 adapter->reset_reason = rwi->reset_reason;
1744 old_num_rx_queues = adapter->req_rx_queues;
1745 old_num_tx_queues = adapter->req_tx_queues;
1747 ibmvnic_cleanup(netdev);
1749 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1750 adapter->reset_reason != VNIC_RESET_FAILOVER) {
1751 rc = __ibmvnic_close(netdev);
1756 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1757 adapter->wait_for_reset) {
1758 release_resources(adapter);
1759 release_sub_crqs(adapter, 1);
1760 release_crq_queue(adapter);
1763 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1764 /* remove the closed state so when we call open it appears
1765 * we are coming from the probed state.
1767 adapter->state = VNIC_PROBED;
1769 if (adapter->wait_for_reset) {
1770 rc = init_crq_queue(adapter);
1771 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1772 rc = ibmvnic_reenable_crq_queue(adapter);
1773 release_sub_crqs(adapter, 1);
1775 rc = ibmvnic_reset_crq(adapter);
1777 rc = vio_enable_interrupts(adapter->vdev);
1781 netdev_err(adapter->netdev,
1782 "Couldn't initialize crq. rc=%d\n", rc);
1786 rc = ibmvnic_init(adapter);
1788 return IBMVNIC_INIT_FAILED;
1790 /* If the adapter was in PROBE state prior to the reset,
1793 if (reset_state == VNIC_PROBED)
1796 rc = ibmvnic_login(netdev);
1798 adapter->state = VNIC_PROBED;
1802 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1803 adapter->wait_for_reset) {
1804 rc = init_resources(adapter);
1807 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1808 adapter->req_tx_queues != old_num_tx_queues) {
1809 adapter->map_id = 1;
1810 release_rx_pools(adapter);
1811 release_tx_pools(adapter);
1812 init_rx_pools(netdev);
1813 init_tx_pools(netdev);
1815 release_napi(adapter);
1818 rc = reset_tx_pools(adapter);
1822 rc = reset_rx_pools(adapter);
1826 ibmvnic_disable_irqs(adapter);
1828 adapter->state = VNIC_CLOSED;
1830 if (reset_state == VNIC_CLOSED)
1833 rc = __ibmvnic_open(netdev);
1835 if (list_empty(&adapter->rwi_list))
1836 adapter->state = VNIC_CLOSED;
1838 adapter->state = reset_state;
1844 for (i = 0; i < adapter->req_rx_queues; i++)
1845 napi_schedule(&adapter->napi[i]);
1847 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1848 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1849 netdev_notify_peers(netdev);
1851 netif_carrier_on(netdev);
1856 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1858 struct ibmvnic_rwi *rwi;
1860 mutex_lock(&adapter->rwi_lock);
1862 if (!list_empty(&adapter->rwi_list)) {
1863 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1865 list_del(&rwi->list);
1870 mutex_unlock(&adapter->rwi_lock);
1874 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1876 struct ibmvnic_rwi *rwi;
1878 rwi = get_next_rwi(adapter);
1881 rwi = get_next_rwi(adapter);
1885 static void __ibmvnic_reset(struct work_struct *work)
1887 struct ibmvnic_rwi *rwi;
1888 struct ibmvnic_adapter *adapter;
1889 struct net_device *netdev;
1893 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1894 netdev = adapter->netdev;
1896 mutex_lock(&adapter->reset_lock);
1897 adapter->resetting = true;
1898 reset_state = adapter->state;
1900 rwi = get_next_rwi(adapter);
1902 rc = do_reset(adapter, rwi, reset_state);
1904 if (rc && rc != IBMVNIC_INIT_FAILED)
1907 rwi = get_next_rwi(adapter);
1910 if (adapter->wait_for_reset) {
1911 adapter->wait_for_reset = false;
1912 adapter->reset_done_rc = rc;
1913 complete(&adapter->reset_done);
1917 netdev_dbg(adapter->netdev, "Reset failed\n");
1918 free_all_rwi(adapter);
1919 mutex_unlock(&adapter->reset_lock);
1923 adapter->resetting = false;
1924 mutex_unlock(&adapter->reset_lock);
1927 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
1928 enum ibmvnic_reset_reason reason)
1930 struct ibmvnic_rwi *rwi, *tmp;
1931 struct net_device *netdev = adapter->netdev;
1932 struct list_head *entry;
1935 if (adapter->state == VNIC_REMOVING ||
1936 adapter->state == VNIC_REMOVED ||
1937 adapter->failover_pending) {
1939 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
1943 if (adapter->state == VNIC_PROBING) {
1944 netdev_warn(netdev, "Adapter reset during probe\n");
1945 ret = adapter->init_done_rc = EAGAIN;
1949 mutex_lock(&adapter->rwi_lock);
1951 list_for_each(entry, &adapter->rwi_list) {
1952 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1953 if (tmp->reset_reason == reason) {
1954 netdev_dbg(netdev, "Skipping matching reset\n");
1955 mutex_unlock(&adapter->rwi_lock);
1961 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1963 mutex_unlock(&adapter->rwi_lock);
1964 ibmvnic_close(netdev);
1969 rwi->reset_reason = reason;
1970 list_add_tail(&rwi->list, &adapter->rwi_list);
1971 mutex_unlock(&adapter->rwi_lock);
1973 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1974 schedule_work(&adapter->ibmvnic_reset);
1978 if (adapter->wait_for_reset)
1979 adapter->wait_for_reset = false;
1983 static void ibmvnic_tx_timeout(struct net_device *dev)
1985 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1987 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1990 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1991 struct ibmvnic_rx_buff *rx_buff)
1993 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1995 rx_buff->skb = NULL;
1997 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1998 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2000 atomic_dec(&pool->available);
2003 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2005 struct net_device *netdev = napi->dev;
2006 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2007 int scrq_num = (int)(napi - adapter->napi);
2008 int frames_processed = 0;
2011 while (frames_processed < budget) {
2012 struct sk_buff *skb;
2013 struct ibmvnic_rx_buff *rx_buff;
2014 union sub_crq *next;
2019 if (unlikely(adapter->resetting &&
2020 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2021 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2022 napi_complete_done(napi, frames_processed);
2023 return frames_processed;
2026 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2028 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2030 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2031 rx_comp.correlator);
2032 /* do error checking */
2033 if (next->rx_comp.rc) {
2034 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2035 be16_to_cpu(next->rx_comp.rc));
2036 /* free the entry */
2037 next->rx_comp.first = 0;
2038 dev_kfree_skb_any(rx_buff->skb);
2039 remove_buff_from_pool(adapter, rx_buff);
2041 } else if (!rx_buff->skb) {
2042 /* free the entry */
2043 next->rx_comp.first = 0;
2044 remove_buff_from_pool(adapter, rx_buff);
2048 length = be32_to_cpu(next->rx_comp.len);
2049 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2050 flags = next->rx_comp.flags;
2052 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2055 /* VLAN Header has been stripped by the system firmware and
2056 * needs to be inserted by the driver
2058 if (adapter->rx_vlan_header_insertion &&
2059 (flags & IBMVNIC_VLAN_STRIPPED))
2060 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2061 ntohs(next->rx_comp.vlan_tci));
2063 /* free the entry */
2064 next->rx_comp.first = 0;
2065 remove_buff_from_pool(adapter, rx_buff);
2067 skb_put(skb, length);
2068 skb->protocol = eth_type_trans(skb, netdev);
2069 skb_record_rx_queue(skb, scrq_num);
2071 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2072 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2073 skb->ip_summed = CHECKSUM_UNNECESSARY;
2077 napi_gro_receive(napi, skb); /* send it up */
2078 netdev->stats.rx_packets++;
2079 netdev->stats.rx_bytes += length;
2080 adapter->rx_stats_buffers[scrq_num].packets++;
2081 adapter->rx_stats_buffers[scrq_num].bytes += length;
2085 if (adapter->state != VNIC_CLOSING)
2086 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2088 if (frames_processed < budget) {
2089 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2090 napi_complete_done(napi, frames_processed);
2091 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2092 napi_reschedule(napi)) {
2093 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2097 return frames_processed;
2100 #ifdef CONFIG_NET_POLL_CONTROLLER
2101 static void ibmvnic_netpoll_controller(struct net_device *dev)
2103 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2106 replenish_pools(netdev_priv(dev));
2107 for (i = 0; i < adapter->req_rx_queues; i++)
2108 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2109 adapter->rx_scrq[i]);
2113 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2117 adapter->fallback.mtu = adapter->req_mtu;
2118 adapter->fallback.rx_queues = adapter->req_rx_queues;
2119 adapter->fallback.tx_queues = adapter->req_tx_queues;
2120 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2121 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2123 init_completion(&adapter->reset_done);
2124 adapter->wait_for_reset = true;
2125 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2128 wait_for_completion(&adapter->reset_done);
2131 if (adapter->reset_done_rc) {
2133 adapter->desired.mtu = adapter->fallback.mtu;
2134 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2135 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2136 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2137 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2139 init_completion(&adapter->reset_done);
2140 adapter->wait_for_reset = true;
2141 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2144 wait_for_completion(&adapter->reset_done);
2146 adapter->wait_for_reset = false;
2151 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2153 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2155 adapter->desired.mtu = new_mtu + ETH_HLEN;
2157 return wait_for_reset(adapter);
2160 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2161 struct net_device *dev,
2162 netdev_features_t features)
2164 /* Some backing hardware adapters can not
2165 * handle packets with a MSS less than 224
2166 * or with only one segment.
2168 if (skb_is_gso(skb)) {
2169 if (skb_shinfo(skb)->gso_size < 224 ||
2170 skb_shinfo(skb)->gso_segs == 1)
2171 features &= ~NETIF_F_GSO_MASK;
2177 static const struct net_device_ops ibmvnic_netdev_ops = {
2178 .ndo_open = ibmvnic_open,
2179 .ndo_stop = ibmvnic_close,
2180 .ndo_start_xmit = ibmvnic_xmit,
2181 .ndo_set_rx_mode = ibmvnic_set_multi,
2182 .ndo_set_mac_address = ibmvnic_set_mac,
2183 .ndo_validate_addr = eth_validate_addr,
2184 .ndo_tx_timeout = ibmvnic_tx_timeout,
2185 #ifdef CONFIG_NET_POLL_CONTROLLER
2186 .ndo_poll_controller = ibmvnic_netpoll_controller,
2188 .ndo_change_mtu = ibmvnic_change_mtu,
2189 .ndo_features_check = ibmvnic_features_check,
2192 /* ethtool functions */
2194 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2195 struct ethtool_link_ksettings *cmd)
2197 u32 supported, advertising;
2199 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2201 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2203 cmd->base.speed = SPEED_1000;
2204 cmd->base.duplex = DUPLEX_FULL;
2205 cmd->base.port = PORT_FIBRE;
2206 cmd->base.phy_address = 0;
2207 cmd->base.autoneg = AUTONEG_ENABLE;
2209 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2211 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2217 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2218 struct ethtool_drvinfo *info)
2220 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2222 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2223 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2224 strlcpy(info->fw_version, adapter->fw_version,
2225 sizeof(info->fw_version));
2228 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2230 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2232 return adapter->msg_enable;
2235 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2237 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2239 adapter->msg_enable = data;
2242 static u32 ibmvnic_get_link(struct net_device *netdev)
2244 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2246 /* Don't need to send a query because we request a logical link up at
2247 * init and then we wait for link state indications
2249 return adapter->logical_link_state;
2252 static void ibmvnic_get_ringparam(struct net_device *netdev,
2253 struct ethtool_ringparam *ring)
2255 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2257 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2258 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2259 ring->rx_mini_max_pending = 0;
2260 ring->rx_jumbo_max_pending = 0;
2261 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2262 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2263 ring->rx_mini_pending = 0;
2264 ring->rx_jumbo_pending = 0;
2267 static int ibmvnic_set_ringparam(struct net_device *netdev,
2268 struct ethtool_ringparam *ring)
2270 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2272 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2273 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2274 netdev_err(netdev, "Invalid request.\n");
2275 netdev_err(netdev, "Max tx buffers = %llu\n",
2276 adapter->max_rx_add_entries_per_subcrq);
2277 netdev_err(netdev, "Max rx buffers = %llu\n",
2278 adapter->max_tx_entries_per_subcrq);
2282 adapter->desired.rx_entries = ring->rx_pending;
2283 adapter->desired.tx_entries = ring->tx_pending;
2285 return wait_for_reset(adapter);
2288 static void ibmvnic_get_channels(struct net_device *netdev,
2289 struct ethtool_channels *channels)
2291 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2293 channels->max_rx = adapter->max_rx_queues;
2294 channels->max_tx = adapter->max_tx_queues;
2295 channels->max_other = 0;
2296 channels->max_combined = 0;
2297 channels->rx_count = adapter->req_rx_queues;
2298 channels->tx_count = adapter->req_tx_queues;
2299 channels->other_count = 0;
2300 channels->combined_count = 0;
2303 static int ibmvnic_set_channels(struct net_device *netdev,
2304 struct ethtool_channels *channels)
2306 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2308 adapter->desired.rx_queues = channels->rx_count;
2309 adapter->desired.tx_queues = channels->tx_count;
2311 return wait_for_reset(adapter);
2314 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2316 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2319 if (stringset != ETH_SS_STATS)
2322 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2323 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2325 for (i = 0; i < adapter->req_tx_queues; i++) {
2326 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2327 data += ETH_GSTRING_LEN;
2329 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2330 data += ETH_GSTRING_LEN;
2332 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2333 data += ETH_GSTRING_LEN;
2336 for (i = 0; i < adapter->req_rx_queues; i++) {
2337 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2338 data += ETH_GSTRING_LEN;
2340 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2341 data += ETH_GSTRING_LEN;
2343 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2344 data += ETH_GSTRING_LEN;
2348 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2350 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2354 return ARRAY_SIZE(ibmvnic_stats) +
2355 adapter->req_tx_queues * NUM_TX_STATS +
2356 adapter->req_rx_queues * NUM_RX_STATS;
2362 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2363 struct ethtool_stats *stats, u64 *data)
2365 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2366 union ibmvnic_crq crq;
2369 memset(&crq, 0, sizeof(crq));
2370 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2371 crq.request_statistics.cmd = REQUEST_STATISTICS;
2372 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2373 crq.request_statistics.len =
2374 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2376 /* Wait for data to be written */
2377 init_completion(&adapter->stats_done);
2378 ibmvnic_send_crq(adapter, &crq);
2379 wait_for_completion(&adapter->stats_done);
2381 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2382 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2383 ibmvnic_stats[i].offset));
2385 for (j = 0; j < adapter->req_tx_queues; j++) {
2386 data[i] = adapter->tx_stats_buffers[j].packets;
2388 data[i] = adapter->tx_stats_buffers[j].bytes;
2390 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2394 for (j = 0; j < adapter->req_rx_queues; j++) {
2395 data[i] = adapter->rx_stats_buffers[j].packets;
2397 data[i] = adapter->rx_stats_buffers[j].bytes;
2399 data[i] = adapter->rx_stats_buffers[j].interrupts;
2404 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2405 .get_drvinfo = ibmvnic_get_drvinfo,
2406 .get_msglevel = ibmvnic_get_msglevel,
2407 .set_msglevel = ibmvnic_set_msglevel,
2408 .get_link = ibmvnic_get_link,
2409 .get_ringparam = ibmvnic_get_ringparam,
2410 .set_ringparam = ibmvnic_set_ringparam,
2411 .get_channels = ibmvnic_get_channels,
2412 .set_channels = ibmvnic_set_channels,
2413 .get_strings = ibmvnic_get_strings,
2414 .get_sset_count = ibmvnic_get_sset_count,
2415 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2416 .get_link_ksettings = ibmvnic_get_link_ksettings,
2419 /* Routines for managing CRQs/sCRQs */
2421 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2422 struct ibmvnic_sub_crq_queue *scrq)
2427 free_irq(scrq->irq, scrq);
2428 irq_dispose_mapping(scrq->irq);
2432 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2433 atomic_set(&scrq->used, 0);
2436 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2437 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2441 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2445 for (i = 0; i < adapter->req_tx_queues; i++) {
2446 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2447 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2452 for (i = 0; i < adapter->req_rx_queues; i++) {
2453 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2454 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2462 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2463 struct ibmvnic_sub_crq_queue *scrq,
2466 struct device *dev = &adapter->vdev->dev;
2469 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2472 /* Close the sub-crqs */
2474 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2475 adapter->vdev->unit_address,
2477 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2480 netdev_err(adapter->netdev,
2481 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2486 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2488 free_pages((unsigned long)scrq->msgs, 2);
2492 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2495 struct device *dev = &adapter->vdev->dev;
2496 struct ibmvnic_sub_crq_queue *scrq;
2499 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2504 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2506 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2507 goto zero_page_failed;
2510 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2512 if (dma_mapping_error(dev, scrq->msg_token)) {
2513 dev_warn(dev, "Couldn't map crq queue messages page\n");
2517 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2518 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2520 if (rc == H_RESOURCE)
2521 rc = ibmvnic_reset_crq(adapter);
2523 if (rc == H_CLOSED) {
2524 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2526 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2530 scrq->adapter = adapter;
2531 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2532 spin_lock_init(&scrq->lock);
2534 netdev_dbg(adapter->netdev,
2535 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2536 scrq->crq_num, scrq->hw_irq, scrq->irq);
2541 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2544 free_pages((unsigned long)scrq->msgs, 2);
2551 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2555 if (adapter->tx_scrq) {
2556 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2557 if (!adapter->tx_scrq[i])
2560 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2562 if (adapter->tx_scrq[i]->irq) {
2563 free_irq(adapter->tx_scrq[i]->irq,
2564 adapter->tx_scrq[i]);
2565 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2566 adapter->tx_scrq[i]->irq = 0;
2569 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2573 kfree(adapter->tx_scrq);
2574 adapter->tx_scrq = NULL;
2575 adapter->num_active_tx_scrqs = 0;
2578 if (adapter->rx_scrq) {
2579 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2580 if (!adapter->rx_scrq[i])
2583 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2585 if (adapter->rx_scrq[i]->irq) {
2586 free_irq(adapter->rx_scrq[i]->irq,
2587 adapter->rx_scrq[i]);
2588 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2589 adapter->rx_scrq[i]->irq = 0;
2592 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2596 kfree(adapter->rx_scrq);
2597 adapter->rx_scrq = NULL;
2598 adapter->num_active_rx_scrqs = 0;
2602 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2603 struct ibmvnic_sub_crq_queue *scrq)
2605 struct device *dev = &adapter->vdev->dev;
2608 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2609 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2611 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2616 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2617 struct ibmvnic_sub_crq_queue *scrq)
2619 struct device *dev = &adapter->vdev->dev;
2623 if (scrq->hw_irq > 0x100000000ULL) {
2624 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2628 val = (0xff000000) | scrq->hw_irq;
2629 rc = plpar_hcall_norets(H_EOI, val);
2631 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2634 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2635 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2637 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2642 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2643 struct ibmvnic_sub_crq_queue *scrq)
2645 struct device *dev = &adapter->vdev->dev;
2646 struct ibmvnic_tx_pool *tx_pool;
2647 struct ibmvnic_tx_buff *txbuff;
2648 union sub_crq *next;
2654 while (pending_scrq(adapter, scrq)) {
2655 unsigned int pool = scrq->pool_index;
2656 int num_entries = 0;
2658 next = ibmvnic_next_scrq(adapter, scrq);
2659 for (i = 0; i < next->tx_comp.num_comps; i++) {
2660 if (next->tx_comp.rcs[i]) {
2661 dev_err(dev, "tx error %x\n",
2662 next->tx_comp.rcs[i]);
2665 index = be32_to_cpu(next->tx_comp.correlators[i]);
2666 if (index & IBMVNIC_TSO_POOL_MASK) {
2667 tx_pool = &adapter->tso_pool[pool];
2668 index &= ~IBMVNIC_TSO_POOL_MASK;
2670 tx_pool = &adapter->tx_pool[pool];
2673 txbuff = &tx_pool->tx_buff[index];
2675 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2676 if (!txbuff->data_dma[j])
2679 txbuff->data_dma[j] = 0;
2681 /* if sub_crq was sent indirectly */
2682 first = &txbuff->indir_arr[0].generic.first;
2683 if (*first == IBMVNIC_CRQ_CMD) {
2684 dma_unmap_single(dev, txbuff->indir_dma,
2685 sizeof(txbuff->indir_arr),
2690 if (txbuff->last_frag) {
2691 dev_kfree_skb_any(txbuff->skb);
2695 num_entries += txbuff->num_entries;
2697 tx_pool->free_map[tx_pool->producer_index] = index;
2698 tx_pool->producer_index =
2699 (tx_pool->producer_index + 1) %
2700 tx_pool->num_buffers;
2702 /* remove tx_comp scrq*/
2703 next->tx_comp.first = 0;
2705 if (atomic_sub_return(num_entries, &scrq->used) <=
2706 (adapter->req_tx_entries_per_subcrq / 2) &&
2707 __netif_subqueue_stopped(adapter->netdev,
2708 scrq->pool_index)) {
2709 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2710 netdev_dbg(adapter->netdev, "Started queue %d\n",
2715 enable_scrq_irq(adapter, scrq);
2717 if (pending_scrq(adapter, scrq)) {
2718 disable_scrq_irq(adapter, scrq);
2725 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2727 struct ibmvnic_sub_crq_queue *scrq = instance;
2728 struct ibmvnic_adapter *adapter = scrq->adapter;
2730 disable_scrq_irq(adapter, scrq);
2731 ibmvnic_complete_tx(adapter, scrq);
2736 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2738 struct ibmvnic_sub_crq_queue *scrq = instance;
2739 struct ibmvnic_adapter *adapter = scrq->adapter;
2741 /* When booting a kdump kernel we can hit pending interrupts
2742 * prior to completing driver initialization.
2744 if (unlikely(adapter->state != VNIC_OPEN))
2747 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2749 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2750 disable_scrq_irq(adapter, scrq);
2751 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2757 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2759 struct device *dev = &adapter->vdev->dev;
2760 struct ibmvnic_sub_crq_queue *scrq;
2764 for (i = 0; i < adapter->req_tx_queues; i++) {
2765 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2767 scrq = adapter->tx_scrq[i];
2768 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2772 dev_err(dev, "Error mapping irq\n");
2773 goto req_tx_irq_failed;
2776 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2777 0, "ibmvnic_tx", scrq);
2780 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2782 irq_dispose_mapping(scrq->irq);
2783 goto req_tx_irq_failed;
2787 for (i = 0; i < adapter->req_rx_queues; i++) {
2788 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2790 scrq = adapter->rx_scrq[i];
2791 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2794 dev_err(dev, "Error mapping irq\n");
2795 goto req_rx_irq_failed;
2797 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2798 0, "ibmvnic_rx", scrq);
2800 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2802 irq_dispose_mapping(scrq->irq);
2803 goto req_rx_irq_failed;
2809 for (j = 0; j < i; j++) {
2810 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2811 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2813 i = adapter->req_tx_queues;
2815 for (j = 0; j < i; j++) {
2816 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2817 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2819 release_sub_crqs(adapter, 1);
2823 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2825 struct device *dev = &adapter->vdev->dev;
2826 struct ibmvnic_sub_crq_queue **allqueues;
2827 int registered_queues = 0;
2832 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2834 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2838 for (i = 0; i < total_queues; i++) {
2839 allqueues[i] = init_sub_crq_queue(adapter);
2840 if (!allqueues[i]) {
2841 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2844 registered_queues++;
2847 /* Make sure we were able to register the minimum number of queues */
2848 if (registered_queues <
2849 adapter->min_tx_queues + adapter->min_rx_queues) {
2850 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2854 /* Distribute the failed allocated queues*/
2855 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2856 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2859 if (adapter->req_rx_queues > adapter->min_rx_queues)
2860 adapter->req_rx_queues--;
2865 if (adapter->req_tx_queues > adapter->min_tx_queues)
2866 adapter->req_tx_queues--;
2873 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2874 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2875 if (!adapter->tx_scrq)
2878 for (i = 0; i < adapter->req_tx_queues; i++) {
2879 adapter->tx_scrq[i] = allqueues[i];
2880 adapter->tx_scrq[i]->pool_index = i;
2881 adapter->num_active_tx_scrqs++;
2884 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2885 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2886 if (!adapter->rx_scrq)
2889 for (i = 0; i < adapter->req_rx_queues; i++) {
2890 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2891 adapter->rx_scrq[i]->scrq_num = i;
2892 adapter->num_active_rx_scrqs++;
2899 kfree(adapter->tx_scrq);
2900 adapter->tx_scrq = NULL;
2902 for (i = 0; i < registered_queues; i++)
2903 release_sub_crq_queue(adapter, allqueues[i], 1);
2908 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2910 struct device *dev = &adapter->vdev->dev;
2911 union ibmvnic_crq crq;
2915 /* Sub-CRQ entries are 32 byte long */
2916 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2918 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2919 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2920 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2924 if (adapter->desired.mtu)
2925 adapter->req_mtu = adapter->desired.mtu;
2927 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2929 if (!adapter->desired.tx_entries)
2930 adapter->desired.tx_entries =
2931 adapter->max_tx_entries_per_subcrq;
2932 if (!adapter->desired.rx_entries)
2933 adapter->desired.rx_entries =
2934 adapter->max_rx_add_entries_per_subcrq;
2936 max_entries = IBMVNIC_MAX_LTB_SIZE /
2937 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2939 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2940 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2941 adapter->desired.tx_entries = max_entries;
2944 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2945 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2946 adapter->desired.rx_entries = max_entries;
2949 if (adapter->desired.tx_entries)
2950 adapter->req_tx_entries_per_subcrq =
2951 adapter->desired.tx_entries;
2953 adapter->req_tx_entries_per_subcrq =
2954 adapter->max_tx_entries_per_subcrq;
2956 if (adapter->desired.rx_entries)
2957 adapter->req_rx_add_entries_per_subcrq =
2958 adapter->desired.rx_entries;
2960 adapter->req_rx_add_entries_per_subcrq =
2961 adapter->max_rx_add_entries_per_subcrq;
2963 if (adapter->desired.tx_queues)
2964 adapter->req_tx_queues =
2965 adapter->desired.tx_queues;
2967 adapter->req_tx_queues =
2968 adapter->opt_tx_comp_sub_queues;
2970 if (adapter->desired.rx_queues)
2971 adapter->req_rx_queues =
2972 adapter->desired.rx_queues;
2974 adapter->req_rx_queues =
2975 adapter->opt_rx_comp_queues;
2977 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2980 memset(&crq, 0, sizeof(crq));
2981 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2982 crq.request_capability.cmd = REQUEST_CAPABILITY;
2984 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2985 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2986 atomic_inc(&adapter->running_cap_crqs);
2987 ibmvnic_send_crq(adapter, &crq);
2989 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2990 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2991 atomic_inc(&adapter->running_cap_crqs);
2992 ibmvnic_send_crq(adapter, &crq);
2994 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2995 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2996 atomic_inc(&adapter->running_cap_crqs);
2997 ibmvnic_send_crq(adapter, &crq);
2999 crq.request_capability.capability =
3000 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3001 crq.request_capability.number =
3002 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3003 atomic_inc(&adapter->running_cap_crqs);
3004 ibmvnic_send_crq(adapter, &crq);
3006 crq.request_capability.capability =
3007 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3008 crq.request_capability.number =
3009 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3010 atomic_inc(&adapter->running_cap_crqs);
3011 ibmvnic_send_crq(adapter, &crq);
3013 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3014 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3015 atomic_inc(&adapter->running_cap_crqs);
3016 ibmvnic_send_crq(adapter, &crq);
3018 if (adapter->netdev->flags & IFF_PROMISC) {
3019 if (adapter->promisc_supported) {
3020 crq.request_capability.capability =
3021 cpu_to_be16(PROMISC_REQUESTED);
3022 crq.request_capability.number = cpu_to_be64(1);
3023 atomic_inc(&adapter->running_cap_crqs);
3024 ibmvnic_send_crq(adapter, &crq);
3027 crq.request_capability.capability =
3028 cpu_to_be16(PROMISC_REQUESTED);
3029 crq.request_capability.number = cpu_to_be64(0);
3030 atomic_inc(&adapter->running_cap_crqs);
3031 ibmvnic_send_crq(adapter, &crq);
3035 static int pending_scrq(struct ibmvnic_adapter *adapter,
3036 struct ibmvnic_sub_crq_queue *scrq)
3038 union sub_crq *entry = &scrq->msgs[scrq->cur];
3040 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3046 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3047 struct ibmvnic_sub_crq_queue *scrq)
3049 union sub_crq *entry;
3050 unsigned long flags;
3052 spin_lock_irqsave(&scrq->lock, flags);
3053 entry = &scrq->msgs[scrq->cur];
3054 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3055 if (++scrq->cur == scrq->size)
3060 spin_unlock_irqrestore(&scrq->lock, flags);
3065 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3067 struct ibmvnic_crq_queue *queue = &adapter->crq;
3068 union ibmvnic_crq *crq;
3070 crq = &queue->msgs[queue->cur];
3071 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3072 if (++queue->cur == queue->size)
3081 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3082 union sub_crq *sub_crq)
3084 unsigned int ua = adapter->vdev->unit_address;
3085 struct device *dev = &adapter->vdev->dev;
3086 u64 *u64_crq = (u64 *)sub_crq;
3089 netdev_dbg(adapter->netdev,
3090 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3091 (unsigned long int)cpu_to_be64(remote_handle),
3092 (unsigned long int)cpu_to_be64(u64_crq[0]),
3093 (unsigned long int)cpu_to_be64(u64_crq[1]),
3094 (unsigned long int)cpu_to_be64(u64_crq[2]),
3095 (unsigned long int)cpu_to_be64(u64_crq[3]));
3097 /* Make sure the hypervisor sees the complete request */
3100 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3101 cpu_to_be64(remote_handle),
3102 cpu_to_be64(u64_crq[0]),
3103 cpu_to_be64(u64_crq[1]),
3104 cpu_to_be64(u64_crq[2]),
3105 cpu_to_be64(u64_crq[3]));
3109 dev_warn(dev, "CRQ Queue closed\n");
3110 dev_err(dev, "Send error (rc=%d)\n", rc);
3116 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3117 u64 remote_handle, u64 ioba, u64 num_entries)
3119 unsigned int ua = adapter->vdev->unit_address;
3120 struct device *dev = &adapter->vdev->dev;
3123 /* Make sure the hypervisor sees the complete request */
3125 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3126 cpu_to_be64(remote_handle),
3131 dev_warn(dev, "CRQ Queue closed\n");
3132 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3138 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3139 union ibmvnic_crq *crq)
3141 unsigned int ua = adapter->vdev->unit_address;
3142 struct device *dev = &adapter->vdev->dev;
3143 u64 *u64_crq = (u64 *)crq;
3146 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3147 (unsigned long int)cpu_to_be64(u64_crq[0]),
3148 (unsigned long int)cpu_to_be64(u64_crq[1]));
3150 if (!adapter->crq.active &&
3151 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3152 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3156 /* Make sure the hypervisor sees the complete request */
3159 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3160 cpu_to_be64(u64_crq[0]),
3161 cpu_to_be64(u64_crq[1]));
3164 if (rc == H_CLOSED) {
3165 dev_warn(dev, "CRQ Queue closed\n");
3166 if (adapter->resetting)
3167 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3170 dev_warn(dev, "Send error (rc=%d)\n", rc);
3176 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3178 union ibmvnic_crq crq;
3180 memset(&crq, 0, sizeof(crq));
3181 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3182 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3183 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3185 return ibmvnic_send_crq(adapter, &crq);
3188 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3190 union ibmvnic_crq crq;
3192 memset(&crq, 0, sizeof(crq));
3193 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3194 crq.version_exchange.cmd = VERSION_EXCHANGE;
3195 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3197 return ibmvnic_send_crq(adapter, &crq);
3200 struct vnic_login_client_data {
3206 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3210 /* Calculate the amount of buffer space needed for the
3211 * vnic client data in the login buffer. There are four entries,
3212 * OS name, LPAR name, device name, and a null last entry.
3214 len = 4 * sizeof(struct vnic_login_client_data);
3215 len += 6; /* "Linux" plus NULL */
3216 len += strlen(utsname()->nodename) + 1;
3217 len += strlen(adapter->netdev->name) + 1;
3222 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3223 struct vnic_login_client_data *vlcd)
3225 const char *os_name = "Linux";
3228 /* Type 1 - LPAR OS */
3230 len = strlen(os_name) + 1;
3231 vlcd->len = cpu_to_be16(len);
3232 strncpy(vlcd->name, os_name, len);
3233 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3235 /* Type 2 - LPAR name */
3237 len = strlen(utsname()->nodename) + 1;
3238 vlcd->len = cpu_to_be16(len);
3239 strncpy(vlcd->name, utsname()->nodename, len);
3240 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3242 /* Type 3 - device name */
3244 len = strlen(adapter->netdev->name) + 1;
3245 vlcd->len = cpu_to_be16(len);
3246 strncpy(vlcd->name, adapter->netdev->name, len);
3249 static int send_login(struct ibmvnic_adapter *adapter)
3251 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3252 struct ibmvnic_login_buffer *login_buffer;
3253 struct device *dev = &adapter->vdev->dev;
3254 dma_addr_t rsp_buffer_token;
3255 dma_addr_t buffer_token;
3256 size_t rsp_buffer_size;
3257 union ibmvnic_crq crq;
3261 int client_data_len;
3262 struct vnic_login_client_data *vlcd;
3265 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3266 netdev_err(adapter->netdev,
3267 "RX or TX queues are not allocated, device login failed\n");
3271 release_login_rsp_buffer(adapter);
3272 client_data_len = vnic_client_data_len(adapter);
3275 sizeof(struct ibmvnic_login_buffer) +
3276 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3279 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3281 goto buf_alloc_failed;
3283 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3285 if (dma_mapping_error(dev, buffer_token)) {
3286 dev_err(dev, "Couldn't map login buffer\n");
3287 goto buf_map_failed;
3290 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3291 sizeof(u64) * adapter->req_tx_queues +
3292 sizeof(u64) * adapter->req_rx_queues +
3293 sizeof(u64) * adapter->req_rx_queues +
3294 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3296 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3297 if (!login_rsp_buffer)
3298 goto buf_rsp_alloc_failed;
3300 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3301 rsp_buffer_size, DMA_FROM_DEVICE);
3302 if (dma_mapping_error(dev, rsp_buffer_token)) {
3303 dev_err(dev, "Couldn't map login rsp buffer\n");
3304 goto buf_rsp_map_failed;
3307 adapter->login_buf = login_buffer;
3308 adapter->login_buf_token = buffer_token;
3309 adapter->login_buf_sz = buffer_size;
3310 adapter->login_rsp_buf = login_rsp_buffer;
3311 adapter->login_rsp_buf_token = rsp_buffer_token;
3312 adapter->login_rsp_buf_sz = rsp_buffer_size;
3314 login_buffer->len = cpu_to_be32(buffer_size);
3315 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3316 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3317 login_buffer->off_txcomp_subcrqs =
3318 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3319 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3320 login_buffer->off_rxcomp_subcrqs =
3321 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3322 sizeof(u64) * adapter->req_tx_queues);
3323 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3324 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3326 tx_list_p = (__be64 *)((char *)login_buffer +
3327 sizeof(struct ibmvnic_login_buffer));
3328 rx_list_p = (__be64 *)((char *)login_buffer +
3329 sizeof(struct ibmvnic_login_buffer) +
3330 sizeof(u64) * adapter->req_tx_queues);
3332 for (i = 0; i < adapter->req_tx_queues; i++) {
3333 if (adapter->tx_scrq[i]) {
3334 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3339 for (i = 0; i < adapter->req_rx_queues; i++) {
3340 if (adapter->rx_scrq[i]) {
3341 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3346 /* Insert vNIC login client data */
3347 vlcd = (struct vnic_login_client_data *)
3348 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3349 login_buffer->client_data_offset =
3350 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3351 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3353 vnic_add_client_data(adapter, vlcd);
3355 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3356 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3357 netdev_dbg(adapter->netdev, "%016lx\n",
3358 ((unsigned long int *)(adapter->login_buf))[i]);
3361 memset(&crq, 0, sizeof(crq));
3362 crq.login.first = IBMVNIC_CRQ_CMD;
3363 crq.login.cmd = LOGIN;
3364 crq.login.ioba = cpu_to_be32(buffer_token);
3365 crq.login.len = cpu_to_be32(buffer_size);
3366 ibmvnic_send_crq(adapter, &crq);
3371 kfree(login_rsp_buffer);
3372 buf_rsp_alloc_failed:
3373 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3375 kfree(login_buffer);
3380 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3383 union ibmvnic_crq crq;
3385 memset(&crq, 0, sizeof(crq));
3386 crq.request_map.first = IBMVNIC_CRQ_CMD;
3387 crq.request_map.cmd = REQUEST_MAP;
3388 crq.request_map.map_id = map_id;
3389 crq.request_map.ioba = cpu_to_be32(addr);
3390 crq.request_map.len = cpu_to_be32(len);
3391 ibmvnic_send_crq(adapter, &crq);
3394 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3396 union ibmvnic_crq crq;
3398 memset(&crq, 0, sizeof(crq));
3399 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3400 crq.request_unmap.cmd = REQUEST_UNMAP;
3401 crq.request_unmap.map_id = map_id;
3402 ibmvnic_send_crq(adapter, &crq);
3405 static void send_map_query(struct ibmvnic_adapter *adapter)
3407 union ibmvnic_crq crq;
3409 memset(&crq, 0, sizeof(crq));
3410 crq.query_map.first = IBMVNIC_CRQ_CMD;
3411 crq.query_map.cmd = QUERY_MAP;
3412 ibmvnic_send_crq(adapter, &crq);
3415 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3416 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3418 union ibmvnic_crq crq;
3420 atomic_set(&adapter->running_cap_crqs, 0);
3421 memset(&crq, 0, sizeof(crq));
3422 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3423 crq.query_capability.cmd = QUERY_CAPABILITY;
3425 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3426 atomic_inc(&adapter->running_cap_crqs);
3427 ibmvnic_send_crq(adapter, &crq);
3429 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3430 atomic_inc(&adapter->running_cap_crqs);
3431 ibmvnic_send_crq(adapter, &crq);
3433 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3434 atomic_inc(&adapter->running_cap_crqs);
3435 ibmvnic_send_crq(adapter, &crq);
3437 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3438 atomic_inc(&adapter->running_cap_crqs);
3439 ibmvnic_send_crq(adapter, &crq);
3441 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3442 atomic_inc(&adapter->running_cap_crqs);
3443 ibmvnic_send_crq(adapter, &crq);
3445 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3446 atomic_inc(&adapter->running_cap_crqs);
3447 ibmvnic_send_crq(adapter, &crq);
3449 crq.query_capability.capability =
3450 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3451 atomic_inc(&adapter->running_cap_crqs);
3452 ibmvnic_send_crq(adapter, &crq);
3454 crq.query_capability.capability =
3455 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3456 atomic_inc(&adapter->running_cap_crqs);
3457 ibmvnic_send_crq(adapter, &crq);
3459 crq.query_capability.capability =
3460 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3461 atomic_inc(&adapter->running_cap_crqs);
3462 ibmvnic_send_crq(adapter, &crq);
3464 crq.query_capability.capability =
3465 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3466 atomic_inc(&adapter->running_cap_crqs);
3467 ibmvnic_send_crq(adapter, &crq);
3469 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3470 atomic_inc(&adapter->running_cap_crqs);
3471 ibmvnic_send_crq(adapter, &crq);
3473 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3474 atomic_inc(&adapter->running_cap_crqs);
3475 ibmvnic_send_crq(adapter, &crq);
3477 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3478 atomic_inc(&adapter->running_cap_crqs);
3479 ibmvnic_send_crq(adapter, &crq);
3481 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3482 atomic_inc(&adapter->running_cap_crqs);
3483 ibmvnic_send_crq(adapter, &crq);
3485 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3486 atomic_inc(&adapter->running_cap_crqs);
3487 ibmvnic_send_crq(adapter, &crq);
3489 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3490 atomic_inc(&adapter->running_cap_crqs);
3491 ibmvnic_send_crq(adapter, &crq);
3493 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3494 atomic_inc(&adapter->running_cap_crqs);
3495 ibmvnic_send_crq(adapter, &crq);
3497 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3498 atomic_inc(&adapter->running_cap_crqs);
3499 ibmvnic_send_crq(adapter, &crq);
3501 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3502 atomic_inc(&adapter->running_cap_crqs);
3503 ibmvnic_send_crq(adapter, &crq);
3505 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3506 atomic_inc(&adapter->running_cap_crqs);
3507 ibmvnic_send_crq(adapter, &crq);
3509 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3510 atomic_inc(&adapter->running_cap_crqs);
3511 ibmvnic_send_crq(adapter, &crq);
3513 crq.query_capability.capability =
3514 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3515 atomic_inc(&adapter->running_cap_crqs);
3516 ibmvnic_send_crq(adapter, &crq);
3518 crq.query_capability.capability =
3519 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3520 atomic_inc(&adapter->running_cap_crqs);
3521 ibmvnic_send_crq(adapter, &crq);
3523 crq.query_capability.capability =
3524 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3525 atomic_inc(&adapter->running_cap_crqs);
3526 ibmvnic_send_crq(adapter, &crq);
3528 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3529 atomic_inc(&adapter->running_cap_crqs);
3530 ibmvnic_send_crq(adapter, &crq);
3533 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3534 struct ibmvnic_adapter *adapter)
3536 struct device *dev = &adapter->vdev->dev;
3538 if (crq->get_vpd_size_rsp.rc.code) {
3539 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3540 crq->get_vpd_size_rsp.rc.code);
3541 complete(&adapter->fw_done);
3545 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3546 complete(&adapter->fw_done);
3549 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3550 struct ibmvnic_adapter *adapter)
3552 struct device *dev = &adapter->vdev->dev;
3553 unsigned char *substr = NULL;
3554 u8 fw_level_len = 0;
3556 memset(adapter->fw_version, 0, 32);
3558 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3561 if (crq->get_vpd_rsp.rc.code) {
3562 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3563 crq->get_vpd_rsp.rc.code);
3567 /* get the position of the firmware version info
3568 * located after the ASCII 'RM' substring in the buffer
3570 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3572 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3576 /* get length of firmware level ASCII substring */
3577 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3578 fw_level_len = *(substr + 2);
3580 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3584 /* copy firmware version string from vpd into adapter */
3585 if ((substr + 3 + fw_level_len) <
3586 (adapter->vpd->buff + adapter->vpd->len)) {
3587 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3589 dev_info(dev, "FW substr extrapolated VPD buff\n");
3593 if (adapter->fw_version[0] == '\0')
3594 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3595 complete(&adapter->fw_done);
3598 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3600 struct device *dev = &adapter->vdev->dev;
3601 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3602 union ibmvnic_crq crq;
3605 dma_unmap_single(dev, adapter->ip_offload_tok,
3606 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3608 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3609 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3610 netdev_dbg(adapter->netdev, "%016lx\n",
3611 ((unsigned long int *)(buf))[i]);
3613 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3614 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3615 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3616 buf->tcp_ipv4_chksum);
3617 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3618 buf->tcp_ipv6_chksum);
3619 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3620 buf->udp_ipv4_chksum);
3621 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3622 buf->udp_ipv6_chksum);
3623 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3624 buf->large_tx_ipv4);
3625 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3626 buf->large_tx_ipv6);
3627 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3628 buf->large_rx_ipv4);
3629 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3630 buf->large_rx_ipv6);
3631 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3632 buf->max_ipv4_header_size);
3633 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3634 buf->max_ipv6_header_size);
3635 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3636 buf->max_tcp_header_size);
3637 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3638 buf->max_udp_header_size);
3639 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3640 buf->max_large_tx_size);
3641 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3642 buf->max_large_rx_size);
3643 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3644 buf->ipv6_extension_header);
3645 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3646 buf->tcp_pseudosum_req);
3647 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3648 buf->num_ipv6_ext_headers);
3649 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3650 buf->off_ipv6_ext_headers);
3652 adapter->ip_offload_ctrl_tok =
3653 dma_map_single(dev, &adapter->ip_offload_ctrl,
3654 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3656 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3657 dev_err(dev, "Couldn't map ip offload control buffer\n");
3661 adapter->ip_offload_ctrl.len =
3662 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3663 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3664 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3665 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3666 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3667 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3668 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3669 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3670 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3671 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3673 /* large_rx disabled for now, additional features needed */
3674 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3675 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3677 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3679 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3680 adapter->netdev->features |= NETIF_F_IP_CSUM;
3682 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3683 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3685 if ((adapter->netdev->features &
3686 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3687 adapter->netdev->features |= NETIF_F_RXCSUM;
3689 if (buf->large_tx_ipv4)
3690 adapter->netdev->features |= NETIF_F_TSO;
3691 if (buf->large_tx_ipv6)
3692 adapter->netdev->features |= NETIF_F_TSO6;
3694 adapter->netdev->hw_features |= adapter->netdev->features;
3696 memset(&crq, 0, sizeof(crq));
3697 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3698 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3699 crq.control_ip_offload.len =
3700 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3701 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3702 ibmvnic_send_crq(adapter, &crq);
3705 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3706 struct ibmvnic_adapter *adapter)
3708 struct device *dev = &adapter->vdev->dev;
3709 struct ibmvnic_error_buff *error_buff, *tmp;
3710 unsigned long flags;
3714 if (!crq->request_error_rsp.rc.code) {
3715 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3716 crq->request_error_rsp.rc.code);
3720 spin_lock_irqsave(&adapter->error_list_lock, flags);
3721 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3722 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3724 list_del(&error_buff->list);
3727 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3730 dev_err(dev, "Couldn't find error id %x\n",
3731 be32_to_cpu(crq->request_error_rsp.error_id));
3735 dev_err(dev, "Detailed info for error id %x:",
3736 be32_to_cpu(crq->request_error_rsp.error_id));
3738 for (i = 0; i < error_buff->len; i++) {
3739 pr_cont("%02x", (int)error_buff->buff[i]);
3745 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3747 kfree(error_buff->buff);
3751 static void request_error_information(struct ibmvnic_adapter *adapter,
3752 union ibmvnic_crq *err_crq)
3754 struct device *dev = &adapter->vdev->dev;
3755 struct net_device *netdev = adapter->netdev;
3756 struct ibmvnic_error_buff *error_buff;
3757 unsigned long timeout = msecs_to_jiffies(30000);
3758 union ibmvnic_crq crq;
3759 unsigned long flags;
3762 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3766 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3767 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3768 if (!error_buff->buff) {
3773 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3775 if (dma_mapping_error(dev, error_buff->dma)) {
3776 netdev_err(netdev, "Couldn't map error buffer\n");
3777 kfree(error_buff->buff);
3782 error_buff->len = detail_len;
3783 error_buff->error_id = err_crq->error_indication.error_id;
3785 spin_lock_irqsave(&adapter->error_list_lock, flags);
3786 list_add_tail(&error_buff->list, &adapter->errors);
3787 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3789 memset(&crq, 0, sizeof(crq));
3790 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3791 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3792 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3793 crq.request_error_info.len = cpu_to_be32(detail_len);
3794 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3796 rc = ibmvnic_send_crq(adapter, &crq);
3798 netdev_err(netdev, "failed to request error information\n");
3802 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3803 netdev_err(netdev, "timeout waiting for error information\n");
3810 spin_lock_irqsave(&adapter->error_list_lock, flags);
3811 list_del(&error_buff->list);
3812 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3814 kfree(error_buff->buff);
3818 static void handle_error_indication(union ibmvnic_crq *crq,
3819 struct ibmvnic_adapter *adapter)
3821 struct device *dev = &adapter->vdev->dev;
3823 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3824 crq->error_indication.flags
3825 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3826 be32_to_cpu(crq->error_indication.error_id),
3827 be16_to_cpu(crq->error_indication.error_cause));
3829 if (be32_to_cpu(crq->error_indication.error_id))
3830 request_error_information(adapter, crq);
3832 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3833 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3835 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3838 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3839 struct ibmvnic_adapter *adapter)
3841 struct net_device *netdev = adapter->netdev;
3842 struct device *dev = &adapter->vdev->dev;
3845 rc = crq->change_mac_addr_rsp.rc.code;
3847 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3850 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3853 complete(&adapter->fw_done);
3857 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3858 struct ibmvnic_adapter *adapter)
3860 struct device *dev = &adapter->vdev->dev;
3864 atomic_dec(&adapter->running_cap_crqs);
3865 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3867 req_value = &adapter->req_tx_queues;
3871 req_value = &adapter->req_rx_queues;
3874 case REQ_RX_ADD_QUEUES:
3875 req_value = &adapter->req_rx_add_queues;
3878 case REQ_TX_ENTRIES_PER_SUBCRQ:
3879 req_value = &adapter->req_tx_entries_per_subcrq;
3880 name = "tx_entries_per_subcrq";
3882 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3883 req_value = &adapter->req_rx_add_entries_per_subcrq;
3884 name = "rx_add_entries_per_subcrq";
3887 req_value = &adapter->req_mtu;
3890 case PROMISC_REQUESTED:
3891 req_value = &adapter->promisc;
3895 dev_err(dev, "Got invalid cap request rsp %d\n",
3896 crq->request_capability.capability);
3900 switch (crq->request_capability_rsp.rc.code) {
3903 case PARTIALSUCCESS:
3904 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3906 (long int)be64_to_cpu(crq->request_capability_rsp.
3909 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3911 pr_err("mtu of %llu is not supported. Reverting.\n",
3913 *req_value = adapter->fallback.mtu;
3916 be64_to_cpu(crq->request_capability_rsp.number);
3919 ibmvnic_send_req_caps(adapter, 1);
3922 dev_err(dev, "Error %d in request cap rsp\n",
3923 crq->request_capability_rsp.rc.code);
3927 /* Done receiving requested capabilities, query IP offload support */
3928 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3929 union ibmvnic_crq newcrq;
3930 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3931 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3932 &adapter->ip_offload_buf;
3934 adapter->wait_capability = false;
3935 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3939 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3940 if (!firmware_has_feature(FW_FEATURE_CMO))
3941 dev_err(dev, "Couldn't map offload buffer\n");
3945 memset(&newcrq, 0, sizeof(newcrq));
3946 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3947 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3948 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3949 newcrq.query_ip_offload.ioba =
3950 cpu_to_be32(adapter->ip_offload_tok);
3952 ibmvnic_send_crq(adapter, &newcrq);
3956 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3957 struct ibmvnic_adapter *adapter)
3959 struct device *dev = &adapter->vdev->dev;
3960 struct net_device *netdev = adapter->netdev;
3961 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3962 struct ibmvnic_login_buffer *login = adapter->login_buf;
3965 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3967 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3968 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
3970 /* If the number of queues requested can't be allocated by the
3971 * server, the login response will return with code 1. We will need
3972 * to resend the login buffer with fewer queues requested.
3974 if (login_rsp_crq->generic.rc.code) {
3975 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
3976 complete(&adapter->init_done);
3980 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3982 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3983 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3984 netdev_dbg(adapter->netdev, "%016lx\n",
3985 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3989 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3990 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3991 adapter->req_rx_add_queues !=
3992 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3993 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3994 ibmvnic_remove(adapter->vdev);
3997 release_login_buffer(adapter);
3998 complete(&adapter->init_done);
4003 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4004 struct ibmvnic_adapter *adapter)
4006 struct device *dev = &adapter->vdev->dev;
4009 rc = crq->request_unmap_rsp.rc.code;
4011 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4014 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4015 struct ibmvnic_adapter *adapter)
4017 struct net_device *netdev = adapter->netdev;
4018 struct device *dev = &adapter->vdev->dev;
4021 rc = crq->query_map_rsp.rc.code;
4023 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4026 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4027 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4028 crq->query_map_rsp.free_pages);
4031 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4032 struct ibmvnic_adapter *adapter)
4034 struct net_device *netdev = adapter->netdev;
4035 struct device *dev = &adapter->vdev->dev;
4038 atomic_dec(&adapter->running_cap_crqs);
4039 netdev_dbg(netdev, "Outstanding queries: %d\n",
4040 atomic_read(&adapter->running_cap_crqs));
4041 rc = crq->query_capability.rc.code;
4043 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4047 switch (be16_to_cpu(crq->query_capability.capability)) {
4049 adapter->min_tx_queues =
4050 be64_to_cpu(crq->query_capability.number);
4051 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4052 adapter->min_tx_queues);
4055 adapter->min_rx_queues =
4056 be64_to_cpu(crq->query_capability.number);
4057 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4058 adapter->min_rx_queues);
4060 case MIN_RX_ADD_QUEUES:
4061 adapter->min_rx_add_queues =
4062 be64_to_cpu(crq->query_capability.number);
4063 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4064 adapter->min_rx_add_queues);
4067 adapter->max_tx_queues =
4068 be64_to_cpu(crq->query_capability.number);
4069 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4070 adapter->max_tx_queues);
4073 adapter->max_rx_queues =
4074 be64_to_cpu(crq->query_capability.number);
4075 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4076 adapter->max_rx_queues);
4078 case MAX_RX_ADD_QUEUES:
4079 adapter->max_rx_add_queues =
4080 be64_to_cpu(crq->query_capability.number);
4081 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4082 adapter->max_rx_add_queues);
4084 case MIN_TX_ENTRIES_PER_SUBCRQ:
4085 adapter->min_tx_entries_per_subcrq =
4086 be64_to_cpu(crq->query_capability.number);
4087 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4088 adapter->min_tx_entries_per_subcrq);
4090 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4091 adapter->min_rx_add_entries_per_subcrq =
4092 be64_to_cpu(crq->query_capability.number);
4093 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4094 adapter->min_rx_add_entries_per_subcrq);
4096 case MAX_TX_ENTRIES_PER_SUBCRQ:
4097 adapter->max_tx_entries_per_subcrq =
4098 be64_to_cpu(crq->query_capability.number);
4099 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4100 adapter->max_tx_entries_per_subcrq);
4102 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4103 adapter->max_rx_add_entries_per_subcrq =
4104 be64_to_cpu(crq->query_capability.number);
4105 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4106 adapter->max_rx_add_entries_per_subcrq);
4108 case TCP_IP_OFFLOAD:
4109 adapter->tcp_ip_offload =
4110 be64_to_cpu(crq->query_capability.number);
4111 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4112 adapter->tcp_ip_offload);
4114 case PROMISC_SUPPORTED:
4115 adapter->promisc_supported =
4116 be64_to_cpu(crq->query_capability.number);
4117 netdev_dbg(netdev, "promisc_supported = %lld\n",
4118 adapter->promisc_supported);
4121 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4122 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4123 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4126 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4127 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4128 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4130 case MAX_MULTICAST_FILTERS:
4131 adapter->max_multicast_filters =
4132 be64_to_cpu(crq->query_capability.number);
4133 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4134 adapter->max_multicast_filters);
4136 case VLAN_HEADER_INSERTION:
4137 adapter->vlan_header_insertion =
4138 be64_to_cpu(crq->query_capability.number);
4139 if (adapter->vlan_header_insertion)
4140 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4141 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4142 adapter->vlan_header_insertion);
4144 case RX_VLAN_HEADER_INSERTION:
4145 adapter->rx_vlan_header_insertion =
4146 be64_to_cpu(crq->query_capability.number);
4147 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4148 adapter->rx_vlan_header_insertion);
4150 case MAX_TX_SG_ENTRIES:
4151 adapter->max_tx_sg_entries =
4152 be64_to_cpu(crq->query_capability.number);
4153 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4154 adapter->max_tx_sg_entries);
4156 case RX_SG_SUPPORTED:
4157 adapter->rx_sg_supported =
4158 be64_to_cpu(crq->query_capability.number);
4159 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4160 adapter->rx_sg_supported);
4162 case OPT_TX_COMP_SUB_QUEUES:
4163 adapter->opt_tx_comp_sub_queues =
4164 be64_to_cpu(crq->query_capability.number);
4165 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4166 adapter->opt_tx_comp_sub_queues);
4168 case OPT_RX_COMP_QUEUES:
4169 adapter->opt_rx_comp_queues =
4170 be64_to_cpu(crq->query_capability.number);
4171 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4172 adapter->opt_rx_comp_queues);
4174 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4175 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4176 be64_to_cpu(crq->query_capability.number);
4177 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4178 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4180 case OPT_TX_ENTRIES_PER_SUBCRQ:
4181 adapter->opt_tx_entries_per_subcrq =
4182 be64_to_cpu(crq->query_capability.number);
4183 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4184 adapter->opt_tx_entries_per_subcrq);
4186 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4187 adapter->opt_rxba_entries_per_subcrq =
4188 be64_to_cpu(crq->query_capability.number);
4189 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4190 adapter->opt_rxba_entries_per_subcrq);
4192 case TX_RX_DESC_REQ:
4193 adapter->tx_rx_desc_req = crq->query_capability.number;
4194 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4195 adapter->tx_rx_desc_req);
4199 netdev_err(netdev, "Got invalid cap rsp %d\n",
4200 crq->query_capability.capability);
4204 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4205 adapter->wait_capability = false;
4206 ibmvnic_send_req_caps(adapter, 0);
4210 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4211 struct ibmvnic_adapter *adapter)
4213 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4214 struct net_device *netdev = adapter->netdev;
4215 struct device *dev = &adapter->vdev->dev;
4216 u64 *u64_crq = (u64 *)crq;
4219 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4220 (unsigned long int)cpu_to_be64(u64_crq[0]),
4221 (unsigned long int)cpu_to_be64(u64_crq[1]));
4222 switch (gen_crq->first) {
4223 case IBMVNIC_CRQ_INIT_RSP:
4224 switch (gen_crq->cmd) {
4225 case IBMVNIC_CRQ_INIT:
4226 dev_info(dev, "Partner initialized\n");
4227 adapter->from_passive_init = true;
4228 adapter->failover_pending = false;
4229 complete(&adapter->init_done);
4230 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4232 case IBMVNIC_CRQ_INIT_COMPLETE:
4233 dev_info(dev, "Partner initialization complete\n");
4234 adapter->crq.active = true;
4235 send_version_xchg(adapter);
4238 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4241 case IBMVNIC_CRQ_XPORT_EVENT:
4242 netif_carrier_off(netdev);
4243 adapter->crq.active = false;
4244 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4245 dev_info(dev, "Migrated, re-enabling adapter\n");
4246 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4247 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4248 dev_info(dev, "Backing device failover detected\n");
4249 adapter->failover_pending = true;
4251 /* The adapter lost the connection */
4252 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4254 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4257 case IBMVNIC_CRQ_CMD_RSP:
4260 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4265 switch (gen_crq->cmd) {
4266 case VERSION_EXCHANGE_RSP:
4267 rc = crq->version_exchange_rsp.rc.code;
4269 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4272 dev_info(dev, "Partner protocol version is %d\n",
4273 crq->version_exchange_rsp.version);
4274 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4277 be16_to_cpu(crq->version_exchange_rsp.version);
4278 send_cap_queries(adapter);
4280 case QUERY_CAPABILITY_RSP:
4281 handle_query_cap_rsp(crq, adapter);
4284 handle_query_map_rsp(crq, adapter);
4286 case REQUEST_MAP_RSP:
4287 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4288 complete(&adapter->fw_done);
4290 case REQUEST_UNMAP_RSP:
4291 handle_request_unmap_rsp(crq, adapter);
4293 case REQUEST_CAPABILITY_RSP:
4294 handle_request_cap_rsp(crq, adapter);
4297 netdev_dbg(netdev, "Got Login Response\n");
4298 handle_login_rsp(crq, adapter);
4300 case LOGICAL_LINK_STATE_RSP:
4302 "Got Logical Link State Response, state: %d rc: %d\n",
4303 crq->logical_link_state_rsp.link_state,
4304 crq->logical_link_state_rsp.rc.code);
4305 adapter->logical_link_state =
4306 crq->logical_link_state_rsp.link_state;
4307 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4308 complete(&adapter->init_done);
4310 case LINK_STATE_INDICATION:
4311 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4312 adapter->phys_link_state =
4313 crq->link_state_indication.phys_link_state;
4314 adapter->logical_link_state =
4315 crq->link_state_indication.logical_link_state;
4317 case CHANGE_MAC_ADDR_RSP:
4318 netdev_dbg(netdev, "Got MAC address change Response\n");
4319 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4321 case ERROR_INDICATION:
4322 netdev_dbg(netdev, "Got Error Indication\n");
4323 handle_error_indication(crq, adapter);
4325 case REQUEST_ERROR_RSP:
4326 netdev_dbg(netdev, "Got Error Detail Response\n");
4327 handle_error_info_rsp(crq, adapter);
4329 case REQUEST_STATISTICS_RSP:
4330 netdev_dbg(netdev, "Got Statistics Response\n");
4331 complete(&adapter->stats_done);
4333 case QUERY_IP_OFFLOAD_RSP:
4334 netdev_dbg(netdev, "Got Query IP offload Response\n");
4335 handle_query_ip_offload_rsp(adapter);
4337 case MULTICAST_CTRL_RSP:
4338 netdev_dbg(netdev, "Got multicast control Response\n");
4340 case CONTROL_IP_OFFLOAD_RSP:
4341 netdev_dbg(netdev, "Got Control IP offload Response\n");
4342 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4343 sizeof(adapter->ip_offload_ctrl),
4345 complete(&adapter->init_done);
4347 case COLLECT_FW_TRACE_RSP:
4348 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4349 complete(&adapter->fw_done);
4351 case GET_VPD_SIZE_RSP:
4352 handle_vpd_size_rsp(crq, adapter);
4355 handle_vpd_rsp(crq, adapter);
4358 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4363 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4365 struct ibmvnic_adapter *adapter = instance;
4367 tasklet_schedule(&adapter->tasklet);
4371 static void ibmvnic_tasklet(void *data)
4373 struct ibmvnic_adapter *adapter = data;
4374 struct ibmvnic_crq_queue *queue = &adapter->crq;
4375 union ibmvnic_crq *crq;
4376 unsigned long flags;
4379 spin_lock_irqsave(&queue->lock, flags);
4381 /* Pull all the valid messages off the CRQ */
4382 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4383 ibmvnic_handle_crq(crq, adapter);
4384 crq->generic.first = 0;
4387 /* remain in tasklet until all
4388 * capabilities responses are received
4390 if (!adapter->wait_capability)
4393 /* if capabilities CRQ's were sent in this tasklet, the following
4394 * tasklet must wait until all responses are received
4396 if (atomic_read(&adapter->running_cap_crqs) != 0)
4397 adapter->wait_capability = true;
4398 spin_unlock_irqrestore(&queue->lock, flags);
4401 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4403 struct vio_dev *vdev = adapter->vdev;
4407 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4408 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4411 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4416 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4418 struct ibmvnic_crq_queue *crq = &adapter->crq;
4419 struct device *dev = &adapter->vdev->dev;
4420 struct vio_dev *vdev = adapter->vdev;
4425 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4426 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4428 /* Clean out the queue */
4429 memset(crq->msgs, 0, PAGE_SIZE);
4431 crq->active = false;
4433 /* And re-open it again */
4434 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4435 crq->msg_token, PAGE_SIZE);
4438 /* Adapter is good, but other end is not ready */
4439 dev_warn(dev, "Partner adapter not ready\n");
4441 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4446 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4448 struct ibmvnic_crq_queue *crq = &adapter->crq;
4449 struct vio_dev *vdev = adapter->vdev;
4455 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4456 free_irq(vdev->irq, adapter);
4457 tasklet_kill(&adapter->tasklet);
4459 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4460 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4462 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4464 free_page((unsigned long)crq->msgs);
4466 crq->active = false;
4469 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4471 struct ibmvnic_crq_queue *crq = &adapter->crq;
4472 struct device *dev = &adapter->vdev->dev;
4473 struct vio_dev *vdev = adapter->vdev;
4474 int rc, retrc = -ENOMEM;
4479 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4480 /* Should we allocate more than one page? */
4485 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4486 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4488 if (dma_mapping_error(dev, crq->msg_token))
4491 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4492 crq->msg_token, PAGE_SIZE);
4494 if (rc == H_RESOURCE)
4495 /* maybe kexecing and resource is busy. try a reset */
4496 rc = ibmvnic_reset_crq(adapter);
4499 if (rc == H_CLOSED) {
4500 dev_warn(dev, "Partner adapter not ready\n");
4502 dev_warn(dev, "Error %d opening adapter\n", rc);
4503 goto reg_crq_failed;
4508 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4509 (unsigned long)adapter);
4511 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4512 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4515 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4517 goto req_irq_failed;
4520 rc = vio_enable_interrupts(vdev);
4522 dev_err(dev, "Error %d enabling interrupts\n", rc);
4523 goto req_irq_failed;
4527 spin_lock_init(&crq->lock);
4532 tasklet_kill(&adapter->tasklet);
4534 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4535 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4537 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4539 free_page((unsigned long)crq->msgs);
4544 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4546 struct device *dev = &adapter->vdev->dev;
4547 unsigned long timeout = msecs_to_jiffies(30000);
4548 u64 old_num_rx_queues, old_num_tx_queues;
4551 adapter->from_passive_init = false;
4553 old_num_rx_queues = adapter->req_rx_queues;
4554 old_num_tx_queues = adapter->req_tx_queues;
4556 init_completion(&adapter->init_done);
4557 adapter->init_done_rc = 0;
4558 ibmvnic_send_crq_init(adapter);
4559 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4560 dev_err(dev, "Initialization sequence timed out\n");
4564 if (adapter->init_done_rc) {
4565 release_crq_queue(adapter);
4566 return adapter->init_done_rc;
4569 if (adapter->from_passive_init) {
4570 adapter->state = VNIC_OPEN;
4571 adapter->from_passive_init = false;
4575 if (adapter->resetting && !adapter->wait_for_reset &&
4576 adapter->reset_reason != VNIC_RESET_MOBILITY) {
4577 if (adapter->req_rx_queues != old_num_rx_queues ||
4578 adapter->req_tx_queues != old_num_tx_queues) {
4579 release_sub_crqs(adapter, 0);
4580 rc = init_sub_crqs(adapter);
4582 rc = reset_sub_crq_queues(adapter);
4585 rc = init_sub_crqs(adapter);
4589 dev_err(dev, "Initialization of sub crqs failed\n");
4590 release_crq_queue(adapter);
4594 rc = init_sub_crq_irqs(adapter);
4596 dev_err(dev, "Failed to initialize sub crq irqs\n");
4597 release_crq_queue(adapter);
4603 static struct device_attribute dev_attr_failover;
4605 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4607 struct ibmvnic_adapter *adapter;
4608 struct net_device *netdev;
4609 unsigned char *mac_addr_p;
4612 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4615 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4616 VETH_MAC_ADDR, NULL);
4619 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4620 __FILE__, __LINE__);
4624 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4625 IBMVNIC_MAX_QUEUES);
4629 adapter = netdev_priv(netdev);
4630 adapter->state = VNIC_PROBING;
4631 dev_set_drvdata(&dev->dev, netdev);
4632 adapter->vdev = dev;
4633 adapter->netdev = netdev;
4635 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4636 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4637 netdev->irq = dev->irq;
4638 netdev->netdev_ops = &ibmvnic_netdev_ops;
4639 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4640 SET_NETDEV_DEV(netdev, &dev->dev);
4642 spin_lock_init(&adapter->stats_lock);
4644 INIT_LIST_HEAD(&adapter->errors);
4645 spin_lock_init(&adapter->error_list_lock);
4647 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4648 INIT_LIST_HEAD(&adapter->rwi_list);
4649 mutex_init(&adapter->reset_lock);
4650 mutex_init(&adapter->rwi_lock);
4651 adapter->resetting = false;
4653 adapter->mac_change_pending = false;
4656 rc = init_crq_queue(adapter);
4658 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4660 goto ibmvnic_init_fail;
4663 rc = ibmvnic_init(adapter);
4664 if (rc && rc != EAGAIN)
4665 goto ibmvnic_init_fail;
4666 } while (rc == EAGAIN);
4668 rc = init_stats_buffers(adapter);
4670 goto ibmvnic_init_fail;
4672 rc = init_stats_token(adapter);
4674 goto ibmvnic_stats_fail;
4676 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4677 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4678 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4680 rc = device_create_file(&dev->dev, &dev_attr_failover);
4682 goto ibmvnic_dev_file_err;
4684 netif_carrier_off(netdev);
4685 rc = register_netdev(netdev);
4687 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4688 goto ibmvnic_register_fail;
4690 dev_info(&dev->dev, "ibmvnic registered\n");
4692 adapter->state = VNIC_PROBED;
4694 adapter->wait_for_reset = false;
4698 ibmvnic_register_fail:
4699 device_remove_file(&dev->dev, &dev_attr_failover);
4701 ibmvnic_dev_file_err:
4702 release_stats_token(adapter);
4705 release_stats_buffers(adapter);
4708 release_sub_crqs(adapter, 1);
4709 release_crq_queue(adapter);
4710 free_netdev(netdev);
4715 static int ibmvnic_remove(struct vio_dev *dev)
4717 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4718 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4720 adapter->state = VNIC_REMOVING;
4721 unregister_netdev(netdev);
4722 mutex_lock(&adapter->reset_lock);
4724 release_resources(adapter);
4725 release_sub_crqs(adapter, 1);
4726 release_crq_queue(adapter);
4728 release_stats_token(adapter);
4729 release_stats_buffers(adapter);
4731 adapter->state = VNIC_REMOVED;
4733 mutex_unlock(&adapter->reset_lock);
4734 device_remove_file(&dev->dev, &dev_attr_failover);
4735 free_netdev(netdev);
4736 dev_set_drvdata(&dev->dev, NULL);
4741 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4742 const char *buf, size_t count)
4744 struct net_device *netdev = dev_get_drvdata(dev);
4745 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4746 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4747 __be64 session_token;
4750 if (!sysfs_streq(buf, "1"))
4753 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4754 H_GET_SESSION_TOKEN, 0, 0, 0);
4756 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4761 session_token = (__be64)retbuf[0];
4762 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4763 be64_to_cpu(session_token));
4764 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4765 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4767 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4775 static DEVICE_ATTR_WO(failover);
4777 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4779 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4780 struct ibmvnic_adapter *adapter;
4781 struct iommu_table *tbl;
4782 unsigned long ret = 0;
4785 tbl = get_iommu_table_base(&vdev->dev);
4787 /* netdev inits at probe time along with the structures we need below*/
4789 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4791 adapter = netdev_priv(netdev);
4793 ret += PAGE_SIZE; /* the crq message queue */
4794 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4796 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4797 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4799 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4801 ret += adapter->rx_pool[i].size *
4802 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4807 static int ibmvnic_resume(struct device *dev)
4809 struct net_device *netdev = dev_get_drvdata(dev);
4810 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4812 if (adapter->state != VNIC_OPEN)
4815 tasklet_schedule(&adapter->tasklet);
4820 static const struct vio_device_id ibmvnic_device_table[] = {
4821 {"network", "IBM,vnic"},
4824 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4826 static const struct dev_pm_ops ibmvnic_pm_ops = {
4827 .resume = ibmvnic_resume
4830 static struct vio_driver ibmvnic_driver = {
4831 .id_table = ibmvnic_device_table,
4832 .probe = ibmvnic_probe,
4833 .remove = ibmvnic_remove,
4834 .get_desired_dma = ibmvnic_get_desired_dma,
4835 .name = ibmvnic_driver_name,
4836 .pm = &ibmvnic_pm_ops,
4839 /* module functions */
4840 static int __init ibmvnic_module_init(void)
4842 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4843 IBMVNIC_DRIVER_VERSION);
4845 return vio_register_driver(&ibmvnic_driver);
4848 static void __exit ibmvnic_module_exit(void)
4850 vio_unregister_driver(&ibmvnic_driver);
4853 module_init(ibmvnic_module_init);
4854 module_exit(ibmvnic_module_exit);