xref: /openbmc/linux/drivers/net/ethernet/ibm/ibmvnic.c (revision 1fa0a7dc)
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 *, u8 *);
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 	__ibmvnic_set_mac(netdev, adapter->mac_addr);
853 
854 	return 0;
855 }
856 
857 static void release_login_buffer(struct ibmvnic_adapter *adapter)
858 {
859 	kfree(adapter->login_buf);
860 	adapter->login_buf = NULL;
861 }
862 
863 static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
864 {
865 	kfree(adapter->login_rsp_buf);
866 	adapter->login_rsp_buf = NULL;
867 }
868 
869 static void release_resources(struct ibmvnic_adapter *adapter)
870 {
871 	release_vpd_data(adapter);
872 
873 	release_tx_pools(adapter);
874 	release_rx_pools(adapter);
875 
876 	release_napi(adapter);
877 	release_login_rsp_buffer(adapter);
878 }
879 
880 static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
881 {
882 	struct net_device *netdev = adapter->netdev;
883 	unsigned long timeout = msecs_to_jiffies(30000);
884 	union ibmvnic_crq crq;
885 	bool resend;
886 	int rc;
887 
888 	netdev_dbg(netdev, "setting link state %d\n", link_state);
889 
890 	memset(&crq, 0, sizeof(crq));
891 	crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
892 	crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
893 	crq.logical_link_state.link_state = link_state;
894 
895 	do {
896 		resend = false;
897 
898 		reinit_completion(&adapter->init_done);
899 		rc = ibmvnic_send_crq(adapter, &crq);
900 		if (rc) {
901 			netdev_err(netdev, "Failed to set link state\n");
902 			return rc;
903 		}
904 
905 		if (!wait_for_completion_timeout(&adapter->init_done,
906 						 timeout)) {
907 			netdev_err(netdev, "timeout setting link state\n");
908 			return -1;
909 		}
910 
911 		if (adapter->init_done_rc == 1) {
912 			/* Partuial success, delay and re-send */
913 			mdelay(1000);
914 			resend = true;
915 		} else if (adapter->init_done_rc) {
916 			netdev_warn(netdev, "Unable to set link state, rc=%d\n",
917 				    adapter->init_done_rc);
918 			return adapter->init_done_rc;
919 		}
920 	} while (resend);
921 
922 	return 0;
923 }
924 
925 static int set_real_num_queues(struct net_device *netdev)
926 {
927 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
928 	int rc;
929 
930 	netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
931 		   adapter->req_tx_queues, adapter->req_rx_queues);
932 
933 	rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
934 	if (rc) {
935 		netdev_err(netdev, "failed to set the number of tx queues\n");
936 		return rc;
937 	}
938 
939 	rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
940 	if (rc)
941 		netdev_err(netdev, "failed to set the number of rx queues\n");
942 
943 	return rc;
944 }
945 
946 static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
947 {
948 	struct device *dev = &adapter->vdev->dev;
949 	union ibmvnic_crq crq;
950 	int len = 0;
951 	int rc;
952 
953 	if (adapter->vpd->buff)
954 		len = adapter->vpd->len;
955 
956 	init_completion(&adapter->fw_done);
957 	crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
958 	crq.get_vpd_size.cmd = GET_VPD_SIZE;
959 	rc = ibmvnic_send_crq(adapter, &crq);
960 	if (rc)
961 		return rc;
962 	wait_for_completion(&adapter->fw_done);
963 
964 	if (!adapter->vpd->len)
965 		return -ENODATA;
966 
967 	if (!adapter->vpd->buff)
968 		adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
969 	else if (adapter->vpd->len != len)
970 		adapter->vpd->buff =
971 			krealloc(adapter->vpd->buff,
972 				 adapter->vpd->len, GFP_KERNEL);
973 
974 	if (!adapter->vpd->buff) {
975 		dev_err(dev, "Could allocate VPD buffer\n");
976 		return -ENOMEM;
977 	}
978 
979 	adapter->vpd->dma_addr =
980 		dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
981 			       DMA_FROM_DEVICE);
982 	if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
983 		dev_err(dev, "Could not map VPD buffer\n");
984 		kfree(adapter->vpd->buff);
985 		adapter->vpd->buff = NULL;
986 		return -ENOMEM;
987 	}
988 
989 	reinit_completion(&adapter->fw_done);
990 	crq.get_vpd.first = IBMVNIC_CRQ_CMD;
991 	crq.get_vpd.cmd = GET_VPD;
992 	crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
993 	crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
994 	rc = ibmvnic_send_crq(adapter, &crq);
995 	if (rc) {
996 		kfree(adapter->vpd->buff);
997 		adapter->vpd->buff = NULL;
998 		return rc;
999 	}
1000 	wait_for_completion(&adapter->fw_done);
1001 
1002 	return 0;
1003 }
1004 
1005 static int init_resources(struct ibmvnic_adapter *adapter)
1006 {
1007 	struct net_device *netdev = adapter->netdev;
1008 	int rc;
1009 
1010 	rc = set_real_num_queues(netdev);
1011 	if (rc)
1012 		return rc;
1013 
1014 	adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1015 	if (!adapter->vpd)
1016 		return -ENOMEM;
1017 
1018 	/* Vital Product Data (VPD) */
1019 	rc = ibmvnic_get_vpd(adapter);
1020 	if (rc) {
1021 		netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1022 		return rc;
1023 	}
1024 
1025 	adapter->map_id = 1;
1026 
1027 	rc = init_napi(adapter);
1028 	if (rc)
1029 		return rc;
1030 
1031 	send_map_query(adapter);
1032 
1033 	rc = init_rx_pools(netdev);
1034 	if (rc)
1035 		return rc;
1036 
1037 	rc = init_tx_pools(netdev);
1038 	return rc;
1039 }
1040 
1041 static int __ibmvnic_open(struct net_device *netdev)
1042 {
1043 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1044 	enum vnic_state prev_state = adapter->state;
1045 	int i, rc;
1046 
1047 	adapter->state = VNIC_OPENING;
1048 	replenish_pools(adapter);
1049 	ibmvnic_napi_enable(adapter);
1050 
1051 	/* We're ready to receive frames, enable the sub-crq interrupts and
1052 	 * set the logical link state to up
1053 	 */
1054 	for (i = 0; i < adapter->req_rx_queues; i++) {
1055 		netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1056 		if (prev_state == VNIC_CLOSED)
1057 			enable_irq(adapter->rx_scrq[i]->irq);
1058 		enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1059 	}
1060 
1061 	for (i = 0; i < adapter->req_tx_queues; i++) {
1062 		netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1063 		if (prev_state == VNIC_CLOSED)
1064 			enable_irq(adapter->tx_scrq[i]->irq);
1065 		enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1066 	}
1067 
1068 	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1069 	if (rc) {
1070 		for (i = 0; i < adapter->req_rx_queues; i++)
1071 			napi_disable(&adapter->napi[i]);
1072 		release_resources(adapter);
1073 		return rc;
1074 	}
1075 
1076 	netif_tx_start_all_queues(netdev);
1077 
1078 	if (prev_state == VNIC_CLOSED) {
1079 		for (i = 0; i < adapter->req_rx_queues; i++)
1080 			napi_schedule(&adapter->napi[i]);
1081 	}
1082 
1083 	adapter->state = VNIC_OPEN;
1084 	return rc;
1085 }
1086 
1087 static int ibmvnic_open(struct net_device *netdev)
1088 {
1089 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1090 	int rc;
1091 
1092 	/* If device failover is pending, just set device state and return.
1093 	 * Device operation will be handled by reset routine.
1094 	 */
1095 	if (adapter->failover_pending) {
1096 		adapter->state = VNIC_OPEN;
1097 		return 0;
1098 	}
1099 
1100 	if (adapter->state != VNIC_CLOSED) {
1101 		rc = ibmvnic_login(netdev);
1102 		if (rc)
1103 			return rc;
1104 
1105 		rc = init_resources(adapter);
1106 		if (rc) {
1107 			netdev_err(netdev, "failed to initialize resources\n");
1108 			release_resources(adapter);
1109 			return rc;
1110 		}
1111 	}
1112 
1113 	rc = __ibmvnic_open(netdev);
1114 
1115 	return rc;
1116 }
1117 
1118 static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1119 {
1120 	struct ibmvnic_rx_pool *rx_pool;
1121 	struct ibmvnic_rx_buff *rx_buff;
1122 	u64 rx_entries;
1123 	int rx_scrqs;
1124 	int i, j;
1125 
1126 	if (!adapter->rx_pool)
1127 		return;
1128 
1129 	rx_scrqs = adapter->num_active_rx_pools;
1130 	rx_entries = adapter->req_rx_add_entries_per_subcrq;
1131 
1132 	/* Free any remaining skbs in the rx buffer pools */
1133 	for (i = 0; i < rx_scrqs; i++) {
1134 		rx_pool = &adapter->rx_pool[i];
1135 		if (!rx_pool || !rx_pool->rx_buff)
1136 			continue;
1137 
1138 		netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1139 		for (j = 0; j < rx_entries; j++) {
1140 			rx_buff = &rx_pool->rx_buff[j];
1141 			if (rx_buff && rx_buff->skb) {
1142 				dev_kfree_skb_any(rx_buff->skb);
1143 				rx_buff->skb = NULL;
1144 			}
1145 		}
1146 	}
1147 }
1148 
1149 static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1150 			      struct ibmvnic_tx_pool *tx_pool)
1151 {
1152 	struct ibmvnic_tx_buff *tx_buff;
1153 	u64 tx_entries;
1154 	int i;
1155 
1156 	if (!tx_pool || !tx_pool->tx_buff)
1157 		return;
1158 
1159 	tx_entries = tx_pool->num_buffers;
1160 
1161 	for (i = 0; i < tx_entries; i++) {
1162 		tx_buff = &tx_pool->tx_buff[i];
1163 		if (tx_buff && tx_buff->skb) {
1164 			dev_kfree_skb_any(tx_buff->skb);
1165 			tx_buff->skb = NULL;
1166 		}
1167 	}
1168 }
1169 
1170 static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1171 {
1172 	int tx_scrqs;
1173 	int i;
1174 
1175 	if (!adapter->tx_pool || !adapter->tso_pool)
1176 		return;
1177 
1178 	tx_scrqs = adapter->num_active_tx_pools;
1179 
1180 	/* Free any remaining skbs in the tx buffer pools */
1181 	for (i = 0; i < tx_scrqs; i++) {
1182 		netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1183 		clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1184 		clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1185 	}
1186 }
1187 
1188 static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1189 {
1190 	struct net_device *netdev = adapter->netdev;
1191 	int i;
1192 
1193 	if (adapter->tx_scrq) {
1194 		for (i = 0; i < adapter->req_tx_queues; i++)
1195 			if (adapter->tx_scrq[i]->irq) {
1196 				netdev_dbg(netdev,
1197 					   "Disabling tx_scrq[%d] irq\n", i);
1198 				disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1199 				disable_irq(adapter->tx_scrq[i]->irq);
1200 			}
1201 	}
1202 
1203 	if (adapter->rx_scrq) {
1204 		for (i = 0; i < adapter->req_rx_queues; i++) {
1205 			if (adapter->rx_scrq[i]->irq) {
1206 				netdev_dbg(netdev,
1207 					   "Disabling rx_scrq[%d] irq\n", i);
1208 				disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1209 				disable_irq(adapter->rx_scrq[i]->irq);
1210 			}
1211 		}
1212 	}
1213 }
1214 
1215 static void ibmvnic_cleanup(struct net_device *netdev)
1216 {
1217 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1218 
1219 	/* ensure that transmissions are stopped if called by do_reset */
1220 	if (adapter->resetting)
1221 		netif_tx_disable(netdev);
1222 	else
1223 		netif_tx_stop_all_queues(netdev);
1224 
1225 	ibmvnic_napi_disable(adapter);
1226 	ibmvnic_disable_irqs(adapter);
1227 
1228 	clean_rx_pools(adapter);
1229 	clean_tx_pools(adapter);
1230 }
1231 
1232 static int __ibmvnic_close(struct net_device *netdev)
1233 {
1234 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1235 	int rc = 0;
1236 
1237 	adapter->state = VNIC_CLOSING;
1238 	rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1239 	if (rc)
1240 		return rc;
1241 	adapter->state = VNIC_CLOSED;
1242 	return 0;
1243 }
1244 
1245 static int ibmvnic_close(struct net_device *netdev)
1246 {
1247 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1248 	int rc;
1249 
1250 	/* If device failover is pending, just set device state and return.
1251 	 * Device operation will be handled by reset routine.
1252 	 */
1253 	if (adapter->failover_pending) {
1254 		adapter->state = VNIC_CLOSED;
1255 		return 0;
1256 	}
1257 
1258 	rc = __ibmvnic_close(netdev);
1259 	ibmvnic_cleanup(netdev);
1260 
1261 	return rc;
1262 }
1263 
1264 /**
1265  * build_hdr_data - creates L2/L3/L4 header data buffer
1266  * @hdr_field - bitfield determining needed headers
1267  * @skb - socket buffer
1268  * @hdr_len - array of header lengths
1269  * @tot_len - total length of data
1270  *
1271  * Reads hdr_field to determine which headers are needed by firmware.
1272  * Builds a buffer containing these headers.  Saves individual header
1273  * lengths and total buffer length to be used to build descriptors.
1274  */
1275 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1276 			  int *hdr_len, u8 *hdr_data)
1277 {
1278 	int len = 0;
1279 	u8 *hdr;
1280 
1281 	if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1282 		hdr_len[0] = sizeof(struct vlan_ethhdr);
1283 	else
1284 		hdr_len[0] = sizeof(struct ethhdr);
1285 
1286 	if (skb->protocol == htons(ETH_P_IP)) {
1287 		hdr_len[1] = ip_hdr(skb)->ihl * 4;
1288 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1289 			hdr_len[2] = tcp_hdrlen(skb);
1290 		else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1291 			hdr_len[2] = sizeof(struct udphdr);
1292 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1293 		hdr_len[1] = sizeof(struct ipv6hdr);
1294 		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1295 			hdr_len[2] = tcp_hdrlen(skb);
1296 		else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1297 			hdr_len[2] = sizeof(struct udphdr);
1298 	} else if (skb->protocol == htons(ETH_P_ARP)) {
1299 		hdr_len[1] = arp_hdr_len(skb->dev);
1300 		hdr_len[2] = 0;
1301 	}
1302 
1303 	memset(hdr_data, 0, 120);
1304 	if ((hdr_field >> 6) & 1) {
1305 		hdr = skb_mac_header(skb);
1306 		memcpy(hdr_data, hdr, hdr_len[0]);
1307 		len += hdr_len[0];
1308 	}
1309 
1310 	if ((hdr_field >> 5) & 1) {
1311 		hdr = skb_network_header(skb);
1312 		memcpy(hdr_data + len, hdr, hdr_len[1]);
1313 		len += hdr_len[1];
1314 	}
1315 
1316 	if ((hdr_field >> 4) & 1) {
1317 		hdr = skb_transport_header(skb);
1318 		memcpy(hdr_data + len, hdr, hdr_len[2]);
1319 		len += hdr_len[2];
1320 	}
1321 	return len;
1322 }
1323 
1324 /**
1325  * create_hdr_descs - create header and header extension descriptors
1326  * @hdr_field - bitfield determining needed headers
1327  * @data - buffer containing header data
1328  * @len - length of data buffer
1329  * @hdr_len - array of individual header lengths
1330  * @scrq_arr - descriptor array
1331  *
1332  * Creates header and, if needed, header extension descriptors and
1333  * places them in a descriptor array, scrq_arr
1334  */
1335 
1336 static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1337 			    union sub_crq *scrq_arr)
1338 {
1339 	union sub_crq hdr_desc;
1340 	int tmp_len = len;
1341 	int num_descs = 0;
1342 	u8 *data, *cur;
1343 	int tmp;
1344 
1345 	while (tmp_len > 0) {
1346 		cur = hdr_data + len - tmp_len;
1347 
1348 		memset(&hdr_desc, 0, sizeof(hdr_desc));
1349 		if (cur != hdr_data) {
1350 			data = hdr_desc.hdr_ext.data;
1351 			tmp = tmp_len > 29 ? 29 : tmp_len;
1352 			hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1353 			hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1354 			hdr_desc.hdr_ext.len = tmp;
1355 		} else {
1356 			data = hdr_desc.hdr.data;
1357 			tmp = tmp_len > 24 ? 24 : tmp_len;
1358 			hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1359 			hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1360 			hdr_desc.hdr.len = tmp;
1361 			hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1362 			hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1363 			hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1364 			hdr_desc.hdr.flag = hdr_field << 1;
1365 		}
1366 		memcpy(data, cur, tmp);
1367 		tmp_len -= tmp;
1368 		*scrq_arr = hdr_desc;
1369 		scrq_arr++;
1370 		num_descs++;
1371 	}
1372 
1373 	return num_descs;
1374 }
1375 
1376 /**
1377  * build_hdr_descs_arr - build a header descriptor array
1378  * @skb - socket buffer
1379  * @num_entries - number of descriptors to be sent
1380  * @subcrq - first TX descriptor
1381  * @hdr_field - bit field determining which headers will be sent
1382  *
1383  * This function will build a TX descriptor array with applicable
1384  * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1385  */
1386 
1387 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1388 				int *num_entries, u8 hdr_field)
1389 {
1390 	int hdr_len[3] = {0, 0, 0};
1391 	int tot_len;
1392 	u8 *hdr_data = txbuff->hdr_data;
1393 
1394 	tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1395 				 txbuff->hdr_data);
1396 	*num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1397 			 txbuff->indir_arr + 1);
1398 }
1399 
1400 static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1401 				    struct net_device *netdev)
1402 {
1403 	/* For some backing devices, mishandling of small packets
1404 	 * can result in a loss of connection or TX stall. Device
1405 	 * architects recommend that no packet should be smaller
1406 	 * than the minimum MTU value provided to the driver, so
1407 	 * pad any packets to that length
1408 	 */
1409 	if (skb->len < netdev->min_mtu)
1410 		return skb_put_padto(skb, netdev->min_mtu);
1411 
1412 	return 0;
1413 }
1414 
1415 static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1416 {
1417 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1418 	int queue_num = skb_get_queue_mapping(skb);
1419 	u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1420 	struct device *dev = &adapter->vdev->dev;
1421 	struct ibmvnic_tx_buff *tx_buff = NULL;
1422 	struct ibmvnic_sub_crq_queue *tx_scrq;
1423 	struct ibmvnic_tx_pool *tx_pool;
1424 	unsigned int tx_send_failed = 0;
1425 	unsigned int tx_map_failed = 0;
1426 	unsigned int tx_dropped = 0;
1427 	unsigned int tx_packets = 0;
1428 	unsigned int tx_bytes = 0;
1429 	dma_addr_t data_dma_addr;
1430 	struct netdev_queue *txq;
1431 	unsigned long lpar_rc;
1432 	union sub_crq tx_crq;
1433 	unsigned int offset;
1434 	int num_entries = 1;
1435 	unsigned char *dst;
1436 	u64 *handle_array;
1437 	int index = 0;
1438 	u8 proto = 0;
1439 	netdev_tx_t ret = NETDEV_TX_OK;
1440 
1441 	if (adapter->resetting) {
1442 		if (!netif_subqueue_stopped(netdev, skb))
1443 			netif_stop_subqueue(netdev, queue_num);
1444 		dev_kfree_skb_any(skb);
1445 
1446 		tx_send_failed++;
1447 		tx_dropped++;
1448 		ret = NETDEV_TX_OK;
1449 		goto out;
1450 	}
1451 
1452 	if (ibmvnic_xmit_workarounds(skb, netdev)) {
1453 		tx_dropped++;
1454 		tx_send_failed++;
1455 		ret = NETDEV_TX_OK;
1456 		goto out;
1457 	}
1458 	if (skb_is_gso(skb))
1459 		tx_pool = &adapter->tso_pool[queue_num];
1460 	else
1461 		tx_pool = &adapter->tx_pool[queue_num];
1462 
1463 	tx_scrq = adapter->tx_scrq[queue_num];
1464 	txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1465 	handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1466 		be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1467 
1468 	index = tx_pool->free_map[tx_pool->consumer_index];
1469 
1470 	if (index == IBMVNIC_INVALID_MAP) {
1471 		dev_kfree_skb_any(skb);
1472 		tx_send_failed++;
1473 		tx_dropped++;
1474 		ret = NETDEV_TX_OK;
1475 		goto out;
1476 	}
1477 
1478 	tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1479 
1480 	offset = index * tx_pool->buf_size;
1481 	dst = tx_pool->long_term_buff.buff + offset;
1482 	memset(dst, 0, tx_pool->buf_size);
1483 	data_dma_addr = tx_pool->long_term_buff.addr + offset;
1484 
1485 	if (skb_shinfo(skb)->nr_frags) {
1486 		int cur, i;
1487 
1488 		/* Copy the head */
1489 		skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1490 		cur = skb_headlen(skb);
1491 
1492 		/* Copy the frags */
1493 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1494 			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1495 
1496 			memcpy(dst + cur,
1497 			       page_address(skb_frag_page(frag)) +
1498 			       frag->page_offset, skb_frag_size(frag));
1499 			cur += skb_frag_size(frag);
1500 		}
1501 	} else {
1502 		skb_copy_from_linear_data(skb, dst, skb->len);
1503 	}
1504 
1505 	tx_pool->consumer_index =
1506 	    (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1507 
1508 	tx_buff = &tx_pool->tx_buff[index];
1509 	tx_buff->skb = skb;
1510 	tx_buff->data_dma[0] = data_dma_addr;
1511 	tx_buff->data_len[0] = skb->len;
1512 	tx_buff->index = index;
1513 	tx_buff->pool_index = queue_num;
1514 	tx_buff->last_frag = true;
1515 
1516 	memset(&tx_crq, 0, sizeof(tx_crq));
1517 	tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1518 	tx_crq.v1.type = IBMVNIC_TX_DESC;
1519 	tx_crq.v1.n_crq_elem = 1;
1520 	tx_crq.v1.n_sge = 1;
1521 	tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1522 
1523 	if (skb_is_gso(skb))
1524 		tx_crq.v1.correlator =
1525 			cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1526 	else
1527 		tx_crq.v1.correlator = cpu_to_be32(index);
1528 	tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1529 	tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1530 	tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1531 
1532 	if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
1533 		tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1534 		tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1535 	}
1536 
1537 	if (skb->protocol == htons(ETH_P_IP)) {
1538 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1539 		proto = ip_hdr(skb)->protocol;
1540 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
1541 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1542 		proto = ipv6_hdr(skb)->nexthdr;
1543 	}
1544 
1545 	if (proto == IPPROTO_TCP)
1546 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1547 	else if (proto == IPPROTO_UDP)
1548 		tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1549 
1550 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1551 		tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1552 		hdrs += 2;
1553 	}
1554 	if (skb_is_gso(skb)) {
1555 		tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1556 		tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1557 		hdrs += 2;
1558 	}
1559 	/* determine if l2/3/4 headers are sent to firmware */
1560 	if ((*hdrs >> 7) & 1) {
1561 		build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1562 		tx_crq.v1.n_crq_elem = num_entries;
1563 		tx_buff->num_entries = num_entries;
1564 		tx_buff->indir_arr[0] = tx_crq;
1565 		tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1566 						    sizeof(tx_buff->indir_arr),
1567 						    DMA_TO_DEVICE);
1568 		if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1569 			dev_kfree_skb_any(skb);
1570 			tx_buff->skb = NULL;
1571 			if (!firmware_has_feature(FW_FEATURE_CMO))
1572 				dev_err(dev, "tx: unable to map descriptor array\n");
1573 			tx_map_failed++;
1574 			tx_dropped++;
1575 			ret = NETDEV_TX_OK;
1576 			goto tx_err_out;
1577 		}
1578 		lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1579 					       (u64)tx_buff->indir_dma,
1580 					       (u64)num_entries);
1581 	} else {
1582 		tx_buff->num_entries = num_entries;
1583 		lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1584 				      &tx_crq);
1585 	}
1586 	if (lpar_rc != H_SUCCESS) {
1587 		if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
1588 			dev_err_ratelimited(dev, "tx: send failed\n");
1589 		dev_kfree_skb_any(skb);
1590 		tx_buff->skb = NULL;
1591 
1592 		if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1593 			/* Disable TX and report carrier off if queue is closed
1594 			 * or pending failover.
1595 			 * Firmware guarantees that a signal will be sent to the
1596 			 * driver, triggering a reset or some other action.
1597 			 */
1598 			netif_tx_stop_all_queues(netdev);
1599 			netif_carrier_off(netdev);
1600 		}
1601 
1602 		tx_send_failed++;
1603 		tx_dropped++;
1604 		ret = NETDEV_TX_OK;
1605 		goto tx_err_out;
1606 	}
1607 
1608 	if (atomic_add_return(num_entries, &tx_scrq->used)
1609 					>= adapter->req_tx_entries_per_subcrq) {
1610 		netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1611 		netif_stop_subqueue(netdev, queue_num);
1612 	}
1613 
1614 	tx_packets++;
1615 	tx_bytes += skb->len;
1616 	txq->trans_start = jiffies;
1617 	ret = NETDEV_TX_OK;
1618 	goto out;
1619 
1620 tx_err_out:
1621 	/* roll back consumer index and map array*/
1622 	if (tx_pool->consumer_index == 0)
1623 		tx_pool->consumer_index =
1624 			tx_pool->num_buffers - 1;
1625 	else
1626 		tx_pool->consumer_index--;
1627 	tx_pool->free_map[tx_pool->consumer_index] = index;
1628 out:
1629 	netdev->stats.tx_dropped += tx_dropped;
1630 	netdev->stats.tx_bytes += tx_bytes;
1631 	netdev->stats.tx_packets += tx_packets;
1632 	adapter->tx_send_failed += tx_send_failed;
1633 	adapter->tx_map_failed += tx_map_failed;
1634 	adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1635 	adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1636 	adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1637 
1638 	return ret;
1639 }
1640 
1641 static void ibmvnic_set_multi(struct net_device *netdev)
1642 {
1643 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1644 	struct netdev_hw_addr *ha;
1645 	union ibmvnic_crq crq;
1646 
1647 	memset(&crq, 0, sizeof(crq));
1648 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
1649 	crq.request_capability.cmd = REQUEST_CAPABILITY;
1650 
1651 	if (netdev->flags & IFF_PROMISC) {
1652 		if (!adapter->promisc_supported)
1653 			return;
1654 	} else {
1655 		if (netdev->flags & IFF_ALLMULTI) {
1656 			/* Accept all multicast */
1657 			memset(&crq, 0, sizeof(crq));
1658 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1659 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1660 			crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1661 			ibmvnic_send_crq(adapter, &crq);
1662 		} else if (netdev_mc_empty(netdev)) {
1663 			/* Reject all multicast */
1664 			memset(&crq, 0, sizeof(crq));
1665 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1666 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1667 			crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1668 			ibmvnic_send_crq(adapter, &crq);
1669 		} else {
1670 			/* Accept one or more multicast(s) */
1671 			netdev_for_each_mc_addr(ha, netdev) {
1672 				memset(&crq, 0, sizeof(crq));
1673 				crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1674 				crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1675 				crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1676 				ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1677 						ha->addr);
1678 				ibmvnic_send_crq(adapter, &crq);
1679 			}
1680 		}
1681 	}
1682 }
1683 
1684 static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
1685 {
1686 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1687 	union ibmvnic_crq crq;
1688 	int rc;
1689 
1690 	if (!is_valid_ether_addr(dev_addr)) {
1691 		rc = -EADDRNOTAVAIL;
1692 		goto err;
1693 	}
1694 
1695 	memset(&crq, 0, sizeof(crq));
1696 	crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1697 	crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1698 	ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
1699 
1700 	init_completion(&adapter->fw_done);
1701 	rc = ibmvnic_send_crq(adapter, &crq);
1702 	if (rc) {
1703 		rc = -EIO;
1704 		goto err;
1705 	}
1706 
1707 	wait_for_completion(&adapter->fw_done);
1708 	/* netdev->dev_addr is changed in handle_change_mac_rsp function */
1709 	if (adapter->fw_done_rc) {
1710 		rc = -EIO;
1711 		goto err;
1712 	}
1713 
1714 	return 0;
1715 err:
1716 	ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
1717 	return rc;
1718 }
1719 
1720 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1721 {
1722 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1723 	struct sockaddr *addr = p;
1724 	int rc;
1725 
1726 	rc = 0;
1727 	ether_addr_copy(adapter->mac_addr, addr->sa_data);
1728 	if (adapter->state != VNIC_PROBED)
1729 		rc = __ibmvnic_set_mac(netdev, addr->sa_data);
1730 
1731 	return rc;
1732 }
1733 
1734 /**
1735  * do_reset returns zero if we are able to keep processing reset events, or
1736  * non-zero if we hit a fatal error and must halt.
1737  */
1738 static int do_reset(struct ibmvnic_adapter *adapter,
1739 		    struct ibmvnic_rwi *rwi, u32 reset_state)
1740 {
1741 	u64 old_num_rx_queues, old_num_tx_queues;
1742 	u64 old_num_rx_slots, old_num_tx_slots;
1743 	struct net_device *netdev = adapter->netdev;
1744 	int i, rc;
1745 
1746 	netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1747 		   rwi->reset_reason);
1748 
1749 	netif_carrier_off(netdev);
1750 	adapter->reset_reason = rwi->reset_reason;
1751 
1752 	old_num_rx_queues = adapter->req_rx_queues;
1753 	old_num_tx_queues = adapter->req_tx_queues;
1754 	old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
1755 	old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
1756 
1757 	ibmvnic_cleanup(netdev);
1758 
1759 	if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1760 	    adapter->reset_reason != VNIC_RESET_FAILOVER) {
1761 		rc = __ibmvnic_close(netdev);
1762 		if (rc)
1763 			return rc;
1764 	}
1765 
1766 	if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1767 	    adapter->wait_for_reset) {
1768 		release_resources(adapter);
1769 		release_sub_crqs(adapter, 1);
1770 		release_crq_queue(adapter);
1771 	}
1772 
1773 	if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1774 		/* remove the closed state so when we call open it appears
1775 		 * we are coming from the probed state.
1776 		 */
1777 		adapter->state = VNIC_PROBED;
1778 
1779 		if (adapter->wait_for_reset) {
1780 			rc = init_crq_queue(adapter);
1781 		} else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1782 			rc = ibmvnic_reenable_crq_queue(adapter);
1783 			release_sub_crqs(adapter, 1);
1784 		} else {
1785 			rc = ibmvnic_reset_crq(adapter);
1786 			if (!rc)
1787 				rc = vio_enable_interrupts(adapter->vdev);
1788 		}
1789 
1790 		if (rc) {
1791 			netdev_err(adapter->netdev,
1792 				   "Couldn't initialize crq. rc=%d\n", rc);
1793 			return rc;
1794 		}
1795 
1796 		rc = ibmvnic_reset_init(adapter);
1797 		if (rc)
1798 			return IBMVNIC_INIT_FAILED;
1799 
1800 		/* If the adapter was in PROBE state prior to the reset,
1801 		 * exit here.
1802 		 */
1803 		if (reset_state == VNIC_PROBED)
1804 			return 0;
1805 
1806 		rc = ibmvnic_login(netdev);
1807 		if (rc) {
1808 			adapter->state = reset_state;
1809 			return rc;
1810 		}
1811 
1812 		if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1813 		    adapter->wait_for_reset) {
1814 			rc = init_resources(adapter);
1815 			if (rc)
1816 				return rc;
1817 		} else if (adapter->req_rx_queues != old_num_rx_queues ||
1818 			   adapter->req_tx_queues != old_num_tx_queues ||
1819 			   adapter->req_rx_add_entries_per_subcrq !=
1820 							old_num_rx_slots ||
1821 			   adapter->req_tx_entries_per_subcrq !=
1822 							old_num_tx_slots) {
1823 			release_rx_pools(adapter);
1824 			release_tx_pools(adapter);
1825 			release_napi(adapter);
1826 			release_vpd_data(adapter);
1827 
1828 			rc = init_resources(adapter);
1829 			if (rc)
1830 				return rc;
1831 
1832 		} else {
1833 			rc = reset_tx_pools(adapter);
1834 			if (rc)
1835 				return rc;
1836 
1837 			rc = reset_rx_pools(adapter);
1838 			if (rc)
1839 				return rc;
1840 		}
1841 		ibmvnic_disable_irqs(adapter);
1842 	}
1843 	adapter->state = VNIC_CLOSED;
1844 
1845 	if (reset_state == VNIC_CLOSED)
1846 		return 0;
1847 
1848 	rc = __ibmvnic_open(netdev);
1849 	if (rc) {
1850 		if (list_empty(&adapter->rwi_list))
1851 			adapter->state = VNIC_CLOSED;
1852 		else
1853 			adapter->state = reset_state;
1854 
1855 		return 0;
1856 	}
1857 
1858 	/* kick napi */
1859 	for (i = 0; i < adapter->req_rx_queues; i++)
1860 		napi_schedule(&adapter->napi[i]);
1861 
1862 	if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1863 	    adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1864 		call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, netdev);
1865 
1866 	return 0;
1867 }
1868 
1869 static int do_hard_reset(struct ibmvnic_adapter *adapter,
1870 			 struct ibmvnic_rwi *rwi, u32 reset_state)
1871 {
1872 	struct net_device *netdev = adapter->netdev;
1873 	int rc;
1874 
1875 	netdev_dbg(adapter->netdev, "Hard resetting driver (%d)\n",
1876 		   rwi->reset_reason);
1877 
1878 	netif_carrier_off(netdev);
1879 	adapter->reset_reason = rwi->reset_reason;
1880 
1881 	ibmvnic_cleanup(netdev);
1882 	release_resources(adapter);
1883 	release_sub_crqs(adapter, 0);
1884 	release_crq_queue(adapter);
1885 
1886 	/* remove the closed state so when we call open it appears
1887 	 * we are coming from the probed state.
1888 	 */
1889 	adapter->state = VNIC_PROBED;
1890 
1891 	reinit_completion(&adapter->init_done);
1892 	rc = init_crq_queue(adapter);
1893 	if (rc) {
1894 		netdev_err(adapter->netdev,
1895 			   "Couldn't initialize crq. rc=%d\n", rc);
1896 		return rc;
1897 	}
1898 
1899 	rc = ibmvnic_init(adapter);
1900 	if (rc)
1901 		return rc;
1902 
1903 	/* If the adapter was in PROBE state prior to the reset,
1904 	 * exit here.
1905 	 */
1906 	if (reset_state == VNIC_PROBED)
1907 		return 0;
1908 
1909 	rc = ibmvnic_login(netdev);
1910 	if (rc) {
1911 		adapter->state = VNIC_PROBED;
1912 		return 0;
1913 	}
1914 
1915 	rc = init_resources(adapter);
1916 	if (rc)
1917 		return rc;
1918 
1919 	ibmvnic_disable_irqs(adapter);
1920 	adapter->state = VNIC_CLOSED;
1921 
1922 	if (reset_state == VNIC_CLOSED)
1923 		return 0;
1924 
1925 	rc = __ibmvnic_open(netdev);
1926 	if (rc) {
1927 		if (list_empty(&adapter->rwi_list))
1928 			adapter->state = VNIC_CLOSED;
1929 		else
1930 			adapter->state = reset_state;
1931 
1932 		return 0;
1933 	}
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 		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
2923 			 adapter->vdev->unit_address, i);
2924 		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2925 				 0, scrq->name, scrq);
2926 
2927 		if (rc) {
2928 			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2929 				scrq->irq, rc);
2930 			irq_dispose_mapping(scrq->irq);
2931 			goto req_tx_irq_failed;
2932 		}
2933 	}
2934 
2935 	for (i = 0; i < adapter->req_rx_queues; i++) {
2936 		netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2937 			   i);
2938 		scrq = adapter->rx_scrq[i];
2939 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2940 		if (!scrq->irq) {
2941 			rc = -EINVAL;
2942 			dev_err(dev, "Error mapping irq\n");
2943 			goto req_rx_irq_failed;
2944 		}
2945 		snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
2946 			 adapter->vdev->unit_address, i);
2947 		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2948 				 0, scrq->name, scrq);
2949 		if (rc) {
2950 			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2951 				scrq->irq, rc);
2952 			irq_dispose_mapping(scrq->irq);
2953 			goto req_rx_irq_failed;
2954 		}
2955 	}
2956 	return rc;
2957 
2958 req_rx_irq_failed:
2959 	for (j = 0; j < i; j++) {
2960 		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2961 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2962 	}
2963 	i = adapter->req_tx_queues;
2964 req_tx_irq_failed:
2965 	for (j = 0; j < i; j++) {
2966 		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2967 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2968 	}
2969 	release_sub_crqs(adapter, 1);
2970 	return rc;
2971 }
2972 
2973 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2974 {
2975 	struct device *dev = &adapter->vdev->dev;
2976 	struct ibmvnic_sub_crq_queue **allqueues;
2977 	int registered_queues = 0;
2978 	int total_queues;
2979 	int more = 0;
2980 	int i;
2981 
2982 	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2983 
2984 	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2985 	if (!allqueues)
2986 		return -1;
2987 
2988 	for (i = 0; i < total_queues; i++) {
2989 		allqueues[i] = init_sub_crq_queue(adapter);
2990 		if (!allqueues[i]) {
2991 			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2992 			break;
2993 		}
2994 		registered_queues++;
2995 	}
2996 
2997 	/* Make sure we were able to register the minimum number of queues */
2998 	if (registered_queues <
2999 	    adapter->min_tx_queues + adapter->min_rx_queues) {
3000 		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
3001 		goto tx_failed;
3002 	}
3003 
3004 	/* Distribute the failed allocated queues*/
3005 	for (i = 0; i < total_queues - registered_queues + more ; i++) {
3006 		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3007 		switch (i % 3) {
3008 		case 0:
3009 			if (adapter->req_rx_queues > adapter->min_rx_queues)
3010 				adapter->req_rx_queues--;
3011 			else
3012 				more++;
3013 			break;
3014 		case 1:
3015 			if (adapter->req_tx_queues > adapter->min_tx_queues)
3016 				adapter->req_tx_queues--;
3017 			else
3018 				more++;
3019 			break;
3020 		}
3021 	}
3022 
3023 	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3024 				   sizeof(*adapter->tx_scrq), GFP_KERNEL);
3025 	if (!adapter->tx_scrq)
3026 		goto tx_failed;
3027 
3028 	for (i = 0; i < adapter->req_tx_queues; i++) {
3029 		adapter->tx_scrq[i] = allqueues[i];
3030 		adapter->tx_scrq[i]->pool_index = i;
3031 		adapter->num_active_tx_scrqs++;
3032 	}
3033 
3034 	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3035 				   sizeof(*adapter->rx_scrq), GFP_KERNEL);
3036 	if (!adapter->rx_scrq)
3037 		goto rx_failed;
3038 
3039 	for (i = 0; i < adapter->req_rx_queues; i++) {
3040 		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3041 		adapter->rx_scrq[i]->scrq_num = i;
3042 		adapter->num_active_rx_scrqs++;
3043 	}
3044 
3045 	kfree(allqueues);
3046 	return 0;
3047 
3048 rx_failed:
3049 	kfree(adapter->tx_scrq);
3050 	adapter->tx_scrq = NULL;
3051 tx_failed:
3052 	for (i = 0; i < registered_queues; i++)
3053 		release_sub_crq_queue(adapter, allqueues[i], 1);
3054 	kfree(allqueues);
3055 	return -1;
3056 }
3057 
3058 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3059 {
3060 	struct device *dev = &adapter->vdev->dev;
3061 	union ibmvnic_crq crq;
3062 	int max_entries;
3063 
3064 	if (!retry) {
3065 		/* Sub-CRQ entries are 32 byte long */
3066 		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3067 
3068 		if (adapter->min_tx_entries_per_subcrq > entries_page ||
3069 		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
3070 			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3071 			return;
3072 		}
3073 
3074 		if (adapter->desired.mtu)
3075 			adapter->req_mtu = adapter->desired.mtu;
3076 		else
3077 			adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
3078 
3079 		if (!adapter->desired.tx_entries)
3080 			adapter->desired.tx_entries =
3081 					adapter->max_tx_entries_per_subcrq;
3082 		if (!adapter->desired.rx_entries)
3083 			adapter->desired.rx_entries =
3084 					adapter->max_rx_add_entries_per_subcrq;
3085 
3086 		max_entries = IBMVNIC_MAX_LTB_SIZE /
3087 			      (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3088 
3089 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3090 			adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3091 			adapter->desired.tx_entries = max_entries;
3092 		}
3093 
3094 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3095 			adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3096 			adapter->desired.rx_entries = max_entries;
3097 		}
3098 
3099 		if (adapter->desired.tx_entries)
3100 			adapter->req_tx_entries_per_subcrq =
3101 					adapter->desired.tx_entries;
3102 		else
3103 			adapter->req_tx_entries_per_subcrq =
3104 					adapter->max_tx_entries_per_subcrq;
3105 
3106 		if (adapter->desired.rx_entries)
3107 			adapter->req_rx_add_entries_per_subcrq =
3108 					adapter->desired.rx_entries;
3109 		else
3110 			adapter->req_rx_add_entries_per_subcrq =
3111 					adapter->max_rx_add_entries_per_subcrq;
3112 
3113 		if (adapter->desired.tx_queues)
3114 			adapter->req_tx_queues =
3115 					adapter->desired.tx_queues;
3116 		else
3117 			adapter->req_tx_queues =
3118 					adapter->opt_tx_comp_sub_queues;
3119 
3120 		if (adapter->desired.rx_queues)
3121 			adapter->req_rx_queues =
3122 					adapter->desired.rx_queues;
3123 		else
3124 			adapter->req_rx_queues =
3125 					adapter->opt_rx_comp_queues;
3126 
3127 		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3128 	}
3129 
3130 	memset(&crq, 0, sizeof(crq));
3131 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
3132 	crq.request_capability.cmd = REQUEST_CAPABILITY;
3133 
3134 	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3135 	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3136 	atomic_inc(&adapter->running_cap_crqs);
3137 	ibmvnic_send_crq(adapter, &crq);
3138 
3139 	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3140 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3141 	atomic_inc(&adapter->running_cap_crqs);
3142 	ibmvnic_send_crq(adapter, &crq);
3143 
3144 	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3145 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3146 	atomic_inc(&adapter->running_cap_crqs);
3147 	ibmvnic_send_crq(adapter, &crq);
3148 
3149 	crq.request_capability.capability =
3150 	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3151 	crq.request_capability.number =
3152 	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3153 	atomic_inc(&adapter->running_cap_crqs);
3154 	ibmvnic_send_crq(adapter, &crq);
3155 
3156 	crq.request_capability.capability =
3157 	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3158 	crq.request_capability.number =
3159 	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3160 	atomic_inc(&adapter->running_cap_crqs);
3161 	ibmvnic_send_crq(adapter, &crq);
3162 
3163 	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3164 	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3165 	atomic_inc(&adapter->running_cap_crqs);
3166 	ibmvnic_send_crq(adapter, &crq);
3167 
3168 	if (adapter->netdev->flags & IFF_PROMISC) {
3169 		if (adapter->promisc_supported) {
3170 			crq.request_capability.capability =
3171 			    cpu_to_be16(PROMISC_REQUESTED);
3172 			crq.request_capability.number = cpu_to_be64(1);
3173 			atomic_inc(&adapter->running_cap_crqs);
3174 			ibmvnic_send_crq(adapter, &crq);
3175 		}
3176 	} else {
3177 		crq.request_capability.capability =
3178 		    cpu_to_be16(PROMISC_REQUESTED);
3179 		crq.request_capability.number = cpu_to_be64(0);
3180 		atomic_inc(&adapter->running_cap_crqs);
3181 		ibmvnic_send_crq(adapter, &crq);
3182 	}
3183 }
3184 
3185 static int pending_scrq(struct ibmvnic_adapter *adapter,
3186 			struct ibmvnic_sub_crq_queue *scrq)
3187 {
3188 	union sub_crq *entry = &scrq->msgs[scrq->cur];
3189 
3190 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3191 		return 1;
3192 	else
3193 		return 0;
3194 }
3195 
3196 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3197 					struct ibmvnic_sub_crq_queue *scrq)
3198 {
3199 	union sub_crq *entry;
3200 	unsigned long flags;
3201 
3202 	spin_lock_irqsave(&scrq->lock, flags);
3203 	entry = &scrq->msgs[scrq->cur];
3204 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3205 		if (++scrq->cur == scrq->size)
3206 			scrq->cur = 0;
3207 	} else {
3208 		entry = NULL;
3209 	}
3210 	spin_unlock_irqrestore(&scrq->lock, flags);
3211 
3212 	return entry;
3213 }
3214 
3215 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3216 {
3217 	struct ibmvnic_crq_queue *queue = &adapter->crq;
3218 	union ibmvnic_crq *crq;
3219 
3220 	crq = &queue->msgs[queue->cur];
3221 	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3222 		if (++queue->cur == queue->size)
3223 			queue->cur = 0;
3224 	} else {
3225 		crq = NULL;
3226 	}
3227 
3228 	return crq;
3229 }
3230 
3231 static void print_subcrq_error(struct device *dev, int rc, const char *func)
3232 {
3233 	switch (rc) {
3234 	case H_PARAMETER:
3235 		dev_warn_ratelimited(dev,
3236 				     "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3237 				     func, rc);
3238 		break;
3239 	case H_CLOSED:
3240 		dev_warn_ratelimited(dev,
3241 				     "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3242 				     func, rc);
3243 		break;
3244 	default:
3245 		dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3246 		break;
3247 	}
3248 }
3249 
3250 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3251 		       union sub_crq *sub_crq)
3252 {
3253 	unsigned int ua = adapter->vdev->unit_address;
3254 	struct device *dev = &adapter->vdev->dev;
3255 	u64 *u64_crq = (u64 *)sub_crq;
3256 	int rc;
3257 
3258 	netdev_dbg(adapter->netdev,
3259 		   "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3260 		   (unsigned long int)cpu_to_be64(remote_handle),
3261 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
3262 		   (unsigned long int)cpu_to_be64(u64_crq[1]),
3263 		   (unsigned long int)cpu_to_be64(u64_crq[2]),
3264 		   (unsigned long int)cpu_to_be64(u64_crq[3]));
3265 
3266 	/* Make sure the hypervisor sees the complete request */
3267 	mb();
3268 
3269 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3270 				cpu_to_be64(remote_handle),
3271 				cpu_to_be64(u64_crq[0]),
3272 				cpu_to_be64(u64_crq[1]),
3273 				cpu_to_be64(u64_crq[2]),
3274 				cpu_to_be64(u64_crq[3]));
3275 
3276 	if (rc)
3277 		print_subcrq_error(dev, rc, __func__);
3278 
3279 	return rc;
3280 }
3281 
3282 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3283 				u64 remote_handle, u64 ioba, u64 num_entries)
3284 {
3285 	unsigned int ua = adapter->vdev->unit_address;
3286 	struct device *dev = &adapter->vdev->dev;
3287 	int rc;
3288 
3289 	/* Make sure the hypervisor sees the complete request */
3290 	mb();
3291 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3292 				cpu_to_be64(remote_handle),
3293 				ioba, num_entries);
3294 
3295 	if (rc)
3296 		print_subcrq_error(dev, rc, __func__);
3297 
3298 	return rc;
3299 }
3300 
3301 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3302 			    union ibmvnic_crq *crq)
3303 {
3304 	unsigned int ua = adapter->vdev->unit_address;
3305 	struct device *dev = &adapter->vdev->dev;
3306 	u64 *u64_crq = (u64 *)crq;
3307 	int rc;
3308 
3309 	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3310 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
3311 		   (unsigned long int)cpu_to_be64(u64_crq[1]));
3312 
3313 	if (!adapter->crq.active &&
3314 	    crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3315 		dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3316 		return -EINVAL;
3317 	}
3318 
3319 	/* Make sure the hypervisor sees the complete request */
3320 	mb();
3321 
3322 	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3323 				cpu_to_be64(u64_crq[0]),
3324 				cpu_to_be64(u64_crq[1]));
3325 
3326 	if (rc) {
3327 		if (rc == H_CLOSED) {
3328 			dev_warn(dev, "CRQ Queue closed\n");
3329 			if (adapter->resetting)
3330 				ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3331 		}
3332 
3333 		dev_warn(dev, "Send error (rc=%d)\n", rc);
3334 	}
3335 
3336 	return rc;
3337 }
3338 
3339 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3340 {
3341 	union ibmvnic_crq crq;
3342 
3343 	memset(&crq, 0, sizeof(crq));
3344 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3345 	crq.generic.cmd = IBMVNIC_CRQ_INIT;
3346 	netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3347 
3348 	return ibmvnic_send_crq(adapter, &crq);
3349 }
3350 
3351 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3352 {
3353 	union ibmvnic_crq crq;
3354 
3355 	memset(&crq, 0, sizeof(crq));
3356 	crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3357 	crq.version_exchange.cmd = VERSION_EXCHANGE;
3358 	crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3359 
3360 	return ibmvnic_send_crq(adapter, &crq);
3361 }
3362 
3363 struct vnic_login_client_data {
3364 	u8	type;
3365 	__be16	len;
3366 	char	name[];
3367 } __packed;
3368 
3369 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3370 {
3371 	int len;
3372 
3373 	/* Calculate the amount of buffer space needed for the
3374 	 * vnic client data in the login buffer. There are four entries,
3375 	 * OS name, LPAR name, device name, and a null last entry.
3376 	 */
3377 	len = 4 * sizeof(struct vnic_login_client_data);
3378 	len += 6; /* "Linux" plus NULL */
3379 	len += strlen(utsname()->nodename) + 1;
3380 	len += strlen(adapter->netdev->name) + 1;
3381 
3382 	return len;
3383 }
3384 
3385 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3386 				 struct vnic_login_client_data *vlcd)
3387 {
3388 	const char *os_name = "Linux";
3389 	int len;
3390 
3391 	/* Type 1 - LPAR OS */
3392 	vlcd->type = 1;
3393 	len = strlen(os_name) + 1;
3394 	vlcd->len = cpu_to_be16(len);
3395 	strncpy(vlcd->name, os_name, len);
3396 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3397 
3398 	/* Type 2 - LPAR name */
3399 	vlcd->type = 2;
3400 	len = strlen(utsname()->nodename) + 1;
3401 	vlcd->len = cpu_to_be16(len);
3402 	strncpy(vlcd->name, utsname()->nodename, len);
3403 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3404 
3405 	/* Type 3 - device name */
3406 	vlcd->type = 3;
3407 	len = strlen(adapter->netdev->name) + 1;
3408 	vlcd->len = cpu_to_be16(len);
3409 	strncpy(vlcd->name, adapter->netdev->name, len);
3410 }
3411 
3412 static int send_login(struct ibmvnic_adapter *adapter)
3413 {
3414 	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3415 	struct ibmvnic_login_buffer *login_buffer;
3416 	struct device *dev = &adapter->vdev->dev;
3417 	dma_addr_t rsp_buffer_token;
3418 	dma_addr_t buffer_token;
3419 	size_t rsp_buffer_size;
3420 	union ibmvnic_crq crq;
3421 	size_t buffer_size;
3422 	__be64 *tx_list_p;
3423 	__be64 *rx_list_p;
3424 	int client_data_len;
3425 	struct vnic_login_client_data *vlcd;
3426 	int i;
3427 
3428 	if (!adapter->tx_scrq || !adapter->rx_scrq) {
3429 		netdev_err(adapter->netdev,
3430 			   "RX or TX queues are not allocated, device login failed\n");
3431 		return -1;
3432 	}
3433 
3434 	release_login_rsp_buffer(adapter);
3435 	client_data_len = vnic_client_data_len(adapter);
3436 
3437 	buffer_size =
3438 	    sizeof(struct ibmvnic_login_buffer) +
3439 	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3440 	    client_data_len;
3441 
3442 	login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3443 	if (!login_buffer)
3444 		goto buf_alloc_failed;
3445 
3446 	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3447 				      DMA_TO_DEVICE);
3448 	if (dma_mapping_error(dev, buffer_token)) {
3449 		dev_err(dev, "Couldn't map login buffer\n");
3450 		goto buf_map_failed;
3451 	}
3452 
3453 	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3454 			  sizeof(u64) * adapter->req_tx_queues +
3455 			  sizeof(u64) * adapter->req_rx_queues +
3456 			  sizeof(u64) * adapter->req_rx_queues +
3457 			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3458 
3459 	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3460 	if (!login_rsp_buffer)
3461 		goto buf_rsp_alloc_failed;
3462 
3463 	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3464 					  rsp_buffer_size, DMA_FROM_DEVICE);
3465 	if (dma_mapping_error(dev, rsp_buffer_token)) {
3466 		dev_err(dev, "Couldn't map login rsp buffer\n");
3467 		goto buf_rsp_map_failed;
3468 	}
3469 
3470 	adapter->login_buf = login_buffer;
3471 	adapter->login_buf_token = buffer_token;
3472 	adapter->login_buf_sz = buffer_size;
3473 	adapter->login_rsp_buf = login_rsp_buffer;
3474 	adapter->login_rsp_buf_token = rsp_buffer_token;
3475 	adapter->login_rsp_buf_sz = rsp_buffer_size;
3476 
3477 	login_buffer->len = cpu_to_be32(buffer_size);
3478 	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3479 	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3480 	login_buffer->off_txcomp_subcrqs =
3481 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3482 	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3483 	login_buffer->off_rxcomp_subcrqs =
3484 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3485 			sizeof(u64) * adapter->req_tx_queues);
3486 	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3487 	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3488 
3489 	tx_list_p = (__be64 *)((char *)login_buffer +
3490 				      sizeof(struct ibmvnic_login_buffer));
3491 	rx_list_p = (__be64 *)((char *)login_buffer +
3492 				      sizeof(struct ibmvnic_login_buffer) +
3493 				      sizeof(u64) * adapter->req_tx_queues);
3494 
3495 	for (i = 0; i < adapter->req_tx_queues; i++) {
3496 		if (adapter->tx_scrq[i]) {
3497 			tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3498 						   crq_num);
3499 		}
3500 	}
3501 
3502 	for (i = 0; i < adapter->req_rx_queues; i++) {
3503 		if (adapter->rx_scrq[i]) {
3504 			rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3505 						   crq_num);
3506 		}
3507 	}
3508 
3509 	/* Insert vNIC login client data */
3510 	vlcd = (struct vnic_login_client_data *)
3511 		((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3512 	login_buffer->client_data_offset =
3513 			cpu_to_be32((char *)vlcd - (char *)login_buffer);
3514 	login_buffer->client_data_len = cpu_to_be32(client_data_len);
3515 
3516 	vnic_add_client_data(adapter, vlcd);
3517 
3518 	netdev_dbg(adapter->netdev, "Login Buffer:\n");
3519 	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3520 		netdev_dbg(adapter->netdev, "%016lx\n",
3521 			   ((unsigned long int *)(adapter->login_buf))[i]);
3522 	}
3523 
3524 	memset(&crq, 0, sizeof(crq));
3525 	crq.login.first = IBMVNIC_CRQ_CMD;
3526 	crq.login.cmd = LOGIN;
3527 	crq.login.ioba = cpu_to_be32(buffer_token);
3528 	crq.login.len = cpu_to_be32(buffer_size);
3529 	ibmvnic_send_crq(adapter, &crq);
3530 
3531 	return 0;
3532 
3533 buf_rsp_map_failed:
3534 	kfree(login_rsp_buffer);
3535 buf_rsp_alloc_failed:
3536 	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3537 buf_map_failed:
3538 	kfree(login_buffer);
3539 buf_alloc_failed:
3540 	return -1;
3541 }
3542 
3543 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3544 			    u32 len, u8 map_id)
3545 {
3546 	union ibmvnic_crq crq;
3547 
3548 	memset(&crq, 0, sizeof(crq));
3549 	crq.request_map.first = IBMVNIC_CRQ_CMD;
3550 	crq.request_map.cmd = REQUEST_MAP;
3551 	crq.request_map.map_id = map_id;
3552 	crq.request_map.ioba = cpu_to_be32(addr);
3553 	crq.request_map.len = cpu_to_be32(len);
3554 	return ibmvnic_send_crq(adapter, &crq);
3555 }
3556 
3557 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3558 {
3559 	union ibmvnic_crq crq;
3560 
3561 	memset(&crq, 0, sizeof(crq));
3562 	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3563 	crq.request_unmap.cmd = REQUEST_UNMAP;
3564 	crq.request_unmap.map_id = map_id;
3565 	return ibmvnic_send_crq(adapter, &crq);
3566 }
3567 
3568 static void send_map_query(struct ibmvnic_adapter *adapter)
3569 {
3570 	union ibmvnic_crq crq;
3571 
3572 	memset(&crq, 0, sizeof(crq));
3573 	crq.query_map.first = IBMVNIC_CRQ_CMD;
3574 	crq.query_map.cmd = QUERY_MAP;
3575 	ibmvnic_send_crq(adapter, &crq);
3576 }
3577 
3578 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3579 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3580 {
3581 	union ibmvnic_crq crq;
3582 
3583 	atomic_set(&adapter->running_cap_crqs, 0);
3584 	memset(&crq, 0, sizeof(crq));
3585 	crq.query_capability.first = IBMVNIC_CRQ_CMD;
3586 	crq.query_capability.cmd = QUERY_CAPABILITY;
3587 
3588 	crq.query_capability.capability = cpu_to_be16(MIN_TX_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_QUEUES);
3593 	atomic_inc(&adapter->running_cap_crqs);
3594 	ibmvnic_send_crq(adapter, &crq);
3595 
3596 	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3597 	atomic_inc(&adapter->running_cap_crqs);
3598 	ibmvnic_send_crq(adapter, &crq);
3599 
3600 	crq.query_capability.capability = cpu_to_be16(MAX_TX_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_QUEUES);
3605 	atomic_inc(&adapter->running_cap_crqs);
3606 	ibmvnic_send_crq(adapter, &crq);
3607 
3608 	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3609 	atomic_inc(&adapter->running_cap_crqs);
3610 	ibmvnic_send_crq(adapter, &crq);
3611 
3612 	crq.query_capability.capability =
3613 	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3614 	atomic_inc(&adapter->running_cap_crqs);
3615 	ibmvnic_send_crq(adapter, &crq);
3616 
3617 	crq.query_capability.capability =
3618 	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3619 	atomic_inc(&adapter->running_cap_crqs);
3620 	ibmvnic_send_crq(adapter, &crq);
3621 
3622 	crq.query_capability.capability =
3623 	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3624 	atomic_inc(&adapter->running_cap_crqs);
3625 	ibmvnic_send_crq(adapter, &crq);
3626 
3627 	crq.query_capability.capability =
3628 	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3629 	atomic_inc(&adapter->running_cap_crqs);
3630 	ibmvnic_send_crq(adapter, &crq);
3631 
3632 	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3633 	atomic_inc(&adapter->running_cap_crqs);
3634 	ibmvnic_send_crq(adapter, &crq);
3635 
3636 	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3637 	atomic_inc(&adapter->running_cap_crqs);
3638 	ibmvnic_send_crq(adapter, &crq);
3639 
3640 	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3641 	atomic_inc(&adapter->running_cap_crqs);
3642 	ibmvnic_send_crq(adapter, &crq);
3643 
3644 	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3645 	atomic_inc(&adapter->running_cap_crqs);
3646 	ibmvnic_send_crq(adapter, &crq);
3647 
3648 	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3649 	atomic_inc(&adapter->running_cap_crqs);
3650 	ibmvnic_send_crq(adapter, &crq);
3651 
3652 	crq.query_capability.capability = cpu_to_be16(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(RX_VLAN_HEADER_INSERTION);
3657 	atomic_inc(&adapter->running_cap_crqs);
3658 	ibmvnic_send_crq(adapter, &crq);
3659 
3660 	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3661 	atomic_inc(&adapter->running_cap_crqs);
3662 	ibmvnic_send_crq(adapter, &crq);
3663 
3664 	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3665 	atomic_inc(&adapter->running_cap_crqs);
3666 	ibmvnic_send_crq(adapter, &crq);
3667 
3668 	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3669 	atomic_inc(&adapter->running_cap_crqs);
3670 	ibmvnic_send_crq(adapter, &crq);
3671 
3672 	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3673 	atomic_inc(&adapter->running_cap_crqs);
3674 	ibmvnic_send_crq(adapter, &crq);
3675 
3676 	crq.query_capability.capability =
3677 			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3678 	atomic_inc(&adapter->running_cap_crqs);
3679 	ibmvnic_send_crq(adapter, &crq);
3680 
3681 	crq.query_capability.capability =
3682 			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3683 	atomic_inc(&adapter->running_cap_crqs);
3684 	ibmvnic_send_crq(adapter, &crq);
3685 
3686 	crq.query_capability.capability =
3687 			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3688 	atomic_inc(&adapter->running_cap_crqs);
3689 	ibmvnic_send_crq(adapter, &crq);
3690 
3691 	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3692 	atomic_inc(&adapter->running_cap_crqs);
3693 	ibmvnic_send_crq(adapter, &crq);
3694 }
3695 
3696 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3697 				struct ibmvnic_adapter *adapter)
3698 {
3699 	struct device *dev = &adapter->vdev->dev;
3700 
3701 	if (crq->get_vpd_size_rsp.rc.code) {
3702 		dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3703 			crq->get_vpd_size_rsp.rc.code);
3704 		complete(&adapter->fw_done);
3705 		return;
3706 	}
3707 
3708 	adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3709 	complete(&adapter->fw_done);
3710 }
3711 
3712 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3713 			   struct ibmvnic_adapter *adapter)
3714 {
3715 	struct device *dev = &adapter->vdev->dev;
3716 	unsigned char *substr = NULL;
3717 	u8 fw_level_len = 0;
3718 
3719 	memset(adapter->fw_version, 0, 32);
3720 
3721 	dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3722 			 DMA_FROM_DEVICE);
3723 
3724 	if (crq->get_vpd_rsp.rc.code) {
3725 		dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3726 			crq->get_vpd_rsp.rc.code);
3727 		goto complete;
3728 	}
3729 
3730 	/* get the position of the firmware version info
3731 	 * located after the ASCII 'RM' substring in the buffer
3732 	 */
3733 	substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3734 	if (!substr) {
3735 		dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3736 		goto complete;
3737 	}
3738 
3739 	/* get length of firmware level ASCII substring */
3740 	if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3741 		fw_level_len = *(substr + 2);
3742 	} else {
3743 		dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3744 		goto complete;
3745 	}
3746 
3747 	/* copy firmware version string from vpd into adapter */
3748 	if ((substr + 3 + fw_level_len) <
3749 	    (adapter->vpd->buff + adapter->vpd->len)) {
3750 		strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3751 	} else {
3752 		dev_info(dev, "FW substr extrapolated VPD buff\n");
3753 	}
3754 
3755 complete:
3756 	if (adapter->fw_version[0] == '\0')
3757 		strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3758 	complete(&adapter->fw_done);
3759 }
3760 
3761 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3762 {
3763 	struct device *dev = &adapter->vdev->dev;
3764 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3765 	netdev_features_t old_hw_features = 0;
3766 	union ibmvnic_crq crq;
3767 	int i;
3768 
3769 	dma_unmap_single(dev, adapter->ip_offload_tok,
3770 			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3771 
3772 	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3773 	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3774 		netdev_dbg(adapter->netdev, "%016lx\n",
3775 			   ((unsigned long int *)(buf))[i]);
3776 
3777 	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3778 	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3779 	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3780 		   buf->tcp_ipv4_chksum);
3781 	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3782 		   buf->tcp_ipv6_chksum);
3783 	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3784 		   buf->udp_ipv4_chksum);
3785 	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3786 		   buf->udp_ipv6_chksum);
3787 	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3788 		   buf->large_tx_ipv4);
3789 	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3790 		   buf->large_tx_ipv6);
3791 	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3792 		   buf->large_rx_ipv4);
3793 	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3794 		   buf->large_rx_ipv6);
3795 	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3796 		   buf->max_ipv4_header_size);
3797 	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3798 		   buf->max_ipv6_header_size);
3799 	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3800 		   buf->max_tcp_header_size);
3801 	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3802 		   buf->max_udp_header_size);
3803 	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3804 		   buf->max_large_tx_size);
3805 	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3806 		   buf->max_large_rx_size);
3807 	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3808 		   buf->ipv6_extension_header);
3809 	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3810 		   buf->tcp_pseudosum_req);
3811 	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3812 		   buf->num_ipv6_ext_headers);
3813 	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3814 		   buf->off_ipv6_ext_headers);
3815 
3816 	adapter->ip_offload_ctrl_tok =
3817 	    dma_map_single(dev, &adapter->ip_offload_ctrl,
3818 			   sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3819 
3820 	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3821 		dev_err(dev, "Couldn't map ip offload control buffer\n");
3822 		return;
3823 	}
3824 
3825 	adapter->ip_offload_ctrl.len =
3826 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3827 	adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3828 	adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3829 	adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3830 	adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3831 	adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3832 	adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3833 	adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3834 	adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3835 	adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3836 
3837 	/* large_rx disabled for now, additional features needed */
3838 	adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3839 	adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3840 
3841 	if (adapter->state != VNIC_PROBING) {
3842 		old_hw_features = adapter->netdev->hw_features;
3843 		adapter->netdev->hw_features = 0;
3844 	}
3845 
3846 	adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
3847 
3848 	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3849 		adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
3850 
3851 	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3852 		adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
3853 
3854 	if ((adapter->netdev->features &
3855 	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3856 		adapter->netdev->hw_features |= NETIF_F_RXCSUM;
3857 
3858 	if (buf->large_tx_ipv4)
3859 		adapter->netdev->hw_features |= NETIF_F_TSO;
3860 	if (buf->large_tx_ipv6)
3861 		adapter->netdev->hw_features |= NETIF_F_TSO6;
3862 
3863 	if (adapter->state == VNIC_PROBING) {
3864 		adapter->netdev->features |= adapter->netdev->hw_features;
3865 	} else if (old_hw_features != adapter->netdev->hw_features) {
3866 		netdev_features_t tmp = 0;
3867 
3868 		/* disable features no longer supported */
3869 		adapter->netdev->features &= adapter->netdev->hw_features;
3870 		/* turn on features now supported if previously enabled */
3871 		tmp = (old_hw_features ^ adapter->netdev->hw_features) &
3872 			adapter->netdev->hw_features;
3873 		adapter->netdev->features |=
3874 				tmp & adapter->netdev->wanted_features;
3875 	}
3876 
3877 	memset(&crq, 0, sizeof(crq));
3878 	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3879 	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3880 	crq.control_ip_offload.len =
3881 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3882 	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3883 	ibmvnic_send_crq(adapter, &crq);
3884 }
3885 
3886 static const char *ibmvnic_fw_err_cause(u16 cause)
3887 {
3888 	switch (cause) {
3889 	case ADAPTER_PROBLEM:
3890 		return "adapter problem";
3891 	case BUS_PROBLEM:
3892 		return "bus problem";
3893 	case FW_PROBLEM:
3894 		return "firmware problem";
3895 	case DD_PROBLEM:
3896 		return "device driver problem";
3897 	case EEH_RECOVERY:
3898 		return "EEH recovery";
3899 	case FW_UPDATED:
3900 		return "firmware updated";
3901 	case LOW_MEMORY:
3902 		return "low Memory";
3903 	default:
3904 		return "unknown";
3905 	}
3906 }
3907 
3908 static void handle_error_indication(union ibmvnic_crq *crq,
3909 				    struct ibmvnic_adapter *adapter)
3910 {
3911 	struct device *dev = &adapter->vdev->dev;
3912 	u16 cause;
3913 
3914 	cause = be16_to_cpu(crq->error_indication.error_cause);
3915 
3916 	dev_warn_ratelimited(dev,
3917 			     "Firmware reports %serror, cause: %s. Starting recovery...\n",
3918 			     crq->error_indication.flags
3919 				& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3920 			     ibmvnic_fw_err_cause(cause));
3921 
3922 	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3923 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3924 	else
3925 		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3926 }
3927 
3928 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3929 				 struct ibmvnic_adapter *adapter)
3930 {
3931 	struct net_device *netdev = adapter->netdev;
3932 	struct device *dev = &adapter->vdev->dev;
3933 	long rc;
3934 
3935 	rc = crq->change_mac_addr_rsp.rc.code;
3936 	if (rc) {
3937 		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3938 		goto out;
3939 	}
3940 	ether_addr_copy(netdev->dev_addr,
3941 			&crq->change_mac_addr_rsp.mac_addr[0]);
3942 out:
3943 	complete(&adapter->fw_done);
3944 	return rc;
3945 }
3946 
3947 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3948 				   struct ibmvnic_adapter *adapter)
3949 {
3950 	struct device *dev = &adapter->vdev->dev;
3951 	u64 *req_value;
3952 	char *name;
3953 
3954 	atomic_dec(&adapter->running_cap_crqs);
3955 	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3956 	case REQ_TX_QUEUES:
3957 		req_value = &adapter->req_tx_queues;
3958 		name = "tx";
3959 		break;
3960 	case REQ_RX_QUEUES:
3961 		req_value = &adapter->req_rx_queues;
3962 		name = "rx";
3963 		break;
3964 	case REQ_RX_ADD_QUEUES:
3965 		req_value = &adapter->req_rx_add_queues;
3966 		name = "rx_add";
3967 		break;
3968 	case REQ_TX_ENTRIES_PER_SUBCRQ:
3969 		req_value = &adapter->req_tx_entries_per_subcrq;
3970 		name = "tx_entries_per_subcrq";
3971 		break;
3972 	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3973 		req_value = &adapter->req_rx_add_entries_per_subcrq;
3974 		name = "rx_add_entries_per_subcrq";
3975 		break;
3976 	case REQ_MTU:
3977 		req_value = &adapter->req_mtu;
3978 		name = "mtu";
3979 		break;
3980 	case PROMISC_REQUESTED:
3981 		req_value = &adapter->promisc;
3982 		name = "promisc";
3983 		break;
3984 	default:
3985 		dev_err(dev, "Got invalid cap request rsp %d\n",
3986 			crq->request_capability.capability);
3987 		return;
3988 	}
3989 
3990 	switch (crq->request_capability_rsp.rc.code) {
3991 	case SUCCESS:
3992 		break;
3993 	case PARTIALSUCCESS:
3994 		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3995 			 *req_value,
3996 			 (long int)be64_to_cpu(crq->request_capability_rsp.
3997 					       number), name);
3998 
3999 		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
4000 		    REQ_MTU) {
4001 			pr_err("mtu of %llu is not supported. Reverting.\n",
4002 			       *req_value);
4003 			*req_value = adapter->fallback.mtu;
4004 		} else {
4005 			*req_value =
4006 				be64_to_cpu(crq->request_capability_rsp.number);
4007 		}
4008 
4009 		ibmvnic_send_req_caps(adapter, 1);
4010 		return;
4011 	default:
4012 		dev_err(dev, "Error %d in request cap rsp\n",
4013 			crq->request_capability_rsp.rc.code);
4014 		return;
4015 	}
4016 
4017 	/* Done receiving requested capabilities, query IP offload support */
4018 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4019 		union ibmvnic_crq newcrq;
4020 		int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4021 		struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4022 		    &adapter->ip_offload_buf;
4023 
4024 		adapter->wait_capability = false;
4025 		adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4026 							 buf_sz,
4027 							 DMA_FROM_DEVICE);
4028 
4029 		if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4030 			if (!firmware_has_feature(FW_FEATURE_CMO))
4031 				dev_err(dev, "Couldn't map offload buffer\n");
4032 			return;
4033 		}
4034 
4035 		memset(&newcrq, 0, sizeof(newcrq));
4036 		newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4037 		newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4038 		newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4039 		newcrq.query_ip_offload.ioba =
4040 		    cpu_to_be32(adapter->ip_offload_tok);
4041 
4042 		ibmvnic_send_crq(adapter, &newcrq);
4043 	}
4044 }
4045 
4046 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4047 			    struct ibmvnic_adapter *adapter)
4048 {
4049 	struct device *dev = &adapter->vdev->dev;
4050 	struct net_device *netdev = adapter->netdev;
4051 	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4052 	struct ibmvnic_login_buffer *login = adapter->login_buf;
4053 	int i;
4054 
4055 	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
4056 			 DMA_TO_DEVICE);
4057 	dma_unmap_single(dev, adapter->login_rsp_buf_token,
4058 			 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
4059 
4060 	/* If the number of queues requested can't be allocated by the
4061 	 * server, the login response will return with code 1. We will need
4062 	 * to resend the login buffer with fewer queues requested.
4063 	 */
4064 	if (login_rsp_crq->generic.rc.code) {
4065 		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4066 		complete(&adapter->init_done);
4067 		return 0;
4068 	}
4069 
4070 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4071 
4072 	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4073 	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4074 		netdev_dbg(adapter->netdev, "%016lx\n",
4075 			   ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4076 	}
4077 
4078 	/* Sanity checks */
4079 	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4080 	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
4081 	     adapter->req_rx_add_queues !=
4082 	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4083 		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4084 		ibmvnic_remove(adapter->vdev);
4085 		return -EIO;
4086 	}
4087 	release_login_buffer(adapter);
4088 	complete(&adapter->init_done);
4089 
4090 	return 0;
4091 }
4092 
4093 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4094 				     struct ibmvnic_adapter *adapter)
4095 {
4096 	struct device *dev = &adapter->vdev->dev;
4097 	long rc;
4098 
4099 	rc = crq->request_unmap_rsp.rc.code;
4100 	if (rc)
4101 		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4102 }
4103 
4104 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4105 				 struct ibmvnic_adapter *adapter)
4106 {
4107 	struct net_device *netdev = adapter->netdev;
4108 	struct device *dev = &adapter->vdev->dev;
4109 	long rc;
4110 
4111 	rc = crq->query_map_rsp.rc.code;
4112 	if (rc) {
4113 		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4114 		return;
4115 	}
4116 	netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4117 		   crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4118 		   crq->query_map_rsp.free_pages);
4119 }
4120 
4121 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4122 				 struct ibmvnic_adapter *adapter)
4123 {
4124 	struct net_device *netdev = adapter->netdev;
4125 	struct device *dev = &adapter->vdev->dev;
4126 	long rc;
4127 
4128 	atomic_dec(&adapter->running_cap_crqs);
4129 	netdev_dbg(netdev, "Outstanding queries: %d\n",
4130 		   atomic_read(&adapter->running_cap_crqs));
4131 	rc = crq->query_capability.rc.code;
4132 	if (rc) {
4133 		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4134 		goto out;
4135 	}
4136 
4137 	switch (be16_to_cpu(crq->query_capability.capability)) {
4138 	case MIN_TX_QUEUES:
4139 		adapter->min_tx_queues =
4140 		    be64_to_cpu(crq->query_capability.number);
4141 		netdev_dbg(netdev, "min_tx_queues = %lld\n",
4142 			   adapter->min_tx_queues);
4143 		break;
4144 	case MIN_RX_QUEUES:
4145 		adapter->min_rx_queues =
4146 		    be64_to_cpu(crq->query_capability.number);
4147 		netdev_dbg(netdev, "min_rx_queues = %lld\n",
4148 			   adapter->min_rx_queues);
4149 		break;
4150 	case MIN_RX_ADD_QUEUES:
4151 		adapter->min_rx_add_queues =
4152 		    be64_to_cpu(crq->query_capability.number);
4153 		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4154 			   adapter->min_rx_add_queues);
4155 		break;
4156 	case MAX_TX_QUEUES:
4157 		adapter->max_tx_queues =
4158 		    be64_to_cpu(crq->query_capability.number);
4159 		netdev_dbg(netdev, "max_tx_queues = %lld\n",
4160 			   adapter->max_tx_queues);
4161 		break;
4162 	case MAX_RX_QUEUES:
4163 		adapter->max_rx_queues =
4164 		    be64_to_cpu(crq->query_capability.number);
4165 		netdev_dbg(netdev, "max_rx_queues = %lld\n",
4166 			   adapter->max_rx_queues);
4167 		break;
4168 	case MAX_RX_ADD_QUEUES:
4169 		adapter->max_rx_add_queues =
4170 		    be64_to_cpu(crq->query_capability.number);
4171 		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4172 			   adapter->max_rx_add_queues);
4173 		break;
4174 	case MIN_TX_ENTRIES_PER_SUBCRQ:
4175 		adapter->min_tx_entries_per_subcrq =
4176 		    be64_to_cpu(crq->query_capability.number);
4177 		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4178 			   adapter->min_tx_entries_per_subcrq);
4179 		break;
4180 	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4181 		adapter->min_rx_add_entries_per_subcrq =
4182 		    be64_to_cpu(crq->query_capability.number);
4183 		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4184 			   adapter->min_rx_add_entries_per_subcrq);
4185 		break;
4186 	case MAX_TX_ENTRIES_PER_SUBCRQ:
4187 		adapter->max_tx_entries_per_subcrq =
4188 		    be64_to_cpu(crq->query_capability.number);
4189 		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4190 			   adapter->max_tx_entries_per_subcrq);
4191 		break;
4192 	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4193 		adapter->max_rx_add_entries_per_subcrq =
4194 		    be64_to_cpu(crq->query_capability.number);
4195 		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4196 			   adapter->max_rx_add_entries_per_subcrq);
4197 		break;
4198 	case TCP_IP_OFFLOAD:
4199 		adapter->tcp_ip_offload =
4200 		    be64_to_cpu(crq->query_capability.number);
4201 		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4202 			   adapter->tcp_ip_offload);
4203 		break;
4204 	case PROMISC_SUPPORTED:
4205 		adapter->promisc_supported =
4206 		    be64_to_cpu(crq->query_capability.number);
4207 		netdev_dbg(netdev, "promisc_supported = %lld\n",
4208 			   adapter->promisc_supported);
4209 		break;
4210 	case MIN_MTU:
4211 		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4212 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4213 		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4214 		break;
4215 	case MAX_MTU:
4216 		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4217 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4218 		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4219 		break;
4220 	case MAX_MULTICAST_FILTERS:
4221 		adapter->max_multicast_filters =
4222 		    be64_to_cpu(crq->query_capability.number);
4223 		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4224 			   adapter->max_multicast_filters);
4225 		break;
4226 	case VLAN_HEADER_INSERTION:
4227 		adapter->vlan_header_insertion =
4228 		    be64_to_cpu(crq->query_capability.number);
4229 		if (adapter->vlan_header_insertion)
4230 			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4231 		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4232 			   adapter->vlan_header_insertion);
4233 		break;
4234 	case RX_VLAN_HEADER_INSERTION:
4235 		adapter->rx_vlan_header_insertion =
4236 		    be64_to_cpu(crq->query_capability.number);
4237 		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4238 			   adapter->rx_vlan_header_insertion);
4239 		break;
4240 	case MAX_TX_SG_ENTRIES:
4241 		adapter->max_tx_sg_entries =
4242 		    be64_to_cpu(crq->query_capability.number);
4243 		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4244 			   adapter->max_tx_sg_entries);
4245 		break;
4246 	case RX_SG_SUPPORTED:
4247 		adapter->rx_sg_supported =
4248 		    be64_to_cpu(crq->query_capability.number);
4249 		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4250 			   adapter->rx_sg_supported);
4251 		break;
4252 	case OPT_TX_COMP_SUB_QUEUES:
4253 		adapter->opt_tx_comp_sub_queues =
4254 		    be64_to_cpu(crq->query_capability.number);
4255 		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4256 			   adapter->opt_tx_comp_sub_queues);
4257 		break;
4258 	case OPT_RX_COMP_QUEUES:
4259 		adapter->opt_rx_comp_queues =
4260 		    be64_to_cpu(crq->query_capability.number);
4261 		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4262 			   adapter->opt_rx_comp_queues);
4263 		break;
4264 	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4265 		adapter->opt_rx_bufadd_q_per_rx_comp_q =
4266 		    be64_to_cpu(crq->query_capability.number);
4267 		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4268 			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
4269 		break;
4270 	case OPT_TX_ENTRIES_PER_SUBCRQ:
4271 		adapter->opt_tx_entries_per_subcrq =
4272 		    be64_to_cpu(crq->query_capability.number);
4273 		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4274 			   adapter->opt_tx_entries_per_subcrq);
4275 		break;
4276 	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4277 		adapter->opt_rxba_entries_per_subcrq =
4278 		    be64_to_cpu(crq->query_capability.number);
4279 		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4280 			   adapter->opt_rxba_entries_per_subcrq);
4281 		break;
4282 	case TX_RX_DESC_REQ:
4283 		adapter->tx_rx_desc_req = crq->query_capability.number;
4284 		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4285 			   adapter->tx_rx_desc_req);
4286 		break;
4287 
4288 	default:
4289 		netdev_err(netdev, "Got invalid cap rsp %d\n",
4290 			   crq->query_capability.capability);
4291 	}
4292 
4293 out:
4294 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4295 		adapter->wait_capability = false;
4296 		ibmvnic_send_req_caps(adapter, 0);
4297 	}
4298 }
4299 
4300 static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
4301 {
4302 	union ibmvnic_crq crq;
4303 	int rc;
4304 
4305 	memset(&crq, 0, sizeof(crq));
4306 	crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
4307 	crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
4308 	init_completion(&adapter->fw_done);
4309 	rc = ibmvnic_send_crq(adapter, &crq);
4310 	if (rc)
4311 		return rc;
4312 	wait_for_completion(&adapter->fw_done);
4313 	return adapter->fw_done_rc ? -EIO : 0;
4314 }
4315 
4316 static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
4317 				       struct ibmvnic_adapter *adapter)
4318 {
4319 	struct net_device *netdev = adapter->netdev;
4320 	int rc;
4321 
4322 	rc = crq->query_phys_parms_rsp.rc.code;
4323 	if (rc) {
4324 		netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
4325 		return rc;
4326 	}
4327 	switch (cpu_to_be32(crq->query_phys_parms_rsp.speed)) {
4328 	case IBMVNIC_10MBPS:
4329 		adapter->speed = SPEED_10;
4330 		break;
4331 	case IBMVNIC_100MBPS:
4332 		adapter->speed = SPEED_100;
4333 		break;
4334 	case IBMVNIC_1GBPS:
4335 		adapter->speed = SPEED_1000;
4336 		break;
4337 	case IBMVNIC_10GBP:
4338 		adapter->speed = SPEED_10000;
4339 		break;
4340 	case IBMVNIC_25GBPS:
4341 		adapter->speed = SPEED_25000;
4342 		break;
4343 	case IBMVNIC_40GBPS:
4344 		adapter->speed = SPEED_40000;
4345 		break;
4346 	case IBMVNIC_50GBPS:
4347 		adapter->speed = SPEED_50000;
4348 		break;
4349 	case IBMVNIC_100GBPS:
4350 		adapter->speed = SPEED_100000;
4351 		break;
4352 	default:
4353 		netdev_warn(netdev, "Unknown speed 0x%08x\n",
4354 			    cpu_to_be32(crq->query_phys_parms_rsp.speed));
4355 		adapter->speed = SPEED_UNKNOWN;
4356 	}
4357 	if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
4358 		adapter->duplex = DUPLEX_FULL;
4359 	else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
4360 		adapter->duplex = DUPLEX_HALF;
4361 	else
4362 		adapter->duplex = DUPLEX_UNKNOWN;
4363 
4364 	return rc;
4365 }
4366 
4367 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4368 			       struct ibmvnic_adapter *adapter)
4369 {
4370 	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4371 	struct net_device *netdev = adapter->netdev;
4372 	struct device *dev = &adapter->vdev->dev;
4373 	u64 *u64_crq = (u64 *)crq;
4374 	long rc;
4375 
4376 	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4377 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
4378 		   (unsigned long int)cpu_to_be64(u64_crq[1]));
4379 	switch (gen_crq->first) {
4380 	case IBMVNIC_CRQ_INIT_RSP:
4381 		switch (gen_crq->cmd) {
4382 		case IBMVNIC_CRQ_INIT:
4383 			dev_info(dev, "Partner initialized\n");
4384 			adapter->from_passive_init = true;
4385 			adapter->failover_pending = false;
4386 			if (!completion_done(&adapter->init_done)) {
4387 				complete(&adapter->init_done);
4388 				adapter->init_done_rc = -EIO;
4389 			}
4390 			ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4391 			break;
4392 		case IBMVNIC_CRQ_INIT_COMPLETE:
4393 			dev_info(dev, "Partner initialization complete\n");
4394 			adapter->crq.active = true;
4395 			send_version_xchg(adapter);
4396 			break;
4397 		default:
4398 			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4399 		}
4400 		return;
4401 	case IBMVNIC_CRQ_XPORT_EVENT:
4402 		netif_carrier_off(netdev);
4403 		adapter->crq.active = false;
4404 		if (adapter->resetting)
4405 			adapter->force_reset_recovery = true;
4406 		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4407 			dev_info(dev, "Migrated, re-enabling adapter\n");
4408 			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4409 		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4410 			dev_info(dev, "Backing device failover detected\n");
4411 			adapter->failover_pending = true;
4412 		} else {
4413 			/* The adapter lost the connection */
4414 			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4415 				gen_crq->cmd);
4416 			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4417 		}
4418 		return;
4419 	case IBMVNIC_CRQ_CMD_RSP:
4420 		break;
4421 	default:
4422 		dev_err(dev, "Got an invalid msg type 0x%02x\n",
4423 			gen_crq->first);
4424 		return;
4425 	}
4426 
4427 	switch (gen_crq->cmd) {
4428 	case VERSION_EXCHANGE_RSP:
4429 		rc = crq->version_exchange_rsp.rc.code;
4430 		if (rc) {
4431 			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4432 			break;
4433 		}
4434 		dev_info(dev, "Partner protocol version is %d\n",
4435 			 crq->version_exchange_rsp.version);
4436 		if (be16_to_cpu(crq->version_exchange_rsp.version) <
4437 		    ibmvnic_version)
4438 			ibmvnic_version =
4439 			    be16_to_cpu(crq->version_exchange_rsp.version);
4440 		send_cap_queries(adapter);
4441 		break;
4442 	case QUERY_CAPABILITY_RSP:
4443 		handle_query_cap_rsp(crq, adapter);
4444 		break;
4445 	case QUERY_MAP_RSP:
4446 		handle_query_map_rsp(crq, adapter);
4447 		break;
4448 	case REQUEST_MAP_RSP:
4449 		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4450 		complete(&adapter->fw_done);
4451 		break;
4452 	case REQUEST_UNMAP_RSP:
4453 		handle_request_unmap_rsp(crq, adapter);
4454 		break;
4455 	case REQUEST_CAPABILITY_RSP:
4456 		handle_request_cap_rsp(crq, adapter);
4457 		break;
4458 	case LOGIN_RSP:
4459 		netdev_dbg(netdev, "Got Login Response\n");
4460 		handle_login_rsp(crq, adapter);
4461 		break;
4462 	case LOGICAL_LINK_STATE_RSP:
4463 		netdev_dbg(netdev,
4464 			   "Got Logical Link State Response, state: %d rc: %d\n",
4465 			   crq->logical_link_state_rsp.link_state,
4466 			   crq->logical_link_state_rsp.rc.code);
4467 		adapter->logical_link_state =
4468 		    crq->logical_link_state_rsp.link_state;
4469 		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4470 		complete(&adapter->init_done);
4471 		break;
4472 	case LINK_STATE_INDICATION:
4473 		netdev_dbg(netdev, "Got Logical Link State Indication\n");
4474 		adapter->phys_link_state =
4475 		    crq->link_state_indication.phys_link_state;
4476 		adapter->logical_link_state =
4477 		    crq->link_state_indication.logical_link_state;
4478 		if (adapter->phys_link_state && adapter->logical_link_state)
4479 			netif_carrier_on(netdev);
4480 		else
4481 			netif_carrier_off(netdev);
4482 		break;
4483 	case CHANGE_MAC_ADDR_RSP:
4484 		netdev_dbg(netdev, "Got MAC address change Response\n");
4485 		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4486 		break;
4487 	case ERROR_INDICATION:
4488 		netdev_dbg(netdev, "Got Error Indication\n");
4489 		handle_error_indication(crq, adapter);
4490 		break;
4491 	case REQUEST_STATISTICS_RSP:
4492 		netdev_dbg(netdev, "Got Statistics Response\n");
4493 		complete(&adapter->stats_done);
4494 		break;
4495 	case QUERY_IP_OFFLOAD_RSP:
4496 		netdev_dbg(netdev, "Got Query IP offload Response\n");
4497 		handle_query_ip_offload_rsp(adapter);
4498 		break;
4499 	case MULTICAST_CTRL_RSP:
4500 		netdev_dbg(netdev, "Got multicast control Response\n");
4501 		break;
4502 	case CONTROL_IP_OFFLOAD_RSP:
4503 		netdev_dbg(netdev, "Got Control IP offload Response\n");
4504 		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4505 				 sizeof(adapter->ip_offload_ctrl),
4506 				 DMA_TO_DEVICE);
4507 		complete(&adapter->init_done);
4508 		break;
4509 	case COLLECT_FW_TRACE_RSP:
4510 		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4511 		complete(&adapter->fw_done);
4512 		break;
4513 	case GET_VPD_SIZE_RSP:
4514 		handle_vpd_size_rsp(crq, adapter);
4515 		break;
4516 	case GET_VPD_RSP:
4517 		handle_vpd_rsp(crq, adapter);
4518 		break;
4519 	case QUERY_PHYS_PARMS_RSP:
4520 		adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
4521 		complete(&adapter->fw_done);
4522 		break;
4523 	default:
4524 		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4525 			   gen_crq->cmd);
4526 	}
4527 }
4528 
4529 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4530 {
4531 	struct ibmvnic_adapter *adapter = instance;
4532 
4533 	tasklet_schedule(&adapter->tasklet);
4534 	return IRQ_HANDLED;
4535 }
4536 
4537 static void ibmvnic_tasklet(void *data)
4538 {
4539 	struct ibmvnic_adapter *adapter = data;
4540 	struct ibmvnic_crq_queue *queue = &adapter->crq;
4541 	union ibmvnic_crq *crq;
4542 	unsigned long flags;
4543 	bool done = false;
4544 
4545 	spin_lock_irqsave(&queue->lock, flags);
4546 	while (!done) {
4547 		/* Pull all the valid messages off the CRQ */
4548 		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4549 			ibmvnic_handle_crq(crq, adapter);
4550 			crq->generic.first = 0;
4551 		}
4552 
4553 		/* remain in tasklet until all
4554 		 * capabilities responses are received
4555 		 */
4556 		if (!adapter->wait_capability)
4557 			done = true;
4558 	}
4559 	/* if capabilities CRQ's were sent in this tasklet, the following
4560 	 * tasklet must wait until all responses are received
4561 	 */
4562 	if (atomic_read(&adapter->running_cap_crqs) != 0)
4563 		adapter->wait_capability = true;
4564 	spin_unlock_irqrestore(&queue->lock, flags);
4565 }
4566 
4567 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4568 {
4569 	struct vio_dev *vdev = adapter->vdev;
4570 	int rc;
4571 
4572 	do {
4573 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4574 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4575 
4576 	if (rc)
4577 		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4578 
4579 	return rc;
4580 }
4581 
4582 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4583 {
4584 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4585 	struct device *dev = &adapter->vdev->dev;
4586 	struct vio_dev *vdev = adapter->vdev;
4587 	int rc;
4588 
4589 	/* Close the CRQ */
4590 	do {
4591 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4592 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4593 
4594 	/* Clean out the queue */
4595 	memset(crq->msgs, 0, PAGE_SIZE);
4596 	crq->cur = 0;
4597 	crq->active = false;
4598 
4599 	/* And re-open it again */
4600 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4601 				crq->msg_token, PAGE_SIZE);
4602 
4603 	if (rc == H_CLOSED)
4604 		/* Adapter is good, but other end is not ready */
4605 		dev_warn(dev, "Partner adapter not ready\n");
4606 	else if (rc != 0)
4607 		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4608 
4609 	return rc;
4610 }
4611 
4612 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4613 {
4614 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4615 	struct vio_dev *vdev = adapter->vdev;
4616 	long rc;
4617 
4618 	if (!crq->msgs)
4619 		return;
4620 
4621 	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4622 	free_irq(vdev->irq, adapter);
4623 	tasklet_kill(&adapter->tasklet);
4624 	do {
4625 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4626 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4627 
4628 	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4629 			 DMA_BIDIRECTIONAL);
4630 	free_page((unsigned long)crq->msgs);
4631 	crq->msgs = NULL;
4632 	crq->active = false;
4633 }
4634 
4635 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4636 {
4637 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4638 	struct device *dev = &adapter->vdev->dev;
4639 	struct vio_dev *vdev = adapter->vdev;
4640 	int rc, retrc = -ENOMEM;
4641 
4642 	if (crq->msgs)
4643 		return 0;
4644 
4645 	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4646 	/* Should we allocate more than one page? */
4647 
4648 	if (!crq->msgs)
4649 		return -ENOMEM;
4650 
4651 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4652 	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4653 					DMA_BIDIRECTIONAL);
4654 	if (dma_mapping_error(dev, crq->msg_token))
4655 		goto map_failed;
4656 
4657 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4658 				crq->msg_token, PAGE_SIZE);
4659 
4660 	if (rc == H_RESOURCE)
4661 		/* maybe kexecing and resource is busy. try a reset */
4662 		rc = ibmvnic_reset_crq(adapter);
4663 	retrc = rc;
4664 
4665 	if (rc == H_CLOSED) {
4666 		dev_warn(dev, "Partner adapter not ready\n");
4667 	} else if (rc) {
4668 		dev_warn(dev, "Error %d opening adapter\n", rc);
4669 		goto reg_crq_failed;
4670 	}
4671 
4672 	retrc = 0;
4673 
4674 	tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4675 		     (unsigned long)adapter);
4676 
4677 	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4678 	snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
4679 		 adapter->vdev->unit_address);
4680 	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
4681 	if (rc) {
4682 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4683 			vdev->irq, rc);
4684 		goto req_irq_failed;
4685 	}
4686 
4687 	rc = vio_enable_interrupts(vdev);
4688 	if (rc) {
4689 		dev_err(dev, "Error %d enabling interrupts\n", rc);
4690 		goto req_irq_failed;
4691 	}
4692 
4693 	crq->cur = 0;
4694 	spin_lock_init(&crq->lock);
4695 
4696 	return retrc;
4697 
4698 req_irq_failed:
4699 	tasklet_kill(&adapter->tasklet);
4700 	do {
4701 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4702 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4703 reg_crq_failed:
4704 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4705 map_failed:
4706 	free_page((unsigned long)crq->msgs);
4707 	crq->msgs = NULL;
4708 	return retrc;
4709 }
4710 
4711 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
4712 {
4713 	struct device *dev = &adapter->vdev->dev;
4714 	unsigned long timeout = msecs_to_jiffies(30000);
4715 	u64 old_num_rx_queues, old_num_tx_queues;
4716 	int rc;
4717 
4718 	adapter->from_passive_init = false;
4719 
4720 	old_num_rx_queues = adapter->req_rx_queues;
4721 	old_num_tx_queues = adapter->req_tx_queues;
4722 
4723 	reinit_completion(&adapter->init_done);
4724 	adapter->init_done_rc = 0;
4725 	ibmvnic_send_crq_init(adapter);
4726 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4727 		dev_err(dev, "Initialization sequence timed out\n");
4728 		return -1;
4729 	}
4730 
4731 	if (adapter->init_done_rc) {
4732 		release_crq_queue(adapter);
4733 		return adapter->init_done_rc;
4734 	}
4735 
4736 	if (adapter->from_passive_init) {
4737 		adapter->state = VNIC_OPEN;
4738 		adapter->from_passive_init = false;
4739 		return -1;
4740 	}
4741 
4742 	if (adapter->resetting && !adapter->wait_for_reset &&
4743 	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
4744 		if (adapter->req_rx_queues != old_num_rx_queues ||
4745 		    adapter->req_tx_queues != old_num_tx_queues) {
4746 			release_sub_crqs(adapter, 0);
4747 			rc = init_sub_crqs(adapter);
4748 		} else {
4749 			rc = reset_sub_crq_queues(adapter);
4750 		}
4751 	} else {
4752 		rc = init_sub_crqs(adapter);
4753 	}
4754 
4755 	if (rc) {
4756 		dev_err(dev, "Initialization of sub crqs failed\n");
4757 		release_crq_queue(adapter);
4758 		return rc;
4759 	}
4760 
4761 	rc = init_sub_crq_irqs(adapter);
4762 	if (rc) {
4763 		dev_err(dev, "Failed to initialize sub crq irqs\n");
4764 		release_crq_queue(adapter);
4765 	}
4766 
4767 	return rc;
4768 }
4769 
4770 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4771 {
4772 	struct device *dev = &adapter->vdev->dev;
4773 	unsigned long timeout = msecs_to_jiffies(30000);
4774 	int rc;
4775 
4776 	adapter->from_passive_init = false;
4777 
4778 	adapter->init_done_rc = 0;
4779 	ibmvnic_send_crq_init(adapter);
4780 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4781 		dev_err(dev, "Initialization sequence timed out\n");
4782 		return -1;
4783 	}
4784 
4785 	if (adapter->init_done_rc) {
4786 		release_crq_queue(adapter);
4787 		return adapter->init_done_rc;
4788 	}
4789 
4790 	if (adapter->from_passive_init) {
4791 		adapter->state = VNIC_OPEN;
4792 		adapter->from_passive_init = false;
4793 		return -1;
4794 	}
4795 
4796 	rc = init_sub_crqs(adapter);
4797 	if (rc) {
4798 		dev_err(dev, "Initialization of sub crqs failed\n");
4799 		release_crq_queue(adapter);
4800 		return rc;
4801 	}
4802 
4803 	rc = init_sub_crq_irqs(adapter);
4804 	if (rc) {
4805 		dev_err(dev, "Failed to initialize sub crq irqs\n");
4806 		release_crq_queue(adapter);
4807 	}
4808 
4809 	return rc;
4810 }
4811 
4812 static struct device_attribute dev_attr_failover;
4813 
4814 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4815 {
4816 	struct ibmvnic_adapter *adapter;
4817 	struct net_device *netdev;
4818 	unsigned char *mac_addr_p;
4819 	int rc;
4820 
4821 	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4822 		dev->unit_address);
4823 
4824 	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4825 							VETH_MAC_ADDR, NULL);
4826 	if (!mac_addr_p) {
4827 		dev_err(&dev->dev,
4828 			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4829 			__FILE__, __LINE__);
4830 		return 0;
4831 	}
4832 
4833 	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4834 				   IBMVNIC_MAX_QUEUES);
4835 	if (!netdev)
4836 		return -ENOMEM;
4837 
4838 	adapter = netdev_priv(netdev);
4839 	adapter->state = VNIC_PROBING;
4840 	dev_set_drvdata(&dev->dev, netdev);
4841 	adapter->vdev = dev;
4842 	adapter->netdev = netdev;
4843 
4844 	ether_addr_copy(adapter->mac_addr, mac_addr_p);
4845 	ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4846 	netdev->irq = dev->irq;
4847 	netdev->netdev_ops = &ibmvnic_netdev_ops;
4848 	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4849 	SET_NETDEV_DEV(netdev, &dev->dev);
4850 
4851 	spin_lock_init(&adapter->stats_lock);
4852 
4853 	INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4854 	INIT_LIST_HEAD(&adapter->rwi_list);
4855 	spin_lock_init(&adapter->rwi_lock);
4856 	init_completion(&adapter->init_done);
4857 	adapter->resetting = false;
4858 
4859 	do {
4860 		rc = init_crq_queue(adapter);
4861 		if (rc) {
4862 			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4863 				rc);
4864 			goto ibmvnic_init_fail;
4865 		}
4866 
4867 		rc = ibmvnic_init(adapter);
4868 		if (rc && rc != EAGAIN)
4869 			goto ibmvnic_init_fail;
4870 	} while (rc == EAGAIN);
4871 
4872 	rc = init_stats_buffers(adapter);
4873 	if (rc)
4874 		goto ibmvnic_init_fail;
4875 
4876 	rc = init_stats_token(adapter);
4877 	if (rc)
4878 		goto ibmvnic_stats_fail;
4879 
4880 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4881 	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4882 	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4883 
4884 	rc = device_create_file(&dev->dev, &dev_attr_failover);
4885 	if (rc)
4886 		goto ibmvnic_dev_file_err;
4887 
4888 	netif_carrier_off(netdev);
4889 	rc = register_netdev(netdev);
4890 	if (rc) {
4891 		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4892 		goto ibmvnic_register_fail;
4893 	}
4894 	dev_info(&dev->dev, "ibmvnic registered\n");
4895 
4896 	adapter->state = VNIC_PROBED;
4897 
4898 	adapter->wait_for_reset = false;
4899 
4900 	return 0;
4901 
4902 ibmvnic_register_fail:
4903 	device_remove_file(&dev->dev, &dev_attr_failover);
4904 
4905 ibmvnic_dev_file_err:
4906 	release_stats_token(adapter);
4907 
4908 ibmvnic_stats_fail:
4909 	release_stats_buffers(adapter);
4910 
4911 ibmvnic_init_fail:
4912 	release_sub_crqs(adapter, 1);
4913 	release_crq_queue(adapter);
4914 	free_netdev(netdev);
4915 
4916 	return rc;
4917 }
4918 
4919 static int ibmvnic_remove(struct vio_dev *dev)
4920 {
4921 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
4922 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4923 
4924 	adapter->state = VNIC_REMOVING;
4925 	rtnl_lock();
4926 	unregister_netdevice(netdev);
4927 
4928 	release_resources(adapter);
4929 	release_sub_crqs(adapter, 1);
4930 	release_crq_queue(adapter);
4931 
4932 	release_stats_token(adapter);
4933 	release_stats_buffers(adapter);
4934 
4935 	adapter->state = VNIC_REMOVED;
4936 
4937 	rtnl_unlock();
4938 	device_remove_file(&dev->dev, &dev_attr_failover);
4939 	free_netdev(netdev);
4940 	dev_set_drvdata(&dev->dev, NULL);
4941 
4942 	return 0;
4943 }
4944 
4945 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4946 			      const char *buf, size_t count)
4947 {
4948 	struct net_device *netdev = dev_get_drvdata(dev);
4949 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4950 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4951 	__be64 session_token;
4952 	long rc;
4953 
4954 	if (!sysfs_streq(buf, "1"))
4955 		return -EINVAL;
4956 
4957 	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4958 			 H_GET_SESSION_TOKEN, 0, 0, 0);
4959 	if (rc) {
4960 		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4961 			   rc);
4962 		return -EINVAL;
4963 	}
4964 
4965 	session_token = (__be64)retbuf[0];
4966 	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4967 		   be64_to_cpu(session_token));
4968 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4969 				H_SESSION_ERR_DETECTED, session_token, 0, 0);
4970 	if (rc) {
4971 		netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4972 			   rc);
4973 		return -EINVAL;
4974 	}
4975 
4976 	return count;
4977 }
4978 
4979 static DEVICE_ATTR_WO(failover);
4980 
4981 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4982 {
4983 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4984 	struct ibmvnic_adapter *adapter;
4985 	struct iommu_table *tbl;
4986 	unsigned long ret = 0;
4987 	int i;
4988 
4989 	tbl = get_iommu_table_base(&vdev->dev);
4990 
4991 	/* netdev inits at probe time along with the structures we need below*/
4992 	if (!netdev)
4993 		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4994 
4995 	adapter = netdev_priv(netdev);
4996 
4997 	ret += PAGE_SIZE; /* the crq message queue */
4998 	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4999 
5000 	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
5001 		ret += 4 * PAGE_SIZE; /* the scrq message queue */
5002 
5003 	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
5004 	     i++)
5005 		ret += adapter->rx_pool[i].size *
5006 		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
5007 
5008 	return ret;
5009 }
5010 
5011 static int ibmvnic_resume(struct device *dev)
5012 {
5013 	struct net_device *netdev = dev_get_drvdata(dev);
5014 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
5015 
5016 	if (adapter->state != VNIC_OPEN)
5017 		return 0;
5018 
5019 	tasklet_schedule(&adapter->tasklet);
5020 
5021 	return 0;
5022 }
5023 
5024 static const struct vio_device_id ibmvnic_device_table[] = {
5025 	{"network", "IBM,vnic"},
5026 	{"", "" }
5027 };
5028 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
5029 
5030 static const struct dev_pm_ops ibmvnic_pm_ops = {
5031 	.resume = ibmvnic_resume
5032 };
5033 
5034 static struct vio_driver ibmvnic_driver = {
5035 	.id_table       = ibmvnic_device_table,
5036 	.probe          = ibmvnic_probe,
5037 	.remove         = ibmvnic_remove,
5038 	.get_desired_dma = ibmvnic_get_desired_dma,
5039 	.name		= ibmvnic_driver_name,
5040 	.pm		= &ibmvnic_pm_ops,
5041 };
5042 
5043 /* module functions */
5044 static int __init ibmvnic_module_init(void)
5045 {
5046 	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
5047 		IBMVNIC_DRIVER_VERSION);
5048 
5049 	return vio_register_driver(&ibmvnic_driver);
5050 }
5051 
5052 static void __exit ibmvnic_module_exit(void)
5053 {
5054 	vio_unregister_driver(&ibmvnic_driver);
5055 }
5056 
5057 module_init(ibmvnic_module_init);
5058 module_exit(ibmvnic_module_exit);
5059