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>
78 #include <linux/utsname.h>
82 static const char ibmvnic_driver_name[] = "ibmvnic";
83 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
85 MODULE_AUTHOR("Santiago Leon");
86 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
90 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
91 static int ibmvnic_remove(struct vio_dev *);
92 static void release_sub_crqs(struct ibmvnic_adapter *);
93 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
94 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
95 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
96 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
97 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
98 union sub_crq *sub_crq);
99 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
100 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
101 static int enable_scrq_irq(struct ibmvnic_adapter *,
102 struct ibmvnic_sub_crq_queue *);
103 static int disable_scrq_irq(struct ibmvnic_adapter *,
104 struct ibmvnic_sub_crq_queue *);
105 static int pending_scrq(struct ibmvnic_adapter *,
106 struct ibmvnic_sub_crq_queue *);
107 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
108 struct ibmvnic_sub_crq_queue *);
109 static int ibmvnic_poll(struct napi_struct *napi, int data);
110 static void send_map_query(struct ibmvnic_adapter *adapter);
111 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
112 static void send_request_unmap(struct ibmvnic_adapter *, u8);
113 static void send_login(struct ibmvnic_adapter *adapter);
114 static void send_cap_queries(struct ibmvnic_adapter *adapter);
115 static int init_sub_crqs(struct ibmvnic_adapter *);
116 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
117 static int ibmvnic_init(struct ibmvnic_adapter *);
118 static void release_crq_queue(struct ibmvnic_adapter *);
119 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
121 struct ibmvnic_stat {
122 char name[ETH_GSTRING_LEN];
126 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
127 offsetof(struct ibmvnic_statistics, stat))
128 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
130 static const struct ibmvnic_stat ibmvnic_stats[] = {
131 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
132 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
133 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
134 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
135 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
136 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
137 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
138 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
139 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
140 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
141 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
142 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
143 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
144 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
145 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
146 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
147 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
148 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
149 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
150 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
151 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
152 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
155 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
156 unsigned long length, unsigned long *number,
159 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
162 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
169 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
170 struct ibmvnic_long_term_buff *ltb, int size)
172 struct device *dev = &adapter->vdev->dev;
175 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
179 dev_err(dev, "Couldn't alloc long term buffer\n");
182 ltb->map_id = adapter->map_id;
185 init_completion(&adapter->fw_done);
186 send_request_map(adapter, ltb->addr,
187 ltb->size, ltb->map_id);
188 wait_for_completion(&adapter->fw_done);
190 if (adapter->fw_done_rc) {
191 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
192 adapter->fw_done_rc);
198 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
199 struct ibmvnic_long_term_buff *ltb)
201 struct device *dev = &adapter->vdev->dev;
206 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
207 adapter->reset_reason != VNIC_RESET_MOBILITY)
208 send_request_unmap(adapter, ltb->map_id);
209 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
212 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
213 struct ibmvnic_long_term_buff *ltb)
215 memset(ltb->buff, 0, ltb->size);
217 init_completion(&adapter->fw_done);
218 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
219 wait_for_completion(&adapter->fw_done);
221 if (adapter->fw_done_rc) {
222 dev_info(&adapter->vdev->dev,
223 "Reset failed, attempting to free and reallocate buffer\n");
224 free_long_term_buff(adapter, ltb);
225 return alloc_long_term_buff(adapter, ltb, ltb->size);
230 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
234 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
236 adapter->rx_pool[i].active = 0;
239 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
240 struct ibmvnic_rx_pool *pool)
242 int count = pool->size - atomic_read(&pool->available);
243 struct device *dev = &adapter->vdev->dev;
244 int buffers_added = 0;
245 unsigned long lpar_rc;
246 union sub_crq sub_crq;
259 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
260 be32_to_cpu(adapter->login_rsp_buf->
263 for (i = 0; i < count; ++i) {
264 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
266 dev_err(dev, "Couldn't replenish rx buff\n");
267 adapter->replenish_no_mem++;
271 index = pool->free_map[pool->next_free];
273 if (pool->rx_buff[index].skb)
274 dev_err(dev, "Inconsistent free_map!\n");
276 /* Copy the skb to the long term mapped DMA buffer */
277 offset = index * pool->buff_size;
278 dst = pool->long_term_buff.buff + offset;
279 memset(dst, 0, pool->buff_size);
280 dma_addr = pool->long_term_buff.addr + offset;
281 pool->rx_buff[index].data = dst;
283 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
284 pool->rx_buff[index].dma = dma_addr;
285 pool->rx_buff[index].skb = skb;
286 pool->rx_buff[index].pool_index = pool->index;
287 pool->rx_buff[index].size = pool->buff_size;
289 memset(&sub_crq, 0, sizeof(sub_crq));
290 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
291 sub_crq.rx_add.correlator =
292 cpu_to_be64((u64)&pool->rx_buff[index]);
293 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
294 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
296 /* The length field of the sCRQ is defined to be 24 bits so the
297 * buffer size needs to be left shifted by a byte before it is
298 * converted to big endian to prevent the last byte from being
301 #ifdef __LITTLE_ENDIAN__
304 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
306 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
308 if (lpar_rc != H_SUCCESS)
312 adapter->replenish_add_buff_success++;
313 pool->next_free = (pool->next_free + 1) % pool->size;
315 atomic_add(buffers_added, &pool->available);
319 dev_info(dev, "replenish pools failure\n");
320 pool->free_map[pool->next_free] = index;
321 pool->rx_buff[index].skb = NULL;
322 if (!dma_mapping_error(dev, dma_addr))
323 dma_unmap_single(dev, dma_addr, pool->buff_size,
326 dev_kfree_skb_any(skb);
327 adapter->replenish_add_buff_failure++;
328 atomic_add(buffers_added, &pool->available);
330 if (lpar_rc == H_CLOSED) {
331 /* Disable buffer pool replenishment and report carrier off if
332 * queue is closed. Firmware guarantees that a signal will
333 * be sent to the driver, triggering a reset.
335 deactivate_rx_pools(adapter);
336 netif_carrier_off(adapter->netdev);
340 static void replenish_pools(struct ibmvnic_adapter *adapter)
344 adapter->replenish_task_cycles++;
345 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
347 if (adapter->rx_pool[i].active)
348 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
352 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
354 kfree(adapter->tx_stats_buffers);
355 kfree(adapter->rx_stats_buffers);
358 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
360 adapter->tx_stats_buffers =
361 kcalloc(adapter->req_tx_queues,
362 sizeof(struct ibmvnic_tx_queue_stats),
364 if (!adapter->tx_stats_buffers)
367 adapter->rx_stats_buffers =
368 kcalloc(adapter->req_rx_queues,
369 sizeof(struct ibmvnic_rx_queue_stats),
371 if (!adapter->rx_stats_buffers)
377 static void release_stats_token(struct ibmvnic_adapter *adapter)
379 struct device *dev = &adapter->vdev->dev;
381 if (!adapter->stats_token)
384 dma_unmap_single(dev, adapter->stats_token,
385 sizeof(struct ibmvnic_statistics),
387 adapter->stats_token = 0;
390 static int init_stats_token(struct ibmvnic_adapter *adapter)
392 struct device *dev = &adapter->vdev->dev;
395 stok = dma_map_single(dev, &adapter->stats,
396 sizeof(struct ibmvnic_statistics),
398 if (dma_mapping_error(dev, stok)) {
399 dev_err(dev, "Couldn't map stats buffer\n");
403 adapter->stats_token = stok;
404 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
408 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
410 struct ibmvnic_rx_pool *rx_pool;
414 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
415 for (i = 0; i < rx_scrqs; i++) {
416 rx_pool = &adapter->rx_pool[i];
418 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
420 rc = reset_long_term_buff(adapter, &rx_pool->long_term_buff);
424 for (j = 0; j < rx_pool->size; j++)
425 rx_pool->free_map[j] = j;
427 memset(rx_pool->rx_buff, 0,
428 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
430 atomic_set(&rx_pool->available, 0);
431 rx_pool->next_alloc = 0;
432 rx_pool->next_free = 0;
439 static void release_rx_pools(struct ibmvnic_adapter *adapter)
441 struct ibmvnic_rx_pool *rx_pool;
445 if (!adapter->rx_pool)
448 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
449 for (i = 0; i < rx_scrqs; i++) {
450 rx_pool = &adapter->rx_pool[i];
452 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
454 kfree(rx_pool->free_map);
455 free_long_term_buff(adapter, &rx_pool->long_term_buff);
457 if (!rx_pool->rx_buff)
460 for (j = 0; j < rx_pool->size; j++) {
461 if (rx_pool->rx_buff[j].skb) {
462 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
463 rx_pool->rx_buff[i].skb = NULL;
467 kfree(rx_pool->rx_buff);
470 kfree(adapter->rx_pool);
471 adapter->rx_pool = NULL;
474 static int init_rx_pools(struct net_device *netdev)
476 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
477 struct device *dev = &adapter->vdev->dev;
478 struct ibmvnic_rx_pool *rx_pool;
484 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
485 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
486 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
488 adapter->rx_pool = kcalloc(rxadd_subcrqs,
489 sizeof(struct ibmvnic_rx_pool),
491 if (!adapter->rx_pool) {
492 dev_err(dev, "Failed to allocate rx pools\n");
496 for (i = 0; i < rxadd_subcrqs; i++) {
497 rx_pool = &adapter->rx_pool[i];
499 netdev_dbg(adapter->netdev,
500 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
501 i, adapter->req_rx_add_entries_per_subcrq,
502 be64_to_cpu(size_array[i]));
504 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
506 rx_pool->buff_size = be64_to_cpu(size_array[i]);
509 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
511 if (!rx_pool->free_map) {
512 release_rx_pools(adapter);
516 rx_pool->rx_buff = kcalloc(rx_pool->size,
517 sizeof(struct ibmvnic_rx_buff),
519 if (!rx_pool->rx_buff) {
520 dev_err(dev, "Couldn't alloc rx buffers\n");
521 release_rx_pools(adapter);
525 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
526 rx_pool->size * rx_pool->buff_size)) {
527 release_rx_pools(adapter);
531 for (j = 0; j < rx_pool->size; ++j)
532 rx_pool->free_map[j] = j;
534 atomic_set(&rx_pool->available, 0);
535 rx_pool->next_alloc = 0;
536 rx_pool->next_free = 0;
542 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
544 struct ibmvnic_tx_pool *tx_pool;
548 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
549 for (i = 0; i < tx_scrqs; i++) {
550 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
552 tx_pool = &adapter->tx_pool[i];
554 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
558 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
562 memset(tx_pool->tx_buff, 0,
563 adapter->req_tx_entries_per_subcrq *
564 sizeof(struct ibmvnic_tx_buff));
566 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
567 tx_pool->free_map[j] = j;
569 tx_pool->consumer_index = 0;
570 tx_pool->producer_index = 0;
571 tx_pool->tso_index = 0;
577 static void release_vpd_data(struct ibmvnic_adapter *adapter)
582 kfree(adapter->vpd->buff);
586 static void release_tx_pools(struct ibmvnic_adapter *adapter)
588 struct ibmvnic_tx_pool *tx_pool;
591 if (!adapter->tx_pool)
594 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
595 for (i = 0; i < tx_scrqs; i++) {
596 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
597 tx_pool = &adapter->tx_pool[i];
598 kfree(tx_pool->tx_buff);
599 free_long_term_buff(adapter, &tx_pool->long_term_buff);
600 free_long_term_buff(adapter, &tx_pool->tso_ltb);
601 kfree(tx_pool->free_map);
604 kfree(adapter->tx_pool);
605 adapter->tx_pool = NULL;
608 static int init_tx_pools(struct net_device *netdev)
610 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
611 struct device *dev = &adapter->vdev->dev;
612 struct ibmvnic_tx_pool *tx_pool;
616 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
617 adapter->tx_pool = kcalloc(tx_subcrqs,
618 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
619 if (!adapter->tx_pool)
622 for (i = 0; i < tx_subcrqs; i++) {
623 tx_pool = &adapter->tx_pool[i];
625 netdev_dbg(adapter->netdev,
626 "Initializing tx_pool[%d], %lld buffs\n",
627 i, adapter->req_tx_entries_per_subcrq);
629 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
630 sizeof(struct ibmvnic_tx_buff),
632 if (!tx_pool->tx_buff) {
633 dev_err(dev, "tx pool buffer allocation failed\n");
634 release_tx_pools(adapter);
638 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
639 adapter->req_tx_entries_per_subcrq *
641 release_tx_pools(adapter);
646 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
648 IBMVNIC_TSO_BUF_SZ)) {
649 release_tx_pools(adapter);
653 tx_pool->tso_index = 0;
655 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
656 sizeof(int), GFP_KERNEL);
657 if (!tx_pool->free_map) {
658 release_tx_pools(adapter);
662 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
663 tx_pool->free_map[j] = j;
665 tx_pool->consumer_index = 0;
666 tx_pool->producer_index = 0;
672 static void release_error_buffers(struct ibmvnic_adapter *adapter)
674 struct device *dev = &adapter->vdev->dev;
675 struct ibmvnic_error_buff *error_buff, *tmp;
678 spin_lock_irqsave(&adapter->error_list_lock, flags);
679 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
680 list_del(&error_buff->list);
681 dma_unmap_single(dev, error_buff->dma, error_buff->len,
683 kfree(error_buff->buff);
686 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
689 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
693 if (adapter->napi_enabled)
696 for (i = 0; i < adapter->req_rx_queues; i++)
697 napi_enable(&adapter->napi[i]);
699 adapter->napi_enabled = true;
702 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
706 if (!adapter->napi_enabled)
709 for (i = 0; i < adapter->req_rx_queues; i++) {
710 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
711 napi_disable(&adapter->napi[i]);
714 adapter->napi_enabled = false;
717 static int ibmvnic_login(struct net_device *netdev)
719 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
720 unsigned long timeout = msecs_to_jiffies(30000);
721 struct device *dev = &adapter->vdev->dev;
725 if (adapter->renegotiate) {
726 adapter->renegotiate = false;
727 release_sub_crqs(adapter);
729 reinit_completion(&adapter->init_done);
730 send_cap_queries(adapter);
731 if (!wait_for_completion_timeout(&adapter->init_done,
733 dev_err(dev, "Capabilities query timeout\n");
736 rc = init_sub_crqs(adapter);
739 "Initialization of SCRQ's failed\n");
742 rc = init_sub_crq_irqs(adapter);
745 "Initialization of SCRQ's irqs failed\n");
750 reinit_completion(&adapter->init_done);
752 if (!wait_for_completion_timeout(&adapter->init_done,
754 dev_err(dev, "Login timeout\n");
757 } while (adapter->renegotiate);
759 /* handle pending MAC address changes after successful login */
760 if (adapter->mac_change_pending) {
761 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
762 adapter->mac_change_pending = false;
768 static void release_resources(struct ibmvnic_adapter *adapter)
772 release_vpd_data(adapter);
774 release_tx_pools(adapter);
775 release_rx_pools(adapter);
777 release_stats_token(adapter);
778 release_stats_buffers(adapter);
779 release_error_buffers(adapter);
782 for (i = 0; i < adapter->req_rx_queues; i++) {
783 if (&adapter->napi[i]) {
784 netdev_dbg(adapter->netdev,
785 "Releasing napi[%d]\n", i);
786 netif_napi_del(&adapter->napi[i]);
792 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
794 struct net_device *netdev = adapter->netdev;
795 unsigned long timeout = msecs_to_jiffies(30000);
796 union ibmvnic_crq crq;
800 netdev_dbg(netdev, "setting link state %d\n", link_state);
802 memset(&crq, 0, sizeof(crq));
803 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
804 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
805 crq.logical_link_state.link_state = link_state;
810 reinit_completion(&adapter->init_done);
811 rc = ibmvnic_send_crq(adapter, &crq);
813 netdev_err(netdev, "Failed to set link state\n");
817 if (!wait_for_completion_timeout(&adapter->init_done,
819 netdev_err(netdev, "timeout setting link state\n");
823 if (adapter->init_done_rc == 1) {
824 /* Partuial success, delay and re-send */
833 static int set_real_num_queues(struct net_device *netdev)
835 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
838 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
839 adapter->req_tx_queues, adapter->req_rx_queues);
841 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
843 netdev_err(netdev, "failed to set the number of tx queues\n");
847 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
849 netdev_err(netdev, "failed to set the number of rx queues\n");
854 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
856 struct device *dev = &adapter->vdev->dev;
857 union ibmvnic_crq crq;
860 if (adapter->vpd->buff)
861 len = adapter->vpd->len;
863 reinit_completion(&adapter->fw_done);
864 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
865 crq.get_vpd_size.cmd = GET_VPD_SIZE;
866 ibmvnic_send_crq(adapter, &crq);
867 wait_for_completion(&adapter->fw_done);
869 if (!adapter->vpd->len)
872 if (!adapter->vpd->buff)
873 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
874 else if (adapter->vpd->len != len)
876 krealloc(adapter->vpd->buff,
877 adapter->vpd->len, GFP_KERNEL);
879 if (!adapter->vpd->buff) {
880 dev_err(dev, "Could allocate VPD buffer\n");
884 adapter->vpd->dma_addr =
885 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
887 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
888 dev_err(dev, "Could not map VPD buffer\n");
889 kfree(adapter->vpd->buff);
893 reinit_completion(&adapter->fw_done);
894 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
895 crq.get_vpd.cmd = GET_VPD;
896 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
897 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
898 ibmvnic_send_crq(adapter, &crq);
899 wait_for_completion(&adapter->fw_done);
904 static int init_resources(struct ibmvnic_adapter *adapter)
906 struct net_device *netdev = adapter->netdev;
909 rc = set_real_num_queues(netdev);
913 rc = init_stats_buffers(adapter);
917 rc = init_stats_token(adapter);
921 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
926 adapter->napi = kcalloc(adapter->req_rx_queues,
927 sizeof(struct napi_struct), GFP_KERNEL);
931 for (i = 0; i < adapter->req_rx_queues; i++) {
932 netdev_dbg(netdev, "Adding napi[%d]\n", i);
933 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
937 send_map_query(adapter);
939 rc = init_rx_pools(netdev);
943 rc = init_tx_pools(netdev);
947 static int __ibmvnic_open(struct net_device *netdev)
949 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
950 enum vnic_state prev_state = adapter->state;
953 adapter->state = VNIC_OPENING;
954 replenish_pools(adapter);
955 ibmvnic_napi_enable(adapter);
957 /* We're ready to receive frames, enable the sub-crq interrupts and
958 * set the logical link state to up
960 for (i = 0; i < adapter->req_rx_queues; i++) {
961 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
962 if (prev_state == VNIC_CLOSED)
963 enable_irq(adapter->rx_scrq[i]->irq);
965 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
968 for (i = 0; i < adapter->req_tx_queues; i++) {
969 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
970 if (prev_state == VNIC_CLOSED)
971 enable_irq(adapter->tx_scrq[i]->irq);
973 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
976 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
978 for (i = 0; i < adapter->req_rx_queues; i++)
979 napi_disable(&adapter->napi[i]);
980 release_resources(adapter);
984 netif_tx_start_all_queues(netdev);
986 if (prev_state == VNIC_CLOSED) {
987 for (i = 0; i < adapter->req_rx_queues; i++)
988 napi_schedule(&adapter->napi[i]);
991 adapter->state = VNIC_OPEN;
995 static int ibmvnic_open(struct net_device *netdev)
997 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1000 mutex_lock(&adapter->reset_lock);
1002 if (adapter->state != VNIC_CLOSED) {
1003 rc = ibmvnic_login(netdev);
1005 mutex_unlock(&adapter->reset_lock);
1009 rc = init_resources(adapter);
1011 netdev_err(netdev, "failed to initialize resources\n");
1012 release_resources(adapter);
1013 mutex_unlock(&adapter->reset_lock);
1018 rc = __ibmvnic_open(netdev);
1019 netif_carrier_on(netdev);
1021 /* Vital Product Data (VPD) */
1022 vpd = ibmvnic_get_vpd(adapter);
1024 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1026 mutex_unlock(&adapter->reset_lock);
1031 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1033 struct ibmvnic_tx_pool *tx_pool;
1038 if (!adapter->tx_pool)
1041 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1042 tx_entries = adapter->req_tx_entries_per_subcrq;
1044 /* Free any remaining skbs in the tx buffer pools */
1045 for (i = 0; i < tx_scrqs; i++) {
1046 tx_pool = &adapter->tx_pool[i];
1050 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1051 for (j = 0; j < tx_entries; j++) {
1052 if (tx_pool->tx_buff[j].skb) {
1053 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1054 tx_pool->tx_buff[j].skb = NULL;
1060 static int __ibmvnic_close(struct net_device *netdev)
1062 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1066 adapter->state = VNIC_CLOSING;
1068 /* ensure that transmissions are stopped if called by do_reset */
1069 if (adapter->resetting)
1070 netif_tx_disable(netdev);
1072 netif_tx_stop_all_queues(netdev);
1074 ibmvnic_napi_disable(adapter);
1076 if (adapter->tx_scrq) {
1077 for (i = 0; i < adapter->req_tx_queues; i++)
1078 if (adapter->tx_scrq[i]->irq) {
1079 netdev_dbg(adapter->netdev,
1080 "Disabling tx_scrq[%d] irq\n", i);
1081 disable_irq(adapter->tx_scrq[i]->irq);
1085 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1089 if (adapter->rx_scrq) {
1090 for (i = 0; i < adapter->req_rx_queues; i++) {
1093 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1101 if (adapter->rx_scrq[i]->irq) {
1102 netdev_dbg(adapter->netdev,
1103 "Disabling rx_scrq[%d] irq\n", i);
1104 disable_irq(adapter->rx_scrq[i]->irq);
1109 clean_tx_pools(adapter);
1110 adapter->state = VNIC_CLOSED;
1114 static int ibmvnic_close(struct net_device *netdev)
1116 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1119 mutex_lock(&adapter->reset_lock);
1120 rc = __ibmvnic_close(netdev);
1121 mutex_unlock(&adapter->reset_lock);
1127 * build_hdr_data - creates L2/L3/L4 header data buffer
1128 * @hdr_field - bitfield determining needed headers
1129 * @skb - socket buffer
1130 * @hdr_len - array of header lengths
1131 * @tot_len - total length of data
1133 * Reads hdr_field to determine which headers are needed by firmware.
1134 * Builds a buffer containing these headers. Saves individual header
1135 * lengths and total buffer length to be used to build descriptors.
1137 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1138 int *hdr_len, u8 *hdr_data)
1143 hdr_len[0] = sizeof(struct ethhdr);
1145 if (skb->protocol == htons(ETH_P_IP)) {
1146 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1147 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1148 hdr_len[2] = tcp_hdrlen(skb);
1149 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1150 hdr_len[2] = sizeof(struct udphdr);
1151 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1152 hdr_len[1] = sizeof(struct ipv6hdr);
1153 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1154 hdr_len[2] = tcp_hdrlen(skb);
1155 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1156 hdr_len[2] = sizeof(struct udphdr);
1159 memset(hdr_data, 0, 120);
1160 if ((hdr_field >> 6) & 1) {
1161 hdr = skb_mac_header(skb);
1162 memcpy(hdr_data, hdr, hdr_len[0]);
1166 if ((hdr_field >> 5) & 1) {
1167 hdr = skb_network_header(skb);
1168 memcpy(hdr_data + len, hdr, hdr_len[1]);
1172 if ((hdr_field >> 4) & 1) {
1173 hdr = skb_transport_header(skb);
1174 memcpy(hdr_data + len, hdr, hdr_len[2]);
1181 * create_hdr_descs - create header and header extension descriptors
1182 * @hdr_field - bitfield determining needed headers
1183 * @data - buffer containing header data
1184 * @len - length of data buffer
1185 * @hdr_len - array of individual header lengths
1186 * @scrq_arr - descriptor array
1188 * Creates header and, if needed, header extension descriptors and
1189 * places them in a descriptor array, scrq_arr
1192 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1193 union sub_crq *scrq_arr)
1195 union sub_crq hdr_desc;
1201 while (tmp_len > 0) {
1202 cur = hdr_data + len - tmp_len;
1204 memset(&hdr_desc, 0, sizeof(hdr_desc));
1205 if (cur != hdr_data) {
1206 data = hdr_desc.hdr_ext.data;
1207 tmp = tmp_len > 29 ? 29 : tmp_len;
1208 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1209 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1210 hdr_desc.hdr_ext.len = tmp;
1212 data = hdr_desc.hdr.data;
1213 tmp = tmp_len > 24 ? 24 : tmp_len;
1214 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1215 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1216 hdr_desc.hdr.len = tmp;
1217 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1218 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1219 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1220 hdr_desc.hdr.flag = hdr_field << 1;
1222 memcpy(data, cur, tmp);
1224 *scrq_arr = hdr_desc;
1233 * build_hdr_descs_arr - build a header descriptor array
1234 * @skb - socket buffer
1235 * @num_entries - number of descriptors to be sent
1236 * @subcrq - first TX descriptor
1237 * @hdr_field - bit field determining which headers will be sent
1239 * This function will build a TX descriptor array with applicable
1240 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1243 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1244 int *num_entries, u8 hdr_field)
1246 int hdr_len[3] = {0, 0, 0};
1248 u8 *hdr_data = txbuff->hdr_data;
1250 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1252 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1253 txbuff->indir_arr + 1);
1256 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1258 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1259 int queue_num = skb_get_queue_mapping(skb);
1260 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1261 struct device *dev = &adapter->vdev->dev;
1262 struct ibmvnic_tx_buff *tx_buff = NULL;
1263 struct ibmvnic_sub_crq_queue *tx_scrq;
1264 struct ibmvnic_tx_pool *tx_pool;
1265 unsigned int tx_send_failed = 0;
1266 unsigned int tx_map_failed = 0;
1267 unsigned int tx_dropped = 0;
1268 unsigned int tx_packets = 0;
1269 unsigned int tx_bytes = 0;
1270 dma_addr_t data_dma_addr;
1271 struct netdev_queue *txq;
1272 unsigned long lpar_rc;
1273 union sub_crq tx_crq;
1274 unsigned int offset;
1275 int num_entries = 1;
1281 if (adapter->resetting) {
1282 if (!netif_subqueue_stopped(netdev, skb))
1283 netif_stop_subqueue(netdev, queue_num);
1284 dev_kfree_skb_any(skb);
1292 tx_pool = &adapter->tx_pool[queue_num];
1293 tx_scrq = adapter->tx_scrq[queue_num];
1294 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1295 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1296 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1298 index = tx_pool->free_map[tx_pool->consumer_index];
1300 if (skb_is_gso(skb)) {
1301 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1302 dst = tx_pool->tso_ltb.buff + offset;
1303 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1304 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1305 tx_pool->tso_index++;
1306 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1307 tx_pool->tso_index = 0;
1309 offset = index * adapter->req_mtu;
1310 dst = tx_pool->long_term_buff.buff + offset;
1311 memset(dst, 0, adapter->req_mtu);
1312 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1315 if (skb_shinfo(skb)->nr_frags) {
1319 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1320 cur = skb_headlen(skb);
1322 /* Copy the frags */
1323 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1324 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1327 page_address(skb_frag_page(frag)) +
1328 frag->page_offset, skb_frag_size(frag));
1329 cur += skb_frag_size(frag);
1332 skb_copy_from_linear_data(skb, dst, skb->len);
1335 tx_pool->consumer_index =
1336 (tx_pool->consumer_index + 1) %
1337 adapter->req_tx_entries_per_subcrq;
1339 tx_buff = &tx_pool->tx_buff[index];
1341 tx_buff->data_dma[0] = data_dma_addr;
1342 tx_buff->data_len[0] = skb->len;
1343 tx_buff->index = index;
1344 tx_buff->pool_index = queue_num;
1345 tx_buff->last_frag = true;
1347 memset(&tx_crq, 0, sizeof(tx_crq));
1348 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1349 tx_crq.v1.type = IBMVNIC_TX_DESC;
1350 tx_crq.v1.n_crq_elem = 1;
1351 tx_crq.v1.n_sge = 1;
1352 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1353 tx_crq.v1.correlator = cpu_to_be32(index);
1354 if (skb_is_gso(skb))
1355 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1357 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1358 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1359 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1361 if (adapter->vlan_header_insertion) {
1362 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1363 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1366 if (skb->protocol == htons(ETH_P_IP)) {
1367 if (ip_hdr(skb)->version == 4)
1368 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1369 else if (ip_hdr(skb)->version == 6)
1370 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1372 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1373 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1374 else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
1375 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1378 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1379 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1382 if (skb_is_gso(skb)) {
1383 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1384 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1387 /* determine if l2/3/4 headers are sent to firmware */
1388 if ((*hdrs >> 7) & 1 &&
1389 (skb->protocol == htons(ETH_P_IP) ||
1390 skb->protocol == htons(ETH_P_IPV6))) {
1391 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1392 tx_crq.v1.n_crq_elem = num_entries;
1393 tx_buff->indir_arr[0] = tx_crq;
1394 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1395 sizeof(tx_buff->indir_arr),
1397 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1398 dev_kfree_skb_any(skb);
1399 tx_buff->skb = NULL;
1400 if (!firmware_has_feature(FW_FEATURE_CMO))
1401 dev_err(dev, "tx: unable to map descriptor array\n");
1407 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1408 (u64)tx_buff->indir_dma,
1411 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1414 if (lpar_rc != H_SUCCESS) {
1415 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1417 if (tx_pool->consumer_index == 0)
1418 tx_pool->consumer_index =
1419 adapter->req_tx_entries_per_subcrq - 1;
1421 tx_pool->consumer_index--;
1423 dev_kfree_skb_any(skb);
1424 tx_buff->skb = NULL;
1426 if (lpar_rc == H_CLOSED) {
1427 /* Disable TX and report carrier off if queue is closed.
1428 * Firmware guarantees that a signal will be sent to the
1429 * driver, triggering a reset or some other action.
1431 netif_tx_stop_all_queues(netdev);
1432 netif_carrier_off(netdev);
1441 if (atomic_inc_return(&tx_scrq->used)
1442 >= adapter->req_tx_entries_per_subcrq) {
1443 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1444 netif_stop_subqueue(netdev, queue_num);
1448 tx_bytes += skb->len;
1449 txq->trans_start = jiffies;
1453 netdev->stats.tx_dropped += tx_dropped;
1454 netdev->stats.tx_bytes += tx_bytes;
1455 netdev->stats.tx_packets += tx_packets;
1456 adapter->tx_send_failed += tx_send_failed;
1457 adapter->tx_map_failed += tx_map_failed;
1458 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1459 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1460 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1465 static void ibmvnic_set_multi(struct net_device *netdev)
1467 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1468 struct netdev_hw_addr *ha;
1469 union ibmvnic_crq crq;
1471 memset(&crq, 0, sizeof(crq));
1472 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1473 crq.request_capability.cmd = REQUEST_CAPABILITY;
1475 if (netdev->flags & IFF_PROMISC) {
1476 if (!adapter->promisc_supported)
1479 if (netdev->flags & IFF_ALLMULTI) {
1480 /* Accept all multicast */
1481 memset(&crq, 0, sizeof(crq));
1482 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1483 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1484 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1485 ibmvnic_send_crq(adapter, &crq);
1486 } else if (netdev_mc_empty(netdev)) {
1487 /* Reject all multicast */
1488 memset(&crq, 0, sizeof(crq));
1489 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1490 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1491 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1492 ibmvnic_send_crq(adapter, &crq);
1494 /* Accept one or more multicast(s) */
1495 netdev_for_each_mc_addr(ha, netdev) {
1496 memset(&crq, 0, sizeof(crq));
1497 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1498 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1499 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1500 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1502 ibmvnic_send_crq(adapter, &crq);
1508 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1510 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1511 struct sockaddr *addr = p;
1512 union ibmvnic_crq crq;
1514 if (!is_valid_ether_addr(addr->sa_data))
1515 return -EADDRNOTAVAIL;
1517 memset(&crq, 0, sizeof(crq));
1518 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1519 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1520 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1521 ibmvnic_send_crq(adapter, &crq);
1522 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1526 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1528 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1529 struct sockaddr *addr = p;
1531 if (adapter->state == VNIC_PROBED) {
1532 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1533 adapter->mac_change_pending = true;
1537 __ibmvnic_set_mac(netdev, addr);
1543 * do_reset returns zero if we are able to keep processing reset events, or
1544 * non-zero if we hit a fatal error and must halt.
1546 static int do_reset(struct ibmvnic_adapter *adapter,
1547 struct ibmvnic_rwi *rwi, u32 reset_state)
1549 struct net_device *netdev = adapter->netdev;
1552 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1555 netif_carrier_off(netdev);
1556 adapter->reset_reason = rwi->reset_reason;
1558 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1559 rc = ibmvnic_reenable_crq_queue(adapter);
1564 rc = __ibmvnic_close(netdev);
1568 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1569 adapter->wait_for_reset) {
1570 release_resources(adapter);
1571 release_sub_crqs(adapter);
1572 release_crq_queue(adapter);
1575 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1576 /* remove the closed state so when we call open it appears
1577 * we are coming from the probed state.
1579 adapter->state = VNIC_PROBED;
1581 rc = ibmvnic_init(adapter);
1583 return IBMVNIC_INIT_FAILED;
1585 /* If the adapter was in PROBE state prior to the reset,
1588 if (reset_state == VNIC_PROBED)
1591 rc = ibmvnic_login(netdev);
1593 adapter->state = VNIC_PROBED;
1597 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1598 adapter->wait_for_reset) {
1599 rc = init_resources(adapter);
1603 rc = reset_tx_pools(adapter);
1607 rc = reset_rx_pools(adapter);
1611 if (reset_state == VNIC_CLOSED)
1616 rc = __ibmvnic_open(netdev);
1618 if (list_empty(&adapter->rwi_list))
1619 adapter->state = VNIC_CLOSED;
1621 adapter->state = reset_state;
1626 netif_carrier_on(netdev);
1629 for (i = 0; i < adapter->req_rx_queues; i++)
1630 napi_schedule(&adapter->napi[i]);
1632 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1633 netdev_notify_peers(netdev);
1638 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1640 struct ibmvnic_rwi *rwi;
1642 mutex_lock(&adapter->rwi_lock);
1644 if (!list_empty(&adapter->rwi_list)) {
1645 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1647 list_del(&rwi->list);
1652 mutex_unlock(&adapter->rwi_lock);
1656 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1658 struct ibmvnic_rwi *rwi;
1660 rwi = get_next_rwi(adapter);
1663 rwi = get_next_rwi(adapter);
1667 static void __ibmvnic_reset(struct work_struct *work)
1669 struct ibmvnic_rwi *rwi;
1670 struct ibmvnic_adapter *adapter;
1671 struct net_device *netdev;
1675 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1676 netdev = adapter->netdev;
1678 mutex_lock(&adapter->reset_lock);
1679 adapter->resetting = true;
1680 reset_state = adapter->state;
1682 rwi = get_next_rwi(adapter);
1684 rc = do_reset(adapter, rwi, reset_state);
1686 if (rc && rc != IBMVNIC_INIT_FAILED)
1689 rwi = get_next_rwi(adapter);
1692 if (adapter->wait_for_reset) {
1693 adapter->wait_for_reset = false;
1694 adapter->reset_done_rc = rc;
1695 complete(&adapter->reset_done);
1699 netdev_dbg(adapter->netdev, "Reset failed\n");
1700 free_all_rwi(adapter);
1701 mutex_unlock(&adapter->reset_lock);
1705 adapter->resetting = false;
1706 mutex_unlock(&adapter->reset_lock);
1709 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1710 enum ibmvnic_reset_reason reason)
1712 struct ibmvnic_rwi *rwi, *tmp;
1713 struct net_device *netdev = adapter->netdev;
1714 struct list_head *entry;
1716 if (adapter->state == VNIC_REMOVING ||
1717 adapter->state == VNIC_REMOVED) {
1718 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1722 if (adapter->state == VNIC_PROBING) {
1723 netdev_warn(netdev, "Adapter reset during probe\n");
1724 adapter->init_done_rc = EAGAIN;
1728 mutex_lock(&adapter->rwi_lock);
1730 list_for_each(entry, &adapter->rwi_list) {
1731 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1732 if (tmp->reset_reason == reason) {
1733 netdev_dbg(netdev, "Skipping matching reset\n");
1734 mutex_unlock(&adapter->rwi_lock);
1739 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1741 mutex_unlock(&adapter->rwi_lock);
1742 ibmvnic_close(netdev);
1746 rwi->reset_reason = reason;
1747 list_add_tail(&rwi->list, &adapter->rwi_list);
1748 mutex_unlock(&adapter->rwi_lock);
1750 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1751 schedule_work(&adapter->ibmvnic_reset);
1754 static void ibmvnic_tx_timeout(struct net_device *dev)
1756 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1758 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1761 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1762 struct ibmvnic_rx_buff *rx_buff)
1764 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1766 rx_buff->skb = NULL;
1768 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1769 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1771 atomic_dec(&pool->available);
1774 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1776 struct net_device *netdev = napi->dev;
1777 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1778 int scrq_num = (int)(napi - adapter->napi);
1779 int frames_processed = 0;
1782 while (frames_processed < budget) {
1783 struct sk_buff *skb;
1784 struct ibmvnic_rx_buff *rx_buff;
1785 union sub_crq *next;
1790 if (unlikely(adapter->resetting)) {
1791 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1792 napi_complete_done(napi, frames_processed);
1793 return frames_processed;
1796 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1798 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1800 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1801 rx_comp.correlator);
1802 /* do error checking */
1803 if (next->rx_comp.rc) {
1804 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1805 be16_to_cpu(next->rx_comp.rc));
1806 /* free the entry */
1807 next->rx_comp.first = 0;
1808 remove_buff_from_pool(adapter, rx_buff);
1812 length = be32_to_cpu(next->rx_comp.len);
1813 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1814 flags = next->rx_comp.flags;
1816 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1819 /* VLAN Header has been stripped by the system firmware and
1820 * needs to be inserted by the driver
1822 if (adapter->rx_vlan_header_insertion &&
1823 (flags & IBMVNIC_VLAN_STRIPPED))
1824 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1825 ntohs(next->rx_comp.vlan_tci));
1827 /* free the entry */
1828 next->rx_comp.first = 0;
1829 remove_buff_from_pool(adapter, rx_buff);
1831 skb_put(skb, length);
1832 skb->protocol = eth_type_trans(skb, netdev);
1833 skb_record_rx_queue(skb, scrq_num);
1835 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1836 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1837 skb->ip_summed = CHECKSUM_UNNECESSARY;
1841 napi_gro_receive(napi, skb); /* send it up */
1842 netdev->stats.rx_packets++;
1843 netdev->stats.rx_bytes += length;
1844 adapter->rx_stats_buffers[scrq_num].packets++;
1845 adapter->rx_stats_buffers[scrq_num].bytes += length;
1849 if (adapter->state != VNIC_CLOSING)
1850 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1852 if (frames_processed < budget) {
1853 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1854 napi_complete_done(napi, frames_processed);
1855 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1856 napi_reschedule(napi)) {
1857 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1861 return frames_processed;
1864 #ifdef CONFIG_NET_POLL_CONTROLLER
1865 static void ibmvnic_netpoll_controller(struct net_device *dev)
1867 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1870 replenish_pools(netdev_priv(dev));
1871 for (i = 0; i < adapter->req_rx_queues; i++)
1872 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1873 adapter->rx_scrq[i]);
1877 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1879 adapter->fallback.mtu = adapter->req_mtu;
1880 adapter->fallback.rx_queues = adapter->req_rx_queues;
1881 adapter->fallback.tx_queues = adapter->req_tx_queues;
1882 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1883 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1885 init_completion(&adapter->reset_done);
1886 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1887 adapter->wait_for_reset = true;
1888 wait_for_completion(&adapter->reset_done);
1890 if (adapter->reset_done_rc) {
1891 adapter->desired.mtu = adapter->fallback.mtu;
1892 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1893 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1894 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1895 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1897 init_completion(&adapter->reset_done);
1898 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1899 wait_for_completion(&adapter->reset_done);
1901 adapter->wait_for_reset = false;
1903 return adapter->reset_done_rc;
1906 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
1908 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1910 adapter->desired.mtu = new_mtu + ETH_HLEN;
1912 return wait_for_reset(adapter);
1915 static const struct net_device_ops ibmvnic_netdev_ops = {
1916 .ndo_open = ibmvnic_open,
1917 .ndo_stop = ibmvnic_close,
1918 .ndo_start_xmit = ibmvnic_xmit,
1919 .ndo_set_rx_mode = ibmvnic_set_multi,
1920 .ndo_set_mac_address = ibmvnic_set_mac,
1921 .ndo_validate_addr = eth_validate_addr,
1922 .ndo_tx_timeout = ibmvnic_tx_timeout,
1923 #ifdef CONFIG_NET_POLL_CONTROLLER
1924 .ndo_poll_controller = ibmvnic_netpoll_controller,
1926 .ndo_change_mtu = ibmvnic_change_mtu,
1929 /* ethtool functions */
1931 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1932 struct ethtool_link_ksettings *cmd)
1934 u32 supported, advertising;
1936 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1938 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1940 cmd->base.speed = SPEED_1000;
1941 cmd->base.duplex = DUPLEX_FULL;
1942 cmd->base.port = PORT_FIBRE;
1943 cmd->base.phy_address = 0;
1944 cmd->base.autoneg = AUTONEG_ENABLE;
1946 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1948 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1954 static void ibmvnic_get_drvinfo(struct net_device *netdev,
1955 struct ethtool_drvinfo *info)
1957 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1959 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1960 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1961 strlcpy(info->fw_version, adapter->fw_version,
1962 sizeof(info->fw_version));
1965 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1967 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1969 return adapter->msg_enable;
1972 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1974 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1976 adapter->msg_enable = data;
1979 static u32 ibmvnic_get_link(struct net_device *netdev)
1981 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1983 /* Don't need to send a query because we request a logical link up at
1984 * init and then we wait for link state indications
1986 return adapter->logical_link_state;
1989 static void ibmvnic_get_ringparam(struct net_device *netdev,
1990 struct ethtool_ringparam *ring)
1992 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1994 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
1995 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
1996 ring->rx_mini_max_pending = 0;
1997 ring->rx_jumbo_max_pending = 0;
1998 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
1999 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2000 ring->rx_mini_pending = 0;
2001 ring->rx_jumbo_pending = 0;
2004 static int ibmvnic_set_ringparam(struct net_device *netdev,
2005 struct ethtool_ringparam *ring)
2007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2009 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2010 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2011 netdev_err(netdev, "Invalid request.\n");
2012 netdev_err(netdev, "Max tx buffers = %llu\n",
2013 adapter->max_rx_add_entries_per_subcrq);
2014 netdev_err(netdev, "Max rx buffers = %llu\n",
2015 adapter->max_tx_entries_per_subcrq);
2019 adapter->desired.rx_entries = ring->rx_pending;
2020 adapter->desired.tx_entries = ring->tx_pending;
2022 return wait_for_reset(adapter);
2025 static void ibmvnic_get_channels(struct net_device *netdev,
2026 struct ethtool_channels *channels)
2028 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2030 channels->max_rx = adapter->max_rx_queues;
2031 channels->max_tx = adapter->max_tx_queues;
2032 channels->max_other = 0;
2033 channels->max_combined = 0;
2034 channels->rx_count = adapter->req_rx_queues;
2035 channels->tx_count = adapter->req_tx_queues;
2036 channels->other_count = 0;
2037 channels->combined_count = 0;
2040 static int ibmvnic_set_channels(struct net_device *netdev,
2041 struct ethtool_channels *channels)
2043 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2045 adapter->desired.rx_queues = channels->rx_count;
2046 adapter->desired.tx_queues = channels->tx_count;
2048 return wait_for_reset(adapter);
2051 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2053 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2056 if (stringset != ETH_SS_STATS)
2059 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2060 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2062 for (i = 0; i < adapter->req_tx_queues; i++) {
2063 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2064 data += ETH_GSTRING_LEN;
2066 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2067 data += ETH_GSTRING_LEN;
2069 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2070 data += ETH_GSTRING_LEN;
2073 for (i = 0; i < adapter->req_rx_queues; i++) {
2074 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2075 data += ETH_GSTRING_LEN;
2077 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2078 data += ETH_GSTRING_LEN;
2080 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2081 data += ETH_GSTRING_LEN;
2085 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2087 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2091 return ARRAY_SIZE(ibmvnic_stats) +
2092 adapter->req_tx_queues * NUM_TX_STATS +
2093 adapter->req_rx_queues * NUM_RX_STATS;
2099 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2100 struct ethtool_stats *stats, u64 *data)
2102 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2103 union ibmvnic_crq crq;
2106 memset(&crq, 0, sizeof(crq));
2107 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2108 crq.request_statistics.cmd = REQUEST_STATISTICS;
2109 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2110 crq.request_statistics.len =
2111 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2113 /* Wait for data to be written */
2114 init_completion(&adapter->stats_done);
2115 ibmvnic_send_crq(adapter, &crq);
2116 wait_for_completion(&adapter->stats_done);
2118 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2119 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2120 ibmvnic_stats[i].offset));
2122 for (j = 0; j < adapter->req_tx_queues; j++) {
2123 data[i] = adapter->tx_stats_buffers[j].packets;
2125 data[i] = adapter->tx_stats_buffers[j].bytes;
2127 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2131 for (j = 0; j < adapter->req_rx_queues; j++) {
2132 data[i] = adapter->rx_stats_buffers[j].packets;
2134 data[i] = adapter->rx_stats_buffers[j].bytes;
2136 data[i] = adapter->rx_stats_buffers[j].interrupts;
2141 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2142 .get_drvinfo = ibmvnic_get_drvinfo,
2143 .get_msglevel = ibmvnic_get_msglevel,
2144 .set_msglevel = ibmvnic_set_msglevel,
2145 .get_link = ibmvnic_get_link,
2146 .get_ringparam = ibmvnic_get_ringparam,
2147 .set_ringparam = ibmvnic_set_ringparam,
2148 .get_channels = ibmvnic_get_channels,
2149 .set_channels = ibmvnic_set_channels,
2150 .get_strings = ibmvnic_get_strings,
2151 .get_sset_count = ibmvnic_get_sset_count,
2152 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2153 .get_link_ksettings = ibmvnic_get_link_ksettings,
2156 /* Routines for managing CRQs/sCRQs */
2158 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2159 struct ibmvnic_sub_crq_queue *scrq)
2164 free_irq(scrq->irq, scrq);
2165 irq_dispose_mapping(scrq->irq);
2169 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2172 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2173 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2177 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2181 for (i = 0; i < adapter->req_tx_queues; i++) {
2182 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2183 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2188 for (i = 0; i < adapter->req_rx_queues; i++) {
2189 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2190 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2198 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2199 struct ibmvnic_sub_crq_queue *scrq)
2201 struct device *dev = &adapter->vdev->dev;
2204 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2206 /* Close the sub-crqs */
2208 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2209 adapter->vdev->unit_address,
2211 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2214 netdev_err(adapter->netdev,
2215 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2219 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2221 free_pages((unsigned long)scrq->msgs, 2);
2225 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2228 struct device *dev = &adapter->vdev->dev;
2229 struct ibmvnic_sub_crq_queue *scrq;
2232 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2237 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2239 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2240 goto zero_page_failed;
2243 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2245 if (dma_mapping_error(dev, scrq->msg_token)) {
2246 dev_warn(dev, "Couldn't map crq queue messages page\n");
2250 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2251 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2253 if (rc == H_RESOURCE)
2254 rc = ibmvnic_reset_crq(adapter);
2256 if (rc == H_CLOSED) {
2257 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2259 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2263 scrq->adapter = adapter;
2264 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2265 spin_lock_init(&scrq->lock);
2267 netdev_dbg(adapter->netdev,
2268 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2269 scrq->crq_num, scrq->hw_irq, scrq->irq);
2274 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2277 free_pages((unsigned long)scrq->msgs, 2);
2284 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2288 if (adapter->tx_scrq) {
2289 for (i = 0; i < adapter->req_tx_queues; i++) {
2290 if (!adapter->tx_scrq[i])
2293 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2295 if (adapter->tx_scrq[i]->irq) {
2296 free_irq(adapter->tx_scrq[i]->irq,
2297 adapter->tx_scrq[i]);
2298 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2299 adapter->tx_scrq[i]->irq = 0;
2302 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2305 kfree(adapter->tx_scrq);
2306 adapter->tx_scrq = NULL;
2309 if (adapter->rx_scrq) {
2310 for (i = 0; i < adapter->req_rx_queues; i++) {
2311 if (!adapter->rx_scrq[i])
2314 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2316 if (adapter->rx_scrq[i]->irq) {
2317 free_irq(adapter->rx_scrq[i]->irq,
2318 adapter->rx_scrq[i]);
2319 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2320 adapter->rx_scrq[i]->irq = 0;
2323 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2326 kfree(adapter->rx_scrq);
2327 adapter->rx_scrq = NULL;
2331 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2332 struct ibmvnic_sub_crq_queue *scrq)
2334 struct device *dev = &adapter->vdev->dev;
2337 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2338 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2340 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2345 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2346 struct ibmvnic_sub_crq_queue *scrq)
2348 struct device *dev = &adapter->vdev->dev;
2351 if (scrq->hw_irq > 0x100000000ULL) {
2352 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2356 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2357 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2359 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2364 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2365 struct ibmvnic_sub_crq_queue *scrq)
2367 struct device *dev = &adapter->vdev->dev;
2368 struct ibmvnic_tx_buff *txbuff;
2369 union sub_crq *next;
2375 while (pending_scrq(adapter, scrq)) {
2376 unsigned int pool = scrq->pool_index;
2378 next = ibmvnic_next_scrq(adapter, scrq);
2379 for (i = 0; i < next->tx_comp.num_comps; i++) {
2380 if (next->tx_comp.rcs[i]) {
2381 dev_err(dev, "tx error %x\n",
2382 next->tx_comp.rcs[i]);
2385 index = be32_to_cpu(next->tx_comp.correlators[i]);
2386 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2388 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2389 if (!txbuff->data_dma[j])
2392 txbuff->data_dma[j] = 0;
2394 /* if sub_crq was sent indirectly */
2395 first = txbuff->indir_arr[0].generic.first;
2396 if (first == IBMVNIC_CRQ_CMD) {
2397 dma_unmap_single(dev, txbuff->indir_dma,
2398 sizeof(txbuff->indir_arr),
2402 if (txbuff->last_frag) {
2403 dev_kfree_skb_any(txbuff->skb);
2407 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2408 producer_index] = index;
2409 adapter->tx_pool[pool].producer_index =
2410 (adapter->tx_pool[pool].producer_index + 1) %
2411 adapter->req_tx_entries_per_subcrq;
2413 /* remove tx_comp scrq*/
2414 next->tx_comp.first = 0;
2416 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2417 (adapter->req_tx_entries_per_subcrq / 2) &&
2418 __netif_subqueue_stopped(adapter->netdev,
2419 scrq->pool_index)) {
2420 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2421 netdev_info(adapter->netdev, "Started queue %d\n",
2426 enable_scrq_irq(adapter, scrq);
2428 if (pending_scrq(adapter, scrq)) {
2429 disable_scrq_irq(adapter, scrq);
2436 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2438 struct ibmvnic_sub_crq_queue *scrq = instance;
2439 struct ibmvnic_adapter *adapter = scrq->adapter;
2441 disable_scrq_irq(adapter, scrq);
2442 ibmvnic_complete_tx(adapter, scrq);
2447 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2449 struct ibmvnic_sub_crq_queue *scrq = instance;
2450 struct ibmvnic_adapter *adapter = scrq->adapter;
2452 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2454 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2455 disable_scrq_irq(adapter, scrq);
2456 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2462 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2464 struct device *dev = &adapter->vdev->dev;
2465 struct ibmvnic_sub_crq_queue *scrq;
2469 for (i = 0; i < adapter->req_tx_queues; i++) {
2470 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2472 scrq = adapter->tx_scrq[i];
2473 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2477 dev_err(dev, "Error mapping irq\n");
2478 goto req_tx_irq_failed;
2481 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2482 0, "ibmvnic_tx", scrq);
2485 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2487 irq_dispose_mapping(scrq->irq);
2488 goto req_rx_irq_failed;
2492 for (i = 0; i < adapter->req_rx_queues; i++) {
2493 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2495 scrq = adapter->rx_scrq[i];
2496 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2499 dev_err(dev, "Error mapping irq\n");
2500 goto req_rx_irq_failed;
2502 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2503 0, "ibmvnic_rx", scrq);
2505 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2507 irq_dispose_mapping(scrq->irq);
2508 goto req_rx_irq_failed;
2514 for (j = 0; j < i; j++) {
2515 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2516 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2518 i = adapter->req_tx_queues;
2520 for (j = 0; j < i; j++) {
2521 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2522 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2524 release_sub_crqs(adapter);
2528 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2530 struct device *dev = &adapter->vdev->dev;
2531 struct ibmvnic_sub_crq_queue **allqueues;
2532 int registered_queues = 0;
2537 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2539 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2543 for (i = 0; i < total_queues; i++) {
2544 allqueues[i] = init_sub_crq_queue(adapter);
2545 if (!allqueues[i]) {
2546 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2549 registered_queues++;
2552 /* Make sure we were able to register the minimum number of queues */
2553 if (registered_queues <
2554 adapter->min_tx_queues + adapter->min_rx_queues) {
2555 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2559 /* Distribute the failed allocated queues*/
2560 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2561 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2564 if (adapter->req_rx_queues > adapter->min_rx_queues)
2565 adapter->req_rx_queues--;
2570 if (adapter->req_tx_queues > adapter->min_tx_queues)
2571 adapter->req_tx_queues--;
2578 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2579 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2580 if (!adapter->tx_scrq)
2583 for (i = 0; i < adapter->req_tx_queues; i++) {
2584 adapter->tx_scrq[i] = allqueues[i];
2585 adapter->tx_scrq[i]->pool_index = i;
2588 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2589 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2590 if (!adapter->rx_scrq)
2593 for (i = 0; i < adapter->req_rx_queues; i++) {
2594 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2595 adapter->rx_scrq[i]->scrq_num = i;
2602 kfree(adapter->tx_scrq);
2603 adapter->tx_scrq = NULL;
2605 for (i = 0; i < registered_queues; i++)
2606 release_sub_crq_queue(adapter, allqueues[i]);
2611 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2613 struct device *dev = &adapter->vdev->dev;
2614 union ibmvnic_crq crq;
2618 /* Sub-CRQ entries are 32 byte long */
2619 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2621 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2622 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2623 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2627 if (adapter->desired.mtu)
2628 adapter->req_mtu = adapter->desired.mtu;
2630 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2632 if (!adapter->desired.tx_entries)
2633 adapter->desired.tx_entries =
2634 adapter->max_tx_entries_per_subcrq;
2635 if (!adapter->desired.rx_entries)
2636 adapter->desired.rx_entries =
2637 adapter->max_rx_add_entries_per_subcrq;
2639 max_entries = IBMVNIC_MAX_LTB_SIZE /
2640 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2642 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2643 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2644 adapter->desired.tx_entries = max_entries;
2647 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2648 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2649 adapter->desired.rx_entries = max_entries;
2652 if (adapter->desired.tx_entries)
2653 adapter->req_tx_entries_per_subcrq =
2654 adapter->desired.tx_entries;
2656 adapter->req_tx_entries_per_subcrq =
2657 adapter->max_tx_entries_per_subcrq;
2659 if (adapter->desired.rx_entries)
2660 adapter->req_rx_add_entries_per_subcrq =
2661 adapter->desired.rx_entries;
2663 adapter->req_rx_add_entries_per_subcrq =
2664 adapter->max_rx_add_entries_per_subcrq;
2666 if (adapter->desired.tx_queues)
2667 adapter->req_tx_queues =
2668 adapter->desired.tx_queues;
2670 adapter->req_tx_queues =
2671 adapter->opt_tx_comp_sub_queues;
2673 if (adapter->desired.rx_queues)
2674 adapter->req_rx_queues =
2675 adapter->desired.rx_queues;
2677 adapter->req_rx_queues =
2678 adapter->opt_rx_comp_queues;
2680 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2683 memset(&crq, 0, sizeof(crq));
2684 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2685 crq.request_capability.cmd = REQUEST_CAPABILITY;
2687 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2688 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2689 atomic_inc(&adapter->running_cap_crqs);
2690 ibmvnic_send_crq(adapter, &crq);
2692 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2693 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2694 atomic_inc(&adapter->running_cap_crqs);
2695 ibmvnic_send_crq(adapter, &crq);
2697 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2698 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2699 atomic_inc(&adapter->running_cap_crqs);
2700 ibmvnic_send_crq(adapter, &crq);
2702 crq.request_capability.capability =
2703 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2704 crq.request_capability.number =
2705 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2706 atomic_inc(&adapter->running_cap_crqs);
2707 ibmvnic_send_crq(adapter, &crq);
2709 crq.request_capability.capability =
2710 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2711 crq.request_capability.number =
2712 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2713 atomic_inc(&adapter->running_cap_crqs);
2714 ibmvnic_send_crq(adapter, &crq);
2716 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2717 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2718 atomic_inc(&adapter->running_cap_crqs);
2719 ibmvnic_send_crq(adapter, &crq);
2721 if (adapter->netdev->flags & IFF_PROMISC) {
2722 if (adapter->promisc_supported) {
2723 crq.request_capability.capability =
2724 cpu_to_be16(PROMISC_REQUESTED);
2725 crq.request_capability.number = cpu_to_be64(1);
2726 atomic_inc(&adapter->running_cap_crqs);
2727 ibmvnic_send_crq(adapter, &crq);
2730 crq.request_capability.capability =
2731 cpu_to_be16(PROMISC_REQUESTED);
2732 crq.request_capability.number = cpu_to_be64(0);
2733 atomic_inc(&adapter->running_cap_crqs);
2734 ibmvnic_send_crq(adapter, &crq);
2738 static int pending_scrq(struct ibmvnic_adapter *adapter,
2739 struct ibmvnic_sub_crq_queue *scrq)
2741 union sub_crq *entry = &scrq->msgs[scrq->cur];
2743 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2749 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2750 struct ibmvnic_sub_crq_queue *scrq)
2752 union sub_crq *entry;
2753 unsigned long flags;
2755 spin_lock_irqsave(&scrq->lock, flags);
2756 entry = &scrq->msgs[scrq->cur];
2757 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2758 if (++scrq->cur == scrq->size)
2763 spin_unlock_irqrestore(&scrq->lock, flags);
2768 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2770 struct ibmvnic_crq_queue *queue = &adapter->crq;
2771 union ibmvnic_crq *crq;
2773 crq = &queue->msgs[queue->cur];
2774 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2775 if (++queue->cur == queue->size)
2784 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2785 union sub_crq *sub_crq)
2787 unsigned int ua = adapter->vdev->unit_address;
2788 struct device *dev = &adapter->vdev->dev;
2789 u64 *u64_crq = (u64 *)sub_crq;
2792 netdev_dbg(adapter->netdev,
2793 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2794 (unsigned long int)cpu_to_be64(remote_handle),
2795 (unsigned long int)cpu_to_be64(u64_crq[0]),
2796 (unsigned long int)cpu_to_be64(u64_crq[1]),
2797 (unsigned long int)cpu_to_be64(u64_crq[2]),
2798 (unsigned long int)cpu_to_be64(u64_crq[3]));
2800 /* Make sure the hypervisor sees the complete request */
2803 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2804 cpu_to_be64(remote_handle),
2805 cpu_to_be64(u64_crq[0]),
2806 cpu_to_be64(u64_crq[1]),
2807 cpu_to_be64(u64_crq[2]),
2808 cpu_to_be64(u64_crq[3]));
2812 dev_warn(dev, "CRQ Queue closed\n");
2813 dev_err(dev, "Send error (rc=%d)\n", rc);
2819 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2820 u64 remote_handle, u64 ioba, u64 num_entries)
2822 unsigned int ua = adapter->vdev->unit_address;
2823 struct device *dev = &adapter->vdev->dev;
2826 /* Make sure the hypervisor sees the complete request */
2828 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2829 cpu_to_be64(remote_handle),
2834 dev_warn(dev, "CRQ Queue closed\n");
2835 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2841 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2842 union ibmvnic_crq *crq)
2844 unsigned int ua = adapter->vdev->unit_address;
2845 struct device *dev = &adapter->vdev->dev;
2846 u64 *u64_crq = (u64 *)crq;
2849 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2850 (unsigned long int)cpu_to_be64(u64_crq[0]),
2851 (unsigned long int)cpu_to_be64(u64_crq[1]));
2853 /* Make sure the hypervisor sees the complete request */
2856 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2857 cpu_to_be64(u64_crq[0]),
2858 cpu_to_be64(u64_crq[1]));
2862 dev_warn(dev, "CRQ Queue closed\n");
2863 dev_warn(dev, "Send error (rc=%d)\n", rc);
2869 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2871 union ibmvnic_crq crq;
2873 memset(&crq, 0, sizeof(crq));
2874 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2875 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2876 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2878 return ibmvnic_send_crq(adapter, &crq);
2881 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2883 union ibmvnic_crq crq;
2885 memset(&crq, 0, sizeof(crq));
2886 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2887 crq.version_exchange.cmd = VERSION_EXCHANGE;
2888 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2890 return ibmvnic_send_crq(adapter, &crq);
2893 struct vnic_login_client_data {
2899 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
2903 /* Calculate the amount of buffer space needed for the
2904 * vnic client data in the login buffer. There are four entries,
2905 * OS name, LPAR name, device name, and a null last entry.
2907 len = 4 * sizeof(struct vnic_login_client_data);
2908 len += 6; /* "Linux" plus NULL */
2909 len += strlen(utsname()->nodename) + 1;
2910 len += strlen(adapter->netdev->name) + 1;
2915 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
2916 struct vnic_login_client_data *vlcd)
2918 const char *os_name = "Linux";
2921 /* Type 1 - LPAR OS */
2923 len = strlen(os_name) + 1;
2924 vlcd->len = cpu_to_be16(len);
2925 strncpy(&vlcd->name, os_name, len);
2926 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2928 /* Type 2 - LPAR name */
2930 len = strlen(utsname()->nodename) + 1;
2931 vlcd->len = cpu_to_be16(len);
2932 strncpy(&vlcd->name, utsname()->nodename, len);
2933 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
2935 /* Type 3 - device name */
2937 len = strlen(adapter->netdev->name) + 1;
2938 vlcd->len = cpu_to_be16(len);
2939 strncpy(&vlcd->name, adapter->netdev->name, len);
2942 static void send_login(struct ibmvnic_adapter *adapter)
2944 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
2945 struct ibmvnic_login_buffer *login_buffer;
2946 struct device *dev = &adapter->vdev->dev;
2947 dma_addr_t rsp_buffer_token;
2948 dma_addr_t buffer_token;
2949 size_t rsp_buffer_size;
2950 union ibmvnic_crq crq;
2954 int client_data_len;
2955 struct vnic_login_client_data *vlcd;
2958 client_data_len = vnic_client_data_len(adapter);
2961 sizeof(struct ibmvnic_login_buffer) +
2962 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
2965 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
2967 goto buf_alloc_failed;
2969 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
2971 if (dma_mapping_error(dev, buffer_token)) {
2972 dev_err(dev, "Couldn't map login buffer\n");
2973 goto buf_map_failed;
2976 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
2977 sizeof(u64) * adapter->req_tx_queues +
2978 sizeof(u64) * adapter->req_rx_queues +
2979 sizeof(u64) * adapter->req_rx_queues +
2980 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
2982 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
2983 if (!login_rsp_buffer)
2984 goto buf_rsp_alloc_failed;
2986 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
2987 rsp_buffer_size, DMA_FROM_DEVICE);
2988 if (dma_mapping_error(dev, rsp_buffer_token)) {
2989 dev_err(dev, "Couldn't map login rsp buffer\n");
2990 goto buf_rsp_map_failed;
2993 adapter->login_buf = login_buffer;
2994 adapter->login_buf_token = buffer_token;
2995 adapter->login_buf_sz = buffer_size;
2996 adapter->login_rsp_buf = login_rsp_buffer;
2997 adapter->login_rsp_buf_token = rsp_buffer_token;
2998 adapter->login_rsp_buf_sz = rsp_buffer_size;
3000 login_buffer->len = cpu_to_be32(buffer_size);
3001 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3002 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3003 login_buffer->off_txcomp_subcrqs =
3004 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3005 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3006 login_buffer->off_rxcomp_subcrqs =
3007 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3008 sizeof(u64) * adapter->req_tx_queues);
3009 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3010 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3012 tx_list_p = (__be64 *)((char *)login_buffer +
3013 sizeof(struct ibmvnic_login_buffer));
3014 rx_list_p = (__be64 *)((char *)login_buffer +
3015 sizeof(struct ibmvnic_login_buffer) +
3016 sizeof(u64) * adapter->req_tx_queues);
3018 for (i = 0; i < adapter->req_tx_queues; i++) {
3019 if (adapter->tx_scrq[i]) {
3020 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3025 for (i = 0; i < adapter->req_rx_queues; i++) {
3026 if (adapter->rx_scrq[i]) {
3027 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3032 /* Insert vNIC login client data */
3033 vlcd = (struct vnic_login_client_data *)
3034 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3035 login_buffer->client_data_offset =
3036 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3037 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3039 vnic_add_client_data(adapter, vlcd);
3041 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3042 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3043 netdev_dbg(adapter->netdev, "%016lx\n",
3044 ((unsigned long int *)(adapter->login_buf))[i]);
3047 memset(&crq, 0, sizeof(crq));
3048 crq.login.first = IBMVNIC_CRQ_CMD;
3049 crq.login.cmd = LOGIN;
3050 crq.login.ioba = cpu_to_be32(buffer_token);
3051 crq.login.len = cpu_to_be32(buffer_size);
3052 ibmvnic_send_crq(adapter, &crq);
3057 kfree(login_rsp_buffer);
3058 buf_rsp_alloc_failed:
3059 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3061 kfree(login_buffer);
3066 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3069 union ibmvnic_crq crq;
3071 memset(&crq, 0, sizeof(crq));
3072 crq.request_map.first = IBMVNIC_CRQ_CMD;
3073 crq.request_map.cmd = REQUEST_MAP;
3074 crq.request_map.map_id = map_id;
3075 crq.request_map.ioba = cpu_to_be32(addr);
3076 crq.request_map.len = cpu_to_be32(len);
3077 ibmvnic_send_crq(adapter, &crq);
3080 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3082 union ibmvnic_crq crq;
3084 memset(&crq, 0, sizeof(crq));
3085 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3086 crq.request_unmap.cmd = REQUEST_UNMAP;
3087 crq.request_unmap.map_id = map_id;
3088 ibmvnic_send_crq(adapter, &crq);
3091 static void send_map_query(struct ibmvnic_adapter *adapter)
3093 union ibmvnic_crq crq;
3095 memset(&crq, 0, sizeof(crq));
3096 crq.query_map.first = IBMVNIC_CRQ_CMD;
3097 crq.query_map.cmd = QUERY_MAP;
3098 ibmvnic_send_crq(adapter, &crq);
3101 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3102 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3104 union ibmvnic_crq crq;
3106 atomic_set(&adapter->running_cap_crqs, 0);
3107 memset(&crq, 0, sizeof(crq));
3108 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3109 crq.query_capability.cmd = QUERY_CAPABILITY;
3111 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3112 atomic_inc(&adapter->running_cap_crqs);
3113 ibmvnic_send_crq(adapter, &crq);
3115 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3116 atomic_inc(&adapter->running_cap_crqs);
3117 ibmvnic_send_crq(adapter, &crq);
3119 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3120 atomic_inc(&adapter->running_cap_crqs);
3121 ibmvnic_send_crq(adapter, &crq);
3123 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3124 atomic_inc(&adapter->running_cap_crqs);
3125 ibmvnic_send_crq(adapter, &crq);
3127 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3128 atomic_inc(&adapter->running_cap_crqs);
3129 ibmvnic_send_crq(adapter, &crq);
3131 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3132 atomic_inc(&adapter->running_cap_crqs);
3133 ibmvnic_send_crq(adapter, &crq);
3135 crq.query_capability.capability =
3136 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3137 atomic_inc(&adapter->running_cap_crqs);
3138 ibmvnic_send_crq(adapter, &crq);
3140 crq.query_capability.capability =
3141 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3142 atomic_inc(&adapter->running_cap_crqs);
3143 ibmvnic_send_crq(adapter, &crq);
3145 crq.query_capability.capability =
3146 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3147 atomic_inc(&adapter->running_cap_crqs);
3148 ibmvnic_send_crq(adapter, &crq);
3150 crq.query_capability.capability =
3151 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3152 atomic_inc(&adapter->running_cap_crqs);
3153 ibmvnic_send_crq(adapter, &crq);
3155 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3156 atomic_inc(&adapter->running_cap_crqs);
3157 ibmvnic_send_crq(adapter, &crq);
3159 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3160 atomic_inc(&adapter->running_cap_crqs);
3161 ibmvnic_send_crq(adapter, &crq);
3163 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3164 atomic_inc(&adapter->running_cap_crqs);
3165 ibmvnic_send_crq(adapter, &crq);
3167 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3168 atomic_inc(&adapter->running_cap_crqs);
3169 ibmvnic_send_crq(adapter, &crq);
3171 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3172 atomic_inc(&adapter->running_cap_crqs);
3173 ibmvnic_send_crq(adapter, &crq);
3175 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3176 atomic_inc(&adapter->running_cap_crqs);
3177 ibmvnic_send_crq(adapter, &crq);
3179 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3180 atomic_inc(&adapter->running_cap_crqs);
3181 ibmvnic_send_crq(adapter, &crq);
3183 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3184 atomic_inc(&adapter->running_cap_crqs);
3185 ibmvnic_send_crq(adapter, &crq);
3187 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3188 atomic_inc(&adapter->running_cap_crqs);
3189 ibmvnic_send_crq(adapter, &crq);
3191 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3192 atomic_inc(&adapter->running_cap_crqs);
3193 ibmvnic_send_crq(adapter, &crq);
3195 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3196 atomic_inc(&adapter->running_cap_crqs);
3197 ibmvnic_send_crq(adapter, &crq);
3199 crq.query_capability.capability =
3200 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3201 atomic_inc(&adapter->running_cap_crqs);
3202 ibmvnic_send_crq(adapter, &crq);
3204 crq.query_capability.capability =
3205 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3206 atomic_inc(&adapter->running_cap_crqs);
3207 ibmvnic_send_crq(adapter, &crq);
3209 crq.query_capability.capability =
3210 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3211 atomic_inc(&adapter->running_cap_crqs);
3212 ibmvnic_send_crq(adapter, &crq);
3214 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3215 atomic_inc(&adapter->running_cap_crqs);
3216 ibmvnic_send_crq(adapter, &crq);
3219 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3220 struct ibmvnic_adapter *adapter)
3222 struct device *dev = &adapter->vdev->dev;
3224 if (crq->get_vpd_size_rsp.rc.code) {
3225 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3226 crq->get_vpd_size_rsp.rc.code);
3227 complete(&adapter->fw_done);
3231 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3232 complete(&adapter->fw_done);
3235 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3236 struct ibmvnic_adapter *adapter)
3238 struct device *dev = &adapter->vdev->dev;
3239 unsigned char *substr = NULL, *ptr = NULL;
3240 u8 fw_level_len = 0;
3242 memset(adapter->fw_version, 0, 32);
3244 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3247 if (crq->get_vpd_rsp.rc.code) {
3248 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3249 crq->get_vpd_rsp.rc.code);
3253 /* get the position of the firmware version info
3254 * located after the ASCII 'RM' substring in the buffer
3256 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3258 dev_info(dev, "No FW level provided by VPD\n");
3262 /* get length of firmware level ASCII substring */
3263 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3264 fw_level_len = *(substr + 2);
3266 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3270 /* copy firmware version string from vpd into adapter */
3271 if ((substr + 3 + fw_level_len) <
3272 (adapter->vpd->buff + adapter->vpd->len)) {
3273 ptr = strncpy((char *)adapter->fw_version,
3274 substr + 3, fw_level_len);
3277 dev_err(dev, "Failed to isolate FW level string\n");
3279 dev_info(dev, "FW substr extrapolated VPD buff\n");
3283 complete(&adapter->fw_done);
3286 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3288 struct device *dev = &adapter->vdev->dev;
3289 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3290 union ibmvnic_crq crq;
3293 dma_unmap_single(dev, adapter->ip_offload_tok,
3294 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3296 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3297 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3298 netdev_dbg(adapter->netdev, "%016lx\n",
3299 ((unsigned long int *)(buf))[i]);
3301 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3302 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3303 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3304 buf->tcp_ipv4_chksum);
3305 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3306 buf->tcp_ipv6_chksum);
3307 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3308 buf->udp_ipv4_chksum);
3309 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3310 buf->udp_ipv6_chksum);
3311 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3312 buf->large_tx_ipv4);
3313 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3314 buf->large_tx_ipv6);
3315 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3316 buf->large_rx_ipv4);
3317 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3318 buf->large_rx_ipv6);
3319 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3320 buf->max_ipv4_header_size);
3321 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3322 buf->max_ipv6_header_size);
3323 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3324 buf->max_tcp_header_size);
3325 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3326 buf->max_udp_header_size);
3327 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3328 buf->max_large_tx_size);
3329 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3330 buf->max_large_rx_size);
3331 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3332 buf->ipv6_extension_header);
3333 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3334 buf->tcp_pseudosum_req);
3335 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3336 buf->num_ipv6_ext_headers);
3337 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3338 buf->off_ipv6_ext_headers);
3340 adapter->ip_offload_ctrl_tok =
3341 dma_map_single(dev, &adapter->ip_offload_ctrl,
3342 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3344 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3345 dev_err(dev, "Couldn't map ip offload control buffer\n");
3349 adapter->ip_offload_ctrl.len =
3350 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3351 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3352 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3353 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3354 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3355 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3356 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3357 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3358 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3359 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3361 /* large_rx disabled for now, additional features needed */
3362 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3363 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3365 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3367 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3368 adapter->netdev->features |= NETIF_F_IP_CSUM;
3370 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3371 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3373 if ((adapter->netdev->features &
3374 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3375 adapter->netdev->features |= NETIF_F_RXCSUM;
3377 if (buf->large_tx_ipv4)
3378 adapter->netdev->features |= NETIF_F_TSO;
3379 if (buf->large_tx_ipv6)
3380 adapter->netdev->features |= NETIF_F_TSO6;
3382 adapter->netdev->hw_features |= adapter->netdev->features;
3384 memset(&crq, 0, sizeof(crq));
3385 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3386 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3387 crq.control_ip_offload.len =
3388 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3389 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3390 ibmvnic_send_crq(adapter, &crq);
3393 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3394 struct ibmvnic_adapter *adapter)
3396 struct device *dev = &adapter->vdev->dev;
3397 struct ibmvnic_error_buff *error_buff, *tmp;
3398 unsigned long flags;
3402 if (!crq->request_error_rsp.rc.code) {
3403 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3404 crq->request_error_rsp.rc.code);
3408 spin_lock_irqsave(&adapter->error_list_lock, flags);
3409 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3410 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3412 list_del(&error_buff->list);
3415 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3418 dev_err(dev, "Couldn't find error id %x\n",
3419 be32_to_cpu(crq->request_error_rsp.error_id));
3423 dev_err(dev, "Detailed info for error id %x:",
3424 be32_to_cpu(crq->request_error_rsp.error_id));
3426 for (i = 0; i < error_buff->len; i++) {
3427 pr_cont("%02x", (int)error_buff->buff[i]);
3433 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3435 kfree(error_buff->buff);
3439 static void request_error_information(struct ibmvnic_adapter *adapter,
3440 union ibmvnic_crq *err_crq)
3442 struct device *dev = &adapter->vdev->dev;
3443 struct net_device *netdev = adapter->netdev;
3444 struct ibmvnic_error_buff *error_buff;
3445 unsigned long timeout = msecs_to_jiffies(30000);
3446 union ibmvnic_crq crq;
3447 unsigned long flags;
3450 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3454 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3455 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3456 if (!error_buff->buff) {
3461 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3463 if (dma_mapping_error(dev, error_buff->dma)) {
3464 netdev_err(netdev, "Couldn't map error buffer\n");
3465 kfree(error_buff->buff);
3470 error_buff->len = detail_len;
3471 error_buff->error_id = err_crq->error_indication.error_id;
3473 spin_lock_irqsave(&adapter->error_list_lock, flags);
3474 list_add_tail(&error_buff->list, &adapter->errors);
3475 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3477 memset(&crq, 0, sizeof(crq));
3478 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3479 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3480 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3481 crq.request_error_info.len = cpu_to_be32(detail_len);
3482 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3484 rc = ibmvnic_send_crq(adapter, &crq);
3486 netdev_err(netdev, "failed to request error information\n");
3490 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3491 netdev_err(netdev, "timeout waiting for error information\n");
3498 spin_lock_irqsave(&adapter->error_list_lock, flags);
3499 list_del(&error_buff->list);
3500 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3502 kfree(error_buff->buff);
3506 static void handle_error_indication(union ibmvnic_crq *crq,
3507 struct ibmvnic_adapter *adapter)
3509 struct device *dev = &adapter->vdev->dev;
3511 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3512 crq->error_indication.flags
3513 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3514 be32_to_cpu(crq->error_indication.error_id),
3515 be16_to_cpu(crq->error_indication.error_cause));
3517 if (be32_to_cpu(crq->error_indication.error_id))
3518 request_error_information(adapter, crq);
3520 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3521 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3523 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3526 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
3527 struct ibmvnic_adapter *adapter)
3529 struct net_device *netdev = adapter->netdev;
3530 struct device *dev = &adapter->vdev->dev;
3533 rc = crq->change_mac_addr_rsp.rc.code;
3535 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3538 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3542 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3543 struct ibmvnic_adapter *adapter)
3545 struct device *dev = &adapter->vdev->dev;
3549 atomic_dec(&adapter->running_cap_crqs);
3550 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3552 req_value = &adapter->req_tx_queues;
3556 req_value = &adapter->req_rx_queues;
3559 case REQ_RX_ADD_QUEUES:
3560 req_value = &adapter->req_rx_add_queues;
3563 case REQ_TX_ENTRIES_PER_SUBCRQ:
3564 req_value = &adapter->req_tx_entries_per_subcrq;
3565 name = "tx_entries_per_subcrq";
3567 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3568 req_value = &adapter->req_rx_add_entries_per_subcrq;
3569 name = "rx_add_entries_per_subcrq";
3572 req_value = &adapter->req_mtu;
3575 case PROMISC_REQUESTED:
3576 req_value = &adapter->promisc;
3580 dev_err(dev, "Got invalid cap request rsp %d\n",
3581 crq->request_capability.capability);
3585 switch (crq->request_capability_rsp.rc.code) {
3588 case PARTIALSUCCESS:
3589 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3591 (long int)be64_to_cpu(crq->request_capability_rsp.
3593 *req_value = be64_to_cpu(crq->request_capability_rsp.number);
3594 ibmvnic_send_req_caps(adapter, 1);
3597 dev_err(dev, "Error %d in request cap rsp\n",
3598 crq->request_capability_rsp.rc.code);
3602 /* Done receiving requested capabilities, query IP offload support */
3603 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3604 union ibmvnic_crq newcrq;
3605 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3606 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3607 &adapter->ip_offload_buf;
3609 adapter->wait_capability = false;
3610 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3614 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3615 if (!firmware_has_feature(FW_FEATURE_CMO))
3616 dev_err(dev, "Couldn't map offload buffer\n");
3620 memset(&newcrq, 0, sizeof(newcrq));
3621 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3622 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3623 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3624 newcrq.query_ip_offload.ioba =
3625 cpu_to_be32(adapter->ip_offload_tok);
3627 ibmvnic_send_crq(adapter, &newcrq);
3631 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3632 struct ibmvnic_adapter *adapter)
3634 struct device *dev = &adapter->vdev->dev;
3635 struct net_device *netdev = adapter->netdev;
3636 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3637 struct ibmvnic_login_buffer *login = adapter->login_buf;
3640 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3642 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3643 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3645 /* If the number of queues requested can't be allocated by the
3646 * server, the login response will return with code 1. We will need
3647 * to resend the login buffer with fewer queues requested.
3649 if (login_rsp_crq->generic.rc.code) {
3650 adapter->renegotiate = true;
3651 complete(&adapter->init_done);
3655 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3657 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3658 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3659 netdev_dbg(adapter->netdev, "%016lx\n",
3660 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3664 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3665 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3666 adapter->req_rx_add_queues !=
3667 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3668 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3669 ibmvnic_remove(adapter->vdev);
3672 complete(&adapter->init_done);
3677 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3678 struct ibmvnic_adapter *adapter)
3680 struct device *dev = &adapter->vdev->dev;
3683 rc = crq->request_unmap_rsp.rc.code;
3685 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3688 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3689 struct ibmvnic_adapter *adapter)
3691 struct net_device *netdev = adapter->netdev;
3692 struct device *dev = &adapter->vdev->dev;
3695 rc = crq->query_map_rsp.rc.code;
3697 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3700 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3701 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3702 crq->query_map_rsp.free_pages);
3705 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3706 struct ibmvnic_adapter *adapter)
3708 struct net_device *netdev = adapter->netdev;
3709 struct device *dev = &adapter->vdev->dev;
3712 atomic_dec(&adapter->running_cap_crqs);
3713 netdev_dbg(netdev, "Outstanding queries: %d\n",
3714 atomic_read(&adapter->running_cap_crqs));
3715 rc = crq->query_capability.rc.code;
3717 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3721 switch (be16_to_cpu(crq->query_capability.capability)) {
3723 adapter->min_tx_queues =
3724 be64_to_cpu(crq->query_capability.number);
3725 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3726 adapter->min_tx_queues);
3729 adapter->min_rx_queues =
3730 be64_to_cpu(crq->query_capability.number);
3731 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3732 adapter->min_rx_queues);
3734 case MIN_RX_ADD_QUEUES:
3735 adapter->min_rx_add_queues =
3736 be64_to_cpu(crq->query_capability.number);
3737 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3738 adapter->min_rx_add_queues);
3741 adapter->max_tx_queues =
3742 be64_to_cpu(crq->query_capability.number);
3743 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3744 adapter->max_tx_queues);
3747 adapter->max_rx_queues =
3748 be64_to_cpu(crq->query_capability.number);
3749 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3750 adapter->max_rx_queues);
3752 case MAX_RX_ADD_QUEUES:
3753 adapter->max_rx_add_queues =
3754 be64_to_cpu(crq->query_capability.number);
3755 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3756 adapter->max_rx_add_queues);
3758 case MIN_TX_ENTRIES_PER_SUBCRQ:
3759 adapter->min_tx_entries_per_subcrq =
3760 be64_to_cpu(crq->query_capability.number);
3761 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3762 adapter->min_tx_entries_per_subcrq);
3764 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3765 adapter->min_rx_add_entries_per_subcrq =
3766 be64_to_cpu(crq->query_capability.number);
3767 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3768 adapter->min_rx_add_entries_per_subcrq);
3770 case MAX_TX_ENTRIES_PER_SUBCRQ:
3771 adapter->max_tx_entries_per_subcrq =
3772 be64_to_cpu(crq->query_capability.number);
3773 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3774 adapter->max_tx_entries_per_subcrq);
3776 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3777 adapter->max_rx_add_entries_per_subcrq =
3778 be64_to_cpu(crq->query_capability.number);
3779 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3780 adapter->max_rx_add_entries_per_subcrq);
3782 case TCP_IP_OFFLOAD:
3783 adapter->tcp_ip_offload =
3784 be64_to_cpu(crq->query_capability.number);
3785 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3786 adapter->tcp_ip_offload);
3788 case PROMISC_SUPPORTED:
3789 adapter->promisc_supported =
3790 be64_to_cpu(crq->query_capability.number);
3791 netdev_dbg(netdev, "promisc_supported = %lld\n",
3792 adapter->promisc_supported);
3795 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3796 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3797 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3800 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3801 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3802 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3804 case MAX_MULTICAST_FILTERS:
3805 adapter->max_multicast_filters =
3806 be64_to_cpu(crq->query_capability.number);
3807 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3808 adapter->max_multicast_filters);
3810 case VLAN_HEADER_INSERTION:
3811 adapter->vlan_header_insertion =
3812 be64_to_cpu(crq->query_capability.number);
3813 if (adapter->vlan_header_insertion)
3814 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3815 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3816 adapter->vlan_header_insertion);
3818 case RX_VLAN_HEADER_INSERTION:
3819 adapter->rx_vlan_header_insertion =
3820 be64_to_cpu(crq->query_capability.number);
3821 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3822 adapter->rx_vlan_header_insertion);
3824 case MAX_TX_SG_ENTRIES:
3825 adapter->max_tx_sg_entries =
3826 be64_to_cpu(crq->query_capability.number);
3827 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3828 adapter->max_tx_sg_entries);
3830 case RX_SG_SUPPORTED:
3831 adapter->rx_sg_supported =
3832 be64_to_cpu(crq->query_capability.number);
3833 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3834 adapter->rx_sg_supported);
3836 case OPT_TX_COMP_SUB_QUEUES:
3837 adapter->opt_tx_comp_sub_queues =
3838 be64_to_cpu(crq->query_capability.number);
3839 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3840 adapter->opt_tx_comp_sub_queues);
3842 case OPT_RX_COMP_QUEUES:
3843 adapter->opt_rx_comp_queues =
3844 be64_to_cpu(crq->query_capability.number);
3845 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3846 adapter->opt_rx_comp_queues);
3848 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3849 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3850 be64_to_cpu(crq->query_capability.number);
3851 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3852 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3854 case OPT_TX_ENTRIES_PER_SUBCRQ:
3855 adapter->opt_tx_entries_per_subcrq =
3856 be64_to_cpu(crq->query_capability.number);
3857 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3858 adapter->opt_tx_entries_per_subcrq);
3860 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3861 adapter->opt_rxba_entries_per_subcrq =
3862 be64_to_cpu(crq->query_capability.number);
3863 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3864 adapter->opt_rxba_entries_per_subcrq);
3866 case TX_RX_DESC_REQ:
3867 adapter->tx_rx_desc_req = crq->query_capability.number;
3868 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3869 adapter->tx_rx_desc_req);
3873 netdev_err(netdev, "Got invalid cap rsp %d\n",
3874 crq->query_capability.capability);
3878 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3879 adapter->wait_capability = false;
3880 ibmvnic_send_req_caps(adapter, 0);
3884 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
3885 struct ibmvnic_adapter *adapter)
3887 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
3888 struct net_device *netdev = adapter->netdev;
3889 struct device *dev = &adapter->vdev->dev;
3890 u64 *u64_crq = (u64 *)crq;
3893 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
3894 (unsigned long int)cpu_to_be64(u64_crq[0]),
3895 (unsigned long int)cpu_to_be64(u64_crq[1]));
3896 switch (gen_crq->first) {
3897 case IBMVNIC_CRQ_INIT_RSP:
3898 switch (gen_crq->cmd) {
3899 case IBMVNIC_CRQ_INIT:
3900 dev_info(dev, "Partner initialized\n");
3901 adapter->from_passive_init = true;
3902 complete(&adapter->init_done);
3904 case IBMVNIC_CRQ_INIT_COMPLETE:
3905 dev_info(dev, "Partner initialization complete\n");
3906 send_version_xchg(adapter);
3909 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
3912 case IBMVNIC_CRQ_XPORT_EVENT:
3913 netif_carrier_off(netdev);
3914 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
3915 dev_info(dev, "Migrated, re-enabling adapter\n");
3916 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
3917 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
3918 dev_info(dev, "Backing device failover detected\n");
3919 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
3921 /* The adapter lost the connection */
3922 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
3924 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3927 case IBMVNIC_CRQ_CMD_RSP:
3930 dev_err(dev, "Got an invalid msg type 0x%02x\n",
3935 switch (gen_crq->cmd) {
3936 case VERSION_EXCHANGE_RSP:
3937 rc = crq->version_exchange_rsp.rc.code;
3939 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
3942 dev_info(dev, "Partner protocol version is %d\n",
3943 crq->version_exchange_rsp.version);
3944 if (be16_to_cpu(crq->version_exchange_rsp.version) <
3947 be16_to_cpu(crq->version_exchange_rsp.version);
3948 send_cap_queries(adapter);
3950 case QUERY_CAPABILITY_RSP:
3951 handle_query_cap_rsp(crq, adapter);
3954 handle_query_map_rsp(crq, adapter);
3956 case REQUEST_MAP_RSP:
3957 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
3958 complete(&adapter->fw_done);
3960 case REQUEST_UNMAP_RSP:
3961 handle_request_unmap_rsp(crq, adapter);
3963 case REQUEST_CAPABILITY_RSP:
3964 handle_request_cap_rsp(crq, adapter);
3967 netdev_dbg(netdev, "Got Login Response\n");
3968 handle_login_rsp(crq, adapter);
3970 case LOGICAL_LINK_STATE_RSP:
3972 "Got Logical Link State Response, state: %d rc: %d\n",
3973 crq->logical_link_state_rsp.link_state,
3974 crq->logical_link_state_rsp.rc.code);
3975 adapter->logical_link_state =
3976 crq->logical_link_state_rsp.link_state;
3977 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
3978 complete(&adapter->init_done);
3980 case LINK_STATE_INDICATION:
3981 netdev_dbg(netdev, "Got Logical Link State Indication\n");
3982 adapter->phys_link_state =
3983 crq->link_state_indication.phys_link_state;
3984 adapter->logical_link_state =
3985 crq->link_state_indication.logical_link_state;
3987 case CHANGE_MAC_ADDR_RSP:
3988 netdev_dbg(netdev, "Got MAC address change Response\n");
3989 handle_change_mac_rsp(crq, adapter);
3991 case ERROR_INDICATION:
3992 netdev_dbg(netdev, "Got Error Indication\n");
3993 handle_error_indication(crq, adapter);
3995 case REQUEST_ERROR_RSP:
3996 netdev_dbg(netdev, "Got Error Detail Response\n");
3997 handle_error_info_rsp(crq, adapter);
3999 case REQUEST_STATISTICS_RSP:
4000 netdev_dbg(netdev, "Got Statistics Response\n");
4001 complete(&adapter->stats_done);
4003 case QUERY_IP_OFFLOAD_RSP:
4004 netdev_dbg(netdev, "Got Query IP offload Response\n");
4005 handle_query_ip_offload_rsp(adapter);
4007 case MULTICAST_CTRL_RSP:
4008 netdev_dbg(netdev, "Got multicast control Response\n");
4010 case CONTROL_IP_OFFLOAD_RSP:
4011 netdev_dbg(netdev, "Got Control IP offload Response\n");
4012 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4013 sizeof(adapter->ip_offload_ctrl),
4015 complete(&adapter->init_done);
4017 case COLLECT_FW_TRACE_RSP:
4018 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4019 complete(&adapter->fw_done);
4021 case GET_VPD_SIZE_RSP:
4022 handle_vpd_size_rsp(crq, adapter);
4025 handle_vpd_rsp(crq, adapter);
4028 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4033 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4035 struct ibmvnic_adapter *adapter = instance;
4037 tasklet_schedule(&adapter->tasklet);
4041 static void ibmvnic_tasklet(void *data)
4043 struct ibmvnic_adapter *adapter = data;
4044 struct ibmvnic_crq_queue *queue = &adapter->crq;
4045 union ibmvnic_crq *crq;
4046 unsigned long flags;
4049 spin_lock_irqsave(&queue->lock, flags);
4051 /* Pull all the valid messages off the CRQ */
4052 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4053 ibmvnic_handle_crq(crq, adapter);
4054 crq->generic.first = 0;
4057 /* remain in tasklet until all
4058 * capabilities responses are received
4060 if (!adapter->wait_capability)
4063 /* if capabilities CRQ's were sent in this tasklet, the following
4064 * tasklet must wait until all responses are received
4066 if (atomic_read(&adapter->running_cap_crqs) != 0)
4067 adapter->wait_capability = true;
4068 spin_unlock_irqrestore(&queue->lock, flags);
4071 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4073 struct vio_dev *vdev = adapter->vdev;
4077 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4078 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4081 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4086 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4088 struct ibmvnic_crq_queue *crq = &adapter->crq;
4089 struct device *dev = &adapter->vdev->dev;
4090 struct vio_dev *vdev = adapter->vdev;
4095 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4096 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4098 /* Clean out the queue */
4099 memset(crq->msgs, 0, PAGE_SIZE);
4102 /* And re-open it again */
4103 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4104 crq->msg_token, PAGE_SIZE);
4107 /* Adapter is good, but other end is not ready */
4108 dev_warn(dev, "Partner adapter not ready\n");
4110 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4115 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4117 struct ibmvnic_crq_queue *crq = &adapter->crq;
4118 struct vio_dev *vdev = adapter->vdev;
4124 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4125 free_irq(vdev->irq, adapter);
4126 tasklet_kill(&adapter->tasklet);
4128 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4129 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4131 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4133 free_page((unsigned long)crq->msgs);
4137 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4139 struct ibmvnic_crq_queue *crq = &adapter->crq;
4140 struct device *dev = &adapter->vdev->dev;
4141 struct vio_dev *vdev = adapter->vdev;
4142 int rc, retrc = -ENOMEM;
4147 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4148 /* Should we allocate more than one page? */
4153 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4154 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4156 if (dma_mapping_error(dev, crq->msg_token))
4159 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4160 crq->msg_token, PAGE_SIZE);
4162 if (rc == H_RESOURCE)
4163 /* maybe kexecing and resource is busy. try a reset */
4164 rc = ibmvnic_reset_crq(adapter);
4167 if (rc == H_CLOSED) {
4168 dev_warn(dev, "Partner adapter not ready\n");
4170 dev_warn(dev, "Error %d opening adapter\n", rc);
4171 goto reg_crq_failed;
4176 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4177 (unsigned long)adapter);
4179 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4180 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4183 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4185 goto req_irq_failed;
4188 rc = vio_enable_interrupts(vdev);
4190 dev_err(dev, "Error %d enabling interrupts\n", rc);
4191 goto req_irq_failed;
4195 spin_lock_init(&crq->lock);
4200 tasklet_kill(&adapter->tasklet);
4202 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4203 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4205 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4207 free_page((unsigned long)crq->msgs);
4212 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4214 struct device *dev = &adapter->vdev->dev;
4215 unsigned long timeout = msecs_to_jiffies(30000);
4218 if (adapter->resetting && !adapter->wait_for_reset) {
4219 rc = ibmvnic_reset_crq(adapter);
4221 rc = vio_enable_interrupts(adapter->vdev);
4223 rc = init_crq_queue(adapter);
4227 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4231 adapter->from_passive_init = false;
4233 init_completion(&adapter->init_done);
4234 adapter->init_done_rc = 0;
4235 ibmvnic_send_crq_init(adapter);
4236 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4237 dev_err(dev, "Initialization sequence timed out\n");
4241 if (adapter->init_done_rc) {
4242 release_crq_queue(adapter);
4243 return adapter->init_done_rc;
4246 if (adapter->from_passive_init) {
4247 adapter->state = VNIC_OPEN;
4248 adapter->from_passive_init = false;
4252 if (adapter->resetting && !adapter->wait_for_reset)
4253 rc = reset_sub_crq_queues(adapter);
4255 rc = init_sub_crqs(adapter);
4257 dev_err(dev, "Initialization of sub crqs failed\n");
4258 release_crq_queue(adapter);
4262 rc = init_sub_crq_irqs(adapter);
4264 dev_err(dev, "Failed to initialize sub crq irqs\n");
4265 release_crq_queue(adapter);
4271 static struct device_attribute dev_attr_failover;
4273 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4275 struct ibmvnic_adapter *adapter;
4276 struct net_device *netdev;
4277 unsigned char *mac_addr_p;
4280 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4283 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4284 VETH_MAC_ADDR, NULL);
4287 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4288 __FILE__, __LINE__);
4292 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4293 IBMVNIC_MAX_TX_QUEUES);
4297 adapter = netdev_priv(netdev);
4298 adapter->state = VNIC_PROBING;
4299 dev_set_drvdata(&dev->dev, netdev);
4300 adapter->vdev = dev;
4301 adapter->netdev = netdev;
4303 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4304 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4305 netdev->irq = dev->irq;
4306 netdev->netdev_ops = &ibmvnic_netdev_ops;
4307 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4308 SET_NETDEV_DEV(netdev, &dev->dev);
4310 spin_lock_init(&adapter->stats_lock);
4312 INIT_LIST_HEAD(&adapter->errors);
4313 spin_lock_init(&adapter->error_list_lock);
4315 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4316 INIT_LIST_HEAD(&adapter->rwi_list);
4317 mutex_init(&adapter->reset_lock);
4318 mutex_init(&adapter->rwi_lock);
4319 adapter->resetting = false;
4321 adapter->mac_change_pending = false;
4324 rc = ibmvnic_init(adapter);
4325 if (rc && rc != EAGAIN)
4326 goto ibmvnic_init_fail;
4327 } while (rc == EAGAIN);
4329 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4330 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4331 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4333 rc = device_create_file(&dev->dev, &dev_attr_failover);
4335 goto ibmvnic_init_fail;
4337 netif_carrier_off(netdev);
4338 rc = register_netdev(netdev);
4340 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4341 goto ibmvnic_register_fail;
4343 dev_info(&dev->dev, "ibmvnic registered\n");
4345 adapter->state = VNIC_PROBED;
4347 adapter->wait_for_reset = false;
4351 ibmvnic_register_fail:
4352 device_remove_file(&dev->dev, &dev_attr_failover);
4355 release_sub_crqs(adapter);
4356 release_crq_queue(adapter);
4357 free_netdev(netdev);
4362 static int ibmvnic_remove(struct vio_dev *dev)
4364 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4365 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4367 adapter->state = VNIC_REMOVING;
4368 unregister_netdev(netdev);
4369 mutex_lock(&adapter->reset_lock);
4371 release_resources(adapter);
4372 release_sub_crqs(adapter);
4373 release_crq_queue(adapter);
4375 adapter->state = VNIC_REMOVED;
4377 mutex_unlock(&adapter->reset_lock);
4378 device_remove_file(&dev->dev, &dev_attr_failover);
4379 free_netdev(netdev);
4380 dev_set_drvdata(&dev->dev, NULL);
4385 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4386 const char *buf, size_t count)
4388 struct net_device *netdev = dev_get_drvdata(dev);
4389 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4390 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4391 __be64 session_token;
4394 if (!sysfs_streq(buf, "1"))
4397 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4398 H_GET_SESSION_TOKEN, 0, 0, 0);
4400 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4405 session_token = (__be64)retbuf[0];
4406 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4407 be64_to_cpu(session_token));
4408 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4409 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4411 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4419 static DEVICE_ATTR(failover, 0200, NULL, failover_store);
4421 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4423 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4424 struct ibmvnic_adapter *adapter;
4425 struct iommu_table *tbl;
4426 unsigned long ret = 0;
4429 tbl = get_iommu_table_base(&vdev->dev);
4431 /* netdev inits at probe time along with the structures we need below*/
4433 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4435 adapter = netdev_priv(netdev);
4437 ret += PAGE_SIZE; /* the crq message queue */
4438 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4440 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4441 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4443 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4445 ret += adapter->rx_pool[i].size *
4446 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4451 static int ibmvnic_resume(struct device *dev)
4453 struct net_device *netdev = dev_get_drvdata(dev);
4454 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4456 if (adapter->state != VNIC_OPEN)
4459 tasklet_schedule(&adapter->tasklet);
4464 static const struct vio_device_id ibmvnic_device_table[] = {
4465 {"network", "IBM,vnic"},
4468 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4470 static const struct dev_pm_ops ibmvnic_pm_ops = {
4471 .resume = ibmvnic_resume
4474 static struct vio_driver ibmvnic_driver = {
4475 .id_table = ibmvnic_device_table,
4476 .probe = ibmvnic_probe,
4477 .remove = ibmvnic_remove,
4478 .get_desired_dma = ibmvnic_get_desired_dma,
4479 .name = ibmvnic_driver_name,
4480 .pm = &ibmvnic_pm_ops,
4483 /* module functions */
4484 static int __init ibmvnic_module_init(void)
4486 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4487 IBMVNIC_DRIVER_VERSION);
4489 return vio_register_driver(&ibmvnic_driver);
4492 static void __exit ibmvnic_module_exit(void)
4494 vio_unregister_driver(&ibmvnic_driver);
4497 module_init(ibmvnic_module_init);
4498 module_exit(ibmvnic_module_exit);