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