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