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