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