xref: /openbmc/linux/drivers/net/ethernet/ibm/ibmvnic.c (revision ae0be8de)
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 	netdev_features_t old_hw_features = 0;
3762 	union ibmvnic_crq crq;
3763 	int i;
3764 
3765 	dma_unmap_single(dev, adapter->ip_offload_tok,
3766 			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3767 
3768 	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3769 	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3770 		netdev_dbg(adapter->netdev, "%016lx\n",
3771 			   ((unsigned long int *)(buf))[i]);
3772 
3773 	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3774 	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3775 	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3776 		   buf->tcp_ipv4_chksum);
3777 	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3778 		   buf->tcp_ipv6_chksum);
3779 	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3780 		   buf->udp_ipv4_chksum);
3781 	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3782 		   buf->udp_ipv6_chksum);
3783 	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3784 		   buf->large_tx_ipv4);
3785 	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3786 		   buf->large_tx_ipv6);
3787 	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3788 		   buf->large_rx_ipv4);
3789 	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3790 		   buf->large_rx_ipv6);
3791 	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3792 		   buf->max_ipv4_header_size);
3793 	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3794 		   buf->max_ipv6_header_size);
3795 	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3796 		   buf->max_tcp_header_size);
3797 	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3798 		   buf->max_udp_header_size);
3799 	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3800 		   buf->max_large_tx_size);
3801 	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3802 		   buf->max_large_rx_size);
3803 	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3804 		   buf->ipv6_extension_header);
3805 	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3806 		   buf->tcp_pseudosum_req);
3807 	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3808 		   buf->num_ipv6_ext_headers);
3809 	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3810 		   buf->off_ipv6_ext_headers);
3811 
3812 	adapter->ip_offload_ctrl_tok =
3813 	    dma_map_single(dev, &adapter->ip_offload_ctrl,
3814 			   sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3815 
3816 	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3817 		dev_err(dev, "Couldn't map ip offload control buffer\n");
3818 		return;
3819 	}
3820 
3821 	adapter->ip_offload_ctrl.len =
3822 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3823 	adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3824 	adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3825 	adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3826 	adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3827 	adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3828 	adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3829 	adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3830 	adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3831 	adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3832 
3833 	/* large_rx disabled for now, additional features needed */
3834 	adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3835 	adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3836 
3837 	if (adapter->state != VNIC_PROBING) {
3838 		old_hw_features = adapter->netdev->hw_features;
3839 		adapter->netdev->hw_features = 0;
3840 	}
3841 
3842 	adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
3843 
3844 	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3845 		adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
3846 
3847 	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3848 		adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
3849 
3850 	if ((adapter->netdev->features &
3851 	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3852 		adapter->netdev->hw_features |= NETIF_F_RXCSUM;
3853 
3854 	if (buf->large_tx_ipv4)
3855 		adapter->netdev->hw_features |= NETIF_F_TSO;
3856 	if (buf->large_tx_ipv6)
3857 		adapter->netdev->hw_features |= NETIF_F_TSO6;
3858 
3859 	if (adapter->state == VNIC_PROBING) {
3860 		adapter->netdev->features |= adapter->netdev->hw_features;
3861 	} else if (old_hw_features != adapter->netdev->hw_features) {
3862 		netdev_features_t tmp = 0;
3863 
3864 		/* disable features no longer supported */
3865 		adapter->netdev->features &= adapter->netdev->hw_features;
3866 		/* turn on features now supported if previously enabled */
3867 		tmp = (old_hw_features ^ adapter->netdev->hw_features) &
3868 			adapter->netdev->hw_features;
3869 		adapter->netdev->features |=
3870 				tmp & adapter->netdev->wanted_features;
3871 	}
3872 
3873 	memset(&crq, 0, sizeof(crq));
3874 	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3875 	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3876 	crq.control_ip_offload.len =
3877 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3878 	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3879 	ibmvnic_send_crq(adapter, &crq);
3880 }
3881 
3882 static const char *ibmvnic_fw_err_cause(u16 cause)
3883 {
3884 	switch (cause) {
3885 	case ADAPTER_PROBLEM:
3886 		return "adapter problem";
3887 	case BUS_PROBLEM:
3888 		return "bus problem";
3889 	case FW_PROBLEM:
3890 		return "firmware problem";
3891 	case DD_PROBLEM:
3892 		return "device driver problem";
3893 	case EEH_RECOVERY:
3894 		return "EEH recovery";
3895 	case FW_UPDATED:
3896 		return "firmware updated";
3897 	case LOW_MEMORY:
3898 		return "low Memory";
3899 	default:
3900 		return "unknown";
3901 	}
3902 }
3903 
3904 static void handle_error_indication(union ibmvnic_crq *crq,
3905 				    struct ibmvnic_adapter *adapter)
3906 {
3907 	struct device *dev = &adapter->vdev->dev;
3908 	u16 cause;
3909 
3910 	cause = be16_to_cpu(crq->error_indication.error_cause);
3911 
3912 	dev_warn_ratelimited(dev,
3913 			     "Firmware reports %serror, cause: %s. Starting recovery...\n",
3914 			     crq->error_indication.flags
3915 				& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3916 			     ibmvnic_fw_err_cause(cause));
3917 
3918 	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3919 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3920 	else
3921 		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3922 }
3923 
3924 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3925 				 struct ibmvnic_adapter *adapter)
3926 {
3927 	struct net_device *netdev = adapter->netdev;
3928 	struct device *dev = &adapter->vdev->dev;
3929 	long rc;
3930 
3931 	rc = crq->change_mac_addr_rsp.rc.code;
3932 	if (rc) {
3933 		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3934 		goto out;
3935 	}
3936 	memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3937 	       ETH_ALEN);
3938 out:
3939 	complete(&adapter->fw_done);
3940 	return rc;
3941 }
3942 
3943 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3944 				   struct ibmvnic_adapter *adapter)
3945 {
3946 	struct device *dev = &adapter->vdev->dev;
3947 	u64 *req_value;
3948 	char *name;
3949 
3950 	atomic_dec(&adapter->running_cap_crqs);
3951 	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3952 	case REQ_TX_QUEUES:
3953 		req_value = &adapter->req_tx_queues;
3954 		name = "tx";
3955 		break;
3956 	case REQ_RX_QUEUES:
3957 		req_value = &adapter->req_rx_queues;
3958 		name = "rx";
3959 		break;
3960 	case REQ_RX_ADD_QUEUES:
3961 		req_value = &adapter->req_rx_add_queues;
3962 		name = "rx_add";
3963 		break;
3964 	case REQ_TX_ENTRIES_PER_SUBCRQ:
3965 		req_value = &adapter->req_tx_entries_per_subcrq;
3966 		name = "tx_entries_per_subcrq";
3967 		break;
3968 	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3969 		req_value = &adapter->req_rx_add_entries_per_subcrq;
3970 		name = "rx_add_entries_per_subcrq";
3971 		break;
3972 	case REQ_MTU:
3973 		req_value = &adapter->req_mtu;
3974 		name = "mtu";
3975 		break;
3976 	case PROMISC_REQUESTED:
3977 		req_value = &adapter->promisc;
3978 		name = "promisc";
3979 		break;
3980 	default:
3981 		dev_err(dev, "Got invalid cap request rsp %d\n",
3982 			crq->request_capability.capability);
3983 		return;
3984 	}
3985 
3986 	switch (crq->request_capability_rsp.rc.code) {
3987 	case SUCCESS:
3988 		break;
3989 	case PARTIALSUCCESS:
3990 		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3991 			 *req_value,
3992 			 (long int)be64_to_cpu(crq->request_capability_rsp.
3993 					       number), name);
3994 
3995 		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3996 		    REQ_MTU) {
3997 			pr_err("mtu of %llu is not supported. Reverting.\n",
3998 			       *req_value);
3999 			*req_value = adapter->fallback.mtu;
4000 		} else {
4001 			*req_value =
4002 				be64_to_cpu(crq->request_capability_rsp.number);
4003 		}
4004 
4005 		ibmvnic_send_req_caps(adapter, 1);
4006 		return;
4007 	default:
4008 		dev_err(dev, "Error %d in request cap rsp\n",
4009 			crq->request_capability_rsp.rc.code);
4010 		return;
4011 	}
4012 
4013 	/* Done receiving requested capabilities, query IP offload support */
4014 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4015 		union ibmvnic_crq newcrq;
4016 		int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4017 		struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4018 		    &adapter->ip_offload_buf;
4019 
4020 		adapter->wait_capability = false;
4021 		adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4022 							 buf_sz,
4023 							 DMA_FROM_DEVICE);
4024 
4025 		if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4026 			if (!firmware_has_feature(FW_FEATURE_CMO))
4027 				dev_err(dev, "Couldn't map offload buffer\n");
4028 			return;
4029 		}
4030 
4031 		memset(&newcrq, 0, sizeof(newcrq));
4032 		newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4033 		newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4034 		newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4035 		newcrq.query_ip_offload.ioba =
4036 		    cpu_to_be32(adapter->ip_offload_tok);
4037 
4038 		ibmvnic_send_crq(adapter, &newcrq);
4039 	}
4040 }
4041 
4042 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4043 			    struct ibmvnic_adapter *adapter)
4044 {
4045 	struct device *dev = &adapter->vdev->dev;
4046 	struct net_device *netdev = adapter->netdev;
4047 	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4048 	struct ibmvnic_login_buffer *login = adapter->login_buf;
4049 	int i;
4050 
4051 	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
4052 			 DMA_TO_DEVICE);
4053 	dma_unmap_single(dev, adapter->login_rsp_buf_token,
4054 			 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
4055 
4056 	/* If the number of queues requested can't be allocated by the
4057 	 * server, the login response will return with code 1. We will need
4058 	 * to resend the login buffer with fewer queues requested.
4059 	 */
4060 	if (login_rsp_crq->generic.rc.code) {
4061 		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4062 		complete(&adapter->init_done);
4063 		return 0;
4064 	}
4065 
4066 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4067 
4068 	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4069 	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4070 		netdev_dbg(adapter->netdev, "%016lx\n",
4071 			   ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4072 	}
4073 
4074 	/* Sanity checks */
4075 	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4076 	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
4077 	     adapter->req_rx_add_queues !=
4078 	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4079 		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4080 		ibmvnic_remove(adapter->vdev);
4081 		return -EIO;
4082 	}
4083 	release_login_buffer(adapter);
4084 	complete(&adapter->init_done);
4085 
4086 	return 0;
4087 }
4088 
4089 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4090 				     struct ibmvnic_adapter *adapter)
4091 {
4092 	struct device *dev = &adapter->vdev->dev;
4093 	long rc;
4094 
4095 	rc = crq->request_unmap_rsp.rc.code;
4096 	if (rc)
4097 		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4098 }
4099 
4100 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4101 				 struct ibmvnic_adapter *adapter)
4102 {
4103 	struct net_device *netdev = adapter->netdev;
4104 	struct device *dev = &adapter->vdev->dev;
4105 	long rc;
4106 
4107 	rc = crq->query_map_rsp.rc.code;
4108 	if (rc) {
4109 		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4110 		return;
4111 	}
4112 	netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4113 		   crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4114 		   crq->query_map_rsp.free_pages);
4115 }
4116 
4117 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4118 				 struct ibmvnic_adapter *adapter)
4119 {
4120 	struct net_device *netdev = adapter->netdev;
4121 	struct device *dev = &adapter->vdev->dev;
4122 	long rc;
4123 
4124 	atomic_dec(&adapter->running_cap_crqs);
4125 	netdev_dbg(netdev, "Outstanding queries: %d\n",
4126 		   atomic_read(&adapter->running_cap_crqs));
4127 	rc = crq->query_capability.rc.code;
4128 	if (rc) {
4129 		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4130 		goto out;
4131 	}
4132 
4133 	switch (be16_to_cpu(crq->query_capability.capability)) {
4134 	case MIN_TX_QUEUES:
4135 		adapter->min_tx_queues =
4136 		    be64_to_cpu(crq->query_capability.number);
4137 		netdev_dbg(netdev, "min_tx_queues = %lld\n",
4138 			   adapter->min_tx_queues);
4139 		break;
4140 	case MIN_RX_QUEUES:
4141 		adapter->min_rx_queues =
4142 		    be64_to_cpu(crq->query_capability.number);
4143 		netdev_dbg(netdev, "min_rx_queues = %lld\n",
4144 			   adapter->min_rx_queues);
4145 		break;
4146 	case MIN_RX_ADD_QUEUES:
4147 		adapter->min_rx_add_queues =
4148 		    be64_to_cpu(crq->query_capability.number);
4149 		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4150 			   adapter->min_rx_add_queues);
4151 		break;
4152 	case MAX_TX_QUEUES:
4153 		adapter->max_tx_queues =
4154 		    be64_to_cpu(crq->query_capability.number);
4155 		netdev_dbg(netdev, "max_tx_queues = %lld\n",
4156 			   adapter->max_tx_queues);
4157 		break;
4158 	case MAX_RX_QUEUES:
4159 		adapter->max_rx_queues =
4160 		    be64_to_cpu(crq->query_capability.number);
4161 		netdev_dbg(netdev, "max_rx_queues = %lld\n",
4162 			   adapter->max_rx_queues);
4163 		break;
4164 	case MAX_RX_ADD_QUEUES:
4165 		adapter->max_rx_add_queues =
4166 		    be64_to_cpu(crq->query_capability.number);
4167 		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4168 			   adapter->max_rx_add_queues);
4169 		break;
4170 	case MIN_TX_ENTRIES_PER_SUBCRQ:
4171 		adapter->min_tx_entries_per_subcrq =
4172 		    be64_to_cpu(crq->query_capability.number);
4173 		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4174 			   adapter->min_tx_entries_per_subcrq);
4175 		break;
4176 	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4177 		adapter->min_rx_add_entries_per_subcrq =
4178 		    be64_to_cpu(crq->query_capability.number);
4179 		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4180 			   adapter->min_rx_add_entries_per_subcrq);
4181 		break;
4182 	case MAX_TX_ENTRIES_PER_SUBCRQ:
4183 		adapter->max_tx_entries_per_subcrq =
4184 		    be64_to_cpu(crq->query_capability.number);
4185 		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4186 			   adapter->max_tx_entries_per_subcrq);
4187 		break;
4188 	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4189 		adapter->max_rx_add_entries_per_subcrq =
4190 		    be64_to_cpu(crq->query_capability.number);
4191 		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4192 			   adapter->max_rx_add_entries_per_subcrq);
4193 		break;
4194 	case TCP_IP_OFFLOAD:
4195 		adapter->tcp_ip_offload =
4196 		    be64_to_cpu(crq->query_capability.number);
4197 		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4198 			   adapter->tcp_ip_offload);
4199 		break;
4200 	case PROMISC_SUPPORTED:
4201 		adapter->promisc_supported =
4202 		    be64_to_cpu(crq->query_capability.number);
4203 		netdev_dbg(netdev, "promisc_supported = %lld\n",
4204 			   adapter->promisc_supported);
4205 		break;
4206 	case MIN_MTU:
4207 		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4208 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4209 		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4210 		break;
4211 	case MAX_MTU:
4212 		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4213 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4214 		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4215 		break;
4216 	case MAX_MULTICAST_FILTERS:
4217 		adapter->max_multicast_filters =
4218 		    be64_to_cpu(crq->query_capability.number);
4219 		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4220 			   adapter->max_multicast_filters);
4221 		break;
4222 	case VLAN_HEADER_INSERTION:
4223 		adapter->vlan_header_insertion =
4224 		    be64_to_cpu(crq->query_capability.number);
4225 		if (adapter->vlan_header_insertion)
4226 			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4227 		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4228 			   adapter->vlan_header_insertion);
4229 		break;
4230 	case RX_VLAN_HEADER_INSERTION:
4231 		adapter->rx_vlan_header_insertion =
4232 		    be64_to_cpu(crq->query_capability.number);
4233 		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4234 			   adapter->rx_vlan_header_insertion);
4235 		break;
4236 	case MAX_TX_SG_ENTRIES:
4237 		adapter->max_tx_sg_entries =
4238 		    be64_to_cpu(crq->query_capability.number);
4239 		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4240 			   adapter->max_tx_sg_entries);
4241 		break;
4242 	case RX_SG_SUPPORTED:
4243 		adapter->rx_sg_supported =
4244 		    be64_to_cpu(crq->query_capability.number);
4245 		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4246 			   adapter->rx_sg_supported);
4247 		break;
4248 	case OPT_TX_COMP_SUB_QUEUES:
4249 		adapter->opt_tx_comp_sub_queues =
4250 		    be64_to_cpu(crq->query_capability.number);
4251 		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4252 			   adapter->opt_tx_comp_sub_queues);
4253 		break;
4254 	case OPT_RX_COMP_QUEUES:
4255 		adapter->opt_rx_comp_queues =
4256 		    be64_to_cpu(crq->query_capability.number);
4257 		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4258 			   adapter->opt_rx_comp_queues);
4259 		break;
4260 	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4261 		adapter->opt_rx_bufadd_q_per_rx_comp_q =
4262 		    be64_to_cpu(crq->query_capability.number);
4263 		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4264 			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
4265 		break;
4266 	case OPT_TX_ENTRIES_PER_SUBCRQ:
4267 		adapter->opt_tx_entries_per_subcrq =
4268 		    be64_to_cpu(crq->query_capability.number);
4269 		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4270 			   adapter->opt_tx_entries_per_subcrq);
4271 		break;
4272 	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4273 		adapter->opt_rxba_entries_per_subcrq =
4274 		    be64_to_cpu(crq->query_capability.number);
4275 		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4276 			   adapter->opt_rxba_entries_per_subcrq);
4277 		break;
4278 	case TX_RX_DESC_REQ:
4279 		adapter->tx_rx_desc_req = crq->query_capability.number;
4280 		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4281 			   adapter->tx_rx_desc_req);
4282 		break;
4283 
4284 	default:
4285 		netdev_err(netdev, "Got invalid cap rsp %d\n",
4286 			   crq->query_capability.capability);
4287 	}
4288 
4289 out:
4290 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4291 		adapter->wait_capability = false;
4292 		ibmvnic_send_req_caps(adapter, 0);
4293 	}
4294 }
4295 
4296 static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4297 {
4298 	union ibmvnic_crq crq;
4299 	int rc;
4300 
4301 	memset(&crq, 0, sizeof(crq));
4302 	crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4303 	crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
4304 	init_completion(&adapter->fw_done);
4305 	rc = ibmvnic_send_crq(adapter, &crq);
4306 	if (rc)
4307 		return rc;
4308 	wait_for_completion(&adapter->fw_done);
4309 	return adapter->fw_done_rc ? -EIO : 0;
4310 }
4311 
4312 static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4313 				       struct ibmvnic_adapter *adapter)
4314 {
4315 	struct net_device *netdev = adapter->netdev;
4316 	int rc;
4317 
4318 	rc = crq->query_phys_parms_rsp.rc.code;
4319 	if (rc) {
4320 		netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4321 		return rc;
4322 	}
4323 	switch (cpu_to_be32(crq->query_phys_parms_rsp.speed)) {
4324 	case IBMVNIC_10MBPS:
4325 		adapter->speed = SPEED_10;
4326 		break;
4327 	case IBMVNIC_100MBPS:
4328 		adapter->speed = SPEED_100;
4329 		break;
4330 	case IBMVNIC_1GBPS:
4331 		adapter->speed = SPEED_1000;
4332 		break;
4333 	case IBMVNIC_10GBP:
4334 		adapter->speed = SPEED_10000;
4335 		break;
4336 	case IBMVNIC_25GBPS:
4337 		adapter->speed = SPEED_25000;
4338 		break;
4339 	case IBMVNIC_40GBPS:
4340 		adapter->speed = SPEED_40000;
4341 		break;
4342 	case IBMVNIC_50GBPS:
4343 		adapter->speed = SPEED_50000;
4344 		break;
4345 	case IBMVNIC_100GBPS:
4346 		adapter->speed = SPEED_100000;
4347 		break;
4348 	default:
4349 		netdev_warn(netdev, "Unknown speed 0x%08x\n",
4350 			    cpu_to_be32(crq->query_phys_parms_rsp.speed));
4351 		adapter->speed = SPEED_UNKNOWN;
4352 	}
4353 	if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4354 		adapter->duplex = DUPLEX_FULL;
4355 	else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4356 		adapter->duplex = DUPLEX_HALF;
4357 	else
4358 		adapter->duplex = DUPLEX_UNKNOWN;
4359 
4360 	return rc;
4361 }
4362 
4363 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4364 			       struct ibmvnic_adapter *adapter)
4365 {
4366 	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4367 	struct net_device *netdev = adapter->netdev;
4368 	struct device *dev = &adapter->vdev->dev;
4369 	u64 *u64_crq = (u64 *)crq;
4370 	long rc;
4371 
4372 	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4373 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
4374 		   (unsigned long int)cpu_to_be64(u64_crq[1]));
4375 	switch (gen_crq->first) {
4376 	case IBMVNIC_CRQ_INIT_RSP:
4377 		switch (gen_crq->cmd) {
4378 		case IBMVNIC_CRQ_INIT:
4379 			dev_info(dev, "Partner initialized\n");
4380 			adapter->from_passive_init = true;
4381 			adapter->failover_pending = false;
4382 			if (!completion_done(&adapter->init_done)) {
4383 				complete(&adapter->init_done);
4384 				adapter->init_done_rc = -EIO;
4385 			}
4386 			ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4387 			break;
4388 		case IBMVNIC_CRQ_INIT_COMPLETE:
4389 			dev_info(dev, "Partner initialization complete\n");
4390 			adapter->crq.active = true;
4391 			send_version_xchg(adapter);
4392 			break;
4393 		default:
4394 			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4395 		}
4396 		return;
4397 	case IBMVNIC_CRQ_XPORT_EVENT:
4398 		netif_carrier_off(netdev);
4399 		adapter->crq.active = false;
4400 		if (adapter->resetting)
4401 			adapter->force_reset_recovery = true;
4402 		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4403 			dev_info(dev, "Migrated, re-enabling adapter\n");
4404 			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4405 		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4406 			dev_info(dev, "Backing device failover detected\n");
4407 			adapter->failover_pending = true;
4408 		} else {
4409 			/* The adapter lost the connection */
4410 			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4411 				gen_crq->cmd);
4412 			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4413 		}
4414 		return;
4415 	case IBMVNIC_CRQ_CMD_RSP:
4416 		break;
4417 	default:
4418 		dev_err(dev, "Got an invalid msg type 0x%02x\n",
4419 			gen_crq->first);
4420 		return;
4421 	}
4422 
4423 	switch (gen_crq->cmd) {
4424 	case VERSION_EXCHANGE_RSP:
4425 		rc = crq->version_exchange_rsp.rc.code;
4426 		if (rc) {
4427 			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4428 			break;
4429 		}
4430 		dev_info(dev, "Partner protocol version is %d\n",
4431 			 crq->version_exchange_rsp.version);
4432 		if (be16_to_cpu(crq->version_exchange_rsp.version) <
4433 		    ibmvnic_version)
4434 			ibmvnic_version =
4435 			    be16_to_cpu(crq->version_exchange_rsp.version);
4436 		send_cap_queries(adapter);
4437 		break;
4438 	case QUERY_CAPABILITY_RSP:
4439 		handle_query_cap_rsp(crq, adapter);
4440 		break;
4441 	case QUERY_MAP_RSP:
4442 		handle_query_map_rsp(crq, adapter);
4443 		break;
4444 	case REQUEST_MAP_RSP:
4445 		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4446 		complete(&adapter->fw_done);
4447 		break;
4448 	case REQUEST_UNMAP_RSP:
4449 		handle_request_unmap_rsp(crq, adapter);
4450 		break;
4451 	case REQUEST_CAPABILITY_RSP:
4452 		handle_request_cap_rsp(crq, adapter);
4453 		break;
4454 	case LOGIN_RSP:
4455 		netdev_dbg(netdev, "Got Login Response\n");
4456 		handle_login_rsp(crq, adapter);
4457 		break;
4458 	case LOGICAL_LINK_STATE_RSP:
4459 		netdev_dbg(netdev,
4460 			   "Got Logical Link State Response, state: %d rc: %d\n",
4461 			   crq->logical_link_state_rsp.link_state,
4462 			   crq->logical_link_state_rsp.rc.code);
4463 		adapter->logical_link_state =
4464 		    crq->logical_link_state_rsp.link_state;
4465 		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4466 		complete(&adapter->init_done);
4467 		break;
4468 	case LINK_STATE_INDICATION:
4469 		netdev_dbg(netdev, "Got Logical Link State Indication\n");
4470 		adapter->phys_link_state =
4471 		    crq->link_state_indication.phys_link_state;
4472 		adapter->logical_link_state =
4473 		    crq->link_state_indication.logical_link_state;
4474 		break;
4475 	case CHANGE_MAC_ADDR_RSP:
4476 		netdev_dbg(netdev, "Got MAC address change Response\n");
4477 		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4478 		break;
4479 	case ERROR_INDICATION:
4480 		netdev_dbg(netdev, "Got Error Indication\n");
4481 		handle_error_indication(crq, adapter);
4482 		break;
4483 	case REQUEST_STATISTICS_RSP:
4484 		netdev_dbg(netdev, "Got Statistics Response\n");
4485 		complete(&adapter->stats_done);
4486 		break;
4487 	case QUERY_IP_OFFLOAD_RSP:
4488 		netdev_dbg(netdev, "Got Query IP offload Response\n");
4489 		handle_query_ip_offload_rsp(adapter);
4490 		break;
4491 	case MULTICAST_CTRL_RSP:
4492 		netdev_dbg(netdev, "Got multicast control Response\n");
4493 		break;
4494 	case CONTROL_IP_OFFLOAD_RSP:
4495 		netdev_dbg(netdev, "Got Control IP offload Response\n");
4496 		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4497 				 sizeof(adapter->ip_offload_ctrl),
4498 				 DMA_TO_DEVICE);
4499 		complete(&adapter->init_done);
4500 		break;
4501 	case COLLECT_FW_TRACE_RSP:
4502 		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4503 		complete(&adapter->fw_done);
4504 		break;
4505 	case GET_VPD_SIZE_RSP:
4506 		handle_vpd_size_rsp(crq, adapter);
4507 		break;
4508 	case GET_VPD_RSP:
4509 		handle_vpd_rsp(crq, adapter);
4510 		break;
4511 	case QUERY_PHYS_PARMS_RSP:
4512 		adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4513 		complete(&adapter->fw_done);
4514 		break;
4515 	default:
4516 		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4517 			   gen_crq->cmd);
4518 	}
4519 }
4520 
4521 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4522 {
4523 	struct ibmvnic_adapter *adapter = instance;
4524 
4525 	tasklet_schedule(&adapter->tasklet);
4526 	return IRQ_HANDLED;
4527 }
4528 
4529 static void ibmvnic_tasklet(void *data)
4530 {
4531 	struct ibmvnic_adapter *adapter = data;
4532 	struct ibmvnic_crq_queue *queue = &adapter->crq;
4533 	union ibmvnic_crq *crq;
4534 	unsigned long flags;
4535 	bool done = false;
4536 
4537 	spin_lock_irqsave(&queue->lock, flags);
4538 	while (!done) {
4539 		/* Pull all the valid messages off the CRQ */
4540 		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4541 			ibmvnic_handle_crq(crq, adapter);
4542 			crq->generic.first = 0;
4543 		}
4544 
4545 		/* remain in tasklet until all
4546 		 * capabilities responses are received
4547 		 */
4548 		if (!adapter->wait_capability)
4549 			done = true;
4550 	}
4551 	/* if capabilities CRQ's were sent in this tasklet, the following
4552 	 * tasklet must wait until all responses are received
4553 	 */
4554 	if (atomic_read(&adapter->running_cap_crqs) != 0)
4555 		adapter->wait_capability = true;
4556 	spin_unlock_irqrestore(&queue->lock, flags);
4557 }
4558 
4559 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4560 {
4561 	struct vio_dev *vdev = adapter->vdev;
4562 	int rc;
4563 
4564 	do {
4565 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4566 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4567 
4568 	if (rc)
4569 		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4570 
4571 	return rc;
4572 }
4573 
4574 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4575 {
4576 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4577 	struct device *dev = &adapter->vdev->dev;
4578 	struct vio_dev *vdev = adapter->vdev;
4579 	int rc;
4580 
4581 	/* Close the CRQ */
4582 	do {
4583 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4584 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4585 
4586 	/* Clean out the queue */
4587 	memset(crq->msgs, 0, PAGE_SIZE);
4588 	crq->cur = 0;
4589 	crq->active = false;
4590 
4591 	/* And re-open it again */
4592 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4593 				crq->msg_token, PAGE_SIZE);
4594 
4595 	if (rc == H_CLOSED)
4596 		/* Adapter is good, but other end is not ready */
4597 		dev_warn(dev, "Partner adapter not ready\n");
4598 	else if (rc != 0)
4599 		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4600 
4601 	return rc;
4602 }
4603 
4604 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4605 {
4606 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4607 	struct vio_dev *vdev = adapter->vdev;
4608 	long rc;
4609 
4610 	if (!crq->msgs)
4611 		return;
4612 
4613 	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4614 	free_irq(vdev->irq, adapter);
4615 	tasklet_kill(&adapter->tasklet);
4616 	do {
4617 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4618 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4619 
4620 	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4621 			 DMA_BIDIRECTIONAL);
4622 	free_page((unsigned long)crq->msgs);
4623 	crq->msgs = NULL;
4624 	crq->active = false;
4625 }
4626 
4627 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4628 {
4629 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4630 	struct device *dev = &adapter->vdev->dev;
4631 	struct vio_dev *vdev = adapter->vdev;
4632 	int rc, retrc = -ENOMEM;
4633 
4634 	if (crq->msgs)
4635 		return 0;
4636 
4637 	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4638 	/* Should we allocate more than one page? */
4639 
4640 	if (!crq->msgs)
4641 		return -ENOMEM;
4642 
4643 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4644 	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4645 					DMA_BIDIRECTIONAL);
4646 	if (dma_mapping_error(dev, crq->msg_token))
4647 		goto map_failed;
4648 
4649 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4650 				crq->msg_token, PAGE_SIZE);
4651 
4652 	if (rc == H_RESOURCE)
4653 		/* maybe kexecing and resource is busy. try a reset */
4654 		rc = ibmvnic_reset_crq(adapter);
4655 	retrc = rc;
4656 
4657 	if (rc == H_CLOSED) {
4658 		dev_warn(dev, "Partner adapter not ready\n");
4659 	} else if (rc) {
4660 		dev_warn(dev, "Error %d opening adapter\n", rc);
4661 		goto reg_crq_failed;
4662 	}
4663 
4664 	retrc = 0;
4665 
4666 	tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4667 		     (unsigned long)adapter);
4668 
4669 	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4670 	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4671 			 adapter);
4672 	if (rc) {
4673 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4674 			vdev->irq, rc);
4675 		goto req_irq_failed;
4676 	}
4677 
4678 	rc = vio_enable_interrupts(vdev);
4679 	if (rc) {
4680 		dev_err(dev, "Error %d enabling interrupts\n", rc);
4681 		goto req_irq_failed;
4682 	}
4683 
4684 	crq->cur = 0;
4685 	spin_lock_init(&crq->lock);
4686 
4687 	return retrc;
4688 
4689 req_irq_failed:
4690 	tasklet_kill(&adapter->tasklet);
4691 	do {
4692 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4693 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4694 reg_crq_failed:
4695 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4696 map_failed:
4697 	free_page((unsigned long)crq->msgs);
4698 	crq->msgs = NULL;
4699 	return retrc;
4700 }
4701 
4702 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
4703 {
4704 	struct device *dev = &adapter->vdev->dev;
4705 	unsigned long timeout = msecs_to_jiffies(30000);
4706 	u64 old_num_rx_queues, old_num_tx_queues;
4707 	int rc;
4708 
4709 	adapter->from_passive_init = false;
4710 
4711 	old_num_rx_queues = adapter->req_rx_queues;
4712 	old_num_tx_queues = adapter->req_tx_queues;
4713 
4714 	reinit_completion(&adapter->init_done);
4715 	adapter->init_done_rc = 0;
4716 	ibmvnic_send_crq_init(adapter);
4717 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4718 		dev_err(dev, "Initialization sequence timed out\n");
4719 		return -1;
4720 	}
4721 
4722 	if (adapter->init_done_rc) {
4723 		release_crq_queue(adapter);
4724 		return adapter->init_done_rc;
4725 	}
4726 
4727 	if (adapter->from_passive_init) {
4728 		adapter->state = VNIC_OPEN;
4729 		adapter->from_passive_init = false;
4730 		return -1;
4731 	}
4732 
4733 	if (adapter->resetting && !adapter->wait_for_reset &&
4734 	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
4735 		if (adapter->req_rx_queues != old_num_rx_queues ||
4736 		    adapter->req_tx_queues != old_num_tx_queues) {
4737 			release_sub_crqs(adapter, 0);
4738 			rc = init_sub_crqs(adapter);
4739 		} else {
4740 			rc = reset_sub_crq_queues(adapter);
4741 		}
4742 	} else {
4743 		rc = init_sub_crqs(adapter);
4744 	}
4745 
4746 	if (rc) {
4747 		dev_err(dev, "Initialization of sub crqs failed\n");
4748 		release_crq_queue(adapter);
4749 		return rc;
4750 	}
4751 
4752 	rc = init_sub_crq_irqs(adapter);
4753 	if (rc) {
4754 		dev_err(dev, "Failed to initialize sub crq irqs\n");
4755 		release_crq_queue(adapter);
4756 	}
4757 
4758 	return rc;
4759 }
4760 
4761 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4762 {
4763 	struct device *dev = &adapter->vdev->dev;
4764 	unsigned long timeout = msecs_to_jiffies(30000);
4765 	int rc;
4766 
4767 	adapter->from_passive_init = false;
4768 
4769 	adapter->init_done_rc = 0;
4770 	ibmvnic_send_crq_init(adapter);
4771 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4772 		dev_err(dev, "Initialization sequence timed out\n");
4773 		return -1;
4774 	}
4775 
4776 	if (adapter->init_done_rc) {
4777 		release_crq_queue(adapter);
4778 		return adapter->init_done_rc;
4779 	}
4780 
4781 	if (adapter->from_passive_init) {
4782 		adapter->state = VNIC_OPEN;
4783 		adapter->from_passive_init = false;
4784 		return -1;
4785 	}
4786 
4787 	rc = init_sub_crqs(adapter);
4788 	if (rc) {
4789 		dev_err(dev, "Initialization of sub crqs failed\n");
4790 		release_crq_queue(adapter);
4791 		return rc;
4792 	}
4793 
4794 	rc = init_sub_crq_irqs(adapter);
4795 	if (rc) {
4796 		dev_err(dev, "Failed to initialize sub crq irqs\n");
4797 		release_crq_queue(adapter);
4798 	}
4799 
4800 	return rc;
4801 }
4802 
4803 static struct device_attribute dev_attr_failover;
4804 
4805 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4806 {
4807 	struct ibmvnic_adapter *adapter;
4808 	struct net_device *netdev;
4809 	unsigned char *mac_addr_p;
4810 	int rc;
4811 
4812 	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4813 		dev->unit_address);
4814 
4815 	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4816 							VETH_MAC_ADDR, NULL);
4817 	if (!mac_addr_p) {
4818 		dev_err(&dev->dev,
4819 			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4820 			__FILE__, __LINE__);
4821 		return 0;
4822 	}
4823 
4824 	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4825 				   IBMVNIC_MAX_QUEUES);
4826 	if (!netdev)
4827 		return -ENOMEM;
4828 
4829 	adapter = netdev_priv(netdev);
4830 	adapter->state = VNIC_PROBING;
4831 	dev_set_drvdata(&dev->dev, netdev);
4832 	adapter->vdev = dev;
4833 	adapter->netdev = netdev;
4834 
4835 	ether_addr_copy(adapter->mac_addr, mac_addr_p);
4836 	ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4837 	netdev->irq = dev->irq;
4838 	netdev->netdev_ops = &ibmvnic_netdev_ops;
4839 	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4840 	SET_NETDEV_DEV(netdev, &dev->dev);
4841 
4842 	spin_lock_init(&adapter->stats_lock);
4843 
4844 	INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4845 	INIT_LIST_HEAD(&adapter->rwi_list);
4846 	spin_lock_init(&adapter->rwi_lock);
4847 	init_completion(&adapter->init_done);
4848 	adapter->resetting = false;
4849 
4850 	adapter->mac_change_pending = false;
4851 
4852 	do {
4853 		rc = init_crq_queue(adapter);
4854 		if (rc) {
4855 			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4856 				rc);
4857 			goto ibmvnic_init_fail;
4858 		}
4859 
4860 		rc = ibmvnic_init(adapter);
4861 		if (rc && rc != EAGAIN)
4862 			goto ibmvnic_init_fail;
4863 	} while (rc == EAGAIN);
4864 
4865 	rc = init_stats_buffers(adapter);
4866 	if (rc)
4867 		goto ibmvnic_init_fail;
4868 
4869 	rc = init_stats_token(adapter);
4870 	if (rc)
4871 		goto ibmvnic_stats_fail;
4872 
4873 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4874 	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4875 	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4876 
4877 	rc = device_create_file(&dev->dev, &dev_attr_failover);
4878 	if (rc)
4879 		goto ibmvnic_dev_file_err;
4880 
4881 	netif_carrier_off(netdev);
4882 	rc = register_netdev(netdev);
4883 	if (rc) {
4884 		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4885 		goto ibmvnic_register_fail;
4886 	}
4887 	dev_info(&dev->dev, "ibmvnic registered\n");
4888 
4889 	adapter->state = VNIC_PROBED;
4890 
4891 	adapter->wait_for_reset = false;
4892 
4893 	return 0;
4894 
4895 ibmvnic_register_fail:
4896 	device_remove_file(&dev->dev, &dev_attr_failover);
4897 
4898 ibmvnic_dev_file_err:
4899 	release_stats_token(adapter);
4900 
4901 ibmvnic_stats_fail:
4902 	release_stats_buffers(adapter);
4903 
4904 ibmvnic_init_fail:
4905 	release_sub_crqs(adapter, 1);
4906 	release_crq_queue(adapter);
4907 	free_netdev(netdev);
4908 
4909 	return rc;
4910 }
4911 
4912 static int ibmvnic_remove(struct vio_dev *dev)
4913 {
4914 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
4915 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4916 
4917 	adapter->state = VNIC_REMOVING;
4918 	rtnl_lock();
4919 	unregister_netdevice(netdev);
4920 
4921 	release_resources(adapter);
4922 	release_sub_crqs(adapter, 1);
4923 	release_crq_queue(adapter);
4924 
4925 	release_stats_token(adapter);
4926 	release_stats_buffers(adapter);
4927 
4928 	adapter->state = VNIC_REMOVED;
4929 
4930 	rtnl_unlock();
4931 	device_remove_file(&dev->dev, &dev_attr_failover);
4932 	free_netdev(netdev);
4933 	dev_set_drvdata(&dev->dev, NULL);
4934 
4935 	return 0;
4936 }
4937 
4938 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4939 			      const char *buf, size_t count)
4940 {
4941 	struct net_device *netdev = dev_get_drvdata(dev);
4942 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4943 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4944 	__be64 session_token;
4945 	long rc;
4946 
4947 	if (!sysfs_streq(buf, "1"))
4948 		return -EINVAL;
4949 
4950 	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4951 			 H_GET_SESSION_TOKEN, 0, 0, 0);
4952 	if (rc) {
4953 		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4954 			   rc);
4955 		return -EINVAL;
4956 	}
4957 
4958 	session_token = (__be64)retbuf[0];
4959 	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4960 		   be64_to_cpu(session_token));
4961 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4962 				H_SESSION_ERR_DETECTED, session_token, 0, 0);
4963 	if (rc) {
4964 		netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4965 			   rc);
4966 		return -EINVAL;
4967 	}
4968 
4969 	return count;
4970 }
4971 
4972 static DEVICE_ATTR_WO(failover);
4973 
4974 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4975 {
4976 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4977 	struct ibmvnic_adapter *adapter;
4978 	struct iommu_table *tbl;
4979 	unsigned long ret = 0;
4980 	int i;
4981 
4982 	tbl = get_iommu_table_base(&vdev->dev);
4983 
4984 	/* netdev inits at probe time along with the structures we need below*/
4985 	if (!netdev)
4986 		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4987 
4988 	adapter = netdev_priv(netdev);
4989 
4990 	ret += PAGE_SIZE; /* the crq message queue */
4991 	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4992 
4993 	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4994 		ret += 4 * PAGE_SIZE; /* the scrq message queue */
4995 
4996 	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4997 	     i++)
4998 		ret += adapter->rx_pool[i].size *
4999 		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5000 
5001 	return ret;
5002 }
5003 
5004 static int ibmvnic_resume(struct device *dev)
5005 {
5006 	struct net_device *netdev = dev_get_drvdata(dev);
5007 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5008 
5009 	if (adapter->state != VNIC_OPEN)
5010 		return 0;
5011 
5012 	tasklet_schedule(&adapter->tasklet);
5013 
5014 	return 0;
5015 }
5016 
5017 static const struct vio_device_id ibmvnic_device_table[] = {
5018 	{"network", "IBM,vnic"},
5019 	{"", "" }
5020 };
5021 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5022 
5023 static const struct dev_pm_ops ibmvnic_pm_ops = {
5024 	.resume = ibmvnic_resume
5025 };
5026 
5027 static struct vio_driver ibmvnic_driver = {
5028 	.id_table       = ibmvnic_device_table,
5029 	.probe          = ibmvnic_probe,
5030 	.remove         = ibmvnic_remove,
5031 	.get_desired_dma = ibmvnic_get_desired_dma,
5032 	.name		= ibmvnic_driver_name,
5033 	.pm		= &ibmvnic_pm_ops,
5034 };
5035 
5036 /* module functions */
5037 static int __init ibmvnic_module_init(void)
5038 {
5039 	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5040 		IBMVNIC_DRIVER_VERSION);
5041 
5042 	return vio_register_driver(&ibmvnic_driver);
5043 }
5044 
5045 static void __exit ibmvnic_module_exit(void)
5046 {
5047 	vio_unregister_driver(&ibmvnic_driver);
5048 }
5049 
5050 module_init(ibmvnic_module_init);
5051 module_exit(ibmvnic_module_exit);
5052