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