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