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");
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 void reset_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb)
169 memset(ltb->buff, 0, ltb->size);
171 init_completion(&adapter->fw_done);
172 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
173 wait_for_completion(&adapter->fw_done);
176 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
177 struct ibmvnic_long_term_buff *ltb, int size)
179 struct device *dev = &adapter->vdev->dev;
182 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
186 dev_err(dev, "Couldn't alloc long term buffer\n");
189 ltb->map_id = adapter->map_id;
192 init_completion(&adapter->fw_done);
193 send_request_map(adapter, ltb->addr,
194 ltb->size, ltb->map_id);
195 wait_for_completion(&adapter->fw_done);
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
202 struct device *dev = &adapter->vdev->dev;
207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
209 send_request_unmap(adapter, ltb->map_id);
210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
217 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
219 adapter->rx_pool[i].active = 0;
222 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
223 struct ibmvnic_rx_pool *pool)
225 int count = pool->size - atomic_read(&pool->available);
226 struct device *dev = &adapter->vdev->dev;
227 int buffers_added = 0;
228 unsigned long lpar_rc;
229 union sub_crq sub_crq;
242 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
243 be32_to_cpu(adapter->login_rsp_buf->
246 for (i = 0; i < count; ++i) {
247 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
249 dev_err(dev, "Couldn't replenish rx buff\n");
250 adapter->replenish_no_mem++;
254 index = pool->free_map[pool->next_free];
256 if (pool->rx_buff[index].skb)
257 dev_err(dev, "Inconsistent free_map!\n");
259 /* Copy the skb to the long term mapped DMA buffer */
260 offset = index * pool->buff_size;
261 dst = pool->long_term_buff.buff + offset;
262 memset(dst, 0, pool->buff_size);
263 dma_addr = pool->long_term_buff.addr + offset;
264 pool->rx_buff[index].data = dst;
266 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
267 pool->rx_buff[index].dma = dma_addr;
268 pool->rx_buff[index].skb = skb;
269 pool->rx_buff[index].pool_index = pool->index;
270 pool->rx_buff[index].size = pool->buff_size;
272 memset(&sub_crq, 0, sizeof(sub_crq));
273 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
274 sub_crq.rx_add.correlator =
275 cpu_to_be64((u64)&pool->rx_buff[index]);
276 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
277 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
279 /* The length field of the sCRQ is defined to be 24 bits so the
280 * buffer size needs to be left shifted by a byte before it is
281 * converted to big endian to prevent the last byte from being
284 #ifdef __LITTLE_ENDIAN__
287 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
289 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
291 if (lpar_rc != H_SUCCESS)
295 adapter->replenish_add_buff_success++;
296 pool->next_free = (pool->next_free + 1) % pool->size;
298 atomic_add(buffers_added, &pool->available);
302 dev_info(dev, "replenish pools failure\n");
303 pool->free_map[pool->next_free] = index;
304 pool->rx_buff[index].skb = NULL;
305 if (!dma_mapping_error(dev, dma_addr))
306 dma_unmap_single(dev, dma_addr, pool->buff_size,
309 dev_kfree_skb_any(skb);
310 adapter->replenish_add_buff_failure++;
311 atomic_add(buffers_added, &pool->available);
313 if (lpar_rc == H_CLOSED) {
314 /* Disable buffer pool replenishment and report carrier off if
315 * queue is closed. Firmware guarantees that a signal will
316 * be sent to the driver, triggering a reset.
318 deactivate_rx_pools(adapter);
319 netif_carrier_off(adapter->netdev);
323 static void replenish_pools(struct ibmvnic_adapter *adapter)
327 adapter->replenish_task_cycles++;
328 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
330 if (adapter->rx_pool[i].active)
331 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
335 static void release_stats_token(struct ibmvnic_adapter *adapter)
337 struct device *dev = &adapter->vdev->dev;
339 if (!adapter->stats_token)
342 dma_unmap_single(dev, adapter->stats_token,
343 sizeof(struct ibmvnic_statistics),
345 adapter->stats_token = 0;
348 static int init_stats_token(struct ibmvnic_adapter *adapter)
350 struct device *dev = &adapter->vdev->dev;
353 stok = dma_map_single(dev, &adapter->stats,
354 sizeof(struct ibmvnic_statistics),
356 if (dma_mapping_error(dev, stok)) {
357 dev_err(dev, "Couldn't map stats buffer\n");
361 adapter->stats_token = stok;
365 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
367 struct ibmvnic_rx_pool *rx_pool;
371 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
372 for (i = 0; i < rx_scrqs; i++) {
373 rx_pool = &adapter->rx_pool[i];
375 reset_long_term_buff(adapter, &rx_pool->long_term_buff);
377 for (j = 0; j < rx_pool->size; j++)
378 rx_pool->free_map[j] = j;
380 memset(rx_pool->rx_buff, 0,
381 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
383 atomic_set(&rx_pool->available, 0);
384 rx_pool->next_alloc = 0;
385 rx_pool->next_free = 0;
392 static void release_rx_pools(struct ibmvnic_adapter *adapter)
394 struct ibmvnic_rx_pool *rx_pool;
398 if (!adapter->rx_pool)
401 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
402 for (i = 0; i < rx_scrqs; i++) {
403 rx_pool = &adapter->rx_pool[i];
405 kfree(rx_pool->free_map);
406 free_long_term_buff(adapter, &rx_pool->long_term_buff);
408 if (!rx_pool->rx_buff)
411 for (j = 0; j < rx_pool->size; j++) {
412 if (rx_pool->rx_buff[j].skb) {
413 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
414 rx_pool->rx_buff[i].skb = NULL;
418 kfree(rx_pool->rx_buff);
421 kfree(adapter->rx_pool);
422 adapter->rx_pool = NULL;
425 static int init_rx_pools(struct net_device *netdev)
427 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
428 struct device *dev = &adapter->vdev->dev;
429 struct ibmvnic_rx_pool *rx_pool;
435 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
436 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
437 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
439 adapter->rx_pool = kcalloc(rxadd_subcrqs,
440 sizeof(struct ibmvnic_rx_pool),
442 if (!adapter->rx_pool) {
443 dev_err(dev, "Failed to allocate rx pools\n");
447 for (i = 0; i < rxadd_subcrqs; i++) {
448 rx_pool = &adapter->rx_pool[i];
450 netdev_dbg(adapter->netdev,
451 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
452 i, adapter->req_rx_add_entries_per_subcrq,
453 be64_to_cpu(size_array[i]));
455 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
457 rx_pool->buff_size = be64_to_cpu(size_array[i]);
460 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
462 if (!rx_pool->free_map) {
463 release_rx_pools(adapter);
467 rx_pool->rx_buff = kcalloc(rx_pool->size,
468 sizeof(struct ibmvnic_rx_buff),
470 if (!rx_pool->rx_buff) {
471 dev_err(dev, "Couldn't alloc rx buffers\n");
472 release_rx_pools(adapter);
476 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
477 rx_pool->size * rx_pool->buff_size)) {
478 release_rx_pools(adapter);
482 for (j = 0; j < rx_pool->size; ++j)
483 rx_pool->free_map[j] = j;
485 atomic_set(&rx_pool->available, 0);
486 rx_pool->next_alloc = 0;
487 rx_pool->next_free = 0;
493 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
495 struct ibmvnic_tx_pool *tx_pool;
499 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
500 for (i = 0; i < tx_scrqs; i++) {
501 tx_pool = &adapter->tx_pool[i];
503 reset_long_term_buff(adapter, &tx_pool->long_term_buff);
505 memset(tx_pool->tx_buff, 0,
506 adapter->req_tx_entries_per_subcrq *
507 sizeof(struct ibmvnic_tx_buff));
509 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
510 tx_pool->free_map[j] = j;
512 tx_pool->consumer_index = 0;
513 tx_pool->producer_index = 0;
519 static void release_tx_pools(struct ibmvnic_adapter *adapter)
521 struct ibmvnic_tx_pool *tx_pool;
524 if (!adapter->tx_pool)
527 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
528 for (i = 0; i < tx_scrqs; i++) {
529 tx_pool = &adapter->tx_pool[i];
530 kfree(tx_pool->tx_buff);
531 free_long_term_buff(adapter, &tx_pool->long_term_buff);
532 kfree(tx_pool->free_map);
535 kfree(adapter->tx_pool);
536 adapter->tx_pool = NULL;
539 static int init_tx_pools(struct net_device *netdev)
541 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
542 struct device *dev = &adapter->vdev->dev;
543 struct ibmvnic_tx_pool *tx_pool;
547 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
548 adapter->tx_pool = kcalloc(tx_subcrqs,
549 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
550 if (!adapter->tx_pool)
553 for (i = 0; i < tx_subcrqs; i++) {
554 tx_pool = &adapter->tx_pool[i];
555 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
556 sizeof(struct ibmvnic_tx_buff),
558 if (!tx_pool->tx_buff) {
559 dev_err(dev, "tx pool buffer allocation failed\n");
560 release_tx_pools(adapter);
564 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
565 adapter->req_tx_entries_per_subcrq *
567 release_tx_pools(adapter);
571 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
572 sizeof(int), GFP_KERNEL);
573 if (!tx_pool->free_map) {
574 release_tx_pools(adapter);
578 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
579 tx_pool->free_map[j] = j;
581 tx_pool->consumer_index = 0;
582 tx_pool->producer_index = 0;
588 static void release_error_buffers(struct ibmvnic_adapter *adapter)
590 struct device *dev = &adapter->vdev->dev;
591 struct ibmvnic_error_buff *error_buff, *tmp;
594 spin_lock_irqsave(&adapter->error_list_lock, flags);
595 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
596 list_del(&error_buff->list);
597 dma_unmap_single(dev, error_buff->dma, error_buff->len,
599 kfree(error_buff->buff);
602 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
605 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
609 if (adapter->napi_enabled)
612 for (i = 0; i < adapter->req_rx_queues; i++)
613 napi_enable(&adapter->napi[i]);
615 adapter->napi_enabled = true;
618 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
622 if (!adapter->napi_enabled)
625 for (i = 0; i < adapter->req_rx_queues; i++)
626 napi_disable(&adapter->napi[i]);
628 adapter->napi_enabled = false;
631 static int ibmvnic_login(struct net_device *netdev)
633 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
634 unsigned long timeout = msecs_to_jiffies(30000);
635 struct device *dev = &adapter->vdev->dev;
638 if (adapter->renegotiate) {
639 adapter->renegotiate = false;
640 release_sub_crqs(adapter);
642 reinit_completion(&adapter->init_done);
643 send_cap_queries(adapter);
644 if (!wait_for_completion_timeout(&adapter->init_done,
646 dev_err(dev, "Capabilities query timeout\n");
651 reinit_completion(&adapter->init_done);
653 if (!wait_for_completion_timeout(&adapter->init_done,
655 dev_err(dev, "Login timeout\n");
658 } while (adapter->renegotiate);
663 static void release_resources(struct ibmvnic_adapter *adapter)
667 release_tx_pools(adapter);
668 release_rx_pools(adapter);
670 release_stats_token(adapter);
671 release_error_buffers(adapter);
674 for (i = 0; i < adapter->req_rx_queues; i++) {
675 if (&adapter->napi[i])
676 netif_napi_del(&adapter->napi[i]);
681 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
683 struct net_device *netdev = adapter->netdev;
684 unsigned long timeout = msecs_to_jiffies(30000);
685 union ibmvnic_crq crq;
689 netdev_err(netdev, "setting link state %d\n", link_state);
690 memset(&crq, 0, sizeof(crq));
691 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
692 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
693 crq.logical_link_state.link_state = link_state;
698 reinit_completion(&adapter->init_done);
699 rc = ibmvnic_send_crq(adapter, &crq);
701 netdev_err(netdev, "Failed to set link state\n");
705 if (!wait_for_completion_timeout(&adapter->init_done,
707 netdev_err(netdev, "timeout setting link state\n");
711 if (adapter->init_done_rc == 1) {
712 /* Partuial success, delay and re-send */
721 static int set_real_num_queues(struct net_device *netdev)
723 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
726 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
728 netdev_err(netdev, "failed to set the number of tx queues\n");
732 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
734 netdev_err(netdev, "failed to set the number of rx queues\n");
739 static int init_resources(struct ibmvnic_adapter *adapter)
741 struct net_device *netdev = adapter->netdev;
744 rc = set_real_num_queues(netdev);
748 rc = init_sub_crq_irqs(adapter);
750 netdev_err(netdev, "failed to initialize sub crq irqs\n");
754 rc = init_stats_token(adapter);
759 adapter->napi = kcalloc(adapter->req_rx_queues,
760 sizeof(struct napi_struct), GFP_KERNEL);
764 for (i = 0; i < adapter->req_rx_queues; i++) {
765 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
769 send_map_query(adapter);
771 rc = init_rx_pools(netdev);
775 rc = init_tx_pools(netdev);
779 static int __ibmvnic_open(struct net_device *netdev)
781 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
782 enum vnic_state prev_state = adapter->state;
785 adapter->state = VNIC_OPENING;
786 replenish_pools(adapter);
787 ibmvnic_napi_enable(adapter);
789 /* We're ready to receive frames, enable the sub-crq interrupts and
790 * set the logical link state to up
792 for (i = 0; i < adapter->req_rx_queues; i++) {
793 if (prev_state == VNIC_CLOSED)
794 enable_irq(adapter->rx_scrq[i]->irq);
796 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
799 for (i = 0; i < adapter->req_tx_queues; i++) {
800 if (prev_state == VNIC_CLOSED)
801 enable_irq(adapter->tx_scrq[i]->irq);
803 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
806 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
808 for (i = 0; i < adapter->req_rx_queues; i++)
809 napi_disable(&adapter->napi[i]);
810 release_resources(adapter);
814 netif_tx_start_all_queues(netdev);
816 if (prev_state == VNIC_CLOSED) {
817 for (i = 0; i < adapter->req_rx_queues; i++)
818 napi_schedule(&adapter->napi[i]);
821 adapter->state = VNIC_OPEN;
825 static int ibmvnic_open(struct net_device *netdev)
827 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
830 mutex_lock(&adapter->reset_lock);
832 if (adapter->state != VNIC_CLOSED) {
833 rc = ibmvnic_login(netdev);
835 mutex_unlock(&adapter->reset_lock);
839 rc = init_resources(adapter);
841 netdev_err(netdev, "failed to initialize resources\n");
842 release_resources(adapter);
843 mutex_unlock(&adapter->reset_lock);
848 rc = __ibmvnic_open(netdev);
849 mutex_unlock(&adapter->reset_lock);
854 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
856 struct ibmvnic_tx_pool *tx_pool;
861 if (!adapter->tx_pool)
864 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
865 tx_entries = adapter->req_tx_entries_per_subcrq;
867 /* Free any remaining skbs in the tx buffer pools */
868 for (i = 0; i < tx_scrqs; i++) {
869 tx_pool = &adapter->tx_pool[i];
873 for (j = 0; j < tx_entries; j++) {
874 if (tx_pool->tx_buff[j].skb) {
875 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
876 tx_pool->tx_buff[j].skb = NULL;
882 static int __ibmvnic_close(struct net_device *netdev)
884 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
888 adapter->state = VNIC_CLOSING;
890 /* ensure that transmissions are stopped if called by do_reset */
891 if (adapter->resetting)
892 netif_tx_disable(netdev);
894 netif_tx_stop_all_queues(netdev);
896 ibmvnic_napi_disable(adapter);
898 if (adapter->tx_scrq) {
899 for (i = 0; i < adapter->req_tx_queues; i++)
900 if (adapter->tx_scrq[i]->irq)
901 disable_irq(adapter->tx_scrq[i]->irq);
904 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
908 if (adapter->rx_scrq) {
909 for (i = 0; i < adapter->req_rx_queues; i++) {
912 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
920 if (adapter->rx_scrq[i]->irq)
921 disable_irq(adapter->rx_scrq[i]->irq);
925 clean_tx_pools(adapter);
926 adapter->state = VNIC_CLOSED;
930 static int ibmvnic_close(struct net_device *netdev)
932 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
935 mutex_lock(&adapter->reset_lock);
936 rc = __ibmvnic_close(netdev);
937 mutex_unlock(&adapter->reset_lock);
943 * build_hdr_data - creates L2/L3/L4 header data buffer
944 * @hdr_field - bitfield determining needed headers
945 * @skb - socket buffer
946 * @hdr_len - array of header lengths
947 * @tot_len - total length of data
949 * Reads hdr_field to determine which headers are needed by firmware.
950 * Builds a buffer containing these headers. Saves individual header
951 * lengths and total buffer length to be used to build descriptors.
953 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
954 int *hdr_len, u8 *hdr_data)
959 hdr_len[0] = sizeof(struct ethhdr);
961 if (skb->protocol == htons(ETH_P_IP)) {
962 hdr_len[1] = ip_hdr(skb)->ihl * 4;
963 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
964 hdr_len[2] = tcp_hdrlen(skb);
965 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
966 hdr_len[2] = sizeof(struct udphdr);
967 } else if (skb->protocol == htons(ETH_P_IPV6)) {
968 hdr_len[1] = sizeof(struct ipv6hdr);
969 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
970 hdr_len[2] = tcp_hdrlen(skb);
971 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
972 hdr_len[2] = sizeof(struct udphdr);
975 memset(hdr_data, 0, 120);
976 if ((hdr_field >> 6) & 1) {
977 hdr = skb_mac_header(skb);
978 memcpy(hdr_data, hdr, hdr_len[0]);
982 if ((hdr_field >> 5) & 1) {
983 hdr = skb_network_header(skb);
984 memcpy(hdr_data + len, hdr, hdr_len[1]);
988 if ((hdr_field >> 4) & 1) {
989 hdr = skb_transport_header(skb);
990 memcpy(hdr_data + len, hdr, hdr_len[2]);
997 * create_hdr_descs - create header and header extension descriptors
998 * @hdr_field - bitfield determining needed headers
999 * @data - buffer containing header data
1000 * @len - length of data buffer
1001 * @hdr_len - array of individual header lengths
1002 * @scrq_arr - descriptor array
1004 * Creates header and, if needed, header extension descriptors and
1005 * places them in a descriptor array, scrq_arr
1008 static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1009 union sub_crq *scrq_arr)
1011 union sub_crq hdr_desc;
1016 while (tmp_len > 0) {
1017 cur = hdr_data + len - tmp_len;
1019 memset(&hdr_desc, 0, sizeof(hdr_desc));
1020 if (cur != hdr_data) {
1021 data = hdr_desc.hdr_ext.data;
1022 tmp = tmp_len > 29 ? 29 : tmp_len;
1023 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1024 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1025 hdr_desc.hdr_ext.len = tmp;
1027 data = hdr_desc.hdr.data;
1028 tmp = tmp_len > 24 ? 24 : tmp_len;
1029 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1030 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1031 hdr_desc.hdr.len = tmp;
1032 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1033 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1034 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1035 hdr_desc.hdr.flag = hdr_field << 1;
1037 memcpy(data, cur, tmp);
1039 *scrq_arr = hdr_desc;
1045 * build_hdr_descs_arr - build a header descriptor array
1046 * @skb - socket buffer
1047 * @num_entries - number of descriptors to be sent
1048 * @subcrq - first TX descriptor
1049 * @hdr_field - bit field determining which headers will be sent
1051 * This function will build a TX descriptor array with applicable
1052 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1055 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1056 int *num_entries, u8 hdr_field)
1058 int hdr_len[3] = {0, 0, 0};
1060 u8 *hdr_data = txbuff->hdr_data;
1062 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1067 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1068 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1069 txbuff->indir_arr + 1);
1072 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1074 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1075 int queue_num = skb_get_queue_mapping(skb);
1076 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1077 struct device *dev = &adapter->vdev->dev;
1078 struct ibmvnic_tx_buff *tx_buff = NULL;
1079 struct ibmvnic_sub_crq_queue *tx_scrq;
1080 struct ibmvnic_tx_pool *tx_pool;
1081 unsigned int tx_send_failed = 0;
1082 unsigned int tx_map_failed = 0;
1083 unsigned int tx_dropped = 0;
1084 unsigned int tx_packets = 0;
1085 unsigned int tx_bytes = 0;
1086 dma_addr_t data_dma_addr;
1087 struct netdev_queue *txq;
1088 unsigned long lpar_rc;
1089 union sub_crq tx_crq;
1090 unsigned int offset;
1091 int num_entries = 1;
1097 if (adapter->resetting) {
1098 if (!netif_subqueue_stopped(netdev, skb))
1099 netif_stop_subqueue(netdev, queue_num);
1100 dev_kfree_skb_any(skb);
1108 tx_pool = &adapter->tx_pool[queue_num];
1109 tx_scrq = adapter->tx_scrq[queue_num];
1110 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1111 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1112 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1114 index = tx_pool->free_map[tx_pool->consumer_index];
1115 offset = index * adapter->req_mtu;
1116 dst = tx_pool->long_term_buff.buff + offset;
1117 memset(dst, 0, adapter->req_mtu);
1118 skb_copy_from_linear_data(skb, dst, skb->len);
1119 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1121 tx_pool->consumer_index =
1122 (tx_pool->consumer_index + 1) %
1123 adapter->req_tx_entries_per_subcrq;
1125 tx_buff = &tx_pool->tx_buff[index];
1127 tx_buff->data_dma[0] = data_dma_addr;
1128 tx_buff->data_len[0] = skb->len;
1129 tx_buff->index = index;
1130 tx_buff->pool_index = queue_num;
1131 tx_buff->last_frag = true;
1133 memset(&tx_crq, 0, sizeof(tx_crq));
1134 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1135 tx_crq.v1.type = IBMVNIC_TX_DESC;
1136 tx_crq.v1.n_crq_elem = 1;
1137 tx_crq.v1.n_sge = 1;
1138 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1139 tx_crq.v1.correlator = cpu_to_be32(index);
1140 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1141 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1142 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1144 if (adapter->vlan_header_insertion) {
1145 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1146 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1149 if (skb->protocol == htons(ETH_P_IP)) {
1150 if (ip_hdr(skb)->version == 4)
1151 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1152 else if (ip_hdr(skb)->version == 6)
1153 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1155 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1156 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1157 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1158 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1161 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1162 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1165 /* determine if l2/3/4 headers are sent to firmware */
1166 if ((*hdrs >> 7) & 1 &&
1167 (skb->protocol == htons(ETH_P_IP) ||
1168 skb->protocol == htons(ETH_P_IPV6))) {
1169 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1170 tx_crq.v1.n_crq_elem = num_entries;
1171 tx_buff->indir_arr[0] = tx_crq;
1172 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1173 sizeof(tx_buff->indir_arr),
1175 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1176 dev_kfree_skb_any(skb);
1177 tx_buff->skb = NULL;
1178 if (!firmware_has_feature(FW_FEATURE_CMO))
1179 dev_err(dev, "tx: unable to map descriptor array\n");
1185 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1186 (u64)tx_buff->indir_dma,
1189 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1192 if (lpar_rc != H_SUCCESS) {
1193 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1195 if (tx_pool->consumer_index == 0)
1196 tx_pool->consumer_index =
1197 adapter->req_tx_entries_per_subcrq - 1;
1199 tx_pool->consumer_index--;
1201 dev_kfree_skb_any(skb);
1202 tx_buff->skb = NULL;
1204 if (lpar_rc == H_CLOSED) {
1205 /* Disable TX and report carrier off if queue is closed.
1206 * Firmware guarantees that a signal will be sent to the
1207 * driver, triggering a reset or some other action.
1209 netif_tx_stop_all_queues(netdev);
1210 netif_carrier_off(netdev);
1219 if (atomic_inc_return(&tx_scrq->used)
1220 >= adapter->req_tx_entries_per_subcrq) {
1221 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1222 netif_stop_subqueue(netdev, queue_num);
1226 tx_bytes += skb->len;
1227 txq->trans_start = jiffies;
1231 netdev->stats.tx_dropped += tx_dropped;
1232 netdev->stats.tx_bytes += tx_bytes;
1233 netdev->stats.tx_packets += tx_packets;
1234 adapter->tx_send_failed += tx_send_failed;
1235 adapter->tx_map_failed += tx_map_failed;
1240 static void ibmvnic_set_multi(struct net_device *netdev)
1242 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1243 struct netdev_hw_addr *ha;
1244 union ibmvnic_crq crq;
1246 memset(&crq, 0, sizeof(crq));
1247 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1248 crq.request_capability.cmd = REQUEST_CAPABILITY;
1250 if (netdev->flags & IFF_PROMISC) {
1251 if (!adapter->promisc_supported)
1254 if (netdev->flags & IFF_ALLMULTI) {
1255 /* Accept all multicast */
1256 memset(&crq, 0, sizeof(crq));
1257 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1258 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1259 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1260 ibmvnic_send_crq(adapter, &crq);
1261 } else if (netdev_mc_empty(netdev)) {
1262 /* Reject all multicast */
1263 memset(&crq, 0, sizeof(crq));
1264 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1265 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1266 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1267 ibmvnic_send_crq(adapter, &crq);
1269 /* Accept one or more multicast(s) */
1270 netdev_for_each_mc_addr(ha, netdev) {
1271 memset(&crq, 0, sizeof(crq));
1272 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1273 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1274 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1275 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1277 ibmvnic_send_crq(adapter, &crq);
1283 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1285 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1286 struct sockaddr *addr = p;
1287 union ibmvnic_crq crq;
1289 if (!is_valid_ether_addr(addr->sa_data))
1290 return -EADDRNOTAVAIL;
1292 memset(&crq, 0, sizeof(crq));
1293 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1294 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1295 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1296 ibmvnic_send_crq(adapter, &crq);
1297 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1302 * do_reset returns zero if we are able to keep processing reset events, or
1303 * non-zero if we hit a fatal error and must halt.
1305 static int do_reset(struct ibmvnic_adapter *adapter,
1306 struct ibmvnic_rwi *rwi, u32 reset_state)
1308 struct net_device *netdev = adapter->netdev;
1311 netif_carrier_off(netdev);
1312 adapter->reset_reason = rwi->reset_reason;
1314 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1315 rc = ibmvnic_reenable_crq_queue(adapter);
1320 rc = __ibmvnic_close(netdev);
1324 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1325 /* remove the closed state so when we call open it appears
1326 * we are coming from the probed state.
1328 adapter->state = VNIC_PROBED;
1330 rc = ibmvnic_init(adapter);
1334 /* If the adapter was in PROBE state prior to the reset,
1337 if (reset_state == VNIC_PROBED)
1340 rc = ibmvnic_login(netdev);
1342 adapter->state = VNIC_PROBED;
1346 rc = reset_tx_pools(adapter);
1350 rc = reset_rx_pools(adapter);
1354 if (reset_state == VNIC_CLOSED)
1358 rc = __ibmvnic_open(netdev);
1360 if (list_empty(&adapter->rwi_list))
1361 adapter->state = VNIC_CLOSED;
1363 adapter->state = reset_state;
1368 netif_carrier_on(netdev);
1371 for (i = 0; i < adapter->req_rx_queues; i++)
1372 napi_schedule(&adapter->napi[i]);
1374 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1375 netdev_notify_peers(netdev);
1380 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1382 struct ibmvnic_rwi *rwi;
1384 mutex_lock(&adapter->rwi_lock);
1386 if (!list_empty(&adapter->rwi_list)) {
1387 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1389 list_del(&rwi->list);
1394 mutex_unlock(&adapter->rwi_lock);
1398 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1400 struct ibmvnic_rwi *rwi;
1402 rwi = get_next_rwi(adapter);
1405 rwi = get_next_rwi(adapter);
1409 static void __ibmvnic_reset(struct work_struct *work)
1411 struct ibmvnic_rwi *rwi;
1412 struct ibmvnic_adapter *adapter;
1413 struct net_device *netdev;
1417 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1418 netdev = adapter->netdev;
1420 mutex_lock(&adapter->reset_lock);
1421 adapter->resetting = true;
1422 reset_state = adapter->state;
1424 rwi = get_next_rwi(adapter);
1426 rc = do_reset(adapter, rwi, reset_state);
1431 rwi = get_next_rwi(adapter);
1435 free_all_rwi(adapter);
1436 mutex_unlock(&adapter->reset_lock);
1440 adapter->resetting = false;
1441 mutex_unlock(&adapter->reset_lock);
1444 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1445 enum ibmvnic_reset_reason reason)
1447 struct ibmvnic_rwi *rwi, *tmp;
1448 struct net_device *netdev = adapter->netdev;
1449 struct list_head *entry;
1451 if (adapter->state == VNIC_REMOVING ||
1452 adapter->state == VNIC_REMOVED) {
1453 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1457 mutex_lock(&adapter->rwi_lock);
1459 list_for_each(entry, &adapter->rwi_list) {
1460 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1461 if (tmp->reset_reason == reason) {
1462 netdev_err(netdev, "Matching reset found, skipping\n");
1463 mutex_unlock(&adapter->rwi_lock);
1468 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1470 mutex_unlock(&adapter->rwi_lock);
1471 ibmvnic_close(netdev);
1475 rwi->reset_reason = reason;
1476 list_add_tail(&rwi->list, &adapter->rwi_list);
1477 mutex_unlock(&adapter->rwi_lock);
1478 schedule_work(&adapter->ibmvnic_reset);
1481 static void ibmvnic_tx_timeout(struct net_device *dev)
1483 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1485 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1488 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1489 struct ibmvnic_rx_buff *rx_buff)
1491 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1493 rx_buff->skb = NULL;
1495 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1496 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1498 atomic_dec(&pool->available);
1501 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1503 struct net_device *netdev = napi->dev;
1504 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1505 int scrq_num = (int)(napi - adapter->napi);
1506 int frames_processed = 0;
1508 if (adapter->resetting)
1512 while (frames_processed < budget) {
1513 struct sk_buff *skb;
1514 struct ibmvnic_rx_buff *rx_buff;
1515 union sub_crq *next;
1520 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1522 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1524 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1525 rx_comp.correlator);
1526 /* do error checking */
1527 if (next->rx_comp.rc) {
1528 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1529 /* free the entry */
1530 next->rx_comp.first = 0;
1531 remove_buff_from_pool(adapter, rx_buff);
1535 length = be32_to_cpu(next->rx_comp.len);
1536 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1537 flags = next->rx_comp.flags;
1539 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1542 /* VLAN Header has been stripped by the system firmware and
1543 * needs to be inserted by the driver
1545 if (adapter->rx_vlan_header_insertion &&
1546 (flags & IBMVNIC_VLAN_STRIPPED))
1547 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1548 ntohs(next->rx_comp.vlan_tci));
1550 /* free the entry */
1551 next->rx_comp.first = 0;
1552 remove_buff_from_pool(adapter, rx_buff);
1554 skb_put(skb, length);
1555 skb->protocol = eth_type_trans(skb, netdev);
1556 skb_record_rx_queue(skb, scrq_num);
1558 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1559 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1560 skb->ip_summed = CHECKSUM_UNNECESSARY;
1564 napi_gro_receive(napi, skb); /* send it up */
1565 netdev->stats.rx_packets++;
1566 netdev->stats.rx_bytes += length;
1570 if (adapter->state != VNIC_CLOSING)
1571 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1573 if (frames_processed < budget) {
1574 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1575 napi_complete_done(napi, frames_processed);
1576 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1577 napi_reschedule(napi)) {
1578 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1582 return frames_processed;
1585 #ifdef CONFIG_NET_POLL_CONTROLLER
1586 static void ibmvnic_netpoll_controller(struct net_device *dev)
1588 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1591 replenish_pools(netdev_priv(dev));
1592 for (i = 0; i < adapter->req_rx_queues; i++)
1593 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1594 adapter->rx_scrq[i]);
1598 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1603 static const struct net_device_ops ibmvnic_netdev_ops = {
1604 .ndo_open = ibmvnic_open,
1605 .ndo_stop = ibmvnic_close,
1606 .ndo_start_xmit = ibmvnic_xmit,
1607 .ndo_set_rx_mode = ibmvnic_set_multi,
1608 .ndo_set_mac_address = ibmvnic_set_mac,
1609 .ndo_validate_addr = eth_validate_addr,
1610 .ndo_tx_timeout = ibmvnic_tx_timeout,
1611 #ifdef CONFIG_NET_POLL_CONTROLLER
1612 .ndo_poll_controller = ibmvnic_netpoll_controller,
1614 .ndo_change_mtu = ibmvnic_change_mtu,
1617 /* ethtool functions */
1619 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1620 struct ethtool_link_ksettings *cmd)
1622 u32 supported, advertising;
1624 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1626 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1628 cmd->base.speed = SPEED_1000;
1629 cmd->base.duplex = DUPLEX_FULL;
1630 cmd->base.port = PORT_FIBRE;
1631 cmd->base.phy_address = 0;
1632 cmd->base.autoneg = AUTONEG_ENABLE;
1634 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1636 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1642 static void ibmvnic_get_drvinfo(struct net_device *dev,
1643 struct ethtool_drvinfo *info)
1645 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1646 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1649 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1651 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1653 return adapter->msg_enable;
1656 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1658 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1660 adapter->msg_enable = data;
1663 static u32 ibmvnic_get_link(struct net_device *netdev)
1665 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1667 /* Don't need to send a query because we request a logical link up at
1668 * init and then we wait for link state indications
1670 return adapter->logical_link_state;
1673 static void ibmvnic_get_ringparam(struct net_device *netdev,
1674 struct ethtool_ringparam *ring)
1676 ring->rx_max_pending = 0;
1677 ring->tx_max_pending = 0;
1678 ring->rx_mini_max_pending = 0;
1679 ring->rx_jumbo_max_pending = 0;
1680 ring->rx_pending = 0;
1681 ring->tx_pending = 0;
1682 ring->rx_mini_pending = 0;
1683 ring->rx_jumbo_pending = 0;
1686 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1690 if (stringset != ETH_SS_STATS)
1693 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1694 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1697 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1701 return ARRAY_SIZE(ibmvnic_stats);
1707 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1708 struct ethtool_stats *stats, u64 *data)
1710 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1711 union ibmvnic_crq crq;
1714 memset(&crq, 0, sizeof(crq));
1715 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1716 crq.request_statistics.cmd = REQUEST_STATISTICS;
1717 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1718 crq.request_statistics.len =
1719 cpu_to_be32(sizeof(struct ibmvnic_statistics));
1721 /* Wait for data to be written */
1722 init_completion(&adapter->stats_done);
1723 ibmvnic_send_crq(adapter, &crq);
1724 wait_for_completion(&adapter->stats_done);
1726 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1727 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1730 static const struct ethtool_ops ibmvnic_ethtool_ops = {
1731 .get_drvinfo = ibmvnic_get_drvinfo,
1732 .get_msglevel = ibmvnic_get_msglevel,
1733 .set_msglevel = ibmvnic_set_msglevel,
1734 .get_link = ibmvnic_get_link,
1735 .get_ringparam = ibmvnic_get_ringparam,
1736 .get_strings = ibmvnic_get_strings,
1737 .get_sset_count = ibmvnic_get_sset_count,
1738 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
1739 .get_link_ksettings = ibmvnic_get_link_ksettings,
1742 /* Routines for managing CRQs/sCRQs */
1744 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1745 struct ibmvnic_sub_crq_queue *scrq)
1750 free_irq(scrq->irq, scrq);
1751 irq_dispose_mapping(scrq->irq);
1755 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1758 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1759 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1763 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1767 for (i = 0; i < adapter->req_tx_queues; i++) {
1768 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1773 for (i = 0; i < adapter->req_rx_queues; i++) {
1774 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1779 rc = init_sub_crq_irqs(adapter);
1783 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1784 struct ibmvnic_sub_crq_queue *scrq)
1786 struct device *dev = &adapter->vdev->dev;
1789 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1791 /* Close the sub-crqs */
1793 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1794 adapter->vdev->unit_address,
1796 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1799 netdev_err(adapter->netdev,
1800 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1804 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1806 free_pages((unsigned long)scrq->msgs, 2);
1810 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1813 struct device *dev = &adapter->vdev->dev;
1814 struct ibmvnic_sub_crq_queue *scrq;
1817 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
1822 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
1824 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1825 goto zero_page_failed;
1828 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1830 if (dma_mapping_error(dev, scrq->msg_token)) {
1831 dev_warn(dev, "Couldn't map crq queue messages page\n");
1835 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1836 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1838 if (rc == H_RESOURCE)
1839 rc = ibmvnic_reset_crq(adapter);
1841 if (rc == H_CLOSED) {
1842 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1844 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1848 scrq->adapter = adapter;
1849 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1850 spin_lock_init(&scrq->lock);
1852 netdev_dbg(adapter->netdev,
1853 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1854 scrq->crq_num, scrq->hw_irq, scrq->irq);
1859 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1862 free_pages((unsigned long)scrq->msgs, 2);
1869 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1873 if (adapter->tx_scrq) {
1874 for (i = 0; i < adapter->req_tx_queues; i++) {
1875 if (!adapter->tx_scrq[i])
1878 if (adapter->tx_scrq[i]->irq) {
1879 free_irq(adapter->tx_scrq[i]->irq,
1880 adapter->tx_scrq[i]);
1881 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
1882 adapter->tx_scrq[i]->irq = 0;
1885 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1888 kfree(adapter->tx_scrq);
1889 adapter->tx_scrq = NULL;
1892 if (adapter->rx_scrq) {
1893 for (i = 0; i < adapter->req_rx_queues; i++) {
1894 if (!adapter->rx_scrq[i])
1897 if (adapter->rx_scrq[i]->irq) {
1898 free_irq(adapter->rx_scrq[i]->irq,
1899 adapter->rx_scrq[i]);
1900 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
1901 adapter->rx_scrq[i]->irq = 0;
1904 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1907 kfree(adapter->rx_scrq);
1908 adapter->rx_scrq = NULL;
1912 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1913 struct ibmvnic_sub_crq_queue *scrq)
1915 struct device *dev = &adapter->vdev->dev;
1918 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1919 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1921 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1926 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1927 struct ibmvnic_sub_crq_queue *scrq)
1929 struct device *dev = &adapter->vdev->dev;
1932 if (scrq->hw_irq > 0x100000000ULL) {
1933 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1937 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1938 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1940 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1945 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1946 struct ibmvnic_sub_crq_queue *scrq)
1948 struct device *dev = &adapter->vdev->dev;
1949 struct ibmvnic_tx_buff *txbuff;
1950 union sub_crq *next;
1956 while (pending_scrq(adapter, scrq)) {
1957 unsigned int pool = scrq->pool_index;
1959 next = ibmvnic_next_scrq(adapter, scrq);
1960 for (i = 0; i < next->tx_comp.num_comps; i++) {
1961 if (next->tx_comp.rcs[i]) {
1962 dev_err(dev, "tx error %x\n",
1963 next->tx_comp.rcs[i]);
1966 index = be32_to_cpu(next->tx_comp.correlators[i]);
1967 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1969 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1970 if (!txbuff->data_dma[j])
1973 txbuff->data_dma[j] = 0;
1975 /* if sub_crq was sent indirectly */
1976 first = txbuff->indir_arr[0].generic.first;
1977 if (first == IBMVNIC_CRQ_CMD) {
1978 dma_unmap_single(dev, txbuff->indir_dma,
1979 sizeof(txbuff->indir_arr),
1983 if (txbuff->last_frag) {
1984 dev_kfree_skb_any(txbuff->skb);
1988 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1989 producer_index] = index;
1990 adapter->tx_pool[pool].producer_index =
1991 (adapter->tx_pool[pool].producer_index + 1) %
1992 adapter->req_tx_entries_per_subcrq;
1994 /* remove tx_comp scrq*/
1995 next->tx_comp.first = 0;
1997 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
1998 (adapter->req_tx_entries_per_subcrq / 2) &&
1999 __netif_subqueue_stopped(adapter->netdev,
2000 scrq->pool_index)) {
2001 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2002 netdev_info(adapter->netdev, "Started queue %d\n",
2007 enable_scrq_irq(adapter, scrq);
2009 if (pending_scrq(adapter, scrq)) {
2010 disable_scrq_irq(adapter, scrq);
2017 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2019 struct ibmvnic_sub_crq_queue *scrq = instance;
2020 struct ibmvnic_adapter *adapter = scrq->adapter;
2022 disable_scrq_irq(adapter, scrq);
2023 ibmvnic_complete_tx(adapter, scrq);
2028 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2030 struct ibmvnic_sub_crq_queue *scrq = instance;
2031 struct ibmvnic_adapter *adapter = scrq->adapter;
2033 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2034 disable_scrq_irq(adapter, scrq);
2035 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2041 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2043 struct device *dev = &adapter->vdev->dev;
2044 struct ibmvnic_sub_crq_queue *scrq;
2048 for (i = 0; i < adapter->req_tx_queues; i++) {
2049 scrq = adapter->tx_scrq[i];
2050 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2054 dev_err(dev, "Error mapping irq\n");
2055 goto req_tx_irq_failed;
2058 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2059 0, "ibmvnic_tx", scrq);
2062 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2064 irq_dispose_mapping(scrq->irq);
2065 goto req_rx_irq_failed;
2069 for (i = 0; i < adapter->req_rx_queues; i++) {
2070 scrq = adapter->rx_scrq[i];
2071 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2074 dev_err(dev, "Error mapping irq\n");
2075 goto req_rx_irq_failed;
2077 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2078 0, "ibmvnic_rx", scrq);
2080 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2082 irq_dispose_mapping(scrq->irq);
2083 goto req_rx_irq_failed;
2089 for (j = 0; j < i; j++) {
2090 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2091 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2093 i = adapter->req_tx_queues;
2095 for (j = 0; j < i; j++) {
2096 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2097 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2099 release_sub_crqs(adapter);
2103 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2105 struct device *dev = &adapter->vdev->dev;
2106 struct ibmvnic_sub_crq_queue **allqueues;
2107 int registered_queues = 0;
2112 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2114 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2118 for (i = 0; i < total_queues; i++) {
2119 allqueues[i] = init_sub_crq_queue(adapter);
2120 if (!allqueues[i]) {
2121 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2124 registered_queues++;
2127 /* Make sure we were able to register the minimum number of queues */
2128 if (registered_queues <
2129 adapter->min_tx_queues + adapter->min_rx_queues) {
2130 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2134 /* Distribute the failed allocated queues*/
2135 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2136 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2139 if (adapter->req_rx_queues > adapter->min_rx_queues)
2140 adapter->req_rx_queues--;
2145 if (adapter->req_tx_queues > adapter->min_tx_queues)
2146 adapter->req_tx_queues--;
2153 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2154 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2155 if (!adapter->tx_scrq)
2158 for (i = 0; i < adapter->req_tx_queues; i++) {
2159 adapter->tx_scrq[i] = allqueues[i];
2160 adapter->tx_scrq[i]->pool_index = i;
2163 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2164 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2165 if (!adapter->rx_scrq)
2168 for (i = 0; i < adapter->req_rx_queues; i++) {
2169 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2170 adapter->rx_scrq[i]->scrq_num = i;
2177 kfree(adapter->tx_scrq);
2178 adapter->tx_scrq = NULL;
2180 for (i = 0; i < registered_queues; i++)
2181 release_sub_crq_queue(adapter, allqueues[i]);
2186 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2188 struct device *dev = &adapter->vdev->dev;
2189 union ibmvnic_crq crq;
2192 /* Sub-CRQ entries are 32 byte long */
2193 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2195 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2196 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2197 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2201 /* Get the minimum between the queried max and the entries
2202 * that fit in our PAGE_SIZE
2204 adapter->req_tx_entries_per_subcrq =
2205 adapter->max_tx_entries_per_subcrq > entries_page ?
2206 entries_page : adapter->max_tx_entries_per_subcrq;
2207 adapter->req_rx_add_entries_per_subcrq =
2208 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2209 entries_page : adapter->max_rx_add_entries_per_subcrq;
2211 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2212 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2213 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2215 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2218 memset(&crq, 0, sizeof(crq));
2219 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2220 crq.request_capability.cmd = REQUEST_CAPABILITY;
2222 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2223 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2224 atomic_inc(&adapter->running_cap_crqs);
2225 ibmvnic_send_crq(adapter, &crq);
2227 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2228 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2229 atomic_inc(&adapter->running_cap_crqs);
2230 ibmvnic_send_crq(adapter, &crq);
2232 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2233 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2234 atomic_inc(&adapter->running_cap_crqs);
2235 ibmvnic_send_crq(adapter, &crq);
2237 crq.request_capability.capability =
2238 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2239 crq.request_capability.number =
2240 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2241 atomic_inc(&adapter->running_cap_crqs);
2242 ibmvnic_send_crq(adapter, &crq);
2244 crq.request_capability.capability =
2245 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2246 crq.request_capability.number =
2247 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2248 atomic_inc(&adapter->running_cap_crqs);
2249 ibmvnic_send_crq(adapter, &crq);
2251 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2252 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2253 atomic_inc(&adapter->running_cap_crqs);
2254 ibmvnic_send_crq(adapter, &crq);
2256 if (adapter->netdev->flags & IFF_PROMISC) {
2257 if (adapter->promisc_supported) {
2258 crq.request_capability.capability =
2259 cpu_to_be16(PROMISC_REQUESTED);
2260 crq.request_capability.number = cpu_to_be64(1);
2261 atomic_inc(&adapter->running_cap_crqs);
2262 ibmvnic_send_crq(adapter, &crq);
2265 crq.request_capability.capability =
2266 cpu_to_be16(PROMISC_REQUESTED);
2267 crq.request_capability.number = cpu_to_be64(0);
2268 atomic_inc(&adapter->running_cap_crqs);
2269 ibmvnic_send_crq(adapter, &crq);
2273 static int pending_scrq(struct ibmvnic_adapter *adapter,
2274 struct ibmvnic_sub_crq_queue *scrq)
2276 union sub_crq *entry = &scrq->msgs[scrq->cur];
2278 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2284 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2285 struct ibmvnic_sub_crq_queue *scrq)
2287 union sub_crq *entry;
2288 unsigned long flags;
2290 spin_lock_irqsave(&scrq->lock, flags);
2291 entry = &scrq->msgs[scrq->cur];
2292 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2293 if (++scrq->cur == scrq->size)
2298 spin_unlock_irqrestore(&scrq->lock, flags);
2303 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2305 struct ibmvnic_crq_queue *queue = &adapter->crq;
2306 union ibmvnic_crq *crq;
2308 crq = &queue->msgs[queue->cur];
2309 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2310 if (++queue->cur == queue->size)
2319 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2320 union sub_crq *sub_crq)
2322 unsigned int ua = adapter->vdev->unit_address;
2323 struct device *dev = &adapter->vdev->dev;
2324 u64 *u64_crq = (u64 *)sub_crq;
2327 netdev_dbg(adapter->netdev,
2328 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2329 (unsigned long int)cpu_to_be64(remote_handle),
2330 (unsigned long int)cpu_to_be64(u64_crq[0]),
2331 (unsigned long int)cpu_to_be64(u64_crq[1]),
2332 (unsigned long int)cpu_to_be64(u64_crq[2]),
2333 (unsigned long int)cpu_to_be64(u64_crq[3]));
2335 /* Make sure the hypervisor sees the complete request */
2338 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2339 cpu_to_be64(remote_handle),
2340 cpu_to_be64(u64_crq[0]),
2341 cpu_to_be64(u64_crq[1]),
2342 cpu_to_be64(u64_crq[2]),
2343 cpu_to_be64(u64_crq[3]));
2347 dev_warn(dev, "CRQ Queue closed\n");
2348 dev_err(dev, "Send error (rc=%d)\n", rc);
2354 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2355 u64 remote_handle, u64 ioba, u64 num_entries)
2357 unsigned int ua = adapter->vdev->unit_address;
2358 struct device *dev = &adapter->vdev->dev;
2361 /* Make sure the hypervisor sees the complete request */
2363 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2364 cpu_to_be64(remote_handle),
2369 dev_warn(dev, "CRQ Queue closed\n");
2370 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2376 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2377 union ibmvnic_crq *crq)
2379 unsigned int ua = adapter->vdev->unit_address;
2380 struct device *dev = &adapter->vdev->dev;
2381 u64 *u64_crq = (u64 *)crq;
2384 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2385 (unsigned long int)cpu_to_be64(u64_crq[0]),
2386 (unsigned long int)cpu_to_be64(u64_crq[1]));
2388 /* Make sure the hypervisor sees the complete request */
2391 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2392 cpu_to_be64(u64_crq[0]),
2393 cpu_to_be64(u64_crq[1]));
2397 dev_warn(dev, "CRQ Queue closed\n");
2398 dev_warn(dev, "Send error (rc=%d)\n", rc);
2404 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2406 union ibmvnic_crq crq;
2408 memset(&crq, 0, sizeof(crq));
2409 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2410 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2411 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2413 return ibmvnic_send_crq(adapter, &crq);
2416 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2418 union ibmvnic_crq crq;
2420 memset(&crq, 0, sizeof(crq));
2421 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2422 crq.version_exchange.cmd = VERSION_EXCHANGE;
2423 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2425 return ibmvnic_send_crq(adapter, &crq);
2428 static void send_login(struct ibmvnic_adapter *adapter)
2430 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2431 struct ibmvnic_login_buffer *login_buffer;
2432 struct device *dev = &adapter->vdev->dev;
2433 dma_addr_t rsp_buffer_token;
2434 dma_addr_t buffer_token;
2435 size_t rsp_buffer_size;
2436 union ibmvnic_crq crq;
2443 sizeof(struct ibmvnic_login_buffer) +
2444 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2446 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2448 goto buf_alloc_failed;
2450 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2452 if (dma_mapping_error(dev, buffer_token)) {
2453 dev_err(dev, "Couldn't map login buffer\n");
2454 goto buf_map_failed;
2457 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2458 sizeof(u64) * adapter->req_tx_queues +
2459 sizeof(u64) * adapter->req_rx_queues +
2460 sizeof(u64) * adapter->req_rx_queues +
2461 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
2463 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2464 if (!login_rsp_buffer)
2465 goto buf_rsp_alloc_failed;
2467 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2468 rsp_buffer_size, DMA_FROM_DEVICE);
2469 if (dma_mapping_error(dev, rsp_buffer_token)) {
2470 dev_err(dev, "Couldn't map login rsp buffer\n");
2471 goto buf_rsp_map_failed;
2474 adapter->login_buf = login_buffer;
2475 adapter->login_buf_token = buffer_token;
2476 adapter->login_buf_sz = buffer_size;
2477 adapter->login_rsp_buf = login_rsp_buffer;
2478 adapter->login_rsp_buf_token = rsp_buffer_token;
2479 adapter->login_rsp_buf_sz = rsp_buffer_size;
2481 login_buffer->len = cpu_to_be32(buffer_size);
2482 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2483 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2484 login_buffer->off_txcomp_subcrqs =
2485 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2486 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2487 login_buffer->off_rxcomp_subcrqs =
2488 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2489 sizeof(u64) * adapter->req_tx_queues);
2490 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2491 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2493 tx_list_p = (__be64 *)((char *)login_buffer +
2494 sizeof(struct ibmvnic_login_buffer));
2495 rx_list_p = (__be64 *)((char *)login_buffer +
2496 sizeof(struct ibmvnic_login_buffer) +
2497 sizeof(u64) * adapter->req_tx_queues);
2499 for (i = 0; i < adapter->req_tx_queues; i++) {
2500 if (adapter->tx_scrq[i]) {
2501 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2506 for (i = 0; i < adapter->req_rx_queues; i++) {
2507 if (adapter->rx_scrq[i]) {
2508 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2513 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2514 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2515 netdev_dbg(adapter->netdev, "%016lx\n",
2516 ((unsigned long int *)(adapter->login_buf))[i]);
2519 memset(&crq, 0, sizeof(crq));
2520 crq.login.first = IBMVNIC_CRQ_CMD;
2521 crq.login.cmd = LOGIN;
2522 crq.login.ioba = cpu_to_be32(buffer_token);
2523 crq.login.len = cpu_to_be32(buffer_size);
2524 ibmvnic_send_crq(adapter, &crq);
2529 kfree(login_rsp_buffer);
2530 buf_rsp_alloc_failed:
2531 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2533 kfree(login_buffer);
2538 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2541 union ibmvnic_crq crq;
2543 memset(&crq, 0, sizeof(crq));
2544 crq.request_map.first = IBMVNIC_CRQ_CMD;
2545 crq.request_map.cmd = REQUEST_MAP;
2546 crq.request_map.map_id = map_id;
2547 crq.request_map.ioba = cpu_to_be32(addr);
2548 crq.request_map.len = cpu_to_be32(len);
2549 ibmvnic_send_crq(adapter, &crq);
2552 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2554 union ibmvnic_crq crq;
2556 memset(&crq, 0, sizeof(crq));
2557 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2558 crq.request_unmap.cmd = REQUEST_UNMAP;
2559 crq.request_unmap.map_id = map_id;
2560 ibmvnic_send_crq(adapter, &crq);
2563 static void send_map_query(struct ibmvnic_adapter *adapter)
2565 union ibmvnic_crq crq;
2567 memset(&crq, 0, sizeof(crq));
2568 crq.query_map.first = IBMVNIC_CRQ_CMD;
2569 crq.query_map.cmd = QUERY_MAP;
2570 ibmvnic_send_crq(adapter, &crq);
2573 /* Send a series of CRQs requesting various capabilities of the VNIC server */
2574 static void send_cap_queries(struct ibmvnic_adapter *adapter)
2576 union ibmvnic_crq crq;
2578 atomic_set(&adapter->running_cap_crqs, 0);
2579 memset(&crq, 0, sizeof(crq));
2580 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2581 crq.query_capability.cmd = QUERY_CAPABILITY;
2583 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
2584 atomic_inc(&adapter->running_cap_crqs);
2585 ibmvnic_send_crq(adapter, &crq);
2587 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
2588 atomic_inc(&adapter->running_cap_crqs);
2589 ibmvnic_send_crq(adapter, &crq);
2591 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
2592 atomic_inc(&adapter->running_cap_crqs);
2593 ibmvnic_send_crq(adapter, &crq);
2595 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
2596 atomic_inc(&adapter->running_cap_crqs);
2597 ibmvnic_send_crq(adapter, &crq);
2599 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
2600 atomic_inc(&adapter->running_cap_crqs);
2601 ibmvnic_send_crq(adapter, &crq);
2603 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
2604 atomic_inc(&adapter->running_cap_crqs);
2605 ibmvnic_send_crq(adapter, &crq);
2607 crq.query_capability.capability =
2608 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
2609 atomic_inc(&adapter->running_cap_crqs);
2610 ibmvnic_send_crq(adapter, &crq);
2612 crq.query_capability.capability =
2613 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
2614 atomic_inc(&adapter->running_cap_crqs);
2615 ibmvnic_send_crq(adapter, &crq);
2617 crq.query_capability.capability =
2618 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
2619 atomic_inc(&adapter->running_cap_crqs);
2620 ibmvnic_send_crq(adapter, &crq);
2622 crq.query_capability.capability =
2623 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
2624 atomic_inc(&adapter->running_cap_crqs);
2625 ibmvnic_send_crq(adapter, &crq);
2627 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
2628 atomic_inc(&adapter->running_cap_crqs);
2629 ibmvnic_send_crq(adapter, &crq);
2631 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
2632 atomic_inc(&adapter->running_cap_crqs);
2633 ibmvnic_send_crq(adapter, &crq);
2635 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
2636 atomic_inc(&adapter->running_cap_crqs);
2637 ibmvnic_send_crq(adapter, &crq);
2639 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
2640 atomic_inc(&adapter->running_cap_crqs);
2641 ibmvnic_send_crq(adapter, &crq);
2643 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
2644 atomic_inc(&adapter->running_cap_crqs);
2645 ibmvnic_send_crq(adapter, &crq);
2647 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
2648 atomic_inc(&adapter->running_cap_crqs);
2649 ibmvnic_send_crq(adapter, &crq);
2651 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2652 atomic_inc(&adapter->running_cap_crqs);
2653 ibmvnic_send_crq(adapter, &crq);
2655 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
2656 atomic_inc(&adapter->running_cap_crqs);
2657 ibmvnic_send_crq(adapter, &crq);
2659 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
2660 atomic_inc(&adapter->running_cap_crqs);
2661 ibmvnic_send_crq(adapter, &crq);
2663 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
2664 atomic_inc(&adapter->running_cap_crqs);
2665 ibmvnic_send_crq(adapter, &crq);
2667 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
2668 atomic_inc(&adapter->running_cap_crqs);
2669 ibmvnic_send_crq(adapter, &crq);
2671 crq.query_capability.capability =
2672 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
2673 atomic_inc(&adapter->running_cap_crqs);
2674 ibmvnic_send_crq(adapter, &crq);
2676 crq.query_capability.capability =
2677 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
2678 atomic_inc(&adapter->running_cap_crqs);
2679 ibmvnic_send_crq(adapter, &crq);
2681 crq.query_capability.capability =
2682 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
2683 atomic_inc(&adapter->running_cap_crqs);
2684 ibmvnic_send_crq(adapter, &crq);
2686 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
2687 atomic_inc(&adapter->running_cap_crqs);
2688 ibmvnic_send_crq(adapter, &crq);
2691 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2693 struct device *dev = &adapter->vdev->dev;
2694 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2695 union ibmvnic_crq crq;
2698 dma_unmap_single(dev, adapter->ip_offload_tok,
2699 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2701 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2702 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2703 netdev_dbg(adapter->netdev, "%016lx\n",
2704 ((unsigned long int *)(buf))[i]);
2706 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2707 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2708 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2709 buf->tcp_ipv4_chksum);
2710 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2711 buf->tcp_ipv6_chksum);
2712 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2713 buf->udp_ipv4_chksum);
2714 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2715 buf->udp_ipv6_chksum);
2716 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2717 buf->large_tx_ipv4);
2718 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2719 buf->large_tx_ipv6);
2720 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2721 buf->large_rx_ipv4);
2722 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2723 buf->large_rx_ipv6);
2724 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2725 buf->max_ipv4_header_size);
2726 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2727 buf->max_ipv6_header_size);
2728 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2729 buf->max_tcp_header_size);
2730 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2731 buf->max_udp_header_size);
2732 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2733 buf->max_large_tx_size);
2734 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2735 buf->max_large_rx_size);
2736 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2737 buf->ipv6_extension_header);
2738 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2739 buf->tcp_pseudosum_req);
2740 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2741 buf->num_ipv6_ext_headers);
2742 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2743 buf->off_ipv6_ext_headers);
2745 adapter->ip_offload_ctrl_tok =
2746 dma_map_single(dev, &adapter->ip_offload_ctrl,
2747 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2749 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2750 dev_err(dev, "Couldn't map ip offload control buffer\n");
2754 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2755 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2756 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2757 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2758 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2760 /* large_tx/rx disabled for now, additional features needed */
2761 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2762 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2763 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2764 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2766 adapter->netdev->features = NETIF_F_GSO;
2768 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2769 adapter->netdev->features |= NETIF_F_IP_CSUM;
2771 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2772 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2774 if ((adapter->netdev->features &
2775 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2776 adapter->netdev->features |= NETIF_F_RXCSUM;
2778 memset(&crq, 0, sizeof(crq));
2779 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2780 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2781 crq.control_ip_offload.len =
2782 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2783 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2784 ibmvnic_send_crq(adapter, &crq);
2787 static void handle_error_info_rsp(union ibmvnic_crq *crq,
2788 struct ibmvnic_adapter *adapter)
2790 struct device *dev = &adapter->vdev->dev;
2791 struct ibmvnic_error_buff *error_buff, *tmp;
2792 unsigned long flags;
2796 if (!crq->request_error_rsp.rc.code) {
2797 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2798 crq->request_error_rsp.rc.code);
2802 spin_lock_irqsave(&adapter->error_list_lock, flags);
2803 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
2804 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2806 list_del(&error_buff->list);
2809 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2812 dev_err(dev, "Couldn't find error id %x\n",
2813 be32_to_cpu(crq->request_error_rsp.error_id));
2817 dev_err(dev, "Detailed info for error id %x:",
2818 be32_to_cpu(crq->request_error_rsp.error_id));
2820 for (i = 0; i < error_buff->len; i++) {
2821 pr_cont("%02x", (int)error_buff->buff[i]);
2827 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2829 kfree(error_buff->buff);
2833 static void request_error_information(struct ibmvnic_adapter *adapter,
2834 union ibmvnic_crq *err_crq)
2836 struct device *dev = &adapter->vdev->dev;
2837 struct net_device *netdev = adapter->netdev;
2838 struct ibmvnic_error_buff *error_buff;
2839 unsigned long timeout = msecs_to_jiffies(30000);
2840 union ibmvnic_crq crq;
2841 unsigned long flags;
2844 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2848 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
2849 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2850 if (!error_buff->buff) {
2855 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2857 if (dma_mapping_error(dev, error_buff->dma)) {
2858 netdev_err(netdev, "Couldn't map error buffer\n");
2859 kfree(error_buff->buff);
2864 error_buff->len = detail_len;
2865 error_buff->error_id = err_crq->error_indication.error_id;
2867 spin_lock_irqsave(&adapter->error_list_lock, flags);
2868 list_add_tail(&error_buff->list, &adapter->errors);
2869 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2871 memset(&crq, 0, sizeof(crq));
2872 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2873 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2874 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2875 crq.request_error_info.len = cpu_to_be32(detail_len);
2876 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2878 rc = ibmvnic_send_crq(adapter, &crq);
2880 netdev_err(netdev, "failed to request error information\n");
2884 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2885 netdev_err(netdev, "timeout waiting for error information\n");
2892 spin_lock_irqsave(&adapter->error_list_lock, flags);
2893 list_del(&error_buff->list);
2894 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2896 kfree(error_buff->buff);
2900 static void handle_error_indication(union ibmvnic_crq *crq,
2901 struct ibmvnic_adapter *adapter)
2903 struct device *dev = &adapter->vdev->dev;
2905 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2906 crq->error_indication.flags
2907 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2908 be32_to_cpu(crq->error_indication.error_id),
2909 be16_to_cpu(crq->error_indication.error_cause));
2911 if (be32_to_cpu(crq->error_indication.error_id))
2912 request_error_information(adapter, crq);
2914 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2915 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2917 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
2920 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2921 struct ibmvnic_adapter *adapter)
2923 struct net_device *netdev = adapter->netdev;
2924 struct device *dev = &adapter->vdev->dev;
2927 rc = crq->change_mac_addr_rsp.rc.code;
2929 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2932 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2936 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2937 struct ibmvnic_adapter *adapter)
2939 struct device *dev = &adapter->vdev->dev;
2943 atomic_dec(&adapter->running_cap_crqs);
2944 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2946 req_value = &adapter->req_tx_queues;
2950 req_value = &adapter->req_rx_queues;
2953 case REQ_RX_ADD_QUEUES:
2954 req_value = &adapter->req_rx_add_queues;
2957 case REQ_TX_ENTRIES_PER_SUBCRQ:
2958 req_value = &adapter->req_tx_entries_per_subcrq;
2959 name = "tx_entries_per_subcrq";
2961 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2962 req_value = &adapter->req_rx_add_entries_per_subcrq;
2963 name = "rx_add_entries_per_subcrq";
2966 req_value = &adapter->req_mtu;
2969 case PROMISC_REQUESTED:
2970 req_value = &adapter->promisc;
2974 dev_err(dev, "Got invalid cap request rsp %d\n",
2975 crq->request_capability.capability);
2979 switch (crq->request_capability_rsp.rc.code) {
2982 case PARTIALSUCCESS:
2983 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2985 (long int)be64_to_cpu(crq->request_capability_rsp.
2987 release_sub_crqs(adapter);
2988 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
2989 ibmvnic_send_req_caps(adapter, 1);
2992 dev_err(dev, "Error %d in request cap rsp\n",
2993 crq->request_capability_rsp.rc.code);
2997 /* Done receiving requested capabilities, query IP offload support */
2998 if (atomic_read(&adapter->running_cap_crqs) == 0) {
2999 union ibmvnic_crq newcrq;
3000 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3001 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3002 &adapter->ip_offload_buf;
3004 adapter->wait_capability = false;
3005 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3009 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3010 if (!firmware_has_feature(FW_FEATURE_CMO))
3011 dev_err(dev, "Couldn't map offload buffer\n");
3015 memset(&newcrq, 0, sizeof(newcrq));
3016 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3017 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3018 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3019 newcrq.query_ip_offload.ioba =
3020 cpu_to_be32(adapter->ip_offload_tok);
3022 ibmvnic_send_crq(adapter, &newcrq);
3026 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3027 struct ibmvnic_adapter *adapter)
3029 struct device *dev = &adapter->vdev->dev;
3030 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3031 struct ibmvnic_login_buffer *login = adapter->login_buf;
3034 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3036 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3037 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3039 /* If the number of queues requested can't be allocated by the
3040 * server, the login response will return with code 1. We will need
3041 * to resend the login buffer with fewer queues requested.
3043 if (login_rsp_crq->generic.rc.code) {
3044 adapter->renegotiate = true;
3045 complete(&adapter->init_done);
3049 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3050 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3051 netdev_dbg(adapter->netdev, "%016lx\n",
3052 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3056 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3057 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3058 adapter->req_rx_add_queues !=
3059 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3060 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3061 ibmvnic_remove(adapter->vdev);
3064 complete(&adapter->init_done);
3069 static void handle_request_map_rsp(union ibmvnic_crq *crq,
3070 struct ibmvnic_adapter *adapter)
3072 struct device *dev = &adapter->vdev->dev;
3073 u8 map_id = crq->request_map_rsp.map_id;
3079 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
3080 rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3082 rc = crq->request_map_rsp.rc.code;
3084 dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
3086 /* need to find and zero tx/rx_pool map_id */
3087 for (i = 0; i < tx_subcrqs; i++) {
3088 if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
3089 adapter->tx_pool[i].long_term_buff.map_id = 0;
3091 for (i = 0; i < rx_subcrqs; i++) {
3092 if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
3093 adapter->rx_pool[i].long_term_buff.map_id = 0;
3096 complete(&adapter->fw_done);
3099 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3100 struct ibmvnic_adapter *adapter)
3102 struct device *dev = &adapter->vdev->dev;
3105 rc = crq->request_unmap_rsp.rc.code;
3107 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3110 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3111 struct ibmvnic_adapter *adapter)
3113 struct net_device *netdev = adapter->netdev;
3114 struct device *dev = &adapter->vdev->dev;
3117 rc = crq->query_map_rsp.rc.code;
3119 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3122 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3123 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3124 crq->query_map_rsp.free_pages);
3127 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3128 struct ibmvnic_adapter *adapter)
3130 struct net_device *netdev = adapter->netdev;
3131 struct device *dev = &adapter->vdev->dev;
3134 atomic_dec(&adapter->running_cap_crqs);
3135 netdev_dbg(netdev, "Outstanding queries: %d\n",
3136 atomic_read(&adapter->running_cap_crqs));
3137 rc = crq->query_capability.rc.code;
3139 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3143 switch (be16_to_cpu(crq->query_capability.capability)) {
3145 adapter->min_tx_queues =
3146 be64_to_cpu(crq->query_capability.number);
3147 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3148 adapter->min_tx_queues);
3151 adapter->min_rx_queues =
3152 be64_to_cpu(crq->query_capability.number);
3153 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3154 adapter->min_rx_queues);
3156 case MIN_RX_ADD_QUEUES:
3157 adapter->min_rx_add_queues =
3158 be64_to_cpu(crq->query_capability.number);
3159 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3160 adapter->min_rx_add_queues);
3163 adapter->max_tx_queues =
3164 be64_to_cpu(crq->query_capability.number);
3165 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3166 adapter->max_tx_queues);
3169 adapter->max_rx_queues =
3170 be64_to_cpu(crq->query_capability.number);
3171 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3172 adapter->max_rx_queues);
3174 case MAX_RX_ADD_QUEUES:
3175 adapter->max_rx_add_queues =
3176 be64_to_cpu(crq->query_capability.number);
3177 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3178 adapter->max_rx_add_queues);
3180 case MIN_TX_ENTRIES_PER_SUBCRQ:
3181 adapter->min_tx_entries_per_subcrq =
3182 be64_to_cpu(crq->query_capability.number);
3183 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3184 adapter->min_tx_entries_per_subcrq);
3186 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3187 adapter->min_rx_add_entries_per_subcrq =
3188 be64_to_cpu(crq->query_capability.number);
3189 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3190 adapter->min_rx_add_entries_per_subcrq);
3192 case MAX_TX_ENTRIES_PER_SUBCRQ:
3193 adapter->max_tx_entries_per_subcrq =
3194 be64_to_cpu(crq->query_capability.number);
3195 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3196 adapter->max_tx_entries_per_subcrq);
3198 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3199 adapter->max_rx_add_entries_per_subcrq =
3200 be64_to_cpu(crq->query_capability.number);
3201 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3202 adapter->max_rx_add_entries_per_subcrq);
3204 case TCP_IP_OFFLOAD:
3205 adapter->tcp_ip_offload =
3206 be64_to_cpu(crq->query_capability.number);
3207 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3208 adapter->tcp_ip_offload);
3210 case PROMISC_SUPPORTED:
3211 adapter->promisc_supported =
3212 be64_to_cpu(crq->query_capability.number);
3213 netdev_dbg(netdev, "promisc_supported = %lld\n",
3214 adapter->promisc_supported);
3217 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3218 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3219 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3222 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3223 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3224 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3226 case MAX_MULTICAST_FILTERS:
3227 adapter->max_multicast_filters =
3228 be64_to_cpu(crq->query_capability.number);
3229 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3230 adapter->max_multicast_filters);
3232 case VLAN_HEADER_INSERTION:
3233 adapter->vlan_header_insertion =
3234 be64_to_cpu(crq->query_capability.number);
3235 if (adapter->vlan_header_insertion)
3236 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3237 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3238 adapter->vlan_header_insertion);
3240 case RX_VLAN_HEADER_INSERTION:
3241 adapter->rx_vlan_header_insertion =
3242 be64_to_cpu(crq->query_capability.number);
3243 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3244 adapter->rx_vlan_header_insertion);
3246 case MAX_TX_SG_ENTRIES:
3247 adapter->max_tx_sg_entries =
3248 be64_to_cpu(crq->query_capability.number);
3249 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3250 adapter->max_tx_sg_entries);
3252 case RX_SG_SUPPORTED:
3253 adapter->rx_sg_supported =
3254 be64_to_cpu(crq->query_capability.number);
3255 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3256 adapter->rx_sg_supported);
3258 case OPT_TX_COMP_SUB_QUEUES:
3259 adapter->opt_tx_comp_sub_queues =
3260 be64_to_cpu(crq->query_capability.number);
3261 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3262 adapter->opt_tx_comp_sub_queues);
3264 case OPT_RX_COMP_QUEUES:
3265 adapter->opt_rx_comp_queues =
3266 be64_to_cpu(crq->query_capability.number);
3267 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3268 adapter->opt_rx_comp_queues);
3270 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3271 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3272 be64_to_cpu(crq->query_capability.number);
3273 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3274 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3276 case OPT_TX_ENTRIES_PER_SUBCRQ:
3277 adapter->opt_tx_entries_per_subcrq =
3278 be64_to_cpu(crq->query_capability.number);
3279 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3280 adapter->opt_tx_entries_per_subcrq);
3282 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3283 adapter->opt_rxba_entries_per_subcrq =
3284 be64_to_cpu(crq->query_capability.number);
3285 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3286 adapter->opt_rxba_entries_per_subcrq);
3288 case TX_RX_DESC_REQ:
3289 adapter->tx_rx_desc_req = crq->query_capability.number;
3290 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3291 adapter->tx_rx_desc_req);
3295 netdev_err(netdev, "Got invalid cap rsp %d\n",
3296 crq->query_capability.capability);
3300 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3301 adapter->wait_capability = false;
3302 ibmvnic_send_req_caps(adapter, 0);
3306 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3307 struct ibmvnic_adapter *adapter)
3309 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3310 struct net_device *netdev = adapter->netdev;
3311 struct device *dev = &adapter->vdev->dev;
3312 u64 *u64_crq = (u64 *)crq;
3315 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3316 (unsigned long int)cpu_to_be64(u64_crq[0]),
3317 (unsigned long int)cpu_to_be64(u64_crq[1]));
3318 switch (gen_crq->first) {
3319 case IBMVNIC_CRQ_INIT_RSP:
3320 switch (gen_crq->cmd) {
3321 case IBMVNIC_CRQ_INIT:
3322 dev_info(dev, "Partner initialized\n");
3323 adapter->from_passive_init = true;
3324 complete(&adapter->init_done);
3326 case IBMVNIC_CRQ_INIT_COMPLETE:
3327 dev_info(dev, "Partner initialization complete\n");
3328 send_version_xchg(adapter);
3331 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3334 case IBMVNIC_CRQ_XPORT_EVENT:
3335 netif_carrier_off(netdev);
3336 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3337 dev_info(dev, "Migrated, re-enabling adapter\n");
3338 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
3339 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3340 dev_info(dev, "Backing device failover detected\n");
3341 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
3343 /* The adapter lost the connection */
3344 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3346 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3349 case IBMVNIC_CRQ_CMD_RSP:
3352 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3357 switch (gen_crq->cmd) {
3358 case VERSION_EXCHANGE_RSP:
3359 rc = crq->version_exchange_rsp.rc.code;
3361 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3364 dev_info(dev, "Partner protocol version is %d\n",
3365 crq->version_exchange_rsp.version);
3366 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3369 be16_to_cpu(crq->version_exchange_rsp.version);
3370 send_cap_queries(adapter);
3372 case QUERY_CAPABILITY_RSP:
3373 handle_query_cap_rsp(crq, adapter);
3376 handle_query_map_rsp(crq, adapter);
3378 case REQUEST_MAP_RSP:
3379 handle_request_map_rsp(crq, adapter);
3381 case REQUEST_UNMAP_RSP:
3382 handle_request_unmap_rsp(crq, adapter);
3384 case REQUEST_CAPABILITY_RSP:
3385 handle_request_cap_rsp(crq, adapter);
3388 netdev_dbg(netdev, "Got Login Response\n");
3389 handle_login_rsp(crq, adapter);
3391 case LOGICAL_LINK_STATE_RSP:
3393 "Got Logical Link State Response, state: %d rc: %d\n",
3394 crq->logical_link_state_rsp.link_state,
3395 crq->logical_link_state_rsp.rc.code);
3396 adapter->logical_link_state =
3397 crq->logical_link_state_rsp.link_state;
3398 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3399 complete(&adapter->init_done);
3401 case LINK_STATE_INDICATION:
3402 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3403 adapter->phys_link_state =
3404 crq->link_state_indication.phys_link_state;
3405 adapter->logical_link_state =
3406 crq->link_state_indication.logical_link_state;
3408 case CHANGE_MAC_ADDR_RSP:
3409 netdev_dbg(netdev, "Got MAC address change Response\n");
3410 handle_change_mac_rsp(crq, adapter);
3412 case ERROR_INDICATION:
3413 netdev_dbg(netdev, "Got Error Indication\n");
3414 handle_error_indication(crq, adapter);
3416 case REQUEST_ERROR_RSP:
3417 netdev_dbg(netdev, "Got Error Detail Response\n");
3418 handle_error_info_rsp(crq, adapter);
3420 case REQUEST_STATISTICS_RSP:
3421 netdev_dbg(netdev, "Got Statistics Response\n");
3422 complete(&adapter->stats_done);
3424 case QUERY_IP_OFFLOAD_RSP:
3425 netdev_dbg(netdev, "Got Query IP offload Response\n");
3426 handle_query_ip_offload_rsp(adapter);
3428 case MULTICAST_CTRL_RSP:
3429 netdev_dbg(netdev, "Got multicast control Response\n");
3431 case CONTROL_IP_OFFLOAD_RSP:
3432 netdev_dbg(netdev, "Got Control IP offload Response\n");
3433 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3434 sizeof(adapter->ip_offload_ctrl),
3436 complete(&adapter->init_done);
3438 case COLLECT_FW_TRACE_RSP:
3439 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3440 complete(&adapter->fw_done);
3443 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3448 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3450 struct ibmvnic_adapter *adapter = instance;
3452 tasklet_schedule(&adapter->tasklet);
3456 static void ibmvnic_tasklet(void *data)
3458 struct ibmvnic_adapter *adapter = data;
3459 struct ibmvnic_crq_queue *queue = &adapter->crq;
3460 union ibmvnic_crq *crq;
3461 unsigned long flags;
3464 spin_lock_irqsave(&queue->lock, flags);
3466 /* Pull all the valid messages off the CRQ */
3467 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3468 ibmvnic_handle_crq(crq, adapter);
3469 crq->generic.first = 0;
3472 /* remain in tasklet until all
3473 * capabilities responses are received
3475 if (!adapter->wait_capability)
3478 /* if capabilities CRQ's were sent in this tasklet, the following
3479 * tasklet must wait until all responses are received
3481 if (atomic_read(&adapter->running_cap_crqs) != 0)
3482 adapter->wait_capability = true;
3483 spin_unlock_irqrestore(&queue->lock, flags);
3486 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3488 struct vio_dev *vdev = adapter->vdev;
3492 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3493 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3496 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3501 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3503 struct ibmvnic_crq_queue *crq = &adapter->crq;
3504 struct device *dev = &adapter->vdev->dev;
3505 struct vio_dev *vdev = adapter->vdev;
3510 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3511 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3513 /* Clean out the queue */
3514 memset(crq->msgs, 0, PAGE_SIZE);
3517 /* And re-open it again */
3518 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3519 crq->msg_token, PAGE_SIZE);
3522 /* Adapter is good, but other end is not ready */
3523 dev_warn(dev, "Partner adapter not ready\n");
3525 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3530 static void release_crq_queue(struct ibmvnic_adapter *adapter)
3532 struct ibmvnic_crq_queue *crq = &adapter->crq;
3533 struct vio_dev *vdev = adapter->vdev;
3539 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3540 free_irq(vdev->irq, adapter);
3541 tasklet_kill(&adapter->tasklet);
3543 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3544 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3546 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3548 free_page((unsigned long)crq->msgs);
3552 static int init_crq_queue(struct ibmvnic_adapter *adapter)
3554 struct ibmvnic_crq_queue *crq = &adapter->crq;
3555 struct device *dev = &adapter->vdev->dev;
3556 struct vio_dev *vdev = adapter->vdev;
3557 int rc, retrc = -ENOMEM;
3562 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3563 /* Should we allocate more than one page? */
3568 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3569 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3571 if (dma_mapping_error(dev, crq->msg_token))
3574 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3575 crq->msg_token, PAGE_SIZE);
3577 if (rc == H_RESOURCE)
3578 /* maybe kexecing and resource is busy. try a reset */
3579 rc = ibmvnic_reset_crq(adapter);
3582 if (rc == H_CLOSED) {
3583 dev_warn(dev, "Partner adapter not ready\n");
3585 dev_warn(dev, "Error %d opening adapter\n", rc);
3586 goto reg_crq_failed;
3591 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3592 (unsigned long)adapter);
3594 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3595 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3598 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3600 goto req_irq_failed;
3603 rc = vio_enable_interrupts(vdev);
3605 dev_err(dev, "Error %d enabling interrupts\n", rc);
3606 goto req_irq_failed;
3610 spin_lock_init(&crq->lock);
3615 tasklet_kill(&adapter->tasklet);
3617 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3618 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3620 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3622 free_page((unsigned long)crq->msgs);
3627 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3629 struct device *dev = &adapter->vdev->dev;
3630 unsigned long timeout = msecs_to_jiffies(30000);
3633 if (adapter->resetting) {
3634 rc = ibmvnic_reset_crq(adapter);
3636 rc = vio_enable_interrupts(adapter->vdev);
3638 rc = init_crq_queue(adapter);
3642 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3646 adapter->from_passive_init = false;
3648 init_completion(&adapter->init_done);
3649 ibmvnic_send_crq_init(adapter);
3650 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3651 dev_err(dev, "Initialization sequence timed out\n");
3655 if (adapter->from_passive_init) {
3656 adapter->state = VNIC_OPEN;
3657 adapter->from_passive_init = false;
3661 if (adapter->resetting)
3662 rc = reset_sub_crq_queues(adapter);
3664 rc = init_sub_crqs(adapter);
3666 dev_err(dev, "Initialization of sub crqs failed\n");
3667 release_crq_queue(adapter);
3673 static struct device_attribute dev_attr_failover;
3675 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3677 struct ibmvnic_adapter *adapter;
3678 struct net_device *netdev;
3679 unsigned char *mac_addr_p;
3682 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3685 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3686 VETH_MAC_ADDR, NULL);
3689 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3690 __FILE__, __LINE__);
3694 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3695 IBMVNIC_MAX_TX_QUEUES);
3699 adapter = netdev_priv(netdev);
3700 adapter->state = VNIC_PROBING;
3701 dev_set_drvdata(&dev->dev, netdev);
3702 adapter->vdev = dev;
3703 adapter->netdev = netdev;
3705 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3706 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3707 netdev->irq = dev->irq;
3708 netdev->netdev_ops = &ibmvnic_netdev_ops;
3709 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3710 SET_NETDEV_DEV(netdev, &dev->dev);
3712 spin_lock_init(&adapter->stats_lock);
3714 INIT_LIST_HEAD(&adapter->errors);
3715 spin_lock_init(&adapter->error_list_lock);
3717 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3718 INIT_LIST_HEAD(&adapter->rwi_list);
3719 mutex_init(&adapter->reset_lock);
3720 mutex_init(&adapter->rwi_lock);
3721 adapter->resetting = false;
3723 rc = ibmvnic_init(adapter);
3725 free_netdev(netdev);
3729 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3731 rc = device_create_file(&dev->dev, &dev_attr_failover);
3733 free_netdev(netdev);
3737 rc = register_netdev(netdev);
3739 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
3740 device_remove_file(&dev->dev, &dev_attr_failover);
3741 free_netdev(netdev);
3744 dev_info(&dev->dev, "ibmvnic registered\n");
3746 adapter->state = VNIC_PROBED;
3750 static int ibmvnic_remove(struct vio_dev *dev)
3752 struct net_device *netdev = dev_get_drvdata(&dev->dev);
3753 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3755 adapter->state = VNIC_REMOVING;
3756 unregister_netdev(netdev);
3757 mutex_lock(&adapter->reset_lock);
3759 release_resources(adapter);
3760 release_sub_crqs(adapter);
3761 release_crq_queue(adapter);
3763 adapter->state = VNIC_REMOVED;
3765 mutex_unlock(&adapter->reset_lock);
3766 device_remove_file(&dev->dev, &dev_attr_failover);
3767 free_netdev(netdev);
3768 dev_set_drvdata(&dev->dev, NULL);
3773 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3774 const char *buf, size_t count)
3776 struct net_device *netdev = dev_get_drvdata(dev);
3777 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3778 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3779 __be64 session_token;
3782 if (!sysfs_streq(buf, "1"))
3785 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3786 H_GET_SESSION_TOKEN, 0, 0, 0);
3788 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3793 session_token = (__be64)retbuf[0];
3794 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3795 be64_to_cpu(session_token));
3796 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3797 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3799 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3807 static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3809 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3811 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3812 struct ibmvnic_adapter *adapter;
3813 struct iommu_table *tbl;
3814 unsigned long ret = 0;
3817 tbl = get_iommu_table_base(&vdev->dev);
3819 /* netdev inits at probe time along with the structures we need below*/
3821 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3823 adapter = netdev_priv(netdev);
3825 ret += PAGE_SIZE; /* the crq message queue */
3826 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3828 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3829 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3831 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3833 ret += adapter->rx_pool[i].size *
3834 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3839 static int ibmvnic_resume(struct device *dev)
3841 struct net_device *netdev = dev_get_drvdata(dev);
3842 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3845 /* kick the interrupt handlers just in case we lost an interrupt */
3846 for (i = 0; i < adapter->req_rx_queues; i++)
3847 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3848 adapter->rx_scrq[i]);
3853 static struct vio_device_id ibmvnic_device_table[] = {
3854 {"network", "IBM,vnic"},
3857 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3859 static const struct dev_pm_ops ibmvnic_pm_ops = {
3860 .resume = ibmvnic_resume
3863 static struct vio_driver ibmvnic_driver = {
3864 .id_table = ibmvnic_device_table,
3865 .probe = ibmvnic_probe,
3866 .remove = ibmvnic_remove,
3867 .get_desired_dma = ibmvnic_get_desired_dma,
3868 .name = ibmvnic_driver_name,
3869 .pm = &ibmvnic_pm_ops,
3872 /* module functions */
3873 static int __init ibmvnic_module_init(void)
3875 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3876 IBMVNIC_DRIVER_VERSION);
3878 return vio_register_driver(&ibmvnic_driver);
3881 static void __exit ibmvnic_module_exit(void)
3883 vio_unregister_driver(&ibmvnic_driver);
3886 module_init(ibmvnic_module_init);
3887 module_exit(ibmvnic_module_exit);