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>
64 #include <linux/ipv6.h>
65 #include <linux/irq.h>
66 #include <linux/kthread.h>
67 #include <linux/seq_file.h>
68 #include <linux/interrupt.h>
69 #include <net/net_namespace.h>
70 #include <asm/hvcall.h>
71 #include <linux/atomic.h>
73 #include <asm/iommu.h>
74 #include <linux/uaccess.h>
75 #include <asm/firmware.h>
76 #include <linux/workqueue.h>
77 #include <linux/if_vlan.h>
81 static const char ibmvnic_driver_name[] = "ibmvnic";
82 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
84 MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
85 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
86 MODULE_LICENSE("GPL");
87 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
89 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
90 static int ibmvnic_remove(struct vio_dev *);
91 static void release_sub_crqs(struct ibmvnic_adapter *);
92 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
93 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
94 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
96 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
97 union sub_crq *sub_crq);
98 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
99 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
100 static int enable_scrq_irq(struct ibmvnic_adapter *,
101 struct ibmvnic_sub_crq_queue *);
102 static int disable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104 static int pending_scrq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108 static int ibmvnic_poll(struct napi_struct *napi, int data);
109 static void send_map_query(struct ibmvnic_adapter *adapter);
110 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
111 static void send_request_unmap(struct ibmvnic_adapter *, u8);
112 static void send_login(struct ibmvnic_adapter *adapter);
113 static void send_cap_queries(struct ibmvnic_adapter *adapter);
114 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
115 static int ibmvnic_init(struct ibmvnic_adapter *);
116 static void release_crq_queue(struct ibmvnic_adapter *);
118 struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
123 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
127 static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
152 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
153 unsigned long length, unsigned long *number,
156 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
159 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
166 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
169 struct device *dev = &adapter->vdev->dev;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
176 dev_err(dev, "Couldn't alloc long term buffer\n");
179 ltb->map_id = adapter->map_id;
182 init_completion(&adapter->fw_done);
183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
185 wait_for_completion(&adapter->fw_done);
189 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
190 struct ibmvnic_long_term_buff *ltb)
192 struct device *dev = &adapter->vdev->dev;
197 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
198 adapter->reset_reason != VNIC_RESET_MOBILITY)
199 send_request_unmap(adapter, ltb->map_id);
200 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
203 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
204 struct ibmvnic_rx_pool *pool)
206 int count = pool->size - atomic_read(&pool->available);
207 struct device *dev = &adapter->vdev->dev;
208 int buffers_added = 0;
209 unsigned long lpar_rc;
210 union sub_crq sub_crq;
220 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
221 be32_to_cpu(adapter->login_rsp_buf->
224 for (i = 0; i < count; ++i) {
225 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
227 dev_err(dev, "Couldn't replenish rx buff\n");
228 adapter->replenish_no_mem++;
232 index = pool->free_map[pool->next_free];
234 if (pool->rx_buff[index].skb)
235 dev_err(dev, "Inconsistent free_map!\n");
237 /* Copy the skb to the long term mapped DMA buffer */
238 offset = index * pool->buff_size;
239 dst = pool->long_term_buff.buff + offset;
240 memset(dst, 0, pool->buff_size);
241 dma_addr = pool->long_term_buff.addr + offset;
242 pool->rx_buff[index].data = dst;
244 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
245 pool->rx_buff[index].dma = dma_addr;
246 pool->rx_buff[index].skb = skb;
247 pool->rx_buff[index].pool_index = pool->index;
248 pool->rx_buff[index].size = pool->buff_size;
250 memset(&sub_crq, 0, sizeof(sub_crq));
251 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
252 sub_crq.rx_add.correlator =
253 cpu_to_be64((u64)&pool->rx_buff[index]);
254 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
255 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
257 /* The length field of the sCRQ is defined to be 24 bits so the
258 * buffer size needs to be left shifted by a byte before it is
259 * converted to big endian to prevent the last byte from being
262 #ifdef __LITTLE_ENDIAN__
265 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
267 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
269 if (lpar_rc != H_SUCCESS)
273 adapter->replenish_add_buff_success++;
274 pool->next_free = (pool->next_free + 1) % pool->size;
276 atomic_add(buffers_added, &pool->available);
280 dev_info(dev, "replenish pools failure\n");
281 pool->free_map[pool->next_free] = index;
282 pool->rx_buff[index].skb = NULL;
283 if (!dma_mapping_error(dev, dma_addr))
284 dma_unmap_single(dev, dma_addr, pool->buff_size,
287 dev_kfree_skb_any(skb);
288 adapter->replenish_add_buff_failure++;
289 atomic_add(buffers_added, &pool->available);
292 static void replenish_pools(struct ibmvnic_adapter *adapter)
296 adapter->replenish_task_cycles++;
297 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
299 if (adapter->rx_pool[i].active)
300 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
304 static void release_stats_token(struct ibmvnic_adapter *adapter)
306 struct device *dev = &adapter->vdev->dev;
308 if (!adapter->stats_token)
311 dma_unmap_single(dev, adapter->stats_token,
312 sizeof(struct ibmvnic_statistics),
314 adapter->stats_token = 0;
317 static int init_stats_token(struct ibmvnic_adapter *adapter)
319 struct device *dev = &adapter->vdev->dev;
322 stok = dma_map_single(dev, &adapter->stats,
323 sizeof(struct ibmvnic_statistics),
325 if (dma_mapping_error(dev, stok)) {
326 dev_err(dev, "Couldn't map stats buffer\n");
330 adapter->stats_token = stok;
334 static void release_rx_pools(struct ibmvnic_adapter *adapter)
336 struct ibmvnic_rx_pool *rx_pool;
340 if (!adapter->rx_pool)
343 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
344 for (i = 0; i < rx_scrqs; i++) {
345 rx_pool = &adapter->rx_pool[i];
347 kfree(rx_pool->free_map);
348 free_long_term_buff(adapter, &rx_pool->long_term_buff);
350 if (!rx_pool->rx_buff)
353 for (j = 0; j < rx_pool->size; j++) {
354 if (rx_pool->rx_buff[j].skb) {
355 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
356 rx_pool->rx_buff[i].skb = NULL;
360 kfree(rx_pool->rx_buff);
363 kfree(adapter->rx_pool);
364 adapter->rx_pool = NULL;
367 static int init_rx_pools(struct net_device *netdev)
369 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
370 struct device *dev = &adapter->vdev->dev;
371 struct ibmvnic_rx_pool *rx_pool;
377 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
378 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
379 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
381 adapter->rx_pool = kcalloc(rxadd_subcrqs,
382 sizeof(struct ibmvnic_rx_pool),
384 if (!adapter->rx_pool) {
385 dev_err(dev, "Failed to allocate rx pools\n");
389 for (i = 0; i < rxadd_subcrqs; i++) {
390 rx_pool = &adapter->rx_pool[i];
392 netdev_dbg(adapter->netdev,
393 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
394 i, adapter->req_rx_add_entries_per_subcrq,
395 be64_to_cpu(size_array[i]));
397 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
399 rx_pool->buff_size = be64_to_cpu(size_array[i]);
402 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
404 if (!rx_pool->free_map) {
405 release_rx_pools(adapter);
409 rx_pool->rx_buff = kcalloc(rx_pool->size,
410 sizeof(struct ibmvnic_rx_buff),
412 if (!rx_pool->rx_buff) {
413 dev_err(dev, "Couldn't alloc rx buffers\n");
414 release_rx_pools(adapter);
418 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
419 rx_pool->size * rx_pool->buff_size)) {
420 release_rx_pools(adapter);
424 for (j = 0; j < rx_pool->size; ++j)
425 rx_pool->free_map[j] = j;
427 atomic_set(&rx_pool->available, 0);
428 rx_pool->next_alloc = 0;
429 rx_pool->next_free = 0;
435 static void release_tx_pools(struct ibmvnic_adapter *adapter)
437 struct ibmvnic_tx_pool *tx_pool;
440 if (!adapter->tx_pool)
443 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
444 for (i = 0; i < tx_scrqs; i++) {
445 tx_pool = &adapter->tx_pool[i];
446 kfree(tx_pool->tx_buff);
447 free_long_term_buff(adapter, &tx_pool->long_term_buff);
448 kfree(tx_pool->free_map);
451 kfree(adapter->tx_pool);
452 adapter->tx_pool = NULL;
455 static int init_tx_pools(struct net_device *netdev)
457 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
458 struct device *dev = &adapter->vdev->dev;
459 struct ibmvnic_tx_pool *tx_pool;
463 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
464 adapter->tx_pool = kcalloc(tx_subcrqs,
465 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
466 if (!adapter->tx_pool)
469 for (i = 0; i < tx_subcrqs; i++) {
470 tx_pool = &adapter->tx_pool[i];
471 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
472 sizeof(struct ibmvnic_tx_buff),
474 if (!tx_pool->tx_buff) {
475 dev_err(dev, "tx pool buffer allocation failed\n");
476 release_tx_pools(adapter);
480 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
481 adapter->req_tx_entries_per_subcrq *
483 release_tx_pools(adapter);
487 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
488 sizeof(int), GFP_KERNEL);
489 if (!tx_pool->free_map) {
490 release_tx_pools(adapter);
494 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
495 tx_pool->free_map[j] = j;
497 tx_pool->consumer_index = 0;
498 tx_pool->producer_index = 0;
504 static void release_error_buffers(struct ibmvnic_adapter *adapter)
506 struct device *dev = &adapter->vdev->dev;
507 struct ibmvnic_error_buff *error_buff, *tmp;
510 spin_lock_irqsave(&adapter->error_list_lock, flags);
511 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
512 list_del(&error_buff->list);
513 dma_unmap_single(dev, error_buff->dma, error_buff->len,
515 kfree(error_buff->buff);
518 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
521 static int ibmvnic_login(struct net_device *netdev)
523 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
524 unsigned long timeout = msecs_to_jiffies(30000);
525 struct device *dev = &adapter->vdev->dev;
528 if (adapter->renegotiate) {
529 adapter->renegotiate = false;
530 release_sub_crqs(adapter);
532 reinit_completion(&adapter->init_done);
533 send_cap_queries(adapter);
534 if (!wait_for_completion_timeout(&adapter->init_done,
536 dev_err(dev, "Capabilities query timeout\n");
541 reinit_completion(&adapter->init_done);
543 if (!wait_for_completion_timeout(&adapter->init_done,
545 dev_err(dev, "Login timeout\n");
548 } while (adapter->renegotiate);
553 static void release_resources(struct ibmvnic_adapter *adapter)
557 release_tx_pools(adapter);
558 release_rx_pools(adapter);
560 release_stats_token(adapter);
561 release_error_buffers(adapter);
564 for (i = 0; i < adapter->req_rx_queues; i++) {
565 if (&adapter->napi[i])
566 netif_napi_del(&adapter->napi[i]);
571 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
573 struct net_device *netdev = adapter->netdev;
574 unsigned long timeout = msecs_to_jiffies(30000);
575 union ibmvnic_crq crq;
579 netdev_err(netdev, "setting link state %d\n", link_state);
580 memset(&crq, 0, sizeof(crq));
581 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
582 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
583 crq.logical_link_state.link_state = link_state;
588 reinit_completion(&adapter->init_done);
589 rc = ibmvnic_send_crq(adapter, &crq);
591 netdev_err(netdev, "Failed to set link state\n");
595 if (!wait_for_completion_timeout(&adapter->init_done,
597 netdev_err(netdev, "timeout setting link state\n");
601 if (adapter->init_done_rc == 1) {
602 /* Partuial success, delay and re-send */
611 static int set_real_num_queues(struct net_device *netdev)
613 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
616 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
618 netdev_err(netdev, "failed to set the number of tx queues\n");
622 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
624 netdev_err(netdev, "failed to set the number of rx queues\n");
629 static int init_resources(struct ibmvnic_adapter *adapter)
631 struct net_device *netdev = adapter->netdev;
634 rc = set_real_num_queues(netdev);
638 rc = init_sub_crq_irqs(adapter);
640 netdev_err(netdev, "failed to initialize sub crq irqs\n");
644 rc = init_stats_token(adapter);
649 adapter->napi = kcalloc(adapter->req_rx_queues,
650 sizeof(struct napi_struct), GFP_KERNEL);
654 for (i = 0; i < adapter->req_rx_queues; i++) {
655 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
659 send_map_query(adapter);
661 rc = init_rx_pools(netdev);
665 rc = init_tx_pools(netdev);
669 static int __ibmvnic_open(struct net_device *netdev)
671 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
672 enum vnic_state prev_state = adapter->state;
675 adapter->state = VNIC_OPENING;
676 replenish_pools(adapter);
678 for (i = 0; i < adapter->req_rx_queues; i++)
679 napi_enable(&adapter->napi[i]);
681 /* We're ready to receive frames, enable the sub-crq interrupts and
682 * set the logical link state to up
684 for (i = 0; i < adapter->req_rx_queues; i++) {
685 if (prev_state == VNIC_CLOSED)
686 enable_irq(adapter->rx_scrq[i]->irq);
688 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
691 for (i = 0; i < adapter->req_tx_queues; i++) {
692 if (prev_state == VNIC_CLOSED)
693 enable_irq(adapter->tx_scrq[i]->irq);
695 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
698 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
700 for (i = 0; i < adapter->req_rx_queues; i++)
701 napi_disable(&adapter->napi[i]);
702 release_resources(adapter);
706 netif_tx_start_all_queues(netdev);
708 if (prev_state == VNIC_CLOSED) {
709 for (i = 0; i < adapter->req_rx_queues; i++)
710 napi_schedule(&adapter->napi[i]);
713 adapter->state = VNIC_OPEN;
717 static int ibmvnic_open(struct net_device *netdev)
719 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
722 mutex_lock(&adapter->reset_lock);
724 if (adapter->state != VNIC_CLOSED) {
725 rc = ibmvnic_login(netdev);
727 mutex_unlock(&adapter->reset_lock);
731 rc = init_resources(adapter);
733 netdev_err(netdev, "failed to initialize resources\n");
734 release_resources(adapter);
735 mutex_unlock(&adapter->reset_lock);
740 rc = __ibmvnic_open(netdev);
741 mutex_unlock(&adapter->reset_lock);
746 static void disable_sub_crqs(struct ibmvnic_adapter *adapter)
750 if (adapter->tx_scrq) {
751 for (i = 0; i < adapter->req_tx_queues; i++)
752 if (adapter->tx_scrq[i])
753 disable_irq(adapter->tx_scrq[i]->irq);
756 if (adapter->rx_scrq) {
757 for (i = 0; i < adapter->req_rx_queues; i++)
758 if (adapter->rx_scrq[i])
759 disable_irq(adapter->rx_scrq[i]->irq);
763 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
765 struct ibmvnic_tx_pool *tx_pool;
770 if (!adapter->tx_pool)
773 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
774 tx_entries = adapter->req_tx_entries_per_subcrq;
776 /* Free any remaining skbs in the tx buffer pools */
777 for (i = 0; i < tx_scrqs; i++) {
778 tx_pool = &adapter->tx_pool[i];
782 for (j = 0; j < tx_entries; j++) {
783 if (tx_pool->tx_buff[j].skb) {
784 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
785 tx_pool->tx_buff[j].skb = NULL;
791 static int __ibmvnic_close(struct net_device *netdev)
793 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
797 adapter->state = VNIC_CLOSING;
798 netif_tx_stop_all_queues(netdev);
800 clean_tx_pools(adapter);
801 disable_sub_crqs(adapter);
804 for (i = 0; i < adapter->req_rx_queues; i++)
805 napi_disable(&adapter->napi[i]);
808 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
810 adapter->state = VNIC_CLOSED;
814 static int ibmvnic_close(struct net_device *netdev)
816 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
819 mutex_lock(&adapter->reset_lock);
820 rc = __ibmvnic_close(netdev);
821 mutex_unlock(&adapter->reset_lock);
827 * build_hdr_data - creates L2/L3/L4 header data buffer
828 * @hdr_field - bitfield determining needed headers
829 * @skb - socket buffer
830 * @hdr_len - array of header lengths
831 * @tot_len - total length of data
833 * Reads hdr_field to determine which headers are needed by firmware.
834 * Builds a buffer containing these headers. Saves individual header
835 * lengths and total buffer length to be used to build descriptors.
837 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
838 int *hdr_len, u8 *hdr_data)
843 hdr_len[0] = sizeof(struct ethhdr);
845 if (skb->protocol == htons(ETH_P_IP)) {
846 hdr_len[1] = ip_hdr(skb)->ihl * 4;
847 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
848 hdr_len[2] = tcp_hdrlen(skb);
849 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
850 hdr_len[2] = sizeof(struct udphdr);
851 } else if (skb->protocol == htons(ETH_P_IPV6)) {
852 hdr_len[1] = sizeof(struct ipv6hdr);
853 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
854 hdr_len[2] = tcp_hdrlen(skb);
855 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
856 hdr_len[2] = sizeof(struct udphdr);
859 memset(hdr_data, 0, 120);
860 if ((hdr_field >> 6) & 1) {
861 hdr = skb_mac_header(skb);
862 memcpy(hdr_data, hdr, hdr_len[0]);
866 if ((hdr_field >> 5) & 1) {
867 hdr = skb_network_header(skb);
868 memcpy(hdr_data + len, hdr, hdr_len[1]);
872 if ((hdr_field >> 4) & 1) {
873 hdr = skb_transport_header(skb);
874 memcpy(hdr_data + len, hdr, hdr_len[2]);
881 * create_hdr_descs - create header and header extension descriptors
882 * @hdr_field - bitfield determining needed headers
883 * @data - buffer containing header data
884 * @len - length of data buffer
885 * @hdr_len - array of individual header lengths
886 * @scrq_arr - descriptor array
888 * Creates header and, if needed, header extension descriptors and
889 * places them in a descriptor array, scrq_arr
892 static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
893 union sub_crq *scrq_arr)
895 union sub_crq hdr_desc;
900 while (tmp_len > 0) {
901 cur = hdr_data + len - tmp_len;
903 memset(&hdr_desc, 0, sizeof(hdr_desc));
904 if (cur != hdr_data) {
905 data = hdr_desc.hdr_ext.data;
906 tmp = tmp_len > 29 ? 29 : tmp_len;
907 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
908 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
909 hdr_desc.hdr_ext.len = tmp;
911 data = hdr_desc.hdr.data;
912 tmp = tmp_len > 24 ? 24 : tmp_len;
913 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
914 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
915 hdr_desc.hdr.len = tmp;
916 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
917 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
918 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
919 hdr_desc.hdr.flag = hdr_field << 1;
921 memcpy(data, cur, tmp);
923 *scrq_arr = hdr_desc;
929 * build_hdr_descs_arr - build a header descriptor array
930 * @skb - socket buffer
931 * @num_entries - number of descriptors to be sent
932 * @subcrq - first TX descriptor
933 * @hdr_field - bit field determining which headers will be sent
935 * This function will build a TX descriptor array with applicable
936 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
939 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
940 int *num_entries, u8 hdr_field)
942 int hdr_len[3] = {0, 0, 0};
944 u8 *hdr_data = txbuff->hdr_data;
946 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
951 num_entries += len % 29 ? len / 29 + 1 : len / 29;
952 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
953 txbuff->indir_arr + 1);
956 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
958 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
959 int queue_num = skb_get_queue_mapping(skb);
960 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
961 struct device *dev = &adapter->vdev->dev;
962 struct ibmvnic_tx_buff *tx_buff = NULL;
963 struct ibmvnic_sub_crq_queue *tx_scrq;
964 struct ibmvnic_tx_pool *tx_pool;
965 unsigned int tx_send_failed = 0;
966 unsigned int tx_map_failed = 0;
967 unsigned int tx_dropped = 0;
968 unsigned int tx_packets = 0;
969 unsigned int tx_bytes = 0;
970 dma_addr_t data_dma_addr;
971 struct netdev_queue *txq;
972 unsigned long lpar_rc;
973 union sub_crq tx_crq;
981 tx_pool = &adapter->tx_pool[queue_num];
982 tx_scrq = adapter->tx_scrq[queue_num];
983 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
984 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
985 be32_to_cpu(adapter->login_rsp_buf->
986 off_txsubm_subcrqs));
987 if (adapter->resetting) {
988 if (!netif_subqueue_stopped(netdev, skb))
989 netif_stop_subqueue(netdev, queue_num);
990 dev_kfree_skb_any(skb);
998 index = tx_pool->free_map[tx_pool->consumer_index];
999 offset = index * adapter->req_mtu;
1000 dst = tx_pool->long_term_buff.buff + offset;
1001 memset(dst, 0, adapter->req_mtu);
1002 skb_copy_from_linear_data(skb, dst, skb->len);
1003 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1005 tx_pool->consumer_index =
1006 (tx_pool->consumer_index + 1) %
1007 adapter->req_tx_entries_per_subcrq;
1009 tx_buff = &tx_pool->tx_buff[index];
1011 tx_buff->data_dma[0] = data_dma_addr;
1012 tx_buff->data_len[0] = skb->len;
1013 tx_buff->index = index;
1014 tx_buff->pool_index = queue_num;
1015 tx_buff->last_frag = true;
1017 memset(&tx_crq, 0, sizeof(tx_crq));
1018 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1019 tx_crq.v1.type = IBMVNIC_TX_DESC;
1020 tx_crq.v1.n_crq_elem = 1;
1021 tx_crq.v1.n_sge = 1;
1022 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1023 tx_crq.v1.correlator = cpu_to_be32(index);
1024 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1025 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1026 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1028 if (adapter->vlan_header_insertion) {
1029 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1030 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1033 if (skb->protocol == htons(ETH_P_IP)) {
1034 if (ip_hdr(skb)->version == 4)
1035 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1036 else if (ip_hdr(skb)->version == 6)
1037 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1039 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1040 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1041 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1042 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1045 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1046 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1049 /* determine if l2/3/4 headers are sent to firmware */
1050 if ((*hdrs >> 7) & 1 &&
1051 (skb->protocol == htons(ETH_P_IP) ||
1052 skb->protocol == htons(ETH_P_IPV6))) {
1053 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1054 tx_crq.v1.n_crq_elem = num_entries;
1055 tx_buff->indir_arr[0] = tx_crq;
1056 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1057 sizeof(tx_buff->indir_arr),
1059 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1060 dev_kfree_skb_any(skb);
1061 tx_buff->skb = NULL;
1062 if (!firmware_has_feature(FW_FEATURE_CMO))
1063 dev_err(dev, "tx: unable to map descriptor array\n");
1069 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1070 (u64)tx_buff->indir_dma,
1073 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1076 if (lpar_rc != H_SUCCESS) {
1077 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1079 if (tx_pool->consumer_index == 0)
1080 tx_pool->consumer_index =
1081 adapter->req_tx_entries_per_subcrq - 1;
1083 tx_pool->consumer_index--;
1085 dev_kfree_skb_any(skb);
1086 tx_buff->skb = NULL;
1088 if (lpar_rc == H_CLOSED)
1089 netif_stop_subqueue(netdev, queue_num);
1097 if (atomic_inc_return(&tx_scrq->used)
1098 >= adapter->req_tx_entries_per_subcrq) {
1099 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1100 netif_stop_subqueue(netdev, queue_num);
1104 tx_bytes += skb->len;
1105 txq->trans_start = jiffies;
1109 netdev->stats.tx_dropped += tx_dropped;
1110 netdev->stats.tx_bytes += tx_bytes;
1111 netdev->stats.tx_packets += tx_packets;
1112 adapter->tx_send_failed += tx_send_failed;
1113 adapter->tx_map_failed += tx_map_failed;
1118 static void ibmvnic_set_multi(struct net_device *netdev)
1120 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1121 struct netdev_hw_addr *ha;
1122 union ibmvnic_crq crq;
1124 memset(&crq, 0, sizeof(crq));
1125 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1126 crq.request_capability.cmd = REQUEST_CAPABILITY;
1128 if (netdev->flags & IFF_PROMISC) {
1129 if (!adapter->promisc_supported)
1132 if (netdev->flags & IFF_ALLMULTI) {
1133 /* Accept all multicast */
1134 memset(&crq, 0, sizeof(crq));
1135 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1136 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1137 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1138 ibmvnic_send_crq(adapter, &crq);
1139 } else if (netdev_mc_empty(netdev)) {
1140 /* Reject all multicast */
1141 memset(&crq, 0, sizeof(crq));
1142 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1143 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1144 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1145 ibmvnic_send_crq(adapter, &crq);
1147 /* Accept one or more multicast(s) */
1148 netdev_for_each_mc_addr(ha, netdev) {
1149 memset(&crq, 0, sizeof(crq));
1150 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1151 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1152 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1153 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1155 ibmvnic_send_crq(adapter, &crq);
1161 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1163 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1164 struct sockaddr *addr = p;
1165 union ibmvnic_crq crq;
1167 if (!is_valid_ether_addr(addr->sa_data))
1168 return -EADDRNOTAVAIL;
1170 memset(&crq, 0, sizeof(crq));
1171 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1172 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1173 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1174 ibmvnic_send_crq(adapter, &crq);
1175 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1180 * do_reset returns zero if we are able to keep processing reset events, or
1181 * non-zero if we hit a fatal error and must halt.
1183 static int do_reset(struct ibmvnic_adapter *adapter,
1184 struct ibmvnic_rwi *rwi, u32 reset_state)
1186 struct net_device *netdev = adapter->netdev;
1189 netif_carrier_off(netdev);
1190 adapter->reset_reason = rwi->reset_reason;
1192 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1193 rc = ibmvnic_reenable_crq_queue(adapter);
1198 rc = __ibmvnic_close(netdev);
1202 /* remove the closed state so when we call open it appears
1203 * we are coming from the probed state.
1205 adapter->state = VNIC_PROBED;
1207 release_resources(adapter);
1208 release_sub_crqs(adapter);
1209 release_crq_queue(adapter);
1211 rc = ibmvnic_init(adapter);
1215 /* If the adapter was in PROBE state prior to the reset, exit here. */
1216 if (reset_state == VNIC_PROBED)
1219 rc = ibmvnic_login(netdev);
1221 adapter->state = VNIC_PROBED;
1226 rc = init_resources(adapter);
1231 if (reset_state == VNIC_CLOSED)
1234 rc = __ibmvnic_open(netdev);
1236 if (list_empty(&adapter->rwi_list))
1237 adapter->state = VNIC_CLOSED;
1239 adapter->state = reset_state;
1244 netif_carrier_on(netdev);
1247 for (i = 0; i < adapter->req_rx_queues; i++)
1248 napi_schedule(&adapter->napi[i]);
1253 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1255 struct ibmvnic_rwi *rwi;
1257 mutex_lock(&adapter->rwi_lock);
1259 if (!list_empty(&adapter->rwi_list)) {
1260 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1262 list_del(&rwi->list);
1267 mutex_unlock(&adapter->rwi_lock);
1271 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1273 struct ibmvnic_rwi *rwi;
1275 rwi = get_next_rwi(adapter);
1278 rwi = get_next_rwi(adapter);
1282 static void __ibmvnic_reset(struct work_struct *work)
1284 struct ibmvnic_rwi *rwi;
1285 struct ibmvnic_adapter *adapter;
1286 struct net_device *netdev;
1290 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1291 netdev = adapter->netdev;
1293 mutex_lock(&adapter->reset_lock);
1294 adapter->resetting = true;
1295 reset_state = adapter->state;
1297 rwi = get_next_rwi(adapter);
1299 rc = do_reset(adapter, rwi, reset_state);
1304 rwi = get_next_rwi(adapter);
1308 free_all_rwi(adapter);
1312 adapter->resetting = false;
1313 mutex_unlock(&adapter->reset_lock);
1316 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1317 enum ibmvnic_reset_reason reason)
1319 struct ibmvnic_rwi *rwi, *tmp;
1320 struct net_device *netdev = adapter->netdev;
1321 struct list_head *entry;
1323 if (adapter->state == VNIC_REMOVING ||
1324 adapter->state == VNIC_REMOVED) {
1325 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1329 mutex_lock(&adapter->rwi_lock);
1331 list_for_each(entry, &adapter->rwi_list) {
1332 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1333 if (tmp->reset_reason == reason) {
1334 netdev_err(netdev, "Matching reset found, skipping\n");
1335 mutex_unlock(&adapter->rwi_lock);
1340 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1342 mutex_unlock(&adapter->rwi_lock);
1343 ibmvnic_close(netdev);
1347 rwi->reset_reason = reason;
1348 list_add_tail(&rwi->list, &adapter->rwi_list);
1349 mutex_unlock(&adapter->rwi_lock);
1350 schedule_work(&adapter->ibmvnic_reset);
1353 static void ibmvnic_tx_timeout(struct net_device *dev)
1355 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1357 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1360 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1361 struct ibmvnic_rx_buff *rx_buff)
1363 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1365 rx_buff->skb = NULL;
1367 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1368 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1370 atomic_dec(&pool->available);
1373 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1375 struct net_device *netdev = napi->dev;
1376 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1377 int scrq_num = (int)(napi - adapter->napi);
1378 int frames_processed = 0;
1380 while (frames_processed < budget) {
1381 struct sk_buff *skb;
1382 struct ibmvnic_rx_buff *rx_buff;
1383 union sub_crq *next;
1388 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1390 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1392 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1393 rx_comp.correlator);
1394 /* do error checking */
1395 if (next->rx_comp.rc) {
1396 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1397 /* free the entry */
1398 next->rx_comp.first = 0;
1399 remove_buff_from_pool(adapter, rx_buff);
1403 length = be32_to_cpu(next->rx_comp.len);
1404 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1405 flags = next->rx_comp.flags;
1407 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1410 /* VLAN Header has been stripped by the system firmware and
1411 * needs to be inserted by the driver
1413 if (adapter->rx_vlan_header_insertion &&
1414 (flags & IBMVNIC_VLAN_STRIPPED))
1415 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1416 ntohs(next->rx_comp.vlan_tci));
1418 /* free the entry */
1419 next->rx_comp.first = 0;
1420 remove_buff_from_pool(adapter, rx_buff);
1422 skb_put(skb, length);
1423 skb->protocol = eth_type_trans(skb, netdev);
1425 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1426 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1427 skb->ip_summed = CHECKSUM_UNNECESSARY;
1431 napi_gro_receive(napi, skb); /* send it up */
1432 netdev->stats.rx_packets++;
1433 netdev->stats.rx_bytes += length;
1436 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1438 if (frames_processed < budget) {
1439 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1440 napi_complete_done(napi, frames_processed);
1441 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1442 napi_reschedule(napi)) {
1443 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1447 return frames_processed;
1450 #ifdef CONFIG_NET_POLL_CONTROLLER
1451 static void ibmvnic_netpoll_controller(struct net_device *dev)
1453 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1456 replenish_pools(netdev_priv(dev));
1457 for (i = 0; i < adapter->req_rx_queues; i++)
1458 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1459 adapter->rx_scrq[i]);
1463 static const struct net_device_ops ibmvnic_netdev_ops = {
1464 .ndo_open = ibmvnic_open,
1465 .ndo_stop = ibmvnic_close,
1466 .ndo_start_xmit = ibmvnic_xmit,
1467 .ndo_set_rx_mode = ibmvnic_set_multi,
1468 .ndo_set_mac_address = ibmvnic_set_mac,
1469 .ndo_validate_addr = eth_validate_addr,
1470 .ndo_tx_timeout = ibmvnic_tx_timeout,
1471 #ifdef CONFIG_NET_POLL_CONTROLLER
1472 .ndo_poll_controller = ibmvnic_netpoll_controller,
1476 /* ethtool functions */
1478 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1479 struct ethtool_link_ksettings *cmd)
1481 u32 supported, advertising;
1483 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1485 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1487 cmd->base.speed = SPEED_1000;
1488 cmd->base.duplex = DUPLEX_FULL;
1489 cmd->base.port = PORT_FIBRE;
1490 cmd->base.phy_address = 0;
1491 cmd->base.autoneg = AUTONEG_ENABLE;
1493 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1495 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1501 static void ibmvnic_get_drvinfo(struct net_device *dev,
1502 struct ethtool_drvinfo *info)
1504 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1505 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1508 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1510 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1512 return adapter->msg_enable;
1515 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1517 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1519 adapter->msg_enable = data;
1522 static u32 ibmvnic_get_link(struct net_device *netdev)
1524 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1526 /* Don't need to send a query because we request a logical link up at
1527 * init and then we wait for link state indications
1529 return adapter->logical_link_state;
1532 static void ibmvnic_get_ringparam(struct net_device *netdev,
1533 struct ethtool_ringparam *ring)
1535 ring->rx_max_pending = 0;
1536 ring->tx_max_pending = 0;
1537 ring->rx_mini_max_pending = 0;
1538 ring->rx_jumbo_max_pending = 0;
1539 ring->rx_pending = 0;
1540 ring->tx_pending = 0;
1541 ring->rx_mini_pending = 0;
1542 ring->rx_jumbo_pending = 0;
1545 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1549 if (stringset != ETH_SS_STATS)
1552 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1553 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1556 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1560 return ARRAY_SIZE(ibmvnic_stats);
1566 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1567 struct ethtool_stats *stats, u64 *data)
1569 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1570 union ibmvnic_crq crq;
1573 memset(&crq, 0, sizeof(crq));
1574 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1575 crq.request_statistics.cmd = REQUEST_STATISTICS;
1576 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1577 crq.request_statistics.len =
1578 cpu_to_be32(sizeof(struct ibmvnic_statistics));
1580 /* Wait for data to be written */
1581 init_completion(&adapter->stats_done);
1582 ibmvnic_send_crq(adapter, &crq);
1583 wait_for_completion(&adapter->stats_done);
1585 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1586 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1589 static const struct ethtool_ops ibmvnic_ethtool_ops = {
1590 .get_drvinfo = ibmvnic_get_drvinfo,
1591 .get_msglevel = ibmvnic_get_msglevel,
1592 .set_msglevel = ibmvnic_set_msglevel,
1593 .get_link = ibmvnic_get_link,
1594 .get_ringparam = ibmvnic_get_ringparam,
1595 .get_strings = ibmvnic_get_strings,
1596 .get_sset_count = ibmvnic_get_sset_count,
1597 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
1598 .get_link_ksettings = ibmvnic_get_link_ksettings,
1601 /* Routines for managing CRQs/sCRQs */
1603 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1604 struct ibmvnic_sub_crq_queue *scrq)
1606 struct device *dev = &adapter->vdev->dev;
1609 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1611 /* Close the sub-crqs */
1613 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1614 adapter->vdev->unit_address,
1616 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1619 netdev_err(adapter->netdev,
1620 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1624 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1626 free_pages((unsigned long)scrq->msgs, 2);
1630 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1633 struct device *dev = &adapter->vdev->dev;
1634 struct ibmvnic_sub_crq_queue *scrq;
1637 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
1642 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
1644 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1645 goto zero_page_failed;
1648 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1650 if (dma_mapping_error(dev, scrq->msg_token)) {
1651 dev_warn(dev, "Couldn't map crq queue messages page\n");
1655 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1656 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1658 if (rc == H_RESOURCE)
1659 rc = ibmvnic_reset_crq(adapter);
1661 if (rc == H_CLOSED) {
1662 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1664 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1668 scrq->adapter = adapter;
1669 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1670 spin_lock_init(&scrq->lock);
1672 netdev_dbg(adapter->netdev,
1673 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1674 scrq->crq_num, scrq->hw_irq, scrq->irq);
1679 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1682 free_pages((unsigned long)scrq->msgs, 2);
1689 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1693 if (adapter->tx_scrq) {
1694 for (i = 0; i < adapter->req_tx_queues; i++) {
1695 if (!adapter->tx_scrq[i])
1698 if (adapter->tx_scrq[i]->irq) {
1699 free_irq(adapter->tx_scrq[i]->irq,
1700 adapter->tx_scrq[i]);
1701 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
1702 adapter->tx_scrq[i]->irq = 0;
1705 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1708 kfree(adapter->tx_scrq);
1709 adapter->tx_scrq = NULL;
1712 if (adapter->rx_scrq) {
1713 for (i = 0; i < adapter->req_rx_queues; i++) {
1714 if (!adapter->rx_scrq[i])
1717 if (adapter->rx_scrq[i]->irq) {
1718 free_irq(adapter->rx_scrq[i]->irq,
1719 adapter->rx_scrq[i]);
1720 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
1721 adapter->rx_scrq[i]->irq = 0;
1724 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1727 kfree(adapter->rx_scrq);
1728 adapter->rx_scrq = NULL;
1732 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1733 struct ibmvnic_sub_crq_queue *scrq)
1735 struct device *dev = &adapter->vdev->dev;
1738 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1739 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1741 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1746 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1747 struct ibmvnic_sub_crq_queue *scrq)
1749 struct device *dev = &adapter->vdev->dev;
1752 if (scrq->hw_irq > 0x100000000ULL) {
1753 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1757 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1758 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1760 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1765 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1766 struct ibmvnic_sub_crq_queue *scrq)
1768 struct device *dev = &adapter->vdev->dev;
1769 struct ibmvnic_tx_buff *txbuff;
1770 union sub_crq *next;
1776 while (pending_scrq(adapter, scrq)) {
1777 unsigned int pool = scrq->pool_index;
1779 next = ibmvnic_next_scrq(adapter, scrq);
1780 for (i = 0; i < next->tx_comp.num_comps; i++) {
1781 if (next->tx_comp.rcs[i]) {
1782 dev_err(dev, "tx error %x\n",
1783 next->tx_comp.rcs[i]);
1786 index = be32_to_cpu(next->tx_comp.correlators[i]);
1787 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1789 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1790 if (!txbuff->data_dma[j])
1793 txbuff->data_dma[j] = 0;
1795 /* if sub_crq was sent indirectly */
1796 first = txbuff->indir_arr[0].generic.first;
1797 if (first == IBMVNIC_CRQ_CMD) {
1798 dma_unmap_single(dev, txbuff->indir_dma,
1799 sizeof(txbuff->indir_arr),
1803 if (txbuff->last_frag) {
1804 if (atomic_sub_return(next->tx_comp.num_comps,
1806 (adapter->req_tx_entries_per_subcrq / 2) &&
1807 netif_subqueue_stopped(adapter->netdev,
1809 netif_wake_subqueue(adapter->netdev,
1811 netdev_dbg(adapter->netdev,
1812 "Started queue %d\n",
1816 dev_kfree_skb_any(txbuff->skb);
1819 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1820 producer_index] = index;
1821 adapter->tx_pool[pool].producer_index =
1822 (adapter->tx_pool[pool].producer_index + 1) %
1823 adapter->req_tx_entries_per_subcrq;
1825 /* remove tx_comp scrq*/
1826 next->tx_comp.first = 0;
1829 enable_scrq_irq(adapter, scrq);
1831 if (pending_scrq(adapter, scrq)) {
1832 disable_scrq_irq(adapter, scrq);
1839 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1841 struct ibmvnic_sub_crq_queue *scrq = instance;
1842 struct ibmvnic_adapter *adapter = scrq->adapter;
1844 disable_scrq_irq(adapter, scrq);
1845 ibmvnic_complete_tx(adapter, scrq);
1850 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1852 struct ibmvnic_sub_crq_queue *scrq = instance;
1853 struct ibmvnic_adapter *adapter = scrq->adapter;
1855 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1856 disable_scrq_irq(adapter, scrq);
1857 __napi_schedule(&adapter->napi[scrq->scrq_num]);
1863 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1865 struct device *dev = &adapter->vdev->dev;
1866 struct ibmvnic_sub_crq_queue *scrq;
1870 for (i = 0; i < adapter->req_tx_queues; i++) {
1871 scrq = adapter->tx_scrq[i];
1872 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1876 dev_err(dev, "Error mapping irq\n");
1877 goto req_tx_irq_failed;
1880 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1881 0, "ibmvnic_tx", scrq);
1884 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1886 irq_dispose_mapping(scrq->irq);
1887 goto req_rx_irq_failed;
1891 for (i = 0; i < adapter->req_rx_queues; i++) {
1892 scrq = adapter->rx_scrq[i];
1893 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1896 dev_err(dev, "Error mapping irq\n");
1897 goto req_rx_irq_failed;
1899 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1900 0, "ibmvnic_rx", scrq);
1902 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1904 irq_dispose_mapping(scrq->irq);
1905 goto req_rx_irq_failed;
1911 for (j = 0; j < i; j++) {
1912 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1913 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
1915 i = adapter->req_tx_queues;
1917 for (j = 0; j < i; j++) {
1918 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1919 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
1921 release_sub_crqs(adapter);
1925 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
1927 struct device *dev = &adapter->vdev->dev;
1928 struct ibmvnic_sub_crq_queue **allqueues;
1929 int registered_queues = 0;
1934 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1936 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
1940 for (i = 0; i < total_queues; i++) {
1941 allqueues[i] = init_sub_crq_queue(adapter);
1942 if (!allqueues[i]) {
1943 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1946 registered_queues++;
1949 /* Make sure we were able to register the minimum number of queues */
1950 if (registered_queues <
1951 adapter->min_tx_queues + adapter->min_rx_queues) {
1952 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
1956 /* Distribute the failed allocated queues*/
1957 for (i = 0; i < total_queues - registered_queues + more ; i++) {
1958 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1961 if (adapter->req_rx_queues > adapter->min_rx_queues)
1962 adapter->req_rx_queues--;
1967 if (adapter->req_tx_queues > adapter->min_tx_queues)
1968 adapter->req_tx_queues--;
1975 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1976 sizeof(*adapter->tx_scrq), GFP_KERNEL);
1977 if (!adapter->tx_scrq)
1980 for (i = 0; i < adapter->req_tx_queues; i++) {
1981 adapter->tx_scrq[i] = allqueues[i];
1982 adapter->tx_scrq[i]->pool_index = i;
1985 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1986 sizeof(*adapter->rx_scrq), GFP_KERNEL);
1987 if (!adapter->rx_scrq)
1990 for (i = 0; i < adapter->req_rx_queues; i++) {
1991 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1992 adapter->rx_scrq[i]->scrq_num = i;
1999 kfree(adapter->tx_scrq);
2000 adapter->tx_scrq = NULL;
2002 for (i = 0; i < registered_queues; i++)
2003 release_sub_crq_queue(adapter, allqueues[i]);
2008 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2010 struct device *dev = &adapter->vdev->dev;
2011 union ibmvnic_crq crq;
2014 /* Sub-CRQ entries are 32 byte long */
2015 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2017 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2018 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2019 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2023 /* Get the minimum between the queried max and the entries
2024 * that fit in our PAGE_SIZE
2026 adapter->req_tx_entries_per_subcrq =
2027 adapter->max_tx_entries_per_subcrq > entries_page ?
2028 entries_page : adapter->max_tx_entries_per_subcrq;
2029 adapter->req_rx_add_entries_per_subcrq =
2030 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2031 entries_page : adapter->max_rx_add_entries_per_subcrq;
2033 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2034 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2035 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2037 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2040 memset(&crq, 0, sizeof(crq));
2041 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2042 crq.request_capability.cmd = REQUEST_CAPABILITY;
2044 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2045 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2046 atomic_inc(&adapter->running_cap_crqs);
2047 ibmvnic_send_crq(adapter, &crq);
2049 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2050 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2051 atomic_inc(&adapter->running_cap_crqs);
2052 ibmvnic_send_crq(adapter, &crq);
2054 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2055 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2056 atomic_inc(&adapter->running_cap_crqs);
2057 ibmvnic_send_crq(adapter, &crq);
2059 crq.request_capability.capability =
2060 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2061 crq.request_capability.number =
2062 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2063 atomic_inc(&adapter->running_cap_crqs);
2064 ibmvnic_send_crq(adapter, &crq);
2066 crq.request_capability.capability =
2067 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2068 crq.request_capability.number =
2069 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2070 atomic_inc(&adapter->running_cap_crqs);
2071 ibmvnic_send_crq(adapter, &crq);
2073 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2074 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2075 atomic_inc(&adapter->running_cap_crqs);
2076 ibmvnic_send_crq(adapter, &crq);
2078 if (adapter->netdev->flags & IFF_PROMISC) {
2079 if (adapter->promisc_supported) {
2080 crq.request_capability.capability =
2081 cpu_to_be16(PROMISC_REQUESTED);
2082 crq.request_capability.number = cpu_to_be64(1);
2083 atomic_inc(&adapter->running_cap_crqs);
2084 ibmvnic_send_crq(adapter, &crq);
2087 crq.request_capability.capability =
2088 cpu_to_be16(PROMISC_REQUESTED);
2089 crq.request_capability.number = cpu_to_be64(0);
2090 atomic_inc(&adapter->running_cap_crqs);
2091 ibmvnic_send_crq(adapter, &crq);
2095 static int pending_scrq(struct ibmvnic_adapter *adapter,
2096 struct ibmvnic_sub_crq_queue *scrq)
2098 union sub_crq *entry = &scrq->msgs[scrq->cur];
2100 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
2101 adapter->state == VNIC_CLOSING)
2107 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2108 struct ibmvnic_sub_crq_queue *scrq)
2110 union sub_crq *entry;
2111 unsigned long flags;
2113 spin_lock_irqsave(&scrq->lock, flags);
2114 entry = &scrq->msgs[scrq->cur];
2115 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2116 if (++scrq->cur == scrq->size)
2121 spin_unlock_irqrestore(&scrq->lock, flags);
2126 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2128 struct ibmvnic_crq_queue *queue = &adapter->crq;
2129 union ibmvnic_crq *crq;
2131 crq = &queue->msgs[queue->cur];
2132 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2133 if (++queue->cur == queue->size)
2142 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2143 union sub_crq *sub_crq)
2145 unsigned int ua = adapter->vdev->unit_address;
2146 struct device *dev = &adapter->vdev->dev;
2147 u64 *u64_crq = (u64 *)sub_crq;
2150 netdev_dbg(adapter->netdev,
2151 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2152 (unsigned long int)cpu_to_be64(remote_handle),
2153 (unsigned long int)cpu_to_be64(u64_crq[0]),
2154 (unsigned long int)cpu_to_be64(u64_crq[1]),
2155 (unsigned long int)cpu_to_be64(u64_crq[2]),
2156 (unsigned long int)cpu_to_be64(u64_crq[3]));
2158 /* Make sure the hypervisor sees the complete request */
2161 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2162 cpu_to_be64(remote_handle),
2163 cpu_to_be64(u64_crq[0]),
2164 cpu_to_be64(u64_crq[1]),
2165 cpu_to_be64(u64_crq[2]),
2166 cpu_to_be64(u64_crq[3]));
2170 dev_warn(dev, "CRQ Queue closed\n");
2171 dev_err(dev, "Send error (rc=%d)\n", rc);
2177 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2178 u64 remote_handle, u64 ioba, u64 num_entries)
2180 unsigned int ua = adapter->vdev->unit_address;
2181 struct device *dev = &adapter->vdev->dev;
2184 /* Make sure the hypervisor sees the complete request */
2186 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2187 cpu_to_be64(remote_handle),
2192 dev_warn(dev, "CRQ Queue closed\n");
2193 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2199 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2200 union ibmvnic_crq *crq)
2202 unsigned int ua = adapter->vdev->unit_address;
2203 struct device *dev = &adapter->vdev->dev;
2204 u64 *u64_crq = (u64 *)crq;
2207 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2208 (unsigned long int)cpu_to_be64(u64_crq[0]),
2209 (unsigned long int)cpu_to_be64(u64_crq[1]));
2211 /* Make sure the hypervisor sees the complete request */
2214 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2215 cpu_to_be64(u64_crq[0]),
2216 cpu_to_be64(u64_crq[1]));
2220 dev_warn(dev, "CRQ Queue closed\n");
2221 dev_warn(dev, "Send error (rc=%d)\n", rc);
2227 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2229 union ibmvnic_crq crq;
2231 memset(&crq, 0, sizeof(crq));
2232 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2233 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2234 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2236 return ibmvnic_send_crq(adapter, &crq);
2239 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2241 union ibmvnic_crq crq;
2243 memset(&crq, 0, sizeof(crq));
2244 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2245 crq.version_exchange.cmd = VERSION_EXCHANGE;
2246 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2248 return ibmvnic_send_crq(adapter, &crq);
2251 static void send_login(struct ibmvnic_adapter *adapter)
2253 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2254 struct ibmvnic_login_buffer *login_buffer;
2255 struct device *dev = &adapter->vdev->dev;
2256 dma_addr_t rsp_buffer_token;
2257 dma_addr_t buffer_token;
2258 size_t rsp_buffer_size;
2259 union ibmvnic_crq crq;
2266 sizeof(struct ibmvnic_login_buffer) +
2267 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2269 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2271 goto buf_alloc_failed;
2273 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2275 if (dma_mapping_error(dev, buffer_token)) {
2276 dev_err(dev, "Couldn't map login buffer\n");
2277 goto buf_map_failed;
2280 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2281 sizeof(u64) * adapter->req_tx_queues +
2282 sizeof(u64) * adapter->req_rx_queues +
2283 sizeof(u64) * adapter->req_rx_queues +
2284 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
2286 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2287 if (!login_rsp_buffer)
2288 goto buf_rsp_alloc_failed;
2290 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2291 rsp_buffer_size, DMA_FROM_DEVICE);
2292 if (dma_mapping_error(dev, rsp_buffer_token)) {
2293 dev_err(dev, "Couldn't map login rsp buffer\n");
2294 goto buf_rsp_map_failed;
2297 adapter->login_buf = login_buffer;
2298 adapter->login_buf_token = buffer_token;
2299 adapter->login_buf_sz = buffer_size;
2300 adapter->login_rsp_buf = login_rsp_buffer;
2301 adapter->login_rsp_buf_token = rsp_buffer_token;
2302 adapter->login_rsp_buf_sz = rsp_buffer_size;
2304 login_buffer->len = cpu_to_be32(buffer_size);
2305 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2306 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2307 login_buffer->off_txcomp_subcrqs =
2308 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2309 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2310 login_buffer->off_rxcomp_subcrqs =
2311 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2312 sizeof(u64) * adapter->req_tx_queues);
2313 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2314 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2316 tx_list_p = (__be64 *)((char *)login_buffer +
2317 sizeof(struct ibmvnic_login_buffer));
2318 rx_list_p = (__be64 *)((char *)login_buffer +
2319 sizeof(struct ibmvnic_login_buffer) +
2320 sizeof(u64) * adapter->req_tx_queues);
2322 for (i = 0; i < adapter->req_tx_queues; i++) {
2323 if (adapter->tx_scrq[i]) {
2324 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2329 for (i = 0; i < adapter->req_rx_queues; i++) {
2330 if (adapter->rx_scrq[i]) {
2331 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2336 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2337 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2338 netdev_dbg(adapter->netdev, "%016lx\n",
2339 ((unsigned long int *)(adapter->login_buf))[i]);
2342 memset(&crq, 0, sizeof(crq));
2343 crq.login.first = IBMVNIC_CRQ_CMD;
2344 crq.login.cmd = LOGIN;
2345 crq.login.ioba = cpu_to_be32(buffer_token);
2346 crq.login.len = cpu_to_be32(buffer_size);
2347 ibmvnic_send_crq(adapter, &crq);
2352 kfree(login_rsp_buffer);
2353 buf_rsp_alloc_failed:
2354 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2356 kfree(login_buffer);
2361 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2364 union ibmvnic_crq crq;
2366 memset(&crq, 0, sizeof(crq));
2367 crq.request_map.first = IBMVNIC_CRQ_CMD;
2368 crq.request_map.cmd = REQUEST_MAP;
2369 crq.request_map.map_id = map_id;
2370 crq.request_map.ioba = cpu_to_be32(addr);
2371 crq.request_map.len = cpu_to_be32(len);
2372 ibmvnic_send_crq(adapter, &crq);
2375 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2377 union ibmvnic_crq crq;
2379 memset(&crq, 0, sizeof(crq));
2380 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2381 crq.request_unmap.cmd = REQUEST_UNMAP;
2382 crq.request_unmap.map_id = map_id;
2383 ibmvnic_send_crq(adapter, &crq);
2386 static void send_map_query(struct ibmvnic_adapter *adapter)
2388 union ibmvnic_crq crq;
2390 memset(&crq, 0, sizeof(crq));
2391 crq.query_map.first = IBMVNIC_CRQ_CMD;
2392 crq.query_map.cmd = QUERY_MAP;
2393 ibmvnic_send_crq(adapter, &crq);
2396 /* Send a series of CRQs requesting various capabilities of the VNIC server */
2397 static void send_cap_queries(struct ibmvnic_adapter *adapter)
2399 union ibmvnic_crq crq;
2401 atomic_set(&adapter->running_cap_crqs, 0);
2402 memset(&crq, 0, sizeof(crq));
2403 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2404 crq.query_capability.cmd = QUERY_CAPABILITY;
2406 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
2407 atomic_inc(&adapter->running_cap_crqs);
2408 ibmvnic_send_crq(adapter, &crq);
2410 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
2411 atomic_inc(&adapter->running_cap_crqs);
2412 ibmvnic_send_crq(adapter, &crq);
2414 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
2415 atomic_inc(&adapter->running_cap_crqs);
2416 ibmvnic_send_crq(adapter, &crq);
2418 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
2419 atomic_inc(&adapter->running_cap_crqs);
2420 ibmvnic_send_crq(adapter, &crq);
2422 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
2423 atomic_inc(&adapter->running_cap_crqs);
2424 ibmvnic_send_crq(adapter, &crq);
2426 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
2427 atomic_inc(&adapter->running_cap_crqs);
2428 ibmvnic_send_crq(adapter, &crq);
2430 crq.query_capability.capability =
2431 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
2432 atomic_inc(&adapter->running_cap_crqs);
2433 ibmvnic_send_crq(adapter, &crq);
2435 crq.query_capability.capability =
2436 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
2437 atomic_inc(&adapter->running_cap_crqs);
2438 ibmvnic_send_crq(adapter, &crq);
2440 crq.query_capability.capability =
2441 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
2442 atomic_inc(&adapter->running_cap_crqs);
2443 ibmvnic_send_crq(adapter, &crq);
2445 crq.query_capability.capability =
2446 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
2447 atomic_inc(&adapter->running_cap_crqs);
2448 ibmvnic_send_crq(adapter, &crq);
2450 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
2451 atomic_inc(&adapter->running_cap_crqs);
2452 ibmvnic_send_crq(adapter, &crq);
2454 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
2455 atomic_inc(&adapter->running_cap_crqs);
2456 ibmvnic_send_crq(adapter, &crq);
2458 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
2459 atomic_inc(&adapter->running_cap_crqs);
2460 ibmvnic_send_crq(adapter, &crq);
2462 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
2463 atomic_inc(&adapter->running_cap_crqs);
2464 ibmvnic_send_crq(adapter, &crq);
2466 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
2467 atomic_inc(&adapter->running_cap_crqs);
2468 ibmvnic_send_crq(adapter, &crq);
2470 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
2471 atomic_inc(&adapter->running_cap_crqs);
2472 ibmvnic_send_crq(adapter, &crq);
2474 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2475 atomic_inc(&adapter->running_cap_crqs);
2476 ibmvnic_send_crq(adapter, &crq);
2478 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
2479 atomic_inc(&adapter->running_cap_crqs);
2480 ibmvnic_send_crq(adapter, &crq);
2482 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
2483 atomic_inc(&adapter->running_cap_crqs);
2484 ibmvnic_send_crq(adapter, &crq);
2486 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
2487 atomic_inc(&adapter->running_cap_crqs);
2488 ibmvnic_send_crq(adapter, &crq);
2490 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
2491 atomic_inc(&adapter->running_cap_crqs);
2492 ibmvnic_send_crq(adapter, &crq);
2494 crq.query_capability.capability =
2495 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
2496 atomic_inc(&adapter->running_cap_crqs);
2497 ibmvnic_send_crq(adapter, &crq);
2499 crq.query_capability.capability =
2500 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
2501 atomic_inc(&adapter->running_cap_crqs);
2502 ibmvnic_send_crq(adapter, &crq);
2504 crq.query_capability.capability =
2505 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
2506 atomic_inc(&adapter->running_cap_crqs);
2507 ibmvnic_send_crq(adapter, &crq);
2509 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
2510 atomic_inc(&adapter->running_cap_crqs);
2511 ibmvnic_send_crq(adapter, &crq);
2514 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2516 struct device *dev = &adapter->vdev->dev;
2517 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2518 union ibmvnic_crq crq;
2521 dma_unmap_single(dev, adapter->ip_offload_tok,
2522 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2524 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2525 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2526 netdev_dbg(adapter->netdev, "%016lx\n",
2527 ((unsigned long int *)(buf))[i]);
2529 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2530 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2531 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2532 buf->tcp_ipv4_chksum);
2533 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2534 buf->tcp_ipv6_chksum);
2535 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2536 buf->udp_ipv4_chksum);
2537 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2538 buf->udp_ipv6_chksum);
2539 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2540 buf->large_tx_ipv4);
2541 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2542 buf->large_tx_ipv6);
2543 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2544 buf->large_rx_ipv4);
2545 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2546 buf->large_rx_ipv6);
2547 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2548 buf->max_ipv4_header_size);
2549 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2550 buf->max_ipv6_header_size);
2551 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2552 buf->max_tcp_header_size);
2553 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2554 buf->max_udp_header_size);
2555 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2556 buf->max_large_tx_size);
2557 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2558 buf->max_large_rx_size);
2559 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2560 buf->ipv6_extension_header);
2561 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2562 buf->tcp_pseudosum_req);
2563 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2564 buf->num_ipv6_ext_headers);
2565 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2566 buf->off_ipv6_ext_headers);
2568 adapter->ip_offload_ctrl_tok =
2569 dma_map_single(dev, &adapter->ip_offload_ctrl,
2570 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2572 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2573 dev_err(dev, "Couldn't map ip offload control buffer\n");
2577 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2578 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2579 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2580 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2581 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2583 /* large_tx/rx disabled for now, additional features needed */
2584 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2585 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2586 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2587 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2589 adapter->netdev->features = NETIF_F_GSO;
2591 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2592 adapter->netdev->features |= NETIF_F_IP_CSUM;
2594 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2595 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2597 if ((adapter->netdev->features &
2598 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2599 adapter->netdev->features |= NETIF_F_RXCSUM;
2601 memset(&crq, 0, sizeof(crq));
2602 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2603 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2604 crq.control_ip_offload.len =
2605 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2606 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2607 ibmvnic_send_crq(adapter, &crq);
2610 static void handle_error_info_rsp(union ibmvnic_crq *crq,
2611 struct ibmvnic_adapter *adapter)
2613 struct device *dev = &adapter->vdev->dev;
2614 struct ibmvnic_error_buff *error_buff, *tmp;
2615 unsigned long flags;
2619 if (!crq->request_error_rsp.rc.code) {
2620 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2621 crq->request_error_rsp.rc.code);
2625 spin_lock_irqsave(&adapter->error_list_lock, flags);
2626 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
2627 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2629 list_del(&error_buff->list);
2632 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2635 dev_err(dev, "Couldn't find error id %x\n",
2636 be32_to_cpu(crq->request_error_rsp.error_id));
2640 dev_err(dev, "Detailed info for error id %x:",
2641 be32_to_cpu(crq->request_error_rsp.error_id));
2643 for (i = 0; i < error_buff->len; i++) {
2644 pr_cont("%02x", (int)error_buff->buff[i]);
2650 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2652 kfree(error_buff->buff);
2656 static void request_error_information(struct ibmvnic_adapter *adapter,
2657 union ibmvnic_crq *err_crq)
2659 struct device *dev = &adapter->vdev->dev;
2660 struct net_device *netdev = adapter->netdev;
2661 struct ibmvnic_error_buff *error_buff;
2662 unsigned long timeout = msecs_to_jiffies(30000);
2663 union ibmvnic_crq crq;
2664 unsigned long flags;
2667 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2671 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
2672 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2673 if (!error_buff->buff) {
2678 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2680 if (dma_mapping_error(dev, error_buff->dma)) {
2681 netdev_err(netdev, "Couldn't map error buffer\n");
2682 kfree(error_buff->buff);
2687 error_buff->len = detail_len;
2688 error_buff->error_id = err_crq->error_indication.error_id;
2690 spin_lock_irqsave(&adapter->error_list_lock, flags);
2691 list_add_tail(&error_buff->list, &adapter->errors);
2692 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2694 memset(&crq, 0, sizeof(crq));
2695 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2696 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2697 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2698 crq.request_error_info.len = cpu_to_be32(detail_len);
2699 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2701 rc = ibmvnic_send_crq(adapter, &crq);
2703 netdev_err(netdev, "failed to request error information\n");
2707 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2708 netdev_err(netdev, "timeout waiting for error information\n");
2715 spin_lock_irqsave(&adapter->error_list_lock, flags);
2716 list_del(&error_buff->list);
2717 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2719 kfree(error_buff->buff);
2723 static void handle_error_indication(union ibmvnic_crq *crq,
2724 struct ibmvnic_adapter *adapter)
2726 struct device *dev = &adapter->vdev->dev;
2728 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2729 crq->error_indication.flags
2730 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2731 be32_to_cpu(crq->error_indication.error_id),
2732 be16_to_cpu(crq->error_indication.error_cause));
2734 if (be32_to_cpu(crq->error_indication.error_id))
2735 request_error_information(adapter, crq);
2737 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2738 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2741 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2742 struct ibmvnic_adapter *adapter)
2744 struct net_device *netdev = adapter->netdev;
2745 struct device *dev = &adapter->vdev->dev;
2748 rc = crq->change_mac_addr_rsp.rc.code;
2750 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2753 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2757 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2758 struct ibmvnic_adapter *adapter)
2760 struct device *dev = &adapter->vdev->dev;
2764 atomic_dec(&adapter->running_cap_crqs);
2765 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2767 req_value = &adapter->req_tx_queues;
2771 req_value = &adapter->req_rx_queues;
2774 case REQ_RX_ADD_QUEUES:
2775 req_value = &adapter->req_rx_add_queues;
2778 case REQ_TX_ENTRIES_PER_SUBCRQ:
2779 req_value = &adapter->req_tx_entries_per_subcrq;
2780 name = "tx_entries_per_subcrq";
2782 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2783 req_value = &adapter->req_rx_add_entries_per_subcrq;
2784 name = "rx_add_entries_per_subcrq";
2787 req_value = &adapter->req_mtu;
2790 case PROMISC_REQUESTED:
2791 req_value = &adapter->promisc;
2795 dev_err(dev, "Got invalid cap request rsp %d\n",
2796 crq->request_capability.capability);
2800 switch (crq->request_capability_rsp.rc.code) {
2803 case PARTIALSUCCESS:
2804 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2806 (long int)be64_to_cpu(crq->request_capability_rsp.
2808 release_sub_crqs(adapter);
2809 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
2810 ibmvnic_send_req_caps(adapter, 1);
2813 dev_err(dev, "Error %d in request cap rsp\n",
2814 crq->request_capability_rsp.rc.code);
2818 /* Done receiving requested capabilities, query IP offload support */
2819 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2820 union ibmvnic_crq newcrq;
2821 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2822 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2823 &adapter->ip_offload_buf;
2825 adapter->wait_capability = false;
2826 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2830 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2831 if (!firmware_has_feature(FW_FEATURE_CMO))
2832 dev_err(dev, "Couldn't map offload buffer\n");
2836 memset(&newcrq, 0, sizeof(newcrq));
2837 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2838 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2839 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2840 newcrq.query_ip_offload.ioba =
2841 cpu_to_be32(adapter->ip_offload_tok);
2843 ibmvnic_send_crq(adapter, &newcrq);
2847 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2848 struct ibmvnic_adapter *adapter)
2850 struct device *dev = &adapter->vdev->dev;
2851 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2852 struct ibmvnic_login_buffer *login = adapter->login_buf;
2855 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2857 dma_unmap_single(dev, adapter->login_rsp_buf_token,
2858 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2860 /* If the number of queues requested can't be allocated by the
2861 * server, the login response will return with code 1. We will need
2862 * to resend the login buffer with fewer queues requested.
2864 if (login_rsp_crq->generic.rc.code) {
2865 adapter->renegotiate = true;
2866 complete(&adapter->init_done);
2870 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2871 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2872 netdev_dbg(adapter->netdev, "%016lx\n",
2873 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2877 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2878 (be32_to_cpu(login->num_rxcomp_subcrqs) *
2879 adapter->req_rx_add_queues !=
2880 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2881 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2882 ibmvnic_remove(adapter->vdev);
2885 complete(&adapter->init_done);
2890 static void handle_request_map_rsp(union ibmvnic_crq *crq,
2891 struct ibmvnic_adapter *adapter)
2893 struct device *dev = &adapter->vdev->dev;
2894 u8 map_id = crq->request_map_rsp.map_id;
2900 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2901 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2903 rc = crq->request_map_rsp.rc.code;
2905 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2907 /* need to find and zero tx/rx_pool map_id */
2908 for (i = 0; i < tx_subcrqs; i++) {
2909 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2910 adapter->tx_pool[i].long_term_buff.map_id = 0;
2912 for (i = 0; i < rx_subcrqs; i++) {
2913 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2914 adapter->rx_pool[i].long_term_buff.map_id = 0;
2917 complete(&adapter->fw_done);
2920 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2921 struct ibmvnic_adapter *adapter)
2923 struct device *dev = &adapter->vdev->dev;
2926 rc = crq->request_unmap_rsp.rc.code;
2928 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2931 static void handle_query_map_rsp(union ibmvnic_crq *crq,
2932 struct ibmvnic_adapter *adapter)
2934 struct net_device *netdev = adapter->netdev;
2935 struct device *dev = &adapter->vdev->dev;
2938 rc = crq->query_map_rsp.rc.code;
2940 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2943 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2944 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2945 crq->query_map_rsp.free_pages);
2948 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2949 struct ibmvnic_adapter *adapter)
2951 struct net_device *netdev = adapter->netdev;
2952 struct device *dev = &adapter->vdev->dev;
2955 atomic_dec(&adapter->running_cap_crqs);
2956 netdev_dbg(netdev, "Outstanding queries: %d\n",
2957 atomic_read(&adapter->running_cap_crqs));
2958 rc = crq->query_capability.rc.code;
2960 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2964 switch (be16_to_cpu(crq->query_capability.capability)) {
2966 adapter->min_tx_queues =
2967 be64_to_cpu(crq->query_capability.number);
2968 netdev_dbg(netdev, "min_tx_queues = %lld\n",
2969 adapter->min_tx_queues);
2972 adapter->min_rx_queues =
2973 be64_to_cpu(crq->query_capability.number);
2974 netdev_dbg(netdev, "min_rx_queues = %lld\n",
2975 adapter->min_rx_queues);
2977 case MIN_RX_ADD_QUEUES:
2978 adapter->min_rx_add_queues =
2979 be64_to_cpu(crq->query_capability.number);
2980 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2981 adapter->min_rx_add_queues);
2984 adapter->max_tx_queues =
2985 be64_to_cpu(crq->query_capability.number);
2986 netdev_dbg(netdev, "max_tx_queues = %lld\n",
2987 adapter->max_tx_queues);
2990 adapter->max_rx_queues =
2991 be64_to_cpu(crq->query_capability.number);
2992 netdev_dbg(netdev, "max_rx_queues = %lld\n",
2993 adapter->max_rx_queues);
2995 case MAX_RX_ADD_QUEUES:
2996 adapter->max_rx_add_queues =
2997 be64_to_cpu(crq->query_capability.number);
2998 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2999 adapter->max_rx_add_queues);
3001 case MIN_TX_ENTRIES_PER_SUBCRQ:
3002 adapter->min_tx_entries_per_subcrq =
3003 be64_to_cpu(crq->query_capability.number);
3004 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3005 adapter->min_tx_entries_per_subcrq);
3007 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3008 adapter->min_rx_add_entries_per_subcrq =
3009 be64_to_cpu(crq->query_capability.number);
3010 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3011 adapter->min_rx_add_entries_per_subcrq);
3013 case MAX_TX_ENTRIES_PER_SUBCRQ:
3014 adapter->max_tx_entries_per_subcrq =
3015 be64_to_cpu(crq->query_capability.number);
3016 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3017 adapter->max_tx_entries_per_subcrq);
3019 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3020 adapter->max_rx_add_entries_per_subcrq =
3021 be64_to_cpu(crq->query_capability.number);
3022 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3023 adapter->max_rx_add_entries_per_subcrq);
3025 case TCP_IP_OFFLOAD:
3026 adapter->tcp_ip_offload =
3027 be64_to_cpu(crq->query_capability.number);
3028 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3029 adapter->tcp_ip_offload);
3031 case PROMISC_SUPPORTED:
3032 adapter->promisc_supported =
3033 be64_to_cpu(crq->query_capability.number);
3034 netdev_dbg(netdev, "promisc_supported = %lld\n",
3035 adapter->promisc_supported);
3038 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3039 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3040 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3043 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3044 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3045 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3047 case MAX_MULTICAST_FILTERS:
3048 adapter->max_multicast_filters =
3049 be64_to_cpu(crq->query_capability.number);
3050 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3051 adapter->max_multicast_filters);
3053 case VLAN_HEADER_INSERTION:
3054 adapter->vlan_header_insertion =
3055 be64_to_cpu(crq->query_capability.number);
3056 if (adapter->vlan_header_insertion)
3057 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3058 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3059 adapter->vlan_header_insertion);
3061 case RX_VLAN_HEADER_INSERTION:
3062 adapter->rx_vlan_header_insertion =
3063 be64_to_cpu(crq->query_capability.number);
3064 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3065 adapter->rx_vlan_header_insertion);
3067 case MAX_TX_SG_ENTRIES:
3068 adapter->max_tx_sg_entries =
3069 be64_to_cpu(crq->query_capability.number);
3070 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3071 adapter->max_tx_sg_entries);
3073 case RX_SG_SUPPORTED:
3074 adapter->rx_sg_supported =
3075 be64_to_cpu(crq->query_capability.number);
3076 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3077 adapter->rx_sg_supported);
3079 case OPT_TX_COMP_SUB_QUEUES:
3080 adapter->opt_tx_comp_sub_queues =
3081 be64_to_cpu(crq->query_capability.number);
3082 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3083 adapter->opt_tx_comp_sub_queues);
3085 case OPT_RX_COMP_QUEUES:
3086 adapter->opt_rx_comp_queues =
3087 be64_to_cpu(crq->query_capability.number);
3088 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3089 adapter->opt_rx_comp_queues);
3091 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3092 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3093 be64_to_cpu(crq->query_capability.number);
3094 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3095 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3097 case OPT_TX_ENTRIES_PER_SUBCRQ:
3098 adapter->opt_tx_entries_per_subcrq =
3099 be64_to_cpu(crq->query_capability.number);
3100 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3101 adapter->opt_tx_entries_per_subcrq);
3103 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3104 adapter->opt_rxba_entries_per_subcrq =
3105 be64_to_cpu(crq->query_capability.number);
3106 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3107 adapter->opt_rxba_entries_per_subcrq);
3109 case TX_RX_DESC_REQ:
3110 adapter->tx_rx_desc_req = crq->query_capability.number;
3111 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3112 adapter->tx_rx_desc_req);
3116 netdev_err(netdev, "Got invalid cap rsp %d\n",
3117 crq->query_capability.capability);
3121 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3122 adapter->wait_capability = false;
3123 ibmvnic_send_req_caps(adapter, 0);
3127 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3128 struct ibmvnic_adapter *adapter)
3130 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3131 struct net_device *netdev = adapter->netdev;
3132 struct device *dev = &adapter->vdev->dev;
3133 u64 *u64_crq = (u64 *)crq;
3136 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3137 (unsigned long int)cpu_to_be64(u64_crq[0]),
3138 (unsigned long int)cpu_to_be64(u64_crq[1]));
3139 switch (gen_crq->first) {
3140 case IBMVNIC_CRQ_INIT_RSP:
3141 switch (gen_crq->cmd) {
3142 case IBMVNIC_CRQ_INIT:
3143 dev_info(dev, "Partner initialized\n");
3145 case IBMVNIC_CRQ_INIT_COMPLETE:
3146 dev_info(dev, "Partner initialization complete\n");
3147 send_version_xchg(adapter);
3150 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3153 case IBMVNIC_CRQ_XPORT_EVENT:
3154 netif_carrier_off(netdev);
3155 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3156 dev_info(dev, "Migrated, re-enabling adapter\n");
3157 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
3158 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3159 dev_info(dev, "Backing device failover detected\n");
3160 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
3162 /* The adapter lost the connection */
3163 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3165 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3168 case IBMVNIC_CRQ_CMD_RSP:
3171 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3176 switch (gen_crq->cmd) {
3177 case VERSION_EXCHANGE_RSP:
3178 rc = crq->version_exchange_rsp.rc.code;
3180 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3183 dev_info(dev, "Partner protocol version is %d\n",
3184 crq->version_exchange_rsp.version);
3185 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3188 be16_to_cpu(crq->version_exchange_rsp.version);
3189 send_cap_queries(adapter);
3191 case QUERY_CAPABILITY_RSP:
3192 handle_query_cap_rsp(crq, adapter);
3195 handle_query_map_rsp(crq, adapter);
3197 case REQUEST_MAP_RSP:
3198 handle_request_map_rsp(crq, adapter);
3200 case REQUEST_UNMAP_RSP:
3201 handle_request_unmap_rsp(crq, adapter);
3203 case REQUEST_CAPABILITY_RSP:
3204 handle_request_cap_rsp(crq, adapter);
3207 netdev_dbg(netdev, "Got Login Response\n");
3208 handle_login_rsp(crq, adapter);
3210 case LOGICAL_LINK_STATE_RSP:
3212 "Got Logical Link State Response, state: %d rc: %d\n",
3213 crq->logical_link_state_rsp.link_state,
3214 crq->logical_link_state_rsp.rc.code);
3215 adapter->logical_link_state =
3216 crq->logical_link_state_rsp.link_state;
3217 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3218 complete(&adapter->init_done);
3220 case LINK_STATE_INDICATION:
3221 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3222 adapter->phys_link_state =
3223 crq->link_state_indication.phys_link_state;
3224 adapter->logical_link_state =
3225 crq->link_state_indication.logical_link_state;
3227 case CHANGE_MAC_ADDR_RSP:
3228 netdev_dbg(netdev, "Got MAC address change Response\n");
3229 handle_change_mac_rsp(crq, adapter);
3231 case ERROR_INDICATION:
3232 netdev_dbg(netdev, "Got Error Indication\n");
3233 handle_error_indication(crq, adapter);
3235 case REQUEST_ERROR_RSP:
3236 netdev_dbg(netdev, "Got Error Detail Response\n");
3237 handle_error_info_rsp(crq, adapter);
3239 case REQUEST_STATISTICS_RSP:
3240 netdev_dbg(netdev, "Got Statistics Response\n");
3241 complete(&adapter->stats_done);
3243 case QUERY_IP_OFFLOAD_RSP:
3244 netdev_dbg(netdev, "Got Query IP offload Response\n");
3245 handle_query_ip_offload_rsp(adapter);
3247 case MULTICAST_CTRL_RSP:
3248 netdev_dbg(netdev, "Got multicast control Response\n");
3250 case CONTROL_IP_OFFLOAD_RSP:
3251 netdev_dbg(netdev, "Got Control IP offload Response\n");
3252 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3253 sizeof(adapter->ip_offload_ctrl),
3255 complete(&adapter->init_done);
3257 case COLLECT_FW_TRACE_RSP:
3258 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3259 complete(&adapter->fw_done);
3262 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3267 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3269 struct ibmvnic_adapter *adapter = instance;
3271 tasklet_schedule(&adapter->tasklet);
3275 static void ibmvnic_tasklet(void *data)
3277 struct ibmvnic_adapter *adapter = data;
3278 struct ibmvnic_crq_queue *queue = &adapter->crq;
3279 union ibmvnic_crq *crq;
3280 unsigned long flags;
3283 spin_lock_irqsave(&queue->lock, flags);
3285 /* Pull all the valid messages off the CRQ */
3286 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3287 ibmvnic_handle_crq(crq, adapter);
3288 crq->generic.first = 0;
3291 /* remain in tasklet until all
3292 * capabilities responses are received
3294 if (!adapter->wait_capability)
3297 /* if capabilities CRQ's were sent in this tasklet, the following
3298 * tasklet must wait until all responses are received
3300 if (atomic_read(&adapter->running_cap_crqs) != 0)
3301 adapter->wait_capability = true;
3302 spin_unlock_irqrestore(&queue->lock, flags);
3305 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3307 struct vio_dev *vdev = adapter->vdev;
3311 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3312 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3315 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3320 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3322 struct ibmvnic_crq_queue *crq = &adapter->crq;
3323 struct device *dev = &adapter->vdev->dev;
3324 struct vio_dev *vdev = adapter->vdev;
3329 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3330 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3332 /* Clean out the queue */
3333 memset(crq->msgs, 0, PAGE_SIZE);
3336 /* And re-open it again */
3337 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3338 crq->msg_token, PAGE_SIZE);
3341 /* Adapter is good, but other end is not ready */
3342 dev_warn(dev, "Partner adapter not ready\n");
3344 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3349 static void release_crq_queue(struct ibmvnic_adapter *adapter)
3351 struct ibmvnic_crq_queue *crq = &adapter->crq;
3352 struct vio_dev *vdev = adapter->vdev;
3358 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3359 free_irq(vdev->irq, adapter);
3360 tasklet_kill(&adapter->tasklet);
3362 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3363 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3365 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3367 free_page((unsigned long)crq->msgs);
3371 static int init_crq_queue(struct ibmvnic_adapter *adapter)
3373 struct ibmvnic_crq_queue *crq = &adapter->crq;
3374 struct device *dev = &adapter->vdev->dev;
3375 struct vio_dev *vdev = adapter->vdev;
3376 int rc, retrc = -ENOMEM;
3381 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3382 /* Should we allocate more than one page? */
3387 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3388 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3390 if (dma_mapping_error(dev, crq->msg_token))
3393 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3394 crq->msg_token, PAGE_SIZE);
3396 if (rc == H_RESOURCE)
3397 /* maybe kexecing and resource is busy. try a reset */
3398 rc = ibmvnic_reset_crq(adapter);
3401 if (rc == H_CLOSED) {
3402 dev_warn(dev, "Partner adapter not ready\n");
3404 dev_warn(dev, "Error %d opening adapter\n", rc);
3405 goto reg_crq_failed;
3410 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3411 (unsigned long)adapter);
3413 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3414 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3417 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3419 goto req_irq_failed;
3422 rc = vio_enable_interrupts(vdev);
3424 dev_err(dev, "Error %d enabling interrupts\n", rc);
3425 goto req_irq_failed;
3429 spin_lock_init(&crq->lock);
3434 tasklet_kill(&adapter->tasklet);
3436 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3437 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3439 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3441 free_page((unsigned long)crq->msgs);
3446 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3448 struct device *dev = &adapter->vdev->dev;
3449 unsigned long timeout = msecs_to_jiffies(30000);
3452 rc = init_crq_queue(adapter);
3454 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3458 init_completion(&adapter->init_done);
3459 ibmvnic_send_crq_init(adapter);
3460 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3461 dev_err(dev, "Initialization sequence timed out\n");
3462 release_crq_queue(adapter);
3466 rc = init_sub_crqs(adapter);
3468 dev_err(dev, "Initialization of sub crqs failed\n");
3469 release_crq_queue(adapter);
3475 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3477 struct ibmvnic_adapter *adapter;
3478 struct net_device *netdev;
3479 unsigned char *mac_addr_p;
3482 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3485 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3486 VETH_MAC_ADDR, NULL);
3489 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3490 __FILE__, __LINE__);
3494 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3495 IBMVNIC_MAX_TX_QUEUES);
3499 adapter = netdev_priv(netdev);
3500 adapter->state = VNIC_PROBING;
3501 dev_set_drvdata(&dev->dev, netdev);
3502 adapter->vdev = dev;
3503 adapter->netdev = netdev;
3505 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3506 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3507 netdev->irq = dev->irq;
3508 netdev->netdev_ops = &ibmvnic_netdev_ops;
3509 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3510 SET_NETDEV_DEV(netdev, &dev->dev);
3512 spin_lock_init(&adapter->stats_lock);
3514 INIT_LIST_HEAD(&adapter->errors);
3515 spin_lock_init(&adapter->error_list_lock);
3517 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3518 INIT_LIST_HEAD(&adapter->rwi_list);
3519 mutex_init(&adapter->reset_lock);
3520 mutex_init(&adapter->rwi_lock);
3521 adapter->resetting = false;
3523 rc = ibmvnic_init(adapter);
3525 free_netdev(netdev);
3529 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3531 rc = register_netdev(netdev);
3533 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
3534 free_netdev(netdev);
3537 dev_info(&dev->dev, "ibmvnic registered\n");
3539 adapter->state = VNIC_PROBED;
3543 static int ibmvnic_remove(struct vio_dev *dev)
3545 struct net_device *netdev = dev_get_drvdata(&dev->dev);
3546 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3548 adapter->state = VNIC_REMOVING;
3549 unregister_netdev(netdev);
3550 mutex_lock(&adapter->reset_lock);
3552 release_resources(adapter);
3553 release_sub_crqs(adapter);
3554 release_crq_queue(adapter);
3556 adapter->state = VNIC_REMOVED;
3558 mutex_unlock(&adapter->reset_lock);
3559 free_netdev(netdev);
3560 dev_set_drvdata(&dev->dev, NULL);
3565 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3567 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3568 struct ibmvnic_adapter *adapter;
3569 struct iommu_table *tbl;
3570 unsigned long ret = 0;
3573 tbl = get_iommu_table_base(&vdev->dev);
3575 /* netdev inits at probe time along with the structures we need below*/
3577 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3579 adapter = netdev_priv(netdev);
3581 ret += PAGE_SIZE; /* the crq message queue */
3582 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3584 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3585 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3587 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3589 ret += adapter->rx_pool[i].size *
3590 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3595 static int ibmvnic_resume(struct device *dev)
3597 struct net_device *netdev = dev_get_drvdata(dev);
3598 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3601 /* kick the interrupt handlers just in case we lost an interrupt */
3602 for (i = 0; i < adapter->req_rx_queues; i++)
3603 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3604 adapter->rx_scrq[i]);
3609 static struct vio_device_id ibmvnic_device_table[] = {
3610 {"network", "IBM,vnic"},
3613 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3615 static const struct dev_pm_ops ibmvnic_pm_ops = {
3616 .resume = ibmvnic_resume
3619 static struct vio_driver ibmvnic_driver = {
3620 .id_table = ibmvnic_device_table,
3621 .probe = ibmvnic_probe,
3622 .remove = ibmvnic_remove,
3623 .get_desired_dma = ibmvnic_get_desired_dma,
3624 .name = ibmvnic_driver_name,
3625 .pm = &ibmvnic_pm_ops,
3628 /* module functions */
3629 static int __init ibmvnic_module_init(void)
3631 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3632 IBMVNIC_DRIVER_VERSION);
3634 return vio_register_driver(&ibmvnic_driver);
3637 static void __exit ibmvnic_module_exit(void)
3639 vio_unregister_driver(&ibmvnic_driver);
3642 module_init(ibmvnic_module_init);
3643 module_exit(ibmvnic_module_exit);