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 */
932 } else if (adapter->init_done_rc) {
933 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
934 adapter->init_done_rc);
935 return adapter->init_done_rc;
942 static int set_real_num_queues(struct net_device *netdev)
944 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
947 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
948 adapter->req_tx_queues, adapter->req_rx_queues);
950 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
952 netdev_err(netdev, "failed to set the number of tx queues\n");
956 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
958 netdev_err(netdev, "failed to set the number of rx queues\n");
963 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
965 struct device *dev = &adapter->vdev->dev;
966 union ibmvnic_crq crq;
970 if (adapter->vpd->buff)
971 len = adapter->vpd->len;
973 init_completion(&adapter->fw_done);
974 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
975 crq.get_vpd_size.cmd = GET_VPD_SIZE;
976 rc = ibmvnic_send_crq(adapter, &crq);
979 wait_for_completion(&adapter->fw_done);
981 if (!adapter->vpd->len)
984 if (!adapter->vpd->buff)
985 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
986 else if (adapter->vpd->len != len)
988 krealloc(adapter->vpd->buff,
989 adapter->vpd->len, GFP_KERNEL);
991 if (!adapter->vpd->buff) {
992 dev_err(dev, "Could allocate VPD buffer\n");
996 adapter->vpd->dma_addr =
997 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
999 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
1000 dev_err(dev, "Could not map VPD buffer\n");
1001 kfree(adapter->vpd->buff);
1002 adapter->vpd->buff = NULL;
1006 reinit_completion(&adapter->fw_done);
1007 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1008 crq.get_vpd.cmd = GET_VPD;
1009 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1010 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1011 rc = ibmvnic_send_crq(adapter, &crq);
1013 kfree(adapter->vpd->buff);
1014 adapter->vpd->buff = NULL;
1017 wait_for_completion(&adapter->fw_done);
1022 static int init_resources(struct ibmvnic_adapter *adapter)
1024 struct net_device *netdev = adapter->netdev;
1027 rc = set_real_num_queues(netdev);
1031 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1035 /* Vital Product Data (VPD) */
1036 rc = ibmvnic_get_vpd(adapter);
1038 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1042 adapter->map_id = 1;
1044 rc = init_napi(adapter);
1048 send_map_query(adapter);
1050 rc = init_rx_pools(netdev);
1054 rc = init_tx_pools(netdev);
1058 static int __ibmvnic_open(struct net_device *netdev)
1060 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1061 enum vnic_state prev_state = adapter->state;
1064 adapter->state = VNIC_OPENING;
1065 replenish_pools(adapter);
1066 ibmvnic_napi_enable(adapter);
1068 /* We're ready to receive frames, enable the sub-crq interrupts and
1069 * set the logical link state to up
1071 for (i = 0; i < adapter->req_rx_queues; i++) {
1072 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1073 if (prev_state == VNIC_CLOSED)
1074 enable_irq(adapter->rx_scrq[i]->irq);
1075 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1078 for (i = 0; i < adapter->req_tx_queues; i++) {
1079 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1080 if (prev_state == VNIC_CLOSED)
1081 enable_irq(adapter->tx_scrq[i]->irq);
1082 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1085 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1087 for (i = 0; i < adapter->req_rx_queues; i++)
1088 napi_disable(&adapter->napi[i]);
1089 release_resources(adapter);
1093 netif_tx_start_all_queues(netdev);
1095 if (prev_state == VNIC_CLOSED) {
1096 for (i = 0; i < adapter->req_rx_queues; i++)
1097 napi_schedule(&adapter->napi[i]);
1100 adapter->state = VNIC_OPEN;
1104 static int ibmvnic_open(struct net_device *netdev)
1106 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1109 /* If device failover is pending, just set device state and return.
1110 * Device operation will be handled by reset routine.
1112 if (adapter->failover_pending) {
1113 adapter->state = VNIC_OPEN;
1117 mutex_lock(&adapter->reset_lock);
1119 if (adapter->state != VNIC_CLOSED) {
1120 rc = ibmvnic_login(netdev);
1122 mutex_unlock(&adapter->reset_lock);
1126 rc = init_resources(adapter);
1128 netdev_err(netdev, "failed to initialize resources\n");
1129 release_resources(adapter);
1130 mutex_unlock(&adapter->reset_lock);
1135 rc = __ibmvnic_open(netdev);
1136 netif_carrier_on(netdev);
1138 mutex_unlock(&adapter->reset_lock);
1143 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1145 struct ibmvnic_rx_pool *rx_pool;
1146 struct ibmvnic_rx_buff *rx_buff;
1151 if (!adapter->rx_pool)
1154 rx_scrqs = adapter->num_active_rx_pools;
1155 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1157 /* Free any remaining skbs in the rx buffer pools */
1158 for (i = 0; i < rx_scrqs; i++) {
1159 rx_pool = &adapter->rx_pool[i];
1160 if (!rx_pool || !rx_pool->rx_buff)
1163 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1164 for (j = 0; j < rx_entries; j++) {
1165 rx_buff = &rx_pool->rx_buff[j];
1166 if (rx_buff && rx_buff->skb) {
1167 dev_kfree_skb_any(rx_buff->skb);
1168 rx_buff->skb = NULL;
1174 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1175 struct ibmvnic_tx_pool *tx_pool)
1177 struct ibmvnic_tx_buff *tx_buff;
1181 if (!tx_pool || !tx_pool->tx_buff)
1184 tx_entries = tx_pool->num_buffers;
1186 for (i = 0; i < tx_entries; i++) {
1187 tx_buff = &tx_pool->tx_buff[i];
1188 if (tx_buff && tx_buff->skb) {
1189 dev_kfree_skb_any(tx_buff->skb);
1190 tx_buff->skb = NULL;
1195 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1200 if (!adapter->tx_pool || !adapter->tso_pool)
1203 tx_scrqs = adapter->num_active_tx_pools;
1205 /* Free any remaining skbs in the tx buffer pools */
1206 for (i = 0; i < tx_scrqs; i++) {
1207 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1208 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1209 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1213 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1215 struct net_device *netdev = adapter->netdev;
1218 if (adapter->tx_scrq) {
1219 for (i = 0; i < adapter->req_tx_queues; i++)
1220 if (adapter->tx_scrq[i]->irq) {
1222 "Disabling tx_scrq[%d] irq\n", i);
1223 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1224 disable_irq(adapter->tx_scrq[i]->irq);
1228 if (adapter->rx_scrq) {
1229 for (i = 0; i < adapter->req_rx_queues; i++) {
1230 if (adapter->rx_scrq[i]->irq) {
1232 "Disabling rx_scrq[%d] irq\n", i);
1233 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1234 disable_irq(adapter->rx_scrq[i]->irq);
1240 static void ibmvnic_cleanup(struct net_device *netdev)
1242 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1244 /* ensure that transmissions are stopped if called by do_reset */
1245 if (adapter->resetting)
1246 netif_tx_disable(netdev);
1248 netif_tx_stop_all_queues(netdev);
1250 ibmvnic_napi_disable(adapter);
1251 ibmvnic_disable_irqs(adapter);
1253 clean_rx_pools(adapter);
1254 clean_tx_pools(adapter);
1257 static int __ibmvnic_close(struct net_device *netdev)
1259 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1262 adapter->state = VNIC_CLOSING;
1263 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1266 adapter->state = VNIC_CLOSED;
1270 static int ibmvnic_close(struct net_device *netdev)
1272 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1275 /* If device failover is pending, just set device state and return.
1276 * Device operation will be handled by reset routine.
1278 if (adapter->failover_pending) {
1279 adapter->state = VNIC_CLOSED;
1283 mutex_lock(&adapter->reset_lock);
1284 rc = __ibmvnic_close(netdev);
1285 ibmvnic_cleanup(netdev);
1286 mutex_unlock(&adapter->reset_lock);
1292 * build_hdr_data - creates L2/L3/L4 header data buffer
1293 * @hdr_field - bitfield determining needed headers
1294 * @skb - socket buffer
1295 * @hdr_len - array of header lengths
1296 * @tot_len - total length of data
1298 * Reads hdr_field to determine which headers are needed by firmware.
1299 * Builds a buffer containing these headers. Saves individual header
1300 * lengths and total buffer length to be used to build descriptors.
1302 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1303 int *hdr_len, u8 *hdr_data)
1308 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1309 hdr_len[0] = sizeof(struct vlan_ethhdr);
1311 hdr_len[0] = sizeof(struct ethhdr);
1313 if (skb->protocol == htons(ETH_P_IP)) {
1314 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1315 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1316 hdr_len[2] = tcp_hdrlen(skb);
1317 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1318 hdr_len[2] = sizeof(struct udphdr);
1319 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1320 hdr_len[1] = sizeof(struct ipv6hdr);
1321 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1322 hdr_len[2] = tcp_hdrlen(skb);
1323 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1324 hdr_len[2] = sizeof(struct udphdr);
1325 } else if (skb->protocol == htons(ETH_P_ARP)) {
1326 hdr_len[1] = arp_hdr_len(skb->dev);
1330 memset(hdr_data, 0, 120);
1331 if ((hdr_field >> 6) & 1) {
1332 hdr = skb_mac_header(skb);
1333 memcpy(hdr_data, hdr, hdr_len[0]);
1337 if ((hdr_field >> 5) & 1) {
1338 hdr = skb_network_header(skb);
1339 memcpy(hdr_data + len, hdr, hdr_len[1]);
1343 if ((hdr_field >> 4) & 1) {
1344 hdr = skb_transport_header(skb);
1345 memcpy(hdr_data + len, hdr, hdr_len[2]);
1352 * create_hdr_descs - create header and header extension descriptors
1353 * @hdr_field - bitfield determining needed headers
1354 * @data - buffer containing header data
1355 * @len - length of data buffer
1356 * @hdr_len - array of individual header lengths
1357 * @scrq_arr - descriptor array
1359 * Creates header and, if needed, header extension descriptors and
1360 * places them in a descriptor array, scrq_arr
1363 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1364 union sub_crq *scrq_arr)
1366 union sub_crq hdr_desc;
1372 while (tmp_len > 0) {
1373 cur = hdr_data + len - tmp_len;
1375 memset(&hdr_desc, 0, sizeof(hdr_desc));
1376 if (cur != hdr_data) {
1377 data = hdr_desc.hdr_ext.data;
1378 tmp = tmp_len > 29 ? 29 : tmp_len;
1379 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1380 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1381 hdr_desc.hdr_ext.len = tmp;
1383 data = hdr_desc.hdr.data;
1384 tmp = tmp_len > 24 ? 24 : tmp_len;
1385 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1386 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1387 hdr_desc.hdr.len = tmp;
1388 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1389 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1390 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1391 hdr_desc.hdr.flag = hdr_field << 1;
1393 memcpy(data, cur, tmp);
1395 *scrq_arr = hdr_desc;
1404 * build_hdr_descs_arr - build a header descriptor array
1405 * @skb - socket buffer
1406 * @num_entries - number of descriptors to be sent
1407 * @subcrq - first TX descriptor
1408 * @hdr_field - bit field determining which headers will be sent
1410 * This function will build a TX descriptor array with applicable
1411 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1414 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1415 int *num_entries, u8 hdr_field)
1417 int hdr_len[3] = {0, 0, 0};
1419 u8 *hdr_data = txbuff->hdr_data;
1421 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1423 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1424 txbuff->indir_arr + 1);
1427 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1428 struct net_device *netdev)
1430 /* For some backing devices, mishandling of small packets
1431 * can result in a loss of connection or TX stall. Device
1432 * architects recommend that no packet should be smaller
1433 * than the minimum MTU value provided to the driver, so
1434 * pad any packets to that length
1436 if (skb->len < netdev->min_mtu)
1437 return skb_put_padto(skb, netdev->min_mtu);
1442 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1444 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1445 int queue_num = skb_get_queue_mapping(skb);
1446 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1447 struct device *dev = &adapter->vdev->dev;
1448 struct ibmvnic_tx_buff *tx_buff = NULL;
1449 struct ibmvnic_sub_crq_queue *tx_scrq;
1450 struct ibmvnic_tx_pool *tx_pool;
1451 unsigned int tx_send_failed = 0;
1452 unsigned int tx_map_failed = 0;
1453 unsigned int tx_dropped = 0;
1454 unsigned int tx_packets = 0;
1455 unsigned int tx_bytes = 0;
1456 dma_addr_t data_dma_addr;
1457 struct netdev_queue *txq;
1458 unsigned long lpar_rc;
1459 union sub_crq tx_crq;
1460 unsigned int offset;
1461 int num_entries = 1;
1468 if (adapter->resetting) {
1469 if (!netif_subqueue_stopped(netdev, skb))
1470 netif_stop_subqueue(netdev, queue_num);
1471 dev_kfree_skb_any(skb);
1479 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1485 if (skb_is_gso(skb))
1486 tx_pool = &adapter->tso_pool[queue_num];
1488 tx_pool = &adapter->tx_pool[queue_num];
1490 tx_scrq = adapter->tx_scrq[queue_num];
1491 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1492 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1493 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1495 index = tx_pool->free_map[tx_pool->consumer_index];
1497 if (index == IBMVNIC_INVALID_MAP) {
1498 dev_kfree_skb_any(skb);
1505 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1507 offset = index * tx_pool->buf_size;
1508 dst = tx_pool->long_term_buff.buff + offset;
1509 memset(dst, 0, tx_pool->buf_size);
1510 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1512 if (skb_shinfo(skb)->nr_frags) {
1516 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1517 cur = skb_headlen(skb);
1519 /* Copy the frags */
1520 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1521 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1524 page_address(skb_frag_page(frag)) +
1525 frag->page_offset, skb_frag_size(frag));
1526 cur += skb_frag_size(frag);
1529 skb_copy_from_linear_data(skb, dst, skb->len);
1532 tx_pool->consumer_index =
1533 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1535 tx_buff = &tx_pool->tx_buff[index];
1537 tx_buff->data_dma[0] = data_dma_addr;
1538 tx_buff->data_len[0] = skb->len;
1539 tx_buff->index = index;
1540 tx_buff->pool_index = queue_num;
1541 tx_buff->last_frag = true;
1543 memset(&tx_crq, 0, sizeof(tx_crq));
1544 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1545 tx_crq.v1.type = IBMVNIC_TX_DESC;
1546 tx_crq.v1.n_crq_elem = 1;
1547 tx_crq.v1.n_sge = 1;
1548 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1550 if (skb_is_gso(skb))
1551 tx_crq.v1.correlator =
1552 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1554 tx_crq.v1.correlator = cpu_to_be32(index);
1555 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1556 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1557 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1559 if (adapter->vlan_header_insertion) {
1560 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1561 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1564 if (skb->protocol == htons(ETH_P_IP)) {
1565 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1566 proto = ip_hdr(skb)->protocol;
1567 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1568 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1569 proto = ipv6_hdr(skb)->nexthdr;
1572 if (proto == IPPROTO_TCP)
1573 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1574 else if (proto == IPPROTO_UDP)
1575 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1577 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1578 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1581 if (skb_is_gso(skb)) {
1582 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1583 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1586 /* determine if l2/3/4 headers are sent to firmware */
1587 if ((*hdrs >> 7) & 1) {
1588 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1589 tx_crq.v1.n_crq_elem = num_entries;
1590 tx_buff->num_entries = num_entries;
1591 tx_buff->indir_arr[0] = tx_crq;
1592 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1593 sizeof(tx_buff->indir_arr),
1595 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1596 dev_kfree_skb_any(skb);
1597 tx_buff->skb = NULL;
1598 if (!firmware_has_feature(FW_FEATURE_CMO))
1599 dev_err(dev, "tx: unable to map descriptor array\n");
1605 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1606 (u64)tx_buff->indir_dma,
1609 tx_buff->num_entries = num_entries;
1610 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1613 if (lpar_rc != H_SUCCESS) {
1614 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1615 dev_kfree_skb_any(skb);
1616 tx_buff->skb = NULL;
1618 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1619 /* Disable TX and report carrier off if queue is closed
1620 * or pending failover.
1621 * Firmware guarantees that a signal will be sent to the
1622 * driver, triggering a reset or some other action.
1624 netif_tx_stop_all_queues(netdev);
1625 netif_carrier_off(netdev);
1634 if (atomic_add_return(num_entries, &tx_scrq->used)
1635 >= adapter->req_tx_entries_per_subcrq) {
1636 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1637 netif_stop_subqueue(netdev, queue_num);
1641 tx_bytes += skb->len;
1642 txq->trans_start = jiffies;
1647 /* roll back consumer index and map array*/
1648 if (tx_pool->consumer_index == 0)
1649 tx_pool->consumer_index =
1650 tx_pool->num_buffers - 1;
1652 tx_pool->consumer_index--;
1653 tx_pool->free_map[tx_pool->consumer_index] = index;
1655 netdev->stats.tx_dropped += tx_dropped;
1656 netdev->stats.tx_bytes += tx_bytes;
1657 netdev->stats.tx_packets += tx_packets;
1658 adapter->tx_send_failed += tx_send_failed;
1659 adapter->tx_map_failed += tx_map_failed;
1660 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1661 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1662 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1667 static void ibmvnic_set_multi(struct net_device *netdev)
1669 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1670 struct netdev_hw_addr *ha;
1671 union ibmvnic_crq crq;
1673 memset(&crq, 0, sizeof(crq));
1674 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1675 crq.request_capability.cmd = REQUEST_CAPABILITY;
1677 if (netdev->flags & IFF_PROMISC) {
1678 if (!adapter->promisc_supported)
1681 if (netdev->flags & IFF_ALLMULTI) {
1682 /* Accept all multicast */
1683 memset(&crq, 0, sizeof(crq));
1684 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1685 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1686 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1687 ibmvnic_send_crq(adapter, &crq);
1688 } else if (netdev_mc_empty(netdev)) {
1689 /* Reject all multicast */
1690 memset(&crq, 0, sizeof(crq));
1691 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1692 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1693 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1694 ibmvnic_send_crq(adapter, &crq);
1696 /* Accept one or more multicast(s) */
1697 netdev_for_each_mc_addr(ha, netdev) {
1698 memset(&crq, 0, sizeof(crq));
1699 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1700 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1701 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1702 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1704 ibmvnic_send_crq(adapter, &crq);
1710 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1712 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1713 struct sockaddr *addr = p;
1714 union ibmvnic_crq crq;
1717 if (!is_valid_ether_addr(addr->sa_data))
1718 return -EADDRNOTAVAIL;
1720 memset(&crq, 0, sizeof(crq));
1721 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1722 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1723 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1725 init_completion(&adapter->fw_done);
1726 rc = ibmvnic_send_crq(adapter, &crq);
1729 wait_for_completion(&adapter->fw_done);
1730 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1731 return adapter->fw_done_rc ? -EIO : 0;
1734 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1736 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1737 struct sockaddr *addr = p;
1740 if (adapter->state == VNIC_PROBED) {
1741 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1742 adapter->mac_change_pending = true;
1746 rc = __ibmvnic_set_mac(netdev, addr);
1752 * do_reset returns zero if we are able to keep processing reset events, or
1753 * non-zero if we hit a fatal error and must halt.
1755 static int do_reset(struct ibmvnic_adapter *adapter,
1756 struct ibmvnic_rwi *rwi, u32 reset_state)
1758 u64 old_num_rx_queues, old_num_tx_queues;
1759 struct net_device *netdev = adapter->netdev;
1762 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1765 netif_carrier_off(netdev);
1766 adapter->reset_reason = rwi->reset_reason;
1768 old_num_rx_queues = adapter->req_rx_queues;
1769 old_num_tx_queues = adapter->req_tx_queues;
1771 ibmvnic_cleanup(netdev);
1773 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1774 adapter->reset_reason != VNIC_RESET_FAILOVER) {
1775 rc = __ibmvnic_close(netdev);
1780 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1781 adapter->wait_for_reset) {
1782 release_resources(adapter);
1783 release_sub_crqs(adapter, 1);
1784 release_crq_queue(adapter);
1787 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1788 /* remove the closed state so when we call open it appears
1789 * we are coming from the probed state.
1791 adapter->state = VNIC_PROBED;
1793 if (adapter->wait_for_reset) {
1794 rc = init_crq_queue(adapter);
1795 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1796 rc = ibmvnic_reenable_crq_queue(adapter);
1797 release_sub_crqs(adapter, 1);
1799 rc = ibmvnic_reset_crq(adapter);
1801 rc = vio_enable_interrupts(adapter->vdev);
1805 netdev_err(adapter->netdev,
1806 "Couldn't initialize crq. rc=%d\n", rc);
1810 rc = ibmvnic_init(adapter);
1812 return IBMVNIC_INIT_FAILED;
1814 /* If the adapter was in PROBE state prior to the reset,
1817 if (reset_state == VNIC_PROBED)
1820 rc = ibmvnic_login(netdev);
1822 adapter->state = VNIC_PROBED;
1826 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1827 adapter->wait_for_reset) {
1828 rc = init_resources(adapter);
1831 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1832 adapter->req_tx_queues != old_num_tx_queues) {
1833 adapter->map_id = 1;
1834 release_rx_pools(adapter);
1835 release_tx_pools(adapter);
1836 init_rx_pools(netdev);
1837 init_tx_pools(netdev);
1839 release_napi(adapter);
1842 rc = reset_tx_pools(adapter);
1846 rc = reset_rx_pools(adapter);
1850 ibmvnic_disable_irqs(adapter);
1852 adapter->state = VNIC_CLOSED;
1854 if (reset_state == VNIC_CLOSED)
1857 rc = __ibmvnic_open(netdev);
1859 if (list_empty(&adapter->rwi_list))
1860 adapter->state = VNIC_CLOSED;
1862 adapter->state = reset_state;
1868 for (i = 0; i < adapter->req_rx_queues; i++)
1869 napi_schedule(&adapter->napi[i]);
1871 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1872 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1873 netdev_notify_peers(netdev);
1875 netif_carrier_on(netdev);
1880 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1882 struct ibmvnic_rwi *rwi;
1884 mutex_lock(&adapter->rwi_lock);
1886 if (!list_empty(&adapter->rwi_list)) {
1887 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1889 list_del(&rwi->list);
1894 mutex_unlock(&adapter->rwi_lock);
1898 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1900 struct ibmvnic_rwi *rwi;
1902 rwi = get_next_rwi(adapter);
1905 rwi = get_next_rwi(adapter);
1909 static void __ibmvnic_reset(struct work_struct *work)
1911 struct ibmvnic_rwi *rwi;
1912 struct ibmvnic_adapter *adapter;
1913 struct net_device *netdev;
1917 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1918 netdev = adapter->netdev;
1920 mutex_lock(&adapter->reset_lock);
1921 adapter->resetting = true;
1922 reset_state = adapter->state;
1924 rwi = get_next_rwi(adapter);
1926 rc = do_reset(adapter, rwi, reset_state);
1928 if (rc && rc != IBMVNIC_INIT_FAILED)
1931 rwi = get_next_rwi(adapter);
1934 if (adapter->wait_for_reset) {
1935 adapter->wait_for_reset = false;
1936 adapter->reset_done_rc = rc;
1937 complete(&adapter->reset_done);
1941 netdev_dbg(adapter->netdev, "Reset failed\n");
1942 free_all_rwi(adapter);
1943 mutex_unlock(&adapter->reset_lock);
1947 adapter->resetting = false;
1948 mutex_unlock(&adapter->reset_lock);
1951 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
1952 enum ibmvnic_reset_reason reason)
1954 struct ibmvnic_rwi *rwi, *tmp;
1955 struct net_device *netdev = adapter->netdev;
1956 struct list_head *entry;
1959 if (adapter->state == VNIC_REMOVING ||
1960 adapter->state == VNIC_REMOVED ||
1961 adapter->failover_pending) {
1963 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
1967 if (adapter->state == VNIC_PROBING) {
1968 netdev_warn(netdev, "Adapter reset during probe\n");
1969 ret = adapter->init_done_rc = EAGAIN;
1973 mutex_lock(&adapter->rwi_lock);
1975 list_for_each(entry, &adapter->rwi_list) {
1976 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1977 if (tmp->reset_reason == reason) {
1978 netdev_dbg(netdev, "Skipping matching reset\n");
1979 mutex_unlock(&adapter->rwi_lock);
1985 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1987 mutex_unlock(&adapter->rwi_lock);
1988 ibmvnic_close(netdev);
1993 rwi->reset_reason = reason;
1994 list_add_tail(&rwi->list, &adapter->rwi_list);
1995 mutex_unlock(&adapter->rwi_lock);
1997 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1998 schedule_work(&adapter->ibmvnic_reset);
2002 if (adapter->wait_for_reset)
2003 adapter->wait_for_reset = false;
2007 static void ibmvnic_tx_timeout(struct net_device *dev)
2009 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2011 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2014 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2015 struct ibmvnic_rx_buff *rx_buff)
2017 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2019 rx_buff->skb = NULL;
2021 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2022 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2024 atomic_dec(&pool->available);
2027 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2029 struct net_device *netdev = napi->dev;
2030 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2031 int scrq_num = (int)(napi - adapter->napi);
2032 int frames_processed = 0;
2035 while (frames_processed < budget) {
2036 struct sk_buff *skb;
2037 struct ibmvnic_rx_buff *rx_buff;
2038 union sub_crq *next;
2043 if (unlikely(adapter->resetting &&
2044 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2045 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2046 napi_complete_done(napi, frames_processed);
2047 return frames_processed;
2050 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2052 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2054 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2055 rx_comp.correlator);
2056 /* do error checking */
2057 if (next->rx_comp.rc) {
2058 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2059 be16_to_cpu(next->rx_comp.rc));
2060 /* free the entry */
2061 next->rx_comp.first = 0;
2062 dev_kfree_skb_any(rx_buff->skb);
2063 remove_buff_from_pool(adapter, rx_buff);
2065 } else if (!rx_buff->skb) {
2066 /* free the entry */
2067 next->rx_comp.first = 0;
2068 remove_buff_from_pool(adapter, rx_buff);
2072 length = be32_to_cpu(next->rx_comp.len);
2073 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2074 flags = next->rx_comp.flags;
2076 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2079 /* VLAN Header has been stripped by the system firmware and
2080 * needs to be inserted by the driver
2082 if (adapter->rx_vlan_header_insertion &&
2083 (flags & IBMVNIC_VLAN_STRIPPED))
2084 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2085 ntohs(next->rx_comp.vlan_tci));
2087 /* free the entry */
2088 next->rx_comp.first = 0;
2089 remove_buff_from_pool(adapter, rx_buff);
2091 skb_put(skb, length);
2092 skb->protocol = eth_type_trans(skb, netdev);
2093 skb_record_rx_queue(skb, scrq_num);
2095 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2096 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2097 skb->ip_summed = CHECKSUM_UNNECESSARY;
2101 napi_gro_receive(napi, skb); /* send it up */
2102 netdev->stats.rx_packets++;
2103 netdev->stats.rx_bytes += length;
2104 adapter->rx_stats_buffers[scrq_num].packets++;
2105 adapter->rx_stats_buffers[scrq_num].bytes += length;
2109 if (adapter->state != VNIC_CLOSING)
2110 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2112 if (frames_processed < budget) {
2113 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2114 napi_complete_done(napi, frames_processed);
2115 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2116 napi_reschedule(napi)) {
2117 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2121 return frames_processed;
2124 #ifdef CONFIG_NET_POLL_CONTROLLER
2125 static void ibmvnic_netpoll_controller(struct net_device *dev)
2127 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2130 replenish_pools(netdev_priv(dev));
2131 for (i = 0; i < adapter->req_rx_queues; i++)
2132 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2133 adapter->rx_scrq[i]);
2137 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2141 adapter->fallback.mtu = adapter->req_mtu;
2142 adapter->fallback.rx_queues = adapter->req_rx_queues;
2143 adapter->fallback.tx_queues = adapter->req_tx_queues;
2144 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2145 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2147 init_completion(&adapter->reset_done);
2148 adapter->wait_for_reset = true;
2149 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2152 wait_for_completion(&adapter->reset_done);
2155 if (adapter->reset_done_rc) {
2157 adapter->desired.mtu = adapter->fallback.mtu;
2158 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2159 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2160 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2161 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2163 init_completion(&adapter->reset_done);
2164 adapter->wait_for_reset = true;
2165 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2168 wait_for_completion(&adapter->reset_done);
2170 adapter->wait_for_reset = false;
2175 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2177 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2179 adapter->desired.mtu = new_mtu + ETH_HLEN;
2181 return wait_for_reset(adapter);
2184 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2185 struct net_device *dev,
2186 netdev_features_t features)
2188 /* Some backing hardware adapters can not
2189 * handle packets with a MSS less than 224
2190 * or with only one segment.
2192 if (skb_is_gso(skb)) {
2193 if (skb_shinfo(skb)->gso_size < 224 ||
2194 skb_shinfo(skb)->gso_segs == 1)
2195 features &= ~NETIF_F_GSO_MASK;
2201 static const struct net_device_ops ibmvnic_netdev_ops = {
2202 .ndo_open = ibmvnic_open,
2203 .ndo_stop = ibmvnic_close,
2204 .ndo_start_xmit = ibmvnic_xmit,
2205 .ndo_set_rx_mode = ibmvnic_set_multi,
2206 .ndo_set_mac_address = ibmvnic_set_mac,
2207 .ndo_validate_addr = eth_validate_addr,
2208 .ndo_tx_timeout = ibmvnic_tx_timeout,
2209 #ifdef CONFIG_NET_POLL_CONTROLLER
2210 .ndo_poll_controller = ibmvnic_netpoll_controller,
2212 .ndo_change_mtu = ibmvnic_change_mtu,
2213 .ndo_features_check = ibmvnic_features_check,
2216 /* ethtool functions */
2218 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2219 struct ethtool_link_ksettings *cmd)
2221 u32 supported, advertising;
2223 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2225 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2227 cmd->base.speed = SPEED_1000;
2228 cmd->base.duplex = DUPLEX_FULL;
2229 cmd->base.port = PORT_FIBRE;
2230 cmd->base.phy_address = 0;
2231 cmd->base.autoneg = AUTONEG_ENABLE;
2233 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2235 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2241 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2242 struct ethtool_drvinfo *info)
2244 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2246 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2247 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2248 strlcpy(info->fw_version, adapter->fw_version,
2249 sizeof(info->fw_version));
2252 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2254 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2256 return adapter->msg_enable;
2259 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2261 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2263 adapter->msg_enable = data;
2266 static u32 ibmvnic_get_link(struct net_device *netdev)
2268 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2270 /* Don't need to send a query because we request a logical link up at
2271 * init and then we wait for link state indications
2273 return adapter->logical_link_state;
2276 static void ibmvnic_get_ringparam(struct net_device *netdev,
2277 struct ethtool_ringparam *ring)
2279 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2281 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2282 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2283 ring->rx_mini_max_pending = 0;
2284 ring->rx_jumbo_max_pending = 0;
2285 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2286 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2287 ring->rx_mini_pending = 0;
2288 ring->rx_jumbo_pending = 0;
2291 static int ibmvnic_set_ringparam(struct net_device *netdev,
2292 struct ethtool_ringparam *ring)
2294 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2296 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2297 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2298 netdev_err(netdev, "Invalid request.\n");
2299 netdev_err(netdev, "Max tx buffers = %llu\n",
2300 adapter->max_rx_add_entries_per_subcrq);
2301 netdev_err(netdev, "Max rx buffers = %llu\n",
2302 adapter->max_tx_entries_per_subcrq);
2306 adapter->desired.rx_entries = ring->rx_pending;
2307 adapter->desired.tx_entries = ring->tx_pending;
2309 return wait_for_reset(adapter);
2312 static void ibmvnic_get_channels(struct net_device *netdev,
2313 struct ethtool_channels *channels)
2315 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2317 channels->max_rx = adapter->max_rx_queues;
2318 channels->max_tx = adapter->max_tx_queues;
2319 channels->max_other = 0;
2320 channels->max_combined = 0;
2321 channels->rx_count = adapter->req_rx_queues;
2322 channels->tx_count = adapter->req_tx_queues;
2323 channels->other_count = 0;
2324 channels->combined_count = 0;
2327 static int ibmvnic_set_channels(struct net_device *netdev,
2328 struct ethtool_channels *channels)
2330 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2332 adapter->desired.rx_queues = channels->rx_count;
2333 adapter->desired.tx_queues = channels->tx_count;
2335 return wait_for_reset(adapter);
2338 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2340 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2343 if (stringset != ETH_SS_STATS)
2346 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2347 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2349 for (i = 0; i < adapter->req_tx_queues; i++) {
2350 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2351 data += ETH_GSTRING_LEN;
2353 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2354 data += ETH_GSTRING_LEN;
2356 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2357 data += ETH_GSTRING_LEN;
2360 for (i = 0; i < adapter->req_rx_queues; i++) {
2361 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2362 data += ETH_GSTRING_LEN;
2364 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2365 data += ETH_GSTRING_LEN;
2367 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2368 data += ETH_GSTRING_LEN;
2372 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2374 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2378 return ARRAY_SIZE(ibmvnic_stats) +
2379 adapter->req_tx_queues * NUM_TX_STATS +
2380 adapter->req_rx_queues * NUM_RX_STATS;
2386 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2387 struct ethtool_stats *stats, u64 *data)
2389 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2390 union ibmvnic_crq crq;
2394 memset(&crq, 0, sizeof(crq));
2395 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2396 crq.request_statistics.cmd = REQUEST_STATISTICS;
2397 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2398 crq.request_statistics.len =
2399 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2401 /* Wait for data to be written */
2402 init_completion(&adapter->stats_done);
2403 rc = ibmvnic_send_crq(adapter, &crq);
2406 wait_for_completion(&adapter->stats_done);
2408 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2409 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2410 ibmvnic_stats[i].offset));
2412 for (j = 0; j < adapter->req_tx_queues; j++) {
2413 data[i] = adapter->tx_stats_buffers[j].packets;
2415 data[i] = adapter->tx_stats_buffers[j].bytes;
2417 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2421 for (j = 0; j < adapter->req_rx_queues; j++) {
2422 data[i] = adapter->rx_stats_buffers[j].packets;
2424 data[i] = adapter->rx_stats_buffers[j].bytes;
2426 data[i] = adapter->rx_stats_buffers[j].interrupts;
2431 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2432 .get_drvinfo = ibmvnic_get_drvinfo,
2433 .get_msglevel = ibmvnic_get_msglevel,
2434 .set_msglevel = ibmvnic_set_msglevel,
2435 .get_link = ibmvnic_get_link,
2436 .get_ringparam = ibmvnic_get_ringparam,
2437 .set_ringparam = ibmvnic_set_ringparam,
2438 .get_channels = ibmvnic_get_channels,
2439 .set_channels = ibmvnic_set_channels,
2440 .get_strings = ibmvnic_get_strings,
2441 .get_sset_count = ibmvnic_get_sset_count,
2442 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2443 .get_link_ksettings = ibmvnic_get_link_ksettings,
2446 /* Routines for managing CRQs/sCRQs */
2448 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2449 struct ibmvnic_sub_crq_queue *scrq)
2454 free_irq(scrq->irq, scrq);
2455 irq_dispose_mapping(scrq->irq);
2459 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2460 atomic_set(&scrq->used, 0);
2463 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2464 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2468 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2472 for (i = 0; i < adapter->req_tx_queues; i++) {
2473 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2474 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2479 for (i = 0; i < adapter->req_rx_queues; i++) {
2480 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2481 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2489 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2490 struct ibmvnic_sub_crq_queue *scrq,
2493 struct device *dev = &adapter->vdev->dev;
2496 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2499 /* Close the sub-crqs */
2501 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2502 adapter->vdev->unit_address,
2504 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2507 netdev_err(adapter->netdev,
2508 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2513 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2515 free_pages((unsigned long)scrq->msgs, 2);
2519 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2522 struct device *dev = &adapter->vdev->dev;
2523 struct ibmvnic_sub_crq_queue *scrq;
2526 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2531 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2533 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2534 goto zero_page_failed;
2537 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2539 if (dma_mapping_error(dev, scrq->msg_token)) {
2540 dev_warn(dev, "Couldn't map crq queue messages page\n");
2544 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2545 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2547 if (rc == H_RESOURCE)
2548 rc = ibmvnic_reset_crq(adapter);
2550 if (rc == H_CLOSED) {
2551 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2553 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2557 scrq->adapter = adapter;
2558 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2559 spin_lock_init(&scrq->lock);
2561 netdev_dbg(adapter->netdev,
2562 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2563 scrq->crq_num, scrq->hw_irq, scrq->irq);
2568 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2571 free_pages((unsigned long)scrq->msgs, 2);
2578 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2582 if (adapter->tx_scrq) {
2583 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2584 if (!adapter->tx_scrq[i])
2587 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2589 if (adapter->tx_scrq[i]->irq) {
2590 free_irq(adapter->tx_scrq[i]->irq,
2591 adapter->tx_scrq[i]);
2592 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2593 adapter->tx_scrq[i]->irq = 0;
2596 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2600 kfree(adapter->tx_scrq);
2601 adapter->tx_scrq = NULL;
2602 adapter->num_active_tx_scrqs = 0;
2605 if (adapter->rx_scrq) {
2606 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2607 if (!adapter->rx_scrq[i])
2610 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2612 if (adapter->rx_scrq[i]->irq) {
2613 free_irq(adapter->rx_scrq[i]->irq,
2614 adapter->rx_scrq[i]);
2615 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2616 adapter->rx_scrq[i]->irq = 0;
2619 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2623 kfree(adapter->rx_scrq);
2624 adapter->rx_scrq = NULL;
2625 adapter->num_active_rx_scrqs = 0;
2629 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2630 struct ibmvnic_sub_crq_queue *scrq)
2632 struct device *dev = &adapter->vdev->dev;
2635 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2636 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2638 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2643 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2644 struct ibmvnic_sub_crq_queue *scrq)
2646 struct device *dev = &adapter->vdev->dev;
2650 if (scrq->hw_irq > 0x100000000ULL) {
2651 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2655 val = (0xff000000) | scrq->hw_irq;
2656 rc = plpar_hcall_norets(H_EOI, val);
2658 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2661 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2662 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2664 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2669 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2670 struct ibmvnic_sub_crq_queue *scrq)
2672 struct device *dev = &adapter->vdev->dev;
2673 struct ibmvnic_tx_pool *tx_pool;
2674 struct ibmvnic_tx_buff *txbuff;
2675 union sub_crq *next;
2681 while (pending_scrq(adapter, scrq)) {
2682 unsigned int pool = scrq->pool_index;
2683 int num_entries = 0;
2685 next = ibmvnic_next_scrq(adapter, scrq);
2686 for (i = 0; i < next->tx_comp.num_comps; i++) {
2687 if (next->tx_comp.rcs[i]) {
2688 dev_err(dev, "tx error %x\n",
2689 next->tx_comp.rcs[i]);
2692 index = be32_to_cpu(next->tx_comp.correlators[i]);
2693 if (index & IBMVNIC_TSO_POOL_MASK) {
2694 tx_pool = &adapter->tso_pool[pool];
2695 index &= ~IBMVNIC_TSO_POOL_MASK;
2697 tx_pool = &adapter->tx_pool[pool];
2700 txbuff = &tx_pool->tx_buff[index];
2702 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2703 if (!txbuff->data_dma[j])
2706 txbuff->data_dma[j] = 0;
2708 /* if sub_crq was sent indirectly */
2709 first = &txbuff->indir_arr[0].generic.first;
2710 if (*first == IBMVNIC_CRQ_CMD) {
2711 dma_unmap_single(dev, txbuff->indir_dma,
2712 sizeof(txbuff->indir_arr),
2717 if (txbuff->last_frag) {
2718 dev_kfree_skb_any(txbuff->skb);
2722 num_entries += txbuff->num_entries;
2724 tx_pool->free_map[tx_pool->producer_index] = index;
2725 tx_pool->producer_index =
2726 (tx_pool->producer_index + 1) %
2727 tx_pool->num_buffers;
2729 /* remove tx_comp scrq*/
2730 next->tx_comp.first = 0;
2732 if (atomic_sub_return(num_entries, &scrq->used) <=
2733 (adapter->req_tx_entries_per_subcrq / 2) &&
2734 __netif_subqueue_stopped(adapter->netdev,
2735 scrq->pool_index)) {
2736 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2737 netdev_dbg(adapter->netdev, "Started queue %d\n",
2742 enable_scrq_irq(adapter, scrq);
2744 if (pending_scrq(adapter, scrq)) {
2745 disable_scrq_irq(adapter, scrq);
2752 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2754 struct ibmvnic_sub_crq_queue *scrq = instance;
2755 struct ibmvnic_adapter *adapter = scrq->adapter;
2757 disable_scrq_irq(adapter, scrq);
2758 ibmvnic_complete_tx(adapter, scrq);
2763 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2765 struct ibmvnic_sub_crq_queue *scrq = instance;
2766 struct ibmvnic_adapter *adapter = scrq->adapter;
2768 /* When booting a kdump kernel we can hit pending interrupts
2769 * prior to completing driver initialization.
2771 if (unlikely(adapter->state != VNIC_OPEN))
2774 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2776 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2777 disable_scrq_irq(adapter, scrq);
2778 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2784 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2786 struct device *dev = &adapter->vdev->dev;
2787 struct ibmvnic_sub_crq_queue *scrq;
2791 for (i = 0; i < adapter->req_tx_queues; i++) {
2792 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2794 scrq = adapter->tx_scrq[i];
2795 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2799 dev_err(dev, "Error mapping irq\n");
2800 goto req_tx_irq_failed;
2803 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2804 0, "ibmvnic_tx", scrq);
2807 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2809 irq_dispose_mapping(scrq->irq);
2810 goto req_tx_irq_failed;
2814 for (i = 0; i < adapter->req_rx_queues; i++) {
2815 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2817 scrq = adapter->rx_scrq[i];
2818 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2821 dev_err(dev, "Error mapping irq\n");
2822 goto req_rx_irq_failed;
2824 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2825 0, "ibmvnic_rx", scrq);
2827 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2829 irq_dispose_mapping(scrq->irq);
2830 goto req_rx_irq_failed;
2836 for (j = 0; j < i; j++) {
2837 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2838 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2840 i = adapter->req_tx_queues;
2842 for (j = 0; j < i; j++) {
2843 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2844 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2846 release_sub_crqs(adapter, 1);
2850 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2852 struct device *dev = &adapter->vdev->dev;
2853 struct ibmvnic_sub_crq_queue **allqueues;
2854 int registered_queues = 0;
2859 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2861 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2865 for (i = 0; i < total_queues; i++) {
2866 allqueues[i] = init_sub_crq_queue(adapter);
2867 if (!allqueues[i]) {
2868 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2871 registered_queues++;
2874 /* Make sure we were able to register the minimum number of queues */
2875 if (registered_queues <
2876 adapter->min_tx_queues + adapter->min_rx_queues) {
2877 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2881 /* Distribute the failed allocated queues*/
2882 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2883 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2886 if (adapter->req_rx_queues > adapter->min_rx_queues)
2887 adapter->req_rx_queues--;
2892 if (adapter->req_tx_queues > adapter->min_tx_queues)
2893 adapter->req_tx_queues--;
2900 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2901 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2902 if (!adapter->tx_scrq)
2905 for (i = 0; i < adapter->req_tx_queues; i++) {
2906 adapter->tx_scrq[i] = allqueues[i];
2907 adapter->tx_scrq[i]->pool_index = i;
2908 adapter->num_active_tx_scrqs++;
2911 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2912 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2913 if (!adapter->rx_scrq)
2916 for (i = 0; i < adapter->req_rx_queues; i++) {
2917 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2918 adapter->rx_scrq[i]->scrq_num = i;
2919 adapter->num_active_rx_scrqs++;
2926 kfree(adapter->tx_scrq);
2927 adapter->tx_scrq = NULL;
2929 for (i = 0; i < registered_queues; i++)
2930 release_sub_crq_queue(adapter, allqueues[i], 1);
2935 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2937 struct device *dev = &adapter->vdev->dev;
2938 union ibmvnic_crq crq;
2942 /* Sub-CRQ entries are 32 byte long */
2943 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2945 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2946 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2947 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2951 if (adapter->desired.mtu)
2952 adapter->req_mtu = adapter->desired.mtu;
2954 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2956 if (!adapter->desired.tx_entries)
2957 adapter->desired.tx_entries =
2958 adapter->max_tx_entries_per_subcrq;
2959 if (!adapter->desired.rx_entries)
2960 adapter->desired.rx_entries =
2961 adapter->max_rx_add_entries_per_subcrq;
2963 max_entries = IBMVNIC_MAX_LTB_SIZE /
2964 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2966 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2967 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2968 adapter->desired.tx_entries = max_entries;
2971 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2972 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2973 adapter->desired.rx_entries = max_entries;
2976 if (adapter->desired.tx_entries)
2977 adapter->req_tx_entries_per_subcrq =
2978 adapter->desired.tx_entries;
2980 adapter->req_tx_entries_per_subcrq =
2981 adapter->max_tx_entries_per_subcrq;
2983 if (adapter->desired.rx_entries)
2984 adapter->req_rx_add_entries_per_subcrq =
2985 adapter->desired.rx_entries;
2987 adapter->req_rx_add_entries_per_subcrq =
2988 adapter->max_rx_add_entries_per_subcrq;
2990 if (adapter->desired.tx_queues)
2991 adapter->req_tx_queues =
2992 adapter->desired.tx_queues;
2994 adapter->req_tx_queues =
2995 adapter->opt_tx_comp_sub_queues;
2997 if (adapter->desired.rx_queues)
2998 adapter->req_rx_queues =
2999 adapter->desired.rx_queues;
3001 adapter->req_rx_queues =
3002 adapter->opt_rx_comp_queues;
3004 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3007 memset(&crq, 0, sizeof(crq));
3008 crq.request_capability.first = IBMVNIC_CRQ_CMD;
3009 crq.request_capability.cmd = REQUEST_CAPABILITY;
3011 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3012 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3013 atomic_inc(&adapter->running_cap_crqs);
3014 ibmvnic_send_crq(adapter, &crq);
3016 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3017 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3018 atomic_inc(&adapter->running_cap_crqs);
3019 ibmvnic_send_crq(adapter, &crq);
3021 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3022 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3023 atomic_inc(&adapter->running_cap_crqs);
3024 ibmvnic_send_crq(adapter, &crq);
3026 crq.request_capability.capability =
3027 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3028 crq.request_capability.number =
3029 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3030 atomic_inc(&adapter->running_cap_crqs);
3031 ibmvnic_send_crq(adapter, &crq);
3033 crq.request_capability.capability =
3034 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3035 crq.request_capability.number =
3036 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3037 atomic_inc(&adapter->running_cap_crqs);
3038 ibmvnic_send_crq(adapter, &crq);
3040 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3041 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3042 atomic_inc(&adapter->running_cap_crqs);
3043 ibmvnic_send_crq(adapter, &crq);
3045 if (adapter->netdev->flags & IFF_PROMISC) {
3046 if (adapter->promisc_supported) {
3047 crq.request_capability.capability =
3048 cpu_to_be16(PROMISC_REQUESTED);
3049 crq.request_capability.number = cpu_to_be64(1);
3050 atomic_inc(&adapter->running_cap_crqs);
3051 ibmvnic_send_crq(adapter, &crq);
3054 crq.request_capability.capability =
3055 cpu_to_be16(PROMISC_REQUESTED);
3056 crq.request_capability.number = cpu_to_be64(0);
3057 atomic_inc(&adapter->running_cap_crqs);
3058 ibmvnic_send_crq(adapter, &crq);
3062 static int pending_scrq(struct ibmvnic_adapter *adapter,
3063 struct ibmvnic_sub_crq_queue *scrq)
3065 union sub_crq *entry = &scrq->msgs[scrq->cur];
3067 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3073 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3074 struct ibmvnic_sub_crq_queue *scrq)
3076 union sub_crq *entry;
3077 unsigned long flags;
3079 spin_lock_irqsave(&scrq->lock, flags);
3080 entry = &scrq->msgs[scrq->cur];
3081 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3082 if (++scrq->cur == scrq->size)
3087 spin_unlock_irqrestore(&scrq->lock, flags);
3092 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3094 struct ibmvnic_crq_queue *queue = &adapter->crq;
3095 union ibmvnic_crq *crq;
3097 crq = &queue->msgs[queue->cur];
3098 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3099 if (++queue->cur == queue->size)
3108 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3109 union sub_crq *sub_crq)
3111 unsigned int ua = adapter->vdev->unit_address;
3112 struct device *dev = &adapter->vdev->dev;
3113 u64 *u64_crq = (u64 *)sub_crq;
3116 netdev_dbg(adapter->netdev,
3117 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3118 (unsigned long int)cpu_to_be64(remote_handle),
3119 (unsigned long int)cpu_to_be64(u64_crq[0]),
3120 (unsigned long int)cpu_to_be64(u64_crq[1]),
3121 (unsigned long int)cpu_to_be64(u64_crq[2]),
3122 (unsigned long int)cpu_to_be64(u64_crq[3]));
3124 /* Make sure the hypervisor sees the complete request */
3127 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3128 cpu_to_be64(remote_handle),
3129 cpu_to_be64(u64_crq[0]),
3130 cpu_to_be64(u64_crq[1]),
3131 cpu_to_be64(u64_crq[2]),
3132 cpu_to_be64(u64_crq[3]));
3136 dev_warn(dev, "CRQ Queue closed\n");
3137 dev_err(dev, "Send error (rc=%d)\n", rc);
3143 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3144 u64 remote_handle, u64 ioba, u64 num_entries)
3146 unsigned int ua = adapter->vdev->unit_address;
3147 struct device *dev = &adapter->vdev->dev;
3150 /* Make sure the hypervisor sees the complete request */
3152 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3153 cpu_to_be64(remote_handle),
3158 dev_warn(dev, "CRQ Queue closed\n");
3159 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3165 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3166 union ibmvnic_crq *crq)
3168 unsigned int ua = adapter->vdev->unit_address;
3169 struct device *dev = &adapter->vdev->dev;
3170 u64 *u64_crq = (u64 *)crq;
3173 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3174 (unsigned long int)cpu_to_be64(u64_crq[0]),
3175 (unsigned long int)cpu_to_be64(u64_crq[1]));
3177 if (!adapter->crq.active &&
3178 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3179 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3183 /* Make sure the hypervisor sees the complete request */
3186 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3187 cpu_to_be64(u64_crq[0]),
3188 cpu_to_be64(u64_crq[1]));
3191 if (rc == H_CLOSED) {
3192 dev_warn(dev, "CRQ Queue closed\n");
3193 if (adapter->resetting)
3194 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3197 dev_warn(dev, "Send error (rc=%d)\n", rc);
3203 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3205 union ibmvnic_crq crq;
3207 memset(&crq, 0, sizeof(crq));
3208 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3209 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3210 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3212 return ibmvnic_send_crq(adapter, &crq);
3215 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3217 union ibmvnic_crq crq;
3219 memset(&crq, 0, sizeof(crq));
3220 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3221 crq.version_exchange.cmd = VERSION_EXCHANGE;
3222 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3224 return ibmvnic_send_crq(adapter, &crq);
3227 struct vnic_login_client_data {
3233 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3237 /* Calculate the amount of buffer space needed for the
3238 * vnic client data in the login buffer. There are four entries,
3239 * OS name, LPAR name, device name, and a null last entry.
3241 len = 4 * sizeof(struct vnic_login_client_data);
3242 len += 6; /* "Linux" plus NULL */
3243 len += strlen(utsname()->nodename) + 1;
3244 len += strlen(adapter->netdev->name) + 1;
3249 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3250 struct vnic_login_client_data *vlcd)
3252 const char *os_name = "Linux";
3255 /* Type 1 - LPAR OS */
3257 len = strlen(os_name) + 1;
3258 vlcd->len = cpu_to_be16(len);
3259 strncpy(vlcd->name, os_name, len);
3260 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3262 /* Type 2 - LPAR name */
3264 len = strlen(utsname()->nodename) + 1;
3265 vlcd->len = cpu_to_be16(len);
3266 strncpy(vlcd->name, utsname()->nodename, len);
3267 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3269 /* Type 3 - device name */
3271 len = strlen(adapter->netdev->name) + 1;
3272 vlcd->len = cpu_to_be16(len);
3273 strncpy(vlcd->name, adapter->netdev->name, len);
3276 static int send_login(struct ibmvnic_adapter *adapter)
3278 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3279 struct ibmvnic_login_buffer *login_buffer;
3280 struct device *dev = &adapter->vdev->dev;
3281 dma_addr_t rsp_buffer_token;
3282 dma_addr_t buffer_token;
3283 size_t rsp_buffer_size;
3284 union ibmvnic_crq crq;
3288 int client_data_len;
3289 struct vnic_login_client_data *vlcd;
3292 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3293 netdev_err(adapter->netdev,
3294 "RX or TX queues are not allocated, device login failed\n");
3298 release_login_rsp_buffer(adapter);
3299 client_data_len = vnic_client_data_len(adapter);
3302 sizeof(struct ibmvnic_login_buffer) +
3303 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3306 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3308 goto buf_alloc_failed;
3310 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3312 if (dma_mapping_error(dev, buffer_token)) {
3313 dev_err(dev, "Couldn't map login buffer\n");
3314 goto buf_map_failed;
3317 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3318 sizeof(u64) * adapter->req_tx_queues +
3319 sizeof(u64) * adapter->req_rx_queues +
3320 sizeof(u64) * adapter->req_rx_queues +
3321 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3323 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3324 if (!login_rsp_buffer)
3325 goto buf_rsp_alloc_failed;
3327 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3328 rsp_buffer_size, DMA_FROM_DEVICE);
3329 if (dma_mapping_error(dev, rsp_buffer_token)) {
3330 dev_err(dev, "Couldn't map login rsp buffer\n");
3331 goto buf_rsp_map_failed;
3334 adapter->login_buf = login_buffer;
3335 adapter->login_buf_token = buffer_token;
3336 adapter->login_buf_sz = buffer_size;
3337 adapter->login_rsp_buf = login_rsp_buffer;
3338 adapter->login_rsp_buf_token = rsp_buffer_token;
3339 adapter->login_rsp_buf_sz = rsp_buffer_size;
3341 login_buffer->len = cpu_to_be32(buffer_size);
3342 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3343 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3344 login_buffer->off_txcomp_subcrqs =
3345 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3346 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3347 login_buffer->off_rxcomp_subcrqs =
3348 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3349 sizeof(u64) * adapter->req_tx_queues);
3350 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3351 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3353 tx_list_p = (__be64 *)((char *)login_buffer +
3354 sizeof(struct ibmvnic_login_buffer));
3355 rx_list_p = (__be64 *)((char *)login_buffer +
3356 sizeof(struct ibmvnic_login_buffer) +
3357 sizeof(u64) * adapter->req_tx_queues);
3359 for (i = 0; i < adapter->req_tx_queues; i++) {
3360 if (adapter->tx_scrq[i]) {
3361 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3366 for (i = 0; i < adapter->req_rx_queues; i++) {
3367 if (adapter->rx_scrq[i]) {
3368 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3373 /* Insert vNIC login client data */
3374 vlcd = (struct vnic_login_client_data *)
3375 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3376 login_buffer->client_data_offset =
3377 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3378 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3380 vnic_add_client_data(adapter, vlcd);
3382 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3383 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3384 netdev_dbg(adapter->netdev, "%016lx\n",
3385 ((unsigned long int *)(adapter->login_buf))[i]);
3388 memset(&crq, 0, sizeof(crq));
3389 crq.login.first = IBMVNIC_CRQ_CMD;
3390 crq.login.cmd = LOGIN;
3391 crq.login.ioba = cpu_to_be32(buffer_token);
3392 crq.login.len = cpu_to_be32(buffer_size);
3393 ibmvnic_send_crq(adapter, &crq);
3398 kfree(login_rsp_buffer);
3399 buf_rsp_alloc_failed:
3400 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3402 kfree(login_buffer);
3407 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3410 union ibmvnic_crq crq;
3412 memset(&crq, 0, sizeof(crq));
3413 crq.request_map.first = IBMVNIC_CRQ_CMD;
3414 crq.request_map.cmd = REQUEST_MAP;
3415 crq.request_map.map_id = map_id;
3416 crq.request_map.ioba = cpu_to_be32(addr);
3417 crq.request_map.len = cpu_to_be32(len);
3418 return ibmvnic_send_crq(adapter, &crq);
3421 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3423 union ibmvnic_crq crq;
3425 memset(&crq, 0, sizeof(crq));
3426 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3427 crq.request_unmap.cmd = REQUEST_UNMAP;
3428 crq.request_unmap.map_id = map_id;
3429 return ibmvnic_send_crq(adapter, &crq);
3432 static void send_map_query(struct ibmvnic_adapter *adapter)
3434 union ibmvnic_crq crq;
3436 memset(&crq, 0, sizeof(crq));
3437 crq.query_map.first = IBMVNIC_CRQ_CMD;
3438 crq.query_map.cmd = QUERY_MAP;
3439 ibmvnic_send_crq(adapter, &crq);
3442 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3443 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3445 union ibmvnic_crq crq;
3447 atomic_set(&adapter->running_cap_crqs, 0);
3448 memset(&crq, 0, sizeof(crq));
3449 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3450 crq.query_capability.cmd = QUERY_CAPABILITY;
3452 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3453 atomic_inc(&adapter->running_cap_crqs);
3454 ibmvnic_send_crq(adapter, &crq);
3456 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3457 atomic_inc(&adapter->running_cap_crqs);
3458 ibmvnic_send_crq(adapter, &crq);
3460 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3461 atomic_inc(&adapter->running_cap_crqs);
3462 ibmvnic_send_crq(adapter, &crq);
3464 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3465 atomic_inc(&adapter->running_cap_crqs);
3466 ibmvnic_send_crq(adapter, &crq);
3468 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3469 atomic_inc(&adapter->running_cap_crqs);
3470 ibmvnic_send_crq(adapter, &crq);
3472 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3473 atomic_inc(&adapter->running_cap_crqs);
3474 ibmvnic_send_crq(adapter, &crq);
3476 crq.query_capability.capability =
3477 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3478 atomic_inc(&adapter->running_cap_crqs);
3479 ibmvnic_send_crq(adapter, &crq);
3481 crq.query_capability.capability =
3482 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3483 atomic_inc(&adapter->running_cap_crqs);
3484 ibmvnic_send_crq(adapter, &crq);
3486 crq.query_capability.capability =
3487 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3488 atomic_inc(&adapter->running_cap_crqs);
3489 ibmvnic_send_crq(adapter, &crq);
3491 crq.query_capability.capability =
3492 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3493 atomic_inc(&adapter->running_cap_crqs);
3494 ibmvnic_send_crq(adapter, &crq);
3496 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3497 atomic_inc(&adapter->running_cap_crqs);
3498 ibmvnic_send_crq(adapter, &crq);
3500 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3501 atomic_inc(&adapter->running_cap_crqs);
3502 ibmvnic_send_crq(adapter, &crq);
3504 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3505 atomic_inc(&adapter->running_cap_crqs);
3506 ibmvnic_send_crq(adapter, &crq);
3508 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3509 atomic_inc(&adapter->running_cap_crqs);
3510 ibmvnic_send_crq(adapter, &crq);
3512 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3513 atomic_inc(&adapter->running_cap_crqs);
3514 ibmvnic_send_crq(adapter, &crq);
3516 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3517 atomic_inc(&adapter->running_cap_crqs);
3518 ibmvnic_send_crq(adapter, &crq);
3520 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3521 atomic_inc(&adapter->running_cap_crqs);
3522 ibmvnic_send_crq(adapter, &crq);
3524 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3525 atomic_inc(&adapter->running_cap_crqs);
3526 ibmvnic_send_crq(adapter, &crq);
3528 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3529 atomic_inc(&adapter->running_cap_crqs);
3530 ibmvnic_send_crq(adapter, &crq);
3532 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3533 atomic_inc(&adapter->running_cap_crqs);
3534 ibmvnic_send_crq(adapter, &crq);
3536 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3537 atomic_inc(&adapter->running_cap_crqs);
3538 ibmvnic_send_crq(adapter, &crq);
3540 crq.query_capability.capability =
3541 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3542 atomic_inc(&adapter->running_cap_crqs);
3543 ibmvnic_send_crq(adapter, &crq);
3545 crq.query_capability.capability =
3546 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3547 atomic_inc(&adapter->running_cap_crqs);
3548 ibmvnic_send_crq(adapter, &crq);
3550 crq.query_capability.capability =
3551 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3552 atomic_inc(&adapter->running_cap_crqs);
3553 ibmvnic_send_crq(adapter, &crq);
3555 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3556 atomic_inc(&adapter->running_cap_crqs);
3557 ibmvnic_send_crq(adapter, &crq);
3560 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3561 struct ibmvnic_adapter *adapter)
3563 struct device *dev = &adapter->vdev->dev;
3565 if (crq->get_vpd_size_rsp.rc.code) {
3566 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3567 crq->get_vpd_size_rsp.rc.code);
3568 complete(&adapter->fw_done);
3572 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3573 complete(&adapter->fw_done);
3576 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3577 struct ibmvnic_adapter *adapter)
3579 struct device *dev = &adapter->vdev->dev;
3580 unsigned char *substr = NULL;
3581 u8 fw_level_len = 0;
3583 memset(adapter->fw_version, 0, 32);
3585 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3588 if (crq->get_vpd_rsp.rc.code) {
3589 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3590 crq->get_vpd_rsp.rc.code);
3594 /* get the position of the firmware version info
3595 * located after the ASCII 'RM' substring in the buffer
3597 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3599 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3603 /* get length of firmware level ASCII substring */
3604 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3605 fw_level_len = *(substr + 2);
3607 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3611 /* copy firmware version string from vpd into adapter */
3612 if ((substr + 3 + fw_level_len) <
3613 (adapter->vpd->buff + adapter->vpd->len)) {
3614 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3616 dev_info(dev, "FW substr extrapolated VPD buff\n");
3620 if (adapter->fw_version[0] == '\0')
3621 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3622 complete(&adapter->fw_done);
3625 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3627 struct device *dev = &adapter->vdev->dev;
3628 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3629 union ibmvnic_crq crq;
3632 dma_unmap_single(dev, adapter->ip_offload_tok,
3633 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3635 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3636 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3637 netdev_dbg(adapter->netdev, "%016lx\n",
3638 ((unsigned long int *)(buf))[i]);
3640 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3641 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3642 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3643 buf->tcp_ipv4_chksum);
3644 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3645 buf->tcp_ipv6_chksum);
3646 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3647 buf->udp_ipv4_chksum);
3648 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3649 buf->udp_ipv6_chksum);
3650 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3651 buf->large_tx_ipv4);
3652 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3653 buf->large_tx_ipv6);
3654 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3655 buf->large_rx_ipv4);
3656 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3657 buf->large_rx_ipv6);
3658 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3659 buf->max_ipv4_header_size);
3660 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3661 buf->max_ipv6_header_size);
3662 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3663 buf->max_tcp_header_size);
3664 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3665 buf->max_udp_header_size);
3666 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3667 buf->max_large_tx_size);
3668 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3669 buf->max_large_rx_size);
3670 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3671 buf->ipv6_extension_header);
3672 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3673 buf->tcp_pseudosum_req);
3674 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3675 buf->num_ipv6_ext_headers);
3676 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3677 buf->off_ipv6_ext_headers);
3679 adapter->ip_offload_ctrl_tok =
3680 dma_map_single(dev, &adapter->ip_offload_ctrl,
3681 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3683 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3684 dev_err(dev, "Couldn't map ip offload control buffer\n");
3688 adapter->ip_offload_ctrl.len =
3689 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3690 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3691 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3692 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3693 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3694 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3695 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3696 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3697 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3698 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3700 /* large_rx disabled for now, additional features needed */
3701 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3702 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3704 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3706 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3707 adapter->netdev->features |= NETIF_F_IP_CSUM;
3709 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3710 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3712 if ((adapter->netdev->features &
3713 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3714 adapter->netdev->features |= NETIF_F_RXCSUM;
3716 if (buf->large_tx_ipv4)
3717 adapter->netdev->features |= NETIF_F_TSO;
3718 if (buf->large_tx_ipv6)
3719 adapter->netdev->features |= NETIF_F_TSO6;
3721 adapter->netdev->hw_features |= adapter->netdev->features;
3723 memset(&crq, 0, sizeof(crq));
3724 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3725 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3726 crq.control_ip_offload.len =
3727 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3728 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3729 ibmvnic_send_crq(adapter, &crq);
3732 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3733 struct ibmvnic_adapter *adapter)
3735 struct device *dev = &adapter->vdev->dev;
3736 struct ibmvnic_error_buff *error_buff, *tmp;
3737 unsigned long flags;
3741 if (!crq->request_error_rsp.rc.code) {
3742 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3743 crq->request_error_rsp.rc.code);
3747 spin_lock_irqsave(&adapter->error_list_lock, flags);
3748 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3749 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3751 list_del(&error_buff->list);
3754 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3757 dev_err(dev, "Couldn't find error id %x\n",
3758 be32_to_cpu(crq->request_error_rsp.error_id));
3762 dev_err(dev, "Detailed info for error id %x:",
3763 be32_to_cpu(crq->request_error_rsp.error_id));
3765 for (i = 0; i < error_buff->len; i++) {
3766 pr_cont("%02x", (int)error_buff->buff[i]);
3772 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3774 kfree(error_buff->buff);
3778 static void request_error_information(struct ibmvnic_adapter *adapter,
3779 union ibmvnic_crq *err_crq)
3781 struct device *dev = &adapter->vdev->dev;
3782 struct net_device *netdev = adapter->netdev;
3783 struct ibmvnic_error_buff *error_buff;
3784 unsigned long timeout = msecs_to_jiffies(30000);
3785 union ibmvnic_crq crq;
3786 unsigned long flags;
3789 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3793 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3794 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3795 if (!error_buff->buff) {
3800 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3802 if (dma_mapping_error(dev, error_buff->dma)) {
3803 netdev_err(netdev, "Couldn't map error buffer\n");
3804 kfree(error_buff->buff);
3809 error_buff->len = detail_len;
3810 error_buff->error_id = err_crq->error_indication.error_id;
3812 spin_lock_irqsave(&adapter->error_list_lock, flags);
3813 list_add_tail(&error_buff->list, &adapter->errors);
3814 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3816 memset(&crq, 0, sizeof(crq));
3817 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3818 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3819 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3820 crq.request_error_info.len = cpu_to_be32(detail_len);
3821 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3823 rc = ibmvnic_send_crq(adapter, &crq);
3825 netdev_err(netdev, "failed to request error information\n");
3829 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3830 netdev_err(netdev, "timeout waiting for error information\n");
3837 spin_lock_irqsave(&adapter->error_list_lock, flags);
3838 list_del(&error_buff->list);
3839 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3841 kfree(error_buff->buff);
3845 static void handle_error_indication(union ibmvnic_crq *crq,
3846 struct ibmvnic_adapter *adapter)
3848 struct device *dev = &adapter->vdev->dev;
3850 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3851 crq->error_indication.flags
3852 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3853 be32_to_cpu(crq->error_indication.error_id),
3854 be16_to_cpu(crq->error_indication.error_cause));
3856 if (be32_to_cpu(crq->error_indication.error_id))
3857 request_error_information(adapter, crq);
3859 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3860 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3862 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3865 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3866 struct ibmvnic_adapter *adapter)
3868 struct net_device *netdev = adapter->netdev;
3869 struct device *dev = &adapter->vdev->dev;
3872 rc = crq->change_mac_addr_rsp.rc.code;
3874 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3877 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3880 complete(&adapter->fw_done);
3884 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3885 struct ibmvnic_adapter *adapter)
3887 struct device *dev = &adapter->vdev->dev;
3891 atomic_dec(&adapter->running_cap_crqs);
3892 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3894 req_value = &adapter->req_tx_queues;
3898 req_value = &adapter->req_rx_queues;
3901 case REQ_RX_ADD_QUEUES:
3902 req_value = &adapter->req_rx_add_queues;
3905 case REQ_TX_ENTRIES_PER_SUBCRQ:
3906 req_value = &adapter->req_tx_entries_per_subcrq;
3907 name = "tx_entries_per_subcrq";
3909 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3910 req_value = &adapter->req_rx_add_entries_per_subcrq;
3911 name = "rx_add_entries_per_subcrq";
3914 req_value = &adapter->req_mtu;
3917 case PROMISC_REQUESTED:
3918 req_value = &adapter->promisc;
3922 dev_err(dev, "Got invalid cap request rsp %d\n",
3923 crq->request_capability.capability);
3927 switch (crq->request_capability_rsp.rc.code) {
3930 case PARTIALSUCCESS:
3931 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3933 (long int)be64_to_cpu(crq->request_capability_rsp.
3936 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3938 pr_err("mtu of %llu is not supported. Reverting.\n",
3940 *req_value = adapter->fallback.mtu;
3943 be64_to_cpu(crq->request_capability_rsp.number);
3946 ibmvnic_send_req_caps(adapter, 1);
3949 dev_err(dev, "Error %d in request cap rsp\n",
3950 crq->request_capability_rsp.rc.code);
3954 /* Done receiving requested capabilities, query IP offload support */
3955 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3956 union ibmvnic_crq newcrq;
3957 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3958 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3959 &adapter->ip_offload_buf;
3961 adapter->wait_capability = false;
3962 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3966 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3967 if (!firmware_has_feature(FW_FEATURE_CMO))
3968 dev_err(dev, "Couldn't map offload buffer\n");
3972 memset(&newcrq, 0, sizeof(newcrq));
3973 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3974 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3975 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3976 newcrq.query_ip_offload.ioba =
3977 cpu_to_be32(adapter->ip_offload_tok);
3979 ibmvnic_send_crq(adapter, &newcrq);
3983 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3984 struct ibmvnic_adapter *adapter)
3986 struct device *dev = &adapter->vdev->dev;
3987 struct net_device *netdev = adapter->netdev;
3988 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3989 struct ibmvnic_login_buffer *login = adapter->login_buf;
3992 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3994 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3995 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
3997 /* If the number of queues requested can't be allocated by the
3998 * server, the login response will return with code 1. We will need
3999 * to resend the login buffer with fewer queues requested.
4001 if (login_rsp_crq->generic.rc.code) {
4002 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4003 complete(&adapter->init_done);
4007 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4009 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4010 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4011 netdev_dbg(adapter->netdev, "%016lx\n",
4012 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4016 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4017 (be32_to_cpu(login->num_rxcomp_subcrqs) *
4018 adapter->req_rx_add_queues !=
4019 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4020 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4021 ibmvnic_remove(adapter->vdev);
4024 release_login_buffer(adapter);
4025 complete(&adapter->init_done);
4030 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4031 struct ibmvnic_adapter *adapter)
4033 struct device *dev = &adapter->vdev->dev;
4036 rc = crq->request_unmap_rsp.rc.code;
4038 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4041 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4042 struct ibmvnic_adapter *adapter)
4044 struct net_device *netdev = adapter->netdev;
4045 struct device *dev = &adapter->vdev->dev;
4048 rc = crq->query_map_rsp.rc.code;
4050 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4053 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4054 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4055 crq->query_map_rsp.free_pages);
4058 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4059 struct ibmvnic_adapter *adapter)
4061 struct net_device *netdev = adapter->netdev;
4062 struct device *dev = &adapter->vdev->dev;
4065 atomic_dec(&adapter->running_cap_crqs);
4066 netdev_dbg(netdev, "Outstanding queries: %d\n",
4067 atomic_read(&adapter->running_cap_crqs));
4068 rc = crq->query_capability.rc.code;
4070 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4074 switch (be16_to_cpu(crq->query_capability.capability)) {
4076 adapter->min_tx_queues =
4077 be64_to_cpu(crq->query_capability.number);
4078 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4079 adapter->min_tx_queues);
4082 adapter->min_rx_queues =
4083 be64_to_cpu(crq->query_capability.number);
4084 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4085 adapter->min_rx_queues);
4087 case MIN_RX_ADD_QUEUES:
4088 adapter->min_rx_add_queues =
4089 be64_to_cpu(crq->query_capability.number);
4090 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4091 adapter->min_rx_add_queues);
4094 adapter->max_tx_queues =
4095 be64_to_cpu(crq->query_capability.number);
4096 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4097 adapter->max_tx_queues);
4100 adapter->max_rx_queues =
4101 be64_to_cpu(crq->query_capability.number);
4102 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4103 adapter->max_rx_queues);
4105 case MAX_RX_ADD_QUEUES:
4106 adapter->max_rx_add_queues =
4107 be64_to_cpu(crq->query_capability.number);
4108 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4109 adapter->max_rx_add_queues);
4111 case MIN_TX_ENTRIES_PER_SUBCRQ:
4112 adapter->min_tx_entries_per_subcrq =
4113 be64_to_cpu(crq->query_capability.number);
4114 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4115 adapter->min_tx_entries_per_subcrq);
4117 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4118 adapter->min_rx_add_entries_per_subcrq =
4119 be64_to_cpu(crq->query_capability.number);
4120 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4121 adapter->min_rx_add_entries_per_subcrq);
4123 case MAX_TX_ENTRIES_PER_SUBCRQ:
4124 adapter->max_tx_entries_per_subcrq =
4125 be64_to_cpu(crq->query_capability.number);
4126 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4127 adapter->max_tx_entries_per_subcrq);
4129 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4130 adapter->max_rx_add_entries_per_subcrq =
4131 be64_to_cpu(crq->query_capability.number);
4132 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4133 adapter->max_rx_add_entries_per_subcrq);
4135 case TCP_IP_OFFLOAD:
4136 adapter->tcp_ip_offload =
4137 be64_to_cpu(crq->query_capability.number);
4138 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4139 adapter->tcp_ip_offload);
4141 case PROMISC_SUPPORTED:
4142 adapter->promisc_supported =
4143 be64_to_cpu(crq->query_capability.number);
4144 netdev_dbg(netdev, "promisc_supported = %lld\n",
4145 adapter->promisc_supported);
4148 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4149 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4150 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4153 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4154 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4155 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4157 case MAX_MULTICAST_FILTERS:
4158 adapter->max_multicast_filters =
4159 be64_to_cpu(crq->query_capability.number);
4160 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4161 adapter->max_multicast_filters);
4163 case VLAN_HEADER_INSERTION:
4164 adapter->vlan_header_insertion =
4165 be64_to_cpu(crq->query_capability.number);
4166 if (adapter->vlan_header_insertion)
4167 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4168 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4169 adapter->vlan_header_insertion);
4171 case RX_VLAN_HEADER_INSERTION:
4172 adapter->rx_vlan_header_insertion =
4173 be64_to_cpu(crq->query_capability.number);
4174 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4175 adapter->rx_vlan_header_insertion);
4177 case MAX_TX_SG_ENTRIES:
4178 adapter->max_tx_sg_entries =
4179 be64_to_cpu(crq->query_capability.number);
4180 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4181 adapter->max_tx_sg_entries);
4183 case RX_SG_SUPPORTED:
4184 adapter->rx_sg_supported =
4185 be64_to_cpu(crq->query_capability.number);
4186 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4187 adapter->rx_sg_supported);
4189 case OPT_TX_COMP_SUB_QUEUES:
4190 adapter->opt_tx_comp_sub_queues =
4191 be64_to_cpu(crq->query_capability.number);
4192 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4193 adapter->opt_tx_comp_sub_queues);
4195 case OPT_RX_COMP_QUEUES:
4196 adapter->opt_rx_comp_queues =
4197 be64_to_cpu(crq->query_capability.number);
4198 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4199 adapter->opt_rx_comp_queues);
4201 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4202 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4203 be64_to_cpu(crq->query_capability.number);
4204 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4205 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4207 case OPT_TX_ENTRIES_PER_SUBCRQ:
4208 adapter->opt_tx_entries_per_subcrq =
4209 be64_to_cpu(crq->query_capability.number);
4210 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4211 adapter->opt_tx_entries_per_subcrq);
4213 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4214 adapter->opt_rxba_entries_per_subcrq =
4215 be64_to_cpu(crq->query_capability.number);
4216 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4217 adapter->opt_rxba_entries_per_subcrq);
4219 case TX_RX_DESC_REQ:
4220 adapter->tx_rx_desc_req = crq->query_capability.number;
4221 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4222 adapter->tx_rx_desc_req);
4226 netdev_err(netdev, "Got invalid cap rsp %d\n",
4227 crq->query_capability.capability);
4231 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4232 adapter->wait_capability = false;
4233 ibmvnic_send_req_caps(adapter, 0);
4237 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4238 struct ibmvnic_adapter *adapter)
4240 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4241 struct net_device *netdev = adapter->netdev;
4242 struct device *dev = &adapter->vdev->dev;
4243 u64 *u64_crq = (u64 *)crq;
4246 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4247 (unsigned long int)cpu_to_be64(u64_crq[0]),
4248 (unsigned long int)cpu_to_be64(u64_crq[1]));
4249 switch (gen_crq->first) {
4250 case IBMVNIC_CRQ_INIT_RSP:
4251 switch (gen_crq->cmd) {
4252 case IBMVNIC_CRQ_INIT:
4253 dev_info(dev, "Partner initialized\n");
4254 adapter->from_passive_init = true;
4255 adapter->failover_pending = false;
4256 if (!completion_done(&adapter->init_done)) {
4257 complete(&adapter->init_done);
4258 adapter->init_done_rc = -EIO;
4260 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4262 case IBMVNIC_CRQ_INIT_COMPLETE:
4263 dev_info(dev, "Partner initialization complete\n");
4264 adapter->crq.active = true;
4265 send_version_xchg(adapter);
4268 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4271 case IBMVNIC_CRQ_XPORT_EVENT:
4272 netif_carrier_off(netdev);
4273 adapter->crq.active = false;
4274 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4275 dev_info(dev, "Migrated, re-enabling adapter\n");
4276 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4277 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4278 dev_info(dev, "Backing device failover detected\n");
4279 adapter->failover_pending = true;
4281 /* The adapter lost the connection */
4282 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4284 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4287 case IBMVNIC_CRQ_CMD_RSP:
4290 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4295 switch (gen_crq->cmd) {
4296 case VERSION_EXCHANGE_RSP:
4297 rc = crq->version_exchange_rsp.rc.code;
4299 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4302 dev_info(dev, "Partner protocol version is %d\n",
4303 crq->version_exchange_rsp.version);
4304 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4307 be16_to_cpu(crq->version_exchange_rsp.version);
4308 send_cap_queries(adapter);
4310 case QUERY_CAPABILITY_RSP:
4311 handle_query_cap_rsp(crq, adapter);
4314 handle_query_map_rsp(crq, adapter);
4316 case REQUEST_MAP_RSP:
4317 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4318 complete(&adapter->fw_done);
4320 case REQUEST_UNMAP_RSP:
4321 handle_request_unmap_rsp(crq, adapter);
4323 case REQUEST_CAPABILITY_RSP:
4324 handle_request_cap_rsp(crq, adapter);
4327 netdev_dbg(netdev, "Got Login Response\n");
4328 handle_login_rsp(crq, adapter);
4330 case LOGICAL_LINK_STATE_RSP:
4332 "Got Logical Link State Response, state: %d rc: %d\n",
4333 crq->logical_link_state_rsp.link_state,
4334 crq->logical_link_state_rsp.rc.code);
4335 adapter->logical_link_state =
4336 crq->logical_link_state_rsp.link_state;
4337 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4338 complete(&adapter->init_done);
4340 case LINK_STATE_INDICATION:
4341 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4342 adapter->phys_link_state =
4343 crq->link_state_indication.phys_link_state;
4344 adapter->logical_link_state =
4345 crq->link_state_indication.logical_link_state;
4347 case CHANGE_MAC_ADDR_RSP:
4348 netdev_dbg(netdev, "Got MAC address change Response\n");
4349 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4351 case ERROR_INDICATION:
4352 netdev_dbg(netdev, "Got Error Indication\n");
4353 handle_error_indication(crq, adapter);
4355 case REQUEST_ERROR_RSP:
4356 netdev_dbg(netdev, "Got Error Detail Response\n");
4357 handle_error_info_rsp(crq, adapter);
4359 case REQUEST_STATISTICS_RSP:
4360 netdev_dbg(netdev, "Got Statistics Response\n");
4361 complete(&adapter->stats_done);
4363 case QUERY_IP_OFFLOAD_RSP:
4364 netdev_dbg(netdev, "Got Query IP offload Response\n");
4365 handle_query_ip_offload_rsp(adapter);
4367 case MULTICAST_CTRL_RSP:
4368 netdev_dbg(netdev, "Got multicast control Response\n");
4370 case CONTROL_IP_OFFLOAD_RSP:
4371 netdev_dbg(netdev, "Got Control IP offload Response\n");
4372 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4373 sizeof(adapter->ip_offload_ctrl),
4375 complete(&adapter->init_done);
4377 case COLLECT_FW_TRACE_RSP:
4378 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4379 complete(&adapter->fw_done);
4381 case GET_VPD_SIZE_RSP:
4382 handle_vpd_size_rsp(crq, adapter);
4385 handle_vpd_rsp(crq, adapter);
4388 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4393 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4395 struct ibmvnic_adapter *adapter = instance;
4397 tasklet_schedule(&adapter->tasklet);
4401 static void ibmvnic_tasklet(void *data)
4403 struct ibmvnic_adapter *adapter = data;
4404 struct ibmvnic_crq_queue *queue = &adapter->crq;
4405 union ibmvnic_crq *crq;
4406 unsigned long flags;
4409 spin_lock_irqsave(&queue->lock, flags);
4411 /* Pull all the valid messages off the CRQ */
4412 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4413 ibmvnic_handle_crq(crq, adapter);
4414 crq->generic.first = 0;
4417 /* remain in tasklet until all
4418 * capabilities responses are received
4420 if (!adapter->wait_capability)
4423 /* if capabilities CRQ's were sent in this tasklet, the following
4424 * tasklet must wait until all responses are received
4426 if (atomic_read(&adapter->running_cap_crqs) != 0)
4427 adapter->wait_capability = true;
4428 spin_unlock_irqrestore(&queue->lock, flags);
4431 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4433 struct vio_dev *vdev = adapter->vdev;
4437 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4438 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4441 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4446 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4448 struct ibmvnic_crq_queue *crq = &adapter->crq;
4449 struct device *dev = &adapter->vdev->dev;
4450 struct vio_dev *vdev = adapter->vdev;
4455 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4456 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4458 /* Clean out the queue */
4459 memset(crq->msgs, 0, PAGE_SIZE);
4461 crq->active = false;
4463 /* And re-open it again */
4464 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4465 crq->msg_token, PAGE_SIZE);
4468 /* Adapter is good, but other end is not ready */
4469 dev_warn(dev, "Partner adapter not ready\n");
4471 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4476 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4478 struct ibmvnic_crq_queue *crq = &adapter->crq;
4479 struct vio_dev *vdev = adapter->vdev;
4485 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4486 free_irq(vdev->irq, adapter);
4487 tasklet_kill(&adapter->tasklet);
4489 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4490 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4492 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4494 free_page((unsigned long)crq->msgs);
4496 crq->active = false;
4499 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4501 struct ibmvnic_crq_queue *crq = &adapter->crq;
4502 struct device *dev = &adapter->vdev->dev;
4503 struct vio_dev *vdev = adapter->vdev;
4504 int rc, retrc = -ENOMEM;
4509 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4510 /* Should we allocate more than one page? */
4515 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4516 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4518 if (dma_mapping_error(dev, crq->msg_token))
4521 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4522 crq->msg_token, PAGE_SIZE);
4524 if (rc == H_RESOURCE)
4525 /* maybe kexecing and resource is busy. try a reset */
4526 rc = ibmvnic_reset_crq(adapter);
4529 if (rc == H_CLOSED) {
4530 dev_warn(dev, "Partner adapter not ready\n");
4532 dev_warn(dev, "Error %d opening adapter\n", rc);
4533 goto reg_crq_failed;
4538 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4539 (unsigned long)adapter);
4541 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4542 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4545 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4547 goto req_irq_failed;
4550 rc = vio_enable_interrupts(vdev);
4552 dev_err(dev, "Error %d enabling interrupts\n", rc);
4553 goto req_irq_failed;
4557 spin_lock_init(&crq->lock);
4562 tasklet_kill(&adapter->tasklet);
4564 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4565 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4567 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4569 free_page((unsigned long)crq->msgs);
4574 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4576 struct device *dev = &adapter->vdev->dev;
4577 unsigned long timeout = msecs_to_jiffies(30000);
4578 u64 old_num_rx_queues, old_num_tx_queues;
4581 adapter->from_passive_init = false;
4583 old_num_rx_queues = adapter->req_rx_queues;
4584 old_num_tx_queues = adapter->req_tx_queues;
4586 init_completion(&adapter->init_done);
4587 adapter->init_done_rc = 0;
4588 ibmvnic_send_crq_init(adapter);
4589 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4590 dev_err(dev, "Initialization sequence timed out\n");
4594 if (adapter->init_done_rc) {
4595 release_crq_queue(adapter);
4596 return adapter->init_done_rc;
4599 if (adapter->from_passive_init) {
4600 adapter->state = VNIC_OPEN;
4601 adapter->from_passive_init = false;
4605 if (adapter->resetting && !adapter->wait_for_reset &&
4606 adapter->reset_reason != VNIC_RESET_MOBILITY) {
4607 if (adapter->req_rx_queues != old_num_rx_queues ||
4608 adapter->req_tx_queues != old_num_tx_queues) {
4609 release_sub_crqs(adapter, 0);
4610 rc = init_sub_crqs(adapter);
4612 rc = reset_sub_crq_queues(adapter);
4615 rc = init_sub_crqs(adapter);
4619 dev_err(dev, "Initialization of sub crqs failed\n");
4620 release_crq_queue(adapter);
4624 rc = init_sub_crq_irqs(adapter);
4626 dev_err(dev, "Failed to initialize sub crq irqs\n");
4627 release_crq_queue(adapter);
4633 static struct device_attribute dev_attr_failover;
4635 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4637 struct ibmvnic_adapter *adapter;
4638 struct net_device *netdev;
4639 unsigned char *mac_addr_p;
4642 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4645 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4646 VETH_MAC_ADDR, NULL);
4649 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4650 __FILE__, __LINE__);
4654 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4655 IBMVNIC_MAX_QUEUES);
4659 adapter = netdev_priv(netdev);
4660 adapter->state = VNIC_PROBING;
4661 dev_set_drvdata(&dev->dev, netdev);
4662 adapter->vdev = dev;
4663 adapter->netdev = netdev;
4665 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4666 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4667 netdev->irq = dev->irq;
4668 netdev->netdev_ops = &ibmvnic_netdev_ops;
4669 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4670 SET_NETDEV_DEV(netdev, &dev->dev);
4672 spin_lock_init(&adapter->stats_lock);
4674 INIT_LIST_HEAD(&adapter->errors);
4675 spin_lock_init(&adapter->error_list_lock);
4677 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4678 INIT_LIST_HEAD(&adapter->rwi_list);
4679 mutex_init(&adapter->reset_lock);
4680 mutex_init(&adapter->rwi_lock);
4681 adapter->resetting = false;
4683 adapter->mac_change_pending = false;
4686 rc = init_crq_queue(adapter);
4688 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4690 goto ibmvnic_init_fail;
4693 rc = ibmvnic_init(adapter);
4694 if (rc && rc != EAGAIN)
4695 goto ibmvnic_init_fail;
4696 } while (rc == EAGAIN);
4698 rc = init_stats_buffers(adapter);
4700 goto ibmvnic_init_fail;
4702 rc = init_stats_token(adapter);
4704 goto ibmvnic_stats_fail;
4706 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4707 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4708 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4710 rc = device_create_file(&dev->dev, &dev_attr_failover);
4712 goto ibmvnic_dev_file_err;
4714 netif_carrier_off(netdev);
4715 rc = register_netdev(netdev);
4717 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4718 goto ibmvnic_register_fail;
4720 dev_info(&dev->dev, "ibmvnic registered\n");
4722 adapter->state = VNIC_PROBED;
4724 adapter->wait_for_reset = false;
4728 ibmvnic_register_fail:
4729 device_remove_file(&dev->dev, &dev_attr_failover);
4731 ibmvnic_dev_file_err:
4732 release_stats_token(adapter);
4735 release_stats_buffers(adapter);
4738 release_sub_crqs(adapter, 1);
4739 release_crq_queue(adapter);
4740 free_netdev(netdev);
4745 static int ibmvnic_remove(struct vio_dev *dev)
4747 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4748 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4750 adapter->state = VNIC_REMOVING;
4751 unregister_netdev(netdev);
4752 mutex_lock(&adapter->reset_lock);
4754 release_resources(adapter);
4755 release_sub_crqs(adapter, 1);
4756 release_crq_queue(adapter);
4758 release_stats_token(adapter);
4759 release_stats_buffers(adapter);
4761 adapter->state = VNIC_REMOVED;
4763 mutex_unlock(&adapter->reset_lock);
4764 device_remove_file(&dev->dev, &dev_attr_failover);
4765 free_netdev(netdev);
4766 dev_set_drvdata(&dev->dev, NULL);
4771 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4772 const char *buf, size_t count)
4774 struct net_device *netdev = dev_get_drvdata(dev);
4775 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4776 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4777 __be64 session_token;
4780 if (!sysfs_streq(buf, "1"))
4783 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4784 H_GET_SESSION_TOKEN, 0, 0, 0);
4786 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4791 session_token = (__be64)retbuf[0];
4792 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4793 be64_to_cpu(session_token));
4794 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4795 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4797 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4805 static DEVICE_ATTR_WO(failover);
4807 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4809 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4810 struct ibmvnic_adapter *adapter;
4811 struct iommu_table *tbl;
4812 unsigned long ret = 0;
4815 tbl = get_iommu_table_base(&vdev->dev);
4817 /* netdev inits at probe time along with the structures we need below*/
4819 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4821 adapter = netdev_priv(netdev);
4823 ret += PAGE_SIZE; /* the crq message queue */
4824 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4826 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4827 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4829 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4831 ret += adapter->rx_pool[i].size *
4832 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4837 static int ibmvnic_resume(struct device *dev)
4839 struct net_device *netdev = dev_get_drvdata(dev);
4840 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4842 if (adapter->state != VNIC_OPEN)
4845 tasklet_schedule(&adapter->tasklet);
4850 static const struct vio_device_id ibmvnic_device_table[] = {
4851 {"network", "IBM,vnic"},
4854 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4856 static const struct dev_pm_ops ibmvnic_pm_ops = {
4857 .resume = ibmvnic_resume
4860 static struct vio_driver ibmvnic_driver = {
4861 .id_table = ibmvnic_device_table,
4862 .probe = ibmvnic_probe,
4863 .remove = ibmvnic_remove,
4864 .get_desired_dma = ibmvnic_get_desired_dma,
4865 .name = ibmvnic_driver_name,
4866 .pm = &ibmvnic_pm_ops,
4869 /* module functions */
4870 static int __init ibmvnic_module_init(void)
4872 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4873 IBMVNIC_DRIVER_VERSION);
4875 return vio_register_driver(&ibmvnic_driver);
4878 static void __exit ibmvnic_module_exit(void)
4880 vio_unregister_driver(&ibmvnic_driver);
4883 module_init(ibmvnic_module_init);
4884 module_exit(ibmvnic_module_exit);