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