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;
794 static int ibmvnic_login(struct net_device *netdev)
796 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
797 unsigned long timeout = msecs_to_jiffies(30000);
802 if (retry_count > IBMVNIC_MAX_QUEUES) {
803 netdev_warn(netdev, "Login attempts exceeded\n");
807 adapter->init_done_rc = 0;
808 reinit_completion(&adapter->init_done);
809 rc = send_login(adapter);
811 netdev_warn(netdev, "Unable to login\n");
815 if (!wait_for_completion_timeout(&adapter->init_done,
817 netdev_warn(netdev, "Login timed out\n");
821 if (adapter->init_done_rc == PARTIALSUCCESS) {
823 release_sub_crqs(adapter, 1);
825 adapter->init_done_rc = 0;
826 reinit_completion(&adapter->init_done);
827 send_cap_queries(adapter);
828 if (!wait_for_completion_timeout(&adapter->init_done,
831 "Capabilities query timed out\n");
835 rc = init_sub_crqs(adapter);
838 "SCRQ initialization failed\n");
842 rc = init_sub_crq_irqs(adapter);
845 "SCRQ irq initialization failed\n");
848 } else if (adapter->init_done_rc) {
849 netdev_warn(netdev, "Adapter login failed\n");
852 } while (adapter->init_done_rc == PARTIALSUCCESS);
854 /* handle pending MAC address changes after successful login */
855 if (adapter->mac_change_pending) {
856 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
857 adapter->mac_change_pending = false;
863 static void release_login_buffer(struct ibmvnic_adapter *adapter)
865 kfree(adapter->login_buf);
866 adapter->login_buf = NULL;
869 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
871 kfree(adapter->login_rsp_buf);
872 adapter->login_rsp_buf = NULL;
875 static void release_resources(struct ibmvnic_adapter *adapter)
877 release_vpd_data(adapter);
879 release_tx_pools(adapter);
880 release_rx_pools(adapter);
882 release_error_buffers(adapter);
883 release_napi(adapter);
884 release_login_rsp_buffer(adapter);
887 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
889 struct net_device *netdev = adapter->netdev;
890 unsigned long timeout = msecs_to_jiffies(30000);
891 union ibmvnic_crq crq;
895 netdev_dbg(netdev, "setting link state %d\n", link_state);
897 memset(&crq, 0, sizeof(crq));
898 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
899 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
900 crq.logical_link_state.link_state = link_state;
905 reinit_completion(&adapter->init_done);
906 rc = ibmvnic_send_crq(adapter, &crq);
908 netdev_err(netdev, "Failed to set link state\n");
912 if (!wait_for_completion_timeout(&adapter->init_done,
914 netdev_err(netdev, "timeout setting link state\n");
918 if (adapter->init_done_rc == 1) {
919 /* Partuial success, delay and re-send */
928 static int set_real_num_queues(struct net_device *netdev)
930 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
933 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
934 adapter->req_tx_queues, adapter->req_rx_queues);
936 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
938 netdev_err(netdev, "failed to set the number of tx queues\n");
942 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
944 netdev_err(netdev, "failed to set the number of rx queues\n");
949 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
951 struct device *dev = &adapter->vdev->dev;
952 union ibmvnic_crq crq;
955 if (adapter->vpd->buff)
956 len = adapter->vpd->len;
958 init_completion(&adapter->fw_done);
959 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
960 crq.get_vpd_size.cmd = GET_VPD_SIZE;
961 ibmvnic_send_crq(adapter, &crq);
962 wait_for_completion(&adapter->fw_done);
964 if (!adapter->vpd->len)
967 if (!adapter->vpd->buff)
968 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
969 else if (adapter->vpd->len != len)
971 krealloc(adapter->vpd->buff,
972 adapter->vpd->len, GFP_KERNEL);
974 if (!adapter->vpd->buff) {
975 dev_err(dev, "Could allocate VPD buffer\n");
979 adapter->vpd->dma_addr =
980 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
982 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
983 dev_err(dev, "Could not map VPD buffer\n");
984 kfree(adapter->vpd->buff);
985 adapter->vpd->buff = NULL;
989 reinit_completion(&adapter->fw_done);
990 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
991 crq.get_vpd.cmd = GET_VPD;
992 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
993 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
994 ibmvnic_send_crq(adapter, &crq);
995 wait_for_completion(&adapter->fw_done);
1000 static int init_resources(struct ibmvnic_adapter *adapter)
1002 struct net_device *netdev = adapter->netdev;
1005 rc = set_real_num_queues(netdev);
1009 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1013 /* Vital Product Data (VPD) */
1014 rc = ibmvnic_get_vpd(adapter);
1016 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1020 adapter->map_id = 1;
1022 rc = init_napi(adapter);
1026 send_map_query(adapter);
1028 rc = init_rx_pools(netdev);
1032 rc = init_tx_pools(netdev);
1036 static int __ibmvnic_open(struct net_device *netdev)
1038 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1039 enum vnic_state prev_state = adapter->state;
1042 adapter->state = VNIC_OPENING;
1043 replenish_pools(adapter);
1044 ibmvnic_napi_enable(adapter);
1046 /* We're ready to receive frames, enable the sub-crq interrupts and
1047 * set the logical link state to up
1049 for (i = 0; i < adapter->req_rx_queues; i++) {
1050 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1051 if (prev_state == VNIC_CLOSED)
1052 enable_irq(adapter->rx_scrq[i]->irq);
1053 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1056 for (i = 0; i < adapter->req_tx_queues; i++) {
1057 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1058 if (prev_state == VNIC_CLOSED)
1059 enable_irq(adapter->tx_scrq[i]->irq);
1060 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1063 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1065 for (i = 0; i < adapter->req_rx_queues; i++)
1066 napi_disable(&adapter->napi[i]);
1067 release_resources(adapter);
1071 netif_tx_start_all_queues(netdev);
1073 if (prev_state == VNIC_CLOSED) {
1074 for (i = 0; i < adapter->req_rx_queues; i++)
1075 napi_schedule(&adapter->napi[i]);
1078 adapter->state = VNIC_OPEN;
1082 static int ibmvnic_open(struct net_device *netdev)
1084 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1087 /* If device failover is pending, just set device state and return.
1088 * Device operation will be handled by reset routine.
1090 if (adapter->failover_pending) {
1091 adapter->state = VNIC_OPEN;
1095 mutex_lock(&adapter->reset_lock);
1097 if (adapter->state != VNIC_CLOSED) {
1098 rc = ibmvnic_login(netdev);
1100 mutex_unlock(&adapter->reset_lock);
1104 rc = init_resources(adapter);
1106 netdev_err(netdev, "failed to initialize resources\n");
1107 release_resources(adapter);
1108 mutex_unlock(&adapter->reset_lock);
1113 rc = __ibmvnic_open(netdev);
1114 netif_carrier_on(netdev);
1116 mutex_unlock(&adapter->reset_lock);
1121 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1123 struct ibmvnic_rx_pool *rx_pool;
1124 struct ibmvnic_rx_buff *rx_buff;
1129 if (!adapter->rx_pool)
1132 rx_scrqs = adapter->num_active_rx_pools;
1133 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1135 /* Free any remaining skbs in the rx buffer pools */
1136 for (i = 0; i < rx_scrqs; i++) {
1137 rx_pool = &adapter->rx_pool[i];
1138 if (!rx_pool || !rx_pool->rx_buff)
1141 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1142 for (j = 0; j < rx_entries; j++) {
1143 rx_buff = &rx_pool->rx_buff[j];
1144 if (rx_buff && rx_buff->skb) {
1145 dev_kfree_skb_any(rx_buff->skb);
1146 rx_buff->skb = NULL;
1152 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1153 struct ibmvnic_tx_pool *tx_pool)
1155 struct ibmvnic_tx_buff *tx_buff;
1159 if (!tx_pool || !tx_pool->tx_buff)
1162 tx_entries = tx_pool->num_buffers;
1164 for (i = 0; i < tx_entries; i++) {
1165 tx_buff = &tx_pool->tx_buff[i];
1166 if (tx_buff && tx_buff->skb) {
1167 dev_kfree_skb_any(tx_buff->skb);
1168 tx_buff->skb = NULL;
1173 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1178 if (!adapter->tx_pool || !adapter->tso_pool)
1181 tx_scrqs = adapter->num_active_tx_pools;
1183 /* Free any remaining skbs in the tx buffer pools */
1184 for (i = 0; i < tx_scrqs; i++) {
1185 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1186 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1187 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1191 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1193 struct net_device *netdev = adapter->netdev;
1196 if (adapter->tx_scrq) {
1197 for (i = 0; i < adapter->req_tx_queues; i++)
1198 if (adapter->tx_scrq[i]->irq) {
1200 "Disabling tx_scrq[%d] irq\n", i);
1201 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1202 disable_irq(adapter->tx_scrq[i]->irq);
1206 if (adapter->rx_scrq) {
1207 for (i = 0; i < adapter->req_rx_queues; i++) {
1208 if (adapter->rx_scrq[i]->irq) {
1210 "Disabling rx_scrq[%d] irq\n", i);
1211 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1212 disable_irq(adapter->rx_scrq[i]->irq);
1218 static void ibmvnic_cleanup(struct net_device *netdev)
1220 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1222 /* ensure that transmissions are stopped if called by do_reset */
1223 if (adapter->resetting)
1224 netif_tx_disable(netdev);
1226 netif_tx_stop_all_queues(netdev);
1228 ibmvnic_napi_disable(adapter);
1229 ibmvnic_disable_irqs(adapter);
1231 clean_rx_pools(adapter);
1232 clean_tx_pools(adapter);
1235 static int __ibmvnic_close(struct net_device *netdev)
1237 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1240 adapter->state = VNIC_CLOSING;
1241 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1244 adapter->state = VNIC_CLOSED;
1248 static int ibmvnic_close(struct net_device *netdev)
1250 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1253 /* If device failover is pending, just set device state and return.
1254 * Device operation will be handled by reset routine.
1256 if (adapter->failover_pending) {
1257 adapter->state = VNIC_CLOSED;
1261 mutex_lock(&adapter->reset_lock);
1262 rc = __ibmvnic_close(netdev);
1263 ibmvnic_cleanup(netdev);
1264 mutex_unlock(&adapter->reset_lock);
1270 * build_hdr_data - creates L2/L3/L4 header data buffer
1271 * @hdr_field - bitfield determining needed headers
1272 * @skb - socket buffer
1273 * @hdr_len - array of header lengths
1274 * @tot_len - total length of data
1276 * Reads hdr_field to determine which headers are needed by firmware.
1277 * Builds a buffer containing these headers. Saves individual header
1278 * lengths and total buffer length to be used to build descriptors.
1280 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1281 int *hdr_len, u8 *hdr_data)
1286 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1287 hdr_len[0] = sizeof(struct vlan_ethhdr);
1289 hdr_len[0] = sizeof(struct ethhdr);
1291 if (skb->protocol == htons(ETH_P_IP)) {
1292 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1293 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1294 hdr_len[2] = tcp_hdrlen(skb);
1295 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1296 hdr_len[2] = sizeof(struct udphdr);
1297 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1298 hdr_len[1] = sizeof(struct ipv6hdr);
1299 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1300 hdr_len[2] = tcp_hdrlen(skb);
1301 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1302 hdr_len[2] = sizeof(struct udphdr);
1303 } else if (skb->protocol == htons(ETH_P_ARP)) {
1304 hdr_len[1] = arp_hdr_len(skb->dev);
1308 memset(hdr_data, 0, 120);
1309 if ((hdr_field >> 6) & 1) {
1310 hdr = skb_mac_header(skb);
1311 memcpy(hdr_data, hdr, hdr_len[0]);
1315 if ((hdr_field >> 5) & 1) {
1316 hdr = skb_network_header(skb);
1317 memcpy(hdr_data + len, hdr, hdr_len[1]);
1321 if ((hdr_field >> 4) & 1) {
1322 hdr = skb_transport_header(skb);
1323 memcpy(hdr_data + len, hdr, hdr_len[2]);
1330 * create_hdr_descs - create header and header extension descriptors
1331 * @hdr_field - bitfield determining needed headers
1332 * @data - buffer containing header data
1333 * @len - length of data buffer
1334 * @hdr_len - array of individual header lengths
1335 * @scrq_arr - descriptor array
1337 * Creates header and, if needed, header extension descriptors and
1338 * places them in a descriptor array, scrq_arr
1341 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1342 union sub_crq *scrq_arr)
1344 union sub_crq hdr_desc;
1350 while (tmp_len > 0) {
1351 cur = hdr_data + len - tmp_len;
1353 memset(&hdr_desc, 0, sizeof(hdr_desc));
1354 if (cur != hdr_data) {
1355 data = hdr_desc.hdr_ext.data;
1356 tmp = tmp_len > 29 ? 29 : tmp_len;
1357 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1358 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1359 hdr_desc.hdr_ext.len = tmp;
1361 data = hdr_desc.hdr.data;
1362 tmp = tmp_len > 24 ? 24 : tmp_len;
1363 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1364 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1365 hdr_desc.hdr.len = tmp;
1366 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1367 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1368 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1369 hdr_desc.hdr.flag = hdr_field << 1;
1371 memcpy(data, cur, tmp);
1373 *scrq_arr = hdr_desc;
1382 * build_hdr_descs_arr - build a header descriptor array
1383 * @skb - socket buffer
1384 * @num_entries - number of descriptors to be sent
1385 * @subcrq - first TX descriptor
1386 * @hdr_field - bit field determining which headers will be sent
1388 * This function will build a TX descriptor array with applicable
1389 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1392 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1393 int *num_entries, u8 hdr_field)
1395 int hdr_len[3] = {0, 0, 0};
1397 u8 *hdr_data = txbuff->hdr_data;
1399 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1401 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1402 txbuff->indir_arr + 1);
1405 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1406 struct net_device *netdev)
1408 /* For some backing devices, mishandling of small packets
1409 * can result in a loss of connection or TX stall. Device
1410 * architects recommend that no packet should be smaller
1411 * than the minimum MTU value provided to the driver, so
1412 * pad any packets to that length
1414 if (skb->len < netdev->min_mtu)
1415 return skb_put_padto(skb, netdev->min_mtu);
1420 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1422 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1423 int queue_num = skb_get_queue_mapping(skb);
1424 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1425 struct device *dev = &adapter->vdev->dev;
1426 struct ibmvnic_tx_buff *tx_buff = NULL;
1427 struct ibmvnic_sub_crq_queue *tx_scrq;
1428 struct ibmvnic_tx_pool *tx_pool;
1429 unsigned int tx_send_failed = 0;
1430 unsigned int tx_map_failed = 0;
1431 unsigned int tx_dropped = 0;
1432 unsigned int tx_packets = 0;
1433 unsigned int tx_bytes = 0;
1434 dma_addr_t data_dma_addr;
1435 struct netdev_queue *txq;
1436 unsigned long lpar_rc;
1437 union sub_crq tx_crq;
1438 unsigned int offset;
1439 int num_entries = 1;
1446 if (adapter->resetting) {
1447 if (!netif_subqueue_stopped(netdev, skb))
1448 netif_stop_subqueue(netdev, queue_num);
1449 dev_kfree_skb_any(skb);
1457 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1463 if (skb_is_gso(skb))
1464 tx_pool = &adapter->tso_pool[queue_num];
1466 tx_pool = &adapter->tx_pool[queue_num];
1468 tx_scrq = adapter->tx_scrq[queue_num];
1469 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1470 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1471 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1473 index = tx_pool->free_map[tx_pool->consumer_index];
1475 if (index == IBMVNIC_INVALID_MAP) {
1476 dev_kfree_skb_any(skb);
1483 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1485 offset = index * tx_pool->buf_size;
1486 dst = tx_pool->long_term_buff.buff + offset;
1487 memset(dst, 0, tx_pool->buf_size);
1488 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1490 if (skb_shinfo(skb)->nr_frags) {
1494 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1495 cur = skb_headlen(skb);
1497 /* Copy the frags */
1498 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1499 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1502 page_address(skb_frag_page(frag)) +
1503 frag->page_offset, skb_frag_size(frag));
1504 cur += skb_frag_size(frag);
1507 skb_copy_from_linear_data(skb, dst, skb->len);
1510 tx_pool->consumer_index =
1511 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1513 tx_buff = &tx_pool->tx_buff[index];
1515 tx_buff->data_dma[0] = data_dma_addr;
1516 tx_buff->data_len[0] = skb->len;
1517 tx_buff->index = index;
1518 tx_buff->pool_index = queue_num;
1519 tx_buff->last_frag = true;
1521 memset(&tx_crq, 0, sizeof(tx_crq));
1522 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1523 tx_crq.v1.type = IBMVNIC_TX_DESC;
1524 tx_crq.v1.n_crq_elem = 1;
1525 tx_crq.v1.n_sge = 1;
1526 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1528 if (skb_is_gso(skb))
1529 tx_crq.v1.correlator =
1530 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1532 tx_crq.v1.correlator = cpu_to_be32(index);
1533 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1534 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1535 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1537 if (adapter->vlan_header_insertion) {
1538 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1539 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1542 if (skb->protocol == htons(ETH_P_IP)) {
1543 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1544 proto = ip_hdr(skb)->protocol;
1545 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1546 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1547 proto = ipv6_hdr(skb)->nexthdr;
1550 if (proto == IPPROTO_TCP)
1551 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1552 else if (proto == IPPROTO_UDP)
1553 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1555 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1556 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1559 if (skb_is_gso(skb)) {
1560 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1561 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1564 /* determine if l2/3/4 headers are sent to firmware */
1565 if ((*hdrs >> 7) & 1) {
1566 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1567 tx_crq.v1.n_crq_elem = num_entries;
1568 tx_buff->num_entries = num_entries;
1569 tx_buff->indir_arr[0] = tx_crq;
1570 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1571 sizeof(tx_buff->indir_arr),
1573 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1574 dev_kfree_skb_any(skb);
1575 tx_buff->skb = NULL;
1576 if (!firmware_has_feature(FW_FEATURE_CMO))
1577 dev_err(dev, "tx: unable to map descriptor array\n");
1583 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1584 (u64)tx_buff->indir_dma,
1587 tx_buff->num_entries = num_entries;
1588 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1591 if (lpar_rc != H_SUCCESS) {
1592 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1593 dev_kfree_skb_any(skb);
1594 tx_buff->skb = NULL;
1596 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1597 /* Disable TX and report carrier off if queue is closed
1598 * or pending failover.
1599 * Firmware guarantees that a signal will be sent to the
1600 * driver, triggering a reset or some other action.
1602 netif_tx_stop_all_queues(netdev);
1603 netif_carrier_off(netdev);
1612 if (atomic_add_return(num_entries, &tx_scrq->used)
1613 >= adapter->req_tx_entries_per_subcrq) {
1614 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1615 netif_stop_subqueue(netdev, queue_num);
1619 tx_bytes += skb->len;
1620 txq->trans_start = jiffies;
1625 /* roll back consumer index and map array*/
1626 if (tx_pool->consumer_index == 0)
1627 tx_pool->consumer_index =
1628 tx_pool->num_buffers - 1;
1630 tx_pool->consumer_index--;
1631 tx_pool->free_map[tx_pool->consumer_index] = index;
1633 netdev->stats.tx_dropped += tx_dropped;
1634 netdev->stats.tx_bytes += tx_bytes;
1635 netdev->stats.tx_packets += tx_packets;
1636 adapter->tx_send_failed += tx_send_failed;
1637 adapter->tx_map_failed += tx_map_failed;
1638 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1639 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1640 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1645 static void ibmvnic_set_multi(struct net_device *netdev)
1647 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1648 struct netdev_hw_addr *ha;
1649 union ibmvnic_crq crq;
1651 memset(&crq, 0, sizeof(crq));
1652 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1653 crq.request_capability.cmd = REQUEST_CAPABILITY;
1655 if (netdev->flags & IFF_PROMISC) {
1656 if (!adapter->promisc_supported)
1659 if (netdev->flags & IFF_ALLMULTI) {
1660 /* Accept all multicast */
1661 memset(&crq, 0, sizeof(crq));
1662 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1663 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1664 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1665 ibmvnic_send_crq(adapter, &crq);
1666 } else if (netdev_mc_empty(netdev)) {
1667 /* Reject all multicast */
1668 memset(&crq, 0, sizeof(crq));
1669 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1670 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1671 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1672 ibmvnic_send_crq(adapter, &crq);
1674 /* Accept one or more multicast(s) */
1675 netdev_for_each_mc_addr(ha, netdev) {
1676 memset(&crq, 0, sizeof(crq));
1677 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1678 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1679 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1680 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1682 ibmvnic_send_crq(adapter, &crq);
1688 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1690 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1691 struct sockaddr *addr = p;
1692 union ibmvnic_crq crq;
1694 if (!is_valid_ether_addr(addr->sa_data))
1695 return -EADDRNOTAVAIL;
1697 memset(&crq, 0, sizeof(crq));
1698 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1699 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1700 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1702 init_completion(&adapter->fw_done);
1703 ibmvnic_send_crq(adapter, &crq);
1704 wait_for_completion(&adapter->fw_done);
1705 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1706 return adapter->fw_done_rc ? -EIO : 0;
1709 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1711 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1712 struct sockaddr *addr = p;
1715 if (adapter->state == VNIC_PROBED) {
1716 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1717 adapter->mac_change_pending = true;
1721 rc = __ibmvnic_set_mac(netdev, addr);
1727 * do_reset returns zero if we are able to keep processing reset events, or
1728 * non-zero if we hit a fatal error and must halt.
1730 static int do_reset(struct ibmvnic_adapter *adapter,
1731 struct ibmvnic_rwi *rwi, u32 reset_state)
1733 u64 old_num_rx_queues, old_num_tx_queues;
1734 struct net_device *netdev = adapter->netdev;
1737 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1740 netif_carrier_off(netdev);
1741 adapter->reset_reason = rwi->reset_reason;
1743 old_num_rx_queues = adapter->req_rx_queues;
1744 old_num_tx_queues = adapter->req_tx_queues;
1746 ibmvnic_cleanup(netdev);
1748 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1749 adapter->reset_reason != VNIC_RESET_FAILOVER) {
1750 rc = __ibmvnic_close(netdev);
1755 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1756 adapter->wait_for_reset) {
1757 release_resources(adapter);
1758 release_sub_crqs(adapter, 1);
1759 release_crq_queue(adapter);
1762 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1763 /* remove the closed state so when we call open it appears
1764 * we are coming from the probed state.
1766 adapter->state = VNIC_PROBED;
1768 if (adapter->wait_for_reset) {
1769 rc = init_crq_queue(adapter);
1770 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1771 rc = ibmvnic_reenable_crq_queue(adapter);
1772 release_sub_crqs(adapter, 1);
1774 rc = ibmvnic_reset_crq(adapter);
1776 rc = vio_enable_interrupts(adapter->vdev);
1780 netdev_err(adapter->netdev,
1781 "Couldn't initialize crq. rc=%d\n", rc);
1785 rc = ibmvnic_init(adapter);
1787 return IBMVNIC_INIT_FAILED;
1789 /* If the adapter was in PROBE state prior to the reset,
1792 if (reset_state == VNIC_PROBED)
1795 rc = ibmvnic_login(netdev);
1797 adapter->state = VNIC_PROBED;
1801 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1802 adapter->wait_for_reset) {
1803 rc = init_resources(adapter);
1806 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1807 adapter->req_tx_queues != old_num_tx_queues) {
1808 adapter->map_id = 1;
1809 release_rx_pools(adapter);
1810 release_tx_pools(adapter);
1811 init_rx_pools(netdev);
1812 init_tx_pools(netdev);
1814 release_napi(adapter);
1817 rc = reset_tx_pools(adapter);
1821 rc = reset_rx_pools(adapter);
1825 ibmvnic_disable_irqs(adapter);
1827 adapter->state = VNIC_CLOSED;
1829 if (reset_state == VNIC_CLOSED)
1832 rc = __ibmvnic_open(netdev);
1834 if (list_empty(&adapter->rwi_list))
1835 adapter->state = VNIC_CLOSED;
1837 adapter->state = reset_state;
1843 for (i = 0; i < adapter->req_rx_queues; i++)
1844 napi_schedule(&adapter->napi[i]);
1846 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1847 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1848 netdev_notify_peers(netdev);
1850 netif_carrier_on(netdev);
1855 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1857 struct ibmvnic_rwi *rwi;
1859 mutex_lock(&adapter->rwi_lock);
1861 if (!list_empty(&adapter->rwi_list)) {
1862 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1864 list_del(&rwi->list);
1869 mutex_unlock(&adapter->rwi_lock);
1873 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1875 struct ibmvnic_rwi *rwi;
1877 rwi = get_next_rwi(adapter);
1880 rwi = get_next_rwi(adapter);
1884 static void __ibmvnic_reset(struct work_struct *work)
1886 struct ibmvnic_rwi *rwi;
1887 struct ibmvnic_adapter *adapter;
1888 struct net_device *netdev;
1892 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1893 netdev = adapter->netdev;
1895 mutex_lock(&adapter->reset_lock);
1896 adapter->resetting = true;
1897 reset_state = adapter->state;
1899 rwi = get_next_rwi(adapter);
1901 rc = do_reset(adapter, rwi, reset_state);
1903 if (rc && rc != IBMVNIC_INIT_FAILED)
1906 rwi = get_next_rwi(adapter);
1909 if (adapter->wait_for_reset) {
1910 adapter->wait_for_reset = false;
1911 adapter->reset_done_rc = rc;
1912 complete(&adapter->reset_done);
1916 netdev_dbg(adapter->netdev, "Reset failed\n");
1917 free_all_rwi(adapter);
1918 mutex_unlock(&adapter->reset_lock);
1922 adapter->resetting = false;
1923 mutex_unlock(&adapter->reset_lock);
1926 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
1927 enum ibmvnic_reset_reason reason)
1929 struct ibmvnic_rwi *rwi, *tmp;
1930 struct net_device *netdev = adapter->netdev;
1931 struct list_head *entry;
1934 if (adapter->state == VNIC_REMOVING ||
1935 adapter->state == VNIC_REMOVED ||
1936 adapter->failover_pending) {
1938 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
1942 if (adapter->state == VNIC_PROBING) {
1943 netdev_warn(netdev, "Adapter reset during probe\n");
1944 ret = adapter->init_done_rc = EAGAIN;
1948 mutex_lock(&adapter->rwi_lock);
1950 list_for_each(entry, &adapter->rwi_list) {
1951 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1952 if (tmp->reset_reason == reason) {
1953 netdev_dbg(netdev, "Skipping matching reset\n");
1954 mutex_unlock(&adapter->rwi_lock);
1960 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1962 mutex_unlock(&adapter->rwi_lock);
1963 ibmvnic_close(netdev);
1968 rwi->reset_reason = reason;
1969 list_add_tail(&rwi->list, &adapter->rwi_list);
1970 mutex_unlock(&adapter->rwi_lock);
1972 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1973 schedule_work(&adapter->ibmvnic_reset);
1977 if (adapter->wait_for_reset)
1978 adapter->wait_for_reset = false;
1982 static void ibmvnic_tx_timeout(struct net_device *dev)
1984 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1986 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1989 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1990 struct ibmvnic_rx_buff *rx_buff)
1992 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1994 rx_buff->skb = NULL;
1996 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1997 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1999 atomic_dec(&pool->available);
2002 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2004 struct net_device *netdev = napi->dev;
2005 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2006 int scrq_num = (int)(napi - adapter->napi);
2007 int frames_processed = 0;
2010 while (frames_processed < budget) {
2011 struct sk_buff *skb;
2012 struct ibmvnic_rx_buff *rx_buff;
2013 union sub_crq *next;
2018 if (unlikely(adapter->resetting &&
2019 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2020 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2021 napi_complete_done(napi, frames_processed);
2022 return frames_processed;
2025 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2027 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2029 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2030 rx_comp.correlator);
2031 /* do error checking */
2032 if (next->rx_comp.rc) {
2033 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2034 be16_to_cpu(next->rx_comp.rc));
2035 /* free the entry */
2036 next->rx_comp.first = 0;
2037 dev_kfree_skb_any(rx_buff->skb);
2038 remove_buff_from_pool(adapter, rx_buff);
2040 } else if (!rx_buff->skb) {
2041 /* free the entry */
2042 next->rx_comp.first = 0;
2043 remove_buff_from_pool(adapter, rx_buff);
2047 length = be32_to_cpu(next->rx_comp.len);
2048 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2049 flags = next->rx_comp.flags;
2051 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2054 /* VLAN Header has been stripped by the system firmware and
2055 * needs to be inserted by the driver
2057 if (adapter->rx_vlan_header_insertion &&
2058 (flags & IBMVNIC_VLAN_STRIPPED))
2059 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2060 ntohs(next->rx_comp.vlan_tci));
2062 /* free the entry */
2063 next->rx_comp.first = 0;
2064 remove_buff_from_pool(adapter, rx_buff);
2066 skb_put(skb, length);
2067 skb->protocol = eth_type_trans(skb, netdev);
2068 skb_record_rx_queue(skb, scrq_num);
2070 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2071 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2072 skb->ip_summed = CHECKSUM_UNNECESSARY;
2076 napi_gro_receive(napi, skb); /* send it up */
2077 netdev->stats.rx_packets++;
2078 netdev->stats.rx_bytes += length;
2079 adapter->rx_stats_buffers[scrq_num].packets++;
2080 adapter->rx_stats_buffers[scrq_num].bytes += length;
2084 if (adapter->state != VNIC_CLOSING)
2085 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2087 if (frames_processed < budget) {
2088 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2089 napi_complete_done(napi, frames_processed);
2090 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2091 napi_reschedule(napi)) {
2092 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2096 return frames_processed;
2099 #ifdef CONFIG_NET_POLL_CONTROLLER
2100 static void ibmvnic_netpoll_controller(struct net_device *dev)
2102 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2105 replenish_pools(netdev_priv(dev));
2106 for (i = 0; i < adapter->req_rx_queues; i++)
2107 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2108 adapter->rx_scrq[i]);
2112 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2116 adapter->fallback.mtu = adapter->req_mtu;
2117 adapter->fallback.rx_queues = adapter->req_rx_queues;
2118 adapter->fallback.tx_queues = adapter->req_tx_queues;
2119 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2120 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2122 init_completion(&adapter->reset_done);
2123 adapter->wait_for_reset = true;
2124 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2127 wait_for_completion(&adapter->reset_done);
2130 if (adapter->reset_done_rc) {
2132 adapter->desired.mtu = adapter->fallback.mtu;
2133 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2134 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2135 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2136 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2138 init_completion(&adapter->reset_done);
2139 adapter->wait_for_reset = true;
2140 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2143 wait_for_completion(&adapter->reset_done);
2145 adapter->wait_for_reset = false;
2150 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2152 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2154 adapter->desired.mtu = new_mtu + ETH_HLEN;
2156 return wait_for_reset(adapter);
2159 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2160 struct net_device *dev,
2161 netdev_features_t features)
2163 /* Some backing hardware adapters can not
2164 * handle packets with a MSS less than 224
2165 * or with only one segment.
2167 if (skb_is_gso(skb)) {
2168 if (skb_shinfo(skb)->gso_size < 224 ||
2169 skb_shinfo(skb)->gso_segs == 1)
2170 features &= ~NETIF_F_GSO_MASK;
2176 static const struct net_device_ops ibmvnic_netdev_ops = {
2177 .ndo_open = ibmvnic_open,
2178 .ndo_stop = ibmvnic_close,
2179 .ndo_start_xmit = ibmvnic_xmit,
2180 .ndo_set_rx_mode = ibmvnic_set_multi,
2181 .ndo_set_mac_address = ibmvnic_set_mac,
2182 .ndo_validate_addr = eth_validate_addr,
2183 .ndo_tx_timeout = ibmvnic_tx_timeout,
2184 #ifdef CONFIG_NET_POLL_CONTROLLER
2185 .ndo_poll_controller = ibmvnic_netpoll_controller,
2187 .ndo_change_mtu = ibmvnic_change_mtu,
2188 .ndo_features_check = ibmvnic_features_check,
2191 /* ethtool functions */
2193 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2194 struct ethtool_link_ksettings *cmd)
2196 u32 supported, advertising;
2198 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2200 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2202 cmd->base.speed = SPEED_1000;
2203 cmd->base.duplex = DUPLEX_FULL;
2204 cmd->base.port = PORT_FIBRE;
2205 cmd->base.phy_address = 0;
2206 cmd->base.autoneg = AUTONEG_ENABLE;
2208 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2210 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2216 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2217 struct ethtool_drvinfo *info)
2219 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2221 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2222 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2223 strlcpy(info->fw_version, adapter->fw_version,
2224 sizeof(info->fw_version));
2227 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2229 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2231 return adapter->msg_enable;
2234 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2236 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2238 adapter->msg_enable = data;
2241 static u32 ibmvnic_get_link(struct net_device *netdev)
2243 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2245 /* Don't need to send a query because we request a logical link up at
2246 * init and then we wait for link state indications
2248 return adapter->logical_link_state;
2251 static void ibmvnic_get_ringparam(struct net_device *netdev,
2252 struct ethtool_ringparam *ring)
2254 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2256 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2257 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2258 ring->rx_mini_max_pending = 0;
2259 ring->rx_jumbo_max_pending = 0;
2260 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2261 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2262 ring->rx_mini_pending = 0;
2263 ring->rx_jumbo_pending = 0;
2266 static int ibmvnic_set_ringparam(struct net_device *netdev,
2267 struct ethtool_ringparam *ring)
2269 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2271 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2272 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2273 netdev_err(netdev, "Invalid request.\n");
2274 netdev_err(netdev, "Max tx buffers = %llu\n",
2275 adapter->max_rx_add_entries_per_subcrq);
2276 netdev_err(netdev, "Max rx buffers = %llu\n",
2277 adapter->max_tx_entries_per_subcrq);
2281 adapter->desired.rx_entries = ring->rx_pending;
2282 adapter->desired.tx_entries = ring->tx_pending;
2284 return wait_for_reset(adapter);
2287 static void ibmvnic_get_channels(struct net_device *netdev,
2288 struct ethtool_channels *channels)
2290 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2292 channels->max_rx = adapter->max_rx_queues;
2293 channels->max_tx = adapter->max_tx_queues;
2294 channels->max_other = 0;
2295 channels->max_combined = 0;
2296 channels->rx_count = adapter->req_rx_queues;
2297 channels->tx_count = adapter->req_tx_queues;
2298 channels->other_count = 0;
2299 channels->combined_count = 0;
2302 static int ibmvnic_set_channels(struct net_device *netdev,
2303 struct ethtool_channels *channels)
2305 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2307 adapter->desired.rx_queues = channels->rx_count;
2308 adapter->desired.tx_queues = channels->tx_count;
2310 return wait_for_reset(adapter);
2313 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2315 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2318 if (stringset != ETH_SS_STATS)
2321 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2322 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2324 for (i = 0; i < adapter->req_tx_queues; i++) {
2325 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2326 data += ETH_GSTRING_LEN;
2328 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2329 data += ETH_GSTRING_LEN;
2331 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2332 data += ETH_GSTRING_LEN;
2335 for (i = 0; i < adapter->req_rx_queues; i++) {
2336 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2337 data += ETH_GSTRING_LEN;
2339 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2340 data += ETH_GSTRING_LEN;
2342 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2343 data += ETH_GSTRING_LEN;
2347 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2349 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2353 return ARRAY_SIZE(ibmvnic_stats) +
2354 adapter->req_tx_queues * NUM_TX_STATS +
2355 adapter->req_rx_queues * NUM_RX_STATS;
2361 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2362 struct ethtool_stats *stats, u64 *data)
2364 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2365 union ibmvnic_crq crq;
2368 memset(&crq, 0, sizeof(crq));
2369 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2370 crq.request_statistics.cmd = REQUEST_STATISTICS;
2371 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2372 crq.request_statistics.len =
2373 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2375 /* Wait for data to be written */
2376 init_completion(&adapter->stats_done);
2377 ibmvnic_send_crq(adapter, &crq);
2378 wait_for_completion(&adapter->stats_done);
2380 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2381 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2382 ibmvnic_stats[i].offset));
2384 for (j = 0; j < adapter->req_tx_queues; j++) {
2385 data[i] = adapter->tx_stats_buffers[j].packets;
2387 data[i] = adapter->tx_stats_buffers[j].bytes;
2389 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2393 for (j = 0; j < adapter->req_rx_queues; j++) {
2394 data[i] = adapter->rx_stats_buffers[j].packets;
2396 data[i] = adapter->rx_stats_buffers[j].bytes;
2398 data[i] = adapter->rx_stats_buffers[j].interrupts;
2403 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2404 .get_drvinfo = ibmvnic_get_drvinfo,
2405 .get_msglevel = ibmvnic_get_msglevel,
2406 .set_msglevel = ibmvnic_set_msglevel,
2407 .get_link = ibmvnic_get_link,
2408 .get_ringparam = ibmvnic_get_ringparam,
2409 .set_ringparam = ibmvnic_set_ringparam,
2410 .get_channels = ibmvnic_get_channels,
2411 .set_channels = ibmvnic_set_channels,
2412 .get_strings = ibmvnic_get_strings,
2413 .get_sset_count = ibmvnic_get_sset_count,
2414 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2415 .get_link_ksettings = ibmvnic_get_link_ksettings,
2418 /* Routines for managing CRQs/sCRQs */
2420 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2421 struct ibmvnic_sub_crq_queue *scrq)
2426 free_irq(scrq->irq, scrq);
2427 irq_dispose_mapping(scrq->irq);
2431 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2432 atomic_set(&scrq->used, 0);
2435 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2436 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2440 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2444 for (i = 0; i < adapter->req_tx_queues; i++) {
2445 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2446 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2451 for (i = 0; i < adapter->req_rx_queues; i++) {
2452 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2453 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2461 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2462 struct ibmvnic_sub_crq_queue *scrq,
2465 struct device *dev = &adapter->vdev->dev;
2468 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2471 /* Close the sub-crqs */
2473 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2474 adapter->vdev->unit_address,
2476 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2479 netdev_err(adapter->netdev,
2480 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2485 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2487 free_pages((unsigned long)scrq->msgs, 2);
2491 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2494 struct device *dev = &adapter->vdev->dev;
2495 struct ibmvnic_sub_crq_queue *scrq;
2498 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2503 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2505 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2506 goto zero_page_failed;
2509 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2511 if (dma_mapping_error(dev, scrq->msg_token)) {
2512 dev_warn(dev, "Couldn't map crq queue messages page\n");
2516 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2517 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2519 if (rc == H_RESOURCE)
2520 rc = ibmvnic_reset_crq(adapter);
2522 if (rc == H_CLOSED) {
2523 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2525 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2529 scrq->adapter = adapter;
2530 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2531 spin_lock_init(&scrq->lock);
2533 netdev_dbg(adapter->netdev,
2534 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2535 scrq->crq_num, scrq->hw_irq, scrq->irq);
2540 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2543 free_pages((unsigned long)scrq->msgs, 2);
2550 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2554 if (adapter->tx_scrq) {
2555 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2556 if (!adapter->tx_scrq[i])
2559 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2561 if (adapter->tx_scrq[i]->irq) {
2562 free_irq(adapter->tx_scrq[i]->irq,
2563 adapter->tx_scrq[i]);
2564 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2565 adapter->tx_scrq[i]->irq = 0;
2568 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2572 kfree(adapter->tx_scrq);
2573 adapter->tx_scrq = NULL;
2574 adapter->num_active_tx_scrqs = 0;
2577 if (adapter->rx_scrq) {
2578 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2579 if (!adapter->rx_scrq[i])
2582 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2584 if (adapter->rx_scrq[i]->irq) {
2585 free_irq(adapter->rx_scrq[i]->irq,
2586 adapter->rx_scrq[i]);
2587 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2588 adapter->rx_scrq[i]->irq = 0;
2591 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2595 kfree(adapter->rx_scrq);
2596 adapter->rx_scrq = NULL;
2597 adapter->num_active_rx_scrqs = 0;
2601 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2602 struct ibmvnic_sub_crq_queue *scrq)
2604 struct device *dev = &adapter->vdev->dev;
2607 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2608 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2610 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2615 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2616 struct ibmvnic_sub_crq_queue *scrq)
2618 struct device *dev = &adapter->vdev->dev;
2622 if (scrq->hw_irq > 0x100000000ULL) {
2623 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2627 val = (0xff000000) | scrq->hw_irq;
2628 rc = plpar_hcall_norets(H_EOI, val);
2630 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2633 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2634 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2636 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2641 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2642 struct ibmvnic_sub_crq_queue *scrq)
2644 struct device *dev = &adapter->vdev->dev;
2645 struct ibmvnic_tx_pool *tx_pool;
2646 struct ibmvnic_tx_buff *txbuff;
2647 union sub_crq *next;
2653 while (pending_scrq(adapter, scrq)) {
2654 unsigned int pool = scrq->pool_index;
2655 int num_entries = 0;
2657 next = ibmvnic_next_scrq(adapter, scrq);
2658 for (i = 0; i < next->tx_comp.num_comps; i++) {
2659 if (next->tx_comp.rcs[i]) {
2660 dev_err(dev, "tx error %x\n",
2661 next->tx_comp.rcs[i]);
2664 index = be32_to_cpu(next->tx_comp.correlators[i]);
2665 if (index & IBMVNIC_TSO_POOL_MASK) {
2666 tx_pool = &adapter->tso_pool[pool];
2667 index &= ~IBMVNIC_TSO_POOL_MASK;
2669 tx_pool = &adapter->tx_pool[pool];
2672 txbuff = &tx_pool->tx_buff[index];
2674 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2675 if (!txbuff->data_dma[j])
2678 txbuff->data_dma[j] = 0;
2680 /* if sub_crq was sent indirectly */
2681 first = &txbuff->indir_arr[0].generic.first;
2682 if (*first == IBMVNIC_CRQ_CMD) {
2683 dma_unmap_single(dev, txbuff->indir_dma,
2684 sizeof(txbuff->indir_arr),
2689 if (txbuff->last_frag) {
2690 dev_kfree_skb_any(txbuff->skb);
2694 num_entries += txbuff->num_entries;
2696 tx_pool->free_map[tx_pool->producer_index] = index;
2697 tx_pool->producer_index =
2698 (tx_pool->producer_index + 1) %
2699 tx_pool->num_buffers;
2701 /* remove tx_comp scrq*/
2702 next->tx_comp.first = 0;
2704 if (atomic_sub_return(num_entries, &scrq->used) <=
2705 (adapter->req_tx_entries_per_subcrq / 2) &&
2706 __netif_subqueue_stopped(adapter->netdev,
2707 scrq->pool_index)) {
2708 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2709 netdev_dbg(adapter->netdev, "Started queue %d\n",
2714 enable_scrq_irq(adapter, scrq);
2716 if (pending_scrq(adapter, scrq)) {
2717 disable_scrq_irq(adapter, scrq);
2724 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2726 struct ibmvnic_sub_crq_queue *scrq = instance;
2727 struct ibmvnic_adapter *adapter = scrq->adapter;
2729 disable_scrq_irq(adapter, scrq);
2730 ibmvnic_complete_tx(adapter, scrq);
2735 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2737 struct ibmvnic_sub_crq_queue *scrq = instance;
2738 struct ibmvnic_adapter *adapter = scrq->adapter;
2740 /* When booting a kdump kernel we can hit pending interrupts
2741 * prior to completing driver initialization.
2743 if (unlikely(adapter->state != VNIC_OPEN))
2746 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2748 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2749 disable_scrq_irq(adapter, scrq);
2750 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2756 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2758 struct device *dev = &adapter->vdev->dev;
2759 struct ibmvnic_sub_crq_queue *scrq;
2763 for (i = 0; i < adapter->req_tx_queues; i++) {
2764 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2766 scrq = adapter->tx_scrq[i];
2767 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2771 dev_err(dev, "Error mapping irq\n");
2772 goto req_tx_irq_failed;
2775 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2776 0, "ibmvnic_tx", scrq);
2779 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2781 irq_dispose_mapping(scrq->irq);
2782 goto req_tx_irq_failed;
2786 for (i = 0; i < adapter->req_rx_queues; i++) {
2787 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2789 scrq = adapter->rx_scrq[i];
2790 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2793 dev_err(dev, "Error mapping irq\n");
2794 goto req_rx_irq_failed;
2796 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2797 0, "ibmvnic_rx", scrq);
2799 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2801 irq_dispose_mapping(scrq->irq);
2802 goto req_rx_irq_failed;
2808 for (j = 0; j < i; j++) {
2809 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2810 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2812 i = adapter->req_tx_queues;
2814 for (j = 0; j < i; j++) {
2815 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2816 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2818 release_sub_crqs(adapter, 1);
2822 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2824 struct device *dev = &adapter->vdev->dev;
2825 struct ibmvnic_sub_crq_queue **allqueues;
2826 int registered_queues = 0;
2831 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2833 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2837 for (i = 0; i < total_queues; i++) {
2838 allqueues[i] = init_sub_crq_queue(adapter);
2839 if (!allqueues[i]) {
2840 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2843 registered_queues++;
2846 /* Make sure we were able to register the minimum number of queues */
2847 if (registered_queues <
2848 adapter->min_tx_queues + adapter->min_rx_queues) {
2849 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2853 /* Distribute the failed allocated queues*/
2854 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2855 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2858 if (adapter->req_rx_queues > adapter->min_rx_queues)
2859 adapter->req_rx_queues--;
2864 if (adapter->req_tx_queues > adapter->min_tx_queues)
2865 adapter->req_tx_queues--;
2872 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2873 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2874 if (!adapter->tx_scrq)
2877 for (i = 0; i < adapter->req_tx_queues; i++) {
2878 adapter->tx_scrq[i] = allqueues[i];
2879 adapter->tx_scrq[i]->pool_index = i;
2880 adapter->num_active_tx_scrqs++;
2883 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2884 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2885 if (!adapter->rx_scrq)
2888 for (i = 0; i < adapter->req_rx_queues; i++) {
2889 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2890 adapter->rx_scrq[i]->scrq_num = i;
2891 adapter->num_active_rx_scrqs++;
2898 kfree(adapter->tx_scrq);
2899 adapter->tx_scrq = NULL;
2901 for (i = 0; i < registered_queues; i++)
2902 release_sub_crq_queue(adapter, allqueues[i], 1);
2907 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2909 struct device *dev = &adapter->vdev->dev;
2910 union ibmvnic_crq crq;
2914 /* Sub-CRQ entries are 32 byte long */
2915 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2917 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2918 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2919 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2923 if (adapter->desired.mtu)
2924 adapter->req_mtu = adapter->desired.mtu;
2926 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2928 if (!adapter->desired.tx_entries)
2929 adapter->desired.tx_entries =
2930 adapter->max_tx_entries_per_subcrq;
2931 if (!adapter->desired.rx_entries)
2932 adapter->desired.rx_entries =
2933 adapter->max_rx_add_entries_per_subcrq;
2935 max_entries = IBMVNIC_MAX_LTB_SIZE /
2936 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2938 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2939 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2940 adapter->desired.tx_entries = max_entries;
2943 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2944 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2945 adapter->desired.rx_entries = max_entries;
2948 if (adapter->desired.tx_entries)
2949 adapter->req_tx_entries_per_subcrq =
2950 adapter->desired.tx_entries;
2952 adapter->req_tx_entries_per_subcrq =
2953 adapter->max_tx_entries_per_subcrq;
2955 if (adapter->desired.rx_entries)
2956 adapter->req_rx_add_entries_per_subcrq =
2957 adapter->desired.rx_entries;
2959 adapter->req_rx_add_entries_per_subcrq =
2960 adapter->max_rx_add_entries_per_subcrq;
2962 if (adapter->desired.tx_queues)
2963 adapter->req_tx_queues =
2964 adapter->desired.tx_queues;
2966 adapter->req_tx_queues =
2967 adapter->opt_tx_comp_sub_queues;
2969 if (adapter->desired.rx_queues)
2970 adapter->req_rx_queues =
2971 adapter->desired.rx_queues;
2973 adapter->req_rx_queues =
2974 adapter->opt_rx_comp_queues;
2976 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2979 memset(&crq, 0, sizeof(crq));
2980 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2981 crq.request_capability.cmd = REQUEST_CAPABILITY;
2983 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2984 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2985 atomic_inc(&adapter->running_cap_crqs);
2986 ibmvnic_send_crq(adapter, &crq);
2988 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2989 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2990 atomic_inc(&adapter->running_cap_crqs);
2991 ibmvnic_send_crq(adapter, &crq);
2993 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2994 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2995 atomic_inc(&adapter->running_cap_crqs);
2996 ibmvnic_send_crq(adapter, &crq);
2998 crq.request_capability.capability =
2999 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3000 crq.request_capability.number =
3001 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3002 atomic_inc(&adapter->running_cap_crqs);
3003 ibmvnic_send_crq(adapter, &crq);
3005 crq.request_capability.capability =
3006 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3007 crq.request_capability.number =
3008 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3009 atomic_inc(&adapter->running_cap_crqs);
3010 ibmvnic_send_crq(adapter, &crq);
3012 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3013 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3014 atomic_inc(&adapter->running_cap_crqs);
3015 ibmvnic_send_crq(adapter, &crq);
3017 if (adapter->netdev->flags & IFF_PROMISC) {
3018 if (adapter->promisc_supported) {
3019 crq.request_capability.capability =
3020 cpu_to_be16(PROMISC_REQUESTED);
3021 crq.request_capability.number = cpu_to_be64(1);
3022 atomic_inc(&adapter->running_cap_crqs);
3023 ibmvnic_send_crq(adapter, &crq);
3026 crq.request_capability.capability =
3027 cpu_to_be16(PROMISC_REQUESTED);
3028 crq.request_capability.number = cpu_to_be64(0);
3029 atomic_inc(&adapter->running_cap_crqs);
3030 ibmvnic_send_crq(adapter, &crq);
3034 static int pending_scrq(struct ibmvnic_adapter *adapter,
3035 struct ibmvnic_sub_crq_queue *scrq)
3037 union sub_crq *entry = &scrq->msgs[scrq->cur];
3039 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3045 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3046 struct ibmvnic_sub_crq_queue *scrq)
3048 union sub_crq *entry;
3049 unsigned long flags;
3051 spin_lock_irqsave(&scrq->lock, flags);
3052 entry = &scrq->msgs[scrq->cur];
3053 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3054 if (++scrq->cur == scrq->size)
3059 spin_unlock_irqrestore(&scrq->lock, flags);
3064 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3066 struct ibmvnic_crq_queue *queue = &adapter->crq;
3067 union ibmvnic_crq *crq;
3069 crq = &queue->msgs[queue->cur];
3070 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3071 if (++queue->cur == queue->size)
3080 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3081 union sub_crq *sub_crq)
3083 unsigned int ua = adapter->vdev->unit_address;
3084 struct device *dev = &adapter->vdev->dev;
3085 u64 *u64_crq = (u64 *)sub_crq;
3088 netdev_dbg(adapter->netdev,
3089 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3090 (unsigned long int)cpu_to_be64(remote_handle),
3091 (unsigned long int)cpu_to_be64(u64_crq[0]),
3092 (unsigned long int)cpu_to_be64(u64_crq[1]),
3093 (unsigned long int)cpu_to_be64(u64_crq[2]),
3094 (unsigned long int)cpu_to_be64(u64_crq[3]));
3096 /* Make sure the hypervisor sees the complete request */
3099 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3100 cpu_to_be64(remote_handle),
3101 cpu_to_be64(u64_crq[0]),
3102 cpu_to_be64(u64_crq[1]),
3103 cpu_to_be64(u64_crq[2]),
3104 cpu_to_be64(u64_crq[3]));
3108 dev_warn(dev, "CRQ Queue closed\n");
3109 dev_err(dev, "Send error (rc=%d)\n", rc);
3115 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3116 u64 remote_handle, u64 ioba, u64 num_entries)
3118 unsigned int ua = adapter->vdev->unit_address;
3119 struct device *dev = &adapter->vdev->dev;
3122 /* Make sure the hypervisor sees the complete request */
3124 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3125 cpu_to_be64(remote_handle),
3130 dev_warn(dev, "CRQ Queue closed\n");
3131 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3137 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3138 union ibmvnic_crq *crq)
3140 unsigned int ua = adapter->vdev->unit_address;
3141 struct device *dev = &adapter->vdev->dev;
3142 u64 *u64_crq = (u64 *)crq;
3145 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3146 (unsigned long int)cpu_to_be64(u64_crq[0]),
3147 (unsigned long int)cpu_to_be64(u64_crq[1]));
3149 /* Make sure the hypervisor sees the complete request */
3152 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3153 cpu_to_be64(u64_crq[0]),
3154 cpu_to_be64(u64_crq[1]));
3157 if (rc == H_CLOSED) {
3158 dev_warn(dev, "CRQ Queue closed\n");
3159 if (adapter->resetting)
3160 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3163 dev_warn(dev, "Send error (rc=%d)\n", rc);
3169 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3171 union ibmvnic_crq crq;
3173 memset(&crq, 0, sizeof(crq));
3174 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3175 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3176 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3178 return ibmvnic_send_crq(adapter, &crq);
3181 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3183 union ibmvnic_crq crq;
3185 memset(&crq, 0, sizeof(crq));
3186 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3187 crq.version_exchange.cmd = VERSION_EXCHANGE;
3188 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3190 return ibmvnic_send_crq(adapter, &crq);
3193 struct vnic_login_client_data {
3199 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3203 /* Calculate the amount of buffer space needed for the
3204 * vnic client data in the login buffer. There are four entries,
3205 * OS name, LPAR name, device name, and a null last entry.
3207 len = 4 * sizeof(struct vnic_login_client_data);
3208 len += 6; /* "Linux" plus NULL */
3209 len += strlen(utsname()->nodename) + 1;
3210 len += strlen(adapter->netdev->name) + 1;
3215 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3216 struct vnic_login_client_data *vlcd)
3218 const char *os_name = "Linux";
3221 /* Type 1 - LPAR OS */
3223 len = strlen(os_name) + 1;
3224 vlcd->len = cpu_to_be16(len);
3225 strncpy(vlcd->name, os_name, len);
3226 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3228 /* Type 2 - LPAR name */
3230 len = strlen(utsname()->nodename) + 1;
3231 vlcd->len = cpu_to_be16(len);
3232 strncpy(vlcd->name, utsname()->nodename, len);
3233 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3235 /* Type 3 - device name */
3237 len = strlen(adapter->netdev->name) + 1;
3238 vlcd->len = cpu_to_be16(len);
3239 strncpy(vlcd->name, adapter->netdev->name, len);
3242 static int send_login(struct ibmvnic_adapter *adapter)
3244 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3245 struct ibmvnic_login_buffer *login_buffer;
3246 struct device *dev = &adapter->vdev->dev;
3247 dma_addr_t rsp_buffer_token;
3248 dma_addr_t buffer_token;
3249 size_t rsp_buffer_size;
3250 union ibmvnic_crq crq;
3254 int client_data_len;
3255 struct vnic_login_client_data *vlcd;
3258 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3259 netdev_err(adapter->netdev,
3260 "RX or TX queues are not allocated, device login failed\n");
3264 release_login_rsp_buffer(adapter);
3265 client_data_len = vnic_client_data_len(adapter);
3268 sizeof(struct ibmvnic_login_buffer) +
3269 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3272 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3274 goto buf_alloc_failed;
3276 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3278 if (dma_mapping_error(dev, buffer_token)) {
3279 dev_err(dev, "Couldn't map login buffer\n");
3280 goto buf_map_failed;
3283 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3284 sizeof(u64) * adapter->req_tx_queues +
3285 sizeof(u64) * adapter->req_rx_queues +
3286 sizeof(u64) * adapter->req_rx_queues +
3287 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3289 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3290 if (!login_rsp_buffer)
3291 goto buf_rsp_alloc_failed;
3293 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3294 rsp_buffer_size, DMA_FROM_DEVICE);
3295 if (dma_mapping_error(dev, rsp_buffer_token)) {
3296 dev_err(dev, "Couldn't map login rsp buffer\n");
3297 goto buf_rsp_map_failed;
3300 adapter->login_buf = login_buffer;
3301 adapter->login_buf_token = buffer_token;
3302 adapter->login_buf_sz = buffer_size;
3303 adapter->login_rsp_buf = login_rsp_buffer;
3304 adapter->login_rsp_buf_token = rsp_buffer_token;
3305 adapter->login_rsp_buf_sz = rsp_buffer_size;
3307 login_buffer->len = cpu_to_be32(buffer_size);
3308 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3309 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3310 login_buffer->off_txcomp_subcrqs =
3311 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3312 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3313 login_buffer->off_rxcomp_subcrqs =
3314 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3315 sizeof(u64) * adapter->req_tx_queues);
3316 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3317 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3319 tx_list_p = (__be64 *)((char *)login_buffer +
3320 sizeof(struct ibmvnic_login_buffer));
3321 rx_list_p = (__be64 *)((char *)login_buffer +
3322 sizeof(struct ibmvnic_login_buffer) +
3323 sizeof(u64) * adapter->req_tx_queues);
3325 for (i = 0; i < adapter->req_tx_queues; i++) {
3326 if (adapter->tx_scrq[i]) {
3327 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3332 for (i = 0; i < adapter->req_rx_queues; i++) {
3333 if (adapter->rx_scrq[i]) {
3334 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3339 /* Insert vNIC login client data */
3340 vlcd = (struct vnic_login_client_data *)
3341 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3342 login_buffer->client_data_offset =
3343 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3344 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3346 vnic_add_client_data(adapter, vlcd);
3348 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3349 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3350 netdev_dbg(adapter->netdev, "%016lx\n",
3351 ((unsigned long int *)(adapter->login_buf))[i]);
3354 memset(&crq, 0, sizeof(crq));
3355 crq.login.first = IBMVNIC_CRQ_CMD;
3356 crq.login.cmd = LOGIN;
3357 crq.login.ioba = cpu_to_be32(buffer_token);
3358 crq.login.len = cpu_to_be32(buffer_size);
3359 ibmvnic_send_crq(adapter, &crq);
3364 kfree(login_rsp_buffer);
3365 buf_rsp_alloc_failed:
3366 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3368 kfree(login_buffer);
3373 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3376 union ibmvnic_crq crq;
3378 memset(&crq, 0, sizeof(crq));
3379 crq.request_map.first = IBMVNIC_CRQ_CMD;
3380 crq.request_map.cmd = REQUEST_MAP;
3381 crq.request_map.map_id = map_id;
3382 crq.request_map.ioba = cpu_to_be32(addr);
3383 crq.request_map.len = cpu_to_be32(len);
3384 ibmvnic_send_crq(adapter, &crq);
3387 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3389 union ibmvnic_crq crq;
3391 memset(&crq, 0, sizeof(crq));
3392 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3393 crq.request_unmap.cmd = REQUEST_UNMAP;
3394 crq.request_unmap.map_id = map_id;
3395 ibmvnic_send_crq(adapter, &crq);
3398 static void send_map_query(struct ibmvnic_adapter *adapter)
3400 union ibmvnic_crq crq;
3402 memset(&crq, 0, sizeof(crq));
3403 crq.query_map.first = IBMVNIC_CRQ_CMD;
3404 crq.query_map.cmd = QUERY_MAP;
3405 ibmvnic_send_crq(adapter, &crq);
3408 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3409 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3411 union ibmvnic_crq crq;
3413 atomic_set(&adapter->running_cap_crqs, 0);
3414 memset(&crq, 0, sizeof(crq));
3415 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3416 crq.query_capability.cmd = QUERY_CAPABILITY;
3418 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3419 atomic_inc(&adapter->running_cap_crqs);
3420 ibmvnic_send_crq(adapter, &crq);
3422 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3423 atomic_inc(&adapter->running_cap_crqs);
3424 ibmvnic_send_crq(adapter, &crq);
3426 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3427 atomic_inc(&adapter->running_cap_crqs);
3428 ibmvnic_send_crq(adapter, &crq);
3430 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3431 atomic_inc(&adapter->running_cap_crqs);
3432 ibmvnic_send_crq(adapter, &crq);
3434 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3435 atomic_inc(&adapter->running_cap_crqs);
3436 ibmvnic_send_crq(adapter, &crq);
3438 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3439 atomic_inc(&adapter->running_cap_crqs);
3440 ibmvnic_send_crq(adapter, &crq);
3442 crq.query_capability.capability =
3443 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3444 atomic_inc(&adapter->running_cap_crqs);
3445 ibmvnic_send_crq(adapter, &crq);
3447 crq.query_capability.capability =
3448 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3449 atomic_inc(&adapter->running_cap_crqs);
3450 ibmvnic_send_crq(adapter, &crq);
3452 crq.query_capability.capability =
3453 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3454 atomic_inc(&adapter->running_cap_crqs);
3455 ibmvnic_send_crq(adapter, &crq);
3457 crq.query_capability.capability =
3458 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3459 atomic_inc(&adapter->running_cap_crqs);
3460 ibmvnic_send_crq(adapter, &crq);
3462 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3463 atomic_inc(&adapter->running_cap_crqs);
3464 ibmvnic_send_crq(adapter, &crq);
3466 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3467 atomic_inc(&adapter->running_cap_crqs);
3468 ibmvnic_send_crq(adapter, &crq);
3470 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3471 atomic_inc(&adapter->running_cap_crqs);
3472 ibmvnic_send_crq(adapter, &crq);
3474 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3475 atomic_inc(&adapter->running_cap_crqs);
3476 ibmvnic_send_crq(adapter, &crq);
3478 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3479 atomic_inc(&adapter->running_cap_crqs);
3480 ibmvnic_send_crq(adapter, &crq);
3482 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3483 atomic_inc(&adapter->running_cap_crqs);
3484 ibmvnic_send_crq(adapter, &crq);
3486 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3487 atomic_inc(&adapter->running_cap_crqs);
3488 ibmvnic_send_crq(adapter, &crq);
3490 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3491 atomic_inc(&adapter->running_cap_crqs);
3492 ibmvnic_send_crq(adapter, &crq);
3494 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3495 atomic_inc(&adapter->running_cap_crqs);
3496 ibmvnic_send_crq(adapter, &crq);
3498 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3499 atomic_inc(&adapter->running_cap_crqs);
3500 ibmvnic_send_crq(adapter, &crq);
3502 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3503 atomic_inc(&adapter->running_cap_crqs);
3504 ibmvnic_send_crq(adapter, &crq);
3506 crq.query_capability.capability =
3507 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3508 atomic_inc(&adapter->running_cap_crqs);
3509 ibmvnic_send_crq(adapter, &crq);
3511 crq.query_capability.capability =
3512 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3513 atomic_inc(&adapter->running_cap_crqs);
3514 ibmvnic_send_crq(adapter, &crq);
3516 crq.query_capability.capability =
3517 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3518 atomic_inc(&adapter->running_cap_crqs);
3519 ibmvnic_send_crq(adapter, &crq);
3521 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3522 atomic_inc(&adapter->running_cap_crqs);
3523 ibmvnic_send_crq(adapter, &crq);
3526 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3527 struct ibmvnic_adapter *adapter)
3529 struct device *dev = &adapter->vdev->dev;
3531 if (crq->get_vpd_size_rsp.rc.code) {
3532 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3533 crq->get_vpd_size_rsp.rc.code);
3534 complete(&adapter->fw_done);
3538 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3539 complete(&adapter->fw_done);
3542 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3543 struct ibmvnic_adapter *adapter)
3545 struct device *dev = &adapter->vdev->dev;
3546 unsigned char *substr = NULL;
3547 u8 fw_level_len = 0;
3549 memset(adapter->fw_version, 0, 32);
3551 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3554 if (crq->get_vpd_rsp.rc.code) {
3555 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3556 crq->get_vpd_rsp.rc.code);
3560 /* get the position of the firmware version info
3561 * located after the ASCII 'RM' substring in the buffer
3563 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3565 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3569 /* get length of firmware level ASCII substring */
3570 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3571 fw_level_len = *(substr + 2);
3573 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3577 /* copy firmware version string from vpd into adapter */
3578 if ((substr + 3 + fw_level_len) <
3579 (adapter->vpd->buff + adapter->vpd->len)) {
3580 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3582 dev_info(dev, "FW substr extrapolated VPD buff\n");
3586 if (adapter->fw_version[0] == '\0')
3587 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3588 complete(&adapter->fw_done);
3591 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3593 struct device *dev = &adapter->vdev->dev;
3594 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3595 union ibmvnic_crq crq;
3598 dma_unmap_single(dev, adapter->ip_offload_tok,
3599 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3601 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3602 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3603 netdev_dbg(adapter->netdev, "%016lx\n",
3604 ((unsigned long int *)(buf))[i]);
3606 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3607 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3608 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3609 buf->tcp_ipv4_chksum);
3610 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3611 buf->tcp_ipv6_chksum);
3612 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3613 buf->udp_ipv4_chksum);
3614 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3615 buf->udp_ipv6_chksum);
3616 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3617 buf->large_tx_ipv4);
3618 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3619 buf->large_tx_ipv6);
3620 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3621 buf->large_rx_ipv4);
3622 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3623 buf->large_rx_ipv6);
3624 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3625 buf->max_ipv4_header_size);
3626 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3627 buf->max_ipv6_header_size);
3628 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3629 buf->max_tcp_header_size);
3630 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3631 buf->max_udp_header_size);
3632 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3633 buf->max_large_tx_size);
3634 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3635 buf->max_large_rx_size);
3636 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3637 buf->ipv6_extension_header);
3638 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3639 buf->tcp_pseudosum_req);
3640 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3641 buf->num_ipv6_ext_headers);
3642 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3643 buf->off_ipv6_ext_headers);
3645 adapter->ip_offload_ctrl_tok =
3646 dma_map_single(dev, &adapter->ip_offload_ctrl,
3647 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3649 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3650 dev_err(dev, "Couldn't map ip offload control buffer\n");
3654 adapter->ip_offload_ctrl.len =
3655 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3656 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3657 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3658 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3659 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3660 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3661 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3662 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3663 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3664 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3666 /* large_rx disabled for now, additional features needed */
3667 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3668 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3670 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3672 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3673 adapter->netdev->features |= NETIF_F_IP_CSUM;
3675 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3676 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3678 if ((adapter->netdev->features &
3679 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3680 adapter->netdev->features |= NETIF_F_RXCSUM;
3682 if (buf->large_tx_ipv4)
3683 adapter->netdev->features |= NETIF_F_TSO;
3684 if (buf->large_tx_ipv6)
3685 adapter->netdev->features |= NETIF_F_TSO6;
3687 adapter->netdev->hw_features |= adapter->netdev->features;
3689 memset(&crq, 0, sizeof(crq));
3690 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3691 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3692 crq.control_ip_offload.len =
3693 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3694 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3695 ibmvnic_send_crq(adapter, &crq);
3698 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3699 struct ibmvnic_adapter *adapter)
3701 struct device *dev = &adapter->vdev->dev;
3702 struct ibmvnic_error_buff *error_buff, *tmp;
3703 unsigned long flags;
3707 if (!crq->request_error_rsp.rc.code) {
3708 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3709 crq->request_error_rsp.rc.code);
3713 spin_lock_irqsave(&adapter->error_list_lock, flags);
3714 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3715 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3717 list_del(&error_buff->list);
3720 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3723 dev_err(dev, "Couldn't find error id %x\n",
3724 be32_to_cpu(crq->request_error_rsp.error_id));
3728 dev_err(dev, "Detailed info for error id %x:",
3729 be32_to_cpu(crq->request_error_rsp.error_id));
3731 for (i = 0; i < error_buff->len; i++) {
3732 pr_cont("%02x", (int)error_buff->buff[i]);
3738 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3740 kfree(error_buff->buff);
3744 static void request_error_information(struct ibmvnic_adapter *adapter,
3745 union ibmvnic_crq *err_crq)
3747 struct device *dev = &adapter->vdev->dev;
3748 struct net_device *netdev = adapter->netdev;
3749 struct ibmvnic_error_buff *error_buff;
3750 unsigned long timeout = msecs_to_jiffies(30000);
3751 union ibmvnic_crq crq;
3752 unsigned long flags;
3755 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3759 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3760 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3761 if (!error_buff->buff) {
3766 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3768 if (dma_mapping_error(dev, error_buff->dma)) {
3769 netdev_err(netdev, "Couldn't map error buffer\n");
3770 kfree(error_buff->buff);
3775 error_buff->len = detail_len;
3776 error_buff->error_id = err_crq->error_indication.error_id;
3778 spin_lock_irqsave(&adapter->error_list_lock, flags);
3779 list_add_tail(&error_buff->list, &adapter->errors);
3780 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3782 memset(&crq, 0, sizeof(crq));
3783 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3784 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3785 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3786 crq.request_error_info.len = cpu_to_be32(detail_len);
3787 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3789 rc = ibmvnic_send_crq(adapter, &crq);
3791 netdev_err(netdev, "failed to request error information\n");
3795 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3796 netdev_err(netdev, "timeout waiting for error information\n");
3803 spin_lock_irqsave(&adapter->error_list_lock, flags);
3804 list_del(&error_buff->list);
3805 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3807 kfree(error_buff->buff);
3811 static void handle_error_indication(union ibmvnic_crq *crq,
3812 struct ibmvnic_adapter *adapter)
3814 struct device *dev = &adapter->vdev->dev;
3816 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3817 crq->error_indication.flags
3818 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3819 be32_to_cpu(crq->error_indication.error_id),
3820 be16_to_cpu(crq->error_indication.error_cause));
3822 if (be32_to_cpu(crq->error_indication.error_id))
3823 request_error_information(adapter, crq);
3825 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3826 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3828 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3831 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3832 struct ibmvnic_adapter *adapter)
3834 struct net_device *netdev = adapter->netdev;
3835 struct device *dev = &adapter->vdev->dev;
3838 rc = crq->change_mac_addr_rsp.rc.code;
3840 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3843 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3846 complete(&adapter->fw_done);
3850 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3851 struct ibmvnic_adapter *adapter)
3853 struct device *dev = &adapter->vdev->dev;
3857 atomic_dec(&adapter->running_cap_crqs);
3858 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3860 req_value = &adapter->req_tx_queues;
3864 req_value = &adapter->req_rx_queues;
3867 case REQ_RX_ADD_QUEUES:
3868 req_value = &adapter->req_rx_add_queues;
3871 case REQ_TX_ENTRIES_PER_SUBCRQ:
3872 req_value = &adapter->req_tx_entries_per_subcrq;
3873 name = "tx_entries_per_subcrq";
3875 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3876 req_value = &adapter->req_rx_add_entries_per_subcrq;
3877 name = "rx_add_entries_per_subcrq";
3880 req_value = &adapter->req_mtu;
3883 case PROMISC_REQUESTED:
3884 req_value = &adapter->promisc;
3888 dev_err(dev, "Got invalid cap request rsp %d\n",
3889 crq->request_capability.capability);
3893 switch (crq->request_capability_rsp.rc.code) {
3896 case PARTIALSUCCESS:
3897 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3899 (long int)be64_to_cpu(crq->request_capability_rsp.
3902 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3904 pr_err("mtu of %llu is not supported. Reverting.\n",
3906 *req_value = adapter->fallback.mtu;
3909 be64_to_cpu(crq->request_capability_rsp.number);
3912 ibmvnic_send_req_caps(adapter, 1);
3915 dev_err(dev, "Error %d in request cap rsp\n",
3916 crq->request_capability_rsp.rc.code);
3920 /* Done receiving requested capabilities, query IP offload support */
3921 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3922 union ibmvnic_crq newcrq;
3923 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3924 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3925 &adapter->ip_offload_buf;
3927 adapter->wait_capability = false;
3928 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3932 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3933 if (!firmware_has_feature(FW_FEATURE_CMO))
3934 dev_err(dev, "Couldn't map offload buffer\n");
3938 memset(&newcrq, 0, sizeof(newcrq));
3939 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3940 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3941 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3942 newcrq.query_ip_offload.ioba =
3943 cpu_to_be32(adapter->ip_offload_tok);
3945 ibmvnic_send_crq(adapter, &newcrq);
3949 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3950 struct ibmvnic_adapter *adapter)
3952 struct device *dev = &adapter->vdev->dev;
3953 struct net_device *netdev = adapter->netdev;
3954 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3955 struct ibmvnic_login_buffer *login = adapter->login_buf;
3958 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3960 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3961 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
3963 /* If the number of queues requested can't be allocated by the
3964 * server, the login response will return with code 1. We will need
3965 * to resend the login buffer with fewer queues requested.
3967 if (login_rsp_crq->generic.rc.code) {
3968 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
3969 complete(&adapter->init_done);
3973 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3975 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3976 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3977 netdev_dbg(adapter->netdev, "%016lx\n",
3978 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3982 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3983 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3984 adapter->req_rx_add_queues !=
3985 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3986 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3987 ibmvnic_remove(adapter->vdev);
3990 release_login_buffer(adapter);
3991 complete(&adapter->init_done);
3996 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3997 struct ibmvnic_adapter *adapter)
3999 struct device *dev = &adapter->vdev->dev;
4002 rc = crq->request_unmap_rsp.rc.code;
4004 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4007 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4008 struct ibmvnic_adapter *adapter)
4010 struct net_device *netdev = adapter->netdev;
4011 struct device *dev = &adapter->vdev->dev;
4014 rc = crq->query_map_rsp.rc.code;
4016 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4019 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4020 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4021 crq->query_map_rsp.free_pages);
4024 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4025 struct ibmvnic_adapter *adapter)
4027 struct net_device *netdev = adapter->netdev;
4028 struct device *dev = &adapter->vdev->dev;
4031 atomic_dec(&adapter->running_cap_crqs);
4032 netdev_dbg(netdev, "Outstanding queries: %d\n",
4033 atomic_read(&adapter->running_cap_crqs));
4034 rc = crq->query_capability.rc.code;
4036 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4040 switch (be16_to_cpu(crq->query_capability.capability)) {
4042 adapter->min_tx_queues =
4043 be64_to_cpu(crq->query_capability.number);
4044 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4045 adapter->min_tx_queues);
4048 adapter->min_rx_queues =
4049 be64_to_cpu(crq->query_capability.number);
4050 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4051 adapter->min_rx_queues);
4053 case MIN_RX_ADD_QUEUES:
4054 adapter->min_rx_add_queues =
4055 be64_to_cpu(crq->query_capability.number);
4056 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4057 adapter->min_rx_add_queues);
4060 adapter->max_tx_queues =
4061 be64_to_cpu(crq->query_capability.number);
4062 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4063 adapter->max_tx_queues);
4066 adapter->max_rx_queues =
4067 be64_to_cpu(crq->query_capability.number);
4068 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4069 adapter->max_rx_queues);
4071 case MAX_RX_ADD_QUEUES:
4072 adapter->max_rx_add_queues =
4073 be64_to_cpu(crq->query_capability.number);
4074 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4075 adapter->max_rx_add_queues);
4077 case MIN_TX_ENTRIES_PER_SUBCRQ:
4078 adapter->min_tx_entries_per_subcrq =
4079 be64_to_cpu(crq->query_capability.number);
4080 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4081 adapter->min_tx_entries_per_subcrq);
4083 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4084 adapter->min_rx_add_entries_per_subcrq =
4085 be64_to_cpu(crq->query_capability.number);
4086 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4087 adapter->min_rx_add_entries_per_subcrq);
4089 case MAX_TX_ENTRIES_PER_SUBCRQ:
4090 adapter->max_tx_entries_per_subcrq =
4091 be64_to_cpu(crq->query_capability.number);
4092 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4093 adapter->max_tx_entries_per_subcrq);
4095 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4096 adapter->max_rx_add_entries_per_subcrq =
4097 be64_to_cpu(crq->query_capability.number);
4098 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4099 adapter->max_rx_add_entries_per_subcrq);
4101 case TCP_IP_OFFLOAD:
4102 adapter->tcp_ip_offload =
4103 be64_to_cpu(crq->query_capability.number);
4104 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4105 adapter->tcp_ip_offload);
4107 case PROMISC_SUPPORTED:
4108 adapter->promisc_supported =
4109 be64_to_cpu(crq->query_capability.number);
4110 netdev_dbg(netdev, "promisc_supported = %lld\n",
4111 adapter->promisc_supported);
4114 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4115 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4116 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4119 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4120 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4121 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4123 case MAX_MULTICAST_FILTERS:
4124 adapter->max_multicast_filters =
4125 be64_to_cpu(crq->query_capability.number);
4126 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4127 adapter->max_multicast_filters);
4129 case VLAN_HEADER_INSERTION:
4130 adapter->vlan_header_insertion =
4131 be64_to_cpu(crq->query_capability.number);
4132 if (adapter->vlan_header_insertion)
4133 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4134 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4135 adapter->vlan_header_insertion);
4137 case RX_VLAN_HEADER_INSERTION:
4138 adapter->rx_vlan_header_insertion =
4139 be64_to_cpu(crq->query_capability.number);
4140 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4141 adapter->rx_vlan_header_insertion);
4143 case MAX_TX_SG_ENTRIES:
4144 adapter->max_tx_sg_entries =
4145 be64_to_cpu(crq->query_capability.number);
4146 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4147 adapter->max_tx_sg_entries);
4149 case RX_SG_SUPPORTED:
4150 adapter->rx_sg_supported =
4151 be64_to_cpu(crq->query_capability.number);
4152 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4153 adapter->rx_sg_supported);
4155 case OPT_TX_COMP_SUB_QUEUES:
4156 adapter->opt_tx_comp_sub_queues =
4157 be64_to_cpu(crq->query_capability.number);
4158 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4159 adapter->opt_tx_comp_sub_queues);
4161 case OPT_RX_COMP_QUEUES:
4162 adapter->opt_rx_comp_queues =
4163 be64_to_cpu(crq->query_capability.number);
4164 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4165 adapter->opt_rx_comp_queues);
4167 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4168 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4169 be64_to_cpu(crq->query_capability.number);
4170 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4171 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4173 case OPT_TX_ENTRIES_PER_SUBCRQ:
4174 adapter->opt_tx_entries_per_subcrq =
4175 be64_to_cpu(crq->query_capability.number);
4176 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4177 adapter->opt_tx_entries_per_subcrq);
4179 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4180 adapter->opt_rxba_entries_per_subcrq =
4181 be64_to_cpu(crq->query_capability.number);
4182 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4183 adapter->opt_rxba_entries_per_subcrq);
4185 case TX_RX_DESC_REQ:
4186 adapter->tx_rx_desc_req = crq->query_capability.number;
4187 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4188 adapter->tx_rx_desc_req);
4192 netdev_err(netdev, "Got invalid cap rsp %d\n",
4193 crq->query_capability.capability);
4197 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4198 adapter->wait_capability = false;
4199 ibmvnic_send_req_caps(adapter, 0);
4203 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4204 struct ibmvnic_adapter *adapter)
4206 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4207 struct net_device *netdev = adapter->netdev;
4208 struct device *dev = &adapter->vdev->dev;
4209 u64 *u64_crq = (u64 *)crq;
4212 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4213 (unsigned long int)cpu_to_be64(u64_crq[0]),
4214 (unsigned long int)cpu_to_be64(u64_crq[1]));
4215 switch (gen_crq->first) {
4216 case IBMVNIC_CRQ_INIT_RSP:
4217 switch (gen_crq->cmd) {
4218 case IBMVNIC_CRQ_INIT:
4219 dev_info(dev, "Partner initialized\n");
4220 adapter->from_passive_init = true;
4221 adapter->failover_pending = false;
4222 complete(&adapter->init_done);
4223 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4225 case IBMVNIC_CRQ_INIT_COMPLETE:
4226 dev_info(dev, "Partner initialization complete\n");
4227 send_version_xchg(adapter);
4230 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4233 case IBMVNIC_CRQ_XPORT_EVENT:
4234 netif_carrier_off(netdev);
4235 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4236 dev_info(dev, "Migrated, re-enabling adapter\n");
4237 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4238 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4239 dev_info(dev, "Backing device failover detected\n");
4240 adapter->failover_pending = true;
4242 /* The adapter lost the connection */
4243 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4245 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4248 case IBMVNIC_CRQ_CMD_RSP:
4251 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4256 switch (gen_crq->cmd) {
4257 case VERSION_EXCHANGE_RSP:
4258 rc = crq->version_exchange_rsp.rc.code;
4260 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4263 dev_info(dev, "Partner protocol version is %d\n",
4264 crq->version_exchange_rsp.version);
4265 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4268 be16_to_cpu(crq->version_exchange_rsp.version);
4269 send_cap_queries(adapter);
4271 case QUERY_CAPABILITY_RSP:
4272 handle_query_cap_rsp(crq, adapter);
4275 handle_query_map_rsp(crq, adapter);
4277 case REQUEST_MAP_RSP:
4278 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4279 complete(&adapter->fw_done);
4281 case REQUEST_UNMAP_RSP:
4282 handle_request_unmap_rsp(crq, adapter);
4284 case REQUEST_CAPABILITY_RSP:
4285 handle_request_cap_rsp(crq, adapter);
4288 netdev_dbg(netdev, "Got Login Response\n");
4289 handle_login_rsp(crq, adapter);
4291 case LOGICAL_LINK_STATE_RSP:
4293 "Got Logical Link State Response, state: %d rc: %d\n",
4294 crq->logical_link_state_rsp.link_state,
4295 crq->logical_link_state_rsp.rc.code);
4296 adapter->logical_link_state =
4297 crq->logical_link_state_rsp.link_state;
4298 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4299 complete(&adapter->init_done);
4301 case LINK_STATE_INDICATION:
4302 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4303 adapter->phys_link_state =
4304 crq->link_state_indication.phys_link_state;
4305 adapter->logical_link_state =
4306 crq->link_state_indication.logical_link_state;
4308 case CHANGE_MAC_ADDR_RSP:
4309 netdev_dbg(netdev, "Got MAC address change Response\n");
4310 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4312 case ERROR_INDICATION:
4313 netdev_dbg(netdev, "Got Error Indication\n");
4314 handle_error_indication(crq, adapter);
4316 case REQUEST_ERROR_RSP:
4317 netdev_dbg(netdev, "Got Error Detail Response\n");
4318 handle_error_info_rsp(crq, adapter);
4320 case REQUEST_STATISTICS_RSP:
4321 netdev_dbg(netdev, "Got Statistics Response\n");
4322 complete(&adapter->stats_done);
4324 case QUERY_IP_OFFLOAD_RSP:
4325 netdev_dbg(netdev, "Got Query IP offload Response\n");
4326 handle_query_ip_offload_rsp(adapter);
4328 case MULTICAST_CTRL_RSP:
4329 netdev_dbg(netdev, "Got multicast control Response\n");
4331 case CONTROL_IP_OFFLOAD_RSP:
4332 netdev_dbg(netdev, "Got Control IP offload Response\n");
4333 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4334 sizeof(adapter->ip_offload_ctrl),
4336 complete(&adapter->init_done);
4338 case COLLECT_FW_TRACE_RSP:
4339 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4340 complete(&adapter->fw_done);
4342 case GET_VPD_SIZE_RSP:
4343 handle_vpd_size_rsp(crq, adapter);
4346 handle_vpd_rsp(crq, adapter);
4349 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4354 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4356 struct ibmvnic_adapter *adapter = instance;
4358 tasklet_schedule(&adapter->tasklet);
4362 static void ibmvnic_tasklet(void *data)
4364 struct ibmvnic_adapter *adapter = data;
4365 struct ibmvnic_crq_queue *queue = &adapter->crq;
4366 union ibmvnic_crq *crq;
4367 unsigned long flags;
4370 spin_lock_irqsave(&queue->lock, flags);
4372 /* Pull all the valid messages off the CRQ */
4373 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4374 ibmvnic_handle_crq(crq, adapter);
4375 crq->generic.first = 0;
4378 /* remain in tasklet until all
4379 * capabilities responses are received
4381 if (!adapter->wait_capability)
4384 /* if capabilities CRQ's were sent in this tasklet, the following
4385 * tasklet must wait until all responses are received
4387 if (atomic_read(&adapter->running_cap_crqs) != 0)
4388 adapter->wait_capability = true;
4389 spin_unlock_irqrestore(&queue->lock, flags);
4392 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4394 struct vio_dev *vdev = adapter->vdev;
4398 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4399 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4402 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4407 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4409 struct ibmvnic_crq_queue *crq = &adapter->crq;
4410 struct device *dev = &adapter->vdev->dev;
4411 struct vio_dev *vdev = adapter->vdev;
4416 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4417 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4419 /* Clean out the queue */
4420 memset(crq->msgs, 0, PAGE_SIZE);
4423 /* And re-open it again */
4424 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4425 crq->msg_token, PAGE_SIZE);
4428 /* Adapter is good, but other end is not ready */
4429 dev_warn(dev, "Partner adapter not ready\n");
4431 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4436 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4438 struct ibmvnic_crq_queue *crq = &adapter->crq;
4439 struct vio_dev *vdev = adapter->vdev;
4445 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4446 free_irq(vdev->irq, adapter);
4447 tasklet_kill(&adapter->tasklet);
4449 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4450 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4452 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4454 free_page((unsigned long)crq->msgs);
4458 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4460 struct ibmvnic_crq_queue *crq = &adapter->crq;
4461 struct device *dev = &adapter->vdev->dev;
4462 struct vio_dev *vdev = adapter->vdev;
4463 int rc, retrc = -ENOMEM;
4468 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4469 /* Should we allocate more than one page? */
4474 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4475 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4477 if (dma_mapping_error(dev, crq->msg_token))
4480 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4481 crq->msg_token, PAGE_SIZE);
4483 if (rc == H_RESOURCE)
4484 /* maybe kexecing and resource is busy. try a reset */
4485 rc = ibmvnic_reset_crq(adapter);
4488 if (rc == H_CLOSED) {
4489 dev_warn(dev, "Partner adapter not ready\n");
4491 dev_warn(dev, "Error %d opening adapter\n", rc);
4492 goto reg_crq_failed;
4497 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4498 (unsigned long)adapter);
4500 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4501 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4504 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4506 goto req_irq_failed;
4509 rc = vio_enable_interrupts(vdev);
4511 dev_err(dev, "Error %d enabling interrupts\n", rc);
4512 goto req_irq_failed;
4516 spin_lock_init(&crq->lock);
4521 tasklet_kill(&adapter->tasklet);
4523 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4524 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4526 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4528 free_page((unsigned long)crq->msgs);
4533 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4535 struct device *dev = &adapter->vdev->dev;
4536 unsigned long timeout = msecs_to_jiffies(30000);
4537 u64 old_num_rx_queues, old_num_tx_queues;
4540 adapter->from_passive_init = false;
4542 old_num_rx_queues = adapter->req_rx_queues;
4543 old_num_tx_queues = adapter->req_tx_queues;
4545 init_completion(&adapter->init_done);
4546 adapter->init_done_rc = 0;
4547 ibmvnic_send_crq_init(adapter);
4548 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4549 dev_err(dev, "Initialization sequence timed out\n");
4553 if (adapter->init_done_rc) {
4554 release_crq_queue(adapter);
4555 return adapter->init_done_rc;
4558 if (adapter->from_passive_init) {
4559 adapter->state = VNIC_OPEN;
4560 adapter->from_passive_init = false;
4564 if (adapter->resetting && !adapter->wait_for_reset &&
4565 adapter->reset_reason != VNIC_RESET_MOBILITY) {
4566 if (adapter->req_rx_queues != old_num_rx_queues ||
4567 adapter->req_tx_queues != old_num_tx_queues) {
4568 release_sub_crqs(adapter, 0);
4569 rc = init_sub_crqs(adapter);
4571 rc = reset_sub_crq_queues(adapter);
4574 rc = init_sub_crqs(adapter);
4578 dev_err(dev, "Initialization of sub crqs failed\n");
4579 release_crq_queue(adapter);
4583 rc = init_sub_crq_irqs(adapter);
4585 dev_err(dev, "Failed to initialize sub crq irqs\n");
4586 release_crq_queue(adapter);
4589 rc = init_stats_buffers(adapter);
4593 rc = init_stats_token(adapter);
4600 static struct device_attribute dev_attr_failover;
4602 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4604 struct ibmvnic_adapter *adapter;
4605 struct net_device *netdev;
4606 unsigned char *mac_addr_p;
4609 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4612 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4613 VETH_MAC_ADDR, NULL);
4616 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4617 __FILE__, __LINE__);
4621 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4622 IBMVNIC_MAX_QUEUES);
4626 adapter = netdev_priv(netdev);
4627 adapter->state = VNIC_PROBING;
4628 dev_set_drvdata(&dev->dev, netdev);
4629 adapter->vdev = dev;
4630 adapter->netdev = netdev;
4632 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4633 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4634 netdev->irq = dev->irq;
4635 netdev->netdev_ops = &ibmvnic_netdev_ops;
4636 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4637 SET_NETDEV_DEV(netdev, &dev->dev);
4639 spin_lock_init(&adapter->stats_lock);
4641 INIT_LIST_HEAD(&adapter->errors);
4642 spin_lock_init(&adapter->error_list_lock);
4644 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4645 INIT_LIST_HEAD(&adapter->rwi_list);
4646 mutex_init(&adapter->reset_lock);
4647 mutex_init(&adapter->rwi_lock);
4648 adapter->resetting = false;
4650 adapter->mac_change_pending = false;
4653 rc = init_crq_queue(adapter);
4655 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4657 goto ibmvnic_init_fail;
4660 rc = ibmvnic_init(adapter);
4661 if (rc && rc != EAGAIN)
4662 goto ibmvnic_init_fail;
4663 } while (rc == EAGAIN);
4665 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4666 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4667 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4669 rc = device_create_file(&dev->dev, &dev_attr_failover);
4671 goto ibmvnic_init_fail;
4673 netif_carrier_off(netdev);
4674 rc = register_netdev(netdev);
4676 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4677 goto ibmvnic_register_fail;
4679 dev_info(&dev->dev, "ibmvnic registered\n");
4681 adapter->state = VNIC_PROBED;
4683 adapter->wait_for_reset = false;
4687 ibmvnic_register_fail:
4688 device_remove_file(&dev->dev, &dev_attr_failover);
4691 release_sub_crqs(adapter, 1);
4692 release_crq_queue(adapter);
4693 free_netdev(netdev);
4698 static int ibmvnic_remove(struct vio_dev *dev)
4700 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4701 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4703 adapter->state = VNIC_REMOVING;
4704 unregister_netdev(netdev);
4705 mutex_lock(&adapter->reset_lock);
4707 release_resources(adapter);
4708 release_sub_crqs(adapter, 1);
4709 release_crq_queue(adapter);
4711 release_stats_token(adapter);
4712 release_stats_buffers(adapter);
4714 adapter->state = VNIC_REMOVED;
4716 mutex_unlock(&adapter->reset_lock);
4717 device_remove_file(&dev->dev, &dev_attr_failover);
4718 free_netdev(netdev);
4719 dev_set_drvdata(&dev->dev, NULL);
4724 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4725 const char *buf, size_t count)
4727 struct net_device *netdev = dev_get_drvdata(dev);
4728 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4729 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4730 __be64 session_token;
4733 if (!sysfs_streq(buf, "1"))
4736 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4737 H_GET_SESSION_TOKEN, 0, 0, 0);
4739 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4744 session_token = (__be64)retbuf[0];
4745 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4746 be64_to_cpu(session_token));
4747 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4748 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4750 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4758 static DEVICE_ATTR_WO(failover);
4760 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4762 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4763 struct ibmvnic_adapter *adapter;
4764 struct iommu_table *tbl;
4765 unsigned long ret = 0;
4768 tbl = get_iommu_table_base(&vdev->dev);
4770 /* netdev inits at probe time along with the structures we need below*/
4772 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4774 adapter = netdev_priv(netdev);
4776 ret += PAGE_SIZE; /* the crq message queue */
4777 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4779 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4780 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4782 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4784 ret += adapter->rx_pool[i].size *
4785 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4790 static int ibmvnic_resume(struct device *dev)
4792 struct net_device *netdev = dev_get_drvdata(dev);
4793 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4795 if (adapter->state != VNIC_OPEN)
4798 tasklet_schedule(&adapter->tasklet);
4803 static const struct vio_device_id ibmvnic_device_table[] = {
4804 {"network", "IBM,vnic"},
4807 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4809 static const struct dev_pm_ops ibmvnic_pm_ops = {
4810 .resume = ibmvnic_resume
4813 static struct vio_driver ibmvnic_driver = {
4814 .id_table = ibmvnic_device_table,
4815 .probe = ibmvnic_probe,
4816 .remove = ibmvnic_remove,
4817 .get_desired_dma = ibmvnic_get_desired_dma,
4818 .name = ibmvnic_driver_name,
4819 .pm = &ibmvnic_pm_ops,
4822 /* module functions */
4823 static int __init ibmvnic_module_init(void)
4825 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4826 IBMVNIC_DRIVER_VERSION);
4828 return vio_register_driver(&ibmvnic_driver);
4831 static void __exit ibmvnic_module_exit(void)
4833 vio_unregister_driver(&ibmvnic_driver);
4836 module_init(ibmvnic_module_init);
4837 module_exit(ibmvnic_module_exit);