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