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