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