1 /**************************************************************************/
3 /* IBM System i and System p Virtual NIC Device Driver */
4 /* Copyright (C) 2014 IBM Corp. */
5 /* Santiago Leon (santi_leon@yahoo.com) */
6 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7 /* John Allen (jallen@linux.vnet.ibm.com) */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2 of the License, or */
12 /* (at your option) any later version. */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program. */
22 /* This module contains the implementation of a virtual ethernet device */
23 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24 /* option of the RS/6000 Platform Architecture to interface with virtual */
25 /* ethernet NICs that are presented to the partition by the hypervisor. */
27 /* Messages are passed between the VNIC driver and the VNIC server using */
28 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29 /* issue and receive commands that initiate communication with the server */
30 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31 /* are used by the driver to notify the server that a packet is */
32 /* ready for transmission or that a buffer has been added to receive a */
33 /* packet. Subsequently, sCRQs are used by the server to notify the */
34 /* driver that a packet transmission has been completed or that a packet */
35 /* has been received and placed in a waiting buffer. */
37 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38 /* which skbs are DMA mapped and immediately unmapped when the transmit */
39 /* or receive has been completed, the VNIC driver is required to use */
40 /* "long term mapping". This entails that large, continuous DMA mapped */
41 /* buffers are allocated on driver initialization and these buffers are */
42 /* then continuously reused to pass skbs to and from the VNIC server. */
44 /**************************************************************************/
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/types.h>
49 #include <linux/errno.h>
50 #include <linux/completion.h>
51 #include <linux/ioport.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/kernel.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
60 #include <linux/ethtool.h>
61 #include <linux/proc_fs.h>
62 #include <linux/if_arp.h>
65 #include <linux/ipv6.h>
66 #include <linux/irq.h>
67 #include <linux/kthread.h>
68 #include <linux/seq_file.h>
69 #include <linux/interrupt.h>
70 #include <net/net_namespace.h>
71 #include <asm/hvcall.h>
72 #include <linux/atomic.h>
74 #include <asm/iommu.h>
75 #include <linux/uaccess.h>
76 #include <asm/firmware.h>
77 #include <linux/workqueue.h>
78 #include <linux/if_vlan.h>
79 #include <linux/utsname.h>
83 static const char ibmvnic_driver_name[] = "ibmvnic";
84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
86 MODULE_AUTHOR("Santiago Leon");
87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92 static int ibmvnic_remove(struct vio_dev *);
93 static void release_sub_crqs(struct ibmvnic_adapter *);
94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102 static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104 static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106 static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110 static int ibmvnic_poll(struct napi_struct *napi, int data);
111 static void send_map_query(struct ibmvnic_adapter *adapter);
112 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static void send_request_unmap(struct ibmvnic_adapter *, u8);
114 static void send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
122 struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
131 static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
173 struct device *dev = &adapter->vdev->dev;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
180 dev_err(dev, "Couldn't alloc long term buffer\n");
183 ltb->map_id = adapter->map_id;
186 init_completion(&adapter->fw_done);
187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
189 wait_for_completion(&adapter->fw_done);
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
202 struct device *dev = &adapter->vdev->dev;
207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
209 send_request_unmap(adapter, ltb->map_id);
210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
216 memset(ltb->buff, 0, ltb->size);
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
237 adapter->rx_pool[i].active = 0;
240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
272 index = pool->free_map[pool->next_free];
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
302 #ifdef __LITTLE_ENDIAN__
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
309 if (lpar_rc != H_SUCCESS)
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
316 atomic_add(buffers_added, &pool->available);
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
359 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
361 adapter->tx_stats_buffers =
362 kcalloc(adapter->req_tx_queues,
363 sizeof(struct ibmvnic_tx_queue_stats),
365 if (!adapter->tx_stats_buffers)
368 adapter->rx_stats_buffers =
369 kcalloc(adapter->req_rx_queues,
370 sizeof(struct ibmvnic_rx_queue_stats),
372 if (!adapter->rx_stats_buffers)
378 static void release_stats_token(struct ibmvnic_adapter *adapter)
380 struct device *dev = &adapter->vdev->dev;
382 if (!adapter->stats_token)
385 dma_unmap_single(dev, adapter->stats_token,
386 sizeof(struct ibmvnic_statistics),
388 adapter->stats_token = 0;
391 static int init_stats_token(struct ibmvnic_adapter *adapter)
393 struct device *dev = &adapter->vdev->dev;
396 stok = dma_map_single(dev, &adapter->stats,
397 sizeof(struct ibmvnic_statistics),
399 if (dma_mapping_error(dev, stok)) {
400 dev_err(dev, "Couldn't map stats buffer\n");
404 adapter->stats_token = stok;
405 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
409 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
411 struct ibmvnic_rx_pool *rx_pool;
416 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
417 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
419 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
420 for (i = 0; i < rx_scrqs; i++) {
421 rx_pool = &adapter->rx_pool[i];
423 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
425 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
426 free_long_term_buff(adapter, &rx_pool->long_term_buff);
427 rx_pool->buff_size = be64_to_cpu(size_array[i]);
428 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
432 rc = reset_long_term_buff(adapter,
433 &rx_pool->long_term_buff);
439 for (j = 0; j < rx_pool->size; j++)
440 rx_pool->free_map[j] = j;
442 memset(rx_pool->rx_buff, 0,
443 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
445 atomic_set(&rx_pool->available, 0);
446 rx_pool->next_alloc = 0;
447 rx_pool->next_free = 0;
454 static void release_rx_pools(struct ibmvnic_adapter *adapter)
456 struct ibmvnic_rx_pool *rx_pool;
459 if (!adapter->rx_pool)
462 for (i = 0; i < adapter->num_active_rx_pools; i++) {
463 rx_pool = &adapter->rx_pool[i];
465 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
467 kfree(rx_pool->free_map);
468 free_long_term_buff(adapter, &rx_pool->long_term_buff);
470 if (!rx_pool->rx_buff)
473 for (j = 0; j < rx_pool->size; j++) {
474 if (rx_pool->rx_buff[j].skb) {
475 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
476 rx_pool->rx_buff[i].skb = NULL;
480 kfree(rx_pool->rx_buff);
483 kfree(adapter->rx_pool);
484 adapter->rx_pool = NULL;
485 adapter->num_active_rx_pools = 0;
488 static int init_rx_pools(struct net_device *netdev)
490 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
491 struct device *dev = &adapter->vdev->dev;
492 struct ibmvnic_rx_pool *rx_pool;
498 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
499 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
500 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
502 adapter->rx_pool = kcalloc(rxadd_subcrqs,
503 sizeof(struct ibmvnic_rx_pool),
505 if (!adapter->rx_pool) {
506 dev_err(dev, "Failed to allocate rx pools\n");
510 adapter->num_active_rx_pools = 0;
512 for (i = 0; i < rxadd_subcrqs; i++) {
513 rx_pool = &adapter->rx_pool[i];
515 netdev_dbg(adapter->netdev,
516 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
517 i, adapter->req_rx_add_entries_per_subcrq,
518 be64_to_cpu(size_array[i]));
520 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
522 rx_pool->buff_size = be64_to_cpu(size_array[i]);
525 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
527 if (!rx_pool->free_map) {
528 release_rx_pools(adapter);
532 rx_pool->rx_buff = kcalloc(rx_pool->size,
533 sizeof(struct ibmvnic_rx_buff),
535 if (!rx_pool->rx_buff) {
536 dev_err(dev, "Couldn't alloc rx buffers\n");
537 release_rx_pools(adapter);
541 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
542 rx_pool->size * rx_pool->buff_size)) {
543 release_rx_pools(adapter);
547 for (j = 0; j < rx_pool->size; ++j)
548 rx_pool->free_map[j] = j;
550 atomic_set(&rx_pool->available, 0);
551 rx_pool->next_alloc = 0;
552 rx_pool->next_free = 0;
555 adapter->num_active_rx_pools = rxadd_subcrqs;
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);
604 static void release_tx_pools(struct ibmvnic_adapter *adapter)
606 struct ibmvnic_tx_pool *tx_pool;
609 if (!adapter->tx_pool)
612 for (i = 0; i < adapter->num_active_tx_pools; i++) {
613 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
614 tx_pool = &adapter->tx_pool[i];
615 kfree(tx_pool->tx_buff);
616 free_long_term_buff(adapter, &tx_pool->long_term_buff);
617 free_long_term_buff(adapter, &tx_pool->tso_ltb);
618 kfree(tx_pool->free_map);
621 kfree(adapter->tx_pool);
622 adapter->tx_pool = NULL;
623 adapter->num_active_tx_pools = 0;
626 static int init_tx_pools(struct net_device *netdev)
628 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
629 struct device *dev = &adapter->vdev->dev;
630 struct ibmvnic_tx_pool *tx_pool;
634 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
635 adapter->tx_pool = kcalloc(tx_subcrqs,
636 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
637 if (!adapter->tx_pool)
640 adapter->num_active_tx_pools = 0;
642 for (i = 0; i < tx_subcrqs; i++) {
643 tx_pool = &adapter->tx_pool[i];
645 netdev_dbg(adapter->netdev,
646 "Initializing tx_pool[%d], %lld buffs\n",
647 i, adapter->req_tx_entries_per_subcrq);
649 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
650 sizeof(struct ibmvnic_tx_buff),
652 if (!tx_pool->tx_buff) {
653 dev_err(dev, "tx pool buffer allocation failed\n");
654 release_tx_pools(adapter);
658 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
659 adapter->req_tx_entries_per_subcrq *
661 release_tx_pools(adapter);
666 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
668 IBMVNIC_TSO_BUF_SZ)) {
669 release_tx_pools(adapter);
673 tx_pool->tso_index = 0;
675 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
676 sizeof(int), GFP_KERNEL);
677 if (!tx_pool->free_map) {
678 release_tx_pools(adapter);
682 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
683 tx_pool->free_map[j] = j;
685 tx_pool->consumer_index = 0;
686 tx_pool->producer_index = 0;
689 adapter->num_active_tx_pools = tx_subcrqs;
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 ibmvnic_login(struct net_device *netdev)
741 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
742 unsigned long timeout = msecs_to_jiffies(30000);
743 struct device *dev = &adapter->vdev->dev;
747 if (adapter->renegotiate) {
748 adapter->renegotiate = false;
749 release_sub_crqs(adapter);
751 reinit_completion(&adapter->init_done);
752 send_cap_queries(adapter);
753 if (!wait_for_completion_timeout(&adapter->init_done,
755 dev_err(dev, "Capabilities query timeout\n");
758 rc = init_sub_crqs(adapter);
761 "Initialization of SCRQ's failed\n");
764 rc = init_sub_crq_irqs(adapter);
767 "Initialization of SCRQ's irqs failed\n");
772 reinit_completion(&adapter->init_done);
774 if (!wait_for_completion_timeout(&adapter->init_done,
776 dev_err(dev, "Login timeout\n");
779 } while (adapter->renegotiate);
781 /* handle pending MAC address changes after successful login */
782 if (adapter->mac_change_pending) {
783 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
784 adapter->mac_change_pending = false;
790 static void release_resources(struct ibmvnic_adapter *adapter)
794 release_vpd_data(adapter);
796 release_tx_pools(adapter);
797 release_rx_pools(adapter);
799 release_stats_token(adapter);
800 release_stats_buffers(adapter);
801 release_error_buffers(adapter);
804 for (i = 0; i < adapter->req_rx_queues; i++) {
805 if (&adapter->napi[i]) {
806 netdev_dbg(adapter->netdev,
807 "Releasing napi[%d]\n", i);
808 netif_napi_del(&adapter->napi[i]);
814 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
816 struct net_device *netdev = adapter->netdev;
817 unsigned long timeout = msecs_to_jiffies(30000);
818 union ibmvnic_crq crq;
822 netdev_dbg(netdev, "setting link state %d\n", link_state);
824 memset(&crq, 0, sizeof(crq));
825 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
826 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
827 crq.logical_link_state.link_state = link_state;
832 reinit_completion(&adapter->init_done);
833 rc = ibmvnic_send_crq(adapter, &crq);
835 netdev_err(netdev, "Failed to set link state\n");
839 if (!wait_for_completion_timeout(&adapter->init_done,
841 netdev_err(netdev, "timeout setting link state\n");
845 if (adapter->init_done_rc == 1) {
846 /* Partuial success, delay and re-send */
855 static int set_real_num_queues(struct net_device *netdev)
857 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
860 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
861 adapter->req_tx_queues, adapter->req_rx_queues);
863 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
865 netdev_err(netdev, "failed to set the number of tx queues\n");
869 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
871 netdev_err(netdev, "failed to set the number of rx queues\n");
876 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
878 struct device *dev = &adapter->vdev->dev;
879 union ibmvnic_crq crq;
882 if (adapter->vpd->buff)
883 len = adapter->vpd->len;
885 init_completion(&adapter->fw_done);
886 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
887 crq.get_vpd_size.cmd = GET_VPD_SIZE;
888 ibmvnic_send_crq(adapter, &crq);
889 wait_for_completion(&adapter->fw_done);
891 if (!adapter->vpd->len)
894 if (!adapter->vpd->buff)
895 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
896 else if (adapter->vpd->len != len)
898 krealloc(adapter->vpd->buff,
899 adapter->vpd->len, GFP_KERNEL);
901 if (!adapter->vpd->buff) {
902 dev_err(dev, "Could allocate VPD buffer\n");
906 adapter->vpd->dma_addr =
907 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
909 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
910 dev_err(dev, "Could not map VPD buffer\n");
911 kfree(adapter->vpd->buff);
915 reinit_completion(&adapter->fw_done);
916 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
917 crq.get_vpd.cmd = GET_VPD;
918 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
919 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
920 ibmvnic_send_crq(adapter, &crq);
921 wait_for_completion(&adapter->fw_done);
926 static int init_resources(struct ibmvnic_adapter *adapter)
928 struct net_device *netdev = adapter->netdev;
931 rc = set_real_num_queues(netdev);
935 rc = init_stats_buffers(adapter);
939 rc = init_stats_token(adapter);
943 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
947 /* Vital Product Data (VPD) */
948 rc = ibmvnic_get_vpd(adapter);
950 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
955 adapter->napi = kcalloc(adapter->req_rx_queues,
956 sizeof(struct napi_struct), GFP_KERNEL);
960 for (i = 0; i < adapter->req_rx_queues; i++) {
961 netdev_dbg(netdev, "Adding napi[%d]\n", i);
962 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
966 send_map_query(adapter);
968 rc = init_rx_pools(netdev);
972 rc = init_tx_pools(netdev);
976 static int __ibmvnic_open(struct net_device *netdev)
978 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
979 enum vnic_state prev_state = adapter->state;
982 adapter->state = VNIC_OPENING;
983 replenish_pools(adapter);
984 ibmvnic_napi_enable(adapter);
986 /* We're ready to receive frames, enable the sub-crq interrupts and
987 * set the logical link state to up
989 for (i = 0; i < adapter->req_rx_queues; i++) {
990 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
991 if (prev_state == VNIC_CLOSED)
992 enable_irq(adapter->rx_scrq[i]->irq);
994 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
997 for (i = 0; i < adapter->req_tx_queues; i++) {
998 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
999 if (prev_state == VNIC_CLOSED)
1000 enable_irq(adapter->tx_scrq[i]->irq);
1002 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1005 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1007 for (i = 0; i < adapter->req_rx_queues; i++)
1008 napi_disable(&adapter->napi[i]);
1009 release_resources(adapter);
1013 netif_tx_start_all_queues(netdev);
1015 if (prev_state == VNIC_CLOSED) {
1016 for (i = 0; i < adapter->req_rx_queues; i++)
1017 napi_schedule(&adapter->napi[i]);
1020 adapter->state = VNIC_OPEN;
1024 static int ibmvnic_open(struct net_device *netdev)
1026 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1029 mutex_lock(&adapter->reset_lock);
1031 if (adapter->state != VNIC_CLOSED) {
1032 rc = ibmvnic_login(netdev);
1034 mutex_unlock(&adapter->reset_lock);
1038 rc = init_resources(adapter);
1040 netdev_err(netdev, "failed to initialize resources\n");
1041 release_resources(adapter);
1042 mutex_unlock(&adapter->reset_lock);
1047 rc = __ibmvnic_open(netdev);
1048 netif_carrier_on(netdev);
1050 mutex_unlock(&adapter->reset_lock);
1055 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1057 struct ibmvnic_tx_pool *tx_pool;
1062 if (!adapter->tx_pool)
1065 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1066 tx_entries = adapter->req_tx_entries_per_subcrq;
1068 /* Free any remaining skbs in the tx buffer pools */
1069 for (i = 0; i < tx_scrqs; i++) {
1070 tx_pool = &adapter->tx_pool[i];
1074 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1075 for (j = 0; j < tx_entries; j++) {
1076 if (tx_pool->tx_buff[j].skb) {
1077 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1078 tx_pool->tx_buff[j].skb = NULL;
1084 static int __ibmvnic_close(struct net_device *netdev)
1086 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1090 adapter->state = VNIC_CLOSING;
1092 /* ensure that transmissions are stopped if called by do_reset */
1093 if (adapter->resetting)
1094 netif_tx_disable(netdev);
1096 netif_tx_stop_all_queues(netdev);
1098 ibmvnic_napi_disable(adapter);
1100 if (adapter->tx_scrq) {
1101 for (i = 0; i < adapter->req_tx_queues; i++)
1102 if (adapter->tx_scrq[i]->irq) {
1103 netdev_dbg(adapter->netdev,
1104 "Disabling tx_scrq[%d] irq\n", i);
1105 disable_irq(adapter->tx_scrq[i]->irq);
1109 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1113 if (adapter->rx_scrq) {
1114 for (i = 0; i < adapter->req_rx_queues; i++) {
1117 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1125 if (adapter->rx_scrq[i]->irq) {
1126 netdev_dbg(adapter->netdev,
1127 "Disabling rx_scrq[%d] irq\n", i);
1128 disable_irq(adapter->rx_scrq[i]->irq);
1133 clean_tx_pools(adapter);
1134 adapter->state = VNIC_CLOSED;
1138 static int ibmvnic_close(struct net_device *netdev)
1140 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1143 mutex_lock(&adapter->reset_lock);
1144 rc = __ibmvnic_close(netdev);
1145 mutex_unlock(&adapter->reset_lock);
1151 * build_hdr_data - creates L2/L3/L4 header data buffer
1152 * @hdr_field - bitfield determining needed headers
1153 * @skb - socket buffer
1154 * @hdr_len - array of header lengths
1155 * @tot_len - total length of data
1157 * Reads hdr_field to determine which headers are needed by firmware.
1158 * Builds a buffer containing these headers. Saves individual header
1159 * lengths and total buffer length to be used to build descriptors.
1161 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1162 int *hdr_len, u8 *hdr_data)
1167 hdr_len[0] = sizeof(struct ethhdr);
1169 if (skb->protocol == htons(ETH_P_IP)) {
1170 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1171 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1172 hdr_len[2] = tcp_hdrlen(skb);
1173 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1174 hdr_len[2] = sizeof(struct udphdr);
1175 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1176 hdr_len[1] = sizeof(struct ipv6hdr);
1177 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1178 hdr_len[2] = tcp_hdrlen(skb);
1179 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1180 hdr_len[2] = sizeof(struct udphdr);
1181 } else if (skb->protocol == htons(ETH_P_ARP)) {
1182 hdr_len[1] = arp_hdr_len(skb->dev);
1186 memset(hdr_data, 0, 120);
1187 if ((hdr_field >> 6) & 1) {
1188 hdr = skb_mac_header(skb);
1189 memcpy(hdr_data, hdr, hdr_len[0]);
1193 if ((hdr_field >> 5) & 1) {
1194 hdr = skb_network_header(skb);
1195 memcpy(hdr_data + len, hdr, hdr_len[1]);
1199 if ((hdr_field >> 4) & 1) {
1200 hdr = skb_transport_header(skb);
1201 memcpy(hdr_data + len, hdr, hdr_len[2]);
1208 * create_hdr_descs - create header and header extension descriptors
1209 * @hdr_field - bitfield determining needed headers
1210 * @data - buffer containing header data
1211 * @len - length of data buffer
1212 * @hdr_len - array of individual header lengths
1213 * @scrq_arr - descriptor array
1215 * Creates header and, if needed, header extension descriptors and
1216 * places them in a descriptor array, scrq_arr
1219 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1220 union sub_crq *scrq_arr)
1222 union sub_crq hdr_desc;
1228 while (tmp_len > 0) {
1229 cur = hdr_data + len - tmp_len;
1231 memset(&hdr_desc, 0, sizeof(hdr_desc));
1232 if (cur != hdr_data) {
1233 data = hdr_desc.hdr_ext.data;
1234 tmp = tmp_len > 29 ? 29 : tmp_len;
1235 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1236 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1237 hdr_desc.hdr_ext.len = tmp;
1239 data = hdr_desc.hdr.data;
1240 tmp = tmp_len > 24 ? 24 : tmp_len;
1241 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1242 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1243 hdr_desc.hdr.len = tmp;
1244 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1245 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1246 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1247 hdr_desc.hdr.flag = hdr_field << 1;
1249 memcpy(data, cur, tmp);
1251 *scrq_arr = hdr_desc;
1260 * build_hdr_descs_arr - build a header descriptor array
1261 * @skb - socket buffer
1262 * @num_entries - number of descriptors to be sent
1263 * @subcrq - first TX descriptor
1264 * @hdr_field - bit field determining which headers will be sent
1266 * This function will build a TX descriptor array with applicable
1267 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1270 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1271 int *num_entries, u8 hdr_field)
1273 int hdr_len[3] = {0, 0, 0};
1275 u8 *hdr_data = txbuff->hdr_data;
1277 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1279 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1280 txbuff->indir_arr + 1);
1283 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1285 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1286 int queue_num = skb_get_queue_mapping(skb);
1287 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1288 struct device *dev = &adapter->vdev->dev;
1289 struct ibmvnic_tx_buff *tx_buff = NULL;
1290 struct ibmvnic_sub_crq_queue *tx_scrq;
1291 struct ibmvnic_tx_pool *tx_pool;
1292 unsigned int tx_send_failed = 0;
1293 unsigned int tx_map_failed = 0;
1294 unsigned int tx_dropped = 0;
1295 unsigned int tx_packets = 0;
1296 unsigned int tx_bytes = 0;
1297 dma_addr_t data_dma_addr;
1298 struct netdev_queue *txq;
1299 unsigned long lpar_rc;
1300 union sub_crq tx_crq;
1301 unsigned int offset;
1302 int num_entries = 1;
1309 if (adapter->resetting) {
1310 if (!netif_subqueue_stopped(netdev, skb))
1311 netif_stop_subqueue(netdev, queue_num);
1312 dev_kfree_skb_any(skb);
1320 tx_pool = &adapter->tx_pool[queue_num];
1321 tx_scrq = adapter->tx_scrq[queue_num];
1322 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1323 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1324 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1326 index = tx_pool->free_map[tx_pool->consumer_index];
1328 if (skb_is_gso(skb)) {
1329 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1330 dst = tx_pool->tso_ltb.buff + offset;
1331 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1332 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1333 tx_pool->tso_index++;
1334 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1335 tx_pool->tso_index = 0;
1337 offset = index * adapter->req_mtu;
1338 dst = tx_pool->long_term_buff.buff + offset;
1339 memset(dst, 0, adapter->req_mtu);
1340 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1343 if (skb_shinfo(skb)->nr_frags) {
1347 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1348 cur = skb_headlen(skb);
1350 /* Copy the frags */
1351 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1352 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1355 page_address(skb_frag_page(frag)) +
1356 frag->page_offset, skb_frag_size(frag));
1357 cur += skb_frag_size(frag);
1360 skb_copy_from_linear_data(skb, dst, skb->len);
1363 tx_pool->consumer_index =
1364 (tx_pool->consumer_index + 1) %
1365 adapter->req_tx_entries_per_subcrq;
1367 tx_buff = &tx_pool->tx_buff[index];
1369 tx_buff->data_dma[0] = data_dma_addr;
1370 tx_buff->data_len[0] = skb->len;
1371 tx_buff->index = index;
1372 tx_buff->pool_index = queue_num;
1373 tx_buff->last_frag = true;
1375 memset(&tx_crq, 0, sizeof(tx_crq));
1376 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1377 tx_crq.v1.type = IBMVNIC_TX_DESC;
1378 tx_crq.v1.n_crq_elem = 1;
1379 tx_crq.v1.n_sge = 1;
1380 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1381 tx_crq.v1.correlator = cpu_to_be32(index);
1382 if (skb_is_gso(skb))
1383 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1385 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1386 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1387 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1389 if (adapter->vlan_header_insertion) {
1390 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1391 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1394 if (skb->protocol == htons(ETH_P_IP)) {
1395 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1396 proto = ip_hdr(skb)->protocol;
1397 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1398 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1399 proto = ipv6_hdr(skb)->nexthdr;
1402 if (proto == IPPROTO_TCP)
1403 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1404 else if (proto == IPPROTO_UDP)
1405 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1407 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1408 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1411 if (skb_is_gso(skb)) {
1412 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1413 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1416 /* determine if l2/3/4 headers are sent to firmware */
1417 if ((*hdrs >> 7) & 1 &&
1418 (skb->protocol == htons(ETH_P_IP) ||
1419 skb->protocol == htons(ETH_P_IPV6) ||
1420 skb->protocol == htons(ETH_P_ARP))) {
1421 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1422 tx_crq.v1.n_crq_elem = num_entries;
1423 tx_buff->indir_arr[0] = tx_crq;
1424 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1425 sizeof(tx_buff->indir_arr),
1427 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1428 dev_kfree_skb_any(skb);
1429 tx_buff->skb = NULL;
1430 if (!firmware_has_feature(FW_FEATURE_CMO))
1431 dev_err(dev, "tx: unable to map descriptor array\n");
1437 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1438 (u64)tx_buff->indir_dma,
1441 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1444 if (lpar_rc != H_SUCCESS) {
1445 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1447 if (tx_pool->consumer_index == 0)
1448 tx_pool->consumer_index =
1449 adapter->req_tx_entries_per_subcrq - 1;
1451 tx_pool->consumer_index--;
1453 dev_kfree_skb_any(skb);
1454 tx_buff->skb = NULL;
1456 if (lpar_rc == H_CLOSED) {
1457 /* Disable TX and report carrier off if queue is closed.
1458 * Firmware guarantees that a signal will be sent to the
1459 * driver, triggering a reset or some other action.
1461 netif_tx_stop_all_queues(netdev);
1462 netif_carrier_off(netdev);
1471 if (atomic_inc_return(&tx_scrq->used)
1472 >= adapter->req_tx_entries_per_subcrq) {
1473 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1474 netif_stop_subqueue(netdev, queue_num);
1478 tx_bytes += skb->len;
1479 txq->trans_start = jiffies;
1483 netdev->stats.tx_dropped += tx_dropped;
1484 netdev->stats.tx_bytes += tx_bytes;
1485 netdev->stats.tx_packets += tx_packets;
1486 adapter->tx_send_failed += tx_send_failed;
1487 adapter->tx_map_failed += tx_map_failed;
1488 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1489 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1490 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1495 static void ibmvnic_set_multi(struct net_device *netdev)
1497 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1498 struct netdev_hw_addr *ha;
1499 union ibmvnic_crq crq;
1501 memset(&crq, 0, sizeof(crq));
1502 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1503 crq.request_capability.cmd = REQUEST_CAPABILITY;
1505 if (netdev->flags & IFF_PROMISC) {
1506 if (!adapter->promisc_supported)
1509 if (netdev->flags & IFF_ALLMULTI) {
1510 /* Accept all multicast */
1511 memset(&crq, 0, sizeof(crq));
1512 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1513 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1514 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1515 ibmvnic_send_crq(adapter, &crq);
1516 } else if (netdev_mc_empty(netdev)) {
1517 /* Reject all multicast */
1518 memset(&crq, 0, sizeof(crq));
1519 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1520 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1521 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1522 ibmvnic_send_crq(adapter, &crq);
1524 /* Accept one or more multicast(s) */
1525 netdev_for_each_mc_addr(ha, netdev) {
1526 memset(&crq, 0, sizeof(crq));
1527 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1528 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1529 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1530 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1532 ibmvnic_send_crq(adapter, &crq);
1538 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1540 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1541 struct sockaddr *addr = p;
1542 union ibmvnic_crq crq;
1544 if (!is_valid_ether_addr(addr->sa_data))
1545 return -EADDRNOTAVAIL;
1547 memset(&crq, 0, sizeof(crq));
1548 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1549 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1550 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1551 ibmvnic_send_crq(adapter, &crq);
1552 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1556 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1558 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1559 struct sockaddr *addr = p;
1561 if (adapter->state == VNIC_PROBED) {
1562 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1563 adapter->mac_change_pending = true;
1567 __ibmvnic_set_mac(netdev, addr);
1573 * do_reset returns zero if we are able to keep processing reset events, or
1574 * non-zero if we hit a fatal error and must halt.
1576 static int do_reset(struct ibmvnic_adapter *adapter,
1577 struct ibmvnic_rwi *rwi, u32 reset_state)
1579 u64 old_num_rx_queues, old_num_tx_queues;
1580 struct net_device *netdev = adapter->netdev;
1583 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1586 netif_carrier_off(netdev);
1587 adapter->reset_reason = rwi->reset_reason;
1589 old_num_rx_queues = adapter->req_rx_queues;
1590 old_num_tx_queues = adapter->req_tx_queues;
1592 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1593 rc = ibmvnic_reenable_crq_queue(adapter);
1598 rc = __ibmvnic_close(netdev);
1602 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1603 adapter->wait_for_reset) {
1604 release_resources(adapter);
1605 release_sub_crqs(adapter);
1606 release_crq_queue(adapter);
1609 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1610 /* remove the closed state so when we call open it appears
1611 * we are coming from the probed state.
1613 adapter->state = VNIC_PROBED;
1615 rc = ibmvnic_init(adapter);
1617 return IBMVNIC_INIT_FAILED;
1619 /* If the adapter was in PROBE state prior to the reset,
1622 if (reset_state == VNIC_PROBED)
1625 rc = ibmvnic_login(netdev);
1627 adapter->state = VNIC_PROBED;
1631 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1632 adapter->wait_for_reset) {
1633 rc = init_resources(adapter);
1636 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1637 adapter->req_tx_queues != old_num_tx_queues) {
1638 release_rx_pools(adapter);
1639 release_tx_pools(adapter);
1640 init_rx_pools(netdev);
1641 init_tx_pools(netdev);
1643 rc = reset_tx_pools(adapter);
1647 rc = reset_rx_pools(adapter);
1651 if (reset_state == VNIC_CLOSED)
1656 rc = __ibmvnic_open(netdev);
1658 if (list_empty(&adapter->rwi_list))
1659 adapter->state = VNIC_CLOSED;
1661 adapter->state = reset_state;
1666 netif_carrier_on(netdev);
1669 for (i = 0; i < adapter->req_rx_queues; i++)
1670 napi_schedule(&adapter->napi[i]);
1672 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1673 netdev_notify_peers(netdev);
1678 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1680 struct ibmvnic_rwi *rwi;
1682 mutex_lock(&adapter->rwi_lock);
1684 if (!list_empty(&adapter->rwi_list)) {
1685 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1687 list_del(&rwi->list);
1692 mutex_unlock(&adapter->rwi_lock);
1696 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1698 struct ibmvnic_rwi *rwi;
1700 rwi = get_next_rwi(adapter);
1703 rwi = get_next_rwi(adapter);
1707 static void __ibmvnic_reset(struct work_struct *work)
1709 struct ibmvnic_rwi *rwi;
1710 struct ibmvnic_adapter *adapter;
1711 struct net_device *netdev;
1715 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1716 netdev = adapter->netdev;
1718 mutex_lock(&adapter->reset_lock);
1719 adapter->resetting = true;
1720 reset_state = adapter->state;
1722 rwi = get_next_rwi(adapter);
1724 rc = do_reset(adapter, rwi, reset_state);
1726 if (rc && rc != IBMVNIC_INIT_FAILED)
1729 rwi = get_next_rwi(adapter);
1732 if (adapter->wait_for_reset) {
1733 adapter->wait_for_reset = false;
1734 adapter->reset_done_rc = rc;
1735 complete(&adapter->reset_done);
1739 netdev_dbg(adapter->netdev, "Reset failed\n");
1740 free_all_rwi(adapter);
1741 mutex_unlock(&adapter->reset_lock);
1745 adapter->resetting = false;
1746 mutex_unlock(&adapter->reset_lock);
1749 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1750 enum ibmvnic_reset_reason reason)
1752 struct ibmvnic_rwi *rwi, *tmp;
1753 struct net_device *netdev = adapter->netdev;
1754 struct list_head *entry;
1756 if (adapter->state == VNIC_REMOVING ||
1757 adapter->state == VNIC_REMOVED) {
1758 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1762 if (adapter->state == VNIC_PROBING) {
1763 netdev_warn(netdev, "Adapter reset during probe\n");
1764 adapter->init_done_rc = EAGAIN;
1768 mutex_lock(&adapter->rwi_lock);
1770 list_for_each(entry, &adapter->rwi_list) {
1771 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1772 if (tmp->reset_reason == reason) {
1773 netdev_dbg(netdev, "Skipping matching reset\n");
1774 mutex_unlock(&adapter->rwi_lock);
1779 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1781 mutex_unlock(&adapter->rwi_lock);
1782 ibmvnic_close(netdev);
1786 rwi->reset_reason = reason;
1787 list_add_tail(&rwi->list, &adapter->rwi_list);
1788 mutex_unlock(&adapter->rwi_lock);
1790 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1791 schedule_work(&adapter->ibmvnic_reset);
1794 static void ibmvnic_tx_timeout(struct net_device *dev)
1796 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1798 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1801 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1802 struct ibmvnic_rx_buff *rx_buff)
1804 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1806 rx_buff->skb = NULL;
1808 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1809 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1811 atomic_dec(&pool->available);
1814 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1816 struct net_device *netdev = napi->dev;
1817 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1818 int scrq_num = (int)(napi - adapter->napi);
1819 int frames_processed = 0;
1822 while (frames_processed < budget) {
1823 struct sk_buff *skb;
1824 struct ibmvnic_rx_buff *rx_buff;
1825 union sub_crq *next;
1830 if (unlikely(adapter->resetting)) {
1831 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1832 napi_complete_done(napi, frames_processed);
1833 return frames_processed;
1836 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1838 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1840 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1841 rx_comp.correlator);
1842 /* do error checking */
1843 if (next->rx_comp.rc) {
1844 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1845 be16_to_cpu(next->rx_comp.rc));
1846 /* free the entry */
1847 next->rx_comp.first = 0;
1848 remove_buff_from_pool(adapter, rx_buff);
1852 length = be32_to_cpu(next->rx_comp.len);
1853 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1854 flags = next->rx_comp.flags;
1856 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1859 /* VLAN Header has been stripped by the system firmware and
1860 * needs to be inserted by the driver
1862 if (adapter->rx_vlan_header_insertion &&
1863 (flags & IBMVNIC_VLAN_STRIPPED))
1864 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1865 ntohs(next->rx_comp.vlan_tci));
1867 /* free the entry */
1868 next->rx_comp.first = 0;
1869 remove_buff_from_pool(adapter, rx_buff);
1871 skb_put(skb, length);
1872 skb->protocol = eth_type_trans(skb, netdev);
1873 skb_record_rx_queue(skb, scrq_num);
1875 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1876 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1877 skb->ip_summed = CHECKSUM_UNNECESSARY;
1881 napi_gro_receive(napi, skb); /* send it up */
1882 netdev->stats.rx_packets++;
1883 netdev->stats.rx_bytes += length;
1884 adapter->rx_stats_buffers[scrq_num].packets++;
1885 adapter->rx_stats_buffers[scrq_num].bytes += length;
1889 if (adapter->state != VNIC_CLOSING)
1890 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1892 if (frames_processed < budget) {
1893 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1894 napi_complete_done(napi, frames_processed);
1895 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1896 napi_reschedule(napi)) {
1897 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1901 return frames_processed;
1904 #ifdef CONFIG_NET_POLL_CONTROLLER
1905 static void ibmvnic_netpoll_controller(struct net_device *dev)
1907 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1910 replenish_pools(netdev_priv(dev));
1911 for (i = 0; i < adapter->req_rx_queues; i++)
1912 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1913 adapter->rx_scrq[i]);
1917 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1919 adapter->fallback.mtu = adapter->req_mtu;
1920 adapter->fallback.rx_queues = adapter->req_rx_queues;
1921 adapter->fallback.tx_queues = adapter->req_tx_queues;
1922 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1923 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1925 init_completion(&adapter->reset_done);
1926 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1927 adapter->wait_for_reset = true;
1928 wait_for_completion(&adapter->reset_done);
1930 if (adapter->reset_done_rc) {
1931 adapter->desired.mtu = adapter->fallback.mtu;
1932 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1933 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1934 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1935 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1937 init_completion(&adapter->reset_done);
1938 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1939 wait_for_completion(&adapter->reset_done);
1941 adapter->wait_for_reset = false;
1943 return adapter->reset_done_rc;
1946 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1948 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1950 adapter->desired.mtu = new_mtu + ETH_HLEN;
1952 return wait_for_reset(adapter);
1955 static const struct net_device_ops ibmvnic_netdev_ops = {
1956 .ndo_open = ibmvnic_open,
1957 .ndo_stop = ibmvnic_close,
1958 .ndo_start_xmit = ibmvnic_xmit,
1959 .ndo_set_rx_mode = ibmvnic_set_multi,
1960 .ndo_set_mac_address = ibmvnic_set_mac,
1961 .ndo_validate_addr = eth_validate_addr,
1962 .ndo_tx_timeout = ibmvnic_tx_timeout,
1963 #ifdef CONFIG_NET_POLL_CONTROLLER
1964 .ndo_poll_controller = ibmvnic_netpoll_controller,
1966 .ndo_change_mtu = ibmvnic_change_mtu,
1969 /* ethtool functions */
1971 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1972 struct ethtool_link_ksettings *cmd)
1974 u32 supported, advertising;
1976 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1978 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1980 cmd->base.speed = SPEED_1000;
1981 cmd->base.duplex = DUPLEX_FULL;
1982 cmd->base.port = PORT_FIBRE;
1983 cmd->base.phy_address = 0;
1984 cmd->base.autoneg = AUTONEG_ENABLE;
1986 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1988 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1994 static void ibmvnic_get_drvinfo(struct net_device *netdev,
1995 struct ethtool_drvinfo *info)
1997 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1999 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2000 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2001 strlcpy(info->fw_version, adapter->fw_version,
2002 sizeof(info->fw_version));
2005 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2009 return adapter->msg_enable;
2012 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2014 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2016 adapter->msg_enable = data;
2019 static u32 ibmvnic_get_link(struct net_device *netdev)
2021 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2023 /* Don't need to send a query because we request a logical link up at
2024 * init and then we wait for link state indications
2026 return adapter->logical_link_state;
2029 static void ibmvnic_get_ringparam(struct net_device *netdev,
2030 struct ethtool_ringparam *ring)
2032 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2034 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2035 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2036 ring->rx_mini_max_pending = 0;
2037 ring->rx_jumbo_max_pending = 0;
2038 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2039 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2040 ring->rx_mini_pending = 0;
2041 ring->rx_jumbo_pending = 0;
2044 static int ibmvnic_set_ringparam(struct net_device *netdev,
2045 struct ethtool_ringparam *ring)
2047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2049 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2050 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2051 netdev_err(netdev, "Invalid request.\n");
2052 netdev_err(netdev, "Max tx buffers = %llu\n",
2053 adapter->max_rx_add_entries_per_subcrq);
2054 netdev_err(netdev, "Max rx buffers = %llu\n",
2055 adapter->max_tx_entries_per_subcrq);
2059 adapter->desired.rx_entries = ring->rx_pending;
2060 adapter->desired.tx_entries = ring->tx_pending;
2062 return wait_for_reset(adapter);
2065 static void ibmvnic_get_channels(struct net_device *netdev,
2066 struct ethtool_channels *channels)
2068 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2070 channels->max_rx = adapter->max_rx_queues;
2071 channels->max_tx = adapter->max_tx_queues;
2072 channels->max_other = 0;
2073 channels->max_combined = 0;
2074 channels->rx_count = adapter->req_rx_queues;
2075 channels->tx_count = adapter->req_tx_queues;
2076 channels->other_count = 0;
2077 channels->combined_count = 0;
2080 static int ibmvnic_set_channels(struct net_device *netdev,
2081 struct ethtool_channels *channels)
2083 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2085 adapter->desired.rx_queues = channels->rx_count;
2086 adapter->desired.tx_queues = channels->tx_count;
2088 return wait_for_reset(adapter);
2091 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2093 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2096 if (stringset != ETH_SS_STATS)
2099 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2100 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2102 for (i = 0; i < adapter->req_tx_queues; i++) {
2103 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2104 data += ETH_GSTRING_LEN;
2106 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2107 data += ETH_GSTRING_LEN;
2109 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2110 data += ETH_GSTRING_LEN;
2113 for (i = 0; i < adapter->req_rx_queues; i++) {
2114 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2115 data += ETH_GSTRING_LEN;
2117 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2118 data += ETH_GSTRING_LEN;
2120 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2121 data += ETH_GSTRING_LEN;
2125 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2127 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2131 return ARRAY_SIZE(ibmvnic_stats) +
2132 adapter->req_tx_queues * NUM_TX_STATS +
2133 adapter->req_rx_queues * NUM_RX_STATS;
2139 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2140 struct ethtool_stats *stats, u64 *data)
2142 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2143 union ibmvnic_crq crq;
2146 memset(&crq, 0, sizeof(crq));
2147 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2148 crq.request_statistics.cmd = REQUEST_STATISTICS;
2149 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2150 crq.request_statistics.len =
2151 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2153 /* Wait for data to be written */
2154 init_completion(&adapter->stats_done);
2155 ibmvnic_send_crq(adapter, &crq);
2156 wait_for_completion(&adapter->stats_done);
2158 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2159 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2160 ibmvnic_stats[i].offset));
2162 for (j = 0; j < adapter->req_tx_queues; j++) {
2163 data[i] = adapter->tx_stats_buffers[j].packets;
2165 data[i] = adapter->tx_stats_buffers[j].bytes;
2167 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2171 for (j = 0; j < adapter->req_rx_queues; j++) {
2172 data[i] = adapter->rx_stats_buffers[j].packets;
2174 data[i] = adapter->rx_stats_buffers[j].bytes;
2176 data[i] = adapter->rx_stats_buffers[j].interrupts;
2181 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2182 .get_drvinfo = ibmvnic_get_drvinfo,
2183 .get_msglevel = ibmvnic_get_msglevel,
2184 .set_msglevel = ibmvnic_set_msglevel,
2185 .get_link = ibmvnic_get_link,
2186 .get_ringparam = ibmvnic_get_ringparam,
2187 .set_ringparam = ibmvnic_set_ringparam,
2188 .get_channels = ibmvnic_get_channels,
2189 .set_channels = ibmvnic_set_channels,
2190 .get_strings = ibmvnic_get_strings,
2191 .get_sset_count = ibmvnic_get_sset_count,
2192 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2193 .get_link_ksettings = ibmvnic_get_link_ksettings,
2196 /* Routines for managing CRQs/sCRQs */
2198 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2199 struct ibmvnic_sub_crq_queue *scrq)
2204 free_irq(scrq->irq, scrq);
2205 irq_dispose_mapping(scrq->irq);
2209 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2212 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2213 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2217 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2221 for (i = 0; i < adapter->req_tx_queues; i++) {
2222 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2223 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2228 for (i = 0; i < adapter->req_rx_queues; i++) {
2229 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2230 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2238 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2239 struct ibmvnic_sub_crq_queue *scrq)
2241 struct device *dev = &adapter->vdev->dev;
2244 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2246 /* Close the sub-crqs */
2248 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2249 adapter->vdev->unit_address,
2251 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2254 netdev_err(adapter->netdev,
2255 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2259 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2261 free_pages((unsigned long)scrq->msgs, 2);
2265 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2268 struct device *dev = &adapter->vdev->dev;
2269 struct ibmvnic_sub_crq_queue *scrq;
2272 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2277 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2279 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2280 goto zero_page_failed;
2283 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2285 if (dma_mapping_error(dev, scrq->msg_token)) {
2286 dev_warn(dev, "Couldn't map crq queue messages page\n");
2290 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2291 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2293 if (rc == H_RESOURCE)
2294 rc = ibmvnic_reset_crq(adapter);
2296 if (rc == H_CLOSED) {
2297 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2299 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2303 scrq->adapter = adapter;
2304 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2305 spin_lock_init(&scrq->lock);
2307 netdev_dbg(adapter->netdev,
2308 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2309 scrq->crq_num, scrq->hw_irq, scrq->irq);
2314 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2317 free_pages((unsigned long)scrq->msgs, 2);
2324 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2328 if (adapter->tx_scrq) {
2329 for (i = 0; i < adapter->req_tx_queues; i++) {
2330 if (!adapter->tx_scrq[i])
2333 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2335 if (adapter->tx_scrq[i]->irq) {
2336 free_irq(adapter->tx_scrq[i]->irq,
2337 adapter->tx_scrq[i]);
2338 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2339 adapter->tx_scrq[i]->irq = 0;
2342 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2345 kfree(adapter->tx_scrq);
2346 adapter->tx_scrq = NULL;
2349 if (adapter->rx_scrq) {
2350 for (i = 0; i < adapter->req_rx_queues; i++) {
2351 if (!adapter->rx_scrq[i])
2354 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2356 if (adapter->rx_scrq[i]->irq) {
2357 free_irq(adapter->rx_scrq[i]->irq,
2358 adapter->rx_scrq[i]);
2359 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2360 adapter->rx_scrq[i]->irq = 0;
2363 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2366 kfree(adapter->rx_scrq);
2367 adapter->rx_scrq = NULL;
2371 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2372 struct ibmvnic_sub_crq_queue *scrq)
2374 struct device *dev = &adapter->vdev->dev;
2377 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2378 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2380 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2385 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2386 struct ibmvnic_sub_crq_queue *scrq)
2388 struct device *dev = &adapter->vdev->dev;
2391 if (scrq->hw_irq > 0x100000000ULL) {
2392 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2396 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2397 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2399 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2404 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2405 struct ibmvnic_sub_crq_queue *scrq)
2407 struct device *dev = &adapter->vdev->dev;
2408 struct ibmvnic_tx_buff *txbuff;
2409 union sub_crq *next;
2415 while (pending_scrq(adapter, scrq)) {
2416 unsigned int pool = scrq->pool_index;
2418 next = ibmvnic_next_scrq(adapter, scrq);
2419 for (i = 0; i < next->tx_comp.num_comps; i++) {
2420 if (next->tx_comp.rcs[i]) {
2421 dev_err(dev, "tx error %x\n",
2422 next->tx_comp.rcs[i]);
2425 index = be32_to_cpu(next->tx_comp.correlators[i]);
2426 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2428 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2429 if (!txbuff->data_dma[j])
2432 txbuff->data_dma[j] = 0;
2434 /* if sub_crq was sent indirectly */
2435 first = txbuff->indir_arr[0].generic.first;
2436 if (first == IBMVNIC_CRQ_CMD) {
2437 dma_unmap_single(dev, txbuff->indir_dma,
2438 sizeof(txbuff->indir_arr),
2442 if (txbuff->last_frag) {
2443 dev_kfree_skb_any(txbuff->skb);
2447 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2448 producer_index] = index;
2449 adapter->tx_pool[pool].producer_index =
2450 (adapter->tx_pool[pool].producer_index + 1) %
2451 adapter->req_tx_entries_per_subcrq;
2453 /* remove tx_comp scrq*/
2454 next->tx_comp.first = 0;
2456 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2457 (adapter->req_tx_entries_per_subcrq / 2) &&
2458 __netif_subqueue_stopped(adapter->netdev,
2459 scrq->pool_index)) {
2460 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2461 netdev_info(adapter->netdev, "Started queue %d\n",
2466 enable_scrq_irq(adapter, scrq);
2468 if (pending_scrq(adapter, scrq)) {
2469 disable_scrq_irq(adapter, scrq);
2476 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2478 struct ibmvnic_sub_crq_queue *scrq = instance;
2479 struct ibmvnic_adapter *adapter = scrq->adapter;
2481 disable_scrq_irq(adapter, scrq);
2482 ibmvnic_complete_tx(adapter, scrq);
2487 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2489 struct ibmvnic_sub_crq_queue *scrq = instance;
2490 struct ibmvnic_adapter *adapter = scrq->adapter;
2492 /* When booting a kdump kernel we can hit pending interrupts
2493 * prior to completing driver initialization.
2495 if (unlikely(adapter->state != VNIC_OPEN))
2498 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2500 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2501 disable_scrq_irq(adapter, scrq);
2502 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2508 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2510 struct device *dev = &adapter->vdev->dev;
2511 struct ibmvnic_sub_crq_queue *scrq;
2515 for (i = 0; i < adapter->req_tx_queues; i++) {
2516 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2518 scrq = adapter->tx_scrq[i];
2519 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2523 dev_err(dev, "Error mapping irq\n");
2524 goto req_tx_irq_failed;
2527 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2528 0, "ibmvnic_tx", scrq);
2531 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2533 irq_dispose_mapping(scrq->irq);
2534 goto req_rx_irq_failed;
2538 for (i = 0; i < adapter->req_rx_queues; i++) {
2539 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2541 scrq = adapter->rx_scrq[i];
2542 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2545 dev_err(dev, "Error mapping irq\n");
2546 goto req_rx_irq_failed;
2548 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2549 0, "ibmvnic_rx", scrq);
2551 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2553 irq_dispose_mapping(scrq->irq);
2554 goto req_rx_irq_failed;
2560 for (j = 0; j < i; j++) {
2561 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2562 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2564 i = adapter->req_tx_queues;
2566 for (j = 0; j < i; j++) {
2567 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2568 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2570 release_sub_crqs(adapter);
2574 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2576 struct device *dev = &adapter->vdev->dev;
2577 struct ibmvnic_sub_crq_queue **allqueues;
2578 int registered_queues = 0;
2583 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2585 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2589 for (i = 0; i < total_queues; i++) {
2590 allqueues[i] = init_sub_crq_queue(adapter);
2591 if (!allqueues[i]) {
2592 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2595 registered_queues++;
2598 /* Make sure we were able to register the minimum number of queues */
2599 if (registered_queues <
2600 adapter->min_tx_queues + adapter->min_rx_queues) {
2601 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2605 /* Distribute the failed allocated queues*/
2606 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2607 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2610 if (adapter->req_rx_queues > adapter->min_rx_queues)
2611 adapter->req_rx_queues--;
2616 if (adapter->req_tx_queues > adapter->min_tx_queues)
2617 adapter->req_tx_queues--;
2624 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2625 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2626 if (!adapter->tx_scrq)
2629 for (i = 0; i < adapter->req_tx_queues; i++) {
2630 adapter->tx_scrq[i] = allqueues[i];
2631 adapter->tx_scrq[i]->pool_index = i;
2634 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2635 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2636 if (!adapter->rx_scrq)
2639 for (i = 0; i < adapter->req_rx_queues; i++) {
2640 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2641 adapter->rx_scrq[i]->scrq_num = i;
2648 kfree(adapter->tx_scrq);
2649 adapter->tx_scrq = NULL;
2651 for (i = 0; i < registered_queues; i++)
2652 release_sub_crq_queue(adapter, allqueues[i]);
2657 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2659 struct device *dev = &adapter->vdev->dev;
2660 union ibmvnic_crq crq;
2664 /* Sub-CRQ entries are 32 byte long */
2665 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2667 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2668 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2669 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2673 if (adapter->desired.mtu)
2674 adapter->req_mtu = adapter->desired.mtu;
2676 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2678 if (!adapter->desired.tx_entries)
2679 adapter->desired.tx_entries =
2680 adapter->max_tx_entries_per_subcrq;
2681 if (!adapter->desired.rx_entries)
2682 adapter->desired.rx_entries =
2683 adapter->max_rx_add_entries_per_subcrq;
2685 max_entries = IBMVNIC_MAX_LTB_SIZE /
2686 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2688 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2689 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2690 adapter->desired.tx_entries = max_entries;
2693 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2694 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2695 adapter->desired.rx_entries = max_entries;
2698 if (adapter->desired.tx_entries)
2699 adapter->req_tx_entries_per_subcrq =
2700 adapter->desired.tx_entries;
2702 adapter->req_tx_entries_per_subcrq =
2703 adapter->max_tx_entries_per_subcrq;
2705 if (adapter->desired.rx_entries)
2706 adapter->req_rx_add_entries_per_subcrq =
2707 adapter->desired.rx_entries;
2709 adapter->req_rx_add_entries_per_subcrq =
2710 adapter->max_rx_add_entries_per_subcrq;
2712 if (adapter->desired.tx_queues)
2713 adapter->req_tx_queues =
2714 adapter->desired.tx_queues;
2716 adapter->req_tx_queues =
2717 adapter->opt_tx_comp_sub_queues;
2719 if (adapter->desired.rx_queues)
2720 adapter->req_rx_queues =
2721 adapter->desired.rx_queues;
2723 adapter->req_rx_queues =
2724 adapter->opt_rx_comp_queues;
2726 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2729 memset(&crq, 0, sizeof(crq));
2730 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2731 crq.request_capability.cmd = REQUEST_CAPABILITY;
2733 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2734 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2735 atomic_inc(&adapter->running_cap_crqs);
2736 ibmvnic_send_crq(adapter, &crq);
2738 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2739 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2740 atomic_inc(&adapter->running_cap_crqs);
2741 ibmvnic_send_crq(adapter, &crq);
2743 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2744 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2745 atomic_inc(&adapter->running_cap_crqs);
2746 ibmvnic_send_crq(adapter, &crq);
2748 crq.request_capability.capability =
2749 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2750 crq.request_capability.number =
2751 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2752 atomic_inc(&adapter->running_cap_crqs);
2753 ibmvnic_send_crq(adapter, &crq);
2755 crq.request_capability.capability =
2756 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2757 crq.request_capability.number =
2758 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2759 atomic_inc(&adapter->running_cap_crqs);
2760 ibmvnic_send_crq(adapter, &crq);
2762 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2763 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2764 atomic_inc(&adapter->running_cap_crqs);
2765 ibmvnic_send_crq(adapter, &crq);
2767 if (adapter->netdev->flags & IFF_PROMISC) {
2768 if (adapter->promisc_supported) {
2769 crq.request_capability.capability =
2770 cpu_to_be16(PROMISC_REQUESTED);
2771 crq.request_capability.number = cpu_to_be64(1);
2772 atomic_inc(&adapter->running_cap_crqs);
2773 ibmvnic_send_crq(adapter, &crq);
2776 crq.request_capability.capability =
2777 cpu_to_be16(PROMISC_REQUESTED);
2778 crq.request_capability.number = cpu_to_be64(0);
2779 atomic_inc(&adapter->running_cap_crqs);
2780 ibmvnic_send_crq(adapter, &crq);
2784 static int pending_scrq(struct ibmvnic_adapter *adapter,
2785 struct ibmvnic_sub_crq_queue *scrq)
2787 union sub_crq *entry = &scrq->msgs[scrq->cur];
2789 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2795 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2796 struct ibmvnic_sub_crq_queue *scrq)
2798 union sub_crq *entry;
2799 unsigned long flags;
2801 spin_lock_irqsave(&scrq->lock, flags);
2802 entry = &scrq->msgs[scrq->cur];
2803 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2804 if (++scrq->cur == scrq->size)
2809 spin_unlock_irqrestore(&scrq->lock, flags);
2814 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2816 struct ibmvnic_crq_queue *queue = &adapter->crq;
2817 union ibmvnic_crq *crq;
2819 crq = &queue->msgs[queue->cur];
2820 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2821 if (++queue->cur == queue->size)
2830 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2831 union sub_crq *sub_crq)
2833 unsigned int ua = adapter->vdev->unit_address;
2834 struct device *dev = &adapter->vdev->dev;
2835 u64 *u64_crq = (u64 *)sub_crq;
2838 netdev_dbg(adapter->netdev,
2839 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2840 (unsigned long int)cpu_to_be64(remote_handle),
2841 (unsigned long int)cpu_to_be64(u64_crq[0]),
2842 (unsigned long int)cpu_to_be64(u64_crq[1]),
2843 (unsigned long int)cpu_to_be64(u64_crq[2]),
2844 (unsigned long int)cpu_to_be64(u64_crq[3]));
2846 /* Make sure the hypervisor sees the complete request */
2849 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2850 cpu_to_be64(remote_handle),
2851 cpu_to_be64(u64_crq[0]),
2852 cpu_to_be64(u64_crq[1]),
2853 cpu_to_be64(u64_crq[2]),
2854 cpu_to_be64(u64_crq[3]));
2858 dev_warn(dev, "CRQ Queue closed\n");
2859 dev_err(dev, "Send error (rc=%d)\n", rc);
2865 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2866 u64 remote_handle, u64 ioba, u64 num_entries)
2868 unsigned int ua = adapter->vdev->unit_address;
2869 struct device *dev = &adapter->vdev->dev;
2872 /* Make sure the hypervisor sees the complete request */
2874 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2875 cpu_to_be64(remote_handle),
2880 dev_warn(dev, "CRQ Queue closed\n");
2881 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2887 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2888 union ibmvnic_crq *crq)
2890 unsigned int ua = adapter->vdev->unit_address;
2891 struct device *dev = &adapter->vdev->dev;
2892 u64 *u64_crq = (u64 *)crq;
2895 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2896 (unsigned long int)cpu_to_be64(u64_crq[0]),
2897 (unsigned long int)cpu_to_be64(u64_crq[1]));
2899 /* Make sure the hypervisor sees the complete request */
2902 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2903 cpu_to_be64(u64_crq[0]),
2904 cpu_to_be64(u64_crq[1]));
2908 dev_warn(dev, "CRQ Queue closed\n");
2909 dev_warn(dev, "Send error (rc=%d)\n", rc);
2915 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2917 union ibmvnic_crq crq;
2919 memset(&crq, 0, sizeof(crq));
2920 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2921 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2922 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2924 return ibmvnic_send_crq(adapter, &crq);
2927 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2929 union ibmvnic_crq crq;
2931 memset(&crq, 0, sizeof(crq));
2932 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2933 crq.version_exchange.cmd = VERSION_EXCHANGE;
2934 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2936 return ibmvnic_send_crq(adapter, &crq);
2939 struct vnic_login_client_data {
2945 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2949 /* Calculate the amount of buffer space needed for the
2950 * vnic client data in the login buffer. There are four entries,
2951 * OS name, LPAR name, device name, and a null last entry.
2953 len = 4 * sizeof(struct vnic_login_client_data);
2954 len += 6; /* "Linux" plus NULL */
2955 len += strlen(utsname()->nodename) + 1;
2956 len += strlen(adapter->netdev->name) + 1;
2961 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2962 struct vnic_login_client_data *vlcd)
2964 const char *os_name = "Linux";
2967 /* Type 1 - LPAR OS */
2969 len = strlen(os_name) + 1;
2970 vlcd->len = cpu_to_be16(len);
2971 strncpy(&vlcd->name, os_name, len);
2972 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2974 /* Type 2 - LPAR name */
2976 len = strlen(utsname()->nodename) + 1;
2977 vlcd->len = cpu_to_be16(len);
2978 strncpy(&vlcd->name, utsname()->nodename, len);
2979 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2981 /* Type 3 - device name */
2983 len = strlen(adapter->netdev->name) + 1;
2984 vlcd->len = cpu_to_be16(len);
2985 strncpy(&vlcd->name, adapter->netdev->name, len);
2988 static void send_login(struct ibmvnic_adapter *adapter)
2990 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2991 struct ibmvnic_login_buffer *login_buffer;
2992 struct device *dev = &adapter->vdev->dev;
2993 dma_addr_t rsp_buffer_token;
2994 dma_addr_t buffer_token;
2995 size_t rsp_buffer_size;
2996 union ibmvnic_crq crq;
3000 int client_data_len;
3001 struct vnic_login_client_data *vlcd;
3004 client_data_len = vnic_client_data_len(adapter);
3007 sizeof(struct ibmvnic_login_buffer) +
3008 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3011 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3013 goto buf_alloc_failed;
3015 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3017 if (dma_mapping_error(dev, buffer_token)) {
3018 dev_err(dev, "Couldn't map login buffer\n");
3019 goto buf_map_failed;
3022 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3023 sizeof(u64) * adapter->req_tx_queues +
3024 sizeof(u64) * adapter->req_rx_queues +
3025 sizeof(u64) * adapter->req_rx_queues +
3026 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3028 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3029 if (!login_rsp_buffer)
3030 goto buf_rsp_alloc_failed;
3032 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3033 rsp_buffer_size, DMA_FROM_DEVICE);
3034 if (dma_mapping_error(dev, rsp_buffer_token)) {
3035 dev_err(dev, "Couldn't map login rsp buffer\n");
3036 goto buf_rsp_map_failed;
3039 adapter->login_buf = login_buffer;
3040 adapter->login_buf_token = buffer_token;
3041 adapter->login_buf_sz = buffer_size;
3042 adapter->login_rsp_buf = login_rsp_buffer;
3043 adapter->login_rsp_buf_token = rsp_buffer_token;
3044 adapter->login_rsp_buf_sz = rsp_buffer_size;
3046 login_buffer->len = cpu_to_be32(buffer_size);
3047 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3048 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3049 login_buffer->off_txcomp_subcrqs =
3050 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3051 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3052 login_buffer->off_rxcomp_subcrqs =
3053 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3054 sizeof(u64) * adapter->req_tx_queues);
3055 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3056 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3058 tx_list_p = (__be64 *)((char *)login_buffer +
3059 sizeof(struct ibmvnic_login_buffer));
3060 rx_list_p = (__be64 *)((char *)login_buffer +
3061 sizeof(struct ibmvnic_login_buffer) +
3062 sizeof(u64) * adapter->req_tx_queues);
3064 for (i = 0; i < adapter->req_tx_queues; i++) {
3065 if (adapter->tx_scrq[i]) {
3066 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3071 for (i = 0; i < adapter->req_rx_queues; i++) {
3072 if (adapter->rx_scrq[i]) {
3073 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3078 /* Insert vNIC login client data */
3079 vlcd = (struct vnic_login_client_data *)
3080 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3081 login_buffer->client_data_offset =
3082 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3083 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3085 vnic_add_client_data(adapter, vlcd);
3087 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3088 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3089 netdev_dbg(adapter->netdev, "%016lx\n",
3090 ((unsigned long int *)(adapter->login_buf))[i]);
3093 memset(&crq, 0, sizeof(crq));
3094 crq.login.first = IBMVNIC_CRQ_CMD;
3095 crq.login.cmd = LOGIN;
3096 crq.login.ioba = cpu_to_be32(buffer_token);
3097 crq.login.len = cpu_to_be32(buffer_size);
3098 ibmvnic_send_crq(adapter, &crq);
3103 kfree(login_rsp_buffer);
3104 buf_rsp_alloc_failed:
3105 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3107 kfree(login_buffer);
3112 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3115 union ibmvnic_crq crq;
3117 memset(&crq, 0, sizeof(crq));
3118 crq.request_map.first = IBMVNIC_CRQ_CMD;
3119 crq.request_map.cmd = REQUEST_MAP;
3120 crq.request_map.map_id = map_id;
3121 crq.request_map.ioba = cpu_to_be32(addr);
3122 crq.request_map.len = cpu_to_be32(len);
3123 ibmvnic_send_crq(adapter, &crq);
3126 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3128 union ibmvnic_crq crq;
3130 memset(&crq, 0, sizeof(crq));
3131 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3132 crq.request_unmap.cmd = REQUEST_UNMAP;
3133 crq.request_unmap.map_id = map_id;
3134 ibmvnic_send_crq(adapter, &crq);
3137 static void send_map_query(struct ibmvnic_adapter *adapter)
3139 union ibmvnic_crq crq;
3141 memset(&crq, 0, sizeof(crq));
3142 crq.query_map.first = IBMVNIC_CRQ_CMD;
3143 crq.query_map.cmd = QUERY_MAP;
3144 ibmvnic_send_crq(adapter, &crq);
3147 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3148 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3150 union ibmvnic_crq crq;
3152 atomic_set(&adapter->running_cap_crqs, 0);
3153 memset(&crq, 0, sizeof(crq));
3154 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3155 crq.query_capability.cmd = QUERY_CAPABILITY;
3157 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3158 atomic_inc(&adapter->running_cap_crqs);
3159 ibmvnic_send_crq(adapter, &crq);
3161 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3162 atomic_inc(&adapter->running_cap_crqs);
3163 ibmvnic_send_crq(adapter, &crq);
3165 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3166 atomic_inc(&adapter->running_cap_crqs);
3167 ibmvnic_send_crq(adapter, &crq);
3169 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3170 atomic_inc(&adapter->running_cap_crqs);
3171 ibmvnic_send_crq(adapter, &crq);
3173 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3174 atomic_inc(&adapter->running_cap_crqs);
3175 ibmvnic_send_crq(adapter, &crq);
3177 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3178 atomic_inc(&adapter->running_cap_crqs);
3179 ibmvnic_send_crq(adapter, &crq);
3181 crq.query_capability.capability =
3182 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3183 atomic_inc(&adapter->running_cap_crqs);
3184 ibmvnic_send_crq(adapter, &crq);
3186 crq.query_capability.capability =
3187 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3188 atomic_inc(&adapter->running_cap_crqs);
3189 ibmvnic_send_crq(adapter, &crq);
3191 crq.query_capability.capability =
3192 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3193 atomic_inc(&adapter->running_cap_crqs);
3194 ibmvnic_send_crq(adapter, &crq);
3196 crq.query_capability.capability =
3197 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3198 atomic_inc(&adapter->running_cap_crqs);
3199 ibmvnic_send_crq(adapter, &crq);
3201 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3202 atomic_inc(&adapter->running_cap_crqs);
3203 ibmvnic_send_crq(adapter, &crq);
3205 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3206 atomic_inc(&adapter->running_cap_crqs);
3207 ibmvnic_send_crq(adapter, &crq);
3209 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3210 atomic_inc(&adapter->running_cap_crqs);
3211 ibmvnic_send_crq(adapter, &crq);
3213 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3214 atomic_inc(&adapter->running_cap_crqs);
3215 ibmvnic_send_crq(adapter, &crq);
3217 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3218 atomic_inc(&adapter->running_cap_crqs);
3219 ibmvnic_send_crq(adapter, &crq);
3221 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3222 atomic_inc(&adapter->running_cap_crqs);
3223 ibmvnic_send_crq(adapter, &crq);
3225 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3226 atomic_inc(&adapter->running_cap_crqs);
3227 ibmvnic_send_crq(adapter, &crq);
3229 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3230 atomic_inc(&adapter->running_cap_crqs);
3231 ibmvnic_send_crq(adapter, &crq);
3233 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3234 atomic_inc(&adapter->running_cap_crqs);
3235 ibmvnic_send_crq(adapter, &crq);
3237 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3238 atomic_inc(&adapter->running_cap_crqs);
3239 ibmvnic_send_crq(adapter, &crq);
3241 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3242 atomic_inc(&adapter->running_cap_crqs);
3243 ibmvnic_send_crq(adapter, &crq);
3245 crq.query_capability.capability =
3246 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3247 atomic_inc(&adapter->running_cap_crqs);
3248 ibmvnic_send_crq(adapter, &crq);
3250 crq.query_capability.capability =
3251 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3252 atomic_inc(&adapter->running_cap_crqs);
3253 ibmvnic_send_crq(adapter, &crq);
3255 crq.query_capability.capability =
3256 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3257 atomic_inc(&adapter->running_cap_crqs);
3258 ibmvnic_send_crq(adapter, &crq);
3260 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3261 atomic_inc(&adapter->running_cap_crqs);
3262 ibmvnic_send_crq(adapter, &crq);
3265 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3266 struct ibmvnic_adapter *adapter)
3268 struct device *dev = &adapter->vdev->dev;
3270 if (crq->get_vpd_size_rsp.rc.code) {
3271 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3272 crq->get_vpd_size_rsp.rc.code);
3273 complete(&adapter->fw_done);
3277 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3278 complete(&adapter->fw_done);
3281 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3282 struct ibmvnic_adapter *adapter)
3284 struct device *dev = &adapter->vdev->dev;
3285 unsigned char *substr = NULL, *ptr = NULL;
3286 u8 fw_level_len = 0;
3288 memset(adapter->fw_version, 0, 32);
3290 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3293 if (crq->get_vpd_rsp.rc.code) {
3294 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3295 crq->get_vpd_rsp.rc.code);
3299 /* get the position of the firmware version info
3300 * located after the ASCII 'RM' substring in the buffer
3302 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3304 dev_info(dev, "No FW level provided by VPD\n");
3308 /* get length of firmware level ASCII substring */
3309 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3310 fw_level_len = *(substr + 2);
3312 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3316 /* copy firmware version string from vpd into adapter */
3317 if ((substr + 3 + fw_level_len) <
3318 (adapter->vpd->buff + adapter->vpd->len)) {
3319 ptr = strncpy((char *)adapter->fw_version,
3320 substr + 3, fw_level_len);
3323 dev_err(dev, "Failed to isolate FW level string\n");
3325 dev_info(dev, "FW substr extrapolated VPD buff\n");
3329 complete(&adapter->fw_done);
3332 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3334 struct device *dev = &adapter->vdev->dev;
3335 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3336 union ibmvnic_crq crq;
3339 dma_unmap_single(dev, adapter->ip_offload_tok,
3340 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3342 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3343 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3344 netdev_dbg(adapter->netdev, "%016lx\n",
3345 ((unsigned long int *)(buf))[i]);
3347 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3348 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3349 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3350 buf->tcp_ipv4_chksum);
3351 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3352 buf->tcp_ipv6_chksum);
3353 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3354 buf->udp_ipv4_chksum);
3355 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3356 buf->udp_ipv6_chksum);
3357 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3358 buf->large_tx_ipv4);
3359 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3360 buf->large_tx_ipv6);
3361 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3362 buf->large_rx_ipv4);
3363 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3364 buf->large_rx_ipv6);
3365 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3366 buf->max_ipv4_header_size);
3367 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3368 buf->max_ipv6_header_size);
3369 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3370 buf->max_tcp_header_size);
3371 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3372 buf->max_udp_header_size);
3373 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3374 buf->max_large_tx_size);
3375 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3376 buf->max_large_rx_size);
3377 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3378 buf->ipv6_extension_header);
3379 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3380 buf->tcp_pseudosum_req);
3381 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3382 buf->num_ipv6_ext_headers);
3383 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3384 buf->off_ipv6_ext_headers);
3386 adapter->ip_offload_ctrl_tok =
3387 dma_map_single(dev, &adapter->ip_offload_ctrl,
3388 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3390 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3391 dev_err(dev, "Couldn't map ip offload control buffer\n");
3395 adapter->ip_offload_ctrl.len =
3396 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3397 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3398 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3399 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3400 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3401 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3402 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3403 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3404 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3405 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3407 /* large_rx disabled for now, additional features needed */
3408 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3409 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3411 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3413 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3414 adapter->netdev->features |= NETIF_F_IP_CSUM;
3416 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3417 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3419 if ((adapter->netdev->features &
3420 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3421 adapter->netdev->features |= NETIF_F_RXCSUM;
3423 if (buf->large_tx_ipv4)
3424 adapter->netdev->features |= NETIF_F_TSO;
3425 if (buf->large_tx_ipv6)
3426 adapter->netdev->features |= NETIF_F_TSO6;
3428 adapter->netdev->hw_features |= adapter->netdev->features;
3430 memset(&crq, 0, sizeof(crq));
3431 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3432 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3433 crq.control_ip_offload.len =
3434 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3435 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3436 ibmvnic_send_crq(adapter, &crq);
3439 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3440 struct ibmvnic_adapter *adapter)
3442 struct device *dev = &adapter->vdev->dev;
3443 struct ibmvnic_error_buff *error_buff, *tmp;
3444 unsigned long flags;
3448 if (!crq->request_error_rsp.rc.code) {
3449 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3450 crq->request_error_rsp.rc.code);
3454 spin_lock_irqsave(&adapter->error_list_lock, flags);
3455 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3456 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3458 list_del(&error_buff->list);
3461 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3464 dev_err(dev, "Couldn't find error id %x\n",
3465 be32_to_cpu(crq->request_error_rsp.error_id));
3469 dev_err(dev, "Detailed info for error id %x:",
3470 be32_to_cpu(crq->request_error_rsp.error_id));
3472 for (i = 0; i < error_buff->len; i++) {
3473 pr_cont("%02x", (int)error_buff->buff[i]);
3479 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3481 kfree(error_buff->buff);
3485 static void request_error_information(struct ibmvnic_adapter *adapter,
3486 union ibmvnic_crq *err_crq)
3488 struct device *dev = &adapter->vdev->dev;
3489 struct net_device *netdev = adapter->netdev;
3490 struct ibmvnic_error_buff *error_buff;
3491 unsigned long timeout = msecs_to_jiffies(30000);
3492 union ibmvnic_crq crq;
3493 unsigned long flags;
3496 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3500 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3501 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3502 if (!error_buff->buff) {
3507 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3509 if (dma_mapping_error(dev, error_buff->dma)) {
3510 netdev_err(netdev, "Couldn't map error buffer\n");
3511 kfree(error_buff->buff);
3516 error_buff->len = detail_len;
3517 error_buff->error_id = err_crq->error_indication.error_id;
3519 spin_lock_irqsave(&adapter->error_list_lock, flags);
3520 list_add_tail(&error_buff->list, &adapter->errors);
3521 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3523 memset(&crq, 0, sizeof(crq));
3524 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3525 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3526 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3527 crq.request_error_info.len = cpu_to_be32(detail_len);
3528 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3530 rc = ibmvnic_send_crq(adapter, &crq);
3532 netdev_err(netdev, "failed to request error information\n");
3536 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3537 netdev_err(netdev, "timeout waiting for error information\n");
3544 spin_lock_irqsave(&adapter->error_list_lock, flags);
3545 list_del(&error_buff->list);
3546 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3548 kfree(error_buff->buff);
3552 static void handle_error_indication(union ibmvnic_crq *crq,
3553 struct ibmvnic_adapter *adapter)
3555 struct device *dev = &adapter->vdev->dev;
3557 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3558 crq->error_indication.flags
3559 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3560 be32_to_cpu(crq->error_indication.error_id),
3561 be16_to_cpu(crq->error_indication.error_cause));
3563 if (be32_to_cpu(crq->error_indication.error_id))
3564 request_error_information(adapter, crq);
3566 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3567 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3569 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3572 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3573 struct ibmvnic_adapter *adapter)
3575 struct net_device *netdev = adapter->netdev;
3576 struct device *dev = &adapter->vdev->dev;
3579 rc = crq->change_mac_addr_rsp.rc.code;
3581 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3584 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3588 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3589 struct ibmvnic_adapter *adapter)
3591 struct device *dev = &adapter->vdev->dev;
3595 atomic_dec(&adapter->running_cap_crqs);
3596 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3598 req_value = &adapter->req_tx_queues;
3602 req_value = &adapter->req_rx_queues;
3605 case REQ_RX_ADD_QUEUES:
3606 req_value = &adapter->req_rx_add_queues;
3609 case REQ_TX_ENTRIES_PER_SUBCRQ:
3610 req_value = &adapter->req_tx_entries_per_subcrq;
3611 name = "tx_entries_per_subcrq";
3613 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3614 req_value = &adapter->req_rx_add_entries_per_subcrq;
3615 name = "rx_add_entries_per_subcrq";
3618 req_value = &adapter->req_mtu;
3621 case PROMISC_REQUESTED:
3622 req_value = &adapter->promisc;
3626 dev_err(dev, "Got invalid cap request rsp %d\n",
3627 crq->request_capability.capability);
3631 switch (crq->request_capability_rsp.rc.code) {
3634 case PARTIALSUCCESS:
3635 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3637 (long int)be64_to_cpu(crq->request_capability_rsp.
3640 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3642 pr_err("mtu of %llu is not supported. Reverting.\n",
3644 *req_value = adapter->fallback.mtu;
3647 be64_to_cpu(crq->request_capability_rsp.number);
3650 ibmvnic_send_req_caps(adapter, 1);
3653 dev_err(dev, "Error %d in request cap rsp\n",
3654 crq->request_capability_rsp.rc.code);
3658 /* Done receiving requested capabilities, query IP offload support */
3659 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3660 union ibmvnic_crq newcrq;
3661 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3662 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3663 &adapter->ip_offload_buf;
3665 adapter->wait_capability = false;
3666 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3670 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3671 if (!firmware_has_feature(FW_FEATURE_CMO))
3672 dev_err(dev, "Couldn't map offload buffer\n");
3676 memset(&newcrq, 0, sizeof(newcrq));
3677 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3678 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3679 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3680 newcrq.query_ip_offload.ioba =
3681 cpu_to_be32(adapter->ip_offload_tok);
3683 ibmvnic_send_crq(adapter, &newcrq);
3687 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3688 struct ibmvnic_adapter *adapter)
3690 struct device *dev = &adapter->vdev->dev;
3691 struct net_device *netdev = adapter->netdev;
3692 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3693 struct ibmvnic_login_buffer *login = adapter->login_buf;
3696 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3698 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3699 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3701 /* If the number of queues requested can't be allocated by the
3702 * server, the login response will return with code 1. We will need
3703 * to resend the login buffer with fewer queues requested.
3705 if (login_rsp_crq->generic.rc.code) {
3706 adapter->renegotiate = true;
3707 complete(&adapter->init_done);
3711 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3713 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3714 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3715 netdev_dbg(adapter->netdev, "%016lx\n",
3716 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3720 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3721 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3722 adapter->req_rx_add_queues !=
3723 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3724 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3725 ibmvnic_remove(adapter->vdev);
3728 complete(&adapter->init_done);
3733 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3734 struct ibmvnic_adapter *adapter)
3736 struct device *dev = &adapter->vdev->dev;
3739 rc = crq->request_unmap_rsp.rc.code;
3741 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3744 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3745 struct ibmvnic_adapter *adapter)
3747 struct net_device *netdev = adapter->netdev;
3748 struct device *dev = &adapter->vdev->dev;
3751 rc = crq->query_map_rsp.rc.code;
3753 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3756 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3757 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3758 crq->query_map_rsp.free_pages);
3761 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3762 struct ibmvnic_adapter *adapter)
3764 struct net_device *netdev = adapter->netdev;
3765 struct device *dev = &adapter->vdev->dev;
3768 atomic_dec(&adapter->running_cap_crqs);
3769 netdev_dbg(netdev, "Outstanding queries: %d\n",
3770 atomic_read(&adapter->running_cap_crqs));
3771 rc = crq->query_capability.rc.code;
3773 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3777 switch (be16_to_cpu(crq->query_capability.capability)) {
3779 adapter->min_tx_queues =
3780 be64_to_cpu(crq->query_capability.number);
3781 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3782 adapter->min_tx_queues);
3785 adapter->min_rx_queues =
3786 be64_to_cpu(crq->query_capability.number);
3787 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3788 adapter->min_rx_queues);
3790 case MIN_RX_ADD_QUEUES:
3791 adapter->min_rx_add_queues =
3792 be64_to_cpu(crq->query_capability.number);
3793 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3794 adapter->min_rx_add_queues);
3797 adapter->max_tx_queues =
3798 be64_to_cpu(crq->query_capability.number);
3799 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3800 adapter->max_tx_queues);
3803 adapter->max_rx_queues =
3804 be64_to_cpu(crq->query_capability.number);
3805 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3806 adapter->max_rx_queues);
3808 case MAX_RX_ADD_QUEUES:
3809 adapter->max_rx_add_queues =
3810 be64_to_cpu(crq->query_capability.number);
3811 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3812 adapter->max_rx_add_queues);
3814 case MIN_TX_ENTRIES_PER_SUBCRQ:
3815 adapter->min_tx_entries_per_subcrq =
3816 be64_to_cpu(crq->query_capability.number);
3817 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3818 adapter->min_tx_entries_per_subcrq);
3820 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3821 adapter->min_rx_add_entries_per_subcrq =
3822 be64_to_cpu(crq->query_capability.number);
3823 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3824 adapter->min_rx_add_entries_per_subcrq);
3826 case MAX_TX_ENTRIES_PER_SUBCRQ:
3827 adapter->max_tx_entries_per_subcrq =
3828 be64_to_cpu(crq->query_capability.number);
3829 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3830 adapter->max_tx_entries_per_subcrq);
3832 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3833 adapter->max_rx_add_entries_per_subcrq =
3834 be64_to_cpu(crq->query_capability.number);
3835 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3836 adapter->max_rx_add_entries_per_subcrq);
3838 case TCP_IP_OFFLOAD:
3839 adapter->tcp_ip_offload =
3840 be64_to_cpu(crq->query_capability.number);
3841 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3842 adapter->tcp_ip_offload);
3844 case PROMISC_SUPPORTED:
3845 adapter->promisc_supported =
3846 be64_to_cpu(crq->query_capability.number);
3847 netdev_dbg(netdev, "promisc_supported = %lld\n",
3848 adapter->promisc_supported);
3851 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3852 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3853 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3856 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3857 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3858 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3860 case MAX_MULTICAST_FILTERS:
3861 adapter->max_multicast_filters =
3862 be64_to_cpu(crq->query_capability.number);
3863 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3864 adapter->max_multicast_filters);
3866 case VLAN_HEADER_INSERTION:
3867 adapter->vlan_header_insertion =
3868 be64_to_cpu(crq->query_capability.number);
3869 if (adapter->vlan_header_insertion)
3870 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3871 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3872 adapter->vlan_header_insertion);
3874 case RX_VLAN_HEADER_INSERTION:
3875 adapter->rx_vlan_header_insertion =
3876 be64_to_cpu(crq->query_capability.number);
3877 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3878 adapter->rx_vlan_header_insertion);
3880 case MAX_TX_SG_ENTRIES:
3881 adapter->max_tx_sg_entries =
3882 be64_to_cpu(crq->query_capability.number);
3883 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3884 adapter->max_tx_sg_entries);
3886 case RX_SG_SUPPORTED:
3887 adapter->rx_sg_supported =
3888 be64_to_cpu(crq->query_capability.number);
3889 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3890 adapter->rx_sg_supported);
3892 case OPT_TX_COMP_SUB_QUEUES:
3893 adapter->opt_tx_comp_sub_queues =
3894 be64_to_cpu(crq->query_capability.number);
3895 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3896 adapter->opt_tx_comp_sub_queues);
3898 case OPT_RX_COMP_QUEUES:
3899 adapter->opt_rx_comp_queues =
3900 be64_to_cpu(crq->query_capability.number);
3901 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3902 adapter->opt_rx_comp_queues);
3904 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3905 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3906 be64_to_cpu(crq->query_capability.number);
3907 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3908 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3910 case OPT_TX_ENTRIES_PER_SUBCRQ:
3911 adapter->opt_tx_entries_per_subcrq =
3912 be64_to_cpu(crq->query_capability.number);
3913 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3914 adapter->opt_tx_entries_per_subcrq);
3916 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3917 adapter->opt_rxba_entries_per_subcrq =
3918 be64_to_cpu(crq->query_capability.number);
3919 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3920 adapter->opt_rxba_entries_per_subcrq);
3922 case TX_RX_DESC_REQ:
3923 adapter->tx_rx_desc_req = crq->query_capability.number;
3924 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3925 adapter->tx_rx_desc_req);
3929 netdev_err(netdev, "Got invalid cap rsp %d\n",
3930 crq->query_capability.capability);
3934 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3935 adapter->wait_capability = false;
3936 ibmvnic_send_req_caps(adapter, 0);
3940 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3941 struct ibmvnic_adapter *adapter)
3943 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3944 struct net_device *netdev = adapter->netdev;
3945 struct device *dev = &adapter->vdev->dev;
3946 u64 *u64_crq = (u64 *)crq;
3949 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3950 (unsigned long int)cpu_to_be64(u64_crq[0]),
3951 (unsigned long int)cpu_to_be64(u64_crq[1]));
3952 switch (gen_crq->first) {
3953 case IBMVNIC_CRQ_INIT_RSP:
3954 switch (gen_crq->cmd) {
3955 case IBMVNIC_CRQ_INIT:
3956 dev_info(dev, "Partner initialized\n");
3957 adapter->from_passive_init = true;
3958 complete(&adapter->init_done);
3960 case IBMVNIC_CRQ_INIT_COMPLETE:
3961 dev_info(dev, "Partner initialization complete\n");
3962 send_version_xchg(adapter);
3965 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3968 case IBMVNIC_CRQ_XPORT_EVENT:
3969 netif_carrier_off(netdev);
3970 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3971 dev_info(dev, "Migrated, re-enabling adapter\n");
3972 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
3973 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3974 dev_info(dev, "Backing device failover detected\n");
3975 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
3977 /* The adapter lost the connection */
3978 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3980 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3983 case IBMVNIC_CRQ_CMD_RSP:
3986 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3991 switch (gen_crq->cmd) {
3992 case VERSION_EXCHANGE_RSP:
3993 rc = crq->version_exchange_rsp.rc.code;
3995 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3998 dev_info(dev, "Partner protocol version is %d\n",
3999 crq->version_exchange_rsp.version);
4000 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4003 be16_to_cpu(crq->version_exchange_rsp.version);
4004 send_cap_queries(adapter);
4006 case QUERY_CAPABILITY_RSP:
4007 handle_query_cap_rsp(crq, adapter);
4010 handle_query_map_rsp(crq, adapter);
4012 case REQUEST_MAP_RSP:
4013 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4014 complete(&adapter->fw_done);
4016 case REQUEST_UNMAP_RSP:
4017 handle_request_unmap_rsp(crq, adapter);
4019 case REQUEST_CAPABILITY_RSP:
4020 handle_request_cap_rsp(crq, adapter);
4023 netdev_dbg(netdev, "Got Login Response\n");
4024 handle_login_rsp(crq, adapter);
4026 case LOGICAL_LINK_STATE_RSP:
4028 "Got Logical Link State Response, state: %d rc: %d\n",
4029 crq->logical_link_state_rsp.link_state,
4030 crq->logical_link_state_rsp.rc.code);
4031 adapter->logical_link_state =
4032 crq->logical_link_state_rsp.link_state;
4033 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4034 complete(&adapter->init_done);
4036 case LINK_STATE_INDICATION:
4037 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4038 adapter->phys_link_state =
4039 crq->link_state_indication.phys_link_state;
4040 adapter->logical_link_state =
4041 crq->link_state_indication.logical_link_state;
4043 case CHANGE_MAC_ADDR_RSP:
4044 netdev_dbg(netdev, "Got MAC address change Response\n");
4045 handle_change_mac_rsp(crq, adapter);
4047 case ERROR_INDICATION:
4048 netdev_dbg(netdev, "Got Error Indication\n");
4049 handle_error_indication(crq, adapter);
4051 case REQUEST_ERROR_RSP:
4052 netdev_dbg(netdev, "Got Error Detail Response\n");
4053 handle_error_info_rsp(crq, adapter);
4055 case REQUEST_STATISTICS_RSP:
4056 netdev_dbg(netdev, "Got Statistics Response\n");
4057 complete(&adapter->stats_done);
4059 case QUERY_IP_OFFLOAD_RSP:
4060 netdev_dbg(netdev, "Got Query IP offload Response\n");
4061 handle_query_ip_offload_rsp(adapter);
4063 case MULTICAST_CTRL_RSP:
4064 netdev_dbg(netdev, "Got multicast control Response\n");
4066 case CONTROL_IP_OFFLOAD_RSP:
4067 netdev_dbg(netdev, "Got Control IP offload Response\n");
4068 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4069 sizeof(adapter->ip_offload_ctrl),
4071 complete(&adapter->init_done);
4073 case COLLECT_FW_TRACE_RSP:
4074 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4075 complete(&adapter->fw_done);
4077 case GET_VPD_SIZE_RSP:
4078 handle_vpd_size_rsp(crq, adapter);
4081 handle_vpd_rsp(crq, adapter);
4084 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4089 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4091 struct ibmvnic_adapter *adapter = instance;
4093 tasklet_schedule(&adapter->tasklet);
4097 static void ibmvnic_tasklet(void *data)
4099 struct ibmvnic_adapter *adapter = data;
4100 struct ibmvnic_crq_queue *queue = &adapter->crq;
4101 union ibmvnic_crq *crq;
4102 unsigned long flags;
4105 spin_lock_irqsave(&queue->lock, flags);
4107 /* Pull all the valid messages off the CRQ */
4108 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4109 ibmvnic_handle_crq(crq, adapter);
4110 crq->generic.first = 0;
4113 /* remain in tasklet until all
4114 * capabilities responses are received
4116 if (!adapter->wait_capability)
4119 /* if capabilities CRQ's were sent in this tasklet, the following
4120 * tasklet must wait until all responses are received
4122 if (atomic_read(&adapter->running_cap_crqs) != 0)
4123 adapter->wait_capability = true;
4124 spin_unlock_irqrestore(&queue->lock, flags);
4127 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4129 struct vio_dev *vdev = adapter->vdev;
4133 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4134 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4137 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4142 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4144 struct ibmvnic_crq_queue *crq = &adapter->crq;
4145 struct device *dev = &adapter->vdev->dev;
4146 struct vio_dev *vdev = adapter->vdev;
4151 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4152 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4154 /* Clean out the queue */
4155 memset(crq->msgs, 0, PAGE_SIZE);
4158 /* And re-open it again */
4159 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4160 crq->msg_token, PAGE_SIZE);
4163 /* Adapter is good, but other end is not ready */
4164 dev_warn(dev, "Partner adapter not ready\n");
4166 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4171 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4173 struct ibmvnic_crq_queue *crq = &adapter->crq;
4174 struct vio_dev *vdev = adapter->vdev;
4180 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4181 free_irq(vdev->irq, adapter);
4182 tasklet_kill(&adapter->tasklet);
4184 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4185 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4187 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4189 free_page((unsigned long)crq->msgs);
4193 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4195 struct ibmvnic_crq_queue *crq = &adapter->crq;
4196 struct device *dev = &adapter->vdev->dev;
4197 struct vio_dev *vdev = adapter->vdev;
4198 int rc, retrc = -ENOMEM;
4203 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4204 /* Should we allocate more than one page? */
4209 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4210 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4212 if (dma_mapping_error(dev, crq->msg_token))
4215 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4216 crq->msg_token, PAGE_SIZE);
4218 if (rc == H_RESOURCE)
4219 /* maybe kexecing and resource is busy. try a reset */
4220 rc = ibmvnic_reset_crq(adapter);
4223 if (rc == H_CLOSED) {
4224 dev_warn(dev, "Partner adapter not ready\n");
4226 dev_warn(dev, "Error %d opening adapter\n", rc);
4227 goto reg_crq_failed;
4232 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4233 (unsigned long)adapter);
4235 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4236 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4239 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4241 goto req_irq_failed;
4244 rc = vio_enable_interrupts(vdev);
4246 dev_err(dev, "Error %d enabling interrupts\n", rc);
4247 goto req_irq_failed;
4251 spin_lock_init(&crq->lock);
4256 tasklet_kill(&adapter->tasklet);
4258 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4259 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4261 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4263 free_page((unsigned long)crq->msgs);
4268 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4270 struct device *dev = &adapter->vdev->dev;
4271 unsigned long timeout = msecs_to_jiffies(30000);
4274 if (adapter->resetting && !adapter->wait_for_reset) {
4275 rc = ibmvnic_reset_crq(adapter);
4277 rc = vio_enable_interrupts(adapter->vdev);
4279 rc = init_crq_queue(adapter);
4283 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4287 adapter->from_passive_init = false;
4289 init_completion(&adapter->init_done);
4290 adapter->init_done_rc = 0;
4291 ibmvnic_send_crq_init(adapter);
4292 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4293 dev_err(dev, "Initialization sequence timed out\n");
4297 if (adapter->init_done_rc) {
4298 release_crq_queue(adapter);
4299 return adapter->init_done_rc;
4302 if (adapter->from_passive_init) {
4303 adapter->state = VNIC_OPEN;
4304 adapter->from_passive_init = false;
4308 if (adapter->resetting && !adapter->wait_for_reset)
4309 rc = reset_sub_crq_queues(adapter);
4311 rc = init_sub_crqs(adapter);
4313 dev_err(dev, "Initialization of sub crqs failed\n");
4314 release_crq_queue(adapter);
4318 rc = init_sub_crq_irqs(adapter);
4320 dev_err(dev, "Failed to initialize sub crq irqs\n");
4321 release_crq_queue(adapter);
4327 static struct device_attribute dev_attr_failover;
4329 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4331 struct ibmvnic_adapter *adapter;
4332 struct net_device *netdev;
4333 unsigned char *mac_addr_p;
4336 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4339 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4340 VETH_MAC_ADDR, NULL);
4343 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4344 __FILE__, __LINE__);
4348 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4349 IBMVNIC_MAX_QUEUES);
4353 adapter = netdev_priv(netdev);
4354 adapter->state = VNIC_PROBING;
4355 dev_set_drvdata(&dev->dev, netdev);
4356 adapter->vdev = dev;
4357 adapter->netdev = netdev;
4359 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4360 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4361 netdev->irq = dev->irq;
4362 netdev->netdev_ops = &ibmvnic_netdev_ops;
4363 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4364 SET_NETDEV_DEV(netdev, &dev->dev);
4366 spin_lock_init(&adapter->stats_lock);
4368 INIT_LIST_HEAD(&adapter->errors);
4369 spin_lock_init(&adapter->error_list_lock);
4371 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4372 INIT_LIST_HEAD(&adapter->rwi_list);
4373 mutex_init(&adapter->reset_lock);
4374 mutex_init(&adapter->rwi_lock);
4375 adapter->resetting = false;
4377 adapter->mac_change_pending = false;
4380 rc = ibmvnic_init(adapter);
4381 if (rc && rc != EAGAIN)
4382 goto ibmvnic_init_fail;
4383 } while (rc == EAGAIN);
4385 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4386 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4387 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4389 rc = device_create_file(&dev->dev, &dev_attr_failover);
4391 goto ibmvnic_init_fail;
4393 netif_carrier_off(netdev);
4394 rc = register_netdev(netdev);
4396 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4397 goto ibmvnic_register_fail;
4399 dev_info(&dev->dev, "ibmvnic registered\n");
4401 adapter->state = VNIC_PROBED;
4403 adapter->wait_for_reset = false;
4407 ibmvnic_register_fail:
4408 device_remove_file(&dev->dev, &dev_attr_failover);
4411 release_sub_crqs(adapter);
4412 release_crq_queue(adapter);
4413 free_netdev(netdev);
4418 static int ibmvnic_remove(struct vio_dev *dev)
4420 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4421 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4423 adapter->state = VNIC_REMOVING;
4424 unregister_netdev(netdev);
4425 mutex_lock(&adapter->reset_lock);
4427 release_resources(adapter);
4428 release_sub_crqs(adapter);
4429 release_crq_queue(adapter);
4431 adapter->state = VNIC_REMOVED;
4433 mutex_unlock(&adapter->reset_lock);
4434 device_remove_file(&dev->dev, &dev_attr_failover);
4435 free_netdev(netdev);
4436 dev_set_drvdata(&dev->dev, NULL);
4441 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4442 const char *buf, size_t count)
4444 struct net_device *netdev = dev_get_drvdata(dev);
4445 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4446 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4447 __be64 session_token;
4450 if (!sysfs_streq(buf, "1"))
4453 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4454 H_GET_SESSION_TOKEN, 0, 0, 0);
4456 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4461 session_token = (__be64)retbuf[0];
4462 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4463 be64_to_cpu(session_token));
4464 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4465 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4467 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4475 static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4477 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4479 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4480 struct ibmvnic_adapter *adapter;
4481 struct iommu_table *tbl;
4482 unsigned long ret = 0;
4485 tbl = get_iommu_table_base(&vdev->dev);
4487 /* netdev inits at probe time along with the structures we need below*/
4489 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4491 adapter = netdev_priv(netdev);
4493 ret += PAGE_SIZE; /* the crq message queue */
4494 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4496 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4497 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4499 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4501 ret += adapter->rx_pool[i].size *
4502 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4507 static int ibmvnic_resume(struct device *dev)
4509 struct net_device *netdev = dev_get_drvdata(dev);
4510 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4512 if (adapter->state != VNIC_OPEN)
4515 tasklet_schedule(&adapter->tasklet);
4520 static const struct vio_device_id ibmvnic_device_table[] = {
4521 {"network", "IBM,vnic"},
4524 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4526 static const struct dev_pm_ops ibmvnic_pm_ops = {
4527 .resume = ibmvnic_resume
4530 static struct vio_driver ibmvnic_driver = {
4531 .id_table = ibmvnic_device_table,
4532 .probe = ibmvnic_probe,
4533 .remove = ibmvnic_remove,
4534 .get_desired_dma = ibmvnic_get_desired_dma,
4535 .name = ibmvnic_driver_name,
4536 .pm = &ibmvnic_pm_ops,
4539 /* module functions */
4540 static int __init ibmvnic_module_init(void)
4542 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4543 IBMVNIC_DRIVER_VERSION);
4545 return vio_register_driver(&ibmvnic_driver);
4548 static void __exit ibmvnic_module_exit(void)
4550 vio_unregister_driver(&ibmvnic_driver);
4553 module_init(ibmvnic_module_init);
4554 module_exit(ibmvnic_module_exit);