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