Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / net / ethernet / ibm / ibmvnic.c
blob1b3cc8bb07050f6856eb4f3e7fe28e4296219d49
1 /**************************************************************************/
2 /* */
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) */
8 /* */
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. */
13 /* */
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. */
18 /* */
19 /* You should have received a copy of the GNU General Public License */
20 /* along with this program. */
21 /* */
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. */
26 /* */
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. */
36 /* */
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. */
43 /* */
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>
59 #include <linux/mm.h>
60 #include <linux/ethtool.h>
61 #include <linux/proc_fs.h>
62 #include <linux/if_arp.h>
63 #include <linux/in.h>
64 #include <linux/ip.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>
73 #include <asm/vio.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>
81 #include "ibmvnic.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 *);
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 void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113 static void send_request_unmap(struct ibmvnic_adapter *, u8);
114 static void 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);
122 struct ibmvnic_stat {
123 char name[ETH_GSTRING_LEN];
124 int offset;
127 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
128 offsetof(struct ibmvnic_statistics, stat))
129 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
131 static const struct ibmvnic_stat ibmvnic_stats[] = {
132 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
133 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
134 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
135 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
136 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
137 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
138 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
139 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
140 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
141 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
142 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
143 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
144 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
145 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
146 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
147 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
148 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
149 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
150 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
151 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
152 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
153 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
156 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
157 unsigned long length, unsigned long *number,
158 unsigned long *irq)
160 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
161 long rc;
163 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
164 *number = retbuf[0];
165 *irq = retbuf[1];
167 return rc;
170 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
171 struct ibmvnic_long_term_buff *ltb, int size)
173 struct device *dev = &adapter->vdev->dev;
175 ltb->size = size;
176 ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
177 GFP_KERNEL);
179 if (!ltb->buff) {
180 dev_err(dev, "Couldn't alloc long term buffer\n");
181 return -ENOMEM;
183 ltb->map_id = adapter->map_id;
184 adapter->map_id++;
186 init_completion(&adapter->fw_done);
187 send_request_map(adapter, ltb->addr,
188 ltb->size, ltb->map_id);
189 wait_for_completion(&adapter->fw_done);
191 if (adapter->fw_done_rc) {
192 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
193 adapter->fw_done_rc);
194 return -1;
196 return 0;
199 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
200 struct ibmvnic_long_term_buff *ltb)
202 struct device *dev = &adapter->vdev->dev;
204 if (!ltb->buff)
205 return;
207 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
208 adapter->reset_reason != VNIC_RESET_MOBILITY)
209 send_request_unmap(adapter, ltb->map_id);
210 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213 static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
214 struct ibmvnic_long_term_buff *ltb)
216 memset(ltb->buff, 0, ltb->size);
218 init_completion(&adapter->fw_done);
219 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
220 wait_for_completion(&adapter->fw_done);
222 if (adapter->fw_done_rc) {
223 dev_info(&adapter->vdev->dev,
224 "Reset failed, attempting to free and reallocate buffer\n");
225 free_long_term_buff(adapter, ltb);
226 return alloc_long_term_buff(adapter, ltb, ltb->size);
228 return 0;
231 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
233 int i;
235 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
236 i++)
237 adapter->rx_pool[i].active = 0;
240 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
241 struct ibmvnic_rx_pool *pool)
243 int count = pool->size - atomic_read(&pool->available);
244 struct device *dev = &adapter->vdev->dev;
245 int buffers_added = 0;
246 unsigned long lpar_rc;
247 union sub_crq sub_crq;
248 struct sk_buff *skb;
249 unsigned int offset;
250 dma_addr_t dma_addr;
251 unsigned char *dst;
252 u64 *handle_array;
253 int shift = 0;
254 int index;
255 int i;
257 if (!pool->active)
258 return;
260 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
261 be32_to_cpu(adapter->login_rsp_buf->
262 off_rxadd_subcrqs));
264 for (i = 0; i < count; ++i) {
265 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
266 if (!skb) {
267 dev_err(dev, "Couldn't replenish rx buff\n");
268 adapter->replenish_no_mem++;
269 break;
272 index = pool->free_map[pool->next_free];
274 if (pool->rx_buff[index].skb)
275 dev_err(dev, "Inconsistent free_map!\n");
277 /* Copy the skb to the long term mapped DMA buffer */
278 offset = index * pool->buff_size;
279 dst = pool->long_term_buff.buff + offset;
280 memset(dst, 0, pool->buff_size);
281 dma_addr = pool->long_term_buff.addr + offset;
282 pool->rx_buff[index].data = dst;
284 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
285 pool->rx_buff[index].dma = dma_addr;
286 pool->rx_buff[index].skb = skb;
287 pool->rx_buff[index].pool_index = pool->index;
288 pool->rx_buff[index].size = pool->buff_size;
290 memset(&sub_crq, 0, sizeof(sub_crq));
291 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
292 sub_crq.rx_add.correlator =
293 cpu_to_be64((u64)&pool->rx_buff[index]);
294 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
295 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
297 /* The length field of the sCRQ is defined to be 24 bits so the
298 * buffer size needs to be left shifted by a byte before it is
299 * converted to big endian to prevent the last byte from being
300 * truncated.
302 #ifdef __LITTLE_ENDIAN__
303 shift = 8;
304 #endif
305 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
307 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
308 &sub_crq);
309 if (lpar_rc != H_SUCCESS)
310 goto failure;
312 buffers_added++;
313 adapter->replenish_add_buff_success++;
314 pool->next_free = (pool->next_free + 1) % pool->size;
316 atomic_add(buffers_added, &pool->available);
317 return;
319 failure:
320 dev_info(dev, "replenish pools failure\n");
321 pool->free_map[pool->next_free] = index;
322 pool->rx_buff[index].skb = NULL;
323 if (!dma_mapping_error(dev, dma_addr))
324 dma_unmap_single(dev, dma_addr, pool->buff_size,
325 DMA_FROM_DEVICE);
327 dev_kfree_skb_any(skb);
328 adapter->replenish_add_buff_failure++;
329 atomic_add(buffers_added, &pool->available);
331 if (lpar_rc == H_CLOSED) {
332 /* Disable buffer pool replenishment and report carrier off if
333 * queue is closed. Firmware guarantees that a signal will
334 * be sent to the driver, triggering a reset.
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
341 static void replenish_pools(struct ibmvnic_adapter *adapter)
343 int i;
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
347 i++) {
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
353 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
361 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
363 adapter->tx_stats_buffers =
364 kcalloc(adapter->req_tx_queues,
365 sizeof(struct ibmvnic_tx_queue_stats),
366 GFP_KERNEL);
367 if (!adapter->tx_stats_buffers)
368 return -ENOMEM;
370 adapter->rx_stats_buffers =
371 kcalloc(adapter->req_rx_queues,
372 sizeof(struct ibmvnic_rx_queue_stats),
373 GFP_KERNEL);
374 if (!adapter->rx_stats_buffers)
375 return -ENOMEM;
377 return 0;
380 static void release_stats_token(struct ibmvnic_adapter *adapter)
382 struct device *dev = &adapter->vdev->dev;
384 if (!adapter->stats_token)
385 return;
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
389 DMA_FROM_DEVICE);
390 adapter->stats_token = 0;
393 static int init_stats_token(struct ibmvnic_adapter *adapter)
395 struct device *dev = &adapter->vdev->dev;
396 dma_addr_t stok;
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
400 DMA_FROM_DEVICE);
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
403 return -1;
406 adapter->stats_token = stok;
407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
408 return 0;
411 static int reset_rx_pools(struct ibmvnic_adapter *adapter)
413 struct ibmvnic_rx_pool *rx_pool;
414 int rx_scrqs;
415 int i, j, rc;
416 u64 *size_array;
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
431 rx_pool->size *
432 rx_pool->buff_size);
433 } else {
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
438 if (rc)
439 return rc;
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
450 rx_pool->active = 1;
453 return 0;
456 static void release_rx_pools(struct ibmvnic_adapter *adapter)
458 struct ibmvnic_rx_pool *rx_pool;
459 int i, j;
461 if (!adapter->rx_pool)
462 return;
464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
465 rx_pool = &adapter->rx_pool[i];
467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
472 if (!rx_pool->rx_buff)
473 continue;
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
482 kfree(rx_pool->rx_buff);
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
487 adapter->num_active_rx_pools = 0;
490 static int init_rx_pools(struct net_device *netdev)
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
495 int rxadd_subcrqs;
496 u64 *size_array;
497 int i, j;
499 rxadd_subcrqs =
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
506 GFP_KERNEL);
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
509 return -1;
512 adapter->num_active_rx_pools = 0;
514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
517 netdev_dbg(adapter->netdev,
518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
523 rx_pool->index = i;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
525 rx_pool->active = 1;
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
528 GFP_KERNEL);
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
531 return -1;
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
536 GFP_KERNEL);
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
540 return -1;
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
546 return -1;
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
557 adapter->num_active_rx_pools = rxadd_subcrqs;
559 return 0;
562 static int reset_tx_pools(struct ibmvnic_adapter *adapter)
564 struct ibmvnic_tx_pool *tx_pool;
565 int tx_scrqs;
566 int i, j, rc;
568 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
569 for (i = 0; i < tx_scrqs; i++) {
570 netdev_dbg(adapter->netdev, "Re-setting tx_pool[%d]\n", i);
572 tx_pool = &adapter->tx_pool[i];
574 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
575 if (rc)
576 return rc;
578 rc = reset_long_term_buff(adapter, &tx_pool->tso_ltb);
579 if (rc)
580 return rc;
582 memset(tx_pool->tx_buff, 0,
583 adapter->req_tx_entries_per_subcrq *
584 sizeof(struct ibmvnic_tx_buff));
586 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
587 tx_pool->free_map[j] = j;
589 tx_pool->consumer_index = 0;
590 tx_pool->producer_index = 0;
591 tx_pool->tso_index = 0;
594 return 0;
597 static void release_vpd_data(struct ibmvnic_adapter *adapter)
599 if (!adapter->vpd)
600 return;
602 kfree(adapter->vpd->buff);
603 kfree(adapter->vpd);
605 adapter->vpd = NULL;
608 static void release_tx_pools(struct ibmvnic_adapter *adapter)
610 struct ibmvnic_tx_pool *tx_pool;
611 int i;
613 if (!adapter->tx_pool)
614 return;
616 for (i = 0; i < adapter->num_active_tx_pools; i++) {
617 netdev_dbg(adapter->netdev, "Releasing tx_pool[%d]\n", i);
618 tx_pool = &adapter->tx_pool[i];
619 kfree(tx_pool->tx_buff);
620 free_long_term_buff(adapter, &tx_pool->long_term_buff);
621 free_long_term_buff(adapter, &tx_pool->tso_ltb);
622 kfree(tx_pool->free_map);
625 kfree(adapter->tx_pool);
626 adapter->tx_pool = NULL;
627 adapter->num_active_tx_pools = 0;
630 static int init_tx_pools(struct net_device *netdev)
632 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
633 struct device *dev = &adapter->vdev->dev;
634 struct ibmvnic_tx_pool *tx_pool;
635 int tx_subcrqs;
636 int i, j;
638 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
639 adapter->tx_pool = kcalloc(tx_subcrqs,
640 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
641 if (!adapter->tx_pool)
642 return -1;
644 adapter->num_active_tx_pools = 0;
646 for (i = 0; i < tx_subcrqs; i++) {
647 tx_pool = &adapter->tx_pool[i];
649 netdev_dbg(adapter->netdev,
650 "Initializing tx_pool[%d], %lld buffs\n",
651 i, adapter->req_tx_entries_per_subcrq);
653 tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
654 sizeof(struct ibmvnic_tx_buff),
655 GFP_KERNEL);
656 if (!tx_pool->tx_buff) {
657 dev_err(dev, "tx pool buffer allocation failed\n");
658 release_tx_pools(adapter);
659 return -1;
662 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
663 adapter->req_tx_entries_per_subcrq *
664 adapter->req_mtu)) {
665 release_tx_pools(adapter);
666 return -1;
669 /* alloc TSO ltb */
670 if (alloc_long_term_buff(adapter, &tx_pool->tso_ltb,
671 IBMVNIC_TSO_BUFS *
672 IBMVNIC_TSO_BUF_SZ)) {
673 release_tx_pools(adapter);
674 return -1;
677 tx_pool->tso_index = 0;
679 tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
680 sizeof(int), GFP_KERNEL);
681 if (!tx_pool->free_map) {
682 release_tx_pools(adapter);
683 return -1;
686 for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
687 tx_pool->free_map[j] = j;
689 tx_pool->consumer_index = 0;
690 tx_pool->producer_index = 0;
693 adapter->num_active_tx_pools = tx_subcrqs;
695 return 0;
698 static void release_error_buffers(struct ibmvnic_adapter *adapter)
700 struct device *dev = &adapter->vdev->dev;
701 struct ibmvnic_error_buff *error_buff, *tmp;
702 unsigned long flags;
704 spin_lock_irqsave(&adapter->error_list_lock, flags);
705 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
706 list_del(&error_buff->list);
707 dma_unmap_single(dev, error_buff->dma, error_buff->len,
708 DMA_FROM_DEVICE);
709 kfree(error_buff->buff);
710 kfree(error_buff);
712 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
715 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
717 int i;
719 if (adapter->napi_enabled)
720 return;
722 for (i = 0; i < adapter->req_rx_queues; i++)
723 napi_enable(&adapter->napi[i]);
725 adapter->napi_enabled = true;
728 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
730 int i;
732 if (!adapter->napi_enabled)
733 return;
735 for (i = 0; i < adapter->req_rx_queues; i++) {
736 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
737 napi_disable(&adapter->napi[i]);
740 adapter->napi_enabled = false;
743 static int ibmvnic_login(struct net_device *netdev)
745 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
746 unsigned long timeout = msecs_to_jiffies(30000);
747 struct device *dev = &adapter->vdev->dev;
748 int rc;
750 do {
751 if (adapter->renegotiate) {
752 adapter->renegotiate = false;
753 release_sub_crqs(adapter);
755 reinit_completion(&adapter->init_done);
756 send_cap_queries(adapter);
757 if (!wait_for_completion_timeout(&adapter->init_done,
758 timeout)) {
759 dev_err(dev, "Capabilities query timeout\n");
760 return -1;
762 rc = init_sub_crqs(adapter);
763 if (rc) {
764 dev_err(dev,
765 "Initialization of SCRQ's failed\n");
766 return -1;
768 rc = init_sub_crq_irqs(adapter);
769 if (rc) {
770 dev_err(dev,
771 "Initialization of SCRQ's irqs failed\n");
772 return -1;
776 reinit_completion(&adapter->init_done);
777 send_login(adapter);
778 if (!wait_for_completion_timeout(&adapter->init_done,
779 timeout)) {
780 dev_err(dev, "Login timeout\n");
781 return -1;
783 } while (adapter->renegotiate);
785 /* handle pending MAC address changes after successful login */
786 if (adapter->mac_change_pending) {
787 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
788 adapter->mac_change_pending = false;
791 return 0;
794 static void release_login_buffer(struct ibmvnic_adapter *adapter)
796 kfree(adapter->login_buf);
797 adapter->login_buf = NULL;
800 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
802 kfree(adapter->login_rsp_buf);
803 adapter->login_rsp_buf = NULL;
806 static void release_resources(struct ibmvnic_adapter *adapter)
808 int i;
810 release_vpd_data(adapter);
812 release_tx_pools(adapter);
813 release_rx_pools(adapter);
815 release_stats_token(adapter);
816 release_stats_buffers(adapter);
817 release_error_buffers(adapter);
819 if (adapter->napi) {
820 for (i = 0; i < adapter->req_rx_queues; i++) {
821 if (&adapter->napi[i]) {
822 netdev_dbg(adapter->netdev,
823 "Releasing napi[%d]\n", i);
824 netif_napi_del(&adapter->napi[i]);
828 kfree(adapter->napi);
829 adapter->napi = NULL;
831 release_login_rsp_buffer(adapter);
834 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
836 struct net_device *netdev = adapter->netdev;
837 unsigned long timeout = msecs_to_jiffies(30000);
838 union ibmvnic_crq crq;
839 bool resend;
840 int rc;
842 netdev_dbg(netdev, "setting link state %d\n", link_state);
844 memset(&crq, 0, sizeof(crq));
845 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
846 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
847 crq.logical_link_state.link_state = link_state;
849 do {
850 resend = false;
852 reinit_completion(&adapter->init_done);
853 rc = ibmvnic_send_crq(adapter, &crq);
854 if (rc) {
855 netdev_err(netdev, "Failed to set link state\n");
856 return rc;
859 if (!wait_for_completion_timeout(&adapter->init_done,
860 timeout)) {
861 netdev_err(netdev, "timeout setting link state\n");
862 return -1;
865 if (adapter->init_done_rc == 1) {
866 /* Partuial success, delay and re-send */
867 mdelay(1000);
868 resend = true;
870 } while (resend);
872 return 0;
875 static int set_real_num_queues(struct net_device *netdev)
877 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
878 int rc;
880 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
881 adapter->req_tx_queues, adapter->req_rx_queues);
883 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
884 if (rc) {
885 netdev_err(netdev, "failed to set the number of tx queues\n");
886 return rc;
889 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
890 if (rc)
891 netdev_err(netdev, "failed to set the number of rx queues\n");
893 return rc;
896 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
898 struct device *dev = &adapter->vdev->dev;
899 union ibmvnic_crq crq;
900 int len = 0;
902 if (adapter->vpd->buff)
903 len = adapter->vpd->len;
905 init_completion(&adapter->fw_done);
906 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
907 crq.get_vpd_size.cmd = GET_VPD_SIZE;
908 ibmvnic_send_crq(adapter, &crq);
909 wait_for_completion(&adapter->fw_done);
911 if (!adapter->vpd->len)
912 return -ENODATA;
914 if (!adapter->vpd->buff)
915 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
916 else if (adapter->vpd->len != len)
917 adapter->vpd->buff =
918 krealloc(adapter->vpd->buff,
919 adapter->vpd->len, GFP_KERNEL);
921 if (!adapter->vpd->buff) {
922 dev_err(dev, "Could allocate VPD buffer\n");
923 return -ENOMEM;
926 adapter->vpd->dma_addr =
927 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
928 DMA_FROM_DEVICE);
929 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
930 dev_err(dev, "Could not map VPD buffer\n");
931 kfree(adapter->vpd->buff);
932 adapter->vpd->buff = NULL;
933 return -ENOMEM;
936 reinit_completion(&adapter->fw_done);
937 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
938 crq.get_vpd.cmd = GET_VPD;
939 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
940 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
941 ibmvnic_send_crq(adapter, &crq);
942 wait_for_completion(&adapter->fw_done);
944 return 0;
947 static int init_resources(struct ibmvnic_adapter *adapter)
949 struct net_device *netdev = adapter->netdev;
950 int i, rc;
952 rc = set_real_num_queues(netdev);
953 if (rc)
954 return rc;
956 rc = init_stats_buffers(adapter);
957 if (rc)
958 return rc;
960 rc = init_stats_token(adapter);
961 if (rc)
962 return rc;
964 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
965 if (!adapter->vpd)
966 return -ENOMEM;
968 /* Vital Product Data (VPD) */
969 rc = ibmvnic_get_vpd(adapter);
970 if (rc) {
971 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
972 return rc;
975 adapter->map_id = 1;
976 adapter->napi = kcalloc(adapter->req_rx_queues,
977 sizeof(struct napi_struct), GFP_KERNEL);
978 if (!adapter->napi)
979 return -ENOMEM;
981 for (i = 0; i < adapter->req_rx_queues; i++) {
982 netdev_dbg(netdev, "Adding napi[%d]\n", i);
983 netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
984 NAPI_POLL_WEIGHT);
987 send_map_query(adapter);
989 rc = init_rx_pools(netdev);
990 if (rc)
991 return rc;
993 rc = init_tx_pools(netdev);
994 return rc;
997 static int __ibmvnic_open(struct net_device *netdev)
999 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1000 enum vnic_state prev_state = adapter->state;
1001 int i, rc;
1003 adapter->state = VNIC_OPENING;
1004 replenish_pools(adapter);
1005 ibmvnic_napi_enable(adapter);
1007 /* We're ready to receive frames, enable the sub-crq interrupts and
1008 * set the logical link state to up
1010 for (i = 0; i < adapter->req_rx_queues; i++) {
1011 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1012 if (prev_state == VNIC_CLOSED)
1013 enable_irq(adapter->rx_scrq[i]->irq);
1014 else
1015 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1018 for (i = 0; i < adapter->req_tx_queues; i++) {
1019 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1020 if (prev_state == VNIC_CLOSED)
1021 enable_irq(adapter->tx_scrq[i]->irq);
1022 else
1023 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1026 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1027 if (rc) {
1028 for (i = 0; i < adapter->req_rx_queues; i++)
1029 napi_disable(&adapter->napi[i]);
1030 release_resources(adapter);
1031 return rc;
1034 netif_tx_start_all_queues(netdev);
1036 if (prev_state == VNIC_CLOSED) {
1037 for (i = 0; i < adapter->req_rx_queues; i++)
1038 napi_schedule(&adapter->napi[i]);
1041 adapter->state = VNIC_OPEN;
1042 return rc;
1045 static int ibmvnic_open(struct net_device *netdev)
1047 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1048 int rc;
1050 mutex_lock(&adapter->reset_lock);
1052 if (adapter->state != VNIC_CLOSED) {
1053 rc = ibmvnic_login(netdev);
1054 if (rc) {
1055 mutex_unlock(&adapter->reset_lock);
1056 return rc;
1059 rc = init_resources(adapter);
1060 if (rc) {
1061 netdev_err(netdev, "failed to initialize resources\n");
1062 release_resources(adapter);
1063 mutex_unlock(&adapter->reset_lock);
1064 return rc;
1068 rc = __ibmvnic_open(netdev);
1069 netif_carrier_on(netdev);
1071 mutex_unlock(&adapter->reset_lock);
1073 return rc;
1076 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1078 struct ibmvnic_rx_pool *rx_pool;
1079 u64 rx_entries;
1080 int rx_scrqs;
1081 int i, j;
1083 if (!adapter->rx_pool)
1084 return;
1086 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
1087 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1089 /* Free any remaining skbs in the rx buffer pools */
1090 for (i = 0; i < rx_scrqs; i++) {
1091 rx_pool = &adapter->rx_pool[i];
1092 if (!rx_pool)
1093 continue;
1095 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1096 for (j = 0; j < rx_entries; j++) {
1097 if (rx_pool->rx_buff[j].skb) {
1098 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
1099 rx_pool->rx_buff[j].skb = NULL;
1105 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1107 struct ibmvnic_tx_pool *tx_pool;
1108 u64 tx_entries;
1109 int tx_scrqs;
1110 int i, j;
1112 if (!adapter->tx_pool)
1113 return;
1115 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
1116 tx_entries = adapter->req_tx_entries_per_subcrq;
1118 /* Free any remaining skbs in the tx buffer pools */
1119 for (i = 0; i < tx_scrqs; i++) {
1120 tx_pool = &adapter->tx_pool[i];
1121 if (!tx_pool)
1122 continue;
1124 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1125 for (j = 0; j < tx_entries; j++) {
1126 if (tx_pool->tx_buff[j].skb) {
1127 dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
1128 tx_pool->tx_buff[j].skb = NULL;
1134 static int __ibmvnic_close(struct net_device *netdev)
1136 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1137 int rc = 0;
1138 int i;
1140 adapter->state = VNIC_CLOSING;
1142 /* ensure that transmissions are stopped if called by do_reset */
1143 if (adapter->resetting)
1144 netif_tx_disable(netdev);
1145 else
1146 netif_tx_stop_all_queues(netdev);
1148 ibmvnic_napi_disable(adapter);
1150 if (adapter->tx_scrq) {
1151 for (i = 0; i < adapter->req_tx_queues; i++)
1152 if (adapter->tx_scrq[i]->irq) {
1153 netdev_dbg(adapter->netdev,
1154 "Disabling tx_scrq[%d] irq\n", i);
1155 disable_irq(adapter->tx_scrq[i]->irq);
1159 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1160 if (rc)
1161 return rc;
1163 if (adapter->rx_scrq) {
1164 for (i = 0; i < adapter->req_rx_queues; i++) {
1165 int retries = 10;
1167 while (pending_scrq(adapter, adapter->rx_scrq[i])) {
1168 retries--;
1169 mdelay(100);
1171 if (retries == 0)
1172 break;
1175 if (adapter->rx_scrq[i]->irq) {
1176 netdev_dbg(adapter->netdev,
1177 "Disabling rx_scrq[%d] irq\n", i);
1178 disable_irq(adapter->rx_scrq[i]->irq);
1182 clean_rx_pools(adapter);
1183 clean_tx_pools(adapter);
1184 adapter->state = VNIC_CLOSED;
1185 return rc;
1188 static int ibmvnic_close(struct net_device *netdev)
1190 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1191 int rc;
1193 mutex_lock(&adapter->reset_lock);
1194 rc = __ibmvnic_close(netdev);
1195 mutex_unlock(&adapter->reset_lock);
1197 return rc;
1201 * build_hdr_data - creates L2/L3/L4 header data buffer
1202 * @hdr_field - bitfield determining needed headers
1203 * @skb - socket buffer
1204 * @hdr_len - array of header lengths
1205 * @tot_len - total length of data
1207 * Reads hdr_field to determine which headers are needed by firmware.
1208 * Builds a buffer containing these headers. Saves individual header
1209 * lengths and total buffer length to be used to build descriptors.
1211 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1212 int *hdr_len, u8 *hdr_data)
1214 int len = 0;
1215 u8 *hdr;
1217 hdr_len[0] = sizeof(struct ethhdr);
1219 if (skb->protocol == htons(ETH_P_IP)) {
1220 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1221 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1222 hdr_len[2] = tcp_hdrlen(skb);
1223 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1224 hdr_len[2] = sizeof(struct udphdr);
1225 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1226 hdr_len[1] = sizeof(struct ipv6hdr);
1227 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1228 hdr_len[2] = tcp_hdrlen(skb);
1229 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1230 hdr_len[2] = sizeof(struct udphdr);
1231 } else if (skb->protocol == htons(ETH_P_ARP)) {
1232 hdr_len[1] = arp_hdr_len(skb->dev);
1233 hdr_len[2] = 0;
1236 memset(hdr_data, 0, 120);
1237 if ((hdr_field >> 6) & 1) {
1238 hdr = skb_mac_header(skb);
1239 memcpy(hdr_data, hdr, hdr_len[0]);
1240 len += hdr_len[0];
1243 if ((hdr_field >> 5) & 1) {
1244 hdr = skb_network_header(skb);
1245 memcpy(hdr_data + len, hdr, hdr_len[1]);
1246 len += hdr_len[1];
1249 if ((hdr_field >> 4) & 1) {
1250 hdr = skb_transport_header(skb);
1251 memcpy(hdr_data + len, hdr, hdr_len[2]);
1252 len += hdr_len[2];
1254 return len;
1258 * create_hdr_descs - create header and header extension descriptors
1259 * @hdr_field - bitfield determining needed headers
1260 * @data - buffer containing header data
1261 * @len - length of data buffer
1262 * @hdr_len - array of individual header lengths
1263 * @scrq_arr - descriptor array
1265 * Creates header and, if needed, header extension descriptors and
1266 * places them in a descriptor array, scrq_arr
1269 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1270 union sub_crq *scrq_arr)
1272 union sub_crq hdr_desc;
1273 int tmp_len = len;
1274 int num_descs = 0;
1275 u8 *data, *cur;
1276 int tmp;
1278 while (tmp_len > 0) {
1279 cur = hdr_data + len - tmp_len;
1281 memset(&hdr_desc, 0, sizeof(hdr_desc));
1282 if (cur != hdr_data) {
1283 data = hdr_desc.hdr_ext.data;
1284 tmp = tmp_len > 29 ? 29 : tmp_len;
1285 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1286 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1287 hdr_desc.hdr_ext.len = tmp;
1288 } else {
1289 data = hdr_desc.hdr.data;
1290 tmp = tmp_len > 24 ? 24 : tmp_len;
1291 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1292 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1293 hdr_desc.hdr.len = tmp;
1294 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1295 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1296 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1297 hdr_desc.hdr.flag = hdr_field << 1;
1299 memcpy(data, cur, tmp);
1300 tmp_len -= tmp;
1301 *scrq_arr = hdr_desc;
1302 scrq_arr++;
1303 num_descs++;
1306 return num_descs;
1310 * build_hdr_descs_arr - build a header descriptor array
1311 * @skb - socket buffer
1312 * @num_entries - number of descriptors to be sent
1313 * @subcrq - first TX descriptor
1314 * @hdr_field - bit field determining which headers will be sent
1316 * This function will build a TX descriptor array with applicable
1317 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1320 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1321 int *num_entries, u8 hdr_field)
1323 int hdr_len[3] = {0, 0, 0};
1324 int tot_len;
1325 u8 *hdr_data = txbuff->hdr_data;
1327 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1328 txbuff->hdr_data);
1329 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1330 txbuff->indir_arr + 1);
1333 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1335 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1336 int queue_num = skb_get_queue_mapping(skb);
1337 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1338 struct device *dev = &adapter->vdev->dev;
1339 struct ibmvnic_tx_buff *tx_buff = NULL;
1340 struct ibmvnic_sub_crq_queue *tx_scrq;
1341 struct ibmvnic_tx_pool *tx_pool;
1342 unsigned int tx_send_failed = 0;
1343 unsigned int tx_map_failed = 0;
1344 unsigned int tx_dropped = 0;
1345 unsigned int tx_packets = 0;
1346 unsigned int tx_bytes = 0;
1347 dma_addr_t data_dma_addr;
1348 struct netdev_queue *txq;
1349 unsigned long lpar_rc;
1350 union sub_crq tx_crq;
1351 unsigned int offset;
1352 int num_entries = 1;
1353 unsigned char *dst;
1354 u64 *handle_array;
1355 int index = 0;
1356 u8 proto = 0;
1357 int ret = 0;
1359 if (adapter->resetting) {
1360 if (!netif_subqueue_stopped(netdev, skb))
1361 netif_stop_subqueue(netdev, queue_num);
1362 dev_kfree_skb_any(skb);
1364 tx_send_failed++;
1365 tx_dropped++;
1366 ret = NETDEV_TX_OK;
1367 goto out;
1370 tx_pool = &adapter->tx_pool[queue_num];
1371 tx_scrq = adapter->tx_scrq[queue_num];
1372 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1373 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1374 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1376 index = tx_pool->free_map[tx_pool->consumer_index];
1378 if (skb_is_gso(skb)) {
1379 offset = tx_pool->tso_index * IBMVNIC_TSO_BUF_SZ;
1380 dst = tx_pool->tso_ltb.buff + offset;
1381 memset(dst, 0, IBMVNIC_TSO_BUF_SZ);
1382 data_dma_addr = tx_pool->tso_ltb.addr + offset;
1383 tx_pool->tso_index++;
1384 if (tx_pool->tso_index == IBMVNIC_TSO_BUFS)
1385 tx_pool->tso_index = 0;
1386 } else {
1387 offset = index * adapter->req_mtu;
1388 dst = tx_pool->long_term_buff.buff + offset;
1389 memset(dst, 0, adapter->req_mtu);
1390 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1393 if (skb_shinfo(skb)->nr_frags) {
1394 int cur, i;
1396 /* Copy the head */
1397 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1398 cur = skb_headlen(skb);
1400 /* Copy the frags */
1401 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1402 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1404 memcpy(dst + cur,
1405 page_address(skb_frag_page(frag)) +
1406 frag->page_offset, skb_frag_size(frag));
1407 cur += skb_frag_size(frag);
1409 } else {
1410 skb_copy_from_linear_data(skb, dst, skb->len);
1413 tx_pool->consumer_index =
1414 (tx_pool->consumer_index + 1) %
1415 adapter->req_tx_entries_per_subcrq;
1417 tx_buff = &tx_pool->tx_buff[index];
1418 tx_buff->skb = skb;
1419 tx_buff->data_dma[0] = data_dma_addr;
1420 tx_buff->data_len[0] = skb->len;
1421 tx_buff->index = index;
1422 tx_buff->pool_index = queue_num;
1423 tx_buff->last_frag = true;
1425 memset(&tx_crq, 0, sizeof(tx_crq));
1426 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1427 tx_crq.v1.type = IBMVNIC_TX_DESC;
1428 tx_crq.v1.n_crq_elem = 1;
1429 tx_crq.v1.n_sge = 1;
1430 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1431 tx_crq.v1.correlator = cpu_to_be32(index);
1432 if (skb_is_gso(skb))
1433 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->tso_ltb.map_id);
1434 else
1435 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1436 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1437 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1439 if (adapter->vlan_header_insertion) {
1440 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1441 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1444 if (skb->protocol == htons(ETH_P_IP)) {
1445 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1446 proto = ip_hdr(skb)->protocol;
1447 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1448 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1449 proto = ipv6_hdr(skb)->nexthdr;
1452 if (proto == IPPROTO_TCP)
1453 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1454 else if (proto == IPPROTO_UDP)
1455 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1457 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1458 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1459 hdrs += 2;
1461 if (skb_is_gso(skb)) {
1462 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1463 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1464 hdrs += 2;
1466 /* determine if l2/3/4 headers are sent to firmware */
1467 if ((*hdrs >> 7) & 1) {
1468 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1469 tx_crq.v1.n_crq_elem = num_entries;
1470 tx_buff->indir_arr[0] = tx_crq;
1471 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1472 sizeof(tx_buff->indir_arr),
1473 DMA_TO_DEVICE);
1474 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1475 dev_kfree_skb_any(skb);
1476 tx_buff->skb = NULL;
1477 if (!firmware_has_feature(FW_FEATURE_CMO))
1478 dev_err(dev, "tx: unable to map descriptor array\n");
1479 tx_map_failed++;
1480 tx_dropped++;
1481 ret = NETDEV_TX_OK;
1482 goto out;
1484 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1485 (u64)tx_buff->indir_dma,
1486 (u64)num_entries);
1487 } else {
1488 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1489 &tx_crq);
1491 if (lpar_rc != H_SUCCESS) {
1492 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1494 if (tx_pool->consumer_index == 0)
1495 tx_pool->consumer_index =
1496 adapter->req_tx_entries_per_subcrq - 1;
1497 else
1498 tx_pool->consumer_index--;
1500 dev_kfree_skb_any(skb);
1501 tx_buff->skb = NULL;
1503 if (lpar_rc == H_CLOSED) {
1504 /* Disable TX and report carrier off if queue is closed.
1505 * Firmware guarantees that a signal will be sent to the
1506 * driver, triggering a reset or some other action.
1508 netif_tx_stop_all_queues(netdev);
1509 netif_carrier_off(netdev);
1512 tx_send_failed++;
1513 tx_dropped++;
1514 ret = NETDEV_TX_OK;
1515 goto out;
1518 if (atomic_inc_return(&tx_scrq->used)
1519 >= adapter->req_tx_entries_per_subcrq) {
1520 netdev_info(netdev, "Stopping queue %d\n", queue_num);
1521 netif_stop_subqueue(netdev, queue_num);
1524 tx_packets++;
1525 tx_bytes += skb->len;
1526 txq->trans_start = jiffies;
1527 ret = NETDEV_TX_OK;
1529 out:
1530 netdev->stats.tx_dropped += tx_dropped;
1531 netdev->stats.tx_bytes += tx_bytes;
1532 netdev->stats.tx_packets += tx_packets;
1533 adapter->tx_send_failed += tx_send_failed;
1534 adapter->tx_map_failed += tx_map_failed;
1535 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1536 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1537 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1539 return ret;
1542 static void ibmvnic_set_multi(struct net_device *netdev)
1544 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1545 struct netdev_hw_addr *ha;
1546 union ibmvnic_crq crq;
1548 memset(&crq, 0, sizeof(crq));
1549 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1550 crq.request_capability.cmd = REQUEST_CAPABILITY;
1552 if (netdev->flags & IFF_PROMISC) {
1553 if (!adapter->promisc_supported)
1554 return;
1555 } else {
1556 if (netdev->flags & IFF_ALLMULTI) {
1557 /* Accept all multicast */
1558 memset(&crq, 0, sizeof(crq));
1559 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1560 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1561 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1562 ibmvnic_send_crq(adapter, &crq);
1563 } else if (netdev_mc_empty(netdev)) {
1564 /* Reject all multicast */
1565 memset(&crq, 0, sizeof(crq));
1566 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1567 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1568 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1569 ibmvnic_send_crq(adapter, &crq);
1570 } else {
1571 /* Accept one or more multicast(s) */
1572 netdev_for_each_mc_addr(ha, netdev) {
1573 memset(&crq, 0, sizeof(crq));
1574 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1575 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1576 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1577 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1578 ha->addr);
1579 ibmvnic_send_crq(adapter, &crq);
1585 static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1587 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1588 struct sockaddr *addr = p;
1589 union ibmvnic_crq crq;
1591 if (!is_valid_ether_addr(addr->sa_data))
1592 return -EADDRNOTAVAIL;
1594 memset(&crq, 0, sizeof(crq));
1595 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1596 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1597 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1599 init_completion(&adapter->fw_done);
1600 ibmvnic_send_crq(adapter, &crq);
1601 wait_for_completion(&adapter->fw_done);
1602 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1603 return adapter->fw_done_rc ? -EIO : 0;
1606 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1608 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1609 struct sockaddr *addr = p;
1610 int rc;
1612 if (adapter->state == VNIC_PROBED) {
1613 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1614 adapter->mac_change_pending = true;
1615 return 0;
1618 rc = __ibmvnic_set_mac(netdev, addr);
1620 return rc;
1624 * do_reset returns zero if we are able to keep processing reset events, or
1625 * non-zero if we hit a fatal error and must halt.
1627 static int do_reset(struct ibmvnic_adapter *adapter,
1628 struct ibmvnic_rwi *rwi, u32 reset_state)
1630 u64 old_num_rx_queues, old_num_tx_queues;
1631 struct net_device *netdev = adapter->netdev;
1632 int i, rc;
1634 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1635 rwi->reset_reason);
1637 netif_carrier_off(netdev);
1638 adapter->reset_reason = rwi->reset_reason;
1640 old_num_rx_queues = adapter->req_rx_queues;
1641 old_num_tx_queues = adapter->req_tx_queues;
1643 if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
1644 rc = ibmvnic_reenable_crq_queue(adapter);
1645 if (rc)
1646 return 0;
1649 rc = __ibmvnic_close(netdev);
1650 if (rc)
1651 return rc;
1653 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1654 adapter->wait_for_reset) {
1655 release_resources(adapter);
1656 release_sub_crqs(adapter);
1657 release_crq_queue(adapter);
1660 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1661 /* remove the closed state so when we call open it appears
1662 * we are coming from the probed state.
1664 adapter->state = VNIC_PROBED;
1666 rc = ibmvnic_init(adapter);
1667 if (rc)
1668 return IBMVNIC_INIT_FAILED;
1670 /* If the adapter was in PROBE state prior to the reset,
1671 * exit here.
1673 if (reset_state == VNIC_PROBED)
1674 return 0;
1676 rc = ibmvnic_login(netdev);
1677 if (rc) {
1678 adapter->state = VNIC_PROBED;
1679 return 0;
1682 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1683 adapter->wait_for_reset) {
1684 rc = init_resources(adapter);
1685 if (rc)
1686 return rc;
1687 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1688 adapter->req_tx_queues != old_num_tx_queues) {
1689 adapter->map_id = 1;
1690 release_rx_pools(adapter);
1691 release_tx_pools(adapter);
1692 init_rx_pools(netdev);
1693 init_tx_pools(netdev);
1694 } else {
1695 rc = reset_tx_pools(adapter);
1696 if (rc)
1697 return rc;
1699 rc = reset_rx_pools(adapter);
1700 if (rc)
1701 return rc;
1703 if (reset_state == VNIC_CLOSED)
1704 return 0;
1708 rc = __ibmvnic_open(netdev);
1709 if (rc) {
1710 if (list_empty(&adapter->rwi_list))
1711 adapter->state = VNIC_CLOSED;
1712 else
1713 adapter->state = reset_state;
1715 return 0;
1718 /* kick napi */
1719 for (i = 0; i < adapter->req_rx_queues; i++)
1720 napi_schedule(&adapter->napi[i]);
1722 if (adapter->reset_reason != VNIC_RESET_FAILOVER)
1723 netdev_notify_peers(netdev);
1725 netif_carrier_on(netdev);
1727 return 0;
1730 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1732 struct ibmvnic_rwi *rwi;
1734 mutex_lock(&adapter->rwi_lock);
1736 if (!list_empty(&adapter->rwi_list)) {
1737 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1738 list);
1739 list_del(&rwi->list);
1740 } else {
1741 rwi = NULL;
1744 mutex_unlock(&adapter->rwi_lock);
1745 return rwi;
1748 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1750 struct ibmvnic_rwi *rwi;
1752 rwi = get_next_rwi(adapter);
1753 while (rwi) {
1754 kfree(rwi);
1755 rwi = get_next_rwi(adapter);
1759 static void __ibmvnic_reset(struct work_struct *work)
1761 struct ibmvnic_rwi *rwi;
1762 struct ibmvnic_adapter *adapter;
1763 struct net_device *netdev;
1764 u32 reset_state;
1765 int rc = 0;
1767 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1768 netdev = adapter->netdev;
1770 mutex_lock(&adapter->reset_lock);
1771 adapter->resetting = true;
1772 reset_state = adapter->state;
1774 rwi = get_next_rwi(adapter);
1775 while (rwi) {
1776 rc = do_reset(adapter, rwi, reset_state);
1777 kfree(rwi);
1778 if (rc && rc != IBMVNIC_INIT_FAILED)
1779 break;
1781 rwi = get_next_rwi(adapter);
1784 if (adapter->wait_for_reset) {
1785 adapter->wait_for_reset = false;
1786 adapter->reset_done_rc = rc;
1787 complete(&adapter->reset_done);
1790 if (rc) {
1791 netdev_dbg(adapter->netdev, "Reset failed\n");
1792 free_all_rwi(adapter);
1793 mutex_unlock(&adapter->reset_lock);
1794 return;
1797 adapter->resetting = false;
1798 mutex_unlock(&adapter->reset_lock);
1801 static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
1802 enum ibmvnic_reset_reason reason)
1804 struct ibmvnic_rwi *rwi, *tmp;
1805 struct net_device *netdev = adapter->netdev;
1806 struct list_head *entry;
1808 if (adapter->state == VNIC_REMOVING ||
1809 adapter->state == VNIC_REMOVED) {
1810 netdev_dbg(netdev, "Adapter removing, skipping reset\n");
1811 return;
1814 if (adapter->state == VNIC_PROBING) {
1815 netdev_warn(netdev, "Adapter reset during probe\n");
1816 adapter->init_done_rc = EAGAIN;
1817 return;
1820 mutex_lock(&adapter->rwi_lock);
1822 list_for_each(entry, &adapter->rwi_list) {
1823 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1824 if (tmp->reset_reason == reason) {
1825 netdev_dbg(netdev, "Skipping matching reset\n");
1826 mutex_unlock(&adapter->rwi_lock);
1827 return;
1831 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1832 if (!rwi) {
1833 mutex_unlock(&adapter->rwi_lock);
1834 ibmvnic_close(netdev);
1835 return;
1838 rwi->reset_reason = reason;
1839 list_add_tail(&rwi->list, &adapter->rwi_list);
1840 mutex_unlock(&adapter->rwi_lock);
1842 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1843 schedule_work(&adapter->ibmvnic_reset);
1846 static void ibmvnic_tx_timeout(struct net_device *dev)
1848 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1850 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1853 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1854 struct ibmvnic_rx_buff *rx_buff)
1856 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1858 rx_buff->skb = NULL;
1860 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1861 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1863 atomic_dec(&pool->available);
1866 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1868 struct net_device *netdev = napi->dev;
1869 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1870 int scrq_num = (int)(napi - adapter->napi);
1871 int frames_processed = 0;
1873 restart_poll:
1874 while (frames_processed < budget) {
1875 struct sk_buff *skb;
1876 struct ibmvnic_rx_buff *rx_buff;
1877 union sub_crq *next;
1878 u32 length;
1879 u16 offset;
1880 u8 flags = 0;
1882 if (unlikely(adapter->resetting &&
1883 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
1884 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1885 napi_complete_done(napi, frames_processed);
1886 return frames_processed;
1889 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1890 break;
1891 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1892 rx_buff =
1893 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1894 rx_comp.correlator);
1895 /* do error checking */
1896 if (next->rx_comp.rc) {
1897 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
1898 be16_to_cpu(next->rx_comp.rc));
1899 /* free the entry */
1900 next->rx_comp.first = 0;
1901 dev_kfree_skb_any(rx_buff->skb);
1902 remove_buff_from_pool(adapter, rx_buff);
1903 continue;
1904 } else if (!rx_buff->skb) {
1905 /* free the entry */
1906 next->rx_comp.first = 0;
1907 remove_buff_from_pool(adapter, rx_buff);
1908 continue;
1911 length = be32_to_cpu(next->rx_comp.len);
1912 offset = be16_to_cpu(next->rx_comp.off_frame_data);
1913 flags = next->rx_comp.flags;
1914 skb = rx_buff->skb;
1915 skb_copy_to_linear_data(skb, rx_buff->data + offset,
1916 length);
1918 /* VLAN Header has been stripped by the system firmware and
1919 * needs to be inserted by the driver
1921 if (adapter->rx_vlan_header_insertion &&
1922 (flags & IBMVNIC_VLAN_STRIPPED))
1923 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1924 ntohs(next->rx_comp.vlan_tci));
1926 /* free the entry */
1927 next->rx_comp.first = 0;
1928 remove_buff_from_pool(adapter, rx_buff);
1930 skb_put(skb, length);
1931 skb->protocol = eth_type_trans(skb, netdev);
1932 skb_record_rx_queue(skb, scrq_num);
1934 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1935 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1936 skb->ip_summed = CHECKSUM_UNNECESSARY;
1939 length = skb->len;
1940 napi_gro_receive(napi, skb); /* send it up */
1941 netdev->stats.rx_packets++;
1942 netdev->stats.rx_bytes += length;
1943 adapter->rx_stats_buffers[scrq_num].packets++;
1944 adapter->rx_stats_buffers[scrq_num].bytes += length;
1945 frames_processed++;
1948 if (adapter->state != VNIC_CLOSING)
1949 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1951 if (frames_processed < budget) {
1952 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1953 napi_complete_done(napi, frames_processed);
1954 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1955 napi_reschedule(napi)) {
1956 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1957 goto restart_poll;
1960 return frames_processed;
1963 #ifdef CONFIG_NET_POLL_CONTROLLER
1964 static void ibmvnic_netpoll_controller(struct net_device *dev)
1966 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1967 int i;
1969 replenish_pools(netdev_priv(dev));
1970 for (i = 0; i < adapter->req_rx_queues; i++)
1971 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1972 adapter->rx_scrq[i]);
1974 #endif
1976 static int wait_for_reset(struct ibmvnic_adapter *adapter)
1978 adapter->fallback.mtu = adapter->req_mtu;
1979 adapter->fallback.rx_queues = adapter->req_rx_queues;
1980 adapter->fallback.tx_queues = adapter->req_tx_queues;
1981 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
1982 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
1984 init_completion(&adapter->reset_done);
1985 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1986 adapter->wait_for_reset = true;
1987 wait_for_completion(&adapter->reset_done);
1989 if (adapter->reset_done_rc) {
1990 adapter->desired.mtu = adapter->fallback.mtu;
1991 adapter->desired.rx_queues = adapter->fallback.rx_queues;
1992 adapter->desired.tx_queues = adapter->fallback.tx_queues;
1993 adapter->desired.rx_entries = adapter->fallback.rx_entries;
1994 adapter->desired.tx_entries = adapter->fallback.tx_entries;
1996 init_completion(&adapter->reset_done);
1997 ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
1998 wait_for_completion(&adapter->reset_done);
2000 adapter->wait_for_reset = false;
2002 return adapter->reset_done_rc;
2005 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2007 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2009 adapter->desired.mtu = new_mtu + ETH_HLEN;
2011 return wait_for_reset(adapter);
2014 static const struct net_device_ops ibmvnic_netdev_ops = {
2015 .ndo_open = ibmvnic_open,
2016 .ndo_stop = ibmvnic_close,
2017 .ndo_start_xmit = ibmvnic_xmit,
2018 .ndo_set_rx_mode = ibmvnic_set_multi,
2019 .ndo_set_mac_address = ibmvnic_set_mac,
2020 .ndo_validate_addr = eth_validate_addr,
2021 .ndo_tx_timeout = ibmvnic_tx_timeout,
2022 #ifdef CONFIG_NET_POLL_CONTROLLER
2023 .ndo_poll_controller = ibmvnic_netpoll_controller,
2024 #endif
2025 .ndo_change_mtu = ibmvnic_change_mtu,
2028 /* ethtool functions */
2030 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2031 struct ethtool_link_ksettings *cmd)
2033 u32 supported, advertising;
2035 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2036 SUPPORTED_FIBRE);
2037 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2038 ADVERTISED_FIBRE);
2039 cmd->base.speed = SPEED_1000;
2040 cmd->base.duplex = DUPLEX_FULL;
2041 cmd->base.port = PORT_FIBRE;
2042 cmd->base.phy_address = 0;
2043 cmd->base.autoneg = AUTONEG_ENABLE;
2045 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2046 supported);
2047 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2048 advertising);
2050 return 0;
2053 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2054 struct ethtool_drvinfo *info)
2056 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2058 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2059 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2060 strlcpy(info->fw_version, adapter->fw_version,
2061 sizeof(info->fw_version));
2064 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2066 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2068 return adapter->msg_enable;
2071 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2073 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2075 adapter->msg_enable = data;
2078 static u32 ibmvnic_get_link(struct net_device *netdev)
2080 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2082 /* Don't need to send a query because we request a logical link up at
2083 * init and then we wait for link state indications
2085 return adapter->logical_link_state;
2088 static void ibmvnic_get_ringparam(struct net_device *netdev,
2089 struct ethtool_ringparam *ring)
2091 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2093 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2094 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2095 ring->rx_mini_max_pending = 0;
2096 ring->rx_jumbo_max_pending = 0;
2097 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2098 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2099 ring->rx_mini_pending = 0;
2100 ring->rx_jumbo_pending = 0;
2103 static int ibmvnic_set_ringparam(struct net_device *netdev,
2104 struct ethtool_ringparam *ring)
2106 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2108 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2109 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2110 netdev_err(netdev, "Invalid request.\n");
2111 netdev_err(netdev, "Max tx buffers = %llu\n",
2112 adapter->max_rx_add_entries_per_subcrq);
2113 netdev_err(netdev, "Max rx buffers = %llu\n",
2114 adapter->max_tx_entries_per_subcrq);
2115 return -EINVAL;
2118 adapter->desired.rx_entries = ring->rx_pending;
2119 adapter->desired.tx_entries = ring->tx_pending;
2121 return wait_for_reset(adapter);
2124 static void ibmvnic_get_channels(struct net_device *netdev,
2125 struct ethtool_channels *channels)
2127 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2129 channels->max_rx = adapter->max_rx_queues;
2130 channels->max_tx = adapter->max_tx_queues;
2131 channels->max_other = 0;
2132 channels->max_combined = 0;
2133 channels->rx_count = adapter->req_rx_queues;
2134 channels->tx_count = adapter->req_tx_queues;
2135 channels->other_count = 0;
2136 channels->combined_count = 0;
2139 static int ibmvnic_set_channels(struct net_device *netdev,
2140 struct ethtool_channels *channels)
2142 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2144 adapter->desired.rx_queues = channels->rx_count;
2145 adapter->desired.tx_queues = channels->tx_count;
2147 return wait_for_reset(adapter);
2150 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2152 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2153 int i;
2155 if (stringset != ETH_SS_STATS)
2156 return;
2158 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2159 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2161 for (i = 0; i < adapter->req_tx_queues; i++) {
2162 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2163 data += ETH_GSTRING_LEN;
2165 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2166 data += ETH_GSTRING_LEN;
2168 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2169 data += ETH_GSTRING_LEN;
2172 for (i = 0; i < adapter->req_rx_queues; i++) {
2173 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2174 data += ETH_GSTRING_LEN;
2176 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2177 data += ETH_GSTRING_LEN;
2179 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2180 data += ETH_GSTRING_LEN;
2184 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2186 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2188 switch (sset) {
2189 case ETH_SS_STATS:
2190 return ARRAY_SIZE(ibmvnic_stats) +
2191 adapter->req_tx_queues * NUM_TX_STATS +
2192 adapter->req_rx_queues * NUM_RX_STATS;
2193 default:
2194 return -EOPNOTSUPP;
2198 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2199 struct ethtool_stats *stats, u64 *data)
2201 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2202 union ibmvnic_crq crq;
2203 int i, j;
2205 memset(&crq, 0, sizeof(crq));
2206 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2207 crq.request_statistics.cmd = REQUEST_STATISTICS;
2208 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2209 crq.request_statistics.len =
2210 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2212 /* Wait for data to be written */
2213 init_completion(&adapter->stats_done);
2214 ibmvnic_send_crq(adapter, &crq);
2215 wait_for_completion(&adapter->stats_done);
2217 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2218 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2219 ibmvnic_stats[i].offset));
2221 for (j = 0; j < adapter->req_tx_queues; j++) {
2222 data[i] = adapter->tx_stats_buffers[j].packets;
2223 i++;
2224 data[i] = adapter->tx_stats_buffers[j].bytes;
2225 i++;
2226 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2227 i++;
2230 for (j = 0; j < adapter->req_rx_queues; j++) {
2231 data[i] = adapter->rx_stats_buffers[j].packets;
2232 i++;
2233 data[i] = adapter->rx_stats_buffers[j].bytes;
2234 i++;
2235 data[i] = adapter->rx_stats_buffers[j].interrupts;
2236 i++;
2240 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2241 .get_drvinfo = ibmvnic_get_drvinfo,
2242 .get_msglevel = ibmvnic_get_msglevel,
2243 .set_msglevel = ibmvnic_set_msglevel,
2244 .get_link = ibmvnic_get_link,
2245 .get_ringparam = ibmvnic_get_ringparam,
2246 .set_ringparam = ibmvnic_set_ringparam,
2247 .get_channels = ibmvnic_get_channels,
2248 .set_channels = ibmvnic_set_channels,
2249 .get_strings = ibmvnic_get_strings,
2250 .get_sset_count = ibmvnic_get_sset_count,
2251 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2252 .get_link_ksettings = ibmvnic_get_link_ksettings,
2255 /* Routines for managing CRQs/sCRQs */
2257 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2258 struct ibmvnic_sub_crq_queue *scrq)
2260 int rc;
2262 if (scrq->irq) {
2263 free_irq(scrq->irq, scrq);
2264 irq_dispose_mapping(scrq->irq);
2265 scrq->irq = 0;
2268 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2269 scrq->cur = 0;
2271 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2272 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2273 return rc;
2276 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2278 int i, rc;
2280 for (i = 0; i < adapter->req_tx_queues; i++) {
2281 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2282 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2283 if (rc)
2284 return rc;
2287 for (i = 0; i < adapter->req_rx_queues; i++) {
2288 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2289 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2290 if (rc)
2291 return rc;
2294 return rc;
2297 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2298 struct ibmvnic_sub_crq_queue *scrq)
2300 struct device *dev = &adapter->vdev->dev;
2301 long rc;
2303 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2305 /* Close the sub-crqs */
2306 do {
2307 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2308 adapter->vdev->unit_address,
2309 scrq->crq_num);
2310 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2312 if (rc) {
2313 netdev_err(adapter->netdev,
2314 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2315 scrq->crq_num, rc);
2318 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2319 DMA_BIDIRECTIONAL);
2320 free_pages((unsigned long)scrq->msgs, 2);
2321 kfree(scrq);
2324 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2325 *adapter)
2327 struct device *dev = &adapter->vdev->dev;
2328 struct ibmvnic_sub_crq_queue *scrq;
2329 int rc;
2331 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2332 if (!scrq)
2333 return NULL;
2335 scrq->msgs =
2336 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2337 if (!scrq->msgs) {
2338 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2339 goto zero_page_failed;
2342 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2343 DMA_BIDIRECTIONAL);
2344 if (dma_mapping_error(dev, scrq->msg_token)) {
2345 dev_warn(dev, "Couldn't map crq queue messages page\n");
2346 goto map_failed;
2349 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2350 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2352 if (rc == H_RESOURCE)
2353 rc = ibmvnic_reset_crq(adapter);
2355 if (rc == H_CLOSED) {
2356 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2357 } else if (rc) {
2358 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2359 goto reg_failed;
2362 scrq->adapter = adapter;
2363 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2364 spin_lock_init(&scrq->lock);
2366 netdev_dbg(adapter->netdev,
2367 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2368 scrq->crq_num, scrq->hw_irq, scrq->irq);
2370 return scrq;
2372 reg_failed:
2373 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2374 DMA_BIDIRECTIONAL);
2375 map_failed:
2376 free_pages((unsigned long)scrq->msgs, 2);
2377 zero_page_failed:
2378 kfree(scrq);
2380 return NULL;
2383 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
2385 int i;
2387 if (adapter->tx_scrq) {
2388 for (i = 0; i < adapter->req_tx_queues; i++) {
2389 if (!adapter->tx_scrq[i])
2390 continue;
2392 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2394 if (adapter->tx_scrq[i]->irq) {
2395 free_irq(adapter->tx_scrq[i]->irq,
2396 adapter->tx_scrq[i]);
2397 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2398 adapter->tx_scrq[i]->irq = 0;
2401 release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2404 kfree(adapter->tx_scrq);
2405 adapter->tx_scrq = NULL;
2408 if (adapter->rx_scrq) {
2409 for (i = 0; i < adapter->req_rx_queues; i++) {
2410 if (!adapter->rx_scrq[i])
2411 continue;
2413 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2415 if (adapter->rx_scrq[i]->irq) {
2416 free_irq(adapter->rx_scrq[i]->irq,
2417 adapter->rx_scrq[i]);
2418 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2419 adapter->rx_scrq[i]->irq = 0;
2422 release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2425 kfree(adapter->rx_scrq);
2426 adapter->rx_scrq = NULL;
2430 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2431 struct ibmvnic_sub_crq_queue *scrq)
2433 struct device *dev = &adapter->vdev->dev;
2434 unsigned long rc;
2436 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2437 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2438 if (rc)
2439 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2440 scrq->hw_irq, rc);
2441 return rc;
2444 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2445 struct ibmvnic_sub_crq_queue *scrq)
2447 struct device *dev = &adapter->vdev->dev;
2448 unsigned long rc;
2450 if (scrq->hw_irq > 0x100000000ULL) {
2451 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2452 return 1;
2455 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2456 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2457 if (rc)
2458 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2459 scrq->hw_irq, rc);
2460 return rc;
2463 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2464 struct ibmvnic_sub_crq_queue *scrq)
2466 struct device *dev = &adapter->vdev->dev;
2467 struct ibmvnic_tx_buff *txbuff;
2468 union sub_crq *next;
2469 int index;
2470 int i, j;
2471 u8 first;
2473 restart_loop:
2474 while (pending_scrq(adapter, scrq)) {
2475 unsigned int pool = scrq->pool_index;
2477 next = ibmvnic_next_scrq(adapter, scrq);
2478 for (i = 0; i < next->tx_comp.num_comps; i++) {
2479 if (next->tx_comp.rcs[i]) {
2480 dev_err(dev, "tx error %x\n",
2481 next->tx_comp.rcs[i]);
2482 continue;
2484 index = be32_to_cpu(next->tx_comp.correlators[i]);
2485 txbuff = &adapter->tx_pool[pool].tx_buff[index];
2487 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2488 if (!txbuff->data_dma[j])
2489 continue;
2491 txbuff->data_dma[j] = 0;
2493 /* if sub_crq was sent indirectly */
2494 first = txbuff->indir_arr[0].generic.first;
2495 if (first == IBMVNIC_CRQ_CMD) {
2496 dma_unmap_single(dev, txbuff->indir_dma,
2497 sizeof(txbuff->indir_arr),
2498 DMA_TO_DEVICE);
2501 if (txbuff->last_frag) {
2502 dev_kfree_skb_any(txbuff->skb);
2503 txbuff->skb = NULL;
2506 adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
2507 producer_index] = index;
2508 adapter->tx_pool[pool].producer_index =
2509 (adapter->tx_pool[pool].producer_index + 1) %
2510 adapter->req_tx_entries_per_subcrq;
2512 /* remove tx_comp scrq*/
2513 next->tx_comp.first = 0;
2515 if (atomic_sub_return(next->tx_comp.num_comps, &scrq->used) <=
2516 (adapter->req_tx_entries_per_subcrq / 2) &&
2517 __netif_subqueue_stopped(adapter->netdev,
2518 scrq->pool_index)) {
2519 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2520 netdev_info(adapter->netdev, "Started queue %d\n",
2521 scrq->pool_index);
2525 enable_scrq_irq(adapter, scrq);
2527 if (pending_scrq(adapter, scrq)) {
2528 disable_scrq_irq(adapter, scrq);
2529 goto restart_loop;
2532 return 0;
2535 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2537 struct ibmvnic_sub_crq_queue *scrq = instance;
2538 struct ibmvnic_adapter *adapter = scrq->adapter;
2540 disable_scrq_irq(adapter, scrq);
2541 ibmvnic_complete_tx(adapter, scrq);
2543 return IRQ_HANDLED;
2546 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2548 struct ibmvnic_sub_crq_queue *scrq = instance;
2549 struct ibmvnic_adapter *adapter = scrq->adapter;
2551 /* When booting a kdump kernel we can hit pending interrupts
2552 * prior to completing driver initialization.
2554 if (unlikely(adapter->state != VNIC_OPEN))
2555 return IRQ_NONE;
2557 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2559 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2560 disable_scrq_irq(adapter, scrq);
2561 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2564 return IRQ_HANDLED;
2567 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2569 struct device *dev = &adapter->vdev->dev;
2570 struct ibmvnic_sub_crq_queue *scrq;
2571 int i = 0, j = 0;
2572 int rc = 0;
2574 for (i = 0; i < adapter->req_tx_queues; i++) {
2575 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2577 scrq = adapter->tx_scrq[i];
2578 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2580 if (!scrq->irq) {
2581 rc = -EINVAL;
2582 dev_err(dev, "Error mapping irq\n");
2583 goto req_tx_irq_failed;
2586 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2587 0, "ibmvnic_tx", scrq);
2589 if (rc) {
2590 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2591 scrq->irq, rc);
2592 irq_dispose_mapping(scrq->irq);
2593 goto req_rx_irq_failed;
2597 for (i = 0; i < adapter->req_rx_queues; i++) {
2598 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2600 scrq = adapter->rx_scrq[i];
2601 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2602 if (!scrq->irq) {
2603 rc = -EINVAL;
2604 dev_err(dev, "Error mapping irq\n");
2605 goto req_rx_irq_failed;
2607 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2608 0, "ibmvnic_rx", scrq);
2609 if (rc) {
2610 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2611 scrq->irq, rc);
2612 irq_dispose_mapping(scrq->irq);
2613 goto req_rx_irq_failed;
2616 return rc;
2618 req_rx_irq_failed:
2619 for (j = 0; j < i; j++) {
2620 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2621 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2623 i = adapter->req_tx_queues;
2624 req_tx_irq_failed:
2625 for (j = 0; j < i; j++) {
2626 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2627 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2629 release_sub_crqs(adapter);
2630 return rc;
2633 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2635 struct device *dev = &adapter->vdev->dev;
2636 struct ibmvnic_sub_crq_queue **allqueues;
2637 int registered_queues = 0;
2638 int total_queues;
2639 int more = 0;
2640 int i;
2642 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2644 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2645 if (!allqueues)
2646 return -1;
2648 for (i = 0; i < total_queues; i++) {
2649 allqueues[i] = init_sub_crq_queue(adapter);
2650 if (!allqueues[i]) {
2651 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2652 break;
2654 registered_queues++;
2657 /* Make sure we were able to register the minimum number of queues */
2658 if (registered_queues <
2659 adapter->min_tx_queues + adapter->min_rx_queues) {
2660 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2661 goto tx_failed;
2664 /* Distribute the failed allocated queues*/
2665 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2666 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2667 switch (i % 3) {
2668 case 0:
2669 if (adapter->req_rx_queues > adapter->min_rx_queues)
2670 adapter->req_rx_queues--;
2671 else
2672 more++;
2673 break;
2674 case 1:
2675 if (adapter->req_tx_queues > adapter->min_tx_queues)
2676 adapter->req_tx_queues--;
2677 else
2678 more++;
2679 break;
2683 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2684 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2685 if (!adapter->tx_scrq)
2686 goto tx_failed;
2688 for (i = 0; i < adapter->req_tx_queues; i++) {
2689 adapter->tx_scrq[i] = allqueues[i];
2690 adapter->tx_scrq[i]->pool_index = i;
2693 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2694 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2695 if (!adapter->rx_scrq)
2696 goto rx_failed;
2698 for (i = 0; i < adapter->req_rx_queues; i++) {
2699 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2700 adapter->rx_scrq[i]->scrq_num = i;
2703 kfree(allqueues);
2704 return 0;
2706 rx_failed:
2707 kfree(adapter->tx_scrq);
2708 adapter->tx_scrq = NULL;
2709 tx_failed:
2710 for (i = 0; i < registered_queues; i++)
2711 release_sub_crq_queue(adapter, allqueues[i]);
2712 kfree(allqueues);
2713 return -1;
2716 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2718 struct device *dev = &adapter->vdev->dev;
2719 union ibmvnic_crq crq;
2720 int max_entries;
2722 if (!retry) {
2723 /* Sub-CRQ entries are 32 byte long */
2724 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2726 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2727 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2728 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2729 return;
2732 if (adapter->desired.mtu)
2733 adapter->req_mtu = adapter->desired.mtu;
2734 else
2735 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2737 if (!adapter->desired.tx_entries)
2738 adapter->desired.tx_entries =
2739 adapter->max_tx_entries_per_subcrq;
2740 if (!adapter->desired.rx_entries)
2741 adapter->desired.rx_entries =
2742 adapter->max_rx_add_entries_per_subcrq;
2744 max_entries = IBMVNIC_MAX_LTB_SIZE /
2745 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2747 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2748 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2749 adapter->desired.tx_entries = max_entries;
2752 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2753 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2754 adapter->desired.rx_entries = max_entries;
2757 if (adapter->desired.tx_entries)
2758 adapter->req_tx_entries_per_subcrq =
2759 adapter->desired.tx_entries;
2760 else
2761 adapter->req_tx_entries_per_subcrq =
2762 adapter->max_tx_entries_per_subcrq;
2764 if (adapter->desired.rx_entries)
2765 adapter->req_rx_add_entries_per_subcrq =
2766 adapter->desired.rx_entries;
2767 else
2768 adapter->req_rx_add_entries_per_subcrq =
2769 adapter->max_rx_add_entries_per_subcrq;
2771 if (adapter->desired.tx_queues)
2772 adapter->req_tx_queues =
2773 adapter->desired.tx_queues;
2774 else
2775 adapter->req_tx_queues =
2776 adapter->opt_tx_comp_sub_queues;
2778 if (adapter->desired.rx_queues)
2779 adapter->req_rx_queues =
2780 adapter->desired.rx_queues;
2781 else
2782 adapter->req_rx_queues =
2783 adapter->opt_rx_comp_queues;
2785 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2788 memset(&crq, 0, sizeof(crq));
2789 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2790 crq.request_capability.cmd = REQUEST_CAPABILITY;
2792 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2793 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2794 atomic_inc(&adapter->running_cap_crqs);
2795 ibmvnic_send_crq(adapter, &crq);
2797 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2798 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2799 atomic_inc(&adapter->running_cap_crqs);
2800 ibmvnic_send_crq(adapter, &crq);
2802 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
2803 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
2804 atomic_inc(&adapter->running_cap_crqs);
2805 ibmvnic_send_crq(adapter, &crq);
2807 crq.request_capability.capability =
2808 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
2809 crq.request_capability.number =
2810 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
2811 atomic_inc(&adapter->running_cap_crqs);
2812 ibmvnic_send_crq(adapter, &crq);
2814 crq.request_capability.capability =
2815 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
2816 crq.request_capability.number =
2817 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
2818 atomic_inc(&adapter->running_cap_crqs);
2819 ibmvnic_send_crq(adapter, &crq);
2821 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
2822 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
2823 atomic_inc(&adapter->running_cap_crqs);
2824 ibmvnic_send_crq(adapter, &crq);
2826 if (adapter->netdev->flags & IFF_PROMISC) {
2827 if (adapter->promisc_supported) {
2828 crq.request_capability.capability =
2829 cpu_to_be16(PROMISC_REQUESTED);
2830 crq.request_capability.number = cpu_to_be64(1);
2831 atomic_inc(&adapter->running_cap_crqs);
2832 ibmvnic_send_crq(adapter, &crq);
2834 } else {
2835 crq.request_capability.capability =
2836 cpu_to_be16(PROMISC_REQUESTED);
2837 crq.request_capability.number = cpu_to_be64(0);
2838 atomic_inc(&adapter->running_cap_crqs);
2839 ibmvnic_send_crq(adapter, &crq);
2843 static int pending_scrq(struct ibmvnic_adapter *adapter,
2844 struct ibmvnic_sub_crq_queue *scrq)
2846 union sub_crq *entry = &scrq->msgs[scrq->cur];
2848 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
2849 return 1;
2850 else
2851 return 0;
2854 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
2855 struct ibmvnic_sub_crq_queue *scrq)
2857 union sub_crq *entry;
2858 unsigned long flags;
2860 spin_lock_irqsave(&scrq->lock, flags);
2861 entry = &scrq->msgs[scrq->cur];
2862 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2863 if (++scrq->cur == scrq->size)
2864 scrq->cur = 0;
2865 } else {
2866 entry = NULL;
2868 spin_unlock_irqrestore(&scrq->lock, flags);
2870 return entry;
2873 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
2875 struct ibmvnic_crq_queue *queue = &adapter->crq;
2876 union ibmvnic_crq *crq;
2878 crq = &queue->msgs[queue->cur];
2879 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
2880 if (++queue->cur == queue->size)
2881 queue->cur = 0;
2882 } else {
2883 crq = NULL;
2886 return crq;
2889 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
2890 union sub_crq *sub_crq)
2892 unsigned int ua = adapter->vdev->unit_address;
2893 struct device *dev = &adapter->vdev->dev;
2894 u64 *u64_crq = (u64 *)sub_crq;
2895 int rc;
2897 netdev_dbg(adapter->netdev,
2898 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
2899 (unsigned long int)cpu_to_be64(remote_handle),
2900 (unsigned long int)cpu_to_be64(u64_crq[0]),
2901 (unsigned long int)cpu_to_be64(u64_crq[1]),
2902 (unsigned long int)cpu_to_be64(u64_crq[2]),
2903 (unsigned long int)cpu_to_be64(u64_crq[3]));
2905 /* Make sure the hypervisor sees the complete request */
2906 mb();
2908 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
2909 cpu_to_be64(remote_handle),
2910 cpu_to_be64(u64_crq[0]),
2911 cpu_to_be64(u64_crq[1]),
2912 cpu_to_be64(u64_crq[2]),
2913 cpu_to_be64(u64_crq[3]));
2915 if (rc) {
2916 if (rc == H_CLOSED)
2917 dev_warn(dev, "CRQ Queue closed\n");
2918 dev_err(dev, "Send error (rc=%d)\n", rc);
2921 return rc;
2924 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
2925 u64 remote_handle, u64 ioba, u64 num_entries)
2927 unsigned int ua = adapter->vdev->unit_address;
2928 struct device *dev = &adapter->vdev->dev;
2929 int rc;
2931 /* Make sure the hypervisor sees the complete request */
2932 mb();
2933 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
2934 cpu_to_be64(remote_handle),
2935 ioba, num_entries);
2937 if (rc) {
2938 if (rc == H_CLOSED)
2939 dev_warn(dev, "CRQ Queue closed\n");
2940 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
2943 return rc;
2946 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
2947 union ibmvnic_crq *crq)
2949 unsigned int ua = adapter->vdev->unit_address;
2950 struct device *dev = &adapter->vdev->dev;
2951 u64 *u64_crq = (u64 *)crq;
2952 int rc;
2954 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
2955 (unsigned long int)cpu_to_be64(u64_crq[0]),
2956 (unsigned long int)cpu_to_be64(u64_crq[1]));
2958 /* Make sure the hypervisor sees the complete request */
2959 mb();
2961 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
2962 cpu_to_be64(u64_crq[0]),
2963 cpu_to_be64(u64_crq[1]));
2965 if (rc) {
2966 if (rc == H_CLOSED) {
2967 dev_warn(dev, "CRQ Queue closed\n");
2968 if (adapter->resetting)
2969 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
2972 dev_warn(dev, "Send error (rc=%d)\n", rc);
2975 return rc;
2978 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
2980 union ibmvnic_crq crq;
2982 memset(&crq, 0, sizeof(crq));
2983 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
2984 crq.generic.cmd = IBMVNIC_CRQ_INIT;
2985 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
2987 return ibmvnic_send_crq(adapter, &crq);
2990 static int send_version_xchg(struct ibmvnic_adapter *adapter)
2992 union ibmvnic_crq crq;
2994 memset(&crq, 0, sizeof(crq));
2995 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
2996 crq.version_exchange.cmd = VERSION_EXCHANGE;
2997 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
2999 return ibmvnic_send_crq(adapter, &crq);
3002 struct vnic_login_client_data {
3003 u8 type;
3004 __be16 len;
3005 char name;
3006 } __packed;
3008 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3010 int len;
3012 /* Calculate the amount of buffer space needed for the
3013 * vnic client data in the login buffer. There are four entries,
3014 * OS name, LPAR name, device name, and a null last entry.
3016 len = 4 * sizeof(struct vnic_login_client_data);
3017 len += 6; /* "Linux" plus NULL */
3018 len += strlen(utsname()->nodename) + 1;
3019 len += strlen(adapter->netdev->name) + 1;
3021 return len;
3024 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3025 struct vnic_login_client_data *vlcd)
3027 const char *os_name = "Linux";
3028 int len;
3030 /* Type 1 - LPAR OS */
3031 vlcd->type = 1;
3032 len = strlen(os_name) + 1;
3033 vlcd->len = cpu_to_be16(len);
3034 strncpy(&vlcd->name, os_name, len);
3035 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3037 /* Type 2 - LPAR name */
3038 vlcd->type = 2;
3039 len = strlen(utsname()->nodename) + 1;
3040 vlcd->len = cpu_to_be16(len);
3041 strncpy(&vlcd->name, utsname()->nodename, len);
3042 vlcd = (struct vnic_login_client_data *)((char *)&vlcd->name + len);
3044 /* Type 3 - device name */
3045 vlcd->type = 3;
3046 len = strlen(adapter->netdev->name) + 1;
3047 vlcd->len = cpu_to_be16(len);
3048 strncpy(&vlcd->name, adapter->netdev->name, len);
3051 static void send_login(struct ibmvnic_adapter *adapter)
3053 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3054 struct ibmvnic_login_buffer *login_buffer;
3055 struct device *dev = &adapter->vdev->dev;
3056 dma_addr_t rsp_buffer_token;
3057 dma_addr_t buffer_token;
3058 size_t rsp_buffer_size;
3059 union ibmvnic_crq crq;
3060 size_t buffer_size;
3061 __be64 *tx_list_p;
3062 __be64 *rx_list_p;
3063 int client_data_len;
3064 struct vnic_login_client_data *vlcd;
3065 int i;
3067 release_login_rsp_buffer(adapter);
3068 client_data_len = vnic_client_data_len(adapter);
3070 buffer_size =
3071 sizeof(struct ibmvnic_login_buffer) +
3072 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3073 client_data_len;
3075 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3076 if (!login_buffer)
3077 goto buf_alloc_failed;
3079 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3080 DMA_TO_DEVICE);
3081 if (dma_mapping_error(dev, buffer_token)) {
3082 dev_err(dev, "Couldn't map login buffer\n");
3083 goto buf_map_failed;
3086 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3087 sizeof(u64) * adapter->req_tx_queues +
3088 sizeof(u64) * adapter->req_rx_queues +
3089 sizeof(u64) * adapter->req_rx_queues +
3090 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3092 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3093 if (!login_rsp_buffer)
3094 goto buf_rsp_alloc_failed;
3096 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3097 rsp_buffer_size, DMA_FROM_DEVICE);
3098 if (dma_mapping_error(dev, rsp_buffer_token)) {
3099 dev_err(dev, "Couldn't map login rsp buffer\n");
3100 goto buf_rsp_map_failed;
3103 adapter->login_buf = login_buffer;
3104 adapter->login_buf_token = buffer_token;
3105 adapter->login_buf_sz = buffer_size;
3106 adapter->login_rsp_buf = login_rsp_buffer;
3107 adapter->login_rsp_buf_token = rsp_buffer_token;
3108 adapter->login_rsp_buf_sz = rsp_buffer_size;
3110 login_buffer->len = cpu_to_be32(buffer_size);
3111 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3112 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3113 login_buffer->off_txcomp_subcrqs =
3114 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3115 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3116 login_buffer->off_rxcomp_subcrqs =
3117 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3118 sizeof(u64) * adapter->req_tx_queues);
3119 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3120 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3122 tx_list_p = (__be64 *)((char *)login_buffer +
3123 sizeof(struct ibmvnic_login_buffer));
3124 rx_list_p = (__be64 *)((char *)login_buffer +
3125 sizeof(struct ibmvnic_login_buffer) +
3126 sizeof(u64) * adapter->req_tx_queues);
3128 for (i = 0; i < adapter->req_tx_queues; i++) {
3129 if (adapter->tx_scrq[i]) {
3130 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3131 crq_num);
3135 for (i = 0; i < adapter->req_rx_queues; i++) {
3136 if (adapter->rx_scrq[i]) {
3137 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3138 crq_num);
3142 /* Insert vNIC login client data */
3143 vlcd = (struct vnic_login_client_data *)
3144 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3145 login_buffer->client_data_offset =
3146 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3147 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3149 vnic_add_client_data(adapter, vlcd);
3151 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3152 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3153 netdev_dbg(adapter->netdev, "%016lx\n",
3154 ((unsigned long int *)(adapter->login_buf))[i]);
3157 memset(&crq, 0, sizeof(crq));
3158 crq.login.first = IBMVNIC_CRQ_CMD;
3159 crq.login.cmd = LOGIN;
3160 crq.login.ioba = cpu_to_be32(buffer_token);
3161 crq.login.len = cpu_to_be32(buffer_size);
3162 ibmvnic_send_crq(adapter, &crq);
3164 return;
3166 buf_rsp_map_failed:
3167 kfree(login_rsp_buffer);
3168 buf_rsp_alloc_failed:
3169 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3170 buf_map_failed:
3171 kfree(login_buffer);
3172 buf_alloc_failed:
3173 return;
3176 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3177 u32 len, u8 map_id)
3179 union ibmvnic_crq crq;
3181 memset(&crq, 0, sizeof(crq));
3182 crq.request_map.first = IBMVNIC_CRQ_CMD;
3183 crq.request_map.cmd = REQUEST_MAP;
3184 crq.request_map.map_id = map_id;
3185 crq.request_map.ioba = cpu_to_be32(addr);
3186 crq.request_map.len = cpu_to_be32(len);
3187 ibmvnic_send_crq(adapter, &crq);
3190 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3192 union ibmvnic_crq crq;
3194 memset(&crq, 0, sizeof(crq));
3195 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3196 crq.request_unmap.cmd = REQUEST_UNMAP;
3197 crq.request_unmap.map_id = map_id;
3198 ibmvnic_send_crq(adapter, &crq);
3201 static void send_map_query(struct ibmvnic_adapter *adapter)
3203 union ibmvnic_crq crq;
3205 memset(&crq, 0, sizeof(crq));
3206 crq.query_map.first = IBMVNIC_CRQ_CMD;
3207 crq.query_map.cmd = QUERY_MAP;
3208 ibmvnic_send_crq(adapter, &crq);
3211 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3212 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3214 union ibmvnic_crq crq;
3216 atomic_set(&adapter->running_cap_crqs, 0);
3217 memset(&crq, 0, sizeof(crq));
3218 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3219 crq.query_capability.cmd = QUERY_CAPABILITY;
3221 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3222 atomic_inc(&adapter->running_cap_crqs);
3223 ibmvnic_send_crq(adapter, &crq);
3225 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3226 atomic_inc(&adapter->running_cap_crqs);
3227 ibmvnic_send_crq(adapter, &crq);
3229 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3230 atomic_inc(&adapter->running_cap_crqs);
3231 ibmvnic_send_crq(adapter, &crq);
3233 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3234 atomic_inc(&adapter->running_cap_crqs);
3235 ibmvnic_send_crq(adapter, &crq);
3237 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3238 atomic_inc(&adapter->running_cap_crqs);
3239 ibmvnic_send_crq(adapter, &crq);
3241 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3242 atomic_inc(&adapter->running_cap_crqs);
3243 ibmvnic_send_crq(adapter, &crq);
3245 crq.query_capability.capability =
3246 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3247 atomic_inc(&adapter->running_cap_crqs);
3248 ibmvnic_send_crq(adapter, &crq);
3250 crq.query_capability.capability =
3251 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3252 atomic_inc(&adapter->running_cap_crqs);
3253 ibmvnic_send_crq(adapter, &crq);
3255 crq.query_capability.capability =
3256 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3257 atomic_inc(&adapter->running_cap_crqs);
3258 ibmvnic_send_crq(adapter, &crq);
3260 crq.query_capability.capability =
3261 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3262 atomic_inc(&adapter->running_cap_crqs);
3263 ibmvnic_send_crq(adapter, &crq);
3265 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3266 atomic_inc(&adapter->running_cap_crqs);
3267 ibmvnic_send_crq(adapter, &crq);
3269 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3270 atomic_inc(&adapter->running_cap_crqs);
3271 ibmvnic_send_crq(adapter, &crq);
3273 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3274 atomic_inc(&adapter->running_cap_crqs);
3275 ibmvnic_send_crq(adapter, &crq);
3277 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3278 atomic_inc(&adapter->running_cap_crqs);
3279 ibmvnic_send_crq(adapter, &crq);
3281 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3282 atomic_inc(&adapter->running_cap_crqs);
3283 ibmvnic_send_crq(adapter, &crq);
3285 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3286 atomic_inc(&adapter->running_cap_crqs);
3287 ibmvnic_send_crq(adapter, &crq);
3289 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3290 atomic_inc(&adapter->running_cap_crqs);
3291 ibmvnic_send_crq(adapter, &crq);
3293 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3294 atomic_inc(&adapter->running_cap_crqs);
3295 ibmvnic_send_crq(adapter, &crq);
3297 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3298 atomic_inc(&adapter->running_cap_crqs);
3299 ibmvnic_send_crq(adapter, &crq);
3301 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3302 atomic_inc(&adapter->running_cap_crqs);
3303 ibmvnic_send_crq(adapter, &crq);
3305 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3306 atomic_inc(&adapter->running_cap_crqs);
3307 ibmvnic_send_crq(adapter, &crq);
3309 crq.query_capability.capability =
3310 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3311 atomic_inc(&adapter->running_cap_crqs);
3312 ibmvnic_send_crq(adapter, &crq);
3314 crq.query_capability.capability =
3315 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3316 atomic_inc(&adapter->running_cap_crqs);
3317 ibmvnic_send_crq(adapter, &crq);
3319 crq.query_capability.capability =
3320 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3321 atomic_inc(&adapter->running_cap_crqs);
3322 ibmvnic_send_crq(adapter, &crq);
3324 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3325 atomic_inc(&adapter->running_cap_crqs);
3326 ibmvnic_send_crq(adapter, &crq);
3329 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3330 struct ibmvnic_adapter *adapter)
3332 struct device *dev = &adapter->vdev->dev;
3334 if (crq->get_vpd_size_rsp.rc.code) {
3335 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3336 crq->get_vpd_size_rsp.rc.code);
3337 complete(&adapter->fw_done);
3338 return;
3341 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3342 complete(&adapter->fw_done);
3345 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3346 struct ibmvnic_adapter *adapter)
3348 struct device *dev = &adapter->vdev->dev;
3349 unsigned char *substr = NULL;
3350 u8 fw_level_len = 0;
3352 memset(adapter->fw_version, 0, 32);
3354 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3355 DMA_FROM_DEVICE);
3357 if (crq->get_vpd_rsp.rc.code) {
3358 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3359 crq->get_vpd_rsp.rc.code);
3360 goto complete;
3363 /* get the position of the firmware version info
3364 * located after the ASCII 'RM' substring in the buffer
3366 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3367 if (!substr) {
3368 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3369 goto complete;
3372 /* get length of firmware level ASCII substring */
3373 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3374 fw_level_len = *(substr + 2);
3375 } else {
3376 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3377 goto complete;
3380 /* copy firmware version string from vpd into adapter */
3381 if ((substr + 3 + fw_level_len) <
3382 (adapter->vpd->buff + adapter->vpd->len)) {
3383 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3384 } else {
3385 dev_info(dev, "FW substr extrapolated VPD buff\n");
3388 complete:
3389 if (adapter->fw_version[0] == '\0')
3390 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3391 complete(&adapter->fw_done);
3394 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3396 struct device *dev = &adapter->vdev->dev;
3397 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3398 union ibmvnic_crq crq;
3399 int i;
3401 dma_unmap_single(dev, adapter->ip_offload_tok,
3402 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3404 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3405 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3406 netdev_dbg(adapter->netdev, "%016lx\n",
3407 ((unsigned long int *)(buf))[i]);
3409 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3410 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3411 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3412 buf->tcp_ipv4_chksum);
3413 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3414 buf->tcp_ipv6_chksum);
3415 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3416 buf->udp_ipv4_chksum);
3417 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3418 buf->udp_ipv6_chksum);
3419 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3420 buf->large_tx_ipv4);
3421 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3422 buf->large_tx_ipv6);
3423 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3424 buf->large_rx_ipv4);
3425 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3426 buf->large_rx_ipv6);
3427 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3428 buf->max_ipv4_header_size);
3429 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3430 buf->max_ipv6_header_size);
3431 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3432 buf->max_tcp_header_size);
3433 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3434 buf->max_udp_header_size);
3435 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3436 buf->max_large_tx_size);
3437 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3438 buf->max_large_rx_size);
3439 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3440 buf->ipv6_extension_header);
3441 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3442 buf->tcp_pseudosum_req);
3443 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3444 buf->num_ipv6_ext_headers);
3445 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3446 buf->off_ipv6_ext_headers);
3448 adapter->ip_offload_ctrl_tok =
3449 dma_map_single(dev, &adapter->ip_offload_ctrl,
3450 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3452 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3453 dev_err(dev, "Couldn't map ip offload control buffer\n");
3454 return;
3457 adapter->ip_offload_ctrl.len =
3458 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3459 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3460 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3461 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3462 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3463 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3464 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3465 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3466 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3467 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3469 /* large_rx disabled for now, additional features needed */
3470 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3471 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3473 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3475 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3476 adapter->netdev->features |= NETIF_F_IP_CSUM;
3478 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3479 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3481 if ((adapter->netdev->features &
3482 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3483 adapter->netdev->features |= NETIF_F_RXCSUM;
3485 if (buf->large_tx_ipv4)
3486 adapter->netdev->features |= NETIF_F_TSO;
3487 if (buf->large_tx_ipv6)
3488 adapter->netdev->features |= NETIF_F_TSO6;
3490 adapter->netdev->hw_features |= adapter->netdev->features;
3492 memset(&crq, 0, sizeof(crq));
3493 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3494 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3495 crq.control_ip_offload.len =
3496 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3497 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3498 ibmvnic_send_crq(adapter, &crq);
3501 static void handle_error_info_rsp(union ibmvnic_crq *crq,
3502 struct ibmvnic_adapter *adapter)
3504 struct device *dev = &adapter->vdev->dev;
3505 struct ibmvnic_error_buff *error_buff, *tmp;
3506 unsigned long flags;
3507 bool found = false;
3508 int i;
3510 if (!crq->request_error_rsp.rc.code) {
3511 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3512 crq->request_error_rsp.rc.code);
3513 return;
3516 spin_lock_irqsave(&adapter->error_list_lock, flags);
3517 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3518 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3519 found = true;
3520 list_del(&error_buff->list);
3521 break;
3523 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3525 if (!found) {
3526 dev_err(dev, "Couldn't find error id %x\n",
3527 be32_to_cpu(crq->request_error_rsp.error_id));
3528 return;
3531 dev_err(dev, "Detailed info for error id %x:",
3532 be32_to_cpu(crq->request_error_rsp.error_id));
3534 for (i = 0; i < error_buff->len; i++) {
3535 pr_cont("%02x", (int)error_buff->buff[i]);
3536 if (i % 8 == 7)
3537 pr_cont(" ");
3539 pr_cont("\n");
3541 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3542 DMA_FROM_DEVICE);
3543 kfree(error_buff->buff);
3544 kfree(error_buff);
3547 static void request_error_information(struct ibmvnic_adapter *adapter,
3548 union ibmvnic_crq *err_crq)
3550 struct device *dev = &adapter->vdev->dev;
3551 struct net_device *netdev = adapter->netdev;
3552 struct ibmvnic_error_buff *error_buff;
3553 unsigned long timeout = msecs_to_jiffies(30000);
3554 union ibmvnic_crq crq;
3555 unsigned long flags;
3556 int rc, detail_len;
3558 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3559 if (!error_buff)
3560 return;
3562 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3563 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3564 if (!error_buff->buff) {
3565 kfree(error_buff);
3566 return;
3569 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3570 DMA_FROM_DEVICE);
3571 if (dma_mapping_error(dev, error_buff->dma)) {
3572 netdev_err(netdev, "Couldn't map error buffer\n");
3573 kfree(error_buff->buff);
3574 kfree(error_buff);
3575 return;
3578 error_buff->len = detail_len;
3579 error_buff->error_id = err_crq->error_indication.error_id;
3581 spin_lock_irqsave(&adapter->error_list_lock, flags);
3582 list_add_tail(&error_buff->list, &adapter->errors);
3583 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3585 memset(&crq, 0, sizeof(crq));
3586 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3587 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3588 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3589 crq.request_error_info.len = cpu_to_be32(detail_len);
3590 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3592 rc = ibmvnic_send_crq(adapter, &crq);
3593 if (rc) {
3594 netdev_err(netdev, "failed to request error information\n");
3595 goto err_info_fail;
3598 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3599 netdev_err(netdev, "timeout waiting for error information\n");
3600 goto err_info_fail;
3603 return;
3605 err_info_fail:
3606 spin_lock_irqsave(&adapter->error_list_lock, flags);
3607 list_del(&error_buff->list);
3608 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3610 kfree(error_buff->buff);
3611 kfree(error_buff);
3614 static void handle_error_indication(union ibmvnic_crq *crq,
3615 struct ibmvnic_adapter *adapter)
3617 struct device *dev = &adapter->vdev->dev;
3619 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3620 crq->error_indication.flags
3621 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3622 be32_to_cpu(crq->error_indication.error_id),
3623 be16_to_cpu(crq->error_indication.error_cause));
3625 if (be32_to_cpu(crq->error_indication.error_id))
3626 request_error_information(adapter, crq);
3628 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3629 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3630 else
3631 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3634 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3635 struct ibmvnic_adapter *adapter)
3637 struct net_device *netdev = adapter->netdev;
3638 struct device *dev = &adapter->vdev->dev;
3639 long rc;
3641 rc = crq->change_mac_addr_rsp.rc.code;
3642 if (rc) {
3643 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3644 goto out;
3646 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3647 ETH_ALEN);
3648 out:
3649 complete(&adapter->fw_done);
3650 return rc;
3653 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3654 struct ibmvnic_adapter *adapter)
3656 struct device *dev = &adapter->vdev->dev;
3657 u64 *req_value;
3658 char *name;
3660 atomic_dec(&adapter->running_cap_crqs);
3661 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3662 case REQ_TX_QUEUES:
3663 req_value = &adapter->req_tx_queues;
3664 name = "tx";
3665 break;
3666 case REQ_RX_QUEUES:
3667 req_value = &adapter->req_rx_queues;
3668 name = "rx";
3669 break;
3670 case REQ_RX_ADD_QUEUES:
3671 req_value = &adapter->req_rx_add_queues;
3672 name = "rx_add";
3673 break;
3674 case REQ_TX_ENTRIES_PER_SUBCRQ:
3675 req_value = &adapter->req_tx_entries_per_subcrq;
3676 name = "tx_entries_per_subcrq";
3677 break;
3678 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3679 req_value = &adapter->req_rx_add_entries_per_subcrq;
3680 name = "rx_add_entries_per_subcrq";
3681 break;
3682 case REQ_MTU:
3683 req_value = &adapter->req_mtu;
3684 name = "mtu";
3685 break;
3686 case PROMISC_REQUESTED:
3687 req_value = &adapter->promisc;
3688 name = "promisc";
3689 break;
3690 default:
3691 dev_err(dev, "Got invalid cap request rsp %d\n",
3692 crq->request_capability.capability);
3693 return;
3696 switch (crq->request_capability_rsp.rc.code) {
3697 case SUCCESS:
3698 break;
3699 case PARTIALSUCCESS:
3700 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3701 *req_value,
3702 (long int)be64_to_cpu(crq->request_capability_rsp.
3703 number), name);
3705 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3706 REQ_MTU) {
3707 pr_err("mtu of %llu is not supported. Reverting.\n",
3708 *req_value);
3709 *req_value = adapter->fallback.mtu;
3710 } else {
3711 *req_value =
3712 be64_to_cpu(crq->request_capability_rsp.number);
3715 ibmvnic_send_req_caps(adapter, 1);
3716 return;
3717 default:
3718 dev_err(dev, "Error %d in request cap rsp\n",
3719 crq->request_capability_rsp.rc.code);
3720 return;
3723 /* Done receiving requested capabilities, query IP offload support */
3724 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3725 union ibmvnic_crq newcrq;
3726 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3727 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3728 &adapter->ip_offload_buf;
3730 adapter->wait_capability = false;
3731 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3732 buf_sz,
3733 DMA_FROM_DEVICE);
3735 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3736 if (!firmware_has_feature(FW_FEATURE_CMO))
3737 dev_err(dev, "Couldn't map offload buffer\n");
3738 return;
3741 memset(&newcrq, 0, sizeof(newcrq));
3742 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3743 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3744 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3745 newcrq.query_ip_offload.ioba =
3746 cpu_to_be32(adapter->ip_offload_tok);
3748 ibmvnic_send_crq(adapter, &newcrq);
3752 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3753 struct ibmvnic_adapter *adapter)
3755 struct device *dev = &adapter->vdev->dev;
3756 struct net_device *netdev = adapter->netdev;
3757 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3758 struct ibmvnic_login_buffer *login = adapter->login_buf;
3759 int i;
3761 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3762 DMA_BIDIRECTIONAL);
3763 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3764 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
3766 /* If the number of queues requested can't be allocated by the
3767 * server, the login response will return with code 1. We will need
3768 * to resend the login buffer with fewer queues requested.
3770 if (login_rsp_crq->generic.rc.code) {
3771 adapter->renegotiate = true;
3772 complete(&adapter->init_done);
3773 return 0;
3776 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3778 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3779 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3780 netdev_dbg(adapter->netdev, "%016lx\n",
3781 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3784 /* Sanity checks */
3785 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3786 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3787 adapter->req_rx_add_queues !=
3788 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3789 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3790 ibmvnic_remove(adapter->vdev);
3791 return -EIO;
3793 release_login_buffer(adapter);
3794 complete(&adapter->init_done);
3796 return 0;
3799 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
3800 struct ibmvnic_adapter *adapter)
3802 struct device *dev = &adapter->vdev->dev;
3803 long rc;
3805 rc = crq->request_unmap_rsp.rc.code;
3806 if (rc)
3807 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
3810 static void handle_query_map_rsp(union ibmvnic_crq *crq,
3811 struct ibmvnic_adapter *adapter)
3813 struct net_device *netdev = adapter->netdev;
3814 struct device *dev = &adapter->vdev->dev;
3815 long rc;
3817 rc = crq->query_map_rsp.rc.code;
3818 if (rc) {
3819 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
3820 return;
3822 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
3823 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
3824 crq->query_map_rsp.free_pages);
3827 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
3828 struct ibmvnic_adapter *adapter)
3830 struct net_device *netdev = adapter->netdev;
3831 struct device *dev = &adapter->vdev->dev;
3832 long rc;
3834 atomic_dec(&adapter->running_cap_crqs);
3835 netdev_dbg(netdev, "Outstanding queries: %d\n",
3836 atomic_read(&adapter->running_cap_crqs));
3837 rc = crq->query_capability.rc.code;
3838 if (rc) {
3839 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
3840 goto out;
3843 switch (be16_to_cpu(crq->query_capability.capability)) {
3844 case MIN_TX_QUEUES:
3845 adapter->min_tx_queues =
3846 be64_to_cpu(crq->query_capability.number);
3847 netdev_dbg(netdev, "min_tx_queues = %lld\n",
3848 adapter->min_tx_queues);
3849 break;
3850 case MIN_RX_QUEUES:
3851 adapter->min_rx_queues =
3852 be64_to_cpu(crq->query_capability.number);
3853 netdev_dbg(netdev, "min_rx_queues = %lld\n",
3854 adapter->min_rx_queues);
3855 break;
3856 case MIN_RX_ADD_QUEUES:
3857 adapter->min_rx_add_queues =
3858 be64_to_cpu(crq->query_capability.number);
3859 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
3860 adapter->min_rx_add_queues);
3861 break;
3862 case MAX_TX_QUEUES:
3863 adapter->max_tx_queues =
3864 be64_to_cpu(crq->query_capability.number);
3865 netdev_dbg(netdev, "max_tx_queues = %lld\n",
3866 adapter->max_tx_queues);
3867 break;
3868 case MAX_RX_QUEUES:
3869 adapter->max_rx_queues =
3870 be64_to_cpu(crq->query_capability.number);
3871 netdev_dbg(netdev, "max_rx_queues = %lld\n",
3872 adapter->max_rx_queues);
3873 break;
3874 case MAX_RX_ADD_QUEUES:
3875 adapter->max_rx_add_queues =
3876 be64_to_cpu(crq->query_capability.number);
3877 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
3878 adapter->max_rx_add_queues);
3879 break;
3880 case MIN_TX_ENTRIES_PER_SUBCRQ:
3881 adapter->min_tx_entries_per_subcrq =
3882 be64_to_cpu(crq->query_capability.number);
3883 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
3884 adapter->min_tx_entries_per_subcrq);
3885 break;
3886 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
3887 adapter->min_rx_add_entries_per_subcrq =
3888 be64_to_cpu(crq->query_capability.number);
3889 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
3890 adapter->min_rx_add_entries_per_subcrq);
3891 break;
3892 case MAX_TX_ENTRIES_PER_SUBCRQ:
3893 adapter->max_tx_entries_per_subcrq =
3894 be64_to_cpu(crq->query_capability.number);
3895 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
3896 adapter->max_tx_entries_per_subcrq);
3897 break;
3898 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
3899 adapter->max_rx_add_entries_per_subcrq =
3900 be64_to_cpu(crq->query_capability.number);
3901 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
3902 adapter->max_rx_add_entries_per_subcrq);
3903 break;
3904 case TCP_IP_OFFLOAD:
3905 adapter->tcp_ip_offload =
3906 be64_to_cpu(crq->query_capability.number);
3907 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
3908 adapter->tcp_ip_offload);
3909 break;
3910 case PROMISC_SUPPORTED:
3911 adapter->promisc_supported =
3912 be64_to_cpu(crq->query_capability.number);
3913 netdev_dbg(netdev, "promisc_supported = %lld\n",
3914 adapter->promisc_supported);
3915 break;
3916 case MIN_MTU:
3917 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
3918 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3919 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
3920 break;
3921 case MAX_MTU:
3922 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
3923 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3924 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
3925 break;
3926 case MAX_MULTICAST_FILTERS:
3927 adapter->max_multicast_filters =
3928 be64_to_cpu(crq->query_capability.number);
3929 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
3930 adapter->max_multicast_filters);
3931 break;
3932 case VLAN_HEADER_INSERTION:
3933 adapter->vlan_header_insertion =
3934 be64_to_cpu(crq->query_capability.number);
3935 if (adapter->vlan_header_insertion)
3936 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
3937 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
3938 adapter->vlan_header_insertion);
3939 break;
3940 case RX_VLAN_HEADER_INSERTION:
3941 adapter->rx_vlan_header_insertion =
3942 be64_to_cpu(crq->query_capability.number);
3943 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
3944 adapter->rx_vlan_header_insertion);
3945 break;
3946 case MAX_TX_SG_ENTRIES:
3947 adapter->max_tx_sg_entries =
3948 be64_to_cpu(crq->query_capability.number);
3949 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
3950 adapter->max_tx_sg_entries);
3951 break;
3952 case RX_SG_SUPPORTED:
3953 adapter->rx_sg_supported =
3954 be64_to_cpu(crq->query_capability.number);
3955 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
3956 adapter->rx_sg_supported);
3957 break;
3958 case OPT_TX_COMP_SUB_QUEUES:
3959 adapter->opt_tx_comp_sub_queues =
3960 be64_to_cpu(crq->query_capability.number);
3961 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
3962 adapter->opt_tx_comp_sub_queues);
3963 break;
3964 case OPT_RX_COMP_QUEUES:
3965 adapter->opt_rx_comp_queues =
3966 be64_to_cpu(crq->query_capability.number);
3967 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
3968 adapter->opt_rx_comp_queues);
3969 break;
3970 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
3971 adapter->opt_rx_bufadd_q_per_rx_comp_q =
3972 be64_to_cpu(crq->query_capability.number);
3973 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
3974 adapter->opt_rx_bufadd_q_per_rx_comp_q);
3975 break;
3976 case OPT_TX_ENTRIES_PER_SUBCRQ:
3977 adapter->opt_tx_entries_per_subcrq =
3978 be64_to_cpu(crq->query_capability.number);
3979 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
3980 adapter->opt_tx_entries_per_subcrq);
3981 break;
3982 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
3983 adapter->opt_rxba_entries_per_subcrq =
3984 be64_to_cpu(crq->query_capability.number);
3985 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
3986 adapter->opt_rxba_entries_per_subcrq);
3987 break;
3988 case TX_RX_DESC_REQ:
3989 adapter->tx_rx_desc_req = crq->query_capability.number;
3990 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
3991 adapter->tx_rx_desc_req);
3992 break;
3994 default:
3995 netdev_err(netdev, "Got invalid cap rsp %d\n",
3996 crq->query_capability.capability);
3999 out:
4000 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4001 adapter->wait_capability = false;
4002 ibmvnic_send_req_caps(adapter, 0);
4006 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4007 struct ibmvnic_adapter *adapter)
4009 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4010 struct net_device *netdev = adapter->netdev;
4011 struct device *dev = &adapter->vdev->dev;
4012 u64 *u64_crq = (u64 *)crq;
4013 long rc;
4015 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4016 (unsigned long int)cpu_to_be64(u64_crq[0]),
4017 (unsigned long int)cpu_to_be64(u64_crq[1]));
4018 switch (gen_crq->first) {
4019 case IBMVNIC_CRQ_INIT_RSP:
4020 switch (gen_crq->cmd) {
4021 case IBMVNIC_CRQ_INIT:
4022 dev_info(dev, "Partner initialized\n");
4023 adapter->from_passive_init = true;
4024 complete(&adapter->init_done);
4025 break;
4026 case IBMVNIC_CRQ_INIT_COMPLETE:
4027 dev_info(dev, "Partner initialization complete\n");
4028 send_version_xchg(adapter);
4029 break;
4030 default:
4031 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4033 return;
4034 case IBMVNIC_CRQ_XPORT_EVENT:
4035 netif_carrier_off(netdev);
4036 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4037 dev_info(dev, "Migrated, re-enabling adapter\n");
4038 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4039 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4040 dev_info(dev, "Backing device failover detected\n");
4041 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4042 } else {
4043 /* The adapter lost the connection */
4044 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4045 gen_crq->cmd);
4046 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4048 return;
4049 case IBMVNIC_CRQ_CMD_RSP:
4050 break;
4051 default:
4052 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4053 gen_crq->first);
4054 return;
4057 switch (gen_crq->cmd) {
4058 case VERSION_EXCHANGE_RSP:
4059 rc = crq->version_exchange_rsp.rc.code;
4060 if (rc) {
4061 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4062 break;
4064 dev_info(dev, "Partner protocol version is %d\n",
4065 crq->version_exchange_rsp.version);
4066 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4067 ibmvnic_version)
4068 ibmvnic_version =
4069 be16_to_cpu(crq->version_exchange_rsp.version);
4070 send_cap_queries(adapter);
4071 break;
4072 case QUERY_CAPABILITY_RSP:
4073 handle_query_cap_rsp(crq, adapter);
4074 break;
4075 case QUERY_MAP_RSP:
4076 handle_query_map_rsp(crq, adapter);
4077 break;
4078 case REQUEST_MAP_RSP:
4079 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4080 complete(&adapter->fw_done);
4081 break;
4082 case REQUEST_UNMAP_RSP:
4083 handle_request_unmap_rsp(crq, adapter);
4084 break;
4085 case REQUEST_CAPABILITY_RSP:
4086 handle_request_cap_rsp(crq, adapter);
4087 break;
4088 case LOGIN_RSP:
4089 netdev_dbg(netdev, "Got Login Response\n");
4090 handle_login_rsp(crq, adapter);
4091 break;
4092 case LOGICAL_LINK_STATE_RSP:
4093 netdev_dbg(netdev,
4094 "Got Logical Link State Response, state: %d rc: %d\n",
4095 crq->logical_link_state_rsp.link_state,
4096 crq->logical_link_state_rsp.rc.code);
4097 adapter->logical_link_state =
4098 crq->logical_link_state_rsp.link_state;
4099 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4100 complete(&adapter->init_done);
4101 break;
4102 case LINK_STATE_INDICATION:
4103 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4104 adapter->phys_link_state =
4105 crq->link_state_indication.phys_link_state;
4106 adapter->logical_link_state =
4107 crq->link_state_indication.logical_link_state;
4108 break;
4109 case CHANGE_MAC_ADDR_RSP:
4110 netdev_dbg(netdev, "Got MAC address change Response\n");
4111 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4112 break;
4113 case ERROR_INDICATION:
4114 netdev_dbg(netdev, "Got Error Indication\n");
4115 handle_error_indication(crq, adapter);
4116 break;
4117 case REQUEST_ERROR_RSP:
4118 netdev_dbg(netdev, "Got Error Detail Response\n");
4119 handle_error_info_rsp(crq, adapter);
4120 break;
4121 case REQUEST_STATISTICS_RSP:
4122 netdev_dbg(netdev, "Got Statistics Response\n");
4123 complete(&adapter->stats_done);
4124 break;
4125 case QUERY_IP_OFFLOAD_RSP:
4126 netdev_dbg(netdev, "Got Query IP offload Response\n");
4127 handle_query_ip_offload_rsp(adapter);
4128 break;
4129 case MULTICAST_CTRL_RSP:
4130 netdev_dbg(netdev, "Got multicast control Response\n");
4131 break;
4132 case CONTROL_IP_OFFLOAD_RSP:
4133 netdev_dbg(netdev, "Got Control IP offload Response\n");
4134 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4135 sizeof(adapter->ip_offload_ctrl),
4136 DMA_TO_DEVICE);
4137 complete(&adapter->init_done);
4138 break;
4139 case COLLECT_FW_TRACE_RSP:
4140 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4141 complete(&adapter->fw_done);
4142 break;
4143 case GET_VPD_SIZE_RSP:
4144 handle_vpd_size_rsp(crq, adapter);
4145 break;
4146 case GET_VPD_RSP:
4147 handle_vpd_rsp(crq, adapter);
4148 break;
4149 default:
4150 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4151 gen_crq->cmd);
4155 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4157 struct ibmvnic_adapter *adapter = instance;
4159 tasklet_schedule(&adapter->tasklet);
4160 return IRQ_HANDLED;
4163 static void ibmvnic_tasklet(void *data)
4165 struct ibmvnic_adapter *adapter = data;
4166 struct ibmvnic_crq_queue *queue = &adapter->crq;
4167 union ibmvnic_crq *crq;
4168 unsigned long flags;
4169 bool done = false;
4171 spin_lock_irqsave(&queue->lock, flags);
4172 while (!done) {
4173 /* Pull all the valid messages off the CRQ */
4174 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4175 ibmvnic_handle_crq(crq, adapter);
4176 crq->generic.first = 0;
4179 /* remain in tasklet until all
4180 * capabilities responses are received
4182 if (!adapter->wait_capability)
4183 done = true;
4185 /* if capabilities CRQ's were sent in this tasklet, the following
4186 * tasklet must wait until all responses are received
4188 if (atomic_read(&adapter->running_cap_crqs) != 0)
4189 adapter->wait_capability = true;
4190 spin_unlock_irqrestore(&queue->lock, flags);
4193 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4195 struct vio_dev *vdev = adapter->vdev;
4196 int rc;
4198 do {
4199 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4200 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4202 if (rc)
4203 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4205 return rc;
4208 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4210 struct ibmvnic_crq_queue *crq = &adapter->crq;
4211 struct device *dev = &adapter->vdev->dev;
4212 struct vio_dev *vdev = adapter->vdev;
4213 int rc;
4215 /* Close the CRQ */
4216 do {
4217 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4218 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4220 /* Clean out the queue */
4221 memset(crq->msgs, 0, PAGE_SIZE);
4222 crq->cur = 0;
4224 /* And re-open it again */
4225 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4226 crq->msg_token, PAGE_SIZE);
4228 if (rc == H_CLOSED)
4229 /* Adapter is good, but other end is not ready */
4230 dev_warn(dev, "Partner adapter not ready\n");
4231 else if (rc != 0)
4232 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4234 return rc;
4237 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4239 struct ibmvnic_crq_queue *crq = &adapter->crq;
4240 struct vio_dev *vdev = adapter->vdev;
4241 long rc;
4243 if (!crq->msgs)
4244 return;
4246 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4247 free_irq(vdev->irq, adapter);
4248 tasklet_kill(&adapter->tasklet);
4249 do {
4250 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4251 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4253 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4254 DMA_BIDIRECTIONAL);
4255 free_page((unsigned long)crq->msgs);
4256 crq->msgs = NULL;
4259 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4261 struct ibmvnic_crq_queue *crq = &adapter->crq;
4262 struct device *dev = &adapter->vdev->dev;
4263 struct vio_dev *vdev = adapter->vdev;
4264 int rc, retrc = -ENOMEM;
4266 if (crq->msgs)
4267 return 0;
4269 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4270 /* Should we allocate more than one page? */
4272 if (!crq->msgs)
4273 return -ENOMEM;
4275 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4276 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4277 DMA_BIDIRECTIONAL);
4278 if (dma_mapping_error(dev, crq->msg_token))
4279 goto map_failed;
4281 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4282 crq->msg_token, PAGE_SIZE);
4284 if (rc == H_RESOURCE)
4285 /* maybe kexecing and resource is busy. try a reset */
4286 rc = ibmvnic_reset_crq(adapter);
4287 retrc = rc;
4289 if (rc == H_CLOSED) {
4290 dev_warn(dev, "Partner adapter not ready\n");
4291 } else if (rc) {
4292 dev_warn(dev, "Error %d opening adapter\n", rc);
4293 goto reg_crq_failed;
4296 retrc = 0;
4298 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4299 (unsigned long)adapter);
4301 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4302 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4303 adapter);
4304 if (rc) {
4305 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4306 vdev->irq, rc);
4307 goto req_irq_failed;
4310 rc = vio_enable_interrupts(vdev);
4311 if (rc) {
4312 dev_err(dev, "Error %d enabling interrupts\n", rc);
4313 goto req_irq_failed;
4316 crq->cur = 0;
4317 spin_lock_init(&crq->lock);
4319 return retrc;
4321 req_irq_failed:
4322 tasklet_kill(&adapter->tasklet);
4323 do {
4324 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4325 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4326 reg_crq_failed:
4327 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4328 map_failed:
4329 free_page((unsigned long)crq->msgs);
4330 crq->msgs = NULL;
4331 return retrc;
4334 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4336 struct device *dev = &adapter->vdev->dev;
4337 unsigned long timeout = msecs_to_jiffies(30000);
4338 int rc;
4340 if (adapter->resetting && !adapter->wait_for_reset) {
4341 rc = ibmvnic_reset_crq(adapter);
4342 if (!rc)
4343 rc = vio_enable_interrupts(adapter->vdev);
4344 } else {
4345 rc = init_crq_queue(adapter);
4348 if (rc) {
4349 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
4350 return rc;
4353 adapter->from_passive_init = false;
4355 init_completion(&adapter->init_done);
4356 adapter->init_done_rc = 0;
4357 ibmvnic_send_crq_init(adapter);
4358 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4359 dev_err(dev, "Initialization sequence timed out\n");
4360 return -1;
4363 if (adapter->init_done_rc) {
4364 release_crq_queue(adapter);
4365 return adapter->init_done_rc;
4368 if (adapter->from_passive_init) {
4369 adapter->state = VNIC_OPEN;
4370 adapter->from_passive_init = false;
4371 return -1;
4374 if (adapter->resetting && !adapter->wait_for_reset)
4375 rc = reset_sub_crq_queues(adapter);
4376 else
4377 rc = init_sub_crqs(adapter);
4378 if (rc) {
4379 dev_err(dev, "Initialization of sub crqs failed\n");
4380 release_crq_queue(adapter);
4381 return rc;
4384 rc = init_sub_crq_irqs(adapter);
4385 if (rc) {
4386 dev_err(dev, "Failed to initialize sub crq irqs\n");
4387 release_crq_queue(adapter);
4390 return rc;
4393 static struct device_attribute dev_attr_failover;
4395 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4397 struct ibmvnic_adapter *adapter;
4398 struct net_device *netdev;
4399 unsigned char *mac_addr_p;
4400 int rc;
4402 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4403 dev->unit_address);
4405 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4406 VETH_MAC_ADDR, NULL);
4407 if (!mac_addr_p) {
4408 dev_err(&dev->dev,
4409 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4410 __FILE__, __LINE__);
4411 return 0;
4414 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4415 IBMVNIC_MAX_QUEUES);
4416 if (!netdev)
4417 return -ENOMEM;
4419 adapter = netdev_priv(netdev);
4420 adapter->state = VNIC_PROBING;
4421 dev_set_drvdata(&dev->dev, netdev);
4422 adapter->vdev = dev;
4423 adapter->netdev = netdev;
4425 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4426 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4427 netdev->irq = dev->irq;
4428 netdev->netdev_ops = &ibmvnic_netdev_ops;
4429 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4430 SET_NETDEV_DEV(netdev, &dev->dev);
4432 spin_lock_init(&adapter->stats_lock);
4434 INIT_LIST_HEAD(&adapter->errors);
4435 spin_lock_init(&adapter->error_list_lock);
4437 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4438 INIT_LIST_HEAD(&adapter->rwi_list);
4439 mutex_init(&adapter->reset_lock);
4440 mutex_init(&adapter->rwi_lock);
4441 adapter->resetting = false;
4443 adapter->mac_change_pending = false;
4445 do {
4446 rc = ibmvnic_init(adapter);
4447 if (rc && rc != EAGAIN)
4448 goto ibmvnic_init_fail;
4449 } while (rc == EAGAIN);
4451 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4452 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4453 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4455 rc = device_create_file(&dev->dev, &dev_attr_failover);
4456 if (rc)
4457 goto ibmvnic_init_fail;
4459 netif_carrier_off(netdev);
4460 rc = register_netdev(netdev);
4461 if (rc) {
4462 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4463 goto ibmvnic_register_fail;
4465 dev_info(&dev->dev, "ibmvnic registered\n");
4467 adapter->state = VNIC_PROBED;
4469 adapter->wait_for_reset = false;
4471 return 0;
4473 ibmvnic_register_fail:
4474 device_remove_file(&dev->dev, &dev_attr_failover);
4476 ibmvnic_init_fail:
4477 release_sub_crqs(adapter);
4478 release_crq_queue(adapter);
4479 free_netdev(netdev);
4481 return rc;
4484 static int ibmvnic_remove(struct vio_dev *dev)
4486 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4487 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4489 adapter->state = VNIC_REMOVING;
4490 unregister_netdev(netdev);
4491 mutex_lock(&adapter->reset_lock);
4493 release_resources(adapter);
4494 release_sub_crqs(adapter);
4495 release_crq_queue(adapter);
4497 adapter->state = VNIC_REMOVED;
4499 mutex_unlock(&adapter->reset_lock);
4500 device_remove_file(&dev->dev, &dev_attr_failover);
4501 free_netdev(netdev);
4502 dev_set_drvdata(&dev->dev, NULL);
4504 return 0;
4507 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4508 const char *buf, size_t count)
4510 struct net_device *netdev = dev_get_drvdata(dev);
4511 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4512 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4513 __be64 session_token;
4514 long rc;
4516 if (!sysfs_streq(buf, "1"))
4517 return -EINVAL;
4519 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4520 H_GET_SESSION_TOKEN, 0, 0, 0);
4521 if (rc) {
4522 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4523 rc);
4524 return -EINVAL;
4527 session_token = (__be64)retbuf[0];
4528 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4529 be64_to_cpu(session_token));
4530 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4531 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4532 if (rc) {
4533 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4534 rc);
4535 return -EINVAL;
4538 return count;
4541 static DEVICE_ATTR_WO(failover);
4543 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4545 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4546 struct ibmvnic_adapter *adapter;
4547 struct iommu_table *tbl;
4548 unsigned long ret = 0;
4549 int i;
4551 tbl = get_iommu_table_base(&vdev->dev);
4553 /* netdev inits at probe time along with the structures we need below*/
4554 if (!netdev)
4555 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4557 adapter = netdev_priv(netdev);
4559 ret += PAGE_SIZE; /* the crq message queue */
4560 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4562 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4563 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4565 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4566 i++)
4567 ret += adapter->rx_pool[i].size *
4568 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4570 return ret;
4573 static int ibmvnic_resume(struct device *dev)
4575 struct net_device *netdev = dev_get_drvdata(dev);
4576 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4578 if (adapter->state != VNIC_OPEN)
4579 return 0;
4581 tasklet_schedule(&adapter->tasklet);
4583 return 0;
4586 static const struct vio_device_id ibmvnic_device_table[] = {
4587 {"network", "IBM,vnic"},
4588 {"", "" }
4590 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4592 static const struct dev_pm_ops ibmvnic_pm_ops = {
4593 .resume = ibmvnic_resume
4596 static struct vio_driver ibmvnic_driver = {
4597 .id_table = ibmvnic_device_table,
4598 .probe = ibmvnic_probe,
4599 .remove = ibmvnic_remove,
4600 .get_desired_dma = ibmvnic_get_desired_dma,
4601 .name = ibmvnic_driver_name,
4602 .pm = &ibmvnic_pm_ops,
4605 /* module functions */
4606 static int __init ibmvnic_module_init(void)
4608 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4609 IBMVNIC_DRIVER_VERSION);
4611 return vio_register_driver(&ibmvnic_driver);
4614 static void __exit ibmvnic_module_exit(void)
4616 vio_unregister_driver(&ibmvnic_driver);
4619 module_init(ibmvnic_module_init);
4620 module_exit(ibmvnic_module_exit);