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