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 int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
167 struct ibmvnic_long_term_buff *ltb, int size)
169 struct device *dev = &adapter->vdev->dev;
172 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
176 dev_err(dev, "Couldn't alloc long term buffer\n");
179 ltb->map_id = adapter->map_id;
182 init_completion(&adapter->fw_done);
183 send_request_map(adapter, ltb->addr,
184 ltb->size, ltb->map_id);
185 wait_for_completion(&adapter->fw_done);
187 if (adapter->fw_done_rc) {
188 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
189 adapter->fw_done_rc);
195 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
196 struct ibmvnic_long_term_buff *ltb)
198 struct device *dev = &adapter->vdev->dev;
203 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
204 adapter->reset_reason != VNIC_RESET_MOBILITY)
205 send_request_unmap(adapter, ltb->map_id);
206 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
209 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
210 struct ibmvnic_long_term_buff *ltb)
212 memset(ltb->buff, 0, ltb->size);
214 init_completion(&adapter->fw_done);
215 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
216 wait_for_completion(&adapter->fw_done);
218 if (adapter->fw_done_rc) {
219 dev_info(&adapter->vdev->dev,
220 "Reset failed, attempting to free and reallocate buffer\n");
221 free_long_term_buff(adapter, ltb);
222 return alloc_long_term_buff(adapter, ltb, ltb->size);
227 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
231 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
233 adapter->rx_pool[i].active = 0;
236 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
237 struct ibmvnic_rx_pool *pool)
239 int count = pool->size - atomic_read(&pool->available);
240 struct device *dev = &adapter->vdev->dev;
241 int buffers_added = 0;
242 unsigned long lpar_rc;
243 union sub_crq sub_crq;
256 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
257 be32_to_cpu(adapter->login_rsp_buf->
260 for (i = 0; i < count; ++i) {
261 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
263 dev_err(dev, "Couldn't replenish rx buff\n");
264 adapter->replenish_no_mem++;
268 index = pool->free_map[pool->next_free];
270 if (pool->rx_buff[index].skb)
271 dev_err(dev, "Inconsistent free_map!\n");
273 /* Copy the skb to the long term mapped DMA buffer */
274 offset = index * pool->buff_size;
275 dst = pool->long_term_buff.buff + offset;
276 memset(dst, 0, pool->buff_size);
277 dma_addr = pool->long_term_buff.addr + offset;
278 pool->rx_buff[index].data = dst;
280 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
281 pool->rx_buff[index].dma = dma_addr;
282 pool->rx_buff[index].skb = skb;
283 pool->rx_buff[index].pool_index = pool->index;
284 pool->rx_buff[index].size = pool->buff_size;
286 memset(&sub_crq, 0, sizeof(sub_crq));
287 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
288 sub_crq.rx_add.correlator =
289 cpu_to_be64((u64)&pool->rx_buff[index]);
290 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
291 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
293 /* The length field of the sCRQ is defined to be 24 bits so the
294 * buffer size needs to be left shifted by a byte before it is
295 * converted to big endian to prevent the last byte from being
298 #ifdef __LITTLE_ENDIAN__
301 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
303 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
305 if (lpar_rc != H_SUCCESS)
309 adapter->replenish_add_buff_success++;
310 pool->next_free = (pool->next_free + 1) % pool->size;
312 atomic_add(buffers_added, &pool->available);
316 dev_info(dev, "replenish pools failure\n");
317 pool->free_map[pool->next_free] = index;
318 pool->rx_buff[index].skb = NULL;
319 if (!dma_mapping_error(dev, dma_addr))
320 dma_unmap_single(dev, dma_addr, pool->buff_size,
323 dev_kfree_skb_any(skb);
324 adapter->replenish_add_buff_failure++;
325 atomic_add(buffers_added, &pool->available);
327 if (lpar_rc == H_CLOSED) {
328 /* Disable buffer pool replenishment and report carrier off if
329 * queue is closed. Firmware guarantees that a signal will
330 * be sent to the driver, triggering a reset.
332 deactivate_rx_pools(adapter);
333 netif_carrier_off(adapter->netdev);
337 static void replenish_pools(struct ibmvnic_adapter *adapter)
341 adapter->replenish_task_cycles++;
342 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
344 if (adapter->rx_pool[i].active)
345 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
349 static void release_stats_token(struct ibmvnic_adapter *adapter)
351 struct device *dev = &adapter->vdev->dev;
353 if (!adapter->stats_token)
356 dma_unmap_single(dev, adapter->stats_token,
357 sizeof(struct ibmvnic_statistics),
359 adapter->stats_token = 0;
362 static int init_stats_token(struct ibmvnic_adapter *adapter)
364 struct device *dev = &adapter->vdev->dev;
367 stok = dma_map_single(dev, &adapter->stats,
368 sizeof(struct ibmvnic_statistics),
370 if (dma_mapping_error(dev, stok)) {
371 dev_err(dev, "Couldn't map stats buffer\n");
375 adapter->stats_token = stok;
379 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
381 struct ibmvnic_rx_pool *rx_pool;
385 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
386 for (i = 0; i < rx_scrqs; i++) {
387 rx_pool = &adapter->rx_pool[i];
389 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
393 for (j = 0; j < rx_pool->size; j++)
394 rx_pool->free_map[j] = j;
396 memset(rx_pool->rx_buff, 0,
397 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
399 atomic_set(&rx_pool->available, 0);
400 rx_pool->next_alloc = 0;
401 rx_pool->next_free = 0;
408 static void release_rx_pools(struct ibmvnic_adapter *adapter)
410 struct ibmvnic_rx_pool *rx_pool;
414 if (!adapter->rx_pool)
417 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
418 for (i = 0; i < rx_scrqs; i++) {
419 rx_pool = &adapter->rx_pool[i];
421 kfree(rx_pool->free_map);
422 free_long_term_buff(adapter, &rx_pool->long_term_buff);
424 if (!rx_pool->rx_buff)
427 for (j = 0; j < rx_pool->size; j++) {
428 if (rx_pool->rx_buff[j].skb) {
429 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
430 rx_pool->rx_buff[i].skb = NULL;
434 kfree(rx_pool->rx_buff);
437 kfree(adapter->rx_pool);
438 adapter->rx_pool = NULL;
441 static int init_rx_pools(struct net_device *netdev)
443 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
444 struct device *dev = &adapter->vdev->dev;
445 struct ibmvnic_rx_pool *rx_pool;
451 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
452 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
453 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
455 adapter->rx_pool = kcalloc(rxadd_subcrqs,
456 sizeof(struct ibmvnic_rx_pool),
458 if (!adapter->rx_pool) {
459 dev_err(dev, "Failed to allocate rx pools\n");
463 for (i = 0; i < rxadd_subcrqs; i++) {
464 rx_pool = &adapter->rx_pool[i];
466 netdev_dbg(adapter->netdev,
467 "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
468 i, adapter->req_rx_add_entries_per_subcrq,
469 be64_to_cpu(size_array[i]));
471 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
473 rx_pool->buff_size = be64_to_cpu(size_array[i]);
476 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
478 if (!rx_pool->free_map) {
479 release_rx_pools(adapter);
483 rx_pool->rx_buff = kcalloc(rx_pool->size,
484 sizeof(struct ibmvnic_rx_buff),
486 if (!rx_pool->rx_buff) {
487 dev_err(dev, "Couldn't alloc rx buffers\n");
488 release_rx_pools(adapter);
492 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
493 rx_pool->size * rx_pool->buff_size)) {
494 release_rx_pools(adapter);
498 for (j = 0; j < rx_pool->size; ++j)
499 rx_pool->free_map[j] = j;
501 atomic_set(&rx_pool->available, 0);
502 rx_pool->next_alloc = 0;
503 rx_pool->next_free = 0;
509 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
511 struct ibmvnic_tx_pool *tx_pool;
515 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
516 for (i = 0; i < tx_scrqs; i++) {
517 tx_pool = &adapter->tx_pool[i];
519 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
523 memset(tx_pool->tx_buff, 0,
524 adapter->req_tx_entries_per_subcrq *
525 sizeof(struct ibmvnic_tx_buff));
527 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
528 tx_pool->free_map[j] = j;
530 tx_pool->consumer_index = 0;
531 tx_pool->producer_index = 0;
537 static void release_tx_pools(struct ibmvnic_adapter *adapter)
539 struct ibmvnic_tx_pool *tx_pool;
542 if (!adapter->tx_pool)
545 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
546 for (i = 0; i < tx_scrqs; i++) {
547 tx_pool = &adapter->tx_pool[i];
548 kfree(tx_pool->tx_buff);
549 free_long_term_buff(adapter, &tx_pool->long_term_buff);
550 kfree(tx_pool->free_map);
553 kfree(adapter->tx_pool);
554 adapter->tx_pool = NULL;
557 static int init_tx_pools(struct net_device *netdev)
559 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
560 struct device *dev = &adapter->vdev->dev;
561 struct ibmvnic_tx_pool *tx_pool;
565 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
566 adapter->tx_pool = kcalloc(tx_subcrqs,
567 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
568 if (!adapter->tx_pool)
571 for (i = 0; i < tx_subcrqs; i++) {
572 tx_pool = &adapter->tx_pool[i];
573 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
574 sizeof(struct ibmvnic_tx_buff),
576 if (!tx_pool->tx_buff) {
577 dev_err(dev, "tx pool buffer allocation failed\n");
578 release_tx_pools(adapter);
582 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
583 adapter->req_tx_entries_per_subcrq *
585 release_tx_pools(adapter);
589 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
590 sizeof(int), GFP_KERNEL);
591 if (!tx_pool->free_map) {
592 release_tx_pools(adapter);
596 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
597 tx_pool->free_map[j] = j;
599 tx_pool->consumer_index = 0;
600 tx_pool->producer_index = 0;
606 static void release_error_buffers(struct ibmvnic_adapter *adapter)
608 struct device *dev = &adapter->vdev->dev;
609 struct ibmvnic_error_buff *error_buff, *tmp;
612 spin_lock_irqsave(&adapter->error_list_lock, flags);
613 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
614 list_del(&error_buff->list);
615 dma_unmap_single(dev, error_buff->dma, error_buff->len,
617 kfree(error_buff->buff);
620 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
623 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
627 if (adapter->napi_enabled)
630 for (i = 0; i < adapter->req_rx_queues; i++)
631 napi_enable(&adapter->napi[i]);
633 adapter->napi_enabled = true;
636 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
640 if (!adapter->napi_enabled)
643 for (i = 0; i < adapter->req_rx_queues; i++)
644 napi_disable(&adapter->napi[i]);
646 adapter->napi_enabled = false;
649 static int ibmvnic_login(struct net_device *netdev)
651 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
652 unsigned long timeout = msecs_to_jiffies(30000);
653 struct device *dev = &adapter->vdev->dev;
656 if (adapter->renegotiate) {
657 adapter->renegotiate = false;
658 release_sub_crqs(adapter);
660 reinit_completion(&adapter->init_done);
661 send_cap_queries(adapter);
662 if (!wait_for_completion_timeout(&adapter->init_done,
664 dev_err(dev, "Capabilities query timeout\n");
669 reinit_completion(&adapter->init_done);
671 if (!wait_for_completion_timeout(&adapter->init_done,
673 dev_err(dev, "Login timeout\n");
676 } while (adapter->renegotiate);
681 static void release_resources(struct ibmvnic_adapter *adapter)
685 release_tx_pools(adapter);
686 release_rx_pools(adapter);
688 release_stats_token(adapter);
689 release_error_buffers(adapter);
692 for (i = 0; i < adapter->req_rx_queues; i++) {
693 if (&adapter->napi[i])
694 netif_napi_del(&adapter->napi[i]);
699 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
701 struct net_device *netdev = adapter->netdev;
702 unsigned long timeout = msecs_to_jiffies(30000);
703 union ibmvnic_crq crq;
707 netdev_err(netdev, "setting link state %d\n", link_state);
708 memset(&crq, 0, sizeof(crq));
709 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
710 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
711 crq.logical_link_state.link_state = link_state;
716 reinit_completion(&adapter->init_done);
717 rc = ibmvnic_send_crq(adapter, &crq);
719 netdev_err(netdev, "Failed to set link state\n");
723 if (!wait_for_completion_timeout(&adapter->init_done,
725 netdev_err(netdev, "timeout setting link state\n");
729 if (adapter->init_done_rc == 1) {
730 /* Partuial success, delay and re-send */
739 static int set_real_num_queues(struct net_device *netdev)
741 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
744 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
746 netdev_err(netdev, "failed to set the number of tx queues\n");
750 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
752 netdev_err(netdev, "failed to set the number of rx queues\n");
757 static int init_resources(struct ibmvnic_adapter *adapter)
759 struct net_device *netdev = adapter->netdev;
762 rc = set_real_num_queues(netdev);
766 rc = init_sub_crq_irqs(adapter);
768 netdev_err(netdev, "failed to initialize sub crq irqs\n");
772 rc = init_stats_token(adapter);
777 adapter->napi = kcalloc(adapter->req_rx_queues,
778 sizeof(struct napi_struct), GFP_KERNEL);
782 for (i = 0; i < adapter->req_rx_queues; i++) {
783 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
787 send_map_query(adapter);
789 rc = init_rx_pools(netdev);
793 rc = init_tx_pools(netdev);
797 static int __ibmvnic_open(struct net_device *netdev)
799 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
800 enum vnic_state prev_state = adapter->state;
803 adapter->state = VNIC_OPENING;
804 replenish_pools(adapter);
805 ibmvnic_napi_enable(adapter);
807 /* We're ready to receive frames, enable the sub-crq interrupts and
808 * set the logical link state to up
810 for (i = 0; i < adapter->req_rx_queues; i++) {
811 if (prev_state == VNIC_CLOSED)
812 enable_irq(adapter->rx_scrq[i]->irq);
814 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
817 for (i = 0; i < adapter->req_tx_queues; i++) {
818 if (prev_state == VNIC_CLOSED)
819 enable_irq(adapter->tx_scrq[i]->irq);
821 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
824 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
826 for (i = 0; i < adapter->req_rx_queues; i++)
827 napi_disable(&adapter->napi[i]);
828 release_resources(adapter);
832 netif_tx_start_all_queues(netdev);
834 if (prev_state == VNIC_CLOSED) {
835 for (i = 0; i < adapter->req_rx_queues; i++)
836 napi_schedule(&adapter->napi[i]);
839 adapter->state = VNIC_OPEN;
843 static int ibmvnic_open(struct net_device *netdev)
845 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
848 mutex_lock(&adapter->reset_lock);
850 if (adapter->state != VNIC_CLOSED) {
851 rc = ibmvnic_login(netdev);
853 mutex_unlock(&adapter->reset_lock);
857 rc = init_resources(adapter);
859 netdev_err(netdev, "failed to initialize resources\n");
860 release_resources(adapter);
861 mutex_unlock(&adapter->reset_lock);
866 rc = __ibmvnic_open(netdev);
867 mutex_unlock(&adapter->reset_lock);
872 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
874 struct ibmvnic_tx_pool *tx_pool;
879 if (!adapter->tx_pool)
882 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
883 tx_entries = adapter->req_tx_entries_per_subcrq;
885 /* Free any remaining skbs in the tx buffer pools */
886 for (i = 0; i < tx_scrqs; i++) {
887 tx_pool = &adapter->tx_pool[i];
891 for (j = 0; j < tx_entries; j++) {
892 if (tx_pool->tx_buff[j].skb) {
893 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
894 tx_pool->tx_buff[j].skb = NULL;
900 static int __ibmvnic_close(struct net_device *netdev)
902 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
906 adapter->state = VNIC_CLOSING;
908 /* ensure that transmissions are stopped if called by do_reset */
909 if (adapter->resetting)
910 netif_tx_disable(netdev);
912 netif_tx_stop_all_queues(netdev);
914 ibmvnic_napi_disable(adapter);
916 if (adapter->tx_scrq) {
917 for (i = 0; i < adapter->req_tx_queues; i++)
918 if (adapter->tx_scrq[i]->irq)
919 disable_irq(adapter->tx_scrq[i]->irq);
922 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
926 if (adapter->rx_scrq) {
927 for (i = 0; i < adapter->req_rx_queues; i++) {
930 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
938 if (adapter->rx_scrq[i]->irq)
939 disable_irq(adapter->rx_scrq[i]->irq);
943 clean_tx_pools(adapter);
944 adapter->state = VNIC_CLOSED;
948 static int ibmvnic_close(struct net_device *netdev)
950 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
953 mutex_lock(&adapter->reset_lock);
954 rc = __ibmvnic_close(netdev);
955 mutex_unlock(&adapter->reset_lock);
961 * build_hdr_data - creates L2/L3/L4 header data buffer
962 * @hdr_field - bitfield determining needed headers
963 * @skb - socket buffer
964 * @hdr_len - array of header lengths
965 * @tot_len - total length of data
967 * Reads hdr_field to determine which headers are needed by firmware.
968 * Builds a buffer containing these headers. Saves individual header
969 * lengths and total buffer length to be used to build descriptors.
971 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
972 int *hdr_len, u8 *hdr_data)
977 hdr_len[0] = sizeof(struct ethhdr);
979 if (skb->protocol == htons(ETH_P_IP)) {
980 hdr_len[1] = ip_hdr(skb)->ihl * 4;
981 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
982 hdr_len[2] = tcp_hdrlen(skb);
983 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
984 hdr_len[2] = sizeof(struct udphdr);
985 } else if (skb->protocol == htons(ETH_P_IPV6)) {
986 hdr_len[1] = sizeof(struct ipv6hdr);
987 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
988 hdr_len[2] = tcp_hdrlen(skb);
989 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
990 hdr_len[2] = sizeof(struct udphdr);
993 memset(hdr_data, 0, 120);
994 if ((hdr_field >> 6) & 1) {
995 hdr = skb_mac_header(skb);
996 memcpy(hdr_data, hdr, hdr_len[0]);
1000 if ((hdr_field >> 5) & 1) {
1001 hdr = skb_network_header(skb);
1002 memcpy(hdr_data + len, hdr, hdr_len[1]);
1006 if ((hdr_field >> 4) & 1) {
1007 hdr = skb_transport_header(skb);
1008 memcpy(hdr_data + len, hdr, hdr_len[2]);
1015 * create_hdr_descs - create header and header extension descriptors
1016 * @hdr_field - bitfield determining needed headers
1017 * @data - buffer containing header data
1018 * @len - length of data buffer
1019 * @hdr_len - array of individual header lengths
1020 * @scrq_arr - descriptor array
1022 * Creates header and, if needed, header extension descriptors and
1023 * places them in a descriptor array, scrq_arr
1026 static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1027 union sub_crq *scrq_arr)
1029 union sub_crq hdr_desc;
1034 while (tmp_len > 0) {
1035 cur = hdr_data + len - tmp_len;
1037 memset(&hdr_desc, 0, sizeof(hdr_desc));
1038 if (cur != hdr_data) {
1039 data = hdr_desc.hdr_ext.data;
1040 tmp = tmp_len > 29 ? 29 : tmp_len;
1041 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1042 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1043 hdr_desc.hdr_ext.len = tmp;
1045 data = hdr_desc.hdr.data;
1046 tmp = tmp_len > 24 ? 24 : tmp_len;
1047 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1048 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1049 hdr_desc.hdr.len = tmp;
1050 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1051 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1052 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1053 hdr_desc.hdr.flag = hdr_field << 1;
1055 memcpy(data, cur, tmp);
1057 *scrq_arr = hdr_desc;
1063 * build_hdr_descs_arr - build a header descriptor array
1064 * @skb - socket buffer
1065 * @num_entries - number of descriptors to be sent
1066 * @subcrq - first TX descriptor
1067 * @hdr_field - bit field determining which headers will be sent
1069 * This function will build a TX descriptor array with applicable
1070 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1073 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1074 int *num_entries, u8 hdr_field)
1076 int hdr_len[3] = {0, 0, 0};
1078 u8 *hdr_data = txbuff->hdr_data;
1080 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1085 num_entries += len % 29 ? len / 29 + 1 : len / 29;
1086 create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1087 txbuff->indir_arr + 1);
1090 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1092 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1093 int queue_num = skb_get_queue_mapping(skb);
1094 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1095 struct device *dev = &adapter->vdev->dev;
1096 struct ibmvnic_tx_buff *tx_buff = NULL;
1097 struct ibmvnic_sub_crq_queue *tx_scrq;
1098 struct ibmvnic_tx_pool *tx_pool;
1099 unsigned int tx_send_failed = 0;
1100 unsigned int tx_map_failed = 0;
1101 unsigned int tx_dropped = 0;
1102 unsigned int tx_packets = 0;
1103 unsigned int tx_bytes = 0;
1104 dma_addr_t data_dma_addr;
1105 struct netdev_queue *txq;
1106 unsigned long lpar_rc;
1107 union sub_crq tx_crq;
1108 unsigned int offset;
1109 int num_entries = 1;
1115 if (adapter->resetting) {
1116 if (!netif_subqueue_stopped(netdev, skb))
1117 netif_stop_subqueue(netdev, queue_num);
1118 dev_kfree_skb_any(skb);
1126 tx_pool = &adapter->tx_pool[queue_num];
1127 tx_scrq = adapter->tx_scrq[queue_num];
1128 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1129 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1130 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1132 index = tx_pool->free_map[tx_pool->consumer_index];
1133 offset = index * adapter->req_mtu;
1134 dst = tx_pool->long_term_buff.buff + offset;
1135 memset(dst, 0, adapter->req_mtu);
1136 skb_copy_from_linear_data(skb, dst, skb->len);
1137 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1139 tx_pool->consumer_index =
1140 (tx_pool->consumer_index + 1) %
1141 adapter->req_tx_entries_per_subcrq;
1143 tx_buff = &tx_pool->tx_buff[index];
1145 tx_buff->data_dma[0] = data_dma_addr;
1146 tx_buff->data_len[0] = skb->len;
1147 tx_buff->index = index;
1148 tx_buff->pool_index = queue_num;
1149 tx_buff->last_frag = true;
1151 memset(&tx_crq, 0, sizeof(tx_crq));
1152 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1153 tx_crq.v1.type = IBMVNIC_TX_DESC;
1154 tx_crq.v1.n_crq_elem = 1;
1155 tx_crq.v1.n_sge = 1;
1156 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1157 tx_crq.v1.correlator = cpu_to_be32(index);
1158 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1159 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1160 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1162 if (adapter->vlan_header_insertion) {
1163 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1164 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1167 if (skb->protocol == htons(ETH_P_IP)) {
1168 if (ip_hdr(skb)->version == 4)
1169 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1170 else if (ip_hdr(skb)->version == 6)
1171 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1173 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1174 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1175 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1176 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1179 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1180 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1183 /* determine if l2/3/4 headers are sent to firmware */
1184 if ((*hdrs >> 7) & 1 &&
1185 (skb->protocol == htons(ETH_P_IP) ||
1186 skb->protocol == htons(ETH_P_IPV6))) {
1187 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1188 tx_crq.v1.n_crq_elem = num_entries;
1189 tx_buff->indir_arr[0] = tx_crq;
1190 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1191 sizeof(tx_buff->indir_arr),
1193 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1194 dev_kfree_skb_any(skb);
1195 tx_buff->skb = NULL;
1196 if (!firmware_has_feature(FW_FEATURE_CMO))
1197 dev_err(dev, "tx: unable to map descriptor array\n");
1203 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1204 (u64)tx_buff->indir_dma,
1207 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1210 if (lpar_rc != H_SUCCESS) {
1211 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1213 if (tx_pool->consumer_index == 0)
1214 tx_pool->consumer_index =
1215 adapter->req_tx_entries_per_subcrq - 1;
1217 tx_pool->consumer_index--;
1219 dev_kfree_skb_any(skb);
1220 tx_buff->skb = NULL;
1222 if (lpar_rc == H_CLOSED) {
1223 /* Disable TX and report carrier off if queue is closed.
1224 * Firmware guarantees that a signal will be sent to the
1225 * driver, triggering a reset or some other action.
1227 netif_tx_stop_all_queues(netdev);
1228 netif_carrier_off(netdev);
1237 if (atomic_inc_return(&tx_scrq->used)
1238 >= adapter->req_tx_entries_per_subcrq) {
1239 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1240 netif_stop_subqueue(netdev, queue_num);
1244 tx_bytes += skb->len;
1245 txq->trans_start = jiffies;
1249 netdev->stats.tx_dropped += tx_dropped;
1250 netdev->stats.tx_bytes += tx_bytes;
1251 netdev->stats.tx_packets += tx_packets;
1252 adapter->tx_send_failed += tx_send_failed;
1253 adapter->tx_map_failed += tx_map_failed;
1258 static void ibmvnic_set_multi(struct net_device *netdev)
1260 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1261 struct netdev_hw_addr *ha;
1262 union ibmvnic_crq crq;
1264 memset(&crq, 0, sizeof(crq));
1265 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1266 crq.request_capability.cmd = REQUEST_CAPABILITY;
1268 if (netdev->flags & IFF_PROMISC) {
1269 if (!adapter->promisc_supported)
1272 if (netdev->flags & IFF_ALLMULTI) {
1273 /* Accept all multicast */
1274 memset(&crq, 0, sizeof(crq));
1275 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1276 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1277 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1278 ibmvnic_send_crq(adapter, &crq);
1279 } else if (netdev_mc_empty(netdev)) {
1280 /* Reject all multicast */
1281 memset(&crq, 0, sizeof(crq));
1282 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1283 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1284 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1285 ibmvnic_send_crq(adapter, &crq);
1287 /* Accept one or more multicast(s) */
1288 netdev_for_each_mc_addr(ha, netdev) {
1289 memset(&crq, 0, sizeof(crq));
1290 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1291 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1292 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1293 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1295 ibmvnic_send_crq(adapter, &crq);
1301 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1303 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1304 struct sockaddr *addr = p;
1305 union ibmvnic_crq crq;
1307 if (!is_valid_ether_addr(addr->sa_data))
1308 return -EADDRNOTAVAIL;
1310 memset(&crq, 0, sizeof(crq));
1311 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1312 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1313 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1314 ibmvnic_send_crq(adapter, &crq);
1315 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1320 * do_reset returns zero if we are able to keep processing reset events, or
1321 * non-zero if we hit a fatal error and must halt.
1323 static int do_reset(struct ibmvnic_adapter *adapter,
1324 struct ibmvnic_rwi *rwi, u32 reset_state)
1326 struct net_device *netdev = adapter->netdev;
1329 netif_carrier_off(netdev);
1330 adapter->reset_reason = rwi->reset_reason;
1332 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1333 rc = ibmvnic_reenable_crq_queue(adapter);
1338 rc = __ibmvnic_close(netdev);
1342 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1343 /* remove the closed state so when we call open it appears
1344 * we are coming from the probed state.
1346 adapter->state = VNIC_PROBED;
1348 rc = ibmvnic_init(adapter);
1352 /* If the adapter was in PROBE state prior to the reset,
1355 if (reset_state == VNIC_PROBED)
1358 rc = ibmvnic_login(netdev);
1360 adapter->state = VNIC_PROBED;
1364 rc = reset_tx_pools(adapter);
1368 rc = reset_rx_pools(adapter);
1372 if (reset_state == VNIC_CLOSED)
1376 rc = __ibmvnic_open(netdev);
1378 if (list_empty(&adapter->rwi_list))
1379 adapter->state = VNIC_CLOSED;
1381 adapter->state = reset_state;
1386 netif_carrier_on(netdev);
1389 for (i = 0; i < adapter->req_rx_queues; i++)
1390 napi_schedule(&adapter->napi[i]);
1392 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1393 netdev_notify_peers(netdev);
1398 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1400 struct ibmvnic_rwi *rwi;
1402 mutex_lock(&adapter->rwi_lock);
1404 if (!list_empty(&adapter->rwi_list)) {
1405 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1407 list_del(&rwi->list);
1412 mutex_unlock(&adapter->rwi_lock);
1416 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1418 struct ibmvnic_rwi *rwi;
1420 rwi = get_next_rwi(adapter);
1423 rwi = get_next_rwi(adapter);
1427 static void __ibmvnic_reset(struct work_struct *work)
1429 struct ibmvnic_rwi *rwi;
1430 struct ibmvnic_adapter *adapter;
1431 struct net_device *netdev;
1435 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1436 netdev = adapter->netdev;
1438 mutex_lock(&adapter->reset_lock);
1439 adapter->resetting = true;
1440 reset_state = adapter->state;
1442 rwi = get_next_rwi(adapter);
1444 rc = do_reset(adapter, rwi, reset_state);
1449 rwi = get_next_rwi(adapter);
1453 free_all_rwi(adapter);
1454 mutex_unlock(&adapter->reset_lock);
1458 adapter->resetting = false;
1459 mutex_unlock(&adapter->reset_lock);
1462 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1463 enum ibmvnic_reset_reason reason)
1465 struct ibmvnic_rwi *rwi, *tmp;
1466 struct net_device *netdev = adapter->netdev;
1467 struct list_head *entry;
1469 if (adapter->state == VNIC_REMOVING ||
1470 adapter->state == VNIC_REMOVED) {
1471 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1475 if (adapter->state == VNIC_PROBING) {
1476 netdev_warn(netdev, "Adapter reset during probe\n");
1477 adapter->init_done_rc = EAGAIN;
1481 mutex_lock(&adapter->rwi_lock);
1483 list_for_each(entry, &adapter->rwi_list) {
1484 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1485 if (tmp->reset_reason == reason) {
1486 netdev_err(netdev, "Matching reset found, skipping\n");
1487 mutex_unlock(&adapter->rwi_lock);
1492 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1494 mutex_unlock(&adapter->rwi_lock);
1495 ibmvnic_close(netdev);
1499 rwi->reset_reason = reason;
1500 list_add_tail(&rwi->list, &adapter->rwi_list);
1501 mutex_unlock(&adapter->rwi_lock);
1502 schedule_work(&adapter->ibmvnic_reset);
1505 static void ibmvnic_tx_timeout(struct net_device *dev)
1507 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1509 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1512 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1513 struct ibmvnic_rx_buff *rx_buff)
1515 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1517 rx_buff->skb = NULL;
1519 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1520 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1522 atomic_dec(&pool->available);
1525 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1527 struct net_device *netdev = napi->dev;
1528 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1529 int scrq_num = (int)(napi - adapter->napi);
1530 int frames_processed = 0;
1533 while (frames_processed < budget) {
1534 struct sk_buff *skb;
1535 struct ibmvnic_rx_buff *rx_buff;
1536 union sub_crq *next;
1541 if (unlikely(adapter->resetting)) {
1542 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1543 napi_complete_done(napi, frames_processed);
1544 return frames_processed;
1547 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1549 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1551 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1552 rx_comp.correlator);
1553 /* do error checking */
1554 if (next->rx_comp.rc) {
1555 netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1556 /* free the entry */
1557 next->rx_comp.first = 0;
1558 remove_buff_from_pool(adapter, rx_buff);
1562 length = be32_to_cpu(next->rx_comp.len);
1563 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1564 flags = next->rx_comp.flags;
1566 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1569 /* VLAN Header has been stripped by the system firmware and
1570 * needs to be inserted by the driver
1572 if (adapter->rx_vlan_header_insertion &&
1573 (flags & IBMVNIC_VLAN_STRIPPED))
1574 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1575 ntohs(next->rx_comp.vlan_tci));
1577 /* free the entry */
1578 next->rx_comp.first = 0;
1579 remove_buff_from_pool(adapter, rx_buff);
1581 skb_put(skb, length);
1582 skb->protocol = eth_type_trans(skb, netdev);
1583 skb_record_rx_queue(skb, scrq_num);
1585 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1586 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1587 skb->ip_summed = CHECKSUM_UNNECESSARY;
1591 napi_gro_receive(napi, skb); /* send it up */
1592 netdev->stats.rx_packets++;
1593 netdev->stats.rx_bytes += length;
1597 if (adapter->state != VNIC_CLOSING)
1598 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1600 if (frames_processed < budget) {
1601 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1602 napi_complete_done(napi, frames_processed);
1603 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1604 napi_reschedule(napi)) {
1605 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1609 return frames_processed;
1612 #ifdef CONFIG_NET_POLL_CONTROLLER
1613 static void ibmvnic_netpoll_controller(struct net_device *dev)
1615 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1618 replenish_pools(netdev_priv(dev));
1619 for (i = 0; i < adapter->req_rx_queues; i++)
1620 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1621 adapter->rx_scrq[i]);
1625 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1630 static const struct net_device_ops ibmvnic_netdev_ops = {
1631 .ndo_open = ibmvnic_open,
1632 .ndo_stop = ibmvnic_close,
1633 .ndo_start_xmit = ibmvnic_xmit,
1634 .ndo_set_rx_mode = ibmvnic_set_multi,
1635 .ndo_set_mac_address = ibmvnic_set_mac,
1636 .ndo_validate_addr = eth_validate_addr,
1637 .ndo_tx_timeout = ibmvnic_tx_timeout,
1638 #ifdef CONFIG_NET_POLL_CONTROLLER
1639 .ndo_poll_controller = ibmvnic_netpoll_controller,
1641 .ndo_change_mtu = ibmvnic_change_mtu,
1644 /* ethtool functions */
1646 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1647 struct ethtool_link_ksettings *cmd)
1649 u32 supported, advertising;
1651 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1653 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1655 cmd->base.speed = SPEED_1000;
1656 cmd->base.duplex = DUPLEX_FULL;
1657 cmd->base.port = PORT_FIBRE;
1658 cmd->base.phy_address = 0;
1659 cmd->base.autoneg = AUTONEG_ENABLE;
1661 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1663 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1669 static void ibmvnic_get_drvinfo(struct net_device *dev,
1670 struct ethtool_drvinfo *info)
1672 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1673 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1676 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1678 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1680 return adapter->msg_enable;
1683 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1685 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1687 adapter->msg_enable = data;
1690 static u32 ibmvnic_get_link(struct net_device *netdev)
1692 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1694 /* Don't need to send a query because we request a logical link up at
1695 * init and then we wait for link state indications
1697 return adapter->logical_link_state;
1700 static void ibmvnic_get_ringparam(struct net_device *netdev,
1701 struct ethtool_ringparam *ring)
1703 ring->rx_max_pending = 0;
1704 ring->tx_max_pending = 0;
1705 ring->rx_mini_max_pending = 0;
1706 ring->rx_jumbo_max_pending = 0;
1707 ring->rx_pending = 0;
1708 ring->tx_pending = 0;
1709 ring->rx_mini_pending = 0;
1710 ring->rx_jumbo_pending = 0;
1713 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1717 if (stringset != ETH_SS_STATS)
1720 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1721 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1724 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1728 return ARRAY_SIZE(ibmvnic_stats);
1734 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1735 struct ethtool_stats *stats, u64 *data)
1737 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1738 union ibmvnic_crq crq;
1741 memset(&crq, 0, sizeof(crq));
1742 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1743 crq.request_statistics.cmd = REQUEST_STATISTICS;
1744 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1745 crq.request_statistics.len =
1746 cpu_to_be32(sizeof(struct ibmvnic_statistics));
1748 /* Wait for data to be written */
1749 init_completion(&adapter->stats_done);
1750 ibmvnic_send_crq(adapter, &crq);
1751 wait_for_completion(&adapter->stats_done);
1753 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1754 data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1757 static const struct ethtool_ops ibmvnic_ethtool_ops = {
1758 .get_drvinfo = ibmvnic_get_drvinfo,
1759 .get_msglevel = ibmvnic_get_msglevel,
1760 .set_msglevel = ibmvnic_set_msglevel,
1761 .get_link = ibmvnic_get_link,
1762 .get_ringparam = ibmvnic_get_ringparam,
1763 .get_strings = ibmvnic_get_strings,
1764 .get_sset_count = ibmvnic_get_sset_count,
1765 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
1766 .get_link_ksettings = ibmvnic_get_link_ksettings,
1769 /* Routines for managing CRQs/sCRQs */
1771 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
1772 struct ibmvnic_sub_crq_queue *scrq)
1777 free_irq(scrq->irq, scrq);
1778 irq_dispose_mapping(scrq->irq);
1782 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1785 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1786 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1790 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
1794 for (i = 0; i < adapter->req_tx_queues; i++) {
1795 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1800 for (i = 0; i < adapter->req_rx_queues; i++) {
1801 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1806 rc = init_sub_crq_irqs(adapter);
1810 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1811 struct ibmvnic_sub_crq_queue *scrq)
1813 struct device *dev = &adapter->vdev->dev;
1816 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1818 /* Close the sub-crqs */
1820 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1821 adapter->vdev->unit_address,
1823 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1826 netdev_err(adapter->netdev,
1827 "Failed to release sub-CRQ %16lx, rc = %ld\n",
1831 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1833 free_pages((unsigned long)scrq->msgs, 2);
1837 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1840 struct device *dev = &adapter->vdev->dev;
1841 struct ibmvnic_sub_crq_queue *scrq;
1844 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
1849 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
1851 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1852 goto zero_page_failed;
1855 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1857 if (dma_mapping_error(dev, scrq->msg_token)) {
1858 dev_warn(dev, "Couldn't map crq queue messages page\n");
1862 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1863 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1865 if (rc == H_RESOURCE)
1866 rc = ibmvnic_reset_crq(adapter);
1868 if (rc == H_CLOSED) {
1869 dev_warn(dev, "Partner adapter not ready, waiting.\n");
1871 dev_warn(dev, "Error %d registering sub-crq\n", rc);
1875 scrq->adapter = adapter;
1876 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1877 spin_lock_init(&scrq->lock);
1879 netdev_dbg(adapter->netdev,
1880 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1881 scrq->crq_num, scrq->hw_irq, scrq->irq);
1886 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1889 free_pages((unsigned long)scrq->msgs, 2);
1896 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1900 if (adapter->tx_scrq) {
1901 for (i = 0; i < adapter->req_tx_queues; i++) {
1902 if (!adapter->tx_scrq[i])
1905 if (adapter->tx_scrq[i]->irq) {
1906 free_irq(adapter->tx_scrq[i]->irq,
1907 adapter->tx_scrq[i]);
1908 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
1909 adapter->tx_scrq[i]->irq = 0;
1912 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1915 kfree(adapter->tx_scrq);
1916 adapter->tx_scrq = NULL;
1919 if (adapter->rx_scrq) {
1920 for (i = 0; i < adapter->req_rx_queues; i++) {
1921 if (!adapter->rx_scrq[i])
1924 if (adapter->rx_scrq[i]->irq) {
1925 free_irq(adapter->rx_scrq[i]->irq,
1926 adapter->rx_scrq[i]);
1927 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
1928 adapter->rx_scrq[i]->irq = 0;
1931 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1934 kfree(adapter->rx_scrq);
1935 adapter->rx_scrq = NULL;
1939 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1940 struct ibmvnic_sub_crq_queue *scrq)
1942 struct device *dev = &adapter->vdev->dev;
1945 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1946 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1948 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1953 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1954 struct ibmvnic_sub_crq_queue *scrq)
1956 struct device *dev = &adapter->vdev->dev;
1959 if (scrq->hw_irq > 0x100000000ULL) {
1960 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1964 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1965 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1967 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1972 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1973 struct ibmvnic_sub_crq_queue *scrq)
1975 struct device *dev = &adapter->vdev->dev;
1976 struct ibmvnic_tx_buff *txbuff;
1977 union sub_crq *next;
1983 while (pending_scrq(adapter, scrq)) {
1984 unsigned int pool = scrq->pool_index;
1986 next = ibmvnic_next_scrq(adapter, scrq);
1987 for (i = 0; i < next->tx_comp.num_comps; i++) {
1988 if (next->tx_comp.rcs[i]) {
1989 dev_err(dev, "tx error %x\n",
1990 next->tx_comp.rcs[i]);
1993 index = be32_to_cpu(next->tx_comp.correlators[i]);
1994 txbuff = &adapter->tx_pool[pool].tx_buff[index];
1996 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1997 if (!txbuff->data_dma[j])
2000 txbuff->data_dma[j] = 0;
2002 /* if sub_crq was sent indirectly */
2003 first = txbuff->indir_arr[0].generic.first;
2004 if (first == IBMVNIC_CRQ_CMD) {
2005 dma_unmap_single(dev, txbuff->indir_dma,
2006 sizeof(txbuff->indir_arr),
2010 if (txbuff->last_frag) {
2011 dev_kfree_skb_any(txbuff->skb);
2015 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2016 producer_index] = index;
2017 adapter->tx_pool[pool].producer_index =
2018 (adapter->tx_pool[pool].producer_index + 1) %
2019 adapter->req_tx_entries_per_subcrq;
2021 /* remove tx_comp scrq*/
2022 next->tx_comp.first = 0;
2024 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2025 (adapter->req_tx_entries_per_subcrq / 2) &&
2026 __netif_subqueue_stopped(adapter->netdev,
2027 scrq->pool_index)) {
2028 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2029 netdev_info(adapter->netdev, "Started queue %d\n",
2034 enable_scrq_irq(adapter, scrq);
2036 if (pending_scrq(adapter, scrq)) {
2037 disable_scrq_irq(adapter, scrq);
2044 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2046 struct ibmvnic_sub_crq_queue *scrq = instance;
2047 struct ibmvnic_adapter *adapter = scrq->adapter;
2049 disable_scrq_irq(adapter, scrq);
2050 ibmvnic_complete_tx(adapter, scrq);
2055 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2057 struct ibmvnic_sub_crq_queue *scrq = instance;
2058 struct ibmvnic_adapter *adapter = scrq->adapter;
2060 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2061 disable_scrq_irq(adapter, scrq);
2062 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2068 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2070 struct device *dev = &adapter->vdev->dev;
2071 struct ibmvnic_sub_crq_queue *scrq;
2075 for (i = 0; i < adapter->req_tx_queues; i++) {
2076 scrq = adapter->tx_scrq[i];
2077 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2081 dev_err(dev, "Error mapping irq\n");
2082 goto req_tx_irq_failed;
2085 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2086 0, "ibmvnic_tx", scrq);
2089 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2091 irq_dispose_mapping(scrq->irq);
2092 goto req_rx_irq_failed;
2096 for (i = 0; i < adapter->req_rx_queues; i++) {
2097 scrq = adapter->rx_scrq[i];
2098 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2101 dev_err(dev, "Error mapping irq\n");
2102 goto req_rx_irq_failed;
2104 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2105 0, "ibmvnic_rx", scrq);
2107 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2109 irq_dispose_mapping(scrq->irq);
2110 goto req_rx_irq_failed;
2116 for (j = 0; j < i; j++) {
2117 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2118 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2120 i = adapter->req_tx_queues;
2122 for (j = 0; j < i; j++) {
2123 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2124 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2126 release_sub_crqs(adapter);
2130 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2132 struct device *dev = &adapter->vdev->dev;
2133 struct ibmvnic_sub_crq_queue **allqueues;
2134 int registered_queues = 0;
2139 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2141 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2145 for (i = 0; i < total_queues; i++) {
2146 allqueues[i] = init_sub_crq_queue(adapter);
2147 if (!allqueues[i]) {
2148 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2151 registered_queues++;
2154 /* Make sure we were able to register the minimum number of queues */
2155 if (registered_queues <
2156 adapter->min_tx_queues + adapter->min_rx_queues) {
2157 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2161 /* Distribute the failed allocated queues*/
2162 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2163 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2166 if (adapter->req_rx_queues > adapter->min_rx_queues)
2167 adapter->req_rx_queues--;
2172 if (adapter->req_tx_queues > adapter->min_tx_queues)
2173 adapter->req_tx_queues--;
2180 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2181 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2182 if (!adapter->tx_scrq)
2185 for (i = 0; i < adapter->req_tx_queues; i++) {
2186 adapter->tx_scrq[i] = allqueues[i];
2187 adapter->tx_scrq[i]->pool_index = i;
2190 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2191 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2192 if (!adapter->rx_scrq)
2195 for (i = 0; i < adapter->req_rx_queues; i++) {
2196 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2197 adapter->rx_scrq[i]->scrq_num = i;
2204 kfree(adapter->tx_scrq);
2205 adapter->tx_scrq = NULL;
2207 for (i = 0; i < registered_queues; i++)
2208 release_sub_crq_queue(adapter, allqueues[i]);
2213 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2215 struct device *dev = &adapter->vdev->dev;
2216 union ibmvnic_crq crq;
2219 /* Sub-CRQ entries are 32 byte long */
2220 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2222 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2223 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2224 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2228 /* Get the minimum between the queried max and the entries
2229 * that fit in our PAGE_SIZE
2231 adapter->req_tx_entries_per_subcrq =
2232 adapter->max_tx_entries_per_subcrq > entries_page ?
2233 entries_page : adapter->max_tx_entries_per_subcrq;
2234 adapter->req_rx_add_entries_per_subcrq =
2235 adapter->max_rx_add_entries_per_subcrq > entries_page ?
2236 entries_page : adapter->max_rx_add_entries_per_subcrq;
2238 adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
2239 adapter->req_rx_queues = adapter->opt_rx_comp_queues;
2240 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2242 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2245 memset(&crq, 0, sizeof(crq));
2246 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2247 crq.request_capability.cmd = REQUEST_CAPABILITY;
2249 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2250 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2251 atomic_inc(&adapter->running_cap_crqs);
2252 ibmvnic_send_crq(adapter, &crq);
2254 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2255 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2256 atomic_inc(&adapter->running_cap_crqs);
2257 ibmvnic_send_crq(adapter, &crq);
2259 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2260 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2261 atomic_inc(&adapter->running_cap_crqs);
2262 ibmvnic_send_crq(adapter, &crq);
2264 crq.request_capability.capability =
2265 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2266 crq.request_capability.number =
2267 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2268 atomic_inc(&adapter->running_cap_crqs);
2269 ibmvnic_send_crq(adapter, &crq);
2271 crq.request_capability.capability =
2272 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2273 crq.request_capability.number =
2274 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2275 atomic_inc(&adapter->running_cap_crqs);
2276 ibmvnic_send_crq(adapter, &crq);
2278 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2279 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2280 atomic_inc(&adapter->running_cap_crqs);
2281 ibmvnic_send_crq(adapter, &crq);
2283 if (adapter->netdev->flags & IFF_PROMISC) {
2284 if (adapter->promisc_supported) {
2285 crq.request_capability.capability =
2286 cpu_to_be16(PROMISC_REQUESTED);
2287 crq.request_capability.number = cpu_to_be64(1);
2288 atomic_inc(&adapter->running_cap_crqs);
2289 ibmvnic_send_crq(adapter, &crq);
2292 crq.request_capability.capability =
2293 cpu_to_be16(PROMISC_REQUESTED);
2294 crq.request_capability.number = cpu_to_be64(0);
2295 atomic_inc(&adapter->running_cap_crqs);
2296 ibmvnic_send_crq(adapter, &crq);
2300 static int pending_scrq(struct ibmvnic_adapter *adapter,
2301 struct ibmvnic_sub_crq_queue *scrq)
2303 union sub_crq *entry = &scrq->msgs[scrq->cur];
2305 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2311 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2312 struct ibmvnic_sub_crq_queue *scrq)
2314 union sub_crq *entry;
2315 unsigned long flags;
2317 spin_lock_irqsave(&scrq->lock, flags);
2318 entry = &scrq->msgs[scrq->cur];
2319 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2320 if (++scrq->cur == scrq->size)
2325 spin_unlock_irqrestore(&scrq->lock, flags);
2330 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2332 struct ibmvnic_crq_queue *queue = &adapter->crq;
2333 union ibmvnic_crq *crq;
2335 crq = &queue->msgs[queue->cur];
2336 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2337 if (++queue->cur == queue->size)
2346 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2347 union sub_crq *sub_crq)
2349 unsigned int ua = adapter->vdev->unit_address;
2350 struct device *dev = &adapter->vdev->dev;
2351 u64 *u64_crq = (u64 *)sub_crq;
2354 netdev_dbg(adapter->netdev,
2355 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2356 (unsigned long int)cpu_to_be64(remote_handle),
2357 (unsigned long int)cpu_to_be64(u64_crq[0]),
2358 (unsigned long int)cpu_to_be64(u64_crq[1]),
2359 (unsigned long int)cpu_to_be64(u64_crq[2]),
2360 (unsigned long int)cpu_to_be64(u64_crq[3]));
2362 /* Make sure the hypervisor sees the complete request */
2365 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2366 cpu_to_be64(remote_handle),
2367 cpu_to_be64(u64_crq[0]),
2368 cpu_to_be64(u64_crq[1]),
2369 cpu_to_be64(u64_crq[2]),
2370 cpu_to_be64(u64_crq[3]));
2374 dev_warn(dev, "CRQ Queue closed\n");
2375 dev_err(dev, "Send error (rc=%d)\n", rc);
2381 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2382 u64 remote_handle, u64 ioba, u64 num_entries)
2384 unsigned int ua = adapter->vdev->unit_address;
2385 struct device *dev = &adapter->vdev->dev;
2388 /* Make sure the hypervisor sees the complete request */
2390 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2391 cpu_to_be64(remote_handle),
2396 dev_warn(dev, "CRQ Queue closed\n");
2397 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2403 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2404 union ibmvnic_crq *crq)
2406 unsigned int ua = adapter->vdev->unit_address;
2407 struct device *dev = &adapter->vdev->dev;
2408 u64 *u64_crq = (u64 *)crq;
2411 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2412 (unsigned long int)cpu_to_be64(u64_crq[0]),
2413 (unsigned long int)cpu_to_be64(u64_crq[1]));
2415 /* Make sure the hypervisor sees the complete request */
2418 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2419 cpu_to_be64(u64_crq[0]),
2420 cpu_to_be64(u64_crq[1]));
2424 dev_warn(dev, "CRQ Queue closed\n");
2425 dev_warn(dev, "Send error (rc=%d)\n", rc);
2431 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2433 union ibmvnic_crq crq;
2435 memset(&crq, 0, sizeof(crq));
2436 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2437 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2438 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2440 return ibmvnic_send_crq(adapter, &crq);
2443 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2445 union ibmvnic_crq crq;
2447 memset(&crq, 0, sizeof(crq));
2448 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2449 crq.version_exchange.cmd = VERSION_EXCHANGE;
2450 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2452 return ibmvnic_send_crq(adapter, &crq);
2455 static void send_login(struct ibmvnic_adapter *adapter)
2457 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2458 struct ibmvnic_login_buffer *login_buffer;
2459 struct device *dev = &adapter->vdev->dev;
2460 dma_addr_t rsp_buffer_token;
2461 dma_addr_t buffer_token;
2462 size_t rsp_buffer_size;
2463 union ibmvnic_crq crq;
2470 sizeof(struct ibmvnic_login_buffer) +
2471 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
2473 login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
2475 goto buf_alloc_failed;
2477 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2479 if (dma_mapping_error(dev, buffer_token)) {
2480 dev_err(dev, "Couldn't map login buffer\n");
2481 goto buf_map_failed;
2484 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2485 sizeof(u64) * adapter->req_tx_queues +
2486 sizeof(u64) * adapter->req_rx_queues +
2487 sizeof(u64) * adapter->req_rx_queues +
2488 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
2490 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2491 if (!login_rsp_buffer)
2492 goto buf_rsp_alloc_failed;
2494 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2495 rsp_buffer_size, DMA_FROM_DEVICE);
2496 if (dma_mapping_error(dev, rsp_buffer_token)) {
2497 dev_err(dev, "Couldn't map login rsp buffer\n");
2498 goto buf_rsp_map_failed;
2501 adapter->login_buf = login_buffer;
2502 adapter->login_buf_token = buffer_token;
2503 adapter->login_buf_sz = buffer_size;
2504 adapter->login_rsp_buf = login_rsp_buffer;
2505 adapter->login_rsp_buf_token = rsp_buffer_token;
2506 adapter->login_rsp_buf_sz = rsp_buffer_size;
2508 login_buffer->len = cpu_to_be32(buffer_size);
2509 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2510 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2511 login_buffer->off_txcomp_subcrqs =
2512 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2513 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2514 login_buffer->off_rxcomp_subcrqs =
2515 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2516 sizeof(u64) * adapter->req_tx_queues);
2517 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2518 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2520 tx_list_p = (__be64 *)((char *)login_buffer +
2521 sizeof(struct ibmvnic_login_buffer));
2522 rx_list_p = (__be64 *)((char *)login_buffer +
2523 sizeof(struct ibmvnic_login_buffer) +
2524 sizeof(u64) * adapter->req_tx_queues);
2526 for (i = 0; i < adapter->req_tx_queues; i++) {
2527 if (adapter->tx_scrq[i]) {
2528 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2533 for (i = 0; i < adapter->req_rx_queues; i++) {
2534 if (adapter->rx_scrq[i]) {
2535 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2540 netdev_dbg(adapter->netdev, "Login Buffer:\n");
2541 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2542 netdev_dbg(adapter->netdev, "%016lx\n",
2543 ((unsigned long int *)(adapter->login_buf))[i]);
2546 memset(&crq, 0, sizeof(crq));
2547 crq.login.first = IBMVNIC_CRQ_CMD;
2548 crq.login.cmd = LOGIN;
2549 crq.login.ioba = cpu_to_be32(buffer_token);
2550 crq.login.len = cpu_to_be32(buffer_size);
2551 ibmvnic_send_crq(adapter, &crq);
2556 kfree(login_rsp_buffer);
2557 buf_rsp_alloc_failed:
2558 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2560 kfree(login_buffer);
2565 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2568 union ibmvnic_crq crq;
2570 memset(&crq, 0, sizeof(crq));
2571 crq.request_map.first = IBMVNIC_CRQ_CMD;
2572 crq.request_map.cmd = REQUEST_MAP;
2573 crq.request_map.map_id = map_id;
2574 crq.request_map.ioba = cpu_to_be32(addr);
2575 crq.request_map.len = cpu_to_be32(len);
2576 ibmvnic_send_crq(adapter, &crq);
2579 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2581 union ibmvnic_crq crq;
2583 memset(&crq, 0, sizeof(crq));
2584 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2585 crq.request_unmap.cmd = REQUEST_UNMAP;
2586 crq.request_unmap.map_id = map_id;
2587 ibmvnic_send_crq(adapter, &crq);
2590 static void send_map_query(struct ibmvnic_adapter *adapter)
2592 union ibmvnic_crq crq;
2594 memset(&crq, 0, sizeof(crq));
2595 crq.query_map.first = IBMVNIC_CRQ_CMD;
2596 crq.query_map.cmd = QUERY_MAP;
2597 ibmvnic_send_crq(adapter, &crq);
2600 /* Send a series of CRQs requesting various capabilities of the VNIC server */
2601 static void send_cap_queries(struct ibmvnic_adapter *adapter)
2603 union ibmvnic_crq crq;
2605 atomic_set(&adapter->running_cap_crqs, 0);
2606 memset(&crq, 0, sizeof(crq));
2607 crq.query_capability.first = IBMVNIC_CRQ_CMD;
2608 crq.query_capability.cmd = QUERY_CAPABILITY;
2610 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
2611 atomic_inc(&adapter->running_cap_crqs);
2612 ibmvnic_send_crq(adapter, &crq);
2614 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
2615 atomic_inc(&adapter->running_cap_crqs);
2616 ibmvnic_send_crq(adapter, &crq);
2618 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
2619 atomic_inc(&adapter->running_cap_crqs);
2620 ibmvnic_send_crq(adapter, &crq);
2622 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
2623 atomic_inc(&adapter->running_cap_crqs);
2624 ibmvnic_send_crq(adapter, &crq);
2626 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
2627 atomic_inc(&adapter->running_cap_crqs);
2628 ibmvnic_send_crq(adapter, &crq);
2630 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
2631 atomic_inc(&adapter->running_cap_crqs);
2632 ibmvnic_send_crq(adapter, &crq);
2634 crq.query_capability.capability =
2635 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
2636 atomic_inc(&adapter->running_cap_crqs);
2637 ibmvnic_send_crq(adapter, &crq);
2639 crq.query_capability.capability =
2640 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
2641 atomic_inc(&adapter->running_cap_crqs);
2642 ibmvnic_send_crq(adapter, &crq);
2644 crq.query_capability.capability =
2645 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
2646 atomic_inc(&adapter->running_cap_crqs);
2647 ibmvnic_send_crq(adapter, &crq);
2649 crq.query_capability.capability =
2650 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
2651 atomic_inc(&adapter->running_cap_crqs);
2652 ibmvnic_send_crq(adapter, &crq);
2654 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
2655 atomic_inc(&adapter->running_cap_crqs);
2656 ibmvnic_send_crq(adapter, &crq);
2658 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
2659 atomic_inc(&adapter->running_cap_crqs);
2660 ibmvnic_send_crq(adapter, &crq);
2662 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
2663 atomic_inc(&adapter->running_cap_crqs);
2664 ibmvnic_send_crq(adapter, &crq);
2666 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
2667 atomic_inc(&adapter->running_cap_crqs);
2668 ibmvnic_send_crq(adapter, &crq);
2670 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
2671 atomic_inc(&adapter->running_cap_crqs);
2672 ibmvnic_send_crq(adapter, &crq);
2674 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
2675 atomic_inc(&adapter->running_cap_crqs);
2676 ibmvnic_send_crq(adapter, &crq);
2678 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
2679 atomic_inc(&adapter->running_cap_crqs);
2680 ibmvnic_send_crq(adapter, &crq);
2682 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
2683 atomic_inc(&adapter->running_cap_crqs);
2684 ibmvnic_send_crq(adapter, &crq);
2686 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
2687 atomic_inc(&adapter->running_cap_crqs);
2688 ibmvnic_send_crq(adapter, &crq);
2690 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
2691 atomic_inc(&adapter->running_cap_crqs);
2692 ibmvnic_send_crq(adapter, &crq);
2694 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
2695 atomic_inc(&adapter->running_cap_crqs);
2696 ibmvnic_send_crq(adapter, &crq);
2698 crq.query_capability.capability =
2699 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
2700 atomic_inc(&adapter->running_cap_crqs);
2701 ibmvnic_send_crq(adapter, &crq);
2703 crq.query_capability.capability =
2704 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
2705 atomic_inc(&adapter->running_cap_crqs);
2706 ibmvnic_send_crq(adapter, &crq);
2708 crq.query_capability.capability =
2709 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
2710 atomic_inc(&adapter->running_cap_crqs);
2711 ibmvnic_send_crq(adapter, &crq);
2713 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
2714 atomic_inc(&adapter->running_cap_crqs);
2715 ibmvnic_send_crq(adapter, &crq);
2718 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2720 struct device *dev = &adapter->vdev->dev;
2721 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2722 union ibmvnic_crq crq;
2725 dma_unmap_single(dev, adapter->ip_offload_tok,
2726 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2728 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2729 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2730 netdev_dbg(adapter->netdev, "%016lx\n",
2731 ((unsigned long int *)(buf))[i]);
2733 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2734 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2735 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2736 buf->tcp_ipv4_chksum);
2737 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2738 buf->tcp_ipv6_chksum);
2739 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2740 buf->udp_ipv4_chksum);
2741 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2742 buf->udp_ipv6_chksum);
2743 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2744 buf->large_tx_ipv4);
2745 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2746 buf->large_tx_ipv6);
2747 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2748 buf->large_rx_ipv4);
2749 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2750 buf->large_rx_ipv6);
2751 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2752 buf->max_ipv4_header_size);
2753 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2754 buf->max_ipv6_header_size);
2755 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2756 buf->max_tcp_header_size);
2757 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2758 buf->max_udp_header_size);
2759 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2760 buf->max_large_tx_size);
2761 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2762 buf->max_large_rx_size);
2763 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2764 buf->ipv6_extension_header);
2765 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2766 buf->tcp_pseudosum_req);
2767 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2768 buf->num_ipv6_ext_headers);
2769 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2770 buf->off_ipv6_ext_headers);
2772 adapter->ip_offload_ctrl_tok =
2773 dma_map_single(dev, &adapter->ip_offload_ctrl,
2774 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2776 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2777 dev_err(dev, "Couldn't map ip offload control buffer\n");
2781 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2782 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2783 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2784 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2785 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2787 /* large_tx/rx disabled for now, additional features needed */
2788 adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2789 adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2790 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2791 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2793 adapter->netdev->features = NETIF_F_GSO;
2795 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2796 adapter->netdev->features |= NETIF_F_IP_CSUM;
2798 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2799 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2801 if ((adapter->netdev->features &
2802 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2803 adapter->netdev->features |= NETIF_F_RXCSUM;
2805 memset(&crq, 0, sizeof(crq));
2806 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2807 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2808 crq.control_ip_offload.len =
2809 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2810 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2811 ibmvnic_send_crq(adapter, &crq);
2814 static void handle_error_info_rsp(union ibmvnic_crq *crq,
2815 struct ibmvnic_adapter *adapter)
2817 struct device *dev = &adapter->vdev->dev;
2818 struct ibmvnic_error_buff *error_buff, *tmp;
2819 unsigned long flags;
2823 if (!crq->request_error_rsp.rc.code) {
2824 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2825 crq->request_error_rsp.rc.code);
2829 spin_lock_irqsave(&adapter->error_list_lock, flags);
2830 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
2831 if (error_buff->error_id == crq->request_error_rsp.error_id) {
2833 list_del(&error_buff->list);
2836 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2839 dev_err(dev, "Couldn't find error id %x\n",
2840 be32_to_cpu(crq->request_error_rsp.error_id));
2844 dev_err(dev, "Detailed info for error id %x:",
2845 be32_to_cpu(crq->request_error_rsp.error_id));
2847 for (i = 0; i < error_buff->len; i++) {
2848 pr_cont("%02x", (int)error_buff->buff[i]);
2854 dma_unmap_single(dev, error_buff->dma, error_buff->len,
2856 kfree(error_buff->buff);
2860 static void request_error_information(struct ibmvnic_adapter *adapter,
2861 union ibmvnic_crq *err_crq)
2863 struct device *dev = &adapter->vdev->dev;
2864 struct net_device *netdev = adapter->netdev;
2865 struct ibmvnic_error_buff *error_buff;
2866 unsigned long timeout = msecs_to_jiffies(30000);
2867 union ibmvnic_crq crq;
2868 unsigned long flags;
2871 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2875 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
2876 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2877 if (!error_buff->buff) {
2882 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2884 if (dma_mapping_error(dev, error_buff->dma)) {
2885 netdev_err(netdev, "Couldn't map error buffer\n");
2886 kfree(error_buff->buff);
2891 error_buff->len = detail_len;
2892 error_buff->error_id = err_crq->error_indication.error_id;
2894 spin_lock_irqsave(&adapter->error_list_lock, flags);
2895 list_add_tail(&error_buff->list, &adapter->errors);
2896 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2898 memset(&crq, 0, sizeof(crq));
2899 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2900 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2901 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2902 crq.request_error_info.len = cpu_to_be32(detail_len);
2903 crq.request_error_info.error_id = err_crq->error_indication.error_id;
2905 rc = ibmvnic_send_crq(adapter, &crq);
2907 netdev_err(netdev, "failed to request error information\n");
2911 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2912 netdev_err(netdev, "timeout waiting for error information\n");
2919 spin_lock_irqsave(&adapter->error_list_lock, flags);
2920 list_del(&error_buff->list);
2921 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2923 kfree(error_buff->buff);
2927 static void handle_error_indication(union ibmvnic_crq *crq,
2928 struct ibmvnic_adapter *adapter)
2930 struct device *dev = &adapter->vdev->dev;
2932 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2933 crq->error_indication.flags
2934 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2935 be32_to_cpu(crq->error_indication.error_id),
2936 be16_to_cpu(crq->error_indication.error_cause));
2938 if (be32_to_cpu(crq->error_indication.error_id))
2939 request_error_information(adapter, crq);
2941 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
2942 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2944 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
2947 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2948 struct ibmvnic_adapter *adapter)
2950 struct net_device *netdev = adapter->netdev;
2951 struct device *dev = &adapter->vdev->dev;
2954 rc = crq->change_mac_addr_rsp.rc.code;
2956 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2959 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2963 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2964 struct ibmvnic_adapter *adapter)
2966 struct device *dev = &adapter->vdev->dev;
2970 atomic_dec(&adapter->running_cap_crqs);
2971 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2973 req_value = &adapter->req_tx_queues;
2977 req_value = &adapter->req_rx_queues;
2980 case REQ_RX_ADD_QUEUES:
2981 req_value = &adapter->req_rx_add_queues;
2984 case REQ_TX_ENTRIES_PER_SUBCRQ:
2985 req_value = &adapter->req_tx_entries_per_subcrq;
2986 name = "tx_entries_per_subcrq";
2988 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2989 req_value = &adapter->req_rx_add_entries_per_subcrq;
2990 name = "rx_add_entries_per_subcrq";
2993 req_value = &adapter->req_mtu;
2996 case PROMISC_REQUESTED:
2997 req_value = &adapter->promisc;
3001 dev_err(dev, "Got invalid cap request rsp %d\n",
3002 crq->request_capability.capability);
3006 switch (crq->request_capability_rsp.rc.code) {
3009 case PARTIALSUCCESS:
3010 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3012 (long int)be64_to_cpu(crq->request_capability_rsp.
3014 release_sub_crqs(adapter);
3015 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
3016 ibmvnic_send_req_caps(adapter, 1);
3019 dev_err(dev, "Error %d in request cap rsp\n",
3020 crq->request_capability_rsp.rc.code);
3024 /* Done receiving requested capabilities, query IP offload support */
3025 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3026 union ibmvnic_crq newcrq;
3027 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3028 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3029 &adapter->ip_offload_buf;
3031 adapter->wait_capability = false;
3032 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3036 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3037 if (!firmware_has_feature(FW_FEATURE_CMO))
3038 dev_err(dev, "Couldn't map offload buffer\n");
3042 memset(&newcrq, 0, sizeof(newcrq));
3043 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3044 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3045 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3046 newcrq.query_ip_offload.ioba =
3047 cpu_to_be32(adapter->ip_offload_tok);
3049 ibmvnic_send_crq(adapter, &newcrq);
3053 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3054 struct ibmvnic_adapter *adapter)
3056 struct device *dev = &adapter->vdev->dev;
3057 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3058 struct ibmvnic_login_buffer *login = adapter->login_buf;
3061 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3063 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3064 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3066 /* If the number of queues requested can't be allocated by the
3067 * server, the login response will return with code 1. We will need
3068 * to resend the login buffer with fewer queues requested.
3070 if (login_rsp_crq->generic.rc.code) {
3071 adapter->renegotiate = true;
3072 complete(&adapter->init_done);
3076 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3077 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3078 netdev_dbg(adapter->netdev, "%016lx\n",
3079 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3083 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3084 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3085 adapter->req_rx_add_queues !=
3086 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3087 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3088 ibmvnic_remove(adapter->vdev);
3091 complete(&adapter->init_done);
3096 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3097 struct ibmvnic_adapter *adapter)
3099 struct device *dev = &adapter->vdev->dev;
3102 rc = crq->request_unmap_rsp.rc.code;
3104 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3107 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3108 struct ibmvnic_adapter *adapter)
3110 struct net_device *netdev = adapter->netdev;
3111 struct device *dev = &adapter->vdev->dev;
3114 rc = crq->query_map_rsp.rc.code;
3116 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3119 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3120 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3121 crq->query_map_rsp.free_pages);
3124 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3125 struct ibmvnic_adapter *adapter)
3127 struct net_device *netdev = adapter->netdev;
3128 struct device *dev = &adapter->vdev->dev;
3131 atomic_dec(&adapter->running_cap_crqs);
3132 netdev_dbg(netdev, "Outstanding queries: %d\n",
3133 atomic_read(&adapter->running_cap_crqs));
3134 rc = crq->query_capability.rc.code;
3136 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3140 switch (be16_to_cpu(crq->query_capability.capability)) {
3142 adapter->min_tx_queues =
3143 be64_to_cpu(crq->query_capability.number);
3144 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3145 adapter->min_tx_queues);
3148 adapter->min_rx_queues =
3149 be64_to_cpu(crq->query_capability.number);
3150 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3151 adapter->min_rx_queues);
3153 case MIN_RX_ADD_QUEUES:
3154 adapter->min_rx_add_queues =
3155 be64_to_cpu(crq->query_capability.number);
3156 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3157 adapter->min_rx_add_queues);
3160 adapter->max_tx_queues =
3161 be64_to_cpu(crq->query_capability.number);
3162 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3163 adapter->max_tx_queues);
3166 adapter->max_rx_queues =
3167 be64_to_cpu(crq->query_capability.number);
3168 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3169 adapter->max_rx_queues);
3171 case MAX_RX_ADD_QUEUES:
3172 adapter->max_rx_add_queues =
3173 be64_to_cpu(crq->query_capability.number);
3174 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3175 adapter->max_rx_add_queues);
3177 case MIN_TX_ENTRIES_PER_SUBCRQ:
3178 adapter->min_tx_entries_per_subcrq =
3179 be64_to_cpu(crq->query_capability.number);
3180 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3181 adapter->min_tx_entries_per_subcrq);
3183 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3184 adapter->min_rx_add_entries_per_subcrq =
3185 be64_to_cpu(crq->query_capability.number);
3186 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3187 adapter->min_rx_add_entries_per_subcrq);
3189 case MAX_TX_ENTRIES_PER_SUBCRQ:
3190 adapter->max_tx_entries_per_subcrq =
3191 be64_to_cpu(crq->query_capability.number);
3192 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3193 adapter->max_tx_entries_per_subcrq);
3195 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3196 adapter->max_rx_add_entries_per_subcrq =
3197 be64_to_cpu(crq->query_capability.number);
3198 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3199 adapter->max_rx_add_entries_per_subcrq);
3201 case TCP_IP_OFFLOAD:
3202 adapter->tcp_ip_offload =
3203 be64_to_cpu(crq->query_capability.number);
3204 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3205 adapter->tcp_ip_offload);
3207 case PROMISC_SUPPORTED:
3208 adapter->promisc_supported =
3209 be64_to_cpu(crq->query_capability.number);
3210 netdev_dbg(netdev, "promisc_supported = %lld\n",
3211 adapter->promisc_supported);
3214 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3215 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3216 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3219 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3220 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3221 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3223 case MAX_MULTICAST_FILTERS:
3224 adapter->max_multicast_filters =
3225 be64_to_cpu(crq->query_capability.number);
3226 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3227 adapter->max_multicast_filters);
3229 case VLAN_HEADER_INSERTION:
3230 adapter->vlan_header_insertion =
3231 be64_to_cpu(crq->query_capability.number);
3232 if (adapter->vlan_header_insertion)
3233 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3234 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3235 adapter->vlan_header_insertion);
3237 case RX_VLAN_HEADER_INSERTION:
3238 adapter->rx_vlan_header_insertion =
3239 be64_to_cpu(crq->query_capability.number);
3240 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3241 adapter->rx_vlan_header_insertion);
3243 case MAX_TX_SG_ENTRIES:
3244 adapter->max_tx_sg_entries =
3245 be64_to_cpu(crq->query_capability.number);
3246 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3247 adapter->max_tx_sg_entries);
3249 case RX_SG_SUPPORTED:
3250 adapter->rx_sg_supported =
3251 be64_to_cpu(crq->query_capability.number);
3252 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3253 adapter->rx_sg_supported);
3255 case OPT_TX_COMP_SUB_QUEUES:
3256 adapter->opt_tx_comp_sub_queues =
3257 be64_to_cpu(crq->query_capability.number);
3258 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3259 adapter->opt_tx_comp_sub_queues);
3261 case OPT_RX_COMP_QUEUES:
3262 adapter->opt_rx_comp_queues =
3263 be64_to_cpu(crq->query_capability.number);
3264 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3265 adapter->opt_rx_comp_queues);
3267 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3268 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3269 be64_to_cpu(crq->query_capability.number);
3270 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3271 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3273 case OPT_TX_ENTRIES_PER_SUBCRQ:
3274 adapter->opt_tx_entries_per_subcrq =
3275 be64_to_cpu(crq->query_capability.number);
3276 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3277 adapter->opt_tx_entries_per_subcrq);
3279 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3280 adapter->opt_rxba_entries_per_subcrq =
3281 be64_to_cpu(crq->query_capability.number);
3282 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3283 adapter->opt_rxba_entries_per_subcrq);
3285 case TX_RX_DESC_REQ:
3286 adapter->tx_rx_desc_req = crq->query_capability.number;
3287 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3288 adapter->tx_rx_desc_req);
3292 netdev_err(netdev, "Got invalid cap rsp %d\n",
3293 crq->query_capability.capability);
3297 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3298 adapter->wait_capability = false;
3299 ibmvnic_send_req_caps(adapter, 0);
3303 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3304 struct ibmvnic_adapter *adapter)
3306 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3307 struct net_device *netdev = adapter->netdev;
3308 struct device *dev = &adapter->vdev->dev;
3309 u64 *u64_crq = (u64 *)crq;
3312 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3313 (unsigned long int)cpu_to_be64(u64_crq[0]),
3314 (unsigned long int)cpu_to_be64(u64_crq[1]));
3315 switch (gen_crq->first) {
3316 case IBMVNIC_CRQ_INIT_RSP:
3317 switch (gen_crq->cmd) {
3318 case IBMVNIC_CRQ_INIT:
3319 dev_info(dev, "Partner initialized\n");
3320 adapter->from_passive_init = true;
3321 complete(&adapter->init_done);
3323 case IBMVNIC_CRQ_INIT_COMPLETE:
3324 dev_info(dev, "Partner initialization complete\n");
3325 send_version_xchg(adapter);
3328 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3331 case IBMVNIC_CRQ_XPORT_EVENT:
3332 netif_carrier_off(netdev);
3333 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3334 dev_info(dev, "Migrated, re-enabling adapter\n");
3335 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
3336 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3337 dev_info(dev, "Backing device failover detected\n");
3338 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
3340 /* The adapter lost the connection */
3341 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3343 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3346 case IBMVNIC_CRQ_CMD_RSP:
3349 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3354 switch (gen_crq->cmd) {
3355 case VERSION_EXCHANGE_RSP:
3356 rc = crq->version_exchange_rsp.rc.code;
3358 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3361 dev_info(dev, "Partner protocol version is %d\n",
3362 crq->version_exchange_rsp.version);
3363 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3366 be16_to_cpu(crq->version_exchange_rsp.version);
3367 send_cap_queries(adapter);
3369 case QUERY_CAPABILITY_RSP:
3370 handle_query_cap_rsp(crq, adapter);
3373 handle_query_map_rsp(crq, adapter);
3375 case REQUEST_MAP_RSP:
3376 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3377 complete(&adapter->fw_done);
3379 case REQUEST_UNMAP_RSP:
3380 handle_request_unmap_rsp(crq, adapter);
3382 case REQUEST_CAPABILITY_RSP:
3383 handle_request_cap_rsp(crq, adapter);
3386 netdev_dbg(netdev, "Got Login Response\n");
3387 handle_login_rsp(crq, adapter);
3389 case LOGICAL_LINK_STATE_RSP:
3391 "Got Logical Link State Response, state: %d rc: %d\n",
3392 crq->logical_link_state_rsp.link_state,
3393 crq->logical_link_state_rsp.rc.code);
3394 adapter->logical_link_state =
3395 crq->logical_link_state_rsp.link_state;
3396 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3397 complete(&adapter->init_done);
3399 case LINK_STATE_INDICATION:
3400 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3401 adapter->phys_link_state =
3402 crq->link_state_indication.phys_link_state;
3403 adapter->logical_link_state =
3404 crq->link_state_indication.logical_link_state;
3406 case CHANGE_MAC_ADDR_RSP:
3407 netdev_dbg(netdev, "Got MAC address change Response\n");
3408 handle_change_mac_rsp(crq, adapter);
3410 case ERROR_INDICATION:
3411 netdev_dbg(netdev, "Got Error Indication\n");
3412 handle_error_indication(crq, adapter);
3414 case REQUEST_ERROR_RSP:
3415 netdev_dbg(netdev, "Got Error Detail Response\n");
3416 handle_error_info_rsp(crq, adapter);
3418 case REQUEST_STATISTICS_RSP:
3419 netdev_dbg(netdev, "Got Statistics Response\n");
3420 complete(&adapter->stats_done);
3422 case QUERY_IP_OFFLOAD_RSP:
3423 netdev_dbg(netdev, "Got Query IP offload Response\n");
3424 handle_query_ip_offload_rsp(adapter);
3426 case MULTICAST_CTRL_RSP:
3427 netdev_dbg(netdev, "Got multicast control Response\n");
3429 case CONTROL_IP_OFFLOAD_RSP:
3430 netdev_dbg(netdev, "Got Control IP offload Response\n");
3431 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3432 sizeof(adapter->ip_offload_ctrl),
3434 complete(&adapter->init_done);
3436 case COLLECT_FW_TRACE_RSP:
3437 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3438 complete(&adapter->fw_done);
3441 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3446 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3448 struct ibmvnic_adapter *adapter = instance;
3450 tasklet_schedule(&adapter->tasklet);
3454 static void ibmvnic_tasklet(void *data)
3456 struct ibmvnic_adapter *adapter = data;
3457 struct ibmvnic_crq_queue *queue = &adapter->crq;
3458 union ibmvnic_crq *crq;
3459 unsigned long flags;
3462 spin_lock_irqsave(&queue->lock, flags);
3464 /* Pull all the valid messages off the CRQ */
3465 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3466 ibmvnic_handle_crq(crq, adapter);
3467 crq->generic.first = 0;
3470 /* remain in tasklet until all
3471 * capabilities responses are received
3473 if (!adapter->wait_capability)
3476 /* if capabilities CRQ's were sent in this tasklet, the following
3477 * tasklet must wait until all responses are received
3479 if (atomic_read(&adapter->running_cap_crqs) != 0)
3480 adapter->wait_capability = true;
3481 spin_unlock_irqrestore(&queue->lock, flags);
3484 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3486 struct vio_dev *vdev = adapter->vdev;
3490 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3491 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3494 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3499 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3501 struct ibmvnic_crq_queue *crq = &adapter->crq;
3502 struct device *dev = &adapter->vdev->dev;
3503 struct vio_dev *vdev = adapter->vdev;
3508 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3509 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3511 /* Clean out the queue */
3512 memset(crq->msgs, 0, PAGE_SIZE);
3515 /* And re-open it again */
3516 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3517 crq->msg_token, PAGE_SIZE);
3520 /* Adapter is good, but other end is not ready */
3521 dev_warn(dev, "Partner adapter not ready\n");
3523 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3528 static void release_crq_queue(struct ibmvnic_adapter *adapter)
3530 struct ibmvnic_crq_queue *crq = &adapter->crq;
3531 struct vio_dev *vdev = adapter->vdev;
3537 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3538 free_irq(vdev->irq, adapter);
3539 tasklet_kill(&adapter->tasklet);
3541 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3542 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3544 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3546 free_page((unsigned long)crq->msgs);
3550 static int init_crq_queue(struct ibmvnic_adapter *adapter)
3552 struct ibmvnic_crq_queue *crq = &adapter->crq;
3553 struct device *dev = &adapter->vdev->dev;
3554 struct vio_dev *vdev = adapter->vdev;
3555 int rc, retrc = -ENOMEM;
3560 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3561 /* Should we allocate more than one page? */
3566 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3567 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3569 if (dma_mapping_error(dev, crq->msg_token))
3572 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3573 crq->msg_token, PAGE_SIZE);
3575 if (rc == H_RESOURCE)
3576 /* maybe kexecing and resource is busy. try a reset */
3577 rc = ibmvnic_reset_crq(adapter);
3580 if (rc == H_CLOSED) {
3581 dev_warn(dev, "Partner adapter not ready\n");
3583 dev_warn(dev, "Error %d opening adapter\n", rc);
3584 goto reg_crq_failed;
3589 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3590 (unsigned long)adapter);
3592 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3593 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3596 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3598 goto req_irq_failed;
3601 rc = vio_enable_interrupts(vdev);
3603 dev_err(dev, "Error %d enabling interrupts\n", rc);
3604 goto req_irq_failed;
3608 spin_lock_init(&crq->lock);
3613 tasklet_kill(&adapter->tasklet);
3615 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3616 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3618 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3620 free_page((unsigned long)crq->msgs);
3625 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3627 struct device *dev = &adapter->vdev->dev;
3628 unsigned long timeout = msecs_to_jiffies(30000);
3631 if (adapter->resetting) {
3632 rc = ibmvnic_reset_crq(adapter);
3634 rc = vio_enable_interrupts(adapter->vdev);
3636 rc = init_crq_queue(adapter);
3640 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3644 adapter->from_passive_init = false;
3646 init_completion(&adapter->init_done);
3647 adapter->init_done_rc = 0;
3648 ibmvnic_send_crq_init(adapter);
3649 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3650 dev_err(dev, "Initialization sequence timed out\n");
3654 if (adapter->init_done_rc) {
3655 release_crq_queue(adapter);
3656 return adapter->init_done_rc;
3659 if (adapter->from_passive_init) {
3660 adapter->state = VNIC_OPEN;
3661 adapter->from_passive_init = false;
3665 if (adapter->resetting)
3666 rc = reset_sub_crq_queues(adapter);
3668 rc = init_sub_crqs(adapter);
3670 dev_err(dev, "Initialization of sub crqs failed\n");
3671 release_crq_queue(adapter);
3677 static struct device_attribute dev_attr_failover;
3679 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3681 struct ibmvnic_adapter *adapter;
3682 struct net_device *netdev;
3683 unsigned char *mac_addr_p;
3686 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3689 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3690 VETH_MAC_ADDR, NULL);
3693 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3694 __FILE__, __LINE__);
3698 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3699 IBMVNIC_MAX_TX_QUEUES);
3703 adapter = netdev_priv(netdev);
3704 adapter->state = VNIC_PROBING;
3705 dev_set_drvdata(&dev->dev, netdev);
3706 adapter->vdev = dev;
3707 adapter->netdev = netdev;
3709 ether_addr_copy(adapter->mac_addr, mac_addr_p);
3710 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3711 netdev->irq = dev->irq;
3712 netdev->netdev_ops = &ibmvnic_netdev_ops;
3713 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3714 SET_NETDEV_DEV(netdev, &dev->dev);
3716 spin_lock_init(&adapter->stats_lock);
3718 INIT_LIST_HEAD(&adapter->errors);
3719 spin_lock_init(&adapter->error_list_lock);
3721 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
3722 INIT_LIST_HEAD(&adapter->rwi_list);
3723 mutex_init(&adapter->reset_lock);
3724 mutex_init(&adapter->rwi_lock);
3725 adapter->resetting = false;
3728 rc = ibmvnic_init(adapter);
3730 free_netdev(netdev);
3733 } while (rc == EAGAIN);
3735 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3737 rc = device_create_file(&dev->dev, &dev_attr_failover);
3739 free_netdev(netdev);
3743 rc = register_netdev(netdev);
3745 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
3746 device_remove_file(&dev->dev, &dev_attr_failover);
3747 free_netdev(netdev);
3750 dev_info(&dev->dev, "ibmvnic registered\n");
3752 adapter->state = VNIC_PROBED;
3756 static int ibmvnic_remove(struct vio_dev *dev)
3758 struct net_device *netdev = dev_get_drvdata(&dev->dev);
3759 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3761 adapter->state = VNIC_REMOVING;
3762 unregister_netdev(netdev);
3763 mutex_lock(&adapter->reset_lock);
3765 release_resources(adapter);
3766 release_sub_crqs(adapter);
3767 release_crq_queue(adapter);
3769 adapter->state = VNIC_REMOVED;
3771 mutex_unlock(&adapter->reset_lock);
3772 device_remove_file(&dev->dev, &dev_attr_failover);
3773 free_netdev(netdev);
3774 dev_set_drvdata(&dev->dev, NULL);
3779 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
3780 const char *buf, size_t count)
3782 struct net_device *netdev = dev_get_drvdata(dev);
3783 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3784 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
3785 __be64 session_token;
3788 if (!sysfs_streq(buf, "1"))
3791 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
3792 H_GET_SESSION_TOKEN, 0, 0, 0);
3794 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
3799 session_token = (__be64)retbuf[0];
3800 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
3801 be64_to_cpu(session_token));
3802 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3803 H_SESSION_ERR_DETECTED, session_token, 0, 0);
3805 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
3813 static DEVICE_ATTR(failover, 0200, NULL, failover_store);
3815 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3817 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3818 struct ibmvnic_adapter *adapter;
3819 struct iommu_table *tbl;
3820 unsigned long ret = 0;
3823 tbl = get_iommu_table_base(&vdev->dev);
3825 /* netdev inits at probe time along with the structures we need below*/
3827 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3829 adapter = netdev_priv(netdev);
3831 ret += PAGE_SIZE; /* the crq message queue */
3832 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3834 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3835 ret += 4 * PAGE_SIZE; /* the scrq message queue */
3837 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3839 ret += adapter->rx_pool[i].size *
3840 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3845 static int ibmvnic_resume(struct device *dev)
3847 struct net_device *netdev = dev_get_drvdata(dev);
3848 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3851 if (adapter->state != VNIC_OPEN)
3854 /* kick the interrupt handlers just in case we lost an interrupt */
3855 for (i = 0; i < adapter->req_rx_queues; i++)
3856 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3857 adapter->rx_scrq[i]);
3862 static struct vio_device_id ibmvnic_device_table[] = {
3863 {"network", "IBM,vnic"},
3866 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3868 static const struct dev_pm_ops ibmvnic_pm_ops = {
3869 .resume = ibmvnic_resume
3872 static struct vio_driver ibmvnic_driver = {
3873 .id_table = ibmvnic_device_table,
3874 .probe = ibmvnic_probe,
3875 .remove = ibmvnic_remove,
3876 .get_desired_dma = ibmvnic_get_desired_dma,
3877 .name = ibmvnic_driver_name,
3878 .pm = &ibmvnic_pm_ops,
3881 /* module functions */
3882 static int __init ibmvnic_module_init(void)
3884 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3885 IBMVNIC_DRIVER_VERSION);
3887 return vio_register_driver(&ibmvnic_driver);
3890 static void __exit ibmvnic_module_exit(void)
3892 vio_unregister_driver(&ibmvnic_driver);
3895 module_init(ibmvnic_module_init);
3896 module_exit(ibmvnic_module_exit);