xref: /linux/drivers/net/ethernet/ibm/ibmvnic.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
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 rc;
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 		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2428 		if (lpar_rc != H_SUCCESS)
2429 			goto tx_err;
2430 		goto out;
2431 	}
2432 
2433 	if (skb_is_gso(skb))
2434 		tx_pool = &adapter->tso_pool[queue_num];
2435 	else
2436 		tx_pool = &adapter->tx_pool[queue_num];
2437 
2438 	bufidx = tx_pool->free_map[tx_pool->consumer_index];
2439 
2440 	if (bufidx == IBMVNIC_INVALID_MAP) {
2441 		dev_kfree_skb_any(skb);
2442 		tx_send_failed++;
2443 		tx_dropped++;
2444 		ret = NETDEV_TX_OK;
2445 		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2446 		if (lpar_rc != H_SUCCESS)
2447 			goto tx_err;
2448 		goto out;
2449 	}
2450 
2451 	tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
2452 
2453 	map_txpool_buf_to_ltb(tx_pool, bufidx, &ltb, &offset);
2454 
2455 	dst = ltb->buff + offset;
2456 	memset(dst, 0, tx_pool->buf_size);
2457 	data_dma_addr = ltb->addr + offset;
2458 
2459 	if (skb_shinfo(skb)->nr_frags) {
2460 		int cur, i;
2461 
2462 		/* Copy the head */
2463 		skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
2464 		cur = skb_headlen(skb);
2465 
2466 		/* Copy the frags */
2467 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2468 			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2469 
2470 			memcpy(dst + cur, skb_frag_address(frag),
2471 			       skb_frag_size(frag));
2472 			cur += skb_frag_size(frag);
2473 		}
2474 	} else {
2475 		skb_copy_from_linear_data(skb, dst, skb->len);
2476 	}
2477 
2478 	/* post changes to long_term_buff *dst before VIOS accessing it */
2479 	dma_wmb();
2480 
2481 	tx_pool->consumer_index =
2482 	    (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
2483 
2484 	tx_buff = &tx_pool->tx_buff[bufidx];
2485 	tx_buff->skb = skb;
2486 	tx_buff->index = bufidx;
2487 	tx_buff->pool_index = queue_num;
2488 
2489 	memset(&tx_crq, 0, sizeof(tx_crq));
2490 	tx_crq.v1.first = IBMVNIC_CRQ_CMD;
2491 	tx_crq.v1.type = IBMVNIC_TX_DESC;
2492 	tx_crq.v1.n_crq_elem = 1;
2493 	tx_crq.v1.n_sge = 1;
2494 	tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
2495 
2496 	if (skb_is_gso(skb))
2497 		tx_crq.v1.correlator =
2498 			cpu_to_be32(bufidx | IBMVNIC_TSO_POOL_MASK);
2499 	else
2500 		tx_crq.v1.correlator = cpu_to_be32(bufidx);
2501 	tx_crq.v1.dma_reg = cpu_to_be16(ltb->map_id);
2502 	tx_crq.v1.sge_len = cpu_to_be32(skb->len);
2503 	tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
2504 
2505 	if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
2506 		tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
2507 		tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
2508 	}
2509 
2510 	if (skb->protocol == htons(ETH_P_IP)) {
2511 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
2512 		proto = ip_hdr(skb)->protocol;
2513 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
2514 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
2515 		proto = ipv6_hdr(skb)->nexthdr;
2516 	}
2517 
2518 	if (proto == IPPROTO_TCP)
2519 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
2520 	else if (proto == IPPROTO_UDP)
2521 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
2522 
2523 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2524 		tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
2525 		hdrs += 2;
2526 	}
2527 	if (skb_is_gso(skb)) {
2528 		tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
2529 		tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
2530 		hdrs += 2;
2531 	}
2532 
2533 	if ((*hdrs >> 7) & 1)
2534 		build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
2535 
2536 	tx_crq.v1.n_crq_elem = num_entries;
2537 	tx_buff->num_entries = num_entries;
2538 	/* flush buffer if current entry can not fit */
2539 	if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
2540 		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2541 		if (lpar_rc != H_SUCCESS)
2542 			goto tx_flush_err;
2543 	}
2544 
2545 	indir_arr[0] = tx_crq;
2546 	memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
2547 	       num_entries * sizeof(struct ibmvnic_generic_scrq));
2548 	ind_bufp->index += num_entries;
2549 	if (__netdev_tx_sent_queue(txq, skb->len,
2550 				   netdev_xmit_more() &&
2551 				   ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
2552 		lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2553 		if (lpar_rc != H_SUCCESS)
2554 			goto tx_err;
2555 	}
2556 
2557 	if (atomic_add_return(num_entries, &tx_scrq->used)
2558 					>= adapter->req_tx_entries_per_subcrq) {
2559 		netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
2560 		netif_stop_subqueue(netdev, queue_num);
2561 	}
2562 
2563 	tx_packets++;
2564 	tx_bytes += skb->len;
2565 	txq_trans_cond_update(txq);
2566 	ret = NETDEV_TX_OK;
2567 	goto out;
2568 
2569 tx_flush_err:
2570 	dev_kfree_skb_any(skb);
2571 	tx_buff->skb = NULL;
2572 	tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
2573 				  tx_pool->num_buffers - 1 :
2574 				  tx_pool->consumer_index - 1;
2575 	tx_dropped++;
2576 tx_err:
2577 	if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
2578 		dev_err_ratelimited(dev, "tx: send failed\n");
2579 
2580 	if (lpar_rc == H_CLOSED || adapter->failover_pending) {
2581 		/* Disable TX and report carrier off if queue is closed
2582 		 * or pending failover.
2583 		 * Firmware guarantees that a signal will be sent to the
2584 		 * driver, triggering a reset or some other action.
2585 		 */
2586 		netif_tx_stop_all_queues(netdev);
2587 		netif_carrier_off(netdev);
2588 	}
2589 out:
2590 	rcu_read_unlock();
2591 	netdev->stats.tx_dropped += tx_dropped;
2592 	netdev->stats.tx_bytes += tx_bytes;
2593 	netdev->stats.tx_packets += tx_packets;
2594 	adapter->tx_send_failed += tx_send_failed;
2595 	adapter->tx_map_failed += tx_map_failed;
2596 	adapter->tx_stats_buffers[queue_num].packets += tx_packets;
2597 	adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
2598 	adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
2599 
2600 	return ret;
2601 }
2602 
2603 static void ibmvnic_set_multi(struct net_device *netdev)
2604 {
2605 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2606 	struct netdev_hw_addr *ha;
2607 	union ibmvnic_crq crq;
2608 
2609 	memset(&crq, 0, sizeof(crq));
2610 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
2611 	crq.request_capability.cmd = REQUEST_CAPABILITY;
2612 
2613 	if (netdev->flags & IFF_PROMISC) {
2614 		if (!adapter->promisc_supported)
2615 			return;
2616 	} else {
2617 		if (netdev->flags & IFF_ALLMULTI) {
2618 			/* Accept all multicast */
2619 			memset(&crq, 0, sizeof(crq));
2620 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2621 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2622 			crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
2623 			ibmvnic_send_crq(adapter, &crq);
2624 		} else if (netdev_mc_empty(netdev)) {
2625 			/* Reject all multicast */
2626 			memset(&crq, 0, sizeof(crq));
2627 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2628 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2629 			crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
2630 			ibmvnic_send_crq(adapter, &crq);
2631 		} else {
2632 			/* Accept one or more multicast(s) */
2633 			netdev_for_each_mc_addr(ha, netdev) {
2634 				memset(&crq, 0, sizeof(crq));
2635 				crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2636 				crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2637 				crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
2638 				ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
2639 						ha->addr);
2640 				ibmvnic_send_crq(adapter, &crq);
2641 			}
2642 		}
2643 	}
2644 }
2645 
2646 static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
2647 {
2648 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2649 	union ibmvnic_crq crq;
2650 	int rc;
2651 
2652 	if (!is_valid_ether_addr(dev_addr)) {
2653 		rc = -EADDRNOTAVAIL;
2654 		goto err;
2655 	}
2656 
2657 	memset(&crq, 0, sizeof(crq));
2658 	crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
2659 	crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
2660 	ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
2661 
2662 	mutex_lock(&adapter->fw_lock);
2663 	adapter->fw_done_rc = 0;
2664 	reinit_completion(&adapter->fw_done);
2665 
2666 	rc = ibmvnic_send_crq(adapter, &crq);
2667 	if (rc) {
2668 		rc = -EIO;
2669 		mutex_unlock(&adapter->fw_lock);
2670 		goto err;
2671 	}
2672 
2673 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
2674 	/* netdev->dev_addr is changed in handle_change_mac_rsp function */
2675 	if (rc || adapter->fw_done_rc) {
2676 		rc = -EIO;
2677 		mutex_unlock(&adapter->fw_lock);
2678 		goto err;
2679 	}
2680 	mutex_unlock(&adapter->fw_lock);
2681 	return 0;
2682 err:
2683 	ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2684 	return rc;
2685 }
2686 
2687 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
2688 {
2689 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2690 	struct sockaddr *addr = p;
2691 	int rc;
2692 
2693 	rc = 0;
2694 	if (!is_valid_ether_addr(addr->sa_data))
2695 		return -EADDRNOTAVAIL;
2696 
2697 	ether_addr_copy(adapter->mac_addr, addr->sa_data);
2698 	if (adapter->state != VNIC_PROBED)
2699 		rc = __ibmvnic_set_mac(netdev, addr->sa_data);
2700 
2701 	return rc;
2702 }
2703 
2704 static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
2705 {
2706 	switch (reason) {
2707 	case VNIC_RESET_FAILOVER:
2708 		return "FAILOVER";
2709 	case VNIC_RESET_MOBILITY:
2710 		return "MOBILITY";
2711 	case VNIC_RESET_FATAL:
2712 		return "FATAL";
2713 	case VNIC_RESET_NON_FATAL:
2714 		return "NON_FATAL";
2715 	case VNIC_RESET_TIMEOUT:
2716 		return "TIMEOUT";
2717 	case VNIC_RESET_CHANGE_PARAM:
2718 		return "CHANGE_PARAM";
2719 	case VNIC_RESET_PASSIVE_INIT:
2720 		return "PASSIVE_INIT";
2721 	}
2722 	return "UNKNOWN";
2723 }
2724 
2725 /*
2726  * Initialize the init_done completion and return code values. We
2727  * can get a transport event just after registering the CRQ and the
2728  * tasklet will use this to communicate the transport event. To ensure
2729  * we don't miss the notification/error, initialize these _before_
2730  * regisering the CRQ.
2731  */
2732 static inline void reinit_init_done(struct ibmvnic_adapter *adapter)
2733 {
2734 	reinit_completion(&adapter->init_done);
2735 	adapter->init_done_rc = 0;
2736 }
2737 
2738 /*
2739  * do_reset returns zero if we are able to keep processing reset events, or
2740  * non-zero if we hit a fatal error and must halt.
2741  */
2742 static int do_reset(struct ibmvnic_adapter *adapter,
2743 		    struct ibmvnic_rwi *rwi, u32 reset_state)
2744 {
2745 	struct net_device *netdev = adapter->netdev;
2746 	u64 old_num_rx_queues, old_num_tx_queues;
2747 	u64 old_num_rx_slots, old_num_tx_slots;
2748 	int rc;
2749 
2750 	netdev_dbg(adapter->netdev,
2751 		   "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2752 		   adapter_state_to_string(adapter->state),
2753 		   adapter->failover_pending,
2754 		   reset_reason_to_string(rwi->reset_reason),
2755 		   adapter_state_to_string(reset_state));
2756 
2757 	adapter->reset_reason = rwi->reset_reason;
2758 	/* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2759 	if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2760 		rtnl_lock();
2761 
2762 	/* Now that we have the rtnl lock, clear any pending failover.
2763 	 * This will ensure ibmvnic_open() has either completed or will
2764 	 * block until failover is complete.
2765 	 */
2766 	if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2767 		adapter->failover_pending = false;
2768 
2769 	/* read the state and check (again) after getting rtnl */
2770 	reset_state = adapter->state;
2771 
2772 	if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2773 		rc = -EBUSY;
2774 		goto out;
2775 	}
2776 
2777 	netif_carrier_off(netdev);
2778 
2779 	old_num_rx_queues = adapter->req_rx_queues;
2780 	old_num_tx_queues = adapter->req_tx_queues;
2781 	old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2782 	old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
2783 
2784 	ibmvnic_cleanup(netdev);
2785 
2786 	if (reset_state == VNIC_OPEN &&
2787 	    adapter->reset_reason != VNIC_RESET_MOBILITY &&
2788 	    adapter->reset_reason != VNIC_RESET_FAILOVER) {
2789 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2790 			rc = __ibmvnic_close(netdev);
2791 			if (rc)
2792 				goto out;
2793 		} else {
2794 			adapter->state = VNIC_CLOSING;
2795 
2796 			/* Release the RTNL lock before link state change and
2797 			 * re-acquire after the link state change to allow
2798 			 * linkwatch_event to grab the RTNL lock and run during
2799 			 * a reset.
2800 			 */
2801 			rtnl_unlock();
2802 			rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2803 			rtnl_lock();
2804 			if (rc)
2805 				goto out;
2806 
2807 			if (adapter->state == VNIC_OPEN) {
2808 				/* When we dropped rtnl, ibmvnic_open() got
2809 				 * it and noticed that we are resetting and
2810 				 * set the adapter state to OPEN. Update our
2811 				 * new "target" state, and resume the reset
2812 				 * from VNIC_CLOSING state.
2813 				 */
2814 				netdev_dbg(netdev,
2815 					   "Open changed state from %s, updating.\n",
2816 					   adapter_state_to_string(reset_state));
2817 				reset_state = VNIC_OPEN;
2818 				adapter->state = VNIC_CLOSING;
2819 			}
2820 
2821 			if (adapter->state != VNIC_CLOSING) {
2822 				/* If someone else changed the adapter state
2823 				 * when we dropped the rtnl, fail the reset
2824 				 */
2825 				rc = -EAGAIN;
2826 				goto out;
2827 			}
2828 			adapter->state = VNIC_CLOSED;
2829 		}
2830 	}
2831 
2832 	if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2833 		release_resources(adapter);
2834 		release_sub_crqs(adapter, 1);
2835 		release_crq_queue(adapter);
2836 	}
2837 
2838 	if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2839 		/* remove the closed state so when we call open it appears
2840 		 * we are coming from the probed state.
2841 		 */
2842 		adapter->state = VNIC_PROBED;
2843 
2844 		reinit_init_done(adapter);
2845 
2846 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2847 			rc = init_crq_queue(adapter);
2848 		} else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
2849 			rc = ibmvnic_reenable_crq_queue(adapter);
2850 			release_sub_crqs(adapter, 1);
2851 		} else {
2852 			rc = ibmvnic_reset_crq(adapter);
2853 			if (rc == H_CLOSED || rc == H_SUCCESS) {
2854 				rc = vio_enable_interrupts(adapter->vdev);
2855 				if (rc)
2856 					netdev_err(adapter->netdev,
2857 						   "Reset failed to enable interrupts. rc=%d\n",
2858 						   rc);
2859 			}
2860 		}
2861 
2862 		if (rc) {
2863 			netdev_err(adapter->netdev,
2864 				   "Reset couldn't initialize crq. rc=%d\n", rc);
2865 			goto out;
2866 		}
2867 
2868 		rc = ibmvnic_reset_init(adapter, true);
2869 		if (rc)
2870 			goto out;
2871 
2872 		/* If the adapter was in PROBE or DOWN state prior to the reset,
2873 		 * exit here.
2874 		 */
2875 		if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) {
2876 			rc = 0;
2877 			goto out;
2878 		}
2879 
2880 		rc = ibmvnic_login(netdev);
2881 		if (rc)
2882 			goto out;
2883 
2884 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2885 			rc = init_resources(adapter);
2886 			if (rc)
2887 				goto out;
2888 		} else if (adapter->req_rx_queues != old_num_rx_queues ||
2889 		    adapter->req_tx_queues != old_num_tx_queues ||
2890 		    adapter->req_rx_add_entries_per_subcrq !=
2891 		    old_num_rx_slots ||
2892 		    adapter->req_tx_entries_per_subcrq !=
2893 		    old_num_tx_slots ||
2894 		    !adapter->rx_pool ||
2895 		    !adapter->tso_pool ||
2896 		    !adapter->tx_pool) {
2897 			release_napi(adapter);
2898 			release_vpd_data(adapter);
2899 
2900 			rc = init_resources(adapter);
2901 			if (rc)
2902 				goto out;
2903 
2904 		} else {
2905 			rc = init_tx_pools(netdev);
2906 			if (rc) {
2907 				netdev_dbg(netdev,
2908 					   "init tx pools failed (%d)\n",
2909 					   rc);
2910 				goto out;
2911 			}
2912 
2913 			rc = init_rx_pools(netdev);
2914 			if (rc) {
2915 				netdev_dbg(netdev,
2916 					   "init rx pools failed (%d)\n",
2917 					   rc);
2918 				goto out;
2919 			}
2920 		}
2921 		ibmvnic_disable_irqs(adapter);
2922 	}
2923 	adapter->state = VNIC_CLOSED;
2924 
2925 	if (reset_state == VNIC_CLOSED) {
2926 		rc = 0;
2927 		goto out;
2928 	}
2929 
2930 	rc = __ibmvnic_open(netdev);
2931 	if (rc) {
2932 		rc = IBMVNIC_OPEN_FAILED;
2933 		goto out;
2934 	}
2935 
2936 	/* refresh device's multicast list */
2937 	ibmvnic_set_multi(netdev);
2938 
2939 	if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
2940 	    adapter->reset_reason == VNIC_RESET_MOBILITY)
2941 		__netdev_notify_peers(netdev);
2942 
2943 	rc = 0;
2944 
2945 out:
2946 	/* restore the adapter state if reset failed */
2947 	if (rc)
2948 		adapter->state = reset_state;
2949 	/* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2950 	if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2951 		rtnl_unlock();
2952 
2953 	netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2954 		   adapter_state_to_string(adapter->state),
2955 		   adapter->failover_pending, rc);
2956 	return rc;
2957 }
2958 
2959 static int do_hard_reset(struct ibmvnic_adapter *adapter,
2960 			 struct ibmvnic_rwi *rwi, u32 reset_state)
2961 {
2962 	struct net_device *netdev = adapter->netdev;
2963 	int rc;
2964 
2965 	netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2966 		   reset_reason_to_string(rwi->reset_reason));
2967 
2968 	/* read the state and check (again) after getting rtnl */
2969 	reset_state = adapter->state;
2970 
2971 	if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2972 		rc = -EBUSY;
2973 		goto out;
2974 	}
2975 
2976 	netif_carrier_off(netdev);
2977 	adapter->reset_reason = rwi->reset_reason;
2978 
2979 	ibmvnic_cleanup(netdev);
2980 	release_resources(adapter);
2981 	release_sub_crqs(adapter, 0);
2982 	release_crq_queue(adapter);
2983 
2984 	/* remove the closed state so when we call open it appears
2985 	 * we are coming from the probed state.
2986 	 */
2987 	adapter->state = VNIC_PROBED;
2988 
2989 	reinit_init_done(adapter);
2990 
2991 	rc = init_crq_queue(adapter);
2992 	if (rc) {
2993 		netdev_err(adapter->netdev,
2994 			   "Couldn't initialize crq. rc=%d\n", rc);
2995 		goto out;
2996 	}
2997 
2998 	rc = ibmvnic_reset_init(adapter, false);
2999 	if (rc)
3000 		goto out;
3001 
3002 	/* If the adapter was in PROBE or DOWN state prior to the reset,
3003 	 * exit here.
3004 	 */
3005 	if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN)
3006 		goto out;
3007 
3008 	rc = ibmvnic_login(netdev);
3009 	if (rc)
3010 		goto out;
3011 
3012 	rc = init_resources(adapter);
3013 	if (rc)
3014 		goto out;
3015 
3016 	ibmvnic_disable_irqs(adapter);
3017 	adapter->state = VNIC_CLOSED;
3018 
3019 	if (reset_state == VNIC_CLOSED)
3020 		goto out;
3021 
3022 	rc = __ibmvnic_open(netdev);
3023 	if (rc) {
3024 		rc = IBMVNIC_OPEN_FAILED;
3025 		goto out;
3026 	}
3027 
3028 	__netdev_notify_peers(netdev);
3029 out:
3030 	/* restore adapter state if reset failed */
3031 	if (rc)
3032 		adapter->state = reset_state;
3033 	netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
3034 		   adapter_state_to_string(adapter->state),
3035 		   adapter->failover_pending, rc);
3036 	return rc;
3037 }
3038 
3039 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
3040 {
3041 	struct ibmvnic_rwi *rwi;
3042 	unsigned long flags;
3043 
3044 	spin_lock_irqsave(&adapter->rwi_lock, flags);
3045 
3046 	if (!list_empty(&adapter->rwi_list)) {
3047 		rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
3048 				       list);
3049 		list_del(&rwi->list);
3050 	} else {
3051 		rwi = NULL;
3052 	}
3053 
3054 	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
3055 	return rwi;
3056 }
3057 
3058 /**
3059  * do_passive_init - complete probing when partner device is detected.
3060  * @adapter: ibmvnic_adapter struct
3061  *
3062  * If the ibmvnic device does not have a partner device to communicate with at boot
3063  * and that partner device comes online at a later time, this function is called
3064  * to complete the initialization process of ibmvnic device.
3065  * Caller is expected to hold rtnl_lock().
3066  *
3067  * Returns non-zero if sub-CRQs are not initialized properly leaving the device
3068  * in the down state.
3069  * Returns 0 upon success and the device is in PROBED state.
3070  */
3071 
3072 static int do_passive_init(struct ibmvnic_adapter *adapter)
3073 {
3074 	unsigned long timeout = msecs_to_jiffies(30000);
3075 	struct net_device *netdev = adapter->netdev;
3076 	struct device *dev = &adapter->vdev->dev;
3077 	int rc;
3078 
3079 	netdev_dbg(netdev, "Partner device found, probing.\n");
3080 
3081 	adapter->state = VNIC_PROBING;
3082 	reinit_completion(&adapter->init_done);
3083 	adapter->init_done_rc = 0;
3084 	adapter->crq.active = true;
3085 
3086 	rc = send_crq_init_complete(adapter);
3087 	if (rc)
3088 		goto out;
3089 
3090 	rc = send_version_xchg(adapter);
3091 	if (rc)
3092 		netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc);
3093 
3094 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3095 		dev_err(dev, "Initialization sequence timed out\n");
3096 		rc = -ETIMEDOUT;
3097 		goto out;
3098 	}
3099 
3100 	rc = init_sub_crqs(adapter);
3101 	if (rc) {
3102 		dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc);
3103 		goto out;
3104 	}
3105 
3106 	rc = init_sub_crq_irqs(adapter);
3107 	if (rc) {
3108 		dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc);
3109 		goto init_failed;
3110 	}
3111 
3112 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
3113 	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3114 	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3115 
3116 	adapter->state = VNIC_PROBED;
3117 	netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n");
3118 
3119 	return 0;
3120 
3121 init_failed:
3122 	release_sub_crqs(adapter, 1);
3123 out:
3124 	adapter->state = VNIC_DOWN;
3125 	return rc;
3126 }
3127 
3128 static void __ibmvnic_reset(struct work_struct *work)
3129 {
3130 	struct ibmvnic_adapter *adapter;
3131 	unsigned int timeout = 5000;
3132 	struct ibmvnic_rwi *tmprwi;
3133 	bool saved_state = false;
3134 	struct ibmvnic_rwi *rwi;
3135 	unsigned long flags;
3136 	struct device *dev;
3137 	bool need_reset;
3138 	int num_fails = 0;
3139 	u32 reset_state;
3140 	int rc = 0;
3141 
3142 	adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
3143 		dev = &adapter->vdev->dev;
3144 
3145 	/* Wait for ibmvnic_probe() to complete. If probe is taking too long
3146 	 * or if another reset is in progress, defer work for now. If probe
3147 	 * eventually fails it will flush and terminate our work.
3148 	 *
3149 	 * Three possibilities here:
3150 	 * 1. Adpater being removed  - just return
3151 	 * 2. Timed out on probe or another reset in progress - delay the work
3152 	 * 3. Completed probe - perform any resets in queue
3153 	 */
3154 	if (adapter->state == VNIC_PROBING &&
3155 	    !wait_for_completion_timeout(&adapter->probe_done, timeout)) {
3156 		dev_err(dev, "Reset thread timed out on probe");
3157 		queue_delayed_work(system_long_wq,
3158 				   &adapter->ibmvnic_delayed_reset,
3159 				   IBMVNIC_RESET_DELAY);
3160 		return;
3161 	}
3162 
3163 	/* adapter is done with probe (i.e state is never VNIC_PROBING now) */
3164 	if (adapter->state == VNIC_REMOVING)
3165 		return;
3166 
3167 	/* ->rwi_list is stable now (no one else is removing entries) */
3168 
3169 	/* ibmvnic_probe() may have purged the reset queue after we were
3170 	 * scheduled to process a reset so there maybe no resets to process.
3171 	 * Before setting the ->resetting bit though, we have to make sure
3172 	 * that there is infact a reset to process. Otherwise we may race
3173 	 * with ibmvnic_open() and end up leaving the vnic down:
3174 	 *
3175 	 *	__ibmvnic_reset()	    ibmvnic_open()
3176 	 *	-----------------	    --------------
3177 	 *
3178 	 *  set ->resetting bit
3179 	 *  				find ->resetting bit is set
3180 	 *  				set ->state to IBMVNIC_OPEN (i.e
3181 	 *  				assume reset will open device)
3182 	 *  				return
3183 	 *  find reset queue empty
3184 	 *  return
3185 	 *
3186 	 *  	Neither performed vnic login/open and vnic stays down
3187 	 *
3188 	 * If we hold the lock and conditionally set the bit, either we
3189 	 * or ibmvnic_open() will complete the open.
3190 	 */
3191 	need_reset = false;
3192 	spin_lock(&adapter->rwi_lock);
3193 	if (!list_empty(&adapter->rwi_list)) {
3194 		if (test_and_set_bit_lock(0, &adapter->resetting)) {
3195 			queue_delayed_work(system_long_wq,
3196 					   &adapter->ibmvnic_delayed_reset,
3197 					   IBMVNIC_RESET_DELAY);
3198 		} else {
3199 			need_reset = true;
3200 		}
3201 	}
3202 	spin_unlock(&adapter->rwi_lock);
3203 
3204 	if (!need_reset)
3205 		return;
3206 
3207 	rwi = get_next_rwi(adapter);
3208 	while (rwi) {
3209 		spin_lock_irqsave(&adapter->state_lock, flags);
3210 
3211 		if (adapter->state == VNIC_REMOVING ||
3212 		    adapter->state == VNIC_REMOVED) {
3213 			spin_unlock_irqrestore(&adapter->state_lock, flags);
3214 			kfree(rwi);
3215 			rc = EBUSY;
3216 			break;
3217 		}
3218 
3219 		if (!saved_state) {
3220 			reset_state = adapter->state;
3221 			saved_state = true;
3222 		}
3223 		spin_unlock_irqrestore(&adapter->state_lock, flags);
3224 
3225 		if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) {
3226 			rtnl_lock();
3227 			rc = do_passive_init(adapter);
3228 			rtnl_unlock();
3229 			if (!rc)
3230 				netif_carrier_on(adapter->netdev);
3231 		} else if (adapter->force_reset_recovery) {
3232 			/* Since we are doing a hard reset now, clear the
3233 			 * failover_pending flag so we don't ignore any
3234 			 * future MOBILITY or other resets.
3235 			 */
3236 			adapter->failover_pending = false;
3237 
3238 			/* Transport event occurred during previous reset */
3239 			if (adapter->wait_for_reset) {
3240 				/* Previous was CHANGE_PARAM; caller locked */
3241 				adapter->force_reset_recovery = false;
3242 				rc = do_hard_reset(adapter, rwi, reset_state);
3243 			} else {
3244 				rtnl_lock();
3245 				adapter->force_reset_recovery = false;
3246 				rc = do_hard_reset(adapter, rwi, reset_state);
3247 				rtnl_unlock();
3248 			}
3249 			if (rc)
3250 				num_fails++;
3251 			else
3252 				num_fails = 0;
3253 
3254 			/* If auto-priority-failover is enabled we can get
3255 			 * back to back failovers during resets, resulting
3256 			 * in at least two failed resets (from high-priority
3257 			 * backing device to low-priority one and then back)
3258 			 * If resets continue to fail beyond that, give the
3259 			 * adapter some time to settle down before retrying.
3260 			 */
3261 			if (num_fails >= 3) {
3262 				netdev_dbg(adapter->netdev,
3263 					   "[S:%s] Hard reset failed %d times, waiting 60 secs\n",
3264 					   adapter_state_to_string(adapter->state),
3265 					   num_fails);
3266 				set_current_state(TASK_UNINTERRUPTIBLE);
3267 				schedule_timeout(60 * HZ);
3268 			}
3269 		} else {
3270 			rc = do_reset(adapter, rwi, reset_state);
3271 		}
3272 		tmprwi = rwi;
3273 		adapter->last_reset_time = jiffies;
3274 
3275 		if (rc)
3276 			netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
3277 
3278 		rwi = get_next_rwi(adapter);
3279 
3280 		/*
3281 		 * If there are no resets queued and the previous reset failed,
3282 		 * the adapter would be in an undefined state. So retry the
3283 		 * previous reset as a hard reset.
3284 		 *
3285 		 * Else, free the previous rwi and, if there is another reset
3286 		 * queued, process the new reset even if previous reset failed
3287 		 * (the previous reset could have failed because of a fail
3288 		 * over for instance, so process the fail over).
3289 		 */
3290 		if (!rwi && rc)
3291 			rwi = tmprwi;
3292 		else
3293 			kfree(tmprwi);
3294 
3295 		if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
3296 			    rwi->reset_reason == VNIC_RESET_MOBILITY || rc))
3297 			adapter->force_reset_recovery = true;
3298 	}
3299 
3300 	if (adapter->wait_for_reset) {
3301 		adapter->reset_done_rc = rc;
3302 		complete(&adapter->reset_done);
3303 	}
3304 
3305 	clear_bit_unlock(0, &adapter->resetting);
3306 
3307 	netdev_dbg(adapter->netdev,
3308 		   "[S:%s FRR:%d WFR:%d] Done processing resets\n",
3309 		   adapter_state_to_string(adapter->state),
3310 		   adapter->force_reset_recovery,
3311 		   adapter->wait_for_reset);
3312 }
3313 
3314 static void __ibmvnic_delayed_reset(struct work_struct *work)
3315 {
3316 	struct ibmvnic_adapter *adapter;
3317 
3318 	adapter = container_of(work, struct ibmvnic_adapter,
3319 			       ibmvnic_delayed_reset.work);
3320 	__ibmvnic_reset(&adapter->ibmvnic_reset);
3321 }
3322 
3323 static void flush_reset_queue(struct ibmvnic_adapter *adapter)
3324 {
3325 	struct list_head *entry, *tmp_entry;
3326 
3327 	if (!list_empty(&adapter->rwi_list)) {
3328 		list_for_each_safe(entry, tmp_entry, &adapter->rwi_list) {
3329 			list_del(entry);
3330 			kfree(list_entry(entry, struct ibmvnic_rwi, list));
3331 		}
3332 	}
3333 }
3334 
3335 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
3336 			 enum ibmvnic_reset_reason reason)
3337 {
3338 	struct net_device *netdev = adapter->netdev;
3339 	struct ibmvnic_rwi *rwi, *tmp;
3340 	unsigned long flags;
3341 	int ret;
3342 
3343 	spin_lock_irqsave(&adapter->rwi_lock, flags);
3344 
3345 	/* If failover is pending don't schedule any other reset.
3346 	 * Instead let the failover complete. If there is already a
3347 	 * a failover reset scheduled, we will detect and drop the
3348 	 * duplicate reset when walking the ->rwi_list below.
3349 	 */
3350 	if (adapter->state == VNIC_REMOVING ||
3351 	    adapter->state == VNIC_REMOVED ||
3352 	    (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
3353 		ret = EBUSY;
3354 		netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
3355 		goto err;
3356 	}
3357 
3358 	list_for_each_entry(tmp, &adapter->rwi_list, list) {
3359 		if (tmp->reset_reason == reason) {
3360 			netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
3361 				   reset_reason_to_string(reason));
3362 			ret = EBUSY;
3363 			goto err;
3364 		}
3365 	}
3366 
3367 	rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
3368 	if (!rwi) {
3369 		ret = ENOMEM;
3370 		goto err;
3371 	}
3372 	/* if we just received a transport event,
3373 	 * flush reset queue and process this reset
3374 	 */
3375 	if (adapter->force_reset_recovery)
3376 		flush_reset_queue(adapter);
3377 
3378 	rwi->reset_reason = reason;
3379 	list_add_tail(&rwi->list, &adapter->rwi_list);
3380 	netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
3381 		   reset_reason_to_string(reason));
3382 	queue_work(system_long_wq, &adapter->ibmvnic_reset);
3383 
3384 	ret = 0;
3385 err:
3386 	/* ibmvnic_close() below can block, so drop the lock first */
3387 	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
3388 
3389 	if (ret == ENOMEM)
3390 		ibmvnic_close(netdev);
3391 
3392 	return -ret;
3393 }
3394 
3395 static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
3396 {
3397 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3398 
3399 	if (test_bit(0, &adapter->resetting)) {
3400 		netdev_err(adapter->netdev,
3401 			   "Adapter is resetting, skip timeout reset\n");
3402 		return;
3403 	}
3404 	/* No queuing up reset until at least 5 seconds (default watchdog val)
3405 	 * after last reset
3406 	 */
3407 	if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
3408 		netdev_dbg(dev, "Not yet time to tx timeout.\n");
3409 		return;
3410 	}
3411 	ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
3412 }
3413 
3414 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
3415 				  struct ibmvnic_rx_buff *rx_buff)
3416 {
3417 	struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
3418 
3419 	rx_buff->skb = NULL;
3420 
3421 	pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
3422 	pool->next_alloc = (pool->next_alloc + 1) % pool->size;
3423 
3424 	atomic_dec(&pool->available);
3425 }
3426 
3427 static int ibmvnic_poll(struct napi_struct *napi, int budget)
3428 {
3429 	struct ibmvnic_sub_crq_queue *rx_scrq;
3430 	struct ibmvnic_adapter *adapter;
3431 	struct net_device *netdev;
3432 	int frames_processed;
3433 	int scrq_num;
3434 
3435 	netdev = napi->dev;
3436 	adapter = netdev_priv(netdev);
3437 	scrq_num = (int)(napi - adapter->napi);
3438 	frames_processed = 0;
3439 	rx_scrq = adapter->rx_scrq[scrq_num];
3440 
3441 restart_poll:
3442 	while (frames_processed < budget) {
3443 		struct sk_buff *skb;
3444 		struct ibmvnic_rx_buff *rx_buff;
3445 		union sub_crq *next;
3446 		u32 length;
3447 		u16 offset;
3448 		u8 flags = 0;
3449 
3450 		if (unlikely(test_bit(0, &adapter->resetting) &&
3451 			     adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
3452 			enable_scrq_irq(adapter, rx_scrq);
3453 			napi_complete_done(napi, frames_processed);
3454 			return frames_processed;
3455 		}
3456 
3457 		if (!pending_scrq(adapter, rx_scrq))
3458 			break;
3459 		next = ibmvnic_next_scrq(adapter, rx_scrq);
3460 		rx_buff = (struct ibmvnic_rx_buff *)
3461 			  be64_to_cpu(next->rx_comp.correlator);
3462 		/* do error checking */
3463 		if (next->rx_comp.rc) {
3464 			netdev_dbg(netdev, "rx buffer returned with rc %x\n",
3465 				   be16_to_cpu(next->rx_comp.rc));
3466 			/* free the entry */
3467 			next->rx_comp.first = 0;
3468 			dev_kfree_skb_any(rx_buff->skb);
3469 			remove_buff_from_pool(adapter, rx_buff);
3470 			continue;
3471 		} else if (!rx_buff->skb) {
3472 			/* free the entry */
3473 			next->rx_comp.first = 0;
3474 			remove_buff_from_pool(adapter, rx_buff);
3475 			continue;
3476 		}
3477 
3478 		length = be32_to_cpu(next->rx_comp.len);
3479 		offset = be16_to_cpu(next->rx_comp.off_frame_data);
3480 		flags = next->rx_comp.flags;
3481 		skb = rx_buff->skb;
3482 		/* load long_term_buff before copying to skb */
3483 		dma_rmb();
3484 		skb_copy_to_linear_data(skb, rx_buff->data + offset,
3485 					length);
3486 
3487 		/* VLAN Header has been stripped by the system firmware and
3488 		 * needs to be inserted by the driver
3489 		 */
3490 		if (adapter->rx_vlan_header_insertion &&
3491 		    (flags & IBMVNIC_VLAN_STRIPPED))
3492 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
3493 					       ntohs(next->rx_comp.vlan_tci));
3494 
3495 		/* free the entry */
3496 		next->rx_comp.first = 0;
3497 		remove_buff_from_pool(adapter, rx_buff);
3498 
3499 		skb_put(skb, length);
3500 		skb->protocol = eth_type_trans(skb, netdev);
3501 		skb_record_rx_queue(skb, scrq_num);
3502 
3503 		if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
3504 		    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
3505 			skb->ip_summed = CHECKSUM_UNNECESSARY;
3506 		}
3507 
3508 		length = skb->len;
3509 		napi_gro_receive(napi, skb); /* send it up */
3510 		netdev->stats.rx_packets++;
3511 		netdev->stats.rx_bytes += length;
3512 		adapter->rx_stats_buffers[scrq_num].packets++;
3513 		adapter->rx_stats_buffers[scrq_num].bytes += length;
3514 		frames_processed++;
3515 	}
3516 
3517 	if (adapter->state != VNIC_CLOSING &&
3518 	    ((atomic_read(&adapter->rx_pool[scrq_num].available) <
3519 	      adapter->req_rx_add_entries_per_subcrq / 2) ||
3520 	      frames_processed < budget))
3521 		replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
3522 	if (frames_processed < budget) {
3523 		if (napi_complete_done(napi, frames_processed)) {
3524 			enable_scrq_irq(adapter, rx_scrq);
3525 			if (pending_scrq(adapter, rx_scrq)) {
3526 				if (napi_schedule(napi)) {
3527 					disable_scrq_irq(adapter, rx_scrq);
3528 					goto restart_poll;
3529 				}
3530 			}
3531 		}
3532 	}
3533 	return frames_processed;
3534 }
3535 
3536 static int wait_for_reset(struct ibmvnic_adapter *adapter)
3537 {
3538 	int rc, ret;
3539 
3540 	adapter->fallback.mtu = adapter->req_mtu;
3541 	adapter->fallback.rx_queues = adapter->req_rx_queues;
3542 	adapter->fallback.tx_queues = adapter->req_tx_queues;
3543 	adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
3544 	adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
3545 
3546 	reinit_completion(&adapter->reset_done);
3547 	adapter->wait_for_reset = true;
3548 	rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
3549 
3550 	if (rc) {
3551 		ret = rc;
3552 		goto out;
3553 	}
3554 	rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
3555 	if (rc) {
3556 		ret = -ENODEV;
3557 		goto out;
3558 	}
3559 
3560 	ret = 0;
3561 	if (adapter->reset_done_rc) {
3562 		ret = -EIO;
3563 		adapter->desired.mtu = adapter->fallback.mtu;
3564 		adapter->desired.rx_queues = adapter->fallback.rx_queues;
3565 		adapter->desired.tx_queues = adapter->fallback.tx_queues;
3566 		adapter->desired.rx_entries = adapter->fallback.rx_entries;
3567 		adapter->desired.tx_entries = adapter->fallback.tx_entries;
3568 
3569 		reinit_completion(&adapter->reset_done);
3570 		adapter->wait_for_reset = true;
3571 		rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
3572 		if (rc) {
3573 			ret = rc;
3574 			goto out;
3575 		}
3576 		rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
3577 						 60000);
3578 		if (rc) {
3579 			ret = -ENODEV;
3580 			goto out;
3581 		}
3582 	}
3583 out:
3584 	adapter->wait_for_reset = false;
3585 
3586 	return ret;
3587 }
3588 
3589 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
3590 {
3591 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3592 
3593 	adapter->desired.mtu = new_mtu + ETH_HLEN;
3594 
3595 	return wait_for_reset(adapter);
3596 }
3597 
3598 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
3599 						struct net_device *dev,
3600 						netdev_features_t features)
3601 {
3602 	/* Some backing hardware adapters can not
3603 	 * handle packets with a MSS less than 224
3604 	 * or with only one segment.
3605 	 */
3606 	if (skb_is_gso(skb)) {
3607 		if (skb_shinfo(skb)->gso_size < 224 ||
3608 		    skb_shinfo(skb)->gso_segs == 1)
3609 			features &= ~NETIF_F_GSO_MASK;
3610 	}
3611 
3612 	return features;
3613 }
3614 
3615 static const struct net_device_ops ibmvnic_netdev_ops = {
3616 	.ndo_open		= ibmvnic_open,
3617 	.ndo_stop		= ibmvnic_close,
3618 	.ndo_start_xmit		= ibmvnic_xmit,
3619 	.ndo_set_rx_mode	= ibmvnic_set_multi,
3620 	.ndo_set_mac_address	= ibmvnic_set_mac,
3621 	.ndo_validate_addr	= eth_validate_addr,
3622 	.ndo_tx_timeout		= ibmvnic_tx_timeout,
3623 	.ndo_change_mtu		= ibmvnic_change_mtu,
3624 	.ndo_features_check     = ibmvnic_features_check,
3625 };
3626 
3627 /* ethtool functions */
3628 
3629 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
3630 				      struct ethtool_link_ksettings *cmd)
3631 {
3632 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3633 	int rc;
3634 
3635 	rc = send_query_phys_parms(adapter);
3636 	if (rc) {
3637 		adapter->speed = SPEED_UNKNOWN;
3638 		adapter->duplex = DUPLEX_UNKNOWN;
3639 	}
3640 	cmd->base.speed = adapter->speed;
3641 	cmd->base.duplex = adapter->duplex;
3642 	cmd->base.port = PORT_FIBRE;
3643 	cmd->base.phy_address = 0;
3644 	cmd->base.autoneg = AUTONEG_ENABLE;
3645 
3646 	return 0;
3647 }
3648 
3649 static void ibmvnic_get_drvinfo(struct net_device *netdev,
3650 				struct ethtool_drvinfo *info)
3651 {
3652 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3653 
3654 	strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
3655 	strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
3656 	strscpy(info->fw_version, adapter->fw_version,
3657 		sizeof(info->fw_version));
3658 }
3659 
3660 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
3661 {
3662 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3663 
3664 	return adapter->msg_enable;
3665 }
3666 
3667 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
3668 {
3669 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3670 
3671 	adapter->msg_enable = data;
3672 }
3673 
3674 static u32 ibmvnic_get_link(struct net_device *netdev)
3675 {
3676 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3677 
3678 	/* Don't need to send a query because we request a logical link up at
3679 	 * init and then we wait for link state indications
3680 	 */
3681 	return adapter->logical_link_state;
3682 }
3683 
3684 static void ibmvnic_get_ringparam(struct net_device *netdev,
3685 				  struct ethtool_ringparam *ring,
3686 				  struct kernel_ethtool_ringparam *kernel_ring,
3687 				  struct netlink_ext_ack *extack)
3688 {
3689 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3690 
3691 	ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
3692 	ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
3693 	ring->rx_mini_max_pending = 0;
3694 	ring->rx_jumbo_max_pending = 0;
3695 	ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
3696 	ring->tx_pending = adapter->req_tx_entries_per_subcrq;
3697 	ring->rx_mini_pending = 0;
3698 	ring->rx_jumbo_pending = 0;
3699 }
3700 
3701 static int ibmvnic_set_ringparam(struct net_device *netdev,
3702 				 struct ethtool_ringparam *ring,
3703 				 struct kernel_ethtool_ringparam *kernel_ring,
3704 				 struct netlink_ext_ack *extack)
3705 {
3706 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3707 
3708 	if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq  ||
3709 	    ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
3710 		netdev_err(netdev, "Invalid request.\n");
3711 		netdev_err(netdev, "Max tx buffers = %llu\n",
3712 			   adapter->max_rx_add_entries_per_subcrq);
3713 		netdev_err(netdev, "Max rx buffers = %llu\n",
3714 			   adapter->max_tx_entries_per_subcrq);
3715 		return -EINVAL;
3716 	}
3717 
3718 	adapter->desired.rx_entries = ring->rx_pending;
3719 	adapter->desired.tx_entries = ring->tx_pending;
3720 
3721 	return wait_for_reset(adapter);
3722 }
3723 
3724 static void ibmvnic_get_channels(struct net_device *netdev,
3725 				 struct ethtool_channels *channels)
3726 {
3727 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3728 
3729 	channels->max_rx = adapter->max_rx_queues;
3730 	channels->max_tx = adapter->max_tx_queues;
3731 	channels->max_other = 0;
3732 	channels->max_combined = 0;
3733 	channels->rx_count = adapter->req_rx_queues;
3734 	channels->tx_count = adapter->req_tx_queues;
3735 	channels->other_count = 0;
3736 	channels->combined_count = 0;
3737 }
3738 
3739 static int ibmvnic_set_channels(struct net_device *netdev,
3740 				struct ethtool_channels *channels)
3741 {
3742 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3743 
3744 	adapter->desired.rx_queues = channels->rx_count;
3745 	adapter->desired.tx_queues = channels->tx_count;
3746 
3747 	return wait_for_reset(adapter);
3748 }
3749 
3750 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3751 {
3752 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3753 	int i;
3754 
3755 	if (stringset != ETH_SS_STATS)
3756 		return;
3757 
3758 	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
3759 		memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
3760 
3761 	for (i = 0; i < adapter->req_tx_queues; i++) {
3762 		snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
3763 		data += ETH_GSTRING_LEN;
3764 
3765 		snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
3766 		data += ETH_GSTRING_LEN;
3767 
3768 		snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
3769 		data += ETH_GSTRING_LEN;
3770 	}
3771 
3772 	for (i = 0; i < adapter->req_rx_queues; i++) {
3773 		snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
3774 		data += ETH_GSTRING_LEN;
3775 
3776 		snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
3777 		data += ETH_GSTRING_LEN;
3778 
3779 		snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
3780 		data += ETH_GSTRING_LEN;
3781 	}
3782 }
3783 
3784 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
3785 {
3786 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3787 
3788 	switch (sset) {
3789 	case ETH_SS_STATS:
3790 		return ARRAY_SIZE(ibmvnic_stats) +
3791 		       adapter->req_tx_queues * NUM_TX_STATS +
3792 		       adapter->req_rx_queues * NUM_RX_STATS;
3793 	default:
3794 		return -EOPNOTSUPP;
3795 	}
3796 }
3797 
3798 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
3799 				      struct ethtool_stats *stats, u64 *data)
3800 {
3801 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
3802 	union ibmvnic_crq crq;
3803 	int i, j;
3804 	int rc;
3805 
3806 	memset(&crq, 0, sizeof(crq));
3807 	crq.request_statistics.first = IBMVNIC_CRQ_CMD;
3808 	crq.request_statistics.cmd = REQUEST_STATISTICS;
3809 	crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
3810 	crq.request_statistics.len =
3811 	    cpu_to_be32(sizeof(struct ibmvnic_statistics));
3812 
3813 	/* Wait for data to be written */
3814 	reinit_completion(&adapter->stats_done);
3815 	rc = ibmvnic_send_crq(adapter, &crq);
3816 	if (rc)
3817 		return;
3818 	rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
3819 	if (rc)
3820 		return;
3821 
3822 	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
3823 		data[i] = be64_to_cpu(IBMVNIC_GET_STAT
3824 				      (adapter, ibmvnic_stats[i].offset));
3825 
3826 	for (j = 0; j < adapter->req_tx_queues; j++) {
3827 		data[i] = adapter->tx_stats_buffers[j].packets;
3828 		i++;
3829 		data[i] = adapter->tx_stats_buffers[j].bytes;
3830 		i++;
3831 		data[i] = adapter->tx_stats_buffers[j].dropped_packets;
3832 		i++;
3833 	}
3834 
3835 	for (j = 0; j < adapter->req_rx_queues; j++) {
3836 		data[i] = adapter->rx_stats_buffers[j].packets;
3837 		i++;
3838 		data[i] = adapter->rx_stats_buffers[j].bytes;
3839 		i++;
3840 		data[i] = adapter->rx_stats_buffers[j].interrupts;
3841 		i++;
3842 	}
3843 }
3844 
3845 static const struct ethtool_ops ibmvnic_ethtool_ops = {
3846 	.get_drvinfo		= ibmvnic_get_drvinfo,
3847 	.get_msglevel		= ibmvnic_get_msglevel,
3848 	.set_msglevel		= ibmvnic_set_msglevel,
3849 	.get_link		= ibmvnic_get_link,
3850 	.get_ringparam		= ibmvnic_get_ringparam,
3851 	.set_ringparam		= ibmvnic_set_ringparam,
3852 	.get_channels		= ibmvnic_get_channels,
3853 	.set_channels		= ibmvnic_set_channels,
3854 	.get_strings            = ibmvnic_get_strings,
3855 	.get_sset_count         = ibmvnic_get_sset_count,
3856 	.get_ethtool_stats	= ibmvnic_get_ethtool_stats,
3857 	.get_link_ksettings	= ibmvnic_get_link_ksettings,
3858 };
3859 
3860 /* Routines for managing CRQs/sCRQs  */
3861 
3862 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3863 				   struct ibmvnic_sub_crq_queue *scrq)
3864 {
3865 	int rc;
3866 
3867 	if (!scrq) {
3868 		netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
3869 		return -EINVAL;
3870 	}
3871 
3872 	if (scrq->irq) {
3873 		free_irq(scrq->irq, scrq);
3874 		irq_dispose_mapping(scrq->irq);
3875 		scrq->irq = 0;
3876 	}
3877 
3878 	if (scrq->msgs) {
3879 		memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3880 		atomic_set(&scrq->used, 0);
3881 		scrq->cur = 0;
3882 		scrq->ind_buf.index = 0;
3883 	} else {
3884 		netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3885 		return -EINVAL;
3886 	}
3887 
3888 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3889 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3890 	return rc;
3891 }
3892 
3893 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3894 {
3895 	int i, rc;
3896 
3897 	if (!adapter->tx_scrq || !adapter->rx_scrq)
3898 		return -EINVAL;
3899 
3900 	ibmvnic_clean_affinity(adapter);
3901 
3902 	for (i = 0; i < adapter->req_tx_queues; i++) {
3903 		netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
3904 		rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3905 		if (rc)
3906 			return rc;
3907 	}
3908 
3909 	for (i = 0; i < adapter->req_rx_queues; i++) {
3910 		netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
3911 		rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3912 		if (rc)
3913 			return rc;
3914 	}
3915 
3916 	return rc;
3917 }
3918 
3919 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
3920 				  struct ibmvnic_sub_crq_queue *scrq,
3921 				  bool do_h_free)
3922 {
3923 	struct device *dev = &adapter->vdev->dev;
3924 	long rc;
3925 
3926 	netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3927 
3928 	if (do_h_free) {
3929 		/* Close the sub-crqs */
3930 		do {
3931 			rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3932 						adapter->vdev->unit_address,
3933 						scrq->crq_num);
3934 		} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3935 
3936 		if (rc) {
3937 			netdev_err(adapter->netdev,
3938 				   "Failed to release sub-CRQ %16lx, rc = %ld\n",
3939 				   scrq->crq_num, rc);
3940 		}
3941 	}
3942 
3943 	dma_free_coherent(dev,
3944 			  IBMVNIC_IND_ARR_SZ,
3945 			  scrq->ind_buf.indir_arr,
3946 			  scrq->ind_buf.indir_dma);
3947 
3948 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3949 			 DMA_BIDIRECTIONAL);
3950 	free_pages((unsigned long)scrq->msgs, 2);
3951 	free_cpumask_var(scrq->affinity_mask);
3952 	kfree(scrq);
3953 }
3954 
3955 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3956 							*adapter)
3957 {
3958 	struct device *dev = &adapter->vdev->dev;
3959 	struct ibmvnic_sub_crq_queue *scrq;
3960 	int rc;
3961 
3962 	scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
3963 	if (!scrq)
3964 		return NULL;
3965 
3966 	scrq->msgs =
3967 		(union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
3968 	if (!scrq->msgs) {
3969 		dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3970 		goto zero_page_failed;
3971 	}
3972 	if (!zalloc_cpumask_var(&scrq->affinity_mask, GFP_KERNEL))
3973 		goto cpumask_alloc_failed;
3974 
3975 	scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3976 					 DMA_BIDIRECTIONAL);
3977 	if (dma_mapping_error(dev, scrq->msg_token)) {
3978 		dev_warn(dev, "Couldn't map crq queue messages page\n");
3979 		goto map_failed;
3980 	}
3981 
3982 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3983 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3984 
3985 	if (rc == H_RESOURCE)
3986 		rc = ibmvnic_reset_crq(adapter);
3987 
3988 	if (rc == H_CLOSED) {
3989 		dev_warn(dev, "Partner adapter not ready, waiting.\n");
3990 	} else if (rc) {
3991 		dev_warn(dev, "Error %d registering sub-crq\n", rc);
3992 		goto reg_failed;
3993 	}
3994 
3995 	scrq->adapter = adapter;
3996 	scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
3997 	scrq->ind_buf.index = 0;
3998 
3999 	scrq->ind_buf.indir_arr =
4000 		dma_alloc_coherent(dev,
4001 				   IBMVNIC_IND_ARR_SZ,
4002 				   &scrq->ind_buf.indir_dma,
4003 				   GFP_KERNEL);
4004 
4005 	if (!scrq->ind_buf.indir_arr)
4006 		goto indir_failed;
4007 
4008 	spin_lock_init(&scrq->lock);
4009 
4010 	netdev_dbg(adapter->netdev,
4011 		   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
4012 		   scrq->crq_num, scrq->hw_irq, scrq->irq);
4013 
4014 	return scrq;
4015 
4016 indir_failed:
4017 	do {
4018 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
4019 					adapter->vdev->unit_address,
4020 					scrq->crq_num);
4021 	} while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
4022 reg_failed:
4023 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
4024 			 DMA_BIDIRECTIONAL);
4025 map_failed:
4026 	free_cpumask_var(scrq->affinity_mask);
4027 cpumask_alloc_failed:
4028 	free_pages((unsigned long)scrq->msgs, 2);
4029 zero_page_failed:
4030 	kfree(scrq);
4031 
4032 	return NULL;
4033 }
4034 
4035 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
4036 {
4037 	int i;
4038 
4039 	ibmvnic_clean_affinity(adapter);
4040 	if (adapter->tx_scrq) {
4041 		for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
4042 			if (!adapter->tx_scrq[i])
4043 				continue;
4044 
4045 			netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
4046 				   i);
4047 			ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
4048 			if (adapter->tx_scrq[i]->irq) {
4049 				free_irq(adapter->tx_scrq[i]->irq,
4050 					 adapter->tx_scrq[i]);
4051 				irq_dispose_mapping(adapter->tx_scrq[i]->irq);
4052 				adapter->tx_scrq[i]->irq = 0;
4053 			}
4054 
4055 			release_sub_crq_queue(adapter, adapter->tx_scrq[i],
4056 					      do_h_free);
4057 		}
4058 
4059 		kfree(adapter->tx_scrq);
4060 		adapter->tx_scrq = NULL;
4061 		adapter->num_active_tx_scrqs = 0;
4062 	}
4063 
4064 	if (adapter->rx_scrq) {
4065 		for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
4066 			if (!adapter->rx_scrq[i])
4067 				continue;
4068 
4069 			netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
4070 				   i);
4071 			if (adapter->rx_scrq[i]->irq) {
4072 				free_irq(adapter->rx_scrq[i]->irq,
4073 					 adapter->rx_scrq[i]);
4074 				irq_dispose_mapping(adapter->rx_scrq[i]->irq);
4075 				adapter->rx_scrq[i]->irq = 0;
4076 			}
4077 
4078 			release_sub_crq_queue(adapter, adapter->rx_scrq[i],
4079 					      do_h_free);
4080 		}
4081 
4082 		kfree(adapter->rx_scrq);
4083 		adapter->rx_scrq = NULL;
4084 		adapter->num_active_rx_scrqs = 0;
4085 	}
4086 }
4087 
4088 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
4089 			    struct ibmvnic_sub_crq_queue *scrq)
4090 {
4091 	struct device *dev = &adapter->vdev->dev;
4092 	unsigned long rc;
4093 
4094 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4095 				H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
4096 	if (rc)
4097 		dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
4098 			scrq->hw_irq, rc);
4099 	return rc;
4100 }
4101 
4102 /* We can not use the IRQ chip EOI handler because that has the
4103  * unintended effect of changing the interrupt priority.
4104  */
4105 static void ibmvnic_xics_eoi(struct device *dev, struct ibmvnic_sub_crq_queue *scrq)
4106 {
4107 	u64 val = 0xff000000 | scrq->hw_irq;
4108 	unsigned long rc;
4109 
4110 	rc = plpar_hcall_norets(H_EOI, val);
4111 	if (rc)
4112 		dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n", val, rc);
4113 }
4114 
4115 /* Due to a firmware bug, the hypervisor can send an interrupt to a
4116  * transmit or receive queue just prior to a partition migration.
4117  * Force an EOI after migration.
4118  */
4119 static void ibmvnic_clear_pending_interrupt(struct device *dev,
4120 					    struct ibmvnic_sub_crq_queue *scrq)
4121 {
4122 	if (!xive_enabled())
4123 		ibmvnic_xics_eoi(dev, scrq);
4124 }
4125 
4126 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
4127 			   struct ibmvnic_sub_crq_queue *scrq)
4128 {
4129 	struct device *dev = &adapter->vdev->dev;
4130 	unsigned long rc;
4131 
4132 	if (scrq->hw_irq > 0x100000000ULL) {
4133 		dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
4134 		return 1;
4135 	}
4136 
4137 	if (test_bit(0, &adapter->resetting) &&
4138 	    adapter->reset_reason == VNIC_RESET_MOBILITY) {
4139 		ibmvnic_clear_pending_interrupt(dev, scrq);
4140 	}
4141 
4142 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4143 				H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
4144 	if (rc)
4145 		dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
4146 			scrq->hw_irq, rc);
4147 	return rc;
4148 }
4149 
4150 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
4151 			       struct ibmvnic_sub_crq_queue *scrq)
4152 {
4153 	struct device *dev = &adapter->vdev->dev;
4154 	struct ibmvnic_tx_pool *tx_pool;
4155 	struct ibmvnic_tx_buff *txbuff;
4156 	struct netdev_queue *txq;
4157 	union sub_crq *next;
4158 	int index;
4159 	int i;
4160 
4161 restart_loop:
4162 	while (pending_scrq(adapter, scrq)) {
4163 		unsigned int pool = scrq->pool_index;
4164 		int num_entries = 0;
4165 		int total_bytes = 0;
4166 		int num_packets = 0;
4167 
4168 		next = ibmvnic_next_scrq(adapter, scrq);
4169 		for (i = 0; i < next->tx_comp.num_comps; i++) {
4170 			index = be32_to_cpu(next->tx_comp.correlators[i]);
4171 			if (index & IBMVNIC_TSO_POOL_MASK) {
4172 				tx_pool = &adapter->tso_pool[pool];
4173 				index &= ~IBMVNIC_TSO_POOL_MASK;
4174 			} else {
4175 				tx_pool = &adapter->tx_pool[pool];
4176 			}
4177 
4178 			txbuff = &tx_pool->tx_buff[index];
4179 			num_packets++;
4180 			num_entries += txbuff->num_entries;
4181 			if (txbuff->skb) {
4182 				total_bytes += txbuff->skb->len;
4183 				if (next->tx_comp.rcs[i]) {
4184 					dev_err(dev, "tx error %x\n",
4185 						next->tx_comp.rcs[i]);
4186 					dev_kfree_skb_irq(txbuff->skb);
4187 				} else {
4188 					dev_consume_skb_irq(txbuff->skb);
4189 				}
4190 				txbuff->skb = NULL;
4191 			} else {
4192 				netdev_warn(adapter->netdev,
4193 					    "TX completion received with NULL socket buffer\n");
4194 			}
4195 			tx_pool->free_map[tx_pool->producer_index] = index;
4196 			tx_pool->producer_index =
4197 				(tx_pool->producer_index + 1) %
4198 					tx_pool->num_buffers;
4199 		}
4200 		/* remove tx_comp scrq*/
4201 		next->tx_comp.first = 0;
4202 
4203 		txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
4204 		netdev_tx_completed_queue(txq, num_packets, total_bytes);
4205 
4206 		if (atomic_sub_return(num_entries, &scrq->used) <=
4207 		    (adapter->req_tx_entries_per_subcrq / 2) &&
4208 		    __netif_subqueue_stopped(adapter->netdev,
4209 					     scrq->pool_index)) {
4210 			rcu_read_lock();
4211 			if (adapter->tx_queues_active) {
4212 				netif_wake_subqueue(adapter->netdev,
4213 						    scrq->pool_index);
4214 				netdev_dbg(adapter->netdev,
4215 					   "Started queue %d\n",
4216 					   scrq->pool_index);
4217 			}
4218 			rcu_read_unlock();
4219 		}
4220 	}
4221 
4222 	enable_scrq_irq(adapter, scrq);
4223 
4224 	if (pending_scrq(adapter, scrq)) {
4225 		disable_scrq_irq(adapter, scrq);
4226 		goto restart_loop;
4227 	}
4228 
4229 	return 0;
4230 }
4231 
4232 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
4233 {
4234 	struct ibmvnic_sub_crq_queue *scrq = instance;
4235 	struct ibmvnic_adapter *adapter = scrq->adapter;
4236 
4237 	disable_scrq_irq(adapter, scrq);
4238 	ibmvnic_complete_tx(adapter, scrq);
4239 
4240 	return IRQ_HANDLED;
4241 }
4242 
4243 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
4244 {
4245 	struct ibmvnic_sub_crq_queue *scrq = instance;
4246 	struct ibmvnic_adapter *adapter = scrq->adapter;
4247 
4248 	/* When booting a kdump kernel we can hit pending interrupts
4249 	 * prior to completing driver initialization.
4250 	 */
4251 	if (unlikely(adapter->state != VNIC_OPEN))
4252 		return IRQ_NONE;
4253 
4254 	adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
4255 
4256 	if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
4257 		disable_scrq_irq(adapter, scrq);
4258 		__napi_schedule(&adapter->napi[scrq->scrq_num]);
4259 	}
4260 
4261 	return IRQ_HANDLED;
4262 }
4263 
4264 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
4265 {
4266 	struct device *dev = &adapter->vdev->dev;
4267 	struct ibmvnic_sub_crq_queue *scrq;
4268 	int i = 0, j = 0;
4269 	int rc = 0;
4270 
4271 	for (i = 0; i < adapter->req_tx_queues; i++) {
4272 		netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
4273 			   i);
4274 		scrq = adapter->tx_scrq[i];
4275 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
4276 
4277 		if (!scrq->irq) {
4278 			rc = -EINVAL;
4279 			dev_err(dev, "Error mapping irq\n");
4280 			goto req_tx_irq_failed;
4281 		}
4282 
4283 		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
4284 			 adapter->vdev->unit_address, i);
4285 		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
4286 				 0, scrq->name, scrq);
4287 
4288 		if (rc) {
4289 			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
4290 				scrq->irq, rc);
4291 			irq_dispose_mapping(scrq->irq);
4292 			goto req_tx_irq_failed;
4293 		}
4294 	}
4295 
4296 	for (i = 0; i < adapter->req_rx_queues; i++) {
4297 		netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
4298 			   i);
4299 		scrq = adapter->rx_scrq[i];
4300 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
4301 		if (!scrq->irq) {
4302 			rc = -EINVAL;
4303 			dev_err(dev, "Error mapping irq\n");
4304 			goto req_rx_irq_failed;
4305 		}
4306 		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
4307 			 adapter->vdev->unit_address, i);
4308 		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
4309 				 0, scrq->name, scrq);
4310 		if (rc) {
4311 			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
4312 				scrq->irq, rc);
4313 			irq_dispose_mapping(scrq->irq);
4314 			goto req_rx_irq_failed;
4315 		}
4316 	}
4317 
4318 	cpus_read_lock();
4319 	ibmvnic_set_affinity(adapter);
4320 	cpus_read_unlock();
4321 
4322 	return rc;
4323 
4324 req_rx_irq_failed:
4325 	for (j = 0; j < i; j++) {
4326 		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
4327 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
4328 	}
4329 	i = adapter->req_tx_queues;
4330 req_tx_irq_failed:
4331 	for (j = 0; j < i; j++) {
4332 		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
4333 		irq_dispose_mapping(adapter->tx_scrq[j]->irq);
4334 	}
4335 	release_sub_crqs(adapter, 1);
4336 	return rc;
4337 }
4338 
4339 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
4340 {
4341 	struct device *dev = &adapter->vdev->dev;
4342 	struct ibmvnic_sub_crq_queue **allqueues;
4343 	int registered_queues = 0;
4344 	int total_queues;
4345 	int more = 0;
4346 	int i;
4347 
4348 	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
4349 
4350 	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
4351 	if (!allqueues)
4352 		return -ENOMEM;
4353 
4354 	for (i = 0; i < total_queues; i++) {
4355 		allqueues[i] = init_sub_crq_queue(adapter);
4356 		if (!allqueues[i]) {
4357 			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
4358 			break;
4359 		}
4360 		registered_queues++;
4361 	}
4362 
4363 	/* Make sure we were able to register the minimum number of queues */
4364 	if (registered_queues <
4365 	    adapter->min_tx_queues + adapter->min_rx_queues) {
4366 		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
4367 		goto tx_failed;
4368 	}
4369 
4370 	/* Distribute the failed allocated queues*/
4371 	for (i = 0; i < total_queues - registered_queues + more ; i++) {
4372 		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
4373 		switch (i % 3) {
4374 		case 0:
4375 			if (adapter->req_rx_queues > adapter->min_rx_queues)
4376 				adapter->req_rx_queues--;
4377 			else
4378 				more++;
4379 			break;
4380 		case 1:
4381 			if (adapter->req_tx_queues > adapter->min_tx_queues)
4382 				adapter->req_tx_queues--;
4383 			else
4384 				more++;
4385 			break;
4386 		}
4387 	}
4388 
4389 	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
4390 				   sizeof(*adapter->tx_scrq), GFP_KERNEL);
4391 	if (!adapter->tx_scrq)
4392 		goto tx_failed;
4393 
4394 	for (i = 0; i < adapter->req_tx_queues; i++) {
4395 		adapter->tx_scrq[i] = allqueues[i];
4396 		adapter->tx_scrq[i]->pool_index = i;
4397 		adapter->num_active_tx_scrqs++;
4398 	}
4399 
4400 	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
4401 				   sizeof(*adapter->rx_scrq), GFP_KERNEL);
4402 	if (!adapter->rx_scrq)
4403 		goto rx_failed;
4404 
4405 	for (i = 0; i < adapter->req_rx_queues; i++) {
4406 		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
4407 		adapter->rx_scrq[i]->scrq_num = i;
4408 		adapter->num_active_rx_scrqs++;
4409 	}
4410 
4411 	kfree(allqueues);
4412 	return 0;
4413 
4414 rx_failed:
4415 	kfree(adapter->tx_scrq);
4416 	adapter->tx_scrq = NULL;
4417 tx_failed:
4418 	for (i = 0; i < registered_queues; i++)
4419 		release_sub_crq_queue(adapter, allqueues[i], 1);
4420 	kfree(allqueues);
4421 	return -ENOMEM;
4422 }
4423 
4424 static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
4425 {
4426 	struct device *dev = &adapter->vdev->dev;
4427 	union ibmvnic_crq crq;
4428 	int max_entries;
4429 	int cap_reqs;
4430 
4431 	/* We send out 6 or 7 REQUEST_CAPABILITY CRQs below (depending on
4432 	 * the PROMISC flag). Initialize this count upfront. When the tasklet
4433 	 * receives a response to all of these, it will send the next protocol
4434 	 * message (QUERY_IP_OFFLOAD).
4435 	 */
4436 	if (!(adapter->netdev->flags & IFF_PROMISC) ||
4437 	    adapter->promisc_supported)
4438 		cap_reqs = 7;
4439 	else
4440 		cap_reqs = 6;
4441 
4442 	if (!retry) {
4443 		/* Sub-CRQ entries are 32 byte long */
4444 		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
4445 
4446 		atomic_set(&adapter->running_cap_crqs, cap_reqs);
4447 
4448 		if (adapter->min_tx_entries_per_subcrq > entries_page ||
4449 		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
4450 			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
4451 			return;
4452 		}
4453 
4454 		if (adapter->desired.mtu)
4455 			adapter->req_mtu = adapter->desired.mtu;
4456 		else
4457 			adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
4458 
4459 		if (!adapter->desired.tx_entries)
4460 			adapter->desired.tx_entries =
4461 					adapter->max_tx_entries_per_subcrq;
4462 		if (!adapter->desired.rx_entries)
4463 			adapter->desired.rx_entries =
4464 					adapter->max_rx_add_entries_per_subcrq;
4465 
4466 		max_entries = IBMVNIC_LTB_SET_SIZE /
4467 			      (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
4468 
4469 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
4470 			adapter->desired.tx_entries > IBMVNIC_LTB_SET_SIZE) {
4471 			adapter->desired.tx_entries = max_entries;
4472 		}
4473 
4474 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
4475 			adapter->desired.rx_entries > IBMVNIC_LTB_SET_SIZE) {
4476 			adapter->desired.rx_entries = max_entries;
4477 		}
4478 
4479 		if (adapter->desired.tx_entries)
4480 			adapter->req_tx_entries_per_subcrq =
4481 					adapter->desired.tx_entries;
4482 		else
4483 			adapter->req_tx_entries_per_subcrq =
4484 					adapter->max_tx_entries_per_subcrq;
4485 
4486 		if (adapter->desired.rx_entries)
4487 			adapter->req_rx_add_entries_per_subcrq =
4488 					adapter->desired.rx_entries;
4489 		else
4490 			adapter->req_rx_add_entries_per_subcrq =
4491 					adapter->max_rx_add_entries_per_subcrq;
4492 
4493 		if (adapter->desired.tx_queues)
4494 			adapter->req_tx_queues =
4495 					adapter->desired.tx_queues;
4496 		else
4497 			adapter->req_tx_queues =
4498 					adapter->opt_tx_comp_sub_queues;
4499 
4500 		if (adapter->desired.rx_queues)
4501 			adapter->req_rx_queues =
4502 					adapter->desired.rx_queues;
4503 		else
4504 			adapter->req_rx_queues =
4505 					adapter->opt_rx_comp_queues;
4506 
4507 		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
4508 	} else {
4509 		atomic_add(cap_reqs, &adapter->running_cap_crqs);
4510 	}
4511 	memset(&crq, 0, sizeof(crq));
4512 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
4513 	crq.request_capability.cmd = REQUEST_CAPABILITY;
4514 
4515 	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
4516 	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
4517 	cap_reqs--;
4518 	ibmvnic_send_crq(adapter, &crq);
4519 
4520 	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
4521 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
4522 	cap_reqs--;
4523 	ibmvnic_send_crq(adapter, &crq);
4524 
4525 	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
4526 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
4527 	cap_reqs--;
4528 	ibmvnic_send_crq(adapter, &crq);
4529 
4530 	crq.request_capability.capability =
4531 	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
4532 	crq.request_capability.number =
4533 	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
4534 	cap_reqs--;
4535 	ibmvnic_send_crq(adapter, &crq);
4536 
4537 	crq.request_capability.capability =
4538 	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
4539 	crq.request_capability.number =
4540 	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
4541 	cap_reqs--;
4542 	ibmvnic_send_crq(adapter, &crq);
4543 
4544 	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
4545 	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
4546 	cap_reqs--;
4547 	ibmvnic_send_crq(adapter, &crq);
4548 
4549 	if (adapter->netdev->flags & IFF_PROMISC) {
4550 		if (adapter->promisc_supported) {
4551 			crq.request_capability.capability =
4552 			    cpu_to_be16(PROMISC_REQUESTED);
4553 			crq.request_capability.number = cpu_to_be64(1);
4554 			cap_reqs--;
4555 			ibmvnic_send_crq(adapter, &crq);
4556 		}
4557 	} else {
4558 		crq.request_capability.capability =
4559 		    cpu_to_be16(PROMISC_REQUESTED);
4560 		crq.request_capability.number = cpu_to_be64(0);
4561 		cap_reqs--;
4562 		ibmvnic_send_crq(adapter, &crq);
4563 	}
4564 
4565 	/* Keep at end to catch any discrepancy between expected and actual
4566 	 * CRQs sent.
4567 	 */
4568 	WARN_ON(cap_reqs != 0);
4569 }
4570 
4571 static int pending_scrq(struct ibmvnic_adapter *adapter,
4572 			struct ibmvnic_sub_crq_queue *scrq)
4573 {
4574 	union sub_crq *entry = &scrq->msgs[scrq->cur];
4575 	int rc;
4576 
4577 	rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
4578 
4579 	/* Ensure that the SCRQ valid flag is loaded prior to loading the
4580 	 * contents of the SCRQ descriptor
4581 	 */
4582 	dma_rmb();
4583 
4584 	return rc;
4585 }
4586 
4587 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
4588 					struct ibmvnic_sub_crq_queue *scrq)
4589 {
4590 	union sub_crq *entry;
4591 	unsigned long flags;
4592 
4593 	spin_lock_irqsave(&scrq->lock, flags);
4594 	entry = &scrq->msgs[scrq->cur];
4595 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4596 		if (++scrq->cur == scrq->size)
4597 			scrq->cur = 0;
4598 	} else {
4599 		entry = NULL;
4600 	}
4601 	spin_unlock_irqrestore(&scrq->lock, flags);
4602 
4603 	/* Ensure that the SCRQ valid flag is loaded prior to loading the
4604 	 * contents of the SCRQ descriptor
4605 	 */
4606 	dma_rmb();
4607 
4608 	return entry;
4609 }
4610 
4611 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
4612 {
4613 	struct ibmvnic_crq_queue *queue = &adapter->crq;
4614 	union ibmvnic_crq *crq;
4615 
4616 	crq = &queue->msgs[queue->cur];
4617 	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4618 		if (++queue->cur == queue->size)
4619 			queue->cur = 0;
4620 	} else {
4621 		crq = NULL;
4622 	}
4623 
4624 	return crq;
4625 }
4626 
4627 static void print_subcrq_error(struct device *dev, int rc, const char *func)
4628 {
4629 	switch (rc) {
4630 	case H_PARAMETER:
4631 		dev_warn_ratelimited(dev,
4632 				     "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
4633 				     func, rc);
4634 		break;
4635 	case H_CLOSED:
4636 		dev_warn_ratelimited(dev,
4637 				     "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
4638 				     func, rc);
4639 		break;
4640 	default:
4641 		dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
4642 		break;
4643 	}
4644 }
4645 
4646 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
4647 				u64 remote_handle, u64 ioba, u64 num_entries)
4648 {
4649 	unsigned int ua = adapter->vdev->unit_address;
4650 	struct device *dev = &adapter->vdev->dev;
4651 	int rc;
4652 
4653 	/* Make sure the hypervisor sees the complete request */
4654 	dma_wmb();
4655 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
4656 				cpu_to_be64(remote_handle),
4657 				ioba, num_entries);
4658 
4659 	if (rc)
4660 		print_subcrq_error(dev, rc, __func__);
4661 
4662 	return rc;
4663 }
4664 
4665 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
4666 			    union ibmvnic_crq *crq)
4667 {
4668 	unsigned int ua = adapter->vdev->unit_address;
4669 	struct device *dev = &adapter->vdev->dev;
4670 	u64 *u64_crq = (u64 *)crq;
4671 	int rc;
4672 
4673 	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
4674 		   (unsigned long)cpu_to_be64(u64_crq[0]),
4675 		   (unsigned long)cpu_to_be64(u64_crq[1]));
4676 
4677 	if (!adapter->crq.active &&
4678 	    crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
4679 		dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
4680 		return -EINVAL;
4681 	}
4682 
4683 	/* Make sure the hypervisor sees the complete request */
4684 	dma_wmb();
4685 
4686 	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
4687 				cpu_to_be64(u64_crq[0]),
4688 				cpu_to_be64(u64_crq[1]));
4689 
4690 	if (rc) {
4691 		if (rc == H_CLOSED) {
4692 			dev_warn(dev, "CRQ Queue closed\n");
4693 			/* do not reset, report the fail, wait for passive init from server */
4694 		}
4695 
4696 		dev_warn(dev, "Send error (rc=%d)\n", rc);
4697 	}
4698 
4699 	return rc;
4700 }
4701 
4702 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
4703 {
4704 	struct device *dev = &adapter->vdev->dev;
4705 	union ibmvnic_crq crq;
4706 	int retries = 100;
4707 	int rc;
4708 
4709 	memset(&crq, 0, sizeof(crq));
4710 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
4711 	crq.generic.cmd = IBMVNIC_CRQ_INIT;
4712 	netdev_dbg(adapter->netdev, "Sending CRQ init\n");
4713 
4714 	do {
4715 		rc = ibmvnic_send_crq(adapter, &crq);
4716 		if (rc != H_CLOSED)
4717 			break;
4718 		retries--;
4719 		msleep(50);
4720 
4721 	} while (retries > 0);
4722 
4723 	if (rc) {
4724 		dev_err(dev, "Failed to send init request, rc = %d\n", rc);
4725 		return rc;
4726 	}
4727 
4728 	return 0;
4729 }
4730 
4731 struct vnic_login_client_data {
4732 	u8	type;
4733 	__be16	len;
4734 	char	name[];
4735 } __packed;
4736 
4737 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
4738 {
4739 	int len;
4740 
4741 	/* Calculate the amount of buffer space needed for the
4742 	 * vnic client data in the login buffer. There are four entries,
4743 	 * OS name, LPAR name, device name, and a null last entry.
4744 	 */
4745 	len = 4 * sizeof(struct vnic_login_client_data);
4746 	len += 6; /* "Linux" plus NULL */
4747 	len += strlen(utsname()->nodename) + 1;
4748 	len += strlen(adapter->netdev->name) + 1;
4749 
4750 	return len;
4751 }
4752 
4753 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
4754 				 struct vnic_login_client_data *vlcd)
4755 {
4756 	const char *os_name = "Linux";
4757 	int len;
4758 
4759 	/* Type 1 - LPAR OS */
4760 	vlcd->type = 1;
4761 	len = strlen(os_name) + 1;
4762 	vlcd->len = cpu_to_be16(len);
4763 	strscpy(vlcd->name, os_name, len);
4764 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
4765 
4766 	/* Type 2 - LPAR name */
4767 	vlcd->type = 2;
4768 	len = strlen(utsname()->nodename) + 1;
4769 	vlcd->len = cpu_to_be16(len);
4770 	strscpy(vlcd->name, utsname()->nodename, len);
4771 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
4772 
4773 	/* Type 3 - device name */
4774 	vlcd->type = 3;
4775 	len = strlen(adapter->netdev->name) + 1;
4776 	vlcd->len = cpu_to_be16(len);
4777 	strscpy(vlcd->name, adapter->netdev->name, len);
4778 }
4779 
4780 static int send_login(struct ibmvnic_adapter *adapter)
4781 {
4782 	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
4783 	struct ibmvnic_login_buffer *login_buffer;
4784 	struct device *dev = &adapter->vdev->dev;
4785 	struct vnic_login_client_data *vlcd;
4786 	dma_addr_t rsp_buffer_token;
4787 	dma_addr_t buffer_token;
4788 	size_t rsp_buffer_size;
4789 	union ibmvnic_crq crq;
4790 	int client_data_len;
4791 	size_t buffer_size;
4792 	__be64 *tx_list_p;
4793 	__be64 *rx_list_p;
4794 	int rc;
4795 	int i;
4796 
4797 	if (!adapter->tx_scrq || !adapter->rx_scrq) {
4798 		netdev_err(adapter->netdev,
4799 			   "RX or TX queues are not allocated, device login failed\n");
4800 		return -ENOMEM;
4801 	}
4802 
4803 	release_login_buffer(adapter);
4804 	release_login_rsp_buffer(adapter);
4805 
4806 	client_data_len = vnic_client_data_len(adapter);
4807 
4808 	buffer_size =
4809 	    sizeof(struct ibmvnic_login_buffer) +
4810 	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
4811 	    client_data_len;
4812 
4813 	login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
4814 	if (!login_buffer)
4815 		goto buf_alloc_failed;
4816 
4817 	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
4818 				      DMA_TO_DEVICE);
4819 	if (dma_mapping_error(dev, buffer_token)) {
4820 		dev_err(dev, "Couldn't map login buffer\n");
4821 		goto buf_map_failed;
4822 	}
4823 
4824 	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
4825 			  sizeof(u64) * adapter->req_tx_queues +
4826 			  sizeof(u64) * adapter->req_rx_queues +
4827 			  sizeof(u64) * adapter->req_rx_queues +
4828 			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
4829 
4830 	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
4831 	if (!login_rsp_buffer)
4832 		goto buf_rsp_alloc_failed;
4833 
4834 	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
4835 					  rsp_buffer_size, DMA_FROM_DEVICE);
4836 	if (dma_mapping_error(dev, rsp_buffer_token)) {
4837 		dev_err(dev, "Couldn't map login rsp buffer\n");
4838 		goto buf_rsp_map_failed;
4839 	}
4840 
4841 	adapter->login_buf = login_buffer;
4842 	adapter->login_buf_token = buffer_token;
4843 	adapter->login_buf_sz = buffer_size;
4844 	adapter->login_rsp_buf = login_rsp_buffer;
4845 	adapter->login_rsp_buf_token = rsp_buffer_token;
4846 	adapter->login_rsp_buf_sz = rsp_buffer_size;
4847 
4848 	login_buffer->len = cpu_to_be32(buffer_size);
4849 	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
4850 	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
4851 	login_buffer->off_txcomp_subcrqs =
4852 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
4853 	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
4854 	login_buffer->off_rxcomp_subcrqs =
4855 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
4856 			sizeof(u64) * adapter->req_tx_queues);
4857 	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
4858 	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
4859 
4860 	tx_list_p = (__be64 *)((char *)login_buffer +
4861 				      sizeof(struct ibmvnic_login_buffer));
4862 	rx_list_p = (__be64 *)((char *)login_buffer +
4863 				      sizeof(struct ibmvnic_login_buffer) +
4864 				      sizeof(u64) * adapter->req_tx_queues);
4865 
4866 	for (i = 0; i < adapter->req_tx_queues; i++) {
4867 		if (adapter->tx_scrq[i]) {
4868 			tx_list_p[i] =
4869 				cpu_to_be64(adapter->tx_scrq[i]->crq_num);
4870 		}
4871 	}
4872 
4873 	for (i = 0; i < adapter->req_rx_queues; i++) {
4874 		if (adapter->rx_scrq[i]) {
4875 			rx_list_p[i] =
4876 				cpu_to_be64(adapter->rx_scrq[i]->crq_num);
4877 		}
4878 	}
4879 
4880 	/* Insert vNIC login client data */
4881 	vlcd = (struct vnic_login_client_data *)
4882 		((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
4883 	login_buffer->client_data_offset =
4884 			cpu_to_be32((char *)vlcd - (char *)login_buffer);
4885 	login_buffer->client_data_len = cpu_to_be32(client_data_len);
4886 
4887 	vnic_add_client_data(adapter, vlcd);
4888 
4889 	netdev_dbg(adapter->netdev, "Login Buffer:\n");
4890 	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
4891 		netdev_dbg(adapter->netdev, "%016lx\n",
4892 			   ((unsigned long *)(adapter->login_buf))[i]);
4893 	}
4894 
4895 	memset(&crq, 0, sizeof(crq));
4896 	crq.login.first = IBMVNIC_CRQ_CMD;
4897 	crq.login.cmd = LOGIN;
4898 	crq.login.ioba = cpu_to_be32(buffer_token);
4899 	crq.login.len = cpu_to_be32(buffer_size);
4900 
4901 	adapter->login_pending = true;
4902 	rc = ibmvnic_send_crq(adapter, &crq);
4903 	if (rc) {
4904 		adapter->login_pending = false;
4905 		netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4906 		goto buf_send_failed;
4907 	}
4908 
4909 	return 0;
4910 
4911 buf_send_failed:
4912 	dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
4913 			 DMA_FROM_DEVICE);
4914 buf_rsp_map_failed:
4915 	kfree(login_rsp_buffer);
4916 	adapter->login_rsp_buf = NULL;
4917 buf_rsp_alloc_failed:
4918 	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4919 buf_map_failed:
4920 	kfree(login_buffer);
4921 	adapter->login_buf = NULL;
4922 buf_alloc_failed:
4923 	return -ENOMEM;
4924 }
4925 
4926 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4927 			    u32 len, u8 map_id)
4928 {
4929 	union ibmvnic_crq crq;
4930 
4931 	memset(&crq, 0, sizeof(crq));
4932 	crq.request_map.first = IBMVNIC_CRQ_CMD;
4933 	crq.request_map.cmd = REQUEST_MAP;
4934 	crq.request_map.map_id = map_id;
4935 	crq.request_map.ioba = cpu_to_be32(addr);
4936 	crq.request_map.len = cpu_to_be32(len);
4937 	return ibmvnic_send_crq(adapter, &crq);
4938 }
4939 
4940 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
4941 {
4942 	union ibmvnic_crq crq;
4943 
4944 	memset(&crq, 0, sizeof(crq));
4945 	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4946 	crq.request_unmap.cmd = REQUEST_UNMAP;
4947 	crq.request_unmap.map_id = map_id;
4948 	return ibmvnic_send_crq(adapter, &crq);
4949 }
4950 
4951 static void send_query_map(struct ibmvnic_adapter *adapter)
4952 {
4953 	union ibmvnic_crq crq;
4954 
4955 	memset(&crq, 0, sizeof(crq));
4956 	crq.query_map.first = IBMVNIC_CRQ_CMD;
4957 	crq.query_map.cmd = QUERY_MAP;
4958 	ibmvnic_send_crq(adapter, &crq);
4959 }
4960 
4961 /* Send a series of CRQs requesting various capabilities of the VNIC server */
4962 static void send_query_cap(struct ibmvnic_adapter *adapter)
4963 {
4964 	union ibmvnic_crq crq;
4965 	int cap_reqs;
4966 
4967 	/* We send out 25 QUERY_CAPABILITY CRQs below.  Initialize this count
4968 	 * upfront. When the tasklet receives a response to all of these, it
4969 	 * can send out the next protocol messaage (REQUEST_CAPABILITY).
4970 	 */
4971 	cap_reqs = 25;
4972 
4973 	atomic_set(&adapter->running_cap_crqs, cap_reqs);
4974 
4975 	memset(&crq, 0, sizeof(crq));
4976 	crq.query_capability.first = IBMVNIC_CRQ_CMD;
4977 	crq.query_capability.cmd = QUERY_CAPABILITY;
4978 
4979 	crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
4980 	ibmvnic_send_crq(adapter, &crq);
4981 	cap_reqs--;
4982 
4983 	crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
4984 	ibmvnic_send_crq(adapter, &crq);
4985 	cap_reqs--;
4986 
4987 	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
4988 	ibmvnic_send_crq(adapter, &crq);
4989 	cap_reqs--;
4990 
4991 	crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
4992 	ibmvnic_send_crq(adapter, &crq);
4993 	cap_reqs--;
4994 
4995 	crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
4996 	ibmvnic_send_crq(adapter, &crq);
4997 	cap_reqs--;
4998 
4999 	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
5000 	ibmvnic_send_crq(adapter, &crq);
5001 	cap_reqs--;
5002 
5003 	crq.query_capability.capability =
5004 	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
5005 	ibmvnic_send_crq(adapter, &crq);
5006 	cap_reqs--;
5007 
5008 	crq.query_capability.capability =
5009 	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
5010 	ibmvnic_send_crq(adapter, &crq);
5011 	cap_reqs--;
5012 
5013 	crq.query_capability.capability =
5014 	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
5015 	ibmvnic_send_crq(adapter, &crq);
5016 	cap_reqs--;
5017 
5018 	crq.query_capability.capability =
5019 	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
5020 	ibmvnic_send_crq(adapter, &crq);
5021 	cap_reqs--;
5022 
5023 	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
5024 	ibmvnic_send_crq(adapter, &crq);
5025 	cap_reqs--;
5026 
5027 	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
5028 	ibmvnic_send_crq(adapter, &crq);
5029 	cap_reqs--;
5030 
5031 	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
5032 	ibmvnic_send_crq(adapter, &crq);
5033 	cap_reqs--;
5034 
5035 	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
5036 	ibmvnic_send_crq(adapter, &crq);
5037 	cap_reqs--;
5038 
5039 	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
5040 	ibmvnic_send_crq(adapter, &crq);
5041 	cap_reqs--;
5042 
5043 	crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
5044 	ibmvnic_send_crq(adapter, &crq);
5045 	cap_reqs--;
5046 
5047 	crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
5048 	ibmvnic_send_crq(adapter, &crq);
5049 	cap_reqs--;
5050 
5051 	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
5052 	ibmvnic_send_crq(adapter, &crq);
5053 	cap_reqs--;
5054 
5055 	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
5056 	ibmvnic_send_crq(adapter, &crq);
5057 	cap_reqs--;
5058 
5059 	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
5060 	ibmvnic_send_crq(adapter, &crq);
5061 	cap_reqs--;
5062 
5063 	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
5064 	ibmvnic_send_crq(adapter, &crq);
5065 	cap_reqs--;
5066 
5067 	crq.query_capability.capability =
5068 			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
5069 	ibmvnic_send_crq(adapter, &crq);
5070 	cap_reqs--;
5071 
5072 	crq.query_capability.capability =
5073 			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
5074 	ibmvnic_send_crq(adapter, &crq);
5075 	cap_reqs--;
5076 
5077 	crq.query_capability.capability =
5078 			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
5079 	ibmvnic_send_crq(adapter, &crq);
5080 	cap_reqs--;
5081 
5082 	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
5083 
5084 	ibmvnic_send_crq(adapter, &crq);
5085 	cap_reqs--;
5086 
5087 	/* Keep at end to catch any discrepancy between expected and actual
5088 	 * CRQs sent.
5089 	 */
5090 	WARN_ON(cap_reqs != 0);
5091 }
5092 
5093 static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
5094 {
5095 	int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
5096 	struct device *dev = &adapter->vdev->dev;
5097 	union ibmvnic_crq crq;
5098 
5099 	adapter->ip_offload_tok =
5100 		dma_map_single(dev,
5101 			       &adapter->ip_offload_buf,
5102 			       buf_sz,
5103 			       DMA_FROM_DEVICE);
5104 
5105 	if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
5106 		if (!firmware_has_feature(FW_FEATURE_CMO))
5107 			dev_err(dev, "Couldn't map offload buffer\n");
5108 		return;
5109 	}
5110 
5111 	memset(&crq, 0, sizeof(crq));
5112 	crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
5113 	crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
5114 	crq.query_ip_offload.len = cpu_to_be32(buf_sz);
5115 	crq.query_ip_offload.ioba =
5116 	    cpu_to_be32(adapter->ip_offload_tok);
5117 
5118 	ibmvnic_send_crq(adapter, &crq);
5119 }
5120 
5121 static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
5122 {
5123 	struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
5124 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
5125 	struct device *dev = &adapter->vdev->dev;
5126 	netdev_features_t old_hw_features = 0;
5127 	union ibmvnic_crq crq;
5128 
5129 	adapter->ip_offload_ctrl_tok =
5130 		dma_map_single(dev,
5131 			       ctrl_buf,
5132 			       sizeof(adapter->ip_offload_ctrl),
5133 			       DMA_TO_DEVICE);
5134 
5135 	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
5136 		dev_err(dev, "Couldn't map ip offload control buffer\n");
5137 		return;
5138 	}
5139 
5140 	ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
5141 	ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
5142 	ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
5143 	ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
5144 	ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
5145 	ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
5146 	ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
5147 	ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
5148 	ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
5149 	ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
5150 
5151 	/* large_rx disabled for now, additional features needed */
5152 	ctrl_buf->large_rx_ipv4 = 0;
5153 	ctrl_buf->large_rx_ipv6 = 0;
5154 
5155 	if (adapter->state != VNIC_PROBING) {
5156 		old_hw_features = adapter->netdev->hw_features;
5157 		adapter->netdev->hw_features = 0;
5158 	}
5159 
5160 	adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
5161 
5162 	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
5163 		adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
5164 
5165 	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
5166 		adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
5167 
5168 	if ((adapter->netdev->features &
5169 	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
5170 		adapter->netdev->hw_features |= NETIF_F_RXCSUM;
5171 
5172 	if (buf->large_tx_ipv4)
5173 		adapter->netdev->hw_features |= NETIF_F_TSO;
5174 	if (buf->large_tx_ipv6)
5175 		adapter->netdev->hw_features |= NETIF_F_TSO6;
5176 
5177 	if (adapter->state == VNIC_PROBING) {
5178 		adapter->netdev->features |= adapter->netdev->hw_features;
5179 	} else if (old_hw_features != adapter->netdev->hw_features) {
5180 		netdev_features_t tmp = 0;
5181 
5182 		/* disable features no longer supported */
5183 		adapter->netdev->features &= adapter->netdev->hw_features;
5184 		/* turn on features now supported if previously enabled */
5185 		tmp = (old_hw_features ^ adapter->netdev->hw_features) &
5186 			adapter->netdev->hw_features;
5187 		adapter->netdev->features |=
5188 				tmp & adapter->netdev->wanted_features;
5189 	}
5190 
5191 	memset(&crq, 0, sizeof(crq));
5192 	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
5193 	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
5194 	crq.control_ip_offload.len =
5195 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
5196 	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
5197 	ibmvnic_send_crq(adapter, &crq);
5198 }
5199 
5200 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
5201 				struct ibmvnic_adapter *adapter)
5202 {
5203 	struct device *dev = &adapter->vdev->dev;
5204 
5205 	if (crq->get_vpd_size_rsp.rc.code) {
5206 		dev_err(dev, "Error retrieving VPD size, rc=%x\n",
5207 			crq->get_vpd_size_rsp.rc.code);
5208 		complete(&adapter->fw_done);
5209 		return;
5210 	}
5211 
5212 	adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
5213 	complete(&adapter->fw_done);
5214 }
5215 
5216 static void handle_vpd_rsp(union ibmvnic_crq *crq,
5217 			   struct ibmvnic_adapter *adapter)
5218 {
5219 	struct device *dev = &adapter->vdev->dev;
5220 	unsigned char *substr = NULL;
5221 	u8 fw_level_len = 0;
5222 
5223 	memset(adapter->fw_version, 0, 32);
5224 
5225 	dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
5226 			 DMA_FROM_DEVICE);
5227 
5228 	if (crq->get_vpd_rsp.rc.code) {
5229 		dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
5230 			crq->get_vpd_rsp.rc.code);
5231 		goto complete;
5232 	}
5233 
5234 	/* get the position of the firmware version info
5235 	 * located after the ASCII 'RM' substring in the buffer
5236 	 */
5237 	substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
5238 	if (!substr) {
5239 		dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
5240 		goto complete;
5241 	}
5242 
5243 	/* get length of firmware level ASCII substring */
5244 	if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
5245 		fw_level_len = *(substr + 2);
5246 	} else {
5247 		dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
5248 		goto complete;
5249 	}
5250 
5251 	/* copy firmware version string from vpd into adapter */
5252 	if ((substr + 3 + fw_level_len) <
5253 	    (adapter->vpd->buff + adapter->vpd->len)) {
5254 		strscpy(adapter->fw_version, substr + 3,
5255 			sizeof(adapter->fw_version));
5256 	} else {
5257 		dev_info(dev, "FW substr extrapolated VPD buff\n");
5258 	}
5259 
5260 complete:
5261 	if (adapter->fw_version[0] == '\0')
5262 		strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version));
5263 	complete(&adapter->fw_done);
5264 }
5265 
5266 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
5267 {
5268 	struct device *dev = &adapter->vdev->dev;
5269 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
5270 	int i;
5271 
5272 	dma_unmap_single(dev, adapter->ip_offload_tok,
5273 			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
5274 
5275 	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
5276 	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
5277 		netdev_dbg(adapter->netdev, "%016lx\n",
5278 			   ((unsigned long *)(buf))[i]);
5279 
5280 	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
5281 	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
5282 	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
5283 		   buf->tcp_ipv4_chksum);
5284 	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
5285 		   buf->tcp_ipv6_chksum);
5286 	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
5287 		   buf->udp_ipv4_chksum);
5288 	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
5289 		   buf->udp_ipv6_chksum);
5290 	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
5291 		   buf->large_tx_ipv4);
5292 	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
5293 		   buf->large_tx_ipv6);
5294 	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
5295 		   buf->large_rx_ipv4);
5296 	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
5297 		   buf->large_rx_ipv6);
5298 	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
5299 		   buf->max_ipv4_header_size);
5300 	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
5301 		   buf->max_ipv6_header_size);
5302 	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
5303 		   buf->max_tcp_header_size);
5304 	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
5305 		   buf->max_udp_header_size);
5306 	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
5307 		   buf->max_large_tx_size);
5308 	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
5309 		   buf->max_large_rx_size);
5310 	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
5311 		   buf->ipv6_extension_header);
5312 	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
5313 		   buf->tcp_pseudosum_req);
5314 	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
5315 		   buf->num_ipv6_ext_headers);
5316 	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
5317 		   buf->off_ipv6_ext_headers);
5318 
5319 	send_control_ip_offload(adapter);
5320 }
5321 
5322 static const char *ibmvnic_fw_err_cause(u16 cause)
5323 {
5324 	switch (cause) {
5325 	case ADAPTER_PROBLEM:
5326 		return "adapter problem";
5327 	case BUS_PROBLEM:
5328 		return "bus problem";
5329 	case FW_PROBLEM:
5330 		return "firmware problem";
5331 	case DD_PROBLEM:
5332 		return "device driver problem";
5333 	case EEH_RECOVERY:
5334 		return "EEH recovery";
5335 	case FW_UPDATED:
5336 		return "firmware updated";
5337 	case LOW_MEMORY:
5338 		return "low Memory";
5339 	default:
5340 		return "unknown";
5341 	}
5342 }
5343 
5344 static void handle_error_indication(union ibmvnic_crq *crq,
5345 				    struct ibmvnic_adapter *adapter)
5346 {
5347 	struct device *dev = &adapter->vdev->dev;
5348 	u16 cause;
5349 
5350 	cause = be16_to_cpu(crq->error_indication.error_cause);
5351 
5352 	dev_warn_ratelimited(dev,
5353 			     "Firmware reports %serror, cause: %s. Starting recovery...\n",
5354 			     crq->error_indication.flags
5355 				& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
5356 			     ibmvnic_fw_err_cause(cause));
5357 
5358 	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
5359 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5360 	else
5361 		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
5362 }
5363 
5364 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
5365 				 struct ibmvnic_adapter *adapter)
5366 {
5367 	struct net_device *netdev = adapter->netdev;
5368 	struct device *dev = &adapter->vdev->dev;
5369 	long rc;
5370 
5371 	rc = crq->change_mac_addr_rsp.rc.code;
5372 	if (rc) {
5373 		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
5374 		goto out;
5375 	}
5376 	/* crq->change_mac_addr.mac_addr is the requested one
5377 	 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
5378 	 */
5379 	eth_hw_addr_set(netdev, &crq->change_mac_addr_rsp.mac_addr[0]);
5380 	ether_addr_copy(adapter->mac_addr,
5381 			&crq->change_mac_addr_rsp.mac_addr[0]);
5382 out:
5383 	complete(&adapter->fw_done);
5384 	return rc;
5385 }
5386 
5387 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
5388 				   struct ibmvnic_adapter *adapter)
5389 {
5390 	struct device *dev = &adapter->vdev->dev;
5391 	u64 *req_value;
5392 	char *name;
5393 
5394 	atomic_dec(&adapter->running_cap_crqs);
5395 	netdev_dbg(adapter->netdev, "Outstanding request-caps: %d\n",
5396 		   atomic_read(&adapter->running_cap_crqs));
5397 	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
5398 	case REQ_TX_QUEUES:
5399 		req_value = &adapter->req_tx_queues;
5400 		name = "tx";
5401 		break;
5402 	case REQ_RX_QUEUES:
5403 		req_value = &adapter->req_rx_queues;
5404 		name = "rx";
5405 		break;
5406 	case REQ_RX_ADD_QUEUES:
5407 		req_value = &adapter->req_rx_add_queues;
5408 		name = "rx_add";
5409 		break;
5410 	case REQ_TX_ENTRIES_PER_SUBCRQ:
5411 		req_value = &adapter->req_tx_entries_per_subcrq;
5412 		name = "tx_entries_per_subcrq";
5413 		break;
5414 	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
5415 		req_value = &adapter->req_rx_add_entries_per_subcrq;
5416 		name = "rx_add_entries_per_subcrq";
5417 		break;
5418 	case REQ_MTU:
5419 		req_value = &adapter->req_mtu;
5420 		name = "mtu";
5421 		break;
5422 	case PROMISC_REQUESTED:
5423 		req_value = &adapter->promisc;
5424 		name = "promisc";
5425 		break;
5426 	default:
5427 		dev_err(dev, "Got invalid cap request rsp %d\n",
5428 			crq->request_capability.capability);
5429 		return;
5430 	}
5431 
5432 	switch (crq->request_capability_rsp.rc.code) {
5433 	case SUCCESS:
5434 		break;
5435 	case PARTIALSUCCESS:
5436 		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
5437 			 *req_value,
5438 			 (long)be64_to_cpu(crq->request_capability_rsp.number),
5439 			 name);
5440 
5441 		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
5442 		    REQ_MTU) {
5443 			pr_err("mtu of %llu is not supported. Reverting.\n",
5444 			       *req_value);
5445 			*req_value = adapter->fallback.mtu;
5446 		} else {
5447 			*req_value =
5448 				be64_to_cpu(crq->request_capability_rsp.number);
5449 		}
5450 
5451 		send_request_cap(adapter, 1);
5452 		return;
5453 	default:
5454 		dev_err(dev, "Error %d in request cap rsp\n",
5455 			crq->request_capability_rsp.rc.code);
5456 		return;
5457 	}
5458 
5459 	/* Done receiving requested capabilities, query IP offload support */
5460 	if (atomic_read(&adapter->running_cap_crqs) == 0)
5461 		send_query_ip_offload(adapter);
5462 }
5463 
5464 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
5465 			    struct ibmvnic_adapter *adapter)
5466 {
5467 	struct device *dev = &adapter->vdev->dev;
5468 	struct net_device *netdev = adapter->netdev;
5469 	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
5470 	struct ibmvnic_login_buffer *login = adapter->login_buf;
5471 	u64 *tx_handle_array;
5472 	u64 *rx_handle_array;
5473 	int num_tx_pools;
5474 	int num_rx_pools;
5475 	u64 *size_array;
5476 	u32 rsp_len;
5477 	int i;
5478 
5479 	/* CHECK: Test/set of login_pending does not need to be atomic
5480 	 * because only ibmvnic_tasklet tests/clears this.
5481 	 */
5482 	if (!adapter->login_pending) {
5483 		netdev_warn(netdev, "Ignoring unexpected login response\n");
5484 		return 0;
5485 	}
5486 	adapter->login_pending = false;
5487 
5488 	/* If the number of queues requested can't be allocated by the
5489 	 * server, the login response will return with code 1. We will need
5490 	 * to resend the login buffer with fewer queues requested.
5491 	 */
5492 	if (login_rsp_crq->generic.rc.code) {
5493 		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
5494 		complete(&adapter->init_done);
5495 		return 0;
5496 	}
5497 
5498 	if (adapter->failover_pending) {
5499 		adapter->init_done_rc = -EAGAIN;
5500 		netdev_dbg(netdev, "Failover pending, ignoring login response\n");
5501 		complete(&adapter->init_done);
5502 		/* login response buffer will be released on reset */
5503 		return 0;
5504 	}
5505 
5506 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
5507 
5508 	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
5509 	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
5510 		netdev_dbg(adapter->netdev, "%016lx\n",
5511 			   ((unsigned long *)(adapter->login_rsp_buf))[i]);
5512 	}
5513 
5514 	/* Sanity checks */
5515 	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
5516 	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
5517 	     adapter->req_rx_add_queues !=
5518 	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
5519 		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
5520 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5521 		return -EIO;
5522 	}
5523 
5524 	rsp_len = be32_to_cpu(login_rsp->len);
5525 	if (be32_to_cpu(login->login_rsp_len) < rsp_len ||
5526 	    rsp_len <= be32_to_cpu(login_rsp->off_txsubm_subcrqs) ||
5527 	    rsp_len <= be32_to_cpu(login_rsp->off_rxadd_subcrqs) ||
5528 	    rsp_len <= be32_to_cpu(login_rsp->off_rxadd_buff_size) ||
5529 	    rsp_len <= be32_to_cpu(login_rsp->off_supp_tx_desc)) {
5530 		/* This can happen if a login request times out and there are
5531 		 * 2 outstanding login requests sent, the LOGIN_RSP crq
5532 		 * could have been for the older login request. So we are
5533 		 * parsing the newer response buffer which may be incomplete
5534 		 */
5535 		dev_err(dev, "FATAL: Login rsp offsets/lengths invalid\n");
5536 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5537 		return -EIO;
5538 	}
5539 
5540 	size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
5541 		be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
5542 	/* variable buffer sizes are not supported, so just read the
5543 	 * first entry.
5544 	 */
5545 	adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
5546 
5547 	num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
5548 	num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
5549 
5550 	tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
5551 				  be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
5552 	rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
5553 				  be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
5554 
5555 	for (i = 0; i < num_tx_pools; i++)
5556 		adapter->tx_scrq[i]->handle = tx_handle_array[i];
5557 
5558 	for (i = 0; i < num_rx_pools; i++)
5559 		adapter->rx_scrq[i]->handle = rx_handle_array[i];
5560 
5561 	adapter->num_active_tx_scrqs = num_tx_pools;
5562 	adapter->num_active_rx_scrqs = num_rx_pools;
5563 	release_login_rsp_buffer(adapter);
5564 	release_login_buffer(adapter);
5565 	complete(&adapter->init_done);
5566 
5567 	return 0;
5568 }
5569 
5570 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
5571 				     struct ibmvnic_adapter *adapter)
5572 {
5573 	struct device *dev = &adapter->vdev->dev;
5574 	long rc;
5575 
5576 	rc = crq->request_unmap_rsp.rc.code;
5577 	if (rc)
5578 		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
5579 }
5580 
5581 static void handle_query_map_rsp(union ibmvnic_crq *crq,
5582 				 struct ibmvnic_adapter *adapter)
5583 {
5584 	struct net_device *netdev = adapter->netdev;
5585 	struct device *dev = &adapter->vdev->dev;
5586 	long rc;
5587 
5588 	rc = crq->query_map_rsp.rc.code;
5589 	if (rc) {
5590 		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
5591 		return;
5592 	}
5593 	netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n",
5594 		   crq->query_map_rsp.page_size,
5595 		   __be32_to_cpu(crq->query_map_rsp.tot_pages),
5596 		   __be32_to_cpu(crq->query_map_rsp.free_pages));
5597 }
5598 
5599 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
5600 				 struct ibmvnic_adapter *adapter)
5601 {
5602 	struct net_device *netdev = adapter->netdev;
5603 	struct device *dev = &adapter->vdev->dev;
5604 	long rc;
5605 
5606 	atomic_dec(&adapter->running_cap_crqs);
5607 	netdev_dbg(netdev, "Outstanding queries: %d\n",
5608 		   atomic_read(&adapter->running_cap_crqs));
5609 	rc = crq->query_capability.rc.code;
5610 	if (rc) {
5611 		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
5612 		goto out;
5613 	}
5614 
5615 	switch (be16_to_cpu(crq->query_capability.capability)) {
5616 	case MIN_TX_QUEUES:
5617 		adapter->min_tx_queues =
5618 		    be64_to_cpu(crq->query_capability.number);
5619 		netdev_dbg(netdev, "min_tx_queues = %lld\n",
5620 			   adapter->min_tx_queues);
5621 		break;
5622 	case MIN_RX_QUEUES:
5623 		adapter->min_rx_queues =
5624 		    be64_to_cpu(crq->query_capability.number);
5625 		netdev_dbg(netdev, "min_rx_queues = %lld\n",
5626 			   adapter->min_rx_queues);
5627 		break;
5628 	case MIN_RX_ADD_QUEUES:
5629 		adapter->min_rx_add_queues =
5630 		    be64_to_cpu(crq->query_capability.number);
5631 		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
5632 			   adapter->min_rx_add_queues);
5633 		break;
5634 	case MAX_TX_QUEUES:
5635 		adapter->max_tx_queues =
5636 		    be64_to_cpu(crq->query_capability.number);
5637 		netdev_dbg(netdev, "max_tx_queues = %lld\n",
5638 			   adapter->max_tx_queues);
5639 		break;
5640 	case MAX_RX_QUEUES:
5641 		adapter->max_rx_queues =
5642 		    be64_to_cpu(crq->query_capability.number);
5643 		netdev_dbg(netdev, "max_rx_queues = %lld\n",
5644 			   adapter->max_rx_queues);
5645 		break;
5646 	case MAX_RX_ADD_QUEUES:
5647 		adapter->max_rx_add_queues =
5648 		    be64_to_cpu(crq->query_capability.number);
5649 		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
5650 			   adapter->max_rx_add_queues);
5651 		break;
5652 	case MIN_TX_ENTRIES_PER_SUBCRQ:
5653 		adapter->min_tx_entries_per_subcrq =
5654 		    be64_to_cpu(crq->query_capability.number);
5655 		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
5656 			   adapter->min_tx_entries_per_subcrq);
5657 		break;
5658 	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
5659 		adapter->min_rx_add_entries_per_subcrq =
5660 		    be64_to_cpu(crq->query_capability.number);
5661 		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
5662 			   adapter->min_rx_add_entries_per_subcrq);
5663 		break;
5664 	case MAX_TX_ENTRIES_PER_SUBCRQ:
5665 		adapter->max_tx_entries_per_subcrq =
5666 		    be64_to_cpu(crq->query_capability.number);
5667 		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
5668 			   adapter->max_tx_entries_per_subcrq);
5669 		break;
5670 	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
5671 		adapter->max_rx_add_entries_per_subcrq =
5672 		    be64_to_cpu(crq->query_capability.number);
5673 		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
5674 			   adapter->max_rx_add_entries_per_subcrq);
5675 		break;
5676 	case TCP_IP_OFFLOAD:
5677 		adapter->tcp_ip_offload =
5678 		    be64_to_cpu(crq->query_capability.number);
5679 		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
5680 			   adapter->tcp_ip_offload);
5681 		break;
5682 	case PROMISC_SUPPORTED:
5683 		adapter->promisc_supported =
5684 		    be64_to_cpu(crq->query_capability.number);
5685 		netdev_dbg(netdev, "promisc_supported = %lld\n",
5686 			   adapter->promisc_supported);
5687 		break;
5688 	case MIN_MTU:
5689 		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
5690 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5691 		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
5692 		break;
5693 	case MAX_MTU:
5694 		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
5695 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5696 		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
5697 		break;
5698 	case MAX_MULTICAST_FILTERS:
5699 		adapter->max_multicast_filters =
5700 		    be64_to_cpu(crq->query_capability.number);
5701 		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
5702 			   adapter->max_multicast_filters);
5703 		break;
5704 	case VLAN_HEADER_INSERTION:
5705 		adapter->vlan_header_insertion =
5706 		    be64_to_cpu(crq->query_capability.number);
5707 		if (adapter->vlan_header_insertion)
5708 			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
5709 		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
5710 			   adapter->vlan_header_insertion);
5711 		break;
5712 	case RX_VLAN_HEADER_INSERTION:
5713 		adapter->rx_vlan_header_insertion =
5714 		    be64_to_cpu(crq->query_capability.number);
5715 		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
5716 			   adapter->rx_vlan_header_insertion);
5717 		break;
5718 	case MAX_TX_SG_ENTRIES:
5719 		adapter->max_tx_sg_entries =
5720 		    be64_to_cpu(crq->query_capability.number);
5721 		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
5722 			   adapter->max_tx_sg_entries);
5723 		break;
5724 	case RX_SG_SUPPORTED:
5725 		adapter->rx_sg_supported =
5726 		    be64_to_cpu(crq->query_capability.number);
5727 		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
5728 			   adapter->rx_sg_supported);
5729 		break;
5730 	case OPT_TX_COMP_SUB_QUEUES:
5731 		adapter->opt_tx_comp_sub_queues =
5732 		    be64_to_cpu(crq->query_capability.number);
5733 		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
5734 			   adapter->opt_tx_comp_sub_queues);
5735 		break;
5736 	case OPT_RX_COMP_QUEUES:
5737 		adapter->opt_rx_comp_queues =
5738 		    be64_to_cpu(crq->query_capability.number);
5739 		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
5740 			   adapter->opt_rx_comp_queues);
5741 		break;
5742 	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
5743 		adapter->opt_rx_bufadd_q_per_rx_comp_q =
5744 		    be64_to_cpu(crq->query_capability.number);
5745 		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
5746 			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
5747 		break;
5748 	case OPT_TX_ENTRIES_PER_SUBCRQ:
5749 		adapter->opt_tx_entries_per_subcrq =
5750 		    be64_to_cpu(crq->query_capability.number);
5751 		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
5752 			   adapter->opt_tx_entries_per_subcrq);
5753 		break;
5754 	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
5755 		adapter->opt_rxba_entries_per_subcrq =
5756 		    be64_to_cpu(crq->query_capability.number);
5757 		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
5758 			   adapter->opt_rxba_entries_per_subcrq);
5759 		break;
5760 	case TX_RX_DESC_REQ:
5761 		adapter->tx_rx_desc_req = crq->query_capability.number;
5762 		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
5763 			   adapter->tx_rx_desc_req);
5764 		break;
5765 
5766 	default:
5767 		netdev_err(netdev, "Got invalid cap rsp %d\n",
5768 			   crq->query_capability.capability);
5769 	}
5770 
5771 out:
5772 	if (atomic_read(&adapter->running_cap_crqs) == 0)
5773 		send_request_cap(adapter, 0);
5774 }
5775 
5776 static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
5777 {
5778 	union ibmvnic_crq crq;
5779 	int rc;
5780 
5781 	memset(&crq, 0, sizeof(crq));
5782 	crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
5783 	crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
5784 
5785 	mutex_lock(&adapter->fw_lock);
5786 	adapter->fw_done_rc = 0;
5787 	reinit_completion(&adapter->fw_done);
5788 
5789 	rc = ibmvnic_send_crq(adapter, &crq);
5790 	if (rc) {
5791 		mutex_unlock(&adapter->fw_lock);
5792 		return rc;
5793 	}
5794 
5795 	rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
5796 	if (rc) {
5797 		mutex_unlock(&adapter->fw_lock);
5798 		return rc;
5799 	}
5800 
5801 	mutex_unlock(&adapter->fw_lock);
5802 	return adapter->fw_done_rc ? -EIO : 0;
5803 }
5804 
5805 static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
5806 				       struct ibmvnic_adapter *adapter)
5807 {
5808 	struct net_device *netdev = adapter->netdev;
5809 	int rc;
5810 	__be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
5811 
5812 	rc = crq->query_phys_parms_rsp.rc.code;
5813 	if (rc) {
5814 		netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
5815 		return rc;
5816 	}
5817 	switch (rspeed) {
5818 	case IBMVNIC_10MBPS:
5819 		adapter->speed = SPEED_10;
5820 		break;
5821 	case IBMVNIC_100MBPS:
5822 		adapter->speed = SPEED_100;
5823 		break;
5824 	case IBMVNIC_1GBPS:
5825 		adapter->speed = SPEED_1000;
5826 		break;
5827 	case IBMVNIC_10GBPS:
5828 		adapter->speed = SPEED_10000;
5829 		break;
5830 	case IBMVNIC_25GBPS:
5831 		adapter->speed = SPEED_25000;
5832 		break;
5833 	case IBMVNIC_40GBPS:
5834 		adapter->speed = SPEED_40000;
5835 		break;
5836 	case IBMVNIC_50GBPS:
5837 		adapter->speed = SPEED_50000;
5838 		break;
5839 	case IBMVNIC_100GBPS:
5840 		adapter->speed = SPEED_100000;
5841 		break;
5842 	case IBMVNIC_200GBPS:
5843 		adapter->speed = SPEED_200000;
5844 		break;
5845 	default:
5846 		if (netif_carrier_ok(netdev))
5847 			netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
5848 		adapter->speed = SPEED_UNKNOWN;
5849 	}
5850 	if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
5851 		adapter->duplex = DUPLEX_FULL;
5852 	else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
5853 		adapter->duplex = DUPLEX_HALF;
5854 	else
5855 		adapter->duplex = DUPLEX_UNKNOWN;
5856 
5857 	return rc;
5858 }
5859 
5860 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
5861 			       struct ibmvnic_adapter *adapter)
5862 {
5863 	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
5864 	struct net_device *netdev = adapter->netdev;
5865 	struct device *dev = &adapter->vdev->dev;
5866 	u64 *u64_crq = (u64 *)crq;
5867 	long rc;
5868 
5869 	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
5870 		   (unsigned long)cpu_to_be64(u64_crq[0]),
5871 		   (unsigned long)cpu_to_be64(u64_crq[1]));
5872 	switch (gen_crq->first) {
5873 	case IBMVNIC_CRQ_INIT_RSP:
5874 		switch (gen_crq->cmd) {
5875 		case IBMVNIC_CRQ_INIT:
5876 			dev_info(dev, "Partner initialized\n");
5877 			adapter->from_passive_init = true;
5878 			/* Discard any stale login responses from prev reset.
5879 			 * CHECK: should we clear even on INIT_COMPLETE?
5880 			 */
5881 			adapter->login_pending = false;
5882 
5883 			if (adapter->state == VNIC_DOWN)
5884 				rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT);
5885 			else
5886 				rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5887 
5888 			if (rc && rc != -EBUSY) {
5889 				/* We were unable to schedule the failover
5890 				 * reset either because the adapter was still
5891 				 * probing (eg: during kexec) or we could not
5892 				 * allocate memory. Clear the failover_pending
5893 				 * flag since no one else will. We ignore
5894 				 * EBUSY because it means either FAILOVER reset
5895 				 * is already scheduled or the adapter is
5896 				 * being removed.
5897 				 */
5898 				netdev_err(netdev,
5899 					   "Error %ld scheduling failover reset\n",
5900 					   rc);
5901 				adapter->failover_pending = false;
5902 			}
5903 
5904 			if (!completion_done(&adapter->init_done)) {
5905 				if (!adapter->init_done_rc)
5906 					adapter->init_done_rc = -EAGAIN;
5907 				complete(&adapter->init_done);
5908 			}
5909 
5910 			break;
5911 		case IBMVNIC_CRQ_INIT_COMPLETE:
5912 			dev_info(dev, "Partner initialization complete\n");
5913 			adapter->crq.active = true;
5914 			send_version_xchg(adapter);
5915 			break;
5916 		default:
5917 			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
5918 		}
5919 		return;
5920 	case IBMVNIC_CRQ_XPORT_EVENT:
5921 		netif_carrier_off(netdev);
5922 		adapter->crq.active = false;
5923 		/* terminate any thread waiting for a response
5924 		 * from the device
5925 		 */
5926 		if (!completion_done(&adapter->fw_done)) {
5927 			adapter->fw_done_rc = -EIO;
5928 			complete(&adapter->fw_done);
5929 		}
5930 
5931 		/* if we got here during crq-init, retry crq-init */
5932 		if (!completion_done(&adapter->init_done)) {
5933 			adapter->init_done_rc = -EAGAIN;
5934 			complete(&adapter->init_done);
5935 		}
5936 
5937 		if (!completion_done(&adapter->stats_done))
5938 			complete(&adapter->stats_done);
5939 		if (test_bit(0, &adapter->resetting))
5940 			adapter->force_reset_recovery = true;
5941 		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
5942 			dev_info(dev, "Migrated, re-enabling adapter\n");
5943 			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
5944 		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5945 			dev_info(dev, "Backing device failover detected\n");
5946 			adapter->failover_pending = true;
5947 		} else {
5948 			/* The adapter lost the connection */
5949 			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5950 				gen_crq->cmd);
5951 			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5952 		}
5953 		return;
5954 	case IBMVNIC_CRQ_CMD_RSP:
5955 		break;
5956 	default:
5957 		dev_err(dev, "Got an invalid msg type 0x%02x\n",
5958 			gen_crq->first);
5959 		return;
5960 	}
5961 
5962 	switch (gen_crq->cmd) {
5963 	case VERSION_EXCHANGE_RSP:
5964 		rc = crq->version_exchange_rsp.rc.code;
5965 		if (rc) {
5966 			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5967 			break;
5968 		}
5969 		ibmvnic_version =
5970 			    be16_to_cpu(crq->version_exchange_rsp.version);
5971 		dev_info(dev, "Partner protocol version is %d\n",
5972 			 ibmvnic_version);
5973 		send_query_cap(adapter);
5974 		break;
5975 	case QUERY_CAPABILITY_RSP:
5976 		handle_query_cap_rsp(crq, adapter);
5977 		break;
5978 	case QUERY_MAP_RSP:
5979 		handle_query_map_rsp(crq, adapter);
5980 		break;
5981 	case REQUEST_MAP_RSP:
5982 		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5983 		complete(&adapter->fw_done);
5984 		break;
5985 	case REQUEST_UNMAP_RSP:
5986 		handle_request_unmap_rsp(crq, adapter);
5987 		break;
5988 	case REQUEST_CAPABILITY_RSP:
5989 		handle_request_cap_rsp(crq, adapter);
5990 		break;
5991 	case LOGIN_RSP:
5992 		netdev_dbg(netdev, "Got Login Response\n");
5993 		handle_login_rsp(crq, adapter);
5994 		break;
5995 	case LOGICAL_LINK_STATE_RSP:
5996 		netdev_dbg(netdev,
5997 			   "Got Logical Link State Response, state: %d rc: %d\n",
5998 			   crq->logical_link_state_rsp.link_state,
5999 			   crq->logical_link_state_rsp.rc.code);
6000 		adapter->logical_link_state =
6001 		    crq->logical_link_state_rsp.link_state;
6002 		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
6003 		complete(&adapter->init_done);
6004 		break;
6005 	case LINK_STATE_INDICATION:
6006 		netdev_dbg(netdev, "Got Logical Link State Indication\n");
6007 		adapter->phys_link_state =
6008 		    crq->link_state_indication.phys_link_state;
6009 		adapter->logical_link_state =
6010 		    crq->link_state_indication.logical_link_state;
6011 		if (adapter->phys_link_state && adapter->logical_link_state)
6012 			netif_carrier_on(netdev);
6013 		else
6014 			netif_carrier_off(netdev);
6015 		break;
6016 	case CHANGE_MAC_ADDR_RSP:
6017 		netdev_dbg(netdev, "Got MAC address change Response\n");
6018 		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
6019 		break;
6020 	case ERROR_INDICATION:
6021 		netdev_dbg(netdev, "Got Error Indication\n");
6022 		handle_error_indication(crq, adapter);
6023 		break;
6024 	case REQUEST_STATISTICS_RSP:
6025 		netdev_dbg(netdev, "Got Statistics Response\n");
6026 		complete(&adapter->stats_done);
6027 		break;
6028 	case QUERY_IP_OFFLOAD_RSP:
6029 		netdev_dbg(netdev, "Got Query IP offload Response\n");
6030 		handle_query_ip_offload_rsp(adapter);
6031 		break;
6032 	case MULTICAST_CTRL_RSP:
6033 		netdev_dbg(netdev, "Got multicast control Response\n");
6034 		break;
6035 	case CONTROL_IP_OFFLOAD_RSP:
6036 		netdev_dbg(netdev, "Got Control IP offload Response\n");
6037 		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
6038 				 sizeof(adapter->ip_offload_ctrl),
6039 				 DMA_TO_DEVICE);
6040 		complete(&adapter->init_done);
6041 		break;
6042 	case COLLECT_FW_TRACE_RSP:
6043 		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
6044 		complete(&adapter->fw_done);
6045 		break;
6046 	case GET_VPD_SIZE_RSP:
6047 		handle_vpd_size_rsp(crq, adapter);
6048 		break;
6049 	case GET_VPD_RSP:
6050 		handle_vpd_rsp(crq, adapter);
6051 		break;
6052 	case QUERY_PHYS_PARMS_RSP:
6053 		adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
6054 		complete(&adapter->fw_done);
6055 		break;
6056 	default:
6057 		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
6058 			   gen_crq->cmd);
6059 	}
6060 }
6061 
6062 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
6063 {
6064 	struct ibmvnic_adapter *adapter = instance;
6065 
6066 	tasklet_schedule(&adapter->tasklet);
6067 	return IRQ_HANDLED;
6068 }
6069 
6070 static void ibmvnic_tasklet(struct tasklet_struct *t)
6071 {
6072 	struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
6073 	struct ibmvnic_crq_queue *queue = &adapter->crq;
6074 	union ibmvnic_crq *crq;
6075 	unsigned long flags;
6076 
6077 	spin_lock_irqsave(&queue->lock, flags);
6078 
6079 	/* Pull all the valid messages off the CRQ */
6080 	while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
6081 		/* This barrier makes sure ibmvnic_next_crq()'s
6082 		 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
6083 		 * before ibmvnic_handle_crq()'s
6084 		 * switch(gen_crq->first) and switch(gen_crq->cmd).
6085 		 */
6086 		dma_rmb();
6087 		ibmvnic_handle_crq(crq, adapter);
6088 		crq->generic.first = 0;
6089 	}
6090 
6091 	spin_unlock_irqrestore(&queue->lock, flags);
6092 }
6093 
6094 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
6095 {
6096 	struct vio_dev *vdev = adapter->vdev;
6097 	int rc;
6098 
6099 	do {
6100 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
6101 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
6102 
6103 	if (rc)
6104 		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
6105 
6106 	return rc;
6107 }
6108 
6109 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
6110 {
6111 	struct ibmvnic_crq_queue *crq = &adapter->crq;
6112 	struct device *dev = &adapter->vdev->dev;
6113 	struct vio_dev *vdev = adapter->vdev;
6114 	int rc;
6115 
6116 	/* Close the CRQ */
6117 	do {
6118 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6119 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6120 
6121 	/* Clean out the queue */
6122 	if (!crq->msgs)
6123 		return -EINVAL;
6124 
6125 	memset(crq->msgs, 0, PAGE_SIZE);
6126 	crq->cur = 0;
6127 	crq->active = false;
6128 
6129 	/* And re-open it again */
6130 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
6131 				crq->msg_token, PAGE_SIZE);
6132 
6133 	if (rc == H_CLOSED)
6134 		/* Adapter is good, but other end is not ready */
6135 		dev_warn(dev, "Partner adapter not ready\n");
6136 	else if (rc != 0)
6137 		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
6138 
6139 	return rc;
6140 }
6141 
6142 static void release_crq_queue(struct ibmvnic_adapter *adapter)
6143 {
6144 	struct ibmvnic_crq_queue *crq = &adapter->crq;
6145 	struct vio_dev *vdev = adapter->vdev;
6146 	long rc;
6147 
6148 	if (!crq->msgs)
6149 		return;
6150 
6151 	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
6152 	free_irq(vdev->irq, adapter);
6153 	tasklet_kill(&adapter->tasklet);
6154 	do {
6155 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6156 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6157 
6158 	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
6159 			 DMA_BIDIRECTIONAL);
6160 	free_page((unsigned long)crq->msgs);
6161 	crq->msgs = NULL;
6162 	crq->active = false;
6163 }
6164 
6165 static int init_crq_queue(struct ibmvnic_adapter *adapter)
6166 {
6167 	struct ibmvnic_crq_queue *crq = &adapter->crq;
6168 	struct device *dev = &adapter->vdev->dev;
6169 	struct vio_dev *vdev = adapter->vdev;
6170 	int rc, retrc = -ENOMEM;
6171 
6172 	if (crq->msgs)
6173 		return 0;
6174 
6175 	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
6176 	/* Should we allocate more than one page? */
6177 
6178 	if (!crq->msgs)
6179 		return -ENOMEM;
6180 
6181 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
6182 	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
6183 					DMA_BIDIRECTIONAL);
6184 	if (dma_mapping_error(dev, crq->msg_token))
6185 		goto map_failed;
6186 
6187 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
6188 				crq->msg_token, PAGE_SIZE);
6189 
6190 	if (rc == H_RESOURCE)
6191 		/* maybe kexecing and resource is busy. try a reset */
6192 		rc = ibmvnic_reset_crq(adapter);
6193 	retrc = rc;
6194 
6195 	if (rc == H_CLOSED) {
6196 		dev_warn(dev, "Partner adapter not ready\n");
6197 	} else if (rc) {
6198 		dev_warn(dev, "Error %d opening adapter\n", rc);
6199 		goto reg_crq_failed;
6200 	}
6201 
6202 	retrc = 0;
6203 
6204 	tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
6205 
6206 	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
6207 	snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
6208 		 adapter->vdev->unit_address);
6209 	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
6210 	if (rc) {
6211 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
6212 			vdev->irq, rc);
6213 		goto req_irq_failed;
6214 	}
6215 
6216 	rc = vio_enable_interrupts(vdev);
6217 	if (rc) {
6218 		dev_err(dev, "Error %d enabling interrupts\n", rc);
6219 		goto req_irq_failed;
6220 	}
6221 
6222 	crq->cur = 0;
6223 	spin_lock_init(&crq->lock);
6224 
6225 	/* process any CRQs that were queued before we enabled interrupts */
6226 	tasklet_schedule(&adapter->tasklet);
6227 
6228 	return retrc;
6229 
6230 req_irq_failed:
6231 	tasklet_kill(&adapter->tasklet);
6232 	do {
6233 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6234 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6235 reg_crq_failed:
6236 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
6237 map_failed:
6238 	free_page((unsigned long)crq->msgs);
6239 	crq->msgs = NULL;
6240 	return retrc;
6241 }
6242 
6243 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
6244 {
6245 	struct device *dev = &adapter->vdev->dev;
6246 	unsigned long timeout = msecs_to_jiffies(20000);
6247 	u64 old_num_rx_queues = adapter->req_rx_queues;
6248 	u64 old_num_tx_queues = adapter->req_tx_queues;
6249 	int rc;
6250 
6251 	adapter->from_passive_init = false;
6252 
6253 	rc = ibmvnic_send_crq_init(adapter);
6254 	if (rc) {
6255 		dev_err(dev, "Send crq init failed with error %d\n", rc);
6256 		return rc;
6257 	}
6258 
6259 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
6260 		dev_err(dev, "Initialization sequence timed out\n");
6261 		return -ETIMEDOUT;
6262 	}
6263 
6264 	if (adapter->init_done_rc) {
6265 		release_crq_queue(adapter);
6266 		dev_err(dev, "CRQ-init failed, %d\n", adapter->init_done_rc);
6267 		return adapter->init_done_rc;
6268 	}
6269 
6270 	if (adapter->from_passive_init) {
6271 		adapter->state = VNIC_OPEN;
6272 		adapter->from_passive_init = false;
6273 		dev_err(dev, "CRQ-init failed, passive-init\n");
6274 		return -EINVAL;
6275 	}
6276 
6277 	if (reset &&
6278 	    test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
6279 	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
6280 		if (adapter->req_rx_queues != old_num_rx_queues ||
6281 		    adapter->req_tx_queues != old_num_tx_queues) {
6282 			release_sub_crqs(adapter, 0);
6283 			rc = init_sub_crqs(adapter);
6284 		} else {
6285 			/* no need to reinitialize completely, but we do
6286 			 * need to clean up transmits that were in flight
6287 			 * when we processed the reset.  Failure to do so
6288 			 * will confound the upper layer, usually TCP, by
6289 			 * creating the illusion of transmits that are
6290 			 * awaiting completion.
6291 			 */
6292 			clean_tx_pools(adapter);
6293 
6294 			rc = reset_sub_crq_queues(adapter);
6295 		}
6296 	} else {
6297 		rc = init_sub_crqs(adapter);
6298 	}
6299 
6300 	if (rc) {
6301 		dev_err(dev, "Initialization of sub crqs failed\n");
6302 		release_crq_queue(adapter);
6303 		return rc;
6304 	}
6305 
6306 	rc = init_sub_crq_irqs(adapter);
6307 	if (rc) {
6308 		dev_err(dev, "Failed to initialize sub crq irqs\n");
6309 		release_crq_queue(adapter);
6310 	}
6311 
6312 	return rc;
6313 }
6314 
6315 static struct device_attribute dev_attr_failover;
6316 
6317 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
6318 {
6319 	struct ibmvnic_adapter *adapter;
6320 	struct net_device *netdev;
6321 	unsigned char *mac_addr_p;
6322 	unsigned long flags;
6323 	bool init_success;
6324 	int rc;
6325 
6326 	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
6327 		dev->unit_address);
6328 
6329 	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
6330 							VETH_MAC_ADDR, NULL);
6331 	if (!mac_addr_p) {
6332 		dev_err(&dev->dev,
6333 			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
6334 			__FILE__, __LINE__);
6335 		return 0;
6336 	}
6337 
6338 	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
6339 				   IBMVNIC_MAX_QUEUES);
6340 	if (!netdev)
6341 		return -ENOMEM;
6342 
6343 	adapter = netdev_priv(netdev);
6344 	adapter->state = VNIC_PROBING;
6345 	dev_set_drvdata(&dev->dev, netdev);
6346 	adapter->vdev = dev;
6347 	adapter->netdev = netdev;
6348 	adapter->login_pending = false;
6349 	memset(&adapter->map_ids, 0, sizeof(adapter->map_ids));
6350 	/* map_ids start at 1, so ensure map_id 0 is always "in-use" */
6351 	bitmap_set(adapter->map_ids, 0, 1);
6352 
6353 	ether_addr_copy(adapter->mac_addr, mac_addr_p);
6354 	eth_hw_addr_set(netdev, adapter->mac_addr);
6355 	netdev->irq = dev->irq;
6356 	netdev->netdev_ops = &ibmvnic_netdev_ops;
6357 	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
6358 	SET_NETDEV_DEV(netdev, &dev->dev);
6359 
6360 	INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
6361 	INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
6362 			  __ibmvnic_delayed_reset);
6363 	INIT_LIST_HEAD(&adapter->rwi_list);
6364 	spin_lock_init(&adapter->rwi_lock);
6365 	spin_lock_init(&adapter->state_lock);
6366 	mutex_init(&adapter->fw_lock);
6367 	init_completion(&adapter->probe_done);
6368 	init_completion(&adapter->init_done);
6369 	init_completion(&adapter->fw_done);
6370 	init_completion(&adapter->reset_done);
6371 	init_completion(&adapter->stats_done);
6372 	clear_bit(0, &adapter->resetting);
6373 	adapter->prev_rx_buf_sz = 0;
6374 	adapter->prev_mtu = 0;
6375 
6376 	init_success = false;
6377 	do {
6378 		reinit_init_done(adapter);
6379 
6380 		/* clear any failovers we got in the previous pass
6381 		 * since we are reinitializing the CRQ
6382 		 */
6383 		adapter->failover_pending = false;
6384 
6385 		/* If we had already initialized CRQ, we may have one or
6386 		 * more resets queued already. Discard those and release
6387 		 * the CRQ before initializing the CRQ again.
6388 		 */
6389 		release_crq_queue(adapter);
6390 
6391 		/* Since we are still in PROBING state, __ibmvnic_reset()
6392 		 * will not access the ->rwi_list and since we released CRQ,
6393 		 * we won't get _new_ transport events. But there maybe an
6394 		 * ongoing ibmvnic_reset() call. So serialize access to
6395 		 * rwi_list. If we win the race, ibvmnic_reset() could add
6396 		 * a reset after we purged but thats ok - we just may end
6397 		 * up with an extra reset (i.e similar to having two or more
6398 		 * resets in the queue at once).
6399 		 * CHECK.
6400 		 */
6401 		spin_lock_irqsave(&adapter->rwi_lock, flags);
6402 		flush_reset_queue(adapter);
6403 		spin_unlock_irqrestore(&adapter->rwi_lock, flags);
6404 
6405 		rc = init_crq_queue(adapter);
6406 		if (rc) {
6407 			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
6408 				rc);
6409 			goto ibmvnic_init_fail;
6410 		}
6411 
6412 		rc = ibmvnic_reset_init(adapter, false);
6413 	} while (rc == -EAGAIN);
6414 
6415 	/* We are ignoring the error from ibmvnic_reset_init() assuming that the
6416 	 * partner is not ready. CRQ is not active. When the partner becomes
6417 	 * ready, we will do the passive init reset.
6418 	 */
6419 
6420 	if (!rc)
6421 		init_success = true;
6422 
6423 	rc = init_stats_buffers(adapter);
6424 	if (rc)
6425 		goto ibmvnic_init_fail;
6426 
6427 	rc = init_stats_token(adapter);
6428 	if (rc)
6429 		goto ibmvnic_stats_fail;
6430 
6431 	rc = device_create_file(&dev->dev, &dev_attr_failover);
6432 	if (rc)
6433 		goto ibmvnic_dev_file_err;
6434 
6435 	netif_carrier_off(netdev);
6436 
6437 	if (init_success) {
6438 		adapter->state = VNIC_PROBED;
6439 		netdev->mtu = adapter->req_mtu - ETH_HLEN;
6440 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
6441 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
6442 	} else {
6443 		adapter->state = VNIC_DOWN;
6444 	}
6445 
6446 	adapter->wait_for_reset = false;
6447 	adapter->last_reset_time = jiffies;
6448 
6449 	rc = register_netdev(netdev);
6450 	if (rc) {
6451 		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
6452 		goto ibmvnic_register_fail;
6453 	}
6454 	dev_info(&dev->dev, "ibmvnic registered\n");
6455 
6456 	rc = ibmvnic_cpu_notif_add(adapter);
6457 	if (rc) {
6458 		netdev_err(netdev, "Registering cpu notifier failed\n");
6459 		goto cpu_notif_add_failed;
6460 	}
6461 
6462 	complete(&adapter->probe_done);
6463 
6464 	return 0;
6465 
6466 cpu_notif_add_failed:
6467 	unregister_netdev(netdev);
6468 
6469 ibmvnic_register_fail:
6470 	device_remove_file(&dev->dev, &dev_attr_failover);
6471 
6472 ibmvnic_dev_file_err:
6473 	release_stats_token(adapter);
6474 
6475 ibmvnic_stats_fail:
6476 	release_stats_buffers(adapter);
6477 
6478 ibmvnic_init_fail:
6479 	release_sub_crqs(adapter, 1);
6480 	release_crq_queue(adapter);
6481 
6482 	/* cleanup worker thread after releasing CRQ so we don't get
6483 	 * transport events (i.e new work items for the worker thread).
6484 	 */
6485 	adapter->state = VNIC_REMOVING;
6486 	complete(&adapter->probe_done);
6487 	flush_work(&adapter->ibmvnic_reset);
6488 	flush_delayed_work(&adapter->ibmvnic_delayed_reset);
6489 
6490 	flush_reset_queue(adapter);
6491 
6492 	mutex_destroy(&adapter->fw_lock);
6493 	free_netdev(netdev);
6494 
6495 	return rc;
6496 }
6497 
6498 static void ibmvnic_remove(struct vio_dev *dev)
6499 {
6500 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
6501 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
6502 	unsigned long flags;
6503 
6504 	spin_lock_irqsave(&adapter->state_lock, flags);
6505 
6506 	/* If ibmvnic_reset() is scheduling a reset, wait for it to
6507 	 * finish. Then, set the state to REMOVING to prevent it from
6508 	 * scheduling any more work and to have reset functions ignore
6509 	 * any resets that have already been scheduled. Drop the lock
6510 	 * after setting state, so __ibmvnic_reset() which is called
6511 	 * from the flush_work() below, can make progress.
6512 	 */
6513 	spin_lock(&adapter->rwi_lock);
6514 	adapter->state = VNIC_REMOVING;
6515 	spin_unlock(&adapter->rwi_lock);
6516 
6517 	spin_unlock_irqrestore(&adapter->state_lock, flags);
6518 
6519 	ibmvnic_cpu_notif_remove(adapter);
6520 
6521 	flush_work(&adapter->ibmvnic_reset);
6522 	flush_delayed_work(&adapter->ibmvnic_delayed_reset);
6523 
6524 	rtnl_lock();
6525 	unregister_netdevice(netdev);
6526 
6527 	release_resources(adapter);
6528 	release_rx_pools(adapter);
6529 	release_tx_pools(adapter);
6530 	release_sub_crqs(adapter, 1);
6531 	release_crq_queue(adapter);
6532 
6533 	release_stats_token(adapter);
6534 	release_stats_buffers(adapter);
6535 
6536 	adapter->state = VNIC_REMOVED;
6537 
6538 	rtnl_unlock();
6539 	mutex_destroy(&adapter->fw_lock);
6540 	device_remove_file(&dev->dev, &dev_attr_failover);
6541 	free_netdev(netdev);
6542 	dev_set_drvdata(&dev->dev, NULL);
6543 }
6544 
6545 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
6546 			      const char *buf, size_t count)
6547 {
6548 	struct net_device *netdev = dev_get_drvdata(dev);
6549 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
6550 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
6551 	__be64 session_token;
6552 	long rc;
6553 
6554 	if (!sysfs_streq(buf, "1"))
6555 		return -EINVAL;
6556 
6557 	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
6558 			 H_GET_SESSION_TOKEN, 0, 0, 0);
6559 	if (rc) {
6560 		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
6561 			   rc);
6562 		goto last_resort;
6563 	}
6564 
6565 	session_token = (__be64)retbuf[0];
6566 	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
6567 		   be64_to_cpu(session_token));
6568 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
6569 				H_SESSION_ERR_DETECTED, session_token, 0, 0);
6570 	if (rc) {
6571 		netdev_err(netdev,
6572 			   "H_VIOCTL initiated failover failed, rc %ld\n",
6573 			   rc);
6574 		goto last_resort;
6575 	}
6576 
6577 	return count;
6578 
6579 last_resort:
6580 	netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
6581 	ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
6582 
6583 	return count;
6584 }
6585 static DEVICE_ATTR_WO(failover);
6586 
6587 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
6588 {
6589 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
6590 	struct ibmvnic_adapter *adapter;
6591 	struct iommu_table *tbl;
6592 	unsigned long ret = 0;
6593 	int i;
6594 
6595 	tbl = get_iommu_table_base(&vdev->dev);
6596 
6597 	/* netdev inits at probe time along with the structures we need below*/
6598 	if (!netdev)
6599 		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
6600 
6601 	adapter = netdev_priv(netdev);
6602 
6603 	ret += PAGE_SIZE; /* the crq message queue */
6604 	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
6605 
6606 	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
6607 		ret += 4 * PAGE_SIZE; /* the scrq message queue */
6608 
6609 	for (i = 0; i < adapter->num_active_rx_pools; i++)
6610 		ret += adapter->rx_pool[i].size *
6611 		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
6612 
6613 	return ret;
6614 }
6615 
6616 static int ibmvnic_resume(struct device *dev)
6617 {
6618 	struct net_device *netdev = dev_get_drvdata(dev);
6619 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
6620 
6621 	if (adapter->state != VNIC_OPEN)
6622 		return 0;
6623 
6624 	tasklet_schedule(&adapter->tasklet);
6625 
6626 	return 0;
6627 }
6628 
6629 static const struct vio_device_id ibmvnic_device_table[] = {
6630 	{"network", "IBM,vnic"},
6631 	{"", "" }
6632 };
6633 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
6634 
6635 static const struct dev_pm_ops ibmvnic_pm_ops = {
6636 	.resume = ibmvnic_resume
6637 };
6638 
6639 static struct vio_driver ibmvnic_driver = {
6640 	.id_table       = ibmvnic_device_table,
6641 	.probe          = ibmvnic_probe,
6642 	.remove         = ibmvnic_remove,
6643 	.get_desired_dma = ibmvnic_get_desired_dma,
6644 	.name		= ibmvnic_driver_name,
6645 	.pm		= &ibmvnic_pm_ops,
6646 };
6647 
6648 /* module functions */
6649 static int __init ibmvnic_module_init(void)
6650 {
6651 	int ret;
6652 
6653 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/ibmvnic:online",
6654 				      ibmvnic_cpu_online,
6655 				      ibmvnic_cpu_down_prep);
6656 	if (ret < 0)
6657 		goto out;
6658 	ibmvnic_online = ret;
6659 	ret = cpuhp_setup_state_multi(CPUHP_IBMVNIC_DEAD, "net/ibmvnic:dead",
6660 				      NULL, ibmvnic_cpu_dead);
6661 	if (ret)
6662 		goto err_dead;
6663 
6664 	ret = vio_register_driver(&ibmvnic_driver);
6665 	if (ret)
6666 		goto err_vio_register;
6667 
6668 	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
6669 		IBMVNIC_DRIVER_VERSION);
6670 
6671 	return 0;
6672 err_vio_register:
6673 	cpuhp_remove_multi_state(CPUHP_IBMVNIC_DEAD);
6674 err_dead:
6675 	cpuhp_remove_multi_state(ibmvnic_online);
6676 out:
6677 	return ret;
6678 }
6679 
6680 static void __exit ibmvnic_module_exit(void)
6681 {
6682 	vio_unregister_driver(&ibmvnic_driver);
6683 	cpuhp_remove_multi_state(CPUHP_IBMVNIC_DEAD);
6684 	cpuhp_remove_multi_state(ibmvnic_online);
6685 }
6686 
6687 module_init(ibmvnic_module_init);
6688 module_exit(ibmvnic_module_exit);
6689