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