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