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