xref: /openbmc/linux/drivers/net/ethernet/ibm/ibmvnic.c (revision 15d90a6a)
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 	rc = init_crq_queue(adapter);
1889 	if (rc) {
1890 		netdev_err(adapter->netdev,
1891 			   "Couldn't initialize crq. rc=%d\n", rc);
1892 		return rc;
1893 	}
1894 
1895 	rc = ibmvnic_init(adapter);
1896 	if (rc)
1897 		return rc;
1898 
1899 	/* If the adapter was in PROBE state prior to the reset,
1900 	 * exit here.
1901 	 */
1902 	if (reset_state == VNIC_PROBED)
1903 		return 0;
1904 
1905 	rc = ibmvnic_login(netdev);
1906 	if (rc) {
1907 		adapter->state = VNIC_PROBED;
1908 		return 0;
1909 	}
1910 
1911 	rc = init_resources(adapter);
1912 	if (rc)
1913 		return rc;
1914 
1915 	ibmvnic_disable_irqs(adapter);
1916 	adapter->state = VNIC_CLOSED;
1917 
1918 	if (reset_state == VNIC_CLOSED)
1919 		return 0;
1920 
1921 	rc = __ibmvnic_open(netdev);
1922 	if (rc) {
1923 		if (list_empty(&adapter->rwi_list))
1924 			adapter->state = VNIC_CLOSED;
1925 		else
1926 			adapter->state = reset_state;
1927 
1928 		return 0;
1929 	}
1930 
1931 	netif_carrier_on(netdev);
1932 
1933 	return 0;
1934 }
1935 
1936 static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1937 {
1938 	struct ibmvnic_rwi *rwi;
1939 	unsigned long flags;
1940 
1941 	spin_lock_irqsave(&adapter->rwi_lock, flags);
1942 
1943 	if (!list_empty(&adapter->rwi_list)) {
1944 		rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1945 				       list);
1946 		list_del(&rwi->list);
1947 	} else {
1948 		rwi = NULL;
1949 	}
1950 
1951 	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
1952 	return rwi;
1953 }
1954 
1955 static void free_all_rwi(struct ibmvnic_adapter *adapter)
1956 {
1957 	struct ibmvnic_rwi *rwi;
1958 
1959 	rwi = get_next_rwi(adapter);
1960 	while (rwi) {
1961 		kfree(rwi);
1962 		rwi = get_next_rwi(adapter);
1963 	}
1964 }
1965 
1966 static void __ibmvnic_reset(struct work_struct *work)
1967 {
1968 	struct ibmvnic_rwi *rwi;
1969 	struct ibmvnic_adapter *adapter;
1970 	struct net_device *netdev;
1971 	bool we_lock_rtnl = false;
1972 	u32 reset_state;
1973 	int rc = 0;
1974 
1975 	adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1976 	netdev = adapter->netdev;
1977 
1978 	/* netif_set_real_num_xx_queues needs to take rtnl lock here
1979 	 * unless wait_for_reset is set, in which case the rtnl lock
1980 	 * has already been taken before initializing the reset
1981 	 */
1982 	if (!adapter->wait_for_reset) {
1983 		rtnl_lock();
1984 		we_lock_rtnl = true;
1985 	}
1986 	reset_state = adapter->state;
1987 
1988 	rwi = get_next_rwi(adapter);
1989 	while (rwi) {
1990 		if (adapter->force_reset_recovery) {
1991 			adapter->force_reset_recovery = false;
1992 			rc = do_hard_reset(adapter, rwi, reset_state);
1993 		} else {
1994 			rc = do_reset(adapter, rwi, reset_state);
1995 		}
1996 		kfree(rwi);
1997 		if (rc && rc != IBMVNIC_INIT_FAILED &&
1998 		    !adapter->force_reset_recovery)
1999 			break;
2000 
2001 		rwi = get_next_rwi(adapter);
2002 	}
2003 
2004 	if (adapter->wait_for_reset) {
2005 		adapter->wait_for_reset = false;
2006 		adapter->reset_done_rc = rc;
2007 		complete(&adapter->reset_done);
2008 	}
2009 
2010 	if (rc) {
2011 		netdev_dbg(adapter->netdev, "Reset failed\n");
2012 		free_all_rwi(adapter);
2013 	}
2014 
2015 	adapter->resetting = false;
2016 	if (we_lock_rtnl)
2017 		rtnl_unlock();
2018 }
2019 
2020 static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
2021 			 enum ibmvnic_reset_reason reason)
2022 {
2023 	struct list_head *entry, *tmp_entry;
2024 	struct ibmvnic_rwi *rwi, *tmp;
2025 	struct net_device *netdev = adapter->netdev;
2026 	unsigned long flags;
2027 	int ret;
2028 
2029 	if (adapter->state == VNIC_REMOVING ||
2030 	    adapter->state == VNIC_REMOVED ||
2031 	    adapter->failover_pending) {
2032 		ret = EBUSY;
2033 		netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
2034 		goto err;
2035 	}
2036 
2037 	if (adapter->state == VNIC_PROBING) {
2038 		netdev_warn(netdev, "Adapter reset during probe\n");
2039 		ret = adapter->init_done_rc = EAGAIN;
2040 		goto err;
2041 	}
2042 
2043 	spin_lock_irqsave(&adapter->rwi_lock, flags);
2044 
2045 	list_for_each(entry, &adapter->rwi_list) {
2046 		tmp = list_entry(entry, struct ibmvnic_rwi, list);
2047 		if (tmp->reset_reason == reason) {
2048 			netdev_dbg(netdev, "Skipping matching reset\n");
2049 			spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2050 			ret = EBUSY;
2051 			goto err;
2052 		}
2053 	}
2054 
2055 	rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
2056 	if (!rwi) {
2057 		spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2058 		ibmvnic_close(netdev);
2059 		ret = ENOMEM;
2060 		goto err;
2061 	}
2062 	/* if we just received a transport event,
2063 	 * flush reset queue and process this reset
2064 	 */
2065 	if (adapter->force_reset_recovery && !list_empty(&adapter->rwi_list)) {
2066 		list_for_each_safe(entry, tmp_entry, &adapter->rwi_list)
2067 			list_del(entry);
2068 	}
2069 	rwi->reset_reason = reason;
2070 	list_add_tail(&rwi->list, &adapter->rwi_list);
2071 	spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2072 	adapter->resetting = true;
2073 	netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
2074 	schedule_work(&adapter->ibmvnic_reset);
2075 
2076 	return 0;
2077 err:
2078 	if (adapter->wait_for_reset)
2079 		adapter->wait_for_reset = false;
2080 	return -ret;
2081 }
2082 
2083 static void ibmvnic_tx_timeout(struct net_device *dev)
2084 {
2085 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2086 
2087 	ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
2088 }
2089 
2090 static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
2091 				  struct ibmvnic_rx_buff *rx_buff)
2092 {
2093 	struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
2094 
2095 	rx_buff->skb = NULL;
2096 
2097 	pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2098 	pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2099 
2100 	atomic_dec(&pool->available);
2101 }
2102 
2103 static int ibmvnic_poll(struct napi_struct *napi, int budget)
2104 {
2105 	struct net_device *netdev = napi->dev;
2106 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2107 	int scrq_num = (int)(napi - adapter->napi);
2108 	int frames_processed = 0;
2109 
2110 restart_poll:
2111 	while (frames_processed < budget) {
2112 		struct sk_buff *skb;
2113 		struct ibmvnic_rx_buff *rx_buff;
2114 		union sub_crq *next;
2115 		u32 length;
2116 		u16 offset;
2117 		u8 flags = 0;
2118 
2119 		if (unlikely(adapter->resetting &&
2120 			     adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2121 			enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2122 			napi_complete_done(napi, frames_processed);
2123 			return frames_processed;
2124 		}
2125 
2126 		if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2127 			break;
2128 		next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2129 		rx_buff =
2130 		    (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2131 							  rx_comp.correlator);
2132 		/* do error checking */
2133 		if (next->rx_comp.rc) {
2134 			netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2135 				   be16_to_cpu(next->rx_comp.rc));
2136 			/* free the entry */
2137 			next->rx_comp.first = 0;
2138 			dev_kfree_skb_any(rx_buff->skb);
2139 			remove_buff_from_pool(adapter, rx_buff);
2140 			continue;
2141 		} else if (!rx_buff->skb) {
2142 			/* free the entry */
2143 			next->rx_comp.first = 0;
2144 			remove_buff_from_pool(adapter, rx_buff);
2145 			continue;
2146 		}
2147 
2148 		length = be32_to_cpu(next->rx_comp.len);
2149 		offset = be16_to_cpu(next->rx_comp.off_frame_data);
2150 		flags = next->rx_comp.flags;
2151 		skb = rx_buff->skb;
2152 		skb_copy_to_linear_data(skb, rx_buff->data + offset,
2153 					length);
2154 
2155 		/* VLAN Header has been stripped by the system firmware and
2156 		 * needs to be inserted by the driver
2157 		 */
2158 		if (adapter->rx_vlan_header_insertion &&
2159 		    (flags & IBMVNIC_VLAN_STRIPPED))
2160 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2161 					       ntohs(next->rx_comp.vlan_tci));
2162 
2163 		/* free the entry */
2164 		next->rx_comp.first = 0;
2165 		remove_buff_from_pool(adapter, rx_buff);
2166 
2167 		skb_put(skb, length);
2168 		skb->protocol = eth_type_trans(skb, netdev);
2169 		skb_record_rx_queue(skb, scrq_num);
2170 
2171 		if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2172 		    flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2173 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2174 		}
2175 
2176 		length = skb->len;
2177 		napi_gro_receive(napi, skb); /* send it up */
2178 		netdev->stats.rx_packets++;
2179 		netdev->stats.rx_bytes += length;
2180 		adapter->rx_stats_buffers[scrq_num].packets++;
2181 		adapter->rx_stats_buffers[scrq_num].bytes += length;
2182 		frames_processed++;
2183 	}
2184 
2185 	if (adapter->state != VNIC_CLOSING)
2186 		replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2187 
2188 	if (frames_processed < budget) {
2189 		enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2190 		napi_complete_done(napi, frames_processed);
2191 		if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2192 		    napi_reschedule(napi)) {
2193 			disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2194 			goto restart_poll;
2195 		}
2196 	}
2197 	return frames_processed;
2198 }
2199 
2200 static int wait_for_reset(struct ibmvnic_adapter *adapter)
2201 {
2202 	int rc, ret;
2203 
2204 	adapter->fallback.mtu = adapter->req_mtu;
2205 	adapter->fallback.rx_queues = adapter->req_rx_queues;
2206 	adapter->fallback.tx_queues = adapter->req_tx_queues;
2207 	adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2208 	adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2209 
2210 	init_completion(&adapter->reset_done);
2211 	adapter->wait_for_reset = true;
2212 	rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2213 	if (rc)
2214 		return rc;
2215 	wait_for_completion(&adapter->reset_done);
2216 
2217 	ret = 0;
2218 	if (adapter->reset_done_rc) {
2219 		ret = -EIO;
2220 		adapter->desired.mtu = adapter->fallback.mtu;
2221 		adapter->desired.rx_queues = adapter->fallback.rx_queues;
2222 		adapter->desired.tx_queues = adapter->fallback.tx_queues;
2223 		adapter->desired.rx_entries = adapter->fallback.rx_entries;
2224 		adapter->desired.tx_entries = adapter->fallback.tx_entries;
2225 
2226 		init_completion(&adapter->reset_done);
2227 		adapter->wait_for_reset = true;
2228 		rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2229 		if (rc)
2230 			return ret;
2231 		wait_for_completion(&adapter->reset_done);
2232 	}
2233 	adapter->wait_for_reset = false;
2234 
2235 	return ret;
2236 }
2237 
2238 static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2239 {
2240 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2241 
2242 	adapter->desired.mtu = new_mtu + ETH_HLEN;
2243 
2244 	return wait_for_reset(adapter);
2245 }
2246 
2247 static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2248 						struct net_device *dev,
2249 						netdev_features_t features)
2250 {
2251 	/* Some backing hardware adapters can not
2252 	 * handle packets with a MSS less than 224
2253 	 * or with only one segment.
2254 	 */
2255 	if (skb_is_gso(skb)) {
2256 		if (skb_shinfo(skb)->gso_size < 224 ||
2257 		    skb_shinfo(skb)->gso_segs == 1)
2258 			features &= ~NETIF_F_GSO_MASK;
2259 	}
2260 
2261 	return features;
2262 }
2263 
2264 static const struct net_device_ops ibmvnic_netdev_ops = {
2265 	.ndo_open		= ibmvnic_open,
2266 	.ndo_stop		= ibmvnic_close,
2267 	.ndo_start_xmit		= ibmvnic_xmit,
2268 	.ndo_set_rx_mode	= ibmvnic_set_multi,
2269 	.ndo_set_mac_address	= ibmvnic_set_mac,
2270 	.ndo_validate_addr	= eth_validate_addr,
2271 	.ndo_tx_timeout		= ibmvnic_tx_timeout,
2272 	.ndo_change_mtu		= ibmvnic_change_mtu,
2273 	.ndo_features_check     = ibmvnic_features_check,
2274 };
2275 
2276 /* ethtool functions */
2277 
2278 static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2279 				      struct ethtool_link_ksettings *cmd)
2280 {
2281 	u32 supported, advertising;
2282 
2283 	supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2284 			  SUPPORTED_FIBRE);
2285 	advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2286 			    ADVERTISED_FIBRE);
2287 	cmd->base.speed = SPEED_1000;
2288 	cmd->base.duplex = DUPLEX_FULL;
2289 	cmd->base.port = PORT_FIBRE;
2290 	cmd->base.phy_address = 0;
2291 	cmd->base.autoneg = AUTONEG_ENABLE;
2292 
2293 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2294 						supported);
2295 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2296 						advertising);
2297 
2298 	return 0;
2299 }
2300 
2301 static void ibmvnic_get_drvinfo(struct net_device *netdev,
2302 				struct ethtool_drvinfo *info)
2303 {
2304 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2305 
2306 	strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2307 	strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2308 	strlcpy(info->fw_version, adapter->fw_version,
2309 		sizeof(info->fw_version));
2310 }
2311 
2312 static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2313 {
2314 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2315 
2316 	return adapter->msg_enable;
2317 }
2318 
2319 static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2320 {
2321 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2322 
2323 	adapter->msg_enable = data;
2324 }
2325 
2326 static u32 ibmvnic_get_link(struct net_device *netdev)
2327 {
2328 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2329 
2330 	/* Don't need to send a query because we request a logical link up at
2331 	 * init and then we wait for link state indications
2332 	 */
2333 	return adapter->logical_link_state;
2334 }
2335 
2336 static void ibmvnic_get_ringparam(struct net_device *netdev,
2337 				  struct ethtool_ringparam *ring)
2338 {
2339 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2340 
2341 	if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2342 		ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2343 		ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2344 	} else {
2345 		ring->rx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2346 		ring->tx_max_pending = IBMVNIC_MAX_QUEUE_SZ;
2347 	}
2348 	ring->rx_mini_max_pending = 0;
2349 	ring->rx_jumbo_max_pending = 0;
2350 	ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2351 	ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2352 	ring->rx_mini_pending = 0;
2353 	ring->rx_jumbo_pending = 0;
2354 }
2355 
2356 static int ibmvnic_set_ringparam(struct net_device *netdev,
2357 				 struct ethtool_ringparam *ring)
2358 {
2359 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2360 	int ret;
2361 
2362 	ret = 0;
2363 	adapter->desired.rx_entries = ring->rx_pending;
2364 	adapter->desired.tx_entries = ring->tx_pending;
2365 
2366 	ret = wait_for_reset(adapter);
2367 
2368 	if (!ret &&
2369 	    (adapter->req_rx_add_entries_per_subcrq != ring->rx_pending ||
2370 	     adapter->req_tx_entries_per_subcrq != ring->tx_pending))
2371 		netdev_info(netdev,
2372 			    "Could not match full ringsize request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2373 			    ring->rx_pending, ring->tx_pending,
2374 			    adapter->req_rx_add_entries_per_subcrq,
2375 			    adapter->req_tx_entries_per_subcrq);
2376 	return ret;
2377 }
2378 
2379 static void ibmvnic_get_channels(struct net_device *netdev,
2380 				 struct ethtool_channels *channels)
2381 {
2382 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2383 
2384 	if (adapter->priv_flags & IBMVNIC_USE_SERVER_MAXES) {
2385 		channels->max_rx = adapter->max_rx_queues;
2386 		channels->max_tx = adapter->max_tx_queues;
2387 	} else {
2388 		channels->max_rx = IBMVNIC_MAX_QUEUES;
2389 		channels->max_tx = IBMVNIC_MAX_QUEUES;
2390 	}
2391 
2392 	channels->max_other = 0;
2393 	channels->max_combined = 0;
2394 	channels->rx_count = adapter->req_rx_queues;
2395 	channels->tx_count = adapter->req_tx_queues;
2396 	channels->other_count = 0;
2397 	channels->combined_count = 0;
2398 }
2399 
2400 static int ibmvnic_set_channels(struct net_device *netdev,
2401 				struct ethtool_channels *channels)
2402 {
2403 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2404 	int ret;
2405 
2406 	ret = 0;
2407 	adapter->desired.rx_queues = channels->rx_count;
2408 	adapter->desired.tx_queues = channels->tx_count;
2409 
2410 	ret = wait_for_reset(adapter);
2411 
2412 	if (!ret &&
2413 	    (adapter->req_rx_queues != channels->rx_count ||
2414 	     adapter->req_tx_queues != channels->tx_count))
2415 		netdev_info(netdev,
2416 			    "Could not match full channels request. Requested: RX %d, TX %d; Allowed: RX %llu, TX %llu\n",
2417 			    channels->rx_count, channels->tx_count,
2418 			    adapter->req_rx_queues, adapter->req_tx_queues);
2419 	return ret;
2420 
2421 }
2422 
2423 static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2424 {
2425 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2426 	int i;
2427 
2428 	switch (stringset) {
2429 	case ETH_SS_STATS:
2430 		for (i = 0; i < ARRAY_SIZE(ibmvnic_stats);
2431 				i++, data += ETH_GSTRING_LEN)
2432 			memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2433 
2434 		for (i = 0; i < adapter->req_tx_queues; i++) {
2435 			snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2436 			data += ETH_GSTRING_LEN;
2437 
2438 			snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2439 			data += ETH_GSTRING_LEN;
2440 
2441 			snprintf(data, ETH_GSTRING_LEN,
2442 				 "tx%d_dropped_packets", i);
2443 			data += ETH_GSTRING_LEN;
2444 		}
2445 
2446 		for (i = 0; i < adapter->req_rx_queues; i++) {
2447 			snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2448 			data += ETH_GSTRING_LEN;
2449 
2450 			snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2451 			data += ETH_GSTRING_LEN;
2452 
2453 			snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2454 			data += ETH_GSTRING_LEN;
2455 		}
2456 		break;
2457 
2458 	case ETH_SS_PRIV_FLAGS:
2459 		for (i = 0; i < ARRAY_SIZE(ibmvnic_priv_flags); i++)
2460 			strcpy(data + i * ETH_GSTRING_LEN,
2461 			       ibmvnic_priv_flags[i]);
2462 		break;
2463 	default:
2464 		return;
2465 	}
2466 }
2467 
2468 static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2469 {
2470 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2471 
2472 	switch (sset) {
2473 	case ETH_SS_STATS:
2474 		return ARRAY_SIZE(ibmvnic_stats) +
2475 		       adapter->req_tx_queues * NUM_TX_STATS +
2476 		       adapter->req_rx_queues * NUM_RX_STATS;
2477 	case ETH_SS_PRIV_FLAGS:
2478 		return ARRAY_SIZE(ibmvnic_priv_flags);
2479 	default:
2480 		return -EOPNOTSUPP;
2481 	}
2482 }
2483 
2484 static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2485 				      struct ethtool_stats *stats, u64 *data)
2486 {
2487 	struct ibmvnic_adapter *adapter = netdev_priv(dev);
2488 	union ibmvnic_crq crq;
2489 	int i, j;
2490 	int rc;
2491 
2492 	memset(&crq, 0, sizeof(crq));
2493 	crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2494 	crq.request_statistics.cmd = REQUEST_STATISTICS;
2495 	crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2496 	crq.request_statistics.len =
2497 	    cpu_to_be32(sizeof(struct ibmvnic_statistics));
2498 
2499 	/* Wait for data to be written */
2500 	init_completion(&adapter->stats_done);
2501 	rc = ibmvnic_send_crq(adapter, &crq);
2502 	if (rc)
2503 		return;
2504 	wait_for_completion(&adapter->stats_done);
2505 
2506 	for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2507 		data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2508 						ibmvnic_stats[i].offset));
2509 
2510 	for (j = 0; j < adapter->req_tx_queues; j++) {
2511 		data[i] = adapter->tx_stats_buffers[j].packets;
2512 		i++;
2513 		data[i] = adapter->tx_stats_buffers[j].bytes;
2514 		i++;
2515 		data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2516 		i++;
2517 	}
2518 
2519 	for (j = 0; j < adapter->req_rx_queues; j++) {
2520 		data[i] = adapter->rx_stats_buffers[j].packets;
2521 		i++;
2522 		data[i] = adapter->rx_stats_buffers[j].bytes;
2523 		i++;
2524 		data[i] = adapter->rx_stats_buffers[j].interrupts;
2525 		i++;
2526 	}
2527 }
2528 
2529 static u32 ibmvnic_get_priv_flags(struct net_device *netdev)
2530 {
2531 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2532 
2533 	return adapter->priv_flags;
2534 }
2535 
2536 static int ibmvnic_set_priv_flags(struct net_device *netdev, u32 flags)
2537 {
2538 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2539 	bool which_maxes = !!(flags & IBMVNIC_USE_SERVER_MAXES);
2540 
2541 	if (which_maxes)
2542 		adapter->priv_flags |= IBMVNIC_USE_SERVER_MAXES;
2543 	else
2544 		adapter->priv_flags &= ~IBMVNIC_USE_SERVER_MAXES;
2545 
2546 	return 0;
2547 }
2548 static const struct ethtool_ops ibmvnic_ethtool_ops = {
2549 	.get_drvinfo		= ibmvnic_get_drvinfo,
2550 	.get_msglevel		= ibmvnic_get_msglevel,
2551 	.set_msglevel		= ibmvnic_set_msglevel,
2552 	.get_link		= ibmvnic_get_link,
2553 	.get_ringparam		= ibmvnic_get_ringparam,
2554 	.set_ringparam		= ibmvnic_set_ringparam,
2555 	.get_channels		= ibmvnic_get_channels,
2556 	.set_channels		= ibmvnic_set_channels,
2557 	.get_strings            = ibmvnic_get_strings,
2558 	.get_sset_count         = ibmvnic_get_sset_count,
2559 	.get_ethtool_stats	= ibmvnic_get_ethtool_stats,
2560 	.get_link_ksettings	= ibmvnic_get_link_ksettings,
2561 	.get_priv_flags		= ibmvnic_get_priv_flags,
2562 	.set_priv_flags		= ibmvnic_set_priv_flags,
2563 };
2564 
2565 /* Routines for managing CRQs/sCRQs  */
2566 
2567 static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2568 				   struct ibmvnic_sub_crq_queue *scrq)
2569 {
2570 	int rc;
2571 
2572 	if (scrq->irq) {
2573 		free_irq(scrq->irq, scrq);
2574 		irq_dispose_mapping(scrq->irq);
2575 		scrq->irq = 0;
2576 	}
2577 
2578 	memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2579 	atomic_set(&scrq->used, 0);
2580 	scrq->cur = 0;
2581 
2582 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2583 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2584 	return rc;
2585 }
2586 
2587 static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2588 {
2589 	int i, rc;
2590 
2591 	for (i = 0; i < adapter->req_tx_queues; i++) {
2592 		netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2593 		rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2594 		if (rc)
2595 			return rc;
2596 	}
2597 
2598 	for (i = 0; i < adapter->req_rx_queues; i++) {
2599 		netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2600 		rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2601 		if (rc)
2602 			return rc;
2603 	}
2604 
2605 	return rc;
2606 }
2607 
2608 static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2609 				  struct ibmvnic_sub_crq_queue *scrq,
2610 				  bool do_h_free)
2611 {
2612 	struct device *dev = &adapter->vdev->dev;
2613 	long rc;
2614 
2615 	netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2616 
2617 	if (do_h_free) {
2618 		/* Close the sub-crqs */
2619 		do {
2620 			rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2621 						adapter->vdev->unit_address,
2622 						scrq->crq_num);
2623 		} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2624 
2625 		if (rc) {
2626 			netdev_err(adapter->netdev,
2627 				   "Failed to release sub-CRQ %16lx, rc = %ld\n",
2628 				   scrq->crq_num, rc);
2629 		}
2630 	}
2631 
2632 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2633 			 DMA_BIDIRECTIONAL);
2634 	free_pages((unsigned long)scrq->msgs, 2);
2635 	kfree(scrq);
2636 }
2637 
2638 static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2639 							*adapter)
2640 {
2641 	struct device *dev = &adapter->vdev->dev;
2642 	struct ibmvnic_sub_crq_queue *scrq;
2643 	int rc;
2644 
2645 	scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2646 	if (!scrq)
2647 		return NULL;
2648 
2649 	scrq->msgs =
2650 		(union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2651 	if (!scrq->msgs) {
2652 		dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2653 		goto zero_page_failed;
2654 	}
2655 
2656 	scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2657 					 DMA_BIDIRECTIONAL);
2658 	if (dma_mapping_error(dev, scrq->msg_token)) {
2659 		dev_warn(dev, "Couldn't map crq queue messages page\n");
2660 		goto map_failed;
2661 	}
2662 
2663 	rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2664 			   4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2665 
2666 	if (rc == H_RESOURCE)
2667 		rc = ibmvnic_reset_crq(adapter);
2668 
2669 	if (rc == H_CLOSED) {
2670 		dev_warn(dev, "Partner adapter not ready, waiting.\n");
2671 	} else if (rc) {
2672 		dev_warn(dev, "Error %d registering sub-crq\n", rc);
2673 		goto reg_failed;
2674 	}
2675 
2676 	scrq->adapter = adapter;
2677 	scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2678 	spin_lock_init(&scrq->lock);
2679 
2680 	netdev_dbg(adapter->netdev,
2681 		   "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2682 		   scrq->crq_num, scrq->hw_irq, scrq->irq);
2683 
2684 	return scrq;
2685 
2686 reg_failed:
2687 	dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2688 			 DMA_BIDIRECTIONAL);
2689 map_failed:
2690 	free_pages((unsigned long)scrq->msgs, 2);
2691 zero_page_failed:
2692 	kfree(scrq);
2693 
2694 	return NULL;
2695 }
2696 
2697 static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2698 {
2699 	int i;
2700 
2701 	if (adapter->tx_scrq) {
2702 		for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2703 			if (!adapter->tx_scrq[i])
2704 				continue;
2705 
2706 			netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2707 				   i);
2708 			if (adapter->tx_scrq[i]->irq) {
2709 				free_irq(adapter->tx_scrq[i]->irq,
2710 					 adapter->tx_scrq[i]);
2711 				irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2712 				adapter->tx_scrq[i]->irq = 0;
2713 			}
2714 
2715 			release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2716 					      do_h_free);
2717 		}
2718 
2719 		kfree(adapter->tx_scrq);
2720 		adapter->tx_scrq = NULL;
2721 		adapter->num_active_tx_scrqs = 0;
2722 	}
2723 
2724 	if (adapter->rx_scrq) {
2725 		for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2726 			if (!adapter->rx_scrq[i])
2727 				continue;
2728 
2729 			netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2730 				   i);
2731 			if (adapter->rx_scrq[i]->irq) {
2732 				free_irq(adapter->rx_scrq[i]->irq,
2733 					 adapter->rx_scrq[i]);
2734 				irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2735 				adapter->rx_scrq[i]->irq = 0;
2736 			}
2737 
2738 			release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2739 					      do_h_free);
2740 		}
2741 
2742 		kfree(adapter->rx_scrq);
2743 		adapter->rx_scrq = NULL;
2744 		adapter->num_active_rx_scrqs = 0;
2745 	}
2746 }
2747 
2748 static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2749 			    struct ibmvnic_sub_crq_queue *scrq)
2750 {
2751 	struct device *dev = &adapter->vdev->dev;
2752 	unsigned long rc;
2753 
2754 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2755 				H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2756 	if (rc)
2757 		dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2758 			scrq->hw_irq, rc);
2759 	return rc;
2760 }
2761 
2762 static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2763 			   struct ibmvnic_sub_crq_queue *scrq)
2764 {
2765 	struct device *dev = &adapter->vdev->dev;
2766 	unsigned long rc;
2767 
2768 	if (scrq->hw_irq > 0x100000000ULL) {
2769 		dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2770 		return 1;
2771 	}
2772 
2773 	if (adapter->resetting &&
2774 	    adapter->reset_reason == VNIC_RESET_MOBILITY) {
2775 		u64 val = (0xff000000) | scrq->hw_irq;
2776 
2777 		rc = plpar_hcall_norets(H_EOI, val);
2778 		if (rc)
2779 			dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2780 				val, rc);
2781 	}
2782 
2783 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2784 				H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2785 	if (rc)
2786 		dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2787 			scrq->hw_irq, rc);
2788 	return rc;
2789 }
2790 
2791 static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2792 			       struct ibmvnic_sub_crq_queue *scrq)
2793 {
2794 	struct device *dev = &adapter->vdev->dev;
2795 	struct ibmvnic_tx_pool *tx_pool;
2796 	struct ibmvnic_tx_buff *txbuff;
2797 	union sub_crq *next;
2798 	int index;
2799 	int i, j;
2800 	u8 *first;
2801 
2802 restart_loop:
2803 	while (pending_scrq(adapter, scrq)) {
2804 		unsigned int pool = scrq->pool_index;
2805 		int num_entries = 0;
2806 
2807 		next = ibmvnic_next_scrq(adapter, scrq);
2808 		for (i = 0; i < next->tx_comp.num_comps; i++) {
2809 			if (next->tx_comp.rcs[i]) {
2810 				dev_err(dev, "tx error %x\n",
2811 					next->tx_comp.rcs[i]);
2812 				continue;
2813 			}
2814 			index = be32_to_cpu(next->tx_comp.correlators[i]);
2815 			if (index & IBMVNIC_TSO_POOL_MASK) {
2816 				tx_pool = &adapter->tso_pool[pool];
2817 				index &= ~IBMVNIC_TSO_POOL_MASK;
2818 			} else {
2819 				tx_pool = &adapter->tx_pool[pool];
2820 			}
2821 
2822 			txbuff = &tx_pool->tx_buff[index];
2823 
2824 			for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2825 				if (!txbuff->data_dma[j])
2826 					continue;
2827 
2828 				txbuff->data_dma[j] = 0;
2829 			}
2830 			/* if sub_crq was sent indirectly */
2831 			first = &txbuff->indir_arr[0].generic.first;
2832 			if (*first == IBMVNIC_CRQ_CMD) {
2833 				dma_unmap_single(dev, txbuff->indir_dma,
2834 						 sizeof(txbuff->indir_arr),
2835 						 DMA_TO_DEVICE);
2836 				*first = 0;
2837 			}
2838 
2839 			if (txbuff->last_frag) {
2840 				dev_kfree_skb_any(txbuff->skb);
2841 				txbuff->skb = NULL;
2842 			}
2843 
2844 			num_entries += txbuff->num_entries;
2845 
2846 			tx_pool->free_map[tx_pool->producer_index] = index;
2847 			tx_pool->producer_index =
2848 				(tx_pool->producer_index + 1) %
2849 					tx_pool->num_buffers;
2850 		}
2851 		/* remove tx_comp scrq*/
2852 		next->tx_comp.first = 0;
2853 
2854 		if (atomic_sub_return(num_entries, &scrq->used) <=
2855 		    (adapter->req_tx_entries_per_subcrq / 2) &&
2856 		    __netif_subqueue_stopped(adapter->netdev,
2857 					     scrq->pool_index)) {
2858 			netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2859 			netdev_dbg(adapter->netdev, "Started queue %d\n",
2860 				   scrq->pool_index);
2861 		}
2862 	}
2863 
2864 	enable_scrq_irq(adapter, scrq);
2865 
2866 	if (pending_scrq(adapter, scrq)) {
2867 		disable_scrq_irq(adapter, scrq);
2868 		goto restart_loop;
2869 	}
2870 
2871 	return 0;
2872 }
2873 
2874 static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2875 {
2876 	struct ibmvnic_sub_crq_queue *scrq = instance;
2877 	struct ibmvnic_adapter *adapter = scrq->adapter;
2878 
2879 	disable_scrq_irq(adapter, scrq);
2880 	ibmvnic_complete_tx(adapter, scrq);
2881 
2882 	return IRQ_HANDLED;
2883 }
2884 
2885 static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2886 {
2887 	struct ibmvnic_sub_crq_queue *scrq = instance;
2888 	struct ibmvnic_adapter *adapter = scrq->adapter;
2889 
2890 	/* When booting a kdump kernel we can hit pending interrupts
2891 	 * prior to completing driver initialization.
2892 	 */
2893 	if (unlikely(adapter->state != VNIC_OPEN))
2894 		return IRQ_NONE;
2895 
2896 	adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2897 
2898 	if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2899 		disable_scrq_irq(adapter, scrq);
2900 		__napi_schedule(&adapter->napi[scrq->scrq_num]);
2901 	}
2902 
2903 	return IRQ_HANDLED;
2904 }
2905 
2906 static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2907 {
2908 	struct device *dev = &adapter->vdev->dev;
2909 	struct ibmvnic_sub_crq_queue *scrq;
2910 	int i = 0, j = 0;
2911 	int rc = 0;
2912 
2913 	for (i = 0; i < adapter->req_tx_queues; i++) {
2914 		netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2915 			   i);
2916 		scrq = adapter->tx_scrq[i];
2917 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2918 
2919 		if (!scrq->irq) {
2920 			rc = -EINVAL;
2921 			dev_err(dev, "Error mapping irq\n");
2922 			goto req_tx_irq_failed;
2923 		}
2924 
2925 		rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2926 				 0, "ibmvnic_tx", scrq);
2927 
2928 		if (rc) {
2929 			dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2930 				scrq->irq, rc);
2931 			irq_dispose_mapping(scrq->irq);
2932 			goto req_tx_irq_failed;
2933 		}
2934 	}
2935 
2936 	for (i = 0; i < adapter->req_rx_queues; i++) {
2937 		netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2938 			   i);
2939 		scrq = adapter->rx_scrq[i];
2940 		scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2941 		if (!scrq->irq) {
2942 			rc = -EINVAL;
2943 			dev_err(dev, "Error mapping irq\n");
2944 			goto req_rx_irq_failed;
2945 		}
2946 		rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2947 				 0, "ibmvnic_rx", scrq);
2948 		if (rc) {
2949 			dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2950 				scrq->irq, rc);
2951 			irq_dispose_mapping(scrq->irq);
2952 			goto req_rx_irq_failed;
2953 		}
2954 	}
2955 	return rc;
2956 
2957 req_rx_irq_failed:
2958 	for (j = 0; j < i; j++) {
2959 		free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2960 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2961 	}
2962 	i = adapter->req_tx_queues;
2963 req_tx_irq_failed:
2964 	for (j = 0; j < i; j++) {
2965 		free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2966 		irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2967 	}
2968 	release_sub_crqs(adapter, 1);
2969 	return rc;
2970 }
2971 
2972 static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2973 {
2974 	struct device *dev = &adapter->vdev->dev;
2975 	struct ibmvnic_sub_crq_queue **allqueues;
2976 	int registered_queues = 0;
2977 	int total_queues;
2978 	int more = 0;
2979 	int i;
2980 
2981 	total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2982 
2983 	allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2984 	if (!allqueues)
2985 		return -1;
2986 
2987 	for (i = 0; i < total_queues; i++) {
2988 		allqueues[i] = init_sub_crq_queue(adapter);
2989 		if (!allqueues[i]) {
2990 			dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2991 			break;
2992 		}
2993 		registered_queues++;
2994 	}
2995 
2996 	/* Make sure we were able to register the minimum number of queues */
2997 	if (registered_queues <
2998 	    adapter->min_tx_queues + adapter->min_rx_queues) {
2999 		dev_err(dev, "Fatal: Couldn't init  min number of sub-crqs\n");
3000 		goto tx_failed;
3001 	}
3002 
3003 	/* Distribute the failed allocated queues*/
3004 	for (i = 0; i < total_queues - registered_queues + more ; i++) {
3005 		netdev_dbg(adapter->netdev, "Reducing number of queues\n");
3006 		switch (i % 3) {
3007 		case 0:
3008 			if (adapter->req_rx_queues > adapter->min_rx_queues)
3009 				adapter->req_rx_queues--;
3010 			else
3011 				more++;
3012 			break;
3013 		case 1:
3014 			if (adapter->req_tx_queues > adapter->min_tx_queues)
3015 				adapter->req_tx_queues--;
3016 			else
3017 				more++;
3018 			break;
3019 		}
3020 	}
3021 
3022 	adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
3023 				   sizeof(*adapter->tx_scrq), GFP_KERNEL);
3024 	if (!adapter->tx_scrq)
3025 		goto tx_failed;
3026 
3027 	for (i = 0; i < adapter->req_tx_queues; i++) {
3028 		adapter->tx_scrq[i] = allqueues[i];
3029 		adapter->tx_scrq[i]->pool_index = i;
3030 		adapter->num_active_tx_scrqs++;
3031 	}
3032 
3033 	adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
3034 				   sizeof(*adapter->rx_scrq), GFP_KERNEL);
3035 	if (!adapter->rx_scrq)
3036 		goto rx_failed;
3037 
3038 	for (i = 0; i < adapter->req_rx_queues; i++) {
3039 		adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
3040 		adapter->rx_scrq[i]->scrq_num = i;
3041 		adapter->num_active_rx_scrqs++;
3042 	}
3043 
3044 	kfree(allqueues);
3045 	return 0;
3046 
3047 rx_failed:
3048 	kfree(adapter->tx_scrq);
3049 	adapter->tx_scrq = NULL;
3050 tx_failed:
3051 	for (i = 0; i < registered_queues; i++)
3052 		release_sub_crq_queue(adapter, allqueues[i], 1);
3053 	kfree(allqueues);
3054 	return -1;
3055 }
3056 
3057 static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
3058 {
3059 	struct device *dev = &adapter->vdev->dev;
3060 	union ibmvnic_crq crq;
3061 	int max_entries;
3062 
3063 	if (!retry) {
3064 		/* Sub-CRQ entries are 32 byte long */
3065 		int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
3066 
3067 		if (adapter->min_tx_entries_per_subcrq > entries_page ||
3068 		    adapter->min_rx_add_entries_per_subcrq > entries_page) {
3069 			dev_err(dev, "Fatal, invalid entries per sub-crq\n");
3070 			return;
3071 		}
3072 
3073 		if (adapter->desired.mtu)
3074 			adapter->req_mtu = adapter->desired.mtu;
3075 		else
3076 			adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
3077 
3078 		if (!adapter->desired.tx_entries)
3079 			adapter->desired.tx_entries =
3080 					adapter->max_tx_entries_per_subcrq;
3081 		if (!adapter->desired.rx_entries)
3082 			adapter->desired.rx_entries =
3083 					adapter->max_rx_add_entries_per_subcrq;
3084 
3085 		max_entries = IBMVNIC_MAX_LTB_SIZE /
3086 			      (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
3087 
3088 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3089 			adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
3090 			adapter->desired.tx_entries = max_entries;
3091 		}
3092 
3093 		if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
3094 			adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
3095 			adapter->desired.rx_entries = max_entries;
3096 		}
3097 
3098 		if (adapter->desired.tx_entries)
3099 			adapter->req_tx_entries_per_subcrq =
3100 					adapter->desired.tx_entries;
3101 		else
3102 			adapter->req_tx_entries_per_subcrq =
3103 					adapter->max_tx_entries_per_subcrq;
3104 
3105 		if (adapter->desired.rx_entries)
3106 			adapter->req_rx_add_entries_per_subcrq =
3107 					adapter->desired.rx_entries;
3108 		else
3109 			adapter->req_rx_add_entries_per_subcrq =
3110 					adapter->max_rx_add_entries_per_subcrq;
3111 
3112 		if (adapter->desired.tx_queues)
3113 			adapter->req_tx_queues =
3114 					adapter->desired.tx_queues;
3115 		else
3116 			adapter->req_tx_queues =
3117 					adapter->opt_tx_comp_sub_queues;
3118 
3119 		if (adapter->desired.rx_queues)
3120 			adapter->req_rx_queues =
3121 					adapter->desired.rx_queues;
3122 		else
3123 			adapter->req_rx_queues =
3124 					adapter->opt_rx_comp_queues;
3125 
3126 		adapter->req_rx_add_queues = adapter->max_rx_add_queues;
3127 	}
3128 
3129 	memset(&crq, 0, sizeof(crq));
3130 	crq.request_capability.first = IBMVNIC_CRQ_CMD;
3131 	crq.request_capability.cmd = REQUEST_CAPABILITY;
3132 
3133 	crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
3134 	crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
3135 	atomic_inc(&adapter->running_cap_crqs);
3136 	ibmvnic_send_crq(adapter, &crq);
3137 
3138 	crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
3139 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
3140 	atomic_inc(&adapter->running_cap_crqs);
3141 	ibmvnic_send_crq(adapter, &crq);
3142 
3143 	crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3144 	crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3145 	atomic_inc(&adapter->running_cap_crqs);
3146 	ibmvnic_send_crq(adapter, &crq);
3147 
3148 	crq.request_capability.capability =
3149 	    cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3150 	crq.request_capability.number =
3151 	    cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3152 	atomic_inc(&adapter->running_cap_crqs);
3153 	ibmvnic_send_crq(adapter, &crq);
3154 
3155 	crq.request_capability.capability =
3156 	    cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3157 	crq.request_capability.number =
3158 	    cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3159 	atomic_inc(&adapter->running_cap_crqs);
3160 	ibmvnic_send_crq(adapter, &crq);
3161 
3162 	crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3163 	crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3164 	atomic_inc(&adapter->running_cap_crqs);
3165 	ibmvnic_send_crq(adapter, &crq);
3166 
3167 	if (adapter->netdev->flags & IFF_PROMISC) {
3168 		if (adapter->promisc_supported) {
3169 			crq.request_capability.capability =
3170 			    cpu_to_be16(PROMISC_REQUESTED);
3171 			crq.request_capability.number = cpu_to_be64(1);
3172 			atomic_inc(&adapter->running_cap_crqs);
3173 			ibmvnic_send_crq(adapter, &crq);
3174 		}
3175 	} else {
3176 		crq.request_capability.capability =
3177 		    cpu_to_be16(PROMISC_REQUESTED);
3178 		crq.request_capability.number = cpu_to_be64(0);
3179 		atomic_inc(&adapter->running_cap_crqs);
3180 		ibmvnic_send_crq(adapter, &crq);
3181 	}
3182 }
3183 
3184 static int pending_scrq(struct ibmvnic_adapter *adapter,
3185 			struct ibmvnic_sub_crq_queue *scrq)
3186 {
3187 	union sub_crq *entry = &scrq->msgs[scrq->cur];
3188 
3189 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3190 		return 1;
3191 	else
3192 		return 0;
3193 }
3194 
3195 static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3196 					struct ibmvnic_sub_crq_queue *scrq)
3197 {
3198 	union sub_crq *entry;
3199 	unsigned long flags;
3200 
3201 	spin_lock_irqsave(&scrq->lock, flags);
3202 	entry = &scrq->msgs[scrq->cur];
3203 	if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3204 		if (++scrq->cur == scrq->size)
3205 			scrq->cur = 0;
3206 	} else {
3207 		entry = NULL;
3208 	}
3209 	spin_unlock_irqrestore(&scrq->lock, flags);
3210 
3211 	return entry;
3212 }
3213 
3214 static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3215 {
3216 	struct ibmvnic_crq_queue *queue = &adapter->crq;
3217 	union ibmvnic_crq *crq;
3218 
3219 	crq = &queue->msgs[queue->cur];
3220 	if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3221 		if (++queue->cur == queue->size)
3222 			queue->cur = 0;
3223 	} else {
3224 		crq = NULL;
3225 	}
3226 
3227 	return crq;
3228 }
3229 
3230 static void print_subcrq_error(struct device *dev, int rc, const char *func)
3231 {
3232 	switch (rc) {
3233 	case H_PARAMETER:
3234 		dev_warn_ratelimited(dev,
3235 				     "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
3236 				     func, rc);
3237 		break;
3238 	case H_CLOSED:
3239 		dev_warn_ratelimited(dev,
3240 				     "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
3241 				     func, rc);
3242 		break;
3243 	default:
3244 		dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
3245 		break;
3246 	}
3247 }
3248 
3249 static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3250 		       union sub_crq *sub_crq)
3251 {
3252 	unsigned int ua = adapter->vdev->unit_address;
3253 	struct device *dev = &adapter->vdev->dev;
3254 	u64 *u64_crq = (u64 *)sub_crq;
3255 	int rc;
3256 
3257 	netdev_dbg(adapter->netdev,
3258 		   "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3259 		   (unsigned long int)cpu_to_be64(remote_handle),
3260 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
3261 		   (unsigned long int)cpu_to_be64(u64_crq[1]),
3262 		   (unsigned long int)cpu_to_be64(u64_crq[2]),
3263 		   (unsigned long int)cpu_to_be64(u64_crq[3]));
3264 
3265 	/* Make sure the hypervisor sees the complete request */
3266 	mb();
3267 
3268 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3269 				cpu_to_be64(remote_handle),
3270 				cpu_to_be64(u64_crq[0]),
3271 				cpu_to_be64(u64_crq[1]),
3272 				cpu_to_be64(u64_crq[2]),
3273 				cpu_to_be64(u64_crq[3]));
3274 
3275 	if (rc)
3276 		print_subcrq_error(dev, rc, __func__);
3277 
3278 	return rc;
3279 }
3280 
3281 static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3282 				u64 remote_handle, u64 ioba, u64 num_entries)
3283 {
3284 	unsigned int ua = adapter->vdev->unit_address;
3285 	struct device *dev = &adapter->vdev->dev;
3286 	int rc;
3287 
3288 	/* Make sure the hypervisor sees the complete request */
3289 	mb();
3290 	rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3291 				cpu_to_be64(remote_handle),
3292 				ioba, num_entries);
3293 
3294 	if (rc)
3295 		print_subcrq_error(dev, rc, __func__);
3296 
3297 	return rc;
3298 }
3299 
3300 static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3301 			    union ibmvnic_crq *crq)
3302 {
3303 	unsigned int ua = adapter->vdev->unit_address;
3304 	struct device *dev = &adapter->vdev->dev;
3305 	u64 *u64_crq = (u64 *)crq;
3306 	int rc;
3307 
3308 	netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3309 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
3310 		   (unsigned long int)cpu_to_be64(u64_crq[1]));
3311 
3312 	if (!adapter->crq.active &&
3313 	    crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
3314 		dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
3315 		return -EINVAL;
3316 	}
3317 
3318 	/* Make sure the hypervisor sees the complete request */
3319 	mb();
3320 
3321 	rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3322 				cpu_to_be64(u64_crq[0]),
3323 				cpu_to_be64(u64_crq[1]));
3324 
3325 	if (rc) {
3326 		if (rc == H_CLOSED) {
3327 			dev_warn(dev, "CRQ Queue closed\n");
3328 			if (adapter->resetting)
3329 				ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3330 		}
3331 
3332 		dev_warn(dev, "Send error (rc=%d)\n", rc);
3333 	}
3334 
3335 	return rc;
3336 }
3337 
3338 static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3339 {
3340 	union ibmvnic_crq crq;
3341 
3342 	memset(&crq, 0, sizeof(crq));
3343 	crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3344 	crq.generic.cmd = IBMVNIC_CRQ_INIT;
3345 	netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3346 
3347 	return ibmvnic_send_crq(adapter, &crq);
3348 }
3349 
3350 static int send_version_xchg(struct ibmvnic_adapter *adapter)
3351 {
3352 	union ibmvnic_crq crq;
3353 
3354 	memset(&crq, 0, sizeof(crq));
3355 	crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3356 	crq.version_exchange.cmd = VERSION_EXCHANGE;
3357 	crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3358 
3359 	return ibmvnic_send_crq(adapter, &crq);
3360 }
3361 
3362 struct vnic_login_client_data {
3363 	u8	type;
3364 	__be16	len;
3365 	char	name[];
3366 } __packed;
3367 
3368 static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3369 {
3370 	int len;
3371 
3372 	/* Calculate the amount of buffer space needed for the
3373 	 * vnic client data in the login buffer. There are four entries,
3374 	 * OS name, LPAR name, device name, and a null last entry.
3375 	 */
3376 	len = 4 * sizeof(struct vnic_login_client_data);
3377 	len += 6; /* "Linux" plus NULL */
3378 	len += strlen(utsname()->nodename) + 1;
3379 	len += strlen(adapter->netdev->name) + 1;
3380 
3381 	return len;
3382 }
3383 
3384 static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3385 				 struct vnic_login_client_data *vlcd)
3386 {
3387 	const char *os_name = "Linux";
3388 	int len;
3389 
3390 	/* Type 1 - LPAR OS */
3391 	vlcd->type = 1;
3392 	len = strlen(os_name) + 1;
3393 	vlcd->len = cpu_to_be16(len);
3394 	strncpy(vlcd->name, os_name, len);
3395 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3396 
3397 	/* Type 2 - LPAR name */
3398 	vlcd->type = 2;
3399 	len = strlen(utsname()->nodename) + 1;
3400 	vlcd->len = cpu_to_be16(len);
3401 	strncpy(vlcd->name, utsname()->nodename, len);
3402 	vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3403 
3404 	/* Type 3 - device name */
3405 	vlcd->type = 3;
3406 	len = strlen(adapter->netdev->name) + 1;
3407 	vlcd->len = cpu_to_be16(len);
3408 	strncpy(vlcd->name, adapter->netdev->name, len);
3409 }
3410 
3411 static int send_login(struct ibmvnic_adapter *adapter)
3412 {
3413 	struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3414 	struct ibmvnic_login_buffer *login_buffer;
3415 	struct device *dev = &adapter->vdev->dev;
3416 	dma_addr_t rsp_buffer_token;
3417 	dma_addr_t buffer_token;
3418 	size_t rsp_buffer_size;
3419 	union ibmvnic_crq crq;
3420 	size_t buffer_size;
3421 	__be64 *tx_list_p;
3422 	__be64 *rx_list_p;
3423 	int client_data_len;
3424 	struct vnic_login_client_data *vlcd;
3425 	int i;
3426 
3427 	if (!adapter->tx_scrq || !adapter->rx_scrq) {
3428 		netdev_err(adapter->netdev,
3429 			   "RX or TX queues are not allocated, device login failed\n");
3430 		return -1;
3431 	}
3432 
3433 	release_login_rsp_buffer(adapter);
3434 	client_data_len = vnic_client_data_len(adapter);
3435 
3436 	buffer_size =
3437 	    sizeof(struct ibmvnic_login_buffer) +
3438 	    sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3439 	    client_data_len;
3440 
3441 	login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3442 	if (!login_buffer)
3443 		goto buf_alloc_failed;
3444 
3445 	buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3446 				      DMA_TO_DEVICE);
3447 	if (dma_mapping_error(dev, buffer_token)) {
3448 		dev_err(dev, "Couldn't map login buffer\n");
3449 		goto buf_map_failed;
3450 	}
3451 
3452 	rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3453 			  sizeof(u64) * adapter->req_tx_queues +
3454 			  sizeof(u64) * adapter->req_rx_queues +
3455 			  sizeof(u64) * adapter->req_rx_queues +
3456 			  sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3457 
3458 	login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3459 	if (!login_rsp_buffer)
3460 		goto buf_rsp_alloc_failed;
3461 
3462 	rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3463 					  rsp_buffer_size, DMA_FROM_DEVICE);
3464 	if (dma_mapping_error(dev, rsp_buffer_token)) {
3465 		dev_err(dev, "Couldn't map login rsp buffer\n");
3466 		goto buf_rsp_map_failed;
3467 	}
3468 
3469 	adapter->login_buf = login_buffer;
3470 	adapter->login_buf_token = buffer_token;
3471 	adapter->login_buf_sz = buffer_size;
3472 	adapter->login_rsp_buf = login_rsp_buffer;
3473 	adapter->login_rsp_buf_token = rsp_buffer_token;
3474 	adapter->login_rsp_buf_sz = rsp_buffer_size;
3475 
3476 	login_buffer->len = cpu_to_be32(buffer_size);
3477 	login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3478 	login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3479 	login_buffer->off_txcomp_subcrqs =
3480 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3481 	login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3482 	login_buffer->off_rxcomp_subcrqs =
3483 	    cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3484 			sizeof(u64) * adapter->req_tx_queues);
3485 	login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3486 	login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3487 
3488 	tx_list_p = (__be64 *)((char *)login_buffer +
3489 				      sizeof(struct ibmvnic_login_buffer));
3490 	rx_list_p = (__be64 *)((char *)login_buffer +
3491 				      sizeof(struct ibmvnic_login_buffer) +
3492 				      sizeof(u64) * adapter->req_tx_queues);
3493 
3494 	for (i = 0; i < adapter->req_tx_queues; i++) {
3495 		if (adapter->tx_scrq[i]) {
3496 			tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3497 						   crq_num);
3498 		}
3499 	}
3500 
3501 	for (i = 0; i < adapter->req_rx_queues; i++) {
3502 		if (adapter->rx_scrq[i]) {
3503 			rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3504 						   crq_num);
3505 		}
3506 	}
3507 
3508 	/* Insert vNIC login client data */
3509 	vlcd = (struct vnic_login_client_data *)
3510 		((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3511 	login_buffer->client_data_offset =
3512 			cpu_to_be32((char *)vlcd - (char *)login_buffer);
3513 	login_buffer->client_data_len = cpu_to_be32(client_data_len);
3514 
3515 	vnic_add_client_data(adapter, vlcd);
3516 
3517 	netdev_dbg(adapter->netdev, "Login Buffer:\n");
3518 	for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3519 		netdev_dbg(adapter->netdev, "%016lx\n",
3520 			   ((unsigned long int *)(adapter->login_buf))[i]);
3521 	}
3522 
3523 	memset(&crq, 0, sizeof(crq));
3524 	crq.login.first = IBMVNIC_CRQ_CMD;
3525 	crq.login.cmd = LOGIN;
3526 	crq.login.ioba = cpu_to_be32(buffer_token);
3527 	crq.login.len = cpu_to_be32(buffer_size);
3528 	ibmvnic_send_crq(adapter, &crq);
3529 
3530 	return 0;
3531 
3532 buf_rsp_map_failed:
3533 	kfree(login_rsp_buffer);
3534 buf_rsp_alloc_failed:
3535 	dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3536 buf_map_failed:
3537 	kfree(login_buffer);
3538 buf_alloc_failed:
3539 	return -1;
3540 }
3541 
3542 static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3543 			    u32 len, u8 map_id)
3544 {
3545 	union ibmvnic_crq crq;
3546 
3547 	memset(&crq, 0, sizeof(crq));
3548 	crq.request_map.first = IBMVNIC_CRQ_CMD;
3549 	crq.request_map.cmd = REQUEST_MAP;
3550 	crq.request_map.map_id = map_id;
3551 	crq.request_map.ioba = cpu_to_be32(addr);
3552 	crq.request_map.len = cpu_to_be32(len);
3553 	return ibmvnic_send_crq(adapter, &crq);
3554 }
3555 
3556 static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3557 {
3558 	union ibmvnic_crq crq;
3559 
3560 	memset(&crq, 0, sizeof(crq));
3561 	crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3562 	crq.request_unmap.cmd = REQUEST_UNMAP;
3563 	crq.request_unmap.map_id = map_id;
3564 	return ibmvnic_send_crq(adapter, &crq);
3565 }
3566 
3567 static void send_map_query(struct ibmvnic_adapter *adapter)
3568 {
3569 	union ibmvnic_crq crq;
3570 
3571 	memset(&crq, 0, sizeof(crq));
3572 	crq.query_map.first = IBMVNIC_CRQ_CMD;
3573 	crq.query_map.cmd = QUERY_MAP;
3574 	ibmvnic_send_crq(adapter, &crq);
3575 }
3576 
3577 /* Send a series of CRQs requesting various capabilities of the VNIC server */
3578 static void send_cap_queries(struct ibmvnic_adapter *adapter)
3579 {
3580 	union ibmvnic_crq crq;
3581 
3582 	atomic_set(&adapter->running_cap_crqs, 0);
3583 	memset(&crq, 0, sizeof(crq));
3584 	crq.query_capability.first = IBMVNIC_CRQ_CMD;
3585 	crq.query_capability.cmd = QUERY_CAPABILITY;
3586 
3587 	crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3588 	atomic_inc(&adapter->running_cap_crqs);
3589 	ibmvnic_send_crq(adapter, &crq);
3590 
3591 	crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3592 	atomic_inc(&adapter->running_cap_crqs);
3593 	ibmvnic_send_crq(adapter, &crq);
3594 
3595 	crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3596 	atomic_inc(&adapter->running_cap_crqs);
3597 	ibmvnic_send_crq(adapter, &crq);
3598 
3599 	crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3600 	atomic_inc(&adapter->running_cap_crqs);
3601 	ibmvnic_send_crq(adapter, &crq);
3602 
3603 	crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3604 	atomic_inc(&adapter->running_cap_crqs);
3605 	ibmvnic_send_crq(adapter, &crq);
3606 
3607 	crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3608 	atomic_inc(&adapter->running_cap_crqs);
3609 	ibmvnic_send_crq(adapter, &crq);
3610 
3611 	crq.query_capability.capability =
3612 	    cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3613 	atomic_inc(&adapter->running_cap_crqs);
3614 	ibmvnic_send_crq(adapter, &crq);
3615 
3616 	crq.query_capability.capability =
3617 	    cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3618 	atomic_inc(&adapter->running_cap_crqs);
3619 	ibmvnic_send_crq(adapter, &crq);
3620 
3621 	crq.query_capability.capability =
3622 	    cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3623 	atomic_inc(&adapter->running_cap_crqs);
3624 	ibmvnic_send_crq(adapter, &crq);
3625 
3626 	crq.query_capability.capability =
3627 	    cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3628 	atomic_inc(&adapter->running_cap_crqs);
3629 	ibmvnic_send_crq(adapter, &crq);
3630 
3631 	crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3632 	atomic_inc(&adapter->running_cap_crqs);
3633 	ibmvnic_send_crq(adapter, &crq);
3634 
3635 	crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3636 	atomic_inc(&adapter->running_cap_crqs);
3637 	ibmvnic_send_crq(adapter, &crq);
3638 
3639 	crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3640 	atomic_inc(&adapter->running_cap_crqs);
3641 	ibmvnic_send_crq(adapter, &crq);
3642 
3643 	crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3644 	atomic_inc(&adapter->running_cap_crqs);
3645 	ibmvnic_send_crq(adapter, &crq);
3646 
3647 	crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3648 	atomic_inc(&adapter->running_cap_crqs);
3649 	ibmvnic_send_crq(adapter, &crq);
3650 
3651 	crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3652 	atomic_inc(&adapter->running_cap_crqs);
3653 	ibmvnic_send_crq(adapter, &crq);
3654 
3655 	crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3656 	atomic_inc(&adapter->running_cap_crqs);
3657 	ibmvnic_send_crq(adapter, &crq);
3658 
3659 	crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3660 	atomic_inc(&adapter->running_cap_crqs);
3661 	ibmvnic_send_crq(adapter, &crq);
3662 
3663 	crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3664 	atomic_inc(&adapter->running_cap_crqs);
3665 	ibmvnic_send_crq(adapter, &crq);
3666 
3667 	crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3668 	atomic_inc(&adapter->running_cap_crqs);
3669 	ibmvnic_send_crq(adapter, &crq);
3670 
3671 	crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3672 	atomic_inc(&adapter->running_cap_crqs);
3673 	ibmvnic_send_crq(adapter, &crq);
3674 
3675 	crq.query_capability.capability =
3676 			cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3677 	atomic_inc(&adapter->running_cap_crqs);
3678 	ibmvnic_send_crq(adapter, &crq);
3679 
3680 	crq.query_capability.capability =
3681 			cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3682 	atomic_inc(&adapter->running_cap_crqs);
3683 	ibmvnic_send_crq(adapter, &crq);
3684 
3685 	crq.query_capability.capability =
3686 			cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3687 	atomic_inc(&adapter->running_cap_crqs);
3688 	ibmvnic_send_crq(adapter, &crq);
3689 
3690 	crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3691 	atomic_inc(&adapter->running_cap_crqs);
3692 	ibmvnic_send_crq(adapter, &crq);
3693 }
3694 
3695 static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3696 				struct ibmvnic_adapter *adapter)
3697 {
3698 	struct device *dev = &adapter->vdev->dev;
3699 
3700 	if (crq->get_vpd_size_rsp.rc.code) {
3701 		dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3702 			crq->get_vpd_size_rsp.rc.code);
3703 		complete(&adapter->fw_done);
3704 		return;
3705 	}
3706 
3707 	adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3708 	complete(&adapter->fw_done);
3709 }
3710 
3711 static void handle_vpd_rsp(union ibmvnic_crq *crq,
3712 			   struct ibmvnic_adapter *adapter)
3713 {
3714 	struct device *dev = &adapter->vdev->dev;
3715 	unsigned char *substr = NULL;
3716 	u8 fw_level_len = 0;
3717 
3718 	memset(adapter->fw_version, 0, 32);
3719 
3720 	dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3721 			 DMA_FROM_DEVICE);
3722 
3723 	if (crq->get_vpd_rsp.rc.code) {
3724 		dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3725 			crq->get_vpd_rsp.rc.code);
3726 		goto complete;
3727 	}
3728 
3729 	/* get the position of the firmware version info
3730 	 * located after the ASCII 'RM' substring in the buffer
3731 	 */
3732 	substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3733 	if (!substr) {
3734 		dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3735 		goto complete;
3736 	}
3737 
3738 	/* get length of firmware level ASCII substring */
3739 	if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3740 		fw_level_len = *(substr + 2);
3741 	} else {
3742 		dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3743 		goto complete;
3744 	}
3745 
3746 	/* copy firmware version string from vpd into adapter */
3747 	if ((substr + 3 + fw_level_len) <
3748 	    (adapter->vpd->buff + adapter->vpd->len)) {
3749 		strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3750 	} else {
3751 		dev_info(dev, "FW substr extrapolated VPD buff\n");
3752 	}
3753 
3754 complete:
3755 	if (adapter->fw_version[0] == '\0')
3756 		strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3757 	complete(&adapter->fw_done);
3758 }
3759 
3760 static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3761 {
3762 	struct device *dev = &adapter->vdev->dev;
3763 	struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3764 	union ibmvnic_crq crq;
3765 	int i;
3766 
3767 	dma_unmap_single(dev, adapter->ip_offload_tok,
3768 			 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3769 
3770 	netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3771 	for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3772 		netdev_dbg(adapter->netdev, "%016lx\n",
3773 			   ((unsigned long int *)(buf))[i]);
3774 
3775 	netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3776 	netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3777 	netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3778 		   buf->tcp_ipv4_chksum);
3779 	netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3780 		   buf->tcp_ipv6_chksum);
3781 	netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3782 		   buf->udp_ipv4_chksum);
3783 	netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3784 		   buf->udp_ipv6_chksum);
3785 	netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3786 		   buf->large_tx_ipv4);
3787 	netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3788 		   buf->large_tx_ipv6);
3789 	netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3790 		   buf->large_rx_ipv4);
3791 	netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3792 		   buf->large_rx_ipv6);
3793 	netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3794 		   buf->max_ipv4_header_size);
3795 	netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3796 		   buf->max_ipv6_header_size);
3797 	netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3798 		   buf->max_tcp_header_size);
3799 	netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3800 		   buf->max_udp_header_size);
3801 	netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3802 		   buf->max_large_tx_size);
3803 	netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3804 		   buf->max_large_rx_size);
3805 	netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3806 		   buf->ipv6_extension_header);
3807 	netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3808 		   buf->tcp_pseudosum_req);
3809 	netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3810 		   buf->num_ipv6_ext_headers);
3811 	netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3812 		   buf->off_ipv6_ext_headers);
3813 
3814 	adapter->ip_offload_ctrl_tok =
3815 	    dma_map_single(dev, &adapter->ip_offload_ctrl,
3816 			   sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3817 
3818 	if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3819 		dev_err(dev, "Couldn't map ip offload control buffer\n");
3820 		return;
3821 	}
3822 
3823 	adapter->ip_offload_ctrl.len =
3824 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3825 	adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3826 	adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3827 	adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3828 	adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3829 	adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3830 	adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3831 	adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3832 	adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3833 	adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3834 
3835 	/* large_rx disabled for now, additional features needed */
3836 	adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3837 	adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3838 
3839 	adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3840 
3841 	if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3842 		adapter->netdev->features |= NETIF_F_IP_CSUM;
3843 
3844 	if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3845 		adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3846 
3847 	if ((adapter->netdev->features &
3848 	    (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3849 		adapter->netdev->features |= NETIF_F_RXCSUM;
3850 
3851 	if (buf->large_tx_ipv4)
3852 		adapter->netdev->features |= NETIF_F_TSO;
3853 	if (buf->large_tx_ipv6)
3854 		adapter->netdev->features |= NETIF_F_TSO6;
3855 
3856 	adapter->netdev->hw_features |= adapter->netdev->features;
3857 
3858 	memset(&crq, 0, sizeof(crq));
3859 	crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3860 	crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3861 	crq.control_ip_offload.len =
3862 	    cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3863 	crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3864 	ibmvnic_send_crq(adapter, &crq);
3865 }
3866 
3867 static const char *ibmvnic_fw_err_cause(u16 cause)
3868 {
3869 	switch (cause) {
3870 	case ADAPTER_PROBLEM:
3871 		return "adapter problem";
3872 	case BUS_PROBLEM:
3873 		return "bus problem";
3874 	case FW_PROBLEM:
3875 		return "firmware problem";
3876 	case DD_PROBLEM:
3877 		return "device driver problem";
3878 	case EEH_RECOVERY:
3879 		return "EEH recovery";
3880 	case FW_UPDATED:
3881 		return "firmware updated";
3882 	case LOW_MEMORY:
3883 		return "low Memory";
3884 	default:
3885 		return "unknown";
3886 	}
3887 }
3888 
3889 static void handle_error_indication(union ibmvnic_crq *crq,
3890 				    struct ibmvnic_adapter *adapter)
3891 {
3892 	struct device *dev = &adapter->vdev->dev;
3893 	u16 cause;
3894 
3895 	cause = be16_to_cpu(crq->error_indication.error_cause);
3896 
3897 	dev_warn_ratelimited(dev,
3898 			     "Firmware reports %serror, cause: %s. Starting recovery...\n",
3899 			     crq->error_indication.flags
3900 				& IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3901 			     ibmvnic_fw_err_cause(cause));
3902 
3903 	if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3904 		ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3905 	else
3906 		ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3907 }
3908 
3909 static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3910 				 struct ibmvnic_adapter *adapter)
3911 {
3912 	struct net_device *netdev = adapter->netdev;
3913 	struct device *dev = &adapter->vdev->dev;
3914 	long rc;
3915 
3916 	rc = crq->change_mac_addr_rsp.rc.code;
3917 	if (rc) {
3918 		dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3919 		goto out;
3920 	}
3921 	memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3922 	       ETH_ALEN);
3923 out:
3924 	complete(&adapter->fw_done);
3925 	return rc;
3926 }
3927 
3928 static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3929 				   struct ibmvnic_adapter *adapter)
3930 {
3931 	struct device *dev = &adapter->vdev->dev;
3932 	u64 *req_value;
3933 	char *name;
3934 
3935 	atomic_dec(&adapter->running_cap_crqs);
3936 	switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3937 	case REQ_TX_QUEUES:
3938 		req_value = &adapter->req_tx_queues;
3939 		name = "tx";
3940 		break;
3941 	case REQ_RX_QUEUES:
3942 		req_value = &adapter->req_rx_queues;
3943 		name = "rx";
3944 		break;
3945 	case REQ_RX_ADD_QUEUES:
3946 		req_value = &adapter->req_rx_add_queues;
3947 		name = "rx_add";
3948 		break;
3949 	case REQ_TX_ENTRIES_PER_SUBCRQ:
3950 		req_value = &adapter->req_tx_entries_per_subcrq;
3951 		name = "tx_entries_per_subcrq";
3952 		break;
3953 	case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3954 		req_value = &adapter->req_rx_add_entries_per_subcrq;
3955 		name = "rx_add_entries_per_subcrq";
3956 		break;
3957 	case REQ_MTU:
3958 		req_value = &adapter->req_mtu;
3959 		name = "mtu";
3960 		break;
3961 	case PROMISC_REQUESTED:
3962 		req_value = &adapter->promisc;
3963 		name = "promisc";
3964 		break;
3965 	default:
3966 		dev_err(dev, "Got invalid cap request rsp %d\n",
3967 			crq->request_capability.capability);
3968 		return;
3969 	}
3970 
3971 	switch (crq->request_capability_rsp.rc.code) {
3972 	case SUCCESS:
3973 		break;
3974 	case PARTIALSUCCESS:
3975 		dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3976 			 *req_value,
3977 			 (long int)be64_to_cpu(crq->request_capability_rsp.
3978 					       number), name);
3979 
3980 		if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3981 		    REQ_MTU) {
3982 			pr_err("mtu of %llu is not supported. Reverting.\n",
3983 			       *req_value);
3984 			*req_value = adapter->fallback.mtu;
3985 		} else {
3986 			*req_value =
3987 				be64_to_cpu(crq->request_capability_rsp.number);
3988 		}
3989 
3990 		ibmvnic_send_req_caps(adapter, 1);
3991 		return;
3992 	default:
3993 		dev_err(dev, "Error %d in request cap rsp\n",
3994 			crq->request_capability_rsp.rc.code);
3995 		return;
3996 	}
3997 
3998 	/* Done receiving requested capabilities, query IP offload support */
3999 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4000 		union ibmvnic_crq newcrq;
4001 		int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
4002 		struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
4003 		    &adapter->ip_offload_buf;
4004 
4005 		adapter->wait_capability = false;
4006 		adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
4007 							 buf_sz,
4008 							 DMA_FROM_DEVICE);
4009 
4010 		if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
4011 			if (!firmware_has_feature(FW_FEATURE_CMO))
4012 				dev_err(dev, "Couldn't map offload buffer\n");
4013 			return;
4014 		}
4015 
4016 		memset(&newcrq, 0, sizeof(newcrq));
4017 		newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
4018 		newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
4019 		newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
4020 		newcrq.query_ip_offload.ioba =
4021 		    cpu_to_be32(adapter->ip_offload_tok);
4022 
4023 		ibmvnic_send_crq(adapter, &newcrq);
4024 	}
4025 }
4026 
4027 static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
4028 			    struct ibmvnic_adapter *adapter)
4029 {
4030 	struct device *dev = &adapter->vdev->dev;
4031 	struct net_device *netdev = adapter->netdev;
4032 	struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
4033 	struct ibmvnic_login_buffer *login = adapter->login_buf;
4034 	int i;
4035 
4036 	dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
4037 			 DMA_TO_DEVICE);
4038 	dma_unmap_single(dev, adapter->login_rsp_buf_token,
4039 			 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
4040 
4041 	/* If the number of queues requested can't be allocated by the
4042 	 * server, the login response will return with code 1. We will need
4043 	 * to resend the login buffer with fewer queues requested.
4044 	 */
4045 	if (login_rsp_crq->generic.rc.code) {
4046 		adapter->init_done_rc = login_rsp_crq->generic.rc.code;
4047 		complete(&adapter->init_done);
4048 		return 0;
4049 	}
4050 
4051 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4052 
4053 	netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
4054 	for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
4055 		netdev_dbg(adapter->netdev, "%016lx\n",
4056 			   ((unsigned long int *)(adapter->login_rsp_buf))[i]);
4057 	}
4058 
4059 	/* Sanity checks */
4060 	if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
4061 	    (be32_to_cpu(login->num_rxcomp_subcrqs) *
4062 	     adapter->req_rx_add_queues !=
4063 	     be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
4064 		dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
4065 		ibmvnic_remove(adapter->vdev);
4066 		return -EIO;
4067 	}
4068 	release_login_buffer(adapter);
4069 	complete(&adapter->init_done);
4070 
4071 	return 0;
4072 }
4073 
4074 static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4075 				     struct ibmvnic_adapter *adapter)
4076 {
4077 	struct device *dev = &adapter->vdev->dev;
4078 	long rc;
4079 
4080 	rc = crq->request_unmap_rsp.rc.code;
4081 	if (rc)
4082 		dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4083 }
4084 
4085 static void handle_query_map_rsp(union ibmvnic_crq *crq,
4086 				 struct ibmvnic_adapter *adapter)
4087 {
4088 	struct net_device *netdev = adapter->netdev;
4089 	struct device *dev = &adapter->vdev->dev;
4090 	long rc;
4091 
4092 	rc = crq->query_map_rsp.rc.code;
4093 	if (rc) {
4094 		dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4095 		return;
4096 	}
4097 	netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4098 		   crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4099 		   crq->query_map_rsp.free_pages);
4100 }
4101 
4102 static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4103 				 struct ibmvnic_adapter *adapter)
4104 {
4105 	struct net_device *netdev = adapter->netdev;
4106 	struct device *dev = &adapter->vdev->dev;
4107 	long rc;
4108 
4109 	atomic_dec(&adapter->running_cap_crqs);
4110 	netdev_dbg(netdev, "Outstanding queries: %d\n",
4111 		   atomic_read(&adapter->running_cap_crqs));
4112 	rc = crq->query_capability.rc.code;
4113 	if (rc) {
4114 		dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4115 		goto out;
4116 	}
4117 
4118 	switch (be16_to_cpu(crq->query_capability.capability)) {
4119 	case MIN_TX_QUEUES:
4120 		adapter->min_tx_queues =
4121 		    be64_to_cpu(crq->query_capability.number);
4122 		netdev_dbg(netdev, "min_tx_queues = %lld\n",
4123 			   adapter->min_tx_queues);
4124 		break;
4125 	case MIN_RX_QUEUES:
4126 		adapter->min_rx_queues =
4127 		    be64_to_cpu(crq->query_capability.number);
4128 		netdev_dbg(netdev, "min_rx_queues = %lld\n",
4129 			   adapter->min_rx_queues);
4130 		break;
4131 	case MIN_RX_ADD_QUEUES:
4132 		adapter->min_rx_add_queues =
4133 		    be64_to_cpu(crq->query_capability.number);
4134 		netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4135 			   adapter->min_rx_add_queues);
4136 		break;
4137 	case MAX_TX_QUEUES:
4138 		adapter->max_tx_queues =
4139 		    be64_to_cpu(crq->query_capability.number);
4140 		netdev_dbg(netdev, "max_tx_queues = %lld\n",
4141 			   adapter->max_tx_queues);
4142 		break;
4143 	case MAX_RX_QUEUES:
4144 		adapter->max_rx_queues =
4145 		    be64_to_cpu(crq->query_capability.number);
4146 		netdev_dbg(netdev, "max_rx_queues = %lld\n",
4147 			   adapter->max_rx_queues);
4148 		break;
4149 	case MAX_RX_ADD_QUEUES:
4150 		adapter->max_rx_add_queues =
4151 		    be64_to_cpu(crq->query_capability.number);
4152 		netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4153 			   adapter->max_rx_add_queues);
4154 		break;
4155 	case MIN_TX_ENTRIES_PER_SUBCRQ:
4156 		adapter->min_tx_entries_per_subcrq =
4157 		    be64_to_cpu(crq->query_capability.number);
4158 		netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4159 			   adapter->min_tx_entries_per_subcrq);
4160 		break;
4161 	case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4162 		adapter->min_rx_add_entries_per_subcrq =
4163 		    be64_to_cpu(crq->query_capability.number);
4164 		netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4165 			   adapter->min_rx_add_entries_per_subcrq);
4166 		break;
4167 	case MAX_TX_ENTRIES_PER_SUBCRQ:
4168 		adapter->max_tx_entries_per_subcrq =
4169 		    be64_to_cpu(crq->query_capability.number);
4170 		netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4171 			   adapter->max_tx_entries_per_subcrq);
4172 		break;
4173 	case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4174 		adapter->max_rx_add_entries_per_subcrq =
4175 		    be64_to_cpu(crq->query_capability.number);
4176 		netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4177 			   adapter->max_rx_add_entries_per_subcrq);
4178 		break;
4179 	case TCP_IP_OFFLOAD:
4180 		adapter->tcp_ip_offload =
4181 		    be64_to_cpu(crq->query_capability.number);
4182 		netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4183 			   adapter->tcp_ip_offload);
4184 		break;
4185 	case PROMISC_SUPPORTED:
4186 		adapter->promisc_supported =
4187 		    be64_to_cpu(crq->query_capability.number);
4188 		netdev_dbg(netdev, "promisc_supported = %lld\n",
4189 			   adapter->promisc_supported);
4190 		break;
4191 	case MIN_MTU:
4192 		adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4193 		netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4194 		netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4195 		break;
4196 	case MAX_MTU:
4197 		adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4198 		netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4199 		netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4200 		break;
4201 	case MAX_MULTICAST_FILTERS:
4202 		adapter->max_multicast_filters =
4203 		    be64_to_cpu(crq->query_capability.number);
4204 		netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4205 			   adapter->max_multicast_filters);
4206 		break;
4207 	case VLAN_HEADER_INSERTION:
4208 		adapter->vlan_header_insertion =
4209 		    be64_to_cpu(crq->query_capability.number);
4210 		if (adapter->vlan_header_insertion)
4211 			netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4212 		netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4213 			   adapter->vlan_header_insertion);
4214 		break;
4215 	case RX_VLAN_HEADER_INSERTION:
4216 		adapter->rx_vlan_header_insertion =
4217 		    be64_to_cpu(crq->query_capability.number);
4218 		netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4219 			   adapter->rx_vlan_header_insertion);
4220 		break;
4221 	case MAX_TX_SG_ENTRIES:
4222 		adapter->max_tx_sg_entries =
4223 		    be64_to_cpu(crq->query_capability.number);
4224 		netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4225 			   adapter->max_tx_sg_entries);
4226 		break;
4227 	case RX_SG_SUPPORTED:
4228 		adapter->rx_sg_supported =
4229 		    be64_to_cpu(crq->query_capability.number);
4230 		netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4231 			   adapter->rx_sg_supported);
4232 		break;
4233 	case OPT_TX_COMP_SUB_QUEUES:
4234 		adapter->opt_tx_comp_sub_queues =
4235 		    be64_to_cpu(crq->query_capability.number);
4236 		netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4237 			   adapter->opt_tx_comp_sub_queues);
4238 		break;
4239 	case OPT_RX_COMP_QUEUES:
4240 		adapter->opt_rx_comp_queues =
4241 		    be64_to_cpu(crq->query_capability.number);
4242 		netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4243 			   adapter->opt_rx_comp_queues);
4244 		break;
4245 	case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4246 		adapter->opt_rx_bufadd_q_per_rx_comp_q =
4247 		    be64_to_cpu(crq->query_capability.number);
4248 		netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4249 			   adapter->opt_rx_bufadd_q_per_rx_comp_q);
4250 		break;
4251 	case OPT_TX_ENTRIES_PER_SUBCRQ:
4252 		adapter->opt_tx_entries_per_subcrq =
4253 		    be64_to_cpu(crq->query_capability.number);
4254 		netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4255 			   adapter->opt_tx_entries_per_subcrq);
4256 		break;
4257 	case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4258 		adapter->opt_rxba_entries_per_subcrq =
4259 		    be64_to_cpu(crq->query_capability.number);
4260 		netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4261 			   adapter->opt_rxba_entries_per_subcrq);
4262 		break;
4263 	case TX_RX_DESC_REQ:
4264 		adapter->tx_rx_desc_req = crq->query_capability.number;
4265 		netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4266 			   adapter->tx_rx_desc_req);
4267 		break;
4268 
4269 	default:
4270 		netdev_err(netdev, "Got invalid cap rsp %d\n",
4271 			   crq->query_capability.capability);
4272 	}
4273 
4274 out:
4275 	if (atomic_read(&adapter->running_cap_crqs) == 0) {
4276 		adapter->wait_capability = false;
4277 		ibmvnic_send_req_caps(adapter, 0);
4278 	}
4279 }
4280 
4281 static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4282 			       struct ibmvnic_adapter *adapter)
4283 {
4284 	struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4285 	struct net_device *netdev = adapter->netdev;
4286 	struct device *dev = &adapter->vdev->dev;
4287 	u64 *u64_crq = (u64 *)crq;
4288 	long rc;
4289 
4290 	netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4291 		   (unsigned long int)cpu_to_be64(u64_crq[0]),
4292 		   (unsigned long int)cpu_to_be64(u64_crq[1]));
4293 	switch (gen_crq->first) {
4294 	case IBMVNIC_CRQ_INIT_RSP:
4295 		switch (gen_crq->cmd) {
4296 		case IBMVNIC_CRQ_INIT:
4297 			dev_info(dev, "Partner initialized\n");
4298 			adapter->from_passive_init = true;
4299 			adapter->failover_pending = false;
4300 			if (!completion_done(&adapter->init_done)) {
4301 				complete(&adapter->init_done);
4302 				adapter->init_done_rc = -EIO;
4303 			}
4304 			ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4305 			break;
4306 		case IBMVNIC_CRQ_INIT_COMPLETE:
4307 			dev_info(dev, "Partner initialization complete\n");
4308 			adapter->crq.active = true;
4309 			send_version_xchg(adapter);
4310 			break;
4311 		default:
4312 			dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4313 		}
4314 		return;
4315 	case IBMVNIC_CRQ_XPORT_EVENT:
4316 		netif_carrier_off(netdev);
4317 		adapter->crq.active = false;
4318 		if (adapter->resetting)
4319 			adapter->force_reset_recovery = true;
4320 		if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4321 			dev_info(dev, "Migrated, re-enabling adapter\n");
4322 			ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4323 		} else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4324 			dev_info(dev, "Backing device failover detected\n");
4325 			adapter->failover_pending = true;
4326 		} else {
4327 			/* The adapter lost the connection */
4328 			dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4329 				gen_crq->cmd);
4330 			ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4331 		}
4332 		return;
4333 	case IBMVNIC_CRQ_CMD_RSP:
4334 		break;
4335 	default:
4336 		dev_err(dev, "Got an invalid msg type 0x%02x\n",
4337 			gen_crq->first);
4338 		return;
4339 	}
4340 
4341 	switch (gen_crq->cmd) {
4342 	case VERSION_EXCHANGE_RSP:
4343 		rc = crq->version_exchange_rsp.rc.code;
4344 		if (rc) {
4345 			dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4346 			break;
4347 		}
4348 		dev_info(dev, "Partner protocol version is %d\n",
4349 			 crq->version_exchange_rsp.version);
4350 		if (be16_to_cpu(crq->version_exchange_rsp.version) <
4351 		    ibmvnic_version)
4352 			ibmvnic_version =
4353 			    be16_to_cpu(crq->version_exchange_rsp.version);
4354 		send_cap_queries(adapter);
4355 		break;
4356 	case QUERY_CAPABILITY_RSP:
4357 		handle_query_cap_rsp(crq, adapter);
4358 		break;
4359 	case QUERY_MAP_RSP:
4360 		handle_query_map_rsp(crq, adapter);
4361 		break;
4362 	case REQUEST_MAP_RSP:
4363 		adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4364 		complete(&adapter->fw_done);
4365 		break;
4366 	case REQUEST_UNMAP_RSP:
4367 		handle_request_unmap_rsp(crq, adapter);
4368 		break;
4369 	case REQUEST_CAPABILITY_RSP:
4370 		handle_request_cap_rsp(crq, adapter);
4371 		break;
4372 	case LOGIN_RSP:
4373 		netdev_dbg(netdev, "Got Login Response\n");
4374 		handle_login_rsp(crq, adapter);
4375 		break;
4376 	case LOGICAL_LINK_STATE_RSP:
4377 		netdev_dbg(netdev,
4378 			   "Got Logical Link State Response, state: %d rc: %d\n",
4379 			   crq->logical_link_state_rsp.link_state,
4380 			   crq->logical_link_state_rsp.rc.code);
4381 		adapter->logical_link_state =
4382 		    crq->logical_link_state_rsp.link_state;
4383 		adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4384 		complete(&adapter->init_done);
4385 		break;
4386 	case LINK_STATE_INDICATION:
4387 		netdev_dbg(netdev, "Got Logical Link State Indication\n");
4388 		adapter->phys_link_state =
4389 		    crq->link_state_indication.phys_link_state;
4390 		adapter->logical_link_state =
4391 		    crq->link_state_indication.logical_link_state;
4392 		break;
4393 	case CHANGE_MAC_ADDR_RSP:
4394 		netdev_dbg(netdev, "Got MAC address change Response\n");
4395 		adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4396 		break;
4397 	case ERROR_INDICATION:
4398 		netdev_dbg(netdev, "Got Error Indication\n");
4399 		handle_error_indication(crq, adapter);
4400 		break;
4401 	case REQUEST_STATISTICS_RSP:
4402 		netdev_dbg(netdev, "Got Statistics Response\n");
4403 		complete(&adapter->stats_done);
4404 		break;
4405 	case QUERY_IP_OFFLOAD_RSP:
4406 		netdev_dbg(netdev, "Got Query IP offload Response\n");
4407 		handle_query_ip_offload_rsp(adapter);
4408 		break;
4409 	case MULTICAST_CTRL_RSP:
4410 		netdev_dbg(netdev, "Got multicast control Response\n");
4411 		break;
4412 	case CONTROL_IP_OFFLOAD_RSP:
4413 		netdev_dbg(netdev, "Got Control IP offload Response\n");
4414 		dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4415 				 sizeof(adapter->ip_offload_ctrl),
4416 				 DMA_TO_DEVICE);
4417 		complete(&adapter->init_done);
4418 		break;
4419 	case COLLECT_FW_TRACE_RSP:
4420 		netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4421 		complete(&adapter->fw_done);
4422 		break;
4423 	case GET_VPD_SIZE_RSP:
4424 		handle_vpd_size_rsp(crq, adapter);
4425 		break;
4426 	case GET_VPD_RSP:
4427 		handle_vpd_rsp(crq, adapter);
4428 		break;
4429 	default:
4430 		netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4431 			   gen_crq->cmd);
4432 	}
4433 }
4434 
4435 static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4436 {
4437 	struct ibmvnic_adapter *adapter = instance;
4438 
4439 	tasklet_schedule(&adapter->tasklet);
4440 	return IRQ_HANDLED;
4441 }
4442 
4443 static void ibmvnic_tasklet(void *data)
4444 {
4445 	struct ibmvnic_adapter *adapter = data;
4446 	struct ibmvnic_crq_queue *queue = &adapter->crq;
4447 	union ibmvnic_crq *crq;
4448 	unsigned long flags;
4449 	bool done = false;
4450 
4451 	spin_lock_irqsave(&queue->lock, flags);
4452 	while (!done) {
4453 		/* Pull all the valid messages off the CRQ */
4454 		while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4455 			ibmvnic_handle_crq(crq, adapter);
4456 			crq->generic.first = 0;
4457 		}
4458 
4459 		/* remain in tasklet until all
4460 		 * capabilities responses are received
4461 		 */
4462 		if (!adapter->wait_capability)
4463 			done = true;
4464 	}
4465 	/* if capabilities CRQ's were sent in this tasklet, the following
4466 	 * tasklet must wait until all responses are received
4467 	 */
4468 	if (atomic_read(&adapter->running_cap_crqs) != 0)
4469 		adapter->wait_capability = true;
4470 	spin_unlock_irqrestore(&queue->lock, flags);
4471 }
4472 
4473 static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4474 {
4475 	struct vio_dev *vdev = adapter->vdev;
4476 	int rc;
4477 
4478 	do {
4479 		rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4480 	} while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4481 
4482 	if (rc)
4483 		dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4484 
4485 	return rc;
4486 }
4487 
4488 static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4489 {
4490 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4491 	struct device *dev = &adapter->vdev->dev;
4492 	struct vio_dev *vdev = adapter->vdev;
4493 	int rc;
4494 
4495 	/* Close the CRQ */
4496 	do {
4497 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4498 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4499 
4500 	/* Clean out the queue */
4501 	memset(crq->msgs, 0, PAGE_SIZE);
4502 	crq->cur = 0;
4503 	crq->active = false;
4504 
4505 	/* And re-open it again */
4506 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4507 				crq->msg_token, PAGE_SIZE);
4508 
4509 	if (rc == H_CLOSED)
4510 		/* Adapter is good, but other end is not ready */
4511 		dev_warn(dev, "Partner adapter not ready\n");
4512 	else if (rc != 0)
4513 		dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4514 
4515 	return rc;
4516 }
4517 
4518 static void release_crq_queue(struct ibmvnic_adapter *adapter)
4519 {
4520 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4521 	struct vio_dev *vdev = adapter->vdev;
4522 	long rc;
4523 
4524 	if (!crq->msgs)
4525 		return;
4526 
4527 	netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4528 	free_irq(vdev->irq, adapter);
4529 	tasklet_kill(&adapter->tasklet);
4530 	do {
4531 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4532 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4533 
4534 	dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4535 			 DMA_BIDIRECTIONAL);
4536 	free_page((unsigned long)crq->msgs);
4537 	crq->msgs = NULL;
4538 	crq->active = false;
4539 }
4540 
4541 static int init_crq_queue(struct ibmvnic_adapter *adapter)
4542 {
4543 	struct ibmvnic_crq_queue *crq = &adapter->crq;
4544 	struct device *dev = &adapter->vdev->dev;
4545 	struct vio_dev *vdev = adapter->vdev;
4546 	int rc, retrc = -ENOMEM;
4547 
4548 	if (crq->msgs)
4549 		return 0;
4550 
4551 	crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4552 	/* Should we allocate more than one page? */
4553 
4554 	if (!crq->msgs)
4555 		return -ENOMEM;
4556 
4557 	crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4558 	crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4559 					DMA_BIDIRECTIONAL);
4560 	if (dma_mapping_error(dev, crq->msg_token))
4561 		goto map_failed;
4562 
4563 	rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4564 				crq->msg_token, PAGE_SIZE);
4565 
4566 	if (rc == H_RESOURCE)
4567 		/* maybe kexecing and resource is busy. try a reset */
4568 		rc = ibmvnic_reset_crq(adapter);
4569 	retrc = rc;
4570 
4571 	if (rc == H_CLOSED) {
4572 		dev_warn(dev, "Partner adapter not ready\n");
4573 	} else if (rc) {
4574 		dev_warn(dev, "Error %d opening adapter\n", rc);
4575 		goto reg_crq_failed;
4576 	}
4577 
4578 	retrc = 0;
4579 
4580 	tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4581 		     (unsigned long)adapter);
4582 
4583 	netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4584 	rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4585 			 adapter);
4586 	if (rc) {
4587 		dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4588 			vdev->irq, rc);
4589 		goto req_irq_failed;
4590 	}
4591 
4592 	rc = vio_enable_interrupts(vdev);
4593 	if (rc) {
4594 		dev_err(dev, "Error %d enabling interrupts\n", rc);
4595 		goto req_irq_failed;
4596 	}
4597 
4598 	crq->cur = 0;
4599 	spin_lock_init(&crq->lock);
4600 
4601 	return retrc;
4602 
4603 req_irq_failed:
4604 	tasklet_kill(&adapter->tasklet);
4605 	do {
4606 		rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4607 	} while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4608 reg_crq_failed:
4609 	dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4610 map_failed:
4611 	free_page((unsigned long)crq->msgs);
4612 	crq->msgs = NULL;
4613 	return retrc;
4614 }
4615 
4616 static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter)
4617 {
4618 	struct device *dev = &adapter->vdev->dev;
4619 	unsigned long timeout = msecs_to_jiffies(30000);
4620 	u64 old_num_rx_queues, old_num_tx_queues;
4621 	int rc;
4622 
4623 	adapter->from_passive_init = false;
4624 
4625 	old_num_rx_queues = adapter->req_rx_queues;
4626 	old_num_tx_queues = adapter->req_tx_queues;
4627 
4628 	init_completion(&adapter->init_done);
4629 	adapter->init_done_rc = 0;
4630 	ibmvnic_send_crq_init(adapter);
4631 	if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4632 		dev_err(dev, "Initialization sequence timed out\n");
4633 		return -1;
4634 	}
4635 
4636 	if (adapter->init_done_rc) {
4637 		release_crq_queue(adapter);
4638 		return adapter->init_done_rc;
4639 	}
4640 
4641 	if (adapter->from_passive_init) {
4642 		adapter->state = VNIC_OPEN;
4643 		adapter->from_passive_init = false;
4644 		return -1;
4645 	}
4646 
4647 	if (adapter->resetting && !adapter->wait_for_reset &&
4648 	    adapter->reset_reason != VNIC_RESET_MOBILITY) {
4649 		if (adapter->req_rx_queues != old_num_rx_queues ||
4650 		    adapter->req_tx_queues != old_num_tx_queues) {
4651 			release_sub_crqs(adapter, 0);
4652 			rc = init_sub_crqs(adapter);
4653 		} else {
4654 			rc = reset_sub_crq_queues(adapter);
4655 		}
4656 	} else {
4657 		rc = init_sub_crqs(adapter);
4658 	}
4659 
4660 	if (rc) {
4661 		dev_err(dev, "Initialization of sub crqs failed\n");
4662 		release_crq_queue(adapter);
4663 		return rc;
4664 	}
4665 
4666 	rc = init_sub_crq_irqs(adapter);
4667 	if (rc) {
4668 		dev_err(dev, "Failed to initialize sub crq irqs\n");
4669 		release_crq_queue(adapter);
4670 	}
4671 
4672 	return rc;
4673 }
4674 
4675 static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4676 {
4677 	struct device *dev = &adapter->vdev->dev;
4678 	unsigned long timeout = msecs_to_jiffies(30000);
4679 	int rc;
4680 
4681 	adapter->from_passive_init = false;
4682 
4683 	init_completion(&adapter->init_done);
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 	adapter->resetting = false;
4763 
4764 	adapter->mac_change_pending = false;
4765 
4766 	do {
4767 		rc = init_crq_queue(adapter);
4768 		if (rc) {
4769 			dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4770 				rc);
4771 			goto ibmvnic_init_fail;
4772 		}
4773 
4774 		rc = ibmvnic_init(adapter);
4775 		if (rc && rc != EAGAIN)
4776 			goto ibmvnic_init_fail;
4777 	} while (rc == EAGAIN);
4778 
4779 	rc = init_stats_buffers(adapter);
4780 	if (rc)
4781 		goto ibmvnic_init_fail;
4782 
4783 	rc = init_stats_token(adapter);
4784 	if (rc)
4785 		goto ibmvnic_stats_fail;
4786 
4787 	netdev->mtu = adapter->req_mtu - ETH_HLEN;
4788 	netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4789 	netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4790 
4791 	rc = device_create_file(&dev->dev, &dev_attr_failover);
4792 	if (rc)
4793 		goto ibmvnic_dev_file_err;
4794 
4795 	netif_carrier_off(netdev);
4796 	rc = register_netdev(netdev);
4797 	if (rc) {
4798 		dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4799 		goto ibmvnic_register_fail;
4800 	}
4801 	dev_info(&dev->dev, "ibmvnic registered\n");
4802 
4803 	adapter->state = VNIC_PROBED;
4804 
4805 	adapter->wait_for_reset = false;
4806 
4807 	return 0;
4808 
4809 ibmvnic_register_fail:
4810 	device_remove_file(&dev->dev, &dev_attr_failover);
4811 
4812 ibmvnic_dev_file_err:
4813 	release_stats_token(adapter);
4814 
4815 ibmvnic_stats_fail:
4816 	release_stats_buffers(adapter);
4817 
4818 ibmvnic_init_fail:
4819 	release_sub_crqs(adapter, 1);
4820 	release_crq_queue(adapter);
4821 	free_netdev(netdev);
4822 
4823 	return rc;
4824 }
4825 
4826 static int ibmvnic_remove(struct vio_dev *dev)
4827 {
4828 	struct net_device *netdev = dev_get_drvdata(&dev->dev);
4829 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4830 
4831 	adapter->state = VNIC_REMOVING;
4832 	rtnl_lock();
4833 	unregister_netdevice(netdev);
4834 
4835 	release_resources(adapter);
4836 	release_sub_crqs(adapter, 1);
4837 	release_crq_queue(adapter);
4838 
4839 	release_stats_token(adapter);
4840 	release_stats_buffers(adapter);
4841 
4842 	adapter->state = VNIC_REMOVED;
4843 
4844 	rtnl_unlock();
4845 	device_remove_file(&dev->dev, &dev_attr_failover);
4846 	free_netdev(netdev);
4847 	dev_set_drvdata(&dev->dev, NULL);
4848 
4849 	return 0;
4850 }
4851 
4852 static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4853 			      const char *buf, size_t count)
4854 {
4855 	struct net_device *netdev = dev_get_drvdata(dev);
4856 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4857 	unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4858 	__be64 session_token;
4859 	long rc;
4860 
4861 	if (!sysfs_streq(buf, "1"))
4862 		return -EINVAL;
4863 
4864 	rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4865 			 H_GET_SESSION_TOKEN, 0, 0, 0);
4866 	if (rc) {
4867 		netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4868 			   rc);
4869 		return -EINVAL;
4870 	}
4871 
4872 	session_token = (__be64)retbuf[0];
4873 	netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4874 		   be64_to_cpu(session_token));
4875 	rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4876 				H_SESSION_ERR_DETECTED, session_token, 0, 0);
4877 	if (rc) {
4878 		netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4879 			   rc);
4880 		return -EINVAL;
4881 	}
4882 
4883 	return count;
4884 }
4885 
4886 static DEVICE_ATTR_WO(failover);
4887 
4888 static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4889 {
4890 	struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4891 	struct ibmvnic_adapter *adapter;
4892 	struct iommu_table *tbl;
4893 	unsigned long ret = 0;
4894 	int i;
4895 
4896 	tbl = get_iommu_table_base(&vdev->dev);
4897 
4898 	/* netdev inits at probe time along with the structures we need below*/
4899 	if (!netdev)
4900 		return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4901 
4902 	adapter = netdev_priv(netdev);
4903 
4904 	ret += PAGE_SIZE; /* the crq message queue */
4905 	ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4906 
4907 	for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4908 		ret += 4 * PAGE_SIZE; /* the scrq message queue */
4909 
4910 	for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4911 	     i++)
4912 		ret += adapter->rx_pool[i].size *
4913 		    IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4914 
4915 	return ret;
4916 }
4917 
4918 static int ibmvnic_resume(struct device *dev)
4919 {
4920 	struct net_device *netdev = dev_get_drvdata(dev);
4921 	struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4922 
4923 	if (adapter->state != VNIC_OPEN)
4924 		return 0;
4925 
4926 	tasklet_schedule(&adapter->tasklet);
4927 
4928 	return 0;
4929 }
4930 
4931 static const struct vio_device_id ibmvnic_device_table[] = {
4932 	{"network", "IBM,vnic"},
4933 	{"", "" }
4934 };
4935 MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4936 
4937 static const struct dev_pm_ops ibmvnic_pm_ops = {
4938 	.resume = ibmvnic_resume
4939 };
4940 
4941 static struct vio_driver ibmvnic_driver = {
4942 	.id_table       = ibmvnic_device_table,
4943 	.probe          = ibmvnic_probe,
4944 	.remove         = ibmvnic_remove,
4945 	.get_desired_dma = ibmvnic_get_desired_dma,
4946 	.name		= ibmvnic_driver_name,
4947 	.pm		= &ibmvnic_pm_ops,
4948 };
4949 
4950 /* module functions */
4951 static int __init ibmvnic_module_init(void)
4952 {
4953 	pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4954 		IBMVNIC_DRIVER_VERSION);
4955 
4956 	return vio_register_driver(&ibmvnic_driver);
4957 }
4958 
4959 static void __exit ibmvnic_module_exit(void)
4960 {
4961 	vio_unregister_driver(&ibmvnic_driver);
4962 }
4963 
4964 module_init(ibmvnic_module_init);
4965 module_exit(ibmvnic_module_exit);
4966