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