1 /**************************************************************************/
3 /* IBM System i and System p Virtual NIC Device Driver */
4 /* Copyright (C) 2014 IBM Corp. */
5 /* Santiago Leon (santi_leon@yahoo.com) */
6 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7 /* John Allen (jallen@linux.vnet.ibm.com) */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2 of the License, or */
12 /* (at your option) any later version. */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program. */
22 /* This module contains the implementation of a virtual ethernet device */
23 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24 /* option of the RS/6000 Platform Architecture to interface with virtual */
25 /* ethernet NICs that are presented to the partition by the hypervisor. */
27 /* Messages are passed between the VNIC driver and the VNIC server using */
28 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29 /* issue and receive commands that initiate communication with the server */
30 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31 /* are used by the driver to notify the server that a packet is */
32 /* ready for transmission or that a buffer has been added to receive a */
33 /* packet. Subsequently, sCRQs are used by the server to notify the */
34 /* driver that a packet transmission has been completed or that a packet */
35 /* has been received and placed in a waiting buffer. */
37 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38 /* which skbs are DMA mapped and immediately unmapped when the transmit */
39 /* or receive has been completed, the VNIC driver is required to use */
40 /* "long term mapping". This entails that large, continuous DMA mapped */
41 /* buffers are allocated on driver initialization and these buffers are */
42 /* then continuously reused to pass skbs to and from the VNIC server. */
44 /**************************************************************************/
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/types.h>
49 #include <linux/errno.h>
50 #include <linux/completion.h>
51 #include <linux/ioport.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/kernel.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
60 #include <linux/ethtool.h>
61 #include <linux/proc_fs.h>
62 #include <linux/if_arp.h>
65 #include <linux/ipv6.h>
66 #include <linux/irq.h>
67 #include <linux/kthread.h>
68 #include <linux/seq_file.h>
69 #include <linux/interrupt.h>
70 #include <net/net_namespace.h>
71 #include <asm/hvcall.h>
72 #include <linux/atomic.h>
74 #include <asm/iommu.h>
75 #include <linux/uaccess.h>
76 #include <asm/firmware.h>
77 #include <linux/workqueue.h>
78 #include <linux/if_vlan.h>
79 #include <linux/utsname.h>
83 static const char ibmvnic_driver_name[] = "ibmvnic";
84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
86 MODULE_AUTHOR("Santiago Leon");
87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92 static int ibmvnic_remove(struct vio_dev *);
93 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102 static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104 static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106 static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110 static int ibmvnic_poll(struct napi_struct *napi, int data);
111 static void send_map_query(struct ibmvnic_adapter *adapter);
112 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static void send_request_unmap(struct ibmvnic_adapter *, u8);
114 static int send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
122 struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
131 static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
173 struct device *dev = &adapter->vdev->dev;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
180 dev_err(dev, "Couldn't alloc long term buffer\n");
183 ltb->map_id = adapter->map_id;
186 init_completion(&adapter->fw_done);
187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
189 wait_for_completion(&adapter->fw_done);
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
202 struct device *dev = &adapter->vdev->dev;
207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
209 send_request_unmap(adapter, ltb->map_id);
210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
216 memset(ltb->buff, 0, ltb->size);
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
237 adapter->rx_pool[i].active = 0;
240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
272 index = pool->free_map[pool->next_free];
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
302 #ifdef __LITTLE_ENDIAN__
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
309 if (lpar_rc != H_SUCCESS)
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
316 atomic_add(buffers_added, &pool->available);
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
361 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
363 adapter->tx_stats_buffers =
364 kcalloc(IBMVNIC_MAX_QUEUES,
365 sizeof(struct ibmvnic_tx_queue_stats),
367 if (!adapter->tx_stats_buffers)
370 adapter->rx_stats_buffers =
371 kcalloc(IBMVNIC_MAX_QUEUES,
372 sizeof(struct ibmvnic_rx_queue_stats),
374 if (!adapter->rx_stats_buffers)
380 static void release_stats_token(struct ibmvnic_adapter *adapter)
382 struct device *dev = &adapter->vdev->dev;
384 if (!adapter->stats_token)
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
390 adapter->stats_token = 0;
393 static int init_stats_token(struct ibmvnic_adapter *adapter)
395 struct device *dev = &adapter->vdev->dev;
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
406 adapter->stats_token = stok;
407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
411 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
413 struct ibmvnic_rx_pool *rx_pool;
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
456 static void release_rx_pools(struct ibmvnic_adapter *adapter)
458 struct ibmvnic_rx_pool *rx_pool;
461 if (!adapter->rx_pool)
464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
465 rx_pool = &adapter->rx_pool[i];
467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
472 if (!rx_pool->rx_buff)
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
482 kfree(rx_pool->rx_buff);
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
487 adapter->num_active_rx_pools = 0;
490 static int init_rx_pools(struct net_device *netdev)
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
512 adapter->num_active_rx_pools = rxadd_subcrqs;
514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
517 netdev_dbg(adapter->netdev,
518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
560 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
562 struct ibmvnic_tx_pool *tx_pool;
566 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
567 for (i = 0; i < tx_scrqs; i++) {
568 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
570 tx_pool = &adapter->tx_pool[i];
572 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
576 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
580 memset(tx_pool->tx_buff, 0,
581 adapter->req_tx_entries_per_subcrq *
582 sizeof(struct ibmvnic_tx_buff));
584 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
585 tx_pool->free_map[j] = j;
587 tx_pool->consumer_index = 0;
588 tx_pool->producer_index = 0;
589 tx_pool->tso_index = 0;
595 static void release_vpd_data(struct ibmvnic_adapter *adapter)
600 kfree(adapter->vpd->buff);
606 static void release_tx_pools(struct ibmvnic_adapter *adapter)
608 struct ibmvnic_tx_pool *tx_pool;
611 if (!adapter->tx_pool)
614 for (i = 0; i < adapter->num_active_tx_pools; i++) {
615 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
616 tx_pool = &adapter->tx_pool[i];
617 kfree(tx_pool->tx_buff);
618 free_long_term_buff(adapter, &tx_pool->long_term_buff);
619 free_long_term_buff(adapter, &tx_pool->tso_ltb);
620 kfree(tx_pool->free_map);
623 kfree(adapter->tx_pool);
624 adapter->tx_pool = NULL;
625 adapter->num_active_tx_pools = 0;
628 static int init_tx_pools(struct net_device *netdev)
630 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
631 struct device *dev = &adapter->vdev->dev;
632 struct ibmvnic_tx_pool *tx_pool;
636 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
637 adapter->tx_pool = kcalloc(tx_subcrqs,
638 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
639 if (!adapter->tx_pool)
642 adapter->num_active_tx_pools = tx_subcrqs;
644 for (i = 0; i < tx_subcrqs; i++) {
645 tx_pool = &adapter->tx_pool[i];
647 netdev_dbg(adapter->netdev,
648 "Initializing tx_pool[%d], %lld buffs\n",
649 i, adapter->req_tx_entries_per_subcrq);
651 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
652 sizeof(struct ibmvnic_tx_buff),
654 if (!tx_pool->tx_buff) {
655 dev_err(dev, "tx pool buffer allocation failed\n");
656 release_tx_pools(adapter);
660 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
661 adapter->req_tx_entries_per_subcrq *
663 release_tx_pools(adapter);
668 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
670 IBMVNIC_TSO_BUF_SZ)) {
671 release_tx_pools(adapter);
675 tx_pool->tso_index = 0;
677 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
678 sizeof(int), GFP_KERNEL);
679 if (!tx_pool->free_map) {
680 release_tx_pools(adapter);
684 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
685 tx_pool->free_map[j] = j;
687 tx_pool->consumer_index = 0;
688 tx_pool->producer_index = 0;
694 static void release_error_buffers(struct ibmvnic_adapter *adapter)
696 struct device *dev = &adapter->vdev->dev;
697 struct ibmvnic_error_buff *error_buff, *tmp;
700 spin_lock_irqsave(&adapter->error_list_lock, flags);
701 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
702 list_del(&error_buff->list);
703 dma_unmap_single(dev, error_buff->dma, error_buff->len,
705 kfree(error_buff->buff);
708 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
711 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
715 if (adapter->napi_enabled)
718 for (i = 0; i < adapter->req_rx_queues; i++)
719 napi_enable(&adapter->napi[i]);
721 adapter->napi_enabled = true;
724 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
728 if (!adapter->napi_enabled)
731 for (i = 0; i < adapter->req_rx_queues; i++) {
732 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
733 napi_disable(&adapter->napi[i]);
736 adapter->napi_enabled = false;
739 static int init_napi(struct ibmvnic_adapter *adapter)
743 adapter->napi = kcalloc(adapter->req_rx_queues,
744 sizeof(struct napi_struct), GFP_KERNEL);
748 for (i = 0; i < adapter->req_rx_queues; i++) {
749 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
750 netif_napi_add(adapter->netdev, &adapter->napi[i],
751 ibmvnic_poll, NAPI_POLL_WEIGHT);
754 adapter->num_active_rx_napi = adapter->req_rx_queues;
758 static void release_napi(struct ibmvnic_adapter *adapter)
765 for (i = 0; i < adapter->num_active_rx_napi; i++) {
766 if (&adapter->napi[i]) {
767 netdev_dbg(adapter->netdev,
768 "Releasing napi[%d]\n", i);
769 netif_napi_del(&adapter->napi[i]);
773 kfree(adapter->napi);
774 adapter->napi = NULL;
775 adapter->num_active_rx_napi = 0;
778 static int ibmvnic_login(struct net_device *netdev)
780 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
781 unsigned long timeout = msecs_to_jiffies(30000);
782 struct device *dev = &adapter->vdev->dev;
786 if (adapter->renegotiate) {
787 adapter->renegotiate = false;
788 release_sub_crqs(adapter, 1);
790 reinit_completion(&adapter->init_done);
791 send_cap_queries(adapter);
792 if (!wait_for_completion_timeout(&adapter->init_done,
794 dev_err(dev, "Capabilities query timeout\n");
797 rc = init_sub_crqs(adapter);
800 "Initialization of SCRQ's failed\n");
803 rc = init_sub_crq_irqs(adapter);
806 "Initialization of SCRQ's irqs failed\n");
811 reinit_completion(&adapter->init_done);
812 rc = send_login(adapter);
814 dev_err(dev, "Unable to attempt device login\n");
816 } else if (!wait_for_completion_timeout(&adapter->init_done,
818 dev_err(dev, "Login timeout\n");
821 } while (adapter->renegotiate);
823 /* handle pending MAC address changes after successful login */
824 if (adapter->mac_change_pending) {
825 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
826 adapter->mac_change_pending = false;
832 static void release_login_buffer(struct ibmvnic_adapter *adapter)
834 kfree(adapter->login_buf);
835 adapter->login_buf = NULL;
838 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
840 kfree(adapter->login_rsp_buf);
841 adapter->login_rsp_buf = NULL;
844 static void release_resources(struct ibmvnic_adapter *adapter)
846 release_vpd_data(adapter);
848 release_tx_pools(adapter);
849 release_rx_pools(adapter);
851 release_error_buffers(adapter);
852 release_napi(adapter);
853 release_login_rsp_buffer(adapter);
856 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
858 struct net_device *netdev = adapter->netdev;
859 unsigned long timeout = msecs_to_jiffies(30000);
860 union ibmvnic_crq crq;
864 netdev_dbg(netdev, "setting link state %d\n", link_state);
866 memset(&crq, 0, sizeof(crq));
867 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
868 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
869 crq.logical_link_state.link_state = link_state;
874 reinit_completion(&adapter->init_done);
875 rc = ibmvnic_send_crq(adapter, &crq);
877 netdev_err(netdev, "Failed to set link state\n");
881 if (!wait_for_completion_timeout(&adapter->init_done,
883 netdev_err(netdev, "timeout setting link state\n");
887 if (adapter->init_done_rc == 1) {
888 /* Partuial success, delay and re-send */
897 static int set_real_num_queues(struct net_device *netdev)
899 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
902 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
903 adapter->req_tx_queues, adapter->req_rx_queues);
905 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
907 netdev_err(netdev, "failed to set the number of tx queues\n");
911 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
913 netdev_err(netdev, "failed to set the number of rx queues\n");
918 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
920 struct device *dev = &adapter->vdev->dev;
921 union ibmvnic_crq crq;
924 if (adapter->vpd->buff)
925 len = adapter->vpd->len;
927 init_completion(&adapter->fw_done);
928 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
929 crq.get_vpd_size.cmd = GET_VPD_SIZE;
930 ibmvnic_send_crq(adapter, &crq);
931 wait_for_completion(&adapter->fw_done);
933 if (!adapter->vpd->len)
936 if (!adapter->vpd->buff)
937 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
938 else if (adapter->vpd->len != len)
940 krealloc(adapter->vpd->buff,
941 adapter->vpd->len, GFP_KERNEL);
943 if (!adapter->vpd->buff) {
944 dev_err(dev, "Could allocate VPD buffer\n");
948 adapter->vpd->dma_addr =
949 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
951 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
952 dev_err(dev, "Could not map VPD buffer\n");
953 kfree(adapter->vpd->buff);
954 adapter->vpd->buff = NULL;
958 reinit_completion(&adapter->fw_done);
959 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
960 crq.get_vpd.cmd = GET_VPD;
961 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
962 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
963 ibmvnic_send_crq(adapter, &crq);
964 wait_for_completion(&adapter->fw_done);
969 static int init_resources(struct ibmvnic_adapter *adapter)
971 struct net_device *netdev = adapter->netdev;
974 rc = set_real_num_queues(netdev);
978 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
982 /* Vital Product Data (VPD) */
983 rc = ibmvnic_get_vpd(adapter);
985 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
991 rc = init_napi(adapter);
995 send_map_query(adapter);
997 rc = init_rx_pools(netdev);
1001 rc = init_tx_pools(netdev);
1005 static int __ibmvnic_open(struct net_device *netdev)
1007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1008 enum vnic_state prev_state = adapter->state;
1011 adapter->state = VNIC_OPENING;
1012 replenish_pools(adapter);
1013 ibmvnic_napi_enable(adapter);
1015 /* We're ready to receive frames, enable the sub-crq interrupts and
1016 * set the logical link state to up
1018 for (i = 0; i < adapter->req_rx_queues; i++) {
1019 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1020 if (prev_state == VNIC_CLOSED)
1021 enable_irq(adapter->rx_scrq[i]->irq);
1023 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1026 for (i = 0; i < adapter->req_tx_queues; i++) {
1027 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1028 if (prev_state == VNIC_CLOSED)
1029 enable_irq(adapter->tx_scrq[i]->irq);
1031 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1034 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1036 for (i = 0; i < adapter->req_rx_queues; i++)
1037 napi_disable(&adapter->napi[i]);
1038 release_resources(adapter);
1042 netif_tx_start_all_queues(netdev);
1044 if (prev_state == VNIC_CLOSED) {
1045 for (i = 0; i < adapter->req_rx_queues; i++)
1046 napi_schedule(&adapter->napi[i]);
1049 adapter->state = VNIC_OPEN;
1053 static int ibmvnic_open(struct net_device *netdev)
1055 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1058 mutex_lock(&adapter->reset_lock);
1060 if (adapter->state != VNIC_CLOSED) {
1061 rc = ibmvnic_login(netdev);
1063 mutex_unlock(&adapter->reset_lock);
1067 rc = init_resources(adapter);
1069 netdev_err(netdev, "failed to initialize resources\n");
1070 release_resources(adapter);
1071 mutex_unlock(&adapter->reset_lock);
1076 rc = __ibmvnic_open(netdev);
1077 netif_carrier_on(netdev);
1079 mutex_unlock(&adapter->reset_lock);
1084 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1086 struct ibmvnic_rx_pool *rx_pool;
1087 struct ibmvnic_rx_buff *rx_buff;
1092 if (!adapter->rx_pool)
1095 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1096 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1098 /* Free any remaining skbs in the rx buffer pools */
1099 for (i = 0; i < rx_scrqs; i++) {
1100 rx_pool = &adapter->rx_pool[i];
1101 if (!rx_pool || !rx_pool->rx_buff)
1104 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1105 for (j = 0; j < rx_entries; j++) {
1106 rx_buff = &rx_pool->rx_buff[j];
1107 if (rx_buff && rx_buff->skb) {
1108 dev_kfree_skb_any(rx_buff->skb);
1109 rx_buff->skb = NULL;
1115 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1117 struct ibmvnic_tx_pool *tx_pool;
1118 struct ibmvnic_tx_buff *tx_buff;
1123 if (!adapter->tx_pool)
1126 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1127 tx_entries = adapter->req_tx_entries_per_subcrq;
1129 /* Free any remaining skbs in the tx buffer pools */
1130 for (i = 0; i < tx_scrqs; i++) {
1131 tx_pool = &adapter->tx_pool[i];
1132 if (!tx_pool && !tx_pool->tx_buff)
1135 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1136 for (j = 0; j < tx_entries; j++) {
1137 tx_buff = &tx_pool->tx_buff[j];
1138 if (tx_buff && tx_buff->skb) {
1139 dev_kfree_skb_any(tx_buff->skb);
1140 tx_buff->skb = NULL;
1146 static int __ibmvnic_close(struct net_device *netdev)
1148 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1152 adapter->state = VNIC_CLOSING;
1154 /* ensure that transmissions are stopped if called by do_reset */
1155 if (adapter->resetting)
1156 netif_tx_disable(netdev);
1158 netif_tx_stop_all_queues(netdev);
1160 ibmvnic_napi_disable(adapter);
1162 if (adapter->tx_scrq) {
1163 for (i = 0; i < adapter->req_tx_queues; i++)
1164 if (adapter->tx_scrq[i]->irq) {
1166 "Disabling tx_scrq[%d] irq\n", i);
1167 disable_irq(adapter->tx_scrq[i]->irq);
1171 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1175 if (adapter->rx_scrq) {
1176 for (i = 0; i < adapter->req_rx_queues; i++) {
1177 if (adapter->rx_scrq[i]->irq) {
1179 "Disabling rx_scrq[%d] irq\n", i);
1180 disable_irq(adapter->rx_scrq[i]->irq);
1184 clean_rx_pools(adapter);
1185 clean_tx_pools(adapter);
1186 adapter->state = VNIC_CLOSED;
1190 static int ibmvnic_close(struct net_device *netdev)
1192 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1195 mutex_lock(&adapter->reset_lock);
1196 rc = __ibmvnic_close(netdev);
1197 mutex_unlock(&adapter->reset_lock);
1203 * build_hdr_data - creates L2/L3/L4 header data buffer
1204 * @hdr_field - bitfield determining needed headers
1205 * @skb - socket buffer
1206 * @hdr_len - array of header lengths
1207 * @tot_len - total length of data
1209 * Reads hdr_field to determine which headers are needed by firmware.
1210 * Builds a buffer containing these headers. Saves individual header
1211 * lengths and total buffer length to be used to build descriptors.
1213 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1214 int *hdr_len, u8 *hdr_data)
1219 hdr_len[0] = sizeof(struct ethhdr);
1221 if (skb->protocol == htons(ETH_P_IP)) {
1222 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1223 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1224 hdr_len[2] = tcp_hdrlen(skb);
1225 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1226 hdr_len[2] = sizeof(struct udphdr);
1227 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1228 hdr_len[1] = sizeof(struct ipv6hdr);
1229 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1230 hdr_len[2] = tcp_hdrlen(skb);
1231 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1232 hdr_len[2] = sizeof(struct udphdr);
1233 } else if (skb->protocol == htons(ETH_P_ARP)) {
1234 hdr_len[1] = arp_hdr_len(skb->dev);
1238 memset(hdr_data, 0, 120);
1239 if ((hdr_field >> 6) & 1) {
1240 hdr = skb_mac_header(skb);
1241 memcpy(hdr_data, hdr, hdr_len[0]);
1245 if ((hdr_field >> 5) & 1) {
1246 hdr = skb_network_header(skb);
1247 memcpy(hdr_data + len, hdr, hdr_len[1]);
1251 if ((hdr_field >> 4) & 1) {
1252 hdr = skb_transport_header(skb);
1253 memcpy(hdr_data + len, hdr, hdr_len[2]);
1260 * create_hdr_descs - create header and header extension descriptors
1261 * @hdr_field - bitfield determining needed headers
1262 * @data - buffer containing header data
1263 * @len - length of data buffer
1264 * @hdr_len - array of individual header lengths
1265 * @scrq_arr - descriptor array
1267 * Creates header and, if needed, header extension descriptors and
1268 * places them in a descriptor array, scrq_arr
1271 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1272 union sub_crq *scrq_arr)
1274 union sub_crq hdr_desc;
1280 while (tmp_len > 0) {
1281 cur = hdr_data + len - tmp_len;
1283 memset(&hdr_desc, 0, sizeof(hdr_desc));
1284 if (cur != hdr_data) {
1285 data = hdr_desc.hdr_ext.data;
1286 tmp = tmp_len > 29 ? 29 : tmp_len;
1287 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1288 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1289 hdr_desc.hdr_ext.len = tmp;
1291 data = hdr_desc.hdr.data;
1292 tmp = tmp_len > 24 ? 24 : tmp_len;
1293 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1294 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1295 hdr_desc.hdr.len = tmp;
1296 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1297 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1298 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1299 hdr_desc.hdr.flag = hdr_field << 1;
1301 memcpy(data, cur, tmp);
1303 *scrq_arr = hdr_desc;
1312 * build_hdr_descs_arr - build a header descriptor array
1313 * @skb - socket buffer
1314 * @num_entries - number of descriptors to be sent
1315 * @subcrq - first TX descriptor
1316 * @hdr_field - bit field determining which headers will be sent
1318 * This function will build a TX descriptor array with applicable
1319 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1322 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1323 int *num_entries, u8 hdr_field)
1325 int hdr_len[3] = {0, 0, 0};
1327 u8 *hdr_data = txbuff->hdr_data;
1329 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1331 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1332 txbuff->indir_arr + 1);
1335 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1337 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1338 int queue_num = skb_get_queue_mapping(skb);
1339 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1340 struct device *dev = &adapter->vdev->dev;
1341 struct ibmvnic_tx_buff *tx_buff = NULL;
1342 struct ibmvnic_sub_crq_queue *tx_scrq;
1343 struct ibmvnic_tx_pool *tx_pool;
1344 unsigned int tx_send_failed = 0;
1345 unsigned int tx_map_failed = 0;
1346 unsigned int tx_dropped = 0;
1347 unsigned int tx_packets = 0;
1348 unsigned int tx_bytes = 0;
1349 dma_addr_t data_dma_addr;
1350 struct netdev_queue *txq;
1351 unsigned long lpar_rc;
1352 union sub_crq tx_crq;
1353 unsigned int offset;
1354 int num_entries = 1;
1361 if (adapter->resetting) {
1362 if (!netif_subqueue_stopped(netdev, skb))
1363 netif_stop_subqueue(netdev, queue_num);
1364 dev_kfree_skb_any(skb);
1372 tx_pool = &adapter->tx_pool[queue_num];
1373 tx_scrq = adapter->tx_scrq[queue_num];
1374 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1375 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1376 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1378 index = tx_pool->free_map[tx_pool->consumer_index];
1380 if (skb_is_gso(skb)) {
1381 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1382 dst = tx_pool->tso_ltb.buff + offset;
1383 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1384 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1385 tx_pool->tso_index++;
1386 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1387 tx_pool->tso_index = 0;
1389 offset = index * adapter->req_mtu;
1390 dst = tx_pool->long_term_buff.buff + offset;
1391 memset(dst, 0, adapter->req_mtu);
1392 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1395 if (skb_shinfo(skb)->nr_frags) {
1399 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1400 cur = skb_headlen(skb);
1402 /* Copy the frags */
1403 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1404 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1407 page_address(skb_frag_page(frag)) +
1408 frag->page_offset, skb_frag_size(frag));
1409 cur += skb_frag_size(frag);
1412 skb_copy_from_linear_data(skb, dst, skb->len);
1415 tx_pool->consumer_index =
1416 (tx_pool->consumer_index + 1) %
1417 adapter->req_tx_entries_per_subcrq;
1419 tx_buff = &tx_pool->tx_buff[index];
1421 tx_buff->data_dma[0] = data_dma_addr;
1422 tx_buff->data_len[0] = skb->len;
1423 tx_buff->index = index;
1424 tx_buff->pool_index = queue_num;
1425 tx_buff->last_frag = true;
1427 memset(&tx_crq, 0, sizeof(tx_crq));
1428 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1429 tx_crq.v1.type = IBMVNIC_TX_DESC;
1430 tx_crq.v1.n_crq_elem = 1;
1431 tx_crq.v1.n_sge = 1;
1432 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1433 tx_crq.v1.correlator = cpu_to_be32(index);
1434 if (skb_is_gso(skb))
1435 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1437 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1438 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1439 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1441 if (adapter->vlan_header_insertion) {
1442 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1443 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1446 if (skb->protocol == htons(ETH_P_IP)) {
1447 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1448 proto = ip_hdr(skb)->protocol;
1449 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1450 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1451 proto = ipv6_hdr(skb)->nexthdr;
1454 if (proto == IPPROTO_TCP)
1455 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1456 else if (proto == IPPROTO_UDP)
1457 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1459 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1460 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1463 if (skb_is_gso(skb)) {
1464 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1465 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1468 /* determine if l2/3/4 headers are sent to firmware */
1469 if ((*hdrs >> 7) & 1) {
1470 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1471 tx_crq.v1.n_crq_elem = num_entries;
1472 tx_buff->num_entries = num_entries;
1473 tx_buff->indir_arr[0] = tx_crq;
1474 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1475 sizeof(tx_buff->indir_arr),
1477 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1478 dev_kfree_skb_any(skb);
1479 tx_buff->skb = NULL;
1480 if (!firmware_has_feature(FW_FEATURE_CMO))
1481 dev_err(dev, "tx: unable to map descriptor array\n");
1487 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1488 (u64)tx_buff->indir_dma,
1491 tx_buff->num_entries = num_entries;
1492 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1495 if (lpar_rc != H_SUCCESS) {
1496 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1498 if (tx_pool->consumer_index == 0)
1499 tx_pool->consumer_index =
1500 adapter->req_tx_entries_per_subcrq - 1;
1502 tx_pool->consumer_index--;
1504 dev_kfree_skb_any(skb);
1505 tx_buff->skb = NULL;
1507 if (lpar_rc == H_CLOSED) {
1508 /* Disable TX and report carrier off if queue is closed.
1509 * Firmware guarantees that a signal will be sent to the
1510 * driver, triggering a reset or some other action.
1512 netif_tx_stop_all_queues(netdev);
1513 netif_carrier_off(netdev);
1522 if (atomic_add_return(num_entries, &tx_scrq->used)
1523 >= adapter->req_tx_entries_per_subcrq) {
1524 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1525 netif_stop_subqueue(netdev, queue_num);
1529 tx_bytes += skb->len;
1530 txq->trans_start = jiffies;
1534 netdev->stats.tx_dropped += tx_dropped;
1535 netdev->stats.tx_bytes += tx_bytes;
1536 netdev->stats.tx_packets += tx_packets;
1537 adapter->tx_send_failed += tx_send_failed;
1538 adapter->tx_map_failed += tx_map_failed;
1539 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1540 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1541 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1546 static void ibmvnic_set_multi(struct net_device *netdev)
1548 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1549 struct netdev_hw_addr *ha;
1550 union ibmvnic_crq crq;
1552 memset(&crq, 0, sizeof(crq));
1553 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1554 crq.request_capability.cmd = REQUEST_CAPABILITY;
1556 if (netdev->flags & IFF_PROMISC) {
1557 if (!adapter->promisc_supported)
1560 if (netdev->flags & IFF_ALLMULTI) {
1561 /* Accept all multicast */
1562 memset(&crq, 0, sizeof(crq));
1563 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1564 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1565 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1566 ibmvnic_send_crq(adapter, &crq);
1567 } else if (netdev_mc_empty(netdev)) {
1568 /* Reject all multicast */
1569 memset(&crq, 0, sizeof(crq));
1570 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1571 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1572 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1573 ibmvnic_send_crq(adapter, &crq);
1575 /* Accept one or more multicast(s) */
1576 netdev_for_each_mc_addr(ha, netdev) {
1577 memset(&crq, 0, sizeof(crq));
1578 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1579 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1580 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1581 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1583 ibmvnic_send_crq(adapter, &crq);
1589 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1591 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1592 struct sockaddr *addr = p;
1593 union ibmvnic_crq crq;
1595 if (!is_valid_ether_addr(addr->sa_data))
1596 return -EADDRNOTAVAIL;
1598 memset(&crq, 0, sizeof(crq));
1599 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1600 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1601 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1603 init_completion(&adapter->fw_done);
1604 ibmvnic_send_crq(adapter, &crq);
1605 wait_for_completion(&adapter->fw_done);
1606 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1607 return adapter->fw_done_rc ? -EIO : 0;
1610 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1612 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1613 struct sockaddr *addr = p;
1616 if (adapter->state == VNIC_PROBED) {
1617 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1618 adapter->mac_change_pending = true;
1622 rc = __ibmvnic_set_mac(netdev, addr);
1628 * do_reset returns zero if we are able to keep processing reset events, or
1629 * non-zero if we hit a fatal error and must halt.
1631 static int do_reset(struct ibmvnic_adapter *adapter,
1632 struct ibmvnic_rwi *rwi, u32 reset_state)
1634 u64 old_num_rx_queues, old_num_tx_queues;
1635 struct net_device *netdev = adapter->netdev;
1638 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1641 netif_carrier_off(netdev);
1642 adapter->reset_reason = rwi->reset_reason;
1644 old_num_rx_queues = adapter->req_rx_queues;
1645 old_num_tx_queues = adapter->req_tx_queues;
1647 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1648 rc = ibmvnic_reenable_crq_queue(adapter);
1653 rc = __ibmvnic_close(netdev);
1657 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1658 adapter->wait_for_reset) {
1659 release_resources(adapter);
1660 release_sub_crqs(adapter, 1);
1661 release_crq_queue(adapter);
1664 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1665 /* remove the closed state so when we call open it appears
1666 * we are coming from the probed state.
1668 adapter->state = VNIC_PROBED;
1670 rc = ibmvnic_init(adapter);
1672 return IBMVNIC_INIT_FAILED;
1674 /* If the adapter was in PROBE state prior to the reset,
1677 if (reset_state == VNIC_PROBED)
1680 rc = ibmvnic_login(netdev);
1682 adapter->state = VNIC_PROBED;
1686 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1687 adapter->wait_for_reset) {
1688 rc = init_resources(adapter);
1691 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1692 adapter->req_tx_queues != old_num_tx_queues) {
1693 adapter->map_id = 1;
1694 release_rx_pools(adapter);
1695 release_tx_pools(adapter);
1696 init_rx_pools(netdev);
1697 init_tx_pools(netdev);
1699 release_napi(adapter);
1702 rc = reset_tx_pools(adapter);
1706 rc = reset_rx_pools(adapter);
1710 if (reset_state == VNIC_CLOSED)
1715 rc = __ibmvnic_open(netdev);
1717 if (list_empty(&adapter->rwi_list))
1718 adapter->state = VNIC_CLOSED;
1720 adapter->state = reset_state;
1726 for (i = 0; i < adapter->req_rx_queues; i++)
1727 napi_schedule(&adapter->napi[i]);
1729 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1730 netdev_notify_peers(netdev);
1732 netif_carrier_on(netdev);
1737 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1739 struct ibmvnic_rwi *rwi;
1741 mutex_lock(&adapter->rwi_lock);
1743 if (!list_empty(&adapter->rwi_list)) {
1744 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1746 list_del(&rwi->list);
1751 mutex_unlock(&adapter->rwi_lock);
1755 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1757 struct ibmvnic_rwi *rwi;
1759 rwi = get_next_rwi(adapter);
1762 rwi = get_next_rwi(adapter);
1766 static void __ibmvnic_reset(struct work_struct *work)
1768 struct ibmvnic_rwi *rwi;
1769 struct ibmvnic_adapter *adapter;
1770 struct net_device *netdev;
1774 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1775 netdev = adapter->netdev;
1777 mutex_lock(&adapter->reset_lock);
1778 adapter->resetting = true;
1779 reset_state = adapter->state;
1781 rwi = get_next_rwi(adapter);
1783 rc = do_reset(adapter, rwi, reset_state);
1785 if (rc && rc != IBMVNIC_INIT_FAILED)
1788 rwi = get_next_rwi(adapter);
1791 if (adapter->wait_for_reset) {
1792 adapter->wait_for_reset = false;
1793 adapter->reset_done_rc = rc;
1794 complete(&adapter->reset_done);
1798 netdev_dbg(adapter->netdev, "Reset failed\n");
1799 free_all_rwi(adapter);
1800 mutex_unlock(&adapter->reset_lock);
1804 adapter->resetting = false;
1805 mutex_unlock(&adapter->reset_lock);
1808 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1809 enum ibmvnic_reset_reason reason)
1811 struct ibmvnic_rwi *rwi, *tmp;
1812 struct net_device *netdev = adapter->netdev;
1813 struct list_head *entry;
1815 if (adapter->state == VNIC_REMOVING ||
1816 adapter->state == VNIC_REMOVED) {
1817 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1821 if (adapter->state == VNIC_PROBING) {
1822 netdev_warn(netdev, "Adapter reset during probe\n");
1823 adapter->init_done_rc = EAGAIN;
1827 mutex_lock(&adapter->rwi_lock);
1829 list_for_each(entry, &adapter->rwi_list) {
1830 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1831 if (tmp->reset_reason == reason) {
1832 netdev_dbg(netdev, "Skipping matching reset\n");
1833 mutex_unlock(&adapter->rwi_lock);
1838 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1840 mutex_unlock(&adapter->rwi_lock);
1841 ibmvnic_close(netdev);
1845 rwi->reset_reason = reason;
1846 list_add_tail(&rwi->list, &adapter->rwi_list);
1847 mutex_unlock(&adapter->rwi_lock);
1849 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1850 schedule_work(&adapter->ibmvnic_reset);
1853 static void ibmvnic_tx_timeout(struct net_device *dev)
1855 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1857 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1860 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1861 struct ibmvnic_rx_buff *rx_buff)
1863 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1865 rx_buff->skb = NULL;
1867 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1868 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1870 atomic_dec(&pool->available);
1873 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1875 struct net_device *netdev = napi->dev;
1876 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1877 int scrq_num = (int)(napi - adapter->napi);
1878 int frames_processed = 0;
1881 while (frames_processed < budget) {
1882 struct sk_buff *skb;
1883 struct ibmvnic_rx_buff *rx_buff;
1884 union sub_crq *next;
1889 if (unlikely(adapter->resetting &&
1890 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1891 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1892 napi_complete_done(napi, frames_processed);
1893 return frames_processed;
1896 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1898 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1900 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1901 rx_comp.correlator);
1902 /* do error checking */
1903 if (next->rx_comp.rc) {
1904 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1905 be16_to_cpu(next->rx_comp.rc));
1906 /* free the entry */
1907 next->rx_comp.first = 0;
1908 dev_kfree_skb_any(rx_buff->skb);
1909 remove_buff_from_pool(adapter, rx_buff);
1911 } else if (!rx_buff->skb) {
1912 /* free the entry */
1913 next->rx_comp.first = 0;
1914 remove_buff_from_pool(adapter, rx_buff);
1918 length = be32_to_cpu(next->rx_comp.len);
1919 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1920 flags = next->rx_comp.flags;
1922 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1925 /* VLAN Header has been stripped by the system firmware and
1926 * needs to be inserted by the driver
1928 if (adapter->rx_vlan_header_insertion &&
1929 (flags & IBMVNIC_VLAN_STRIPPED))
1930 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1931 ntohs(next->rx_comp.vlan_tci));
1933 /* free the entry */
1934 next->rx_comp.first = 0;
1935 remove_buff_from_pool(adapter, rx_buff);
1937 skb_put(skb, length);
1938 skb->protocol = eth_type_trans(skb, netdev);
1939 skb_record_rx_queue(skb, scrq_num);
1941 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1942 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1943 skb->ip_summed = CHECKSUM_UNNECESSARY;
1947 napi_gro_receive(napi, skb); /* send it up */
1948 netdev->stats.rx_packets++;
1949 netdev->stats.rx_bytes += length;
1950 adapter->rx_stats_buffers[scrq_num].packets++;
1951 adapter->rx_stats_buffers[scrq_num].bytes += length;
1955 if (adapter->state != VNIC_CLOSING)
1956 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1958 if (frames_processed < budget) {
1959 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1960 napi_complete_done(napi, frames_processed);
1961 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1962 napi_reschedule(napi)) {
1963 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1967 return frames_processed;
1970 #ifdef CONFIG_NET_POLL_CONTROLLER
1971 static void ibmvnic_netpoll_controller(struct net_device *dev)
1973 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1976 replenish_pools(netdev_priv(dev));
1977 for (i = 0; i < adapter->req_rx_queues; i++)
1978 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1979 adapter->rx_scrq[i]);
1983 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1985 adapter->fallback.mtu = adapter->req_mtu;
1986 adapter->fallback.rx_queues = adapter->req_rx_queues;
1987 adapter->fallback.tx_queues = adapter->req_tx_queues;
1988 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1989 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1991 init_completion(&adapter->reset_done);
1992 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1993 adapter->wait_for_reset = true;
1994 wait_for_completion(&adapter->reset_done);
1996 if (adapter->reset_done_rc) {
1997 adapter->desired.mtu = adapter->fallback.mtu;
1998 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1999 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2000 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2001 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2003 init_completion(&adapter->reset_done);
2004 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2005 wait_for_completion(&adapter->reset_done);
2007 adapter->wait_for_reset = false;
2009 return adapter->reset_done_rc;
2012 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2014 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2016 adapter->desired.mtu = new_mtu + ETH_HLEN;
2018 return wait_for_reset(adapter);
2021 static const struct net_device_ops ibmvnic_netdev_ops = {
2022 .ndo_open = ibmvnic_open,
2023 .ndo_stop = ibmvnic_close,
2024 .ndo_start_xmit = ibmvnic_xmit,
2025 .ndo_set_rx_mode = ibmvnic_set_multi,
2026 .ndo_set_mac_address = ibmvnic_set_mac,
2027 .ndo_validate_addr = eth_validate_addr,
2028 .ndo_tx_timeout = ibmvnic_tx_timeout,
2029 #ifdef CONFIG_NET_POLL_CONTROLLER
2030 .ndo_poll_controller = ibmvnic_netpoll_controller,
2032 .ndo_change_mtu = ibmvnic_change_mtu,
2035 /* ethtool functions */
2037 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2038 struct ethtool_link_ksettings *cmd)
2040 u32 supported, advertising;
2042 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2044 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2046 cmd->base.speed = SPEED_1000;
2047 cmd->base.duplex = DUPLEX_FULL;
2048 cmd->base.port = PORT_FIBRE;
2049 cmd->base.phy_address = 0;
2050 cmd->base.autoneg = AUTONEG_ENABLE;
2052 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2054 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2060 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2061 struct ethtool_drvinfo *info)
2063 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2065 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2066 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2067 strlcpy(info->fw_version, adapter->fw_version,
2068 sizeof(info->fw_version));
2071 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2073 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2075 return adapter->msg_enable;
2078 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2080 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2082 adapter->msg_enable = data;
2085 static u32 ibmvnic_get_link(struct net_device *netdev)
2087 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2089 /* Don't need to send a query because we request a logical link up at
2090 * init and then we wait for link state indications
2092 return adapter->logical_link_state;
2095 static void ibmvnic_get_ringparam(struct net_device *netdev,
2096 struct ethtool_ringparam *ring)
2098 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2100 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2101 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2102 ring->rx_mini_max_pending = 0;
2103 ring->rx_jumbo_max_pending = 0;
2104 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2105 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2106 ring->rx_mini_pending = 0;
2107 ring->rx_jumbo_pending = 0;
2110 static int ibmvnic_set_ringparam(struct net_device *netdev,
2111 struct ethtool_ringparam *ring)
2113 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2115 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2116 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2117 netdev_err(netdev, "Invalid request.\n");
2118 netdev_err(netdev, "Max tx buffers = %llu\n",
2119 adapter->max_rx_add_entries_per_subcrq);
2120 netdev_err(netdev, "Max rx buffers = %llu\n",
2121 adapter->max_tx_entries_per_subcrq);
2125 adapter->desired.rx_entries = ring->rx_pending;
2126 adapter->desired.tx_entries = ring->tx_pending;
2128 return wait_for_reset(adapter);
2131 static void ibmvnic_get_channels(struct net_device *netdev,
2132 struct ethtool_channels *channels)
2134 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2136 channels->max_rx = adapter->max_rx_queues;
2137 channels->max_tx = adapter->max_tx_queues;
2138 channels->max_other = 0;
2139 channels->max_combined = 0;
2140 channels->rx_count = adapter->req_rx_queues;
2141 channels->tx_count = adapter->req_tx_queues;
2142 channels->other_count = 0;
2143 channels->combined_count = 0;
2146 static int ibmvnic_set_channels(struct net_device *netdev,
2147 struct ethtool_channels *channels)
2149 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2151 adapter->desired.rx_queues = channels->rx_count;
2152 adapter->desired.tx_queues = channels->tx_count;
2154 return wait_for_reset(adapter);
2157 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2159 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2162 if (stringset != ETH_SS_STATS)
2165 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2166 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2168 for (i = 0; i < adapter->req_tx_queues; i++) {
2169 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2170 data += ETH_GSTRING_LEN;
2172 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2173 data += ETH_GSTRING_LEN;
2175 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2176 data += ETH_GSTRING_LEN;
2179 for (i = 0; i < adapter->req_rx_queues; i++) {
2180 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2181 data += ETH_GSTRING_LEN;
2183 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2184 data += ETH_GSTRING_LEN;
2186 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2187 data += ETH_GSTRING_LEN;
2191 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2193 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2197 return ARRAY_SIZE(ibmvnic_stats) +
2198 adapter->req_tx_queues * NUM_TX_STATS +
2199 adapter->req_rx_queues * NUM_RX_STATS;
2205 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2206 struct ethtool_stats *stats, u64 *data)
2208 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2209 union ibmvnic_crq crq;
2212 memset(&crq, 0, sizeof(crq));
2213 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2214 crq.request_statistics.cmd = REQUEST_STATISTICS;
2215 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2216 crq.request_statistics.len =
2217 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2219 /* Wait for data to be written */
2220 init_completion(&adapter->stats_done);
2221 ibmvnic_send_crq(adapter, &crq);
2222 wait_for_completion(&adapter->stats_done);
2224 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2225 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2226 ibmvnic_stats[i].offset));
2228 for (j = 0; j < adapter->req_tx_queues; j++) {
2229 data[i] = adapter->tx_stats_buffers[j].packets;
2231 data[i] = adapter->tx_stats_buffers[j].bytes;
2233 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2237 for (j = 0; j < adapter->req_rx_queues; j++) {
2238 data[i] = adapter->rx_stats_buffers[j].packets;
2240 data[i] = adapter->rx_stats_buffers[j].bytes;
2242 data[i] = adapter->rx_stats_buffers[j].interrupts;
2247 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2248 .get_drvinfo = ibmvnic_get_drvinfo,
2249 .get_msglevel = ibmvnic_get_msglevel,
2250 .set_msglevel = ibmvnic_set_msglevel,
2251 .get_link = ibmvnic_get_link,
2252 .get_ringparam = ibmvnic_get_ringparam,
2253 .set_ringparam = ibmvnic_set_ringparam,
2254 .get_channels = ibmvnic_get_channels,
2255 .set_channels = ibmvnic_set_channels,
2256 .get_strings = ibmvnic_get_strings,
2257 .get_sset_count = ibmvnic_get_sset_count,
2258 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2259 .get_link_ksettings = ibmvnic_get_link_ksettings,
2262 /* Routines for managing CRQs/sCRQs */
2264 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2265 struct ibmvnic_sub_crq_queue *scrq)
2270 free_irq(scrq->irq, scrq);
2271 irq_dispose_mapping(scrq->irq);
2275 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2278 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2279 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2283 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2287 for (i = 0; i < adapter->req_tx_queues; i++) {
2288 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2289 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2294 for (i = 0; i < adapter->req_rx_queues; i++) {
2295 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2296 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2304 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2305 struct ibmvnic_sub_crq_queue *scrq,
2308 struct device *dev = &adapter->vdev->dev;
2311 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2314 /* Close the sub-crqs */
2316 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2317 adapter->vdev->unit_address,
2319 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2322 netdev_err(adapter->netdev,
2323 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2328 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2330 free_pages((unsigned long)scrq->msgs, 2);
2334 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2337 struct device *dev = &adapter->vdev->dev;
2338 struct ibmvnic_sub_crq_queue *scrq;
2341 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2346 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2348 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2349 goto zero_page_failed;
2352 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2354 if (dma_mapping_error(dev, scrq->msg_token)) {
2355 dev_warn(dev, "Couldn't map crq queue messages page\n");
2359 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2360 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2362 if (rc == H_RESOURCE)
2363 rc = ibmvnic_reset_crq(adapter);
2365 if (rc == H_CLOSED) {
2366 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2368 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2372 scrq->adapter = adapter;
2373 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2374 spin_lock_init(&scrq->lock);
2376 netdev_dbg(adapter->netdev,
2377 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2378 scrq->crq_num, scrq->hw_irq, scrq->irq);
2383 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2386 free_pages((unsigned long)scrq->msgs, 2);
2393 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2397 if (adapter->tx_scrq) {
2398 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2399 if (!adapter->tx_scrq[i])
2402 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2404 if (adapter->tx_scrq[i]->irq) {
2405 free_irq(adapter->tx_scrq[i]->irq,
2406 adapter->tx_scrq[i]);
2407 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2408 adapter->tx_scrq[i]->irq = 0;
2411 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2415 kfree(adapter->tx_scrq);
2416 adapter->tx_scrq = NULL;
2417 adapter->num_active_tx_scrqs = 0;
2420 if (adapter->rx_scrq) {
2421 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2422 if (!adapter->rx_scrq[i])
2425 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2427 if (adapter->rx_scrq[i]->irq) {
2428 free_irq(adapter->rx_scrq[i]->irq,
2429 adapter->rx_scrq[i]);
2430 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2431 adapter->rx_scrq[i]->irq = 0;
2434 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2438 kfree(adapter->rx_scrq);
2439 adapter->rx_scrq = NULL;
2440 adapter->num_active_rx_scrqs = 0;
2444 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2445 struct ibmvnic_sub_crq_queue *scrq)
2447 struct device *dev = &adapter->vdev->dev;
2450 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2451 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2453 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2458 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2459 struct ibmvnic_sub_crq_queue *scrq)
2461 struct device *dev = &adapter->vdev->dev;
2464 if (scrq->hw_irq > 0x100000000ULL) {
2465 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2469 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2470 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2472 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2477 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2478 struct ibmvnic_sub_crq_queue *scrq)
2480 struct device *dev = &adapter->vdev->dev;
2481 struct ibmvnic_tx_buff *txbuff;
2482 union sub_crq *next;
2488 while (pending_scrq(adapter, scrq)) {
2489 unsigned int pool = scrq->pool_index;
2490 int num_entries = 0;
2492 next = ibmvnic_next_scrq(adapter, scrq);
2493 for (i = 0; i < next->tx_comp.num_comps; i++) {
2494 if (next->tx_comp.rcs[i]) {
2495 dev_err(dev, "tx error %x\n",
2496 next->tx_comp.rcs[i]);
2499 index = be32_to_cpu(next->tx_comp.correlators[i]);
2500 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2502 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2503 if (!txbuff->data_dma[j])
2506 txbuff->data_dma[j] = 0;
2508 /* if sub_crq was sent indirectly */
2509 first = txbuff->indir_arr[0].generic.first;
2510 if (first == IBMVNIC_CRQ_CMD) {
2511 dma_unmap_single(dev, txbuff->indir_dma,
2512 sizeof(txbuff->indir_arr),
2516 if (txbuff->last_frag) {
2517 dev_kfree_skb_any(txbuff->skb);
2521 num_entries += txbuff->num_entries;
2523 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2524 producer_index] = index;
2525 adapter->tx_pool[pool].producer_index =
2526 (adapter->tx_pool[pool].producer_index + 1) %
2527 adapter->req_tx_entries_per_subcrq;
2529 /* remove tx_comp scrq*/
2530 next->tx_comp.first = 0;
2532 if (atomic_sub_return(num_entries, &scrq->used) <=
2533 (adapter->req_tx_entries_per_subcrq / 2) &&
2534 __netif_subqueue_stopped(adapter->netdev,
2535 scrq->pool_index)) {
2536 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2537 netdev_dbg(adapter->netdev, "Started queue %d\n",
2542 enable_scrq_irq(adapter, scrq);
2544 if (pending_scrq(adapter, scrq)) {
2545 disable_scrq_irq(adapter, scrq);
2552 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2554 struct ibmvnic_sub_crq_queue *scrq = instance;
2555 struct ibmvnic_adapter *adapter = scrq->adapter;
2557 disable_scrq_irq(adapter, scrq);
2558 ibmvnic_complete_tx(adapter, scrq);
2563 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2565 struct ibmvnic_sub_crq_queue *scrq = instance;
2566 struct ibmvnic_adapter *adapter = scrq->adapter;
2568 /* When booting a kdump kernel we can hit pending interrupts
2569 * prior to completing driver initialization.
2571 if (unlikely(adapter->state != VNIC_OPEN))
2574 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2576 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2577 disable_scrq_irq(adapter, scrq);
2578 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2584 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2586 struct device *dev = &adapter->vdev->dev;
2587 struct ibmvnic_sub_crq_queue *scrq;
2591 for (i = 0; i < adapter->req_tx_queues; i++) {
2592 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2594 scrq = adapter->tx_scrq[i];
2595 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2599 dev_err(dev, "Error mapping irq\n");
2600 goto req_tx_irq_failed;
2603 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2604 0, "ibmvnic_tx", scrq);
2607 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2609 irq_dispose_mapping(scrq->irq);
2610 goto req_tx_irq_failed;
2614 for (i = 0; i < adapter->req_rx_queues; i++) {
2615 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2617 scrq = adapter->rx_scrq[i];
2618 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2621 dev_err(dev, "Error mapping irq\n");
2622 goto req_rx_irq_failed;
2624 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2625 0, "ibmvnic_rx", scrq);
2627 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2629 irq_dispose_mapping(scrq->irq);
2630 goto req_rx_irq_failed;
2636 for (j = 0; j < i; j++) {
2637 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2638 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2640 i = adapter->req_tx_queues;
2642 for (j = 0; j < i; j++) {
2643 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2644 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2646 release_sub_crqs(adapter, 1);
2650 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2652 struct device *dev = &adapter->vdev->dev;
2653 struct ibmvnic_sub_crq_queue **allqueues;
2654 int registered_queues = 0;
2659 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2661 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2665 for (i = 0; i < total_queues; i++) {
2666 allqueues[i] = init_sub_crq_queue(adapter);
2667 if (!allqueues[i]) {
2668 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2671 registered_queues++;
2674 /* Make sure we were able to register the minimum number of queues */
2675 if (registered_queues <
2676 adapter->min_tx_queues + adapter->min_rx_queues) {
2677 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2681 /* Distribute the failed allocated queues*/
2682 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2683 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2686 if (adapter->req_rx_queues > adapter->min_rx_queues)
2687 adapter->req_rx_queues--;
2692 if (adapter->req_tx_queues > adapter->min_tx_queues)
2693 adapter->req_tx_queues--;
2700 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2701 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2702 if (!adapter->tx_scrq)
2705 for (i = 0; i < adapter->req_tx_queues; i++) {
2706 adapter->tx_scrq[i] = allqueues[i];
2707 adapter->tx_scrq[i]->pool_index = i;
2708 adapter->num_active_tx_scrqs++;
2711 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2712 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2713 if (!adapter->rx_scrq)
2716 for (i = 0; i < adapter->req_rx_queues; i++) {
2717 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2718 adapter->rx_scrq[i]->scrq_num = i;
2719 adapter->num_active_rx_scrqs++;
2726 kfree(adapter->tx_scrq);
2727 adapter->tx_scrq = NULL;
2729 for (i = 0; i < registered_queues; i++)
2730 release_sub_crq_queue(adapter, allqueues[i], 1);
2735 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2737 struct device *dev = &adapter->vdev->dev;
2738 union ibmvnic_crq crq;
2742 /* Sub-CRQ entries are 32 byte long */
2743 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2745 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2746 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2747 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2751 if (adapter->desired.mtu)
2752 adapter->req_mtu = adapter->desired.mtu;
2754 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2756 if (!adapter->desired.tx_entries)
2757 adapter->desired.tx_entries =
2758 adapter->max_tx_entries_per_subcrq;
2759 if (!adapter->desired.rx_entries)
2760 adapter->desired.rx_entries =
2761 adapter->max_rx_add_entries_per_subcrq;
2763 max_entries = IBMVNIC_MAX_LTB_SIZE /
2764 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2766 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2767 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2768 adapter->desired.tx_entries = max_entries;
2771 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2772 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2773 adapter->desired.rx_entries = max_entries;
2776 if (adapter->desired.tx_entries)
2777 adapter->req_tx_entries_per_subcrq =
2778 adapter->desired.tx_entries;
2780 adapter->req_tx_entries_per_subcrq =
2781 adapter->max_tx_entries_per_subcrq;
2783 if (adapter->desired.rx_entries)
2784 adapter->req_rx_add_entries_per_subcrq =
2785 adapter->desired.rx_entries;
2787 adapter->req_rx_add_entries_per_subcrq =
2788 adapter->max_rx_add_entries_per_subcrq;
2790 if (adapter->desired.tx_queues)
2791 adapter->req_tx_queues =
2792 adapter->desired.tx_queues;
2794 adapter->req_tx_queues =
2795 adapter->opt_tx_comp_sub_queues;
2797 if (adapter->desired.rx_queues)
2798 adapter->req_rx_queues =
2799 adapter->desired.rx_queues;
2801 adapter->req_rx_queues =
2802 adapter->opt_rx_comp_queues;
2804 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2807 memset(&crq, 0, sizeof(crq));
2808 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2809 crq.request_capability.cmd = REQUEST_CAPABILITY;
2811 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2812 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2813 atomic_inc(&adapter->running_cap_crqs);
2814 ibmvnic_send_crq(adapter, &crq);
2816 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2817 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2818 atomic_inc(&adapter->running_cap_crqs);
2819 ibmvnic_send_crq(adapter, &crq);
2821 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2822 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2823 atomic_inc(&adapter->running_cap_crqs);
2824 ibmvnic_send_crq(adapter, &crq);
2826 crq.request_capability.capability =
2827 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2828 crq.request_capability.number =
2829 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2830 atomic_inc(&adapter->running_cap_crqs);
2831 ibmvnic_send_crq(adapter, &crq);
2833 crq.request_capability.capability =
2834 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2835 crq.request_capability.number =
2836 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2837 atomic_inc(&adapter->running_cap_crqs);
2838 ibmvnic_send_crq(adapter, &crq);
2840 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2841 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2842 atomic_inc(&adapter->running_cap_crqs);
2843 ibmvnic_send_crq(adapter, &crq);
2845 if (adapter->netdev->flags & IFF_PROMISC) {
2846 if (adapter->promisc_supported) {
2847 crq.request_capability.capability =
2848 cpu_to_be16(PROMISC_REQUESTED);
2849 crq.request_capability.number = cpu_to_be64(1);
2850 atomic_inc(&adapter->running_cap_crqs);
2851 ibmvnic_send_crq(adapter, &crq);
2854 crq.request_capability.capability =
2855 cpu_to_be16(PROMISC_REQUESTED);
2856 crq.request_capability.number = cpu_to_be64(0);
2857 atomic_inc(&adapter->running_cap_crqs);
2858 ibmvnic_send_crq(adapter, &crq);
2862 static int pending_scrq(struct ibmvnic_adapter *adapter,
2863 struct ibmvnic_sub_crq_queue *scrq)
2865 union sub_crq *entry = &scrq->msgs[scrq->cur];
2867 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2873 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2874 struct ibmvnic_sub_crq_queue *scrq)
2876 union sub_crq *entry;
2877 unsigned long flags;
2879 spin_lock_irqsave(&scrq->lock, flags);
2880 entry = &scrq->msgs[scrq->cur];
2881 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2882 if (++scrq->cur == scrq->size)
2887 spin_unlock_irqrestore(&scrq->lock, flags);
2892 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2894 struct ibmvnic_crq_queue *queue = &adapter->crq;
2895 union ibmvnic_crq *crq;
2897 crq = &queue->msgs[queue->cur];
2898 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2899 if (++queue->cur == queue->size)
2908 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2909 union sub_crq *sub_crq)
2911 unsigned int ua = adapter->vdev->unit_address;
2912 struct device *dev = &adapter->vdev->dev;
2913 u64 *u64_crq = (u64 *)sub_crq;
2916 netdev_dbg(adapter->netdev,
2917 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2918 (unsigned long int)cpu_to_be64(remote_handle),
2919 (unsigned long int)cpu_to_be64(u64_crq[0]),
2920 (unsigned long int)cpu_to_be64(u64_crq[1]),
2921 (unsigned long int)cpu_to_be64(u64_crq[2]),
2922 (unsigned long int)cpu_to_be64(u64_crq[3]));
2924 /* Make sure the hypervisor sees the complete request */
2927 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2928 cpu_to_be64(remote_handle),
2929 cpu_to_be64(u64_crq[0]),
2930 cpu_to_be64(u64_crq[1]),
2931 cpu_to_be64(u64_crq[2]),
2932 cpu_to_be64(u64_crq[3]));
2936 dev_warn(dev, "CRQ Queue closed\n");
2937 dev_err(dev, "Send error (rc=%d)\n", rc);
2943 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2944 u64 remote_handle, u64 ioba, u64 num_entries)
2946 unsigned int ua = adapter->vdev->unit_address;
2947 struct device *dev = &adapter->vdev->dev;
2950 /* Make sure the hypervisor sees the complete request */
2952 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2953 cpu_to_be64(remote_handle),
2958 dev_warn(dev, "CRQ Queue closed\n");
2959 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2965 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2966 union ibmvnic_crq *crq)
2968 unsigned int ua = adapter->vdev->unit_address;
2969 struct device *dev = &adapter->vdev->dev;
2970 u64 *u64_crq = (u64 *)crq;
2973 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2974 (unsigned long int)cpu_to_be64(u64_crq[0]),
2975 (unsigned long int)cpu_to_be64(u64_crq[1]));
2977 /* Make sure the hypervisor sees the complete request */
2980 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2981 cpu_to_be64(u64_crq[0]),
2982 cpu_to_be64(u64_crq[1]));
2985 if (rc == H_CLOSED) {
2986 dev_warn(dev, "CRQ Queue closed\n");
2987 if (adapter->resetting)
2988 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2991 dev_warn(dev, "Send error (rc=%d)\n", rc);
2997 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2999 union ibmvnic_crq crq;
3001 memset(&crq, 0, sizeof(crq));
3002 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3003 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3004 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3006 return ibmvnic_send_crq(adapter, &crq);
3009 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3011 union ibmvnic_crq crq;
3013 memset(&crq, 0, sizeof(crq));
3014 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3015 crq.version_exchange.cmd = VERSION_EXCHANGE;
3016 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3018 return ibmvnic_send_crq(adapter, &crq);
3021 struct vnic_login_client_data {
3027 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3031 /* Calculate the amount of buffer space needed for the
3032 * vnic client data in the login buffer. There are four entries,
3033 * OS name, LPAR name, device name, and a null last entry.
3035 len = 4 * sizeof(struct vnic_login_client_data);
3036 len += 6; /* "Linux" plus NULL */
3037 len += strlen(utsname()->nodename) + 1;
3038 len += strlen(adapter->netdev->name) + 1;
3043 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3044 struct vnic_login_client_data *vlcd)
3046 const char *os_name = "Linux";
3049 /* Type 1 - LPAR OS */
3051 len = strlen(os_name) + 1;
3052 vlcd->len = cpu_to_be16(len);
3053 strncpy(&vlcd->name, os_name, len);
3054 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3056 /* Type 2 - LPAR name */
3058 len = strlen(utsname()->nodename) + 1;
3059 vlcd->len = cpu_to_be16(len);
3060 strncpy(&vlcd->name, utsname()->nodename, len);
3061 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3063 /* Type 3 - device name */
3065 len = strlen(adapter->netdev->name) + 1;
3066 vlcd->len = cpu_to_be16(len);
3067 strncpy(&vlcd->name, adapter->netdev->name, len);
3070 static int send_login(struct ibmvnic_adapter *adapter)
3072 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3073 struct ibmvnic_login_buffer *login_buffer;
3074 struct device *dev = &adapter->vdev->dev;
3075 dma_addr_t rsp_buffer_token;
3076 dma_addr_t buffer_token;
3077 size_t rsp_buffer_size;
3078 union ibmvnic_crq crq;
3082 int client_data_len;
3083 struct vnic_login_client_data *vlcd;
3086 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3087 netdev_err(adapter->netdev,
3088 "RX or TX queues are not allocated, device login failed\n");
3092 release_login_rsp_buffer(adapter);
3093 client_data_len = vnic_client_data_len(adapter);
3096 sizeof(struct ibmvnic_login_buffer) +
3097 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3100 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3102 goto buf_alloc_failed;
3104 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3106 if (dma_mapping_error(dev, buffer_token)) {
3107 dev_err(dev, "Couldn't map login buffer\n");
3108 goto buf_map_failed;
3111 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3112 sizeof(u64) * adapter->req_tx_queues +
3113 sizeof(u64) * adapter->req_rx_queues +
3114 sizeof(u64) * adapter->req_rx_queues +
3115 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3117 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3118 if (!login_rsp_buffer)
3119 goto buf_rsp_alloc_failed;
3121 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3122 rsp_buffer_size, DMA_FROM_DEVICE);
3123 if (dma_mapping_error(dev, rsp_buffer_token)) {
3124 dev_err(dev, "Couldn't map login rsp buffer\n");
3125 goto buf_rsp_map_failed;
3128 adapter->login_buf = login_buffer;
3129 adapter->login_buf_token = buffer_token;
3130 adapter->login_buf_sz = buffer_size;
3131 adapter->login_rsp_buf = login_rsp_buffer;
3132 adapter->login_rsp_buf_token = rsp_buffer_token;
3133 adapter->login_rsp_buf_sz = rsp_buffer_size;
3135 login_buffer->len = cpu_to_be32(buffer_size);
3136 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3137 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3138 login_buffer->off_txcomp_subcrqs =
3139 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3140 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3141 login_buffer->off_rxcomp_subcrqs =
3142 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3143 sizeof(u64) * adapter->req_tx_queues);
3144 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3145 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3147 tx_list_p = (__be64 *)((char *)login_buffer +
3148 sizeof(struct ibmvnic_login_buffer));
3149 rx_list_p = (__be64 *)((char *)login_buffer +
3150 sizeof(struct ibmvnic_login_buffer) +
3151 sizeof(u64) * adapter->req_tx_queues);
3153 for (i = 0; i < adapter->req_tx_queues; i++) {
3154 if (adapter->tx_scrq[i]) {
3155 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3160 for (i = 0; i < adapter->req_rx_queues; i++) {
3161 if (adapter->rx_scrq[i]) {
3162 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3167 /* Insert vNIC login client data */
3168 vlcd = (struct vnic_login_client_data *)
3169 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3170 login_buffer->client_data_offset =
3171 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3172 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3174 vnic_add_client_data(adapter, vlcd);
3176 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3177 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3178 netdev_dbg(adapter->netdev, "%016lx\n",
3179 ((unsigned long int *)(adapter->login_buf))[i]);
3182 memset(&crq, 0, sizeof(crq));
3183 crq.login.first = IBMVNIC_CRQ_CMD;
3184 crq.login.cmd = LOGIN;
3185 crq.login.ioba = cpu_to_be32(buffer_token);
3186 crq.login.len = cpu_to_be32(buffer_size);
3187 ibmvnic_send_crq(adapter, &crq);
3192 kfree(login_rsp_buffer);
3193 buf_rsp_alloc_failed:
3194 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3196 kfree(login_buffer);
3201 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3204 union ibmvnic_crq crq;
3206 memset(&crq, 0, sizeof(crq));
3207 crq.request_map.first = IBMVNIC_CRQ_CMD;
3208 crq.request_map.cmd = REQUEST_MAP;
3209 crq.request_map.map_id = map_id;
3210 crq.request_map.ioba = cpu_to_be32(addr);
3211 crq.request_map.len = cpu_to_be32(len);
3212 ibmvnic_send_crq(adapter, &crq);
3215 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3217 union ibmvnic_crq crq;
3219 memset(&crq, 0, sizeof(crq));
3220 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3221 crq.request_unmap.cmd = REQUEST_UNMAP;
3222 crq.request_unmap.map_id = map_id;
3223 ibmvnic_send_crq(adapter, &crq);
3226 static void send_map_query(struct ibmvnic_adapter *adapter)
3228 union ibmvnic_crq crq;
3230 memset(&crq, 0, sizeof(crq));
3231 crq.query_map.first = IBMVNIC_CRQ_CMD;
3232 crq.query_map.cmd = QUERY_MAP;
3233 ibmvnic_send_crq(adapter, &crq);
3236 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3237 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3239 union ibmvnic_crq crq;
3241 atomic_set(&adapter->running_cap_crqs, 0);
3242 memset(&crq, 0, sizeof(crq));
3243 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3244 crq.query_capability.cmd = QUERY_CAPABILITY;
3246 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3247 atomic_inc(&adapter->running_cap_crqs);
3248 ibmvnic_send_crq(adapter, &crq);
3250 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3251 atomic_inc(&adapter->running_cap_crqs);
3252 ibmvnic_send_crq(adapter, &crq);
3254 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3255 atomic_inc(&adapter->running_cap_crqs);
3256 ibmvnic_send_crq(adapter, &crq);
3258 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3259 atomic_inc(&adapter->running_cap_crqs);
3260 ibmvnic_send_crq(adapter, &crq);
3262 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3263 atomic_inc(&adapter->running_cap_crqs);
3264 ibmvnic_send_crq(adapter, &crq);
3266 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3267 atomic_inc(&adapter->running_cap_crqs);
3268 ibmvnic_send_crq(adapter, &crq);
3270 crq.query_capability.capability =
3271 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3272 atomic_inc(&adapter->running_cap_crqs);
3273 ibmvnic_send_crq(adapter, &crq);
3275 crq.query_capability.capability =
3276 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3277 atomic_inc(&adapter->running_cap_crqs);
3278 ibmvnic_send_crq(adapter, &crq);
3280 crq.query_capability.capability =
3281 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3282 atomic_inc(&adapter->running_cap_crqs);
3283 ibmvnic_send_crq(adapter, &crq);
3285 crq.query_capability.capability =
3286 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3287 atomic_inc(&adapter->running_cap_crqs);
3288 ibmvnic_send_crq(adapter, &crq);
3290 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3291 atomic_inc(&adapter->running_cap_crqs);
3292 ibmvnic_send_crq(adapter, &crq);
3294 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3295 atomic_inc(&adapter->running_cap_crqs);
3296 ibmvnic_send_crq(adapter, &crq);
3298 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3299 atomic_inc(&adapter->running_cap_crqs);
3300 ibmvnic_send_crq(adapter, &crq);
3302 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3303 atomic_inc(&adapter->running_cap_crqs);
3304 ibmvnic_send_crq(adapter, &crq);
3306 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3307 atomic_inc(&adapter->running_cap_crqs);
3308 ibmvnic_send_crq(adapter, &crq);
3310 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3311 atomic_inc(&adapter->running_cap_crqs);
3312 ibmvnic_send_crq(adapter, &crq);
3314 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3315 atomic_inc(&adapter->running_cap_crqs);
3316 ibmvnic_send_crq(adapter, &crq);
3318 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3319 atomic_inc(&adapter->running_cap_crqs);
3320 ibmvnic_send_crq(adapter, &crq);
3322 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3323 atomic_inc(&adapter->running_cap_crqs);
3324 ibmvnic_send_crq(adapter, &crq);
3326 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3327 atomic_inc(&adapter->running_cap_crqs);
3328 ibmvnic_send_crq(adapter, &crq);
3330 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3331 atomic_inc(&adapter->running_cap_crqs);
3332 ibmvnic_send_crq(adapter, &crq);
3334 crq.query_capability.capability =
3335 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3336 atomic_inc(&adapter->running_cap_crqs);
3337 ibmvnic_send_crq(adapter, &crq);
3339 crq.query_capability.capability =
3340 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3341 atomic_inc(&adapter->running_cap_crqs);
3342 ibmvnic_send_crq(adapter, &crq);
3344 crq.query_capability.capability =
3345 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3346 atomic_inc(&adapter->running_cap_crqs);
3347 ibmvnic_send_crq(adapter, &crq);
3349 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3350 atomic_inc(&adapter->running_cap_crqs);
3351 ibmvnic_send_crq(adapter, &crq);
3354 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3355 struct ibmvnic_adapter *adapter)
3357 struct device *dev = &adapter->vdev->dev;
3359 if (crq->get_vpd_size_rsp.rc.code) {
3360 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3361 crq->get_vpd_size_rsp.rc.code);
3362 complete(&adapter->fw_done);
3366 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3367 complete(&adapter->fw_done);
3370 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3371 struct ibmvnic_adapter *adapter)
3373 struct device *dev = &adapter->vdev->dev;
3374 unsigned char *substr = NULL;
3375 u8 fw_level_len = 0;
3377 memset(adapter->fw_version, 0, 32);
3379 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3382 if (crq->get_vpd_rsp.rc.code) {
3383 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3384 crq->get_vpd_rsp.rc.code);
3388 /* get the position of the firmware version info
3389 * located after the ASCII 'RM' substring in the buffer
3391 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3393 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3397 /* get length of firmware level ASCII substring */
3398 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3399 fw_level_len = *(substr + 2);
3401 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3405 /* copy firmware version string from vpd into adapter */
3406 if ((substr + 3 + fw_level_len) <
3407 (adapter->vpd->buff + adapter->vpd->len)) {
3408 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3410 dev_info(dev, "FW substr extrapolated VPD buff\n");
3414 if (adapter->fw_version[0] == '\0')
3415 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3416 complete(&adapter->fw_done);
3419 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3421 struct device *dev = &adapter->vdev->dev;
3422 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3423 union ibmvnic_crq crq;
3426 dma_unmap_single(dev, adapter->ip_offload_tok,
3427 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3429 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3430 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3431 netdev_dbg(adapter->netdev, "%016lx\n",
3432 ((unsigned long int *)(buf))[i]);
3434 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3435 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3436 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3437 buf->tcp_ipv4_chksum);
3438 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3439 buf->tcp_ipv6_chksum);
3440 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3441 buf->udp_ipv4_chksum);
3442 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3443 buf->udp_ipv6_chksum);
3444 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3445 buf->large_tx_ipv4);
3446 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3447 buf->large_tx_ipv6);
3448 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3449 buf->large_rx_ipv4);
3450 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3451 buf->large_rx_ipv6);
3452 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3453 buf->max_ipv4_header_size);
3454 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3455 buf->max_ipv6_header_size);
3456 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3457 buf->max_tcp_header_size);
3458 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3459 buf->max_udp_header_size);
3460 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3461 buf->max_large_tx_size);
3462 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3463 buf->max_large_rx_size);
3464 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3465 buf->ipv6_extension_header);
3466 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3467 buf->tcp_pseudosum_req);
3468 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3469 buf->num_ipv6_ext_headers);
3470 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3471 buf->off_ipv6_ext_headers);
3473 adapter->ip_offload_ctrl_tok =
3474 dma_map_single(dev, &adapter->ip_offload_ctrl,
3475 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3477 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3478 dev_err(dev, "Couldn't map ip offload control buffer\n");
3482 adapter->ip_offload_ctrl.len =
3483 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3484 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3485 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3486 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3487 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3488 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3489 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3490 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3491 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3492 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3494 /* large_rx disabled for now, additional features needed */
3495 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3496 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3498 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3500 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3501 adapter->netdev->features |= NETIF_F_IP_CSUM;
3503 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3504 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3506 if ((adapter->netdev->features &
3507 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3508 adapter->netdev->features |= NETIF_F_RXCSUM;
3510 if (buf->large_tx_ipv4)
3511 adapter->netdev->features |= NETIF_F_TSO;
3512 if (buf->large_tx_ipv6)
3513 adapter->netdev->features |= NETIF_F_TSO6;
3515 adapter->netdev->hw_features |= adapter->netdev->features;
3517 memset(&crq, 0, sizeof(crq));
3518 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3519 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3520 crq.control_ip_offload.len =
3521 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3522 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3523 ibmvnic_send_crq(adapter, &crq);
3526 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3527 struct ibmvnic_adapter *adapter)
3529 struct device *dev = &adapter->vdev->dev;
3530 struct ibmvnic_error_buff *error_buff, *tmp;
3531 unsigned long flags;
3535 if (!crq->request_error_rsp.rc.code) {
3536 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3537 crq->request_error_rsp.rc.code);
3541 spin_lock_irqsave(&adapter->error_list_lock, flags);
3542 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3543 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3545 list_del(&error_buff->list);
3548 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3551 dev_err(dev, "Couldn't find error id %x\n",
3552 be32_to_cpu(crq->request_error_rsp.error_id));
3556 dev_err(dev, "Detailed info for error id %x:",
3557 be32_to_cpu(crq->request_error_rsp.error_id));
3559 for (i = 0; i < error_buff->len; i++) {
3560 pr_cont("%02x", (int)error_buff->buff[i]);
3566 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3568 kfree(error_buff->buff);
3572 static void request_error_information(struct ibmvnic_adapter *adapter,
3573 union ibmvnic_crq *err_crq)
3575 struct device *dev = &adapter->vdev->dev;
3576 struct net_device *netdev = adapter->netdev;
3577 struct ibmvnic_error_buff *error_buff;
3578 unsigned long timeout = msecs_to_jiffies(30000);
3579 union ibmvnic_crq crq;
3580 unsigned long flags;
3583 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3587 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3588 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3589 if (!error_buff->buff) {
3594 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3596 if (dma_mapping_error(dev, error_buff->dma)) {
3597 netdev_err(netdev, "Couldn't map error buffer\n");
3598 kfree(error_buff->buff);
3603 error_buff->len = detail_len;
3604 error_buff->error_id = err_crq->error_indication.error_id;
3606 spin_lock_irqsave(&adapter->error_list_lock, flags);
3607 list_add_tail(&error_buff->list, &adapter->errors);
3608 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3610 memset(&crq, 0, sizeof(crq));
3611 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3612 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3613 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3614 crq.request_error_info.len = cpu_to_be32(detail_len);
3615 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3617 rc = ibmvnic_send_crq(adapter, &crq);
3619 netdev_err(netdev, "failed to request error information\n");
3623 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3624 netdev_err(netdev, "timeout waiting for error information\n");
3631 spin_lock_irqsave(&adapter->error_list_lock, flags);
3632 list_del(&error_buff->list);
3633 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3635 kfree(error_buff->buff);
3639 static void handle_error_indication(union ibmvnic_crq *crq,
3640 struct ibmvnic_adapter *adapter)
3642 struct device *dev = &adapter->vdev->dev;
3644 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3645 crq->error_indication.flags
3646 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3647 be32_to_cpu(crq->error_indication.error_id),
3648 be16_to_cpu(crq->error_indication.error_cause));
3650 if (be32_to_cpu(crq->error_indication.error_id))
3651 request_error_information(adapter, crq);
3653 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3654 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3656 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3659 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3660 struct ibmvnic_adapter *adapter)
3662 struct net_device *netdev = adapter->netdev;
3663 struct device *dev = &adapter->vdev->dev;
3666 rc = crq->change_mac_addr_rsp.rc.code;
3668 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3671 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3674 complete(&adapter->fw_done);
3678 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3679 struct ibmvnic_adapter *adapter)
3681 struct device *dev = &adapter->vdev->dev;
3685 atomic_dec(&adapter->running_cap_crqs);
3686 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3688 req_value = &adapter->req_tx_queues;
3692 req_value = &adapter->req_rx_queues;
3695 case REQ_RX_ADD_QUEUES:
3696 req_value = &adapter->req_rx_add_queues;
3699 case REQ_TX_ENTRIES_PER_SUBCRQ:
3700 req_value = &adapter->req_tx_entries_per_subcrq;
3701 name = "tx_entries_per_subcrq";
3703 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3704 req_value = &adapter->req_rx_add_entries_per_subcrq;
3705 name = "rx_add_entries_per_subcrq";
3708 req_value = &adapter->req_mtu;
3711 case PROMISC_REQUESTED:
3712 req_value = &adapter->promisc;
3716 dev_err(dev, "Got invalid cap request rsp %d\n",
3717 crq->request_capability.capability);
3721 switch (crq->request_capability_rsp.rc.code) {
3724 case PARTIALSUCCESS:
3725 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3727 (long int)be64_to_cpu(crq->request_capability_rsp.
3730 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3732 pr_err("mtu of %llu is not supported. Reverting.\n",
3734 *req_value = adapter->fallback.mtu;
3737 be64_to_cpu(crq->request_capability_rsp.number);
3740 ibmvnic_send_req_caps(adapter, 1);
3743 dev_err(dev, "Error %d in request cap rsp\n",
3744 crq->request_capability_rsp.rc.code);
3748 /* Done receiving requested capabilities, query IP offload support */
3749 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3750 union ibmvnic_crq newcrq;
3751 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3752 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3753 &adapter->ip_offload_buf;
3755 adapter->wait_capability = false;
3756 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3760 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3761 if (!firmware_has_feature(FW_FEATURE_CMO))
3762 dev_err(dev, "Couldn't map offload buffer\n");
3766 memset(&newcrq, 0, sizeof(newcrq));
3767 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3768 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3769 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3770 newcrq.query_ip_offload.ioba =
3771 cpu_to_be32(adapter->ip_offload_tok);
3773 ibmvnic_send_crq(adapter, &newcrq);
3777 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3778 struct ibmvnic_adapter *adapter)
3780 struct device *dev = &adapter->vdev->dev;
3781 struct net_device *netdev = adapter->netdev;
3782 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3783 struct ibmvnic_login_buffer *login = adapter->login_buf;
3786 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3788 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3789 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3791 /* If the number of queues requested can't be allocated by the
3792 * server, the login response will return with code 1. We will need
3793 * to resend the login buffer with fewer queues requested.
3795 if (login_rsp_crq->generic.rc.code) {
3796 adapter->renegotiate = true;
3797 complete(&adapter->init_done);
3801 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3803 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3804 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3805 netdev_dbg(adapter->netdev, "%016lx\n",
3806 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3810 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3811 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3812 adapter->req_rx_add_queues !=
3813 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3814 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3815 ibmvnic_remove(adapter->vdev);
3818 release_login_buffer(adapter);
3819 complete(&adapter->init_done);
3824 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3825 struct ibmvnic_adapter *adapter)
3827 struct device *dev = &adapter->vdev->dev;
3830 rc = crq->request_unmap_rsp.rc.code;
3832 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3835 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3836 struct ibmvnic_adapter *adapter)
3838 struct net_device *netdev = adapter->netdev;
3839 struct device *dev = &adapter->vdev->dev;
3842 rc = crq->query_map_rsp.rc.code;
3844 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3847 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3848 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3849 crq->query_map_rsp.free_pages);
3852 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3853 struct ibmvnic_adapter *adapter)
3855 struct net_device *netdev = adapter->netdev;
3856 struct device *dev = &adapter->vdev->dev;
3859 atomic_dec(&adapter->running_cap_crqs);
3860 netdev_dbg(netdev, "Outstanding queries: %d\n",
3861 atomic_read(&adapter->running_cap_crqs));
3862 rc = crq->query_capability.rc.code;
3864 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3868 switch (be16_to_cpu(crq->query_capability.capability)) {
3870 adapter->min_tx_queues =
3871 be64_to_cpu(crq->query_capability.number);
3872 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3873 adapter->min_tx_queues);
3876 adapter->min_rx_queues =
3877 be64_to_cpu(crq->query_capability.number);
3878 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3879 adapter->min_rx_queues);
3881 case MIN_RX_ADD_QUEUES:
3882 adapter->min_rx_add_queues =
3883 be64_to_cpu(crq->query_capability.number);
3884 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3885 adapter->min_rx_add_queues);
3888 adapter->max_tx_queues =
3889 be64_to_cpu(crq->query_capability.number);
3890 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3891 adapter->max_tx_queues);
3894 adapter->max_rx_queues =
3895 be64_to_cpu(crq->query_capability.number);
3896 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3897 adapter->max_rx_queues);
3899 case MAX_RX_ADD_QUEUES:
3900 adapter->max_rx_add_queues =
3901 be64_to_cpu(crq->query_capability.number);
3902 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3903 adapter->max_rx_add_queues);
3905 case MIN_TX_ENTRIES_PER_SUBCRQ:
3906 adapter->min_tx_entries_per_subcrq =
3907 be64_to_cpu(crq->query_capability.number);
3908 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3909 adapter->min_tx_entries_per_subcrq);
3911 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3912 adapter->min_rx_add_entries_per_subcrq =
3913 be64_to_cpu(crq->query_capability.number);
3914 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3915 adapter->min_rx_add_entries_per_subcrq);
3917 case MAX_TX_ENTRIES_PER_SUBCRQ:
3918 adapter->max_tx_entries_per_subcrq =
3919 be64_to_cpu(crq->query_capability.number);
3920 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3921 adapter->max_tx_entries_per_subcrq);
3923 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3924 adapter->max_rx_add_entries_per_subcrq =
3925 be64_to_cpu(crq->query_capability.number);
3926 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3927 adapter->max_rx_add_entries_per_subcrq);
3929 case TCP_IP_OFFLOAD:
3930 adapter->tcp_ip_offload =
3931 be64_to_cpu(crq->query_capability.number);
3932 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3933 adapter->tcp_ip_offload);
3935 case PROMISC_SUPPORTED:
3936 adapter->promisc_supported =
3937 be64_to_cpu(crq->query_capability.number);
3938 netdev_dbg(netdev, "promisc_supported = %lld\n",
3939 adapter->promisc_supported);
3942 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3943 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3944 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3947 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3948 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3949 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3951 case MAX_MULTICAST_FILTERS:
3952 adapter->max_multicast_filters =
3953 be64_to_cpu(crq->query_capability.number);
3954 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3955 adapter->max_multicast_filters);
3957 case VLAN_HEADER_INSERTION:
3958 adapter->vlan_header_insertion =
3959 be64_to_cpu(crq->query_capability.number);
3960 if (adapter->vlan_header_insertion)
3961 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3962 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3963 adapter->vlan_header_insertion);
3965 case RX_VLAN_HEADER_INSERTION:
3966 adapter->rx_vlan_header_insertion =
3967 be64_to_cpu(crq->query_capability.number);
3968 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3969 adapter->rx_vlan_header_insertion);
3971 case MAX_TX_SG_ENTRIES:
3972 adapter->max_tx_sg_entries =
3973 be64_to_cpu(crq->query_capability.number);
3974 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3975 adapter->max_tx_sg_entries);
3977 case RX_SG_SUPPORTED:
3978 adapter->rx_sg_supported =
3979 be64_to_cpu(crq->query_capability.number);
3980 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3981 adapter->rx_sg_supported);
3983 case OPT_TX_COMP_SUB_QUEUES:
3984 adapter->opt_tx_comp_sub_queues =
3985 be64_to_cpu(crq->query_capability.number);
3986 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3987 adapter->opt_tx_comp_sub_queues);
3989 case OPT_RX_COMP_QUEUES:
3990 adapter->opt_rx_comp_queues =
3991 be64_to_cpu(crq->query_capability.number);
3992 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3993 adapter->opt_rx_comp_queues);
3995 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3996 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3997 be64_to_cpu(crq->query_capability.number);
3998 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3999 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4001 case OPT_TX_ENTRIES_PER_SUBCRQ:
4002 adapter->opt_tx_entries_per_subcrq =
4003 be64_to_cpu(crq->query_capability.number);
4004 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4005 adapter->opt_tx_entries_per_subcrq);
4007 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4008 adapter->opt_rxba_entries_per_subcrq =
4009 be64_to_cpu(crq->query_capability.number);
4010 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4011 adapter->opt_rxba_entries_per_subcrq);
4013 case TX_RX_DESC_REQ:
4014 adapter->tx_rx_desc_req = crq->query_capability.number;
4015 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4016 adapter->tx_rx_desc_req);
4020 netdev_err(netdev, "Got invalid cap rsp %d\n",
4021 crq->query_capability.capability);
4025 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4026 adapter->wait_capability = false;
4027 ibmvnic_send_req_caps(adapter, 0);
4031 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4032 struct ibmvnic_adapter *adapter)
4034 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4035 struct net_device *netdev = adapter->netdev;
4036 struct device *dev = &adapter->vdev->dev;
4037 u64 *u64_crq = (u64 *)crq;
4040 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4041 (unsigned long int)cpu_to_be64(u64_crq[0]),
4042 (unsigned long int)cpu_to_be64(u64_crq[1]));
4043 switch (gen_crq->first) {
4044 case IBMVNIC_CRQ_INIT_RSP:
4045 switch (gen_crq->cmd) {
4046 case IBMVNIC_CRQ_INIT:
4047 dev_info(dev, "Partner initialized\n");
4048 adapter->from_passive_init = true;
4049 complete(&adapter->init_done);
4051 case IBMVNIC_CRQ_INIT_COMPLETE:
4052 dev_info(dev, "Partner initialization complete\n");
4053 send_version_xchg(adapter);
4056 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4059 case IBMVNIC_CRQ_XPORT_EVENT:
4060 netif_carrier_off(netdev);
4061 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4062 dev_info(dev, "Migrated, re-enabling adapter\n");
4063 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4064 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4065 dev_info(dev, "Backing device failover detected\n");
4066 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4068 /* The adapter lost the connection */
4069 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4071 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4074 case IBMVNIC_CRQ_CMD_RSP:
4077 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4082 switch (gen_crq->cmd) {
4083 case VERSION_EXCHANGE_RSP:
4084 rc = crq->version_exchange_rsp.rc.code;
4086 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4089 dev_info(dev, "Partner protocol version is %d\n",
4090 crq->version_exchange_rsp.version);
4091 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4094 be16_to_cpu(crq->version_exchange_rsp.version);
4095 send_cap_queries(adapter);
4097 case QUERY_CAPABILITY_RSP:
4098 handle_query_cap_rsp(crq, adapter);
4101 handle_query_map_rsp(crq, adapter);
4103 case REQUEST_MAP_RSP:
4104 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4105 complete(&adapter->fw_done);
4107 case REQUEST_UNMAP_RSP:
4108 handle_request_unmap_rsp(crq, adapter);
4110 case REQUEST_CAPABILITY_RSP:
4111 handle_request_cap_rsp(crq, adapter);
4114 netdev_dbg(netdev, "Got Login Response\n");
4115 handle_login_rsp(crq, adapter);
4117 case LOGICAL_LINK_STATE_RSP:
4119 "Got Logical Link State Response, state: %d rc: %d\n",
4120 crq->logical_link_state_rsp.link_state,
4121 crq->logical_link_state_rsp.rc.code);
4122 adapter->logical_link_state =
4123 crq->logical_link_state_rsp.link_state;
4124 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4125 complete(&adapter->init_done);
4127 case LINK_STATE_INDICATION:
4128 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4129 adapter->phys_link_state =
4130 crq->link_state_indication.phys_link_state;
4131 adapter->logical_link_state =
4132 crq->link_state_indication.logical_link_state;
4134 case CHANGE_MAC_ADDR_RSP:
4135 netdev_dbg(netdev, "Got MAC address change Response\n");
4136 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4138 case ERROR_INDICATION:
4139 netdev_dbg(netdev, "Got Error Indication\n");
4140 handle_error_indication(crq, adapter);
4142 case REQUEST_ERROR_RSP:
4143 netdev_dbg(netdev, "Got Error Detail Response\n");
4144 handle_error_info_rsp(crq, adapter);
4146 case REQUEST_STATISTICS_RSP:
4147 netdev_dbg(netdev, "Got Statistics Response\n");
4148 complete(&adapter->stats_done);
4150 case QUERY_IP_OFFLOAD_RSP:
4151 netdev_dbg(netdev, "Got Query IP offload Response\n");
4152 handle_query_ip_offload_rsp(adapter);
4154 case MULTICAST_CTRL_RSP:
4155 netdev_dbg(netdev, "Got multicast control Response\n");
4157 case CONTROL_IP_OFFLOAD_RSP:
4158 netdev_dbg(netdev, "Got Control IP offload Response\n");
4159 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4160 sizeof(adapter->ip_offload_ctrl),
4162 complete(&adapter->init_done);
4164 case COLLECT_FW_TRACE_RSP:
4165 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4166 complete(&adapter->fw_done);
4168 case GET_VPD_SIZE_RSP:
4169 handle_vpd_size_rsp(crq, adapter);
4172 handle_vpd_rsp(crq, adapter);
4175 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4180 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4182 struct ibmvnic_adapter *adapter = instance;
4184 tasklet_schedule(&adapter->tasklet);
4188 static void ibmvnic_tasklet(void *data)
4190 struct ibmvnic_adapter *adapter = data;
4191 struct ibmvnic_crq_queue *queue = &adapter->crq;
4192 union ibmvnic_crq *crq;
4193 unsigned long flags;
4196 spin_lock_irqsave(&queue->lock, flags);
4198 /* Pull all the valid messages off the CRQ */
4199 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4200 ibmvnic_handle_crq(crq, adapter);
4201 crq->generic.first = 0;
4204 /* remain in tasklet until all
4205 * capabilities responses are received
4207 if (!adapter->wait_capability)
4210 /* if capabilities CRQ's were sent in this tasklet, the following
4211 * tasklet must wait until all responses are received
4213 if (atomic_read(&adapter->running_cap_crqs) != 0)
4214 adapter->wait_capability = true;
4215 spin_unlock_irqrestore(&queue->lock, flags);
4218 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4220 struct vio_dev *vdev = adapter->vdev;
4224 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4225 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4228 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4233 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4235 struct ibmvnic_crq_queue *crq = &adapter->crq;
4236 struct device *dev = &adapter->vdev->dev;
4237 struct vio_dev *vdev = adapter->vdev;
4242 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4243 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4245 /* Clean out the queue */
4246 memset(crq->msgs, 0, PAGE_SIZE);
4249 /* And re-open it again */
4250 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4251 crq->msg_token, PAGE_SIZE);
4254 /* Adapter is good, but other end is not ready */
4255 dev_warn(dev, "Partner adapter not ready\n");
4257 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4262 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4264 struct ibmvnic_crq_queue *crq = &adapter->crq;
4265 struct vio_dev *vdev = adapter->vdev;
4271 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4272 free_irq(vdev->irq, adapter);
4273 tasklet_kill(&adapter->tasklet);
4275 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4276 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4278 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4280 free_page((unsigned long)crq->msgs);
4284 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4286 struct ibmvnic_crq_queue *crq = &adapter->crq;
4287 struct device *dev = &adapter->vdev->dev;
4288 struct vio_dev *vdev = adapter->vdev;
4289 int rc, retrc = -ENOMEM;
4294 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4295 /* Should we allocate more than one page? */
4300 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4301 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4303 if (dma_mapping_error(dev, crq->msg_token))
4306 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4307 crq->msg_token, PAGE_SIZE);
4309 if (rc == H_RESOURCE)
4310 /* maybe kexecing and resource is busy. try a reset */
4311 rc = ibmvnic_reset_crq(adapter);
4314 if (rc == H_CLOSED) {
4315 dev_warn(dev, "Partner adapter not ready\n");
4317 dev_warn(dev, "Error %d opening adapter\n", rc);
4318 goto reg_crq_failed;
4323 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4324 (unsigned long)adapter);
4326 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4327 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4330 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4332 goto req_irq_failed;
4335 rc = vio_enable_interrupts(vdev);
4337 dev_err(dev, "Error %d enabling interrupts\n", rc);
4338 goto req_irq_failed;
4342 spin_lock_init(&crq->lock);
4347 tasklet_kill(&adapter->tasklet);
4349 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4350 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4352 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4354 free_page((unsigned long)crq->msgs);
4359 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4361 struct device *dev = &adapter->vdev->dev;
4362 unsigned long timeout = msecs_to_jiffies(30000);
4363 u64 old_num_rx_queues, old_num_tx_queues;
4366 if (adapter->resetting && !adapter->wait_for_reset) {
4367 rc = ibmvnic_reset_crq(adapter);
4369 rc = vio_enable_interrupts(adapter->vdev);
4371 rc = init_crq_queue(adapter);
4375 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4379 adapter->from_passive_init = false;
4381 old_num_rx_queues = adapter->req_rx_queues;
4382 old_num_tx_queues = adapter->req_tx_queues;
4384 init_completion(&adapter->init_done);
4385 adapter->init_done_rc = 0;
4386 ibmvnic_send_crq_init(adapter);
4387 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4388 dev_err(dev, "Initialization sequence timed out\n");
4392 if (adapter->init_done_rc) {
4393 release_crq_queue(adapter);
4394 return adapter->init_done_rc;
4397 if (adapter->from_passive_init) {
4398 adapter->state = VNIC_OPEN;
4399 adapter->from_passive_init = false;
4403 if (adapter->resetting && !adapter->wait_for_reset) {
4404 if (adapter->req_rx_queues != old_num_rx_queues ||
4405 adapter->req_tx_queues != old_num_tx_queues) {
4406 release_sub_crqs(adapter, 0);
4407 rc = init_sub_crqs(adapter);
4409 rc = reset_sub_crq_queues(adapter);
4412 rc = init_sub_crqs(adapter);
4416 dev_err(dev, "Initialization of sub crqs failed\n");
4417 release_crq_queue(adapter);
4421 rc = init_sub_crq_irqs(adapter);
4423 dev_err(dev, "Failed to initialize sub crq irqs\n");
4424 release_crq_queue(adapter);
4427 rc = init_stats_buffers(adapter);
4431 rc = init_stats_token(adapter);
4438 static struct device_attribute dev_attr_failover;
4440 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4442 struct ibmvnic_adapter *adapter;
4443 struct net_device *netdev;
4444 unsigned char *mac_addr_p;
4447 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4450 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4451 VETH_MAC_ADDR, NULL);
4454 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4455 __FILE__, __LINE__);
4459 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4460 IBMVNIC_MAX_QUEUES);
4464 adapter = netdev_priv(netdev);
4465 adapter->state = VNIC_PROBING;
4466 dev_set_drvdata(&dev->dev, netdev);
4467 adapter->vdev = dev;
4468 adapter->netdev = netdev;
4470 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4471 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4472 netdev->irq = dev->irq;
4473 netdev->netdev_ops = &ibmvnic_netdev_ops;
4474 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4475 SET_NETDEV_DEV(netdev, &dev->dev);
4477 spin_lock_init(&adapter->stats_lock);
4479 INIT_LIST_HEAD(&adapter->errors);
4480 spin_lock_init(&adapter->error_list_lock);
4482 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4483 INIT_LIST_HEAD(&adapter->rwi_list);
4484 mutex_init(&adapter->reset_lock);
4485 mutex_init(&adapter->rwi_lock);
4486 adapter->resetting = false;
4488 adapter->mac_change_pending = false;
4491 rc = ibmvnic_init(adapter);
4492 if (rc && rc != EAGAIN)
4493 goto ibmvnic_init_fail;
4494 } while (rc == EAGAIN);
4496 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4497 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4498 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4500 rc = device_create_file(&dev->dev, &dev_attr_failover);
4502 goto ibmvnic_init_fail;
4504 netif_carrier_off(netdev);
4505 rc = register_netdev(netdev);
4507 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4508 goto ibmvnic_register_fail;
4510 dev_info(&dev->dev, "ibmvnic registered\n");
4512 adapter->state = VNIC_PROBED;
4514 adapter->wait_for_reset = false;
4518 ibmvnic_register_fail:
4519 device_remove_file(&dev->dev, &dev_attr_failover);
4522 release_sub_crqs(adapter, 1);
4523 release_crq_queue(adapter);
4524 free_netdev(netdev);
4529 static int ibmvnic_remove(struct vio_dev *dev)
4531 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4532 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4534 adapter->state = VNIC_REMOVING;
4535 unregister_netdev(netdev);
4536 mutex_lock(&adapter->reset_lock);
4538 release_resources(adapter);
4539 release_sub_crqs(adapter, 1);
4540 release_crq_queue(adapter);
4542 release_stats_token(adapter);
4543 release_stats_buffers(adapter);
4545 adapter->state = VNIC_REMOVED;
4547 mutex_unlock(&adapter->reset_lock);
4548 device_remove_file(&dev->dev, &dev_attr_failover);
4549 free_netdev(netdev);
4550 dev_set_drvdata(&dev->dev, NULL);
4555 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4556 const char *buf, size_t count)
4558 struct net_device *netdev = dev_get_drvdata(dev);
4559 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4560 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4561 __be64 session_token;
4564 if (!sysfs_streq(buf, "1"))
4567 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4568 H_GET_SESSION_TOKEN, 0, 0, 0);
4570 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4575 session_token = (__be64)retbuf[0];
4576 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4577 be64_to_cpu(session_token));
4578 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4579 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4581 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4589 static DEVICE_ATTR_WO(failover);
4591 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4593 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4594 struct ibmvnic_adapter *adapter;
4595 struct iommu_table *tbl;
4596 unsigned long ret = 0;
4599 tbl = get_iommu_table_base(&vdev->dev);
4601 /* netdev inits at probe time along with the structures we need below*/
4603 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4605 adapter = netdev_priv(netdev);
4607 ret += PAGE_SIZE; /* the crq message queue */
4608 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4610 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4611 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4613 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4615 ret += adapter->rx_pool[i].size *
4616 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4621 static int ibmvnic_resume(struct device *dev)
4623 struct net_device *netdev = dev_get_drvdata(dev);
4624 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4626 if (adapter->state != VNIC_OPEN)
4629 tasklet_schedule(&adapter->tasklet);
4634 static const struct vio_device_id ibmvnic_device_table[] = {
4635 {"network", "IBM,vnic"},
4638 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4640 static const struct dev_pm_ops ibmvnic_pm_ops = {
4641 .resume = ibmvnic_resume
4644 static struct vio_driver ibmvnic_driver = {
4645 .id_table = ibmvnic_device_table,
4646 .probe = ibmvnic_probe,
4647 .remove = ibmvnic_remove,
4648 .get_desired_dma = ibmvnic_get_desired_dma,
4649 .name = ibmvnic_driver_name,
4650 .pm = &ibmvnic_pm_ops,
4653 /* module functions */
4654 static int __init ibmvnic_module_init(void)
4656 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4657 IBMVNIC_DRIVER_VERSION);
4659 return vio_register_driver(&ibmvnic_driver);
4662 static void __exit ibmvnic_module_exit(void)
4664 vio_unregister_driver(&ibmvnic_driver);
4667 module_init(ibmvnic_module_init);
4668 module_exit(ibmvnic_module_exit);