1 /**************************************************************************/
3 /* IBM System i and System p Virtual NIC Device Driver */
4 /* Copyright (C) 2014 IBM Corp. */
5 /* Santiago Leon (santi_leon@yahoo.com) */
6 /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
7 /* John Allen (jallen@linux.vnet.ibm.com) */
9 /* This program is free software; you can redistribute it and/or modify */
10 /* it under the terms of the GNU General Public License as published by */
11 /* the Free Software Foundation; either version 2 of the License, or */
12 /* (at your option) any later version. */
14 /* This program is distributed in the hope that it will be useful, */
15 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 /* GNU General Public License for more details. */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program. */
22 /* This module contains the implementation of a virtual ethernet device */
23 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
24 /* option of the RS/6000 Platform Architecture to interface with virtual */
25 /* ethernet NICs that are presented to the partition by the hypervisor. */
27 /* Messages are passed between the VNIC driver and the VNIC server using */
28 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
29 /* issue and receive commands that initiate communication with the server */
30 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
31 /* are used by the driver to notify the server that a packet is */
32 /* ready for transmission or that a buffer has been added to receive a */
33 /* packet. Subsequently, sCRQs are used by the server to notify the */
34 /* driver that a packet transmission has been completed or that a packet */
35 /* has been received and placed in a waiting buffer. */
37 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
38 /* which skbs are DMA mapped and immediately unmapped when the transmit */
39 /* or receive has been completed, the VNIC driver is required to use */
40 /* "long term mapping". This entails that large, continuous DMA mapped */
41 /* buffers are allocated on driver initialization and these buffers are */
42 /* then continuously reused to pass skbs to and from the VNIC server. */
44 /**************************************************************************/
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48 #include <linux/types.h>
49 #include <linux/errno.h>
50 #include <linux/completion.h>
51 #include <linux/ioport.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/kernel.h>
54 #include <linux/netdevice.h>
55 #include <linux/etherdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
60 #include <linux/ethtool.h>
61 #include <linux/proc_fs.h>
62 #include <linux/if_arp.h>
65 #include <linux/ipv6.h>
66 #include <linux/irq.h>
67 #include <linux/kthread.h>
68 #include <linux/seq_file.h>
69 #include <linux/interrupt.h>
70 #include <net/net_namespace.h>
71 #include <asm/hvcall.h>
72 #include <linux/atomic.h>
74 #include <asm/iommu.h>
75 #include <linux/uaccess.h>
76 #include <asm/firmware.h>
77 #include <linux/workqueue.h>
78 #include <linux/if_vlan.h>
79 #include <linux/utsname.h>
83 static const char ibmvnic_driver_name[] = "ibmvnic";
84 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
86 MODULE_AUTHOR("Santiago Leon");
87 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
91 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92 static int ibmvnic_remove(struct vio_dev *);
93 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
94 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
100 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102 static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104 static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106 static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110 static int ibmvnic_poll(struct napi_struct *napi, int data);
111 static void send_map_query(struct ibmvnic_adapter *adapter);
112 static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static int send_request_unmap(struct ibmvnic_adapter *, u8);
114 static int send_login(struct ibmvnic_adapter *adapter);
115 static void send_cap_queries(struct ibmvnic_adapter *adapter);
116 static int init_sub_crqs(struct ibmvnic_adapter *);
117 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118 static int ibmvnic_init(struct ibmvnic_adapter *);
119 static void release_crq_queue(struct ibmvnic_adapter *);
120 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
121 static int init_crq_queue(struct ibmvnic_adapter *adapter);
123 struct ibmvnic_stat {
124 char name[ETH_GSTRING_LEN];
128 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
129 offsetof(struct ibmvnic_statistics, stat))
130 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
132 static const struct ibmvnic_stat ibmvnic_stats[] = {
133 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
134 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
135 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
136 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
137 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
138 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
139 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
140 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
141 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
142 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
143 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
144 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
145 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
146 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
147 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
148 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
149 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
150 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
151 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
152 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
153 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
154 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
157 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
158 unsigned long length, unsigned long *number,
161 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
164 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
171 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
172 struct ibmvnic_long_term_buff *ltb, int size)
174 struct device *dev = &adapter->vdev->dev;
178 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
182 dev_err(dev, "Couldn't alloc long term buffer\n");
185 ltb->map_id = adapter->map_id;
188 init_completion(&adapter->fw_done);
189 rc = send_request_map(adapter, ltb->addr,
190 ltb->size, ltb->map_id);
192 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
195 wait_for_completion(&adapter->fw_done);
197 if (adapter->fw_done_rc) {
198 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
199 adapter->fw_done_rc);
200 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
206 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
207 struct ibmvnic_long_term_buff *ltb)
209 struct device *dev = &adapter->vdev->dev;
214 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
215 adapter->reset_reason != VNIC_RESET_MOBILITY)
216 send_request_unmap(adapter, ltb->map_id);
217 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
220 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
221 struct ibmvnic_long_term_buff *ltb)
225 memset(ltb->buff, 0, ltb->size);
227 init_completion(&adapter->fw_done);
228 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
231 wait_for_completion(&adapter->fw_done);
233 if (adapter->fw_done_rc) {
234 dev_info(&adapter->vdev->dev,
235 "Reset failed, attempting to free and reallocate buffer\n");
236 free_long_term_buff(adapter, ltb);
237 return alloc_long_term_buff(adapter, ltb, ltb->size);
242 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
246 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
248 adapter->rx_pool[i].active = 0;
251 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
252 struct ibmvnic_rx_pool *pool)
254 int count = pool->size - atomic_read(&pool->available);
255 struct device *dev = &adapter->vdev->dev;
256 int buffers_added = 0;
257 unsigned long lpar_rc;
258 union sub_crq sub_crq;
271 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
272 be32_to_cpu(adapter->login_rsp_buf->
275 for (i = 0; i < count; ++i) {
276 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
278 dev_err(dev, "Couldn't replenish rx buff\n");
279 adapter->replenish_no_mem++;
283 index = pool->free_map[pool->next_free];
285 if (pool->rx_buff[index].skb)
286 dev_err(dev, "Inconsistent free_map!\n");
288 /* Copy the skb to the long term mapped DMA buffer */
289 offset = index * pool->buff_size;
290 dst = pool->long_term_buff.buff + offset;
291 memset(dst, 0, pool->buff_size);
292 dma_addr = pool->long_term_buff.addr + offset;
293 pool->rx_buff[index].data = dst;
295 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
296 pool->rx_buff[index].dma = dma_addr;
297 pool->rx_buff[index].skb = skb;
298 pool->rx_buff[index].pool_index = pool->index;
299 pool->rx_buff[index].size = pool->buff_size;
301 memset(&sub_crq, 0, sizeof(sub_crq));
302 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
303 sub_crq.rx_add.correlator =
304 cpu_to_be64((u64)&pool->rx_buff[index]);
305 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
306 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
308 /* The length field of the sCRQ is defined to be 24 bits so the
309 * buffer size needs to be left shifted by a byte before it is
310 * converted to big endian to prevent the last byte from being
313 #ifdef __LITTLE_ENDIAN__
316 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
318 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
320 if (lpar_rc != H_SUCCESS)
324 adapter->replenish_add_buff_success++;
325 pool->next_free = (pool->next_free + 1) % pool->size;
327 atomic_add(buffers_added, &pool->available);
331 dev_info(dev, "replenish pools failure\n");
332 pool->free_map[pool->next_free] = index;
333 pool->rx_buff[index].skb = NULL;
335 dev_kfree_skb_any(skb);
336 adapter->replenish_add_buff_failure++;
337 atomic_add(buffers_added, &pool->available);
339 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
340 /* Disable buffer pool replenishment and report carrier off if
341 * queue is closed or pending failover.
342 * Firmware guarantees that a signal will be sent to the
343 * driver, triggering a reset.
345 deactivate_rx_pools(adapter);
346 netif_carrier_off(adapter->netdev);
350 static void replenish_pools(struct ibmvnic_adapter *adapter)
354 adapter->replenish_task_cycles++;
355 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
357 if (adapter->rx_pool[i].active)
358 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
362 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
364 kfree(adapter->tx_stats_buffers);
365 kfree(adapter->rx_stats_buffers);
366 adapter->tx_stats_buffers = NULL;
367 adapter->rx_stats_buffers = NULL;
370 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
372 adapter->tx_stats_buffers =
373 kcalloc(IBMVNIC_MAX_QUEUES,
374 sizeof(struct ibmvnic_tx_queue_stats),
376 if (!adapter->tx_stats_buffers)
379 adapter->rx_stats_buffers =
380 kcalloc(IBMVNIC_MAX_QUEUES,
381 sizeof(struct ibmvnic_rx_queue_stats),
383 if (!adapter->rx_stats_buffers)
389 static void release_stats_token(struct ibmvnic_adapter *adapter)
391 struct device *dev = &adapter->vdev->dev;
393 if (!adapter->stats_token)
396 dma_unmap_single(dev, adapter->stats_token,
397 sizeof(struct ibmvnic_statistics),
399 adapter->stats_token = 0;
402 static int init_stats_token(struct ibmvnic_adapter *adapter)
404 struct device *dev = &adapter->vdev->dev;
407 stok = dma_map_single(dev, &adapter->stats,
408 sizeof(struct ibmvnic_statistics),
410 if (dma_mapping_error(dev, stok)) {
411 dev_err(dev, "Couldn't map stats buffer\n");
415 adapter->stats_token = stok;
416 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
420 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
422 struct ibmvnic_rx_pool *rx_pool;
427 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
428 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
430 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
431 for (i = 0; i < rx_scrqs; i++) {
432 rx_pool = &adapter->rx_pool[i];
434 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
436 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
437 free_long_term_buff(adapter, &rx_pool->long_term_buff);
438 rx_pool->buff_size = be64_to_cpu(size_array[i]);
439 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
443 rc = reset_long_term_buff(adapter,
444 &rx_pool->long_term_buff);
450 for (j = 0; j < rx_pool->size; j++)
451 rx_pool->free_map[j] = j;
453 memset(rx_pool->rx_buff, 0,
454 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
456 atomic_set(&rx_pool->available, 0);
457 rx_pool->next_alloc = 0;
458 rx_pool->next_free = 0;
465 static void release_rx_pools(struct ibmvnic_adapter *adapter)
467 struct ibmvnic_rx_pool *rx_pool;
470 if (!adapter->rx_pool)
473 for (i = 0; i < adapter->num_active_rx_pools; i++) {
474 rx_pool = &adapter->rx_pool[i];
476 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
478 kfree(rx_pool->free_map);
479 free_long_term_buff(adapter, &rx_pool->long_term_buff);
481 if (!rx_pool->rx_buff)
484 for (j = 0; j < rx_pool->size; j++) {
485 if (rx_pool->rx_buff[j].skb) {
486 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
487 rx_pool->rx_buff[i].skb = NULL;
491 kfree(rx_pool->rx_buff);
494 kfree(adapter->rx_pool);
495 adapter->rx_pool = NULL;
496 adapter->num_active_rx_pools = 0;
499 static int init_rx_pools(struct net_device *netdev)
501 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
502 struct device *dev = &adapter->vdev->dev;
503 struct ibmvnic_rx_pool *rx_pool;
509 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
510 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
511 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
513 adapter->rx_pool = kcalloc(rxadd_subcrqs,
514 sizeof(struct ibmvnic_rx_pool),
516 if (!adapter->rx_pool) {
517 dev_err(dev, "Failed to allocate rx pools\n");
521 adapter->num_active_rx_pools = rxadd_subcrqs;
523 for (i = 0; i < rxadd_subcrqs; i++) {
524 rx_pool = &adapter->rx_pool[i];
526 netdev_dbg(adapter->netdev,
527 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
528 i, adapter->req_rx_add_entries_per_subcrq,
529 be64_to_cpu(size_array[i]));
531 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
533 rx_pool->buff_size = be64_to_cpu(size_array[i]);
536 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
538 if (!rx_pool->free_map) {
539 release_rx_pools(adapter);
543 rx_pool->rx_buff = kcalloc(rx_pool->size,
544 sizeof(struct ibmvnic_rx_buff),
546 if (!rx_pool->rx_buff) {
547 dev_err(dev, "Couldn't alloc rx buffers\n");
548 release_rx_pools(adapter);
552 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
553 rx_pool->size * rx_pool->buff_size)) {
554 release_rx_pools(adapter);
558 for (j = 0; j < rx_pool->size; ++j)
559 rx_pool->free_map[j] = j;
561 atomic_set(&rx_pool->available, 0);
562 rx_pool->next_alloc = 0;
563 rx_pool->next_free = 0;
569 static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
570 struct ibmvnic_tx_pool *tx_pool)
574 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
578 memset(tx_pool->tx_buff, 0,
579 tx_pool->num_buffers *
580 sizeof(struct ibmvnic_tx_buff));
582 for (i = 0; i < tx_pool->num_buffers; i++)
583 tx_pool->free_map[i] = i;
585 tx_pool->consumer_index = 0;
586 tx_pool->producer_index = 0;
591 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
596 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
597 for (i = 0; i < tx_scrqs; i++) {
598 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
601 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
609 static void release_vpd_data(struct ibmvnic_adapter *adapter)
614 kfree(adapter->vpd->buff);
620 static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
621 struct ibmvnic_tx_pool *tx_pool)
623 kfree(tx_pool->tx_buff);
624 kfree(tx_pool->free_map);
625 free_long_term_buff(adapter, &tx_pool->long_term_buff);
628 static void release_tx_pools(struct ibmvnic_adapter *adapter)
632 if (!adapter->tx_pool)
635 for (i = 0; i < adapter->num_active_tx_pools; i++) {
636 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
637 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
640 kfree(adapter->tx_pool);
641 adapter->tx_pool = NULL;
642 kfree(adapter->tso_pool);
643 adapter->tso_pool = NULL;
644 adapter->num_active_tx_pools = 0;
647 static int init_one_tx_pool(struct net_device *netdev,
648 struct ibmvnic_tx_pool *tx_pool,
649 int num_entries, int buf_size)
651 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
654 tx_pool->tx_buff = kcalloc(num_entries,
655 sizeof(struct ibmvnic_tx_buff),
657 if (!tx_pool->tx_buff)
660 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
661 num_entries * buf_size))
664 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
665 if (!tx_pool->free_map)
668 for (i = 0; i < num_entries; i++)
669 tx_pool->free_map[i] = i;
671 tx_pool->consumer_index = 0;
672 tx_pool->producer_index = 0;
673 tx_pool->num_buffers = num_entries;
674 tx_pool->buf_size = buf_size;
679 static int init_tx_pools(struct net_device *netdev)
681 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
685 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
686 adapter->tx_pool = kcalloc(tx_subcrqs,
687 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
688 if (!adapter->tx_pool)
691 adapter->tso_pool = kcalloc(tx_subcrqs,
692 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
693 if (!adapter->tso_pool)
696 adapter->num_active_tx_pools = tx_subcrqs;
698 for (i = 0; i < tx_subcrqs; i++) {
699 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
700 adapter->req_tx_entries_per_subcrq,
701 adapter->req_mtu + VLAN_HLEN);
703 release_tx_pools(adapter);
707 init_one_tx_pool(netdev, &adapter->tso_pool[i],
711 release_tx_pools(adapter);
719 static void release_error_buffers(struct ibmvnic_adapter *adapter)
721 struct device *dev = &adapter->vdev->dev;
722 struct ibmvnic_error_buff *error_buff, *tmp;
725 spin_lock_irqsave(&adapter->error_list_lock, flags);
726 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
727 list_del(&error_buff->list);
728 dma_unmap_single(dev, error_buff->dma, error_buff->len,
730 kfree(error_buff->buff);
733 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
736 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
740 if (adapter->napi_enabled)
743 for (i = 0; i < adapter->req_rx_queues; i++)
744 napi_enable(&adapter->napi[i]);
746 adapter->napi_enabled = true;
749 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
753 if (!adapter->napi_enabled)
756 for (i = 0; i < adapter->req_rx_queues; i++) {
757 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
758 napi_disable(&adapter->napi[i]);
761 adapter->napi_enabled = false;
764 static int init_napi(struct ibmvnic_adapter *adapter)
768 adapter->napi = kcalloc(adapter->req_rx_queues,
769 sizeof(struct napi_struct), GFP_KERNEL);
773 for (i = 0; i < adapter->req_rx_queues; i++) {
774 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
775 netif_napi_add(adapter->netdev, &adapter->napi[i],
776 ibmvnic_poll, NAPI_POLL_WEIGHT);
779 adapter->num_active_rx_napi = adapter->req_rx_queues;
783 static void release_napi(struct ibmvnic_adapter *adapter)
790 for (i = 0; i < adapter->num_active_rx_napi; i++) {
791 if (&adapter->napi[i]) {
792 netdev_dbg(adapter->netdev,
793 "Releasing napi[%d]\n", i);
794 netif_napi_del(&adapter->napi[i]);
798 kfree(adapter->napi);
799 adapter->napi = NULL;
800 adapter->num_active_rx_napi = 0;
801 adapter->napi_enabled = false;
804 static int ibmvnic_login(struct net_device *netdev)
806 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
807 unsigned long timeout = msecs_to_jiffies(30000);
812 if (retry_count > IBMVNIC_MAX_QUEUES) {
813 netdev_warn(netdev, "Login attempts exceeded\n");
817 adapter->init_done_rc = 0;
818 reinit_completion(&adapter->init_done);
819 rc = send_login(adapter);
821 netdev_warn(netdev, "Unable to login\n");
825 if (!wait_for_completion_timeout(&adapter->init_done,
827 netdev_warn(netdev, "Login timed out\n");
831 if (adapter->init_done_rc == PARTIALSUCCESS) {
833 release_sub_crqs(adapter, 1);
835 adapter->init_done_rc = 0;
836 reinit_completion(&adapter->init_done);
837 send_cap_queries(adapter);
838 if (!wait_for_completion_timeout(&adapter->init_done,
841 "Capabilities query timed out\n");
845 rc = init_sub_crqs(adapter);
848 "SCRQ initialization failed\n");
852 rc = init_sub_crq_irqs(adapter);
855 "SCRQ irq initialization failed\n");
858 } else if (adapter->init_done_rc) {
859 netdev_warn(netdev, "Adapter login failed\n");
862 } while (adapter->init_done_rc == PARTIALSUCCESS);
864 /* handle pending MAC address changes after successful login */
865 if (adapter->mac_change_pending) {
866 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
867 adapter->mac_change_pending = false;
873 static void release_login_buffer(struct ibmvnic_adapter *adapter)
875 kfree(adapter->login_buf);
876 adapter->login_buf = NULL;
879 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
881 kfree(adapter->login_rsp_buf);
882 adapter->login_rsp_buf = NULL;
885 static void release_resources(struct ibmvnic_adapter *adapter)
887 release_vpd_data(adapter);
889 release_tx_pools(adapter);
890 release_rx_pools(adapter);
892 release_error_buffers(adapter);
893 release_napi(adapter);
894 release_login_rsp_buffer(adapter);
897 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
899 struct net_device *netdev = adapter->netdev;
900 unsigned long timeout = msecs_to_jiffies(30000);
901 union ibmvnic_crq crq;
905 netdev_dbg(netdev, "setting link state %d\n", link_state);
907 memset(&crq, 0, sizeof(crq));
908 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
909 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
910 crq.logical_link_state.link_state = link_state;
915 reinit_completion(&adapter->init_done);
916 rc = ibmvnic_send_crq(adapter, &crq);
918 netdev_err(netdev, "Failed to set link state\n");
922 if (!wait_for_completion_timeout(&adapter->init_done,
924 netdev_err(netdev, "timeout setting link state\n");
928 if (adapter->init_done_rc == 1) {
929 /* Partuial success, delay and re-send */
938 static int set_real_num_queues(struct net_device *netdev)
940 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
943 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
944 adapter->req_tx_queues, adapter->req_rx_queues);
946 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
948 netdev_err(netdev, "failed to set the number of tx queues\n");
952 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
954 netdev_err(netdev, "failed to set the number of rx queues\n");
959 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
961 struct device *dev = &adapter->vdev->dev;
962 union ibmvnic_crq crq;
966 if (adapter->vpd->buff)
967 len = adapter->vpd->len;
969 init_completion(&adapter->fw_done);
970 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
971 crq.get_vpd_size.cmd = GET_VPD_SIZE;
972 rc = ibmvnic_send_crq(adapter, &crq);
975 wait_for_completion(&adapter->fw_done);
977 if (!adapter->vpd->len)
980 if (!adapter->vpd->buff)
981 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
982 else if (adapter->vpd->len != len)
984 krealloc(adapter->vpd->buff,
985 adapter->vpd->len, GFP_KERNEL);
987 if (!adapter->vpd->buff) {
988 dev_err(dev, "Could allocate VPD buffer\n");
992 adapter->vpd->dma_addr =
993 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
995 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
996 dev_err(dev, "Could not map VPD buffer\n");
997 kfree(adapter->vpd->buff);
998 adapter->vpd->buff = NULL;
1002 reinit_completion(&adapter->fw_done);
1003 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1004 crq.get_vpd.cmd = GET_VPD;
1005 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1006 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1007 rc = ibmvnic_send_crq(adapter, &crq);
1009 kfree(adapter->vpd->buff);
1010 adapter->vpd->buff = NULL;
1013 wait_for_completion(&adapter->fw_done);
1018 static int init_resources(struct ibmvnic_adapter *adapter)
1020 struct net_device *netdev = adapter->netdev;
1023 rc = set_real_num_queues(netdev);
1027 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1031 /* Vital Product Data (VPD) */
1032 rc = ibmvnic_get_vpd(adapter);
1034 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1038 adapter->map_id = 1;
1040 rc = init_napi(adapter);
1044 send_map_query(adapter);
1046 rc = init_rx_pools(netdev);
1050 rc = init_tx_pools(netdev);
1054 static int __ibmvnic_open(struct net_device *netdev)
1056 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1057 enum vnic_state prev_state = adapter->state;
1060 adapter->state = VNIC_OPENING;
1061 replenish_pools(adapter);
1062 ibmvnic_napi_enable(adapter);
1064 /* We're ready to receive frames, enable the sub-crq interrupts and
1065 * set the logical link state to up
1067 for (i = 0; i < adapter->req_rx_queues; i++) {
1068 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1069 if (prev_state == VNIC_CLOSED)
1070 enable_irq(adapter->rx_scrq[i]->irq);
1071 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1074 for (i = 0; i < adapter->req_tx_queues; i++) {
1075 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1076 if (prev_state == VNIC_CLOSED)
1077 enable_irq(adapter->tx_scrq[i]->irq);
1078 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1081 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1083 for (i = 0; i < adapter->req_rx_queues; i++)
1084 napi_disable(&adapter->napi[i]);
1085 release_resources(adapter);
1089 netif_tx_start_all_queues(netdev);
1091 if (prev_state == VNIC_CLOSED) {
1092 for (i = 0; i < adapter->req_rx_queues; i++)
1093 napi_schedule(&adapter->napi[i]);
1096 adapter->state = VNIC_OPEN;
1100 static int ibmvnic_open(struct net_device *netdev)
1102 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1105 /* If device failover is pending, just set device state and return.
1106 * Device operation will be handled by reset routine.
1108 if (adapter->failover_pending) {
1109 adapter->state = VNIC_OPEN;
1113 mutex_lock(&adapter->reset_lock);
1115 if (adapter->state != VNIC_CLOSED) {
1116 rc = ibmvnic_login(netdev);
1118 mutex_unlock(&adapter->reset_lock);
1122 rc = init_resources(adapter);
1124 netdev_err(netdev, "failed to initialize resources\n");
1125 release_resources(adapter);
1126 mutex_unlock(&adapter->reset_lock);
1131 rc = __ibmvnic_open(netdev);
1132 netif_carrier_on(netdev);
1134 mutex_unlock(&adapter->reset_lock);
1139 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1141 struct ibmvnic_rx_pool *rx_pool;
1142 struct ibmvnic_rx_buff *rx_buff;
1147 if (!adapter->rx_pool)
1150 rx_scrqs = adapter->num_active_rx_pools;
1151 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1153 /* Free any remaining skbs in the rx buffer pools */
1154 for (i = 0; i < rx_scrqs; i++) {
1155 rx_pool = &adapter->rx_pool[i];
1156 if (!rx_pool || !rx_pool->rx_buff)
1159 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1160 for (j = 0; j < rx_entries; j++) {
1161 rx_buff = &rx_pool->rx_buff[j];
1162 if (rx_buff && rx_buff->skb) {
1163 dev_kfree_skb_any(rx_buff->skb);
1164 rx_buff->skb = NULL;
1170 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1171 struct ibmvnic_tx_pool *tx_pool)
1173 struct ibmvnic_tx_buff *tx_buff;
1177 if (!tx_pool || !tx_pool->tx_buff)
1180 tx_entries = tx_pool->num_buffers;
1182 for (i = 0; i < tx_entries; i++) {
1183 tx_buff = &tx_pool->tx_buff[i];
1184 if (tx_buff && tx_buff->skb) {
1185 dev_kfree_skb_any(tx_buff->skb);
1186 tx_buff->skb = NULL;
1191 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1196 if (!adapter->tx_pool || !adapter->tso_pool)
1199 tx_scrqs = adapter->num_active_tx_pools;
1201 /* Free any remaining skbs in the tx buffer pools */
1202 for (i = 0; i < tx_scrqs; i++) {
1203 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1204 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1205 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1209 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1211 struct net_device *netdev = adapter->netdev;
1214 if (adapter->tx_scrq) {
1215 for (i = 0; i < adapter->req_tx_queues; i++)
1216 if (adapter->tx_scrq[i]->irq) {
1218 "Disabling tx_scrq[%d] irq\n", i);
1219 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1220 disable_irq(adapter->tx_scrq[i]->irq);
1224 if (adapter->rx_scrq) {
1225 for (i = 0; i < adapter->req_rx_queues; i++) {
1226 if (adapter->rx_scrq[i]->irq) {
1228 "Disabling rx_scrq[%d] irq\n", i);
1229 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1230 disable_irq(adapter->rx_scrq[i]->irq);
1236 static void ibmvnic_cleanup(struct net_device *netdev)
1238 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1240 /* ensure that transmissions are stopped if called by do_reset */
1241 if (adapter->resetting)
1242 netif_tx_disable(netdev);
1244 netif_tx_stop_all_queues(netdev);
1246 ibmvnic_napi_disable(adapter);
1247 ibmvnic_disable_irqs(adapter);
1249 clean_rx_pools(adapter);
1250 clean_tx_pools(adapter);
1253 static int __ibmvnic_close(struct net_device *netdev)
1255 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1258 adapter->state = VNIC_CLOSING;
1259 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1262 adapter->state = VNIC_CLOSED;
1266 static int ibmvnic_close(struct net_device *netdev)
1268 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1271 /* If device failover is pending, just set device state and return.
1272 * Device operation will be handled by reset routine.
1274 if (adapter->failover_pending) {
1275 adapter->state = VNIC_CLOSED;
1279 mutex_lock(&adapter->reset_lock);
1280 rc = __ibmvnic_close(netdev);
1281 ibmvnic_cleanup(netdev);
1282 mutex_unlock(&adapter->reset_lock);
1288 * build_hdr_data - creates L2/L3/L4 header data buffer
1289 * @hdr_field - bitfield determining needed headers
1290 * @skb - socket buffer
1291 * @hdr_len - array of header lengths
1292 * @tot_len - total length of data
1294 * Reads hdr_field to determine which headers are needed by firmware.
1295 * Builds a buffer containing these headers. Saves individual header
1296 * lengths and total buffer length to be used to build descriptors.
1298 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1299 int *hdr_len, u8 *hdr_data)
1304 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1305 hdr_len[0] = sizeof(struct vlan_ethhdr);
1307 hdr_len[0] = sizeof(struct ethhdr);
1309 if (skb->protocol == htons(ETH_P_IP)) {
1310 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1311 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1312 hdr_len[2] = tcp_hdrlen(skb);
1313 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1314 hdr_len[2] = sizeof(struct udphdr);
1315 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1316 hdr_len[1] = sizeof(struct ipv6hdr);
1317 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1318 hdr_len[2] = tcp_hdrlen(skb);
1319 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1320 hdr_len[2] = sizeof(struct udphdr);
1321 } else if (skb->protocol == htons(ETH_P_ARP)) {
1322 hdr_len[1] = arp_hdr_len(skb->dev);
1326 memset(hdr_data, 0, 120);
1327 if ((hdr_field >> 6) & 1) {
1328 hdr = skb_mac_header(skb);
1329 memcpy(hdr_data, hdr, hdr_len[0]);
1333 if ((hdr_field >> 5) & 1) {
1334 hdr = skb_network_header(skb);
1335 memcpy(hdr_data + len, hdr, hdr_len[1]);
1339 if ((hdr_field >> 4) & 1) {
1340 hdr = skb_transport_header(skb);
1341 memcpy(hdr_data + len, hdr, hdr_len[2]);
1348 * create_hdr_descs - create header and header extension descriptors
1349 * @hdr_field - bitfield determining needed headers
1350 * @data - buffer containing header data
1351 * @len - length of data buffer
1352 * @hdr_len - array of individual header lengths
1353 * @scrq_arr - descriptor array
1355 * Creates header and, if needed, header extension descriptors and
1356 * places them in a descriptor array, scrq_arr
1359 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1360 union sub_crq *scrq_arr)
1362 union sub_crq hdr_desc;
1368 while (tmp_len > 0) {
1369 cur = hdr_data + len - tmp_len;
1371 memset(&hdr_desc, 0, sizeof(hdr_desc));
1372 if (cur != hdr_data) {
1373 data = hdr_desc.hdr_ext.data;
1374 tmp = tmp_len > 29 ? 29 : tmp_len;
1375 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1376 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1377 hdr_desc.hdr_ext.len = tmp;
1379 data = hdr_desc.hdr.data;
1380 tmp = tmp_len > 24 ? 24 : tmp_len;
1381 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1382 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1383 hdr_desc.hdr.len = tmp;
1384 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1385 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1386 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1387 hdr_desc.hdr.flag = hdr_field << 1;
1389 memcpy(data, cur, tmp);
1391 *scrq_arr = hdr_desc;
1400 * build_hdr_descs_arr - build a header descriptor array
1401 * @skb - socket buffer
1402 * @num_entries - number of descriptors to be sent
1403 * @subcrq - first TX descriptor
1404 * @hdr_field - bit field determining which headers will be sent
1406 * This function will build a TX descriptor array with applicable
1407 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1410 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1411 int *num_entries, u8 hdr_field)
1413 int hdr_len[3] = {0, 0, 0};
1415 u8 *hdr_data = txbuff->hdr_data;
1417 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1419 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1420 txbuff->indir_arr + 1);
1423 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1424 struct net_device *netdev)
1426 /* For some backing devices, mishandling of small packets
1427 * can result in a loss of connection or TX stall. Device
1428 * architects recommend that no packet should be smaller
1429 * than the minimum MTU value provided to the driver, so
1430 * pad any packets to that length
1432 if (skb->len < netdev->min_mtu)
1433 return skb_put_padto(skb, netdev->min_mtu);
1438 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1440 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1441 int queue_num = skb_get_queue_mapping(skb);
1442 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1443 struct device *dev = &adapter->vdev->dev;
1444 struct ibmvnic_tx_buff *tx_buff = NULL;
1445 struct ibmvnic_sub_crq_queue *tx_scrq;
1446 struct ibmvnic_tx_pool *tx_pool;
1447 unsigned int tx_send_failed = 0;
1448 unsigned int tx_map_failed = 0;
1449 unsigned int tx_dropped = 0;
1450 unsigned int tx_packets = 0;
1451 unsigned int tx_bytes = 0;
1452 dma_addr_t data_dma_addr;
1453 struct netdev_queue *txq;
1454 unsigned long lpar_rc;
1455 union sub_crq tx_crq;
1456 unsigned int offset;
1457 int num_entries = 1;
1464 if (adapter->resetting) {
1465 if (!netif_subqueue_stopped(netdev, skb))
1466 netif_stop_subqueue(netdev, queue_num);
1467 dev_kfree_skb_any(skb);
1475 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1481 if (skb_is_gso(skb))
1482 tx_pool = &adapter->tso_pool[queue_num];
1484 tx_pool = &adapter->tx_pool[queue_num];
1486 tx_scrq = adapter->tx_scrq[queue_num];
1487 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1488 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1489 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1491 index = tx_pool->free_map[tx_pool->consumer_index];
1493 if (index == IBMVNIC_INVALID_MAP) {
1494 dev_kfree_skb_any(skb);
1501 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1503 offset = index * tx_pool->buf_size;
1504 dst = tx_pool->long_term_buff.buff + offset;
1505 memset(dst, 0, tx_pool->buf_size);
1506 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1508 if (skb_shinfo(skb)->nr_frags) {
1512 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1513 cur = skb_headlen(skb);
1515 /* Copy the frags */
1516 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1517 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1520 page_address(skb_frag_page(frag)) +
1521 frag->page_offset, skb_frag_size(frag));
1522 cur += skb_frag_size(frag);
1525 skb_copy_from_linear_data(skb, dst, skb->len);
1528 tx_pool->consumer_index =
1529 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1531 tx_buff = &tx_pool->tx_buff[index];
1533 tx_buff->data_dma[0] = data_dma_addr;
1534 tx_buff->data_len[0] = skb->len;
1535 tx_buff->index = index;
1536 tx_buff->pool_index = queue_num;
1537 tx_buff->last_frag = true;
1539 memset(&tx_crq, 0, sizeof(tx_crq));
1540 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1541 tx_crq.v1.type = IBMVNIC_TX_DESC;
1542 tx_crq.v1.n_crq_elem = 1;
1543 tx_crq.v1.n_sge = 1;
1544 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1546 if (skb_is_gso(skb))
1547 tx_crq.v1.correlator =
1548 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1550 tx_crq.v1.correlator = cpu_to_be32(index);
1551 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1552 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1553 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1555 if (adapter->vlan_header_insertion) {
1556 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1557 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1560 if (skb->protocol == htons(ETH_P_IP)) {
1561 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1562 proto = ip_hdr(skb)->protocol;
1563 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1564 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1565 proto = ipv6_hdr(skb)->nexthdr;
1568 if (proto == IPPROTO_TCP)
1569 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1570 else if (proto == IPPROTO_UDP)
1571 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1573 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1574 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1577 if (skb_is_gso(skb)) {
1578 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1579 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1582 /* determine if l2/3/4 headers are sent to firmware */
1583 if ((*hdrs >> 7) & 1) {
1584 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1585 tx_crq.v1.n_crq_elem = num_entries;
1586 tx_buff->num_entries = num_entries;
1587 tx_buff->indir_arr[0] = tx_crq;
1588 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1589 sizeof(tx_buff->indir_arr),
1591 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1592 dev_kfree_skb_any(skb);
1593 tx_buff->skb = NULL;
1594 if (!firmware_has_feature(FW_FEATURE_CMO))
1595 dev_err(dev, "tx: unable to map descriptor array\n");
1601 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1602 (u64)tx_buff->indir_dma,
1605 tx_buff->num_entries = num_entries;
1606 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1609 if (lpar_rc != H_SUCCESS) {
1610 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1611 dev_kfree_skb_any(skb);
1612 tx_buff->skb = NULL;
1614 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1615 /* Disable TX and report carrier off if queue is closed
1616 * or pending failover.
1617 * Firmware guarantees that a signal will be sent to the
1618 * driver, triggering a reset or some other action.
1620 netif_tx_stop_all_queues(netdev);
1621 netif_carrier_off(netdev);
1630 if (atomic_add_return(num_entries, &tx_scrq->used)
1631 >= adapter->req_tx_entries_per_subcrq) {
1632 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1633 netif_stop_subqueue(netdev, queue_num);
1637 tx_bytes += skb->len;
1638 txq->trans_start = jiffies;
1643 /* roll back consumer index and map array*/
1644 if (tx_pool->consumer_index == 0)
1645 tx_pool->consumer_index =
1646 tx_pool->num_buffers - 1;
1648 tx_pool->consumer_index--;
1649 tx_pool->free_map[tx_pool->consumer_index] = index;
1651 netdev->stats.tx_dropped += tx_dropped;
1652 netdev->stats.tx_bytes += tx_bytes;
1653 netdev->stats.tx_packets += tx_packets;
1654 adapter->tx_send_failed += tx_send_failed;
1655 adapter->tx_map_failed += tx_map_failed;
1656 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1657 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1658 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1663 static void ibmvnic_set_multi(struct net_device *netdev)
1665 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1666 struct netdev_hw_addr *ha;
1667 union ibmvnic_crq crq;
1669 memset(&crq, 0, sizeof(crq));
1670 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1671 crq.request_capability.cmd = REQUEST_CAPABILITY;
1673 if (netdev->flags & IFF_PROMISC) {
1674 if (!adapter->promisc_supported)
1677 if (netdev->flags & IFF_ALLMULTI) {
1678 /* Accept all multicast */
1679 memset(&crq, 0, sizeof(crq));
1680 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1681 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1682 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1683 ibmvnic_send_crq(adapter, &crq);
1684 } else if (netdev_mc_empty(netdev)) {
1685 /* Reject all multicast */
1686 memset(&crq, 0, sizeof(crq));
1687 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1688 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1689 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1690 ibmvnic_send_crq(adapter, &crq);
1692 /* Accept one or more multicast(s) */
1693 netdev_for_each_mc_addr(ha, netdev) {
1694 memset(&crq, 0, sizeof(crq));
1695 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1696 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1697 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1698 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1700 ibmvnic_send_crq(adapter, &crq);
1706 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1708 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1709 struct sockaddr *addr = p;
1710 union ibmvnic_crq crq;
1713 if (!is_valid_ether_addr(addr->sa_data))
1714 return -EADDRNOTAVAIL;
1716 memset(&crq, 0, sizeof(crq));
1717 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1718 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1719 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1721 init_completion(&adapter->fw_done);
1722 rc = ibmvnic_send_crq(adapter, &crq);
1725 wait_for_completion(&adapter->fw_done);
1726 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1727 return adapter->fw_done_rc ? -EIO : 0;
1730 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1732 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1733 struct sockaddr *addr = p;
1736 if (adapter->state == VNIC_PROBED) {
1737 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1738 adapter->mac_change_pending = true;
1742 rc = __ibmvnic_set_mac(netdev, addr);
1748 * do_reset returns zero if we are able to keep processing reset events, or
1749 * non-zero if we hit a fatal error and must halt.
1751 static int do_reset(struct ibmvnic_adapter *adapter,
1752 struct ibmvnic_rwi *rwi, u32 reset_state)
1754 u64 old_num_rx_queues, old_num_tx_queues;
1755 struct net_device *netdev = adapter->netdev;
1758 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1761 netif_carrier_off(netdev);
1762 adapter->reset_reason = rwi->reset_reason;
1764 old_num_rx_queues = adapter->req_rx_queues;
1765 old_num_tx_queues = adapter->req_tx_queues;
1767 ibmvnic_cleanup(netdev);
1769 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1770 adapter->reset_reason != VNIC_RESET_FAILOVER) {
1771 rc = __ibmvnic_close(netdev);
1776 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1777 adapter->wait_for_reset) {
1778 release_resources(adapter);
1779 release_sub_crqs(adapter, 1);
1780 release_crq_queue(adapter);
1783 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1784 /* remove the closed state so when we call open it appears
1785 * we are coming from the probed state.
1787 adapter->state = VNIC_PROBED;
1789 if (adapter->wait_for_reset) {
1790 rc = init_crq_queue(adapter);
1791 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1792 rc = ibmvnic_reenable_crq_queue(adapter);
1793 release_sub_crqs(adapter, 1);
1795 rc = ibmvnic_reset_crq(adapter);
1797 rc = vio_enable_interrupts(adapter->vdev);
1801 netdev_err(adapter->netdev,
1802 "Couldn't initialize crq. rc=%d\n", rc);
1806 rc = ibmvnic_init(adapter);
1808 return IBMVNIC_INIT_FAILED;
1810 /* If the adapter was in PROBE state prior to the reset,
1813 if (reset_state == VNIC_PROBED)
1816 rc = ibmvnic_login(netdev);
1818 adapter->state = VNIC_PROBED;
1822 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1823 adapter->wait_for_reset) {
1824 rc = init_resources(adapter);
1827 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1828 adapter->req_tx_queues != old_num_tx_queues) {
1829 adapter->map_id = 1;
1830 release_rx_pools(adapter);
1831 release_tx_pools(adapter);
1832 init_rx_pools(netdev);
1833 init_tx_pools(netdev);
1835 release_napi(adapter);
1838 rc = reset_tx_pools(adapter);
1842 rc = reset_rx_pools(adapter);
1846 ibmvnic_disable_irqs(adapter);
1848 adapter->state = VNIC_CLOSED;
1850 if (reset_state == VNIC_CLOSED)
1853 rc = __ibmvnic_open(netdev);
1855 if (list_empty(&adapter->rwi_list))
1856 adapter->state = VNIC_CLOSED;
1858 adapter->state = reset_state;
1864 for (i = 0; i < adapter->req_rx_queues; i++)
1865 napi_schedule(&adapter->napi[i]);
1867 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1868 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1869 netdev_notify_peers(netdev);
1871 netif_carrier_on(netdev);
1876 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1878 struct ibmvnic_rwi *rwi;
1880 mutex_lock(&adapter->rwi_lock);
1882 if (!list_empty(&adapter->rwi_list)) {
1883 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1885 list_del(&rwi->list);
1890 mutex_unlock(&adapter->rwi_lock);
1894 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1896 struct ibmvnic_rwi *rwi;
1898 rwi = get_next_rwi(adapter);
1901 rwi = get_next_rwi(adapter);
1905 static void __ibmvnic_reset(struct work_struct *work)
1907 struct ibmvnic_rwi *rwi;
1908 struct ibmvnic_adapter *adapter;
1909 struct net_device *netdev;
1913 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1914 netdev = adapter->netdev;
1916 mutex_lock(&adapter->reset_lock);
1917 adapter->resetting = true;
1918 reset_state = adapter->state;
1920 rwi = get_next_rwi(adapter);
1922 rc = do_reset(adapter, rwi, reset_state);
1924 if (rc && rc != IBMVNIC_INIT_FAILED)
1927 rwi = get_next_rwi(adapter);
1930 if (adapter->wait_for_reset) {
1931 adapter->wait_for_reset = false;
1932 adapter->reset_done_rc = rc;
1933 complete(&adapter->reset_done);
1937 netdev_dbg(adapter->netdev, "Reset failed\n");
1938 free_all_rwi(adapter);
1939 mutex_unlock(&adapter->reset_lock);
1943 adapter->resetting = false;
1944 mutex_unlock(&adapter->reset_lock);
1947 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
1948 enum ibmvnic_reset_reason reason)
1950 struct ibmvnic_rwi *rwi, *tmp;
1951 struct net_device *netdev = adapter->netdev;
1952 struct list_head *entry;
1955 if (adapter->state == VNIC_REMOVING ||
1956 adapter->state == VNIC_REMOVED ||
1957 adapter->failover_pending) {
1959 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
1963 if (adapter->state == VNIC_PROBING) {
1964 netdev_warn(netdev, "Adapter reset during probe\n");
1965 ret = adapter->init_done_rc = EAGAIN;
1969 mutex_lock(&adapter->rwi_lock);
1971 list_for_each(entry, &adapter->rwi_list) {
1972 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1973 if (tmp->reset_reason == reason) {
1974 netdev_dbg(netdev, "Skipping matching reset\n");
1975 mutex_unlock(&adapter->rwi_lock);
1981 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1983 mutex_unlock(&adapter->rwi_lock);
1984 ibmvnic_close(netdev);
1989 rwi->reset_reason = reason;
1990 list_add_tail(&rwi->list, &adapter->rwi_list);
1991 mutex_unlock(&adapter->rwi_lock);
1993 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1994 schedule_work(&adapter->ibmvnic_reset);
1998 if (adapter->wait_for_reset)
1999 adapter->wait_for_reset = false;
2003 static void ibmvnic_tx_timeout(struct net_device *dev)
2005 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2007 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2010 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2011 struct ibmvnic_rx_buff *rx_buff)
2013 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2015 rx_buff->skb = NULL;
2017 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2018 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2020 atomic_dec(&pool->available);
2023 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2025 struct net_device *netdev = napi->dev;
2026 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2027 int scrq_num = (int)(napi - adapter->napi);
2028 int frames_processed = 0;
2031 while (frames_processed < budget) {
2032 struct sk_buff *skb;
2033 struct ibmvnic_rx_buff *rx_buff;
2034 union sub_crq *next;
2039 if (unlikely(adapter->resetting &&
2040 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2041 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2042 napi_complete_done(napi, frames_processed);
2043 return frames_processed;
2046 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2048 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2050 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2051 rx_comp.correlator);
2052 /* do error checking */
2053 if (next->rx_comp.rc) {
2054 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2055 be16_to_cpu(next->rx_comp.rc));
2056 /* free the entry */
2057 next->rx_comp.first = 0;
2058 dev_kfree_skb_any(rx_buff->skb);
2059 remove_buff_from_pool(adapter, rx_buff);
2061 } else if (!rx_buff->skb) {
2062 /* free the entry */
2063 next->rx_comp.first = 0;
2064 remove_buff_from_pool(adapter, rx_buff);
2068 length = be32_to_cpu(next->rx_comp.len);
2069 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2070 flags = next->rx_comp.flags;
2072 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2075 /* VLAN Header has been stripped by the system firmware and
2076 * needs to be inserted by the driver
2078 if (adapter->rx_vlan_header_insertion &&
2079 (flags & IBMVNIC_VLAN_STRIPPED))
2080 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2081 ntohs(next->rx_comp.vlan_tci));
2083 /* free the entry */
2084 next->rx_comp.first = 0;
2085 remove_buff_from_pool(adapter, rx_buff);
2087 skb_put(skb, length);
2088 skb->protocol = eth_type_trans(skb, netdev);
2089 skb_record_rx_queue(skb, scrq_num);
2091 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2092 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2093 skb->ip_summed = CHECKSUM_UNNECESSARY;
2097 napi_gro_receive(napi, skb); /* send it up */
2098 netdev->stats.rx_packets++;
2099 netdev->stats.rx_bytes += length;
2100 adapter->rx_stats_buffers[scrq_num].packets++;
2101 adapter->rx_stats_buffers[scrq_num].bytes += length;
2105 if (adapter->state != VNIC_CLOSING)
2106 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2108 if (frames_processed < budget) {
2109 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2110 napi_complete_done(napi, frames_processed);
2111 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2112 napi_reschedule(napi)) {
2113 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2117 return frames_processed;
2120 #ifdef CONFIG_NET_POLL_CONTROLLER
2121 static void ibmvnic_netpoll_controller(struct net_device *dev)
2123 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2126 replenish_pools(netdev_priv(dev));
2127 for (i = 0; i < adapter->req_rx_queues; i++)
2128 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2129 adapter->rx_scrq[i]);
2133 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2137 adapter->fallback.mtu = adapter->req_mtu;
2138 adapter->fallback.rx_queues = adapter->req_rx_queues;
2139 adapter->fallback.tx_queues = adapter->req_tx_queues;
2140 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2141 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2143 init_completion(&adapter->reset_done);
2144 adapter->wait_for_reset = true;
2145 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2148 wait_for_completion(&adapter->reset_done);
2151 if (adapter->reset_done_rc) {
2153 adapter->desired.mtu = adapter->fallback.mtu;
2154 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2155 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2156 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2157 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2159 init_completion(&adapter->reset_done);
2160 adapter->wait_for_reset = true;
2161 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2164 wait_for_completion(&adapter->reset_done);
2166 adapter->wait_for_reset = false;
2171 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2173 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2175 adapter->desired.mtu = new_mtu + ETH_HLEN;
2177 return wait_for_reset(adapter);
2180 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2181 struct net_device *dev,
2182 netdev_features_t features)
2184 /* Some backing hardware adapters can not
2185 * handle packets with a MSS less than 224
2186 * or with only one segment.
2188 if (skb_is_gso(skb)) {
2189 if (skb_shinfo(skb)->gso_size < 224 ||
2190 skb_shinfo(skb)->gso_segs == 1)
2191 features &= ~NETIF_F_GSO_MASK;
2197 static const struct net_device_ops ibmvnic_netdev_ops = {
2198 .ndo_open = ibmvnic_open,
2199 .ndo_stop = ibmvnic_close,
2200 .ndo_start_xmit = ibmvnic_xmit,
2201 .ndo_set_rx_mode = ibmvnic_set_multi,
2202 .ndo_set_mac_address = ibmvnic_set_mac,
2203 .ndo_validate_addr = eth_validate_addr,
2204 .ndo_tx_timeout = ibmvnic_tx_timeout,
2205 #ifdef CONFIG_NET_POLL_CONTROLLER
2206 .ndo_poll_controller = ibmvnic_netpoll_controller,
2208 .ndo_change_mtu = ibmvnic_change_mtu,
2209 .ndo_features_check = ibmvnic_features_check,
2212 /* ethtool functions */
2214 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2215 struct ethtool_link_ksettings *cmd)
2217 u32 supported, advertising;
2219 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2221 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2223 cmd->base.speed = SPEED_1000;
2224 cmd->base.duplex = DUPLEX_FULL;
2225 cmd->base.port = PORT_FIBRE;
2226 cmd->base.phy_address = 0;
2227 cmd->base.autoneg = AUTONEG_ENABLE;
2229 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2231 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2237 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2238 struct ethtool_drvinfo *info)
2240 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2242 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2243 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2244 strlcpy(info->fw_version, adapter->fw_version,
2245 sizeof(info->fw_version));
2248 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2250 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2252 return adapter->msg_enable;
2255 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2257 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2259 adapter->msg_enable = data;
2262 static u32 ibmvnic_get_link(struct net_device *netdev)
2264 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2266 /* Don't need to send a query because we request a logical link up at
2267 * init and then we wait for link state indications
2269 return adapter->logical_link_state;
2272 static void ibmvnic_get_ringparam(struct net_device *netdev,
2273 struct ethtool_ringparam *ring)
2275 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2277 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2278 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2279 ring->rx_mini_max_pending = 0;
2280 ring->rx_jumbo_max_pending = 0;
2281 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2282 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2283 ring->rx_mini_pending = 0;
2284 ring->rx_jumbo_pending = 0;
2287 static int ibmvnic_set_ringparam(struct net_device *netdev,
2288 struct ethtool_ringparam *ring)
2290 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2292 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2293 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2294 netdev_err(netdev, "Invalid request.\n");
2295 netdev_err(netdev, "Max tx buffers = %llu\n",
2296 adapter->max_rx_add_entries_per_subcrq);
2297 netdev_err(netdev, "Max rx buffers = %llu\n",
2298 adapter->max_tx_entries_per_subcrq);
2302 adapter->desired.rx_entries = ring->rx_pending;
2303 adapter->desired.tx_entries = ring->tx_pending;
2305 return wait_for_reset(adapter);
2308 static void ibmvnic_get_channels(struct net_device *netdev,
2309 struct ethtool_channels *channels)
2311 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2313 channels->max_rx = adapter->max_rx_queues;
2314 channels->max_tx = adapter->max_tx_queues;
2315 channels->max_other = 0;
2316 channels->max_combined = 0;
2317 channels->rx_count = adapter->req_rx_queues;
2318 channels->tx_count = adapter->req_tx_queues;
2319 channels->other_count = 0;
2320 channels->combined_count = 0;
2323 static int ibmvnic_set_channels(struct net_device *netdev,
2324 struct ethtool_channels *channels)
2326 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2328 adapter->desired.rx_queues = channels->rx_count;
2329 adapter->desired.tx_queues = channels->tx_count;
2331 return wait_for_reset(adapter);
2334 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2336 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2339 if (stringset != ETH_SS_STATS)
2342 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2343 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2345 for (i = 0; i < adapter->req_tx_queues; i++) {
2346 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2347 data += ETH_GSTRING_LEN;
2349 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2350 data += ETH_GSTRING_LEN;
2352 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2353 data += ETH_GSTRING_LEN;
2356 for (i = 0; i < adapter->req_rx_queues; i++) {
2357 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2358 data += ETH_GSTRING_LEN;
2360 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2361 data += ETH_GSTRING_LEN;
2363 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2364 data += ETH_GSTRING_LEN;
2368 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2370 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2374 return ARRAY_SIZE(ibmvnic_stats) +
2375 adapter->req_tx_queues * NUM_TX_STATS +
2376 adapter->req_rx_queues * NUM_RX_STATS;
2382 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2383 struct ethtool_stats *stats, u64 *data)
2385 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2386 union ibmvnic_crq crq;
2390 memset(&crq, 0, sizeof(crq));
2391 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2392 crq.request_statistics.cmd = REQUEST_STATISTICS;
2393 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2394 crq.request_statistics.len =
2395 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2397 /* Wait for data to be written */
2398 init_completion(&adapter->stats_done);
2399 rc = ibmvnic_send_crq(adapter, &crq);
2402 wait_for_completion(&adapter->stats_done);
2404 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2405 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2406 ibmvnic_stats[i].offset));
2408 for (j = 0; j < adapter->req_tx_queues; j++) {
2409 data[i] = adapter->tx_stats_buffers[j].packets;
2411 data[i] = adapter->tx_stats_buffers[j].bytes;
2413 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2417 for (j = 0; j < adapter->req_rx_queues; j++) {
2418 data[i] = adapter->rx_stats_buffers[j].packets;
2420 data[i] = adapter->rx_stats_buffers[j].bytes;
2422 data[i] = adapter->rx_stats_buffers[j].interrupts;
2427 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2428 .get_drvinfo = ibmvnic_get_drvinfo,
2429 .get_msglevel = ibmvnic_get_msglevel,
2430 .set_msglevel = ibmvnic_set_msglevel,
2431 .get_link = ibmvnic_get_link,
2432 .get_ringparam = ibmvnic_get_ringparam,
2433 .set_ringparam = ibmvnic_set_ringparam,
2434 .get_channels = ibmvnic_get_channels,
2435 .set_channels = ibmvnic_set_channels,
2436 .get_strings = ibmvnic_get_strings,
2437 .get_sset_count = ibmvnic_get_sset_count,
2438 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2439 .get_link_ksettings = ibmvnic_get_link_ksettings,
2442 /* Routines for managing CRQs/sCRQs */
2444 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2445 struct ibmvnic_sub_crq_queue *scrq)
2450 free_irq(scrq->irq, scrq);
2451 irq_dispose_mapping(scrq->irq);
2455 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2456 atomic_set(&scrq->used, 0);
2459 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2460 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2464 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2468 for (i = 0; i < adapter->req_tx_queues; i++) {
2469 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2470 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2475 for (i = 0; i < adapter->req_rx_queues; i++) {
2476 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2477 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2485 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2486 struct ibmvnic_sub_crq_queue *scrq,
2489 struct device *dev = &adapter->vdev->dev;
2492 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2495 /* Close the sub-crqs */
2497 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2498 adapter->vdev->unit_address,
2500 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2503 netdev_err(adapter->netdev,
2504 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2509 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2511 free_pages((unsigned long)scrq->msgs, 2);
2515 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2518 struct device *dev = &adapter->vdev->dev;
2519 struct ibmvnic_sub_crq_queue *scrq;
2522 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2527 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2529 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2530 goto zero_page_failed;
2533 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2535 if (dma_mapping_error(dev, scrq->msg_token)) {
2536 dev_warn(dev, "Couldn't map crq queue messages page\n");
2540 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2541 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2543 if (rc == H_RESOURCE)
2544 rc = ibmvnic_reset_crq(adapter);
2546 if (rc == H_CLOSED) {
2547 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2549 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2553 scrq->adapter = adapter;
2554 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2555 spin_lock_init(&scrq->lock);
2557 netdev_dbg(adapter->netdev,
2558 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2559 scrq->crq_num, scrq->hw_irq, scrq->irq);
2564 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2567 free_pages((unsigned long)scrq->msgs, 2);
2574 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2578 if (adapter->tx_scrq) {
2579 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2580 if (!adapter->tx_scrq[i])
2583 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2585 if (adapter->tx_scrq[i]->irq) {
2586 free_irq(adapter->tx_scrq[i]->irq,
2587 adapter->tx_scrq[i]);
2588 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2589 adapter->tx_scrq[i]->irq = 0;
2592 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2596 kfree(adapter->tx_scrq);
2597 adapter->tx_scrq = NULL;
2598 adapter->num_active_tx_scrqs = 0;
2601 if (adapter->rx_scrq) {
2602 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2603 if (!adapter->rx_scrq[i])
2606 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2608 if (adapter->rx_scrq[i]->irq) {
2609 free_irq(adapter->rx_scrq[i]->irq,
2610 adapter->rx_scrq[i]);
2611 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2612 adapter->rx_scrq[i]->irq = 0;
2615 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2619 kfree(adapter->rx_scrq);
2620 adapter->rx_scrq = NULL;
2621 adapter->num_active_rx_scrqs = 0;
2625 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2626 struct ibmvnic_sub_crq_queue *scrq)
2628 struct device *dev = &adapter->vdev->dev;
2631 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2632 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2634 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2639 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2640 struct ibmvnic_sub_crq_queue *scrq)
2642 struct device *dev = &adapter->vdev->dev;
2646 if (scrq->hw_irq > 0x100000000ULL) {
2647 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2651 val = (0xff000000) | scrq->hw_irq;
2652 rc = plpar_hcall_norets(H_EOI, val);
2654 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2657 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2658 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2660 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2665 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2666 struct ibmvnic_sub_crq_queue *scrq)
2668 struct device *dev = &adapter->vdev->dev;
2669 struct ibmvnic_tx_pool *tx_pool;
2670 struct ibmvnic_tx_buff *txbuff;
2671 union sub_crq *next;
2677 while (pending_scrq(adapter, scrq)) {
2678 unsigned int pool = scrq->pool_index;
2679 int num_entries = 0;
2681 next = ibmvnic_next_scrq(adapter, scrq);
2682 for (i = 0; i < next->tx_comp.num_comps; i++) {
2683 if (next->tx_comp.rcs[i]) {
2684 dev_err(dev, "tx error %x\n",
2685 next->tx_comp.rcs[i]);
2688 index = be32_to_cpu(next->tx_comp.correlators[i]);
2689 if (index & IBMVNIC_TSO_POOL_MASK) {
2690 tx_pool = &adapter->tso_pool[pool];
2691 index &= ~IBMVNIC_TSO_POOL_MASK;
2693 tx_pool = &adapter->tx_pool[pool];
2696 txbuff = &tx_pool->tx_buff[index];
2698 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2699 if (!txbuff->data_dma[j])
2702 txbuff->data_dma[j] = 0;
2704 /* if sub_crq was sent indirectly */
2705 first = &txbuff->indir_arr[0].generic.first;
2706 if (*first == IBMVNIC_CRQ_CMD) {
2707 dma_unmap_single(dev, txbuff->indir_dma,
2708 sizeof(txbuff->indir_arr),
2713 if (txbuff->last_frag) {
2714 dev_kfree_skb_any(txbuff->skb);
2718 num_entries += txbuff->num_entries;
2720 tx_pool->free_map[tx_pool->producer_index] = index;
2721 tx_pool->producer_index =
2722 (tx_pool->producer_index + 1) %
2723 tx_pool->num_buffers;
2725 /* remove tx_comp scrq*/
2726 next->tx_comp.first = 0;
2728 if (atomic_sub_return(num_entries, &scrq->used) <=
2729 (adapter->req_tx_entries_per_subcrq / 2) &&
2730 __netif_subqueue_stopped(adapter->netdev,
2731 scrq->pool_index)) {
2732 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2733 netdev_dbg(adapter->netdev, "Started queue %d\n",
2738 enable_scrq_irq(adapter, scrq);
2740 if (pending_scrq(adapter, scrq)) {
2741 disable_scrq_irq(adapter, scrq);
2748 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2750 struct ibmvnic_sub_crq_queue *scrq = instance;
2751 struct ibmvnic_adapter *adapter = scrq->adapter;
2753 disable_scrq_irq(adapter, scrq);
2754 ibmvnic_complete_tx(adapter, scrq);
2759 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2761 struct ibmvnic_sub_crq_queue *scrq = instance;
2762 struct ibmvnic_adapter *adapter = scrq->adapter;
2764 /* When booting a kdump kernel we can hit pending interrupts
2765 * prior to completing driver initialization.
2767 if (unlikely(adapter->state != VNIC_OPEN))
2770 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2772 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2773 disable_scrq_irq(adapter, scrq);
2774 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2780 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2782 struct device *dev = &adapter->vdev->dev;
2783 struct ibmvnic_sub_crq_queue *scrq;
2787 for (i = 0; i < adapter->req_tx_queues; i++) {
2788 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2790 scrq = adapter->tx_scrq[i];
2791 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2795 dev_err(dev, "Error mapping irq\n");
2796 goto req_tx_irq_failed;
2799 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2800 0, "ibmvnic_tx", scrq);
2803 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2805 irq_dispose_mapping(scrq->irq);
2806 goto req_tx_irq_failed;
2810 for (i = 0; i < adapter->req_rx_queues; i++) {
2811 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2813 scrq = adapter->rx_scrq[i];
2814 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2817 dev_err(dev, "Error mapping irq\n");
2818 goto req_rx_irq_failed;
2820 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2821 0, "ibmvnic_rx", scrq);
2823 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2825 irq_dispose_mapping(scrq->irq);
2826 goto req_rx_irq_failed;
2832 for (j = 0; j < i; j++) {
2833 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2834 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2836 i = adapter->req_tx_queues;
2838 for (j = 0; j < i; j++) {
2839 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2840 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2842 release_sub_crqs(adapter, 1);
2846 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2848 struct device *dev = &adapter->vdev->dev;
2849 struct ibmvnic_sub_crq_queue **allqueues;
2850 int registered_queues = 0;
2855 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2857 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2861 for (i = 0; i < total_queues; i++) {
2862 allqueues[i] = init_sub_crq_queue(adapter);
2863 if (!allqueues[i]) {
2864 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2867 registered_queues++;
2870 /* Make sure we were able to register the minimum number of queues */
2871 if (registered_queues <
2872 adapter->min_tx_queues + adapter->min_rx_queues) {
2873 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2877 /* Distribute the failed allocated queues*/
2878 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2879 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2882 if (adapter->req_rx_queues > adapter->min_rx_queues)
2883 adapter->req_rx_queues--;
2888 if (adapter->req_tx_queues > adapter->min_tx_queues)
2889 adapter->req_tx_queues--;
2896 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2897 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2898 if (!adapter->tx_scrq)
2901 for (i = 0; i < adapter->req_tx_queues; i++) {
2902 adapter->tx_scrq[i] = allqueues[i];
2903 adapter->tx_scrq[i]->pool_index = i;
2904 adapter->num_active_tx_scrqs++;
2907 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2908 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2909 if (!adapter->rx_scrq)
2912 for (i = 0; i < adapter->req_rx_queues; i++) {
2913 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2914 adapter->rx_scrq[i]->scrq_num = i;
2915 adapter->num_active_rx_scrqs++;
2922 kfree(adapter->tx_scrq);
2923 adapter->tx_scrq = NULL;
2925 for (i = 0; i < registered_queues; i++)
2926 release_sub_crq_queue(adapter, allqueues[i], 1);
2931 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2933 struct device *dev = &adapter->vdev->dev;
2934 union ibmvnic_crq crq;
2938 /* Sub-CRQ entries are 32 byte long */
2939 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2941 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2942 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2943 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2947 if (adapter->desired.mtu)
2948 adapter->req_mtu = adapter->desired.mtu;
2950 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2952 if (!adapter->desired.tx_entries)
2953 adapter->desired.tx_entries =
2954 adapter->max_tx_entries_per_subcrq;
2955 if (!adapter->desired.rx_entries)
2956 adapter->desired.rx_entries =
2957 adapter->max_rx_add_entries_per_subcrq;
2959 max_entries = IBMVNIC_MAX_LTB_SIZE /
2960 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2962 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2963 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2964 adapter->desired.tx_entries = max_entries;
2967 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2968 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2969 adapter->desired.rx_entries = max_entries;
2972 if (adapter->desired.tx_entries)
2973 adapter->req_tx_entries_per_subcrq =
2974 adapter->desired.tx_entries;
2976 adapter->req_tx_entries_per_subcrq =
2977 adapter->max_tx_entries_per_subcrq;
2979 if (adapter->desired.rx_entries)
2980 adapter->req_rx_add_entries_per_subcrq =
2981 adapter->desired.rx_entries;
2983 adapter->req_rx_add_entries_per_subcrq =
2984 adapter->max_rx_add_entries_per_subcrq;
2986 if (adapter->desired.tx_queues)
2987 adapter->req_tx_queues =
2988 adapter->desired.tx_queues;
2990 adapter->req_tx_queues =
2991 adapter->opt_tx_comp_sub_queues;
2993 if (adapter->desired.rx_queues)
2994 adapter->req_rx_queues =
2995 adapter->desired.rx_queues;
2997 adapter->req_rx_queues =
2998 adapter->opt_rx_comp_queues;
3000 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3003 memset(&crq, 0, sizeof(crq));
3004 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3005 crq.request_capability.cmd = REQUEST_CAPABILITY;
3007 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3008 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3009 atomic_inc(&adapter->running_cap_crqs);
3010 ibmvnic_send_crq(adapter, &crq);
3012 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3013 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3014 atomic_inc(&adapter->running_cap_crqs);
3015 ibmvnic_send_crq(adapter, &crq);
3017 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3018 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3019 atomic_inc(&adapter->running_cap_crqs);
3020 ibmvnic_send_crq(adapter, &crq);
3022 crq.request_capability.capability =
3023 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3024 crq.request_capability.number =
3025 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3026 atomic_inc(&adapter->running_cap_crqs);
3027 ibmvnic_send_crq(adapter, &crq);
3029 crq.request_capability.capability =
3030 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3031 crq.request_capability.number =
3032 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3033 atomic_inc(&adapter->running_cap_crqs);
3034 ibmvnic_send_crq(adapter, &crq);
3036 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3037 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3038 atomic_inc(&adapter->running_cap_crqs);
3039 ibmvnic_send_crq(adapter, &crq);
3041 if (adapter->netdev->flags & IFF_PROMISC) {
3042 if (adapter->promisc_supported) {
3043 crq.request_capability.capability =
3044 cpu_to_be16(PROMISC_REQUESTED);
3045 crq.request_capability.number = cpu_to_be64(1);
3046 atomic_inc(&adapter->running_cap_crqs);
3047 ibmvnic_send_crq(adapter, &crq);
3050 crq.request_capability.capability =
3051 cpu_to_be16(PROMISC_REQUESTED);
3052 crq.request_capability.number = cpu_to_be64(0);
3053 atomic_inc(&adapter->running_cap_crqs);
3054 ibmvnic_send_crq(adapter, &crq);
3058 static int pending_scrq(struct ibmvnic_adapter *adapter,
3059 struct ibmvnic_sub_crq_queue *scrq)
3061 union sub_crq *entry = &scrq->msgs[scrq->cur];
3063 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3069 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3070 struct ibmvnic_sub_crq_queue *scrq)
3072 union sub_crq *entry;
3073 unsigned long flags;
3075 spin_lock_irqsave(&scrq->lock, flags);
3076 entry = &scrq->msgs[scrq->cur];
3077 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3078 if (++scrq->cur == scrq->size)
3083 spin_unlock_irqrestore(&scrq->lock, flags);
3088 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3090 struct ibmvnic_crq_queue *queue = &adapter->crq;
3091 union ibmvnic_crq *crq;
3093 crq = &queue->msgs[queue->cur];
3094 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3095 if (++queue->cur == queue->size)
3104 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3105 union sub_crq *sub_crq)
3107 unsigned int ua = adapter->vdev->unit_address;
3108 struct device *dev = &adapter->vdev->dev;
3109 u64 *u64_crq = (u64 *)sub_crq;
3112 netdev_dbg(adapter->netdev,
3113 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3114 (unsigned long int)cpu_to_be64(remote_handle),
3115 (unsigned long int)cpu_to_be64(u64_crq[0]),
3116 (unsigned long int)cpu_to_be64(u64_crq[1]),
3117 (unsigned long int)cpu_to_be64(u64_crq[2]),
3118 (unsigned long int)cpu_to_be64(u64_crq[3]));
3120 /* Make sure the hypervisor sees the complete request */
3123 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3124 cpu_to_be64(remote_handle),
3125 cpu_to_be64(u64_crq[0]),
3126 cpu_to_be64(u64_crq[1]),
3127 cpu_to_be64(u64_crq[2]),
3128 cpu_to_be64(u64_crq[3]));
3132 dev_warn(dev, "CRQ Queue closed\n");
3133 dev_err(dev, "Send error (rc=%d)\n", rc);
3139 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3140 u64 remote_handle, u64 ioba, u64 num_entries)
3142 unsigned int ua = adapter->vdev->unit_address;
3143 struct device *dev = &adapter->vdev->dev;
3146 /* Make sure the hypervisor sees the complete request */
3148 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3149 cpu_to_be64(remote_handle),
3154 dev_warn(dev, "CRQ Queue closed\n");
3155 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3161 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3162 union ibmvnic_crq *crq)
3164 unsigned int ua = adapter->vdev->unit_address;
3165 struct device *dev = &adapter->vdev->dev;
3166 u64 *u64_crq = (u64 *)crq;
3169 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3170 (unsigned long int)cpu_to_be64(u64_crq[0]),
3171 (unsigned long int)cpu_to_be64(u64_crq[1]));
3173 if (!adapter->crq.active &&
3174 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3175 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3179 /* Make sure the hypervisor sees the complete request */
3182 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3183 cpu_to_be64(u64_crq[0]),
3184 cpu_to_be64(u64_crq[1]));
3187 if (rc == H_CLOSED) {
3188 dev_warn(dev, "CRQ Queue closed\n");
3189 if (adapter->resetting)
3190 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3193 dev_warn(dev, "Send error (rc=%d)\n", rc);
3199 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3201 union ibmvnic_crq crq;
3203 memset(&crq, 0, sizeof(crq));
3204 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3205 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3206 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3208 return ibmvnic_send_crq(adapter, &crq);
3211 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3213 union ibmvnic_crq crq;
3215 memset(&crq, 0, sizeof(crq));
3216 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3217 crq.version_exchange.cmd = VERSION_EXCHANGE;
3218 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3220 return ibmvnic_send_crq(adapter, &crq);
3223 struct vnic_login_client_data {
3229 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3233 /* Calculate the amount of buffer space needed for the
3234 * vnic client data in the login buffer. There are four entries,
3235 * OS name, LPAR name, device name, and a null last entry.
3237 len = 4 * sizeof(struct vnic_login_client_data);
3238 len += 6; /* "Linux" plus NULL */
3239 len += strlen(utsname()->nodename) + 1;
3240 len += strlen(adapter->netdev->name) + 1;
3245 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3246 struct vnic_login_client_data *vlcd)
3248 const char *os_name = "Linux";
3251 /* Type 1 - LPAR OS */
3253 len = strlen(os_name) + 1;
3254 vlcd->len = cpu_to_be16(len);
3255 strncpy(vlcd->name, os_name, len);
3256 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3258 /* Type 2 - LPAR name */
3260 len = strlen(utsname()->nodename) + 1;
3261 vlcd->len = cpu_to_be16(len);
3262 strncpy(vlcd->name, utsname()->nodename, len);
3263 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3265 /* Type 3 - device name */
3267 len = strlen(adapter->netdev->name) + 1;
3268 vlcd->len = cpu_to_be16(len);
3269 strncpy(vlcd->name, adapter->netdev->name, len);
3272 static int send_login(struct ibmvnic_adapter *adapter)
3274 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3275 struct ibmvnic_login_buffer *login_buffer;
3276 struct device *dev = &adapter->vdev->dev;
3277 dma_addr_t rsp_buffer_token;
3278 dma_addr_t buffer_token;
3279 size_t rsp_buffer_size;
3280 union ibmvnic_crq crq;
3284 int client_data_len;
3285 struct vnic_login_client_data *vlcd;
3288 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3289 netdev_err(adapter->netdev,
3290 "RX or TX queues are not allocated, device login failed\n");
3294 release_login_rsp_buffer(adapter);
3295 client_data_len = vnic_client_data_len(adapter);
3298 sizeof(struct ibmvnic_login_buffer) +
3299 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3302 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3304 goto buf_alloc_failed;
3306 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3308 if (dma_mapping_error(dev, buffer_token)) {
3309 dev_err(dev, "Couldn't map login buffer\n");
3310 goto buf_map_failed;
3313 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3314 sizeof(u64) * adapter->req_tx_queues +
3315 sizeof(u64) * adapter->req_rx_queues +
3316 sizeof(u64) * adapter->req_rx_queues +
3317 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3319 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3320 if (!login_rsp_buffer)
3321 goto buf_rsp_alloc_failed;
3323 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3324 rsp_buffer_size, DMA_FROM_DEVICE);
3325 if (dma_mapping_error(dev, rsp_buffer_token)) {
3326 dev_err(dev, "Couldn't map login rsp buffer\n");
3327 goto buf_rsp_map_failed;
3330 adapter->login_buf = login_buffer;
3331 adapter->login_buf_token = buffer_token;
3332 adapter->login_buf_sz = buffer_size;
3333 adapter->login_rsp_buf = login_rsp_buffer;
3334 adapter->login_rsp_buf_token = rsp_buffer_token;
3335 adapter->login_rsp_buf_sz = rsp_buffer_size;
3337 login_buffer->len = cpu_to_be32(buffer_size);
3338 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3339 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3340 login_buffer->off_txcomp_subcrqs =
3341 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3342 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3343 login_buffer->off_rxcomp_subcrqs =
3344 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3345 sizeof(u64) * adapter->req_tx_queues);
3346 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3347 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3349 tx_list_p = (__be64 *)((char *)login_buffer +
3350 sizeof(struct ibmvnic_login_buffer));
3351 rx_list_p = (__be64 *)((char *)login_buffer +
3352 sizeof(struct ibmvnic_login_buffer) +
3353 sizeof(u64) * adapter->req_tx_queues);
3355 for (i = 0; i < adapter->req_tx_queues; i++) {
3356 if (adapter->tx_scrq[i]) {
3357 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3362 for (i = 0; i < adapter->req_rx_queues; i++) {
3363 if (adapter->rx_scrq[i]) {
3364 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3369 /* Insert vNIC login client data */
3370 vlcd = (struct vnic_login_client_data *)
3371 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3372 login_buffer->client_data_offset =
3373 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3374 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3376 vnic_add_client_data(adapter, vlcd);
3378 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3379 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3380 netdev_dbg(adapter->netdev, "%016lx\n",
3381 ((unsigned long int *)(adapter->login_buf))[i]);
3384 memset(&crq, 0, sizeof(crq));
3385 crq.login.first = IBMVNIC_CRQ_CMD;
3386 crq.login.cmd = LOGIN;
3387 crq.login.ioba = cpu_to_be32(buffer_token);
3388 crq.login.len = cpu_to_be32(buffer_size);
3389 ibmvnic_send_crq(adapter, &crq);
3394 kfree(login_rsp_buffer);
3395 buf_rsp_alloc_failed:
3396 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3398 kfree(login_buffer);
3403 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3406 union ibmvnic_crq crq;
3408 memset(&crq, 0, sizeof(crq));
3409 crq.request_map.first = IBMVNIC_CRQ_CMD;
3410 crq.request_map.cmd = REQUEST_MAP;
3411 crq.request_map.map_id = map_id;
3412 crq.request_map.ioba = cpu_to_be32(addr);
3413 crq.request_map.len = cpu_to_be32(len);
3414 return ibmvnic_send_crq(adapter, &crq);
3417 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3419 union ibmvnic_crq crq;
3421 memset(&crq, 0, sizeof(crq));
3422 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3423 crq.request_unmap.cmd = REQUEST_UNMAP;
3424 crq.request_unmap.map_id = map_id;
3425 return ibmvnic_send_crq(adapter, &crq);
3428 static void send_map_query(struct ibmvnic_adapter *adapter)
3430 union ibmvnic_crq crq;
3432 memset(&crq, 0, sizeof(crq));
3433 crq.query_map.first = IBMVNIC_CRQ_CMD;
3434 crq.query_map.cmd = QUERY_MAP;
3435 ibmvnic_send_crq(adapter, &crq);
3438 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3439 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3441 union ibmvnic_crq crq;
3443 atomic_set(&adapter->running_cap_crqs, 0);
3444 memset(&crq, 0, sizeof(crq));
3445 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3446 crq.query_capability.cmd = QUERY_CAPABILITY;
3448 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3449 atomic_inc(&adapter->running_cap_crqs);
3450 ibmvnic_send_crq(adapter, &crq);
3452 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3453 atomic_inc(&adapter->running_cap_crqs);
3454 ibmvnic_send_crq(adapter, &crq);
3456 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3457 atomic_inc(&adapter->running_cap_crqs);
3458 ibmvnic_send_crq(adapter, &crq);
3460 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3461 atomic_inc(&adapter->running_cap_crqs);
3462 ibmvnic_send_crq(adapter, &crq);
3464 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3465 atomic_inc(&adapter->running_cap_crqs);
3466 ibmvnic_send_crq(adapter, &crq);
3468 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3469 atomic_inc(&adapter->running_cap_crqs);
3470 ibmvnic_send_crq(adapter, &crq);
3472 crq.query_capability.capability =
3473 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3474 atomic_inc(&adapter->running_cap_crqs);
3475 ibmvnic_send_crq(adapter, &crq);
3477 crq.query_capability.capability =
3478 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3479 atomic_inc(&adapter->running_cap_crqs);
3480 ibmvnic_send_crq(adapter, &crq);
3482 crq.query_capability.capability =
3483 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3484 atomic_inc(&adapter->running_cap_crqs);
3485 ibmvnic_send_crq(adapter, &crq);
3487 crq.query_capability.capability =
3488 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3489 atomic_inc(&adapter->running_cap_crqs);
3490 ibmvnic_send_crq(adapter, &crq);
3492 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3493 atomic_inc(&adapter->running_cap_crqs);
3494 ibmvnic_send_crq(adapter, &crq);
3496 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3497 atomic_inc(&adapter->running_cap_crqs);
3498 ibmvnic_send_crq(adapter, &crq);
3500 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3501 atomic_inc(&adapter->running_cap_crqs);
3502 ibmvnic_send_crq(adapter, &crq);
3504 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3505 atomic_inc(&adapter->running_cap_crqs);
3506 ibmvnic_send_crq(adapter, &crq);
3508 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3509 atomic_inc(&adapter->running_cap_crqs);
3510 ibmvnic_send_crq(adapter, &crq);
3512 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3513 atomic_inc(&adapter->running_cap_crqs);
3514 ibmvnic_send_crq(adapter, &crq);
3516 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3517 atomic_inc(&adapter->running_cap_crqs);
3518 ibmvnic_send_crq(adapter, &crq);
3520 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3521 atomic_inc(&adapter->running_cap_crqs);
3522 ibmvnic_send_crq(adapter, &crq);
3524 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3525 atomic_inc(&adapter->running_cap_crqs);
3526 ibmvnic_send_crq(adapter, &crq);
3528 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3529 atomic_inc(&adapter->running_cap_crqs);
3530 ibmvnic_send_crq(adapter, &crq);
3532 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3533 atomic_inc(&adapter->running_cap_crqs);
3534 ibmvnic_send_crq(adapter, &crq);
3536 crq.query_capability.capability =
3537 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3538 atomic_inc(&adapter->running_cap_crqs);
3539 ibmvnic_send_crq(adapter, &crq);
3541 crq.query_capability.capability =
3542 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3543 atomic_inc(&adapter->running_cap_crqs);
3544 ibmvnic_send_crq(adapter, &crq);
3546 crq.query_capability.capability =
3547 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3548 atomic_inc(&adapter->running_cap_crqs);
3549 ibmvnic_send_crq(adapter, &crq);
3551 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3552 atomic_inc(&adapter->running_cap_crqs);
3553 ibmvnic_send_crq(adapter, &crq);
3556 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3557 struct ibmvnic_adapter *adapter)
3559 struct device *dev = &adapter->vdev->dev;
3561 if (crq->get_vpd_size_rsp.rc.code) {
3562 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3563 crq->get_vpd_size_rsp.rc.code);
3564 complete(&adapter->fw_done);
3568 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3569 complete(&adapter->fw_done);
3572 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3573 struct ibmvnic_adapter *adapter)
3575 struct device *dev = &adapter->vdev->dev;
3576 unsigned char *substr = NULL;
3577 u8 fw_level_len = 0;
3579 memset(adapter->fw_version, 0, 32);
3581 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3584 if (crq->get_vpd_rsp.rc.code) {
3585 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3586 crq->get_vpd_rsp.rc.code);
3590 /* get the position of the firmware version info
3591 * located after the ASCII 'RM' substring in the buffer
3593 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3595 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3599 /* get length of firmware level ASCII substring */
3600 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3601 fw_level_len = *(substr + 2);
3603 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3607 /* copy firmware version string from vpd into adapter */
3608 if ((substr + 3 + fw_level_len) <
3609 (adapter->vpd->buff + adapter->vpd->len)) {
3610 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3612 dev_info(dev, "FW substr extrapolated VPD buff\n");
3616 if (adapter->fw_version[0] == '\0')
3617 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3618 complete(&adapter->fw_done);
3621 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3623 struct device *dev = &adapter->vdev->dev;
3624 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3625 union ibmvnic_crq crq;
3628 dma_unmap_single(dev, adapter->ip_offload_tok,
3629 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3631 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3632 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3633 netdev_dbg(adapter->netdev, "%016lx\n",
3634 ((unsigned long int *)(buf))[i]);
3636 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3637 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3638 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3639 buf->tcp_ipv4_chksum);
3640 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3641 buf->tcp_ipv6_chksum);
3642 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3643 buf->udp_ipv4_chksum);
3644 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3645 buf->udp_ipv6_chksum);
3646 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3647 buf->large_tx_ipv4);
3648 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3649 buf->large_tx_ipv6);
3650 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3651 buf->large_rx_ipv4);
3652 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3653 buf->large_rx_ipv6);
3654 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3655 buf->max_ipv4_header_size);
3656 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3657 buf->max_ipv6_header_size);
3658 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3659 buf->max_tcp_header_size);
3660 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3661 buf->max_udp_header_size);
3662 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3663 buf->max_large_tx_size);
3664 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3665 buf->max_large_rx_size);
3666 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3667 buf->ipv6_extension_header);
3668 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3669 buf->tcp_pseudosum_req);
3670 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3671 buf->num_ipv6_ext_headers);
3672 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3673 buf->off_ipv6_ext_headers);
3675 adapter->ip_offload_ctrl_tok =
3676 dma_map_single(dev, &adapter->ip_offload_ctrl,
3677 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3679 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3680 dev_err(dev, "Couldn't map ip offload control buffer\n");
3684 adapter->ip_offload_ctrl.len =
3685 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3686 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3687 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3688 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3689 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3690 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3691 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3692 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3693 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3694 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3696 /* large_rx disabled for now, additional features needed */
3697 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3698 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3700 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3702 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3703 adapter->netdev->features |= NETIF_F_IP_CSUM;
3705 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3706 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3708 if ((adapter->netdev->features &
3709 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3710 adapter->netdev->features |= NETIF_F_RXCSUM;
3712 if (buf->large_tx_ipv4)
3713 adapter->netdev->features |= NETIF_F_TSO;
3714 if (buf->large_tx_ipv6)
3715 adapter->netdev->features |= NETIF_F_TSO6;
3717 adapter->netdev->hw_features |= adapter->netdev->features;
3719 memset(&crq, 0, sizeof(crq));
3720 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3721 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3722 crq.control_ip_offload.len =
3723 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3724 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3725 ibmvnic_send_crq(adapter, &crq);
3728 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3729 struct ibmvnic_adapter *adapter)
3731 struct device *dev = &adapter->vdev->dev;
3732 struct ibmvnic_error_buff *error_buff, *tmp;
3733 unsigned long flags;
3737 if (!crq->request_error_rsp.rc.code) {
3738 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3739 crq->request_error_rsp.rc.code);
3743 spin_lock_irqsave(&adapter->error_list_lock, flags);
3744 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3745 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3747 list_del(&error_buff->list);
3750 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3753 dev_err(dev, "Couldn't find error id %x\n",
3754 be32_to_cpu(crq->request_error_rsp.error_id));
3758 dev_err(dev, "Detailed info for error id %x:",
3759 be32_to_cpu(crq->request_error_rsp.error_id));
3761 for (i = 0; i < error_buff->len; i++) {
3762 pr_cont("%02x", (int)error_buff->buff[i]);
3768 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3770 kfree(error_buff->buff);
3774 static void request_error_information(struct ibmvnic_adapter *adapter,
3775 union ibmvnic_crq *err_crq)
3777 struct device *dev = &adapter->vdev->dev;
3778 struct net_device *netdev = adapter->netdev;
3779 struct ibmvnic_error_buff *error_buff;
3780 unsigned long timeout = msecs_to_jiffies(30000);
3781 union ibmvnic_crq crq;
3782 unsigned long flags;
3785 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3789 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3790 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3791 if (!error_buff->buff) {
3796 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3798 if (dma_mapping_error(dev, error_buff->dma)) {
3799 netdev_err(netdev, "Couldn't map error buffer\n");
3800 kfree(error_buff->buff);
3805 error_buff->len = detail_len;
3806 error_buff->error_id = err_crq->error_indication.error_id;
3808 spin_lock_irqsave(&adapter->error_list_lock, flags);
3809 list_add_tail(&error_buff->list, &adapter->errors);
3810 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3812 memset(&crq, 0, sizeof(crq));
3813 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3814 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3815 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3816 crq.request_error_info.len = cpu_to_be32(detail_len);
3817 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3819 rc = ibmvnic_send_crq(adapter, &crq);
3821 netdev_err(netdev, "failed to request error information\n");
3825 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3826 netdev_err(netdev, "timeout waiting for error information\n");
3833 spin_lock_irqsave(&adapter->error_list_lock, flags);
3834 list_del(&error_buff->list);
3835 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3837 kfree(error_buff->buff);
3841 static void handle_error_indication(union ibmvnic_crq *crq,
3842 struct ibmvnic_adapter *adapter)
3844 struct device *dev = &adapter->vdev->dev;
3846 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3847 crq->error_indication.flags
3848 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3849 be32_to_cpu(crq->error_indication.error_id),
3850 be16_to_cpu(crq->error_indication.error_cause));
3852 if (be32_to_cpu(crq->error_indication.error_id))
3853 request_error_information(adapter, crq);
3855 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3856 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3858 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3861 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3862 struct ibmvnic_adapter *adapter)
3864 struct net_device *netdev = adapter->netdev;
3865 struct device *dev = &adapter->vdev->dev;
3868 rc = crq->change_mac_addr_rsp.rc.code;
3870 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3873 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3876 complete(&adapter->fw_done);
3880 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3881 struct ibmvnic_adapter *adapter)
3883 struct device *dev = &adapter->vdev->dev;
3887 atomic_dec(&adapter->running_cap_crqs);
3888 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3890 req_value = &adapter->req_tx_queues;
3894 req_value = &adapter->req_rx_queues;
3897 case REQ_RX_ADD_QUEUES:
3898 req_value = &adapter->req_rx_add_queues;
3901 case REQ_TX_ENTRIES_PER_SUBCRQ:
3902 req_value = &adapter->req_tx_entries_per_subcrq;
3903 name = "tx_entries_per_subcrq";
3905 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3906 req_value = &adapter->req_rx_add_entries_per_subcrq;
3907 name = "rx_add_entries_per_subcrq";
3910 req_value = &adapter->req_mtu;
3913 case PROMISC_REQUESTED:
3914 req_value = &adapter->promisc;
3918 dev_err(dev, "Got invalid cap request rsp %d\n",
3919 crq->request_capability.capability);
3923 switch (crq->request_capability_rsp.rc.code) {
3926 case PARTIALSUCCESS:
3927 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3929 (long int)be64_to_cpu(crq->request_capability_rsp.
3932 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3934 pr_err("mtu of %llu is not supported. Reverting.\n",
3936 *req_value = adapter->fallback.mtu;
3939 be64_to_cpu(crq->request_capability_rsp.number);
3942 ibmvnic_send_req_caps(adapter, 1);
3945 dev_err(dev, "Error %d in request cap rsp\n",
3946 crq->request_capability_rsp.rc.code);
3950 /* Done receiving requested capabilities, query IP offload support */
3951 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3952 union ibmvnic_crq newcrq;
3953 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3954 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3955 &adapter->ip_offload_buf;
3957 adapter->wait_capability = false;
3958 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3962 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3963 if (!firmware_has_feature(FW_FEATURE_CMO))
3964 dev_err(dev, "Couldn't map offload buffer\n");
3968 memset(&newcrq, 0, sizeof(newcrq));
3969 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3970 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3971 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3972 newcrq.query_ip_offload.ioba =
3973 cpu_to_be32(adapter->ip_offload_tok);
3975 ibmvnic_send_crq(adapter, &newcrq);
3979 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3980 struct ibmvnic_adapter *adapter)
3982 struct device *dev = &adapter->vdev->dev;
3983 struct net_device *netdev = adapter->netdev;
3984 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3985 struct ibmvnic_login_buffer *login = adapter->login_buf;
3988 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3990 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3991 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
3993 /* If the number of queues requested can't be allocated by the
3994 * server, the login response will return with code 1. We will need
3995 * to resend the login buffer with fewer queues requested.
3997 if (login_rsp_crq->generic.rc.code) {
3998 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
3999 complete(&adapter->init_done);
4003 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4005 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4006 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4007 netdev_dbg(adapter->netdev, "%016lx\n",
4008 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4012 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4013 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4014 adapter->req_rx_add_queues !=
4015 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4016 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4017 ibmvnic_remove(adapter->vdev);
4020 release_login_buffer(adapter);
4021 complete(&adapter->init_done);
4026 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4027 struct ibmvnic_adapter *adapter)
4029 struct device *dev = &adapter->vdev->dev;
4032 rc = crq->request_unmap_rsp.rc.code;
4034 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4037 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4038 struct ibmvnic_adapter *adapter)
4040 struct net_device *netdev = adapter->netdev;
4041 struct device *dev = &adapter->vdev->dev;
4044 rc = crq->query_map_rsp.rc.code;
4046 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4049 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4050 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4051 crq->query_map_rsp.free_pages);
4054 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4055 struct ibmvnic_adapter *adapter)
4057 struct net_device *netdev = adapter->netdev;
4058 struct device *dev = &adapter->vdev->dev;
4061 atomic_dec(&adapter->running_cap_crqs);
4062 netdev_dbg(netdev, "Outstanding queries: %d\n",
4063 atomic_read(&adapter->running_cap_crqs));
4064 rc = crq->query_capability.rc.code;
4066 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4070 switch (be16_to_cpu(crq->query_capability.capability)) {
4072 adapter->min_tx_queues =
4073 be64_to_cpu(crq->query_capability.number);
4074 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4075 adapter->min_tx_queues);
4078 adapter->min_rx_queues =
4079 be64_to_cpu(crq->query_capability.number);
4080 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4081 adapter->min_rx_queues);
4083 case MIN_RX_ADD_QUEUES:
4084 adapter->min_rx_add_queues =
4085 be64_to_cpu(crq->query_capability.number);
4086 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4087 adapter->min_rx_add_queues);
4090 adapter->max_tx_queues =
4091 be64_to_cpu(crq->query_capability.number);
4092 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4093 adapter->max_tx_queues);
4096 adapter->max_rx_queues =
4097 be64_to_cpu(crq->query_capability.number);
4098 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4099 adapter->max_rx_queues);
4101 case MAX_RX_ADD_QUEUES:
4102 adapter->max_rx_add_queues =
4103 be64_to_cpu(crq->query_capability.number);
4104 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4105 adapter->max_rx_add_queues);
4107 case MIN_TX_ENTRIES_PER_SUBCRQ:
4108 adapter->min_tx_entries_per_subcrq =
4109 be64_to_cpu(crq->query_capability.number);
4110 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4111 adapter->min_tx_entries_per_subcrq);
4113 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4114 adapter->min_rx_add_entries_per_subcrq =
4115 be64_to_cpu(crq->query_capability.number);
4116 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4117 adapter->min_rx_add_entries_per_subcrq);
4119 case MAX_TX_ENTRIES_PER_SUBCRQ:
4120 adapter->max_tx_entries_per_subcrq =
4121 be64_to_cpu(crq->query_capability.number);
4122 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4123 adapter->max_tx_entries_per_subcrq);
4125 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4126 adapter->max_rx_add_entries_per_subcrq =
4127 be64_to_cpu(crq->query_capability.number);
4128 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4129 adapter->max_rx_add_entries_per_subcrq);
4131 case TCP_IP_OFFLOAD:
4132 adapter->tcp_ip_offload =
4133 be64_to_cpu(crq->query_capability.number);
4134 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4135 adapter->tcp_ip_offload);
4137 case PROMISC_SUPPORTED:
4138 adapter->promisc_supported =
4139 be64_to_cpu(crq->query_capability.number);
4140 netdev_dbg(netdev, "promisc_supported = %lld\n",
4141 adapter->promisc_supported);
4144 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4145 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4146 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4149 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4150 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4151 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4153 case MAX_MULTICAST_FILTERS:
4154 adapter->max_multicast_filters =
4155 be64_to_cpu(crq->query_capability.number);
4156 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4157 adapter->max_multicast_filters);
4159 case VLAN_HEADER_INSERTION:
4160 adapter->vlan_header_insertion =
4161 be64_to_cpu(crq->query_capability.number);
4162 if (adapter->vlan_header_insertion)
4163 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4164 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4165 adapter->vlan_header_insertion);
4167 case RX_VLAN_HEADER_INSERTION:
4168 adapter->rx_vlan_header_insertion =
4169 be64_to_cpu(crq->query_capability.number);
4170 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4171 adapter->rx_vlan_header_insertion);
4173 case MAX_TX_SG_ENTRIES:
4174 adapter->max_tx_sg_entries =
4175 be64_to_cpu(crq->query_capability.number);
4176 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4177 adapter->max_tx_sg_entries);
4179 case RX_SG_SUPPORTED:
4180 adapter->rx_sg_supported =
4181 be64_to_cpu(crq->query_capability.number);
4182 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4183 adapter->rx_sg_supported);
4185 case OPT_TX_COMP_SUB_QUEUES:
4186 adapter->opt_tx_comp_sub_queues =
4187 be64_to_cpu(crq->query_capability.number);
4188 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4189 adapter->opt_tx_comp_sub_queues);
4191 case OPT_RX_COMP_QUEUES:
4192 adapter->opt_rx_comp_queues =
4193 be64_to_cpu(crq->query_capability.number);
4194 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4195 adapter->opt_rx_comp_queues);
4197 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4198 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4199 be64_to_cpu(crq->query_capability.number);
4200 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4201 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4203 case OPT_TX_ENTRIES_PER_SUBCRQ:
4204 adapter->opt_tx_entries_per_subcrq =
4205 be64_to_cpu(crq->query_capability.number);
4206 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4207 adapter->opt_tx_entries_per_subcrq);
4209 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4210 adapter->opt_rxba_entries_per_subcrq =
4211 be64_to_cpu(crq->query_capability.number);
4212 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4213 adapter->opt_rxba_entries_per_subcrq);
4215 case TX_RX_DESC_REQ:
4216 adapter->tx_rx_desc_req = crq->query_capability.number;
4217 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4218 adapter->tx_rx_desc_req);
4222 netdev_err(netdev, "Got invalid cap rsp %d\n",
4223 crq->query_capability.capability);
4227 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4228 adapter->wait_capability = false;
4229 ibmvnic_send_req_caps(adapter, 0);
4233 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4234 struct ibmvnic_adapter *adapter)
4236 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4237 struct net_device *netdev = adapter->netdev;
4238 struct device *dev = &adapter->vdev->dev;
4239 u64 *u64_crq = (u64 *)crq;
4242 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4243 (unsigned long int)cpu_to_be64(u64_crq[0]),
4244 (unsigned long int)cpu_to_be64(u64_crq[1]));
4245 switch (gen_crq->first) {
4246 case IBMVNIC_CRQ_INIT_RSP:
4247 switch (gen_crq->cmd) {
4248 case IBMVNIC_CRQ_INIT:
4249 dev_info(dev, "Partner initialized\n");
4250 adapter->from_passive_init = true;
4251 adapter->failover_pending = false;
4252 if (!completion_done(&adapter->init_done)) {
4253 complete(&adapter->init_done);
4254 adapter->init_done_rc = -EIO;
4256 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4258 case IBMVNIC_CRQ_INIT_COMPLETE:
4259 dev_info(dev, "Partner initialization complete\n");
4260 adapter->crq.active = true;
4261 send_version_xchg(adapter);
4264 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4267 case IBMVNIC_CRQ_XPORT_EVENT:
4268 netif_carrier_off(netdev);
4269 adapter->crq.active = false;
4270 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4271 dev_info(dev, "Migrated, re-enabling adapter\n");
4272 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4273 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4274 dev_info(dev, "Backing device failover detected\n");
4275 adapter->failover_pending = true;
4277 /* The adapter lost the connection */
4278 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4280 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4283 case IBMVNIC_CRQ_CMD_RSP:
4286 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4291 switch (gen_crq->cmd) {
4292 case VERSION_EXCHANGE_RSP:
4293 rc = crq->version_exchange_rsp.rc.code;
4295 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4298 dev_info(dev, "Partner protocol version is %d\n",
4299 crq->version_exchange_rsp.version);
4300 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4303 be16_to_cpu(crq->version_exchange_rsp.version);
4304 send_cap_queries(adapter);
4306 case QUERY_CAPABILITY_RSP:
4307 handle_query_cap_rsp(crq, adapter);
4310 handle_query_map_rsp(crq, adapter);
4312 case REQUEST_MAP_RSP:
4313 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4314 complete(&adapter->fw_done);
4316 case REQUEST_UNMAP_RSP:
4317 handle_request_unmap_rsp(crq, adapter);
4319 case REQUEST_CAPABILITY_RSP:
4320 handle_request_cap_rsp(crq, adapter);
4323 netdev_dbg(netdev, "Got Login Response\n");
4324 handle_login_rsp(crq, adapter);
4326 case LOGICAL_LINK_STATE_RSP:
4328 "Got Logical Link State Response, state: %d rc: %d\n",
4329 crq->logical_link_state_rsp.link_state,
4330 crq->logical_link_state_rsp.rc.code);
4331 adapter->logical_link_state =
4332 crq->logical_link_state_rsp.link_state;
4333 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4334 complete(&adapter->init_done);
4336 case LINK_STATE_INDICATION:
4337 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4338 adapter->phys_link_state =
4339 crq->link_state_indication.phys_link_state;
4340 adapter->logical_link_state =
4341 crq->link_state_indication.logical_link_state;
4343 case CHANGE_MAC_ADDR_RSP:
4344 netdev_dbg(netdev, "Got MAC address change Response\n");
4345 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4347 case ERROR_INDICATION:
4348 netdev_dbg(netdev, "Got Error Indication\n");
4349 handle_error_indication(crq, adapter);
4351 case REQUEST_ERROR_RSP:
4352 netdev_dbg(netdev, "Got Error Detail Response\n");
4353 handle_error_info_rsp(crq, adapter);
4355 case REQUEST_STATISTICS_RSP:
4356 netdev_dbg(netdev, "Got Statistics Response\n");
4357 complete(&adapter->stats_done);
4359 case QUERY_IP_OFFLOAD_RSP:
4360 netdev_dbg(netdev, "Got Query IP offload Response\n");
4361 handle_query_ip_offload_rsp(adapter);
4363 case MULTICAST_CTRL_RSP:
4364 netdev_dbg(netdev, "Got multicast control Response\n");
4366 case CONTROL_IP_OFFLOAD_RSP:
4367 netdev_dbg(netdev, "Got Control IP offload Response\n");
4368 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4369 sizeof(adapter->ip_offload_ctrl),
4371 complete(&adapter->init_done);
4373 case COLLECT_FW_TRACE_RSP:
4374 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4375 complete(&adapter->fw_done);
4377 case GET_VPD_SIZE_RSP:
4378 handle_vpd_size_rsp(crq, adapter);
4381 handle_vpd_rsp(crq, adapter);
4384 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4389 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4391 struct ibmvnic_adapter *adapter = instance;
4393 tasklet_schedule(&adapter->tasklet);
4397 static void ibmvnic_tasklet(void *data)
4399 struct ibmvnic_adapter *adapter = data;
4400 struct ibmvnic_crq_queue *queue = &adapter->crq;
4401 union ibmvnic_crq *crq;
4402 unsigned long flags;
4405 spin_lock_irqsave(&queue->lock, flags);
4407 /* Pull all the valid messages off the CRQ */
4408 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4409 ibmvnic_handle_crq(crq, adapter);
4410 crq->generic.first = 0;
4413 /* remain in tasklet until all
4414 * capabilities responses are received
4416 if (!adapter->wait_capability)
4419 /* if capabilities CRQ's were sent in this tasklet, the following
4420 * tasklet must wait until all responses are received
4422 if (atomic_read(&adapter->running_cap_crqs) != 0)
4423 adapter->wait_capability = true;
4424 spin_unlock_irqrestore(&queue->lock, flags);
4427 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4429 struct vio_dev *vdev = adapter->vdev;
4433 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4434 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4437 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4442 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4444 struct ibmvnic_crq_queue *crq = &adapter->crq;
4445 struct device *dev = &adapter->vdev->dev;
4446 struct vio_dev *vdev = adapter->vdev;
4451 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4452 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4454 /* Clean out the queue */
4455 memset(crq->msgs, 0, PAGE_SIZE);
4457 crq->active = false;
4459 /* And re-open it again */
4460 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4461 crq->msg_token, PAGE_SIZE);
4464 /* Adapter is good, but other end is not ready */
4465 dev_warn(dev, "Partner adapter not ready\n");
4467 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4472 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4474 struct ibmvnic_crq_queue *crq = &adapter->crq;
4475 struct vio_dev *vdev = adapter->vdev;
4481 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4482 free_irq(vdev->irq, adapter);
4483 tasklet_kill(&adapter->tasklet);
4485 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4486 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4488 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4490 free_page((unsigned long)crq->msgs);
4492 crq->active = false;
4495 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4497 struct ibmvnic_crq_queue *crq = &adapter->crq;
4498 struct device *dev = &adapter->vdev->dev;
4499 struct vio_dev *vdev = adapter->vdev;
4500 int rc, retrc = -ENOMEM;
4505 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4506 /* Should we allocate more than one page? */
4511 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4512 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4514 if (dma_mapping_error(dev, crq->msg_token))
4517 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4518 crq->msg_token, PAGE_SIZE);
4520 if (rc == H_RESOURCE)
4521 /* maybe kexecing and resource is busy. try a reset */
4522 rc = ibmvnic_reset_crq(adapter);
4525 if (rc == H_CLOSED) {
4526 dev_warn(dev, "Partner adapter not ready\n");
4528 dev_warn(dev, "Error %d opening adapter\n", rc);
4529 goto reg_crq_failed;
4534 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4535 (unsigned long)adapter);
4537 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4538 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4541 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4543 goto req_irq_failed;
4546 rc = vio_enable_interrupts(vdev);
4548 dev_err(dev, "Error %d enabling interrupts\n", rc);
4549 goto req_irq_failed;
4553 spin_lock_init(&crq->lock);
4558 tasklet_kill(&adapter->tasklet);
4560 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4561 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4563 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4565 free_page((unsigned long)crq->msgs);
4570 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4572 struct device *dev = &adapter->vdev->dev;
4573 unsigned long timeout = msecs_to_jiffies(30000);
4574 u64 old_num_rx_queues, old_num_tx_queues;
4577 adapter->from_passive_init = false;
4579 old_num_rx_queues = adapter->req_rx_queues;
4580 old_num_tx_queues = adapter->req_tx_queues;
4582 init_completion(&adapter->init_done);
4583 adapter->init_done_rc = 0;
4584 ibmvnic_send_crq_init(adapter);
4585 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4586 dev_err(dev, "Initialization sequence timed out\n");
4590 if (adapter->init_done_rc) {
4591 release_crq_queue(adapter);
4592 return adapter->init_done_rc;
4595 if (adapter->from_passive_init) {
4596 adapter->state = VNIC_OPEN;
4597 adapter->from_passive_init = false;
4601 if (adapter->resetting && !adapter->wait_for_reset &&
4602 adapter->reset_reason != VNIC_RESET_MOBILITY) {
4603 if (adapter->req_rx_queues != old_num_rx_queues ||
4604 adapter->req_tx_queues != old_num_tx_queues) {
4605 release_sub_crqs(adapter, 0);
4606 rc = init_sub_crqs(adapter);
4608 rc = reset_sub_crq_queues(adapter);
4611 rc = init_sub_crqs(adapter);
4615 dev_err(dev, "Initialization of sub crqs failed\n");
4616 release_crq_queue(adapter);
4620 rc = init_sub_crq_irqs(adapter);
4622 dev_err(dev, "Failed to initialize sub crq irqs\n");
4623 release_crq_queue(adapter);
4629 static struct device_attribute dev_attr_failover;
4631 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4633 struct ibmvnic_adapter *adapter;
4634 struct net_device *netdev;
4635 unsigned char *mac_addr_p;
4638 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4641 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4642 VETH_MAC_ADDR, NULL);
4645 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4646 __FILE__, __LINE__);
4650 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4651 IBMVNIC_MAX_QUEUES);
4655 adapter = netdev_priv(netdev);
4656 adapter->state = VNIC_PROBING;
4657 dev_set_drvdata(&dev->dev, netdev);
4658 adapter->vdev = dev;
4659 adapter->netdev = netdev;
4661 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4662 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4663 netdev->irq = dev->irq;
4664 netdev->netdev_ops = &ibmvnic_netdev_ops;
4665 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4666 SET_NETDEV_DEV(netdev, &dev->dev);
4668 spin_lock_init(&adapter->stats_lock);
4670 INIT_LIST_HEAD(&adapter->errors);
4671 spin_lock_init(&adapter->error_list_lock);
4673 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4674 INIT_LIST_HEAD(&adapter->rwi_list);
4675 mutex_init(&adapter->reset_lock);
4676 mutex_init(&adapter->rwi_lock);
4677 adapter->resetting = false;
4679 adapter->mac_change_pending = false;
4682 rc = init_crq_queue(adapter);
4684 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4686 goto ibmvnic_init_fail;
4689 rc = ibmvnic_init(adapter);
4690 if (rc && rc != EAGAIN)
4691 goto ibmvnic_init_fail;
4692 } while (rc == EAGAIN);
4694 rc = init_stats_buffers(adapter);
4696 goto ibmvnic_init_fail;
4698 rc = init_stats_token(adapter);
4700 goto ibmvnic_stats_fail;
4702 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4703 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4704 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4706 rc = device_create_file(&dev->dev, &dev_attr_failover);
4708 goto ibmvnic_dev_file_err;
4710 netif_carrier_off(netdev);
4711 rc = register_netdev(netdev);
4713 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4714 goto ibmvnic_register_fail;
4716 dev_info(&dev->dev, "ibmvnic registered\n");
4718 adapter->state = VNIC_PROBED;
4720 adapter->wait_for_reset = false;
4724 ibmvnic_register_fail:
4725 device_remove_file(&dev->dev, &dev_attr_failover);
4727 ibmvnic_dev_file_err:
4728 release_stats_token(adapter);
4731 release_stats_buffers(adapter);
4734 release_sub_crqs(adapter, 1);
4735 release_crq_queue(adapter);
4736 free_netdev(netdev);
4741 static int ibmvnic_remove(struct vio_dev *dev)
4743 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4744 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4746 adapter->state = VNIC_REMOVING;
4747 unregister_netdev(netdev);
4748 mutex_lock(&adapter->reset_lock);
4750 release_resources(adapter);
4751 release_sub_crqs(adapter, 1);
4752 release_crq_queue(adapter);
4754 release_stats_token(adapter);
4755 release_stats_buffers(adapter);
4757 adapter->state = VNIC_REMOVED;
4759 mutex_unlock(&adapter->reset_lock);
4760 device_remove_file(&dev->dev, &dev_attr_failover);
4761 free_netdev(netdev);
4762 dev_set_drvdata(&dev->dev, NULL);
4767 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4768 const char *buf, size_t count)
4770 struct net_device *netdev = dev_get_drvdata(dev);
4771 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4772 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4773 __be64 session_token;
4776 if (!sysfs_streq(buf, "1"))
4779 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4780 H_GET_SESSION_TOKEN, 0, 0, 0);
4782 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4787 session_token = (__be64)retbuf[0];
4788 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4789 be64_to_cpu(session_token));
4790 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4791 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4793 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4801 static DEVICE_ATTR_WO(failover);
4803 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4805 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4806 struct ibmvnic_adapter *adapter;
4807 struct iommu_table *tbl;
4808 unsigned long ret = 0;
4811 tbl = get_iommu_table_base(&vdev->dev);
4813 /* netdev inits at probe time along with the structures we need below*/
4815 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4817 adapter = netdev_priv(netdev);
4819 ret += PAGE_SIZE; /* the crq message queue */
4820 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4822 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4823 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4825 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4827 ret += adapter->rx_pool[i].size *
4828 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4833 static int ibmvnic_resume(struct device *dev)
4835 struct net_device *netdev = dev_get_drvdata(dev);
4836 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4838 if (adapter->state != VNIC_OPEN)
4841 tasklet_schedule(&adapter->tasklet);
4846 static const struct vio_device_id ibmvnic_device_table[] = {
4847 {"network", "IBM,vnic"},
4850 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4852 static const struct dev_pm_ops ibmvnic_pm_ops = {
4853 .resume = ibmvnic_resume
4856 static struct vio_driver ibmvnic_driver = {
4857 .id_table = ibmvnic_device_table,
4858 .probe = ibmvnic_probe,
4859 .remove = ibmvnic_remove,
4860 .get_desired_dma = ibmvnic_get_desired_dma,
4861 .name = ibmvnic_driver_name,
4862 .pm = &ibmvnic_pm_ops,
4865 /* module functions */
4866 static int __init ibmvnic_module_init(void)
4868 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4869 IBMVNIC_DRIVER_VERSION);
4871 return vio_register_driver(&ibmvnic_driver);
4874 static void __exit ibmvnic_module_exit(void)
4876 vio_unregister_driver(&ibmvnic_driver);
4879 module_init(ibmvnic_module_init);
4880 module_exit(ibmvnic_module_exit);