xref: /openbmc/linux/drivers/net/ethernet/ibm/ibmvnic.c (revision 4f139972b489f8bc2c821aa25ac65018d92af3f7)
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/in.h>
63 #include <linux/ip.h>
64 #include <linux/ipv6.h>
65 #include <linux/irq.h>
66 #include <linux/kthread.h>
67 #include <linux/seq_file.h>
68 #include <linux/interrupt.h>
69 #include <net/net_namespace.h>
70 #include <asm/hvcall.h>
71 #include <linux/atomic.h>
72 #include <asm/vio.h>
73 #include <asm/iommu.h>
74 #include <linux/uaccess.h>
75 #include <asm/firmware.h>
76 #include <linux/workqueue.h>
77 
78 #include "ibmvnic.h"
79 
80 static const char ibmvnic_driver_name[] = "ibmvnic";
81 static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
82 
83 MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
84 MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
87 
88 static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
89 static int ibmvnic_remove(struct vio_dev *);
90 static void release_sub_crqs(struct ibmvnic_adapter *);
91 static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
92 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
93 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
94 static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
95 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
96 		       union sub_crq *sub_crq);
97 static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
98 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
99 static int enable_scrq_irq(struct ibmvnic_adapter *,
100 			   struct ibmvnic_sub_crq_queue *);
101 static int disable_scrq_irq(struct ibmvnic_adapter *,
102 			    struct ibmvnic_sub_crq_queue *);
103 static int pending_scrq(struct ibmvnic_adapter *,
104 			struct ibmvnic_sub_crq_queue *);
105 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
106 					struct ibmvnic_sub_crq_queue *);
107 static int ibmvnic_poll(struct napi_struct *napi, int data);
108 static void send_map_query(struct ibmvnic_adapter *adapter);
109 static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
110 static void send_request_unmap(struct ibmvnic_adapter *, u8);
111 static void send_login(struct ibmvnic_adapter *adapter);
112 static void send_cap_queries(struct ibmvnic_adapter *adapter);
113 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
114 static int ibmvnic_init(struct ibmvnic_adapter *);
115 static void release_crq_queue(struct ibmvnic_adapter *);
116 
117 struct ibmvnic_stat {
118 	char name[ETH_GSTRING_LEN];
119 	int offset;
120 };
121 
122 #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
123 			     offsetof(struct ibmvnic_statistics, stat))
124 #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
125 
126 static const struct ibmvnic_stat ibmvnic_stats[] = {
127 	{"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
128 	{"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
129 	{"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
130 	{"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
131 	{"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
132 	{"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
133 	{"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
134 	{"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
135 	{"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
136 	{"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
137 	{"align_errors", IBMVNIC_STAT_OFF(align_errors)},
138 	{"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
139 	{"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
140 	{"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
141 	{"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
142 	{"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
143 	{"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
144 	{"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
145 	{"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
146 	{"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
147 	{"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
148 	{"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
149 };
150 
151 static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
152 			  unsigned long length, unsigned long *number,
153 			  unsigned long *irq)
154 {
155 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
156 	long rc;
157 
158 	rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
159 	*number = retbuf[0];
160 	*irq = retbuf[1];
161 
162 	return rc;
163 }
164 
165 static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
166 				struct ibmvnic_long_term_buff *ltb, int size)
167 {
168 	struct device *dev = &adapter->vdev->dev;
169 
170 	ltb->size = size;
171 	ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
172 				       GFP_KERNEL);
173 
174 	if (!ltb->buff) {
175 		dev_err(dev, "Couldn't alloc long term buffer\n");
176 		return -ENOMEM;
177 	}
178 	ltb->map_id = adapter->map_id;
179 	adapter->map_id++;
180 
181 	init_completion(&adapter->fw_done);
182 	send_request_map(adapter, ltb->addr,
183 			 ltb->size, ltb->map_id);
184 	wait_for_completion(&adapter->fw_done);
185 	return 0;
186 }
187 
188 static void free_long_term_buff(struct ibmvnic_adapter *adapter,
189 				struct ibmvnic_long_term_buff *ltb)
190 {
191 	struct device *dev = &adapter->vdev->dev;
192 
193 	if (!ltb->buff)
194 		return;
195 
196 	dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
197 	if (!adapter->failover)
198 		send_request_unmap(adapter, ltb->map_id);
199 }
200 
201 static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
202 			      struct ibmvnic_rx_pool *pool)
203 {
204 	int count = pool->size - atomic_read(&pool->available);
205 	struct device *dev = &adapter->vdev->dev;
206 	int buffers_added = 0;
207 	unsigned long lpar_rc;
208 	union sub_crq sub_crq;
209 	struct sk_buff *skb;
210 	unsigned int offset;
211 	dma_addr_t dma_addr;
212 	unsigned char *dst;
213 	u64 *handle_array;
214 	int shift = 0;
215 	int index;
216 	int i;
217 
218 	handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
219 				      be32_to_cpu(adapter->login_rsp_buf->
220 				      off_rxadd_subcrqs));
221 
222 	for (i = 0; i < count; ++i) {
223 		skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
224 		if (!skb) {
225 			dev_err(dev, "Couldn't replenish rx buff\n");
226 			adapter->replenish_no_mem++;
227 			break;
228 		}
229 
230 		index = pool->free_map[pool->next_free];
231 
232 		if (pool->rx_buff[index].skb)
233 			dev_err(dev, "Inconsistent free_map!\n");
234 
235 		/* Copy the skb to the long term mapped DMA buffer */
236 		offset = index * pool->buff_size;
237 		dst = pool->long_term_buff.buff + offset;
238 		memset(dst, 0, pool->buff_size);
239 		dma_addr = pool->long_term_buff.addr + offset;
240 		pool->rx_buff[index].data = dst;
241 
242 		pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
243 		pool->rx_buff[index].dma = dma_addr;
244 		pool->rx_buff[index].skb = skb;
245 		pool->rx_buff[index].pool_index = pool->index;
246 		pool->rx_buff[index].size = pool->buff_size;
247 
248 		memset(&sub_crq, 0, sizeof(sub_crq));
249 		sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
250 		sub_crq.rx_add.correlator =
251 		    cpu_to_be64((u64)&pool->rx_buff[index]);
252 		sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
253 		sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
254 
255 		/* The length field of the sCRQ is defined to be 24 bits so the
256 		 * buffer size needs to be left shifted by a byte before it is
257 		 * converted to big endian to prevent the last byte from being
258 		 * truncated.
259 		 */
260 #ifdef __LITTLE_ENDIAN__
261 		shift = 8;
262 #endif
263 		sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
264 
265 		lpar_rc = send_subcrq(adapter, handle_array[pool->index],
266 				      &sub_crq);
267 		if (lpar_rc != H_SUCCESS)
268 			goto failure;
269 
270 		buffers_added++;
271 		adapter->replenish_add_buff_success++;
272 		pool->next_free = (pool->next_free + 1) % pool->size;
273 	}
274 	atomic_add(buffers_added, &pool->available);
275 	return;
276 
277 failure:
278 	dev_info(dev, "replenish pools failure\n");
279 	pool->free_map[pool->next_free] = index;
280 	pool->rx_buff[index].skb = NULL;
281 	if (!dma_mapping_error(dev, dma_addr))
282 		dma_unmap_single(dev, dma_addr, pool->buff_size,
283 				 DMA_FROM_DEVICE);
284 
285 	dev_kfree_skb_any(skb);
286 	adapter->replenish_add_buff_failure++;
287 	atomic_add(buffers_added, &pool->available);
288 }
289 
290 static void replenish_pools(struct ibmvnic_adapter *adapter)
291 {
292 	int i;
293 
294 	if (adapter->migrated)
295 		return;
296 
297 	adapter->replenish_task_cycles++;
298 	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
299 	     i++) {
300 		if (adapter->rx_pool[i].active)
301 			replenish_rx_pool(adapter, &adapter->rx_pool[i]);
302 	}
303 }
304 
305 static void release_stats_token(struct ibmvnic_adapter *adapter)
306 {
307 	struct device *dev = &adapter->vdev->dev;
308 
309 	if (!adapter->stats_token)
310 		return;
311 
312 	dma_unmap_single(dev, adapter->stats_token,
313 			 sizeof(struct ibmvnic_statistics),
314 			 DMA_FROM_DEVICE);
315 	adapter->stats_token = 0;
316 }
317 
318 static int init_stats_token(struct ibmvnic_adapter *adapter)
319 {
320 	struct device *dev = &adapter->vdev->dev;
321 	dma_addr_t stok;
322 
323 	stok = dma_map_single(dev, &adapter->stats,
324 			      sizeof(struct ibmvnic_statistics),
325 			      DMA_FROM_DEVICE);
326 	if (dma_mapping_error(dev, stok)) {
327 		dev_err(dev, "Couldn't map stats buffer\n");
328 		return -1;
329 	}
330 
331 	adapter->stats_token = stok;
332 	return 0;
333 }
334 
335 static void release_rx_pools(struct ibmvnic_adapter *adapter)
336 {
337 	struct ibmvnic_rx_pool *rx_pool;
338 	int rx_scrqs;
339 	int i, j;
340 
341 	if (!adapter->rx_pool)
342 		return;
343 
344 	rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
345 	for (i = 0; i < rx_scrqs; i++) {
346 		rx_pool = &adapter->rx_pool[i];
347 
348 		kfree(rx_pool->free_map);
349 		free_long_term_buff(adapter, &rx_pool->long_term_buff);
350 
351 		if (!rx_pool->rx_buff)
352 		continue;
353 
354 		for (j = 0; j < rx_pool->size; j++) {
355 			if (rx_pool->rx_buff[j].skb) {
356 				dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
357 				rx_pool->rx_buff[i].skb = NULL;
358 			}
359 		}
360 
361 		kfree(rx_pool->rx_buff);
362 	}
363 
364 	kfree(adapter->rx_pool);
365 	adapter->rx_pool = NULL;
366 }
367 
368 static int init_rx_pools(struct net_device *netdev)
369 {
370 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
371 	struct device *dev = &adapter->vdev->dev;
372 	struct ibmvnic_rx_pool *rx_pool;
373 	int rxadd_subcrqs;
374 	u64 *size_array;
375 	int i, j;
376 
377 	rxadd_subcrqs =
378 		be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
379 	size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
380 		be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
381 
382 	adapter->rx_pool = kcalloc(rxadd_subcrqs,
383 				   sizeof(struct ibmvnic_rx_pool),
384 				   GFP_KERNEL);
385 	if (!adapter->rx_pool) {
386 		dev_err(dev, "Failed to allocate rx pools\n");
387 		return -1;
388 	}
389 
390 	for (i = 0; i < rxadd_subcrqs; i++) {
391 		rx_pool = &adapter->rx_pool[i];
392 
393 		netdev_dbg(adapter->netdev,
394 			   "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
395 			   i, adapter->req_rx_add_entries_per_subcrq,
396 			   be64_to_cpu(size_array[i]));
397 
398 		rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
399 		rx_pool->index = i;
400 		rx_pool->buff_size = be64_to_cpu(size_array[i]);
401 		rx_pool->active = 1;
402 
403 		rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
404 					    GFP_KERNEL);
405 		if (!rx_pool->free_map) {
406 			release_rx_pools(adapter);
407 			return -1;
408 		}
409 
410 		rx_pool->rx_buff = kcalloc(rx_pool->size,
411 					   sizeof(struct ibmvnic_rx_buff),
412 					   GFP_KERNEL);
413 		if (!rx_pool->rx_buff) {
414 			dev_err(dev, "Couldn't alloc rx buffers\n");
415 			release_rx_pools(adapter);
416 			return -1;
417 		}
418 
419 		if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
420 					 rx_pool->size * rx_pool->buff_size)) {
421 			release_rx_pools(adapter);
422 			return -1;
423 		}
424 
425 		for (j = 0; j < rx_pool->size; ++j)
426 			rx_pool->free_map[j] = j;
427 
428 		atomic_set(&rx_pool->available, 0);
429 		rx_pool->next_alloc = 0;
430 		rx_pool->next_free = 0;
431 	}
432 
433 	return 0;
434 }
435 
436 static void release_tx_pools(struct ibmvnic_adapter *adapter)
437 {
438 	struct ibmvnic_tx_pool *tx_pool;
439 	int i, tx_scrqs;
440 
441 	if (!adapter->tx_pool)
442 		return;
443 
444 	tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
445 	for (i = 0; i < tx_scrqs; i++) {
446 		tx_pool = &adapter->tx_pool[i];
447 		kfree(tx_pool->tx_buff);
448 		free_long_term_buff(adapter, &tx_pool->long_term_buff);
449 		kfree(tx_pool->free_map);
450 	}
451 
452 	kfree(adapter->tx_pool);
453 	adapter->tx_pool = NULL;
454 }
455 
456 static int init_tx_pools(struct net_device *netdev)
457 {
458 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
459 	struct device *dev = &adapter->vdev->dev;
460 	struct ibmvnic_tx_pool *tx_pool;
461 	int tx_subcrqs;
462 	int i, j;
463 
464 	tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
465 	adapter->tx_pool = kcalloc(tx_subcrqs,
466 				   sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
467 	if (!adapter->tx_pool)
468 		return -1;
469 
470 	for (i = 0; i < tx_subcrqs; i++) {
471 		tx_pool = &adapter->tx_pool[i];
472 		tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
473 					   sizeof(struct ibmvnic_tx_buff),
474 					   GFP_KERNEL);
475 		if (!tx_pool->tx_buff) {
476 			dev_err(dev, "tx pool buffer allocation failed\n");
477 			release_tx_pools(adapter);
478 			return -1;
479 		}
480 
481 		if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
482 					 adapter->req_tx_entries_per_subcrq *
483 					 adapter->req_mtu)) {
484 			release_tx_pools(adapter);
485 			return -1;
486 		}
487 
488 		tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
489 					    sizeof(int), GFP_KERNEL);
490 		if (!tx_pool->free_map) {
491 			release_tx_pools(adapter);
492 			return -1;
493 		}
494 
495 		for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
496 			tx_pool->free_map[j] = j;
497 
498 		tx_pool->consumer_index = 0;
499 		tx_pool->producer_index = 0;
500 	}
501 
502 	return 0;
503 }
504 
505 static void release_bounce_buffer(struct ibmvnic_adapter *adapter)
506 {
507 	struct device *dev = &adapter->vdev->dev;
508 
509 	if (!adapter->bounce_buffer)
510 		return;
511 
512 	if (!dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
513 		dma_unmap_single(dev, adapter->bounce_buffer_dma,
514 				 adapter->bounce_buffer_size,
515 				 DMA_BIDIRECTIONAL);
516 		adapter->bounce_buffer_dma = DMA_ERROR_CODE;
517 	}
518 
519 	kfree(adapter->bounce_buffer);
520 	adapter->bounce_buffer = NULL;
521 }
522 
523 static int init_bounce_buffer(struct net_device *netdev)
524 {
525 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
526 	struct device *dev = &adapter->vdev->dev;
527 	char *buf;
528 	int buf_sz;
529 	dma_addr_t map_addr;
530 
531 	buf_sz = (netdev->mtu + ETH_HLEN - 1) / PAGE_SIZE + 1;
532 	buf = kmalloc(adapter->bounce_buffer_size, GFP_KERNEL);
533 	if (!buf)
534 		return -1;
535 
536 	map_addr = dma_map_single(dev, buf, buf_sz, DMA_TO_DEVICE);
537 	if (dma_mapping_error(dev, map_addr)) {
538 		dev_err(dev, "Couldn't map bounce buffer\n");
539 		kfree(buf);
540 		return -1;
541 	}
542 
543 	adapter->bounce_buffer = buf;
544 	adapter->bounce_buffer_size = buf_sz;
545 	adapter->bounce_buffer_dma = map_addr;
546 	return 0;
547 }
548 
549 static int ibmvnic_login(struct net_device *netdev)
550 {
551 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
552 	unsigned long timeout = msecs_to_jiffies(30000);
553 	struct device *dev = &adapter->vdev->dev;
554 
555 	do {
556 		if (adapter->renegotiate) {
557 			adapter->renegotiate = false;
558 			release_sub_crqs(adapter);
559 
560 			reinit_completion(&adapter->init_done);
561 			send_cap_queries(adapter);
562 			if (!wait_for_completion_timeout(&adapter->init_done,
563 							 timeout)) {
564 				dev_err(dev, "Capabilities query timeout\n");
565 				return -1;
566 			}
567 		}
568 
569 		reinit_completion(&adapter->init_done);
570 		send_login(adapter);
571 		if (!wait_for_completion_timeout(&adapter->init_done,
572 						 timeout)) {
573 			dev_err(dev, "Login timeout\n");
574 			return -1;
575 		}
576 	} while (adapter->renegotiate);
577 
578 	return 0;
579 }
580 
581 static void release_resources(struct ibmvnic_adapter *adapter)
582 {
583 	release_bounce_buffer(adapter);
584 	release_tx_pools(adapter);
585 	release_rx_pools(adapter);
586 
587 	release_sub_crqs(adapter);
588 	release_crq_queue(adapter);
589 
590 	release_stats_token(adapter);
591 }
592 
593 static int ibmvnic_open(struct net_device *netdev)
594 {
595 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
596 	struct device *dev = &adapter->vdev->dev;
597 	union ibmvnic_crq crq;
598 	int rc = 0;
599 	int i;
600 
601 	if (adapter->is_closed) {
602 		rc = ibmvnic_init(adapter);
603 		if (rc)
604 			return rc;
605 	}
606 
607 	rc = ibmvnic_login(netdev);
608 	if (rc)
609 		return rc;
610 
611 	rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
612 	if (rc) {
613 		dev_err(dev, "failed to set the number of tx queues\n");
614 		return -1;
615 	}
616 
617 	rc = init_sub_crq_irqs(adapter);
618 	if (rc) {
619 		dev_err(dev, "failed to initialize sub crq irqs\n");
620 		return -1;
621 	}
622 
623 	adapter->map_id = 1;
624 	adapter->napi = kcalloc(adapter->req_rx_queues,
625 				sizeof(struct napi_struct), GFP_KERNEL);
626 	if (!adapter->napi)
627 		goto ibmvnic_open_fail;
628 	for (i = 0; i < adapter->req_rx_queues; i++) {
629 		netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
630 			       NAPI_POLL_WEIGHT);
631 		napi_enable(&adapter->napi[i]);
632 	}
633 
634 	send_map_query(adapter);
635 
636 	rc = init_rx_pools(netdev);
637 	if (rc)
638 		goto ibmvnic_open_fail;
639 
640 	rc = init_tx_pools(netdev);
641 	if (rc)
642 		goto ibmvnic_open_fail;
643 
644 	rc = init_bounce_buffer(netdev);
645 	if (rc)
646 		goto ibmvnic_open_fail;
647 
648 	replenish_pools(adapter);
649 
650 	/* We're ready to receive frames, enable the sub-crq interrupts and
651 	 * set the logical link state to up
652 	 */
653 	for (i = 0; i < adapter->req_rx_queues; i++)
654 		enable_scrq_irq(adapter, adapter->rx_scrq[i]);
655 
656 	for (i = 0; i < adapter->req_tx_queues; i++)
657 		enable_scrq_irq(adapter, adapter->tx_scrq[i]);
658 
659 	memset(&crq, 0, sizeof(crq));
660 	crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
661 	crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
662 	crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_UP;
663 	ibmvnic_send_crq(adapter, &crq);
664 
665 	netif_tx_start_all_queues(netdev);
666 	adapter->is_closed = false;
667 
668 	return 0;
669 
670 ibmvnic_open_fail:
671 	for (i = 0; i < adapter->req_rx_queues; i++)
672 		napi_disable(&adapter->napi[i]);
673 	release_resources(adapter);
674 	return -ENOMEM;
675 }
676 
677 static int ibmvnic_close(struct net_device *netdev)
678 {
679 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
680 	union ibmvnic_crq crq;
681 	int i;
682 
683 	adapter->closing = true;
684 
685 	for (i = 0; i < adapter->req_rx_queues; i++)
686 		napi_disable(&adapter->napi[i]);
687 
688 	if (!adapter->failover)
689 		netif_tx_stop_all_queues(netdev);
690 
691 	memset(&crq, 0, sizeof(crq));
692 	crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
693 	crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
694 	crq.logical_link_state.link_state = IBMVNIC_LOGICAL_LNK_DN;
695 	ibmvnic_send_crq(adapter, &crq);
696 
697 	release_resources(adapter);
698 
699 	adapter->is_closed = true;
700 	adapter->closing = false;
701 	return 0;
702 }
703 
704 /**
705  * build_hdr_data - creates L2/L3/L4 header data buffer
706  * @hdr_field - bitfield determining needed headers
707  * @skb - socket buffer
708  * @hdr_len - array of header lengths
709  * @tot_len - total length of data
710  *
711  * Reads hdr_field to determine which headers are needed by firmware.
712  * Builds a buffer containing these headers.  Saves individual header
713  * lengths and total buffer length to be used to build descriptors.
714  */
715 static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
716 			  int *hdr_len, u8 *hdr_data)
717 {
718 	int len = 0;
719 	u8 *hdr;
720 
721 	hdr_len[0] = sizeof(struct ethhdr);
722 
723 	if (skb->protocol == htons(ETH_P_IP)) {
724 		hdr_len[1] = ip_hdr(skb)->ihl * 4;
725 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
726 			hdr_len[2] = tcp_hdrlen(skb);
727 		else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
728 			hdr_len[2] = sizeof(struct udphdr);
729 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
730 		hdr_len[1] = sizeof(struct ipv6hdr);
731 		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
732 			hdr_len[2] = tcp_hdrlen(skb);
733 		else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
734 			hdr_len[2] = sizeof(struct udphdr);
735 	}
736 
737 	memset(hdr_data, 0, 120);
738 	if ((hdr_field >> 6) & 1) {
739 		hdr = skb_mac_header(skb);
740 		memcpy(hdr_data, hdr, hdr_len[0]);
741 		len += hdr_len[0];
742 	}
743 
744 	if ((hdr_field >> 5) & 1) {
745 		hdr = skb_network_header(skb);
746 		memcpy(hdr_data + len, hdr, hdr_len[1]);
747 		len += hdr_len[1];
748 	}
749 
750 	if ((hdr_field >> 4) & 1) {
751 		hdr = skb_transport_header(skb);
752 		memcpy(hdr_data + len, hdr, hdr_len[2]);
753 		len += hdr_len[2];
754 	}
755 	return len;
756 }
757 
758 /**
759  * create_hdr_descs - create header and header extension descriptors
760  * @hdr_field - bitfield determining needed headers
761  * @data - buffer containing header data
762  * @len - length of data buffer
763  * @hdr_len - array of individual header lengths
764  * @scrq_arr - descriptor array
765  *
766  * Creates header and, if needed, header extension descriptors and
767  * places them in a descriptor array, scrq_arr
768  */
769 
770 static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
771 			     union sub_crq *scrq_arr)
772 {
773 	union sub_crq hdr_desc;
774 	int tmp_len = len;
775 	u8 *data, *cur;
776 	int tmp;
777 
778 	while (tmp_len > 0) {
779 		cur = hdr_data + len - tmp_len;
780 
781 		memset(&hdr_desc, 0, sizeof(hdr_desc));
782 		if (cur != hdr_data) {
783 			data = hdr_desc.hdr_ext.data;
784 			tmp = tmp_len > 29 ? 29 : tmp_len;
785 			hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
786 			hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
787 			hdr_desc.hdr_ext.len = tmp;
788 		} else {
789 			data = hdr_desc.hdr.data;
790 			tmp = tmp_len > 24 ? 24 : tmp_len;
791 			hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
792 			hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
793 			hdr_desc.hdr.len = tmp;
794 			hdr_desc.hdr.l2_len = (u8)hdr_len[0];
795 			hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
796 			hdr_desc.hdr.l4_len = (u8)hdr_len[2];
797 			hdr_desc.hdr.flag = hdr_field << 1;
798 		}
799 		memcpy(data, cur, tmp);
800 		tmp_len -= tmp;
801 		*scrq_arr = hdr_desc;
802 		scrq_arr++;
803 	}
804 }
805 
806 /**
807  * build_hdr_descs_arr - build a header descriptor array
808  * @skb - socket buffer
809  * @num_entries - number of descriptors to be sent
810  * @subcrq - first TX descriptor
811  * @hdr_field - bit field determining which headers will be sent
812  *
813  * This function will build a TX descriptor array with applicable
814  * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
815  */
816 
817 static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
818 				int *num_entries, u8 hdr_field)
819 {
820 	int hdr_len[3] = {0, 0, 0};
821 	int tot_len, len;
822 	u8 *hdr_data = txbuff->hdr_data;
823 
824 	tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
825 				 txbuff->hdr_data);
826 	len = tot_len;
827 	len -= 24;
828 	if (len > 0)
829 		num_entries += len % 29 ? len / 29 + 1 : len / 29;
830 	create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
831 			 txbuff->indir_arr + 1);
832 }
833 
834 static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
835 {
836 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
837 	int queue_num = skb_get_queue_mapping(skb);
838 	u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
839 	struct device *dev = &adapter->vdev->dev;
840 	struct ibmvnic_tx_buff *tx_buff = NULL;
841 	struct ibmvnic_sub_crq_queue *tx_scrq;
842 	struct ibmvnic_tx_pool *tx_pool;
843 	unsigned int tx_send_failed = 0;
844 	unsigned int tx_map_failed = 0;
845 	unsigned int tx_dropped = 0;
846 	unsigned int tx_packets = 0;
847 	unsigned int tx_bytes = 0;
848 	dma_addr_t data_dma_addr;
849 	struct netdev_queue *txq;
850 	bool used_bounce = false;
851 	unsigned long lpar_rc;
852 	union sub_crq tx_crq;
853 	unsigned int offset;
854 	int num_entries = 1;
855 	unsigned char *dst;
856 	u64 *handle_array;
857 	int index = 0;
858 	int ret = 0;
859 
860 	tx_pool = &adapter->tx_pool[queue_num];
861 	tx_scrq = adapter->tx_scrq[queue_num];
862 	txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
863 	handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
864 				   be32_to_cpu(adapter->login_rsp_buf->
865 					       off_txsubm_subcrqs));
866 	if (adapter->migrated) {
867 		tx_send_failed++;
868 		tx_dropped++;
869 		ret = NETDEV_TX_BUSY;
870 		goto out;
871 	}
872 
873 	index = tx_pool->free_map[tx_pool->consumer_index];
874 	offset = index * adapter->req_mtu;
875 	dst = tx_pool->long_term_buff.buff + offset;
876 	memset(dst, 0, adapter->req_mtu);
877 	skb_copy_from_linear_data(skb, dst, skb->len);
878 	data_dma_addr = tx_pool->long_term_buff.addr + offset;
879 
880 	tx_pool->consumer_index =
881 	    (tx_pool->consumer_index + 1) %
882 		adapter->req_tx_entries_per_subcrq;
883 
884 	tx_buff = &tx_pool->tx_buff[index];
885 	tx_buff->skb = skb;
886 	tx_buff->data_dma[0] = data_dma_addr;
887 	tx_buff->data_len[0] = skb->len;
888 	tx_buff->index = index;
889 	tx_buff->pool_index = queue_num;
890 	tx_buff->last_frag = true;
891 	tx_buff->used_bounce = used_bounce;
892 
893 	memset(&tx_crq, 0, sizeof(tx_crq));
894 	tx_crq.v1.first = IBMVNIC_CRQ_CMD;
895 	tx_crq.v1.type = IBMVNIC_TX_DESC;
896 	tx_crq.v1.n_crq_elem = 1;
897 	tx_crq.v1.n_sge = 1;
898 	tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
899 	tx_crq.v1.correlator = cpu_to_be32(index);
900 	tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
901 	tx_crq.v1.sge_len = cpu_to_be32(skb->len);
902 	tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
903 
904 	if (adapter->vlan_header_insertion) {
905 		tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
906 		tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
907 	}
908 
909 	if (skb->protocol == htons(ETH_P_IP)) {
910 		if (ip_hdr(skb)->version == 4)
911 			tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
912 		else if (ip_hdr(skb)->version == 6)
913 			tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
914 
915 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
916 			tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
917 		else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
918 			tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
919 	}
920 
921 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
922 		tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
923 		hdrs += 2;
924 	}
925 	/* determine if l2/3/4 headers are sent to firmware */
926 	if ((*hdrs >> 7) & 1 &&
927 	    (skb->protocol == htons(ETH_P_IP) ||
928 	     skb->protocol == htons(ETH_P_IPV6))) {
929 		build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
930 		tx_crq.v1.n_crq_elem = num_entries;
931 		tx_buff->indir_arr[0] = tx_crq;
932 		tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
933 						    sizeof(tx_buff->indir_arr),
934 						    DMA_TO_DEVICE);
935 		if (dma_mapping_error(dev, tx_buff->indir_dma)) {
936 			if (!firmware_has_feature(FW_FEATURE_CMO))
937 				dev_err(dev, "tx: unable to map descriptor array\n");
938 			tx_map_failed++;
939 			tx_dropped++;
940 			ret = NETDEV_TX_BUSY;
941 			goto out;
942 		}
943 		lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
944 					       (u64)tx_buff->indir_dma,
945 					       (u64)num_entries);
946 	} else {
947 		lpar_rc = send_subcrq(adapter, handle_array[queue_num],
948 				      &tx_crq);
949 	}
950 	if (lpar_rc != H_SUCCESS) {
951 		dev_err(dev, "tx failed with code %ld\n", lpar_rc);
952 
953 		if (tx_pool->consumer_index == 0)
954 			tx_pool->consumer_index =
955 				adapter->req_tx_entries_per_subcrq - 1;
956 		else
957 			tx_pool->consumer_index--;
958 
959 		tx_send_failed++;
960 		tx_dropped++;
961 		ret = NETDEV_TX_BUSY;
962 		goto out;
963 	}
964 
965 	atomic_inc(&tx_scrq->used);
966 
967 	if (atomic_read(&tx_scrq->used) >= adapter->req_tx_entries_per_subcrq) {
968 		netdev_info(netdev, "Stopping queue %d\n", queue_num);
969 		netif_stop_subqueue(netdev, queue_num);
970 	}
971 
972 	tx_packets++;
973 	tx_bytes += skb->len;
974 	txq->trans_start = jiffies;
975 	ret = NETDEV_TX_OK;
976 
977 out:
978 	netdev->stats.tx_dropped += tx_dropped;
979 	netdev->stats.tx_bytes += tx_bytes;
980 	netdev->stats.tx_packets += tx_packets;
981 	adapter->tx_send_failed += tx_send_failed;
982 	adapter->tx_map_failed += tx_map_failed;
983 
984 	return ret;
985 }
986 
987 static void ibmvnic_set_multi(struct net_device *netdev)
988 {
989 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
990 	struct netdev_hw_addr *ha;
991 	union ibmvnic_crq crq;
992 
993 	memset(&crq, 0, sizeof(crq));
994 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
995 	crq.request_capability.cmd = REQUEST_CAPABILITY;
996 
997 	if (netdev->flags & IFF_PROMISC) {
998 		if (!adapter->promisc_supported)
999 			return;
1000 	} else {
1001 		if (netdev->flags & IFF_ALLMULTI) {
1002 			/* Accept all multicast */
1003 			memset(&crq, 0, sizeof(crq));
1004 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1005 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1006 			crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1007 			ibmvnic_send_crq(adapter, &crq);
1008 		} else if (netdev_mc_empty(netdev)) {
1009 			/* Reject all multicast */
1010 			memset(&crq, 0, sizeof(crq));
1011 			crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1012 			crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1013 			crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1014 			ibmvnic_send_crq(adapter, &crq);
1015 		} else {
1016 			/* Accept one or more multicast(s) */
1017 			netdev_for_each_mc_addr(ha, netdev) {
1018 				memset(&crq, 0, sizeof(crq));
1019 				crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1020 				crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1021 				crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1022 				ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1023 						ha->addr);
1024 				ibmvnic_send_crq(adapter, &crq);
1025 			}
1026 		}
1027 	}
1028 }
1029 
1030 static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1031 {
1032 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1033 	struct sockaddr *addr = p;
1034 	union ibmvnic_crq crq;
1035 
1036 	if (!is_valid_ether_addr(addr->sa_data))
1037 		return -EADDRNOTAVAIL;
1038 
1039 	memset(&crq, 0, sizeof(crq));
1040 	crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1041 	crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1042 	ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1043 	ibmvnic_send_crq(adapter, &crq);
1044 	/* netdev->dev_addr is changed in handle_change_mac_rsp function */
1045 	return 0;
1046 }
1047 
1048 static void ibmvnic_tx_timeout(struct net_device *dev)
1049 {
1050 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
1051 	int rc;
1052 
1053 	/* Adapter timed out, resetting it */
1054 	release_sub_crqs(adapter);
1055 	rc = ibmvnic_reset_crq(adapter);
1056 	if (rc)
1057 		dev_err(&adapter->vdev->dev, "Adapter timeout, reset failed\n");
1058 	else
1059 		ibmvnic_send_crq_init(adapter);
1060 }
1061 
1062 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1063 				  struct ibmvnic_rx_buff *rx_buff)
1064 {
1065 	struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1066 
1067 	rx_buff->skb = NULL;
1068 
1069 	pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
1070 	pool->next_alloc = (pool->next_alloc + 1) % pool->size;
1071 
1072 	atomic_dec(&pool->available);
1073 }
1074 
1075 static int ibmvnic_poll(struct napi_struct *napi, int budget)
1076 {
1077 	struct net_device *netdev = napi->dev;
1078 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1079 	int scrq_num = (int)(napi - adapter->napi);
1080 	int frames_processed = 0;
1081 restart_poll:
1082 	while (frames_processed < budget) {
1083 		struct sk_buff *skb;
1084 		struct ibmvnic_rx_buff *rx_buff;
1085 		union sub_crq *next;
1086 		u32 length;
1087 		u16 offset;
1088 		u8 flags = 0;
1089 
1090 		if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
1091 			break;
1092 		next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
1093 		rx_buff =
1094 		    (struct ibmvnic_rx_buff *)be64_to_cpu(next->
1095 							  rx_comp.correlator);
1096 		/* do error checking */
1097 		if (next->rx_comp.rc) {
1098 			netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
1099 			/* free the entry */
1100 			next->rx_comp.first = 0;
1101 			remove_buff_from_pool(adapter, rx_buff);
1102 			break;
1103 		}
1104 
1105 		length = be32_to_cpu(next->rx_comp.len);
1106 		offset = be16_to_cpu(next->rx_comp.off_frame_data);
1107 		flags = next->rx_comp.flags;
1108 		skb = rx_buff->skb;
1109 		skb_copy_to_linear_data(skb, rx_buff->data + offset,
1110 					length);
1111 		skb->vlan_tci = be16_to_cpu(next->rx_comp.vlan_tci);
1112 		/* free the entry */
1113 		next->rx_comp.first = 0;
1114 		remove_buff_from_pool(adapter, rx_buff);
1115 
1116 		skb_put(skb, length);
1117 		skb->protocol = eth_type_trans(skb, netdev);
1118 
1119 		if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
1120 		    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
1121 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1122 		}
1123 
1124 		length = skb->len;
1125 		napi_gro_receive(napi, skb); /* send it up */
1126 		netdev->stats.rx_packets++;
1127 		netdev->stats.rx_bytes += length;
1128 		frames_processed++;
1129 	}
1130 	replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
1131 
1132 	if (frames_processed < budget) {
1133 		enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1134 		napi_complete_done(napi, frames_processed);
1135 		if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
1136 		    napi_reschedule(napi)) {
1137 			disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
1138 			goto restart_poll;
1139 		}
1140 	}
1141 	return frames_processed;
1142 }
1143 
1144 #ifdef CONFIG_NET_POLL_CONTROLLER
1145 static void ibmvnic_netpoll_controller(struct net_device *dev)
1146 {
1147 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
1148 	int i;
1149 
1150 	replenish_pools(netdev_priv(dev));
1151 	for (i = 0; i < adapter->req_rx_queues; i++)
1152 		ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
1153 				     adapter->rx_scrq[i]);
1154 }
1155 #endif
1156 
1157 static const struct net_device_ops ibmvnic_netdev_ops = {
1158 	.ndo_open		= ibmvnic_open,
1159 	.ndo_stop		= ibmvnic_close,
1160 	.ndo_start_xmit		= ibmvnic_xmit,
1161 	.ndo_set_rx_mode	= ibmvnic_set_multi,
1162 	.ndo_set_mac_address	= ibmvnic_set_mac,
1163 	.ndo_validate_addr	= eth_validate_addr,
1164 	.ndo_tx_timeout		= ibmvnic_tx_timeout,
1165 #ifdef CONFIG_NET_POLL_CONTROLLER
1166 	.ndo_poll_controller	= ibmvnic_netpoll_controller,
1167 #endif
1168 };
1169 
1170 /* ethtool functions */
1171 
1172 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
1173 				      struct ethtool_link_ksettings *cmd)
1174 {
1175 	u32 supported, advertising;
1176 
1177 	supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
1178 			  SUPPORTED_FIBRE);
1179 	advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
1180 			    ADVERTISED_FIBRE);
1181 	cmd->base.speed = SPEED_1000;
1182 	cmd->base.duplex = DUPLEX_FULL;
1183 	cmd->base.port = PORT_FIBRE;
1184 	cmd->base.phy_address = 0;
1185 	cmd->base.autoneg = AUTONEG_ENABLE;
1186 
1187 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1188 						supported);
1189 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1190 						advertising);
1191 
1192 	return 0;
1193 }
1194 
1195 static void ibmvnic_get_drvinfo(struct net_device *dev,
1196 				struct ethtool_drvinfo *info)
1197 {
1198 	strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
1199 	strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
1200 }
1201 
1202 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
1203 {
1204 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1205 
1206 	return adapter->msg_enable;
1207 }
1208 
1209 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
1210 {
1211 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1212 
1213 	adapter->msg_enable = data;
1214 }
1215 
1216 static u32 ibmvnic_get_link(struct net_device *netdev)
1217 {
1218 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1219 
1220 	/* Don't need to send a query because we request a logical link up at
1221 	 * init and then we wait for link state indications
1222 	 */
1223 	return adapter->logical_link_state;
1224 }
1225 
1226 static void ibmvnic_get_ringparam(struct net_device *netdev,
1227 				  struct ethtool_ringparam *ring)
1228 {
1229 	ring->rx_max_pending = 0;
1230 	ring->tx_max_pending = 0;
1231 	ring->rx_mini_max_pending = 0;
1232 	ring->rx_jumbo_max_pending = 0;
1233 	ring->rx_pending = 0;
1234 	ring->tx_pending = 0;
1235 	ring->rx_mini_pending = 0;
1236 	ring->rx_jumbo_pending = 0;
1237 }
1238 
1239 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1240 {
1241 	int i;
1242 
1243 	if (stringset != ETH_SS_STATS)
1244 		return;
1245 
1246 	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
1247 		memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
1248 }
1249 
1250 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
1251 {
1252 	switch (sset) {
1253 	case ETH_SS_STATS:
1254 		return ARRAY_SIZE(ibmvnic_stats);
1255 	default:
1256 		return -EOPNOTSUPP;
1257 	}
1258 }
1259 
1260 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
1261 				      struct ethtool_stats *stats, u64 *data)
1262 {
1263 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
1264 	union ibmvnic_crq crq;
1265 	int i;
1266 
1267 	memset(&crq, 0, sizeof(crq));
1268 	crq.request_statistics.first = IBMVNIC_CRQ_CMD;
1269 	crq.request_statistics.cmd = REQUEST_STATISTICS;
1270 	crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
1271 	crq.request_statistics.len =
1272 	    cpu_to_be32(sizeof(struct ibmvnic_statistics));
1273 
1274 	/* Wait for data to be written */
1275 	init_completion(&adapter->stats_done);
1276 	ibmvnic_send_crq(adapter, &crq);
1277 	wait_for_completion(&adapter->stats_done);
1278 
1279 	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
1280 		data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
1281 }
1282 
1283 static const struct ethtool_ops ibmvnic_ethtool_ops = {
1284 	.get_drvinfo		= ibmvnic_get_drvinfo,
1285 	.get_msglevel		= ibmvnic_get_msglevel,
1286 	.set_msglevel		= ibmvnic_set_msglevel,
1287 	.get_link		= ibmvnic_get_link,
1288 	.get_ringparam		= ibmvnic_get_ringparam,
1289 	.get_strings            = ibmvnic_get_strings,
1290 	.get_sset_count         = ibmvnic_get_sset_count,
1291 	.get_ethtool_stats	= ibmvnic_get_ethtool_stats,
1292 	.get_link_ksettings	= ibmvnic_get_link_ksettings,
1293 };
1294 
1295 /* Routines for managing CRQs/sCRQs  */
1296 
1297 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
1298 				  struct ibmvnic_sub_crq_queue *scrq)
1299 {
1300 	struct device *dev = &adapter->vdev->dev;
1301 	long rc;
1302 
1303 	netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
1304 
1305 	/* Close the sub-crqs */
1306 	do {
1307 		rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
1308 					adapter->vdev->unit_address,
1309 					scrq->crq_num);
1310 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
1311 
1312 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1313 			 DMA_BIDIRECTIONAL);
1314 	free_pages((unsigned long)scrq->msgs, 2);
1315 	kfree(scrq);
1316 }
1317 
1318 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
1319 							*adapter)
1320 {
1321 	struct device *dev = &adapter->vdev->dev;
1322 	struct ibmvnic_sub_crq_queue *scrq;
1323 	int rc;
1324 
1325 	scrq = kmalloc(sizeof(*scrq), GFP_ATOMIC);
1326 	if (!scrq)
1327 		return NULL;
1328 
1329 	scrq->msgs = (union sub_crq *)__get_free_pages(GFP_ATOMIC, 2);
1330 	memset(scrq->msgs, 0, 4 * PAGE_SIZE);
1331 	if (!scrq->msgs) {
1332 		dev_warn(dev, "Couldn't allocate crq queue messages page\n");
1333 		goto zero_page_failed;
1334 	}
1335 
1336 	scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
1337 					 DMA_BIDIRECTIONAL);
1338 	if (dma_mapping_error(dev, scrq->msg_token)) {
1339 		dev_warn(dev, "Couldn't map crq queue messages page\n");
1340 		goto map_failed;
1341 	}
1342 
1343 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
1344 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
1345 
1346 	if (rc == H_RESOURCE)
1347 		rc = ibmvnic_reset_crq(adapter);
1348 
1349 	if (rc == H_CLOSED) {
1350 		dev_warn(dev, "Partner adapter not ready, waiting.\n");
1351 	} else if (rc) {
1352 		dev_warn(dev, "Error %d registering sub-crq\n", rc);
1353 		goto reg_failed;
1354 	}
1355 
1356 	scrq->adapter = adapter;
1357 	scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
1358 	scrq->cur = 0;
1359 	atomic_set(&scrq->used, 0);
1360 	scrq->rx_skb_top = NULL;
1361 	spin_lock_init(&scrq->lock);
1362 
1363 	netdev_dbg(adapter->netdev,
1364 		   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
1365 		   scrq->crq_num, scrq->hw_irq, scrq->irq);
1366 
1367 	return scrq;
1368 
1369 reg_failed:
1370 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
1371 			 DMA_BIDIRECTIONAL);
1372 map_failed:
1373 	free_pages((unsigned long)scrq->msgs, 2);
1374 zero_page_failed:
1375 	kfree(scrq);
1376 
1377 	return NULL;
1378 }
1379 
1380 static void release_sub_crqs(struct ibmvnic_adapter *adapter)
1381 {
1382 	int i;
1383 
1384 	if (adapter->tx_scrq) {
1385 		for (i = 0; i < adapter->req_tx_queues; i++) {
1386 			if (!adapter->tx_scrq[i])
1387 				continue;
1388 
1389 			if (adapter->tx_scrq[i]->irq) {
1390 				free_irq(adapter->tx_scrq[i]->irq,
1391 					 adapter->tx_scrq[i]);
1392 				irq_dispose_mapping(adapter->tx_scrq[i]->irq);
1393 				adapter->tx_scrq[i]->irq = 0;
1394 			}
1395 
1396 			release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
1397 		}
1398 
1399 		kfree(adapter->tx_scrq);
1400 		adapter->tx_scrq = NULL;
1401 	}
1402 
1403 	if (adapter->rx_scrq) {
1404 		for (i = 0; i < adapter->req_rx_queues; i++) {
1405 			if (!adapter->rx_scrq[i])
1406 				continue;
1407 
1408 			if (adapter->rx_scrq[i]->irq) {
1409 				free_irq(adapter->rx_scrq[i]->irq,
1410 					 adapter->rx_scrq[i]);
1411 				irq_dispose_mapping(adapter->rx_scrq[i]->irq);
1412 				adapter->rx_scrq[i]->irq = 0;
1413 			}
1414 
1415 			release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
1416 		}
1417 
1418 		kfree(adapter->rx_scrq);
1419 		adapter->rx_scrq = NULL;
1420 	}
1421 }
1422 
1423 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
1424 			    struct ibmvnic_sub_crq_queue *scrq)
1425 {
1426 	struct device *dev = &adapter->vdev->dev;
1427 	unsigned long rc;
1428 
1429 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1430 				H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1431 	if (rc)
1432 		dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
1433 			scrq->hw_irq, rc);
1434 	return rc;
1435 }
1436 
1437 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
1438 			   struct ibmvnic_sub_crq_queue *scrq)
1439 {
1440 	struct device *dev = &adapter->vdev->dev;
1441 	unsigned long rc;
1442 
1443 	if (scrq->hw_irq > 0x100000000ULL) {
1444 		dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
1445 		return 1;
1446 	}
1447 
1448 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
1449 				H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
1450 	if (rc)
1451 		dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
1452 			scrq->hw_irq, rc);
1453 	return rc;
1454 }
1455 
1456 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
1457 			       struct ibmvnic_sub_crq_queue *scrq)
1458 {
1459 	struct device *dev = &adapter->vdev->dev;
1460 	struct ibmvnic_tx_buff *txbuff;
1461 	union sub_crq *next;
1462 	int index;
1463 	int i, j;
1464 	u8 first;
1465 
1466 restart_loop:
1467 	while (pending_scrq(adapter, scrq)) {
1468 		unsigned int pool = scrq->pool_index;
1469 
1470 		next = ibmvnic_next_scrq(adapter, scrq);
1471 		for (i = 0; i < next->tx_comp.num_comps; i++) {
1472 			if (next->tx_comp.rcs[i]) {
1473 				dev_err(dev, "tx error %x\n",
1474 					next->tx_comp.rcs[i]);
1475 				continue;
1476 			}
1477 			index = be32_to_cpu(next->tx_comp.correlators[i]);
1478 			txbuff = &adapter->tx_pool[pool].tx_buff[index];
1479 
1480 			for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
1481 				if (!txbuff->data_dma[j])
1482 					continue;
1483 
1484 				txbuff->data_dma[j] = 0;
1485 				txbuff->used_bounce = false;
1486 			}
1487 			/* if sub_crq was sent indirectly */
1488 			first = txbuff->indir_arr[0].generic.first;
1489 			if (first == IBMVNIC_CRQ_CMD) {
1490 				dma_unmap_single(dev, txbuff->indir_dma,
1491 						 sizeof(txbuff->indir_arr),
1492 						 DMA_TO_DEVICE);
1493 			}
1494 
1495 			if (txbuff->last_frag) {
1496 				atomic_dec(&scrq->used);
1497 
1498 				if (atomic_read(&scrq->used) <=
1499 				    (adapter->req_tx_entries_per_subcrq / 2) &&
1500 				    netif_subqueue_stopped(adapter->netdev,
1501 							   txbuff->skb)) {
1502 					netif_wake_subqueue(adapter->netdev,
1503 							    scrq->pool_index);
1504 					netdev_dbg(adapter->netdev,
1505 						   "Started queue %d\n",
1506 						   scrq->pool_index);
1507 				}
1508 
1509 				dev_kfree_skb_any(txbuff->skb);
1510 			}
1511 
1512 			adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
1513 						     producer_index] = index;
1514 			adapter->tx_pool[pool].producer_index =
1515 			    (adapter->tx_pool[pool].producer_index + 1) %
1516 			    adapter->req_tx_entries_per_subcrq;
1517 		}
1518 		/* remove tx_comp scrq*/
1519 		next->tx_comp.first = 0;
1520 	}
1521 
1522 	enable_scrq_irq(adapter, scrq);
1523 
1524 	if (pending_scrq(adapter, scrq)) {
1525 		disable_scrq_irq(adapter, scrq);
1526 		goto restart_loop;
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
1533 {
1534 	struct ibmvnic_sub_crq_queue *scrq = instance;
1535 	struct ibmvnic_adapter *adapter = scrq->adapter;
1536 
1537 	disable_scrq_irq(adapter, scrq);
1538 	ibmvnic_complete_tx(adapter, scrq);
1539 
1540 	return IRQ_HANDLED;
1541 }
1542 
1543 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
1544 {
1545 	struct ibmvnic_sub_crq_queue *scrq = instance;
1546 	struct ibmvnic_adapter *adapter = scrq->adapter;
1547 
1548 	if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
1549 		disable_scrq_irq(adapter, scrq);
1550 		__napi_schedule(&adapter->napi[scrq->scrq_num]);
1551 	}
1552 
1553 	return IRQ_HANDLED;
1554 }
1555 
1556 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
1557 {
1558 	struct device *dev = &adapter->vdev->dev;
1559 	struct ibmvnic_sub_crq_queue *scrq;
1560 	int i = 0, j = 0;
1561 	int rc = 0;
1562 
1563 	for (i = 0; i < adapter->req_tx_queues; i++) {
1564 		scrq = adapter->tx_scrq[i];
1565 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1566 
1567 		if (!scrq->irq) {
1568 			rc = -EINVAL;
1569 			dev_err(dev, "Error mapping irq\n");
1570 			goto req_tx_irq_failed;
1571 		}
1572 
1573 		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
1574 				 0, "ibmvnic_tx", scrq);
1575 
1576 		if (rc) {
1577 			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
1578 				scrq->irq, rc);
1579 			irq_dispose_mapping(scrq->irq);
1580 			goto req_rx_irq_failed;
1581 		}
1582 	}
1583 
1584 	for (i = 0; i < adapter->req_rx_queues; i++) {
1585 		scrq = adapter->rx_scrq[i];
1586 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
1587 		if (!scrq->irq) {
1588 			rc = -EINVAL;
1589 			dev_err(dev, "Error mapping irq\n");
1590 			goto req_rx_irq_failed;
1591 		}
1592 		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
1593 				 0, "ibmvnic_rx", scrq);
1594 		if (rc) {
1595 			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
1596 				scrq->irq, rc);
1597 			irq_dispose_mapping(scrq->irq);
1598 			goto req_rx_irq_failed;
1599 		}
1600 	}
1601 	return rc;
1602 
1603 req_rx_irq_failed:
1604 	for (j = 0; j < i; j++) {
1605 		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
1606 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
1607 	}
1608 	i = adapter->req_tx_queues;
1609 req_tx_irq_failed:
1610 	for (j = 0; j < i; j++) {
1611 		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
1612 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
1613 	}
1614 	release_sub_crqs(adapter);
1615 	return rc;
1616 }
1617 
1618 static void init_sub_crqs(struct ibmvnic_adapter *adapter, int retry)
1619 {
1620 	struct device *dev = &adapter->vdev->dev;
1621 	struct ibmvnic_sub_crq_queue **allqueues;
1622 	int registered_queues = 0;
1623 	union ibmvnic_crq crq;
1624 	int total_queues;
1625 	int more = 0;
1626 	int i;
1627 
1628 	if (!retry) {
1629 		/* Sub-CRQ entries are 32 byte long */
1630 		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
1631 
1632 		if (adapter->min_tx_entries_per_subcrq > entries_page ||
1633 		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
1634 			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
1635 			goto allqueues_failed;
1636 		}
1637 
1638 		/* Get the minimum between the queried max and the entries
1639 		 * that fit in our PAGE_SIZE
1640 		 */
1641 		adapter->req_tx_entries_per_subcrq =
1642 		    adapter->max_tx_entries_per_subcrq > entries_page ?
1643 		    entries_page : adapter->max_tx_entries_per_subcrq;
1644 		adapter->req_rx_add_entries_per_subcrq =
1645 		    adapter->max_rx_add_entries_per_subcrq > entries_page ?
1646 		    entries_page : adapter->max_rx_add_entries_per_subcrq;
1647 
1648 		adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
1649 		adapter->req_rx_queues = adapter->opt_rx_comp_queues;
1650 		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
1651 
1652 		adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
1653 	}
1654 
1655 	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
1656 
1657 	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_ATOMIC);
1658 	if (!allqueues)
1659 		goto allqueues_failed;
1660 
1661 	for (i = 0; i < total_queues; i++) {
1662 		allqueues[i] = init_sub_crq_queue(adapter);
1663 		if (!allqueues[i]) {
1664 			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
1665 			break;
1666 		}
1667 		registered_queues++;
1668 	}
1669 
1670 	/* Make sure we were able to register the minimum number of queues */
1671 	if (registered_queues <
1672 	    adapter->min_tx_queues + adapter->min_rx_queues) {
1673 		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
1674 		goto tx_failed;
1675 	}
1676 
1677 	/* Distribute the failed allocated queues*/
1678 	for (i = 0; i < total_queues - registered_queues + more ; i++) {
1679 		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
1680 		switch (i % 3) {
1681 		case 0:
1682 			if (adapter->req_rx_queues > adapter->min_rx_queues)
1683 				adapter->req_rx_queues--;
1684 			else
1685 				more++;
1686 			break;
1687 		case 1:
1688 			if (adapter->req_tx_queues > adapter->min_tx_queues)
1689 				adapter->req_tx_queues--;
1690 			else
1691 				more++;
1692 			break;
1693 		}
1694 	}
1695 
1696 	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
1697 				   sizeof(*adapter->tx_scrq), GFP_ATOMIC);
1698 	if (!adapter->tx_scrq)
1699 		goto tx_failed;
1700 
1701 	for (i = 0; i < adapter->req_tx_queues; i++) {
1702 		adapter->tx_scrq[i] = allqueues[i];
1703 		adapter->tx_scrq[i]->pool_index = i;
1704 	}
1705 
1706 	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
1707 				   sizeof(*adapter->rx_scrq), GFP_ATOMIC);
1708 	if (!adapter->rx_scrq)
1709 		goto rx_failed;
1710 
1711 	for (i = 0; i < adapter->req_rx_queues; i++) {
1712 		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
1713 		adapter->rx_scrq[i]->scrq_num = i;
1714 	}
1715 
1716 	memset(&crq, 0, sizeof(crq));
1717 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
1718 	crq.request_capability.cmd = REQUEST_CAPABILITY;
1719 
1720 	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
1721 	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
1722 	atomic_inc(&adapter->running_cap_crqs);
1723 	ibmvnic_send_crq(adapter, &crq);
1724 
1725 	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
1726 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
1727 	atomic_inc(&adapter->running_cap_crqs);
1728 	ibmvnic_send_crq(adapter, &crq);
1729 
1730 	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
1731 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
1732 	atomic_inc(&adapter->running_cap_crqs);
1733 	ibmvnic_send_crq(adapter, &crq);
1734 
1735 	crq.request_capability.capability =
1736 	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
1737 	crq.request_capability.number =
1738 	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
1739 	atomic_inc(&adapter->running_cap_crqs);
1740 	ibmvnic_send_crq(adapter, &crq);
1741 
1742 	crq.request_capability.capability =
1743 	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
1744 	crq.request_capability.number =
1745 	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
1746 	atomic_inc(&adapter->running_cap_crqs);
1747 	ibmvnic_send_crq(adapter, &crq);
1748 
1749 	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
1750 	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
1751 	atomic_inc(&adapter->running_cap_crqs);
1752 	ibmvnic_send_crq(adapter, &crq);
1753 
1754 	if (adapter->netdev->flags & IFF_PROMISC) {
1755 		if (adapter->promisc_supported) {
1756 			crq.request_capability.capability =
1757 			    cpu_to_be16(PROMISC_REQUESTED);
1758 			crq.request_capability.number = cpu_to_be64(1);
1759 			atomic_inc(&adapter->running_cap_crqs);
1760 			ibmvnic_send_crq(adapter, &crq);
1761 		}
1762 	} else {
1763 		crq.request_capability.capability =
1764 		    cpu_to_be16(PROMISC_REQUESTED);
1765 		crq.request_capability.number = cpu_to_be64(0);
1766 		atomic_inc(&adapter->running_cap_crqs);
1767 		ibmvnic_send_crq(adapter, &crq);
1768 	}
1769 
1770 	kfree(allqueues);
1771 
1772 	return;
1773 
1774 rx_failed:
1775 	kfree(adapter->tx_scrq);
1776 	adapter->tx_scrq = NULL;
1777 tx_failed:
1778 	for (i = 0; i < registered_queues; i++)
1779 		release_sub_crq_queue(adapter, allqueues[i]);
1780 	kfree(allqueues);
1781 allqueues_failed:
1782 	ibmvnic_remove(adapter->vdev);
1783 }
1784 
1785 static int pending_scrq(struct ibmvnic_adapter *adapter,
1786 			struct ibmvnic_sub_crq_queue *scrq)
1787 {
1788 	union sub_crq *entry = &scrq->msgs[scrq->cur];
1789 
1790 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP || adapter->closing)
1791 		return 1;
1792 	else
1793 		return 0;
1794 }
1795 
1796 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
1797 					struct ibmvnic_sub_crq_queue *scrq)
1798 {
1799 	union sub_crq *entry;
1800 	unsigned long flags;
1801 
1802 	spin_lock_irqsave(&scrq->lock, flags);
1803 	entry = &scrq->msgs[scrq->cur];
1804 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1805 		if (++scrq->cur == scrq->size)
1806 			scrq->cur = 0;
1807 	} else {
1808 		entry = NULL;
1809 	}
1810 	spin_unlock_irqrestore(&scrq->lock, flags);
1811 
1812 	return entry;
1813 }
1814 
1815 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
1816 {
1817 	struct ibmvnic_crq_queue *queue = &adapter->crq;
1818 	union ibmvnic_crq *crq;
1819 
1820 	crq = &queue->msgs[queue->cur];
1821 	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
1822 		if (++queue->cur == queue->size)
1823 			queue->cur = 0;
1824 	} else {
1825 		crq = NULL;
1826 	}
1827 
1828 	return crq;
1829 }
1830 
1831 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
1832 		       union sub_crq *sub_crq)
1833 {
1834 	unsigned int ua = adapter->vdev->unit_address;
1835 	struct device *dev = &adapter->vdev->dev;
1836 	u64 *u64_crq = (u64 *)sub_crq;
1837 	int rc;
1838 
1839 	netdev_dbg(adapter->netdev,
1840 		   "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
1841 		   (unsigned long int)cpu_to_be64(remote_handle),
1842 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
1843 		   (unsigned long int)cpu_to_be64(u64_crq[1]),
1844 		   (unsigned long int)cpu_to_be64(u64_crq[2]),
1845 		   (unsigned long int)cpu_to_be64(u64_crq[3]));
1846 
1847 	/* Make sure the hypervisor sees the complete request */
1848 	mb();
1849 
1850 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
1851 				cpu_to_be64(remote_handle),
1852 				cpu_to_be64(u64_crq[0]),
1853 				cpu_to_be64(u64_crq[1]),
1854 				cpu_to_be64(u64_crq[2]),
1855 				cpu_to_be64(u64_crq[3]));
1856 
1857 	if (rc) {
1858 		if (rc == H_CLOSED)
1859 			dev_warn(dev, "CRQ Queue closed\n");
1860 		dev_err(dev, "Send error (rc=%d)\n", rc);
1861 	}
1862 
1863 	return rc;
1864 }
1865 
1866 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
1867 				u64 remote_handle, u64 ioba, u64 num_entries)
1868 {
1869 	unsigned int ua = adapter->vdev->unit_address;
1870 	struct device *dev = &adapter->vdev->dev;
1871 	int rc;
1872 
1873 	/* Make sure the hypervisor sees the complete request */
1874 	mb();
1875 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
1876 				cpu_to_be64(remote_handle),
1877 				ioba, num_entries);
1878 
1879 	if (rc) {
1880 		if (rc == H_CLOSED)
1881 			dev_warn(dev, "CRQ Queue closed\n");
1882 		dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
1883 	}
1884 
1885 	return rc;
1886 }
1887 
1888 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
1889 			    union ibmvnic_crq *crq)
1890 {
1891 	unsigned int ua = adapter->vdev->unit_address;
1892 	struct device *dev = &adapter->vdev->dev;
1893 	u64 *u64_crq = (u64 *)crq;
1894 	int rc;
1895 
1896 	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
1897 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
1898 		   (unsigned long int)cpu_to_be64(u64_crq[1]));
1899 
1900 	/* Make sure the hypervisor sees the complete request */
1901 	mb();
1902 
1903 	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
1904 				cpu_to_be64(u64_crq[0]),
1905 				cpu_to_be64(u64_crq[1]));
1906 
1907 	if (rc) {
1908 		if (rc == H_CLOSED)
1909 			dev_warn(dev, "CRQ Queue closed\n");
1910 		dev_warn(dev, "Send error (rc=%d)\n", rc);
1911 	}
1912 
1913 	return rc;
1914 }
1915 
1916 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
1917 {
1918 	union ibmvnic_crq crq;
1919 
1920 	memset(&crq, 0, sizeof(crq));
1921 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1922 	crq.generic.cmd = IBMVNIC_CRQ_INIT;
1923 	netdev_dbg(adapter->netdev, "Sending CRQ init\n");
1924 
1925 	return ibmvnic_send_crq(adapter, &crq);
1926 }
1927 
1928 static int ibmvnic_send_crq_init_complete(struct ibmvnic_adapter *adapter)
1929 {
1930 	union ibmvnic_crq crq;
1931 
1932 	memset(&crq, 0, sizeof(crq));
1933 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
1934 	crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
1935 	netdev_dbg(adapter->netdev, "Sending CRQ init complete\n");
1936 
1937 	return ibmvnic_send_crq(adapter, &crq);
1938 }
1939 
1940 static int send_version_xchg(struct ibmvnic_adapter *adapter)
1941 {
1942 	union ibmvnic_crq crq;
1943 
1944 	memset(&crq, 0, sizeof(crq));
1945 	crq.version_exchange.first = IBMVNIC_CRQ_CMD;
1946 	crq.version_exchange.cmd = VERSION_EXCHANGE;
1947 	crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
1948 
1949 	return ibmvnic_send_crq(adapter, &crq);
1950 }
1951 
1952 static void send_login(struct ibmvnic_adapter *adapter)
1953 {
1954 	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
1955 	struct ibmvnic_login_buffer *login_buffer;
1956 	struct ibmvnic_inflight_cmd *inflight_cmd;
1957 	struct device *dev = &adapter->vdev->dev;
1958 	dma_addr_t rsp_buffer_token;
1959 	dma_addr_t buffer_token;
1960 	size_t rsp_buffer_size;
1961 	union ibmvnic_crq crq;
1962 	unsigned long flags;
1963 	size_t buffer_size;
1964 	__be64 *tx_list_p;
1965 	__be64 *rx_list_p;
1966 	int i;
1967 
1968 	buffer_size =
1969 	    sizeof(struct ibmvnic_login_buffer) +
1970 	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
1971 
1972 	login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
1973 	if (!login_buffer)
1974 		goto buf_alloc_failed;
1975 
1976 	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
1977 				      DMA_TO_DEVICE);
1978 	if (dma_mapping_error(dev, buffer_token)) {
1979 		dev_err(dev, "Couldn't map login buffer\n");
1980 		goto buf_map_failed;
1981 	}
1982 
1983 	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
1984 			  sizeof(u64) * adapter->req_tx_queues +
1985 			  sizeof(u64) * adapter->req_rx_queues +
1986 			  sizeof(u64) * adapter->req_rx_queues +
1987 			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
1988 
1989 	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
1990 	if (!login_rsp_buffer)
1991 		goto buf_rsp_alloc_failed;
1992 
1993 	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
1994 					  rsp_buffer_size, DMA_FROM_DEVICE);
1995 	if (dma_mapping_error(dev, rsp_buffer_token)) {
1996 		dev_err(dev, "Couldn't map login rsp buffer\n");
1997 		goto buf_rsp_map_failed;
1998 	}
1999 	inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2000 	if (!inflight_cmd) {
2001 		dev_err(dev, "Couldn't allocate inflight_cmd\n");
2002 		goto inflight_alloc_failed;
2003 	}
2004 	adapter->login_buf = login_buffer;
2005 	adapter->login_buf_token = buffer_token;
2006 	adapter->login_buf_sz = buffer_size;
2007 	adapter->login_rsp_buf = login_rsp_buffer;
2008 	adapter->login_rsp_buf_token = rsp_buffer_token;
2009 	adapter->login_rsp_buf_sz = rsp_buffer_size;
2010 
2011 	login_buffer->len = cpu_to_be32(buffer_size);
2012 	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
2013 	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
2014 	login_buffer->off_txcomp_subcrqs =
2015 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
2016 	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
2017 	login_buffer->off_rxcomp_subcrqs =
2018 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
2019 			sizeof(u64) * adapter->req_tx_queues);
2020 	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
2021 	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
2022 
2023 	tx_list_p = (__be64 *)((char *)login_buffer +
2024 				      sizeof(struct ibmvnic_login_buffer));
2025 	rx_list_p = (__be64 *)((char *)login_buffer +
2026 				      sizeof(struct ibmvnic_login_buffer) +
2027 				      sizeof(u64) * adapter->req_tx_queues);
2028 
2029 	for (i = 0; i < adapter->req_tx_queues; i++) {
2030 		if (adapter->tx_scrq[i]) {
2031 			tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
2032 						   crq_num);
2033 		}
2034 	}
2035 
2036 	for (i = 0; i < adapter->req_rx_queues; i++) {
2037 		if (adapter->rx_scrq[i]) {
2038 			rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
2039 						   crq_num);
2040 		}
2041 	}
2042 
2043 	netdev_dbg(adapter->netdev, "Login Buffer:\n");
2044 	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
2045 		netdev_dbg(adapter->netdev, "%016lx\n",
2046 			   ((unsigned long int *)(adapter->login_buf))[i]);
2047 	}
2048 
2049 	memset(&crq, 0, sizeof(crq));
2050 	crq.login.first = IBMVNIC_CRQ_CMD;
2051 	crq.login.cmd = LOGIN;
2052 	crq.login.ioba = cpu_to_be32(buffer_token);
2053 	crq.login.len = cpu_to_be32(buffer_size);
2054 
2055 	memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2056 
2057 	spin_lock_irqsave(&adapter->inflight_lock, flags);
2058 	list_add_tail(&inflight_cmd->list, &adapter->inflight);
2059 	spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2060 
2061 	ibmvnic_send_crq(adapter, &crq);
2062 
2063 	return;
2064 
2065 inflight_alloc_failed:
2066 	dma_unmap_single(dev, rsp_buffer_token, rsp_buffer_size,
2067 			 DMA_FROM_DEVICE);
2068 buf_rsp_map_failed:
2069 	kfree(login_rsp_buffer);
2070 buf_rsp_alloc_failed:
2071 	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
2072 buf_map_failed:
2073 	kfree(login_buffer);
2074 buf_alloc_failed:
2075 	return;
2076 }
2077 
2078 static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
2079 			     u32 len, u8 map_id)
2080 {
2081 	union ibmvnic_crq crq;
2082 
2083 	memset(&crq, 0, sizeof(crq));
2084 	crq.request_map.first = IBMVNIC_CRQ_CMD;
2085 	crq.request_map.cmd = REQUEST_MAP;
2086 	crq.request_map.map_id = map_id;
2087 	crq.request_map.ioba = cpu_to_be32(addr);
2088 	crq.request_map.len = cpu_to_be32(len);
2089 	ibmvnic_send_crq(adapter, &crq);
2090 }
2091 
2092 static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
2093 {
2094 	union ibmvnic_crq crq;
2095 
2096 	memset(&crq, 0, sizeof(crq));
2097 	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
2098 	crq.request_unmap.cmd = REQUEST_UNMAP;
2099 	crq.request_unmap.map_id = map_id;
2100 	ibmvnic_send_crq(adapter, &crq);
2101 }
2102 
2103 static void send_map_query(struct ibmvnic_adapter *adapter)
2104 {
2105 	union ibmvnic_crq crq;
2106 
2107 	memset(&crq, 0, sizeof(crq));
2108 	crq.query_map.first = IBMVNIC_CRQ_CMD;
2109 	crq.query_map.cmd = QUERY_MAP;
2110 	ibmvnic_send_crq(adapter, &crq);
2111 }
2112 
2113 /* Send a series of CRQs requesting various capabilities of the VNIC server */
2114 static void send_cap_queries(struct ibmvnic_adapter *adapter)
2115 {
2116 	union ibmvnic_crq crq;
2117 
2118 	atomic_set(&adapter->running_cap_crqs, 0);
2119 	memset(&crq, 0, sizeof(crq));
2120 	crq.query_capability.first = IBMVNIC_CRQ_CMD;
2121 	crq.query_capability.cmd = QUERY_CAPABILITY;
2122 
2123 	crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
2124 	atomic_inc(&adapter->running_cap_crqs);
2125 	ibmvnic_send_crq(adapter, &crq);
2126 
2127 	crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
2128 	atomic_inc(&adapter->running_cap_crqs);
2129 	ibmvnic_send_crq(adapter, &crq);
2130 
2131 	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
2132 	atomic_inc(&adapter->running_cap_crqs);
2133 	ibmvnic_send_crq(adapter, &crq);
2134 
2135 	crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
2136 	atomic_inc(&adapter->running_cap_crqs);
2137 	ibmvnic_send_crq(adapter, &crq);
2138 
2139 	crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
2140 	atomic_inc(&adapter->running_cap_crqs);
2141 	ibmvnic_send_crq(adapter, &crq);
2142 
2143 	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
2144 	atomic_inc(&adapter->running_cap_crqs);
2145 	ibmvnic_send_crq(adapter, &crq);
2146 
2147 	crq.query_capability.capability =
2148 	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
2149 	atomic_inc(&adapter->running_cap_crqs);
2150 	ibmvnic_send_crq(adapter, &crq);
2151 
2152 	crq.query_capability.capability =
2153 	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
2154 	atomic_inc(&adapter->running_cap_crqs);
2155 	ibmvnic_send_crq(adapter, &crq);
2156 
2157 	crq.query_capability.capability =
2158 	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
2159 	atomic_inc(&adapter->running_cap_crqs);
2160 	ibmvnic_send_crq(adapter, &crq);
2161 
2162 	crq.query_capability.capability =
2163 	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
2164 	atomic_inc(&adapter->running_cap_crqs);
2165 	ibmvnic_send_crq(adapter, &crq);
2166 
2167 	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
2168 	atomic_inc(&adapter->running_cap_crqs);
2169 	ibmvnic_send_crq(adapter, &crq);
2170 
2171 	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
2172 	atomic_inc(&adapter->running_cap_crqs);
2173 	ibmvnic_send_crq(adapter, &crq);
2174 
2175 	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
2176 	atomic_inc(&adapter->running_cap_crqs);
2177 	ibmvnic_send_crq(adapter, &crq);
2178 
2179 	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
2180 	atomic_inc(&adapter->running_cap_crqs);
2181 	ibmvnic_send_crq(adapter, &crq);
2182 
2183 	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
2184 	atomic_inc(&adapter->running_cap_crqs);
2185 	ibmvnic_send_crq(adapter, &crq);
2186 
2187 	crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
2188 	atomic_inc(&adapter->running_cap_crqs);
2189 	ibmvnic_send_crq(adapter, &crq);
2190 
2191 	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
2192 	atomic_inc(&adapter->running_cap_crqs);
2193 	ibmvnic_send_crq(adapter, &crq);
2194 
2195 	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
2196 	atomic_inc(&adapter->running_cap_crqs);
2197 	ibmvnic_send_crq(adapter, &crq);
2198 
2199 	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
2200 	atomic_inc(&adapter->running_cap_crqs);
2201 	ibmvnic_send_crq(adapter, &crq);
2202 
2203 	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
2204 	atomic_inc(&adapter->running_cap_crqs);
2205 	ibmvnic_send_crq(adapter, &crq);
2206 
2207 	crq.query_capability.capability =
2208 			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
2209 	atomic_inc(&adapter->running_cap_crqs);
2210 	ibmvnic_send_crq(adapter, &crq);
2211 
2212 	crq.query_capability.capability =
2213 			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
2214 	atomic_inc(&adapter->running_cap_crqs);
2215 	ibmvnic_send_crq(adapter, &crq);
2216 
2217 	crq.query_capability.capability =
2218 			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
2219 	atomic_inc(&adapter->running_cap_crqs);
2220 	ibmvnic_send_crq(adapter, &crq);
2221 
2222 	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
2223 	atomic_inc(&adapter->running_cap_crqs);
2224 	ibmvnic_send_crq(adapter, &crq);
2225 }
2226 
2227 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
2228 {
2229 	struct device *dev = &adapter->vdev->dev;
2230 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
2231 	union ibmvnic_crq crq;
2232 	int i;
2233 
2234 	dma_unmap_single(dev, adapter->ip_offload_tok,
2235 			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
2236 
2237 	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
2238 	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
2239 		netdev_dbg(adapter->netdev, "%016lx\n",
2240 			   ((unsigned long int *)(buf))[i]);
2241 
2242 	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
2243 	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
2244 	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
2245 		   buf->tcp_ipv4_chksum);
2246 	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
2247 		   buf->tcp_ipv6_chksum);
2248 	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
2249 		   buf->udp_ipv4_chksum);
2250 	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
2251 		   buf->udp_ipv6_chksum);
2252 	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
2253 		   buf->large_tx_ipv4);
2254 	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
2255 		   buf->large_tx_ipv6);
2256 	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
2257 		   buf->large_rx_ipv4);
2258 	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
2259 		   buf->large_rx_ipv6);
2260 	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
2261 		   buf->max_ipv4_header_size);
2262 	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
2263 		   buf->max_ipv6_header_size);
2264 	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
2265 		   buf->max_tcp_header_size);
2266 	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
2267 		   buf->max_udp_header_size);
2268 	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
2269 		   buf->max_large_tx_size);
2270 	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
2271 		   buf->max_large_rx_size);
2272 	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
2273 		   buf->ipv6_extension_header);
2274 	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
2275 		   buf->tcp_pseudosum_req);
2276 	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
2277 		   buf->num_ipv6_ext_headers);
2278 	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
2279 		   buf->off_ipv6_ext_headers);
2280 
2281 	adapter->ip_offload_ctrl_tok =
2282 	    dma_map_single(dev, &adapter->ip_offload_ctrl,
2283 			   sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
2284 
2285 	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
2286 		dev_err(dev, "Couldn't map ip offload control buffer\n");
2287 		return;
2288 	}
2289 
2290 	adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
2291 	adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
2292 	adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
2293 	adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
2294 	adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
2295 
2296 	/* large_tx/rx disabled for now, additional features needed */
2297 	adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
2298 	adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
2299 	adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
2300 	adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
2301 
2302 	adapter->netdev->features = NETIF_F_GSO;
2303 
2304 	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
2305 		adapter->netdev->features |= NETIF_F_IP_CSUM;
2306 
2307 	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
2308 		adapter->netdev->features |= NETIF_F_IPV6_CSUM;
2309 
2310 	if ((adapter->netdev->features &
2311 	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
2312 		adapter->netdev->features |= NETIF_F_RXCSUM;
2313 
2314 	memset(&crq, 0, sizeof(crq));
2315 	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
2316 	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
2317 	crq.control_ip_offload.len =
2318 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
2319 	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
2320 	ibmvnic_send_crq(adapter, &crq);
2321 }
2322 
2323 static void handle_error_info_rsp(union ibmvnic_crq *crq,
2324 				  struct ibmvnic_adapter *adapter)
2325 {
2326 	struct device *dev = &adapter->vdev->dev;
2327 	struct ibmvnic_error_buff *error_buff, *tmp;
2328 	unsigned long flags;
2329 	bool found = false;
2330 	int i;
2331 
2332 	if (!crq->request_error_rsp.rc.code) {
2333 		dev_info(dev, "Request Error Rsp returned with rc=%x\n",
2334 			 crq->request_error_rsp.rc.code);
2335 		return;
2336 	}
2337 
2338 	spin_lock_irqsave(&adapter->error_list_lock, flags);
2339 	list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
2340 		if (error_buff->error_id == crq->request_error_rsp.error_id) {
2341 			found = true;
2342 			list_del(&error_buff->list);
2343 			break;
2344 		}
2345 	spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2346 
2347 	if (!found) {
2348 		dev_err(dev, "Couldn't find error id %x\n",
2349 			be32_to_cpu(crq->request_error_rsp.error_id));
2350 		return;
2351 	}
2352 
2353 	dev_err(dev, "Detailed info for error id %x:",
2354 		be32_to_cpu(crq->request_error_rsp.error_id));
2355 
2356 	for (i = 0; i < error_buff->len; i++) {
2357 		pr_cont("%02x", (int)error_buff->buff[i]);
2358 		if (i % 8 == 7)
2359 			pr_cont(" ");
2360 	}
2361 	pr_cont("\n");
2362 
2363 	dma_unmap_single(dev, error_buff->dma, error_buff->len,
2364 			 DMA_FROM_DEVICE);
2365 	kfree(error_buff->buff);
2366 	kfree(error_buff);
2367 }
2368 
2369 static void handle_error_indication(union ibmvnic_crq *crq,
2370 				    struct ibmvnic_adapter *adapter)
2371 {
2372 	int detail_len = be32_to_cpu(crq->error_indication.detail_error_sz);
2373 	struct ibmvnic_inflight_cmd *inflight_cmd;
2374 	struct device *dev = &adapter->vdev->dev;
2375 	struct ibmvnic_error_buff *error_buff;
2376 	union ibmvnic_crq new_crq;
2377 	unsigned long flags;
2378 
2379 	dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
2380 		crq->error_indication.
2381 		    flags & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
2382 		be32_to_cpu(crq->error_indication.error_id),
2383 		be16_to_cpu(crq->error_indication.error_cause));
2384 
2385 	error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
2386 	if (!error_buff)
2387 		return;
2388 
2389 	error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
2390 	if (!error_buff->buff) {
2391 		kfree(error_buff);
2392 		return;
2393 	}
2394 
2395 	error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
2396 					 DMA_FROM_DEVICE);
2397 	if (dma_mapping_error(dev, error_buff->dma)) {
2398 		if (!firmware_has_feature(FW_FEATURE_CMO))
2399 			dev_err(dev, "Couldn't map error buffer\n");
2400 		kfree(error_buff->buff);
2401 		kfree(error_buff);
2402 		return;
2403 	}
2404 
2405 	inflight_cmd = kmalloc(sizeof(*inflight_cmd), GFP_ATOMIC);
2406 	if (!inflight_cmd) {
2407 		dma_unmap_single(dev, error_buff->dma, detail_len,
2408 				 DMA_FROM_DEVICE);
2409 		kfree(error_buff->buff);
2410 		kfree(error_buff);
2411 		return;
2412 	}
2413 
2414 	error_buff->len = detail_len;
2415 	error_buff->error_id = crq->error_indication.error_id;
2416 
2417 	spin_lock_irqsave(&adapter->error_list_lock, flags);
2418 	list_add_tail(&error_buff->list, &adapter->errors);
2419 	spin_unlock_irqrestore(&adapter->error_list_lock, flags);
2420 
2421 	memset(&new_crq, 0, sizeof(new_crq));
2422 	new_crq.request_error_info.first = IBMVNIC_CRQ_CMD;
2423 	new_crq.request_error_info.cmd = REQUEST_ERROR_INFO;
2424 	new_crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
2425 	new_crq.request_error_info.len = cpu_to_be32(detail_len);
2426 	new_crq.request_error_info.error_id = crq->error_indication.error_id;
2427 
2428 	memcpy(&inflight_cmd->crq, &crq, sizeof(crq));
2429 
2430 	spin_lock_irqsave(&adapter->inflight_lock, flags);
2431 	list_add_tail(&inflight_cmd->list, &adapter->inflight);
2432 	spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2433 
2434 	ibmvnic_send_crq(adapter, &new_crq);
2435 }
2436 
2437 static void handle_change_mac_rsp(union ibmvnic_crq *crq,
2438 				  struct ibmvnic_adapter *adapter)
2439 {
2440 	struct net_device *netdev = adapter->netdev;
2441 	struct device *dev = &adapter->vdev->dev;
2442 	long rc;
2443 
2444 	rc = crq->change_mac_addr_rsp.rc.code;
2445 	if (rc) {
2446 		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
2447 		return;
2448 	}
2449 	memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
2450 	       ETH_ALEN);
2451 }
2452 
2453 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
2454 				   struct ibmvnic_adapter *adapter)
2455 {
2456 	struct device *dev = &adapter->vdev->dev;
2457 	u64 *req_value;
2458 	char *name;
2459 
2460 	atomic_dec(&adapter->running_cap_crqs);
2461 	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
2462 	case REQ_TX_QUEUES:
2463 		req_value = &adapter->req_tx_queues;
2464 		name = "tx";
2465 		break;
2466 	case REQ_RX_QUEUES:
2467 		req_value = &adapter->req_rx_queues;
2468 		name = "rx";
2469 		break;
2470 	case REQ_RX_ADD_QUEUES:
2471 		req_value = &adapter->req_rx_add_queues;
2472 		name = "rx_add";
2473 		break;
2474 	case REQ_TX_ENTRIES_PER_SUBCRQ:
2475 		req_value = &adapter->req_tx_entries_per_subcrq;
2476 		name = "tx_entries_per_subcrq";
2477 		break;
2478 	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
2479 		req_value = &adapter->req_rx_add_entries_per_subcrq;
2480 		name = "rx_add_entries_per_subcrq";
2481 		break;
2482 	case REQ_MTU:
2483 		req_value = &adapter->req_mtu;
2484 		name = "mtu";
2485 		break;
2486 	case PROMISC_REQUESTED:
2487 		req_value = &adapter->promisc;
2488 		name = "promisc";
2489 		break;
2490 	default:
2491 		dev_err(dev, "Got invalid cap request rsp %d\n",
2492 			crq->request_capability.capability);
2493 		return;
2494 	}
2495 
2496 	switch (crq->request_capability_rsp.rc.code) {
2497 	case SUCCESS:
2498 		break;
2499 	case PARTIALSUCCESS:
2500 		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
2501 			 *req_value,
2502 			 (long int)be64_to_cpu(crq->request_capability_rsp.
2503 					       number), name);
2504 		release_sub_crqs(adapter);
2505 		*req_value = be64_to_cpu(crq->request_capability_rsp.number);
2506 		init_sub_crqs(adapter, 1);
2507 		return;
2508 	default:
2509 		dev_err(dev, "Error %d in request cap rsp\n",
2510 			crq->request_capability_rsp.rc.code);
2511 		return;
2512 	}
2513 
2514 	/* Done receiving requested capabilities, query IP offload support */
2515 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
2516 		union ibmvnic_crq newcrq;
2517 		int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
2518 		struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
2519 		    &adapter->ip_offload_buf;
2520 
2521 		adapter->wait_capability = false;
2522 		adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
2523 							 buf_sz,
2524 							 DMA_FROM_DEVICE);
2525 
2526 		if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
2527 			if (!firmware_has_feature(FW_FEATURE_CMO))
2528 				dev_err(dev, "Couldn't map offload buffer\n");
2529 			return;
2530 		}
2531 
2532 		memset(&newcrq, 0, sizeof(newcrq));
2533 		newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
2534 		newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
2535 		newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
2536 		newcrq.query_ip_offload.ioba =
2537 		    cpu_to_be32(adapter->ip_offload_tok);
2538 
2539 		ibmvnic_send_crq(adapter, &newcrq);
2540 	}
2541 }
2542 
2543 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
2544 			    struct ibmvnic_adapter *adapter)
2545 {
2546 	struct device *dev = &adapter->vdev->dev;
2547 	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
2548 	struct ibmvnic_login_buffer *login = adapter->login_buf;
2549 	int i;
2550 
2551 	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
2552 			 DMA_BIDIRECTIONAL);
2553 	dma_unmap_single(dev, adapter->login_rsp_buf_token,
2554 			 adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
2555 
2556 	/* If the number of queues requested can't be allocated by the
2557 	 * server, the login response will return with code 1. We will need
2558 	 * to resend the login buffer with fewer queues requested.
2559 	 */
2560 	if (login_rsp_crq->generic.rc.code) {
2561 		adapter->renegotiate = true;
2562 		complete(&adapter->init_done);
2563 		return 0;
2564 	}
2565 
2566 	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
2567 	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
2568 		netdev_dbg(adapter->netdev, "%016lx\n",
2569 			   ((unsigned long int *)(adapter->login_rsp_buf))[i]);
2570 	}
2571 
2572 	/* Sanity checks */
2573 	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
2574 	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
2575 	     adapter->req_rx_add_queues !=
2576 	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
2577 		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
2578 		ibmvnic_remove(adapter->vdev);
2579 		return -EIO;
2580 	}
2581 	complete(&adapter->init_done);
2582 
2583 	return 0;
2584 }
2585 
2586 static void handle_request_map_rsp(union ibmvnic_crq *crq,
2587 				   struct ibmvnic_adapter *adapter)
2588 {
2589 	struct device *dev = &adapter->vdev->dev;
2590 	u8 map_id = crq->request_map_rsp.map_id;
2591 	int tx_subcrqs;
2592 	int rx_subcrqs;
2593 	long rc;
2594 	int i;
2595 
2596 	tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
2597 	rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
2598 
2599 	rc = crq->request_map_rsp.rc.code;
2600 	if (rc) {
2601 		dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
2602 		adapter->map_id--;
2603 		/* need to find and zero tx/rx_pool map_id */
2604 		for (i = 0; i < tx_subcrqs; i++) {
2605 			if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
2606 				adapter->tx_pool[i].long_term_buff.map_id = 0;
2607 		}
2608 		for (i = 0; i < rx_subcrqs; i++) {
2609 			if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
2610 				adapter->rx_pool[i].long_term_buff.map_id = 0;
2611 		}
2612 	}
2613 	complete(&adapter->fw_done);
2614 }
2615 
2616 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
2617 				     struct ibmvnic_adapter *adapter)
2618 {
2619 	struct device *dev = &adapter->vdev->dev;
2620 	long rc;
2621 
2622 	rc = crq->request_unmap_rsp.rc.code;
2623 	if (rc)
2624 		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
2625 }
2626 
2627 static void handle_query_map_rsp(union ibmvnic_crq *crq,
2628 				 struct ibmvnic_adapter *adapter)
2629 {
2630 	struct net_device *netdev = adapter->netdev;
2631 	struct device *dev = &adapter->vdev->dev;
2632 	long rc;
2633 
2634 	rc = crq->query_map_rsp.rc.code;
2635 	if (rc) {
2636 		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
2637 		return;
2638 	}
2639 	netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
2640 		   crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
2641 		   crq->query_map_rsp.free_pages);
2642 }
2643 
2644 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
2645 				 struct ibmvnic_adapter *adapter)
2646 {
2647 	struct net_device *netdev = adapter->netdev;
2648 	struct device *dev = &adapter->vdev->dev;
2649 	long rc;
2650 
2651 	atomic_dec(&adapter->running_cap_crqs);
2652 	netdev_dbg(netdev, "Outstanding queries: %d\n",
2653 		   atomic_read(&adapter->running_cap_crqs));
2654 	rc = crq->query_capability.rc.code;
2655 	if (rc) {
2656 		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
2657 		goto out;
2658 	}
2659 
2660 	switch (be16_to_cpu(crq->query_capability.capability)) {
2661 	case MIN_TX_QUEUES:
2662 		adapter->min_tx_queues =
2663 		    be64_to_cpu(crq->query_capability.number);
2664 		netdev_dbg(netdev, "min_tx_queues = %lld\n",
2665 			   adapter->min_tx_queues);
2666 		break;
2667 	case MIN_RX_QUEUES:
2668 		adapter->min_rx_queues =
2669 		    be64_to_cpu(crq->query_capability.number);
2670 		netdev_dbg(netdev, "min_rx_queues = %lld\n",
2671 			   adapter->min_rx_queues);
2672 		break;
2673 	case MIN_RX_ADD_QUEUES:
2674 		adapter->min_rx_add_queues =
2675 		    be64_to_cpu(crq->query_capability.number);
2676 		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
2677 			   adapter->min_rx_add_queues);
2678 		break;
2679 	case MAX_TX_QUEUES:
2680 		adapter->max_tx_queues =
2681 		    be64_to_cpu(crq->query_capability.number);
2682 		netdev_dbg(netdev, "max_tx_queues = %lld\n",
2683 			   adapter->max_tx_queues);
2684 		break;
2685 	case MAX_RX_QUEUES:
2686 		adapter->max_rx_queues =
2687 		    be64_to_cpu(crq->query_capability.number);
2688 		netdev_dbg(netdev, "max_rx_queues = %lld\n",
2689 			   adapter->max_rx_queues);
2690 		break;
2691 	case MAX_RX_ADD_QUEUES:
2692 		adapter->max_rx_add_queues =
2693 		    be64_to_cpu(crq->query_capability.number);
2694 		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
2695 			   adapter->max_rx_add_queues);
2696 		break;
2697 	case MIN_TX_ENTRIES_PER_SUBCRQ:
2698 		adapter->min_tx_entries_per_subcrq =
2699 		    be64_to_cpu(crq->query_capability.number);
2700 		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
2701 			   adapter->min_tx_entries_per_subcrq);
2702 		break;
2703 	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
2704 		adapter->min_rx_add_entries_per_subcrq =
2705 		    be64_to_cpu(crq->query_capability.number);
2706 		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
2707 			   adapter->min_rx_add_entries_per_subcrq);
2708 		break;
2709 	case MAX_TX_ENTRIES_PER_SUBCRQ:
2710 		adapter->max_tx_entries_per_subcrq =
2711 		    be64_to_cpu(crq->query_capability.number);
2712 		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
2713 			   adapter->max_tx_entries_per_subcrq);
2714 		break;
2715 	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
2716 		adapter->max_rx_add_entries_per_subcrq =
2717 		    be64_to_cpu(crq->query_capability.number);
2718 		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
2719 			   adapter->max_rx_add_entries_per_subcrq);
2720 		break;
2721 	case TCP_IP_OFFLOAD:
2722 		adapter->tcp_ip_offload =
2723 		    be64_to_cpu(crq->query_capability.number);
2724 		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
2725 			   adapter->tcp_ip_offload);
2726 		break;
2727 	case PROMISC_SUPPORTED:
2728 		adapter->promisc_supported =
2729 		    be64_to_cpu(crq->query_capability.number);
2730 		netdev_dbg(netdev, "promisc_supported = %lld\n",
2731 			   adapter->promisc_supported);
2732 		break;
2733 	case MIN_MTU:
2734 		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
2735 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
2736 		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
2737 		break;
2738 	case MAX_MTU:
2739 		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
2740 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
2741 		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
2742 		break;
2743 	case MAX_MULTICAST_FILTERS:
2744 		adapter->max_multicast_filters =
2745 		    be64_to_cpu(crq->query_capability.number);
2746 		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
2747 			   adapter->max_multicast_filters);
2748 		break;
2749 	case VLAN_HEADER_INSERTION:
2750 		adapter->vlan_header_insertion =
2751 		    be64_to_cpu(crq->query_capability.number);
2752 		if (adapter->vlan_header_insertion)
2753 			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
2754 		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
2755 			   adapter->vlan_header_insertion);
2756 		break;
2757 	case MAX_TX_SG_ENTRIES:
2758 		adapter->max_tx_sg_entries =
2759 		    be64_to_cpu(crq->query_capability.number);
2760 		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
2761 			   adapter->max_tx_sg_entries);
2762 		break;
2763 	case RX_SG_SUPPORTED:
2764 		adapter->rx_sg_supported =
2765 		    be64_to_cpu(crq->query_capability.number);
2766 		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
2767 			   adapter->rx_sg_supported);
2768 		break;
2769 	case OPT_TX_COMP_SUB_QUEUES:
2770 		adapter->opt_tx_comp_sub_queues =
2771 		    be64_to_cpu(crq->query_capability.number);
2772 		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
2773 			   adapter->opt_tx_comp_sub_queues);
2774 		break;
2775 	case OPT_RX_COMP_QUEUES:
2776 		adapter->opt_rx_comp_queues =
2777 		    be64_to_cpu(crq->query_capability.number);
2778 		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
2779 			   adapter->opt_rx_comp_queues);
2780 		break;
2781 	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
2782 		adapter->opt_rx_bufadd_q_per_rx_comp_q =
2783 		    be64_to_cpu(crq->query_capability.number);
2784 		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
2785 			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
2786 		break;
2787 	case OPT_TX_ENTRIES_PER_SUBCRQ:
2788 		adapter->opt_tx_entries_per_subcrq =
2789 		    be64_to_cpu(crq->query_capability.number);
2790 		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
2791 			   adapter->opt_tx_entries_per_subcrq);
2792 		break;
2793 	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
2794 		adapter->opt_rxba_entries_per_subcrq =
2795 		    be64_to_cpu(crq->query_capability.number);
2796 		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
2797 			   adapter->opt_rxba_entries_per_subcrq);
2798 		break;
2799 	case TX_RX_DESC_REQ:
2800 		adapter->tx_rx_desc_req = crq->query_capability.number;
2801 		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
2802 			   adapter->tx_rx_desc_req);
2803 		break;
2804 
2805 	default:
2806 		netdev_err(netdev, "Got invalid cap rsp %d\n",
2807 			   crq->query_capability.capability);
2808 	}
2809 
2810 out:
2811 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
2812 		adapter->wait_capability = false;
2813 		init_sub_crqs(adapter, 0);
2814 		/* We're done querying the capabilities, initialize sub-crqs */
2815 	}
2816 }
2817 
2818 static void ibmvnic_free_inflight(struct ibmvnic_adapter *adapter)
2819 {
2820 	struct ibmvnic_inflight_cmd *inflight_cmd, *tmp1;
2821 	struct device *dev = &adapter->vdev->dev;
2822 	struct ibmvnic_error_buff *error_buff, *tmp2;
2823 	unsigned long flags;
2824 	unsigned long flags2;
2825 
2826 	spin_lock_irqsave(&adapter->inflight_lock, flags);
2827 	list_for_each_entry_safe(inflight_cmd, tmp1, &adapter->inflight, list) {
2828 		switch (inflight_cmd->crq.generic.cmd) {
2829 		case LOGIN:
2830 			dma_unmap_single(dev, adapter->login_buf_token,
2831 					 adapter->login_buf_sz,
2832 					 DMA_BIDIRECTIONAL);
2833 			dma_unmap_single(dev, adapter->login_rsp_buf_token,
2834 					 adapter->login_rsp_buf_sz,
2835 					 DMA_BIDIRECTIONAL);
2836 			kfree(adapter->login_rsp_buf);
2837 			kfree(adapter->login_buf);
2838 			break;
2839 		case REQUEST_ERROR_INFO:
2840 			spin_lock_irqsave(&adapter->error_list_lock, flags2);
2841 			list_for_each_entry_safe(error_buff, tmp2,
2842 						 &adapter->errors, list) {
2843 				dma_unmap_single(dev, error_buff->dma,
2844 						 error_buff->len,
2845 						 DMA_FROM_DEVICE);
2846 				kfree(error_buff->buff);
2847 				list_del(&error_buff->list);
2848 				kfree(error_buff);
2849 			}
2850 			spin_unlock_irqrestore(&adapter->error_list_lock,
2851 					       flags2);
2852 			break;
2853 		}
2854 		list_del(&inflight_cmd->list);
2855 		kfree(inflight_cmd);
2856 	}
2857 	spin_unlock_irqrestore(&adapter->inflight_lock, flags);
2858 }
2859 
2860 static void ibmvnic_xport_event(struct work_struct *work)
2861 {
2862 	struct ibmvnic_adapter *adapter = container_of(work,
2863 						       struct ibmvnic_adapter,
2864 						       ibmvnic_xport);
2865 	struct device *dev = &adapter->vdev->dev;
2866 	long rc;
2867 
2868 	ibmvnic_free_inflight(adapter);
2869 	release_sub_crqs(adapter);
2870 	if (adapter->migrated) {
2871 		rc = ibmvnic_reenable_crq_queue(adapter);
2872 		if (rc)
2873 			dev_err(dev, "Error after enable rc=%ld\n", rc);
2874 		adapter->migrated = false;
2875 		rc = ibmvnic_send_crq_init(adapter);
2876 		if (rc)
2877 			dev_err(dev, "Error sending init rc=%ld\n", rc);
2878 	}
2879 }
2880 
2881 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
2882 			       struct ibmvnic_adapter *adapter)
2883 {
2884 	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
2885 	struct net_device *netdev = adapter->netdev;
2886 	struct device *dev = &adapter->vdev->dev;
2887 	long rc;
2888 
2889 	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
2890 		   ((unsigned long int *)crq)[0],
2891 		   ((unsigned long int *)crq)[1]);
2892 	switch (gen_crq->first) {
2893 	case IBMVNIC_CRQ_INIT_RSP:
2894 		switch (gen_crq->cmd) {
2895 		case IBMVNIC_CRQ_INIT:
2896 			dev_info(dev, "Partner initialized\n");
2897 			/* Send back a response */
2898 			rc = ibmvnic_send_crq_init_complete(adapter);
2899 			if (!rc)
2900 				schedule_work(&adapter->vnic_crq_init);
2901 			else
2902 				dev_err(dev, "Can't send initrsp rc=%ld\n", rc);
2903 			break;
2904 		case IBMVNIC_CRQ_INIT_COMPLETE:
2905 			dev_info(dev, "Partner initialization complete\n");
2906 			send_version_xchg(adapter);
2907 			break;
2908 		default:
2909 			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
2910 		}
2911 		return;
2912 	case IBMVNIC_CRQ_XPORT_EVENT:
2913 		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
2914 			dev_info(dev, "Re-enabling adapter\n");
2915 			adapter->migrated = true;
2916 			schedule_work(&adapter->ibmvnic_xport);
2917 		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
2918 			dev_info(dev, "Backing device failover detected\n");
2919 			netif_carrier_off(netdev);
2920 			adapter->failover = true;
2921 		} else {
2922 			/* The adapter lost the connection */
2923 			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
2924 				gen_crq->cmd);
2925 			schedule_work(&adapter->ibmvnic_xport);
2926 		}
2927 		return;
2928 	case IBMVNIC_CRQ_CMD_RSP:
2929 		break;
2930 	default:
2931 		dev_err(dev, "Got an invalid msg type 0x%02x\n",
2932 			gen_crq->first);
2933 		return;
2934 	}
2935 
2936 	switch (gen_crq->cmd) {
2937 	case VERSION_EXCHANGE_RSP:
2938 		rc = crq->version_exchange_rsp.rc.code;
2939 		if (rc) {
2940 			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
2941 			break;
2942 		}
2943 		dev_info(dev, "Partner protocol version is %d\n",
2944 			 crq->version_exchange_rsp.version);
2945 		if (be16_to_cpu(crq->version_exchange_rsp.version) <
2946 		    ibmvnic_version)
2947 			ibmvnic_version =
2948 			    be16_to_cpu(crq->version_exchange_rsp.version);
2949 		send_cap_queries(adapter);
2950 		break;
2951 	case QUERY_CAPABILITY_RSP:
2952 		handle_query_cap_rsp(crq, adapter);
2953 		break;
2954 	case QUERY_MAP_RSP:
2955 		handle_query_map_rsp(crq, adapter);
2956 		break;
2957 	case REQUEST_MAP_RSP:
2958 		handle_request_map_rsp(crq, adapter);
2959 		break;
2960 	case REQUEST_UNMAP_RSP:
2961 		handle_request_unmap_rsp(crq, adapter);
2962 		break;
2963 	case REQUEST_CAPABILITY_RSP:
2964 		handle_request_cap_rsp(crq, adapter);
2965 		break;
2966 	case LOGIN_RSP:
2967 		netdev_dbg(netdev, "Got Login Response\n");
2968 		handle_login_rsp(crq, adapter);
2969 		break;
2970 	case LOGICAL_LINK_STATE_RSP:
2971 		netdev_dbg(netdev, "Got Logical Link State Response\n");
2972 		adapter->logical_link_state =
2973 		    crq->logical_link_state_rsp.link_state;
2974 		break;
2975 	case LINK_STATE_INDICATION:
2976 		netdev_dbg(netdev, "Got Logical Link State Indication\n");
2977 		adapter->phys_link_state =
2978 		    crq->link_state_indication.phys_link_state;
2979 		adapter->logical_link_state =
2980 		    crq->link_state_indication.logical_link_state;
2981 		break;
2982 	case CHANGE_MAC_ADDR_RSP:
2983 		netdev_dbg(netdev, "Got MAC address change Response\n");
2984 		handle_change_mac_rsp(crq, adapter);
2985 		break;
2986 	case ERROR_INDICATION:
2987 		netdev_dbg(netdev, "Got Error Indication\n");
2988 		handle_error_indication(crq, adapter);
2989 		break;
2990 	case REQUEST_ERROR_RSP:
2991 		netdev_dbg(netdev, "Got Error Detail Response\n");
2992 		handle_error_info_rsp(crq, adapter);
2993 		break;
2994 	case REQUEST_STATISTICS_RSP:
2995 		netdev_dbg(netdev, "Got Statistics Response\n");
2996 		complete(&adapter->stats_done);
2997 		break;
2998 	case QUERY_IP_OFFLOAD_RSP:
2999 		netdev_dbg(netdev, "Got Query IP offload Response\n");
3000 		handle_query_ip_offload_rsp(adapter);
3001 		break;
3002 	case MULTICAST_CTRL_RSP:
3003 		netdev_dbg(netdev, "Got multicast control Response\n");
3004 		break;
3005 	case CONTROL_IP_OFFLOAD_RSP:
3006 		netdev_dbg(netdev, "Got Control IP offload Response\n");
3007 		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
3008 				 sizeof(adapter->ip_offload_ctrl),
3009 				 DMA_TO_DEVICE);
3010 		complete(&adapter->init_done);
3011 		break;
3012 	case COLLECT_FW_TRACE_RSP:
3013 		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
3014 		complete(&adapter->fw_done);
3015 		break;
3016 	default:
3017 		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
3018 			   gen_crq->cmd);
3019 	}
3020 }
3021 
3022 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
3023 {
3024 	struct ibmvnic_adapter *adapter = instance;
3025 	unsigned long flags;
3026 
3027 	spin_lock_irqsave(&adapter->crq.lock, flags);
3028 	vio_disable_interrupts(adapter->vdev);
3029 	tasklet_schedule(&adapter->tasklet);
3030 	spin_unlock_irqrestore(&adapter->crq.lock, flags);
3031 	return IRQ_HANDLED;
3032 }
3033 
3034 static void ibmvnic_tasklet(void *data)
3035 {
3036 	struct ibmvnic_adapter *adapter = data;
3037 	struct ibmvnic_crq_queue *queue = &adapter->crq;
3038 	struct vio_dev *vdev = adapter->vdev;
3039 	union ibmvnic_crq *crq;
3040 	unsigned long flags;
3041 	bool done = false;
3042 
3043 	spin_lock_irqsave(&queue->lock, flags);
3044 	vio_disable_interrupts(vdev);
3045 	while (!done) {
3046 		/* Pull all the valid messages off the CRQ */
3047 		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
3048 			ibmvnic_handle_crq(crq, adapter);
3049 			crq->generic.first = 0;
3050 		}
3051 		vio_enable_interrupts(vdev);
3052 		crq = ibmvnic_next_crq(adapter);
3053 		if (crq) {
3054 			vio_disable_interrupts(vdev);
3055 			ibmvnic_handle_crq(crq, adapter);
3056 			crq->generic.first = 0;
3057 		} else {
3058 			/* remain in tasklet until all
3059 			 * capabilities responses are received
3060 			 */
3061 			if (!adapter->wait_capability)
3062 				done = true;
3063 		}
3064 	}
3065 	/* if capabilities CRQ's were sent in this tasklet, the following
3066 	 * tasklet must wait until all responses are received
3067 	 */
3068 	if (atomic_read(&adapter->running_cap_crqs) != 0)
3069 		adapter->wait_capability = true;
3070 	spin_unlock_irqrestore(&queue->lock, flags);
3071 }
3072 
3073 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
3074 {
3075 	struct vio_dev *vdev = adapter->vdev;
3076 	int rc;
3077 
3078 	do {
3079 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
3080 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
3081 
3082 	if (rc)
3083 		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
3084 
3085 	return rc;
3086 }
3087 
3088 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
3089 {
3090 	struct ibmvnic_crq_queue *crq = &adapter->crq;
3091 	struct device *dev = &adapter->vdev->dev;
3092 	struct vio_dev *vdev = adapter->vdev;
3093 	int rc;
3094 
3095 	/* Close the CRQ */
3096 	do {
3097 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3098 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3099 
3100 	/* Clean out the queue */
3101 	memset(crq->msgs, 0, PAGE_SIZE);
3102 	crq->cur = 0;
3103 
3104 	/* And re-open it again */
3105 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3106 				crq->msg_token, PAGE_SIZE);
3107 
3108 	if (rc == H_CLOSED)
3109 		/* Adapter is good, but other end is not ready */
3110 		dev_warn(dev, "Partner adapter not ready\n");
3111 	else if (rc != 0)
3112 		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
3113 
3114 	return rc;
3115 }
3116 
3117 static void release_crq_queue(struct ibmvnic_adapter *adapter)
3118 {
3119 	struct ibmvnic_crq_queue *crq = &adapter->crq;
3120 	struct vio_dev *vdev = adapter->vdev;
3121 	long rc;
3122 
3123 	if (!crq->msgs)
3124 		return;
3125 
3126 	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
3127 	free_irq(vdev->irq, adapter);
3128 	tasklet_kill(&adapter->tasklet);
3129 	do {
3130 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3131 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3132 
3133 	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
3134 			 DMA_BIDIRECTIONAL);
3135 	free_page((unsigned long)crq->msgs);
3136 	crq->msgs = NULL;
3137 }
3138 
3139 static int init_crq_queue(struct ibmvnic_adapter *adapter)
3140 {
3141 	struct ibmvnic_crq_queue *crq = &adapter->crq;
3142 	struct device *dev = &adapter->vdev->dev;
3143 	struct vio_dev *vdev = adapter->vdev;
3144 	int rc, retrc = -ENOMEM;
3145 
3146 	if (crq->msgs)
3147 		return 0;
3148 
3149 	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
3150 	/* Should we allocate more than one page? */
3151 
3152 	if (!crq->msgs)
3153 		return -ENOMEM;
3154 
3155 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3156 	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
3157 					DMA_BIDIRECTIONAL);
3158 	if (dma_mapping_error(dev, crq->msg_token))
3159 		goto map_failed;
3160 
3161 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3162 				crq->msg_token, PAGE_SIZE);
3163 
3164 	if (rc == H_RESOURCE)
3165 		/* maybe kexecing and resource is busy. try a reset */
3166 		rc = ibmvnic_reset_crq(adapter);
3167 	retrc = rc;
3168 
3169 	if (rc == H_CLOSED) {
3170 		dev_warn(dev, "Partner adapter not ready\n");
3171 	} else if (rc) {
3172 		dev_warn(dev, "Error %d opening adapter\n", rc);
3173 		goto reg_crq_failed;
3174 	}
3175 
3176 	retrc = 0;
3177 
3178 	tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
3179 		     (unsigned long)adapter);
3180 
3181 	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
3182 	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
3183 			 adapter);
3184 	if (rc) {
3185 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
3186 			vdev->irq, rc);
3187 		goto req_irq_failed;
3188 	}
3189 
3190 	rc = vio_enable_interrupts(vdev);
3191 	if (rc) {
3192 		dev_err(dev, "Error %d enabling interrupts\n", rc);
3193 		goto req_irq_failed;
3194 	}
3195 
3196 	crq->cur = 0;
3197 	spin_lock_init(&crq->lock);
3198 
3199 	return retrc;
3200 
3201 req_irq_failed:
3202 	tasklet_kill(&adapter->tasklet);
3203 	do {
3204 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3205 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3206 reg_crq_failed:
3207 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3208 map_failed:
3209 	free_page((unsigned long)crq->msgs);
3210 	crq->msgs = NULL;
3211 	return retrc;
3212 }
3213 
3214 static void handle_crq_init_rsp(struct work_struct *work)
3215 {
3216 	struct ibmvnic_adapter *adapter = container_of(work,
3217 						       struct ibmvnic_adapter,
3218 						       vnic_crq_init);
3219 	struct device *dev = &adapter->vdev->dev;
3220 	struct net_device *netdev = adapter->netdev;
3221 	unsigned long timeout = msecs_to_jiffies(30000);
3222 	bool restart = false;
3223 	int rc;
3224 
3225 	if (adapter->failover) {
3226 		release_sub_crqs(adapter);
3227 		if (netif_running(netdev)) {
3228 			netif_tx_disable(netdev);
3229 			ibmvnic_close(netdev);
3230 			restart = true;
3231 		}
3232 	}
3233 
3234 	reinit_completion(&adapter->init_done);
3235 	send_version_xchg(adapter);
3236 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3237 		dev_err(dev, "Passive init timeout\n");
3238 		goto task_failed;
3239 	}
3240 
3241 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
3242 
3243 	if (adapter->failover) {
3244 		adapter->failover = false;
3245 		if (restart) {
3246 			rc = ibmvnic_open(netdev);
3247 			if (rc)
3248 				goto restart_failed;
3249 		}
3250 		netif_carrier_on(netdev);
3251 		return;
3252 	}
3253 
3254 	rc = register_netdev(netdev);
3255 	if (rc) {
3256 		dev_err(dev,
3257 			"failed to register netdev rc=%d\n", rc);
3258 		goto register_failed;
3259 	}
3260 	dev_info(dev, "ibmvnic registered\n");
3261 
3262 	return;
3263 
3264 restart_failed:
3265 	dev_err(dev, "Failed to restart ibmvnic, rc=%d\n", rc);
3266 register_failed:
3267 	release_sub_crqs(adapter);
3268 task_failed:
3269 	dev_err(dev, "Passive initialization was not successful\n");
3270 }
3271 
3272 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
3273 {
3274 	struct device *dev = &adapter->vdev->dev;
3275 	unsigned long timeout = msecs_to_jiffies(30000);
3276 	int rc;
3277 
3278 	rc = init_crq_queue(adapter);
3279 	if (rc) {
3280 		dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3281 		return rc;
3282 	}
3283 
3284 	rc = init_stats_token(adapter);
3285 	if (rc) {
3286 		release_crq_queue(adapter);
3287 		return rc;
3288 	}
3289 
3290 	init_completion(&adapter->init_done);
3291 	ibmvnic_send_crq_init(adapter);
3292 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3293 		dev_err(dev, "Initialization sequence timed out\n");
3294 		release_crq_queue(adapter);
3295 		return -1;
3296 	}
3297 
3298 	return 0;
3299 }
3300 
3301 static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
3302 {
3303 	struct ibmvnic_adapter *adapter;
3304 	struct net_device *netdev;
3305 	unsigned char *mac_addr_p;
3306 	int rc;
3307 
3308 	dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
3309 		dev->unit_address);
3310 
3311 	mac_addr_p = (unsigned char *)vio_get_attribute(dev,
3312 							VETH_MAC_ADDR, NULL);
3313 	if (!mac_addr_p) {
3314 		dev_err(&dev->dev,
3315 			"(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
3316 			__FILE__, __LINE__);
3317 		return 0;
3318 	}
3319 
3320 	netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
3321 				   IBMVNIC_MAX_TX_QUEUES);
3322 	if (!netdev)
3323 		return -ENOMEM;
3324 
3325 	adapter = netdev_priv(netdev);
3326 	dev_set_drvdata(&dev->dev, netdev);
3327 	adapter->vdev = dev;
3328 	adapter->netdev = netdev;
3329 	adapter->failover = false;
3330 
3331 	ether_addr_copy(adapter->mac_addr, mac_addr_p);
3332 	ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
3333 	netdev->irq = dev->irq;
3334 	netdev->netdev_ops = &ibmvnic_netdev_ops;
3335 	netdev->ethtool_ops = &ibmvnic_ethtool_ops;
3336 	SET_NETDEV_DEV(netdev, &dev->dev);
3337 
3338 	INIT_WORK(&adapter->vnic_crq_init, handle_crq_init_rsp);
3339 	INIT_WORK(&adapter->ibmvnic_xport, ibmvnic_xport_event);
3340 
3341 	spin_lock_init(&adapter->stats_lock);
3342 
3343 	INIT_LIST_HEAD(&adapter->errors);
3344 	INIT_LIST_HEAD(&adapter->inflight);
3345 	spin_lock_init(&adapter->error_list_lock);
3346 	spin_lock_init(&adapter->inflight_lock);
3347 
3348 	rc = ibmvnic_init(adapter);
3349 	if (rc) {
3350 		free_netdev(netdev);
3351 		return rc;
3352 	}
3353 
3354 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
3355 	adapter->is_closed = false;
3356 
3357 	rc = register_netdev(netdev);
3358 	if (rc) {
3359 		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
3360 		free_netdev(netdev);
3361 		return rc;
3362 	}
3363 	dev_info(&dev->dev, "ibmvnic registered\n");
3364 
3365 	return 0;
3366 }
3367 
3368 static int ibmvnic_remove(struct vio_dev *dev)
3369 {
3370 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
3371 
3372 	unregister_netdev(netdev);
3373 	free_netdev(netdev);
3374 	dev_set_drvdata(&dev->dev, NULL);
3375 
3376 	return 0;
3377 }
3378 
3379 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
3380 {
3381 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
3382 	struct ibmvnic_adapter *adapter;
3383 	struct iommu_table *tbl;
3384 	unsigned long ret = 0;
3385 	int i;
3386 
3387 	tbl = get_iommu_table_base(&vdev->dev);
3388 
3389 	/* netdev inits at probe time along with the structures we need below*/
3390 	if (!netdev)
3391 		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
3392 
3393 	adapter = netdev_priv(netdev);
3394 
3395 	ret += PAGE_SIZE; /* the crq message queue */
3396 	ret += adapter->bounce_buffer_size;
3397 	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
3398 
3399 	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
3400 		ret += 4 * PAGE_SIZE; /* the scrq message queue */
3401 
3402 	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
3403 	     i++)
3404 		ret += adapter->rx_pool[i].size *
3405 		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
3406 
3407 	return ret;
3408 }
3409 
3410 static int ibmvnic_resume(struct device *dev)
3411 {
3412 	struct net_device *netdev = dev_get_drvdata(dev);
3413 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3414 	int i;
3415 
3416 	/* kick the interrupt handlers just in case we lost an interrupt */
3417 	for (i = 0; i < adapter->req_rx_queues; i++)
3418 		ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
3419 				     adapter->rx_scrq[i]);
3420 
3421 	return 0;
3422 }
3423 
3424 static struct vio_device_id ibmvnic_device_table[] = {
3425 	{"network", "IBM,vnic"},
3426 	{"", "" }
3427 };
3428 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
3429 
3430 static const struct dev_pm_ops ibmvnic_pm_ops = {
3431 	.resume = ibmvnic_resume
3432 };
3433 
3434 static struct vio_driver ibmvnic_driver = {
3435 	.id_table       = ibmvnic_device_table,
3436 	.probe          = ibmvnic_probe,
3437 	.remove         = ibmvnic_remove,
3438 	.get_desired_dma = ibmvnic_get_desired_dma,
3439 	.name		= ibmvnic_driver_name,
3440 	.pm		= &ibmvnic_pm_ops,
3441 };
3442 
3443 /* module functions */
3444 static int __init ibmvnic_module_init(void)
3445 {
3446 	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
3447 		IBMVNIC_DRIVER_VERSION);
3448 
3449 	return vio_register_driver(&ibmvnic_driver);
3450 }
3451 
3452 static void __exit ibmvnic_module_exit(void)
3453 {
3454 	vio_unregister_driver(&ibmvnic_driver);
3455 }
3456 
3457 module_init(ibmvnic_module_init);
3458 module_exit(ibmvnic_module_exit);
3459