xref: /linux/drivers/net/ethernet/ibm/ibmvnic.c (revision f3956ebb3bf06ab2266ad5ee2214aed46405810c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**************************************************************************/
3 /*                                                                        */
4 /*  IBM System i and System p Virtual NIC Device Driver                   */
5 /*  Copyright (C) 2014 IBM Corp.                                          */
6 /*  Santiago Leon (santi_leon@yahoo.com)                                  */
7 /*  Thomas Falcon (tlfalcon@linux.vnet.ibm.com)                           */
8 /*  John Allen (jallen@linux.vnet.ibm.com)                                */
9 /*                                                                        */
10 /*                                                                        */
11 /* This module contains the implementation of a virtual ethernet device   */
12 /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN    */
13 /* option of the RS/6000 Platform Architecture to interface with virtual  */
14 /* ethernet NICs that are presented to the partition by the hypervisor.   */
15 /*									   */
16 /* Messages are passed between the VNIC driver and the VNIC server using  */
17 /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to  */
18 /* issue and receive commands that initiate communication with the server */
19 /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but    */
20 /* are used by the driver to notify the server that a packet is           */
21 /* ready for transmission or that a buffer has been added to receive a    */
22 /* packet. Subsequently, sCRQs are used by the server to notify the       */
23 /* driver that a packet transmission has been completed or that a packet  */
24 /* has been received and placed in a waiting buffer.                      */
25 /*                                                                        */
26 /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in    */
27 /* which skbs are DMA mapped and immediately unmapped when the transmit   */
28 /* or receive has been completed, the VNIC driver is required to use      */
29 /* "long term mapping". This entails that large, continuous DMA mapped    */
30 /* buffers are allocated on driver initialization and these buffers are   */
31 /* then continuously reused to pass skbs to and from the VNIC server.     */
32 /*                                                                        */
33 /**************************************************************************/
34 
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/types.h>
38 #include <linux/errno.h>
39 #include <linux/completion.h>
40 #include <linux/ioport.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/kernel.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
46 #include <linux/init.h>
47 #include <linux/delay.h>
48 #include <linux/mm.h>
49 #include <linux/ethtool.h>
50 #include <linux/proc_fs.h>
51 #include <linux/if_arp.h>
52 #include <linux/in.h>
53 #include <linux/ip.h>
54 #include <linux/ipv6.h>
55 #include <linux/irq.h>
56 #include <linux/kthread.h>
57 #include <linux/seq_file.h>
58 #include <linux/interrupt.h>
59 #include <net/net_namespace.h>
60 #include <asm/hvcall.h>
61 #include <linux/atomic.h>
62 #include <asm/vio.h>
63 #include <asm/iommu.h>
64 #include <linux/uaccess.h>
65 #include <asm/firmware.h>
66 #include <linux/workqueue.h>
67 #include <linux/if_vlan.h>
68 #include <linux/utsname.h>
69 
70 #include "ibmvnic.h"
71 
72 static const char ibmvnic_driver_name[] = "ibmvnic";
73 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
74 
75 MODULE_AUTHOR("Santiago Leon");
76 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
77 MODULE_LICENSE("GPL");
78 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
79 
80 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
81 static void release_sub_crqs(struct ibmvnic_adapter *, bool);
82 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
83 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
84 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
85 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
86 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
87 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
88 static int enable_scrq_irq(struct ibmvnic_adapter *,
89 			   struct ibmvnic_sub_crq_queue *);
90 static int disable_scrq_irq(struct ibmvnic_adapter *,
91 			    struct ibmvnic_sub_crq_queue *);
92 static int pending_scrq(struct ibmvnic_adapter *,
93 			struct ibmvnic_sub_crq_queue *);
94 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
95 					struct ibmvnic_sub_crq_queue *);
96 static int ibmvnic_poll(struct napi_struct *napi, int data);
97 static void send_query_map(struct ibmvnic_adapter *adapter);
98 static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, u32, u8);
99 static int send_request_unmap(struct ibmvnic_adapter *, u8);
100 static int send_login(struct ibmvnic_adapter *adapter);
101 static void send_query_cap(struct ibmvnic_adapter *adapter);
102 static int init_sub_crqs(struct ibmvnic_adapter *);
103 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
104 static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
105 static void release_crq_queue(struct ibmvnic_adapter *);
106 static int __ibmvnic_set_mac(struct net_device *, u8 *);
107 static int init_crq_queue(struct ibmvnic_adapter *adapter);
108 static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
109 static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
110 					 struct ibmvnic_sub_crq_queue *tx_scrq);
111 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
112 				struct ibmvnic_long_term_buff *ltb);
113 
114 struct ibmvnic_stat {
115 	char name[ETH_GSTRING_LEN];
116 	int offset;
117 };
118 
119 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
120 			     offsetof(struct ibmvnic_statistics, stat))
121 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
122 
123 static const struct ibmvnic_stat ibmvnic_stats[] = {
124 	{"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
125 	{"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
126 	{"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
127 	{"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
128 	{"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
129 	{"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
130 	{"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
131 	{"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
132 	{"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
133 	{"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
134 	{"align_errors", IBMVNIC_STAT_OFF(align_errors)},
135 	{"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
136 	{"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
137 	{"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
138 	{"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
139 	{"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
140 	{"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
141 	{"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
142 	{"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
143 	{"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
144 	{"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
145 	{"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
146 };
147 
148 static int send_crq_init_complete(struct ibmvnic_adapter *adapter)
149 {
150 	union ibmvnic_crq crq;
151 
152 	memset(&crq, 0, sizeof(crq));
153 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
154 	crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
155 
156 	return ibmvnic_send_crq(adapter, &crq);
157 }
158 
159 static int send_version_xchg(struct ibmvnic_adapter *adapter)
160 {
161 	union ibmvnic_crq crq;
162 
163 	memset(&crq, 0, sizeof(crq));
164 	crq.version_exchange.first = IBMVNIC_CRQ_CMD;
165 	crq.version_exchange.cmd = VERSION_EXCHANGE;
166 	crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
167 
168 	return ibmvnic_send_crq(adapter, &crq);
169 }
170 
171 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
172 			  unsigned long length, unsigned long *number,
173 			  unsigned long *irq)
174 {
175 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
176 	long rc;
177 
178 	rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
179 	*number = retbuf[0];
180 	*irq = retbuf[1];
181 
182 	return rc;
183 }
184 
185 /**
186  * ibmvnic_wait_for_completion - Check device state and wait for completion
187  * @adapter: private device data
188  * @comp_done: completion structure to wait for
189  * @timeout: time to wait in milliseconds
190  *
191  * Wait for a completion signal or until the timeout limit is reached
192  * while checking that the device is still active.
193  */
194 static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
195 				       struct completion *comp_done,
196 				       unsigned long timeout)
197 {
198 	struct net_device *netdev;
199 	unsigned long div_timeout;
200 	u8 retry;
201 
202 	netdev = adapter->netdev;
203 	retry = 5;
204 	div_timeout = msecs_to_jiffies(timeout / retry);
205 	while (true) {
206 		if (!adapter->crq.active) {
207 			netdev_err(netdev, "Device down!\n");
208 			return -ENODEV;
209 		}
210 		if (!retry--)
211 			break;
212 		if (wait_for_completion_timeout(comp_done, div_timeout))
213 			return 0;
214 	}
215 	netdev_err(netdev, "Operation timed out.\n");
216 	return -ETIMEDOUT;
217 }
218 
219 /**
220  * reuse_ltb() - Check if a long term buffer can be reused
221  * @ltb:  The long term buffer to be checked
222  * @size: The size of the long term buffer.
223  *
224  * An LTB can be reused unless its size has changed.
225  *
226  * Return: Return true if the LTB can be reused, false otherwise.
227  */
228 static bool reuse_ltb(struct ibmvnic_long_term_buff *ltb, int size)
229 {
230 	return (ltb->buff && ltb->size == size);
231 }
232 
233 /**
234  * alloc_long_term_buff() - Allocate a long term buffer (LTB)
235  *
236  * @adapter: ibmvnic adapter associated to the LTB
237  * @ltb:     container object for the LTB
238  * @size:    size of the LTB
239  *
240  * Allocate an LTB of the specified size and notify VIOS.
241  *
242  * If the given @ltb already has the correct size, reuse it. Otherwise if
243  * its non-NULL, free it. Then allocate a new one of the correct size.
244  * Notify the VIOS either way since we may now be working with a new VIOS.
245  *
246  * Allocating larger chunks of memory during resets, specially LPM or under
247  * low memory situations can cause resets to fail/timeout and for LPAR to
248  * lose connectivity. So hold onto the LTB even if we fail to communicate
249  * with the VIOS and reuse it on next open. Free LTB when adapter is closed.
250  *
251  * Return: 0 if we were able to allocate the LTB and notify the VIOS and
252  *	   a negative value otherwise.
253  */
254 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
255 				struct ibmvnic_long_term_buff *ltb, int size)
256 {
257 	struct device *dev = &adapter->vdev->dev;
258 	int rc;
259 
260 	if (!reuse_ltb(ltb, size)) {
261 		dev_dbg(dev,
262 			"LTB size changed from 0x%llx to 0x%x, reallocating\n",
263 			 ltb->size, size);
264 		free_long_term_buff(adapter, ltb);
265 	}
266 
267 	if (ltb->buff) {
268 		dev_dbg(dev, "Reusing LTB [map %d, size 0x%llx]\n",
269 			ltb->map_id, ltb->size);
270 	} else {
271 		ltb->buff = dma_alloc_coherent(dev, size, &ltb->addr,
272 					       GFP_KERNEL);
273 		if (!ltb->buff) {
274 			dev_err(dev, "Couldn't alloc long term buffer\n");
275 			return -ENOMEM;
276 		}
277 		ltb->size = size;
278 
279 		ltb->map_id = find_first_zero_bit(adapter->map_ids,
280 						  MAX_MAP_ID);
281 		bitmap_set(adapter->map_ids, ltb->map_id, 1);
282 
283 		dev_dbg(dev,
284 			"Allocated new LTB [map %d, size 0x%llx]\n",
285 			 ltb->map_id, ltb->size);
286 	}
287 
288 	/* Ensure ltb is zeroed - specially when reusing it. */
289 	memset(ltb->buff, 0, ltb->size);
290 
291 	mutex_lock(&adapter->fw_lock);
292 	adapter->fw_done_rc = 0;
293 	reinit_completion(&adapter->fw_done);
294 
295 	rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
296 	if (rc) {
297 		dev_err(dev, "send_request_map failed, rc = %d\n", rc);
298 		goto out;
299 	}
300 
301 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
302 	if (rc) {
303 		dev_err(dev, "LTB map request aborted or timed out, rc = %d\n",
304 			rc);
305 		goto out;
306 	}
307 
308 	if (adapter->fw_done_rc) {
309 		dev_err(dev, "Couldn't map LTB, rc = %d\n",
310 			adapter->fw_done_rc);
311 		rc = -1;
312 		goto out;
313 	}
314 	rc = 0;
315 out:
316 	/* don't free LTB on communication error - see function header */
317 	mutex_unlock(&adapter->fw_lock);
318 	return rc;
319 }
320 
321 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
322 				struct ibmvnic_long_term_buff *ltb)
323 {
324 	struct device *dev = &adapter->vdev->dev;
325 
326 	if (!ltb->buff)
327 		return;
328 
329 	/* VIOS automatically unmaps the long term buffer at remote
330 	 * end for the following resets:
331 	 * FAILOVER, MOBILITY, TIMEOUT.
332 	 */
333 	if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
334 	    adapter->reset_reason != VNIC_RESET_MOBILITY &&
335 	    adapter->reset_reason != VNIC_RESET_TIMEOUT)
336 		send_request_unmap(adapter, ltb->map_id);
337 
338 	dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
339 
340 	ltb->buff = NULL;
341 	/* mark this map_id free */
342 	bitmap_clear(adapter->map_ids, ltb->map_id, 1);
343 	ltb->map_id = 0;
344 }
345 
346 static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
347 {
348 	int i;
349 
350 	for (i = 0; i < adapter->num_active_rx_pools; i++)
351 		adapter->rx_pool[i].active = 0;
352 }
353 
354 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
355 			      struct ibmvnic_rx_pool *pool)
356 {
357 	int count = pool->size - atomic_read(&pool->available);
358 	u64 handle = adapter->rx_scrq[pool->index]->handle;
359 	struct device *dev = &adapter->vdev->dev;
360 	struct ibmvnic_ind_xmit_queue *ind_bufp;
361 	struct ibmvnic_sub_crq_queue *rx_scrq;
362 	union sub_crq *sub_crq;
363 	int buffers_added = 0;
364 	unsigned long lpar_rc;
365 	struct sk_buff *skb;
366 	unsigned int offset;
367 	dma_addr_t dma_addr;
368 	unsigned char *dst;
369 	int shift = 0;
370 	int index;
371 	int i;
372 
373 	if (!pool->active)
374 		return;
375 
376 	rx_scrq = adapter->rx_scrq[pool->index];
377 	ind_bufp = &rx_scrq->ind_buf;
378 
379 	/* netdev_skb_alloc() could have failed after we saved a few skbs
380 	 * in the indir_buf and we would not have sent them to VIOS yet.
381 	 * To account for them, start the loop at ind_bufp->index rather
382 	 * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will
383 	 * be 0.
384 	 */
385 	for (i = ind_bufp->index; i < count; ++i) {
386 		index = pool->free_map[pool->next_free];
387 
388 		/* We maybe reusing the skb from earlier resets. Allocate
389 		 * only if necessary. But since the LTB may have changed
390 		 * during reset (see init_rx_pools()), update LTB below
391 		 * even if reusing skb.
392 		 */
393 		skb = pool->rx_buff[index].skb;
394 		if (!skb) {
395 			skb = netdev_alloc_skb(adapter->netdev,
396 					       pool->buff_size);
397 			if (!skb) {
398 				dev_err(dev, "Couldn't replenish rx buff\n");
399 				adapter->replenish_no_mem++;
400 				break;
401 			}
402 		}
403 
404 		pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
405 		pool->next_free = (pool->next_free + 1) % pool->size;
406 
407 		/* Copy the skb to the long term mapped DMA buffer */
408 		offset = index * pool->buff_size;
409 		dst = pool->long_term_buff.buff + offset;
410 		memset(dst, 0, pool->buff_size);
411 		dma_addr = pool->long_term_buff.addr + offset;
412 
413 		/* add the skb to an rx_buff in the pool */
414 		pool->rx_buff[index].data = dst;
415 		pool->rx_buff[index].dma = dma_addr;
416 		pool->rx_buff[index].skb = skb;
417 		pool->rx_buff[index].pool_index = pool->index;
418 		pool->rx_buff[index].size = pool->buff_size;
419 
420 		/* queue the rx_buff for the next send_subcrq_indirect */
421 		sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
422 		memset(sub_crq, 0, sizeof(*sub_crq));
423 		sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
424 		sub_crq->rx_add.correlator =
425 		    cpu_to_be64((u64)&pool->rx_buff[index]);
426 		sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
427 		sub_crq->rx_add.map_id = pool->long_term_buff.map_id;
428 
429 		/* The length field of the sCRQ is defined to be 24 bits so the
430 		 * buffer size needs to be left shifted by a byte before it is
431 		 * converted to big endian to prevent the last byte from being
432 		 * truncated.
433 		 */
434 #ifdef __LITTLE_ENDIAN__
435 		shift = 8;
436 #endif
437 		sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
438 
439 		/* if send_subcrq_indirect queue is full, flush to VIOS */
440 		if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
441 		    i == count - 1) {
442 			lpar_rc =
443 				send_subcrq_indirect(adapter, handle,
444 						     (u64)ind_bufp->indir_dma,
445 						     (u64)ind_bufp->index);
446 			if (lpar_rc != H_SUCCESS)
447 				goto failure;
448 			buffers_added += ind_bufp->index;
449 			adapter->replenish_add_buff_success += ind_bufp->index;
450 			ind_bufp->index = 0;
451 		}
452 	}
453 	atomic_add(buffers_added, &pool->available);
454 	return;
455 
456 failure:
457 	if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
458 		dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
459 	for (i = ind_bufp->index - 1; i >= 0; --i) {
460 		struct ibmvnic_rx_buff *rx_buff;
461 
462 		pool->next_free = pool->next_free == 0 ?
463 				  pool->size - 1 : pool->next_free - 1;
464 		sub_crq = &ind_bufp->indir_arr[i];
465 		rx_buff = (struct ibmvnic_rx_buff *)
466 				be64_to_cpu(sub_crq->rx_add.correlator);
467 		index = (int)(rx_buff - pool->rx_buff);
468 		pool->free_map[pool->next_free] = index;
469 		dev_kfree_skb_any(pool->rx_buff[index].skb);
470 		pool->rx_buff[index].skb = NULL;
471 	}
472 	adapter->replenish_add_buff_failure += ind_bufp->index;
473 	atomic_add(buffers_added, &pool->available);
474 	ind_bufp->index = 0;
475 	if (lpar_rc == H_CLOSED || adapter->failover_pending) {
476 		/* Disable buffer pool replenishment and report carrier off if
477 		 * queue is closed or pending failover.
478 		 * Firmware guarantees that a signal will be sent to the
479 		 * driver, triggering a reset.
480 		 */
481 		deactivate_rx_pools(adapter);
482 		netif_carrier_off(adapter->netdev);
483 	}
484 }
485 
486 static void replenish_pools(struct ibmvnic_adapter *adapter)
487 {
488 	int i;
489 
490 	adapter->replenish_task_cycles++;
491 	for (i = 0; i < adapter->num_active_rx_pools; i++) {
492 		if (adapter->rx_pool[i].active)
493 			replenish_rx_pool(adapter, &adapter->rx_pool[i]);
494 	}
495 
496 	netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
497 }
498 
499 static void release_stats_buffers(struct ibmvnic_adapter *adapter)
500 {
501 	kfree(adapter->tx_stats_buffers);
502 	kfree(adapter->rx_stats_buffers);
503 	adapter->tx_stats_buffers = NULL;
504 	adapter->rx_stats_buffers = NULL;
505 }
506 
507 static int init_stats_buffers(struct ibmvnic_adapter *adapter)
508 {
509 	adapter->tx_stats_buffers =
510 				kcalloc(IBMVNIC_MAX_QUEUES,
511 					sizeof(struct ibmvnic_tx_queue_stats),
512 					GFP_KERNEL);
513 	if (!adapter->tx_stats_buffers)
514 		return -ENOMEM;
515 
516 	adapter->rx_stats_buffers =
517 				kcalloc(IBMVNIC_MAX_QUEUES,
518 					sizeof(struct ibmvnic_rx_queue_stats),
519 					GFP_KERNEL);
520 	if (!adapter->rx_stats_buffers)
521 		return -ENOMEM;
522 
523 	return 0;
524 }
525 
526 static void release_stats_token(struct ibmvnic_adapter *adapter)
527 {
528 	struct device *dev = &adapter->vdev->dev;
529 
530 	if (!adapter->stats_token)
531 		return;
532 
533 	dma_unmap_single(dev, adapter->stats_token,
534 			 sizeof(struct ibmvnic_statistics),
535 			 DMA_FROM_DEVICE);
536 	adapter->stats_token = 0;
537 }
538 
539 static int init_stats_token(struct ibmvnic_adapter *adapter)
540 {
541 	struct device *dev = &adapter->vdev->dev;
542 	dma_addr_t stok;
543 
544 	stok = dma_map_single(dev, &adapter->stats,
545 			      sizeof(struct ibmvnic_statistics),
546 			      DMA_FROM_DEVICE);
547 	if (dma_mapping_error(dev, stok)) {
548 		dev_err(dev, "Couldn't map stats buffer\n");
549 		return -1;
550 	}
551 
552 	adapter->stats_token = stok;
553 	netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
554 	return 0;
555 }
556 
557 /**
558  * release_rx_pools() - Release any rx pools attached to @adapter.
559  * @adapter: ibmvnic adapter
560  *
561  * Safe to call this multiple times - even if no pools are attached.
562  */
563 static void release_rx_pools(struct ibmvnic_adapter *adapter)
564 {
565 	struct ibmvnic_rx_pool *rx_pool;
566 	int i, j;
567 
568 	if (!adapter->rx_pool)
569 		return;
570 
571 	for (i = 0; i < adapter->num_active_rx_pools; i++) {
572 		rx_pool = &adapter->rx_pool[i];
573 
574 		netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
575 
576 		kfree(rx_pool->free_map);
577 
578 		free_long_term_buff(adapter, &rx_pool->long_term_buff);
579 
580 		if (!rx_pool->rx_buff)
581 			continue;
582 
583 		for (j = 0; j < rx_pool->size; j++) {
584 			if (rx_pool->rx_buff[j].skb) {
585 				dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
586 				rx_pool->rx_buff[j].skb = NULL;
587 			}
588 		}
589 
590 		kfree(rx_pool->rx_buff);
591 	}
592 
593 	kfree(adapter->rx_pool);
594 	adapter->rx_pool = NULL;
595 	adapter->num_active_rx_pools = 0;
596 	adapter->prev_rx_pool_size = 0;
597 }
598 
599 /**
600  * reuse_rx_pools() - Check if the existing rx pools can be reused.
601  * @adapter: ibmvnic adapter
602  *
603  * Check if the existing rx pools in the adapter can be reused. The
604  * pools can be reused if the pool parameters (number of pools,
605  * number of buffers in the pool and size of each buffer) have not
606  * changed.
607  *
608  * NOTE: This assumes that all pools have the same number of buffers
609  *       which is the case currently. If that changes, we must fix this.
610  *
611  * Return: true if the rx pools can be reused, false otherwise.
612  */
613 static bool reuse_rx_pools(struct ibmvnic_adapter *adapter)
614 {
615 	u64 old_num_pools, new_num_pools;
616 	u64 old_pool_size, new_pool_size;
617 	u64 old_buff_size, new_buff_size;
618 
619 	if (!adapter->rx_pool)
620 		return false;
621 
622 	old_num_pools = adapter->num_active_rx_pools;
623 	new_num_pools = adapter->req_rx_queues;
624 
625 	old_pool_size = adapter->prev_rx_pool_size;
626 	new_pool_size = adapter->req_rx_add_entries_per_subcrq;
627 
628 	old_buff_size = adapter->prev_rx_buf_sz;
629 	new_buff_size = adapter->cur_rx_buf_sz;
630 
631 	/* Require buff size to be exactly same for now */
632 	if (old_buff_size != new_buff_size)
633 		return false;
634 
635 	if (old_num_pools == new_num_pools && old_pool_size == new_pool_size)
636 		return true;
637 
638 	if (old_num_pools < adapter->min_rx_queues ||
639 	    old_num_pools > adapter->max_rx_queues ||
640 	    old_pool_size < adapter->min_rx_add_entries_per_subcrq ||
641 	    old_pool_size > adapter->max_rx_add_entries_per_subcrq)
642 		return false;
643 
644 	return true;
645 }
646 
647 /**
648  * init_rx_pools(): Initialize the set of receiver pools in the adapter.
649  * @netdev: net device associated with the vnic interface
650  *
651  * Initialize the set of receiver pools in the ibmvnic adapter associated
652  * with the net_device @netdev. If possible, reuse the existing rx pools.
653  * Otherwise free any existing pools and  allocate a new set of pools
654  * before initializing them.
655  *
656  * Return: 0 on success and negative value on error.
657  */
658 static int init_rx_pools(struct net_device *netdev)
659 {
660 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
661 	struct device *dev = &adapter->vdev->dev;
662 	struct ibmvnic_rx_pool *rx_pool;
663 	u64 num_pools;
664 	u64 pool_size;		/* # of buffers in one pool */
665 	u64 buff_size;
666 	int i, j;
667 
668 	pool_size = adapter->req_rx_add_entries_per_subcrq;
669 	num_pools = adapter->req_rx_queues;
670 	buff_size = adapter->cur_rx_buf_sz;
671 
672 	if (reuse_rx_pools(adapter)) {
673 		dev_dbg(dev, "Reusing rx pools\n");
674 		goto update_ltb;
675 	}
676 
677 	/* Allocate/populate the pools. */
678 	release_rx_pools(adapter);
679 
680 	adapter->rx_pool = kcalloc(num_pools,
681 				   sizeof(struct ibmvnic_rx_pool),
682 				   GFP_KERNEL);
683 	if (!adapter->rx_pool) {
684 		dev_err(dev, "Failed to allocate rx pools\n");
685 		return -1;
686 	}
687 
688 	/* Set num_active_rx_pools early. If we fail below after partial
689 	 * allocation, release_rx_pools() will know how many to look for.
690 	 */
691 	adapter->num_active_rx_pools = num_pools;
692 
693 	for (i = 0; i < num_pools; i++) {
694 		rx_pool = &adapter->rx_pool[i];
695 
696 		netdev_dbg(adapter->netdev,
697 			   "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
698 			   i, pool_size, buff_size);
699 
700 		rx_pool->size = pool_size;
701 		rx_pool->index = i;
702 		rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
703 
704 		rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
705 					    GFP_KERNEL);
706 		if (!rx_pool->free_map) {
707 			dev_err(dev, "Couldn't alloc free_map %d\n", i);
708 			goto out_release;
709 		}
710 
711 		rx_pool->rx_buff = kcalloc(rx_pool->size,
712 					   sizeof(struct ibmvnic_rx_buff),
713 					   GFP_KERNEL);
714 		if (!rx_pool->rx_buff) {
715 			dev_err(dev, "Couldn't alloc rx buffers\n");
716 			goto out_release;
717 		}
718 	}
719 
720 	adapter->prev_rx_pool_size = pool_size;
721 	adapter->prev_rx_buf_sz = adapter->cur_rx_buf_sz;
722 
723 update_ltb:
724 	for (i = 0; i < num_pools; i++) {
725 		rx_pool = &adapter->rx_pool[i];
726 		dev_dbg(dev, "Updating LTB for rx pool %d [%d, %d]\n",
727 			i, rx_pool->size, rx_pool->buff_size);
728 
729 		if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
730 					 rx_pool->size * rx_pool->buff_size))
731 			goto out;
732 
733 		for (j = 0; j < rx_pool->size; ++j) {
734 			struct ibmvnic_rx_buff *rx_buff;
735 
736 			rx_pool->free_map[j] = j;
737 
738 			/* NOTE: Don't clear rx_buff->skb here - will leak
739 			 * memory! replenish_rx_pool() will reuse skbs or
740 			 * allocate as necessary.
741 			 */
742 			rx_buff = &rx_pool->rx_buff[j];
743 			rx_buff->dma = 0;
744 			rx_buff->data = 0;
745 			rx_buff->size = 0;
746 			rx_buff->pool_index = 0;
747 		}
748 
749 		/* Mark pool "empty" so replenish_rx_pools() will
750 		 * update the LTB info for each buffer
751 		 */
752 		atomic_set(&rx_pool->available, 0);
753 		rx_pool->next_alloc = 0;
754 		rx_pool->next_free = 0;
755 		/* replenish_rx_pool() may have called deactivate_rx_pools()
756 		 * on failover. Ensure pool is active now.
757 		 */
758 		rx_pool->active = 1;
759 	}
760 	return 0;
761 out_release:
762 	release_rx_pools(adapter);
763 out:
764 	/* We failed to allocate one or more LTBs or map them on the VIOS.
765 	 * Hold onto the pools and any LTBs that we did allocate/map.
766 	 */
767 	return -1;
768 }
769 
770 static void release_vpd_data(struct ibmvnic_adapter *adapter)
771 {
772 	if (!adapter->vpd)
773 		return;
774 
775 	kfree(adapter->vpd->buff);
776 	kfree(adapter->vpd);
777 
778 	adapter->vpd = NULL;
779 }
780 
781 static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
782 				struct ibmvnic_tx_pool *tx_pool)
783 {
784 	kfree(tx_pool->tx_buff);
785 	kfree(tx_pool->free_map);
786 	free_long_term_buff(adapter, &tx_pool->long_term_buff);
787 }
788 
789 /**
790  * release_tx_pools() - Release any tx pools attached to @adapter.
791  * @adapter: ibmvnic adapter
792  *
793  * Safe to call this multiple times - even if no pools are attached.
794  */
795 static void release_tx_pools(struct ibmvnic_adapter *adapter)
796 {
797 	int i;
798 
799 	/* init_tx_pools() ensures that ->tx_pool and ->tso_pool are
800 	 * both NULL or both non-NULL. So we only need to check one.
801 	 */
802 	if (!adapter->tx_pool)
803 		return;
804 
805 	for (i = 0; i < adapter->num_active_tx_pools; i++) {
806 		release_one_tx_pool(adapter, &adapter->tx_pool[i]);
807 		release_one_tx_pool(adapter, &adapter->tso_pool[i]);
808 	}
809 
810 	kfree(adapter->tx_pool);
811 	adapter->tx_pool = NULL;
812 	kfree(adapter->tso_pool);
813 	adapter->tso_pool = NULL;
814 	adapter->num_active_tx_pools = 0;
815 	adapter->prev_tx_pool_size = 0;
816 }
817 
818 static int init_one_tx_pool(struct net_device *netdev,
819 			    struct ibmvnic_tx_pool *tx_pool,
820 			    int pool_size, int buf_size)
821 {
822 	int i;
823 
824 	tx_pool->tx_buff = kcalloc(pool_size,
825 				   sizeof(struct ibmvnic_tx_buff),
826 				   GFP_KERNEL);
827 	if (!tx_pool->tx_buff)
828 		return -1;
829 
830 	tx_pool->free_map = kcalloc(pool_size, sizeof(int), GFP_KERNEL);
831 	if (!tx_pool->free_map) {
832 		kfree(tx_pool->tx_buff);
833 		tx_pool->tx_buff = NULL;
834 		return -1;
835 	}
836 
837 	for (i = 0; i < pool_size; i++)
838 		tx_pool->free_map[i] = i;
839 
840 	tx_pool->consumer_index = 0;
841 	tx_pool->producer_index = 0;
842 	tx_pool->num_buffers = pool_size;
843 	tx_pool->buf_size = buf_size;
844 
845 	return 0;
846 }
847 
848 /**
849  * reuse_tx_pools() - Check if the existing tx pools can be reused.
850  * @adapter: ibmvnic adapter
851  *
852  * Check if the existing tx pools in the adapter can be reused. The
853  * pools can be reused if the pool parameters (number of pools,
854  * number of buffers in the pool and mtu) have not changed.
855  *
856  * NOTE: This assumes that all pools have the same number of buffers
857  *       which is the case currently. If that changes, we must fix this.
858  *
859  * Return: true if the tx pools can be reused, false otherwise.
860  */
861 static bool reuse_tx_pools(struct ibmvnic_adapter *adapter)
862 {
863 	u64 old_num_pools, new_num_pools;
864 	u64 old_pool_size, new_pool_size;
865 	u64 old_mtu, new_mtu;
866 
867 	if (!adapter->tx_pool)
868 		return false;
869 
870 	old_num_pools = adapter->num_active_tx_pools;
871 	new_num_pools = adapter->num_active_tx_scrqs;
872 	old_pool_size = adapter->prev_tx_pool_size;
873 	new_pool_size = adapter->req_tx_entries_per_subcrq;
874 	old_mtu = adapter->prev_mtu;
875 	new_mtu = adapter->req_mtu;
876 
877 	/* Require MTU to be exactly same to reuse pools for now */
878 	if (old_mtu != new_mtu)
879 		return false;
880 
881 	if (old_num_pools == new_num_pools && old_pool_size == new_pool_size)
882 		return true;
883 
884 	if (old_num_pools < adapter->min_tx_queues ||
885 	    old_num_pools > adapter->max_tx_queues ||
886 	    old_pool_size < adapter->min_tx_entries_per_subcrq ||
887 	    old_pool_size > adapter->max_tx_entries_per_subcrq)
888 		return false;
889 
890 	return true;
891 }
892 
893 /**
894  * init_tx_pools(): Initialize the set of transmit pools in the adapter.
895  * @netdev: net device associated with the vnic interface
896  *
897  * Initialize the set of transmit pools in the ibmvnic adapter associated
898  * with the net_device @netdev. If possible, reuse the existing tx pools.
899  * Otherwise free any existing pools and  allocate a new set of pools
900  * before initializing them.
901  *
902  * Return: 0 on success and negative value on error.
903  */
904 static int init_tx_pools(struct net_device *netdev)
905 {
906 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
907 	struct device *dev = &adapter->vdev->dev;
908 	int num_pools;
909 	u64 pool_size;		/* # of buffers in pool */
910 	u64 buff_size;
911 	int i, j, rc;
912 
913 	num_pools = adapter->req_tx_queues;
914 
915 	/* We must notify the VIOS about the LTB on all resets - but we only
916 	 * need to alloc/populate pools if either the number of buffers or
917 	 * size of each buffer in the pool has changed.
918 	 */
919 	if (reuse_tx_pools(adapter)) {
920 		netdev_dbg(netdev, "Reusing tx pools\n");
921 		goto update_ltb;
922 	}
923 
924 	/* Allocate/populate the pools. */
925 	release_tx_pools(adapter);
926 
927 	pool_size = adapter->req_tx_entries_per_subcrq;
928 	num_pools = adapter->num_active_tx_scrqs;
929 
930 	adapter->tx_pool = kcalloc(num_pools,
931 				   sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
932 	if (!adapter->tx_pool)
933 		return -1;
934 
935 	adapter->tso_pool = kcalloc(num_pools,
936 				    sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
937 	/* To simplify release_tx_pools() ensure that ->tx_pool and
938 	 * ->tso_pool are either both NULL or both non-NULL.
939 	 */
940 	if (!adapter->tso_pool) {
941 		kfree(adapter->tx_pool);
942 		adapter->tx_pool = NULL;
943 		return -1;
944 	}
945 
946 	/* Set num_active_tx_pools early. If we fail below after partial
947 	 * allocation, release_tx_pools() will know how many to look for.
948 	 */
949 	adapter->num_active_tx_pools = num_pools;
950 
951 	buff_size = adapter->req_mtu + VLAN_HLEN;
952 	buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
953 
954 	for (i = 0; i < num_pools; i++) {
955 		dev_dbg(dev, "Init tx pool %d [%llu, %llu]\n",
956 			i, adapter->req_tx_entries_per_subcrq, buff_size);
957 
958 		rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
959 				      pool_size, buff_size);
960 		if (rc)
961 			goto out_release;
962 
963 		rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
964 				      IBMVNIC_TSO_BUFS,
965 				      IBMVNIC_TSO_BUF_SZ);
966 		if (rc)
967 			goto out_release;
968 	}
969 
970 	adapter->prev_tx_pool_size = pool_size;
971 	adapter->prev_mtu = adapter->req_mtu;
972 
973 update_ltb:
974 	/* NOTE: All tx_pools have the same number of buffers (which is
975 	 *       same as pool_size). All tso_pools have IBMVNIC_TSO_BUFS
976 	 *       buffers (see calls init_one_tx_pool() for these).
977 	 *       For consistency, we use tx_pool->num_buffers and
978 	 *       tso_pool->num_buffers below.
979 	 */
980 	rc = -1;
981 	for (i = 0; i < num_pools; i++) {
982 		struct ibmvnic_tx_pool *tso_pool;
983 		struct ibmvnic_tx_pool *tx_pool;
984 		u32 ltb_size;
985 
986 		tx_pool = &adapter->tx_pool[i];
987 		ltb_size = tx_pool->num_buffers * tx_pool->buf_size;
988 		if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
989 					 ltb_size))
990 			goto out;
991 
992 		dev_dbg(dev, "Updated LTB for tx pool %d [%p, %d, %d]\n",
993 			i, tx_pool->long_term_buff.buff,
994 			tx_pool->num_buffers, tx_pool->buf_size);
995 
996 		tx_pool->consumer_index = 0;
997 		tx_pool->producer_index = 0;
998 
999 		for (j = 0; j < tx_pool->num_buffers; j++)
1000 			tx_pool->free_map[j] = j;
1001 
1002 		tso_pool = &adapter->tso_pool[i];
1003 		ltb_size = tso_pool->num_buffers * tso_pool->buf_size;
1004 		if (alloc_long_term_buff(adapter, &tso_pool->long_term_buff,
1005 					 ltb_size))
1006 			goto out;
1007 
1008 		dev_dbg(dev, "Updated LTB for tso pool %d [%p, %d, %d]\n",
1009 			i, tso_pool->long_term_buff.buff,
1010 			tso_pool->num_buffers, tso_pool->buf_size);
1011 
1012 		tso_pool->consumer_index = 0;
1013 		tso_pool->producer_index = 0;
1014 
1015 		for (j = 0; j < tso_pool->num_buffers; j++)
1016 			tso_pool->free_map[j] = j;
1017 	}
1018 
1019 	return 0;
1020 out_release:
1021 	release_tx_pools(adapter);
1022 out:
1023 	/* We failed to allocate one or more LTBs or map them on the VIOS.
1024 	 * Hold onto the pools and any LTBs that we did allocate/map.
1025 	 */
1026 	return rc;
1027 }
1028 
1029 static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
1030 {
1031 	int i;
1032 
1033 	if (adapter->napi_enabled)
1034 		return;
1035 
1036 	for (i = 0; i < adapter->req_rx_queues; i++)
1037 		napi_enable(&adapter->napi[i]);
1038 
1039 	adapter->napi_enabled = true;
1040 }
1041 
1042 static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
1043 {
1044 	int i;
1045 
1046 	if (!adapter->napi_enabled)
1047 		return;
1048 
1049 	for (i = 0; i < adapter->req_rx_queues; i++) {
1050 		netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
1051 		napi_disable(&adapter->napi[i]);
1052 	}
1053 
1054 	adapter->napi_enabled = false;
1055 }
1056 
1057 static int init_napi(struct ibmvnic_adapter *adapter)
1058 {
1059 	int i;
1060 
1061 	adapter->napi = kcalloc(adapter->req_rx_queues,
1062 				sizeof(struct napi_struct), GFP_KERNEL);
1063 	if (!adapter->napi)
1064 		return -ENOMEM;
1065 
1066 	for (i = 0; i < adapter->req_rx_queues; i++) {
1067 		netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
1068 		netif_napi_add(adapter->netdev, &adapter->napi[i],
1069 			       ibmvnic_poll, NAPI_POLL_WEIGHT);
1070 	}
1071 
1072 	adapter->num_active_rx_napi = adapter->req_rx_queues;
1073 	return 0;
1074 }
1075 
1076 static void release_napi(struct ibmvnic_adapter *adapter)
1077 {
1078 	int i;
1079 
1080 	if (!adapter->napi)
1081 		return;
1082 
1083 	for (i = 0; i < adapter->num_active_rx_napi; i++) {
1084 		netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
1085 		netif_napi_del(&adapter->napi[i]);
1086 	}
1087 
1088 	kfree(adapter->napi);
1089 	adapter->napi = NULL;
1090 	adapter->num_active_rx_napi = 0;
1091 	adapter->napi_enabled = false;
1092 }
1093 
1094 static const char *adapter_state_to_string(enum vnic_state state)
1095 {
1096 	switch (state) {
1097 	case VNIC_PROBING:
1098 		return "PROBING";
1099 	case VNIC_PROBED:
1100 		return "PROBED";
1101 	case VNIC_OPENING:
1102 		return "OPENING";
1103 	case VNIC_OPEN:
1104 		return "OPEN";
1105 	case VNIC_CLOSING:
1106 		return "CLOSING";
1107 	case VNIC_CLOSED:
1108 		return "CLOSED";
1109 	case VNIC_REMOVING:
1110 		return "REMOVING";
1111 	case VNIC_REMOVED:
1112 		return "REMOVED";
1113 	case VNIC_DOWN:
1114 		return "DOWN";
1115 	}
1116 	return "UNKNOWN";
1117 }
1118 
1119 static int ibmvnic_login(struct net_device *netdev)
1120 {
1121 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1122 	unsigned long timeout = msecs_to_jiffies(20000);
1123 	int retry_count = 0;
1124 	int retries = 10;
1125 	bool retry;
1126 	int rc;
1127 
1128 	do {
1129 		retry = false;
1130 		if (retry_count > retries) {
1131 			netdev_warn(netdev, "Login attempts exceeded\n");
1132 			return -1;
1133 		}
1134 
1135 		adapter->init_done_rc = 0;
1136 		reinit_completion(&adapter->init_done);
1137 		rc = send_login(adapter);
1138 		if (rc)
1139 			return rc;
1140 
1141 		if (!wait_for_completion_timeout(&adapter->init_done,
1142 						 timeout)) {
1143 			netdev_warn(netdev, "Login timed out, retrying...\n");
1144 			retry = true;
1145 			adapter->init_done_rc = 0;
1146 			retry_count++;
1147 			continue;
1148 		}
1149 
1150 		if (adapter->init_done_rc == ABORTED) {
1151 			netdev_warn(netdev, "Login aborted, retrying...\n");
1152 			retry = true;
1153 			adapter->init_done_rc = 0;
1154 			retry_count++;
1155 			/* FW or device may be busy, so
1156 			 * wait a bit before retrying login
1157 			 */
1158 			msleep(500);
1159 		} else if (adapter->init_done_rc == PARTIALSUCCESS) {
1160 			retry_count++;
1161 			release_sub_crqs(adapter, 1);
1162 
1163 			retry = true;
1164 			netdev_dbg(netdev,
1165 				   "Received partial success, retrying...\n");
1166 			adapter->init_done_rc = 0;
1167 			reinit_completion(&adapter->init_done);
1168 			send_query_cap(adapter);
1169 			if (!wait_for_completion_timeout(&adapter->init_done,
1170 							 timeout)) {
1171 				netdev_warn(netdev,
1172 					    "Capabilities query timed out\n");
1173 				return -1;
1174 			}
1175 
1176 			rc = init_sub_crqs(adapter);
1177 			if (rc) {
1178 				netdev_warn(netdev,
1179 					    "SCRQ initialization failed\n");
1180 				return -1;
1181 			}
1182 
1183 			rc = init_sub_crq_irqs(adapter);
1184 			if (rc) {
1185 				netdev_warn(netdev,
1186 					    "SCRQ irq initialization failed\n");
1187 				return -1;
1188 			}
1189 		} else if (adapter->init_done_rc) {
1190 			netdev_warn(netdev, "Adapter login failed\n");
1191 			return -1;
1192 		}
1193 	} while (retry);
1194 
1195 	__ibmvnic_set_mac(netdev, adapter->mac_addr);
1196 
1197 	netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
1198 	return 0;
1199 }
1200 
1201 static void release_login_buffer(struct ibmvnic_adapter *adapter)
1202 {
1203 	kfree(adapter->login_buf);
1204 	adapter->login_buf = NULL;
1205 }
1206 
1207 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
1208 {
1209 	kfree(adapter->login_rsp_buf);
1210 	adapter->login_rsp_buf = NULL;
1211 }
1212 
1213 static void release_resources(struct ibmvnic_adapter *adapter)
1214 {
1215 	release_vpd_data(adapter);
1216 
1217 	release_napi(adapter);
1218 	release_login_buffer(adapter);
1219 	release_login_rsp_buffer(adapter);
1220 }
1221 
1222 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1223 {
1224 	struct net_device *netdev = adapter->netdev;
1225 	unsigned long timeout = msecs_to_jiffies(20000);
1226 	union ibmvnic_crq crq;
1227 	bool resend;
1228 	int rc;
1229 
1230 	netdev_dbg(netdev, "setting link state %d\n", link_state);
1231 
1232 	memset(&crq, 0, sizeof(crq));
1233 	crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1234 	crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1235 	crq.logical_link_state.link_state = link_state;
1236 
1237 	do {
1238 		resend = false;
1239 
1240 		reinit_completion(&adapter->init_done);
1241 		rc = ibmvnic_send_crq(adapter, &crq);
1242 		if (rc) {
1243 			netdev_err(netdev, "Failed to set link state\n");
1244 			return rc;
1245 		}
1246 
1247 		if (!wait_for_completion_timeout(&adapter->init_done,
1248 						 timeout)) {
1249 			netdev_err(netdev, "timeout setting link state\n");
1250 			return -1;
1251 		}
1252 
1253 		if (adapter->init_done_rc == PARTIALSUCCESS) {
1254 			/* Partuial success, delay and re-send */
1255 			mdelay(1000);
1256 			resend = true;
1257 		} else if (adapter->init_done_rc) {
1258 			netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1259 				    adapter->init_done_rc);
1260 			return adapter->init_done_rc;
1261 		}
1262 	} while (resend);
1263 
1264 	return 0;
1265 }
1266 
1267 static int set_real_num_queues(struct net_device *netdev)
1268 {
1269 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1270 	int rc;
1271 
1272 	netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1273 		   adapter->req_tx_queues, adapter->req_rx_queues);
1274 
1275 	rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1276 	if (rc) {
1277 		netdev_err(netdev, "failed to set the number of tx queues\n");
1278 		return rc;
1279 	}
1280 
1281 	rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1282 	if (rc)
1283 		netdev_err(netdev, "failed to set the number of rx queues\n");
1284 
1285 	return rc;
1286 }
1287 
1288 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1289 {
1290 	struct device *dev = &adapter->vdev->dev;
1291 	union ibmvnic_crq crq;
1292 	int len = 0;
1293 	int rc;
1294 
1295 	if (adapter->vpd->buff)
1296 		len = adapter->vpd->len;
1297 
1298 	mutex_lock(&adapter->fw_lock);
1299 	adapter->fw_done_rc = 0;
1300 	reinit_completion(&adapter->fw_done);
1301 
1302 	crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1303 	crq.get_vpd_size.cmd = GET_VPD_SIZE;
1304 	rc = ibmvnic_send_crq(adapter, &crq);
1305 	if (rc) {
1306 		mutex_unlock(&adapter->fw_lock);
1307 		return rc;
1308 	}
1309 
1310 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1311 	if (rc) {
1312 		dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
1313 		mutex_unlock(&adapter->fw_lock);
1314 		return rc;
1315 	}
1316 	mutex_unlock(&adapter->fw_lock);
1317 
1318 	if (!adapter->vpd->len)
1319 		return -ENODATA;
1320 
1321 	if (!adapter->vpd->buff)
1322 		adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1323 	else if (adapter->vpd->len != len)
1324 		adapter->vpd->buff =
1325 			krealloc(adapter->vpd->buff,
1326 				 adapter->vpd->len, GFP_KERNEL);
1327 
1328 	if (!adapter->vpd->buff) {
1329 		dev_err(dev, "Could allocate VPD buffer\n");
1330 		return -ENOMEM;
1331 	}
1332 
1333 	adapter->vpd->dma_addr =
1334 		dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1335 			       DMA_FROM_DEVICE);
1336 	if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
1337 		dev_err(dev, "Could not map VPD buffer\n");
1338 		kfree(adapter->vpd->buff);
1339 		adapter->vpd->buff = NULL;
1340 		return -ENOMEM;
1341 	}
1342 
1343 	mutex_lock(&adapter->fw_lock);
1344 	adapter->fw_done_rc = 0;
1345 	reinit_completion(&adapter->fw_done);
1346 
1347 	crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1348 	crq.get_vpd.cmd = GET_VPD;
1349 	crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1350 	crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1351 	rc = ibmvnic_send_crq(adapter, &crq);
1352 	if (rc) {
1353 		kfree(adapter->vpd->buff);
1354 		adapter->vpd->buff = NULL;
1355 		mutex_unlock(&adapter->fw_lock);
1356 		return rc;
1357 	}
1358 
1359 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1360 	if (rc) {
1361 		dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1362 		kfree(adapter->vpd->buff);
1363 		adapter->vpd->buff = NULL;
1364 		mutex_unlock(&adapter->fw_lock);
1365 		return rc;
1366 	}
1367 
1368 	mutex_unlock(&adapter->fw_lock);
1369 	return 0;
1370 }
1371 
1372 static int init_resources(struct ibmvnic_adapter *adapter)
1373 {
1374 	struct net_device *netdev = adapter->netdev;
1375 	int rc;
1376 
1377 	rc = set_real_num_queues(netdev);
1378 	if (rc)
1379 		return rc;
1380 
1381 	adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1382 	if (!adapter->vpd)
1383 		return -ENOMEM;
1384 
1385 	/* Vital Product Data (VPD) */
1386 	rc = ibmvnic_get_vpd(adapter);
1387 	if (rc) {
1388 		netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1389 		return rc;
1390 	}
1391 
1392 	rc = init_napi(adapter);
1393 	if (rc)
1394 		return rc;
1395 
1396 	send_query_map(adapter);
1397 
1398 	rc = init_rx_pools(netdev);
1399 	if (rc)
1400 		return rc;
1401 
1402 	rc = init_tx_pools(netdev);
1403 	return rc;
1404 }
1405 
1406 static int __ibmvnic_open(struct net_device *netdev)
1407 {
1408 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1409 	enum vnic_state prev_state = adapter->state;
1410 	int i, rc;
1411 
1412 	adapter->state = VNIC_OPENING;
1413 	replenish_pools(adapter);
1414 	ibmvnic_napi_enable(adapter);
1415 
1416 	/* We're ready to receive frames, enable the sub-crq interrupts and
1417 	 * set the logical link state to up
1418 	 */
1419 	for (i = 0; i < adapter->req_rx_queues; i++) {
1420 		netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1421 		if (prev_state == VNIC_CLOSED)
1422 			enable_irq(adapter->rx_scrq[i]->irq);
1423 		enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1424 	}
1425 
1426 	for (i = 0; i < adapter->req_tx_queues; i++) {
1427 		netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1428 		if (prev_state == VNIC_CLOSED)
1429 			enable_irq(adapter->tx_scrq[i]->irq);
1430 		enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1431 		netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
1432 	}
1433 
1434 	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1435 	if (rc) {
1436 		ibmvnic_napi_disable(adapter);
1437 		release_resources(adapter);
1438 		return rc;
1439 	}
1440 
1441 	netif_tx_start_all_queues(netdev);
1442 
1443 	if (prev_state == VNIC_CLOSED) {
1444 		for (i = 0; i < adapter->req_rx_queues; i++)
1445 			napi_schedule(&adapter->napi[i]);
1446 	}
1447 
1448 	adapter->state = VNIC_OPEN;
1449 	return rc;
1450 }
1451 
1452 static int ibmvnic_open(struct net_device *netdev)
1453 {
1454 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1455 	int rc;
1456 
1457 	ASSERT_RTNL();
1458 
1459 	/* If device failover is pending or we are about to reset, just set
1460 	 * device state and return. Device operation will be handled by reset
1461 	 * routine.
1462 	 *
1463 	 * It should be safe to overwrite the adapter->state here. Since
1464 	 * we hold the rtnl, either the reset has not actually started or
1465 	 * the rtnl got dropped during the set_link_state() in do_reset().
1466 	 * In the former case, no one else is changing the state (again we
1467 	 * have the rtnl) and in the latter case, do_reset() will detect and
1468 	 * honor our setting below.
1469 	 */
1470 	if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
1471 		netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1472 			   adapter_state_to_string(adapter->state),
1473 			   adapter->failover_pending);
1474 		adapter->state = VNIC_OPEN;
1475 		rc = 0;
1476 		goto out;
1477 	}
1478 
1479 	if (adapter->state != VNIC_CLOSED) {
1480 		rc = ibmvnic_login(netdev);
1481 		if (rc)
1482 			goto out;
1483 
1484 		rc = init_resources(adapter);
1485 		if (rc) {
1486 			netdev_err(netdev, "failed to initialize resources\n");
1487 			release_resources(adapter);
1488 			release_rx_pools(adapter);
1489 			release_tx_pools(adapter);
1490 			goto out;
1491 		}
1492 	}
1493 
1494 	rc = __ibmvnic_open(netdev);
1495 
1496 out:
1497 	/* If open failed and there is a pending failover or in-progress reset,
1498 	 * set device state and return. Device operation will be handled by
1499 	 * reset routine. See also comments above regarding rtnl.
1500 	 */
1501 	if (rc &&
1502 	    (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
1503 		adapter->state = VNIC_OPEN;
1504 		rc = 0;
1505 	}
1506 	return rc;
1507 }
1508 
1509 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1510 {
1511 	struct ibmvnic_rx_pool *rx_pool;
1512 	struct ibmvnic_rx_buff *rx_buff;
1513 	u64 rx_entries;
1514 	int rx_scrqs;
1515 	int i, j;
1516 
1517 	if (!adapter->rx_pool)
1518 		return;
1519 
1520 	rx_scrqs = adapter->num_active_rx_pools;
1521 	rx_entries = adapter->req_rx_add_entries_per_subcrq;
1522 
1523 	/* Free any remaining skbs in the rx buffer pools */
1524 	for (i = 0; i < rx_scrqs; i++) {
1525 		rx_pool = &adapter->rx_pool[i];
1526 		if (!rx_pool || !rx_pool->rx_buff)
1527 			continue;
1528 
1529 		netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1530 		for (j = 0; j < rx_entries; j++) {
1531 			rx_buff = &rx_pool->rx_buff[j];
1532 			if (rx_buff && rx_buff->skb) {
1533 				dev_kfree_skb_any(rx_buff->skb);
1534 				rx_buff->skb = NULL;
1535 			}
1536 		}
1537 	}
1538 }
1539 
1540 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1541 			      struct ibmvnic_tx_pool *tx_pool)
1542 {
1543 	struct ibmvnic_tx_buff *tx_buff;
1544 	u64 tx_entries;
1545 	int i;
1546 
1547 	if (!tx_pool || !tx_pool->tx_buff)
1548 		return;
1549 
1550 	tx_entries = tx_pool->num_buffers;
1551 
1552 	for (i = 0; i < tx_entries; i++) {
1553 		tx_buff = &tx_pool->tx_buff[i];
1554 		if (tx_buff && tx_buff->skb) {
1555 			dev_kfree_skb_any(tx_buff->skb);
1556 			tx_buff->skb = NULL;
1557 		}
1558 	}
1559 }
1560 
1561 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1562 {
1563 	int tx_scrqs;
1564 	int i;
1565 
1566 	if (!adapter->tx_pool || !adapter->tso_pool)
1567 		return;
1568 
1569 	tx_scrqs = adapter->num_active_tx_pools;
1570 
1571 	/* Free any remaining skbs in the tx buffer pools */
1572 	for (i = 0; i < tx_scrqs; i++) {
1573 		netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1574 		clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1575 		clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1576 	}
1577 }
1578 
1579 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1580 {
1581 	struct net_device *netdev = adapter->netdev;
1582 	int i;
1583 
1584 	if (adapter->tx_scrq) {
1585 		for (i = 0; i < adapter->req_tx_queues; i++)
1586 			if (adapter->tx_scrq[i]->irq) {
1587 				netdev_dbg(netdev,
1588 					   "Disabling tx_scrq[%d] irq\n", i);
1589 				disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1590 				disable_irq(adapter->tx_scrq[i]->irq);
1591 			}
1592 	}
1593 
1594 	if (adapter->rx_scrq) {
1595 		for (i = 0; i < adapter->req_rx_queues; i++) {
1596 			if (adapter->rx_scrq[i]->irq) {
1597 				netdev_dbg(netdev,
1598 					   "Disabling rx_scrq[%d] irq\n", i);
1599 				disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1600 				disable_irq(adapter->rx_scrq[i]->irq);
1601 			}
1602 		}
1603 	}
1604 }
1605 
1606 static void ibmvnic_cleanup(struct net_device *netdev)
1607 {
1608 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1609 
1610 	/* ensure that transmissions are stopped if called by do_reset */
1611 	if (test_bit(0, &adapter->resetting))
1612 		netif_tx_disable(netdev);
1613 	else
1614 		netif_tx_stop_all_queues(netdev);
1615 
1616 	ibmvnic_napi_disable(adapter);
1617 	ibmvnic_disable_irqs(adapter);
1618 }
1619 
1620 static int __ibmvnic_close(struct net_device *netdev)
1621 {
1622 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1623 	int rc = 0;
1624 
1625 	adapter->state = VNIC_CLOSING;
1626 	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1627 	adapter->state = VNIC_CLOSED;
1628 	return rc;
1629 }
1630 
1631 static int ibmvnic_close(struct net_device *netdev)
1632 {
1633 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1634 	int rc;
1635 
1636 	netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
1637 		   adapter_state_to_string(adapter->state),
1638 		   adapter->failover_pending,
1639 		   adapter->force_reset_recovery);
1640 
1641 	/* If device failover is pending, just set device state and return.
1642 	 * Device operation will be handled by reset routine.
1643 	 */
1644 	if (adapter->failover_pending) {
1645 		adapter->state = VNIC_CLOSED;
1646 		return 0;
1647 	}
1648 
1649 	rc = __ibmvnic_close(netdev);
1650 	ibmvnic_cleanup(netdev);
1651 	clean_rx_pools(adapter);
1652 	clean_tx_pools(adapter);
1653 
1654 	return rc;
1655 }
1656 
1657 /**
1658  * build_hdr_data - creates L2/L3/L4 header data buffer
1659  * @hdr_field: bitfield determining needed headers
1660  * @skb: socket buffer
1661  * @hdr_len: array of header lengths
1662  * @hdr_data: buffer to write the header to
1663  *
1664  * Reads hdr_field to determine which headers are needed by firmware.
1665  * Builds a buffer containing these headers.  Saves individual header
1666  * lengths and total buffer length to be used to build descriptors.
1667  */
1668 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1669 			  int *hdr_len, u8 *hdr_data)
1670 {
1671 	int len = 0;
1672 	u8 *hdr;
1673 
1674 	if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1675 		hdr_len[0] = sizeof(struct vlan_ethhdr);
1676 	else
1677 		hdr_len[0] = sizeof(struct ethhdr);
1678 
1679 	if (skb->protocol == htons(ETH_P_IP)) {
1680 		hdr_len[1] = ip_hdr(skb)->ihl * 4;
1681 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1682 			hdr_len[2] = tcp_hdrlen(skb);
1683 		else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1684 			hdr_len[2] = sizeof(struct udphdr);
1685 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1686 		hdr_len[1] = sizeof(struct ipv6hdr);
1687 		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1688 			hdr_len[2] = tcp_hdrlen(skb);
1689 		else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1690 			hdr_len[2] = sizeof(struct udphdr);
1691 	} else if (skb->protocol == htons(ETH_P_ARP)) {
1692 		hdr_len[1] = arp_hdr_len(skb->dev);
1693 		hdr_len[2] = 0;
1694 	}
1695 
1696 	memset(hdr_data, 0, 120);
1697 	if ((hdr_field >> 6) & 1) {
1698 		hdr = skb_mac_header(skb);
1699 		memcpy(hdr_data, hdr, hdr_len[0]);
1700 		len += hdr_len[0];
1701 	}
1702 
1703 	if ((hdr_field >> 5) & 1) {
1704 		hdr = skb_network_header(skb);
1705 		memcpy(hdr_data + len, hdr, hdr_len[1]);
1706 		len += hdr_len[1];
1707 	}
1708 
1709 	if ((hdr_field >> 4) & 1) {
1710 		hdr = skb_transport_header(skb);
1711 		memcpy(hdr_data + len, hdr, hdr_len[2]);
1712 		len += hdr_len[2];
1713 	}
1714 	return len;
1715 }
1716 
1717 /**
1718  * create_hdr_descs - create header and header extension descriptors
1719  * @hdr_field: bitfield determining needed headers
1720  * @hdr_data: buffer containing header data
1721  * @len: length of data buffer
1722  * @hdr_len: array of individual header lengths
1723  * @scrq_arr: descriptor array
1724  *
1725  * Creates header and, if needed, header extension descriptors and
1726  * places them in a descriptor array, scrq_arr
1727  */
1728 
1729 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1730 			    union sub_crq *scrq_arr)
1731 {
1732 	union sub_crq hdr_desc;
1733 	int tmp_len = len;
1734 	int num_descs = 0;
1735 	u8 *data, *cur;
1736 	int tmp;
1737 
1738 	while (tmp_len > 0) {
1739 		cur = hdr_data + len - tmp_len;
1740 
1741 		memset(&hdr_desc, 0, sizeof(hdr_desc));
1742 		if (cur != hdr_data) {
1743 			data = hdr_desc.hdr_ext.data;
1744 			tmp = tmp_len > 29 ? 29 : tmp_len;
1745 			hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1746 			hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1747 			hdr_desc.hdr_ext.len = tmp;
1748 		} else {
1749 			data = hdr_desc.hdr.data;
1750 			tmp = tmp_len > 24 ? 24 : tmp_len;
1751 			hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1752 			hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1753 			hdr_desc.hdr.len = tmp;
1754 			hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1755 			hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1756 			hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1757 			hdr_desc.hdr.flag = hdr_field << 1;
1758 		}
1759 		memcpy(data, cur, tmp);
1760 		tmp_len -= tmp;
1761 		*scrq_arr = hdr_desc;
1762 		scrq_arr++;
1763 		num_descs++;
1764 	}
1765 
1766 	return num_descs;
1767 }
1768 
1769 /**
1770  * build_hdr_descs_arr - build a header descriptor array
1771  * @skb: tx socket buffer
1772  * @indir_arr: indirect array
1773  * @num_entries: number of descriptors to be sent
1774  * @hdr_field: bit field determining which headers will be sent
1775  *
1776  * This function will build a TX descriptor array with applicable
1777  * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1778  */
1779 
1780 static void build_hdr_descs_arr(struct sk_buff *skb,
1781 				union sub_crq *indir_arr,
1782 				int *num_entries, u8 hdr_field)
1783 {
1784 	int hdr_len[3] = {0, 0, 0};
1785 	u8 hdr_data[140] = {0};
1786 	int tot_len;
1787 
1788 	tot_len = build_hdr_data(hdr_field, skb, hdr_len,
1789 				 hdr_data);
1790 	*num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1791 					 indir_arr + 1);
1792 }
1793 
1794 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1795 				    struct net_device *netdev)
1796 {
1797 	/* For some backing devices, mishandling of small packets
1798 	 * can result in a loss of connection or TX stall. Device
1799 	 * architects recommend that no packet should be smaller
1800 	 * than the minimum MTU value provided to the driver, so
1801 	 * pad any packets to that length
1802 	 */
1803 	if (skb->len < netdev->min_mtu)
1804 		return skb_put_padto(skb, netdev->min_mtu);
1805 
1806 	return 0;
1807 }
1808 
1809 static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
1810 					 struct ibmvnic_sub_crq_queue *tx_scrq)
1811 {
1812 	struct ibmvnic_ind_xmit_queue *ind_bufp;
1813 	struct ibmvnic_tx_buff *tx_buff;
1814 	struct ibmvnic_tx_pool *tx_pool;
1815 	union sub_crq tx_scrq_entry;
1816 	int queue_num;
1817 	int entries;
1818 	int index;
1819 	int i;
1820 
1821 	ind_bufp = &tx_scrq->ind_buf;
1822 	entries = (u64)ind_bufp->index;
1823 	queue_num = tx_scrq->pool_index;
1824 
1825 	for (i = entries - 1; i >= 0; --i) {
1826 		tx_scrq_entry = ind_bufp->indir_arr[i];
1827 		if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
1828 			continue;
1829 		index = be32_to_cpu(tx_scrq_entry.v1.correlator);
1830 		if (index & IBMVNIC_TSO_POOL_MASK) {
1831 			tx_pool = &adapter->tso_pool[queue_num];
1832 			index &= ~IBMVNIC_TSO_POOL_MASK;
1833 		} else {
1834 			tx_pool = &adapter->tx_pool[queue_num];
1835 		}
1836 		tx_pool->free_map[tx_pool->consumer_index] = index;
1837 		tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
1838 					  tx_pool->num_buffers - 1 :
1839 					  tx_pool->consumer_index - 1;
1840 		tx_buff = &tx_pool->tx_buff[index];
1841 		adapter->netdev->stats.tx_packets--;
1842 		adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
1843 		adapter->tx_stats_buffers[queue_num].packets--;
1844 		adapter->tx_stats_buffers[queue_num].bytes -=
1845 						tx_buff->skb->len;
1846 		dev_kfree_skb_any(tx_buff->skb);
1847 		tx_buff->skb = NULL;
1848 		adapter->netdev->stats.tx_dropped++;
1849 	}
1850 	ind_bufp->index = 0;
1851 	if (atomic_sub_return(entries, &tx_scrq->used) <=
1852 	    (adapter->req_tx_entries_per_subcrq / 2) &&
1853 	    __netif_subqueue_stopped(adapter->netdev, queue_num) &&
1854 	    !test_bit(0, &adapter->resetting)) {
1855 		netif_wake_subqueue(adapter->netdev, queue_num);
1856 		netdev_dbg(adapter->netdev, "Started queue %d\n",
1857 			   queue_num);
1858 	}
1859 }
1860 
1861 static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
1862 				 struct ibmvnic_sub_crq_queue *tx_scrq)
1863 {
1864 	struct ibmvnic_ind_xmit_queue *ind_bufp;
1865 	u64 dma_addr;
1866 	u64 entries;
1867 	u64 handle;
1868 	int rc;
1869 
1870 	ind_bufp = &tx_scrq->ind_buf;
1871 	dma_addr = (u64)ind_bufp->indir_dma;
1872 	entries = (u64)ind_bufp->index;
1873 	handle = tx_scrq->handle;
1874 
1875 	if (!entries)
1876 		return 0;
1877 	rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
1878 	if (rc)
1879 		ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
1880 	else
1881 		ind_bufp->index = 0;
1882 	return 0;
1883 }
1884 
1885 static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1886 {
1887 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1888 	int queue_num = skb_get_queue_mapping(skb);
1889 	u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1890 	struct device *dev = &adapter->vdev->dev;
1891 	struct ibmvnic_ind_xmit_queue *ind_bufp;
1892 	struct ibmvnic_tx_buff *tx_buff = NULL;
1893 	struct ibmvnic_sub_crq_queue *tx_scrq;
1894 	struct ibmvnic_tx_pool *tx_pool;
1895 	unsigned int tx_send_failed = 0;
1896 	netdev_tx_t ret = NETDEV_TX_OK;
1897 	unsigned int tx_map_failed = 0;
1898 	union sub_crq indir_arr[16];
1899 	unsigned int tx_dropped = 0;
1900 	unsigned int tx_packets = 0;
1901 	unsigned int tx_bytes = 0;
1902 	dma_addr_t data_dma_addr;
1903 	struct netdev_queue *txq;
1904 	unsigned long lpar_rc;
1905 	union sub_crq tx_crq;
1906 	unsigned int offset;
1907 	int num_entries = 1;
1908 	unsigned char *dst;
1909 	int index = 0;
1910 	u8 proto = 0;
1911 
1912 	tx_scrq = adapter->tx_scrq[queue_num];
1913 	txq = netdev_get_tx_queue(netdev, queue_num);
1914 	ind_bufp = &tx_scrq->ind_buf;
1915 
1916 	if (test_bit(0, &adapter->resetting)) {
1917 		if (!netif_subqueue_stopped(netdev, skb))
1918 			netif_stop_subqueue(netdev, queue_num);
1919 		dev_kfree_skb_any(skb);
1920 
1921 		tx_send_failed++;
1922 		tx_dropped++;
1923 		ret = NETDEV_TX_OK;
1924 		goto out;
1925 	}
1926 
1927 	if (ibmvnic_xmit_workarounds(skb, netdev)) {
1928 		tx_dropped++;
1929 		tx_send_failed++;
1930 		ret = NETDEV_TX_OK;
1931 		ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1932 		goto out;
1933 	}
1934 	if (skb_is_gso(skb))
1935 		tx_pool = &adapter->tso_pool[queue_num];
1936 	else
1937 		tx_pool = &adapter->tx_pool[queue_num];
1938 
1939 	index = tx_pool->free_map[tx_pool->consumer_index];
1940 
1941 	if (index == IBMVNIC_INVALID_MAP) {
1942 		dev_kfree_skb_any(skb);
1943 		tx_send_failed++;
1944 		tx_dropped++;
1945 		ibmvnic_tx_scrq_flush(adapter, tx_scrq);
1946 		ret = NETDEV_TX_OK;
1947 		goto out;
1948 	}
1949 
1950 	tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1951 
1952 	offset = index * tx_pool->buf_size;
1953 	dst = tx_pool->long_term_buff.buff + offset;
1954 	memset(dst, 0, tx_pool->buf_size);
1955 	data_dma_addr = tx_pool->long_term_buff.addr + offset;
1956 
1957 	if (skb_shinfo(skb)->nr_frags) {
1958 		int cur, i;
1959 
1960 		/* Copy the head */
1961 		skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1962 		cur = skb_headlen(skb);
1963 
1964 		/* Copy the frags */
1965 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1966 			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1967 
1968 			memcpy(dst + cur, skb_frag_address(frag),
1969 			       skb_frag_size(frag));
1970 			cur += skb_frag_size(frag);
1971 		}
1972 	} else {
1973 		skb_copy_from_linear_data(skb, dst, skb->len);
1974 	}
1975 
1976 	/* post changes to long_term_buff *dst before VIOS accessing it */
1977 	dma_wmb();
1978 
1979 	tx_pool->consumer_index =
1980 	    (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1981 
1982 	tx_buff = &tx_pool->tx_buff[index];
1983 	tx_buff->skb = skb;
1984 	tx_buff->index = index;
1985 	tx_buff->pool_index = queue_num;
1986 
1987 	memset(&tx_crq, 0, sizeof(tx_crq));
1988 	tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1989 	tx_crq.v1.type = IBMVNIC_TX_DESC;
1990 	tx_crq.v1.n_crq_elem = 1;
1991 	tx_crq.v1.n_sge = 1;
1992 	tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1993 
1994 	if (skb_is_gso(skb))
1995 		tx_crq.v1.correlator =
1996 			cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1997 	else
1998 		tx_crq.v1.correlator = cpu_to_be32(index);
1999 	tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
2000 	tx_crq.v1.sge_len = cpu_to_be32(skb->len);
2001 	tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
2002 
2003 	if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
2004 		tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
2005 		tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
2006 	}
2007 
2008 	if (skb->protocol == htons(ETH_P_IP)) {
2009 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
2010 		proto = ip_hdr(skb)->protocol;
2011 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
2012 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
2013 		proto = ipv6_hdr(skb)->nexthdr;
2014 	}
2015 
2016 	if (proto == IPPROTO_TCP)
2017 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
2018 	else if (proto == IPPROTO_UDP)
2019 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
2020 
2021 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2022 		tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
2023 		hdrs += 2;
2024 	}
2025 	if (skb_is_gso(skb)) {
2026 		tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
2027 		tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
2028 		hdrs += 2;
2029 	}
2030 
2031 	if ((*hdrs >> 7) & 1)
2032 		build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
2033 
2034 	tx_crq.v1.n_crq_elem = num_entries;
2035 	tx_buff->num_entries = num_entries;
2036 	/* flush buffer if current entry can not fit */
2037 	if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
2038 		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2039 		if (lpar_rc != H_SUCCESS)
2040 			goto tx_flush_err;
2041 	}
2042 
2043 	indir_arr[0] = tx_crq;
2044 	memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
2045 	       num_entries * sizeof(struct ibmvnic_generic_scrq));
2046 	ind_bufp->index += num_entries;
2047 	if (__netdev_tx_sent_queue(txq, skb->len,
2048 				   netdev_xmit_more() &&
2049 				   ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
2050 		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2051 		if (lpar_rc != H_SUCCESS)
2052 			goto tx_err;
2053 	}
2054 
2055 	if (atomic_add_return(num_entries, &tx_scrq->used)
2056 					>= adapter->req_tx_entries_per_subcrq) {
2057 		netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
2058 		netif_stop_subqueue(netdev, queue_num);
2059 	}
2060 
2061 	tx_packets++;
2062 	tx_bytes += skb->len;
2063 	txq->trans_start = jiffies;
2064 	ret = NETDEV_TX_OK;
2065 	goto out;
2066 
2067 tx_flush_err:
2068 	dev_kfree_skb_any(skb);
2069 	tx_buff->skb = NULL;
2070 	tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
2071 				  tx_pool->num_buffers - 1 :
2072 				  tx_pool->consumer_index - 1;
2073 	tx_dropped++;
2074 tx_err:
2075 	if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
2076 		dev_err_ratelimited(dev, "tx: send failed\n");
2077 
2078 	if (lpar_rc == H_CLOSED || adapter->failover_pending) {
2079 		/* Disable TX and report carrier off if queue is closed
2080 		 * or pending failover.
2081 		 * Firmware guarantees that a signal will be sent to the
2082 		 * driver, triggering a reset or some other action.
2083 		 */
2084 		netif_tx_stop_all_queues(netdev);
2085 		netif_carrier_off(netdev);
2086 	}
2087 out:
2088 	netdev->stats.tx_dropped += tx_dropped;
2089 	netdev->stats.tx_bytes += tx_bytes;
2090 	netdev->stats.tx_packets += tx_packets;
2091 	adapter->tx_send_failed += tx_send_failed;
2092 	adapter->tx_map_failed += tx_map_failed;
2093 	adapter->tx_stats_buffers[queue_num].packets += tx_packets;
2094 	adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
2095 	adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
2096 
2097 	return ret;
2098 }
2099 
2100 static void ibmvnic_set_multi(struct net_device *netdev)
2101 {
2102 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2103 	struct netdev_hw_addr *ha;
2104 	union ibmvnic_crq crq;
2105 
2106 	memset(&crq, 0, sizeof(crq));
2107 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
2108 	crq.request_capability.cmd = REQUEST_CAPABILITY;
2109 
2110 	if (netdev->flags & IFF_PROMISC) {
2111 		if (!adapter->promisc_supported)
2112 			return;
2113 	} else {
2114 		if (netdev->flags & IFF_ALLMULTI) {
2115 			/* Accept all multicast */
2116 			memset(&crq, 0, sizeof(crq));
2117 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2118 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2119 			crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
2120 			ibmvnic_send_crq(adapter, &crq);
2121 		} else if (netdev_mc_empty(netdev)) {
2122 			/* Reject all multicast */
2123 			memset(&crq, 0, sizeof(crq));
2124 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2125 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2126 			crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
2127 			ibmvnic_send_crq(adapter, &crq);
2128 		} else {
2129 			/* Accept one or more multicast(s) */
2130 			netdev_for_each_mc_addr(ha, netdev) {
2131 				memset(&crq, 0, sizeof(crq));
2132 				crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2133 				crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2134 				crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
2135 				ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
2136 						ha->addr);
2137 				ibmvnic_send_crq(adapter, &crq);
2138 			}
2139 		}
2140 	}
2141 }
2142 
2143 static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
2144 {
2145 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2146 	union ibmvnic_crq crq;
2147 	int rc;
2148 
2149 	if (!is_valid_ether_addr(dev_addr)) {
2150 		rc = -EADDRNOTAVAIL;
2151 		goto err;
2152 	}
2153 
2154 	memset(&crq, 0, sizeof(crq));
2155 	crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
2156 	crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
2157 	ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
2158 
2159 	mutex_lock(&adapter->fw_lock);
2160 	adapter->fw_done_rc = 0;
2161 	reinit_completion(&adapter->fw_done);
2162 
2163 	rc = ibmvnic_send_crq(adapter, &crq);
2164 	if (rc) {
2165 		rc = -EIO;
2166 		mutex_unlock(&adapter->fw_lock);
2167 		goto err;
2168 	}
2169 
2170 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
2171 	/* netdev->dev_addr is changed in handle_change_mac_rsp function */
2172 	if (rc || adapter->fw_done_rc) {
2173 		rc = -EIO;
2174 		mutex_unlock(&adapter->fw_lock);
2175 		goto err;
2176 	}
2177 	mutex_unlock(&adapter->fw_lock);
2178 	return 0;
2179 err:
2180 	ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2181 	return rc;
2182 }
2183 
2184 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
2185 {
2186 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2187 	struct sockaddr *addr = p;
2188 	int rc;
2189 
2190 	rc = 0;
2191 	if (!is_valid_ether_addr(addr->sa_data))
2192 		return -EADDRNOTAVAIL;
2193 
2194 	ether_addr_copy(adapter->mac_addr, addr->sa_data);
2195 	if (adapter->state != VNIC_PROBED)
2196 		rc = __ibmvnic_set_mac(netdev, addr->sa_data);
2197 
2198 	return rc;
2199 }
2200 
2201 static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
2202 {
2203 	switch (reason) {
2204 	case VNIC_RESET_FAILOVER:
2205 		return "FAILOVER";
2206 	case VNIC_RESET_MOBILITY:
2207 		return "MOBILITY";
2208 	case VNIC_RESET_FATAL:
2209 		return "FATAL";
2210 	case VNIC_RESET_NON_FATAL:
2211 		return "NON_FATAL";
2212 	case VNIC_RESET_TIMEOUT:
2213 		return "TIMEOUT";
2214 	case VNIC_RESET_CHANGE_PARAM:
2215 		return "CHANGE_PARAM";
2216 	case VNIC_RESET_PASSIVE_INIT:
2217 		return "PASSIVE_INIT";
2218 	}
2219 	return "UNKNOWN";
2220 }
2221 
2222 /*
2223  * do_reset returns zero if we are able to keep processing reset events, or
2224  * non-zero if we hit a fatal error and must halt.
2225  */
2226 static int do_reset(struct ibmvnic_adapter *adapter,
2227 		    struct ibmvnic_rwi *rwi, u32 reset_state)
2228 {
2229 	struct net_device *netdev = adapter->netdev;
2230 	u64 old_num_rx_queues, old_num_tx_queues;
2231 	u64 old_num_rx_slots, old_num_tx_slots;
2232 	int rc;
2233 
2234 	netdev_dbg(adapter->netdev,
2235 		   "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2236 		   adapter_state_to_string(adapter->state),
2237 		   adapter->failover_pending,
2238 		   reset_reason_to_string(rwi->reset_reason),
2239 		   adapter_state_to_string(reset_state));
2240 
2241 	adapter->reset_reason = rwi->reset_reason;
2242 	/* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2243 	if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2244 		rtnl_lock();
2245 
2246 	/* Now that we have the rtnl lock, clear any pending failover.
2247 	 * This will ensure ibmvnic_open() has either completed or will
2248 	 * block until failover is complete.
2249 	 */
2250 	if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2251 		adapter->failover_pending = false;
2252 
2253 	/* read the state and check (again) after getting rtnl */
2254 	reset_state = adapter->state;
2255 
2256 	if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2257 		rc = -EBUSY;
2258 		goto out;
2259 	}
2260 
2261 	netif_carrier_off(netdev);
2262 
2263 	old_num_rx_queues = adapter->req_rx_queues;
2264 	old_num_tx_queues = adapter->req_tx_queues;
2265 	old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2266 	old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
2267 
2268 	ibmvnic_cleanup(netdev);
2269 
2270 	if (reset_state == VNIC_OPEN &&
2271 	    adapter->reset_reason != VNIC_RESET_MOBILITY &&
2272 	    adapter->reset_reason != VNIC_RESET_FAILOVER) {
2273 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2274 			rc = __ibmvnic_close(netdev);
2275 			if (rc)
2276 				goto out;
2277 		} else {
2278 			adapter->state = VNIC_CLOSING;
2279 
2280 			/* Release the RTNL lock before link state change and
2281 			 * re-acquire after the link state change to allow
2282 			 * linkwatch_event to grab the RTNL lock and run during
2283 			 * a reset.
2284 			 */
2285 			rtnl_unlock();
2286 			rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2287 			rtnl_lock();
2288 			if (rc)
2289 				goto out;
2290 
2291 			if (adapter->state == VNIC_OPEN) {
2292 				/* When we dropped rtnl, ibmvnic_open() got
2293 				 * it and noticed that we are resetting and
2294 				 * set the adapter state to OPEN. Update our
2295 				 * new "target" state, and resume the reset
2296 				 * from VNIC_CLOSING state.
2297 				 */
2298 				netdev_dbg(netdev,
2299 					   "Open changed state from %s, updating.\n",
2300 					   adapter_state_to_string(reset_state));
2301 				reset_state = VNIC_OPEN;
2302 				adapter->state = VNIC_CLOSING;
2303 			}
2304 
2305 			if (adapter->state != VNIC_CLOSING) {
2306 				/* If someone else changed the adapter state
2307 				 * when we dropped the rtnl, fail the reset
2308 				 */
2309 				rc = -1;
2310 				goto out;
2311 			}
2312 			adapter->state = VNIC_CLOSED;
2313 		}
2314 	}
2315 
2316 	if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2317 		release_resources(adapter);
2318 		release_sub_crqs(adapter, 1);
2319 		release_crq_queue(adapter);
2320 	}
2321 
2322 	if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2323 		/* remove the closed state so when we call open it appears
2324 		 * we are coming from the probed state.
2325 		 */
2326 		adapter->state = VNIC_PROBED;
2327 
2328 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2329 			rc = init_crq_queue(adapter);
2330 		} else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
2331 			rc = ibmvnic_reenable_crq_queue(adapter);
2332 			release_sub_crqs(adapter, 1);
2333 		} else {
2334 			rc = ibmvnic_reset_crq(adapter);
2335 			if (rc == H_CLOSED || rc == H_SUCCESS) {
2336 				rc = vio_enable_interrupts(adapter->vdev);
2337 				if (rc)
2338 					netdev_err(adapter->netdev,
2339 						   "Reset failed to enable interrupts. rc=%d\n",
2340 						   rc);
2341 			}
2342 		}
2343 
2344 		if (rc) {
2345 			netdev_err(adapter->netdev,
2346 				   "Reset couldn't initialize crq. rc=%d\n", rc);
2347 			goto out;
2348 		}
2349 
2350 		rc = ibmvnic_reset_init(adapter, true);
2351 		if (rc) {
2352 			rc = IBMVNIC_INIT_FAILED;
2353 			goto out;
2354 		}
2355 
2356 		/* If the adapter was in PROBE or DOWN state prior to the reset,
2357 		 * exit here.
2358 		 */
2359 		if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) {
2360 			rc = 0;
2361 			goto out;
2362 		}
2363 
2364 		rc = ibmvnic_login(netdev);
2365 		if (rc)
2366 			goto out;
2367 
2368 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2369 			rc = init_resources(adapter);
2370 			if (rc)
2371 				goto out;
2372 		} else if (adapter->req_rx_queues != old_num_rx_queues ||
2373 		    adapter->req_tx_queues != old_num_tx_queues ||
2374 		    adapter->req_rx_add_entries_per_subcrq !=
2375 		    old_num_rx_slots ||
2376 		    adapter->req_tx_entries_per_subcrq !=
2377 		    old_num_tx_slots ||
2378 		    !adapter->rx_pool ||
2379 		    !adapter->tso_pool ||
2380 		    !adapter->tx_pool) {
2381 			release_napi(adapter);
2382 			release_vpd_data(adapter);
2383 
2384 			rc = init_resources(adapter);
2385 			if (rc)
2386 				goto out;
2387 
2388 		} else {
2389 			rc = init_tx_pools(netdev);
2390 			if (rc) {
2391 				netdev_dbg(netdev,
2392 					   "init tx pools failed (%d)\n",
2393 					   rc);
2394 				goto out;
2395 			}
2396 
2397 			rc = init_rx_pools(netdev);
2398 			if (rc) {
2399 				netdev_dbg(netdev,
2400 					   "init rx pools failed (%d)\n",
2401 					   rc);
2402 				goto out;
2403 			}
2404 		}
2405 		ibmvnic_disable_irqs(adapter);
2406 	}
2407 	adapter->state = VNIC_CLOSED;
2408 
2409 	if (reset_state == VNIC_CLOSED) {
2410 		rc = 0;
2411 		goto out;
2412 	}
2413 
2414 	rc = __ibmvnic_open(netdev);
2415 	if (rc) {
2416 		rc = IBMVNIC_OPEN_FAILED;
2417 		goto out;
2418 	}
2419 
2420 	/* refresh device's multicast list */
2421 	ibmvnic_set_multi(netdev);
2422 
2423 	if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
2424 	    adapter->reset_reason == VNIC_RESET_MOBILITY)
2425 		__netdev_notify_peers(netdev);
2426 
2427 	rc = 0;
2428 
2429 out:
2430 	/* restore the adapter state if reset failed */
2431 	if (rc)
2432 		adapter->state = reset_state;
2433 	/* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2434 	if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2435 		rtnl_unlock();
2436 
2437 	netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2438 		   adapter_state_to_string(adapter->state),
2439 		   adapter->failover_pending, rc);
2440 	return rc;
2441 }
2442 
2443 static int do_hard_reset(struct ibmvnic_adapter *adapter,
2444 			 struct ibmvnic_rwi *rwi, u32 reset_state)
2445 {
2446 	struct net_device *netdev = adapter->netdev;
2447 	int rc;
2448 
2449 	netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2450 		   reset_reason_to_string(rwi->reset_reason));
2451 
2452 	/* read the state and check (again) after getting rtnl */
2453 	reset_state = adapter->state;
2454 
2455 	if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2456 		rc = -EBUSY;
2457 		goto out;
2458 	}
2459 
2460 	netif_carrier_off(netdev);
2461 	adapter->reset_reason = rwi->reset_reason;
2462 
2463 	ibmvnic_cleanup(netdev);
2464 	release_resources(adapter);
2465 	release_sub_crqs(adapter, 0);
2466 	release_crq_queue(adapter);
2467 
2468 	/* remove the closed state so when we call open it appears
2469 	 * we are coming from the probed state.
2470 	 */
2471 	adapter->state = VNIC_PROBED;
2472 
2473 	reinit_completion(&adapter->init_done);
2474 	rc = init_crq_queue(adapter);
2475 	if (rc) {
2476 		netdev_err(adapter->netdev,
2477 			   "Couldn't initialize crq. rc=%d\n", rc);
2478 		goto out;
2479 	}
2480 
2481 	rc = ibmvnic_reset_init(adapter, false);
2482 	if (rc)
2483 		goto out;
2484 
2485 	/* If the adapter was in PROBE or DOWN state prior to the reset,
2486 	 * exit here.
2487 	 */
2488 	if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN)
2489 		goto out;
2490 
2491 	rc = ibmvnic_login(netdev);
2492 	if (rc)
2493 		goto out;
2494 
2495 	rc = init_resources(adapter);
2496 	if (rc)
2497 		goto out;
2498 
2499 	ibmvnic_disable_irqs(adapter);
2500 	adapter->state = VNIC_CLOSED;
2501 
2502 	if (reset_state == VNIC_CLOSED)
2503 		goto out;
2504 
2505 	rc = __ibmvnic_open(netdev);
2506 	if (rc) {
2507 		rc = IBMVNIC_OPEN_FAILED;
2508 		goto out;
2509 	}
2510 
2511 	__netdev_notify_peers(netdev);
2512 out:
2513 	/* restore adapter state if reset failed */
2514 	if (rc)
2515 		adapter->state = reset_state;
2516 	netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2517 		   adapter_state_to_string(adapter->state),
2518 		   adapter->failover_pending, rc);
2519 	return rc;
2520 }
2521 
2522 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2523 {
2524 	struct ibmvnic_rwi *rwi;
2525 	unsigned long flags;
2526 
2527 	spin_lock_irqsave(&adapter->rwi_lock, flags);
2528 
2529 	if (!list_empty(&adapter->rwi_list)) {
2530 		rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2531 				       list);
2532 		list_del(&rwi->list);
2533 	} else {
2534 		rwi = NULL;
2535 	}
2536 
2537 	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2538 	return rwi;
2539 }
2540 
2541 /**
2542  * do_passive_init - complete probing when partner device is detected.
2543  * @adapter: ibmvnic_adapter struct
2544  *
2545  * If the ibmvnic device does not have a partner device to communicate with at boot
2546  * and that partner device comes online at a later time, this function is called
2547  * to complete the initialization process of ibmvnic device.
2548  * Caller is expected to hold rtnl_lock().
2549  *
2550  * Returns non-zero if sub-CRQs are not initialized properly leaving the device
2551  * in the down state.
2552  * Returns 0 upon success and the device is in PROBED state.
2553  */
2554 
2555 static int do_passive_init(struct ibmvnic_adapter *adapter)
2556 {
2557 	unsigned long timeout = msecs_to_jiffies(30000);
2558 	struct net_device *netdev = adapter->netdev;
2559 	struct device *dev = &adapter->vdev->dev;
2560 	int rc;
2561 
2562 	netdev_dbg(netdev, "Partner device found, probing.\n");
2563 
2564 	adapter->state = VNIC_PROBING;
2565 	reinit_completion(&adapter->init_done);
2566 	adapter->init_done_rc = 0;
2567 	adapter->crq.active = true;
2568 
2569 	rc = send_crq_init_complete(adapter);
2570 	if (rc)
2571 		goto out;
2572 
2573 	rc = send_version_xchg(adapter);
2574 	if (rc)
2575 		netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc);
2576 
2577 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
2578 		dev_err(dev, "Initialization sequence timed out\n");
2579 		rc = -ETIMEDOUT;
2580 		goto out;
2581 	}
2582 
2583 	rc = init_sub_crqs(adapter);
2584 	if (rc) {
2585 		dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc);
2586 		goto out;
2587 	}
2588 
2589 	rc = init_sub_crq_irqs(adapter);
2590 	if (rc) {
2591 		dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc);
2592 		goto init_failed;
2593 	}
2594 
2595 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
2596 	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
2597 	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
2598 
2599 	adapter->state = VNIC_PROBED;
2600 	netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n");
2601 
2602 	return 0;
2603 
2604 init_failed:
2605 	release_sub_crqs(adapter, 1);
2606 out:
2607 	adapter->state = VNIC_DOWN;
2608 	return rc;
2609 }
2610 
2611 static void __ibmvnic_reset(struct work_struct *work)
2612 {
2613 	struct ibmvnic_adapter *adapter;
2614 	bool saved_state = false;
2615 	struct ibmvnic_rwi *tmprwi;
2616 	struct ibmvnic_rwi *rwi;
2617 	unsigned long flags;
2618 	u32 reset_state;
2619 	int rc = 0;
2620 
2621 	adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
2622 
2623 	if (test_and_set_bit_lock(0, &adapter->resetting)) {
2624 		queue_delayed_work(system_long_wq,
2625 				   &adapter->ibmvnic_delayed_reset,
2626 				   IBMVNIC_RESET_DELAY);
2627 		return;
2628 	}
2629 
2630 	rwi = get_next_rwi(adapter);
2631 	while (rwi) {
2632 		spin_lock_irqsave(&adapter->state_lock, flags);
2633 
2634 		if (adapter->state == VNIC_REMOVING ||
2635 		    adapter->state == VNIC_REMOVED) {
2636 			spin_unlock_irqrestore(&adapter->state_lock, flags);
2637 			kfree(rwi);
2638 			rc = EBUSY;
2639 			break;
2640 		}
2641 
2642 		if (!saved_state) {
2643 			reset_state = adapter->state;
2644 			saved_state = true;
2645 		}
2646 		spin_unlock_irqrestore(&adapter->state_lock, flags);
2647 
2648 		if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) {
2649 			rtnl_lock();
2650 			rc = do_passive_init(adapter);
2651 			rtnl_unlock();
2652 			if (!rc)
2653 				netif_carrier_on(adapter->netdev);
2654 		} else if (adapter->force_reset_recovery) {
2655 			/* Since we are doing a hard reset now, clear the
2656 			 * failover_pending flag so we don't ignore any
2657 			 * future MOBILITY or other resets.
2658 			 */
2659 			adapter->failover_pending = false;
2660 
2661 			/* Transport event occurred during previous reset */
2662 			if (adapter->wait_for_reset) {
2663 				/* Previous was CHANGE_PARAM; caller locked */
2664 				adapter->force_reset_recovery = false;
2665 				rc = do_hard_reset(adapter, rwi, reset_state);
2666 			} else {
2667 				rtnl_lock();
2668 				adapter->force_reset_recovery = false;
2669 				rc = do_hard_reset(adapter, rwi, reset_state);
2670 				rtnl_unlock();
2671 			}
2672 			if (rc) {
2673 				/* give backing device time to settle down */
2674 				netdev_dbg(adapter->netdev,
2675 					   "[S:%s] Hard reset failed, waiting 60 secs\n",
2676 					   adapter_state_to_string(adapter->state));
2677 				set_current_state(TASK_UNINTERRUPTIBLE);
2678 				schedule_timeout(60 * HZ);
2679 			}
2680 		} else {
2681 			rc = do_reset(adapter, rwi, reset_state);
2682 		}
2683 		tmprwi = rwi;
2684 		adapter->last_reset_time = jiffies;
2685 
2686 		if (rc)
2687 			netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
2688 
2689 		rwi = get_next_rwi(adapter);
2690 
2691 		/*
2692 		 * If there is another reset queued, free the previous rwi
2693 		 * and process the new reset even if previous reset failed
2694 		 * (the previous reset could have failed because of a fail
2695 		 * over for instance, so process the fail over).
2696 		 *
2697 		 * If there are no resets queued and the previous reset failed,
2698 		 * the adapter would be in an undefined state. So retry the
2699 		 * previous reset as a hard reset.
2700 		 */
2701 		if (rwi)
2702 			kfree(tmprwi);
2703 		else if (rc)
2704 			rwi = tmprwi;
2705 
2706 		if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
2707 			    rwi->reset_reason == VNIC_RESET_MOBILITY || rc))
2708 			adapter->force_reset_recovery = true;
2709 	}
2710 
2711 	if (adapter->wait_for_reset) {
2712 		adapter->reset_done_rc = rc;
2713 		complete(&adapter->reset_done);
2714 	}
2715 
2716 	clear_bit_unlock(0, &adapter->resetting);
2717 
2718 	netdev_dbg(adapter->netdev,
2719 		   "[S:%s FRR:%d WFR:%d] Done processing resets\n",
2720 		   adapter_state_to_string(adapter->state),
2721 		   adapter->force_reset_recovery,
2722 		   adapter->wait_for_reset);
2723 }
2724 
2725 static void __ibmvnic_delayed_reset(struct work_struct *work)
2726 {
2727 	struct ibmvnic_adapter *adapter;
2728 
2729 	adapter = container_of(work, struct ibmvnic_adapter,
2730 			       ibmvnic_delayed_reset.work);
2731 	__ibmvnic_reset(&adapter->ibmvnic_reset);
2732 }
2733 
2734 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2735 			 enum ibmvnic_reset_reason reason)
2736 {
2737 	struct list_head *entry, *tmp_entry;
2738 	struct ibmvnic_rwi *rwi, *tmp;
2739 	struct net_device *netdev = adapter->netdev;
2740 	unsigned long flags;
2741 	int ret;
2742 
2743 	spin_lock_irqsave(&adapter->rwi_lock, flags);
2744 
2745 	/* If failover is pending don't schedule any other reset.
2746 	 * Instead let the failover complete. If there is already a
2747 	 * a failover reset scheduled, we will detect and drop the
2748 	 * duplicate reset when walking the ->rwi_list below.
2749 	 */
2750 	if (adapter->state == VNIC_REMOVING ||
2751 	    adapter->state == VNIC_REMOVED ||
2752 	    (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
2753 		ret = EBUSY;
2754 		netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
2755 		goto err;
2756 	}
2757 
2758 	if (adapter->state == VNIC_PROBING) {
2759 		netdev_warn(netdev, "Adapter reset during probe\n");
2760 		adapter->init_done_rc = EAGAIN;
2761 		ret = EAGAIN;
2762 		goto err;
2763 	}
2764 
2765 	list_for_each_entry(tmp, &adapter->rwi_list, list) {
2766 		if (tmp->reset_reason == reason) {
2767 			netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
2768 				   reset_reason_to_string(reason));
2769 			ret = EBUSY;
2770 			goto err;
2771 		}
2772 	}
2773 
2774 	rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
2775 	if (!rwi) {
2776 		ret = ENOMEM;
2777 		goto err;
2778 	}
2779 	/* if we just received a transport event,
2780 	 * flush reset queue and process this reset
2781 	 */
2782 	if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2783 		list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2784 			list_del(entry);
2785 	}
2786 	rwi->reset_reason = reason;
2787 	list_add_tail(&rwi->list, &adapter->rwi_list);
2788 	netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
2789 		   reset_reason_to_string(reason));
2790 	queue_work(system_long_wq, &adapter->ibmvnic_reset);
2791 
2792 	ret = 0;
2793 err:
2794 	/* ibmvnic_close() below can block, so drop the lock first */
2795 	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2796 
2797 	if (ret == ENOMEM)
2798 		ibmvnic_close(netdev);
2799 
2800 	return -ret;
2801 }
2802 
2803 static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
2804 {
2805 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2806 
2807 	if (test_bit(0, &adapter->resetting)) {
2808 		netdev_err(adapter->netdev,
2809 			   "Adapter is resetting, skip timeout reset\n");
2810 		return;
2811 	}
2812 	/* No queuing up reset until at least 5 seconds (default watchdog val)
2813 	 * after last reset
2814 	 */
2815 	if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
2816 		netdev_dbg(dev, "Not yet time to tx timeout.\n");
2817 		return;
2818 	}
2819 	ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2820 }
2821 
2822 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2823 				  struct ibmvnic_rx_buff *rx_buff)
2824 {
2825 	struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2826 
2827 	rx_buff->skb = NULL;
2828 
2829 	pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2830 	pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2831 
2832 	atomic_dec(&pool->available);
2833 }
2834 
2835 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2836 {
2837 	struct ibmvnic_sub_crq_queue *rx_scrq;
2838 	struct ibmvnic_adapter *adapter;
2839 	struct net_device *netdev;
2840 	int frames_processed;
2841 	int scrq_num;
2842 
2843 	netdev = napi->dev;
2844 	adapter = netdev_priv(netdev);
2845 	scrq_num = (int)(napi - adapter->napi);
2846 	frames_processed = 0;
2847 	rx_scrq = adapter->rx_scrq[scrq_num];
2848 
2849 restart_poll:
2850 	while (frames_processed < budget) {
2851 		struct sk_buff *skb;
2852 		struct ibmvnic_rx_buff *rx_buff;
2853 		union sub_crq *next;
2854 		u32 length;
2855 		u16 offset;
2856 		u8 flags = 0;
2857 
2858 		if (unlikely(test_bit(0, &adapter->resetting) &&
2859 			     adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2860 			enable_scrq_irq(adapter, rx_scrq);
2861 			napi_complete_done(napi, frames_processed);
2862 			return frames_processed;
2863 		}
2864 
2865 		if (!pending_scrq(adapter, rx_scrq))
2866 			break;
2867 		next = ibmvnic_next_scrq(adapter, rx_scrq);
2868 		rx_buff = (struct ibmvnic_rx_buff *)
2869 			  be64_to_cpu(next->rx_comp.correlator);
2870 		/* do error checking */
2871 		if (next->rx_comp.rc) {
2872 			netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2873 				   be16_to_cpu(next->rx_comp.rc));
2874 			/* free the entry */
2875 			next->rx_comp.first = 0;
2876 			dev_kfree_skb_any(rx_buff->skb);
2877 			remove_buff_from_pool(adapter, rx_buff);
2878 			continue;
2879 		} else if (!rx_buff->skb) {
2880 			/* free the entry */
2881 			next->rx_comp.first = 0;
2882 			remove_buff_from_pool(adapter, rx_buff);
2883 			continue;
2884 		}
2885 
2886 		length = be32_to_cpu(next->rx_comp.len);
2887 		offset = be16_to_cpu(next->rx_comp.off_frame_data);
2888 		flags = next->rx_comp.flags;
2889 		skb = rx_buff->skb;
2890 		/* load long_term_buff before copying to skb */
2891 		dma_rmb();
2892 		skb_copy_to_linear_data(skb, rx_buff->data + offset,
2893 					length);
2894 
2895 		/* VLAN Header has been stripped by the system firmware and
2896 		 * needs to be inserted by the driver
2897 		 */
2898 		if (adapter->rx_vlan_header_insertion &&
2899 		    (flags & IBMVNIC_VLAN_STRIPPED))
2900 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2901 					       ntohs(next->rx_comp.vlan_tci));
2902 
2903 		/* free the entry */
2904 		next->rx_comp.first = 0;
2905 		remove_buff_from_pool(adapter, rx_buff);
2906 
2907 		skb_put(skb, length);
2908 		skb->protocol = eth_type_trans(skb, netdev);
2909 		skb_record_rx_queue(skb, scrq_num);
2910 
2911 		if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2912 		    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2913 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2914 		}
2915 
2916 		length = skb->len;
2917 		napi_gro_receive(napi, skb); /* send it up */
2918 		netdev->stats.rx_packets++;
2919 		netdev->stats.rx_bytes += length;
2920 		adapter->rx_stats_buffers[scrq_num].packets++;
2921 		adapter->rx_stats_buffers[scrq_num].bytes += length;
2922 		frames_processed++;
2923 	}
2924 
2925 	if (adapter->state != VNIC_CLOSING &&
2926 	    ((atomic_read(&adapter->rx_pool[scrq_num].available) <
2927 	      adapter->req_rx_add_entries_per_subcrq / 2) ||
2928 	      frames_processed < budget))
2929 		replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2930 	if (frames_processed < budget) {
2931 		if (napi_complete_done(napi, frames_processed)) {
2932 			enable_scrq_irq(adapter, rx_scrq);
2933 			if (pending_scrq(adapter, rx_scrq)) {
2934 				if (napi_reschedule(napi)) {
2935 					disable_scrq_irq(adapter, rx_scrq);
2936 					goto restart_poll;
2937 				}
2938 			}
2939 		}
2940 	}
2941 	return frames_processed;
2942 }
2943 
2944 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2945 {
2946 	int rc, ret;
2947 
2948 	adapter->fallback.mtu = adapter->req_mtu;
2949 	adapter->fallback.rx_queues = adapter->req_rx_queues;
2950 	adapter->fallback.tx_queues = adapter->req_tx_queues;
2951 	adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2952 	adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2953 
2954 	reinit_completion(&adapter->reset_done);
2955 	adapter->wait_for_reset = true;
2956 	rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2957 
2958 	if (rc) {
2959 		ret = rc;
2960 		goto out;
2961 	}
2962 	rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
2963 	if (rc) {
2964 		ret = -ENODEV;
2965 		goto out;
2966 	}
2967 
2968 	ret = 0;
2969 	if (adapter->reset_done_rc) {
2970 		ret = -EIO;
2971 		adapter->desired.mtu = adapter->fallback.mtu;
2972 		adapter->desired.rx_queues = adapter->fallback.rx_queues;
2973 		adapter->desired.tx_queues = adapter->fallback.tx_queues;
2974 		adapter->desired.rx_entries = adapter->fallback.rx_entries;
2975 		adapter->desired.tx_entries = adapter->fallback.tx_entries;
2976 
2977 		reinit_completion(&adapter->reset_done);
2978 		adapter->wait_for_reset = true;
2979 		rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2980 		if (rc) {
2981 			ret = rc;
2982 			goto out;
2983 		}
2984 		rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
2985 						 60000);
2986 		if (rc) {
2987 			ret = -ENODEV;
2988 			goto out;
2989 		}
2990 	}
2991 out:
2992 	adapter->wait_for_reset = false;
2993 
2994 	return ret;
2995 }
2996 
2997 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2998 {
2999 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3000 
3001 	adapter->desired.mtu = new_mtu + ETH_HLEN;
3002 
3003 	return wait_for_reset(adapter);
3004 }
3005 
3006 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
3007 						struct net_device *dev,
3008 						netdev_features_t features)
3009 {
3010 	/* Some backing hardware adapters can not
3011 	 * handle packets with a MSS less than 224
3012 	 * or with only one segment.
3013 	 */
3014 	if (skb_is_gso(skb)) {
3015 		if (skb_shinfo(skb)->gso_size < 224 ||
3016 		    skb_shinfo(skb)->gso_segs == 1)
3017 			features &= ~NETIF_F_GSO_MASK;
3018 	}
3019 
3020 	return features;
3021 }
3022 
3023 static const struct net_device_ops ibmvnic_netdev_ops = {
3024 	.ndo_open		= ibmvnic_open,
3025 	.ndo_stop		= ibmvnic_close,
3026 	.ndo_start_xmit		= ibmvnic_xmit,
3027 	.ndo_set_rx_mode	= ibmvnic_set_multi,
3028 	.ndo_set_mac_address	= ibmvnic_set_mac,
3029 	.ndo_validate_addr	= eth_validate_addr,
3030 	.ndo_tx_timeout		= ibmvnic_tx_timeout,
3031 	.ndo_change_mtu		= ibmvnic_change_mtu,
3032 	.ndo_features_check     = ibmvnic_features_check,
3033 };
3034 
3035 /* ethtool functions */
3036 
3037 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
3038 				      struct ethtool_link_ksettings *cmd)
3039 {
3040 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3041 	int rc;
3042 
3043 	rc = send_query_phys_parms(adapter);
3044 	if (rc) {
3045 		adapter->speed = SPEED_UNKNOWN;
3046 		adapter->duplex = DUPLEX_UNKNOWN;
3047 	}
3048 	cmd->base.speed = adapter->speed;
3049 	cmd->base.duplex = adapter->duplex;
3050 	cmd->base.port = PORT_FIBRE;
3051 	cmd->base.phy_address = 0;
3052 	cmd->base.autoneg = AUTONEG_ENABLE;
3053 
3054 	return 0;
3055 }
3056 
3057 static void ibmvnic_get_drvinfo(struct net_device *netdev,
3058 				struct ethtool_drvinfo *info)
3059 {
3060 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3061 
3062 	strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
3063 	strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
3064 	strscpy(info->fw_version, adapter->fw_version,
3065 		sizeof(info->fw_version));
3066 }
3067 
3068 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
3069 {
3070 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3071 
3072 	return adapter->msg_enable;
3073 }
3074 
3075 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
3076 {
3077 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3078 
3079 	adapter->msg_enable = data;
3080 }
3081 
3082 static u32 ibmvnic_get_link(struct net_device *netdev)
3083 {
3084 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3085 
3086 	/* Don't need to send a query because we request a logical link up at
3087 	 * init and then we wait for link state indications
3088 	 */
3089 	return adapter->logical_link_state;
3090 }
3091 
3092 static void ibmvnic_get_ringparam(struct net_device *netdev,
3093 				  struct ethtool_ringparam *ring)
3094 {
3095 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3096 
3097 	if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
3098 		ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
3099 		ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
3100 	} else {
3101 		ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
3102 		ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
3103 	}
3104 	ring->rx_mini_max_pending = 0;
3105 	ring->rx_jumbo_max_pending = 0;
3106 	ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
3107 	ring->tx_pending = adapter->req_tx_entries_per_subcrq;
3108 	ring->rx_mini_pending = 0;
3109 	ring->rx_jumbo_pending = 0;
3110 }
3111 
3112 static int ibmvnic_set_ringparam(struct net_device *netdev,
3113 				 struct ethtool_ringparam *ring)
3114 {
3115 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3116 	int ret;
3117 
3118 	ret = 0;
3119 	adapter->desired.rx_entries = ring->rx_pending;
3120 	adapter->desired.tx_entries = ring->tx_pending;
3121 
3122 	ret = wait_for_reset(adapter);
3123 
3124 	if (!ret &&
3125 	    (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
3126 	     adapter->req_tx_entries_per_subcrq != ring->tx_pending))
3127 		netdev_info(netdev,
3128 			    "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
3129 			    ring->rx_pending, ring->tx_pending,
3130 			    adapter->req_rx_add_entries_per_subcrq,
3131 			    adapter->req_tx_entries_per_subcrq);
3132 	return ret;
3133 }
3134 
3135 static void ibmvnic_get_channels(struct net_device *netdev,
3136 				 struct ethtool_channels *channels)
3137 {
3138 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3139 
3140 	if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
3141 		channels->max_rx = adapter->max_rx_queues;
3142 		channels->max_tx = adapter->max_tx_queues;
3143 	} else {
3144 		channels->max_rx = IBMVNIC_MAX_QUEUES;
3145 		channels->max_tx = IBMVNIC_MAX_QUEUES;
3146 	}
3147 
3148 	channels->max_other = 0;
3149 	channels->max_combined = 0;
3150 	channels->rx_count = adapter->req_rx_queues;
3151 	channels->tx_count = adapter->req_tx_queues;
3152 	channels->other_count = 0;
3153 	channels->combined_count = 0;
3154 }
3155 
3156 static int ibmvnic_set_channels(struct net_device *netdev,
3157 				struct ethtool_channels *channels)
3158 {
3159 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3160 	int ret;
3161 
3162 	ret = 0;
3163 	adapter->desired.rx_queues = channels->rx_count;
3164 	adapter->desired.tx_queues = channels->tx_count;
3165 
3166 	ret = wait_for_reset(adapter);
3167 
3168 	if (!ret &&
3169 	    (adapter->req_rx_queues != channels->rx_count ||
3170 	     adapter->req_tx_queues != channels->tx_count))
3171 		netdev_info(netdev,
3172 			    "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
3173 			    channels->rx_count, channels->tx_count,
3174 			    adapter->req_rx_queues, adapter->req_tx_queues);
3175 	return ret;
3176 }
3177 
3178 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3179 {
3180 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3181 	int i;
3182 
3183 	switch (stringset) {
3184 	case ETH_SS_STATS:
3185 		for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
3186 				i++, data += ETH_GSTRING_LEN)
3187 			memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
3188 
3189 		for (i = 0; i < adapter->req_tx_queues; i++) {
3190 			snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
3191 			data += ETH_GSTRING_LEN;
3192 
3193 			snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
3194 			data += ETH_GSTRING_LEN;
3195 
3196 			snprintf(data, ETH_GSTRING_LEN,
3197 				 "tx%d_dropped_packets", i);
3198 			data += ETH_GSTRING_LEN;
3199 		}
3200 
3201 		for (i = 0; i < adapter->req_rx_queues; i++) {
3202 			snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
3203 			data += ETH_GSTRING_LEN;
3204 
3205 			snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
3206 			data += ETH_GSTRING_LEN;
3207 
3208 			snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
3209 			data += ETH_GSTRING_LEN;
3210 		}
3211 		break;
3212 
3213 	case ETH_SS_PRIV_FLAGS:
3214 		for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
3215 			strcpy(data + i * ETH_GSTRING_LEN,
3216 			       ibmvnic_priv_flags[i]);
3217 		break;
3218 	default:
3219 		return;
3220 	}
3221 }
3222 
3223 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
3224 {
3225 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3226 
3227 	switch (sset) {
3228 	case ETH_SS_STATS:
3229 		return ARRAY_SIZE(ibmvnic_stats) +
3230 		       adapter->req_tx_queues * NUM_TX_STATS +
3231 		       adapter->req_rx_queues * NUM_RX_STATS;
3232 	case ETH_SS_PRIV_FLAGS:
3233 		return ARRAY_SIZE(ibmvnic_priv_flags);
3234 	default:
3235 		return -EOPNOTSUPP;
3236 	}
3237 }
3238 
3239 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
3240 				      struct ethtool_stats *stats, u64 *data)
3241 {
3242 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3243 	union ibmvnic_crq crq;
3244 	int i, j;
3245 	int rc;
3246 
3247 	memset(&crq, 0, sizeof(crq));
3248 	crq.request_statistics.first = IBMVNIC_CRQ_CMD;
3249 	crq.request_statistics.cmd = REQUEST_STATISTICS;
3250 	crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
3251 	crq.request_statistics.len =
3252 	    cpu_to_be32(sizeof(struct ibmvnic_statistics));
3253 
3254 	/* Wait for data to be written */
3255 	reinit_completion(&adapter->stats_done);
3256 	rc = ibmvnic_send_crq(adapter, &crq);
3257 	if (rc)
3258 		return;
3259 	rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
3260 	if (rc)
3261 		return;
3262 
3263 	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
3264 		data[i] = be64_to_cpu(IBMVNIC_GET_STAT
3265 				      (adapter, ibmvnic_stats[i].offset));
3266 
3267 	for (j = 0; j < adapter->req_tx_queues; j++) {
3268 		data[i] = adapter->tx_stats_buffers[j].packets;
3269 		i++;
3270 		data[i] = adapter->tx_stats_buffers[j].bytes;
3271 		i++;
3272 		data[i] = adapter->tx_stats_buffers[j].dropped_packets;
3273 		i++;
3274 	}
3275 
3276 	for (j = 0; j < adapter->req_rx_queues; j++) {
3277 		data[i] = adapter->rx_stats_buffers[j].packets;
3278 		i++;
3279 		data[i] = adapter->rx_stats_buffers[j].bytes;
3280 		i++;
3281 		data[i] = adapter->rx_stats_buffers[j].interrupts;
3282 		i++;
3283 	}
3284 }
3285 
3286 static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
3287 {
3288 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3289 
3290 	return adapter->priv_flags;
3291 }
3292 
3293 static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
3294 {
3295 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3296 	bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
3297 
3298 	if (which_maxes)
3299 		adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
3300 	else
3301 		adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
3302 
3303 	return 0;
3304 }
3305 
3306 static const struct ethtool_ops ibmvnic_ethtool_ops = {
3307 	.get_drvinfo		= ibmvnic_get_drvinfo,
3308 	.get_msglevel		= ibmvnic_get_msglevel,
3309 	.set_msglevel		= ibmvnic_set_msglevel,
3310 	.get_link		= ibmvnic_get_link,
3311 	.get_ringparam		= ibmvnic_get_ringparam,
3312 	.set_ringparam		= ibmvnic_set_ringparam,
3313 	.get_channels		= ibmvnic_get_channels,
3314 	.set_channels		= ibmvnic_set_channels,
3315 	.get_strings            = ibmvnic_get_strings,
3316 	.get_sset_count         = ibmvnic_get_sset_count,
3317 	.get_ethtool_stats	= ibmvnic_get_ethtool_stats,
3318 	.get_link_ksettings	= ibmvnic_get_link_ksettings,
3319 	.get_priv_flags		= ibmvnic_get_priv_flags,
3320 	.set_priv_flags		= ibmvnic_set_priv_flags,
3321 };
3322 
3323 /* Routines for managing CRQs/sCRQs  */
3324 
3325 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3326 				   struct ibmvnic_sub_crq_queue *scrq)
3327 {
3328 	int rc;
3329 
3330 	if (!scrq) {
3331 		netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
3332 		return -EINVAL;
3333 	}
3334 
3335 	if (scrq->irq) {
3336 		free_irq(scrq->irq, scrq);
3337 		irq_dispose_mapping(scrq->irq);
3338 		scrq->irq = 0;
3339 	}
3340 
3341 	if (scrq->msgs) {
3342 		memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3343 		atomic_set(&scrq->used, 0);
3344 		scrq->cur = 0;
3345 		scrq->ind_buf.index = 0;
3346 	} else {
3347 		netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3348 		return -EINVAL;
3349 	}
3350 
3351 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3352 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3353 	return rc;
3354 }
3355 
3356 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3357 {
3358 	int i, rc;
3359 
3360 	if (!adapter->tx_scrq || !adapter->rx_scrq)
3361 		return -EINVAL;
3362 
3363 	for (i = 0; i < adapter->req_tx_queues; i++) {
3364 		netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
3365 		rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3366 		if (rc)
3367 			return rc;
3368 	}
3369 
3370 	for (i = 0; i < adapter->req_rx_queues; i++) {
3371 		netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
3372 		rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3373 		if (rc)
3374 			return rc;
3375 	}
3376 
3377 	return rc;
3378 }
3379 
3380 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
3381 				  struct ibmvnic_sub_crq_queue *scrq,
3382 				  bool do_h_free)
3383 {
3384 	struct device *dev = &adapter->vdev->dev;
3385 	long rc;
3386 
3387 	netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3388 
3389 	if (do_h_free) {
3390 		/* Close the sub-crqs */
3391 		do {
3392 			rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3393 						adapter->vdev->unit_address,
3394 						scrq->crq_num);
3395 		} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3396 
3397 		if (rc) {
3398 			netdev_err(adapter->netdev,
3399 				   "Failed to release sub-CRQ %16lx, rc = %ld\n",
3400 				   scrq->crq_num, rc);
3401 		}
3402 	}
3403 
3404 	dma_free_coherent(dev,
3405 			  IBMVNIC_IND_ARR_SZ,
3406 			  scrq->ind_buf.indir_arr,
3407 			  scrq->ind_buf.indir_dma);
3408 
3409 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3410 			 DMA_BIDIRECTIONAL);
3411 	free_pages((unsigned long)scrq->msgs, 2);
3412 	kfree(scrq);
3413 }
3414 
3415 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3416 							*adapter)
3417 {
3418 	struct device *dev = &adapter->vdev->dev;
3419 	struct ibmvnic_sub_crq_queue *scrq;
3420 	int rc;
3421 
3422 	scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
3423 	if (!scrq)
3424 		return NULL;
3425 
3426 	scrq->msgs =
3427 		(union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
3428 	if (!scrq->msgs) {
3429 		dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3430 		goto zero_page_failed;
3431 	}
3432 
3433 	scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3434 					 DMA_BIDIRECTIONAL);
3435 	if (dma_mapping_error(dev, scrq->msg_token)) {
3436 		dev_warn(dev, "Couldn't map crq queue messages page\n");
3437 		goto map_failed;
3438 	}
3439 
3440 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3441 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3442 
3443 	if (rc == H_RESOURCE)
3444 		rc = ibmvnic_reset_crq(adapter);
3445 
3446 	if (rc == H_CLOSED) {
3447 		dev_warn(dev, "Partner adapter not ready, waiting.\n");
3448 	} else if (rc) {
3449 		dev_warn(dev, "Error %d registering sub-crq\n", rc);
3450 		goto reg_failed;
3451 	}
3452 
3453 	scrq->adapter = adapter;
3454 	scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
3455 	scrq->ind_buf.index = 0;
3456 
3457 	scrq->ind_buf.indir_arr =
3458 		dma_alloc_coherent(dev,
3459 				   IBMVNIC_IND_ARR_SZ,
3460 				   &scrq->ind_buf.indir_dma,
3461 				   GFP_KERNEL);
3462 
3463 	if (!scrq->ind_buf.indir_arr)
3464 		goto indir_failed;
3465 
3466 	spin_lock_init(&scrq->lock);
3467 
3468 	netdev_dbg(adapter->netdev,
3469 		   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3470 		   scrq->crq_num, scrq->hw_irq, scrq->irq);
3471 
3472 	return scrq;
3473 
3474 indir_failed:
3475 	do {
3476 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3477 					adapter->vdev->unit_address,
3478 					scrq->crq_num);
3479 	} while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
3480 reg_failed:
3481 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3482 			 DMA_BIDIRECTIONAL);
3483 map_failed:
3484 	free_pages((unsigned long)scrq->msgs, 2);
3485 zero_page_failed:
3486 	kfree(scrq);
3487 
3488 	return NULL;
3489 }
3490 
3491 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
3492 {
3493 	int i;
3494 
3495 	if (adapter->tx_scrq) {
3496 		for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
3497 			if (!adapter->tx_scrq[i])
3498 				continue;
3499 
3500 			netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3501 				   i);
3502 			ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
3503 			if (adapter->tx_scrq[i]->irq) {
3504 				free_irq(adapter->tx_scrq[i]->irq,
3505 					 adapter->tx_scrq[i]);
3506 				irq_dispose_mapping(adapter->tx_scrq[i]->irq);
3507 				adapter->tx_scrq[i]->irq = 0;
3508 			}
3509 
3510 			release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3511 					      do_h_free);
3512 		}
3513 
3514 		kfree(adapter->tx_scrq);
3515 		adapter->tx_scrq = NULL;
3516 		adapter->num_active_tx_scrqs = 0;
3517 	}
3518 
3519 	if (adapter->rx_scrq) {
3520 		for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
3521 			if (!adapter->rx_scrq[i])
3522 				continue;
3523 
3524 			netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3525 				   i);
3526 			if (adapter->rx_scrq[i]->irq) {
3527 				free_irq(adapter->rx_scrq[i]->irq,
3528 					 adapter->rx_scrq[i]);
3529 				irq_dispose_mapping(adapter->rx_scrq[i]->irq);
3530 				adapter->rx_scrq[i]->irq = 0;
3531 			}
3532 
3533 			release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3534 					      do_h_free);
3535 		}
3536 
3537 		kfree(adapter->rx_scrq);
3538 		adapter->rx_scrq = NULL;
3539 		adapter->num_active_rx_scrqs = 0;
3540 	}
3541 }
3542 
3543 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
3544 			    struct ibmvnic_sub_crq_queue *scrq)
3545 {
3546 	struct device *dev = &adapter->vdev->dev;
3547 	unsigned long rc;
3548 
3549 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3550 				H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3551 	if (rc)
3552 		dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
3553 			scrq->hw_irq, rc);
3554 	return rc;
3555 }
3556 
3557 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
3558 			   struct ibmvnic_sub_crq_queue *scrq)
3559 {
3560 	struct device *dev = &adapter->vdev->dev;
3561 	unsigned long rc;
3562 
3563 	if (scrq->hw_irq > 0x100000000ULL) {
3564 		dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
3565 		return 1;
3566 	}
3567 
3568 	if (test_bit(0, &adapter->resetting) &&
3569 	    adapter->reset_reason == VNIC_RESET_MOBILITY) {
3570 		u64 val = (0xff000000) | scrq->hw_irq;
3571 
3572 		rc = plpar_hcall_norets(H_EOI, val);
3573 		/* H_EOI would fail with rc = H_FUNCTION when running
3574 		 * in XIVE mode which is expected, but not an error.
3575 		 */
3576 		if (rc && (rc != H_FUNCTION))
3577 			dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
3578 				val, rc);
3579 	}
3580 
3581 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
3582 				H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
3583 	if (rc)
3584 		dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
3585 			scrq->hw_irq, rc);
3586 	return rc;
3587 }
3588 
3589 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
3590 			       struct ibmvnic_sub_crq_queue *scrq)
3591 {
3592 	struct device *dev = &adapter->vdev->dev;
3593 	struct ibmvnic_tx_pool *tx_pool;
3594 	struct ibmvnic_tx_buff *txbuff;
3595 	struct netdev_queue *txq;
3596 	union sub_crq *next;
3597 	int index;
3598 	int i;
3599 
3600 restart_loop:
3601 	while (pending_scrq(adapter, scrq)) {
3602 		unsigned int pool = scrq->pool_index;
3603 		int num_entries = 0;
3604 		int total_bytes = 0;
3605 		int num_packets = 0;
3606 
3607 		next = ibmvnic_next_scrq(adapter, scrq);
3608 		for (i = 0; i < next->tx_comp.num_comps; i++) {
3609 			index = be32_to_cpu(next->tx_comp.correlators[i]);
3610 			if (index & IBMVNIC_TSO_POOL_MASK) {
3611 				tx_pool = &adapter->tso_pool[pool];
3612 				index &= ~IBMVNIC_TSO_POOL_MASK;
3613 			} else {
3614 				tx_pool = &adapter->tx_pool[pool];
3615 			}
3616 
3617 			txbuff = &tx_pool->tx_buff[index];
3618 			num_packets++;
3619 			num_entries += txbuff->num_entries;
3620 			if (txbuff->skb) {
3621 				total_bytes += txbuff->skb->len;
3622 				if (next->tx_comp.rcs[i]) {
3623 					dev_err(dev, "tx error %x\n",
3624 						next->tx_comp.rcs[i]);
3625 					dev_kfree_skb_irq(txbuff->skb);
3626 				} else {
3627 					dev_consume_skb_irq(txbuff->skb);
3628 				}
3629 				txbuff->skb = NULL;
3630 			} else {
3631 				netdev_warn(adapter->netdev,
3632 					    "TX completion received with NULL socket buffer\n");
3633 			}
3634 			tx_pool->free_map[tx_pool->producer_index] = index;
3635 			tx_pool->producer_index =
3636 				(tx_pool->producer_index + 1) %
3637 					tx_pool->num_buffers;
3638 		}
3639 		/* remove tx_comp scrq*/
3640 		next->tx_comp.first = 0;
3641 
3642 		txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
3643 		netdev_tx_completed_queue(txq, num_packets, total_bytes);
3644 
3645 		if (atomic_sub_return(num_entries, &scrq->used) <=
3646 		    (adapter->req_tx_entries_per_subcrq / 2) &&
3647 		    __netif_subqueue_stopped(adapter->netdev,
3648 					     scrq->pool_index)) {
3649 			netif_wake_subqueue(adapter->netdev, scrq->pool_index);
3650 			netdev_dbg(adapter->netdev, "Started queue %d\n",
3651 				   scrq->pool_index);
3652 		}
3653 	}
3654 
3655 	enable_scrq_irq(adapter, scrq);
3656 
3657 	if (pending_scrq(adapter, scrq)) {
3658 		disable_scrq_irq(adapter, scrq);
3659 		goto restart_loop;
3660 	}
3661 
3662 	return 0;
3663 }
3664 
3665 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
3666 {
3667 	struct ibmvnic_sub_crq_queue *scrq = instance;
3668 	struct ibmvnic_adapter *adapter = scrq->adapter;
3669 
3670 	disable_scrq_irq(adapter, scrq);
3671 	ibmvnic_complete_tx(adapter, scrq);
3672 
3673 	return IRQ_HANDLED;
3674 }
3675 
3676 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
3677 {
3678 	struct ibmvnic_sub_crq_queue *scrq = instance;
3679 	struct ibmvnic_adapter *adapter = scrq->adapter;
3680 
3681 	/* When booting a kdump kernel we can hit pending interrupts
3682 	 * prior to completing driver initialization.
3683 	 */
3684 	if (unlikely(adapter->state != VNIC_OPEN))
3685 		return IRQ_NONE;
3686 
3687 	adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
3688 
3689 	if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
3690 		disable_scrq_irq(adapter, scrq);
3691 		__napi_schedule(&adapter->napi[scrq->scrq_num]);
3692 	}
3693 
3694 	return IRQ_HANDLED;
3695 }
3696 
3697 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
3698 {
3699 	struct device *dev = &adapter->vdev->dev;
3700 	struct ibmvnic_sub_crq_queue *scrq;
3701 	int i = 0, j = 0;
3702 	int rc = 0;
3703 
3704 	for (i = 0; i < adapter->req_tx_queues; i++) {
3705 		netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
3706 			   i);
3707 		scrq = adapter->tx_scrq[i];
3708 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3709 
3710 		if (!scrq->irq) {
3711 			rc = -EINVAL;
3712 			dev_err(dev, "Error mapping irq\n");
3713 			goto req_tx_irq_failed;
3714 		}
3715 
3716 		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
3717 			 adapter->vdev->unit_address, i);
3718 		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
3719 				 0, scrq->name, scrq);
3720 
3721 		if (rc) {
3722 			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
3723 				scrq->irq, rc);
3724 			irq_dispose_mapping(scrq->irq);
3725 			goto req_tx_irq_failed;
3726 		}
3727 	}
3728 
3729 	for (i = 0; i < adapter->req_rx_queues; i++) {
3730 		netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
3731 			   i);
3732 		scrq = adapter->rx_scrq[i];
3733 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
3734 		if (!scrq->irq) {
3735 			rc = -EINVAL;
3736 			dev_err(dev, "Error mapping irq\n");
3737 			goto req_rx_irq_failed;
3738 		}
3739 		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
3740 			 adapter->vdev->unit_address, i);
3741 		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
3742 				 0, scrq->name, scrq);
3743 		if (rc) {
3744 			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
3745 				scrq->irq, rc);
3746 			irq_dispose_mapping(scrq->irq);
3747 			goto req_rx_irq_failed;
3748 		}
3749 	}
3750 	return rc;
3751 
3752 req_rx_irq_failed:
3753 	for (j = 0; j < i; j++) {
3754 		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
3755 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
3756 	}
3757 	i = adapter->req_tx_queues;
3758 req_tx_irq_failed:
3759 	for (j = 0; j < i; j++) {
3760 		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
3761 		irq_dispose_mapping(adapter->tx_scrq[j]->irq);
3762 	}
3763 	release_sub_crqs(adapter, 1);
3764 	return rc;
3765 }
3766 
3767 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
3768 {
3769 	struct device *dev = &adapter->vdev->dev;
3770 	struct ibmvnic_sub_crq_queue **allqueues;
3771 	int registered_queues = 0;
3772 	int total_queues;
3773 	int more = 0;
3774 	int i;
3775 
3776 	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
3777 
3778 	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
3779 	if (!allqueues)
3780 		return -1;
3781 
3782 	for (i = 0; i < total_queues; i++) {
3783 		allqueues[i] = init_sub_crq_queue(adapter);
3784 		if (!allqueues[i]) {
3785 			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
3786 			break;
3787 		}
3788 		registered_queues++;
3789 	}
3790 
3791 	/* Make sure we were able to register the minimum number of queues */
3792 	if (registered_queues <
3793 	    adapter->min_tx_queues + adapter->min_rx_queues) {
3794 		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
3795 		goto tx_failed;
3796 	}
3797 
3798 	/* Distribute the failed allocated queues*/
3799 	for (i = 0; i < total_queues - registered_queues + more ; i++) {
3800 		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3801 		switch (i % 3) {
3802 		case 0:
3803 			if (adapter->req_rx_queues > adapter->min_rx_queues)
3804 				adapter->req_rx_queues--;
3805 			else
3806 				more++;
3807 			break;
3808 		case 1:
3809 			if (adapter->req_tx_queues > adapter->min_tx_queues)
3810 				adapter->req_tx_queues--;
3811 			else
3812 				more++;
3813 			break;
3814 		}
3815 	}
3816 
3817 	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3818 				   sizeof(*adapter->tx_scrq), GFP_KERNEL);
3819 	if (!adapter->tx_scrq)
3820 		goto tx_failed;
3821 
3822 	for (i = 0; i < adapter->req_tx_queues; i++) {
3823 		adapter->tx_scrq[i] = allqueues[i];
3824 		adapter->tx_scrq[i]->pool_index = i;
3825 		adapter->num_active_tx_scrqs++;
3826 	}
3827 
3828 	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3829 				   sizeof(*adapter->rx_scrq), GFP_KERNEL);
3830 	if (!adapter->rx_scrq)
3831 		goto rx_failed;
3832 
3833 	for (i = 0; i < adapter->req_rx_queues; i++) {
3834 		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3835 		adapter->rx_scrq[i]->scrq_num = i;
3836 		adapter->num_active_rx_scrqs++;
3837 	}
3838 
3839 	kfree(allqueues);
3840 	return 0;
3841 
3842 rx_failed:
3843 	kfree(adapter->tx_scrq);
3844 	adapter->tx_scrq = NULL;
3845 tx_failed:
3846 	for (i = 0; i < registered_queues; i++)
3847 		release_sub_crq_queue(adapter, allqueues[i], 1);
3848 	kfree(allqueues);
3849 	return -1;
3850 }
3851 
3852 static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
3853 {
3854 	struct device *dev = &adapter->vdev->dev;
3855 	union ibmvnic_crq crq;
3856 	int max_entries;
3857 
3858 	if (!retry) {
3859 		/* Sub-CRQ entries are 32 byte long */
3860 		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3861 
3862 		if (adapter->min_tx_entries_per_subcrq > entries_page ||
3863 		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
3864 			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3865 			return;
3866 		}
3867 
3868 		if (adapter->desired.mtu)
3869 			adapter->req_mtu = adapter->desired.mtu;
3870 		else
3871 			adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
3872 
3873 		if (!adapter->desired.tx_entries)
3874 			adapter->desired.tx_entries =
3875 					adapter->max_tx_entries_per_subcrq;
3876 		if (!adapter->desired.rx_entries)
3877 			adapter->desired.rx_entries =
3878 					adapter->max_rx_add_entries_per_subcrq;
3879 
3880 		max_entries = IBMVNIC_MAX_LTB_SIZE /
3881 			      (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3882 
3883 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3884 			adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3885 			adapter->desired.tx_entries = max_entries;
3886 		}
3887 
3888 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3889 			adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3890 			adapter->desired.rx_entries = max_entries;
3891 		}
3892 
3893 		if (adapter->desired.tx_entries)
3894 			adapter->req_tx_entries_per_subcrq =
3895 					adapter->desired.tx_entries;
3896 		else
3897 			adapter->req_tx_entries_per_subcrq =
3898 					adapter->max_tx_entries_per_subcrq;
3899 
3900 		if (adapter->desired.rx_entries)
3901 			adapter->req_rx_add_entries_per_subcrq =
3902 					adapter->desired.rx_entries;
3903 		else
3904 			adapter->req_rx_add_entries_per_subcrq =
3905 					adapter->max_rx_add_entries_per_subcrq;
3906 
3907 		if (adapter->desired.tx_queues)
3908 			adapter->req_tx_queues =
3909 					adapter->desired.tx_queues;
3910 		else
3911 			adapter->req_tx_queues =
3912 					adapter->opt_tx_comp_sub_queues;
3913 
3914 		if (adapter->desired.rx_queues)
3915 			adapter->req_rx_queues =
3916 					adapter->desired.rx_queues;
3917 		else
3918 			adapter->req_rx_queues =
3919 					adapter->opt_rx_comp_queues;
3920 
3921 		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3922 	}
3923 
3924 	memset(&crq, 0, sizeof(crq));
3925 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
3926 	crq.request_capability.cmd = REQUEST_CAPABILITY;
3927 
3928 	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3929 	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3930 	atomic_inc(&adapter->running_cap_crqs);
3931 	ibmvnic_send_crq(adapter, &crq);
3932 
3933 	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3934 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3935 	atomic_inc(&adapter->running_cap_crqs);
3936 	ibmvnic_send_crq(adapter, &crq);
3937 
3938 	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3939 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3940 	atomic_inc(&adapter->running_cap_crqs);
3941 	ibmvnic_send_crq(adapter, &crq);
3942 
3943 	crq.request_capability.capability =
3944 	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3945 	crq.request_capability.number =
3946 	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3947 	atomic_inc(&adapter->running_cap_crqs);
3948 	ibmvnic_send_crq(adapter, &crq);
3949 
3950 	crq.request_capability.capability =
3951 	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3952 	crq.request_capability.number =
3953 	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3954 	atomic_inc(&adapter->running_cap_crqs);
3955 	ibmvnic_send_crq(adapter, &crq);
3956 
3957 	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3958 	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3959 	atomic_inc(&adapter->running_cap_crqs);
3960 	ibmvnic_send_crq(adapter, &crq);
3961 
3962 	if (adapter->netdev->flags & IFF_PROMISC) {
3963 		if (adapter->promisc_supported) {
3964 			crq.request_capability.capability =
3965 			    cpu_to_be16(PROMISC_REQUESTED);
3966 			crq.request_capability.number = cpu_to_be64(1);
3967 			atomic_inc(&adapter->running_cap_crqs);
3968 			ibmvnic_send_crq(adapter, &crq);
3969 		}
3970 	} else {
3971 		crq.request_capability.capability =
3972 		    cpu_to_be16(PROMISC_REQUESTED);
3973 		crq.request_capability.number = cpu_to_be64(0);
3974 		atomic_inc(&adapter->running_cap_crqs);
3975 		ibmvnic_send_crq(adapter, &crq);
3976 	}
3977 }
3978 
3979 static int pending_scrq(struct ibmvnic_adapter *adapter,
3980 			struct ibmvnic_sub_crq_queue *scrq)
3981 {
3982 	union sub_crq *entry = &scrq->msgs[scrq->cur];
3983 	int rc;
3984 
3985 	rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
3986 
3987 	/* Ensure that the SCRQ valid flag is loaded prior to loading the
3988 	 * contents of the SCRQ descriptor
3989 	 */
3990 	dma_rmb();
3991 
3992 	return rc;
3993 }
3994 
3995 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3996 					struct ibmvnic_sub_crq_queue *scrq)
3997 {
3998 	union sub_crq *entry;
3999 	unsigned long flags;
4000 
4001 	spin_lock_irqsave(&scrq->lock, flags);
4002 	entry = &scrq->msgs[scrq->cur];
4003 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4004 		if (++scrq->cur == scrq->size)
4005 			scrq->cur = 0;
4006 	} else {
4007 		entry = NULL;
4008 	}
4009 	spin_unlock_irqrestore(&scrq->lock, flags);
4010 
4011 	/* Ensure that the SCRQ valid flag is loaded prior to loading the
4012 	 * contents of the SCRQ descriptor
4013 	 */
4014 	dma_rmb();
4015 
4016 	return entry;
4017 }
4018 
4019 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
4020 {
4021 	struct ibmvnic_crq_queue *queue = &adapter->crq;
4022 	union ibmvnic_crq *crq;
4023 
4024 	crq = &queue->msgs[queue->cur];
4025 	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4026 		if (++queue->cur == queue->size)
4027 			queue->cur = 0;
4028 	} else {
4029 		crq = NULL;
4030 	}
4031 
4032 	return crq;
4033 }
4034 
4035 static void print_subcrq_error(struct device *dev, int rc, const char *func)
4036 {
4037 	switch (rc) {
4038 	case H_PARAMETER:
4039 		dev_warn_ratelimited(dev,
4040 				     "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
4041 				     func, rc);
4042 		break;
4043 	case H_CLOSED:
4044 		dev_warn_ratelimited(dev,
4045 				     "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
4046 				     func, rc);
4047 		break;
4048 	default:
4049 		dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
4050 		break;
4051 	}
4052 }
4053 
4054 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
4055 				u64 remote_handle, u64 ioba, u64 num_entries)
4056 {
4057 	unsigned int ua = adapter->vdev->unit_address;
4058 	struct device *dev = &adapter->vdev->dev;
4059 	int rc;
4060 
4061 	/* Make sure the hypervisor sees the complete request */
4062 	dma_wmb();
4063 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
4064 				cpu_to_be64(remote_handle),
4065 				ioba, num_entries);
4066 
4067 	if (rc)
4068 		print_subcrq_error(dev, rc, __func__);
4069 
4070 	return rc;
4071 }
4072 
4073 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
4074 			    union ibmvnic_crq *crq)
4075 {
4076 	unsigned int ua = adapter->vdev->unit_address;
4077 	struct device *dev = &adapter->vdev->dev;
4078 	u64 *u64_crq = (u64 *)crq;
4079 	int rc;
4080 
4081 	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
4082 		   (unsigned long)cpu_to_be64(u64_crq[0]),
4083 		   (unsigned long)cpu_to_be64(u64_crq[1]));
4084 
4085 	if (!adapter->crq.active &&
4086 	    crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
4087 		dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
4088 		return -EINVAL;
4089 	}
4090 
4091 	/* Make sure the hypervisor sees the complete request */
4092 	dma_wmb();
4093 
4094 	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
4095 				cpu_to_be64(u64_crq[0]),
4096 				cpu_to_be64(u64_crq[1]));
4097 
4098 	if (rc) {
4099 		if (rc == H_CLOSED) {
4100 			dev_warn(dev, "CRQ Queue closed\n");
4101 			/* do not reset, report the fail, wait for passive init from server */
4102 		}
4103 
4104 		dev_warn(dev, "Send error (rc=%d)\n", rc);
4105 	}
4106 
4107 	return rc;
4108 }
4109 
4110 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
4111 {
4112 	struct device *dev = &adapter->vdev->dev;
4113 	union ibmvnic_crq crq;
4114 	int retries = 100;
4115 	int rc;
4116 
4117 	memset(&crq, 0, sizeof(crq));
4118 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
4119 	crq.generic.cmd = IBMVNIC_CRQ_INIT;
4120 	netdev_dbg(adapter->netdev, "Sending CRQ init\n");
4121 
4122 	do {
4123 		rc = ibmvnic_send_crq(adapter, &crq);
4124 		if (rc != H_CLOSED)
4125 			break;
4126 		retries--;
4127 		msleep(50);
4128 
4129 	} while (retries > 0);
4130 
4131 	if (rc) {
4132 		dev_err(dev, "Failed to send init request, rc = %d\n", rc);
4133 		return rc;
4134 	}
4135 
4136 	return 0;
4137 }
4138 
4139 struct vnic_login_client_data {
4140 	u8	type;
4141 	__be16	len;
4142 	char	name[];
4143 } __packed;
4144 
4145 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
4146 {
4147 	int len;
4148 
4149 	/* Calculate the amount of buffer space needed for the
4150 	 * vnic client data in the login buffer. There are four entries,
4151 	 * OS name, LPAR name, device name, and a null last entry.
4152 	 */
4153 	len = 4 * sizeof(struct vnic_login_client_data);
4154 	len += 6; /* "Linux" plus NULL */
4155 	len += strlen(utsname()->nodename) + 1;
4156 	len += strlen(adapter->netdev->name) + 1;
4157 
4158 	return len;
4159 }
4160 
4161 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
4162 				 struct vnic_login_client_data *vlcd)
4163 {
4164 	const char *os_name = "Linux";
4165 	int len;
4166 
4167 	/* Type 1 - LPAR OS */
4168 	vlcd->type = 1;
4169 	len = strlen(os_name) + 1;
4170 	vlcd->len = cpu_to_be16(len);
4171 	strscpy(vlcd->name, os_name, len);
4172 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
4173 
4174 	/* Type 2 - LPAR name */
4175 	vlcd->type = 2;
4176 	len = strlen(utsname()->nodename) + 1;
4177 	vlcd->len = cpu_to_be16(len);
4178 	strscpy(vlcd->name, utsname()->nodename, len);
4179 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
4180 
4181 	/* Type 3 - device name */
4182 	vlcd->type = 3;
4183 	len = strlen(adapter->netdev->name) + 1;
4184 	vlcd->len = cpu_to_be16(len);
4185 	strscpy(vlcd->name, adapter->netdev->name, len);
4186 }
4187 
4188 static int send_login(struct ibmvnic_adapter *adapter)
4189 {
4190 	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
4191 	struct ibmvnic_login_buffer *login_buffer;
4192 	struct device *dev = &adapter->vdev->dev;
4193 	struct vnic_login_client_data *vlcd;
4194 	dma_addr_t rsp_buffer_token;
4195 	dma_addr_t buffer_token;
4196 	size_t rsp_buffer_size;
4197 	union ibmvnic_crq crq;
4198 	int client_data_len;
4199 	size_t buffer_size;
4200 	__be64 *tx_list_p;
4201 	__be64 *rx_list_p;
4202 	int rc;
4203 	int i;
4204 
4205 	if (!adapter->tx_scrq || !adapter->rx_scrq) {
4206 		netdev_err(adapter->netdev,
4207 			   "RX or TX queues are not allocated, device login failed\n");
4208 		return -1;
4209 	}
4210 
4211 	release_login_buffer(adapter);
4212 	release_login_rsp_buffer(adapter);
4213 
4214 	client_data_len = vnic_client_data_len(adapter);
4215 
4216 	buffer_size =
4217 	    sizeof(struct ibmvnic_login_buffer) +
4218 	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
4219 	    client_data_len;
4220 
4221 	login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
4222 	if (!login_buffer)
4223 		goto buf_alloc_failed;
4224 
4225 	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
4226 				      DMA_TO_DEVICE);
4227 	if (dma_mapping_error(dev, buffer_token)) {
4228 		dev_err(dev, "Couldn't map login buffer\n");
4229 		goto buf_map_failed;
4230 	}
4231 
4232 	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
4233 			  sizeof(u64) * adapter->req_tx_queues +
4234 			  sizeof(u64) * adapter->req_rx_queues +
4235 			  sizeof(u64) * adapter->req_rx_queues +
4236 			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
4237 
4238 	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
4239 	if (!login_rsp_buffer)
4240 		goto buf_rsp_alloc_failed;
4241 
4242 	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
4243 					  rsp_buffer_size, DMA_FROM_DEVICE);
4244 	if (dma_mapping_error(dev, rsp_buffer_token)) {
4245 		dev_err(dev, "Couldn't map login rsp buffer\n");
4246 		goto buf_rsp_map_failed;
4247 	}
4248 
4249 	adapter->login_buf = login_buffer;
4250 	adapter->login_buf_token = buffer_token;
4251 	adapter->login_buf_sz = buffer_size;
4252 	adapter->login_rsp_buf = login_rsp_buffer;
4253 	adapter->login_rsp_buf_token = rsp_buffer_token;
4254 	adapter->login_rsp_buf_sz = rsp_buffer_size;
4255 
4256 	login_buffer->len = cpu_to_be32(buffer_size);
4257 	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
4258 	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
4259 	login_buffer->off_txcomp_subcrqs =
4260 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
4261 	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
4262 	login_buffer->off_rxcomp_subcrqs =
4263 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
4264 			sizeof(u64) * adapter->req_tx_queues);
4265 	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
4266 	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
4267 
4268 	tx_list_p = (__be64 *)((char *)login_buffer +
4269 				      sizeof(struct ibmvnic_login_buffer));
4270 	rx_list_p = (__be64 *)((char *)login_buffer +
4271 				      sizeof(struct ibmvnic_login_buffer) +
4272 				      sizeof(u64) * adapter->req_tx_queues);
4273 
4274 	for (i = 0; i < adapter->req_tx_queues; i++) {
4275 		if (adapter->tx_scrq[i]) {
4276 			tx_list_p[i] =
4277 				cpu_to_be64(adapter->tx_scrq[i]->crq_num);
4278 		}
4279 	}
4280 
4281 	for (i = 0; i < adapter->req_rx_queues; i++) {
4282 		if (adapter->rx_scrq[i]) {
4283 			rx_list_p[i] =
4284 				cpu_to_be64(adapter->rx_scrq[i]->crq_num);
4285 		}
4286 	}
4287 
4288 	/* Insert vNIC login client data */
4289 	vlcd = (struct vnic_login_client_data *)
4290 		((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
4291 	login_buffer->client_data_offset =
4292 			cpu_to_be32((char *)vlcd - (char *)login_buffer);
4293 	login_buffer->client_data_len = cpu_to_be32(client_data_len);
4294 
4295 	vnic_add_client_data(adapter, vlcd);
4296 
4297 	netdev_dbg(adapter->netdev, "Login Buffer:\n");
4298 	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
4299 		netdev_dbg(adapter->netdev, "%016lx\n",
4300 			   ((unsigned long *)(adapter->login_buf))[i]);
4301 	}
4302 
4303 	memset(&crq, 0, sizeof(crq));
4304 	crq.login.first = IBMVNIC_CRQ_CMD;
4305 	crq.login.cmd = LOGIN;
4306 	crq.login.ioba = cpu_to_be32(buffer_token);
4307 	crq.login.len = cpu_to_be32(buffer_size);
4308 
4309 	adapter->login_pending = true;
4310 	rc = ibmvnic_send_crq(adapter, &crq);
4311 	if (rc) {
4312 		adapter->login_pending = false;
4313 		netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4314 		goto buf_rsp_map_failed;
4315 	}
4316 
4317 	return 0;
4318 
4319 buf_rsp_map_failed:
4320 	kfree(login_rsp_buffer);
4321 	adapter->login_rsp_buf = NULL;
4322 buf_rsp_alloc_failed:
4323 	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4324 buf_map_failed:
4325 	kfree(login_buffer);
4326 	adapter->login_buf = NULL;
4327 buf_alloc_failed:
4328 	return -1;
4329 }
4330 
4331 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4332 			    u32 len, u8 map_id)
4333 {
4334 	union ibmvnic_crq crq;
4335 
4336 	memset(&crq, 0, sizeof(crq));
4337 	crq.request_map.first = IBMVNIC_CRQ_CMD;
4338 	crq.request_map.cmd = REQUEST_MAP;
4339 	crq.request_map.map_id = map_id;
4340 	crq.request_map.ioba = cpu_to_be32(addr);
4341 	crq.request_map.len = cpu_to_be32(len);
4342 	return ibmvnic_send_crq(adapter, &crq);
4343 }
4344 
4345 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
4346 {
4347 	union ibmvnic_crq crq;
4348 
4349 	memset(&crq, 0, sizeof(crq));
4350 	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4351 	crq.request_unmap.cmd = REQUEST_UNMAP;
4352 	crq.request_unmap.map_id = map_id;
4353 	return ibmvnic_send_crq(adapter, &crq);
4354 }
4355 
4356 static void send_query_map(struct ibmvnic_adapter *adapter)
4357 {
4358 	union ibmvnic_crq crq;
4359 
4360 	memset(&crq, 0, sizeof(crq));
4361 	crq.query_map.first = IBMVNIC_CRQ_CMD;
4362 	crq.query_map.cmd = QUERY_MAP;
4363 	ibmvnic_send_crq(adapter, &crq);
4364 }
4365 
4366 /* Send a series of CRQs requesting various capabilities of the VNIC server */
4367 static void send_query_cap(struct ibmvnic_adapter *adapter)
4368 {
4369 	union ibmvnic_crq crq;
4370 
4371 	atomic_set(&adapter->running_cap_crqs, 0);
4372 	memset(&crq, 0, sizeof(crq));
4373 	crq.query_capability.first = IBMVNIC_CRQ_CMD;
4374 	crq.query_capability.cmd = QUERY_CAPABILITY;
4375 
4376 	crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
4377 	atomic_inc(&adapter->running_cap_crqs);
4378 	ibmvnic_send_crq(adapter, &crq);
4379 
4380 	crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
4381 	atomic_inc(&adapter->running_cap_crqs);
4382 	ibmvnic_send_crq(adapter, &crq);
4383 
4384 	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
4385 	atomic_inc(&adapter->running_cap_crqs);
4386 	ibmvnic_send_crq(adapter, &crq);
4387 
4388 	crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
4389 	atomic_inc(&adapter->running_cap_crqs);
4390 	ibmvnic_send_crq(adapter, &crq);
4391 
4392 	crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
4393 	atomic_inc(&adapter->running_cap_crqs);
4394 	ibmvnic_send_crq(adapter, &crq);
4395 
4396 	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
4397 	atomic_inc(&adapter->running_cap_crqs);
4398 	ibmvnic_send_crq(adapter, &crq);
4399 
4400 	crq.query_capability.capability =
4401 	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
4402 	atomic_inc(&adapter->running_cap_crqs);
4403 	ibmvnic_send_crq(adapter, &crq);
4404 
4405 	crq.query_capability.capability =
4406 	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
4407 	atomic_inc(&adapter->running_cap_crqs);
4408 	ibmvnic_send_crq(adapter, &crq);
4409 
4410 	crq.query_capability.capability =
4411 	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
4412 	atomic_inc(&adapter->running_cap_crqs);
4413 	ibmvnic_send_crq(adapter, &crq);
4414 
4415 	crq.query_capability.capability =
4416 	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
4417 	atomic_inc(&adapter->running_cap_crqs);
4418 	ibmvnic_send_crq(adapter, &crq);
4419 
4420 	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
4421 	atomic_inc(&adapter->running_cap_crqs);
4422 	ibmvnic_send_crq(adapter, &crq);
4423 
4424 	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
4425 	atomic_inc(&adapter->running_cap_crqs);
4426 	ibmvnic_send_crq(adapter, &crq);
4427 
4428 	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
4429 	atomic_inc(&adapter->running_cap_crqs);
4430 	ibmvnic_send_crq(adapter, &crq);
4431 
4432 	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
4433 	atomic_inc(&adapter->running_cap_crqs);
4434 	ibmvnic_send_crq(adapter, &crq);
4435 
4436 	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
4437 	atomic_inc(&adapter->running_cap_crqs);
4438 	ibmvnic_send_crq(adapter, &crq);
4439 
4440 	crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
4441 	atomic_inc(&adapter->running_cap_crqs);
4442 	ibmvnic_send_crq(adapter, &crq);
4443 
4444 	crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4445 	atomic_inc(&adapter->running_cap_crqs);
4446 	ibmvnic_send_crq(adapter, &crq);
4447 
4448 	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
4449 	atomic_inc(&adapter->running_cap_crqs);
4450 	ibmvnic_send_crq(adapter, &crq);
4451 
4452 	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
4453 	atomic_inc(&adapter->running_cap_crqs);
4454 	ibmvnic_send_crq(adapter, &crq);
4455 
4456 	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
4457 	atomic_inc(&adapter->running_cap_crqs);
4458 	ibmvnic_send_crq(adapter, &crq);
4459 
4460 	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
4461 	atomic_inc(&adapter->running_cap_crqs);
4462 	ibmvnic_send_crq(adapter, &crq);
4463 
4464 	crq.query_capability.capability =
4465 			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
4466 	atomic_inc(&adapter->running_cap_crqs);
4467 	ibmvnic_send_crq(adapter, &crq);
4468 
4469 	crq.query_capability.capability =
4470 			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
4471 	atomic_inc(&adapter->running_cap_crqs);
4472 	ibmvnic_send_crq(adapter, &crq);
4473 
4474 	crq.query_capability.capability =
4475 			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
4476 	atomic_inc(&adapter->running_cap_crqs);
4477 	ibmvnic_send_crq(adapter, &crq);
4478 
4479 	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
4480 	atomic_inc(&adapter->running_cap_crqs);
4481 	ibmvnic_send_crq(adapter, &crq);
4482 }
4483 
4484 static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
4485 {
4486 	int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4487 	struct device *dev = &adapter->vdev->dev;
4488 	union ibmvnic_crq crq;
4489 
4490 	adapter->ip_offload_tok =
4491 		dma_map_single(dev,
4492 			       &adapter->ip_offload_buf,
4493 			       buf_sz,
4494 			       DMA_FROM_DEVICE);
4495 
4496 	if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4497 		if (!firmware_has_feature(FW_FEATURE_CMO))
4498 			dev_err(dev, "Couldn't map offload buffer\n");
4499 		return;
4500 	}
4501 
4502 	memset(&crq, 0, sizeof(crq));
4503 	crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4504 	crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4505 	crq.query_ip_offload.len = cpu_to_be32(buf_sz);
4506 	crq.query_ip_offload.ioba =
4507 	    cpu_to_be32(adapter->ip_offload_tok);
4508 
4509 	ibmvnic_send_crq(adapter, &crq);
4510 }
4511 
4512 static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
4513 {
4514 	struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
4515 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4516 	struct device *dev = &adapter->vdev->dev;
4517 	netdev_features_t old_hw_features = 0;
4518 	union ibmvnic_crq crq;
4519 
4520 	adapter->ip_offload_ctrl_tok =
4521 		dma_map_single(dev,
4522 			       ctrl_buf,
4523 			       sizeof(adapter->ip_offload_ctrl),
4524 			       DMA_TO_DEVICE);
4525 
4526 	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
4527 		dev_err(dev, "Couldn't map ip offload control buffer\n");
4528 		return;
4529 	}
4530 
4531 	ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4532 	ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
4533 	ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
4534 	ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
4535 	ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
4536 	ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
4537 	ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
4538 	ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
4539 	ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
4540 	ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
4541 
4542 	/* large_rx disabled for now, additional features needed */
4543 	ctrl_buf->large_rx_ipv4 = 0;
4544 	ctrl_buf->large_rx_ipv6 = 0;
4545 
4546 	if (adapter->state != VNIC_PROBING) {
4547 		old_hw_features = adapter->netdev->hw_features;
4548 		adapter->netdev->hw_features = 0;
4549 	}
4550 
4551 	adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
4552 
4553 	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
4554 		adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
4555 
4556 	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
4557 		adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
4558 
4559 	if ((adapter->netdev->features &
4560 	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
4561 		adapter->netdev->hw_features |= NETIF_F_RXCSUM;
4562 
4563 	if (buf->large_tx_ipv4)
4564 		adapter->netdev->hw_features |= NETIF_F_TSO;
4565 	if (buf->large_tx_ipv6)
4566 		adapter->netdev->hw_features |= NETIF_F_TSO6;
4567 
4568 	if (adapter->state == VNIC_PROBING) {
4569 		adapter->netdev->features |= adapter->netdev->hw_features;
4570 	} else if (old_hw_features != adapter->netdev->hw_features) {
4571 		netdev_features_t tmp = 0;
4572 
4573 		/* disable features no longer supported */
4574 		adapter->netdev->features &= adapter->netdev->hw_features;
4575 		/* turn on features now supported if previously enabled */
4576 		tmp = (old_hw_features ^ adapter->netdev->hw_features) &
4577 			adapter->netdev->hw_features;
4578 		adapter->netdev->features |=
4579 				tmp & adapter->netdev->wanted_features;
4580 	}
4581 
4582 	memset(&crq, 0, sizeof(crq));
4583 	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
4584 	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
4585 	crq.control_ip_offload.len =
4586 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
4587 	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
4588 	ibmvnic_send_crq(adapter, &crq);
4589 }
4590 
4591 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
4592 				struct ibmvnic_adapter *adapter)
4593 {
4594 	struct device *dev = &adapter->vdev->dev;
4595 
4596 	if (crq->get_vpd_size_rsp.rc.code) {
4597 		dev_err(dev, "Error retrieving VPD size, rc=%x\n",
4598 			crq->get_vpd_size_rsp.rc.code);
4599 		complete(&adapter->fw_done);
4600 		return;
4601 	}
4602 
4603 	adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
4604 	complete(&adapter->fw_done);
4605 }
4606 
4607 static void handle_vpd_rsp(union ibmvnic_crq *crq,
4608 			   struct ibmvnic_adapter *adapter)
4609 {
4610 	struct device *dev = &adapter->vdev->dev;
4611 	unsigned char *substr = NULL;
4612 	u8 fw_level_len = 0;
4613 
4614 	memset(adapter->fw_version, 0, 32);
4615 
4616 	dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
4617 			 DMA_FROM_DEVICE);
4618 
4619 	if (crq->get_vpd_rsp.rc.code) {
4620 		dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
4621 			crq->get_vpd_rsp.rc.code);
4622 		goto complete;
4623 	}
4624 
4625 	/* get the position of the firmware version info
4626 	 * located after the ASCII 'RM' substring in the buffer
4627 	 */
4628 	substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
4629 	if (!substr) {
4630 		dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
4631 		goto complete;
4632 	}
4633 
4634 	/* get length of firmware level ASCII substring */
4635 	if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
4636 		fw_level_len = *(substr + 2);
4637 	} else {
4638 		dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
4639 		goto complete;
4640 	}
4641 
4642 	/* copy firmware version string from vpd into adapter */
4643 	if ((substr + 3 + fw_level_len) <
4644 	    (adapter->vpd->buff + adapter->vpd->len)) {
4645 		strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
4646 	} else {
4647 		dev_info(dev, "FW substr extrapolated VPD buff\n");
4648 	}
4649 
4650 complete:
4651 	if (adapter->fw_version[0] == '\0')
4652 		strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version));
4653 	complete(&adapter->fw_done);
4654 }
4655 
4656 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
4657 {
4658 	struct device *dev = &adapter->vdev->dev;
4659 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
4660 	int i;
4661 
4662 	dma_unmap_single(dev, adapter->ip_offload_tok,
4663 			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
4664 
4665 	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
4666 	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
4667 		netdev_dbg(adapter->netdev, "%016lx\n",
4668 			   ((unsigned long *)(buf))[i]);
4669 
4670 	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
4671 	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
4672 	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
4673 		   buf->tcp_ipv4_chksum);
4674 	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
4675 		   buf->tcp_ipv6_chksum);
4676 	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
4677 		   buf->udp_ipv4_chksum);
4678 	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
4679 		   buf->udp_ipv6_chksum);
4680 	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
4681 		   buf->large_tx_ipv4);
4682 	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
4683 		   buf->large_tx_ipv6);
4684 	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
4685 		   buf->large_rx_ipv4);
4686 	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
4687 		   buf->large_rx_ipv6);
4688 	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
4689 		   buf->max_ipv4_header_size);
4690 	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
4691 		   buf->max_ipv6_header_size);
4692 	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
4693 		   buf->max_tcp_header_size);
4694 	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
4695 		   buf->max_udp_header_size);
4696 	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
4697 		   buf->max_large_tx_size);
4698 	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
4699 		   buf->max_large_rx_size);
4700 	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
4701 		   buf->ipv6_extension_header);
4702 	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
4703 		   buf->tcp_pseudosum_req);
4704 	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
4705 		   buf->num_ipv6_ext_headers);
4706 	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
4707 		   buf->off_ipv6_ext_headers);
4708 
4709 	send_control_ip_offload(adapter);
4710 }
4711 
4712 static const char *ibmvnic_fw_err_cause(u16 cause)
4713 {
4714 	switch (cause) {
4715 	case ADAPTER_PROBLEM:
4716 		return "adapter problem";
4717 	case BUS_PROBLEM:
4718 		return "bus problem";
4719 	case FW_PROBLEM:
4720 		return "firmware problem";
4721 	case DD_PROBLEM:
4722 		return "device driver problem";
4723 	case EEH_RECOVERY:
4724 		return "EEH recovery";
4725 	case FW_UPDATED:
4726 		return "firmware updated";
4727 	case LOW_MEMORY:
4728 		return "low Memory";
4729 	default:
4730 		return "unknown";
4731 	}
4732 }
4733 
4734 static void handle_error_indication(union ibmvnic_crq *crq,
4735 				    struct ibmvnic_adapter *adapter)
4736 {
4737 	struct device *dev = &adapter->vdev->dev;
4738 	u16 cause;
4739 
4740 	cause = be16_to_cpu(crq->error_indication.error_cause);
4741 
4742 	dev_warn_ratelimited(dev,
4743 			     "Firmware reports %serror, cause: %s. Starting recovery...\n",
4744 			     crq->error_indication.flags
4745 				& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
4746 			     ibmvnic_fw_err_cause(cause));
4747 
4748 	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
4749 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4750 	else
4751 		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
4752 }
4753 
4754 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
4755 				 struct ibmvnic_adapter *adapter)
4756 {
4757 	struct net_device *netdev = adapter->netdev;
4758 	struct device *dev = &adapter->vdev->dev;
4759 	long rc;
4760 
4761 	rc = crq->change_mac_addr_rsp.rc.code;
4762 	if (rc) {
4763 		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
4764 		goto out;
4765 	}
4766 	/* crq->change_mac_addr.mac_addr is the requested one
4767 	 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
4768 	 */
4769 	eth_hw_addr_set(netdev, &crq->change_mac_addr_rsp.mac_addr[0]);
4770 	ether_addr_copy(adapter->mac_addr,
4771 			&crq->change_mac_addr_rsp.mac_addr[0]);
4772 out:
4773 	complete(&adapter->fw_done);
4774 	return rc;
4775 }
4776 
4777 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
4778 				   struct ibmvnic_adapter *adapter)
4779 {
4780 	struct device *dev = &adapter->vdev->dev;
4781 	u64 *req_value;
4782 	char *name;
4783 
4784 	atomic_dec(&adapter->running_cap_crqs);
4785 	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
4786 	case REQ_TX_QUEUES:
4787 		req_value = &adapter->req_tx_queues;
4788 		name = "tx";
4789 		break;
4790 	case REQ_RX_QUEUES:
4791 		req_value = &adapter->req_rx_queues;
4792 		name = "rx";
4793 		break;
4794 	case REQ_RX_ADD_QUEUES:
4795 		req_value = &adapter->req_rx_add_queues;
4796 		name = "rx_add";
4797 		break;
4798 	case REQ_TX_ENTRIES_PER_SUBCRQ:
4799 		req_value = &adapter->req_tx_entries_per_subcrq;
4800 		name = "tx_entries_per_subcrq";
4801 		break;
4802 	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
4803 		req_value = &adapter->req_rx_add_entries_per_subcrq;
4804 		name = "rx_add_entries_per_subcrq";
4805 		break;
4806 	case REQ_MTU:
4807 		req_value = &adapter->req_mtu;
4808 		name = "mtu";
4809 		break;
4810 	case PROMISC_REQUESTED:
4811 		req_value = &adapter->promisc;
4812 		name = "promisc";
4813 		break;
4814 	default:
4815 		dev_err(dev, "Got invalid cap request rsp %d\n",
4816 			crq->request_capability.capability);
4817 		return;
4818 	}
4819 
4820 	switch (crq->request_capability_rsp.rc.code) {
4821 	case SUCCESS:
4822 		break;
4823 	case PARTIALSUCCESS:
4824 		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
4825 			 *req_value,
4826 			 (long)be64_to_cpu(crq->request_capability_rsp.number),
4827 			 name);
4828 
4829 		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4830 		    REQ_MTU) {
4831 			pr_err("mtu of %llu is not supported. Reverting.\n",
4832 			       *req_value);
4833 			*req_value = adapter->fallback.mtu;
4834 		} else {
4835 			*req_value =
4836 				be64_to_cpu(crq->request_capability_rsp.number);
4837 		}
4838 
4839 		send_request_cap(adapter, 1);
4840 		return;
4841 	default:
4842 		dev_err(dev, "Error %d in request cap rsp\n",
4843 			crq->request_capability_rsp.rc.code);
4844 		return;
4845 	}
4846 
4847 	/* Done receiving requested capabilities, query IP offload support */
4848 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4849 		adapter->wait_capability = false;
4850 		send_query_ip_offload(adapter);
4851 	}
4852 }
4853 
4854 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4855 			    struct ibmvnic_adapter *adapter)
4856 {
4857 	struct device *dev = &adapter->vdev->dev;
4858 	struct net_device *netdev = adapter->netdev;
4859 	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4860 	struct ibmvnic_login_buffer *login = adapter->login_buf;
4861 	u64 *tx_handle_array;
4862 	u64 *rx_handle_array;
4863 	int num_tx_pools;
4864 	int num_rx_pools;
4865 	u64 *size_array;
4866 	int i;
4867 
4868 	/* CHECK: Test/set of login_pending does not need to be atomic
4869 	 * because only ibmvnic_tasklet tests/clears this.
4870 	 */
4871 	if (!adapter->login_pending) {
4872 		netdev_warn(netdev, "Ignoring unexpected login response\n");
4873 		return 0;
4874 	}
4875 	adapter->login_pending = false;
4876 
4877 	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
4878 			 DMA_TO_DEVICE);
4879 	dma_unmap_single(dev, adapter->login_rsp_buf_token,
4880 			 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
4881 
4882 	/* If the number of queues requested can't be allocated by the
4883 	 * server, the login response will return with code 1. We will need
4884 	 * to resend the login buffer with fewer queues requested.
4885 	 */
4886 	if (login_rsp_crq->generic.rc.code) {
4887 		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4888 		complete(&adapter->init_done);
4889 		return 0;
4890 	}
4891 
4892 	if (adapter->failover_pending) {
4893 		adapter->init_done_rc = -EAGAIN;
4894 		netdev_dbg(netdev, "Failover pending, ignoring login response\n");
4895 		complete(&adapter->init_done);
4896 		/* login response buffer will be released on reset */
4897 		return 0;
4898 	}
4899 
4900 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4901 
4902 	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4903 	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4904 		netdev_dbg(adapter->netdev, "%016lx\n",
4905 			   ((unsigned long *)(adapter->login_rsp_buf))[i]);
4906 	}
4907 
4908 	/* Sanity checks */
4909 	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4910 	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
4911 	     adapter->req_rx_add_queues !=
4912 	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4913 		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4914 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4915 		return -EIO;
4916 	}
4917 	size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4918 		be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
4919 	/* variable buffer sizes are not supported, so just read the
4920 	 * first entry.
4921 	 */
4922 	adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
4923 
4924 	num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
4925 	num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4926 
4927 	tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4928 				  be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
4929 	rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
4930 				  be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
4931 
4932 	for (i = 0; i < num_tx_pools; i++)
4933 		adapter->tx_scrq[i]->handle = tx_handle_array[i];
4934 
4935 	for (i = 0; i < num_rx_pools; i++)
4936 		adapter->rx_scrq[i]->handle = rx_handle_array[i];
4937 
4938 	adapter->num_active_tx_scrqs = num_tx_pools;
4939 	adapter->num_active_rx_scrqs = num_rx_pools;
4940 	release_login_rsp_buffer(adapter);
4941 	release_login_buffer(adapter);
4942 	complete(&adapter->init_done);
4943 
4944 	return 0;
4945 }
4946 
4947 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4948 				     struct ibmvnic_adapter *adapter)
4949 {
4950 	struct device *dev = &adapter->vdev->dev;
4951 	long rc;
4952 
4953 	rc = crq->request_unmap_rsp.rc.code;
4954 	if (rc)
4955 		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4956 }
4957 
4958 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4959 				 struct ibmvnic_adapter *adapter)
4960 {
4961 	struct net_device *netdev = adapter->netdev;
4962 	struct device *dev = &adapter->vdev->dev;
4963 	long rc;
4964 
4965 	rc = crq->query_map_rsp.rc.code;
4966 	if (rc) {
4967 		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4968 		return;
4969 	}
4970 	netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n",
4971 		   crq->query_map_rsp.page_size,
4972 		   __be32_to_cpu(crq->query_map_rsp.tot_pages),
4973 		   __be32_to_cpu(crq->query_map_rsp.free_pages));
4974 }
4975 
4976 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4977 				 struct ibmvnic_adapter *adapter)
4978 {
4979 	struct net_device *netdev = adapter->netdev;
4980 	struct device *dev = &adapter->vdev->dev;
4981 	long rc;
4982 
4983 	atomic_dec(&adapter->running_cap_crqs);
4984 	netdev_dbg(netdev, "Outstanding queries: %d\n",
4985 		   atomic_read(&adapter->running_cap_crqs));
4986 	rc = crq->query_capability.rc.code;
4987 	if (rc) {
4988 		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4989 		goto out;
4990 	}
4991 
4992 	switch (be16_to_cpu(crq->query_capability.capability)) {
4993 	case MIN_TX_QUEUES:
4994 		adapter->min_tx_queues =
4995 		    be64_to_cpu(crq->query_capability.number);
4996 		netdev_dbg(netdev, "min_tx_queues = %lld\n",
4997 			   adapter->min_tx_queues);
4998 		break;
4999 	case MIN_RX_QUEUES:
5000 		adapter->min_rx_queues =
5001 		    be64_to_cpu(crq->query_capability.number);
5002 		netdev_dbg(netdev, "min_rx_queues = %lld\n",
5003 			   adapter->min_rx_queues);
5004 		break;
5005 	case MIN_RX_ADD_QUEUES:
5006 		adapter->min_rx_add_queues =
5007 		    be64_to_cpu(crq->query_capability.number);
5008 		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
5009 			   adapter->min_rx_add_queues);
5010 		break;
5011 	case MAX_TX_QUEUES:
5012 		adapter->max_tx_queues =
5013 		    be64_to_cpu(crq->query_capability.number);
5014 		netdev_dbg(netdev, "max_tx_queues = %lld\n",
5015 			   adapter->max_tx_queues);
5016 		break;
5017 	case MAX_RX_QUEUES:
5018 		adapter->max_rx_queues =
5019 		    be64_to_cpu(crq->query_capability.number);
5020 		netdev_dbg(netdev, "max_rx_queues = %lld\n",
5021 			   adapter->max_rx_queues);
5022 		break;
5023 	case MAX_RX_ADD_QUEUES:
5024 		adapter->max_rx_add_queues =
5025 		    be64_to_cpu(crq->query_capability.number);
5026 		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
5027 			   adapter->max_rx_add_queues);
5028 		break;
5029 	case MIN_TX_ENTRIES_PER_SUBCRQ:
5030 		adapter->min_tx_entries_per_subcrq =
5031 		    be64_to_cpu(crq->query_capability.number);
5032 		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
5033 			   adapter->min_tx_entries_per_subcrq);
5034 		break;
5035 	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
5036 		adapter->min_rx_add_entries_per_subcrq =
5037 		    be64_to_cpu(crq->query_capability.number);
5038 		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
5039 			   adapter->min_rx_add_entries_per_subcrq);
5040 		break;
5041 	case MAX_TX_ENTRIES_PER_SUBCRQ:
5042 		adapter->max_tx_entries_per_subcrq =
5043 		    be64_to_cpu(crq->query_capability.number);
5044 		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
5045 			   adapter->max_tx_entries_per_subcrq);
5046 		break;
5047 	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
5048 		adapter->max_rx_add_entries_per_subcrq =
5049 		    be64_to_cpu(crq->query_capability.number);
5050 		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
5051 			   adapter->max_rx_add_entries_per_subcrq);
5052 		break;
5053 	case TCP_IP_OFFLOAD:
5054 		adapter->tcp_ip_offload =
5055 		    be64_to_cpu(crq->query_capability.number);
5056 		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
5057 			   adapter->tcp_ip_offload);
5058 		break;
5059 	case PROMISC_SUPPORTED:
5060 		adapter->promisc_supported =
5061 		    be64_to_cpu(crq->query_capability.number);
5062 		netdev_dbg(netdev, "promisc_supported = %lld\n",
5063 			   adapter->promisc_supported);
5064 		break;
5065 	case MIN_MTU:
5066 		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
5067 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5068 		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
5069 		break;
5070 	case MAX_MTU:
5071 		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
5072 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5073 		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
5074 		break;
5075 	case MAX_MULTICAST_FILTERS:
5076 		adapter->max_multicast_filters =
5077 		    be64_to_cpu(crq->query_capability.number);
5078 		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
5079 			   adapter->max_multicast_filters);
5080 		break;
5081 	case VLAN_HEADER_INSERTION:
5082 		adapter->vlan_header_insertion =
5083 		    be64_to_cpu(crq->query_capability.number);
5084 		if (adapter->vlan_header_insertion)
5085 			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
5086 		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
5087 			   adapter->vlan_header_insertion);
5088 		break;
5089 	case RX_VLAN_HEADER_INSERTION:
5090 		adapter->rx_vlan_header_insertion =
5091 		    be64_to_cpu(crq->query_capability.number);
5092 		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
5093 			   adapter->rx_vlan_header_insertion);
5094 		break;
5095 	case MAX_TX_SG_ENTRIES:
5096 		adapter->max_tx_sg_entries =
5097 		    be64_to_cpu(crq->query_capability.number);
5098 		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
5099 			   adapter->max_tx_sg_entries);
5100 		break;
5101 	case RX_SG_SUPPORTED:
5102 		adapter->rx_sg_supported =
5103 		    be64_to_cpu(crq->query_capability.number);
5104 		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
5105 			   adapter->rx_sg_supported);
5106 		break;
5107 	case OPT_TX_COMP_SUB_QUEUES:
5108 		adapter->opt_tx_comp_sub_queues =
5109 		    be64_to_cpu(crq->query_capability.number);
5110 		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
5111 			   adapter->opt_tx_comp_sub_queues);
5112 		break;
5113 	case OPT_RX_COMP_QUEUES:
5114 		adapter->opt_rx_comp_queues =
5115 		    be64_to_cpu(crq->query_capability.number);
5116 		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
5117 			   adapter->opt_rx_comp_queues);
5118 		break;
5119 	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
5120 		adapter->opt_rx_bufadd_q_per_rx_comp_q =
5121 		    be64_to_cpu(crq->query_capability.number);
5122 		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
5123 			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
5124 		break;
5125 	case OPT_TX_ENTRIES_PER_SUBCRQ:
5126 		adapter->opt_tx_entries_per_subcrq =
5127 		    be64_to_cpu(crq->query_capability.number);
5128 		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
5129 			   adapter->opt_tx_entries_per_subcrq);
5130 		break;
5131 	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
5132 		adapter->opt_rxba_entries_per_subcrq =
5133 		    be64_to_cpu(crq->query_capability.number);
5134 		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
5135 			   adapter->opt_rxba_entries_per_subcrq);
5136 		break;
5137 	case TX_RX_DESC_REQ:
5138 		adapter->tx_rx_desc_req = crq->query_capability.number;
5139 		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
5140 			   adapter->tx_rx_desc_req);
5141 		break;
5142 
5143 	default:
5144 		netdev_err(netdev, "Got invalid cap rsp %d\n",
5145 			   crq->query_capability.capability);
5146 	}
5147 
5148 out:
5149 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
5150 		adapter->wait_capability = false;
5151 		send_request_cap(adapter, 0);
5152 	}
5153 }
5154 
5155 static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
5156 {
5157 	union ibmvnic_crq crq;
5158 	int rc;
5159 
5160 	memset(&crq, 0, sizeof(crq));
5161 	crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
5162 	crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
5163 
5164 	mutex_lock(&adapter->fw_lock);
5165 	adapter->fw_done_rc = 0;
5166 	reinit_completion(&adapter->fw_done);
5167 
5168 	rc = ibmvnic_send_crq(adapter, &crq);
5169 	if (rc) {
5170 		mutex_unlock(&adapter->fw_lock);
5171 		return rc;
5172 	}
5173 
5174 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
5175 	if (rc) {
5176 		mutex_unlock(&adapter->fw_lock);
5177 		return rc;
5178 	}
5179 
5180 	mutex_unlock(&adapter->fw_lock);
5181 	return adapter->fw_done_rc ? -EIO : 0;
5182 }
5183 
5184 static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
5185 				       struct ibmvnic_adapter *adapter)
5186 {
5187 	struct net_device *netdev = adapter->netdev;
5188 	int rc;
5189 	__be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
5190 
5191 	rc = crq->query_phys_parms_rsp.rc.code;
5192 	if (rc) {
5193 		netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
5194 		return rc;
5195 	}
5196 	switch (rspeed) {
5197 	case IBMVNIC_10MBPS:
5198 		adapter->speed = SPEED_10;
5199 		break;
5200 	case IBMVNIC_100MBPS:
5201 		adapter->speed = SPEED_100;
5202 		break;
5203 	case IBMVNIC_1GBPS:
5204 		adapter->speed = SPEED_1000;
5205 		break;
5206 	case IBMVNIC_10GBPS:
5207 		adapter->speed = SPEED_10000;
5208 		break;
5209 	case IBMVNIC_25GBPS:
5210 		adapter->speed = SPEED_25000;
5211 		break;
5212 	case IBMVNIC_40GBPS:
5213 		adapter->speed = SPEED_40000;
5214 		break;
5215 	case IBMVNIC_50GBPS:
5216 		adapter->speed = SPEED_50000;
5217 		break;
5218 	case IBMVNIC_100GBPS:
5219 		adapter->speed = SPEED_100000;
5220 		break;
5221 	case IBMVNIC_200GBPS:
5222 		adapter->speed = SPEED_200000;
5223 		break;
5224 	default:
5225 		if (netif_carrier_ok(netdev))
5226 			netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
5227 		adapter->speed = SPEED_UNKNOWN;
5228 	}
5229 	if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
5230 		adapter->duplex = DUPLEX_FULL;
5231 	else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
5232 		adapter->duplex = DUPLEX_HALF;
5233 	else
5234 		adapter->duplex = DUPLEX_UNKNOWN;
5235 
5236 	return rc;
5237 }
5238 
5239 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
5240 			       struct ibmvnic_adapter *adapter)
5241 {
5242 	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
5243 	struct net_device *netdev = adapter->netdev;
5244 	struct device *dev = &adapter->vdev->dev;
5245 	u64 *u64_crq = (u64 *)crq;
5246 	long rc;
5247 
5248 	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
5249 		   (unsigned long)cpu_to_be64(u64_crq[0]),
5250 		   (unsigned long)cpu_to_be64(u64_crq[1]));
5251 	switch (gen_crq->first) {
5252 	case IBMVNIC_CRQ_INIT_RSP:
5253 		switch (gen_crq->cmd) {
5254 		case IBMVNIC_CRQ_INIT:
5255 			dev_info(dev, "Partner initialized\n");
5256 			adapter->from_passive_init = true;
5257 			/* Discard any stale login responses from prev reset.
5258 			 * CHECK: should we clear even on INIT_COMPLETE?
5259 			 */
5260 			adapter->login_pending = false;
5261 
5262 			if (!completion_done(&adapter->init_done)) {
5263 				complete(&adapter->init_done);
5264 				adapter->init_done_rc = -EIO;
5265 			}
5266 
5267 			if (adapter->state == VNIC_DOWN)
5268 				rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT);
5269 			else
5270 				rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5271 
5272 			if (rc && rc != -EBUSY) {
5273 				/* We were unable to schedule the failover
5274 				 * reset either because the adapter was still
5275 				 * probing (eg: during kexec) or we could not
5276 				 * allocate memory. Clear the failover_pending
5277 				 * flag since no one else will. We ignore
5278 				 * EBUSY because it means either FAILOVER reset
5279 				 * is already scheduled or the adapter is
5280 				 * being removed.
5281 				 */
5282 				netdev_err(netdev,
5283 					   "Error %ld scheduling failover reset\n",
5284 					   rc);
5285 				adapter->failover_pending = false;
5286 			}
5287 			break;
5288 		case IBMVNIC_CRQ_INIT_COMPLETE:
5289 			dev_info(dev, "Partner initialization complete\n");
5290 			adapter->crq.active = true;
5291 			send_version_xchg(adapter);
5292 			break;
5293 		default:
5294 			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
5295 		}
5296 		return;
5297 	case IBMVNIC_CRQ_XPORT_EVENT:
5298 		netif_carrier_off(netdev);
5299 		adapter->crq.active = false;
5300 		/* terminate any thread waiting for a response
5301 		 * from the device
5302 		 */
5303 		if (!completion_done(&adapter->fw_done)) {
5304 			adapter->fw_done_rc = -EIO;
5305 			complete(&adapter->fw_done);
5306 		}
5307 		if (!completion_done(&adapter->stats_done))
5308 			complete(&adapter->stats_done);
5309 		if (test_bit(0, &adapter->resetting))
5310 			adapter->force_reset_recovery = true;
5311 		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
5312 			dev_info(dev, "Migrated, re-enabling adapter\n");
5313 			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
5314 		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5315 			dev_info(dev, "Backing device failover detected\n");
5316 			adapter->failover_pending = true;
5317 		} else {
5318 			/* The adapter lost the connection */
5319 			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5320 				gen_crq->cmd);
5321 			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5322 		}
5323 		return;
5324 	case IBMVNIC_CRQ_CMD_RSP:
5325 		break;
5326 	default:
5327 		dev_err(dev, "Got an invalid msg type 0x%02x\n",
5328 			gen_crq->first);
5329 		return;
5330 	}
5331 
5332 	switch (gen_crq->cmd) {
5333 	case VERSION_EXCHANGE_RSP:
5334 		rc = crq->version_exchange_rsp.rc.code;
5335 		if (rc) {
5336 			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5337 			break;
5338 		}
5339 		ibmvnic_version =
5340 			    be16_to_cpu(crq->version_exchange_rsp.version);
5341 		dev_info(dev, "Partner protocol version is %d\n",
5342 			 ibmvnic_version);
5343 		send_query_cap(adapter);
5344 		break;
5345 	case QUERY_CAPABILITY_RSP:
5346 		handle_query_cap_rsp(crq, adapter);
5347 		break;
5348 	case QUERY_MAP_RSP:
5349 		handle_query_map_rsp(crq, adapter);
5350 		break;
5351 	case REQUEST_MAP_RSP:
5352 		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5353 		complete(&adapter->fw_done);
5354 		break;
5355 	case REQUEST_UNMAP_RSP:
5356 		handle_request_unmap_rsp(crq, adapter);
5357 		break;
5358 	case REQUEST_CAPABILITY_RSP:
5359 		handle_request_cap_rsp(crq, adapter);
5360 		break;
5361 	case LOGIN_RSP:
5362 		netdev_dbg(netdev, "Got Login Response\n");
5363 		handle_login_rsp(crq, adapter);
5364 		break;
5365 	case LOGICAL_LINK_STATE_RSP:
5366 		netdev_dbg(netdev,
5367 			   "Got Logical Link State Response, state: %d rc: %d\n",
5368 			   crq->logical_link_state_rsp.link_state,
5369 			   crq->logical_link_state_rsp.rc.code);
5370 		adapter->logical_link_state =
5371 		    crq->logical_link_state_rsp.link_state;
5372 		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5373 		complete(&adapter->init_done);
5374 		break;
5375 	case LINK_STATE_INDICATION:
5376 		netdev_dbg(netdev, "Got Logical Link State Indication\n");
5377 		adapter->phys_link_state =
5378 		    crq->link_state_indication.phys_link_state;
5379 		adapter->logical_link_state =
5380 		    crq->link_state_indication.logical_link_state;
5381 		if (adapter->phys_link_state && adapter->logical_link_state)
5382 			netif_carrier_on(netdev);
5383 		else
5384 			netif_carrier_off(netdev);
5385 		break;
5386 	case CHANGE_MAC_ADDR_RSP:
5387 		netdev_dbg(netdev, "Got MAC address change Response\n");
5388 		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
5389 		break;
5390 	case ERROR_INDICATION:
5391 		netdev_dbg(netdev, "Got Error Indication\n");
5392 		handle_error_indication(crq, adapter);
5393 		break;
5394 	case REQUEST_STATISTICS_RSP:
5395 		netdev_dbg(netdev, "Got Statistics Response\n");
5396 		complete(&adapter->stats_done);
5397 		break;
5398 	case QUERY_IP_OFFLOAD_RSP:
5399 		netdev_dbg(netdev, "Got Query IP offload Response\n");
5400 		handle_query_ip_offload_rsp(adapter);
5401 		break;
5402 	case MULTICAST_CTRL_RSP:
5403 		netdev_dbg(netdev, "Got multicast control Response\n");
5404 		break;
5405 	case CONTROL_IP_OFFLOAD_RSP:
5406 		netdev_dbg(netdev, "Got Control IP offload Response\n");
5407 		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5408 				 sizeof(adapter->ip_offload_ctrl),
5409 				 DMA_TO_DEVICE);
5410 		complete(&adapter->init_done);
5411 		break;
5412 	case COLLECT_FW_TRACE_RSP:
5413 		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5414 		complete(&adapter->fw_done);
5415 		break;
5416 	case GET_VPD_SIZE_RSP:
5417 		handle_vpd_size_rsp(crq, adapter);
5418 		break;
5419 	case GET_VPD_RSP:
5420 		handle_vpd_rsp(crq, adapter);
5421 		break;
5422 	case QUERY_PHYS_PARMS_RSP:
5423 		adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5424 		complete(&adapter->fw_done);
5425 		break;
5426 	default:
5427 		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5428 			   gen_crq->cmd);
5429 	}
5430 }
5431 
5432 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5433 {
5434 	struct ibmvnic_adapter *adapter = instance;
5435 
5436 	tasklet_schedule(&adapter->tasklet);
5437 	return IRQ_HANDLED;
5438 }
5439 
5440 static void ibmvnic_tasklet(struct tasklet_struct *t)
5441 {
5442 	struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
5443 	struct ibmvnic_crq_queue *queue = &adapter->crq;
5444 	union ibmvnic_crq *crq;
5445 	unsigned long flags;
5446 	bool done = false;
5447 
5448 	spin_lock_irqsave(&queue->lock, flags);
5449 	while (!done) {
5450 		/* Pull all the valid messages off the CRQ */
5451 		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
5452 			/* This barrier makes sure ibmvnic_next_crq()'s
5453 			 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5454 			 * before ibmvnic_handle_crq()'s
5455 			 * switch(gen_crq->first) and switch(gen_crq->cmd).
5456 			 */
5457 			dma_rmb();
5458 			ibmvnic_handle_crq(crq, adapter);
5459 			crq->generic.first = 0;
5460 		}
5461 
5462 		/* remain in tasklet until all
5463 		 * capabilities responses are received
5464 		 */
5465 		if (!adapter->wait_capability)
5466 			done = true;
5467 	}
5468 	/* if capabilities CRQ's were sent in this tasklet, the following
5469 	 * tasklet must wait until all responses are received
5470 	 */
5471 	if (atomic_read(&adapter->running_cap_crqs) != 0)
5472 		adapter->wait_capability = true;
5473 	spin_unlock_irqrestore(&queue->lock, flags);
5474 }
5475 
5476 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5477 {
5478 	struct vio_dev *vdev = adapter->vdev;
5479 	int rc;
5480 
5481 	do {
5482 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
5483 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
5484 
5485 	if (rc)
5486 		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
5487 
5488 	return rc;
5489 }
5490 
5491 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
5492 {
5493 	struct ibmvnic_crq_queue *crq = &adapter->crq;
5494 	struct device *dev = &adapter->vdev->dev;
5495 	struct vio_dev *vdev = adapter->vdev;
5496 	int rc;
5497 
5498 	/* Close the CRQ */
5499 	do {
5500 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5501 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5502 
5503 	/* Clean out the queue */
5504 	if (!crq->msgs)
5505 		return -EINVAL;
5506 
5507 	memset(crq->msgs, 0, PAGE_SIZE);
5508 	crq->cur = 0;
5509 	crq->active = false;
5510 
5511 	/* And re-open it again */
5512 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5513 				crq->msg_token, PAGE_SIZE);
5514 
5515 	if (rc == H_CLOSED)
5516 		/* Adapter is good, but other end is not ready */
5517 		dev_warn(dev, "Partner adapter not ready\n");
5518 	else if (rc != 0)
5519 		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
5520 
5521 	return rc;
5522 }
5523 
5524 static void release_crq_queue(struct ibmvnic_adapter *adapter)
5525 {
5526 	struct ibmvnic_crq_queue *crq = &adapter->crq;
5527 	struct vio_dev *vdev = adapter->vdev;
5528 	long rc;
5529 
5530 	if (!crq->msgs)
5531 		return;
5532 
5533 	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
5534 	free_irq(vdev->irq, adapter);
5535 	tasklet_kill(&adapter->tasklet);
5536 	do {
5537 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5538 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5539 
5540 	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
5541 			 DMA_BIDIRECTIONAL);
5542 	free_page((unsigned long)crq->msgs);
5543 	crq->msgs = NULL;
5544 	crq->active = false;
5545 }
5546 
5547 static int init_crq_queue(struct ibmvnic_adapter *adapter)
5548 {
5549 	struct ibmvnic_crq_queue *crq = &adapter->crq;
5550 	struct device *dev = &adapter->vdev->dev;
5551 	struct vio_dev *vdev = adapter->vdev;
5552 	int rc, retrc = -ENOMEM;
5553 
5554 	if (crq->msgs)
5555 		return 0;
5556 
5557 	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
5558 	/* Should we allocate more than one page? */
5559 
5560 	if (!crq->msgs)
5561 		return -ENOMEM;
5562 
5563 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
5564 	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
5565 					DMA_BIDIRECTIONAL);
5566 	if (dma_mapping_error(dev, crq->msg_token))
5567 		goto map_failed;
5568 
5569 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
5570 				crq->msg_token, PAGE_SIZE);
5571 
5572 	if (rc == H_RESOURCE)
5573 		/* maybe kexecing and resource is busy. try a reset */
5574 		rc = ibmvnic_reset_crq(adapter);
5575 	retrc = rc;
5576 
5577 	if (rc == H_CLOSED) {
5578 		dev_warn(dev, "Partner adapter not ready\n");
5579 	} else if (rc) {
5580 		dev_warn(dev, "Error %d opening adapter\n", rc);
5581 		goto reg_crq_failed;
5582 	}
5583 
5584 	retrc = 0;
5585 
5586 	tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
5587 
5588 	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
5589 	snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
5590 		 adapter->vdev->unit_address);
5591 	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
5592 	if (rc) {
5593 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
5594 			vdev->irq, rc);
5595 		goto req_irq_failed;
5596 	}
5597 
5598 	rc = vio_enable_interrupts(vdev);
5599 	if (rc) {
5600 		dev_err(dev, "Error %d enabling interrupts\n", rc);
5601 		goto req_irq_failed;
5602 	}
5603 
5604 	crq->cur = 0;
5605 	spin_lock_init(&crq->lock);
5606 
5607 	return retrc;
5608 
5609 req_irq_failed:
5610 	tasklet_kill(&adapter->tasklet);
5611 	do {
5612 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
5613 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
5614 reg_crq_failed:
5615 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
5616 map_failed:
5617 	free_page((unsigned long)crq->msgs);
5618 	crq->msgs = NULL;
5619 	return retrc;
5620 }
5621 
5622 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
5623 {
5624 	struct device *dev = &adapter->vdev->dev;
5625 	unsigned long timeout = msecs_to_jiffies(20000);
5626 	u64 old_num_rx_queues = adapter->req_rx_queues;
5627 	u64 old_num_tx_queues = adapter->req_tx_queues;
5628 	int rc;
5629 
5630 	adapter->from_passive_init = false;
5631 
5632 	if (reset)
5633 		reinit_completion(&adapter->init_done);
5634 
5635 	adapter->init_done_rc = 0;
5636 	rc = ibmvnic_send_crq_init(adapter);
5637 	if (rc) {
5638 		dev_err(dev, "Send crq init failed with error %d\n", rc);
5639 		return rc;
5640 	}
5641 
5642 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
5643 		dev_err(dev, "Initialization sequence timed out\n");
5644 		return -1;
5645 	}
5646 
5647 	if (adapter->init_done_rc) {
5648 		release_crq_queue(adapter);
5649 		return adapter->init_done_rc;
5650 	}
5651 
5652 	if (adapter->from_passive_init) {
5653 		adapter->state = VNIC_OPEN;
5654 		adapter->from_passive_init = false;
5655 		return -1;
5656 	}
5657 
5658 	if (reset &&
5659 	    test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
5660 	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
5661 		if (adapter->req_rx_queues != old_num_rx_queues ||
5662 		    adapter->req_tx_queues != old_num_tx_queues) {
5663 			release_sub_crqs(adapter, 0);
5664 			rc = init_sub_crqs(adapter);
5665 		} else {
5666 			rc = reset_sub_crq_queues(adapter);
5667 		}
5668 	} else {
5669 		rc = init_sub_crqs(adapter);
5670 	}
5671 
5672 	if (rc) {
5673 		dev_err(dev, "Initialization of sub crqs failed\n");
5674 		release_crq_queue(adapter);
5675 		return rc;
5676 	}
5677 
5678 	rc = init_sub_crq_irqs(adapter);
5679 	if (rc) {
5680 		dev_err(dev, "Failed to initialize sub crq irqs\n");
5681 		release_crq_queue(adapter);
5682 	}
5683 
5684 	return rc;
5685 }
5686 
5687 static struct device_attribute dev_attr_failover;
5688 
5689 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
5690 {
5691 	struct ibmvnic_adapter *adapter;
5692 	struct net_device *netdev;
5693 	unsigned char *mac_addr_p;
5694 	bool init_success;
5695 	int rc;
5696 
5697 	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
5698 		dev->unit_address);
5699 
5700 	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
5701 							VETH_MAC_ADDR, NULL);
5702 	if (!mac_addr_p) {
5703 		dev_err(&dev->dev,
5704 			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
5705 			__FILE__, __LINE__);
5706 		return 0;
5707 	}
5708 
5709 	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
5710 				   IBMVNIC_MAX_QUEUES);
5711 	if (!netdev)
5712 		return -ENOMEM;
5713 
5714 	adapter = netdev_priv(netdev);
5715 	adapter->state = VNIC_PROBING;
5716 	dev_set_drvdata(&dev->dev, netdev);
5717 	adapter->vdev = dev;
5718 	adapter->netdev = netdev;
5719 	adapter->login_pending = false;
5720 	memset(&adapter->map_ids, 0, sizeof(adapter->map_ids));
5721 	/* map_ids start at 1, so ensure map_id 0 is always "in-use" */
5722 	bitmap_set(adapter->map_ids, 0, 1);
5723 
5724 	ether_addr_copy(adapter->mac_addr, mac_addr_p);
5725 	eth_hw_addr_set(netdev, adapter->mac_addr);
5726 	netdev->irq = dev->irq;
5727 	netdev->netdev_ops = &ibmvnic_netdev_ops;
5728 	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
5729 	SET_NETDEV_DEV(netdev, &dev->dev);
5730 
5731 	INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
5732 	INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
5733 			  __ibmvnic_delayed_reset);
5734 	INIT_LIST_HEAD(&adapter->rwi_list);
5735 	spin_lock_init(&adapter->rwi_lock);
5736 	spin_lock_init(&adapter->state_lock);
5737 	mutex_init(&adapter->fw_lock);
5738 	init_completion(&adapter->init_done);
5739 	init_completion(&adapter->fw_done);
5740 	init_completion(&adapter->reset_done);
5741 	init_completion(&adapter->stats_done);
5742 	clear_bit(0, &adapter->resetting);
5743 	adapter->prev_rx_buf_sz = 0;
5744 	adapter->prev_mtu = 0;
5745 
5746 	init_success = false;
5747 	do {
5748 		rc = init_crq_queue(adapter);
5749 		if (rc) {
5750 			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
5751 				rc);
5752 			goto ibmvnic_init_fail;
5753 		}
5754 
5755 		rc = ibmvnic_reset_init(adapter, false);
5756 	} while (rc == EAGAIN);
5757 
5758 	/* We are ignoring the error from ibmvnic_reset_init() assuming that the
5759 	 * partner is not ready. CRQ is not active. When the partner becomes
5760 	 * ready, we will do the passive init reset.
5761 	 */
5762 
5763 	if (!rc)
5764 		init_success = true;
5765 
5766 	rc = init_stats_buffers(adapter);
5767 	if (rc)
5768 		goto ibmvnic_init_fail;
5769 
5770 	rc = init_stats_token(adapter);
5771 	if (rc)
5772 		goto ibmvnic_stats_fail;
5773 
5774 	rc = device_create_file(&dev->dev, &dev_attr_failover);
5775 	if (rc)
5776 		goto ibmvnic_dev_file_err;
5777 
5778 	netif_carrier_off(netdev);
5779 	rc = register_netdev(netdev);
5780 	if (rc) {
5781 		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
5782 		goto ibmvnic_register_fail;
5783 	}
5784 	dev_info(&dev->dev, "ibmvnic registered\n");
5785 
5786 	if (init_success) {
5787 		adapter->state = VNIC_PROBED;
5788 		netdev->mtu = adapter->req_mtu - ETH_HLEN;
5789 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5790 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5791 	} else {
5792 		adapter->state = VNIC_DOWN;
5793 	}
5794 
5795 	adapter->wait_for_reset = false;
5796 	adapter->last_reset_time = jiffies;
5797 	return 0;
5798 
5799 ibmvnic_register_fail:
5800 	device_remove_file(&dev->dev, &dev_attr_failover);
5801 
5802 ibmvnic_dev_file_err:
5803 	release_stats_token(adapter);
5804 
5805 ibmvnic_stats_fail:
5806 	release_stats_buffers(adapter);
5807 
5808 ibmvnic_init_fail:
5809 	release_sub_crqs(adapter, 1);
5810 	release_crq_queue(adapter);
5811 	mutex_destroy(&adapter->fw_lock);
5812 	free_netdev(netdev);
5813 
5814 	return rc;
5815 }
5816 
5817 static void ibmvnic_remove(struct vio_dev *dev)
5818 {
5819 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
5820 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5821 	unsigned long flags;
5822 
5823 	spin_lock_irqsave(&adapter->state_lock, flags);
5824 
5825 	/* If ibmvnic_reset() is scheduling a reset, wait for it to
5826 	 * finish. Then, set the state to REMOVING to prevent it from
5827 	 * scheduling any more work and to have reset functions ignore
5828 	 * any resets that have already been scheduled. Drop the lock
5829 	 * after setting state, so __ibmvnic_reset() which is called
5830 	 * from the flush_work() below, can make progress.
5831 	 */
5832 	spin_lock(&adapter->rwi_lock);
5833 	adapter->state = VNIC_REMOVING;
5834 	spin_unlock(&adapter->rwi_lock);
5835 
5836 	spin_unlock_irqrestore(&adapter->state_lock, flags);
5837 
5838 	flush_work(&adapter->ibmvnic_reset);
5839 	flush_delayed_work(&adapter->ibmvnic_delayed_reset);
5840 
5841 	rtnl_lock();
5842 	unregister_netdevice(netdev);
5843 
5844 	release_resources(adapter);
5845 	release_rx_pools(adapter);
5846 	release_tx_pools(adapter);
5847 	release_sub_crqs(adapter, 1);
5848 	release_crq_queue(adapter);
5849 
5850 	release_stats_token(adapter);
5851 	release_stats_buffers(adapter);
5852 
5853 	adapter->state = VNIC_REMOVED;
5854 
5855 	rtnl_unlock();
5856 	mutex_destroy(&adapter->fw_lock);
5857 	device_remove_file(&dev->dev, &dev_attr_failover);
5858 	free_netdev(netdev);
5859 	dev_set_drvdata(&dev->dev, NULL);
5860 }
5861 
5862 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
5863 			      const char *buf, size_t count)
5864 {
5865 	struct net_device *netdev = dev_get_drvdata(dev);
5866 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5867 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
5868 	__be64 session_token;
5869 	long rc;
5870 
5871 	if (!sysfs_streq(buf, "1"))
5872 		return -EINVAL;
5873 
5874 	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
5875 			 H_GET_SESSION_TOKEN, 0, 0, 0);
5876 	if (rc) {
5877 		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
5878 			   rc);
5879 		goto last_resort;
5880 	}
5881 
5882 	session_token = (__be64)retbuf[0];
5883 	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
5884 		   be64_to_cpu(session_token));
5885 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
5886 				H_SESSION_ERR_DETECTED, session_token, 0, 0);
5887 	if (rc)
5888 		netdev_err(netdev,
5889 			   "H_VIOCTL initiated failover failed, rc %ld\n",
5890 			   rc);
5891 
5892 last_resort:
5893 	netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
5894 	ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5895 
5896 	return count;
5897 }
5898 static DEVICE_ATTR_WO(failover);
5899 
5900 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
5901 {
5902 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
5903 	struct ibmvnic_adapter *adapter;
5904 	struct iommu_table *tbl;
5905 	unsigned long ret = 0;
5906 	int i;
5907 
5908 	tbl = get_iommu_table_base(&vdev->dev);
5909 
5910 	/* netdev inits at probe time along with the structures we need below*/
5911 	if (!netdev)
5912 		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
5913 
5914 	adapter = netdev_priv(netdev);
5915 
5916 	ret += PAGE_SIZE; /* the crq message queue */
5917 	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
5918 
5919 	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5920 		ret += 4 * PAGE_SIZE; /* the scrq message queue */
5921 
5922 	for (i = 0; i < adapter->num_active_rx_pools; i++)
5923 		ret += adapter->rx_pool[i].size *
5924 		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5925 
5926 	return ret;
5927 }
5928 
5929 static int ibmvnic_resume(struct device *dev)
5930 {
5931 	struct net_device *netdev = dev_get_drvdata(dev);
5932 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5933 
5934 	if (adapter->state != VNIC_OPEN)
5935 		return 0;
5936 
5937 	tasklet_schedule(&adapter->tasklet);
5938 
5939 	return 0;
5940 }
5941 
5942 static const struct vio_device_id ibmvnic_device_table[] = {
5943 	{"network", "IBM,vnic"},
5944 	{"", "" }
5945 };
5946 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5947 
5948 static const struct dev_pm_ops ibmvnic_pm_ops = {
5949 	.resume = ibmvnic_resume
5950 };
5951 
5952 static struct vio_driver ibmvnic_driver = {
5953 	.id_table       = ibmvnic_device_table,
5954 	.probe          = ibmvnic_probe,
5955 	.remove         = ibmvnic_remove,
5956 	.get_desired_dma = ibmvnic_get_desired_dma,
5957 	.name		= ibmvnic_driver_name,
5958 	.pm		= &ibmvnic_pm_ops,
5959 };
5960 
5961 /* module functions */
5962 static int __init ibmvnic_module_init(void)
5963 {
5964 	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5965 		IBMVNIC_DRIVER_VERSION);
5966 
5967 	return vio_register_driver(&ibmvnic_driver);
5968 }
5969 
5970 static void __exit ibmvnic_module_exit(void)
5971 {
5972 	vio_unregister_driver(&ibmvnic_driver);
5973 }
5974 
5975 module_init(ibmvnic_module_init);
5976 module_exit(ibmvnic_module_exit);
5977