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 void send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
122 struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
131 static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
173 struct device *dev = &adapter->vdev->dev;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
180 dev_err(dev, "Couldn't alloc long term buffer\n");
183 ltb->map_id = adapter->map_id;
186 init_completion(&adapter->fw_done);
187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
189 wait_for_completion(&adapter->fw_done);
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
202 struct device *dev = &adapter->vdev->dev;
207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
209 send_request_unmap(adapter, ltb->map_id);
210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
216 memset(ltb->buff, 0, ltb->size);
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
237 adapter->rx_pool[i].active = 0;
240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
272 index = pool->free_map[pool->next_free];
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
302 #ifdef __LITTLE_ENDIAN__
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
309 if (lpar_rc != H_SUCCESS)
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
316 atomic_add(buffers_added, &pool->available);
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
361 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
363 adapter->tx_stats_buffers =
364 kcalloc(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);
813 if (!wait_for_completion_timeout(&adapter->init_done,
815 dev_err(dev, "Login timeout\n");
818 } while (adapter->renegotiate);
820 /* handle pending MAC address changes after successful login */
821 if (adapter->mac_change_pending) {
822 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
823 adapter->mac_change_pending = false;
829 static void release_login_buffer(struct ibmvnic_adapter *adapter)
831 kfree(adapter->login_buf);
832 adapter->login_buf = NULL;
835 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
837 kfree(adapter->login_rsp_buf);
838 adapter->login_rsp_buf = NULL;
841 static void release_resources(struct ibmvnic_adapter *adapter)
843 release_vpd_data(adapter);
845 release_tx_pools(adapter);
846 release_rx_pools(adapter);
848 release_stats_token(adapter);
849 release_stats_buffers(adapter);
850 release_error_buffers(adapter);
851 release_napi(adapter);
852 release_login_rsp_buffer(adapter);
855 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
857 struct net_device *netdev = adapter->netdev;
858 unsigned long timeout = msecs_to_jiffies(30000);
859 union ibmvnic_crq crq;
863 netdev_dbg(netdev, "setting link state %d\n", link_state);
865 memset(&crq, 0, sizeof(crq));
866 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
867 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
868 crq.logical_link_state.link_state = link_state;
873 reinit_completion(&adapter->init_done);
874 rc = ibmvnic_send_crq(adapter, &crq);
876 netdev_err(netdev, "Failed to set link state\n");
880 if (!wait_for_completion_timeout(&adapter->init_done,
882 netdev_err(netdev, "timeout setting link state\n");
886 if (adapter->init_done_rc == 1) {
887 /* Partuial success, delay and re-send */
896 static int set_real_num_queues(struct net_device *netdev)
898 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
901 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
902 adapter->req_tx_queues, adapter->req_rx_queues);
904 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
906 netdev_err(netdev, "failed to set the number of tx queues\n");
910 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
912 netdev_err(netdev, "failed to set the number of rx queues\n");
917 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
919 struct device *dev = &adapter->vdev->dev;
920 union ibmvnic_crq crq;
923 if (adapter->vpd->buff)
924 len = adapter->vpd->len;
926 init_completion(&adapter->fw_done);
927 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
928 crq.get_vpd_size.cmd = GET_VPD_SIZE;
929 ibmvnic_send_crq(adapter, &crq);
930 wait_for_completion(&adapter->fw_done);
932 if (!adapter->vpd->len)
935 if (!adapter->vpd->buff)
936 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
937 else if (adapter->vpd->len != len)
939 krealloc(adapter->vpd->buff,
940 adapter->vpd->len, GFP_KERNEL);
942 if (!adapter->vpd->buff) {
943 dev_err(dev, "Could allocate VPD buffer\n");
947 adapter->vpd->dma_addr =
948 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
950 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
951 dev_err(dev, "Could not map VPD buffer\n");
952 kfree(adapter->vpd->buff);
953 adapter->vpd->buff = NULL;
957 reinit_completion(&adapter->fw_done);
958 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
959 crq.get_vpd.cmd = GET_VPD;
960 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
961 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
962 ibmvnic_send_crq(adapter, &crq);
963 wait_for_completion(&adapter->fw_done);
968 static int init_resources(struct ibmvnic_adapter *adapter)
970 struct net_device *netdev = adapter->netdev;
973 rc = set_real_num_queues(netdev);
977 rc = init_stats_buffers(adapter);
981 rc = init_stats_token(adapter);
985 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
989 /* Vital Product Data (VPD) */
990 rc = ibmvnic_get_vpd(adapter);
992 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
998 rc = init_napi(adapter);
1002 send_map_query(adapter);
1004 rc = init_rx_pools(netdev);
1008 rc = init_tx_pools(netdev);
1012 static int __ibmvnic_open(struct net_device *netdev)
1014 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1015 enum vnic_state prev_state = adapter->state;
1018 adapter->state = VNIC_OPENING;
1019 replenish_pools(adapter);
1020 ibmvnic_napi_enable(adapter);
1022 /* We're ready to receive frames, enable the sub-crq interrupts and
1023 * set the logical link state to up
1025 for (i = 0; i < adapter->req_rx_queues; i++) {
1026 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1027 if (prev_state == VNIC_CLOSED)
1028 enable_irq(adapter->rx_scrq[i]->irq);
1030 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1033 for (i = 0; i < adapter->req_tx_queues; i++) {
1034 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1035 if (prev_state == VNIC_CLOSED)
1036 enable_irq(adapter->tx_scrq[i]->irq);
1038 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1041 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1043 for (i = 0; i < adapter->req_rx_queues; i++)
1044 napi_disable(&adapter->napi[i]);
1045 release_resources(adapter);
1049 netif_tx_start_all_queues(netdev);
1051 if (prev_state == VNIC_CLOSED) {
1052 for (i = 0; i < adapter->req_rx_queues; i++)
1053 napi_schedule(&adapter->napi[i]);
1056 adapter->state = VNIC_OPEN;
1060 static int ibmvnic_open(struct net_device *netdev)
1062 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1065 mutex_lock(&adapter->reset_lock);
1067 if (adapter->state != VNIC_CLOSED) {
1068 rc = ibmvnic_login(netdev);
1070 mutex_unlock(&adapter->reset_lock);
1074 rc = init_resources(adapter);
1076 netdev_err(netdev, "failed to initialize resources\n");
1077 release_resources(adapter);
1078 mutex_unlock(&adapter->reset_lock);
1083 rc = __ibmvnic_open(netdev);
1084 netif_carrier_on(netdev);
1086 mutex_unlock(&adapter->reset_lock);
1091 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1093 struct ibmvnic_rx_pool *rx_pool;
1098 if (!adapter->rx_pool)
1101 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1102 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1104 /* Free any remaining skbs in the rx buffer pools */
1105 for (i = 0; i < rx_scrqs; i++) {
1106 rx_pool = &adapter->rx_pool[i];
1110 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1111 for (j = 0; j < rx_entries; j++) {
1112 if (rx_pool->rx_buff[j].skb) {
1113 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
1114 rx_pool->rx_buff[j].skb = NULL;
1120 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1122 struct ibmvnic_tx_pool *tx_pool;
1127 if (!adapter->tx_pool)
1130 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1131 tx_entries = adapter->req_tx_entries_per_subcrq;
1133 /* Free any remaining skbs in the tx buffer pools */
1134 for (i = 0; i < tx_scrqs; i++) {
1135 tx_pool = &adapter->tx_pool[i];
1139 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1140 for (j = 0; j < tx_entries; j++) {
1141 if (tx_pool->tx_buff[j].skb) {
1142 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1143 tx_pool->tx_buff[j].skb = NULL;
1149 static int __ibmvnic_close(struct net_device *netdev)
1151 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1155 adapter->state = VNIC_CLOSING;
1157 /* ensure that transmissions are stopped if called by do_reset */
1158 if (adapter->resetting)
1159 netif_tx_disable(netdev);
1161 netif_tx_stop_all_queues(netdev);
1163 ibmvnic_napi_disable(adapter);
1165 if (adapter->tx_scrq) {
1166 for (i = 0; i < adapter->req_tx_queues; i++)
1167 if (adapter->tx_scrq[i]->irq) {
1168 netdev_dbg(adapter->netdev,
1169 "Disabling tx_scrq[%d] irq\n", i);
1170 disable_irq(adapter->tx_scrq[i]->irq);
1174 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1178 if (adapter->rx_scrq) {
1179 for (i = 0; i < adapter->req_rx_queues; i++) {
1182 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1190 if (adapter->rx_scrq[i]->irq) {
1191 netdev_dbg(adapter->netdev,
1192 "Disabling rx_scrq[%d] irq\n", i);
1193 disable_irq(adapter->rx_scrq[i]->irq);
1197 clean_rx_pools(adapter);
1198 clean_tx_pools(adapter);
1199 adapter->state = VNIC_CLOSED;
1203 static int ibmvnic_close(struct net_device *netdev)
1205 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1208 mutex_lock(&adapter->reset_lock);
1209 rc = __ibmvnic_close(netdev);
1210 mutex_unlock(&adapter->reset_lock);
1216 * build_hdr_data - creates L2/L3/L4 header data buffer
1217 * @hdr_field - bitfield determining needed headers
1218 * @skb - socket buffer
1219 * @hdr_len - array of header lengths
1220 * @tot_len - total length of data
1222 * Reads hdr_field to determine which headers are needed by firmware.
1223 * Builds a buffer containing these headers. Saves individual header
1224 * lengths and total buffer length to be used to build descriptors.
1226 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1227 int *hdr_len, u8 *hdr_data)
1232 hdr_len[0] = sizeof(struct ethhdr);
1234 if (skb->protocol == htons(ETH_P_IP)) {
1235 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1236 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1237 hdr_len[2] = tcp_hdrlen(skb);
1238 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1239 hdr_len[2] = sizeof(struct udphdr);
1240 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1241 hdr_len[1] = sizeof(struct ipv6hdr);
1242 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1243 hdr_len[2] = tcp_hdrlen(skb);
1244 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1245 hdr_len[2] = sizeof(struct udphdr);
1246 } else if (skb->protocol == htons(ETH_P_ARP)) {
1247 hdr_len[1] = arp_hdr_len(skb->dev);
1251 memset(hdr_data, 0, 120);
1252 if ((hdr_field >> 6) & 1) {
1253 hdr = skb_mac_header(skb);
1254 memcpy(hdr_data, hdr, hdr_len[0]);
1258 if ((hdr_field >> 5) & 1) {
1259 hdr = skb_network_header(skb);
1260 memcpy(hdr_data + len, hdr, hdr_len[1]);
1264 if ((hdr_field >> 4) & 1) {
1265 hdr = skb_transport_header(skb);
1266 memcpy(hdr_data + len, hdr, hdr_len[2]);
1273 * create_hdr_descs - create header and header extension descriptors
1274 * @hdr_field - bitfield determining needed headers
1275 * @data - buffer containing header data
1276 * @len - length of data buffer
1277 * @hdr_len - array of individual header lengths
1278 * @scrq_arr - descriptor array
1280 * Creates header and, if needed, header extension descriptors and
1281 * places them in a descriptor array, scrq_arr
1284 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1285 union sub_crq *scrq_arr)
1287 union sub_crq hdr_desc;
1293 while (tmp_len > 0) {
1294 cur = hdr_data + len - tmp_len;
1296 memset(&hdr_desc, 0, sizeof(hdr_desc));
1297 if (cur != hdr_data) {
1298 data = hdr_desc.hdr_ext.data;
1299 tmp = tmp_len > 29 ? 29 : tmp_len;
1300 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1301 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1302 hdr_desc.hdr_ext.len = tmp;
1304 data = hdr_desc.hdr.data;
1305 tmp = tmp_len > 24 ? 24 : tmp_len;
1306 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1307 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1308 hdr_desc.hdr.len = tmp;
1309 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1310 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1311 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1312 hdr_desc.hdr.flag = hdr_field << 1;
1314 memcpy(data, cur, tmp);
1316 *scrq_arr = hdr_desc;
1325 * build_hdr_descs_arr - build a header descriptor array
1326 * @skb - socket buffer
1327 * @num_entries - number of descriptors to be sent
1328 * @subcrq - first TX descriptor
1329 * @hdr_field - bit field determining which headers will be sent
1331 * This function will build a TX descriptor array with applicable
1332 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1335 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1336 int *num_entries, u8 hdr_field)
1338 int hdr_len[3] = {0, 0, 0};
1340 u8 *hdr_data = txbuff->hdr_data;
1342 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1344 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1345 txbuff->indir_arr + 1);
1348 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1350 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1351 int queue_num = skb_get_queue_mapping(skb);
1352 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1353 struct device *dev = &adapter->vdev->dev;
1354 struct ibmvnic_tx_buff *tx_buff = NULL;
1355 struct ibmvnic_sub_crq_queue *tx_scrq;
1356 struct ibmvnic_tx_pool *tx_pool;
1357 unsigned int tx_send_failed = 0;
1358 unsigned int tx_map_failed = 0;
1359 unsigned int tx_dropped = 0;
1360 unsigned int tx_packets = 0;
1361 unsigned int tx_bytes = 0;
1362 dma_addr_t data_dma_addr;
1363 struct netdev_queue *txq;
1364 unsigned long lpar_rc;
1365 union sub_crq tx_crq;
1366 unsigned int offset;
1367 int num_entries = 1;
1374 if (adapter->resetting) {
1375 if (!netif_subqueue_stopped(netdev, skb))
1376 netif_stop_subqueue(netdev, queue_num);
1377 dev_kfree_skb_any(skb);
1385 tx_pool = &adapter->tx_pool[queue_num];
1386 tx_scrq = adapter->tx_scrq[queue_num];
1387 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1388 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1389 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1391 index = tx_pool->free_map[tx_pool->consumer_index];
1393 if (skb_is_gso(skb)) {
1394 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1395 dst = tx_pool->tso_ltb.buff + offset;
1396 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1397 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1398 tx_pool->tso_index++;
1399 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1400 tx_pool->tso_index = 0;
1402 offset = index * adapter->req_mtu;
1403 dst = tx_pool->long_term_buff.buff + offset;
1404 memset(dst, 0, adapter->req_mtu);
1405 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1408 if (skb_shinfo(skb)->nr_frags) {
1412 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1413 cur = skb_headlen(skb);
1415 /* Copy the frags */
1416 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1417 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1420 page_address(skb_frag_page(frag)) +
1421 frag->page_offset, skb_frag_size(frag));
1422 cur += skb_frag_size(frag);
1425 skb_copy_from_linear_data(skb, dst, skb->len);
1428 tx_pool->consumer_index =
1429 (tx_pool->consumer_index + 1) %
1430 adapter->req_tx_entries_per_subcrq;
1432 tx_buff = &tx_pool->tx_buff[index];
1434 tx_buff->data_dma[0] = data_dma_addr;
1435 tx_buff->data_len[0] = skb->len;
1436 tx_buff->index = index;
1437 tx_buff->pool_index = queue_num;
1438 tx_buff->last_frag = true;
1440 memset(&tx_crq, 0, sizeof(tx_crq));
1441 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1442 tx_crq.v1.type = IBMVNIC_TX_DESC;
1443 tx_crq.v1.n_crq_elem = 1;
1444 tx_crq.v1.n_sge = 1;
1445 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1446 tx_crq.v1.correlator = cpu_to_be32(index);
1447 if (skb_is_gso(skb))
1448 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1450 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1451 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1452 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1454 if (adapter->vlan_header_insertion) {
1455 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1456 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1459 if (skb->protocol == htons(ETH_P_IP)) {
1460 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1461 proto = ip_hdr(skb)->protocol;
1462 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1463 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1464 proto = ipv6_hdr(skb)->nexthdr;
1467 if (proto == IPPROTO_TCP)
1468 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1469 else if (proto == IPPROTO_UDP)
1470 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1472 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1473 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1476 if (skb_is_gso(skb)) {
1477 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1478 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1481 /* determine if l2/3/4 headers are sent to firmware */
1482 if ((*hdrs >> 7) & 1) {
1483 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1484 tx_crq.v1.n_crq_elem = num_entries;
1485 tx_buff->indir_arr[0] = tx_crq;
1486 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1487 sizeof(tx_buff->indir_arr),
1489 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1490 dev_kfree_skb_any(skb);
1491 tx_buff->skb = NULL;
1492 if (!firmware_has_feature(FW_FEATURE_CMO))
1493 dev_err(dev, "tx: unable to map descriptor array\n");
1499 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1500 (u64)tx_buff->indir_dma,
1503 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1506 if (lpar_rc != H_SUCCESS) {
1507 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1509 if (tx_pool->consumer_index == 0)
1510 tx_pool->consumer_index =
1511 adapter->req_tx_entries_per_subcrq - 1;
1513 tx_pool->consumer_index--;
1515 dev_kfree_skb_any(skb);
1516 tx_buff->skb = NULL;
1518 if (lpar_rc == H_CLOSED) {
1519 /* Disable TX and report carrier off if queue is closed.
1520 * Firmware guarantees that a signal will be sent to the
1521 * driver, triggering a reset or some other action.
1523 netif_tx_stop_all_queues(netdev);
1524 netif_carrier_off(netdev);
1533 if (atomic_add_return(num_entries, &tx_scrq->used)
1534 >= adapter->req_tx_entries_per_subcrq) {
1535 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1536 netif_stop_subqueue(netdev, queue_num);
1539 tx_buff->num_entries = num_entries;
1541 tx_bytes += skb->len;
1542 txq->trans_start = jiffies;
1546 netdev->stats.tx_dropped += tx_dropped;
1547 netdev->stats.tx_bytes += tx_bytes;
1548 netdev->stats.tx_packets += tx_packets;
1549 adapter->tx_send_failed += tx_send_failed;
1550 adapter->tx_map_failed += tx_map_failed;
1551 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1552 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1553 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1558 static void ibmvnic_set_multi(struct net_device *netdev)
1560 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1561 struct netdev_hw_addr *ha;
1562 union ibmvnic_crq crq;
1564 memset(&crq, 0, sizeof(crq));
1565 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1566 crq.request_capability.cmd = REQUEST_CAPABILITY;
1568 if (netdev->flags & IFF_PROMISC) {
1569 if (!adapter->promisc_supported)
1572 if (netdev->flags & IFF_ALLMULTI) {
1573 /* Accept all multicast */
1574 memset(&crq, 0, sizeof(crq));
1575 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1576 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1577 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1578 ibmvnic_send_crq(adapter, &crq);
1579 } else if (netdev_mc_empty(netdev)) {
1580 /* Reject all multicast */
1581 memset(&crq, 0, sizeof(crq));
1582 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1583 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1584 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1585 ibmvnic_send_crq(adapter, &crq);
1587 /* Accept one or more multicast(s) */
1588 netdev_for_each_mc_addr(ha, netdev) {
1589 memset(&crq, 0, sizeof(crq));
1590 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1591 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1592 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1593 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1595 ibmvnic_send_crq(adapter, &crq);
1601 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1603 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1604 struct sockaddr *addr = p;
1605 union ibmvnic_crq crq;
1607 if (!is_valid_ether_addr(addr->sa_data))
1608 return -EADDRNOTAVAIL;
1610 memset(&crq, 0, sizeof(crq));
1611 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1612 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1613 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1615 init_completion(&adapter->fw_done);
1616 ibmvnic_send_crq(adapter, &crq);
1617 wait_for_completion(&adapter->fw_done);
1618 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1619 return adapter->fw_done_rc ? -EIO : 0;
1622 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1624 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1625 struct sockaddr *addr = p;
1628 if (adapter->state == VNIC_PROBED) {
1629 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1630 adapter->mac_change_pending = true;
1634 rc = __ibmvnic_set_mac(netdev, addr);
1640 * do_reset returns zero if we are able to keep processing reset events, or
1641 * non-zero if we hit a fatal error and must halt.
1643 static int do_reset(struct ibmvnic_adapter *adapter,
1644 struct ibmvnic_rwi *rwi, u32 reset_state)
1646 u64 old_num_rx_queues, old_num_tx_queues;
1647 struct net_device *netdev = adapter->netdev;
1650 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1653 netif_carrier_off(netdev);
1654 adapter->reset_reason = rwi->reset_reason;
1656 old_num_rx_queues = adapter->req_rx_queues;
1657 old_num_tx_queues = adapter->req_tx_queues;
1659 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1660 rc = ibmvnic_reenable_crq_queue(adapter);
1665 rc = __ibmvnic_close(netdev);
1669 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1670 adapter->wait_for_reset) {
1671 release_resources(adapter);
1672 release_sub_crqs(adapter, 1);
1673 release_crq_queue(adapter);
1676 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1677 /* remove the closed state so when we call open it appears
1678 * we are coming from the probed state.
1680 adapter->state = VNIC_PROBED;
1682 rc = ibmvnic_init(adapter);
1684 return IBMVNIC_INIT_FAILED;
1686 /* If the adapter was in PROBE state prior to the reset,
1689 if (reset_state == VNIC_PROBED)
1692 rc = ibmvnic_login(netdev);
1694 adapter->state = VNIC_PROBED;
1698 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1699 adapter->wait_for_reset) {
1700 rc = init_resources(adapter);
1703 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1704 adapter->req_tx_queues != old_num_tx_queues) {
1705 adapter->map_id = 1;
1706 release_rx_pools(adapter);
1707 release_tx_pools(adapter);
1708 init_rx_pools(netdev);
1709 init_tx_pools(netdev);
1711 release_napi(adapter);
1714 rc = reset_tx_pools(adapter);
1718 rc = reset_rx_pools(adapter);
1722 if (reset_state == VNIC_CLOSED)
1727 rc = __ibmvnic_open(netdev);
1729 if (list_empty(&adapter->rwi_list))
1730 adapter->state = VNIC_CLOSED;
1732 adapter->state = reset_state;
1738 for (i = 0; i < adapter->req_rx_queues; i++)
1739 napi_schedule(&adapter->napi[i]);
1741 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1742 netdev_notify_peers(netdev);
1744 netif_carrier_on(netdev);
1749 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1751 struct ibmvnic_rwi *rwi;
1753 mutex_lock(&adapter->rwi_lock);
1755 if (!list_empty(&adapter->rwi_list)) {
1756 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1758 list_del(&rwi->list);
1763 mutex_unlock(&adapter->rwi_lock);
1767 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1769 struct ibmvnic_rwi *rwi;
1771 rwi = get_next_rwi(adapter);
1774 rwi = get_next_rwi(adapter);
1778 static void __ibmvnic_reset(struct work_struct *work)
1780 struct ibmvnic_rwi *rwi;
1781 struct ibmvnic_adapter *adapter;
1782 struct net_device *netdev;
1786 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1787 netdev = adapter->netdev;
1789 mutex_lock(&adapter->reset_lock);
1790 adapter->resetting = true;
1791 reset_state = adapter->state;
1793 rwi = get_next_rwi(adapter);
1795 rc = do_reset(adapter, rwi, reset_state);
1797 if (rc && rc != IBMVNIC_INIT_FAILED)
1800 rwi = get_next_rwi(adapter);
1803 if (adapter->wait_for_reset) {
1804 adapter->wait_for_reset = false;
1805 adapter->reset_done_rc = rc;
1806 complete(&adapter->reset_done);
1810 netdev_dbg(adapter->netdev, "Reset failed\n");
1811 free_all_rwi(adapter);
1812 mutex_unlock(&adapter->reset_lock);
1816 adapter->resetting = false;
1817 mutex_unlock(&adapter->reset_lock);
1820 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1821 enum ibmvnic_reset_reason reason)
1823 struct ibmvnic_rwi *rwi, *tmp;
1824 struct net_device *netdev = adapter->netdev;
1825 struct list_head *entry;
1827 if (adapter->state == VNIC_REMOVING ||
1828 adapter->state == VNIC_REMOVED) {
1829 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1833 if (adapter->state == VNIC_PROBING) {
1834 netdev_warn(netdev, "Adapter reset during probe\n");
1835 adapter->init_done_rc = EAGAIN;
1839 mutex_lock(&adapter->rwi_lock);
1841 list_for_each(entry, &adapter->rwi_list) {
1842 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1843 if (tmp->reset_reason == reason) {
1844 netdev_dbg(netdev, "Skipping matching reset\n");
1845 mutex_unlock(&adapter->rwi_lock);
1850 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1852 mutex_unlock(&adapter->rwi_lock);
1853 ibmvnic_close(netdev);
1857 rwi->reset_reason = reason;
1858 list_add_tail(&rwi->list, &adapter->rwi_list);
1859 mutex_unlock(&adapter->rwi_lock);
1861 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1862 schedule_work(&adapter->ibmvnic_reset);
1865 static void ibmvnic_tx_timeout(struct net_device *dev)
1867 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1869 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1872 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1873 struct ibmvnic_rx_buff *rx_buff)
1875 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1877 rx_buff->skb = NULL;
1879 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1880 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1882 atomic_dec(&pool->available);
1885 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1887 struct net_device *netdev = napi->dev;
1888 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1889 int scrq_num = (int)(napi - adapter->napi);
1890 int frames_processed = 0;
1893 while (frames_processed < budget) {
1894 struct sk_buff *skb;
1895 struct ibmvnic_rx_buff *rx_buff;
1896 union sub_crq *next;
1901 if (unlikely(adapter->resetting &&
1902 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1903 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1904 napi_complete_done(napi, frames_processed);
1905 return frames_processed;
1908 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1910 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1912 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1913 rx_comp.correlator);
1914 /* do error checking */
1915 if (next->rx_comp.rc) {
1916 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1917 be16_to_cpu(next->rx_comp.rc));
1918 /* free the entry */
1919 next->rx_comp.first = 0;
1920 dev_kfree_skb_any(rx_buff->skb);
1921 remove_buff_from_pool(adapter, rx_buff);
1923 } else if (!rx_buff->skb) {
1924 /* free the entry */
1925 next->rx_comp.first = 0;
1926 remove_buff_from_pool(adapter, rx_buff);
1930 length = be32_to_cpu(next->rx_comp.len);
1931 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1932 flags = next->rx_comp.flags;
1934 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1937 /* VLAN Header has been stripped by the system firmware and
1938 * needs to be inserted by the driver
1940 if (adapter->rx_vlan_header_insertion &&
1941 (flags & IBMVNIC_VLAN_STRIPPED))
1942 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1943 ntohs(next->rx_comp.vlan_tci));
1945 /* free the entry */
1946 next->rx_comp.first = 0;
1947 remove_buff_from_pool(adapter, rx_buff);
1949 skb_put(skb, length);
1950 skb->protocol = eth_type_trans(skb, netdev);
1951 skb_record_rx_queue(skb, scrq_num);
1953 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1954 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1955 skb->ip_summed = CHECKSUM_UNNECESSARY;
1959 napi_gro_receive(napi, skb); /* send it up */
1960 netdev->stats.rx_packets++;
1961 netdev->stats.rx_bytes += length;
1962 adapter->rx_stats_buffers[scrq_num].packets++;
1963 adapter->rx_stats_buffers[scrq_num].bytes += length;
1967 if (adapter->state != VNIC_CLOSING)
1968 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1970 if (frames_processed < budget) {
1971 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1972 napi_complete_done(napi, frames_processed);
1973 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1974 napi_reschedule(napi)) {
1975 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1979 return frames_processed;
1982 #ifdef CONFIG_NET_POLL_CONTROLLER
1983 static void ibmvnic_netpoll_controller(struct net_device *dev)
1985 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1988 replenish_pools(netdev_priv(dev));
1989 for (i = 0; i < adapter->req_rx_queues; i++)
1990 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1991 adapter->rx_scrq[i]);
1995 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1997 adapter->fallback.mtu = adapter->req_mtu;
1998 adapter->fallback.rx_queues = adapter->req_rx_queues;
1999 adapter->fallback.tx_queues = adapter->req_tx_queues;
2000 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2001 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2003 init_completion(&adapter->reset_done);
2004 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2005 adapter->wait_for_reset = true;
2006 wait_for_completion(&adapter->reset_done);
2008 if (adapter->reset_done_rc) {
2009 adapter->desired.mtu = adapter->fallback.mtu;
2010 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2011 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2012 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2013 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2015 init_completion(&adapter->reset_done);
2016 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2017 wait_for_completion(&adapter->reset_done);
2019 adapter->wait_for_reset = false;
2021 return adapter->reset_done_rc;
2024 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2026 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2028 adapter->desired.mtu = new_mtu + ETH_HLEN;
2030 return wait_for_reset(adapter);
2033 static const struct net_device_ops ibmvnic_netdev_ops = {
2034 .ndo_open = ibmvnic_open,
2035 .ndo_stop = ibmvnic_close,
2036 .ndo_start_xmit = ibmvnic_xmit,
2037 .ndo_set_rx_mode = ibmvnic_set_multi,
2038 .ndo_set_mac_address = ibmvnic_set_mac,
2039 .ndo_validate_addr = eth_validate_addr,
2040 .ndo_tx_timeout = ibmvnic_tx_timeout,
2041 #ifdef CONFIG_NET_POLL_CONTROLLER
2042 .ndo_poll_controller = ibmvnic_netpoll_controller,
2044 .ndo_change_mtu = ibmvnic_change_mtu,
2047 /* ethtool functions */
2049 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2050 struct ethtool_link_ksettings *cmd)
2052 u32 supported, advertising;
2054 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2056 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2058 cmd->base.speed = SPEED_1000;
2059 cmd->base.duplex = DUPLEX_FULL;
2060 cmd->base.port = PORT_FIBRE;
2061 cmd->base.phy_address = 0;
2062 cmd->base.autoneg = AUTONEG_ENABLE;
2064 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2066 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2072 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2073 struct ethtool_drvinfo *info)
2075 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2077 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2078 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2079 strlcpy(info->fw_version, adapter->fw_version,
2080 sizeof(info->fw_version));
2083 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2085 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2087 return adapter->msg_enable;
2090 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2092 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2094 adapter->msg_enable = data;
2097 static u32 ibmvnic_get_link(struct net_device *netdev)
2099 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2101 /* Don't need to send a query because we request a logical link up at
2102 * init and then we wait for link state indications
2104 return adapter->logical_link_state;
2107 static void ibmvnic_get_ringparam(struct net_device *netdev,
2108 struct ethtool_ringparam *ring)
2110 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2112 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2113 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2114 ring->rx_mini_max_pending = 0;
2115 ring->rx_jumbo_max_pending = 0;
2116 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2117 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2118 ring->rx_mini_pending = 0;
2119 ring->rx_jumbo_pending = 0;
2122 static int ibmvnic_set_ringparam(struct net_device *netdev,
2123 struct ethtool_ringparam *ring)
2125 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2127 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2128 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2129 netdev_err(netdev, "Invalid request.\n");
2130 netdev_err(netdev, "Max tx buffers = %llu\n",
2131 adapter->max_rx_add_entries_per_subcrq);
2132 netdev_err(netdev, "Max rx buffers = %llu\n",
2133 adapter->max_tx_entries_per_subcrq);
2137 adapter->desired.rx_entries = ring->rx_pending;
2138 adapter->desired.tx_entries = ring->tx_pending;
2140 return wait_for_reset(adapter);
2143 static void ibmvnic_get_channels(struct net_device *netdev,
2144 struct ethtool_channels *channels)
2146 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2148 channels->max_rx = adapter->max_rx_queues;
2149 channels->max_tx = adapter->max_tx_queues;
2150 channels->max_other = 0;
2151 channels->max_combined = 0;
2152 channels->rx_count = adapter->req_rx_queues;
2153 channels->tx_count = adapter->req_tx_queues;
2154 channels->other_count = 0;
2155 channels->combined_count = 0;
2158 static int ibmvnic_set_channels(struct net_device *netdev,
2159 struct ethtool_channels *channels)
2161 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2163 adapter->desired.rx_queues = channels->rx_count;
2164 adapter->desired.tx_queues = channels->tx_count;
2166 return wait_for_reset(adapter);
2169 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2171 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2174 if (stringset != ETH_SS_STATS)
2177 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2178 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2180 for (i = 0; i < adapter->req_tx_queues; i++) {
2181 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2182 data += ETH_GSTRING_LEN;
2184 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2185 data += ETH_GSTRING_LEN;
2187 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2188 data += ETH_GSTRING_LEN;
2191 for (i = 0; i < adapter->req_rx_queues; i++) {
2192 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2193 data += ETH_GSTRING_LEN;
2195 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2196 data += ETH_GSTRING_LEN;
2198 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2199 data += ETH_GSTRING_LEN;
2203 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2205 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2209 return ARRAY_SIZE(ibmvnic_stats) +
2210 adapter->req_tx_queues * NUM_TX_STATS +
2211 adapter->req_rx_queues * NUM_RX_STATS;
2217 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2218 struct ethtool_stats *stats, u64 *data)
2220 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2221 union ibmvnic_crq crq;
2224 memset(&crq, 0, sizeof(crq));
2225 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2226 crq.request_statistics.cmd = REQUEST_STATISTICS;
2227 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2228 crq.request_statistics.len =
2229 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2231 /* Wait for data to be written */
2232 init_completion(&adapter->stats_done);
2233 ibmvnic_send_crq(adapter, &crq);
2234 wait_for_completion(&adapter->stats_done);
2236 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2237 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2238 ibmvnic_stats[i].offset));
2240 for (j = 0; j < adapter->req_tx_queues; j++) {
2241 data[i] = adapter->tx_stats_buffers[j].packets;
2243 data[i] = adapter->tx_stats_buffers[j].bytes;
2245 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2249 for (j = 0; j < adapter->req_rx_queues; j++) {
2250 data[i] = adapter->rx_stats_buffers[j].packets;
2252 data[i] = adapter->rx_stats_buffers[j].bytes;
2254 data[i] = adapter->rx_stats_buffers[j].interrupts;
2259 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2260 .get_drvinfo = ibmvnic_get_drvinfo,
2261 .get_msglevel = ibmvnic_get_msglevel,
2262 .set_msglevel = ibmvnic_set_msglevel,
2263 .get_link = ibmvnic_get_link,
2264 .get_ringparam = ibmvnic_get_ringparam,
2265 .set_ringparam = ibmvnic_set_ringparam,
2266 .get_channels = ibmvnic_get_channels,
2267 .set_channels = ibmvnic_set_channels,
2268 .get_strings = ibmvnic_get_strings,
2269 .get_sset_count = ibmvnic_get_sset_count,
2270 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2271 .get_link_ksettings = ibmvnic_get_link_ksettings,
2274 /* Routines for managing CRQs/sCRQs */
2276 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2277 struct ibmvnic_sub_crq_queue *scrq)
2282 free_irq(scrq->irq, scrq);
2283 irq_dispose_mapping(scrq->irq);
2287 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2290 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2291 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2295 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2299 for (i = 0; i < adapter->req_tx_queues; i++) {
2300 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2301 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2306 for (i = 0; i < adapter->req_rx_queues; i++) {
2307 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2308 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2316 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2317 struct ibmvnic_sub_crq_queue *scrq,
2320 struct device *dev = &adapter->vdev->dev;
2323 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2326 /* Close the sub-crqs */
2328 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2329 adapter->vdev->unit_address,
2331 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2334 netdev_err(adapter->netdev,
2335 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2340 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2342 free_pages((unsigned long)scrq->msgs, 2);
2346 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2349 struct device *dev = &adapter->vdev->dev;
2350 struct ibmvnic_sub_crq_queue *scrq;
2353 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2358 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2360 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2361 goto zero_page_failed;
2364 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2366 if (dma_mapping_error(dev, scrq->msg_token)) {
2367 dev_warn(dev, "Couldn't map crq queue messages page\n");
2371 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2372 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2374 if (rc == H_RESOURCE)
2375 rc = ibmvnic_reset_crq(adapter);
2377 if (rc == H_CLOSED) {
2378 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2380 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2384 scrq->adapter = adapter;
2385 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2386 spin_lock_init(&scrq->lock);
2388 netdev_dbg(adapter->netdev,
2389 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2390 scrq->crq_num, scrq->hw_irq, scrq->irq);
2395 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2398 free_pages((unsigned long)scrq->msgs, 2);
2405 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2409 if (adapter->tx_scrq) {
2410 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2411 if (!adapter->tx_scrq[i])
2414 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2416 if (adapter->tx_scrq[i]->irq) {
2417 free_irq(adapter->tx_scrq[i]->irq,
2418 adapter->tx_scrq[i]);
2419 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2420 adapter->tx_scrq[i]->irq = 0;
2423 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2427 kfree(adapter->tx_scrq);
2428 adapter->tx_scrq = NULL;
2429 adapter->num_active_tx_scrqs = 0;
2432 if (adapter->rx_scrq) {
2433 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2434 if (!adapter->rx_scrq[i])
2437 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2439 if (adapter->rx_scrq[i]->irq) {
2440 free_irq(adapter->rx_scrq[i]->irq,
2441 adapter->rx_scrq[i]);
2442 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2443 adapter->rx_scrq[i]->irq = 0;
2446 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2450 kfree(adapter->rx_scrq);
2451 adapter->rx_scrq = NULL;
2452 adapter->num_active_rx_scrqs = 0;
2456 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2457 struct ibmvnic_sub_crq_queue *scrq)
2459 struct device *dev = &adapter->vdev->dev;
2462 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2463 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2465 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2470 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2471 struct ibmvnic_sub_crq_queue *scrq)
2473 struct device *dev = &adapter->vdev->dev;
2476 if (scrq->hw_irq > 0x100000000ULL) {
2477 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2481 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2482 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2484 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2489 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2490 struct ibmvnic_sub_crq_queue *scrq)
2492 struct device *dev = &adapter->vdev->dev;
2493 struct ibmvnic_tx_buff *txbuff;
2494 union sub_crq *next;
2500 while (pending_scrq(adapter, scrq)) {
2501 unsigned int pool = scrq->pool_index;
2502 int num_entries = 0;
2504 next = ibmvnic_next_scrq(adapter, scrq);
2505 for (i = 0; i < next->tx_comp.num_comps; i++) {
2506 if (next->tx_comp.rcs[i]) {
2507 dev_err(dev, "tx error %x\n",
2508 next->tx_comp.rcs[i]);
2511 index = be32_to_cpu(next->tx_comp.correlators[i]);
2512 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2514 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2515 if (!txbuff->data_dma[j])
2518 txbuff->data_dma[j] = 0;
2520 /* if sub_crq was sent indirectly */
2521 first = txbuff->indir_arr[0].generic.first;
2522 if (first == IBMVNIC_CRQ_CMD) {
2523 dma_unmap_single(dev, txbuff->indir_dma,
2524 sizeof(txbuff->indir_arr),
2528 if (txbuff->last_frag) {
2529 dev_kfree_skb_any(txbuff->skb);
2533 num_entries += txbuff->num_entries;
2535 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2536 producer_index] = index;
2537 adapter->tx_pool[pool].producer_index =
2538 (adapter->tx_pool[pool].producer_index + 1) %
2539 adapter->req_tx_entries_per_subcrq;
2541 /* remove tx_comp scrq*/
2542 next->tx_comp.first = 0;
2544 if (atomic_sub_return(num_entries, &scrq->used) <=
2545 (adapter->req_tx_entries_per_subcrq / 2) &&
2546 __netif_subqueue_stopped(adapter->netdev,
2547 scrq->pool_index)) {
2548 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2549 netdev_info(adapter->netdev, "Started queue %d\n",
2554 enable_scrq_irq(adapter, scrq);
2556 if (pending_scrq(adapter, scrq)) {
2557 disable_scrq_irq(adapter, scrq);
2564 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2566 struct ibmvnic_sub_crq_queue *scrq = instance;
2567 struct ibmvnic_adapter *adapter = scrq->adapter;
2569 disable_scrq_irq(adapter, scrq);
2570 ibmvnic_complete_tx(adapter, scrq);
2575 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2577 struct ibmvnic_sub_crq_queue *scrq = instance;
2578 struct ibmvnic_adapter *adapter = scrq->adapter;
2580 /* When booting a kdump kernel we can hit pending interrupts
2581 * prior to completing driver initialization.
2583 if (unlikely(adapter->state != VNIC_OPEN))
2586 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2588 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2589 disable_scrq_irq(adapter, scrq);
2590 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2596 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2598 struct device *dev = &adapter->vdev->dev;
2599 struct ibmvnic_sub_crq_queue *scrq;
2603 for (i = 0; i < adapter->req_tx_queues; i++) {
2604 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2606 scrq = adapter->tx_scrq[i];
2607 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2611 dev_err(dev, "Error mapping irq\n");
2612 goto req_tx_irq_failed;
2615 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2616 0, "ibmvnic_tx", scrq);
2619 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2621 irq_dispose_mapping(scrq->irq);
2622 goto req_tx_irq_failed;
2626 for (i = 0; i < adapter->req_rx_queues; i++) {
2627 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2629 scrq = adapter->rx_scrq[i];
2630 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2633 dev_err(dev, "Error mapping irq\n");
2634 goto req_rx_irq_failed;
2636 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2637 0, "ibmvnic_rx", scrq);
2639 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2641 irq_dispose_mapping(scrq->irq);
2642 goto req_rx_irq_failed;
2648 for (j = 0; j < i; j++) {
2649 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2650 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2652 i = adapter->req_tx_queues;
2654 for (j = 0; j < i; j++) {
2655 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2656 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2658 release_sub_crqs(adapter, 1);
2662 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2664 struct device *dev = &adapter->vdev->dev;
2665 struct ibmvnic_sub_crq_queue **allqueues;
2666 int registered_queues = 0;
2671 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2673 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2677 for (i = 0; i < total_queues; i++) {
2678 allqueues[i] = init_sub_crq_queue(adapter);
2679 if (!allqueues[i]) {
2680 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2683 registered_queues++;
2686 /* Make sure we were able to register the minimum number of queues */
2687 if (registered_queues <
2688 adapter->min_tx_queues + adapter->min_rx_queues) {
2689 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2693 /* Distribute the failed allocated queues*/
2694 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2695 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2698 if (adapter->req_rx_queues > adapter->min_rx_queues)
2699 adapter->req_rx_queues--;
2704 if (adapter->req_tx_queues > adapter->min_tx_queues)
2705 adapter->req_tx_queues--;
2712 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2713 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2714 if (!adapter->tx_scrq)
2717 for (i = 0; i < adapter->req_tx_queues; i++) {
2718 adapter->tx_scrq[i] = allqueues[i];
2719 adapter->tx_scrq[i]->pool_index = i;
2720 adapter->num_active_tx_scrqs++;
2723 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2724 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2725 if (!adapter->rx_scrq)
2728 for (i = 0; i < adapter->req_rx_queues; i++) {
2729 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2730 adapter->rx_scrq[i]->scrq_num = i;
2731 adapter->num_active_rx_scrqs++;
2738 kfree(adapter->tx_scrq);
2739 adapter->tx_scrq = NULL;
2741 for (i = 0; i < registered_queues; i++)
2742 release_sub_crq_queue(adapter, allqueues[i], 1);
2747 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2749 struct device *dev = &adapter->vdev->dev;
2750 union ibmvnic_crq crq;
2754 /* Sub-CRQ entries are 32 byte long */
2755 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2757 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2758 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2759 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2763 if (adapter->desired.mtu)
2764 adapter->req_mtu = adapter->desired.mtu;
2766 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2768 if (!adapter->desired.tx_entries)
2769 adapter->desired.tx_entries =
2770 adapter->max_tx_entries_per_subcrq;
2771 if (!adapter->desired.rx_entries)
2772 adapter->desired.rx_entries =
2773 adapter->max_rx_add_entries_per_subcrq;
2775 max_entries = IBMVNIC_MAX_LTB_SIZE /
2776 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2778 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2779 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2780 adapter->desired.tx_entries = max_entries;
2783 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2784 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2785 adapter->desired.rx_entries = max_entries;
2788 if (adapter->desired.tx_entries)
2789 adapter->req_tx_entries_per_subcrq =
2790 adapter->desired.tx_entries;
2792 adapter->req_tx_entries_per_subcrq =
2793 adapter->max_tx_entries_per_subcrq;
2795 if (adapter->desired.rx_entries)
2796 adapter->req_rx_add_entries_per_subcrq =
2797 adapter->desired.rx_entries;
2799 adapter->req_rx_add_entries_per_subcrq =
2800 adapter->max_rx_add_entries_per_subcrq;
2802 if (adapter->desired.tx_queues)
2803 adapter->req_tx_queues =
2804 adapter->desired.tx_queues;
2806 adapter->req_tx_queues =
2807 adapter->opt_tx_comp_sub_queues;
2809 if (adapter->desired.rx_queues)
2810 adapter->req_rx_queues =
2811 adapter->desired.rx_queues;
2813 adapter->req_rx_queues =
2814 adapter->opt_rx_comp_queues;
2816 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2819 memset(&crq, 0, sizeof(crq));
2820 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2821 crq.request_capability.cmd = REQUEST_CAPABILITY;
2823 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2824 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2825 atomic_inc(&adapter->running_cap_crqs);
2826 ibmvnic_send_crq(adapter, &crq);
2828 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2829 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2830 atomic_inc(&adapter->running_cap_crqs);
2831 ibmvnic_send_crq(adapter, &crq);
2833 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2834 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2835 atomic_inc(&adapter->running_cap_crqs);
2836 ibmvnic_send_crq(adapter, &crq);
2838 crq.request_capability.capability =
2839 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2840 crq.request_capability.number =
2841 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2842 atomic_inc(&adapter->running_cap_crqs);
2843 ibmvnic_send_crq(adapter, &crq);
2845 crq.request_capability.capability =
2846 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2847 crq.request_capability.number =
2848 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2849 atomic_inc(&adapter->running_cap_crqs);
2850 ibmvnic_send_crq(adapter, &crq);
2852 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2853 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2854 atomic_inc(&adapter->running_cap_crqs);
2855 ibmvnic_send_crq(adapter, &crq);
2857 if (adapter->netdev->flags & IFF_PROMISC) {
2858 if (adapter->promisc_supported) {
2859 crq.request_capability.capability =
2860 cpu_to_be16(PROMISC_REQUESTED);
2861 crq.request_capability.number = cpu_to_be64(1);
2862 atomic_inc(&adapter->running_cap_crqs);
2863 ibmvnic_send_crq(adapter, &crq);
2866 crq.request_capability.capability =
2867 cpu_to_be16(PROMISC_REQUESTED);
2868 crq.request_capability.number = cpu_to_be64(0);
2869 atomic_inc(&adapter->running_cap_crqs);
2870 ibmvnic_send_crq(adapter, &crq);
2874 static int pending_scrq(struct ibmvnic_adapter *adapter,
2875 struct ibmvnic_sub_crq_queue *scrq)
2877 union sub_crq *entry = &scrq->msgs[scrq->cur];
2879 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2885 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2886 struct ibmvnic_sub_crq_queue *scrq)
2888 union sub_crq *entry;
2889 unsigned long flags;
2891 spin_lock_irqsave(&scrq->lock, flags);
2892 entry = &scrq->msgs[scrq->cur];
2893 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2894 if (++scrq->cur == scrq->size)
2899 spin_unlock_irqrestore(&scrq->lock, flags);
2904 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2906 struct ibmvnic_crq_queue *queue = &adapter->crq;
2907 union ibmvnic_crq *crq;
2909 crq = &queue->msgs[queue->cur];
2910 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2911 if (++queue->cur == queue->size)
2920 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2921 union sub_crq *sub_crq)
2923 unsigned int ua = adapter->vdev->unit_address;
2924 struct device *dev = &adapter->vdev->dev;
2925 u64 *u64_crq = (u64 *)sub_crq;
2928 netdev_dbg(adapter->netdev,
2929 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2930 (unsigned long int)cpu_to_be64(remote_handle),
2931 (unsigned long int)cpu_to_be64(u64_crq[0]),
2932 (unsigned long int)cpu_to_be64(u64_crq[1]),
2933 (unsigned long int)cpu_to_be64(u64_crq[2]),
2934 (unsigned long int)cpu_to_be64(u64_crq[3]));
2936 /* Make sure the hypervisor sees the complete request */
2939 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2940 cpu_to_be64(remote_handle),
2941 cpu_to_be64(u64_crq[0]),
2942 cpu_to_be64(u64_crq[1]),
2943 cpu_to_be64(u64_crq[2]),
2944 cpu_to_be64(u64_crq[3]));
2948 dev_warn(dev, "CRQ Queue closed\n");
2949 dev_err(dev, "Send error (rc=%d)\n", rc);
2955 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2956 u64 remote_handle, u64 ioba, u64 num_entries)
2958 unsigned int ua = adapter->vdev->unit_address;
2959 struct device *dev = &adapter->vdev->dev;
2962 /* Make sure the hypervisor sees the complete request */
2964 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2965 cpu_to_be64(remote_handle),
2970 dev_warn(dev, "CRQ Queue closed\n");
2971 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2977 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2978 union ibmvnic_crq *crq)
2980 unsigned int ua = adapter->vdev->unit_address;
2981 struct device *dev = &adapter->vdev->dev;
2982 u64 *u64_crq = (u64 *)crq;
2985 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2986 (unsigned long int)cpu_to_be64(u64_crq[0]),
2987 (unsigned long int)cpu_to_be64(u64_crq[1]));
2989 /* Make sure the hypervisor sees the complete request */
2992 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2993 cpu_to_be64(u64_crq[0]),
2994 cpu_to_be64(u64_crq[1]));
2997 if (rc == H_CLOSED) {
2998 dev_warn(dev, "CRQ Queue closed\n");
2999 if (adapter->resetting)
3000 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3003 dev_warn(dev, "Send error (rc=%d)\n", rc);
3009 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3011 union ibmvnic_crq crq;
3013 memset(&crq, 0, sizeof(crq));
3014 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3015 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3016 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3018 return ibmvnic_send_crq(adapter, &crq);
3021 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3023 union ibmvnic_crq crq;
3025 memset(&crq, 0, sizeof(crq));
3026 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3027 crq.version_exchange.cmd = VERSION_EXCHANGE;
3028 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3030 return ibmvnic_send_crq(adapter, &crq);
3033 struct vnic_login_client_data {
3039 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3043 /* Calculate the amount of buffer space needed for the
3044 * vnic client data in the login buffer. There are four entries,
3045 * OS name, LPAR name, device name, and a null last entry.
3047 len = 4 * sizeof(struct vnic_login_client_data);
3048 len += 6; /* "Linux" plus NULL */
3049 len += strlen(utsname()->nodename) + 1;
3050 len += strlen(adapter->netdev->name) + 1;
3055 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3056 struct vnic_login_client_data *vlcd)
3058 const char *os_name = "Linux";
3061 /* Type 1 - LPAR OS */
3063 len = strlen(os_name) + 1;
3064 vlcd->len = cpu_to_be16(len);
3065 strncpy(&vlcd->name, os_name, len);
3066 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3068 /* Type 2 - LPAR name */
3070 len = strlen(utsname()->nodename) + 1;
3071 vlcd->len = cpu_to_be16(len);
3072 strncpy(&vlcd->name, utsname()->nodename, len);
3073 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3075 /* Type 3 - device name */
3077 len = strlen(adapter->netdev->name) + 1;
3078 vlcd->len = cpu_to_be16(len);
3079 strncpy(&vlcd->name, adapter->netdev->name, len);
3082 static void send_login(struct ibmvnic_adapter *adapter)
3084 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3085 struct ibmvnic_login_buffer *login_buffer;
3086 struct device *dev = &adapter->vdev->dev;
3087 dma_addr_t rsp_buffer_token;
3088 dma_addr_t buffer_token;
3089 size_t rsp_buffer_size;
3090 union ibmvnic_crq crq;
3094 int client_data_len;
3095 struct vnic_login_client_data *vlcd;
3098 release_login_rsp_buffer(adapter);
3099 client_data_len = vnic_client_data_len(adapter);
3102 sizeof(struct ibmvnic_login_buffer) +
3103 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3106 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3108 goto buf_alloc_failed;
3110 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3112 if (dma_mapping_error(dev, buffer_token)) {
3113 dev_err(dev, "Couldn't map login buffer\n");
3114 goto buf_map_failed;
3117 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3118 sizeof(u64) * adapter->req_tx_queues +
3119 sizeof(u64) * adapter->req_rx_queues +
3120 sizeof(u64) * adapter->req_rx_queues +
3121 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3123 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3124 if (!login_rsp_buffer)
3125 goto buf_rsp_alloc_failed;
3127 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3128 rsp_buffer_size, DMA_FROM_DEVICE);
3129 if (dma_mapping_error(dev, rsp_buffer_token)) {
3130 dev_err(dev, "Couldn't map login rsp buffer\n");
3131 goto buf_rsp_map_failed;
3134 adapter->login_buf = login_buffer;
3135 adapter->login_buf_token = buffer_token;
3136 adapter->login_buf_sz = buffer_size;
3137 adapter->login_rsp_buf = login_rsp_buffer;
3138 adapter->login_rsp_buf_token = rsp_buffer_token;
3139 adapter->login_rsp_buf_sz = rsp_buffer_size;
3141 login_buffer->len = cpu_to_be32(buffer_size);
3142 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3143 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3144 login_buffer->off_txcomp_subcrqs =
3145 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3146 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3147 login_buffer->off_rxcomp_subcrqs =
3148 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3149 sizeof(u64) * adapter->req_tx_queues);
3150 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3151 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3153 tx_list_p = (__be64 *)((char *)login_buffer +
3154 sizeof(struct ibmvnic_login_buffer));
3155 rx_list_p = (__be64 *)((char *)login_buffer +
3156 sizeof(struct ibmvnic_login_buffer) +
3157 sizeof(u64) * adapter->req_tx_queues);
3159 for (i = 0; i < adapter->req_tx_queues; i++) {
3160 if (adapter->tx_scrq[i]) {
3161 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3166 for (i = 0; i < adapter->req_rx_queues; i++) {
3167 if (adapter->rx_scrq[i]) {
3168 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3173 /* Insert vNIC login client data */
3174 vlcd = (struct vnic_login_client_data *)
3175 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3176 login_buffer->client_data_offset =
3177 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3178 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3180 vnic_add_client_data(adapter, vlcd);
3182 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3183 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3184 netdev_dbg(adapter->netdev, "%016lx\n",
3185 ((unsigned long int *)(adapter->login_buf))[i]);
3188 memset(&crq, 0, sizeof(crq));
3189 crq.login.first = IBMVNIC_CRQ_CMD;
3190 crq.login.cmd = LOGIN;
3191 crq.login.ioba = cpu_to_be32(buffer_token);
3192 crq.login.len = cpu_to_be32(buffer_size);
3193 ibmvnic_send_crq(adapter, &crq);
3198 kfree(login_rsp_buffer);
3199 buf_rsp_alloc_failed:
3200 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3202 kfree(login_buffer);
3207 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3210 union ibmvnic_crq crq;
3212 memset(&crq, 0, sizeof(crq));
3213 crq.request_map.first = IBMVNIC_CRQ_CMD;
3214 crq.request_map.cmd = REQUEST_MAP;
3215 crq.request_map.map_id = map_id;
3216 crq.request_map.ioba = cpu_to_be32(addr);
3217 crq.request_map.len = cpu_to_be32(len);
3218 ibmvnic_send_crq(adapter, &crq);
3221 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3223 union ibmvnic_crq crq;
3225 memset(&crq, 0, sizeof(crq));
3226 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3227 crq.request_unmap.cmd = REQUEST_UNMAP;
3228 crq.request_unmap.map_id = map_id;
3229 ibmvnic_send_crq(adapter, &crq);
3232 static void send_map_query(struct ibmvnic_adapter *adapter)
3234 union ibmvnic_crq crq;
3236 memset(&crq, 0, sizeof(crq));
3237 crq.query_map.first = IBMVNIC_CRQ_CMD;
3238 crq.query_map.cmd = QUERY_MAP;
3239 ibmvnic_send_crq(adapter, &crq);
3242 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3243 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3245 union ibmvnic_crq crq;
3247 atomic_set(&adapter->running_cap_crqs, 0);
3248 memset(&crq, 0, sizeof(crq));
3249 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3250 crq.query_capability.cmd = QUERY_CAPABILITY;
3252 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3253 atomic_inc(&adapter->running_cap_crqs);
3254 ibmvnic_send_crq(adapter, &crq);
3256 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3257 atomic_inc(&adapter->running_cap_crqs);
3258 ibmvnic_send_crq(adapter, &crq);
3260 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3261 atomic_inc(&adapter->running_cap_crqs);
3262 ibmvnic_send_crq(adapter, &crq);
3264 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3265 atomic_inc(&adapter->running_cap_crqs);
3266 ibmvnic_send_crq(adapter, &crq);
3268 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3269 atomic_inc(&adapter->running_cap_crqs);
3270 ibmvnic_send_crq(adapter, &crq);
3272 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3273 atomic_inc(&adapter->running_cap_crqs);
3274 ibmvnic_send_crq(adapter, &crq);
3276 crq.query_capability.capability =
3277 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3278 atomic_inc(&adapter->running_cap_crqs);
3279 ibmvnic_send_crq(adapter, &crq);
3281 crq.query_capability.capability =
3282 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3283 atomic_inc(&adapter->running_cap_crqs);
3284 ibmvnic_send_crq(adapter, &crq);
3286 crq.query_capability.capability =
3287 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3288 atomic_inc(&adapter->running_cap_crqs);
3289 ibmvnic_send_crq(adapter, &crq);
3291 crq.query_capability.capability =
3292 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3293 atomic_inc(&adapter->running_cap_crqs);
3294 ibmvnic_send_crq(adapter, &crq);
3296 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3297 atomic_inc(&adapter->running_cap_crqs);
3298 ibmvnic_send_crq(adapter, &crq);
3300 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3301 atomic_inc(&adapter->running_cap_crqs);
3302 ibmvnic_send_crq(adapter, &crq);
3304 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3305 atomic_inc(&adapter->running_cap_crqs);
3306 ibmvnic_send_crq(adapter, &crq);
3308 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3309 atomic_inc(&adapter->running_cap_crqs);
3310 ibmvnic_send_crq(adapter, &crq);
3312 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3313 atomic_inc(&adapter->running_cap_crqs);
3314 ibmvnic_send_crq(adapter, &crq);
3316 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3317 atomic_inc(&adapter->running_cap_crqs);
3318 ibmvnic_send_crq(adapter, &crq);
3320 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3321 atomic_inc(&adapter->running_cap_crqs);
3322 ibmvnic_send_crq(adapter, &crq);
3324 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3325 atomic_inc(&adapter->running_cap_crqs);
3326 ibmvnic_send_crq(adapter, &crq);
3328 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3329 atomic_inc(&adapter->running_cap_crqs);
3330 ibmvnic_send_crq(adapter, &crq);
3332 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3333 atomic_inc(&adapter->running_cap_crqs);
3334 ibmvnic_send_crq(adapter, &crq);
3336 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3337 atomic_inc(&adapter->running_cap_crqs);
3338 ibmvnic_send_crq(adapter, &crq);
3340 crq.query_capability.capability =
3341 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3342 atomic_inc(&adapter->running_cap_crqs);
3343 ibmvnic_send_crq(adapter, &crq);
3345 crq.query_capability.capability =
3346 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3347 atomic_inc(&adapter->running_cap_crqs);
3348 ibmvnic_send_crq(adapter, &crq);
3350 crq.query_capability.capability =
3351 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3352 atomic_inc(&adapter->running_cap_crqs);
3353 ibmvnic_send_crq(adapter, &crq);
3355 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3356 atomic_inc(&adapter->running_cap_crqs);
3357 ibmvnic_send_crq(adapter, &crq);
3360 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3361 struct ibmvnic_adapter *adapter)
3363 struct device *dev = &adapter->vdev->dev;
3365 if (crq->get_vpd_size_rsp.rc.code) {
3366 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3367 crq->get_vpd_size_rsp.rc.code);
3368 complete(&adapter->fw_done);
3372 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3373 complete(&adapter->fw_done);
3376 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3377 struct ibmvnic_adapter *adapter)
3379 struct device *dev = &adapter->vdev->dev;
3380 unsigned char *substr = NULL;
3381 u8 fw_level_len = 0;
3383 memset(adapter->fw_version, 0, 32);
3385 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3388 if (crq->get_vpd_rsp.rc.code) {
3389 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3390 crq->get_vpd_rsp.rc.code);
3394 /* get the position of the firmware version info
3395 * located after the ASCII 'RM' substring in the buffer
3397 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3399 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3403 /* get length of firmware level ASCII substring */
3404 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3405 fw_level_len = *(substr + 2);
3407 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3411 /* copy firmware version string from vpd into adapter */
3412 if ((substr + 3 + fw_level_len) <
3413 (adapter->vpd->buff + adapter->vpd->len)) {
3414 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3416 dev_info(dev, "FW substr extrapolated VPD buff\n");
3420 if (adapter->fw_version[0] == '\0')
3421 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3422 complete(&adapter->fw_done);
3425 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3427 struct device *dev = &adapter->vdev->dev;
3428 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3429 union ibmvnic_crq crq;
3432 dma_unmap_single(dev, adapter->ip_offload_tok,
3433 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3435 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3436 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3437 netdev_dbg(adapter->netdev, "%016lx\n",
3438 ((unsigned long int *)(buf))[i]);
3440 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3441 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3442 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3443 buf->tcp_ipv4_chksum);
3444 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3445 buf->tcp_ipv6_chksum);
3446 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3447 buf->udp_ipv4_chksum);
3448 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3449 buf->udp_ipv6_chksum);
3450 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3451 buf->large_tx_ipv4);
3452 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3453 buf->large_tx_ipv6);
3454 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3455 buf->large_rx_ipv4);
3456 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3457 buf->large_rx_ipv6);
3458 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3459 buf->max_ipv4_header_size);
3460 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3461 buf->max_ipv6_header_size);
3462 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3463 buf->max_tcp_header_size);
3464 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3465 buf->max_udp_header_size);
3466 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3467 buf->max_large_tx_size);
3468 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3469 buf->max_large_rx_size);
3470 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3471 buf->ipv6_extension_header);
3472 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3473 buf->tcp_pseudosum_req);
3474 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3475 buf->num_ipv6_ext_headers);
3476 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3477 buf->off_ipv6_ext_headers);
3479 adapter->ip_offload_ctrl_tok =
3480 dma_map_single(dev, &adapter->ip_offload_ctrl,
3481 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3483 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3484 dev_err(dev, "Couldn't map ip offload control buffer\n");
3488 adapter->ip_offload_ctrl.len =
3489 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3490 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3491 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3492 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3493 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3494 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3495 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3496 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3497 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3498 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3500 /* large_rx disabled for now, additional features needed */
3501 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3502 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3504 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3506 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3507 adapter->netdev->features |= NETIF_F_IP_CSUM;
3509 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3510 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3512 if ((adapter->netdev->features &
3513 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3514 adapter->netdev->features |= NETIF_F_RXCSUM;
3516 if (buf->large_tx_ipv4)
3517 adapter->netdev->features |= NETIF_F_TSO;
3518 if (buf->large_tx_ipv6)
3519 adapter->netdev->features |= NETIF_F_TSO6;
3521 adapter->netdev->hw_features |= adapter->netdev->features;
3523 memset(&crq, 0, sizeof(crq));
3524 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3525 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3526 crq.control_ip_offload.len =
3527 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3528 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3529 ibmvnic_send_crq(adapter, &crq);
3532 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3533 struct ibmvnic_adapter *adapter)
3535 struct device *dev = &adapter->vdev->dev;
3536 struct ibmvnic_error_buff *error_buff, *tmp;
3537 unsigned long flags;
3541 if (!crq->request_error_rsp.rc.code) {
3542 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3543 crq->request_error_rsp.rc.code);
3547 spin_lock_irqsave(&adapter->error_list_lock, flags);
3548 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3549 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3551 list_del(&error_buff->list);
3554 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3557 dev_err(dev, "Couldn't find error id %x\n",
3558 be32_to_cpu(crq->request_error_rsp.error_id));
3562 dev_err(dev, "Detailed info for error id %x:",
3563 be32_to_cpu(crq->request_error_rsp.error_id));
3565 for (i = 0; i < error_buff->len; i++) {
3566 pr_cont("%02x", (int)error_buff->buff[i]);
3572 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3574 kfree(error_buff->buff);
3578 static void request_error_information(struct ibmvnic_adapter *adapter,
3579 union ibmvnic_crq *err_crq)
3581 struct device *dev = &adapter->vdev->dev;
3582 struct net_device *netdev = adapter->netdev;
3583 struct ibmvnic_error_buff *error_buff;
3584 unsigned long timeout = msecs_to_jiffies(30000);
3585 union ibmvnic_crq crq;
3586 unsigned long flags;
3589 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3593 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3594 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3595 if (!error_buff->buff) {
3600 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3602 if (dma_mapping_error(dev, error_buff->dma)) {
3603 netdev_err(netdev, "Couldn't map error buffer\n");
3604 kfree(error_buff->buff);
3609 error_buff->len = detail_len;
3610 error_buff->error_id = err_crq->error_indication.error_id;
3612 spin_lock_irqsave(&adapter->error_list_lock, flags);
3613 list_add_tail(&error_buff->list, &adapter->errors);
3614 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3616 memset(&crq, 0, sizeof(crq));
3617 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3618 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3619 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3620 crq.request_error_info.len = cpu_to_be32(detail_len);
3621 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3623 rc = ibmvnic_send_crq(adapter, &crq);
3625 netdev_err(netdev, "failed to request error information\n");
3629 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3630 netdev_err(netdev, "timeout waiting for error information\n");
3637 spin_lock_irqsave(&adapter->error_list_lock, flags);
3638 list_del(&error_buff->list);
3639 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3641 kfree(error_buff->buff);
3645 static void handle_error_indication(union ibmvnic_crq *crq,
3646 struct ibmvnic_adapter *adapter)
3648 struct device *dev = &adapter->vdev->dev;
3650 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3651 crq->error_indication.flags
3652 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3653 be32_to_cpu(crq->error_indication.error_id),
3654 be16_to_cpu(crq->error_indication.error_cause));
3656 if (be32_to_cpu(crq->error_indication.error_id))
3657 request_error_information(adapter, crq);
3659 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3660 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3662 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3665 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3666 struct ibmvnic_adapter *adapter)
3668 struct net_device *netdev = adapter->netdev;
3669 struct device *dev = &adapter->vdev->dev;
3672 rc = crq->change_mac_addr_rsp.rc.code;
3674 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3677 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3680 complete(&adapter->fw_done);
3684 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3685 struct ibmvnic_adapter *adapter)
3687 struct device *dev = &adapter->vdev->dev;
3691 atomic_dec(&adapter->running_cap_crqs);
3692 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3694 req_value = &adapter->req_tx_queues;
3698 req_value = &adapter->req_rx_queues;
3701 case REQ_RX_ADD_QUEUES:
3702 req_value = &adapter->req_rx_add_queues;
3705 case REQ_TX_ENTRIES_PER_SUBCRQ:
3706 req_value = &adapter->req_tx_entries_per_subcrq;
3707 name = "tx_entries_per_subcrq";
3709 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3710 req_value = &adapter->req_rx_add_entries_per_subcrq;
3711 name = "rx_add_entries_per_subcrq";
3714 req_value = &adapter->req_mtu;
3717 case PROMISC_REQUESTED:
3718 req_value = &adapter->promisc;
3722 dev_err(dev, "Got invalid cap request rsp %d\n",
3723 crq->request_capability.capability);
3727 switch (crq->request_capability_rsp.rc.code) {
3730 case PARTIALSUCCESS:
3731 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3733 (long int)be64_to_cpu(crq->request_capability_rsp.
3736 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3738 pr_err("mtu of %llu is not supported. Reverting.\n",
3740 *req_value = adapter->fallback.mtu;
3743 be64_to_cpu(crq->request_capability_rsp.number);
3746 ibmvnic_send_req_caps(adapter, 1);
3749 dev_err(dev, "Error %d in request cap rsp\n",
3750 crq->request_capability_rsp.rc.code);
3754 /* Done receiving requested capabilities, query IP offload support */
3755 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3756 union ibmvnic_crq newcrq;
3757 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3758 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3759 &adapter->ip_offload_buf;
3761 adapter->wait_capability = false;
3762 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3766 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3767 if (!firmware_has_feature(FW_FEATURE_CMO))
3768 dev_err(dev, "Couldn't map offload buffer\n");
3772 memset(&newcrq, 0, sizeof(newcrq));
3773 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3774 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3775 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3776 newcrq.query_ip_offload.ioba =
3777 cpu_to_be32(adapter->ip_offload_tok);
3779 ibmvnic_send_crq(adapter, &newcrq);
3783 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3784 struct ibmvnic_adapter *adapter)
3786 struct device *dev = &adapter->vdev->dev;
3787 struct net_device *netdev = adapter->netdev;
3788 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3789 struct ibmvnic_login_buffer *login = adapter->login_buf;
3792 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3794 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3795 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3797 /* If the number of queues requested can't be allocated by the
3798 * server, the login response will return with code 1. We will need
3799 * to resend the login buffer with fewer queues requested.
3801 if (login_rsp_crq->generic.rc.code) {
3802 adapter->renegotiate = true;
3803 complete(&adapter->init_done);
3807 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3809 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3810 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3811 netdev_dbg(adapter->netdev, "%016lx\n",
3812 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3816 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3817 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3818 adapter->req_rx_add_queues !=
3819 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3820 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3821 ibmvnic_remove(adapter->vdev);
3824 release_login_buffer(adapter);
3825 complete(&adapter->init_done);
3830 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3831 struct ibmvnic_adapter *adapter)
3833 struct device *dev = &adapter->vdev->dev;
3836 rc = crq->request_unmap_rsp.rc.code;
3838 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3841 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3842 struct ibmvnic_adapter *adapter)
3844 struct net_device *netdev = adapter->netdev;
3845 struct device *dev = &adapter->vdev->dev;
3848 rc = crq->query_map_rsp.rc.code;
3850 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3853 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3854 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3855 crq->query_map_rsp.free_pages);
3858 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3859 struct ibmvnic_adapter *adapter)
3861 struct net_device *netdev = adapter->netdev;
3862 struct device *dev = &adapter->vdev->dev;
3865 atomic_dec(&adapter->running_cap_crqs);
3866 netdev_dbg(netdev, "Outstanding queries: %d\n",
3867 atomic_read(&adapter->running_cap_crqs));
3868 rc = crq->query_capability.rc.code;
3870 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3874 switch (be16_to_cpu(crq->query_capability.capability)) {
3876 adapter->min_tx_queues =
3877 be64_to_cpu(crq->query_capability.number);
3878 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3879 adapter->min_tx_queues);
3882 adapter->min_rx_queues =
3883 be64_to_cpu(crq->query_capability.number);
3884 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3885 adapter->min_rx_queues);
3887 case MIN_RX_ADD_QUEUES:
3888 adapter->min_rx_add_queues =
3889 be64_to_cpu(crq->query_capability.number);
3890 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3891 adapter->min_rx_add_queues);
3894 adapter->max_tx_queues =
3895 be64_to_cpu(crq->query_capability.number);
3896 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3897 adapter->max_tx_queues);
3900 adapter->max_rx_queues =
3901 be64_to_cpu(crq->query_capability.number);
3902 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3903 adapter->max_rx_queues);
3905 case MAX_RX_ADD_QUEUES:
3906 adapter->max_rx_add_queues =
3907 be64_to_cpu(crq->query_capability.number);
3908 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3909 adapter->max_rx_add_queues);
3911 case MIN_TX_ENTRIES_PER_SUBCRQ:
3912 adapter->min_tx_entries_per_subcrq =
3913 be64_to_cpu(crq->query_capability.number);
3914 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3915 adapter->min_tx_entries_per_subcrq);
3917 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3918 adapter->min_rx_add_entries_per_subcrq =
3919 be64_to_cpu(crq->query_capability.number);
3920 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3921 adapter->min_rx_add_entries_per_subcrq);
3923 case MAX_TX_ENTRIES_PER_SUBCRQ:
3924 adapter->max_tx_entries_per_subcrq =
3925 be64_to_cpu(crq->query_capability.number);
3926 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3927 adapter->max_tx_entries_per_subcrq);
3929 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3930 adapter->max_rx_add_entries_per_subcrq =
3931 be64_to_cpu(crq->query_capability.number);
3932 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3933 adapter->max_rx_add_entries_per_subcrq);
3935 case TCP_IP_OFFLOAD:
3936 adapter->tcp_ip_offload =
3937 be64_to_cpu(crq->query_capability.number);
3938 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3939 adapter->tcp_ip_offload);
3941 case PROMISC_SUPPORTED:
3942 adapter->promisc_supported =
3943 be64_to_cpu(crq->query_capability.number);
3944 netdev_dbg(netdev, "promisc_supported = %lld\n",
3945 adapter->promisc_supported);
3948 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3949 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3950 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3953 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3954 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3955 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3957 case MAX_MULTICAST_FILTERS:
3958 adapter->max_multicast_filters =
3959 be64_to_cpu(crq->query_capability.number);
3960 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3961 adapter->max_multicast_filters);
3963 case VLAN_HEADER_INSERTION:
3964 adapter->vlan_header_insertion =
3965 be64_to_cpu(crq->query_capability.number);
3966 if (adapter->vlan_header_insertion)
3967 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3968 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3969 adapter->vlan_header_insertion);
3971 case RX_VLAN_HEADER_INSERTION:
3972 adapter->rx_vlan_header_insertion =
3973 be64_to_cpu(crq->query_capability.number);
3974 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3975 adapter->rx_vlan_header_insertion);
3977 case MAX_TX_SG_ENTRIES:
3978 adapter->max_tx_sg_entries =
3979 be64_to_cpu(crq->query_capability.number);
3980 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3981 adapter->max_tx_sg_entries);
3983 case RX_SG_SUPPORTED:
3984 adapter->rx_sg_supported =
3985 be64_to_cpu(crq->query_capability.number);
3986 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3987 adapter->rx_sg_supported);
3989 case OPT_TX_COMP_SUB_QUEUES:
3990 adapter->opt_tx_comp_sub_queues =
3991 be64_to_cpu(crq->query_capability.number);
3992 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3993 adapter->opt_tx_comp_sub_queues);
3995 case OPT_RX_COMP_QUEUES:
3996 adapter->opt_rx_comp_queues =
3997 be64_to_cpu(crq->query_capability.number);
3998 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3999 adapter->opt_rx_comp_queues);
4001 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4002 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4003 be64_to_cpu(crq->query_capability.number);
4004 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4005 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4007 case OPT_TX_ENTRIES_PER_SUBCRQ:
4008 adapter->opt_tx_entries_per_subcrq =
4009 be64_to_cpu(crq->query_capability.number);
4010 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4011 adapter->opt_tx_entries_per_subcrq);
4013 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4014 adapter->opt_rxba_entries_per_subcrq =
4015 be64_to_cpu(crq->query_capability.number);
4016 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4017 adapter->opt_rxba_entries_per_subcrq);
4019 case TX_RX_DESC_REQ:
4020 adapter->tx_rx_desc_req = crq->query_capability.number;
4021 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4022 adapter->tx_rx_desc_req);
4026 netdev_err(netdev, "Got invalid cap rsp %d\n",
4027 crq->query_capability.capability);
4031 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4032 adapter->wait_capability = false;
4033 ibmvnic_send_req_caps(adapter, 0);
4037 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4038 struct ibmvnic_adapter *adapter)
4040 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4041 struct net_device *netdev = adapter->netdev;
4042 struct device *dev = &adapter->vdev->dev;
4043 u64 *u64_crq = (u64 *)crq;
4046 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4047 (unsigned long int)cpu_to_be64(u64_crq[0]),
4048 (unsigned long int)cpu_to_be64(u64_crq[1]));
4049 switch (gen_crq->first) {
4050 case IBMVNIC_CRQ_INIT_RSP:
4051 switch (gen_crq->cmd) {
4052 case IBMVNIC_CRQ_INIT:
4053 dev_info(dev, "Partner initialized\n");
4054 adapter->from_passive_init = true;
4055 complete(&adapter->init_done);
4057 case IBMVNIC_CRQ_INIT_COMPLETE:
4058 dev_info(dev, "Partner initialization complete\n");
4059 send_version_xchg(adapter);
4062 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4065 case IBMVNIC_CRQ_XPORT_EVENT:
4066 netif_carrier_off(netdev);
4067 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4068 dev_info(dev, "Migrated, re-enabling adapter\n");
4069 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4070 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4071 dev_info(dev, "Backing device failover detected\n");
4072 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4074 /* The adapter lost the connection */
4075 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4077 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4080 case IBMVNIC_CRQ_CMD_RSP:
4083 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4088 switch (gen_crq->cmd) {
4089 case VERSION_EXCHANGE_RSP:
4090 rc = crq->version_exchange_rsp.rc.code;
4092 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4095 dev_info(dev, "Partner protocol version is %d\n",
4096 crq->version_exchange_rsp.version);
4097 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4100 be16_to_cpu(crq->version_exchange_rsp.version);
4101 send_cap_queries(adapter);
4103 case QUERY_CAPABILITY_RSP:
4104 handle_query_cap_rsp(crq, adapter);
4107 handle_query_map_rsp(crq, adapter);
4109 case REQUEST_MAP_RSP:
4110 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4111 complete(&adapter->fw_done);
4113 case REQUEST_UNMAP_RSP:
4114 handle_request_unmap_rsp(crq, adapter);
4116 case REQUEST_CAPABILITY_RSP:
4117 handle_request_cap_rsp(crq, adapter);
4120 netdev_dbg(netdev, "Got Login Response\n");
4121 handle_login_rsp(crq, adapter);
4123 case LOGICAL_LINK_STATE_RSP:
4125 "Got Logical Link State Response, state: %d rc: %d\n",
4126 crq->logical_link_state_rsp.link_state,
4127 crq->logical_link_state_rsp.rc.code);
4128 adapter->logical_link_state =
4129 crq->logical_link_state_rsp.link_state;
4130 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4131 complete(&adapter->init_done);
4133 case LINK_STATE_INDICATION:
4134 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4135 adapter->phys_link_state =
4136 crq->link_state_indication.phys_link_state;
4137 adapter->logical_link_state =
4138 crq->link_state_indication.logical_link_state;
4140 case CHANGE_MAC_ADDR_RSP:
4141 netdev_dbg(netdev, "Got MAC address change Response\n");
4142 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4144 case ERROR_INDICATION:
4145 netdev_dbg(netdev, "Got Error Indication\n");
4146 handle_error_indication(crq, adapter);
4148 case REQUEST_ERROR_RSP:
4149 netdev_dbg(netdev, "Got Error Detail Response\n");
4150 handle_error_info_rsp(crq, adapter);
4152 case REQUEST_STATISTICS_RSP:
4153 netdev_dbg(netdev, "Got Statistics Response\n");
4154 complete(&adapter->stats_done);
4156 case QUERY_IP_OFFLOAD_RSP:
4157 netdev_dbg(netdev, "Got Query IP offload Response\n");
4158 handle_query_ip_offload_rsp(adapter);
4160 case MULTICAST_CTRL_RSP:
4161 netdev_dbg(netdev, "Got multicast control Response\n");
4163 case CONTROL_IP_OFFLOAD_RSP:
4164 netdev_dbg(netdev, "Got Control IP offload Response\n");
4165 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4166 sizeof(adapter->ip_offload_ctrl),
4168 complete(&adapter->init_done);
4170 case COLLECT_FW_TRACE_RSP:
4171 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4172 complete(&adapter->fw_done);
4174 case GET_VPD_SIZE_RSP:
4175 handle_vpd_size_rsp(crq, adapter);
4178 handle_vpd_rsp(crq, adapter);
4181 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4186 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4188 struct ibmvnic_adapter *adapter = instance;
4190 tasklet_schedule(&adapter->tasklet);
4194 static void ibmvnic_tasklet(void *data)
4196 struct ibmvnic_adapter *adapter = data;
4197 struct ibmvnic_crq_queue *queue = &adapter->crq;
4198 union ibmvnic_crq *crq;
4199 unsigned long flags;
4202 spin_lock_irqsave(&queue->lock, flags);
4204 /* Pull all the valid messages off the CRQ */
4205 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4206 ibmvnic_handle_crq(crq, adapter);
4207 crq->generic.first = 0;
4210 /* remain in tasklet until all
4211 * capabilities responses are received
4213 if (!adapter->wait_capability)
4216 /* if capabilities CRQ's were sent in this tasklet, the following
4217 * tasklet must wait until all responses are received
4219 if (atomic_read(&adapter->running_cap_crqs) != 0)
4220 adapter->wait_capability = true;
4221 spin_unlock_irqrestore(&queue->lock, flags);
4224 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4226 struct vio_dev *vdev = adapter->vdev;
4230 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4231 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4234 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4239 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4241 struct ibmvnic_crq_queue *crq = &adapter->crq;
4242 struct device *dev = &adapter->vdev->dev;
4243 struct vio_dev *vdev = adapter->vdev;
4248 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4249 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4251 /* Clean out the queue */
4252 memset(crq->msgs, 0, PAGE_SIZE);
4255 /* And re-open it again */
4256 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4257 crq->msg_token, PAGE_SIZE);
4260 /* Adapter is good, but other end is not ready */
4261 dev_warn(dev, "Partner adapter not ready\n");
4263 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4268 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4270 struct ibmvnic_crq_queue *crq = &adapter->crq;
4271 struct vio_dev *vdev = adapter->vdev;
4277 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4278 free_irq(vdev->irq, adapter);
4279 tasklet_kill(&adapter->tasklet);
4281 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4282 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4284 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4286 free_page((unsigned long)crq->msgs);
4290 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4292 struct ibmvnic_crq_queue *crq = &adapter->crq;
4293 struct device *dev = &adapter->vdev->dev;
4294 struct vio_dev *vdev = adapter->vdev;
4295 int rc, retrc = -ENOMEM;
4300 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4301 /* Should we allocate more than one page? */
4306 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4307 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4309 if (dma_mapping_error(dev, crq->msg_token))
4312 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4313 crq->msg_token, PAGE_SIZE);
4315 if (rc == H_RESOURCE)
4316 /* maybe kexecing and resource is busy. try a reset */
4317 rc = ibmvnic_reset_crq(adapter);
4320 if (rc == H_CLOSED) {
4321 dev_warn(dev, "Partner adapter not ready\n");
4323 dev_warn(dev, "Error %d opening adapter\n", rc);
4324 goto reg_crq_failed;
4329 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4330 (unsigned long)adapter);
4332 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4333 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4336 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4338 goto req_irq_failed;
4341 rc = vio_enable_interrupts(vdev);
4343 dev_err(dev, "Error %d enabling interrupts\n", rc);
4344 goto req_irq_failed;
4348 spin_lock_init(&crq->lock);
4353 tasklet_kill(&adapter->tasklet);
4355 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4356 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4358 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4360 free_page((unsigned long)crq->msgs);
4365 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4367 struct device *dev = &adapter->vdev->dev;
4368 unsigned long timeout = msecs_to_jiffies(30000);
4369 u64 old_num_rx_queues, old_num_tx_queues;
4372 if (adapter->resetting && !adapter->wait_for_reset) {
4373 rc = ibmvnic_reset_crq(adapter);
4375 rc = vio_enable_interrupts(adapter->vdev);
4377 rc = init_crq_queue(adapter);
4381 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4385 adapter->from_passive_init = false;
4387 old_num_rx_queues = adapter->req_rx_queues;
4388 old_num_tx_queues = adapter->req_tx_queues;
4390 init_completion(&adapter->init_done);
4391 adapter->init_done_rc = 0;
4392 ibmvnic_send_crq_init(adapter);
4393 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4394 dev_err(dev, "Initialization sequence timed out\n");
4398 if (adapter->init_done_rc) {
4399 release_crq_queue(adapter);
4400 return adapter->init_done_rc;
4403 if (adapter->from_passive_init) {
4404 adapter->state = VNIC_OPEN;
4405 adapter->from_passive_init = false;
4409 if (adapter->resetting && !adapter->wait_for_reset) {
4410 if (adapter->req_rx_queues != old_num_rx_queues ||
4411 adapter->req_tx_queues != old_num_tx_queues) {
4412 release_sub_crqs(adapter, 0);
4413 rc = init_sub_crqs(adapter);
4415 rc = reset_sub_crq_queues(adapter);
4418 rc = init_sub_crqs(adapter);
4422 dev_err(dev, "Initialization of sub crqs failed\n");
4423 release_crq_queue(adapter);
4427 rc = init_sub_crq_irqs(adapter);
4429 dev_err(dev, "Failed to initialize sub crq irqs\n");
4430 release_crq_queue(adapter);
4436 static struct device_attribute dev_attr_failover;
4438 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4440 struct ibmvnic_adapter *adapter;
4441 struct net_device *netdev;
4442 unsigned char *mac_addr_p;
4445 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4448 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4449 VETH_MAC_ADDR, NULL);
4452 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4453 __FILE__, __LINE__);
4457 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4458 IBMVNIC_MAX_QUEUES);
4462 adapter = netdev_priv(netdev);
4463 adapter->state = VNIC_PROBING;
4464 dev_set_drvdata(&dev->dev, netdev);
4465 adapter->vdev = dev;
4466 adapter->netdev = netdev;
4468 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4469 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4470 netdev->irq = dev->irq;
4471 netdev->netdev_ops = &ibmvnic_netdev_ops;
4472 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4473 SET_NETDEV_DEV(netdev, &dev->dev);
4475 spin_lock_init(&adapter->stats_lock);
4477 INIT_LIST_HEAD(&adapter->errors);
4478 spin_lock_init(&adapter->error_list_lock);
4480 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4481 INIT_LIST_HEAD(&adapter->rwi_list);
4482 mutex_init(&adapter->reset_lock);
4483 mutex_init(&adapter->rwi_lock);
4484 adapter->resetting = false;
4486 adapter->mac_change_pending = false;
4489 rc = ibmvnic_init(adapter);
4490 if (rc && rc != EAGAIN)
4491 goto ibmvnic_init_fail;
4492 } while (rc == EAGAIN);
4494 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4495 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4496 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4498 rc = device_create_file(&dev->dev, &dev_attr_failover);
4500 goto ibmvnic_init_fail;
4502 netif_carrier_off(netdev);
4503 rc = register_netdev(netdev);
4505 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4506 goto ibmvnic_register_fail;
4508 dev_info(&dev->dev, "ibmvnic registered\n");
4510 adapter->state = VNIC_PROBED;
4512 adapter->wait_for_reset = false;
4516 ibmvnic_register_fail:
4517 device_remove_file(&dev->dev, &dev_attr_failover);
4520 release_sub_crqs(adapter, 1);
4521 release_crq_queue(adapter);
4522 free_netdev(netdev);
4527 static int ibmvnic_remove(struct vio_dev *dev)
4529 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4530 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4532 adapter->state = VNIC_REMOVING;
4533 unregister_netdev(netdev);
4534 mutex_lock(&adapter->reset_lock);
4536 release_resources(adapter);
4537 release_sub_crqs(adapter, 1);
4538 release_crq_queue(adapter);
4540 adapter->state = VNIC_REMOVED;
4542 mutex_unlock(&adapter->reset_lock);
4543 device_remove_file(&dev->dev, &dev_attr_failover);
4544 free_netdev(netdev);
4545 dev_set_drvdata(&dev->dev, NULL);
4550 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4551 const char *buf, size_t count)
4553 struct net_device *netdev = dev_get_drvdata(dev);
4554 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4555 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4556 __be64 session_token;
4559 if (!sysfs_streq(buf, "1"))
4562 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4563 H_GET_SESSION_TOKEN, 0, 0, 0);
4565 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4570 session_token = (__be64)retbuf[0];
4571 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4572 be64_to_cpu(session_token));
4573 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4574 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4576 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4584 static DEVICE_ATTR_WO(failover);
4586 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4588 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4589 struct ibmvnic_adapter *adapter;
4590 struct iommu_table *tbl;
4591 unsigned long ret = 0;
4594 tbl = get_iommu_table_base(&vdev->dev);
4596 /* netdev inits at probe time along with the structures we need below*/
4598 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4600 adapter = netdev_priv(netdev);
4602 ret += PAGE_SIZE; /* the crq message queue */
4603 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4605 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4606 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4608 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4610 ret += adapter->rx_pool[i].size *
4611 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4616 static int ibmvnic_resume(struct device *dev)
4618 struct net_device *netdev = dev_get_drvdata(dev);
4619 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4621 if (adapter->state != VNIC_OPEN)
4624 tasklet_schedule(&adapter->tasklet);
4629 static const struct vio_device_id ibmvnic_device_table[] = {
4630 {"network", "IBM,vnic"},
4633 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4635 static const struct dev_pm_ops ibmvnic_pm_ops = {
4636 .resume = ibmvnic_resume
4639 static struct vio_driver ibmvnic_driver = {
4640 .id_table = ibmvnic_device_table,
4641 .probe = ibmvnic_probe,
4642 .remove = ibmvnic_remove,
4643 .get_desired_dma = ibmvnic_get_desired_dma,
4644 .name = ibmvnic_driver_name,
4645 .pm = &ibmvnic_pm_ops,
4648 /* module functions */
4649 static int __init ibmvnic_module_init(void)
4651 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4652 IBMVNIC_DRIVER_VERSION);
4654 return vio_register_driver(&ibmvnic_driver);
4657 static void __exit ibmvnic_module_exit(void)
4659 vio_unregister_driver(&ibmvnic_driver);
4662 module_init(ibmvnic_module_init);
4663 module_exit(ibmvnic_module_exit);