Loading...
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
83static const char ibmvnic_driver_name[] = "ibmvnic";
84static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
85
86MODULE_AUTHOR("Santiago Leon");
87MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
90
91static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
92static int ibmvnic_remove(struct vio_dev *);
93static void release_sub_crqs(struct ibmvnic_adapter *, bool);
94static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
95static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
96static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
97static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
98static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
99 union sub_crq *sub_crq);
100static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
101static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
102static int enable_scrq_irq(struct ibmvnic_adapter *,
103 struct ibmvnic_sub_crq_queue *);
104static int disable_scrq_irq(struct ibmvnic_adapter *,
105 struct ibmvnic_sub_crq_queue *);
106static int pending_scrq(struct ibmvnic_adapter *,
107 struct ibmvnic_sub_crq_queue *);
108static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
109 struct ibmvnic_sub_crq_queue *);
110static int ibmvnic_poll(struct napi_struct *napi, int data);
111static void send_map_query(struct ibmvnic_adapter *adapter);
112static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
113static void send_request_unmap(struct ibmvnic_adapter *, u8);
114static int send_login(struct ibmvnic_adapter *adapter);
115static void send_cap_queries(struct ibmvnic_adapter *adapter);
116static int init_sub_crqs(struct ibmvnic_adapter *);
117static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
118static int ibmvnic_init(struct ibmvnic_adapter *);
119static void release_crq_queue(struct ibmvnic_adapter *);
120static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p);
121static int init_crq_queue(struct ibmvnic_adapter *adapter);
122
123struct ibmvnic_stat {
124 char name[ETH_GSTRING_LEN];
125 int offset;
126};
127
128#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
129 offsetof(struct ibmvnic_statistics, stat))
130#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
131
132static const struct ibmvnic_stat ibmvnic_stats[] = {
133 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
134 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
135 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
136 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
137 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
138 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
139 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
140 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
141 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
142 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
143 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
144 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
145 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
146 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
147 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
148 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
149 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
150 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
151 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
152 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
153 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
154 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
155};
156
157static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
158 unsigned long length, unsigned long *number,
159 unsigned long *irq)
160{
161 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
162 long rc;
163
164 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
165 *number = retbuf[0];
166 *irq = retbuf[1];
167
168 return rc;
169}
170
171static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
172 struct ibmvnic_long_term_buff *ltb, int size)
173{
174 struct device *dev = &adapter->vdev->dev;
175
176 ltb->size = size;
177 ltb->buff = dma_alloc_coherent(dev, ltb->size, <b->addr,
178 GFP_KERNEL);
179
180 if (!ltb->buff) {
181 dev_err(dev, "Couldn't alloc long term buffer\n");
182 return -ENOMEM;
183 }
184 ltb->map_id = adapter->map_id;
185 adapter->map_id++;
186
187 init_completion(&adapter->fw_done);
188 send_request_map(adapter, ltb->addr,
189 ltb->size, ltb->map_id);
190 wait_for_completion(&adapter->fw_done);
191
192 if (adapter->fw_done_rc) {
193 dev_err(dev, "Couldn't map long term buffer,rc = %d\n",
194 adapter->fw_done_rc);
195 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
196 return -1;
197 }
198 return 0;
199}
200
201static void free_long_term_buff(struct ibmvnic_adapter *adapter,
202 struct ibmvnic_long_term_buff *ltb)
203{
204 struct device *dev = &adapter->vdev->dev;
205
206 if (!ltb->buff)
207 return;
208
209 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
210 adapter->reset_reason != VNIC_RESET_MOBILITY)
211 send_request_unmap(adapter, ltb->map_id);
212 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
213}
214
215static int reset_long_term_buff(struct ibmvnic_adapter *adapter,
216 struct ibmvnic_long_term_buff *ltb)
217{
218 memset(ltb->buff, 0, ltb->size);
219
220 init_completion(&adapter->fw_done);
221 send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
222 wait_for_completion(&adapter->fw_done);
223
224 if (adapter->fw_done_rc) {
225 dev_info(&adapter->vdev->dev,
226 "Reset failed, attempting to free and reallocate buffer\n");
227 free_long_term_buff(adapter, ltb);
228 return alloc_long_term_buff(adapter, ltb, ltb->size);
229 }
230 return 0;
231}
232
233static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
234{
235 int i;
236
237 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
238 i++)
239 adapter->rx_pool[i].active = 0;
240}
241
242static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
243 struct ibmvnic_rx_pool *pool)
244{
245 int count = pool->size - atomic_read(&pool->available);
246 struct device *dev = &adapter->vdev->dev;
247 int buffers_added = 0;
248 unsigned long lpar_rc;
249 union sub_crq sub_crq;
250 struct sk_buff *skb;
251 unsigned int offset;
252 dma_addr_t dma_addr;
253 unsigned char *dst;
254 u64 *handle_array;
255 int shift = 0;
256 int index;
257 int i;
258
259 if (!pool->active)
260 return;
261
262 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
263 be32_to_cpu(adapter->login_rsp_buf->
264 off_rxadd_subcrqs));
265
266 for (i = 0; i < count; ++i) {
267 skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
268 if (!skb) {
269 dev_err(dev, "Couldn't replenish rx buff\n");
270 adapter->replenish_no_mem++;
271 break;
272 }
273
274 index = pool->free_map[pool->next_free];
275
276 if (pool->rx_buff[index].skb)
277 dev_err(dev, "Inconsistent free_map!\n");
278
279 /* Copy the skb to the long term mapped DMA buffer */
280 offset = index * pool->buff_size;
281 dst = pool->long_term_buff.buff + offset;
282 memset(dst, 0, pool->buff_size);
283 dma_addr = pool->long_term_buff.addr + offset;
284 pool->rx_buff[index].data = dst;
285
286 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
287 pool->rx_buff[index].dma = dma_addr;
288 pool->rx_buff[index].skb = skb;
289 pool->rx_buff[index].pool_index = pool->index;
290 pool->rx_buff[index].size = pool->buff_size;
291
292 memset(&sub_crq, 0, sizeof(sub_crq));
293 sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
294 sub_crq.rx_add.correlator =
295 cpu_to_be64((u64)&pool->rx_buff[index]);
296 sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
297 sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
298
299 /* The length field of the sCRQ is defined to be 24 bits so the
300 * buffer size needs to be left shifted by a byte before it is
301 * converted to big endian to prevent the last byte from being
302 * truncated.
303 */
304#ifdef __LITTLE_ENDIAN__
305 shift = 8;
306#endif
307 sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
308
309 lpar_rc = send_subcrq(adapter, handle_array[pool->index],
310 &sub_crq);
311 if (lpar_rc != H_SUCCESS)
312 goto failure;
313
314 buffers_added++;
315 adapter->replenish_add_buff_success++;
316 pool->next_free = (pool->next_free + 1) % pool->size;
317 }
318 atomic_add(buffers_added, &pool->available);
319 return;
320
321failure:
322 dev_info(dev, "replenish pools failure\n");
323 pool->free_map[pool->next_free] = index;
324 pool->rx_buff[index].skb = NULL;
325
326 dev_kfree_skb_any(skb);
327 adapter->replenish_add_buff_failure++;
328 atomic_add(buffers_added, &pool->available);
329
330 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
331 /* Disable buffer pool replenishment and report carrier off if
332 * queue is closed or pending failover.
333 * Firmware guarantees that a signal will be sent to the
334 * driver, triggering a reset.
335 */
336 deactivate_rx_pools(adapter);
337 netif_carrier_off(adapter->netdev);
338 }
339}
340
341static void replenish_pools(struct ibmvnic_adapter *adapter)
342{
343 int i;
344
345 adapter->replenish_task_cycles++;
346 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
347 i++) {
348 if (adapter->rx_pool[i].active)
349 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
350 }
351}
352
353static void release_stats_buffers(struct ibmvnic_adapter *adapter)
354{
355 kfree(adapter->tx_stats_buffers);
356 kfree(adapter->rx_stats_buffers);
357 adapter->tx_stats_buffers = NULL;
358 adapter->rx_stats_buffers = NULL;
359}
360
361static int init_stats_buffers(struct ibmvnic_adapter *adapter)
362{
363 adapter->tx_stats_buffers =
364 kcalloc(IBMVNIC_MAX_QUEUES,
365 sizeof(struct ibmvnic_tx_queue_stats),
366 GFP_KERNEL);
367 if (!adapter->tx_stats_buffers)
368 return -ENOMEM;
369
370 adapter->rx_stats_buffers =
371 kcalloc(IBMVNIC_MAX_QUEUES,
372 sizeof(struct ibmvnic_rx_queue_stats),
373 GFP_KERNEL);
374 if (!adapter->rx_stats_buffers)
375 return -ENOMEM;
376
377 return 0;
378}
379
380static void release_stats_token(struct ibmvnic_adapter *adapter)
381{
382 struct device *dev = &adapter->vdev->dev;
383
384 if (!adapter->stats_token)
385 return;
386
387 dma_unmap_single(dev, adapter->stats_token,
388 sizeof(struct ibmvnic_statistics),
389 DMA_FROM_DEVICE);
390 adapter->stats_token = 0;
391}
392
393static int init_stats_token(struct ibmvnic_adapter *adapter)
394{
395 struct device *dev = &adapter->vdev->dev;
396 dma_addr_t stok;
397
398 stok = dma_map_single(dev, &adapter->stats,
399 sizeof(struct ibmvnic_statistics),
400 DMA_FROM_DEVICE);
401 if (dma_mapping_error(dev, stok)) {
402 dev_err(dev, "Couldn't map stats buffer\n");
403 return -1;
404 }
405
406 adapter->stats_token = stok;
407 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
408 return 0;
409}
410
411static int reset_rx_pools(struct ibmvnic_adapter *adapter)
412{
413 struct ibmvnic_rx_pool *rx_pool;
414 int rx_scrqs;
415 int i, j, rc;
416 u64 *size_array;
417
418 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
419 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
420
421 rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
422 for (i = 0; i < rx_scrqs; i++) {
423 rx_pool = &adapter->rx_pool[i];
424
425 netdev_dbg(adapter->netdev, "Re-setting rx_pool[%d]\n", i);
426
427 if (rx_pool->buff_size != be64_to_cpu(size_array[i])) {
428 free_long_term_buff(adapter, &rx_pool->long_term_buff);
429 rx_pool->buff_size = be64_to_cpu(size_array[i]);
430 alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
431 rx_pool->size *
432 rx_pool->buff_size);
433 } else {
434 rc = reset_long_term_buff(adapter,
435 &rx_pool->long_term_buff);
436 }
437
438 if (rc)
439 return rc;
440
441 for (j = 0; j < rx_pool->size; j++)
442 rx_pool->free_map[j] = j;
443
444 memset(rx_pool->rx_buff, 0,
445 rx_pool->size * sizeof(struct ibmvnic_rx_buff));
446
447 atomic_set(&rx_pool->available, 0);
448 rx_pool->next_alloc = 0;
449 rx_pool->next_free = 0;
450 rx_pool->active = 1;
451 }
452
453 return 0;
454}
455
456static void release_rx_pools(struct ibmvnic_adapter *adapter)
457{
458 struct ibmvnic_rx_pool *rx_pool;
459 int i, j;
460
461 if (!adapter->rx_pool)
462 return;
463
464 for (i = 0; i < adapter->num_active_rx_pools; i++) {
465 rx_pool = &adapter->rx_pool[i];
466
467 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
468
469 kfree(rx_pool->free_map);
470 free_long_term_buff(adapter, &rx_pool->long_term_buff);
471
472 if (!rx_pool->rx_buff)
473 continue;
474
475 for (j = 0; j < rx_pool->size; j++) {
476 if (rx_pool->rx_buff[j].skb) {
477 dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
478 rx_pool->rx_buff[i].skb = NULL;
479 }
480 }
481
482 kfree(rx_pool->rx_buff);
483 }
484
485 kfree(adapter->rx_pool);
486 adapter->rx_pool = NULL;
487 adapter->num_active_rx_pools = 0;
488}
489
490static int init_rx_pools(struct net_device *netdev)
491{
492 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
493 struct device *dev = &adapter->vdev->dev;
494 struct ibmvnic_rx_pool *rx_pool;
495 int rxadd_subcrqs;
496 u64 *size_array;
497 int i, j;
498
499 rxadd_subcrqs =
500 be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
501 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
502 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
503
504 adapter->rx_pool = kcalloc(rxadd_subcrqs,
505 sizeof(struct ibmvnic_rx_pool),
506 GFP_KERNEL);
507 if (!adapter->rx_pool) {
508 dev_err(dev, "Failed to allocate rx pools\n");
509 return -1;
510 }
511
512 adapter->num_active_rx_pools = rxadd_subcrqs;
513
514 for (i = 0; i < rxadd_subcrqs; i++) {
515 rx_pool = &adapter->rx_pool[i];
516
517 netdev_dbg(adapter->netdev,
518 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
519 i, adapter->req_rx_add_entries_per_subcrq,
520 be64_to_cpu(size_array[i]));
521
522 rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
523 rx_pool->index = i;
524 rx_pool->buff_size = be64_to_cpu(size_array[i]);
525 rx_pool->active = 1;
526
527 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
528 GFP_KERNEL);
529 if (!rx_pool->free_map) {
530 release_rx_pools(adapter);
531 return -1;
532 }
533
534 rx_pool->rx_buff = kcalloc(rx_pool->size,
535 sizeof(struct ibmvnic_rx_buff),
536 GFP_KERNEL);
537 if (!rx_pool->rx_buff) {
538 dev_err(dev, "Couldn't alloc rx buffers\n");
539 release_rx_pools(adapter);
540 return -1;
541 }
542
543 if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
544 rx_pool->size * rx_pool->buff_size)) {
545 release_rx_pools(adapter);
546 return -1;
547 }
548
549 for (j = 0; j < rx_pool->size; ++j)
550 rx_pool->free_map[j] = j;
551
552 atomic_set(&rx_pool->available, 0);
553 rx_pool->next_alloc = 0;
554 rx_pool->next_free = 0;
555 }
556
557 return 0;
558}
559
560static int reset_one_tx_pool(struct ibmvnic_adapter *adapter,
561 struct ibmvnic_tx_pool *tx_pool)
562{
563 int rc, i;
564
565 rc = reset_long_term_buff(adapter, &tx_pool->long_term_buff);
566 if (rc)
567 return rc;
568
569 memset(tx_pool->tx_buff, 0,
570 tx_pool->num_buffers *
571 sizeof(struct ibmvnic_tx_buff));
572
573 for (i = 0; i < tx_pool->num_buffers; i++)
574 tx_pool->free_map[i] = i;
575
576 tx_pool->consumer_index = 0;
577 tx_pool->producer_index = 0;
578
579 return 0;
580}
581
582static int reset_tx_pools(struct ibmvnic_adapter *adapter)
583{
584 int tx_scrqs;
585 int i, rc;
586
587 tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
588 for (i = 0; i < tx_scrqs; i++) {
589 rc = reset_one_tx_pool(adapter, &adapter->tso_pool[i]);
590 if (rc)
591 return rc;
592 rc = reset_one_tx_pool(adapter, &adapter->tx_pool[i]);
593 if (rc)
594 return rc;
595 }
596
597 return 0;
598}
599
600static void release_vpd_data(struct ibmvnic_adapter *adapter)
601{
602 if (!adapter->vpd)
603 return;
604
605 kfree(adapter->vpd->buff);
606 kfree(adapter->vpd);
607
608 adapter->vpd = NULL;
609}
610
611static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
612 struct ibmvnic_tx_pool *tx_pool)
613{
614 kfree(tx_pool->tx_buff);
615 kfree(tx_pool->free_map);
616 free_long_term_buff(adapter, &tx_pool->long_term_buff);
617}
618
619static void release_tx_pools(struct ibmvnic_adapter *adapter)
620{
621 int i;
622
623 if (!adapter->tx_pool)
624 return;
625
626 for (i = 0; i < adapter->num_active_tx_pools; i++) {
627 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
628 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
629 }
630
631 kfree(adapter->tx_pool);
632 adapter->tx_pool = NULL;
633 kfree(adapter->tso_pool);
634 adapter->tso_pool = NULL;
635 adapter->num_active_tx_pools = 0;
636}
637
638static int init_one_tx_pool(struct net_device *netdev,
639 struct ibmvnic_tx_pool *tx_pool,
640 int num_entries, int buf_size)
641{
642 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
643 int i;
644
645 tx_pool->tx_buff = kcalloc(num_entries,
646 sizeof(struct ibmvnic_tx_buff),
647 GFP_KERNEL);
648 if (!tx_pool->tx_buff)
649 return -1;
650
651 if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
652 num_entries * buf_size))
653 return -1;
654
655 tx_pool->free_map = kcalloc(num_entries, sizeof(int), GFP_KERNEL);
656 if (!tx_pool->free_map)
657 return -1;
658
659 for (i = 0; i < num_entries; i++)
660 tx_pool->free_map[i] = i;
661
662 tx_pool->consumer_index = 0;
663 tx_pool->producer_index = 0;
664 tx_pool->num_buffers = num_entries;
665 tx_pool->buf_size = buf_size;
666
667 return 0;
668}
669
670static int init_tx_pools(struct net_device *netdev)
671{
672 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
673 int tx_subcrqs;
674 int i, rc;
675
676 tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
677 adapter->tx_pool = kcalloc(tx_subcrqs,
678 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
679 if (!adapter->tx_pool)
680 return -1;
681
682 adapter->tso_pool = kcalloc(tx_subcrqs,
683 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
684 if (!adapter->tso_pool)
685 return -1;
686
687 adapter->num_active_tx_pools = tx_subcrqs;
688
689 for (i = 0; i < tx_subcrqs; i++) {
690 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
691 adapter->req_tx_entries_per_subcrq,
692 adapter->req_mtu + VLAN_HLEN);
693 if (rc) {
694 release_tx_pools(adapter);
695 return rc;
696 }
697
698 init_one_tx_pool(netdev, &adapter->tso_pool[i],
699 IBMVNIC_TSO_BUFS,
700 IBMVNIC_TSO_BUF_SZ);
701 if (rc) {
702 release_tx_pools(adapter);
703 return rc;
704 }
705 }
706
707 return 0;
708}
709
710static void release_error_buffers(struct ibmvnic_adapter *adapter)
711{
712 struct device *dev = &adapter->vdev->dev;
713 struct ibmvnic_error_buff *error_buff, *tmp;
714 unsigned long flags;
715
716 spin_lock_irqsave(&adapter->error_list_lock, flags);
717 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
718 list_del(&error_buff->list);
719 dma_unmap_single(dev, error_buff->dma, error_buff->len,
720 DMA_FROM_DEVICE);
721 kfree(error_buff->buff);
722 kfree(error_buff);
723 }
724 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
725}
726
727static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
728{
729 int i;
730
731 if (adapter->napi_enabled)
732 return;
733
734 for (i = 0; i < adapter->req_rx_queues; i++)
735 napi_enable(&adapter->napi[i]);
736
737 adapter->napi_enabled = true;
738}
739
740static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
741{
742 int i;
743
744 if (!adapter->napi_enabled)
745 return;
746
747 for (i = 0; i < adapter->req_rx_queues; i++) {
748 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
749 napi_disable(&adapter->napi[i]);
750 }
751
752 adapter->napi_enabled = false;
753}
754
755static int init_napi(struct ibmvnic_adapter *adapter)
756{
757 int i;
758
759 adapter->napi = kcalloc(adapter->req_rx_queues,
760 sizeof(struct napi_struct), GFP_KERNEL);
761 if (!adapter->napi)
762 return -ENOMEM;
763
764 for (i = 0; i < adapter->req_rx_queues; i++) {
765 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
766 netif_napi_add(adapter->netdev, &adapter->napi[i],
767 ibmvnic_poll, NAPI_POLL_WEIGHT);
768 }
769
770 adapter->num_active_rx_napi = adapter->req_rx_queues;
771 return 0;
772}
773
774static void release_napi(struct ibmvnic_adapter *adapter)
775{
776 int i;
777
778 if (!adapter->napi)
779 return;
780
781 for (i = 0; i < adapter->num_active_rx_napi; i++) {
782 if (&adapter->napi[i]) {
783 netdev_dbg(adapter->netdev,
784 "Releasing napi[%d]\n", i);
785 netif_napi_del(&adapter->napi[i]);
786 }
787 }
788
789 kfree(adapter->napi);
790 adapter->napi = NULL;
791 adapter->num_active_rx_napi = 0;
792}
793
794static int ibmvnic_login(struct net_device *netdev)
795{
796 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
797 unsigned long timeout = msecs_to_jiffies(30000);
798 int retry_count = 0;
799 bool retry;
800 int rc;
801
802 do {
803 retry = false;
804 if (retry_count > IBMVNIC_MAX_QUEUES) {
805 netdev_warn(netdev, "Login attempts exceeded\n");
806 return -1;
807 }
808
809 adapter->init_done_rc = 0;
810 reinit_completion(&adapter->init_done);
811 rc = send_login(adapter);
812 if (rc) {
813 netdev_warn(netdev, "Unable to login\n");
814 return rc;
815 }
816
817 if (!wait_for_completion_timeout(&adapter->init_done,
818 timeout)) {
819 netdev_warn(netdev, "Login timed out\n");
820 return -1;
821 }
822
823 if (adapter->init_done_rc == PARTIALSUCCESS) {
824 retry_count++;
825 release_sub_crqs(adapter, 1);
826
827 retry = true;
828 netdev_dbg(netdev,
829 "Received partial success, retrying...\n");
830 adapter->init_done_rc = 0;
831 reinit_completion(&adapter->init_done);
832 send_cap_queries(adapter);
833 if (!wait_for_completion_timeout(&adapter->init_done,
834 timeout)) {
835 netdev_warn(netdev,
836 "Capabilities query timed out\n");
837 return -1;
838 }
839
840 rc = init_sub_crqs(adapter);
841 if (rc) {
842 netdev_warn(netdev,
843 "SCRQ initialization failed\n");
844 return -1;
845 }
846
847 rc = init_sub_crq_irqs(adapter);
848 if (rc) {
849 netdev_warn(netdev,
850 "SCRQ irq initialization failed\n");
851 return -1;
852 }
853 } else if (adapter->init_done_rc) {
854 netdev_warn(netdev, "Adapter login failed\n");
855 return -1;
856 }
857 } while (retry);
858
859 /* handle pending MAC address changes after successful login */
860 if (adapter->mac_change_pending) {
861 __ibmvnic_set_mac(netdev, &adapter->desired.mac);
862 adapter->mac_change_pending = false;
863 }
864
865 return 0;
866}
867
868static void release_login_buffer(struct ibmvnic_adapter *adapter)
869{
870 kfree(adapter->login_buf);
871 adapter->login_buf = NULL;
872}
873
874static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
875{
876 kfree(adapter->login_rsp_buf);
877 adapter->login_rsp_buf = NULL;
878}
879
880static void release_resources(struct ibmvnic_adapter *adapter)
881{
882 release_vpd_data(adapter);
883
884 release_tx_pools(adapter);
885 release_rx_pools(adapter);
886
887 release_error_buffers(adapter);
888 release_napi(adapter);
889 release_login_rsp_buffer(adapter);
890}
891
892static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
893{
894 struct net_device *netdev = adapter->netdev;
895 unsigned long timeout = msecs_to_jiffies(30000);
896 union ibmvnic_crq crq;
897 bool resend;
898 int rc;
899
900 netdev_dbg(netdev, "setting link state %d\n", link_state);
901
902 memset(&crq, 0, sizeof(crq));
903 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
904 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
905 crq.logical_link_state.link_state = link_state;
906
907 do {
908 resend = false;
909
910 reinit_completion(&adapter->init_done);
911 rc = ibmvnic_send_crq(adapter, &crq);
912 if (rc) {
913 netdev_err(netdev, "Failed to set link state\n");
914 return rc;
915 }
916
917 if (!wait_for_completion_timeout(&adapter->init_done,
918 timeout)) {
919 netdev_err(netdev, "timeout setting link state\n");
920 return -1;
921 }
922
923 if (adapter->init_done_rc == 1) {
924 /* Partuial success, delay and re-send */
925 mdelay(1000);
926 resend = true;
927 }
928 } while (resend);
929
930 return 0;
931}
932
933static int set_real_num_queues(struct net_device *netdev)
934{
935 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
936 int rc;
937
938 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
939 adapter->req_tx_queues, adapter->req_rx_queues);
940
941 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
942 if (rc) {
943 netdev_err(netdev, "failed to set the number of tx queues\n");
944 return rc;
945 }
946
947 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
948 if (rc)
949 netdev_err(netdev, "failed to set the number of rx queues\n");
950
951 return rc;
952}
953
954static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
955{
956 struct device *dev = &adapter->vdev->dev;
957 union ibmvnic_crq crq;
958 int len = 0;
959
960 if (adapter->vpd->buff)
961 len = adapter->vpd->len;
962
963 init_completion(&adapter->fw_done);
964 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
965 crq.get_vpd_size.cmd = GET_VPD_SIZE;
966 ibmvnic_send_crq(adapter, &crq);
967 wait_for_completion(&adapter->fw_done);
968
969 if (!adapter->vpd->len)
970 return -ENODATA;
971
972 if (!adapter->vpd->buff)
973 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
974 else if (adapter->vpd->len != len)
975 adapter->vpd->buff =
976 krealloc(adapter->vpd->buff,
977 adapter->vpd->len, GFP_KERNEL);
978
979 if (!adapter->vpd->buff) {
980 dev_err(dev, "Could allocate VPD buffer\n");
981 return -ENOMEM;
982 }
983
984 adapter->vpd->dma_addr =
985 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
986 DMA_FROM_DEVICE);
987 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
988 dev_err(dev, "Could not map VPD buffer\n");
989 kfree(adapter->vpd->buff);
990 adapter->vpd->buff = NULL;
991 return -ENOMEM;
992 }
993
994 reinit_completion(&adapter->fw_done);
995 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
996 crq.get_vpd.cmd = GET_VPD;
997 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
998 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
999 ibmvnic_send_crq(adapter, &crq);
1000 wait_for_completion(&adapter->fw_done);
1001
1002 return 0;
1003}
1004
1005static int init_resources(struct ibmvnic_adapter *adapter)
1006{
1007 struct net_device *netdev = adapter->netdev;
1008 int rc;
1009
1010 rc = set_real_num_queues(netdev);
1011 if (rc)
1012 return rc;
1013
1014 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1015 if (!adapter->vpd)
1016 return -ENOMEM;
1017
1018 /* Vital Product Data (VPD) */
1019 rc = ibmvnic_get_vpd(adapter);
1020 if (rc) {
1021 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1022 return rc;
1023 }
1024
1025 adapter->map_id = 1;
1026
1027 rc = init_napi(adapter);
1028 if (rc)
1029 return rc;
1030
1031 send_map_query(adapter);
1032
1033 rc = init_rx_pools(netdev);
1034 if (rc)
1035 return rc;
1036
1037 rc = init_tx_pools(netdev);
1038 return rc;
1039}
1040
1041static int __ibmvnic_open(struct net_device *netdev)
1042{
1043 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1044 enum vnic_state prev_state = adapter->state;
1045 int i, rc;
1046
1047 adapter->state = VNIC_OPENING;
1048 replenish_pools(adapter);
1049 ibmvnic_napi_enable(adapter);
1050
1051 /* We're ready to receive frames, enable the sub-crq interrupts and
1052 * set the logical link state to up
1053 */
1054 for (i = 0; i < adapter->req_rx_queues; i++) {
1055 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1056 if (prev_state == VNIC_CLOSED)
1057 enable_irq(adapter->rx_scrq[i]->irq);
1058 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1059 }
1060
1061 for (i = 0; i < adapter->req_tx_queues; i++) {
1062 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1063 if (prev_state == VNIC_CLOSED)
1064 enable_irq(adapter->tx_scrq[i]->irq);
1065 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1066 }
1067
1068 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1069 if (rc) {
1070 for (i = 0; i < adapter->req_rx_queues; i++)
1071 napi_disable(&adapter->napi[i]);
1072 release_resources(adapter);
1073 return rc;
1074 }
1075
1076 netif_tx_start_all_queues(netdev);
1077
1078 if (prev_state == VNIC_CLOSED) {
1079 for (i = 0; i < adapter->req_rx_queues; i++)
1080 napi_schedule(&adapter->napi[i]);
1081 }
1082
1083 adapter->state = VNIC_OPEN;
1084 return rc;
1085}
1086
1087static int ibmvnic_open(struct net_device *netdev)
1088{
1089 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1090 int rc;
1091
1092 /* If device failover is pending, just set device state and return.
1093 * Device operation will be handled by reset routine.
1094 */
1095 if (adapter->failover_pending) {
1096 adapter->state = VNIC_OPEN;
1097 return 0;
1098 }
1099
1100 mutex_lock(&adapter->reset_lock);
1101
1102 if (adapter->state != VNIC_CLOSED) {
1103 rc = ibmvnic_login(netdev);
1104 if (rc) {
1105 mutex_unlock(&adapter->reset_lock);
1106 return rc;
1107 }
1108
1109 rc = init_resources(adapter);
1110 if (rc) {
1111 netdev_err(netdev, "failed to initialize resources\n");
1112 release_resources(adapter);
1113 mutex_unlock(&adapter->reset_lock);
1114 return rc;
1115 }
1116 }
1117
1118 rc = __ibmvnic_open(netdev);
1119 netif_carrier_on(netdev);
1120
1121 mutex_unlock(&adapter->reset_lock);
1122
1123 return rc;
1124}
1125
1126static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1127{
1128 struct ibmvnic_rx_pool *rx_pool;
1129 struct ibmvnic_rx_buff *rx_buff;
1130 u64 rx_entries;
1131 int rx_scrqs;
1132 int i, j;
1133
1134 if (!adapter->rx_pool)
1135 return;
1136
1137 rx_scrqs = adapter->num_active_rx_pools;
1138 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1139
1140 /* Free any remaining skbs in the rx buffer pools */
1141 for (i = 0; i < rx_scrqs; i++) {
1142 rx_pool = &adapter->rx_pool[i];
1143 if (!rx_pool || !rx_pool->rx_buff)
1144 continue;
1145
1146 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1147 for (j = 0; j < rx_entries; j++) {
1148 rx_buff = &rx_pool->rx_buff[j];
1149 if (rx_buff && rx_buff->skb) {
1150 dev_kfree_skb_any(rx_buff->skb);
1151 rx_buff->skb = NULL;
1152 }
1153 }
1154 }
1155}
1156
1157static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1158 struct ibmvnic_tx_pool *tx_pool)
1159{
1160 struct ibmvnic_tx_buff *tx_buff;
1161 u64 tx_entries;
1162 int i;
1163
1164 if (!tx_pool || !tx_pool->tx_buff)
1165 return;
1166
1167 tx_entries = tx_pool->num_buffers;
1168
1169 for (i = 0; i < tx_entries; i++) {
1170 tx_buff = &tx_pool->tx_buff[i];
1171 if (tx_buff && tx_buff->skb) {
1172 dev_kfree_skb_any(tx_buff->skb);
1173 tx_buff->skb = NULL;
1174 }
1175 }
1176}
1177
1178static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1179{
1180 int tx_scrqs;
1181 int i;
1182
1183 if (!adapter->tx_pool || !adapter->tso_pool)
1184 return;
1185
1186 tx_scrqs = adapter->num_active_tx_pools;
1187
1188 /* Free any remaining skbs in the tx buffer pools */
1189 for (i = 0; i < tx_scrqs; i++) {
1190 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1191 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1192 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1193 }
1194}
1195
1196static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1197{
1198 struct net_device *netdev = adapter->netdev;
1199 int i;
1200
1201 if (adapter->tx_scrq) {
1202 for (i = 0; i < adapter->req_tx_queues; i++)
1203 if (adapter->tx_scrq[i]->irq) {
1204 netdev_dbg(netdev,
1205 "Disabling tx_scrq[%d] irq\n", i);
1206 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1207 disable_irq(adapter->tx_scrq[i]->irq);
1208 }
1209 }
1210
1211 if (adapter->rx_scrq) {
1212 for (i = 0; i < adapter->req_rx_queues; i++) {
1213 if (adapter->rx_scrq[i]->irq) {
1214 netdev_dbg(netdev,
1215 "Disabling rx_scrq[%d] irq\n", i);
1216 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1217 disable_irq(adapter->rx_scrq[i]->irq);
1218 }
1219 }
1220 }
1221}
1222
1223static void ibmvnic_cleanup(struct net_device *netdev)
1224{
1225 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1226
1227 /* ensure that transmissions are stopped if called by do_reset */
1228 if (adapter->resetting)
1229 netif_tx_disable(netdev);
1230 else
1231 netif_tx_stop_all_queues(netdev);
1232
1233 ibmvnic_napi_disable(adapter);
1234 ibmvnic_disable_irqs(adapter);
1235
1236 clean_rx_pools(adapter);
1237 clean_tx_pools(adapter);
1238}
1239
1240static int __ibmvnic_close(struct net_device *netdev)
1241{
1242 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1243 int rc = 0;
1244
1245 adapter->state = VNIC_CLOSING;
1246 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
1247 if (rc)
1248 return rc;
1249 adapter->state = VNIC_CLOSED;
1250 return 0;
1251}
1252
1253static int ibmvnic_close(struct net_device *netdev)
1254{
1255 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1256 int rc;
1257
1258 /* If device failover is pending, just set device state and return.
1259 * Device operation will be handled by reset routine.
1260 */
1261 if (adapter->failover_pending) {
1262 adapter->state = VNIC_CLOSED;
1263 return 0;
1264 }
1265
1266 mutex_lock(&adapter->reset_lock);
1267 rc = __ibmvnic_close(netdev);
1268 ibmvnic_cleanup(netdev);
1269 mutex_unlock(&adapter->reset_lock);
1270
1271 return rc;
1272}
1273
1274/**
1275 * build_hdr_data - creates L2/L3/L4 header data buffer
1276 * @hdr_field - bitfield determining needed headers
1277 * @skb - socket buffer
1278 * @hdr_len - array of header lengths
1279 * @tot_len - total length of data
1280 *
1281 * Reads hdr_field to determine which headers are needed by firmware.
1282 * Builds a buffer containing these headers. Saves individual header
1283 * lengths and total buffer length to be used to build descriptors.
1284 */
1285static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
1286 int *hdr_len, u8 *hdr_data)
1287{
1288 int len = 0;
1289 u8 *hdr;
1290
1291 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
1292 hdr_len[0] = sizeof(struct vlan_ethhdr);
1293 else
1294 hdr_len[0] = sizeof(struct ethhdr);
1295
1296 if (skb->protocol == htons(ETH_P_IP)) {
1297 hdr_len[1] = ip_hdr(skb)->ihl * 4;
1298 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1299 hdr_len[2] = tcp_hdrlen(skb);
1300 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1301 hdr_len[2] = sizeof(struct udphdr);
1302 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1303 hdr_len[1] = sizeof(struct ipv6hdr);
1304 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
1305 hdr_len[2] = tcp_hdrlen(skb);
1306 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
1307 hdr_len[2] = sizeof(struct udphdr);
1308 } else if (skb->protocol == htons(ETH_P_ARP)) {
1309 hdr_len[1] = arp_hdr_len(skb->dev);
1310 hdr_len[2] = 0;
1311 }
1312
1313 memset(hdr_data, 0, 120);
1314 if ((hdr_field >> 6) & 1) {
1315 hdr = skb_mac_header(skb);
1316 memcpy(hdr_data, hdr, hdr_len[0]);
1317 len += hdr_len[0];
1318 }
1319
1320 if ((hdr_field >> 5) & 1) {
1321 hdr = skb_network_header(skb);
1322 memcpy(hdr_data + len, hdr, hdr_len[1]);
1323 len += hdr_len[1];
1324 }
1325
1326 if ((hdr_field >> 4) & 1) {
1327 hdr = skb_transport_header(skb);
1328 memcpy(hdr_data + len, hdr, hdr_len[2]);
1329 len += hdr_len[2];
1330 }
1331 return len;
1332}
1333
1334/**
1335 * create_hdr_descs - create header and header extension descriptors
1336 * @hdr_field - bitfield determining needed headers
1337 * @data - buffer containing header data
1338 * @len - length of data buffer
1339 * @hdr_len - array of individual header lengths
1340 * @scrq_arr - descriptor array
1341 *
1342 * Creates header and, if needed, header extension descriptors and
1343 * places them in a descriptor array, scrq_arr
1344 */
1345
1346static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
1347 union sub_crq *scrq_arr)
1348{
1349 union sub_crq hdr_desc;
1350 int tmp_len = len;
1351 int num_descs = 0;
1352 u8 *data, *cur;
1353 int tmp;
1354
1355 while (tmp_len > 0) {
1356 cur = hdr_data + len - tmp_len;
1357
1358 memset(&hdr_desc, 0, sizeof(hdr_desc));
1359 if (cur != hdr_data) {
1360 data = hdr_desc.hdr_ext.data;
1361 tmp = tmp_len > 29 ? 29 : tmp_len;
1362 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
1363 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
1364 hdr_desc.hdr_ext.len = tmp;
1365 } else {
1366 data = hdr_desc.hdr.data;
1367 tmp = tmp_len > 24 ? 24 : tmp_len;
1368 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
1369 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
1370 hdr_desc.hdr.len = tmp;
1371 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
1372 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
1373 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
1374 hdr_desc.hdr.flag = hdr_field << 1;
1375 }
1376 memcpy(data, cur, tmp);
1377 tmp_len -= tmp;
1378 *scrq_arr = hdr_desc;
1379 scrq_arr++;
1380 num_descs++;
1381 }
1382
1383 return num_descs;
1384}
1385
1386/**
1387 * build_hdr_descs_arr - build a header descriptor array
1388 * @skb - socket buffer
1389 * @num_entries - number of descriptors to be sent
1390 * @subcrq - first TX descriptor
1391 * @hdr_field - bit field determining which headers will be sent
1392 *
1393 * This function will build a TX descriptor array with applicable
1394 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
1395 */
1396
1397static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
1398 int *num_entries, u8 hdr_field)
1399{
1400 int hdr_len[3] = {0, 0, 0};
1401 int tot_len;
1402 u8 *hdr_data = txbuff->hdr_data;
1403
1404 tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
1405 txbuff->hdr_data);
1406 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
1407 txbuff->indir_arr + 1);
1408}
1409
1410static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
1411 struct net_device *netdev)
1412{
1413 /* For some backing devices, mishandling of small packets
1414 * can result in a loss of connection or TX stall. Device
1415 * architects recommend that no packet should be smaller
1416 * than the minimum MTU value provided to the driver, so
1417 * pad any packets to that length
1418 */
1419 if (skb->len < netdev->min_mtu)
1420 return skb_put_padto(skb, netdev->min_mtu);
1421
1422 return 0;
1423}
1424
1425static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
1426{
1427 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1428 int queue_num = skb_get_queue_mapping(skb);
1429 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
1430 struct device *dev = &adapter->vdev->dev;
1431 struct ibmvnic_tx_buff *tx_buff = NULL;
1432 struct ibmvnic_sub_crq_queue *tx_scrq;
1433 struct ibmvnic_tx_pool *tx_pool;
1434 unsigned int tx_send_failed = 0;
1435 unsigned int tx_map_failed = 0;
1436 unsigned int tx_dropped = 0;
1437 unsigned int tx_packets = 0;
1438 unsigned int tx_bytes = 0;
1439 dma_addr_t data_dma_addr;
1440 struct netdev_queue *txq;
1441 unsigned long lpar_rc;
1442 union sub_crq tx_crq;
1443 unsigned int offset;
1444 int num_entries = 1;
1445 unsigned char *dst;
1446 u64 *handle_array;
1447 int index = 0;
1448 u8 proto = 0;
1449 int ret = 0;
1450
1451 if (adapter->resetting) {
1452 if (!netif_subqueue_stopped(netdev, skb))
1453 netif_stop_subqueue(netdev, queue_num);
1454 dev_kfree_skb_any(skb);
1455
1456 tx_send_failed++;
1457 tx_dropped++;
1458 ret = NETDEV_TX_OK;
1459 goto out;
1460 }
1461
1462 if (ibmvnic_xmit_workarounds(skb, netdev)) {
1463 tx_dropped++;
1464 tx_send_failed++;
1465 ret = NETDEV_TX_OK;
1466 goto out;
1467 }
1468 if (skb_is_gso(skb))
1469 tx_pool = &adapter->tso_pool[queue_num];
1470 else
1471 tx_pool = &adapter->tx_pool[queue_num];
1472
1473 tx_scrq = adapter->tx_scrq[queue_num];
1474 txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
1475 handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
1476 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
1477
1478 index = tx_pool->free_map[tx_pool->consumer_index];
1479
1480 if (index == IBMVNIC_INVALID_MAP) {
1481 dev_kfree_skb_any(skb);
1482 tx_send_failed++;
1483 tx_dropped++;
1484 ret = NETDEV_TX_OK;
1485 goto out;
1486 }
1487
1488 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
1489
1490 offset = index * tx_pool->buf_size;
1491 dst = tx_pool->long_term_buff.buff + offset;
1492 memset(dst, 0, tx_pool->buf_size);
1493 data_dma_addr = tx_pool->long_term_buff.addr + offset;
1494
1495 if (skb_shinfo(skb)->nr_frags) {
1496 int cur, i;
1497
1498 /* Copy the head */
1499 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
1500 cur = skb_headlen(skb);
1501
1502 /* Copy the frags */
1503 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1504 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1505
1506 memcpy(dst + cur,
1507 page_address(skb_frag_page(frag)) +
1508 frag->page_offset, skb_frag_size(frag));
1509 cur += skb_frag_size(frag);
1510 }
1511 } else {
1512 skb_copy_from_linear_data(skb, dst, skb->len);
1513 }
1514
1515 tx_pool->consumer_index =
1516 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
1517
1518 tx_buff = &tx_pool->tx_buff[index];
1519 tx_buff->skb = skb;
1520 tx_buff->data_dma[0] = data_dma_addr;
1521 tx_buff->data_len[0] = skb->len;
1522 tx_buff->index = index;
1523 tx_buff->pool_index = queue_num;
1524 tx_buff->last_frag = true;
1525
1526 memset(&tx_crq, 0, sizeof(tx_crq));
1527 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
1528 tx_crq.v1.type = IBMVNIC_TX_DESC;
1529 tx_crq.v1.n_crq_elem = 1;
1530 tx_crq.v1.n_sge = 1;
1531 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
1532
1533 if (skb_is_gso(skb))
1534 tx_crq.v1.correlator =
1535 cpu_to_be32(index | IBMVNIC_TSO_POOL_MASK);
1536 else
1537 tx_crq.v1.correlator = cpu_to_be32(index);
1538 tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
1539 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
1540 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
1541
1542 if (adapter->vlan_header_insertion) {
1543 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
1544 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
1545 }
1546
1547 if (skb->protocol == htons(ETH_P_IP)) {
1548 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
1549 proto = ip_hdr(skb)->protocol;
1550 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1551 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
1552 proto = ipv6_hdr(skb)->nexthdr;
1553 }
1554
1555 if (proto == IPPROTO_TCP)
1556 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
1557 else if (proto == IPPROTO_UDP)
1558 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
1559
1560 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1561 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
1562 hdrs += 2;
1563 }
1564 if (skb_is_gso(skb)) {
1565 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
1566 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
1567 hdrs += 2;
1568 }
1569 /* determine if l2/3/4 headers are sent to firmware */
1570 if ((*hdrs >> 7) & 1) {
1571 build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
1572 tx_crq.v1.n_crq_elem = num_entries;
1573 tx_buff->num_entries = num_entries;
1574 tx_buff->indir_arr[0] = tx_crq;
1575 tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
1576 sizeof(tx_buff->indir_arr),
1577 DMA_TO_DEVICE);
1578 if (dma_mapping_error(dev, tx_buff->indir_dma)) {
1579 dev_kfree_skb_any(skb);
1580 tx_buff->skb = NULL;
1581 if (!firmware_has_feature(FW_FEATURE_CMO))
1582 dev_err(dev, "tx: unable to map descriptor array\n");
1583 tx_map_failed++;
1584 tx_dropped++;
1585 ret = NETDEV_TX_OK;
1586 goto tx_err_out;
1587 }
1588 lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
1589 (u64)tx_buff->indir_dma,
1590 (u64)num_entries);
1591 } else {
1592 tx_buff->num_entries = num_entries;
1593 lpar_rc = send_subcrq(adapter, handle_array[queue_num],
1594 &tx_crq);
1595 }
1596 if (lpar_rc != H_SUCCESS) {
1597 dev_err(dev, "tx failed with code %ld\n", lpar_rc);
1598 dev_kfree_skb_any(skb);
1599 tx_buff->skb = NULL;
1600
1601 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
1602 /* Disable TX and report carrier off if queue is closed
1603 * or pending failover.
1604 * Firmware guarantees that a signal will be sent to the
1605 * driver, triggering a reset or some other action.
1606 */
1607 netif_tx_stop_all_queues(netdev);
1608 netif_carrier_off(netdev);
1609 }
1610
1611 tx_send_failed++;
1612 tx_dropped++;
1613 ret = NETDEV_TX_OK;
1614 goto tx_err_out;
1615 }
1616
1617 if (atomic_add_return(num_entries, &tx_scrq->used)
1618 >= adapter->req_tx_entries_per_subcrq) {
1619 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
1620 netif_stop_subqueue(netdev, queue_num);
1621 }
1622
1623 tx_packets++;
1624 tx_bytes += skb->len;
1625 txq->trans_start = jiffies;
1626 ret = NETDEV_TX_OK;
1627 goto out;
1628
1629tx_err_out:
1630 /* roll back consumer index and map array*/
1631 if (tx_pool->consumer_index == 0)
1632 tx_pool->consumer_index =
1633 tx_pool->num_buffers - 1;
1634 else
1635 tx_pool->consumer_index--;
1636 tx_pool->free_map[tx_pool->consumer_index] = index;
1637out:
1638 netdev->stats.tx_dropped += tx_dropped;
1639 netdev->stats.tx_bytes += tx_bytes;
1640 netdev->stats.tx_packets += tx_packets;
1641 adapter->tx_send_failed += tx_send_failed;
1642 adapter->tx_map_failed += tx_map_failed;
1643 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
1644 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
1645 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
1646
1647 return ret;
1648}
1649
1650static void ibmvnic_set_multi(struct net_device *netdev)
1651{
1652 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1653 struct netdev_hw_addr *ha;
1654 union ibmvnic_crq crq;
1655
1656 memset(&crq, 0, sizeof(crq));
1657 crq.request_capability.first = IBMVNIC_CRQ_CMD;
1658 crq.request_capability.cmd = REQUEST_CAPABILITY;
1659
1660 if (netdev->flags & IFF_PROMISC) {
1661 if (!adapter->promisc_supported)
1662 return;
1663 } else {
1664 if (netdev->flags & IFF_ALLMULTI) {
1665 /* Accept all multicast */
1666 memset(&crq, 0, sizeof(crq));
1667 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1668 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1669 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
1670 ibmvnic_send_crq(adapter, &crq);
1671 } else if (netdev_mc_empty(netdev)) {
1672 /* Reject all multicast */
1673 memset(&crq, 0, sizeof(crq));
1674 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1675 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1676 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
1677 ibmvnic_send_crq(adapter, &crq);
1678 } else {
1679 /* Accept one or more multicast(s) */
1680 netdev_for_each_mc_addr(ha, netdev) {
1681 memset(&crq, 0, sizeof(crq));
1682 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
1683 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
1684 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
1685 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
1686 ha->addr);
1687 ibmvnic_send_crq(adapter, &crq);
1688 }
1689 }
1690 }
1691}
1692
1693static int __ibmvnic_set_mac(struct net_device *netdev, struct sockaddr *p)
1694{
1695 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1696 struct sockaddr *addr = p;
1697 union ibmvnic_crq crq;
1698
1699 if (!is_valid_ether_addr(addr->sa_data))
1700 return -EADDRNOTAVAIL;
1701
1702 memset(&crq, 0, sizeof(crq));
1703 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
1704 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
1705 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
1706
1707 init_completion(&adapter->fw_done);
1708 ibmvnic_send_crq(adapter, &crq);
1709 wait_for_completion(&adapter->fw_done);
1710 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
1711 return adapter->fw_done_rc ? -EIO : 0;
1712}
1713
1714static int ibmvnic_set_mac(struct net_device *netdev, void *p)
1715{
1716 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1717 struct sockaddr *addr = p;
1718 int rc;
1719
1720 if (adapter->state == VNIC_PROBED) {
1721 memcpy(&adapter->desired.mac, addr, sizeof(struct sockaddr));
1722 adapter->mac_change_pending = true;
1723 return 0;
1724 }
1725
1726 rc = __ibmvnic_set_mac(netdev, addr);
1727
1728 return rc;
1729}
1730
1731/**
1732 * do_reset returns zero if we are able to keep processing reset events, or
1733 * non-zero if we hit a fatal error and must halt.
1734 */
1735static int do_reset(struct ibmvnic_adapter *adapter,
1736 struct ibmvnic_rwi *rwi, u32 reset_state)
1737{
1738 u64 old_num_rx_queues, old_num_tx_queues;
1739 struct net_device *netdev = adapter->netdev;
1740 int i, rc;
1741
1742 netdev_dbg(adapter->netdev, "Re-setting driver (%d)\n",
1743 rwi->reset_reason);
1744
1745 netif_carrier_off(netdev);
1746 adapter->reset_reason = rwi->reset_reason;
1747
1748 old_num_rx_queues = adapter->req_rx_queues;
1749 old_num_tx_queues = adapter->req_tx_queues;
1750
1751 ibmvnic_cleanup(netdev);
1752
1753 if (adapter->reset_reason != VNIC_RESET_MOBILITY &&
1754 adapter->reset_reason != VNIC_RESET_FAILOVER) {
1755 rc = __ibmvnic_close(netdev);
1756 if (rc)
1757 return rc;
1758 }
1759
1760 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1761 adapter->wait_for_reset) {
1762 release_resources(adapter);
1763 release_sub_crqs(adapter, 1);
1764 release_crq_queue(adapter);
1765 }
1766
1767 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
1768 /* remove the closed state so when we call open it appears
1769 * we are coming from the probed state.
1770 */
1771 adapter->state = VNIC_PROBED;
1772
1773 if (adapter->wait_for_reset) {
1774 rc = init_crq_queue(adapter);
1775 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
1776 rc = ibmvnic_reenable_crq_queue(adapter);
1777 release_sub_crqs(adapter, 1);
1778 } else {
1779 rc = ibmvnic_reset_crq(adapter);
1780 if (!rc)
1781 rc = vio_enable_interrupts(adapter->vdev);
1782 }
1783
1784 if (rc) {
1785 netdev_err(adapter->netdev,
1786 "Couldn't initialize crq. rc=%d\n", rc);
1787 return rc;
1788 }
1789
1790 rc = ibmvnic_init(adapter);
1791 if (rc)
1792 return IBMVNIC_INIT_FAILED;
1793
1794 /* If the adapter was in PROBE state prior to the reset,
1795 * exit here.
1796 */
1797 if (reset_state == VNIC_PROBED)
1798 return 0;
1799
1800 rc = ibmvnic_login(netdev);
1801 if (rc) {
1802 adapter->state = VNIC_PROBED;
1803 return 0;
1804 }
1805
1806 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM ||
1807 adapter->wait_for_reset) {
1808 rc = init_resources(adapter);
1809 if (rc)
1810 return rc;
1811 } else if (adapter->req_rx_queues != old_num_rx_queues ||
1812 adapter->req_tx_queues != old_num_tx_queues) {
1813 adapter->map_id = 1;
1814 release_rx_pools(adapter);
1815 release_tx_pools(adapter);
1816 init_rx_pools(netdev);
1817 init_tx_pools(netdev);
1818
1819 release_napi(adapter);
1820 init_napi(adapter);
1821 } else {
1822 rc = reset_tx_pools(adapter);
1823 if (rc)
1824 return rc;
1825
1826 rc = reset_rx_pools(adapter);
1827 if (rc)
1828 return rc;
1829 }
1830 ibmvnic_disable_irqs(adapter);
1831 }
1832 adapter->state = VNIC_CLOSED;
1833
1834 if (reset_state == VNIC_CLOSED)
1835 return 0;
1836
1837 rc = __ibmvnic_open(netdev);
1838 if (rc) {
1839 if (list_empty(&adapter->rwi_list))
1840 adapter->state = VNIC_CLOSED;
1841 else
1842 adapter->state = reset_state;
1843
1844 return 0;
1845 }
1846
1847 /* kick napi */
1848 for (i = 0; i < adapter->req_rx_queues; i++)
1849 napi_schedule(&adapter->napi[i]);
1850
1851 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
1852 adapter->reset_reason != VNIC_RESET_CHANGE_PARAM)
1853 netdev_notify_peers(netdev);
1854
1855 netif_carrier_on(netdev);
1856
1857 return 0;
1858}
1859
1860static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
1861{
1862 struct ibmvnic_rwi *rwi;
1863
1864 mutex_lock(&adapter->rwi_lock);
1865
1866 if (!list_empty(&adapter->rwi_list)) {
1867 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
1868 list);
1869 list_del(&rwi->list);
1870 } else {
1871 rwi = NULL;
1872 }
1873
1874 mutex_unlock(&adapter->rwi_lock);
1875 return rwi;
1876}
1877
1878static void free_all_rwi(struct ibmvnic_adapter *adapter)
1879{
1880 struct ibmvnic_rwi *rwi;
1881
1882 rwi = get_next_rwi(adapter);
1883 while (rwi) {
1884 kfree(rwi);
1885 rwi = get_next_rwi(adapter);
1886 }
1887}
1888
1889static void __ibmvnic_reset(struct work_struct *work)
1890{
1891 struct ibmvnic_rwi *rwi;
1892 struct ibmvnic_adapter *adapter;
1893 struct net_device *netdev;
1894 u32 reset_state;
1895 int rc = 0;
1896
1897 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
1898 netdev = adapter->netdev;
1899
1900 mutex_lock(&adapter->reset_lock);
1901 adapter->resetting = true;
1902 reset_state = adapter->state;
1903
1904 rwi = get_next_rwi(adapter);
1905 while (rwi) {
1906 rc = do_reset(adapter, rwi, reset_state);
1907 kfree(rwi);
1908 if (rc && rc != IBMVNIC_INIT_FAILED)
1909 break;
1910
1911 rwi = get_next_rwi(adapter);
1912 }
1913
1914 if (adapter->wait_for_reset) {
1915 adapter->wait_for_reset = false;
1916 adapter->reset_done_rc = rc;
1917 complete(&adapter->reset_done);
1918 }
1919
1920 if (rc) {
1921 netdev_dbg(adapter->netdev, "Reset failed\n");
1922 free_all_rwi(adapter);
1923 mutex_unlock(&adapter->reset_lock);
1924 return;
1925 }
1926
1927 adapter->resetting = false;
1928 mutex_unlock(&adapter->reset_lock);
1929}
1930
1931static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
1932 enum ibmvnic_reset_reason reason)
1933{
1934 struct ibmvnic_rwi *rwi, *tmp;
1935 struct net_device *netdev = adapter->netdev;
1936 struct list_head *entry;
1937 int ret;
1938
1939 if (adapter->state == VNIC_REMOVING ||
1940 adapter->state == VNIC_REMOVED ||
1941 adapter->failover_pending) {
1942 ret = EBUSY;
1943 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
1944 goto err;
1945 }
1946
1947 if (adapter->state == VNIC_PROBING) {
1948 netdev_warn(netdev, "Adapter reset during probe\n");
1949 ret = adapter->init_done_rc = EAGAIN;
1950 goto err;
1951 }
1952
1953 mutex_lock(&adapter->rwi_lock);
1954
1955 list_for_each(entry, &adapter->rwi_list) {
1956 tmp = list_entry(entry, struct ibmvnic_rwi, list);
1957 if (tmp->reset_reason == reason) {
1958 netdev_dbg(netdev, "Skipping matching reset\n");
1959 mutex_unlock(&adapter->rwi_lock);
1960 ret = EBUSY;
1961 goto err;
1962 }
1963 }
1964
1965 rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
1966 if (!rwi) {
1967 mutex_unlock(&adapter->rwi_lock);
1968 ibmvnic_close(netdev);
1969 ret = ENOMEM;
1970 goto err;
1971 }
1972
1973 rwi->reset_reason = reason;
1974 list_add_tail(&rwi->list, &adapter->rwi_list);
1975 mutex_unlock(&adapter->rwi_lock);
1976
1977 netdev_dbg(adapter->netdev, "Scheduling reset (reason %d)\n", reason);
1978 schedule_work(&adapter->ibmvnic_reset);
1979
1980 return 0;
1981err:
1982 if (adapter->wait_for_reset)
1983 adapter->wait_for_reset = false;
1984 return -ret;
1985}
1986
1987static void ibmvnic_tx_timeout(struct net_device *dev)
1988{
1989 struct ibmvnic_adapter *adapter = netdev_priv(dev);
1990
1991 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
1992}
1993
1994static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
1995 struct ibmvnic_rx_buff *rx_buff)
1996{
1997 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
1998
1999 rx_buff->skb = NULL;
2000
2001 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
2002 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
2003
2004 atomic_dec(&pool->available);
2005}
2006
2007static int ibmvnic_poll(struct napi_struct *napi, int budget)
2008{
2009 struct net_device *netdev = napi->dev;
2010 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2011 int scrq_num = (int)(napi - adapter->napi);
2012 int frames_processed = 0;
2013
2014restart_poll:
2015 while (frames_processed < budget) {
2016 struct sk_buff *skb;
2017 struct ibmvnic_rx_buff *rx_buff;
2018 union sub_crq *next;
2019 u32 length;
2020 u16 offset;
2021 u8 flags = 0;
2022
2023 if (unlikely(adapter->resetting &&
2024 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
2025 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2026 napi_complete_done(napi, frames_processed);
2027 return frames_processed;
2028 }
2029
2030 if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
2031 break;
2032 next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
2033 rx_buff =
2034 (struct ibmvnic_rx_buff *)be64_to_cpu(next->
2035 rx_comp.correlator);
2036 /* do error checking */
2037 if (next->rx_comp.rc) {
2038 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
2039 be16_to_cpu(next->rx_comp.rc));
2040 /* free the entry */
2041 next->rx_comp.first = 0;
2042 dev_kfree_skb_any(rx_buff->skb);
2043 remove_buff_from_pool(adapter, rx_buff);
2044 continue;
2045 } else if (!rx_buff->skb) {
2046 /* free the entry */
2047 next->rx_comp.first = 0;
2048 remove_buff_from_pool(adapter, rx_buff);
2049 continue;
2050 }
2051
2052 length = be32_to_cpu(next->rx_comp.len);
2053 offset = be16_to_cpu(next->rx_comp.off_frame_data);
2054 flags = next->rx_comp.flags;
2055 skb = rx_buff->skb;
2056 skb_copy_to_linear_data(skb, rx_buff->data + offset,
2057 length);
2058
2059 /* VLAN Header has been stripped by the system firmware and
2060 * needs to be inserted by the driver
2061 */
2062 if (adapter->rx_vlan_header_insertion &&
2063 (flags & IBMVNIC_VLAN_STRIPPED))
2064 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2065 ntohs(next->rx_comp.vlan_tci));
2066
2067 /* free the entry */
2068 next->rx_comp.first = 0;
2069 remove_buff_from_pool(adapter, rx_buff);
2070
2071 skb_put(skb, length);
2072 skb->protocol = eth_type_trans(skb, netdev);
2073 skb_record_rx_queue(skb, scrq_num);
2074
2075 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
2076 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
2077 skb->ip_summed = CHECKSUM_UNNECESSARY;
2078 }
2079
2080 length = skb->len;
2081 napi_gro_receive(napi, skb); /* send it up */
2082 netdev->stats.rx_packets++;
2083 netdev->stats.rx_bytes += length;
2084 adapter->rx_stats_buffers[scrq_num].packets++;
2085 adapter->rx_stats_buffers[scrq_num].bytes += length;
2086 frames_processed++;
2087 }
2088
2089 if (adapter->state != VNIC_CLOSING)
2090 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
2091
2092 if (frames_processed < budget) {
2093 enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2094 napi_complete_done(napi, frames_processed);
2095 if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
2096 napi_reschedule(napi)) {
2097 disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
2098 goto restart_poll;
2099 }
2100 }
2101 return frames_processed;
2102}
2103
2104#ifdef CONFIG_NET_POLL_CONTROLLER
2105static void ibmvnic_netpoll_controller(struct net_device *dev)
2106{
2107 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2108 int i;
2109
2110 replenish_pools(netdev_priv(dev));
2111 for (i = 0; i < adapter->req_rx_queues; i++)
2112 ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
2113 adapter->rx_scrq[i]);
2114}
2115#endif
2116
2117static int wait_for_reset(struct ibmvnic_adapter *adapter)
2118{
2119 int rc, ret;
2120
2121 adapter->fallback.mtu = adapter->req_mtu;
2122 adapter->fallback.rx_queues = adapter->req_rx_queues;
2123 adapter->fallback.tx_queues = adapter->req_tx_queues;
2124 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
2125 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
2126
2127 init_completion(&adapter->reset_done);
2128 adapter->wait_for_reset = true;
2129 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2130 if (rc)
2131 return rc;
2132 wait_for_completion(&adapter->reset_done);
2133
2134 ret = 0;
2135 if (adapter->reset_done_rc) {
2136 ret = -EIO;
2137 adapter->desired.mtu = adapter->fallback.mtu;
2138 adapter->desired.rx_queues = adapter->fallback.rx_queues;
2139 adapter->desired.tx_queues = adapter->fallback.tx_queues;
2140 adapter->desired.rx_entries = adapter->fallback.rx_entries;
2141 adapter->desired.tx_entries = adapter->fallback.tx_entries;
2142
2143 init_completion(&adapter->reset_done);
2144 adapter->wait_for_reset = true;
2145 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
2146 if (rc)
2147 return ret;
2148 wait_for_completion(&adapter->reset_done);
2149 }
2150 adapter->wait_for_reset = false;
2151
2152 return ret;
2153}
2154
2155static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
2156{
2157 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2158
2159 adapter->desired.mtu = new_mtu + ETH_HLEN;
2160
2161 return wait_for_reset(adapter);
2162}
2163
2164static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
2165 struct net_device *dev,
2166 netdev_features_t features)
2167{
2168 /* Some backing hardware adapters can not
2169 * handle packets with a MSS less than 224
2170 * or with only one segment.
2171 */
2172 if (skb_is_gso(skb)) {
2173 if (skb_shinfo(skb)->gso_size < 224 ||
2174 skb_shinfo(skb)->gso_segs == 1)
2175 features &= ~NETIF_F_GSO_MASK;
2176 }
2177
2178 return features;
2179}
2180
2181static const struct net_device_ops ibmvnic_netdev_ops = {
2182 .ndo_open = ibmvnic_open,
2183 .ndo_stop = ibmvnic_close,
2184 .ndo_start_xmit = ibmvnic_xmit,
2185 .ndo_set_rx_mode = ibmvnic_set_multi,
2186 .ndo_set_mac_address = ibmvnic_set_mac,
2187 .ndo_validate_addr = eth_validate_addr,
2188 .ndo_tx_timeout = ibmvnic_tx_timeout,
2189#ifdef CONFIG_NET_POLL_CONTROLLER
2190 .ndo_poll_controller = ibmvnic_netpoll_controller,
2191#endif
2192 .ndo_change_mtu = ibmvnic_change_mtu,
2193 .ndo_features_check = ibmvnic_features_check,
2194};
2195
2196/* ethtool functions */
2197
2198static int ibmvnic_get_link_ksettings(struct net_device *netdev,
2199 struct ethtool_link_ksettings *cmd)
2200{
2201 u32 supported, advertising;
2202
2203 supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
2204 SUPPORTED_FIBRE);
2205 advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
2206 ADVERTISED_FIBRE);
2207 cmd->base.speed = SPEED_1000;
2208 cmd->base.duplex = DUPLEX_FULL;
2209 cmd->base.port = PORT_FIBRE;
2210 cmd->base.phy_address = 0;
2211 cmd->base.autoneg = AUTONEG_ENABLE;
2212
2213 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2214 supported);
2215 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2216 advertising);
2217
2218 return 0;
2219}
2220
2221static void ibmvnic_get_drvinfo(struct net_device *netdev,
2222 struct ethtool_drvinfo *info)
2223{
2224 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2225
2226 strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
2227 strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
2228 strlcpy(info->fw_version, adapter->fw_version,
2229 sizeof(info->fw_version));
2230}
2231
2232static u32 ibmvnic_get_msglevel(struct net_device *netdev)
2233{
2234 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2235
2236 return adapter->msg_enable;
2237}
2238
2239static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
2240{
2241 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2242
2243 adapter->msg_enable = data;
2244}
2245
2246static u32 ibmvnic_get_link(struct net_device *netdev)
2247{
2248 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2249
2250 /* Don't need to send a query because we request a logical link up at
2251 * init and then we wait for link state indications
2252 */
2253 return adapter->logical_link_state;
2254}
2255
2256static void ibmvnic_get_ringparam(struct net_device *netdev,
2257 struct ethtool_ringparam *ring)
2258{
2259 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2260
2261 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
2262 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
2263 ring->rx_mini_max_pending = 0;
2264 ring->rx_jumbo_max_pending = 0;
2265 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
2266 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
2267 ring->rx_mini_pending = 0;
2268 ring->rx_jumbo_pending = 0;
2269}
2270
2271static int ibmvnic_set_ringparam(struct net_device *netdev,
2272 struct ethtool_ringparam *ring)
2273{
2274 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2275
2276 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
2277 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
2278 netdev_err(netdev, "Invalid request.\n");
2279 netdev_err(netdev, "Max tx buffers = %llu\n",
2280 adapter->max_rx_add_entries_per_subcrq);
2281 netdev_err(netdev, "Max rx buffers = %llu\n",
2282 adapter->max_tx_entries_per_subcrq);
2283 return -EINVAL;
2284 }
2285
2286 adapter->desired.rx_entries = ring->rx_pending;
2287 adapter->desired.tx_entries = ring->tx_pending;
2288
2289 return wait_for_reset(adapter);
2290}
2291
2292static void ibmvnic_get_channels(struct net_device *netdev,
2293 struct ethtool_channels *channels)
2294{
2295 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2296
2297 channels->max_rx = adapter->max_rx_queues;
2298 channels->max_tx = adapter->max_tx_queues;
2299 channels->max_other = 0;
2300 channels->max_combined = 0;
2301 channels->rx_count = adapter->req_rx_queues;
2302 channels->tx_count = adapter->req_tx_queues;
2303 channels->other_count = 0;
2304 channels->combined_count = 0;
2305}
2306
2307static int ibmvnic_set_channels(struct net_device *netdev,
2308 struct ethtool_channels *channels)
2309{
2310 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2311
2312 adapter->desired.rx_queues = channels->rx_count;
2313 adapter->desired.tx_queues = channels->tx_count;
2314
2315 return wait_for_reset(adapter);
2316}
2317
2318static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2319{
2320 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2321 int i;
2322
2323 if (stringset != ETH_SS_STATS)
2324 return;
2325
2326 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
2327 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
2328
2329 for (i = 0; i < adapter->req_tx_queues; i++) {
2330 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
2331 data += ETH_GSTRING_LEN;
2332
2333 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
2334 data += ETH_GSTRING_LEN;
2335
2336 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
2337 data += ETH_GSTRING_LEN;
2338 }
2339
2340 for (i = 0; i < adapter->req_rx_queues; i++) {
2341 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
2342 data += ETH_GSTRING_LEN;
2343
2344 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
2345 data += ETH_GSTRING_LEN;
2346
2347 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
2348 data += ETH_GSTRING_LEN;
2349 }
2350}
2351
2352static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
2353{
2354 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2355
2356 switch (sset) {
2357 case ETH_SS_STATS:
2358 return ARRAY_SIZE(ibmvnic_stats) +
2359 adapter->req_tx_queues * NUM_TX_STATS +
2360 adapter->req_rx_queues * NUM_RX_STATS;
2361 default:
2362 return -EOPNOTSUPP;
2363 }
2364}
2365
2366static void ibmvnic_get_ethtool_stats(struct net_device *dev,
2367 struct ethtool_stats *stats, u64 *data)
2368{
2369 struct ibmvnic_adapter *adapter = netdev_priv(dev);
2370 union ibmvnic_crq crq;
2371 int i, j;
2372
2373 memset(&crq, 0, sizeof(crq));
2374 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
2375 crq.request_statistics.cmd = REQUEST_STATISTICS;
2376 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
2377 crq.request_statistics.len =
2378 cpu_to_be32(sizeof(struct ibmvnic_statistics));
2379
2380 /* Wait for data to be written */
2381 init_completion(&adapter->stats_done);
2382 ibmvnic_send_crq(adapter, &crq);
2383 wait_for_completion(&adapter->stats_done);
2384
2385 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
2386 data[i] = be64_to_cpu(IBMVNIC_GET_STAT(adapter,
2387 ibmvnic_stats[i].offset));
2388
2389 for (j = 0; j < adapter->req_tx_queues; j++) {
2390 data[i] = adapter->tx_stats_buffers[j].packets;
2391 i++;
2392 data[i] = adapter->tx_stats_buffers[j].bytes;
2393 i++;
2394 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
2395 i++;
2396 }
2397
2398 for (j = 0; j < adapter->req_rx_queues; j++) {
2399 data[i] = adapter->rx_stats_buffers[j].packets;
2400 i++;
2401 data[i] = adapter->rx_stats_buffers[j].bytes;
2402 i++;
2403 data[i] = adapter->rx_stats_buffers[j].interrupts;
2404 i++;
2405 }
2406}
2407
2408static const struct ethtool_ops ibmvnic_ethtool_ops = {
2409 .get_drvinfo = ibmvnic_get_drvinfo,
2410 .get_msglevel = ibmvnic_get_msglevel,
2411 .set_msglevel = ibmvnic_set_msglevel,
2412 .get_link = ibmvnic_get_link,
2413 .get_ringparam = ibmvnic_get_ringparam,
2414 .set_ringparam = ibmvnic_set_ringparam,
2415 .get_channels = ibmvnic_get_channels,
2416 .set_channels = ibmvnic_set_channels,
2417 .get_strings = ibmvnic_get_strings,
2418 .get_sset_count = ibmvnic_get_sset_count,
2419 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
2420 .get_link_ksettings = ibmvnic_get_link_ksettings,
2421};
2422
2423/* Routines for managing CRQs/sCRQs */
2424
2425static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
2426 struct ibmvnic_sub_crq_queue *scrq)
2427{
2428 int rc;
2429
2430 if (scrq->irq) {
2431 free_irq(scrq->irq, scrq);
2432 irq_dispose_mapping(scrq->irq);
2433 scrq->irq = 0;
2434 }
2435
2436 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
2437 atomic_set(&scrq->used, 0);
2438 scrq->cur = 0;
2439
2440 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2441 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2442 return rc;
2443}
2444
2445static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
2446{
2447 int i, rc;
2448
2449 for (i = 0; i < adapter->req_tx_queues; i++) {
2450 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
2451 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
2452 if (rc)
2453 return rc;
2454 }
2455
2456 for (i = 0; i < adapter->req_rx_queues; i++) {
2457 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
2458 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
2459 if (rc)
2460 return rc;
2461 }
2462
2463 return rc;
2464}
2465
2466static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
2467 struct ibmvnic_sub_crq_queue *scrq,
2468 bool do_h_free)
2469{
2470 struct device *dev = &adapter->vdev->dev;
2471 long rc;
2472
2473 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
2474
2475 if (do_h_free) {
2476 /* Close the sub-crqs */
2477 do {
2478 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
2479 adapter->vdev->unit_address,
2480 scrq->crq_num);
2481 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
2482
2483 if (rc) {
2484 netdev_err(adapter->netdev,
2485 "Failed to release sub-CRQ %16lx, rc = %ld\n",
2486 scrq->crq_num, rc);
2487 }
2488 }
2489
2490 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2491 DMA_BIDIRECTIONAL);
2492 free_pages((unsigned long)scrq->msgs, 2);
2493 kfree(scrq);
2494}
2495
2496static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
2497 *adapter)
2498{
2499 struct device *dev = &adapter->vdev->dev;
2500 struct ibmvnic_sub_crq_queue *scrq;
2501 int rc;
2502
2503 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
2504 if (!scrq)
2505 return NULL;
2506
2507 scrq->msgs =
2508 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
2509 if (!scrq->msgs) {
2510 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
2511 goto zero_page_failed;
2512 }
2513
2514 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
2515 DMA_BIDIRECTIONAL);
2516 if (dma_mapping_error(dev, scrq->msg_token)) {
2517 dev_warn(dev, "Couldn't map crq queue messages page\n");
2518 goto map_failed;
2519 }
2520
2521 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
2522 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
2523
2524 if (rc == H_RESOURCE)
2525 rc = ibmvnic_reset_crq(adapter);
2526
2527 if (rc == H_CLOSED) {
2528 dev_warn(dev, "Partner adapter not ready, waiting.\n");
2529 } else if (rc) {
2530 dev_warn(dev, "Error %d registering sub-crq\n", rc);
2531 goto reg_failed;
2532 }
2533
2534 scrq->adapter = adapter;
2535 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
2536 spin_lock_init(&scrq->lock);
2537
2538 netdev_dbg(adapter->netdev,
2539 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
2540 scrq->crq_num, scrq->hw_irq, scrq->irq);
2541
2542 return scrq;
2543
2544reg_failed:
2545 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
2546 DMA_BIDIRECTIONAL);
2547map_failed:
2548 free_pages((unsigned long)scrq->msgs, 2);
2549zero_page_failed:
2550 kfree(scrq);
2551
2552 return NULL;
2553}
2554
2555static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
2556{
2557 int i;
2558
2559 if (adapter->tx_scrq) {
2560 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
2561 if (!adapter->tx_scrq[i])
2562 continue;
2563
2564 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
2565 i);
2566 if (adapter->tx_scrq[i]->irq) {
2567 free_irq(adapter->tx_scrq[i]->irq,
2568 adapter->tx_scrq[i]);
2569 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
2570 adapter->tx_scrq[i]->irq = 0;
2571 }
2572
2573 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
2574 do_h_free);
2575 }
2576
2577 kfree(adapter->tx_scrq);
2578 adapter->tx_scrq = NULL;
2579 adapter->num_active_tx_scrqs = 0;
2580 }
2581
2582 if (adapter->rx_scrq) {
2583 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
2584 if (!adapter->rx_scrq[i])
2585 continue;
2586
2587 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
2588 i);
2589 if (adapter->rx_scrq[i]->irq) {
2590 free_irq(adapter->rx_scrq[i]->irq,
2591 adapter->rx_scrq[i]);
2592 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
2593 adapter->rx_scrq[i]->irq = 0;
2594 }
2595
2596 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
2597 do_h_free);
2598 }
2599
2600 kfree(adapter->rx_scrq);
2601 adapter->rx_scrq = NULL;
2602 adapter->num_active_rx_scrqs = 0;
2603 }
2604}
2605
2606static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
2607 struct ibmvnic_sub_crq_queue *scrq)
2608{
2609 struct device *dev = &adapter->vdev->dev;
2610 unsigned long rc;
2611
2612 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2613 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2614 if (rc)
2615 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
2616 scrq->hw_irq, rc);
2617 return rc;
2618}
2619
2620static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
2621 struct ibmvnic_sub_crq_queue *scrq)
2622{
2623 struct device *dev = &adapter->vdev->dev;
2624 unsigned long rc;
2625
2626 if (scrq->hw_irq > 0x100000000ULL) {
2627 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
2628 return 1;
2629 }
2630
2631 if (adapter->resetting &&
2632 adapter->reset_reason == VNIC_RESET_MOBILITY) {
2633 u64 val = (0xff000000) | scrq->hw_irq;
2634
2635 rc = plpar_hcall_norets(H_EOI, val);
2636 if (rc)
2637 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n",
2638 val, rc);
2639 }
2640
2641 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
2642 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
2643 if (rc)
2644 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
2645 scrq->hw_irq, rc);
2646 return rc;
2647}
2648
2649static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
2650 struct ibmvnic_sub_crq_queue *scrq)
2651{
2652 struct device *dev = &adapter->vdev->dev;
2653 struct ibmvnic_tx_pool *tx_pool;
2654 struct ibmvnic_tx_buff *txbuff;
2655 union sub_crq *next;
2656 int index;
2657 int i, j;
2658 u8 *first;
2659
2660restart_loop:
2661 while (pending_scrq(adapter, scrq)) {
2662 unsigned int pool = scrq->pool_index;
2663 int num_entries = 0;
2664
2665 next = ibmvnic_next_scrq(adapter, scrq);
2666 for (i = 0; i < next->tx_comp.num_comps; i++) {
2667 if (next->tx_comp.rcs[i]) {
2668 dev_err(dev, "tx error %x\n",
2669 next->tx_comp.rcs[i]);
2670 continue;
2671 }
2672 index = be32_to_cpu(next->tx_comp.correlators[i]);
2673 if (index & IBMVNIC_TSO_POOL_MASK) {
2674 tx_pool = &adapter->tso_pool[pool];
2675 index &= ~IBMVNIC_TSO_POOL_MASK;
2676 } else {
2677 tx_pool = &adapter->tx_pool[pool];
2678 }
2679
2680 txbuff = &tx_pool->tx_buff[index];
2681
2682 for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
2683 if (!txbuff->data_dma[j])
2684 continue;
2685
2686 txbuff->data_dma[j] = 0;
2687 }
2688 /* if sub_crq was sent indirectly */
2689 first = &txbuff->indir_arr[0].generic.first;
2690 if (*first == IBMVNIC_CRQ_CMD) {
2691 dma_unmap_single(dev, txbuff->indir_dma,
2692 sizeof(txbuff->indir_arr),
2693 DMA_TO_DEVICE);
2694 *first = 0;
2695 }
2696
2697 if (txbuff->last_frag) {
2698 dev_kfree_skb_any(txbuff->skb);
2699 txbuff->skb = NULL;
2700 }
2701
2702 num_entries += txbuff->num_entries;
2703
2704 tx_pool->free_map[tx_pool->producer_index] = index;
2705 tx_pool->producer_index =
2706 (tx_pool->producer_index + 1) %
2707 tx_pool->num_buffers;
2708 }
2709 /* remove tx_comp scrq*/
2710 next->tx_comp.first = 0;
2711
2712 if (atomic_sub_return(num_entries, &scrq->used) <=
2713 (adapter->req_tx_entries_per_subcrq / 2) &&
2714 __netif_subqueue_stopped(adapter->netdev,
2715 scrq->pool_index)) {
2716 netif_wake_subqueue(adapter->netdev, scrq->pool_index);
2717 netdev_dbg(adapter->netdev, "Started queue %d\n",
2718 scrq->pool_index);
2719 }
2720 }
2721
2722 enable_scrq_irq(adapter, scrq);
2723
2724 if (pending_scrq(adapter, scrq)) {
2725 disable_scrq_irq(adapter, scrq);
2726 goto restart_loop;
2727 }
2728
2729 return 0;
2730}
2731
2732static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
2733{
2734 struct ibmvnic_sub_crq_queue *scrq = instance;
2735 struct ibmvnic_adapter *adapter = scrq->adapter;
2736
2737 disable_scrq_irq(adapter, scrq);
2738 ibmvnic_complete_tx(adapter, scrq);
2739
2740 return IRQ_HANDLED;
2741}
2742
2743static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
2744{
2745 struct ibmvnic_sub_crq_queue *scrq = instance;
2746 struct ibmvnic_adapter *adapter = scrq->adapter;
2747
2748 /* When booting a kdump kernel we can hit pending interrupts
2749 * prior to completing driver initialization.
2750 */
2751 if (unlikely(adapter->state != VNIC_OPEN))
2752 return IRQ_NONE;
2753
2754 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
2755
2756 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
2757 disable_scrq_irq(adapter, scrq);
2758 __napi_schedule(&adapter->napi[scrq->scrq_num]);
2759 }
2760
2761 return IRQ_HANDLED;
2762}
2763
2764static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
2765{
2766 struct device *dev = &adapter->vdev->dev;
2767 struct ibmvnic_sub_crq_queue *scrq;
2768 int i = 0, j = 0;
2769 int rc = 0;
2770
2771 for (i = 0; i < adapter->req_tx_queues; i++) {
2772 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
2773 i);
2774 scrq = adapter->tx_scrq[i];
2775 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2776
2777 if (!scrq->irq) {
2778 rc = -EINVAL;
2779 dev_err(dev, "Error mapping irq\n");
2780 goto req_tx_irq_failed;
2781 }
2782
2783 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
2784 0, "ibmvnic_tx", scrq);
2785
2786 if (rc) {
2787 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
2788 scrq->irq, rc);
2789 irq_dispose_mapping(scrq->irq);
2790 goto req_tx_irq_failed;
2791 }
2792 }
2793
2794 for (i = 0; i < adapter->req_rx_queues; i++) {
2795 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
2796 i);
2797 scrq = adapter->rx_scrq[i];
2798 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
2799 if (!scrq->irq) {
2800 rc = -EINVAL;
2801 dev_err(dev, "Error mapping irq\n");
2802 goto req_rx_irq_failed;
2803 }
2804 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
2805 0, "ibmvnic_rx", scrq);
2806 if (rc) {
2807 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
2808 scrq->irq, rc);
2809 irq_dispose_mapping(scrq->irq);
2810 goto req_rx_irq_failed;
2811 }
2812 }
2813 return rc;
2814
2815req_rx_irq_failed:
2816 for (j = 0; j < i; j++) {
2817 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
2818 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2819 }
2820 i = adapter->req_tx_queues;
2821req_tx_irq_failed:
2822 for (j = 0; j < i; j++) {
2823 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
2824 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
2825 }
2826 release_sub_crqs(adapter, 1);
2827 return rc;
2828}
2829
2830static int init_sub_crqs(struct ibmvnic_adapter *adapter)
2831{
2832 struct device *dev = &adapter->vdev->dev;
2833 struct ibmvnic_sub_crq_queue **allqueues;
2834 int registered_queues = 0;
2835 int total_queues;
2836 int more = 0;
2837 int i;
2838
2839 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
2840
2841 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
2842 if (!allqueues)
2843 return -1;
2844
2845 for (i = 0; i < total_queues; i++) {
2846 allqueues[i] = init_sub_crq_queue(adapter);
2847 if (!allqueues[i]) {
2848 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
2849 break;
2850 }
2851 registered_queues++;
2852 }
2853
2854 /* Make sure we were able to register the minimum number of queues */
2855 if (registered_queues <
2856 adapter->min_tx_queues + adapter->min_rx_queues) {
2857 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
2858 goto tx_failed;
2859 }
2860
2861 /* Distribute the failed allocated queues*/
2862 for (i = 0; i < total_queues - registered_queues + more ; i++) {
2863 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
2864 switch (i % 3) {
2865 case 0:
2866 if (adapter->req_rx_queues > adapter->min_rx_queues)
2867 adapter->req_rx_queues--;
2868 else
2869 more++;
2870 break;
2871 case 1:
2872 if (adapter->req_tx_queues > adapter->min_tx_queues)
2873 adapter->req_tx_queues--;
2874 else
2875 more++;
2876 break;
2877 }
2878 }
2879
2880 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
2881 sizeof(*adapter->tx_scrq), GFP_KERNEL);
2882 if (!adapter->tx_scrq)
2883 goto tx_failed;
2884
2885 for (i = 0; i < adapter->req_tx_queues; i++) {
2886 adapter->tx_scrq[i] = allqueues[i];
2887 adapter->tx_scrq[i]->pool_index = i;
2888 adapter->num_active_tx_scrqs++;
2889 }
2890
2891 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
2892 sizeof(*adapter->rx_scrq), GFP_KERNEL);
2893 if (!adapter->rx_scrq)
2894 goto rx_failed;
2895
2896 for (i = 0; i < adapter->req_rx_queues; i++) {
2897 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
2898 adapter->rx_scrq[i]->scrq_num = i;
2899 adapter->num_active_rx_scrqs++;
2900 }
2901
2902 kfree(allqueues);
2903 return 0;
2904
2905rx_failed:
2906 kfree(adapter->tx_scrq);
2907 adapter->tx_scrq = NULL;
2908tx_failed:
2909 for (i = 0; i < registered_queues; i++)
2910 release_sub_crq_queue(adapter, allqueues[i], 1);
2911 kfree(allqueues);
2912 return -1;
2913}
2914
2915static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
2916{
2917 struct device *dev = &adapter->vdev->dev;
2918 union ibmvnic_crq crq;
2919 int max_entries;
2920
2921 if (!retry) {
2922 /* Sub-CRQ entries are 32 byte long */
2923 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
2924
2925 if (adapter->min_tx_entries_per_subcrq > entries_page ||
2926 adapter->min_rx_add_entries_per_subcrq > entries_page) {
2927 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
2928 return;
2929 }
2930
2931 if (adapter->desired.mtu)
2932 adapter->req_mtu = adapter->desired.mtu;
2933 else
2934 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
2935
2936 if (!adapter->desired.tx_entries)
2937 adapter->desired.tx_entries =
2938 adapter->max_tx_entries_per_subcrq;
2939 if (!adapter->desired.rx_entries)
2940 adapter->desired.rx_entries =
2941 adapter->max_rx_add_entries_per_subcrq;
2942
2943 max_entries = IBMVNIC_MAX_LTB_SIZE /
2944 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
2945
2946 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2947 adapter->desired.tx_entries > IBMVNIC_MAX_LTB_SIZE) {
2948 adapter->desired.tx_entries = max_entries;
2949 }
2950
2951 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
2952 adapter->desired.rx_entries > IBMVNIC_MAX_LTB_SIZE) {
2953 adapter->desired.rx_entries = max_entries;
2954 }
2955
2956 if (adapter->desired.tx_entries)
2957 adapter->req_tx_entries_per_subcrq =
2958 adapter->desired.tx_entries;
2959 else
2960 adapter->req_tx_entries_per_subcrq =
2961 adapter->max_tx_entries_per_subcrq;
2962
2963 if (adapter->desired.rx_entries)
2964 adapter->req_rx_add_entries_per_subcrq =
2965 adapter->desired.rx_entries;
2966 else
2967 adapter->req_rx_add_entries_per_subcrq =
2968 adapter->max_rx_add_entries_per_subcrq;
2969
2970 if (adapter->desired.tx_queues)
2971 adapter->req_tx_queues =
2972 adapter->desired.tx_queues;
2973 else
2974 adapter->req_tx_queues =
2975 adapter->opt_tx_comp_sub_queues;
2976
2977 if (adapter->desired.rx_queues)
2978 adapter->req_rx_queues =
2979 adapter->desired.rx_queues;
2980 else
2981 adapter->req_rx_queues =
2982 adapter->opt_rx_comp_queues;
2983
2984 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
2985 }
2986
2987 memset(&crq, 0, sizeof(crq));
2988 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2989 crq.request_capability.cmd = REQUEST_CAPABILITY;
2990
2991 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
2992 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
2993 atomic_inc(&adapter->running_cap_crqs);
2994 ibmvnic_send_crq(adapter, &crq);
2995
2996 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
2997 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
2998 atomic_inc(&adapter->running_cap_crqs);
2999 ibmvnic_send_crq(adapter, &crq);
3000
3001 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
3002 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
3003 atomic_inc(&adapter->running_cap_crqs);
3004 ibmvnic_send_crq(adapter, &crq);
3005
3006 crq.request_capability.capability =
3007 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
3008 crq.request_capability.number =
3009 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
3010 atomic_inc(&adapter->running_cap_crqs);
3011 ibmvnic_send_crq(adapter, &crq);
3012
3013 crq.request_capability.capability =
3014 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
3015 crq.request_capability.number =
3016 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
3017 atomic_inc(&adapter->running_cap_crqs);
3018 ibmvnic_send_crq(adapter, &crq);
3019
3020 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
3021 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
3022 atomic_inc(&adapter->running_cap_crqs);
3023 ibmvnic_send_crq(adapter, &crq);
3024
3025 if (adapter->netdev->flags & IFF_PROMISC) {
3026 if (adapter->promisc_supported) {
3027 crq.request_capability.capability =
3028 cpu_to_be16(PROMISC_REQUESTED);
3029 crq.request_capability.number = cpu_to_be64(1);
3030 atomic_inc(&adapter->running_cap_crqs);
3031 ibmvnic_send_crq(adapter, &crq);
3032 }
3033 } else {
3034 crq.request_capability.capability =
3035 cpu_to_be16(PROMISC_REQUESTED);
3036 crq.request_capability.number = cpu_to_be64(0);
3037 atomic_inc(&adapter->running_cap_crqs);
3038 ibmvnic_send_crq(adapter, &crq);
3039 }
3040}
3041
3042static int pending_scrq(struct ibmvnic_adapter *adapter,
3043 struct ibmvnic_sub_crq_queue *scrq)
3044{
3045 union sub_crq *entry = &scrq->msgs[scrq->cur];
3046
3047 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP)
3048 return 1;
3049 else
3050 return 0;
3051}
3052
3053static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
3054 struct ibmvnic_sub_crq_queue *scrq)
3055{
3056 union sub_crq *entry;
3057 unsigned long flags;
3058
3059 spin_lock_irqsave(&scrq->lock, flags);
3060 entry = &scrq->msgs[scrq->cur];
3061 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3062 if (++scrq->cur == scrq->size)
3063 scrq->cur = 0;
3064 } else {
3065 entry = NULL;
3066 }
3067 spin_unlock_irqrestore(&scrq->lock, flags);
3068
3069 return entry;
3070}
3071
3072static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
3073{
3074 struct ibmvnic_crq_queue *queue = &adapter->crq;
3075 union ibmvnic_crq *crq;
3076
3077 crq = &queue->msgs[queue->cur];
3078 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
3079 if (++queue->cur == queue->size)
3080 queue->cur = 0;
3081 } else {
3082 crq = NULL;
3083 }
3084
3085 return crq;
3086}
3087
3088static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
3089 union sub_crq *sub_crq)
3090{
3091 unsigned int ua = adapter->vdev->unit_address;
3092 struct device *dev = &adapter->vdev->dev;
3093 u64 *u64_crq = (u64 *)sub_crq;
3094 int rc;
3095
3096 netdev_dbg(adapter->netdev,
3097 "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
3098 (unsigned long int)cpu_to_be64(remote_handle),
3099 (unsigned long int)cpu_to_be64(u64_crq[0]),
3100 (unsigned long int)cpu_to_be64(u64_crq[1]),
3101 (unsigned long int)cpu_to_be64(u64_crq[2]),
3102 (unsigned long int)cpu_to_be64(u64_crq[3]));
3103
3104 /* Make sure the hypervisor sees the complete request */
3105 mb();
3106
3107 rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
3108 cpu_to_be64(remote_handle),
3109 cpu_to_be64(u64_crq[0]),
3110 cpu_to_be64(u64_crq[1]),
3111 cpu_to_be64(u64_crq[2]),
3112 cpu_to_be64(u64_crq[3]));
3113
3114 if (rc) {
3115 if (rc == H_CLOSED)
3116 dev_warn(dev, "CRQ Queue closed\n");
3117 dev_err(dev, "Send error (rc=%d)\n", rc);
3118 }
3119
3120 return rc;
3121}
3122
3123static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
3124 u64 remote_handle, u64 ioba, u64 num_entries)
3125{
3126 unsigned int ua = adapter->vdev->unit_address;
3127 struct device *dev = &adapter->vdev->dev;
3128 int rc;
3129
3130 /* Make sure the hypervisor sees the complete request */
3131 mb();
3132 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
3133 cpu_to_be64(remote_handle),
3134 ioba, num_entries);
3135
3136 if (rc) {
3137 if (rc == H_CLOSED)
3138 dev_warn(dev, "CRQ Queue closed\n");
3139 dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
3140 }
3141
3142 return rc;
3143}
3144
3145static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
3146 union ibmvnic_crq *crq)
3147{
3148 unsigned int ua = adapter->vdev->unit_address;
3149 struct device *dev = &adapter->vdev->dev;
3150 u64 *u64_crq = (u64 *)crq;
3151 int rc;
3152
3153 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
3154 (unsigned long int)cpu_to_be64(u64_crq[0]),
3155 (unsigned long int)cpu_to_be64(u64_crq[1]));
3156
3157 /* Make sure the hypervisor sees the complete request */
3158 mb();
3159
3160 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
3161 cpu_to_be64(u64_crq[0]),
3162 cpu_to_be64(u64_crq[1]));
3163
3164 if (rc) {
3165 if (rc == H_CLOSED) {
3166 dev_warn(dev, "CRQ Queue closed\n");
3167 if (adapter->resetting)
3168 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3169 }
3170
3171 dev_warn(dev, "Send error (rc=%d)\n", rc);
3172 }
3173
3174 return rc;
3175}
3176
3177static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
3178{
3179 union ibmvnic_crq crq;
3180
3181 memset(&crq, 0, sizeof(crq));
3182 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
3183 crq.generic.cmd = IBMVNIC_CRQ_INIT;
3184 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
3185
3186 return ibmvnic_send_crq(adapter, &crq);
3187}
3188
3189static int send_version_xchg(struct ibmvnic_adapter *adapter)
3190{
3191 union ibmvnic_crq crq;
3192
3193 memset(&crq, 0, sizeof(crq));
3194 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
3195 crq.version_exchange.cmd = VERSION_EXCHANGE;
3196 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
3197
3198 return ibmvnic_send_crq(adapter, &crq);
3199}
3200
3201struct vnic_login_client_data {
3202 u8 type;
3203 __be16 len;
3204 char name[];
3205} __packed;
3206
3207static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
3208{
3209 int len;
3210
3211 /* Calculate the amount of buffer space needed for the
3212 * vnic client data in the login buffer. There are four entries,
3213 * OS name, LPAR name, device name, and a null last entry.
3214 */
3215 len = 4 * sizeof(struct vnic_login_client_data);
3216 len += 6; /* "Linux" plus NULL */
3217 len += strlen(utsname()->nodename) + 1;
3218 len += strlen(adapter->netdev->name) + 1;
3219
3220 return len;
3221}
3222
3223static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
3224 struct vnic_login_client_data *vlcd)
3225{
3226 const char *os_name = "Linux";
3227 int len;
3228
3229 /* Type 1 - LPAR OS */
3230 vlcd->type = 1;
3231 len = strlen(os_name) + 1;
3232 vlcd->len = cpu_to_be16(len);
3233 strncpy(vlcd->name, os_name, len);
3234 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3235
3236 /* Type 2 - LPAR name */
3237 vlcd->type = 2;
3238 len = strlen(utsname()->nodename) + 1;
3239 vlcd->len = cpu_to_be16(len);
3240 strncpy(vlcd->name, utsname()->nodename, len);
3241 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
3242
3243 /* Type 3 - device name */
3244 vlcd->type = 3;
3245 len = strlen(adapter->netdev->name) + 1;
3246 vlcd->len = cpu_to_be16(len);
3247 strncpy(vlcd->name, adapter->netdev->name, len);
3248}
3249
3250static int send_login(struct ibmvnic_adapter *adapter)
3251{
3252 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
3253 struct ibmvnic_login_buffer *login_buffer;
3254 struct device *dev = &adapter->vdev->dev;
3255 dma_addr_t rsp_buffer_token;
3256 dma_addr_t buffer_token;
3257 size_t rsp_buffer_size;
3258 union ibmvnic_crq crq;
3259 size_t buffer_size;
3260 __be64 *tx_list_p;
3261 __be64 *rx_list_p;
3262 int client_data_len;
3263 struct vnic_login_client_data *vlcd;
3264 int i;
3265
3266 if (!adapter->tx_scrq || !adapter->rx_scrq) {
3267 netdev_err(adapter->netdev,
3268 "RX or TX queues are not allocated, device login failed\n");
3269 return -1;
3270 }
3271
3272 release_login_rsp_buffer(adapter);
3273 client_data_len = vnic_client_data_len(adapter);
3274
3275 buffer_size =
3276 sizeof(struct ibmvnic_login_buffer) +
3277 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
3278 client_data_len;
3279
3280 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
3281 if (!login_buffer)
3282 goto buf_alloc_failed;
3283
3284 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
3285 DMA_TO_DEVICE);
3286 if (dma_mapping_error(dev, buffer_token)) {
3287 dev_err(dev, "Couldn't map login buffer\n");
3288 goto buf_map_failed;
3289 }
3290
3291 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
3292 sizeof(u64) * adapter->req_tx_queues +
3293 sizeof(u64) * adapter->req_rx_queues +
3294 sizeof(u64) * adapter->req_rx_queues +
3295 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
3296
3297 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
3298 if (!login_rsp_buffer)
3299 goto buf_rsp_alloc_failed;
3300
3301 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
3302 rsp_buffer_size, DMA_FROM_DEVICE);
3303 if (dma_mapping_error(dev, rsp_buffer_token)) {
3304 dev_err(dev, "Couldn't map login rsp buffer\n");
3305 goto buf_rsp_map_failed;
3306 }
3307
3308 adapter->login_buf = login_buffer;
3309 adapter->login_buf_token = buffer_token;
3310 adapter->login_buf_sz = buffer_size;
3311 adapter->login_rsp_buf = login_rsp_buffer;
3312 adapter->login_rsp_buf_token = rsp_buffer_token;
3313 adapter->login_rsp_buf_sz = rsp_buffer_size;
3314
3315 login_buffer->len = cpu_to_be32(buffer_size);
3316 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
3317 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
3318 login_buffer->off_txcomp_subcrqs =
3319 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
3320 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
3321 login_buffer->off_rxcomp_subcrqs =
3322 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
3323 sizeof(u64) * adapter->req_tx_queues);
3324 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
3325 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
3326
3327 tx_list_p = (__be64 *)((char *)login_buffer +
3328 sizeof(struct ibmvnic_login_buffer));
3329 rx_list_p = (__be64 *)((char *)login_buffer +
3330 sizeof(struct ibmvnic_login_buffer) +
3331 sizeof(u64) * adapter->req_tx_queues);
3332
3333 for (i = 0; i < adapter->req_tx_queues; i++) {
3334 if (adapter->tx_scrq[i]) {
3335 tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
3336 crq_num);
3337 }
3338 }
3339
3340 for (i = 0; i < adapter->req_rx_queues; i++) {
3341 if (adapter->rx_scrq[i]) {
3342 rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
3343 crq_num);
3344 }
3345 }
3346
3347 /* Insert vNIC login client data */
3348 vlcd = (struct vnic_login_client_data *)
3349 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
3350 login_buffer->client_data_offset =
3351 cpu_to_be32((char *)vlcd - (char *)login_buffer);
3352 login_buffer->client_data_len = cpu_to_be32(client_data_len);
3353
3354 vnic_add_client_data(adapter, vlcd);
3355
3356 netdev_dbg(adapter->netdev, "Login Buffer:\n");
3357 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
3358 netdev_dbg(adapter->netdev, "%016lx\n",
3359 ((unsigned long int *)(adapter->login_buf))[i]);
3360 }
3361
3362 memset(&crq, 0, sizeof(crq));
3363 crq.login.first = IBMVNIC_CRQ_CMD;
3364 crq.login.cmd = LOGIN;
3365 crq.login.ioba = cpu_to_be32(buffer_token);
3366 crq.login.len = cpu_to_be32(buffer_size);
3367 ibmvnic_send_crq(adapter, &crq);
3368
3369 return 0;
3370
3371buf_rsp_map_failed:
3372 kfree(login_rsp_buffer);
3373buf_rsp_alloc_failed:
3374 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
3375buf_map_failed:
3376 kfree(login_buffer);
3377buf_alloc_failed:
3378 return -1;
3379}
3380
3381static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
3382 u32 len, u8 map_id)
3383{
3384 union ibmvnic_crq crq;
3385
3386 memset(&crq, 0, sizeof(crq));
3387 crq.request_map.first = IBMVNIC_CRQ_CMD;
3388 crq.request_map.cmd = REQUEST_MAP;
3389 crq.request_map.map_id = map_id;
3390 crq.request_map.ioba = cpu_to_be32(addr);
3391 crq.request_map.len = cpu_to_be32(len);
3392 ibmvnic_send_crq(adapter, &crq);
3393}
3394
3395static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
3396{
3397 union ibmvnic_crq crq;
3398
3399 memset(&crq, 0, sizeof(crq));
3400 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
3401 crq.request_unmap.cmd = REQUEST_UNMAP;
3402 crq.request_unmap.map_id = map_id;
3403 ibmvnic_send_crq(adapter, &crq);
3404}
3405
3406static void send_map_query(struct ibmvnic_adapter *adapter)
3407{
3408 union ibmvnic_crq crq;
3409
3410 memset(&crq, 0, sizeof(crq));
3411 crq.query_map.first = IBMVNIC_CRQ_CMD;
3412 crq.query_map.cmd = QUERY_MAP;
3413 ibmvnic_send_crq(adapter, &crq);
3414}
3415
3416/* Send a series of CRQs requesting various capabilities of the VNIC server */
3417static void send_cap_queries(struct ibmvnic_adapter *adapter)
3418{
3419 union ibmvnic_crq crq;
3420
3421 atomic_set(&adapter->running_cap_crqs, 0);
3422 memset(&crq, 0, sizeof(crq));
3423 crq.query_capability.first = IBMVNIC_CRQ_CMD;
3424 crq.query_capability.cmd = QUERY_CAPABILITY;
3425
3426 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
3427 atomic_inc(&adapter->running_cap_crqs);
3428 ibmvnic_send_crq(adapter, &crq);
3429
3430 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
3431 atomic_inc(&adapter->running_cap_crqs);
3432 ibmvnic_send_crq(adapter, &crq);
3433
3434 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
3435 atomic_inc(&adapter->running_cap_crqs);
3436 ibmvnic_send_crq(adapter, &crq);
3437
3438 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
3439 atomic_inc(&adapter->running_cap_crqs);
3440 ibmvnic_send_crq(adapter, &crq);
3441
3442 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
3443 atomic_inc(&adapter->running_cap_crqs);
3444 ibmvnic_send_crq(adapter, &crq);
3445
3446 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
3447 atomic_inc(&adapter->running_cap_crqs);
3448 ibmvnic_send_crq(adapter, &crq);
3449
3450 crq.query_capability.capability =
3451 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
3452 atomic_inc(&adapter->running_cap_crqs);
3453 ibmvnic_send_crq(adapter, &crq);
3454
3455 crq.query_capability.capability =
3456 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
3457 atomic_inc(&adapter->running_cap_crqs);
3458 ibmvnic_send_crq(adapter, &crq);
3459
3460 crq.query_capability.capability =
3461 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
3462 atomic_inc(&adapter->running_cap_crqs);
3463 ibmvnic_send_crq(adapter, &crq);
3464
3465 crq.query_capability.capability =
3466 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
3467 atomic_inc(&adapter->running_cap_crqs);
3468 ibmvnic_send_crq(adapter, &crq);
3469
3470 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
3471 atomic_inc(&adapter->running_cap_crqs);
3472 ibmvnic_send_crq(adapter, &crq);
3473
3474 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
3475 atomic_inc(&adapter->running_cap_crqs);
3476 ibmvnic_send_crq(adapter, &crq);
3477
3478 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
3479 atomic_inc(&adapter->running_cap_crqs);
3480 ibmvnic_send_crq(adapter, &crq);
3481
3482 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
3483 atomic_inc(&adapter->running_cap_crqs);
3484 ibmvnic_send_crq(adapter, &crq);
3485
3486 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
3487 atomic_inc(&adapter->running_cap_crqs);
3488 ibmvnic_send_crq(adapter, &crq);
3489
3490 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
3491 atomic_inc(&adapter->running_cap_crqs);
3492 ibmvnic_send_crq(adapter, &crq);
3493
3494 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
3495 atomic_inc(&adapter->running_cap_crqs);
3496 ibmvnic_send_crq(adapter, &crq);
3497
3498 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
3499 atomic_inc(&adapter->running_cap_crqs);
3500 ibmvnic_send_crq(adapter, &crq);
3501
3502 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
3503 atomic_inc(&adapter->running_cap_crqs);
3504 ibmvnic_send_crq(adapter, &crq);
3505
3506 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
3507 atomic_inc(&adapter->running_cap_crqs);
3508 ibmvnic_send_crq(adapter, &crq);
3509
3510 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
3511 atomic_inc(&adapter->running_cap_crqs);
3512 ibmvnic_send_crq(adapter, &crq);
3513
3514 crq.query_capability.capability =
3515 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
3516 atomic_inc(&adapter->running_cap_crqs);
3517 ibmvnic_send_crq(adapter, &crq);
3518
3519 crq.query_capability.capability =
3520 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
3521 atomic_inc(&adapter->running_cap_crqs);
3522 ibmvnic_send_crq(adapter, &crq);
3523
3524 crq.query_capability.capability =
3525 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
3526 atomic_inc(&adapter->running_cap_crqs);
3527 ibmvnic_send_crq(adapter, &crq);
3528
3529 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
3530 atomic_inc(&adapter->running_cap_crqs);
3531 ibmvnic_send_crq(adapter, &crq);
3532}
3533
3534static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
3535 struct ibmvnic_adapter *adapter)
3536{
3537 struct device *dev = &adapter->vdev->dev;
3538
3539 if (crq->get_vpd_size_rsp.rc.code) {
3540 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
3541 crq->get_vpd_size_rsp.rc.code);
3542 complete(&adapter->fw_done);
3543 return;
3544 }
3545
3546 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
3547 complete(&adapter->fw_done);
3548}
3549
3550static void handle_vpd_rsp(union ibmvnic_crq *crq,
3551 struct ibmvnic_adapter *adapter)
3552{
3553 struct device *dev = &adapter->vdev->dev;
3554 unsigned char *substr = NULL;
3555 u8 fw_level_len = 0;
3556
3557 memset(adapter->fw_version, 0, 32);
3558
3559 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
3560 DMA_FROM_DEVICE);
3561
3562 if (crq->get_vpd_rsp.rc.code) {
3563 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
3564 crq->get_vpd_rsp.rc.code);
3565 goto complete;
3566 }
3567
3568 /* get the position of the firmware version info
3569 * located after the ASCII 'RM' substring in the buffer
3570 */
3571 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
3572 if (!substr) {
3573 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
3574 goto complete;
3575 }
3576
3577 /* get length of firmware level ASCII substring */
3578 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
3579 fw_level_len = *(substr + 2);
3580 } else {
3581 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
3582 goto complete;
3583 }
3584
3585 /* copy firmware version string from vpd into adapter */
3586 if ((substr + 3 + fw_level_len) <
3587 (adapter->vpd->buff + adapter->vpd->len)) {
3588 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
3589 } else {
3590 dev_info(dev, "FW substr extrapolated VPD buff\n");
3591 }
3592
3593complete:
3594 if (adapter->fw_version[0] == '\0')
3595 strncpy((char *)adapter->fw_version, "N/A", 3 * sizeof(char));
3596 complete(&adapter->fw_done);
3597}
3598
3599static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
3600{
3601 struct device *dev = &adapter->vdev->dev;
3602 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
3603 union ibmvnic_crq crq;
3604 int i;
3605
3606 dma_unmap_single(dev, adapter->ip_offload_tok,
3607 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
3608
3609 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
3610 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
3611 netdev_dbg(adapter->netdev, "%016lx\n",
3612 ((unsigned long int *)(buf))[i]);
3613
3614 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
3615 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
3616 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
3617 buf->tcp_ipv4_chksum);
3618 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
3619 buf->tcp_ipv6_chksum);
3620 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
3621 buf->udp_ipv4_chksum);
3622 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
3623 buf->udp_ipv6_chksum);
3624 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
3625 buf->large_tx_ipv4);
3626 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
3627 buf->large_tx_ipv6);
3628 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
3629 buf->large_rx_ipv4);
3630 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
3631 buf->large_rx_ipv6);
3632 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
3633 buf->max_ipv4_header_size);
3634 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
3635 buf->max_ipv6_header_size);
3636 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
3637 buf->max_tcp_header_size);
3638 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
3639 buf->max_udp_header_size);
3640 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
3641 buf->max_large_tx_size);
3642 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
3643 buf->max_large_rx_size);
3644 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
3645 buf->ipv6_extension_header);
3646 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
3647 buf->tcp_pseudosum_req);
3648 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
3649 buf->num_ipv6_ext_headers);
3650 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
3651 buf->off_ipv6_ext_headers);
3652
3653 adapter->ip_offload_ctrl_tok =
3654 dma_map_single(dev, &adapter->ip_offload_ctrl,
3655 sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
3656
3657 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
3658 dev_err(dev, "Couldn't map ip offload control buffer\n");
3659 return;
3660 }
3661
3662 adapter->ip_offload_ctrl.len =
3663 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3664 adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
3665 adapter->ip_offload_ctrl.ipv4_chksum = buf->ipv4_chksum;
3666 adapter->ip_offload_ctrl.ipv6_chksum = buf->ipv6_chksum;
3667 adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
3668 adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
3669 adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
3670 adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
3671 adapter->ip_offload_ctrl.large_tx_ipv4 = buf->large_tx_ipv4;
3672 adapter->ip_offload_ctrl.large_tx_ipv6 = buf->large_tx_ipv6;
3673
3674 /* large_rx disabled for now, additional features needed */
3675 adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
3676 adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
3677
3678 adapter->netdev->features = NETIF_F_SG | NETIF_F_GSO;
3679
3680 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
3681 adapter->netdev->features |= NETIF_F_IP_CSUM;
3682
3683 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
3684 adapter->netdev->features |= NETIF_F_IPV6_CSUM;
3685
3686 if ((adapter->netdev->features &
3687 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
3688 adapter->netdev->features |= NETIF_F_RXCSUM;
3689
3690 if (buf->large_tx_ipv4)
3691 adapter->netdev->features |= NETIF_F_TSO;
3692 if (buf->large_tx_ipv6)
3693 adapter->netdev->features |= NETIF_F_TSO6;
3694
3695 adapter->netdev->hw_features |= adapter->netdev->features;
3696
3697 memset(&crq, 0, sizeof(crq));
3698 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
3699 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
3700 crq.control_ip_offload.len =
3701 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
3702 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
3703 ibmvnic_send_crq(adapter, &crq);
3704}
3705
3706static void handle_error_info_rsp(union ibmvnic_crq *crq,
3707 struct ibmvnic_adapter *adapter)
3708{
3709 struct device *dev = &adapter->vdev->dev;
3710 struct ibmvnic_error_buff *error_buff, *tmp;
3711 unsigned long flags;
3712 bool found = false;
3713 int i;
3714
3715 if (!crq->request_error_rsp.rc.code) {
3716 dev_info(dev, "Request Error Rsp returned with rc=%x\n",
3717 crq->request_error_rsp.rc.code);
3718 return;
3719 }
3720
3721 spin_lock_irqsave(&adapter->error_list_lock, flags);
3722 list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
3723 if (error_buff->error_id == crq->request_error_rsp.error_id) {
3724 found = true;
3725 list_del(&error_buff->list);
3726 break;
3727 }
3728 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3729
3730 if (!found) {
3731 dev_err(dev, "Couldn't find error id %x\n",
3732 be32_to_cpu(crq->request_error_rsp.error_id));
3733 return;
3734 }
3735
3736 dev_err(dev, "Detailed info for error id %x:",
3737 be32_to_cpu(crq->request_error_rsp.error_id));
3738
3739 for (i = 0; i < error_buff->len; i++) {
3740 pr_cont("%02x", (int)error_buff->buff[i]);
3741 if (i % 8 == 7)
3742 pr_cont(" ");
3743 }
3744 pr_cont("\n");
3745
3746 dma_unmap_single(dev, error_buff->dma, error_buff->len,
3747 DMA_FROM_DEVICE);
3748 kfree(error_buff->buff);
3749 kfree(error_buff);
3750}
3751
3752static void request_error_information(struct ibmvnic_adapter *adapter,
3753 union ibmvnic_crq *err_crq)
3754{
3755 struct device *dev = &adapter->vdev->dev;
3756 struct net_device *netdev = adapter->netdev;
3757 struct ibmvnic_error_buff *error_buff;
3758 unsigned long timeout = msecs_to_jiffies(30000);
3759 union ibmvnic_crq crq;
3760 unsigned long flags;
3761 int rc, detail_len;
3762
3763 error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
3764 if (!error_buff)
3765 return;
3766
3767 detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
3768 error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
3769 if (!error_buff->buff) {
3770 kfree(error_buff);
3771 return;
3772 }
3773
3774 error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
3775 DMA_FROM_DEVICE);
3776 if (dma_mapping_error(dev, error_buff->dma)) {
3777 netdev_err(netdev, "Couldn't map error buffer\n");
3778 kfree(error_buff->buff);
3779 kfree(error_buff);
3780 return;
3781 }
3782
3783 error_buff->len = detail_len;
3784 error_buff->error_id = err_crq->error_indication.error_id;
3785
3786 spin_lock_irqsave(&adapter->error_list_lock, flags);
3787 list_add_tail(&error_buff->list, &adapter->errors);
3788 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3789
3790 memset(&crq, 0, sizeof(crq));
3791 crq.request_error_info.first = IBMVNIC_CRQ_CMD;
3792 crq.request_error_info.cmd = REQUEST_ERROR_INFO;
3793 crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
3794 crq.request_error_info.len = cpu_to_be32(detail_len);
3795 crq.request_error_info.error_id = err_crq->error_indication.error_id;
3796
3797 rc = ibmvnic_send_crq(adapter, &crq);
3798 if (rc) {
3799 netdev_err(netdev, "failed to request error information\n");
3800 goto err_info_fail;
3801 }
3802
3803 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3804 netdev_err(netdev, "timeout waiting for error information\n");
3805 goto err_info_fail;
3806 }
3807
3808 return;
3809
3810err_info_fail:
3811 spin_lock_irqsave(&adapter->error_list_lock, flags);
3812 list_del(&error_buff->list);
3813 spin_unlock_irqrestore(&adapter->error_list_lock, flags);
3814
3815 kfree(error_buff->buff);
3816 kfree(error_buff);
3817}
3818
3819static void handle_error_indication(union ibmvnic_crq *crq,
3820 struct ibmvnic_adapter *adapter)
3821{
3822 struct device *dev = &adapter->vdev->dev;
3823
3824 dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
3825 crq->error_indication.flags
3826 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
3827 be32_to_cpu(crq->error_indication.error_id),
3828 be16_to_cpu(crq->error_indication.error_cause));
3829
3830 if (be32_to_cpu(crq->error_indication.error_id))
3831 request_error_information(adapter, crq);
3832
3833 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
3834 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
3835 else
3836 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
3837}
3838
3839static int handle_change_mac_rsp(union ibmvnic_crq *crq,
3840 struct ibmvnic_adapter *adapter)
3841{
3842 struct net_device *netdev = adapter->netdev;
3843 struct device *dev = &adapter->vdev->dev;
3844 long rc;
3845
3846 rc = crq->change_mac_addr_rsp.rc.code;
3847 if (rc) {
3848 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
3849 goto out;
3850 }
3851 memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
3852 ETH_ALEN);
3853out:
3854 complete(&adapter->fw_done);
3855 return rc;
3856}
3857
3858static void handle_request_cap_rsp(union ibmvnic_crq *crq,
3859 struct ibmvnic_adapter *adapter)
3860{
3861 struct device *dev = &adapter->vdev->dev;
3862 u64 *req_value;
3863 char *name;
3864
3865 atomic_dec(&adapter->running_cap_crqs);
3866 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
3867 case REQ_TX_QUEUES:
3868 req_value = &adapter->req_tx_queues;
3869 name = "tx";
3870 break;
3871 case REQ_RX_QUEUES:
3872 req_value = &adapter->req_rx_queues;
3873 name = "rx";
3874 break;
3875 case REQ_RX_ADD_QUEUES:
3876 req_value = &adapter->req_rx_add_queues;
3877 name = "rx_add";
3878 break;
3879 case REQ_TX_ENTRIES_PER_SUBCRQ:
3880 req_value = &adapter->req_tx_entries_per_subcrq;
3881 name = "tx_entries_per_subcrq";
3882 break;
3883 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
3884 req_value = &adapter->req_rx_add_entries_per_subcrq;
3885 name = "rx_add_entries_per_subcrq";
3886 break;
3887 case REQ_MTU:
3888 req_value = &adapter->req_mtu;
3889 name = "mtu";
3890 break;
3891 case PROMISC_REQUESTED:
3892 req_value = &adapter->promisc;
3893 name = "promisc";
3894 break;
3895 default:
3896 dev_err(dev, "Got invalid cap request rsp %d\n",
3897 crq->request_capability.capability);
3898 return;
3899 }
3900
3901 switch (crq->request_capability_rsp.rc.code) {
3902 case SUCCESS:
3903 break;
3904 case PARTIALSUCCESS:
3905 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
3906 *req_value,
3907 (long int)be64_to_cpu(crq->request_capability_rsp.
3908 number), name);
3909
3910 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
3911 REQ_MTU) {
3912 pr_err("mtu of %llu is not supported. Reverting.\n",
3913 *req_value);
3914 *req_value = adapter->fallback.mtu;
3915 } else {
3916 *req_value =
3917 be64_to_cpu(crq->request_capability_rsp.number);
3918 }
3919
3920 ibmvnic_send_req_caps(adapter, 1);
3921 return;
3922 default:
3923 dev_err(dev, "Error %d in request cap rsp\n",
3924 crq->request_capability_rsp.rc.code);
3925 return;
3926 }
3927
3928 /* Done receiving requested capabilities, query IP offload support */
3929 if (atomic_read(&adapter->running_cap_crqs) == 0) {
3930 union ibmvnic_crq newcrq;
3931 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
3932 struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
3933 &adapter->ip_offload_buf;
3934
3935 adapter->wait_capability = false;
3936 adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
3937 buf_sz,
3938 DMA_FROM_DEVICE);
3939
3940 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
3941 if (!firmware_has_feature(FW_FEATURE_CMO))
3942 dev_err(dev, "Couldn't map offload buffer\n");
3943 return;
3944 }
3945
3946 memset(&newcrq, 0, sizeof(newcrq));
3947 newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
3948 newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
3949 newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
3950 newcrq.query_ip_offload.ioba =
3951 cpu_to_be32(adapter->ip_offload_tok);
3952
3953 ibmvnic_send_crq(adapter, &newcrq);
3954 }
3955}
3956
3957static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
3958 struct ibmvnic_adapter *adapter)
3959{
3960 struct device *dev = &adapter->vdev->dev;
3961 struct net_device *netdev = adapter->netdev;
3962 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
3963 struct ibmvnic_login_buffer *login = adapter->login_buf;
3964 int i;
3965
3966 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
3967 DMA_TO_DEVICE);
3968 dma_unmap_single(dev, adapter->login_rsp_buf_token,
3969 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
3970
3971 /* If the number of queues requested can't be allocated by the
3972 * server, the login response will return with code 1. We will need
3973 * to resend the login buffer with fewer queues requested.
3974 */
3975 if (login_rsp_crq->generic.rc.code) {
3976 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
3977 complete(&adapter->init_done);
3978 return 0;
3979 }
3980
3981 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3982
3983 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
3984 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
3985 netdev_dbg(adapter->netdev, "%016lx\n",
3986 ((unsigned long int *)(adapter->login_rsp_buf))[i]);
3987 }
3988
3989 /* Sanity checks */
3990 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
3991 (be32_to_cpu(login->num_rxcomp_subcrqs) *
3992 adapter->req_rx_add_queues !=
3993 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
3994 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
3995 ibmvnic_remove(adapter->vdev);
3996 return -EIO;
3997 }
3998 release_login_buffer(adapter);
3999 complete(&adapter->init_done);
4000
4001 return 0;
4002}
4003
4004static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
4005 struct ibmvnic_adapter *adapter)
4006{
4007 struct device *dev = &adapter->vdev->dev;
4008 long rc;
4009
4010 rc = crq->request_unmap_rsp.rc.code;
4011 if (rc)
4012 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
4013}
4014
4015static void handle_query_map_rsp(union ibmvnic_crq *crq,
4016 struct ibmvnic_adapter *adapter)
4017{
4018 struct net_device *netdev = adapter->netdev;
4019 struct device *dev = &adapter->vdev->dev;
4020 long rc;
4021
4022 rc = crq->query_map_rsp.rc.code;
4023 if (rc) {
4024 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
4025 return;
4026 }
4027 netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
4028 crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
4029 crq->query_map_rsp.free_pages);
4030}
4031
4032static void handle_query_cap_rsp(union ibmvnic_crq *crq,
4033 struct ibmvnic_adapter *adapter)
4034{
4035 struct net_device *netdev = adapter->netdev;
4036 struct device *dev = &adapter->vdev->dev;
4037 long rc;
4038
4039 atomic_dec(&adapter->running_cap_crqs);
4040 netdev_dbg(netdev, "Outstanding queries: %d\n",
4041 atomic_read(&adapter->running_cap_crqs));
4042 rc = crq->query_capability.rc.code;
4043 if (rc) {
4044 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
4045 goto out;
4046 }
4047
4048 switch (be16_to_cpu(crq->query_capability.capability)) {
4049 case MIN_TX_QUEUES:
4050 adapter->min_tx_queues =
4051 be64_to_cpu(crq->query_capability.number);
4052 netdev_dbg(netdev, "min_tx_queues = %lld\n",
4053 adapter->min_tx_queues);
4054 break;
4055 case MIN_RX_QUEUES:
4056 adapter->min_rx_queues =
4057 be64_to_cpu(crq->query_capability.number);
4058 netdev_dbg(netdev, "min_rx_queues = %lld\n",
4059 adapter->min_rx_queues);
4060 break;
4061 case MIN_RX_ADD_QUEUES:
4062 adapter->min_rx_add_queues =
4063 be64_to_cpu(crq->query_capability.number);
4064 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
4065 adapter->min_rx_add_queues);
4066 break;
4067 case MAX_TX_QUEUES:
4068 adapter->max_tx_queues =
4069 be64_to_cpu(crq->query_capability.number);
4070 netdev_dbg(netdev, "max_tx_queues = %lld\n",
4071 adapter->max_tx_queues);
4072 break;
4073 case MAX_RX_QUEUES:
4074 adapter->max_rx_queues =
4075 be64_to_cpu(crq->query_capability.number);
4076 netdev_dbg(netdev, "max_rx_queues = %lld\n",
4077 adapter->max_rx_queues);
4078 break;
4079 case MAX_RX_ADD_QUEUES:
4080 adapter->max_rx_add_queues =
4081 be64_to_cpu(crq->query_capability.number);
4082 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
4083 adapter->max_rx_add_queues);
4084 break;
4085 case MIN_TX_ENTRIES_PER_SUBCRQ:
4086 adapter->min_tx_entries_per_subcrq =
4087 be64_to_cpu(crq->query_capability.number);
4088 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
4089 adapter->min_tx_entries_per_subcrq);
4090 break;
4091 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
4092 adapter->min_rx_add_entries_per_subcrq =
4093 be64_to_cpu(crq->query_capability.number);
4094 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
4095 adapter->min_rx_add_entries_per_subcrq);
4096 break;
4097 case MAX_TX_ENTRIES_PER_SUBCRQ:
4098 adapter->max_tx_entries_per_subcrq =
4099 be64_to_cpu(crq->query_capability.number);
4100 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
4101 adapter->max_tx_entries_per_subcrq);
4102 break;
4103 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
4104 adapter->max_rx_add_entries_per_subcrq =
4105 be64_to_cpu(crq->query_capability.number);
4106 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
4107 adapter->max_rx_add_entries_per_subcrq);
4108 break;
4109 case TCP_IP_OFFLOAD:
4110 adapter->tcp_ip_offload =
4111 be64_to_cpu(crq->query_capability.number);
4112 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
4113 adapter->tcp_ip_offload);
4114 break;
4115 case PROMISC_SUPPORTED:
4116 adapter->promisc_supported =
4117 be64_to_cpu(crq->query_capability.number);
4118 netdev_dbg(netdev, "promisc_supported = %lld\n",
4119 adapter->promisc_supported);
4120 break;
4121 case MIN_MTU:
4122 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
4123 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4124 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
4125 break;
4126 case MAX_MTU:
4127 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
4128 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4129 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
4130 break;
4131 case MAX_MULTICAST_FILTERS:
4132 adapter->max_multicast_filters =
4133 be64_to_cpu(crq->query_capability.number);
4134 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
4135 adapter->max_multicast_filters);
4136 break;
4137 case VLAN_HEADER_INSERTION:
4138 adapter->vlan_header_insertion =
4139 be64_to_cpu(crq->query_capability.number);
4140 if (adapter->vlan_header_insertion)
4141 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
4142 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
4143 adapter->vlan_header_insertion);
4144 break;
4145 case RX_VLAN_HEADER_INSERTION:
4146 adapter->rx_vlan_header_insertion =
4147 be64_to_cpu(crq->query_capability.number);
4148 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
4149 adapter->rx_vlan_header_insertion);
4150 break;
4151 case MAX_TX_SG_ENTRIES:
4152 adapter->max_tx_sg_entries =
4153 be64_to_cpu(crq->query_capability.number);
4154 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
4155 adapter->max_tx_sg_entries);
4156 break;
4157 case RX_SG_SUPPORTED:
4158 adapter->rx_sg_supported =
4159 be64_to_cpu(crq->query_capability.number);
4160 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
4161 adapter->rx_sg_supported);
4162 break;
4163 case OPT_TX_COMP_SUB_QUEUES:
4164 adapter->opt_tx_comp_sub_queues =
4165 be64_to_cpu(crq->query_capability.number);
4166 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
4167 adapter->opt_tx_comp_sub_queues);
4168 break;
4169 case OPT_RX_COMP_QUEUES:
4170 adapter->opt_rx_comp_queues =
4171 be64_to_cpu(crq->query_capability.number);
4172 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
4173 adapter->opt_rx_comp_queues);
4174 break;
4175 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
4176 adapter->opt_rx_bufadd_q_per_rx_comp_q =
4177 be64_to_cpu(crq->query_capability.number);
4178 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
4179 adapter->opt_rx_bufadd_q_per_rx_comp_q);
4180 break;
4181 case OPT_TX_ENTRIES_PER_SUBCRQ:
4182 adapter->opt_tx_entries_per_subcrq =
4183 be64_to_cpu(crq->query_capability.number);
4184 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
4185 adapter->opt_tx_entries_per_subcrq);
4186 break;
4187 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
4188 adapter->opt_rxba_entries_per_subcrq =
4189 be64_to_cpu(crq->query_capability.number);
4190 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
4191 adapter->opt_rxba_entries_per_subcrq);
4192 break;
4193 case TX_RX_DESC_REQ:
4194 adapter->tx_rx_desc_req = crq->query_capability.number;
4195 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
4196 adapter->tx_rx_desc_req);
4197 break;
4198
4199 default:
4200 netdev_err(netdev, "Got invalid cap rsp %d\n",
4201 crq->query_capability.capability);
4202 }
4203
4204out:
4205 if (atomic_read(&adapter->running_cap_crqs) == 0) {
4206 adapter->wait_capability = false;
4207 ibmvnic_send_req_caps(adapter, 0);
4208 }
4209}
4210
4211static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
4212 struct ibmvnic_adapter *adapter)
4213{
4214 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
4215 struct net_device *netdev = adapter->netdev;
4216 struct device *dev = &adapter->vdev->dev;
4217 u64 *u64_crq = (u64 *)crq;
4218 long rc;
4219
4220 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
4221 (unsigned long int)cpu_to_be64(u64_crq[0]),
4222 (unsigned long int)cpu_to_be64(u64_crq[1]));
4223 switch (gen_crq->first) {
4224 case IBMVNIC_CRQ_INIT_RSP:
4225 switch (gen_crq->cmd) {
4226 case IBMVNIC_CRQ_INIT:
4227 dev_info(dev, "Partner initialized\n");
4228 adapter->from_passive_init = true;
4229 adapter->failover_pending = false;
4230 complete(&adapter->init_done);
4231 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
4232 break;
4233 case IBMVNIC_CRQ_INIT_COMPLETE:
4234 dev_info(dev, "Partner initialization complete\n");
4235 send_version_xchg(adapter);
4236 break;
4237 default:
4238 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
4239 }
4240 return;
4241 case IBMVNIC_CRQ_XPORT_EVENT:
4242 netif_carrier_off(netdev);
4243 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
4244 dev_info(dev, "Migrated, re-enabling adapter\n");
4245 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
4246 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
4247 dev_info(dev, "Backing device failover detected\n");
4248 adapter->failover_pending = true;
4249 } else {
4250 /* The adapter lost the connection */
4251 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
4252 gen_crq->cmd);
4253 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
4254 }
4255 return;
4256 case IBMVNIC_CRQ_CMD_RSP:
4257 break;
4258 default:
4259 dev_err(dev, "Got an invalid msg type 0x%02x\n",
4260 gen_crq->first);
4261 return;
4262 }
4263
4264 switch (gen_crq->cmd) {
4265 case VERSION_EXCHANGE_RSP:
4266 rc = crq->version_exchange_rsp.rc.code;
4267 if (rc) {
4268 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
4269 break;
4270 }
4271 dev_info(dev, "Partner protocol version is %d\n",
4272 crq->version_exchange_rsp.version);
4273 if (be16_to_cpu(crq->version_exchange_rsp.version) <
4274 ibmvnic_version)
4275 ibmvnic_version =
4276 be16_to_cpu(crq->version_exchange_rsp.version);
4277 send_cap_queries(adapter);
4278 break;
4279 case QUERY_CAPABILITY_RSP:
4280 handle_query_cap_rsp(crq, adapter);
4281 break;
4282 case QUERY_MAP_RSP:
4283 handle_query_map_rsp(crq, adapter);
4284 break;
4285 case REQUEST_MAP_RSP:
4286 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
4287 complete(&adapter->fw_done);
4288 break;
4289 case REQUEST_UNMAP_RSP:
4290 handle_request_unmap_rsp(crq, adapter);
4291 break;
4292 case REQUEST_CAPABILITY_RSP:
4293 handle_request_cap_rsp(crq, adapter);
4294 break;
4295 case LOGIN_RSP:
4296 netdev_dbg(netdev, "Got Login Response\n");
4297 handle_login_rsp(crq, adapter);
4298 break;
4299 case LOGICAL_LINK_STATE_RSP:
4300 netdev_dbg(netdev,
4301 "Got Logical Link State Response, state: %d rc: %d\n",
4302 crq->logical_link_state_rsp.link_state,
4303 crq->logical_link_state_rsp.rc.code);
4304 adapter->logical_link_state =
4305 crq->logical_link_state_rsp.link_state;
4306 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
4307 complete(&adapter->init_done);
4308 break;
4309 case LINK_STATE_INDICATION:
4310 netdev_dbg(netdev, "Got Logical Link State Indication\n");
4311 adapter->phys_link_state =
4312 crq->link_state_indication.phys_link_state;
4313 adapter->logical_link_state =
4314 crq->link_state_indication.logical_link_state;
4315 break;
4316 case CHANGE_MAC_ADDR_RSP:
4317 netdev_dbg(netdev, "Got MAC address change Response\n");
4318 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
4319 break;
4320 case ERROR_INDICATION:
4321 netdev_dbg(netdev, "Got Error Indication\n");
4322 handle_error_indication(crq, adapter);
4323 break;
4324 case REQUEST_ERROR_RSP:
4325 netdev_dbg(netdev, "Got Error Detail Response\n");
4326 handle_error_info_rsp(crq, adapter);
4327 break;
4328 case REQUEST_STATISTICS_RSP:
4329 netdev_dbg(netdev, "Got Statistics Response\n");
4330 complete(&adapter->stats_done);
4331 break;
4332 case QUERY_IP_OFFLOAD_RSP:
4333 netdev_dbg(netdev, "Got Query IP offload Response\n");
4334 handle_query_ip_offload_rsp(adapter);
4335 break;
4336 case MULTICAST_CTRL_RSP:
4337 netdev_dbg(netdev, "Got multicast control Response\n");
4338 break;
4339 case CONTROL_IP_OFFLOAD_RSP:
4340 netdev_dbg(netdev, "Got Control IP offload Response\n");
4341 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
4342 sizeof(adapter->ip_offload_ctrl),
4343 DMA_TO_DEVICE);
4344 complete(&adapter->init_done);
4345 break;
4346 case COLLECT_FW_TRACE_RSP:
4347 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
4348 complete(&adapter->fw_done);
4349 break;
4350 case GET_VPD_SIZE_RSP:
4351 handle_vpd_size_rsp(crq, adapter);
4352 break;
4353 case GET_VPD_RSP:
4354 handle_vpd_rsp(crq, adapter);
4355 break;
4356 default:
4357 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
4358 gen_crq->cmd);
4359 }
4360}
4361
4362static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
4363{
4364 struct ibmvnic_adapter *adapter = instance;
4365
4366 tasklet_schedule(&adapter->tasklet);
4367 return IRQ_HANDLED;
4368}
4369
4370static void ibmvnic_tasklet(void *data)
4371{
4372 struct ibmvnic_adapter *adapter = data;
4373 struct ibmvnic_crq_queue *queue = &adapter->crq;
4374 union ibmvnic_crq *crq;
4375 unsigned long flags;
4376 bool done = false;
4377
4378 spin_lock_irqsave(&queue->lock, flags);
4379 while (!done) {
4380 /* Pull all the valid messages off the CRQ */
4381 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
4382 ibmvnic_handle_crq(crq, adapter);
4383 crq->generic.first = 0;
4384 }
4385
4386 /* remain in tasklet until all
4387 * capabilities responses are received
4388 */
4389 if (!adapter->wait_capability)
4390 done = true;
4391 }
4392 /* if capabilities CRQ's were sent in this tasklet, the following
4393 * tasklet must wait until all responses are received
4394 */
4395 if (atomic_read(&adapter->running_cap_crqs) != 0)
4396 adapter->wait_capability = true;
4397 spin_unlock_irqrestore(&queue->lock, flags);
4398}
4399
4400static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
4401{
4402 struct vio_dev *vdev = adapter->vdev;
4403 int rc;
4404
4405 do {
4406 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
4407 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
4408
4409 if (rc)
4410 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
4411
4412 return rc;
4413}
4414
4415static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
4416{
4417 struct ibmvnic_crq_queue *crq = &adapter->crq;
4418 struct device *dev = &adapter->vdev->dev;
4419 struct vio_dev *vdev = adapter->vdev;
4420 int rc;
4421
4422 /* Close the CRQ */
4423 do {
4424 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4425 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4426
4427 /* Clean out the queue */
4428 memset(crq->msgs, 0, PAGE_SIZE);
4429 crq->cur = 0;
4430
4431 /* And re-open it again */
4432 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4433 crq->msg_token, PAGE_SIZE);
4434
4435 if (rc == H_CLOSED)
4436 /* Adapter is good, but other end is not ready */
4437 dev_warn(dev, "Partner adapter not ready\n");
4438 else if (rc != 0)
4439 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
4440
4441 return rc;
4442}
4443
4444static void release_crq_queue(struct ibmvnic_adapter *adapter)
4445{
4446 struct ibmvnic_crq_queue *crq = &adapter->crq;
4447 struct vio_dev *vdev = adapter->vdev;
4448 long rc;
4449
4450 if (!crq->msgs)
4451 return;
4452
4453 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
4454 free_irq(vdev->irq, adapter);
4455 tasklet_kill(&adapter->tasklet);
4456 do {
4457 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4458 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4459
4460 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
4461 DMA_BIDIRECTIONAL);
4462 free_page((unsigned long)crq->msgs);
4463 crq->msgs = NULL;
4464}
4465
4466static int init_crq_queue(struct ibmvnic_adapter *adapter)
4467{
4468 struct ibmvnic_crq_queue *crq = &adapter->crq;
4469 struct device *dev = &adapter->vdev->dev;
4470 struct vio_dev *vdev = adapter->vdev;
4471 int rc, retrc = -ENOMEM;
4472
4473 if (crq->msgs)
4474 return 0;
4475
4476 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
4477 /* Should we allocate more than one page? */
4478
4479 if (!crq->msgs)
4480 return -ENOMEM;
4481
4482 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4483 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
4484 DMA_BIDIRECTIONAL);
4485 if (dma_mapping_error(dev, crq->msg_token))
4486 goto map_failed;
4487
4488 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4489 crq->msg_token, PAGE_SIZE);
4490
4491 if (rc == H_RESOURCE)
4492 /* maybe kexecing and resource is busy. try a reset */
4493 rc = ibmvnic_reset_crq(adapter);
4494 retrc = rc;
4495
4496 if (rc == H_CLOSED) {
4497 dev_warn(dev, "Partner adapter not ready\n");
4498 } else if (rc) {
4499 dev_warn(dev, "Error %d opening adapter\n", rc);
4500 goto reg_crq_failed;
4501 }
4502
4503 retrc = 0;
4504
4505 tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
4506 (unsigned long)adapter);
4507
4508 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
4509 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
4510 adapter);
4511 if (rc) {
4512 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
4513 vdev->irq, rc);
4514 goto req_irq_failed;
4515 }
4516
4517 rc = vio_enable_interrupts(vdev);
4518 if (rc) {
4519 dev_err(dev, "Error %d enabling interrupts\n", rc);
4520 goto req_irq_failed;
4521 }
4522
4523 crq->cur = 0;
4524 spin_lock_init(&crq->lock);
4525
4526 return retrc;
4527
4528req_irq_failed:
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));
4533reg_crq_failed:
4534 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4535map_failed:
4536 free_page((unsigned long)crq->msgs);
4537 crq->msgs = NULL;
4538 return retrc;
4539}
4540
4541static int ibmvnic_init(struct ibmvnic_adapter *adapter)
4542{
4543 struct device *dev = &adapter->vdev->dev;
4544 unsigned long timeout = msecs_to_jiffies(30000);
4545 u64 old_num_rx_queues, old_num_tx_queues;
4546 int rc;
4547
4548 adapter->from_passive_init = false;
4549
4550 old_num_rx_queues = adapter->req_rx_queues;
4551 old_num_tx_queues = adapter->req_tx_queues;
4552
4553 init_completion(&adapter->init_done);
4554 adapter->init_done_rc = 0;
4555 ibmvnic_send_crq_init(adapter);
4556 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
4557 dev_err(dev, "Initialization sequence timed out\n");
4558 return -1;
4559 }
4560
4561 if (adapter->init_done_rc) {
4562 release_crq_queue(adapter);
4563 return adapter->init_done_rc;
4564 }
4565
4566 if (adapter->from_passive_init) {
4567 adapter->state = VNIC_OPEN;
4568 adapter->from_passive_init = false;
4569 return -1;
4570 }
4571
4572 if (adapter->resetting && !adapter->wait_for_reset &&
4573 adapter->reset_reason != VNIC_RESET_MOBILITY) {
4574 if (adapter->req_rx_queues != old_num_rx_queues ||
4575 adapter->req_tx_queues != old_num_tx_queues) {
4576 release_sub_crqs(adapter, 0);
4577 rc = init_sub_crqs(adapter);
4578 } else {
4579 rc = reset_sub_crq_queues(adapter);
4580 }
4581 } else {
4582 rc = init_sub_crqs(adapter);
4583 }
4584
4585 if (rc) {
4586 dev_err(dev, "Initialization of sub crqs failed\n");
4587 release_crq_queue(adapter);
4588 return rc;
4589 }
4590
4591 rc = init_sub_crq_irqs(adapter);
4592 if (rc) {
4593 dev_err(dev, "Failed to initialize sub crq irqs\n");
4594 release_crq_queue(adapter);
4595 }
4596
4597 return rc;
4598}
4599
4600static struct device_attribute dev_attr_failover;
4601
4602static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
4603{
4604 struct ibmvnic_adapter *adapter;
4605 struct net_device *netdev;
4606 unsigned char *mac_addr_p;
4607 int rc;
4608
4609 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
4610 dev->unit_address);
4611
4612 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
4613 VETH_MAC_ADDR, NULL);
4614 if (!mac_addr_p) {
4615 dev_err(&dev->dev,
4616 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
4617 __FILE__, __LINE__);
4618 return 0;
4619 }
4620
4621 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
4622 IBMVNIC_MAX_QUEUES);
4623 if (!netdev)
4624 return -ENOMEM;
4625
4626 adapter = netdev_priv(netdev);
4627 adapter->state = VNIC_PROBING;
4628 dev_set_drvdata(&dev->dev, netdev);
4629 adapter->vdev = dev;
4630 adapter->netdev = netdev;
4631
4632 ether_addr_copy(adapter->mac_addr, mac_addr_p);
4633 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4634 netdev->irq = dev->irq;
4635 netdev->netdev_ops = &ibmvnic_netdev_ops;
4636 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
4637 SET_NETDEV_DEV(netdev, &dev->dev);
4638
4639 spin_lock_init(&adapter->stats_lock);
4640
4641 INIT_LIST_HEAD(&adapter->errors);
4642 spin_lock_init(&adapter->error_list_lock);
4643
4644 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
4645 INIT_LIST_HEAD(&adapter->rwi_list);
4646 mutex_init(&adapter->reset_lock);
4647 mutex_init(&adapter->rwi_lock);
4648 adapter->resetting = false;
4649
4650 adapter->mac_change_pending = false;
4651
4652 do {
4653 rc = init_crq_queue(adapter);
4654 if (rc) {
4655 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
4656 rc);
4657 goto ibmvnic_init_fail;
4658 }
4659
4660 rc = ibmvnic_init(adapter);
4661 if (rc && rc != EAGAIN)
4662 goto ibmvnic_init_fail;
4663 } while (rc == EAGAIN);
4664
4665 rc = init_stats_buffers(adapter);
4666 if (rc)
4667 goto ibmvnic_init_fail;
4668
4669 rc = init_stats_token(adapter);
4670 if (rc)
4671 goto ibmvnic_stats_fail;
4672
4673 netdev->mtu = adapter->req_mtu - ETH_HLEN;
4674 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
4675 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
4676
4677 rc = device_create_file(&dev->dev, &dev_attr_failover);
4678 if (rc)
4679 goto ibmvnic_dev_file_err;
4680
4681 netif_carrier_off(netdev);
4682 rc = register_netdev(netdev);
4683 if (rc) {
4684 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
4685 goto ibmvnic_register_fail;
4686 }
4687 dev_info(&dev->dev, "ibmvnic registered\n");
4688
4689 adapter->state = VNIC_PROBED;
4690
4691 adapter->wait_for_reset = false;
4692
4693 return 0;
4694
4695ibmvnic_register_fail:
4696 device_remove_file(&dev->dev, &dev_attr_failover);
4697
4698ibmvnic_dev_file_err:
4699 release_stats_token(adapter);
4700
4701ibmvnic_stats_fail:
4702 release_stats_buffers(adapter);
4703
4704ibmvnic_init_fail:
4705 release_sub_crqs(adapter, 1);
4706 release_crq_queue(adapter);
4707 free_netdev(netdev);
4708
4709 return rc;
4710}
4711
4712static int ibmvnic_remove(struct vio_dev *dev)
4713{
4714 struct net_device *netdev = dev_get_drvdata(&dev->dev);
4715 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4716
4717 adapter->state = VNIC_REMOVING;
4718 unregister_netdev(netdev);
4719 mutex_lock(&adapter->reset_lock);
4720
4721 release_resources(adapter);
4722 release_sub_crqs(adapter, 1);
4723 release_crq_queue(adapter);
4724
4725 release_stats_token(adapter);
4726 release_stats_buffers(adapter);
4727
4728 adapter->state = VNIC_REMOVED;
4729
4730 mutex_unlock(&adapter->reset_lock);
4731 device_remove_file(&dev->dev, &dev_attr_failover);
4732 free_netdev(netdev);
4733 dev_set_drvdata(&dev->dev, NULL);
4734
4735 return 0;
4736}
4737
4738static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
4739 const char *buf, size_t count)
4740{
4741 struct net_device *netdev = dev_get_drvdata(dev);
4742 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4743 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
4744 __be64 session_token;
4745 long rc;
4746
4747 if (!sysfs_streq(buf, "1"))
4748 return -EINVAL;
4749
4750 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
4751 H_GET_SESSION_TOKEN, 0, 0, 0);
4752 if (rc) {
4753 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
4754 rc);
4755 return -EINVAL;
4756 }
4757
4758 session_token = (__be64)retbuf[0];
4759 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
4760 be64_to_cpu(session_token));
4761 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4762 H_SESSION_ERR_DETECTED, session_token, 0, 0);
4763 if (rc) {
4764 netdev_err(netdev, "Client initiated failover failed, rc %ld\n",
4765 rc);
4766 return -EINVAL;
4767 }
4768
4769 return count;
4770}
4771
4772static DEVICE_ATTR_WO(failover);
4773
4774static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
4775{
4776 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
4777 struct ibmvnic_adapter *adapter;
4778 struct iommu_table *tbl;
4779 unsigned long ret = 0;
4780 int i;
4781
4782 tbl = get_iommu_table_base(&vdev->dev);
4783
4784 /* netdev inits at probe time along with the structures we need below*/
4785 if (!netdev)
4786 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
4787
4788 adapter = netdev_priv(netdev);
4789
4790 ret += PAGE_SIZE; /* the crq message queue */
4791 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
4792
4793 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
4794 ret += 4 * PAGE_SIZE; /* the scrq message queue */
4795
4796 for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
4797 i++)
4798 ret += adapter->rx_pool[i].size *
4799 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
4800
4801 return ret;
4802}
4803
4804static int ibmvnic_resume(struct device *dev)
4805{
4806 struct net_device *netdev = dev_get_drvdata(dev);
4807 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
4808
4809 if (adapter->state != VNIC_OPEN)
4810 return 0;
4811
4812 tasklet_schedule(&adapter->tasklet);
4813
4814 return 0;
4815}
4816
4817static const struct vio_device_id ibmvnic_device_table[] = {
4818 {"network", "IBM,vnic"},
4819 {"", "" }
4820};
4821MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
4822
4823static const struct dev_pm_ops ibmvnic_pm_ops = {
4824 .resume = ibmvnic_resume
4825};
4826
4827static struct vio_driver ibmvnic_driver = {
4828 .id_table = ibmvnic_device_table,
4829 .probe = ibmvnic_probe,
4830 .remove = ibmvnic_remove,
4831 .get_desired_dma = ibmvnic_get_desired_dma,
4832 .name = ibmvnic_driver_name,
4833 .pm = &ibmvnic_pm_ops,
4834};
4835
4836/* module functions */
4837static int __init ibmvnic_module_init(void)
4838{
4839 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
4840 IBMVNIC_DRIVER_VERSION);
4841
4842 return vio_register_driver(&ibmvnic_driver);
4843}
4844
4845static void __exit ibmvnic_module_exit(void)
4846{
4847 vio_unregister_driver(&ibmvnic_driver);
4848}
4849
4850module_init(ibmvnic_module_init);
4851module_exit(ibmvnic_module_exit);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/**************************************************************************/
3/* */
4/* IBM System i and System p Virtual NIC Device Driver */
5/* Copyright (C) 2014 IBM Corp. */
6/* Santiago Leon (santi_leon@yahoo.com) */
7/* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
8/* John Allen (jallen@linux.vnet.ibm.com) */
9/* */
10/* */
11/* This module contains the implementation of a virtual ethernet device */
12/* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
13/* option of the RS/6000 Platform Architecture to interface with virtual */
14/* ethernet NICs that are presented to the partition by the hypervisor. */
15/* */
16/* Messages are passed between the VNIC driver and the VNIC server using */
17/* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
18/* issue and receive commands that initiate communication with the server */
19/* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
20/* are used by the driver to notify the server that a packet is */
21/* ready for transmission or that a buffer has been added to receive a */
22/* packet. Subsequently, sCRQs are used by the server to notify the */
23/* driver that a packet transmission has been completed or that a packet */
24/* has been received and placed in a waiting buffer. */
25/* */
26/* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
27/* which skbs are DMA mapped and immediately unmapped when the transmit */
28/* or receive has been completed, the VNIC driver is required to use */
29/* "long term mapping". This entails that large, continuous DMA mapped */
30/* buffers are allocated on driver initialization and these buffers are */
31/* then continuously reused to pass skbs to and from the VNIC server. */
32/* */
33/**************************************************************************/
34
35#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/types.h>
38#include <linux/errno.h>
39#include <linux/completion.h>
40#include <linux/ioport.h>
41#include <linux/dma-mapping.h>
42#include <linux/kernel.h>
43#include <linux/netdevice.h>
44#include <linux/etherdevice.h>
45#include <linux/skbuff.h>
46#include <linux/init.h>
47#include <linux/delay.h>
48#include <linux/mm.h>
49#include <linux/ethtool.h>
50#include <linux/proc_fs.h>
51#include <linux/if_arp.h>
52#include <linux/in.h>
53#include <linux/ip.h>
54#include <linux/ipv6.h>
55#include <linux/irq.h>
56#include <linux/irqdomain.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
59#include <linux/interrupt.h>
60#include <net/net_namespace.h>
61#include <asm/hvcall.h>
62#include <linux/atomic.h>
63#include <asm/vio.h>
64#include <asm/xive.h>
65#include <asm/iommu.h>
66#include <linux/uaccess.h>
67#include <asm/firmware.h>
68#include <linux/workqueue.h>
69#include <linux/if_vlan.h>
70#include <linux/utsname.h>
71#include <linux/cpu.h>
72
73#include "ibmvnic.h"
74
75static const char ibmvnic_driver_name[] = "ibmvnic";
76static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
77
78MODULE_AUTHOR("Santiago Leon");
79MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
80MODULE_LICENSE("GPL");
81MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
82
83static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
84static void release_sub_crqs(struct ibmvnic_adapter *, bool);
85static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
86static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
87static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
88static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
89static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
90static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
91static int enable_scrq_irq(struct ibmvnic_adapter *,
92 struct ibmvnic_sub_crq_queue *);
93static int disable_scrq_irq(struct ibmvnic_adapter *,
94 struct ibmvnic_sub_crq_queue *);
95static int pending_scrq(struct ibmvnic_adapter *,
96 struct ibmvnic_sub_crq_queue *);
97static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
98 struct ibmvnic_sub_crq_queue *);
99static int ibmvnic_poll(struct napi_struct *napi, int data);
100static void send_query_map(struct ibmvnic_adapter *adapter);
101static int send_request_map(struct ibmvnic_adapter *, dma_addr_t, u32, u8);
102static int send_request_unmap(struct ibmvnic_adapter *, u8);
103static int send_login(struct ibmvnic_adapter *adapter);
104static void send_query_cap(struct ibmvnic_adapter *adapter);
105static int init_sub_crqs(struct ibmvnic_adapter *);
106static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
107static int ibmvnic_reset_init(struct ibmvnic_adapter *, bool reset);
108static void release_crq_queue(struct ibmvnic_adapter *);
109static int __ibmvnic_set_mac(struct net_device *, u8 *);
110static int init_crq_queue(struct ibmvnic_adapter *adapter);
111static int send_query_phys_parms(struct ibmvnic_adapter *adapter);
112static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
113 struct ibmvnic_sub_crq_queue *tx_scrq);
114static void free_long_term_buff(struct ibmvnic_adapter *adapter,
115 struct ibmvnic_long_term_buff *ltb);
116static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter);
117
118struct ibmvnic_stat {
119 char name[ETH_GSTRING_LEN];
120 int offset;
121};
122
123#define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
124 offsetof(struct ibmvnic_statistics, stat))
125#define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + (off))))
126
127static const struct ibmvnic_stat ibmvnic_stats[] = {
128 {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
129 {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
130 {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
131 {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
132 {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
133 {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
134 {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
135 {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
136 {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
137 {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
138 {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
139 {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
140 {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
141 {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
142 {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
143 {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
144 {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
145 {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
146 {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
147 {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
148 {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
149 {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
150};
151
152static int send_crq_init_complete(struct ibmvnic_adapter *adapter)
153{
154 union ibmvnic_crq crq;
155
156 memset(&crq, 0, sizeof(crq));
157 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
158 crq.generic.cmd = IBMVNIC_CRQ_INIT_COMPLETE;
159
160 return ibmvnic_send_crq(adapter, &crq);
161}
162
163static int send_version_xchg(struct ibmvnic_adapter *adapter)
164{
165 union ibmvnic_crq crq;
166
167 memset(&crq, 0, sizeof(crq));
168 crq.version_exchange.first = IBMVNIC_CRQ_CMD;
169 crq.version_exchange.cmd = VERSION_EXCHANGE;
170 crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
171
172 return ibmvnic_send_crq(adapter, &crq);
173}
174
175static void ibmvnic_clean_queue_affinity(struct ibmvnic_adapter *adapter,
176 struct ibmvnic_sub_crq_queue *queue)
177{
178 if (!(queue && queue->irq))
179 return;
180
181 cpumask_clear(queue->affinity_mask);
182
183 if (irq_set_affinity_and_hint(queue->irq, NULL))
184 netdev_warn(adapter->netdev,
185 "%s: Clear affinity failed, queue addr = %p, IRQ = %d\n",
186 __func__, queue, queue->irq);
187}
188
189static void ibmvnic_clean_affinity(struct ibmvnic_adapter *adapter)
190{
191 struct ibmvnic_sub_crq_queue **rxqs;
192 struct ibmvnic_sub_crq_queue **txqs;
193 int num_rxqs, num_txqs;
194 int rc, i;
195
196 rc = 0;
197 rxqs = adapter->rx_scrq;
198 txqs = adapter->tx_scrq;
199 num_txqs = adapter->num_active_tx_scrqs;
200 num_rxqs = adapter->num_active_rx_scrqs;
201
202 netdev_dbg(adapter->netdev, "%s: Cleaning irq affinity hints", __func__);
203 if (txqs) {
204 for (i = 0; i < num_txqs; i++)
205 ibmvnic_clean_queue_affinity(adapter, txqs[i]);
206 }
207 if (rxqs) {
208 for (i = 0; i < num_rxqs; i++)
209 ibmvnic_clean_queue_affinity(adapter, rxqs[i]);
210 }
211}
212
213static int ibmvnic_set_queue_affinity(struct ibmvnic_sub_crq_queue *queue,
214 unsigned int *cpu, int *stragglers,
215 int stride)
216{
217 cpumask_var_t mask;
218 int i;
219 int rc = 0;
220
221 if (!(queue && queue->irq))
222 return rc;
223
224 /* cpumask_var_t is either a pointer or array, allocation works here */
225 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
226 return -ENOMEM;
227
228 /* while we have extra cpu give one extra to this irq */
229 if (*stragglers) {
230 stride++;
231 (*stragglers)--;
232 }
233 /* atomic write is safer than writing bit by bit directly */
234 for (i = 0; i < stride; i++) {
235 cpumask_set_cpu(*cpu, mask);
236 *cpu = cpumask_next_wrap(*cpu, cpu_online_mask,
237 nr_cpu_ids, false);
238 }
239 /* set queue affinity mask */
240 cpumask_copy(queue->affinity_mask, mask);
241 rc = irq_set_affinity_and_hint(queue->irq, queue->affinity_mask);
242 free_cpumask_var(mask);
243
244 return rc;
245}
246
247/* assumes cpu read lock is held */
248static void ibmvnic_set_affinity(struct ibmvnic_adapter *adapter)
249{
250 struct ibmvnic_sub_crq_queue **rxqs = adapter->rx_scrq;
251 struct ibmvnic_sub_crq_queue **txqs = adapter->tx_scrq;
252 struct ibmvnic_sub_crq_queue *queue;
253 int num_rxqs = adapter->num_active_rx_scrqs;
254 int num_txqs = adapter->num_active_tx_scrqs;
255 int total_queues, stride, stragglers, i;
256 unsigned int num_cpu, cpu;
257 int rc = 0;
258
259 netdev_dbg(adapter->netdev, "%s: Setting irq affinity hints", __func__);
260 if (!(adapter->rx_scrq && adapter->tx_scrq)) {
261 netdev_warn(adapter->netdev,
262 "%s: Set affinity failed, queues not allocated\n",
263 __func__);
264 return;
265 }
266
267 total_queues = num_rxqs + num_txqs;
268 num_cpu = num_online_cpus();
269 /* number of cpu's assigned per irq */
270 stride = max_t(int, num_cpu / total_queues, 1);
271 /* number of leftover cpu's */
272 stragglers = num_cpu >= total_queues ? num_cpu % total_queues : 0;
273 /* next available cpu to assign irq to */
274 cpu = cpumask_next(-1, cpu_online_mask);
275
276 for (i = 0; i < num_txqs; i++) {
277 queue = txqs[i];
278 rc = ibmvnic_set_queue_affinity(queue, &cpu, &stragglers,
279 stride);
280 if (rc)
281 goto out;
282
283 if (!queue)
284 continue;
285
286 rc = __netif_set_xps_queue(adapter->netdev,
287 cpumask_bits(queue->affinity_mask),
288 i, XPS_CPUS);
289 if (rc)
290 netdev_warn(adapter->netdev, "%s: Set XPS on queue %d failed, rc = %d.\n",
291 __func__, i, rc);
292 }
293
294 for (i = 0; i < num_rxqs; i++) {
295 queue = rxqs[i];
296 rc = ibmvnic_set_queue_affinity(queue, &cpu, &stragglers,
297 stride);
298 if (rc)
299 goto out;
300 }
301
302out:
303 if (rc) {
304 netdev_warn(adapter->netdev,
305 "%s: Set affinity failed, queue addr = %p, IRQ = %d, rc = %d.\n",
306 __func__, queue, queue->irq, rc);
307 ibmvnic_clean_affinity(adapter);
308 }
309}
310
311static int ibmvnic_cpu_online(unsigned int cpu, struct hlist_node *node)
312{
313 struct ibmvnic_adapter *adapter;
314
315 adapter = hlist_entry_safe(node, struct ibmvnic_adapter, node);
316 ibmvnic_set_affinity(adapter);
317 return 0;
318}
319
320static int ibmvnic_cpu_dead(unsigned int cpu, struct hlist_node *node)
321{
322 struct ibmvnic_adapter *adapter;
323
324 adapter = hlist_entry_safe(node, struct ibmvnic_adapter, node_dead);
325 ibmvnic_set_affinity(adapter);
326 return 0;
327}
328
329static int ibmvnic_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
330{
331 struct ibmvnic_adapter *adapter;
332
333 adapter = hlist_entry_safe(node, struct ibmvnic_adapter, node);
334 ibmvnic_clean_affinity(adapter);
335 return 0;
336}
337
338static enum cpuhp_state ibmvnic_online;
339
340static int ibmvnic_cpu_notif_add(struct ibmvnic_adapter *adapter)
341{
342 int ret;
343
344 ret = cpuhp_state_add_instance_nocalls(ibmvnic_online, &adapter->node);
345 if (ret)
346 return ret;
347 ret = cpuhp_state_add_instance_nocalls(CPUHP_IBMVNIC_DEAD,
348 &adapter->node_dead);
349 if (!ret)
350 return ret;
351 cpuhp_state_remove_instance_nocalls(ibmvnic_online, &adapter->node);
352 return ret;
353}
354
355static void ibmvnic_cpu_notif_remove(struct ibmvnic_adapter *adapter)
356{
357 cpuhp_state_remove_instance_nocalls(ibmvnic_online, &adapter->node);
358 cpuhp_state_remove_instance_nocalls(CPUHP_IBMVNIC_DEAD,
359 &adapter->node_dead);
360}
361
362static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
363 unsigned long length, unsigned long *number,
364 unsigned long *irq)
365{
366 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
367 long rc;
368
369 rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
370 *number = retbuf[0];
371 *irq = retbuf[1];
372
373 return rc;
374}
375
376/**
377 * ibmvnic_wait_for_completion - Check device state and wait for completion
378 * @adapter: private device data
379 * @comp_done: completion structure to wait for
380 * @timeout: time to wait in milliseconds
381 *
382 * Wait for a completion signal or until the timeout limit is reached
383 * while checking that the device is still active.
384 */
385static int ibmvnic_wait_for_completion(struct ibmvnic_adapter *adapter,
386 struct completion *comp_done,
387 unsigned long timeout)
388{
389 struct net_device *netdev;
390 unsigned long div_timeout;
391 u8 retry;
392
393 netdev = adapter->netdev;
394 retry = 5;
395 div_timeout = msecs_to_jiffies(timeout / retry);
396 while (true) {
397 if (!adapter->crq.active) {
398 netdev_err(netdev, "Device down!\n");
399 return -ENODEV;
400 }
401 if (!retry--)
402 break;
403 if (wait_for_completion_timeout(comp_done, div_timeout))
404 return 0;
405 }
406 netdev_err(netdev, "Operation timed out.\n");
407 return -ETIMEDOUT;
408}
409
410/**
411 * reuse_ltb() - Check if a long term buffer can be reused
412 * @ltb: The long term buffer to be checked
413 * @size: The size of the long term buffer.
414 *
415 * An LTB can be reused unless its size has changed.
416 *
417 * Return: Return true if the LTB can be reused, false otherwise.
418 */
419static bool reuse_ltb(struct ibmvnic_long_term_buff *ltb, int size)
420{
421 return (ltb->buff && ltb->size == size);
422}
423
424/**
425 * alloc_long_term_buff() - Allocate a long term buffer (LTB)
426 *
427 * @adapter: ibmvnic adapter associated to the LTB
428 * @ltb: container object for the LTB
429 * @size: size of the LTB
430 *
431 * Allocate an LTB of the specified size and notify VIOS.
432 *
433 * If the given @ltb already has the correct size, reuse it. Otherwise if
434 * its non-NULL, free it. Then allocate a new one of the correct size.
435 * Notify the VIOS either way since we may now be working with a new VIOS.
436 *
437 * Allocating larger chunks of memory during resets, specially LPM or under
438 * low memory situations can cause resets to fail/timeout and for LPAR to
439 * lose connectivity. So hold onto the LTB even if we fail to communicate
440 * with the VIOS and reuse it on next open. Free LTB when adapter is closed.
441 *
442 * Return: 0 if we were able to allocate the LTB and notify the VIOS and
443 * a negative value otherwise.
444 */
445static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
446 struct ibmvnic_long_term_buff *ltb, int size)
447{
448 struct device *dev = &adapter->vdev->dev;
449 u64 prev = 0;
450 int rc;
451
452 if (!reuse_ltb(ltb, size)) {
453 dev_dbg(dev,
454 "LTB size changed from 0x%llx to 0x%x, reallocating\n",
455 ltb->size, size);
456 prev = ltb->size;
457 free_long_term_buff(adapter, ltb);
458 }
459
460 if (ltb->buff) {
461 dev_dbg(dev, "Reusing LTB [map %d, size 0x%llx]\n",
462 ltb->map_id, ltb->size);
463 } else {
464 ltb->buff = dma_alloc_coherent(dev, size, <b->addr,
465 GFP_KERNEL);
466 if (!ltb->buff) {
467 dev_err(dev, "Couldn't alloc long term buffer\n");
468 return -ENOMEM;
469 }
470 ltb->size = size;
471
472 ltb->map_id = find_first_zero_bit(adapter->map_ids,
473 MAX_MAP_ID);
474 bitmap_set(adapter->map_ids, ltb->map_id, 1);
475
476 dev_dbg(dev,
477 "Allocated new LTB [map %d, size 0x%llx was 0x%llx]\n",
478 ltb->map_id, ltb->size, prev);
479 }
480
481 /* Ensure ltb is zeroed - specially when reusing it. */
482 memset(ltb->buff, 0, ltb->size);
483
484 mutex_lock(&adapter->fw_lock);
485 adapter->fw_done_rc = 0;
486 reinit_completion(&adapter->fw_done);
487
488 rc = send_request_map(adapter, ltb->addr, ltb->size, ltb->map_id);
489 if (rc) {
490 dev_err(dev, "send_request_map failed, rc = %d\n", rc);
491 goto out;
492 }
493
494 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
495 if (rc) {
496 dev_err(dev, "LTB map request aborted or timed out, rc = %d\n",
497 rc);
498 goto out;
499 }
500
501 if (adapter->fw_done_rc) {
502 dev_err(dev, "Couldn't map LTB, rc = %d\n",
503 adapter->fw_done_rc);
504 rc = -EIO;
505 goto out;
506 }
507 rc = 0;
508out:
509 /* don't free LTB on communication error - see function header */
510 mutex_unlock(&adapter->fw_lock);
511 return rc;
512}
513
514static void free_long_term_buff(struct ibmvnic_adapter *adapter,
515 struct ibmvnic_long_term_buff *ltb)
516{
517 struct device *dev = &adapter->vdev->dev;
518
519 if (!ltb->buff)
520 return;
521
522 /* VIOS automatically unmaps the long term buffer at remote
523 * end for the following resets:
524 * FAILOVER, MOBILITY, TIMEOUT.
525 */
526 if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
527 adapter->reset_reason != VNIC_RESET_MOBILITY &&
528 adapter->reset_reason != VNIC_RESET_TIMEOUT)
529 send_request_unmap(adapter, ltb->map_id);
530
531 dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
532
533 ltb->buff = NULL;
534 /* mark this map_id free */
535 bitmap_clear(adapter->map_ids, ltb->map_id, 1);
536 ltb->map_id = 0;
537}
538
539/**
540 * free_ltb_set - free the given set of long term buffers (LTBS)
541 * @adapter: The ibmvnic adapter containing this ltb set
542 * @ltb_set: The ltb_set to be freed
543 *
544 * Free the set of LTBs in the given set.
545 */
546
547static void free_ltb_set(struct ibmvnic_adapter *adapter,
548 struct ibmvnic_ltb_set *ltb_set)
549{
550 int i;
551
552 for (i = 0; i < ltb_set->num_ltbs; i++)
553 free_long_term_buff(adapter, <b_set->ltbs[i]);
554
555 kfree(ltb_set->ltbs);
556 ltb_set->ltbs = NULL;
557 ltb_set->num_ltbs = 0;
558}
559
560/**
561 * alloc_ltb_set() - Allocate a set of long term buffers (LTBs)
562 *
563 * @adapter: ibmvnic adapter associated to the LTB
564 * @ltb_set: container object for the set of LTBs
565 * @num_buffs: Number of buffers in the LTB
566 * @buff_size: Size of each buffer in the LTB
567 *
568 * Allocate a set of LTBs to accommodate @num_buffs buffers of @buff_size
569 * each. We currently cap size each LTB to IBMVNIC_ONE_LTB_SIZE. If the
570 * new set of LTBs have fewer LTBs than the old set, free the excess LTBs.
571 * If new set needs more than in old set, allocate the remaining ones.
572 * Try and reuse as many LTBs as possible and avoid reallocation.
573 *
574 * Any changes to this allocation strategy must be reflected in
575 * map_rxpool_buff_to_ltb() and map_txpool_buff_to_ltb().
576 */
577static int alloc_ltb_set(struct ibmvnic_adapter *adapter,
578 struct ibmvnic_ltb_set *ltb_set, int num_buffs,
579 int buff_size)
580{
581 struct device *dev = &adapter->vdev->dev;
582 struct ibmvnic_ltb_set old_set;
583 struct ibmvnic_ltb_set new_set;
584 int rem_size;
585 int tot_size; /* size of all ltbs */
586 int ltb_size; /* size of one ltb */
587 int nltbs;
588 int rc;
589 int n;
590 int i;
591
592 dev_dbg(dev, "%s() num_buffs %d, buff_size %d\n", __func__, num_buffs,
593 buff_size);
594
595 ltb_size = rounddown(IBMVNIC_ONE_LTB_SIZE, buff_size);
596 tot_size = num_buffs * buff_size;
597
598 if (ltb_size > tot_size)
599 ltb_size = tot_size;
600
601 nltbs = tot_size / ltb_size;
602 if (tot_size % ltb_size)
603 nltbs++;
604
605 old_set = *ltb_set;
606
607 if (old_set.num_ltbs == nltbs) {
608 new_set = old_set;
609 } else {
610 int tmp = nltbs * sizeof(struct ibmvnic_long_term_buff);
611
612 new_set.ltbs = kzalloc(tmp, GFP_KERNEL);
613 if (!new_set.ltbs)
614 return -ENOMEM;
615
616 new_set.num_ltbs = nltbs;
617
618 /* Free any excess ltbs in old set */
619 for (i = new_set.num_ltbs; i < old_set.num_ltbs; i++)
620 free_long_term_buff(adapter, &old_set.ltbs[i]);
621
622 /* Copy remaining ltbs to new set. All LTBs except the
623 * last one are of the same size. alloc_long_term_buff()
624 * will realloc if the size changes.
625 */
626 n = min(old_set.num_ltbs, new_set.num_ltbs);
627 for (i = 0; i < n; i++)
628 new_set.ltbs[i] = old_set.ltbs[i];
629
630 /* Any additional ltbs in new set will have NULL ltbs for
631 * now and will be allocated in alloc_long_term_buff().
632 */
633
634 /* We no longer need the old_set so free it. Note that we
635 * may have reused some ltbs from old set and freed excess
636 * ltbs above. So we only need to free the container now
637 * not the LTBs themselves. (i.e. dont free_ltb_set()!)
638 */
639 kfree(old_set.ltbs);
640 old_set.ltbs = NULL;
641 old_set.num_ltbs = 0;
642
643 /* Install the new set. If allocations fail below, we will
644 * retry later and know what size LTBs we need.
645 */
646 *ltb_set = new_set;
647 }
648
649 i = 0;
650 rem_size = tot_size;
651 while (rem_size) {
652 if (ltb_size > rem_size)
653 ltb_size = rem_size;
654
655 rem_size -= ltb_size;
656
657 rc = alloc_long_term_buff(adapter, &new_set.ltbs[i], ltb_size);
658 if (rc)
659 goto out;
660 i++;
661 }
662
663 WARN_ON(i != new_set.num_ltbs);
664
665 return 0;
666out:
667 /* We may have allocated one/more LTBs before failing and we
668 * want to try and reuse on next reset. So don't free ltb set.
669 */
670 return rc;
671}
672
673/**
674 * map_rxpool_buf_to_ltb - Map given rxpool buffer to offset in an LTB.
675 * @rxpool: The receive buffer pool containing buffer
676 * @bufidx: Index of buffer in rxpool
677 * @ltbp: (Output) pointer to the long term buffer containing the buffer
678 * @offset: (Output) offset of buffer in the LTB from @ltbp
679 *
680 * Map the given buffer identified by [rxpool, bufidx] to an LTB in the
681 * pool and its corresponding offset. Assume for now that each LTB is of
682 * different size but could possibly be optimized based on the allocation
683 * strategy in alloc_ltb_set().
684 */
685static void map_rxpool_buf_to_ltb(struct ibmvnic_rx_pool *rxpool,
686 unsigned int bufidx,
687 struct ibmvnic_long_term_buff **ltbp,
688 unsigned int *offset)
689{
690 struct ibmvnic_long_term_buff *ltb;
691 int nbufs; /* # of buffers in one ltb */
692 int i;
693
694 WARN_ON(bufidx >= rxpool->size);
695
696 for (i = 0; i < rxpool->ltb_set.num_ltbs; i++) {
697 ltb = &rxpool->ltb_set.ltbs[i];
698 nbufs = ltb->size / rxpool->buff_size;
699 if (bufidx < nbufs)
700 break;
701 bufidx -= nbufs;
702 }
703
704 *ltbp = ltb;
705 *offset = bufidx * rxpool->buff_size;
706}
707
708/**
709 * map_txpool_buf_to_ltb - Map given txpool buffer to offset in an LTB.
710 * @txpool: The transmit buffer pool containing buffer
711 * @bufidx: Index of buffer in txpool
712 * @ltbp: (Output) pointer to the long term buffer (LTB) containing the buffer
713 * @offset: (Output) offset of buffer in the LTB from @ltbp
714 *
715 * Map the given buffer identified by [txpool, bufidx] to an LTB in the
716 * pool and its corresponding offset.
717 */
718static void map_txpool_buf_to_ltb(struct ibmvnic_tx_pool *txpool,
719 unsigned int bufidx,
720 struct ibmvnic_long_term_buff **ltbp,
721 unsigned int *offset)
722{
723 struct ibmvnic_long_term_buff *ltb;
724 int nbufs; /* # of buffers in one ltb */
725 int i;
726
727 WARN_ON_ONCE(bufidx >= txpool->num_buffers);
728
729 for (i = 0; i < txpool->ltb_set.num_ltbs; i++) {
730 ltb = &txpool->ltb_set.ltbs[i];
731 nbufs = ltb->size / txpool->buf_size;
732 if (bufidx < nbufs)
733 break;
734 bufidx -= nbufs;
735 }
736
737 *ltbp = ltb;
738 *offset = bufidx * txpool->buf_size;
739}
740
741static void deactivate_rx_pools(struct ibmvnic_adapter *adapter)
742{
743 int i;
744
745 for (i = 0; i < adapter->num_active_rx_pools; i++)
746 adapter->rx_pool[i].active = 0;
747}
748
749static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
750 struct ibmvnic_rx_pool *pool)
751{
752 int count = pool->size - atomic_read(&pool->available);
753 u64 handle = adapter->rx_scrq[pool->index]->handle;
754 struct device *dev = &adapter->vdev->dev;
755 struct ibmvnic_ind_xmit_queue *ind_bufp;
756 struct ibmvnic_sub_crq_queue *rx_scrq;
757 struct ibmvnic_long_term_buff *ltb;
758 union sub_crq *sub_crq;
759 int buffers_added = 0;
760 unsigned long lpar_rc;
761 struct sk_buff *skb;
762 unsigned int offset;
763 dma_addr_t dma_addr;
764 unsigned char *dst;
765 int shift = 0;
766 int bufidx;
767 int i;
768
769 if (!pool->active)
770 return;
771
772 rx_scrq = adapter->rx_scrq[pool->index];
773 ind_bufp = &rx_scrq->ind_buf;
774
775 /* netdev_skb_alloc() could have failed after we saved a few skbs
776 * in the indir_buf and we would not have sent them to VIOS yet.
777 * To account for them, start the loop at ind_bufp->index rather
778 * than 0. If we pushed all the skbs to VIOS, ind_bufp->index will
779 * be 0.
780 */
781 for (i = ind_bufp->index; i < count; ++i) {
782 bufidx = pool->free_map[pool->next_free];
783
784 /* We maybe reusing the skb from earlier resets. Allocate
785 * only if necessary. But since the LTB may have changed
786 * during reset (see init_rx_pools()), update LTB below
787 * even if reusing skb.
788 */
789 skb = pool->rx_buff[bufidx].skb;
790 if (!skb) {
791 skb = netdev_alloc_skb(adapter->netdev,
792 pool->buff_size);
793 if (!skb) {
794 dev_err(dev, "Couldn't replenish rx buff\n");
795 adapter->replenish_no_mem++;
796 break;
797 }
798 }
799
800 pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
801 pool->next_free = (pool->next_free + 1) % pool->size;
802
803 /* Copy the skb to the long term mapped DMA buffer */
804 map_rxpool_buf_to_ltb(pool, bufidx, <b, &offset);
805 dst = ltb->buff + offset;
806 memset(dst, 0, pool->buff_size);
807 dma_addr = ltb->addr + offset;
808
809 /* add the skb to an rx_buff in the pool */
810 pool->rx_buff[bufidx].data = dst;
811 pool->rx_buff[bufidx].dma = dma_addr;
812 pool->rx_buff[bufidx].skb = skb;
813 pool->rx_buff[bufidx].pool_index = pool->index;
814 pool->rx_buff[bufidx].size = pool->buff_size;
815
816 /* queue the rx_buff for the next send_subcrq_indirect */
817 sub_crq = &ind_bufp->indir_arr[ind_bufp->index++];
818 memset(sub_crq, 0, sizeof(*sub_crq));
819 sub_crq->rx_add.first = IBMVNIC_CRQ_CMD;
820 sub_crq->rx_add.correlator =
821 cpu_to_be64((u64)&pool->rx_buff[bufidx]);
822 sub_crq->rx_add.ioba = cpu_to_be32(dma_addr);
823 sub_crq->rx_add.map_id = ltb->map_id;
824
825 /* The length field of the sCRQ is defined to be 24 bits so the
826 * buffer size needs to be left shifted by a byte before it is
827 * converted to big endian to prevent the last byte from being
828 * truncated.
829 */
830#ifdef __LITTLE_ENDIAN__
831 shift = 8;
832#endif
833 sub_crq->rx_add.len = cpu_to_be32(pool->buff_size << shift);
834
835 /* if send_subcrq_indirect queue is full, flush to VIOS */
836 if (ind_bufp->index == IBMVNIC_MAX_IND_DESCS ||
837 i == count - 1) {
838 lpar_rc =
839 send_subcrq_indirect(adapter, handle,
840 (u64)ind_bufp->indir_dma,
841 (u64)ind_bufp->index);
842 if (lpar_rc != H_SUCCESS)
843 goto failure;
844 buffers_added += ind_bufp->index;
845 adapter->replenish_add_buff_success += ind_bufp->index;
846 ind_bufp->index = 0;
847 }
848 }
849 atomic_add(buffers_added, &pool->available);
850 return;
851
852failure:
853 if (lpar_rc != H_PARAMETER && lpar_rc != H_CLOSED)
854 dev_err_ratelimited(dev, "rx: replenish packet buffer failed\n");
855 for (i = ind_bufp->index - 1; i >= 0; --i) {
856 struct ibmvnic_rx_buff *rx_buff;
857
858 pool->next_free = pool->next_free == 0 ?
859 pool->size - 1 : pool->next_free - 1;
860 sub_crq = &ind_bufp->indir_arr[i];
861 rx_buff = (struct ibmvnic_rx_buff *)
862 be64_to_cpu(sub_crq->rx_add.correlator);
863 bufidx = (int)(rx_buff - pool->rx_buff);
864 pool->free_map[pool->next_free] = bufidx;
865 dev_kfree_skb_any(pool->rx_buff[bufidx].skb);
866 pool->rx_buff[bufidx].skb = NULL;
867 }
868 adapter->replenish_add_buff_failure += ind_bufp->index;
869 atomic_add(buffers_added, &pool->available);
870 ind_bufp->index = 0;
871 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
872 /* Disable buffer pool replenishment and report carrier off if
873 * queue is closed or pending failover.
874 * Firmware guarantees that a signal will be sent to the
875 * driver, triggering a reset.
876 */
877 deactivate_rx_pools(adapter);
878 netif_carrier_off(adapter->netdev);
879 }
880}
881
882static void replenish_pools(struct ibmvnic_adapter *adapter)
883{
884 int i;
885
886 adapter->replenish_task_cycles++;
887 for (i = 0; i < adapter->num_active_rx_pools; i++) {
888 if (adapter->rx_pool[i].active)
889 replenish_rx_pool(adapter, &adapter->rx_pool[i]);
890 }
891
892 netdev_dbg(adapter->netdev, "Replenished %d pools\n", i);
893}
894
895static void release_stats_buffers(struct ibmvnic_adapter *adapter)
896{
897 kfree(adapter->tx_stats_buffers);
898 kfree(adapter->rx_stats_buffers);
899 adapter->tx_stats_buffers = NULL;
900 adapter->rx_stats_buffers = NULL;
901}
902
903static int init_stats_buffers(struct ibmvnic_adapter *adapter)
904{
905 adapter->tx_stats_buffers =
906 kcalloc(IBMVNIC_MAX_QUEUES,
907 sizeof(struct ibmvnic_tx_queue_stats),
908 GFP_KERNEL);
909 if (!adapter->tx_stats_buffers)
910 return -ENOMEM;
911
912 adapter->rx_stats_buffers =
913 kcalloc(IBMVNIC_MAX_QUEUES,
914 sizeof(struct ibmvnic_rx_queue_stats),
915 GFP_KERNEL);
916 if (!adapter->rx_stats_buffers)
917 return -ENOMEM;
918
919 return 0;
920}
921
922static void release_stats_token(struct ibmvnic_adapter *adapter)
923{
924 struct device *dev = &adapter->vdev->dev;
925
926 if (!adapter->stats_token)
927 return;
928
929 dma_unmap_single(dev, adapter->stats_token,
930 sizeof(struct ibmvnic_statistics),
931 DMA_FROM_DEVICE);
932 adapter->stats_token = 0;
933}
934
935static int init_stats_token(struct ibmvnic_adapter *adapter)
936{
937 struct device *dev = &adapter->vdev->dev;
938 dma_addr_t stok;
939 int rc;
940
941 stok = dma_map_single(dev, &adapter->stats,
942 sizeof(struct ibmvnic_statistics),
943 DMA_FROM_DEVICE);
944 rc = dma_mapping_error(dev, stok);
945 if (rc) {
946 dev_err(dev, "Couldn't map stats buffer, rc = %d\n", rc);
947 return rc;
948 }
949
950 adapter->stats_token = stok;
951 netdev_dbg(adapter->netdev, "Stats token initialized (%llx)\n", stok);
952 return 0;
953}
954
955/**
956 * release_rx_pools() - Release any rx pools attached to @adapter.
957 * @adapter: ibmvnic adapter
958 *
959 * Safe to call this multiple times - even if no pools are attached.
960 */
961static void release_rx_pools(struct ibmvnic_adapter *adapter)
962{
963 struct ibmvnic_rx_pool *rx_pool;
964 int i, j;
965
966 if (!adapter->rx_pool)
967 return;
968
969 for (i = 0; i < adapter->num_active_rx_pools; i++) {
970 rx_pool = &adapter->rx_pool[i];
971
972 netdev_dbg(adapter->netdev, "Releasing rx_pool[%d]\n", i);
973
974 kfree(rx_pool->free_map);
975
976 free_ltb_set(adapter, &rx_pool->ltb_set);
977
978 if (!rx_pool->rx_buff)
979 continue;
980
981 for (j = 0; j < rx_pool->size; j++) {
982 if (rx_pool->rx_buff[j].skb) {
983 dev_kfree_skb_any(rx_pool->rx_buff[j].skb);
984 rx_pool->rx_buff[j].skb = NULL;
985 }
986 }
987
988 kfree(rx_pool->rx_buff);
989 }
990
991 kfree(adapter->rx_pool);
992 adapter->rx_pool = NULL;
993 adapter->num_active_rx_pools = 0;
994 adapter->prev_rx_pool_size = 0;
995}
996
997/**
998 * reuse_rx_pools() - Check if the existing rx pools can be reused.
999 * @adapter: ibmvnic adapter
1000 *
1001 * Check if the existing rx pools in the adapter can be reused. The
1002 * pools can be reused if the pool parameters (number of pools,
1003 * number of buffers in the pool and size of each buffer) have not
1004 * changed.
1005 *
1006 * NOTE: This assumes that all pools have the same number of buffers
1007 * which is the case currently. If that changes, we must fix this.
1008 *
1009 * Return: true if the rx pools can be reused, false otherwise.
1010 */
1011static bool reuse_rx_pools(struct ibmvnic_adapter *adapter)
1012{
1013 u64 old_num_pools, new_num_pools;
1014 u64 old_pool_size, new_pool_size;
1015 u64 old_buff_size, new_buff_size;
1016
1017 if (!adapter->rx_pool)
1018 return false;
1019
1020 old_num_pools = adapter->num_active_rx_pools;
1021 new_num_pools = adapter->req_rx_queues;
1022
1023 old_pool_size = adapter->prev_rx_pool_size;
1024 new_pool_size = adapter->req_rx_add_entries_per_subcrq;
1025
1026 old_buff_size = adapter->prev_rx_buf_sz;
1027 new_buff_size = adapter->cur_rx_buf_sz;
1028
1029 if (old_buff_size != new_buff_size ||
1030 old_num_pools != new_num_pools ||
1031 old_pool_size != new_pool_size)
1032 return false;
1033
1034 return true;
1035}
1036
1037/**
1038 * init_rx_pools(): Initialize the set of receiver pools in the adapter.
1039 * @netdev: net device associated with the vnic interface
1040 *
1041 * Initialize the set of receiver pools in the ibmvnic adapter associated
1042 * with the net_device @netdev. If possible, reuse the existing rx pools.
1043 * Otherwise free any existing pools and allocate a new set of pools
1044 * before initializing them.
1045 *
1046 * Return: 0 on success and negative value on error.
1047 */
1048static int init_rx_pools(struct net_device *netdev)
1049{
1050 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1051 struct device *dev = &adapter->vdev->dev;
1052 struct ibmvnic_rx_pool *rx_pool;
1053 u64 num_pools;
1054 u64 pool_size; /* # of buffers in one pool */
1055 u64 buff_size;
1056 int i, j, rc;
1057
1058 pool_size = adapter->req_rx_add_entries_per_subcrq;
1059 num_pools = adapter->req_rx_queues;
1060 buff_size = adapter->cur_rx_buf_sz;
1061
1062 if (reuse_rx_pools(adapter)) {
1063 dev_dbg(dev, "Reusing rx pools\n");
1064 goto update_ltb;
1065 }
1066
1067 /* Allocate/populate the pools. */
1068 release_rx_pools(adapter);
1069
1070 adapter->rx_pool = kcalloc(num_pools,
1071 sizeof(struct ibmvnic_rx_pool),
1072 GFP_KERNEL);
1073 if (!adapter->rx_pool) {
1074 dev_err(dev, "Failed to allocate rx pools\n");
1075 return -ENOMEM;
1076 }
1077
1078 /* Set num_active_rx_pools early. If we fail below after partial
1079 * allocation, release_rx_pools() will know how many to look for.
1080 */
1081 adapter->num_active_rx_pools = num_pools;
1082
1083 for (i = 0; i < num_pools; i++) {
1084 rx_pool = &adapter->rx_pool[i];
1085
1086 netdev_dbg(adapter->netdev,
1087 "Initializing rx_pool[%d], %lld buffs, %lld bytes each\n",
1088 i, pool_size, buff_size);
1089
1090 rx_pool->size = pool_size;
1091 rx_pool->index = i;
1092 rx_pool->buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
1093
1094 rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
1095 GFP_KERNEL);
1096 if (!rx_pool->free_map) {
1097 dev_err(dev, "Couldn't alloc free_map %d\n", i);
1098 rc = -ENOMEM;
1099 goto out_release;
1100 }
1101
1102 rx_pool->rx_buff = kcalloc(rx_pool->size,
1103 sizeof(struct ibmvnic_rx_buff),
1104 GFP_KERNEL);
1105 if (!rx_pool->rx_buff) {
1106 dev_err(dev, "Couldn't alloc rx buffers\n");
1107 rc = -ENOMEM;
1108 goto out_release;
1109 }
1110 }
1111
1112 adapter->prev_rx_pool_size = pool_size;
1113 adapter->prev_rx_buf_sz = adapter->cur_rx_buf_sz;
1114
1115update_ltb:
1116 for (i = 0; i < num_pools; i++) {
1117 rx_pool = &adapter->rx_pool[i];
1118 dev_dbg(dev, "Updating LTB for rx pool %d [%d, %d]\n",
1119 i, rx_pool->size, rx_pool->buff_size);
1120
1121 rc = alloc_ltb_set(adapter, &rx_pool->ltb_set,
1122 rx_pool->size, rx_pool->buff_size);
1123 if (rc)
1124 goto out;
1125
1126 for (j = 0; j < rx_pool->size; ++j) {
1127 struct ibmvnic_rx_buff *rx_buff;
1128
1129 rx_pool->free_map[j] = j;
1130
1131 /* NOTE: Don't clear rx_buff->skb here - will leak
1132 * memory! replenish_rx_pool() will reuse skbs or
1133 * allocate as necessary.
1134 */
1135 rx_buff = &rx_pool->rx_buff[j];
1136 rx_buff->dma = 0;
1137 rx_buff->data = 0;
1138 rx_buff->size = 0;
1139 rx_buff->pool_index = 0;
1140 }
1141
1142 /* Mark pool "empty" so replenish_rx_pools() will
1143 * update the LTB info for each buffer
1144 */
1145 atomic_set(&rx_pool->available, 0);
1146 rx_pool->next_alloc = 0;
1147 rx_pool->next_free = 0;
1148 /* replenish_rx_pool() may have called deactivate_rx_pools()
1149 * on failover. Ensure pool is active now.
1150 */
1151 rx_pool->active = 1;
1152 }
1153 return 0;
1154out_release:
1155 release_rx_pools(adapter);
1156out:
1157 /* We failed to allocate one or more LTBs or map them on the VIOS.
1158 * Hold onto the pools and any LTBs that we did allocate/map.
1159 */
1160 return rc;
1161}
1162
1163static void release_vpd_data(struct ibmvnic_adapter *adapter)
1164{
1165 if (!adapter->vpd)
1166 return;
1167
1168 kfree(adapter->vpd->buff);
1169 kfree(adapter->vpd);
1170
1171 adapter->vpd = NULL;
1172}
1173
1174static void release_one_tx_pool(struct ibmvnic_adapter *adapter,
1175 struct ibmvnic_tx_pool *tx_pool)
1176{
1177 kfree(tx_pool->tx_buff);
1178 kfree(tx_pool->free_map);
1179 free_ltb_set(adapter, &tx_pool->ltb_set);
1180}
1181
1182/**
1183 * release_tx_pools() - Release any tx pools attached to @adapter.
1184 * @adapter: ibmvnic adapter
1185 *
1186 * Safe to call this multiple times - even if no pools are attached.
1187 */
1188static void release_tx_pools(struct ibmvnic_adapter *adapter)
1189{
1190 int i;
1191
1192 /* init_tx_pools() ensures that ->tx_pool and ->tso_pool are
1193 * both NULL or both non-NULL. So we only need to check one.
1194 */
1195 if (!adapter->tx_pool)
1196 return;
1197
1198 for (i = 0; i < adapter->num_active_tx_pools; i++) {
1199 release_one_tx_pool(adapter, &adapter->tx_pool[i]);
1200 release_one_tx_pool(adapter, &adapter->tso_pool[i]);
1201 }
1202
1203 kfree(adapter->tx_pool);
1204 adapter->tx_pool = NULL;
1205 kfree(adapter->tso_pool);
1206 adapter->tso_pool = NULL;
1207 adapter->num_active_tx_pools = 0;
1208 adapter->prev_tx_pool_size = 0;
1209}
1210
1211static int init_one_tx_pool(struct net_device *netdev,
1212 struct ibmvnic_tx_pool *tx_pool,
1213 int pool_size, int buf_size)
1214{
1215 int i;
1216
1217 tx_pool->tx_buff = kcalloc(pool_size,
1218 sizeof(struct ibmvnic_tx_buff),
1219 GFP_KERNEL);
1220 if (!tx_pool->tx_buff)
1221 return -ENOMEM;
1222
1223 tx_pool->free_map = kcalloc(pool_size, sizeof(int), GFP_KERNEL);
1224 if (!tx_pool->free_map) {
1225 kfree(tx_pool->tx_buff);
1226 tx_pool->tx_buff = NULL;
1227 return -ENOMEM;
1228 }
1229
1230 for (i = 0; i < pool_size; i++)
1231 tx_pool->free_map[i] = i;
1232
1233 tx_pool->consumer_index = 0;
1234 tx_pool->producer_index = 0;
1235 tx_pool->num_buffers = pool_size;
1236 tx_pool->buf_size = buf_size;
1237
1238 return 0;
1239}
1240
1241/**
1242 * reuse_tx_pools() - Check if the existing tx pools can be reused.
1243 * @adapter: ibmvnic adapter
1244 *
1245 * Check if the existing tx pools in the adapter can be reused. The
1246 * pools can be reused if the pool parameters (number of pools,
1247 * number of buffers in the pool and mtu) have not changed.
1248 *
1249 * NOTE: This assumes that all pools have the same number of buffers
1250 * which is the case currently. If that changes, we must fix this.
1251 *
1252 * Return: true if the tx pools can be reused, false otherwise.
1253 */
1254static bool reuse_tx_pools(struct ibmvnic_adapter *adapter)
1255{
1256 u64 old_num_pools, new_num_pools;
1257 u64 old_pool_size, new_pool_size;
1258 u64 old_mtu, new_mtu;
1259
1260 if (!adapter->tx_pool)
1261 return false;
1262
1263 old_num_pools = adapter->num_active_tx_pools;
1264 new_num_pools = adapter->num_active_tx_scrqs;
1265 old_pool_size = adapter->prev_tx_pool_size;
1266 new_pool_size = adapter->req_tx_entries_per_subcrq;
1267 old_mtu = adapter->prev_mtu;
1268 new_mtu = adapter->req_mtu;
1269
1270 if (old_mtu != new_mtu ||
1271 old_num_pools != new_num_pools ||
1272 old_pool_size != new_pool_size)
1273 return false;
1274
1275 return true;
1276}
1277
1278/**
1279 * init_tx_pools(): Initialize the set of transmit pools in the adapter.
1280 * @netdev: net device associated with the vnic interface
1281 *
1282 * Initialize the set of transmit pools in the ibmvnic adapter associated
1283 * with the net_device @netdev. If possible, reuse the existing tx pools.
1284 * Otherwise free any existing pools and allocate a new set of pools
1285 * before initializing them.
1286 *
1287 * Return: 0 on success and negative value on error.
1288 */
1289static int init_tx_pools(struct net_device *netdev)
1290{
1291 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1292 struct device *dev = &adapter->vdev->dev;
1293 int num_pools;
1294 u64 pool_size; /* # of buffers in pool */
1295 u64 buff_size;
1296 int i, j, rc;
1297
1298 num_pools = adapter->req_tx_queues;
1299
1300 /* We must notify the VIOS about the LTB on all resets - but we only
1301 * need to alloc/populate pools if either the number of buffers or
1302 * size of each buffer in the pool has changed.
1303 */
1304 if (reuse_tx_pools(adapter)) {
1305 netdev_dbg(netdev, "Reusing tx pools\n");
1306 goto update_ltb;
1307 }
1308
1309 /* Allocate/populate the pools. */
1310 release_tx_pools(adapter);
1311
1312 pool_size = adapter->req_tx_entries_per_subcrq;
1313 num_pools = adapter->num_active_tx_scrqs;
1314
1315 adapter->tx_pool = kcalloc(num_pools,
1316 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
1317 if (!adapter->tx_pool)
1318 return -ENOMEM;
1319
1320 adapter->tso_pool = kcalloc(num_pools,
1321 sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
1322 /* To simplify release_tx_pools() ensure that ->tx_pool and
1323 * ->tso_pool are either both NULL or both non-NULL.
1324 */
1325 if (!adapter->tso_pool) {
1326 kfree(adapter->tx_pool);
1327 adapter->tx_pool = NULL;
1328 return -ENOMEM;
1329 }
1330
1331 /* Set num_active_tx_pools early. If we fail below after partial
1332 * allocation, release_tx_pools() will know how many to look for.
1333 */
1334 adapter->num_active_tx_pools = num_pools;
1335
1336 buff_size = adapter->req_mtu + VLAN_HLEN;
1337 buff_size = ALIGN(buff_size, L1_CACHE_BYTES);
1338
1339 for (i = 0; i < num_pools; i++) {
1340 dev_dbg(dev, "Init tx pool %d [%llu, %llu]\n",
1341 i, adapter->req_tx_entries_per_subcrq, buff_size);
1342
1343 rc = init_one_tx_pool(netdev, &adapter->tx_pool[i],
1344 pool_size, buff_size);
1345 if (rc)
1346 goto out_release;
1347
1348 rc = init_one_tx_pool(netdev, &adapter->tso_pool[i],
1349 IBMVNIC_TSO_BUFS,
1350 IBMVNIC_TSO_BUF_SZ);
1351 if (rc)
1352 goto out_release;
1353 }
1354
1355 adapter->prev_tx_pool_size = pool_size;
1356 adapter->prev_mtu = adapter->req_mtu;
1357
1358update_ltb:
1359 /* NOTE: All tx_pools have the same number of buffers (which is
1360 * same as pool_size). All tso_pools have IBMVNIC_TSO_BUFS
1361 * buffers (see calls init_one_tx_pool() for these).
1362 * For consistency, we use tx_pool->num_buffers and
1363 * tso_pool->num_buffers below.
1364 */
1365 rc = -1;
1366 for (i = 0; i < num_pools; i++) {
1367 struct ibmvnic_tx_pool *tso_pool;
1368 struct ibmvnic_tx_pool *tx_pool;
1369
1370 tx_pool = &adapter->tx_pool[i];
1371
1372 dev_dbg(dev, "Updating LTB for tx pool %d [%d, %d]\n",
1373 i, tx_pool->num_buffers, tx_pool->buf_size);
1374
1375 rc = alloc_ltb_set(adapter, &tx_pool->ltb_set,
1376 tx_pool->num_buffers, tx_pool->buf_size);
1377 if (rc)
1378 goto out;
1379
1380 tx_pool->consumer_index = 0;
1381 tx_pool->producer_index = 0;
1382
1383 for (j = 0; j < tx_pool->num_buffers; j++)
1384 tx_pool->free_map[j] = j;
1385
1386 tso_pool = &adapter->tso_pool[i];
1387
1388 dev_dbg(dev, "Updating LTB for tso pool %d [%d, %d]\n",
1389 i, tso_pool->num_buffers, tso_pool->buf_size);
1390
1391 rc = alloc_ltb_set(adapter, &tso_pool->ltb_set,
1392 tso_pool->num_buffers, tso_pool->buf_size);
1393 if (rc)
1394 goto out;
1395
1396 tso_pool->consumer_index = 0;
1397 tso_pool->producer_index = 0;
1398
1399 for (j = 0; j < tso_pool->num_buffers; j++)
1400 tso_pool->free_map[j] = j;
1401 }
1402
1403 return 0;
1404out_release:
1405 release_tx_pools(adapter);
1406out:
1407 /* We failed to allocate one or more LTBs or map them on the VIOS.
1408 * Hold onto the pools and any LTBs that we did allocate/map.
1409 */
1410 return rc;
1411}
1412
1413static void ibmvnic_napi_enable(struct ibmvnic_adapter *adapter)
1414{
1415 int i;
1416
1417 if (adapter->napi_enabled)
1418 return;
1419
1420 for (i = 0; i < adapter->req_rx_queues; i++)
1421 napi_enable(&adapter->napi[i]);
1422
1423 adapter->napi_enabled = true;
1424}
1425
1426static void ibmvnic_napi_disable(struct ibmvnic_adapter *adapter)
1427{
1428 int i;
1429
1430 if (!adapter->napi_enabled)
1431 return;
1432
1433 for (i = 0; i < adapter->req_rx_queues; i++) {
1434 netdev_dbg(adapter->netdev, "Disabling napi[%d]\n", i);
1435 napi_disable(&adapter->napi[i]);
1436 }
1437
1438 adapter->napi_enabled = false;
1439}
1440
1441static int init_napi(struct ibmvnic_adapter *adapter)
1442{
1443 int i;
1444
1445 adapter->napi = kcalloc(adapter->req_rx_queues,
1446 sizeof(struct napi_struct), GFP_KERNEL);
1447 if (!adapter->napi)
1448 return -ENOMEM;
1449
1450 for (i = 0; i < adapter->req_rx_queues; i++) {
1451 netdev_dbg(adapter->netdev, "Adding napi[%d]\n", i);
1452 netif_napi_add(adapter->netdev, &adapter->napi[i],
1453 ibmvnic_poll);
1454 }
1455
1456 adapter->num_active_rx_napi = adapter->req_rx_queues;
1457 return 0;
1458}
1459
1460static void release_napi(struct ibmvnic_adapter *adapter)
1461{
1462 int i;
1463
1464 if (!adapter->napi)
1465 return;
1466
1467 for (i = 0; i < adapter->num_active_rx_napi; i++) {
1468 netdev_dbg(adapter->netdev, "Releasing napi[%d]\n", i);
1469 netif_napi_del(&adapter->napi[i]);
1470 }
1471
1472 kfree(adapter->napi);
1473 adapter->napi = NULL;
1474 adapter->num_active_rx_napi = 0;
1475 adapter->napi_enabled = false;
1476}
1477
1478static const char *adapter_state_to_string(enum vnic_state state)
1479{
1480 switch (state) {
1481 case VNIC_PROBING:
1482 return "PROBING";
1483 case VNIC_PROBED:
1484 return "PROBED";
1485 case VNIC_OPENING:
1486 return "OPENING";
1487 case VNIC_OPEN:
1488 return "OPEN";
1489 case VNIC_CLOSING:
1490 return "CLOSING";
1491 case VNIC_CLOSED:
1492 return "CLOSED";
1493 case VNIC_REMOVING:
1494 return "REMOVING";
1495 case VNIC_REMOVED:
1496 return "REMOVED";
1497 case VNIC_DOWN:
1498 return "DOWN";
1499 }
1500 return "UNKNOWN";
1501}
1502
1503static int ibmvnic_login(struct net_device *netdev)
1504{
1505 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1506 unsigned long timeout = msecs_to_jiffies(20000);
1507 int retry_count = 0;
1508 int retries = 10;
1509 bool retry;
1510 int rc;
1511
1512 do {
1513 retry = false;
1514 if (retry_count > retries) {
1515 netdev_warn(netdev, "Login attempts exceeded\n");
1516 return -EACCES;
1517 }
1518
1519 adapter->init_done_rc = 0;
1520 reinit_completion(&adapter->init_done);
1521 rc = send_login(adapter);
1522 if (rc)
1523 return rc;
1524
1525 if (!wait_for_completion_timeout(&adapter->init_done,
1526 timeout)) {
1527 netdev_warn(netdev, "Login timed out, retrying...\n");
1528 retry = true;
1529 adapter->init_done_rc = 0;
1530 retry_count++;
1531 continue;
1532 }
1533
1534 if (adapter->init_done_rc == ABORTED) {
1535 netdev_warn(netdev, "Login aborted, retrying...\n");
1536 retry = true;
1537 adapter->init_done_rc = 0;
1538 retry_count++;
1539 /* FW or device may be busy, so
1540 * wait a bit before retrying login
1541 */
1542 msleep(500);
1543 } else if (adapter->init_done_rc == PARTIALSUCCESS) {
1544 retry_count++;
1545 release_sub_crqs(adapter, 1);
1546
1547 retry = true;
1548 netdev_dbg(netdev,
1549 "Received partial success, retrying...\n");
1550 adapter->init_done_rc = 0;
1551 reinit_completion(&adapter->init_done);
1552 send_query_cap(adapter);
1553 if (!wait_for_completion_timeout(&adapter->init_done,
1554 timeout)) {
1555 netdev_warn(netdev,
1556 "Capabilities query timed out\n");
1557 return -ETIMEDOUT;
1558 }
1559
1560 rc = init_sub_crqs(adapter);
1561 if (rc) {
1562 netdev_warn(netdev,
1563 "SCRQ initialization failed\n");
1564 return rc;
1565 }
1566
1567 rc = init_sub_crq_irqs(adapter);
1568 if (rc) {
1569 netdev_warn(netdev,
1570 "SCRQ irq initialization failed\n");
1571 return rc;
1572 }
1573 } else if (adapter->init_done_rc) {
1574 netdev_warn(netdev, "Adapter login failed, init_done_rc = %d\n",
1575 adapter->init_done_rc);
1576 return -EIO;
1577 }
1578 } while (retry);
1579
1580 __ibmvnic_set_mac(netdev, adapter->mac_addr);
1581
1582 netdev_dbg(netdev, "[S:%s] Login succeeded\n", adapter_state_to_string(adapter->state));
1583 return 0;
1584}
1585
1586static void release_login_buffer(struct ibmvnic_adapter *adapter)
1587{
1588 kfree(adapter->login_buf);
1589 adapter->login_buf = NULL;
1590}
1591
1592static void release_login_rsp_buffer(struct ibmvnic_adapter *adapter)
1593{
1594 kfree(adapter->login_rsp_buf);
1595 adapter->login_rsp_buf = NULL;
1596}
1597
1598static void release_resources(struct ibmvnic_adapter *adapter)
1599{
1600 release_vpd_data(adapter);
1601
1602 release_napi(adapter);
1603 release_login_buffer(adapter);
1604 release_login_rsp_buffer(adapter);
1605}
1606
1607static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
1608{
1609 struct net_device *netdev = adapter->netdev;
1610 unsigned long timeout = msecs_to_jiffies(20000);
1611 union ibmvnic_crq crq;
1612 bool resend;
1613 int rc;
1614
1615 netdev_dbg(netdev, "setting link state %d\n", link_state);
1616
1617 memset(&crq, 0, sizeof(crq));
1618 crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
1619 crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
1620 crq.logical_link_state.link_state = link_state;
1621
1622 do {
1623 resend = false;
1624
1625 reinit_completion(&adapter->init_done);
1626 rc = ibmvnic_send_crq(adapter, &crq);
1627 if (rc) {
1628 netdev_err(netdev, "Failed to set link state\n");
1629 return rc;
1630 }
1631
1632 if (!wait_for_completion_timeout(&adapter->init_done,
1633 timeout)) {
1634 netdev_err(netdev, "timeout setting link state\n");
1635 return -ETIMEDOUT;
1636 }
1637
1638 if (adapter->init_done_rc == PARTIALSUCCESS) {
1639 /* Partuial success, delay and re-send */
1640 mdelay(1000);
1641 resend = true;
1642 } else if (adapter->init_done_rc) {
1643 netdev_warn(netdev, "Unable to set link state, rc=%d\n",
1644 adapter->init_done_rc);
1645 return adapter->init_done_rc;
1646 }
1647 } while (resend);
1648
1649 return 0;
1650}
1651
1652static int set_real_num_queues(struct net_device *netdev)
1653{
1654 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1655 int rc;
1656
1657 netdev_dbg(netdev, "Setting real tx/rx queues (%llx/%llx)\n",
1658 adapter->req_tx_queues, adapter->req_rx_queues);
1659
1660 rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
1661 if (rc) {
1662 netdev_err(netdev, "failed to set the number of tx queues\n");
1663 return rc;
1664 }
1665
1666 rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
1667 if (rc)
1668 netdev_err(netdev, "failed to set the number of rx queues\n");
1669
1670 return rc;
1671}
1672
1673static int ibmvnic_get_vpd(struct ibmvnic_adapter *adapter)
1674{
1675 struct device *dev = &adapter->vdev->dev;
1676 union ibmvnic_crq crq;
1677 int len = 0;
1678 int rc;
1679
1680 if (adapter->vpd->buff)
1681 len = adapter->vpd->len;
1682
1683 mutex_lock(&adapter->fw_lock);
1684 adapter->fw_done_rc = 0;
1685 reinit_completion(&adapter->fw_done);
1686
1687 crq.get_vpd_size.first = IBMVNIC_CRQ_CMD;
1688 crq.get_vpd_size.cmd = GET_VPD_SIZE;
1689 rc = ibmvnic_send_crq(adapter, &crq);
1690 if (rc) {
1691 mutex_unlock(&adapter->fw_lock);
1692 return rc;
1693 }
1694
1695 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1696 if (rc) {
1697 dev_err(dev, "Could not retrieve VPD size, rc = %d\n", rc);
1698 mutex_unlock(&adapter->fw_lock);
1699 return rc;
1700 }
1701 mutex_unlock(&adapter->fw_lock);
1702
1703 if (!adapter->vpd->len)
1704 return -ENODATA;
1705
1706 if (!adapter->vpd->buff)
1707 adapter->vpd->buff = kzalloc(adapter->vpd->len, GFP_KERNEL);
1708 else if (adapter->vpd->len != len)
1709 adapter->vpd->buff =
1710 krealloc(adapter->vpd->buff,
1711 adapter->vpd->len, GFP_KERNEL);
1712
1713 if (!adapter->vpd->buff) {
1714 dev_err(dev, "Could allocate VPD buffer\n");
1715 return -ENOMEM;
1716 }
1717
1718 adapter->vpd->dma_addr =
1719 dma_map_single(dev, adapter->vpd->buff, adapter->vpd->len,
1720 DMA_FROM_DEVICE);
1721 if (dma_mapping_error(dev, adapter->vpd->dma_addr)) {
1722 dev_err(dev, "Could not map VPD buffer\n");
1723 kfree(adapter->vpd->buff);
1724 adapter->vpd->buff = NULL;
1725 return -ENOMEM;
1726 }
1727
1728 mutex_lock(&adapter->fw_lock);
1729 adapter->fw_done_rc = 0;
1730 reinit_completion(&adapter->fw_done);
1731
1732 crq.get_vpd.first = IBMVNIC_CRQ_CMD;
1733 crq.get_vpd.cmd = GET_VPD;
1734 crq.get_vpd.ioba = cpu_to_be32(adapter->vpd->dma_addr);
1735 crq.get_vpd.len = cpu_to_be32((u32)adapter->vpd->len);
1736 rc = ibmvnic_send_crq(adapter, &crq);
1737 if (rc) {
1738 kfree(adapter->vpd->buff);
1739 adapter->vpd->buff = NULL;
1740 mutex_unlock(&adapter->fw_lock);
1741 return rc;
1742 }
1743
1744 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
1745 if (rc) {
1746 dev_err(dev, "Unable to retrieve VPD, rc = %d\n", rc);
1747 kfree(adapter->vpd->buff);
1748 adapter->vpd->buff = NULL;
1749 mutex_unlock(&adapter->fw_lock);
1750 return rc;
1751 }
1752
1753 mutex_unlock(&adapter->fw_lock);
1754 return 0;
1755}
1756
1757static int init_resources(struct ibmvnic_adapter *adapter)
1758{
1759 struct net_device *netdev = adapter->netdev;
1760 int rc;
1761
1762 rc = set_real_num_queues(netdev);
1763 if (rc)
1764 return rc;
1765
1766 adapter->vpd = kzalloc(sizeof(*adapter->vpd), GFP_KERNEL);
1767 if (!adapter->vpd)
1768 return -ENOMEM;
1769
1770 /* Vital Product Data (VPD) */
1771 rc = ibmvnic_get_vpd(adapter);
1772 if (rc) {
1773 netdev_err(netdev, "failed to initialize Vital Product Data (VPD)\n");
1774 return rc;
1775 }
1776
1777 rc = init_napi(adapter);
1778 if (rc)
1779 return rc;
1780
1781 send_query_map(adapter);
1782
1783 rc = init_rx_pools(netdev);
1784 if (rc)
1785 return rc;
1786
1787 rc = init_tx_pools(netdev);
1788 return rc;
1789}
1790
1791static int __ibmvnic_open(struct net_device *netdev)
1792{
1793 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1794 enum vnic_state prev_state = adapter->state;
1795 int i, rc;
1796
1797 adapter->state = VNIC_OPENING;
1798 replenish_pools(adapter);
1799 ibmvnic_napi_enable(adapter);
1800
1801 /* We're ready to receive frames, enable the sub-crq interrupts and
1802 * set the logical link state to up
1803 */
1804 for (i = 0; i < adapter->req_rx_queues; i++) {
1805 netdev_dbg(netdev, "Enabling rx_scrq[%d] irq\n", i);
1806 if (prev_state == VNIC_CLOSED)
1807 enable_irq(adapter->rx_scrq[i]->irq);
1808 enable_scrq_irq(adapter, adapter->rx_scrq[i]);
1809 }
1810
1811 for (i = 0; i < adapter->req_tx_queues; i++) {
1812 netdev_dbg(netdev, "Enabling tx_scrq[%d] irq\n", i);
1813 if (prev_state == VNIC_CLOSED)
1814 enable_irq(adapter->tx_scrq[i]->irq);
1815 enable_scrq_irq(adapter, adapter->tx_scrq[i]);
1816 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, i));
1817 }
1818
1819 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
1820 if (rc) {
1821 ibmvnic_napi_disable(adapter);
1822 ibmvnic_disable_irqs(adapter);
1823 return rc;
1824 }
1825
1826 adapter->tx_queues_active = true;
1827
1828 /* Since queues were stopped until now, there shouldn't be any
1829 * one in ibmvnic_complete_tx() or ibmvnic_xmit() so maybe we
1830 * don't need the synchronize_rcu()? Leaving it for consistency
1831 * with setting ->tx_queues_active = false.
1832 */
1833 synchronize_rcu();
1834
1835 netif_tx_start_all_queues(netdev);
1836
1837 if (prev_state == VNIC_CLOSED) {
1838 for (i = 0; i < adapter->req_rx_queues; i++)
1839 napi_schedule(&adapter->napi[i]);
1840 }
1841
1842 adapter->state = VNIC_OPEN;
1843 return rc;
1844}
1845
1846static int ibmvnic_open(struct net_device *netdev)
1847{
1848 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
1849 int rc;
1850
1851 ASSERT_RTNL();
1852
1853 /* If device failover is pending or we are about to reset, just set
1854 * device state and return. Device operation will be handled by reset
1855 * routine.
1856 *
1857 * It should be safe to overwrite the adapter->state here. Since
1858 * we hold the rtnl, either the reset has not actually started or
1859 * the rtnl got dropped during the set_link_state() in do_reset().
1860 * In the former case, no one else is changing the state (again we
1861 * have the rtnl) and in the latter case, do_reset() will detect and
1862 * honor our setting below.
1863 */
1864 if (adapter->failover_pending || (test_bit(0, &adapter->resetting))) {
1865 netdev_dbg(netdev, "[S:%s FOP:%d] Resetting, deferring open\n",
1866 adapter_state_to_string(adapter->state),
1867 adapter->failover_pending);
1868 adapter->state = VNIC_OPEN;
1869 rc = 0;
1870 goto out;
1871 }
1872
1873 if (adapter->state != VNIC_CLOSED) {
1874 rc = ibmvnic_login(netdev);
1875 if (rc)
1876 goto out;
1877
1878 rc = init_resources(adapter);
1879 if (rc) {
1880 netdev_err(netdev, "failed to initialize resources\n");
1881 goto out;
1882 }
1883 }
1884
1885 rc = __ibmvnic_open(netdev);
1886
1887out:
1888 /* If open failed and there is a pending failover or in-progress reset,
1889 * set device state and return. Device operation will be handled by
1890 * reset routine. See also comments above regarding rtnl.
1891 */
1892 if (rc &&
1893 (adapter->failover_pending || (test_bit(0, &adapter->resetting)))) {
1894 adapter->state = VNIC_OPEN;
1895 rc = 0;
1896 }
1897
1898 if (rc) {
1899 release_resources(adapter);
1900 release_rx_pools(adapter);
1901 release_tx_pools(adapter);
1902 }
1903
1904 return rc;
1905}
1906
1907static void clean_rx_pools(struct ibmvnic_adapter *adapter)
1908{
1909 struct ibmvnic_rx_pool *rx_pool;
1910 struct ibmvnic_rx_buff *rx_buff;
1911 u64 rx_entries;
1912 int rx_scrqs;
1913 int i, j;
1914
1915 if (!adapter->rx_pool)
1916 return;
1917
1918 rx_scrqs = adapter->num_active_rx_pools;
1919 rx_entries = adapter->req_rx_add_entries_per_subcrq;
1920
1921 /* Free any remaining skbs in the rx buffer pools */
1922 for (i = 0; i < rx_scrqs; i++) {
1923 rx_pool = &adapter->rx_pool[i];
1924 if (!rx_pool || !rx_pool->rx_buff)
1925 continue;
1926
1927 netdev_dbg(adapter->netdev, "Cleaning rx_pool[%d]\n", i);
1928 for (j = 0; j < rx_entries; j++) {
1929 rx_buff = &rx_pool->rx_buff[j];
1930 if (rx_buff && rx_buff->skb) {
1931 dev_kfree_skb_any(rx_buff->skb);
1932 rx_buff->skb = NULL;
1933 }
1934 }
1935 }
1936}
1937
1938static void clean_one_tx_pool(struct ibmvnic_adapter *adapter,
1939 struct ibmvnic_tx_pool *tx_pool)
1940{
1941 struct ibmvnic_tx_buff *tx_buff;
1942 u64 tx_entries;
1943 int i;
1944
1945 if (!tx_pool || !tx_pool->tx_buff)
1946 return;
1947
1948 tx_entries = tx_pool->num_buffers;
1949
1950 for (i = 0; i < tx_entries; i++) {
1951 tx_buff = &tx_pool->tx_buff[i];
1952 if (tx_buff && tx_buff->skb) {
1953 dev_kfree_skb_any(tx_buff->skb);
1954 tx_buff->skb = NULL;
1955 }
1956 }
1957}
1958
1959static void clean_tx_pools(struct ibmvnic_adapter *adapter)
1960{
1961 int tx_scrqs;
1962 int i;
1963
1964 if (!adapter->tx_pool || !adapter->tso_pool)
1965 return;
1966
1967 tx_scrqs = adapter->num_active_tx_pools;
1968
1969 /* Free any remaining skbs in the tx buffer pools */
1970 for (i = 0; i < tx_scrqs; i++) {
1971 netdev_dbg(adapter->netdev, "Cleaning tx_pool[%d]\n", i);
1972 clean_one_tx_pool(adapter, &adapter->tx_pool[i]);
1973 clean_one_tx_pool(adapter, &adapter->tso_pool[i]);
1974 }
1975}
1976
1977static void ibmvnic_disable_irqs(struct ibmvnic_adapter *adapter)
1978{
1979 struct net_device *netdev = adapter->netdev;
1980 int i;
1981
1982 if (adapter->tx_scrq) {
1983 for (i = 0; i < adapter->req_tx_queues; i++)
1984 if (adapter->tx_scrq[i]->irq) {
1985 netdev_dbg(netdev,
1986 "Disabling tx_scrq[%d] irq\n", i);
1987 disable_scrq_irq(adapter, adapter->tx_scrq[i]);
1988 disable_irq(adapter->tx_scrq[i]->irq);
1989 }
1990 }
1991
1992 if (adapter->rx_scrq) {
1993 for (i = 0; i < adapter->req_rx_queues; i++) {
1994 if (adapter->rx_scrq[i]->irq) {
1995 netdev_dbg(netdev,
1996 "Disabling rx_scrq[%d] irq\n", i);
1997 disable_scrq_irq(adapter, adapter->rx_scrq[i]);
1998 disable_irq(adapter->rx_scrq[i]->irq);
1999 }
2000 }
2001 }
2002}
2003
2004static void ibmvnic_cleanup(struct net_device *netdev)
2005{
2006 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2007
2008 /* ensure that transmissions are stopped if called by do_reset */
2009
2010 adapter->tx_queues_active = false;
2011
2012 /* Ensure complete_tx() and ibmvnic_xmit() see ->tx_queues_active
2013 * update so they don't restart a queue after we stop it below.
2014 */
2015 synchronize_rcu();
2016
2017 if (test_bit(0, &adapter->resetting))
2018 netif_tx_disable(netdev);
2019 else
2020 netif_tx_stop_all_queues(netdev);
2021
2022 ibmvnic_napi_disable(adapter);
2023 ibmvnic_disable_irqs(adapter);
2024}
2025
2026static int __ibmvnic_close(struct net_device *netdev)
2027{
2028 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2029 int rc = 0;
2030
2031 adapter->state = VNIC_CLOSING;
2032 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2033 adapter->state = VNIC_CLOSED;
2034 return rc;
2035}
2036
2037static int ibmvnic_close(struct net_device *netdev)
2038{
2039 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2040 int rc;
2041
2042 netdev_dbg(netdev, "[S:%s FOP:%d FRR:%d] Closing\n",
2043 adapter_state_to_string(adapter->state),
2044 adapter->failover_pending,
2045 adapter->force_reset_recovery);
2046
2047 /* If device failover is pending, just set device state and return.
2048 * Device operation will be handled by reset routine.
2049 */
2050 if (adapter->failover_pending) {
2051 adapter->state = VNIC_CLOSED;
2052 return 0;
2053 }
2054
2055 rc = __ibmvnic_close(netdev);
2056 ibmvnic_cleanup(netdev);
2057 clean_rx_pools(adapter);
2058 clean_tx_pools(adapter);
2059
2060 return rc;
2061}
2062
2063/**
2064 * build_hdr_data - creates L2/L3/L4 header data buffer
2065 * @hdr_field: bitfield determining needed headers
2066 * @skb: socket buffer
2067 * @hdr_len: array of header lengths
2068 * @hdr_data: buffer to write the header to
2069 *
2070 * Reads hdr_field to determine which headers are needed by firmware.
2071 * Builds a buffer containing these headers. Saves individual header
2072 * lengths and total buffer length to be used to build descriptors.
2073 */
2074static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
2075 int *hdr_len, u8 *hdr_data)
2076{
2077 int len = 0;
2078 u8 *hdr;
2079
2080 if (skb_vlan_tagged(skb) && !skb_vlan_tag_present(skb))
2081 hdr_len[0] = sizeof(struct vlan_ethhdr);
2082 else
2083 hdr_len[0] = sizeof(struct ethhdr);
2084
2085 if (skb->protocol == htons(ETH_P_IP)) {
2086 hdr_len[1] = ip_hdr(skb)->ihl * 4;
2087 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2088 hdr_len[2] = tcp_hdrlen(skb);
2089 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
2090 hdr_len[2] = sizeof(struct udphdr);
2091 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2092 hdr_len[1] = sizeof(struct ipv6hdr);
2093 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2094 hdr_len[2] = tcp_hdrlen(skb);
2095 else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
2096 hdr_len[2] = sizeof(struct udphdr);
2097 } else if (skb->protocol == htons(ETH_P_ARP)) {
2098 hdr_len[1] = arp_hdr_len(skb->dev);
2099 hdr_len[2] = 0;
2100 }
2101
2102 memset(hdr_data, 0, 120);
2103 if ((hdr_field >> 6) & 1) {
2104 hdr = skb_mac_header(skb);
2105 memcpy(hdr_data, hdr, hdr_len[0]);
2106 len += hdr_len[0];
2107 }
2108
2109 if ((hdr_field >> 5) & 1) {
2110 hdr = skb_network_header(skb);
2111 memcpy(hdr_data + len, hdr, hdr_len[1]);
2112 len += hdr_len[1];
2113 }
2114
2115 if ((hdr_field >> 4) & 1) {
2116 hdr = skb_transport_header(skb);
2117 memcpy(hdr_data + len, hdr, hdr_len[2]);
2118 len += hdr_len[2];
2119 }
2120 return len;
2121}
2122
2123/**
2124 * create_hdr_descs - create header and header extension descriptors
2125 * @hdr_field: bitfield determining needed headers
2126 * @hdr_data: buffer containing header data
2127 * @len: length of data buffer
2128 * @hdr_len: array of individual header lengths
2129 * @scrq_arr: descriptor array
2130 *
2131 * Creates header and, if needed, header extension descriptors and
2132 * places them in a descriptor array, scrq_arr
2133 */
2134
2135static int create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
2136 union sub_crq *scrq_arr)
2137{
2138 union sub_crq hdr_desc;
2139 int tmp_len = len;
2140 int num_descs = 0;
2141 u8 *data, *cur;
2142 int tmp;
2143
2144 while (tmp_len > 0) {
2145 cur = hdr_data + len - tmp_len;
2146
2147 memset(&hdr_desc, 0, sizeof(hdr_desc));
2148 if (cur != hdr_data) {
2149 data = hdr_desc.hdr_ext.data;
2150 tmp = tmp_len > 29 ? 29 : tmp_len;
2151 hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
2152 hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
2153 hdr_desc.hdr_ext.len = tmp;
2154 } else {
2155 data = hdr_desc.hdr.data;
2156 tmp = tmp_len > 24 ? 24 : tmp_len;
2157 hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
2158 hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
2159 hdr_desc.hdr.len = tmp;
2160 hdr_desc.hdr.l2_len = (u8)hdr_len[0];
2161 hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
2162 hdr_desc.hdr.l4_len = (u8)hdr_len[2];
2163 hdr_desc.hdr.flag = hdr_field << 1;
2164 }
2165 memcpy(data, cur, tmp);
2166 tmp_len -= tmp;
2167 *scrq_arr = hdr_desc;
2168 scrq_arr++;
2169 num_descs++;
2170 }
2171
2172 return num_descs;
2173}
2174
2175/**
2176 * build_hdr_descs_arr - build a header descriptor array
2177 * @skb: tx socket buffer
2178 * @indir_arr: indirect array
2179 * @num_entries: number of descriptors to be sent
2180 * @hdr_field: bit field determining which headers will be sent
2181 *
2182 * This function will build a TX descriptor array with applicable
2183 * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
2184 */
2185
2186static void build_hdr_descs_arr(struct sk_buff *skb,
2187 union sub_crq *indir_arr,
2188 int *num_entries, u8 hdr_field)
2189{
2190 int hdr_len[3] = {0, 0, 0};
2191 u8 hdr_data[140] = {0};
2192 int tot_len;
2193
2194 tot_len = build_hdr_data(hdr_field, skb, hdr_len,
2195 hdr_data);
2196 *num_entries += create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
2197 indir_arr + 1);
2198}
2199
2200static int ibmvnic_xmit_workarounds(struct sk_buff *skb,
2201 struct net_device *netdev)
2202{
2203 /* For some backing devices, mishandling of small packets
2204 * can result in a loss of connection or TX stall. Device
2205 * architects recommend that no packet should be smaller
2206 * than the minimum MTU value provided to the driver, so
2207 * pad any packets to that length
2208 */
2209 if (skb->len < netdev->min_mtu)
2210 return skb_put_padto(skb, netdev->min_mtu);
2211
2212 return 0;
2213}
2214
2215static void ibmvnic_tx_scrq_clean_buffer(struct ibmvnic_adapter *adapter,
2216 struct ibmvnic_sub_crq_queue *tx_scrq)
2217{
2218 struct ibmvnic_ind_xmit_queue *ind_bufp;
2219 struct ibmvnic_tx_buff *tx_buff;
2220 struct ibmvnic_tx_pool *tx_pool;
2221 union sub_crq tx_scrq_entry;
2222 int queue_num;
2223 int entries;
2224 int index;
2225 int i;
2226
2227 ind_bufp = &tx_scrq->ind_buf;
2228 entries = (u64)ind_bufp->index;
2229 queue_num = tx_scrq->pool_index;
2230
2231 for (i = entries - 1; i >= 0; --i) {
2232 tx_scrq_entry = ind_bufp->indir_arr[i];
2233 if (tx_scrq_entry.v1.type != IBMVNIC_TX_DESC)
2234 continue;
2235 index = be32_to_cpu(tx_scrq_entry.v1.correlator);
2236 if (index & IBMVNIC_TSO_POOL_MASK) {
2237 tx_pool = &adapter->tso_pool[queue_num];
2238 index &= ~IBMVNIC_TSO_POOL_MASK;
2239 } else {
2240 tx_pool = &adapter->tx_pool[queue_num];
2241 }
2242 tx_pool->free_map[tx_pool->consumer_index] = index;
2243 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
2244 tx_pool->num_buffers - 1 :
2245 tx_pool->consumer_index - 1;
2246 tx_buff = &tx_pool->tx_buff[index];
2247 adapter->netdev->stats.tx_packets--;
2248 adapter->netdev->stats.tx_bytes -= tx_buff->skb->len;
2249 adapter->tx_stats_buffers[queue_num].packets--;
2250 adapter->tx_stats_buffers[queue_num].bytes -=
2251 tx_buff->skb->len;
2252 dev_kfree_skb_any(tx_buff->skb);
2253 tx_buff->skb = NULL;
2254 adapter->netdev->stats.tx_dropped++;
2255 }
2256
2257 ind_bufp->index = 0;
2258
2259 if (atomic_sub_return(entries, &tx_scrq->used) <=
2260 (adapter->req_tx_entries_per_subcrq / 2) &&
2261 __netif_subqueue_stopped(adapter->netdev, queue_num)) {
2262 rcu_read_lock();
2263
2264 if (adapter->tx_queues_active) {
2265 netif_wake_subqueue(adapter->netdev, queue_num);
2266 netdev_dbg(adapter->netdev, "Started queue %d\n",
2267 queue_num);
2268 }
2269
2270 rcu_read_unlock();
2271 }
2272}
2273
2274static int ibmvnic_tx_scrq_flush(struct ibmvnic_adapter *adapter,
2275 struct ibmvnic_sub_crq_queue *tx_scrq)
2276{
2277 struct ibmvnic_ind_xmit_queue *ind_bufp;
2278 u64 dma_addr;
2279 u64 entries;
2280 u64 handle;
2281 int rc;
2282
2283 ind_bufp = &tx_scrq->ind_buf;
2284 dma_addr = (u64)ind_bufp->indir_dma;
2285 entries = (u64)ind_bufp->index;
2286 handle = tx_scrq->handle;
2287
2288 if (!entries)
2289 return 0;
2290 rc = send_subcrq_indirect(adapter, handle, dma_addr, entries);
2291 if (rc)
2292 ibmvnic_tx_scrq_clean_buffer(adapter, tx_scrq);
2293 else
2294 ind_bufp->index = 0;
2295 return 0;
2296}
2297
2298static netdev_tx_t ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
2299{
2300 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2301 int queue_num = skb_get_queue_mapping(skb);
2302 u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
2303 struct device *dev = &adapter->vdev->dev;
2304 struct ibmvnic_ind_xmit_queue *ind_bufp;
2305 struct ibmvnic_tx_buff *tx_buff = NULL;
2306 struct ibmvnic_sub_crq_queue *tx_scrq;
2307 struct ibmvnic_long_term_buff *ltb;
2308 struct ibmvnic_tx_pool *tx_pool;
2309 unsigned int tx_send_failed = 0;
2310 netdev_tx_t ret = NETDEV_TX_OK;
2311 unsigned int tx_map_failed = 0;
2312 union sub_crq indir_arr[16];
2313 unsigned int tx_dropped = 0;
2314 unsigned int tx_packets = 0;
2315 unsigned int tx_bytes = 0;
2316 dma_addr_t data_dma_addr;
2317 struct netdev_queue *txq;
2318 unsigned long lpar_rc;
2319 union sub_crq tx_crq;
2320 unsigned int offset;
2321 int num_entries = 1;
2322 unsigned char *dst;
2323 int bufidx = 0;
2324 u8 proto = 0;
2325
2326 /* If a reset is in progress, drop the packet since
2327 * the scrqs may get torn down. Otherwise use the
2328 * rcu to ensure reset waits for us to complete.
2329 */
2330 rcu_read_lock();
2331 if (!adapter->tx_queues_active) {
2332 dev_kfree_skb_any(skb);
2333
2334 tx_send_failed++;
2335 tx_dropped++;
2336 ret = NETDEV_TX_OK;
2337 goto out;
2338 }
2339
2340 tx_scrq = adapter->tx_scrq[queue_num];
2341 txq = netdev_get_tx_queue(netdev, queue_num);
2342 ind_bufp = &tx_scrq->ind_buf;
2343
2344 if (ibmvnic_xmit_workarounds(skb, netdev)) {
2345 tx_dropped++;
2346 tx_send_failed++;
2347 ret = NETDEV_TX_OK;
2348 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2349 goto out;
2350 }
2351
2352 if (skb_is_gso(skb))
2353 tx_pool = &adapter->tso_pool[queue_num];
2354 else
2355 tx_pool = &adapter->tx_pool[queue_num];
2356
2357 bufidx = tx_pool->free_map[tx_pool->consumer_index];
2358
2359 if (bufidx == IBMVNIC_INVALID_MAP) {
2360 dev_kfree_skb_any(skb);
2361 tx_send_failed++;
2362 tx_dropped++;
2363 ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2364 ret = NETDEV_TX_OK;
2365 goto out;
2366 }
2367
2368 tx_pool->free_map[tx_pool->consumer_index] = IBMVNIC_INVALID_MAP;
2369
2370 map_txpool_buf_to_ltb(tx_pool, bufidx, <b, &offset);
2371
2372 dst = ltb->buff + offset;
2373 memset(dst, 0, tx_pool->buf_size);
2374 data_dma_addr = ltb->addr + offset;
2375
2376 if (skb_shinfo(skb)->nr_frags) {
2377 int cur, i;
2378
2379 /* Copy the head */
2380 skb_copy_from_linear_data(skb, dst, skb_headlen(skb));
2381 cur = skb_headlen(skb);
2382
2383 /* Copy the frags */
2384 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2385 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2386
2387 memcpy(dst + cur, skb_frag_address(frag),
2388 skb_frag_size(frag));
2389 cur += skb_frag_size(frag);
2390 }
2391 } else {
2392 skb_copy_from_linear_data(skb, dst, skb->len);
2393 }
2394
2395 /* post changes to long_term_buff *dst before VIOS accessing it */
2396 dma_wmb();
2397
2398 tx_pool->consumer_index =
2399 (tx_pool->consumer_index + 1) % tx_pool->num_buffers;
2400
2401 tx_buff = &tx_pool->tx_buff[bufidx];
2402 tx_buff->skb = skb;
2403 tx_buff->index = bufidx;
2404 tx_buff->pool_index = queue_num;
2405
2406 memset(&tx_crq, 0, sizeof(tx_crq));
2407 tx_crq.v1.first = IBMVNIC_CRQ_CMD;
2408 tx_crq.v1.type = IBMVNIC_TX_DESC;
2409 tx_crq.v1.n_crq_elem = 1;
2410 tx_crq.v1.n_sge = 1;
2411 tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
2412
2413 if (skb_is_gso(skb))
2414 tx_crq.v1.correlator =
2415 cpu_to_be32(bufidx | IBMVNIC_TSO_POOL_MASK);
2416 else
2417 tx_crq.v1.correlator = cpu_to_be32(bufidx);
2418 tx_crq.v1.dma_reg = cpu_to_be16(ltb->map_id);
2419 tx_crq.v1.sge_len = cpu_to_be32(skb->len);
2420 tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
2421
2422 if (adapter->vlan_header_insertion && skb_vlan_tag_present(skb)) {
2423 tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
2424 tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
2425 }
2426
2427 if (skb->protocol == htons(ETH_P_IP)) {
2428 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
2429 proto = ip_hdr(skb)->protocol;
2430 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2431 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
2432 proto = ipv6_hdr(skb)->nexthdr;
2433 }
2434
2435 if (proto == IPPROTO_TCP)
2436 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
2437 else if (proto == IPPROTO_UDP)
2438 tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
2439
2440 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2441 tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
2442 hdrs += 2;
2443 }
2444 if (skb_is_gso(skb)) {
2445 tx_crq.v1.flags1 |= IBMVNIC_TX_LSO;
2446 tx_crq.v1.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
2447 hdrs += 2;
2448 }
2449
2450 if ((*hdrs >> 7) & 1)
2451 build_hdr_descs_arr(skb, indir_arr, &num_entries, *hdrs);
2452
2453 tx_crq.v1.n_crq_elem = num_entries;
2454 tx_buff->num_entries = num_entries;
2455 /* flush buffer if current entry can not fit */
2456 if (num_entries + ind_bufp->index > IBMVNIC_MAX_IND_DESCS) {
2457 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2458 if (lpar_rc != H_SUCCESS)
2459 goto tx_flush_err;
2460 }
2461
2462 indir_arr[0] = tx_crq;
2463 memcpy(&ind_bufp->indir_arr[ind_bufp->index], &indir_arr[0],
2464 num_entries * sizeof(struct ibmvnic_generic_scrq));
2465 ind_bufp->index += num_entries;
2466 if (__netdev_tx_sent_queue(txq, skb->len,
2467 netdev_xmit_more() &&
2468 ind_bufp->index < IBMVNIC_MAX_IND_DESCS)) {
2469 lpar_rc = ibmvnic_tx_scrq_flush(adapter, tx_scrq);
2470 if (lpar_rc != H_SUCCESS)
2471 goto tx_err;
2472 }
2473
2474 if (atomic_add_return(num_entries, &tx_scrq->used)
2475 >= adapter->req_tx_entries_per_subcrq) {
2476 netdev_dbg(netdev, "Stopping queue %d\n", queue_num);
2477 netif_stop_subqueue(netdev, queue_num);
2478 }
2479
2480 tx_packets++;
2481 tx_bytes += skb->len;
2482 txq_trans_cond_update(txq);
2483 ret = NETDEV_TX_OK;
2484 goto out;
2485
2486tx_flush_err:
2487 dev_kfree_skb_any(skb);
2488 tx_buff->skb = NULL;
2489 tx_pool->consumer_index = tx_pool->consumer_index == 0 ?
2490 tx_pool->num_buffers - 1 :
2491 tx_pool->consumer_index - 1;
2492 tx_dropped++;
2493tx_err:
2494 if (lpar_rc != H_CLOSED && lpar_rc != H_PARAMETER)
2495 dev_err_ratelimited(dev, "tx: send failed\n");
2496
2497 if (lpar_rc == H_CLOSED || adapter->failover_pending) {
2498 /* Disable TX and report carrier off if queue is closed
2499 * or pending failover.
2500 * Firmware guarantees that a signal will be sent to the
2501 * driver, triggering a reset or some other action.
2502 */
2503 netif_tx_stop_all_queues(netdev);
2504 netif_carrier_off(netdev);
2505 }
2506out:
2507 rcu_read_unlock();
2508 netdev->stats.tx_dropped += tx_dropped;
2509 netdev->stats.tx_bytes += tx_bytes;
2510 netdev->stats.tx_packets += tx_packets;
2511 adapter->tx_send_failed += tx_send_failed;
2512 adapter->tx_map_failed += tx_map_failed;
2513 adapter->tx_stats_buffers[queue_num].packets += tx_packets;
2514 adapter->tx_stats_buffers[queue_num].bytes += tx_bytes;
2515 adapter->tx_stats_buffers[queue_num].dropped_packets += tx_dropped;
2516
2517 return ret;
2518}
2519
2520static void ibmvnic_set_multi(struct net_device *netdev)
2521{
2522 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2523 struct netdev_hw_addr *ha;
2524 union ibmvnic_crq crq;
2525
2526 memset(&crq, 0, sizeof(crq));
2527 crq.request_capability.first = IBMVNIC_CRQ_CMD;
2528 crq.request_capability.cmd = REQUEST_CAPABILITY;
2529
2530 if (netdev->flags & IFF_PROMISC) {
2531 if (!adapter->promisc_supported)
2532 return;
2533 } else {
2534 if (netdev->flags & IFF_ALLMULTI) {
2535 /* Accept all multicast */
2536 memset(&crq, 0, sizeof(crq));
2537 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2538 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2539 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
2540 ibmvnic_send_crq(adapter, &crq);
2541 } else if (netdev_mc_empty(netdev)) {
2542 /* Reject all multicast */
2543 memset(&crq, 0, sizeof(crq));
2544 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2545 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2546 crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
2547 ibmvnic_send_crq(adapter, &crq);
2548 } else {
2549 /* Accept one or more multicast(s) */
2550 netdev_for_each_mc_addr(ha, netdev) {
2551 memset(&crq, 0, sizeof(crq));
2552 crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
2553 crq.multicast_ctrl.cmd = MULTICAST_CTRL;
2554 crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
2555 ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
2556 ha->addr);
2557 ibmvnic_send_crq(adapter, &crq);
2558 }
2559 }
2560 }
2561}
2562
2563static int __ibmvnic_set_mac(struct net_device *netdev, u8 *dev_addr)
2564{
2565 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2566 union ibmvnic_crq crq;
2567 int rc;
2568
2569 if (!is_valid_ether_addr(dev_addr)) {
2570 rc = -EADDRNOTAVAIL;
2571 goto err;
2572 }
2573
2574 memset(&crq, 0, sizeof(crq));
2575 crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
2576 crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
2577 ether_addr_copy(&crq.change_mac_addr.mac_addr[0], dev_addr);
2578
2579 mutex_lock(&adapter->fw_lock);
2580 adapter->fw_done_rc = 0;
2581 reinit_completion(&adapter->fw_done);
2582
2583 rc = ibmvnic_send_crq(adapter, &crq);
2584 if (rc) {
2585 rc = -EIO;
2586 mutex_unlock(&adapter->fw_lock);
2587 goto err;
2588 }
2589
2590 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
2591 /* netdev->dev_addr is changed in handle_change_mac_rsp function */
2592 if (rc || adapter->fw_done_rc) {
2593 rc = -EIO;
2594 mutex_unlock(&adapter->fw_lock);
2595 goto err;
2596 }
2597 mutex_unlock(&adapter->fw_lock);
2598 return 0;
2599err:
2600 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2601 return rc;
2602}
2603
2604static int ibmvnic_set_mac(struct net_device *netdev, void *p)
2605{
2606 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
2607 struct sockaddr *addr = p;
2608 int rc;
2609
2610 rc = 0;
2611 if (!is_valid_ether_addr(addr->sa_data))
2612 return -EADDRNOTAVAIL;
2613
2614 ether_addr_copy(adapter->mac_addr, addr->sa_data);
2615 if (adapter->state != VNIC_PROBED)
2616 rc = __ibmvnic_set_mac(netdev, addr->sa_data);
2617
2618 return rc;
2619}
2620
2621static const char *reset_reason_to_string(enum ibmvnic_reset_reason reason)
2622{
2623 switch (reason) {
2624 case VNIC_RESET_FAILOVER:
2625 return "FAILOVER";
2626 case VNIC_RESET_MOBILITY:
2627 return "MOBILITY";
2628 case VNIC_RESET_FATAL:
2629 return "FATAL";
2630 case VNIC_RESET_NON_FATAL:
2631 return "NON_FATAL";
2632 case VNIC_RESET_TIMEOUT:
2633 return "TIMEOUT";
2634 case VNIC_RESET_CHANGE_PARAM:
2635 return "CHANGE_PARAM";
2636 case VNIC_RESET_PASSIVE_INIT:
2637 return "PASSIVE_INIT";
2638 }
2639 return "UNKNOWN";
2640}
2641
2642/*
2643 * Initialize the init_done completion and return code values. We
2644 * can get a transport event just after registering the CRQ and the
2645 * tasklet will use this to communicate the transport event. To ensure
2646 * we don't miss the notification/error, initialize these _before_
2647 * regisering the CRQ.
2648 */
2649static inline void reinit_init_done(struct ibmvnic_adapter *adapter)
2650{
2651 reinit_completion(&adapter->init_done);
2652 adapter->init_done_rc = 0;
2653}
2654
2655/*
2656 * do_reset returns zero if we are able to keep processing reset events, or
2657 * non-zero if we hit a fatal error and must halt.
2658 */
2659static int do_reset(struct ibmvnic_adapter *adapter,
2660 struct ibmvnic_rwi *rwi, u32 reset_state)
2661{
2662 struct net_device *netdev = adapter->netdev;
2663 u64 old_num_rx_queues, old_num_tx_queues;
2664 u64 old_num_rx_slots, old_num_tx_slots;
2665 int rc;
2666
2667 netdev_dbg(adapter->netdev,
2668 "[S:%s FOP:%d] Reset reason: %s, reset_state: %s\n",
2669 adapter_state_to_string(adapter->state),
2670 adapter->failover_pending,
2671 reset_reason_to_string(rwi->reset_reason),
2672 adapter_state_to_string(reset_state));
2673
2674 adapter->reset_reason = rwi->reset_reason;
2675 /* requestor of VNIC_RESET_CHANGE_PARAM already has the rtnl lock */
2676 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2677 rtnl_lock();
2678
2679 /* Now that we have the rtnl lock, clear any pending failover.
2680 * This will ensure ibmvnic_open() has either completed or will
2681 * block until failover is complete.
2682 */
2683 if (rwi->reset_reason == VNIC_RESET_FAILOVER)
2684 adapter->failover_pending = false;
2685
2686 /* read the state and check (again) after getting rtnl */
2687 reset_state = adapter->state;
2688
2689 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2690 rc = -EBUSY;
2691 goto out;
2692 }
2693
2694 netif_carrier_off(netdev);
2695
2696 old_num_rx_queues = adapter->req_rx_queues;
2697 old_num_tx_queues = adapter->req_tx_queues;
2698 old_num_rx_slots = adapter->req_rx_add_entries_per_subcrq;
2699 old_num_tx_slots = adapter->req_tx_entries_per_subcrq;
2700
2701 ibmvnic_cleanup(netdev);
2702
2703 if (reset_state == VNIC_OPEN &&
2704 adapter->reset_reason != VNIC_RESET_MOBILITY &&
2705 adapter->reset_reason != VNIC_RESET_FAILOVER) {
2706 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2707 rc = __ibmvnic_close(netdev);
2708 if (rc)
2709 goto out;
2710 } else {
2711 adapter->state = VNIC_CLOSING;
2712
2713 /* Release the RTNL lock before link state change and
2714 * re-acquire after the link state change to allow
2715 * linkwatch_event to grab the RTNL lock and run during
2716 * a reset.
2717 */
2718 rtnl_unlock();
2719 rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
2720 rtnl_lock();
2721 if (rc)
2722 goto out;
2723
2724 if (adapter->state == VNIC_OPEN) {
2725 /* When we dropped rtnl, ibmvnic_open() got
2726 * it and noticed that we are resetting and
2727 * set the adapter state to OPEN. Update our
2728 * new "target" state, and resume the reset
2729 * from VNIC_CLOSING state.
2730 */
2731 netdev_dbg(netdev,
2732 "Open changed state from %s, updating.\n",
2733 adapter_state_to_string(reset_state));
2734 reset_state = VNIC_OPEN;
2735 adapter->state = VNIC_CLOSING;
2736 }
2737
2738 if (adapter->state != VNIC_CLOSING) {
2739 /* If someone else changed the adapter state
2740 * when we dropped the rtnl, fail the reset
2741 */
2742 rc = -EAGAIN;
2743 goto out;
2744 }
2745 adapter->state = VNIC_CLOSED;
2746 }
2747 }
2748
2749 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2750 release_resources(adapter);
2751 release_sub_crqs(adapter, 1);
2752 release_crq_queue(adapter);
2753 }
2754
2755 if (adapter->reset_reason != VNIC_RESET_NON_FATAL) {
2756 /* remove the closed state so when we call open it appears
2757 * we are coming from the probed state.
2758 */
2759 adapter->state = VNIC_PROBED;
2760
2761 reinit_init_done(adapter);
2762
2763 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2764 rc = init_crq_queue(adapter);
2765 } else if (adapter->reset_reason == VNIC_RESET_MOBILITY) {
2766 rc = ibmvnic_reenable_crq_queue(adapter);
2767 release_sub_crqs(adapter, 1);
2768 } else {
2769 rc = ibmvnic_reset_crq(adapter);
2770 if (rc == H_CLOSED || rc == H_SUCCESS) {
2771 rc = vio_enable_interrupts(adapter->vdev);
2772 if (rc)
2773 netdev_err(adapter->netdev,
2774 "Reset failed to enable interrupts. rc=%d\n",
2775 rc);
2776 }
2777 }
2778
2779 if (rc) {
2780 netdev_err(adapter->netdev,
2781 "Reset couldn't initialize crq. rc=%d\n", rc);
2782 goto out;
2783 }
2784
2785 rc = ibmvnic_reset_init(adapter, true);
2786 if (rc)
2787 goto out;
2788
2789 /* If the adapter was in PROBE or DOWN state prior to the reset,
2790 * exit here.
2791 */
2792 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN) {
2793 rc = 0;
2794 goto out;
2795 }
2796
2797 rc = ibmvnic_login(netdev);
2798 if (rc)
2799 goto out;
2800
2801 if (adapter->reset_reason == VNIC_RESET_CHANGE_PARAM) {
2802 rc = init_resources(adapter);
2803 if (rc)
2804 goto out;
2805 } else if (adapter->req_rx_queues != old_num_rx_queues ||
2806 adapter->req_tx_queues != old_num_tx_queues ||
2807 adapter->req_rx_add_entries_per_subcrq !=
2808 old_num_rx_slots ||
2809 adapter->req_tx_entries_per_subcrq !=
2810 old_num_tx_slots ||
2811 !adapter->rx_pool ||
2812 !adapter->tso_pool ||
2813 !adapter->tx_pool) {
2814 release_napi(adapter);
2815 release_vpd_data(adapter);
2816
2817 rc = init_resources(adapter);
2818 if (rc)
2819 goto out;
2820
2821 } else {
2822 rc = init_tx_pools(netdev);
2823 if (rc) {
2824 netdev_dbg(netdev,
2825 "init tx pools failed (%d)\n",
2826 rc);
2827 goto out;
2828 }
2829
2830 rc = init_rx_pools(netdev);
2831 if (rc) {
2832 netdev_dbg(netdev,
2833 "init rx pools failed (%d)\n",
2834 rc);
2835 goto out;
2836 }
2837 }
2838 ibmvnic_disable_irqs(adapter);
2839 }
2840 adapter->state = VNIC_CLOSED;
2841
2842 if (reset_state == VNIC_CLOSED) {
2843 rc = 0;
2844 goto out;
2845 }
2846
2847 rc = __ibmvnic_open(netdev);
2848 if (rc) {
2849 rc = IBMVNIC_OPEN_FAILED;
2850 goto out;
2851 }
2852
2853 /* refresh device's multicast list */
2854 ibmvnic_set_multi(netdev);
2855
2856 if (adapter->reset_reason == VNIC_RESET_FAILOVER ||
2857 adapter->reset_reason == VNIC_RESET_MOBILITY)
2858 __netdev_notify_peers(netdev);
2859
2860 rc = 0;
2861
2862out:
2863 /* restore the adapter state if reset failed */
2864 if (rc)
2865 adapter->state = reset_state;
2866 /* requestor of VNIC_RESET_CHANGE_PARAM should still hold the rtnl lock */
2867 if (!(adapter->reset_reason == VNIC_RESET_CHANGE_PARAM))
2868 rtnl_unlock();
2869
2870 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Reset done, rc %d\n",
2871 adapter_state_to_string(adapter->state),
2872 adapter->failover_pending, rc);
2873 return rc;
2874}
2875
2876static int do_hard_reset(struct ibmvnic_adapter *adapter,
2877 struct ibmvnic_rwi *rwi, u32 reset_state)
2878{
2879 struct net_device *netdev = adapter->netdev;
2880 int rc;
2881
2882 netdev_dbg(adapter->netdev, "Hard resetting driver (%s)\n",
2883 reset_reason_to_string(rwi->reset_reason));
2884
2885 /* read the state and check (again) after getting rtnl */
2886 reset_state = adapter->state;
2887
2888 if (reset_state == VNIC_REMOVING || reset_state == VNIC_REMOVED) {
2889 rc = -EBUSY;
2890 goto out;
2891 }
2892
2893 netif_carrier_off(netdev);
2894 adapter->reset_reason = rwi->reset_reason;
2895
2896 ibmvnic_cleanup(netdev);
2897 release_resources(adapter);
2898 release_sub_crqs(adapter, 0);
2899 release_crq_queue(adapter);
2900
2901 /* remove the closed state so when we call open it appears
2902 * we are coming from the probed state.
2903 */
2904 adapter->state = VNIC_PROBED;
2905
2906 reinit_init_done(adapter);
2907
2908 rc = init_crq_queue(adapter);
2909 if (rc) {
2910 netdev_err(adapter->netdev,
2911 "Couldn't initialize crq. rc=%d\n", rc);
2912 goto out;
2913 }
2914
2915 rc = ibmvnic_reset_init(adapter, false);
2916 if (rc)
2917 goto out;
2918
2919 /* If the adapter was in PROBE or DOWN state prior to the reset,
2920 * exit here.
2921 */
2922 if (reset_state == VNIC_PROBED || reset_state == VNIC_DOWN)
2923 goto out;
2924
2925 rc = ibmvnic_login(netdev);
2926 if (rc)
2927 goto out;
2928
2929 rc = init_resources(adapter);
2930 if (rc)
2931 goto out;
2932
2933 ibmvnic_disable_irqs(adapter);
2934 adapter->state = VNIC_CLOSED;
2935
2936 if (reset_state == VNIC_CLOSED)
2937 goto out;
2938
2939 rc = __ibmvnic_open(netdev);
2940 if (rc) {
2941 rc = IBMVNIC_OPEN_FAILED;
2942 goto out;
2943 }
2944
2945 __netdev_notify_peers(netdev);
2946out:
2947 /* restore adapter state if reset failed */
2948 if (rc)
2949 adapter->state = reset_state;
2950 netdev_dbg(adapter->netdev, "[S:%s FOP:%d] Hard reset done, rc %d\n",
2951 adapter_state_to_string(adapter->state),
2952 adapter->failover_pending, rc);
2953 return rc;
2954}
2955
2956static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
2957{
2958 struct ibmvnic_rwi *rwi;
2959 unsigned long flags;
2960
2961 spin_lock_irqsave(&adapter->rwi_lock, flags);
2962
2963 if (!list_empty(&adapter->rwi_list)) {
2964 rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
2965 list);
2966 list_del(&rwi->list);
2967 } else {
2968 rwi = NULL;
2969 }
2970
2971 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
2972 return rwi;
2973}
2974
2975/**
2976 * do_passive_init - complete probing when partner device is detected.
2977 * @adapter: ibmvnic_adapter struct
2978 *
2979 * If the ibmvnic device does not have a partner device to communicate with at boot
2980 * and that partner device comes online at a later time, this function is called
2981 * to complete the initialization process of ibmvnic device.
2982 * Caller is expected to hold rtnl_lock().
2983 *
2984 * Returns non-zero if sub-CRQs are not initialized properly leaving the device
2985 * in the down state.
2986 * Returns 0 upon success and the device is in PROBED state.
2987 */
2988
2989static int do_passive_init(struct ibmvnic_adapter *adapter)
2990{
2991 unsigned long timeout = msecs_to_jiffies(30000);
2992 struct net_device *netdev = adapter->netdev;
2993 struct device *dev = &adapter->vdev->dev;
2994 int rc;
2995
2996 netdev_dbg(netdev, "Partner device found, probing.\n");
2997
2998 adapter->state = VNIC_PROBING;
2999 reinit_completion(&adapter->init_done);
3000 adapter->init_done_rc = 0;
3001 adapter->crq.active = true;
3002
3003 rc = send_crq_init_complete(adapter);
3004 if (rc)
3005 goto out;
3006
3007 rc = send_version_xchg(adapter);
3008 if (rc)
3009 netdev_dbg(adapter->netdev, "send_version_xchg failed, rc=%d\n", rc);
3010
3011 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
3012 dev_err(dev, "Initialization sequence timed out\n");
3013 rc = -ETIMEDOUT;
3014 goto out;
3015 }
3016
3017 rc = init_sub_crqs(adapter);
3018 if (rc) {
3019 dev_err(dev, "Initialization of sub crqs failed, rc=%d\n", rc);
3020 goto out;
3021 }
3022
3023 rc = init_sub_crq_irqs(adapter);
3024 if (rc) {
3025 dev_err(dev, "Failed to initialize sub crq irqs\n, rc=%d", rc);
3026 goto init_failed;
3027 }
3028
3029 netdev->mtu = adapter->req_mtu - ETH_HLEN;
3030 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
3031 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
3032
3033 adapter->state = VNIC_PROBED;
3034 netdev_dbg(netdev, "Probed successfully. Waiting for signal from partner device.\n");
3035
3036 return 0;
3037
3038init_failed:
3039 release_sub_crqs(adapter, 1);
3040out:
3041 adapter->state = VNIC_DOWN;
3042 return rc;
3043}
3044
3045static void __ibmvnic_reset(struct work_struct *work)
3046{
3047 struct ibmvnic_adapter *adapter;
3048 unsigned int timeout = 5000;
3049 struct ibmvnic_rwi *tmprwi;
3050 bool saved_state = false;
3051 struct ibmvnic_rwi *rwi;
3052 unsigned long flags;
3053 struct device *dev;
3054 bool need_reset;
3055 int num_fails = 0;
3056 u32 reset_state;
3057 int rc = 0;
3058
3059 adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
3060 dev = &adapter->vdev->dev;
3061
3062 /* Wait for ibmvnic_probe() to complete. If probe is taking too long
3063 * or if another reset is in progress, defer work for now. If probe
3064 * eventually fails it will flush and terminate our work.
3065 *
3066 * Three possibilities here:
3067 * 1. Adpater being removed - just return
3068 * 2. Timed out on probe or another reset in progress - delay the work
3069 * 3. Completed probe - perform any resets in queue
3070 */
3071 if (adapter->state == VNIC_PROBING &&
3072 !wait_for_completion_timeout(&adapter->probe_done, timeout)) {
3073 dev_err(dev, "Reset thread timed out on probe");
3074 queue_delayed_work(system_long_wq,
3075 &adapter->ibmvnic_delayed_reset,
3076 IBMVNIC_RESET_DELAY);
3077 return;
3078 }
3079
3080 /* adapter is done with probe (i.e state is never VNIC_PROBING now) */
3081 if (adapter->state == VNIC_REMOVING)
3082 return;
3083
3084 /* ->rwi_list is stable now (no one else is removing entries) */
3085
3086 /* ibmvnic_probe() may have purged the reset queue after we were
3087 * scheduled to process a reset so there maybe no resets to process.
3088 * Before setting the ->resetting bit though, we have to make sure
3089 * that there is infact a reset to process. Otherwise we may race
3090 * with ibmvnic_open() and end up leaving the vnic down:
3091 *
3092 * __ibmvnic_reset() ibmvnic_open()
3093 * ----------------- --------------
3094 *
3095 * set ->resetting bit
3096 * find ->resetting bit is set
3097 * set ->state to IBMVNIC_OPEN (i.e
3098 * assume reset will open device)
3099 * return
3100 * find reset queue empty
3101 * return
3102 *
3103 * Neither performed vnic login/open and vnic stays down
3104 *
3105 * If we hold the lock and conditionally set the bit, either we
3106 * or ibmvnic_open() will complete the open.
3107 */
3108 need_reset = false;
3109 spin_lock(&adapter->rwi_lock);
3110 if (!list_empty(&adapter->rwi_list)) {
3111 if (test_and_set_bit_lock(0, &adapter->resetting)) {
3112 queue_delayed_work(system_long_wq,
3113 &adapter->ibmvnic_delayed_reset,
3114 IBMVNIC_RESET_DELAY);
3115 } else {
3116 need_reset = true;
3117 }
3118 }
3119 spin_unlock(&adapter->rwi_lock);
3120
3121 if (!need_reset)
3122 return;
3123
3124 rwi = get_next_rwi(adapter);
3125 while (rwi) {
3126 spin_lock_irqsave(&adapter->state_lock, flags);
3127
3128 if (adapter->state == VNIC_REMOVING ||
3129 adapter->state == VNIC_REMOVED) {
3130 spin_unlock_irqrestore(&adapter->state_lock, flags);
3131 kfree(rwi);
3132 rc = EBUSY;
3133 break;
3134 }
3135
3136 if (!saved_state) {
3137 reset_state = adapter->state;
3138 saved_state = true;
3139 }
3140 spin_unlock_irqrestore(&adapter->state_lock, flags);
3141
3142 if (rwi->reset_reason == VNIC_RESET_PASSIVE_INIT) {
3143 rtnl_lock();
3144 rc = do_passive_init(adapter);
3145 rtnl_unlock();
3146 if (!rc)
3147 netif_carrier_on(adapter->netdev);
3148 } else if (adapter->force_reset_recovery) {
3149 /* Since we are doing a hard reset now, clear the
3150 * failover_pending flag so we don't ignore any
3151 * future MOBILITY or other resets.
3152 */
3153 adapter->failover_pending = false;
3154
3155 /* Transport event occurred during previous reset */
3156 if (adapter->wait_for_reset) {
3157 /* Previous was CHANGE_PARAM; caller locked */
3158 adapter->force_reset_recovery = false;
3159 rc = do_hard_reset(adapter, rwi, reset_state);
3160 } else {
3161 rtnl_lock();
3162 adapter->force_reset_recovery = false;
3163 rc = do_hard_reset(adapter, rwi, reset_state);
3164 rtnl_unlock();
3165 }
3166 if (rc)
3167 num_fails++;
3168 else
3169 num_fails = 0;
3170
3171 /* If auto-priority-failover is enabled we can get
3172 * back to back failovers during resets, resulting
3173 * in at least two failed resets (from high-priority
3174 * backing device to low-priority one and then back)
3175 * If resets continue to fail beyond that, give the
3176 * adapter some time to settle down before retrying.
3177 */
3178 if (num_fails >= 3) {
3179 netdev_dbg(adapter->netdev,
3180 "[S:%s] Hard reset failed %d times, waiting 60 secs\n",
3181 adapter_state_to_string(adapter->state),
3182 num_fails);
3183 set_current_state(TASK_UNINTERRUPTIBLE);
3184 schedule_timeout(60 * HZ);
3185 }
3186 } else {
3187 rc = do_reset(adapter, rwi, reset_state);
3188 }
3189 tmprwi = rwi;
3190 adapter->last_reset_time = jiffies;
3191
3192 if (rc)
3193 netdev_dbg(adapter->netdev, "Reset failed, rc=%d\n", rc);
3194
3195 rwi = get_next_rwi(adapter);
3196
3197 /*
3198 * If there are no resets queued and the previous reset failed,
3199 * the adapter would be in an undefined state. So retry the
3200 * previous reset as a hard reset.
3201 *
3202 * Else, free the previous rwi and, if there is another reset
3203 * queued, process the new reset even if previous reset failed
3204 * (the previous reset could have failed because of a fail
3205 * over for instance, so process the fail over).
3206 */
3207 if (!rwi && rc)
3208 rwi = tmprwi;
3209 else
3210 kfree(tmprwi);
3211
3212 if (rwi && (rwi->reset_reason == VNIC_RESET_FAILOVER ||
3213 rwi->reset_reason == VNIC_RESET_MOBILITY || rc))
3214 adapter->force_reset_recovery = true;
3215 }
3216
3217 if (adapter->wait_for_reset) {
3218 adapter->reset_done_rc = rc;
3219 complete(&adapter->reset_done);
3220 }
3221
3222 clear_bit_unlock(0, &adapter->resetting);
3223
3224 netdev_dbg(adapter->netdev,
3225 "[S:%s FRR:%d WFR:%d] Done processing resets\n",
3226 adapter_state_to_string(adapter->state),
3227 adapter->force_reset_recovery,
3228 adapter->wait_for_reset);
3229}
3230
3231static void __ibmvnic_delayed_reset(struct work_struct *work)
3232{
3233 struct ibmvnic_adapter *adapter;
3234
3235 adapter = container_of(work, struct ibmvnic_adapter,
3236 ibmvnic_delayed_reset.work);
3237 __ibmvnic_reset(&adapter->ibmvnic_reset);
3238}
3239
3240static void flush_reset_queue(struct ibmvnic_adapter *adapter)
3241{
3242 struct list_head *entry, *tmp_entry;
3243
3244 if (!list_empty(&adapter->rwi_list)) {
3245 list_for_each_safe(entry, tmp_entry, &adapter->rwi_list) {
3246 list_del(entry);
3247 kfree(list_entry(entry, struct ibmvnic_rwi, list));
3248 }
3249 }
3250}
3251
3252static int ibmvnic_reset(struct ibmvnic_adapter *adapter,
3253 enum ibmvnic_reset_reason reason)
3254{
3255 struct net_device *netdev = adapter->netdev;
3256 struct ibmvnic_rwi *rwi, *tmp;
3257 unsigned long flags;
3258 int ret;
3259
3260 spin_lock_irqsave(&adapter->rwi_lock, flags);
3261
3262 /* If failover is pending don't schedule any other reset.
3263 * Instead let the failover complete. If there is already a
3264 * a failover reset scheduled, we will detect and drop the
3265 * duplicate reset when walking the ->rwi_list below.
3266 */
3267 if (adapter->state == VNIC_REMOVING ||
3268 adapter->state == VNIC_REMOVED ||
3269 (adapter->failover_pending && reason != VNIC_RESET_FAILOVER)) {
3270 ret = EBUSY;
3271 netdev_dbg(netdev, "Adapter removing or pending failover, skipping reset\n");
3272 goto err;
3273 }
3274
3275 list_for_each_entry(tmp, &adapter->rwi_list, list) {
3276 if (tmp->reset_reason == reason) {
3277 netdev_dbg(netdev, "Skipping matching reset, reason=%s\n",
3278 reset_reason_to_string(reason));
3279 ret = EBUSY;
3280 goto err;
3281 }
3282 }
3283
3284 rwi = kzalloc(sizeof(*rwi), GFP_ATOMIC);
3285 if (!rwi) {
3286 ret = ENOMEM;
3287 goto err;
3288 }
3289 /* if we just received a transport event,
3290 * flush reset queue and process this reset
3291 */
3292 if (adapter->force_reset_recovery)
3293 flush_reset_queue(adapter);
3294
3295 rwi->reset_reason = reason;
3296 list_add_tail(&rwi->list, &adapter->rwi_list);
3297 netdev_dbg(adapter->netdev, "Scheduling reset (reason %s)\n",
3298 reset_reason_to_string(reason));
3299 queue_work(system_long_wq, &adapter->ibmvnic_reset);
3300
3301 ret = 0;
3302err:
3303 /* ibmvnic_close() below can block, so drop the lock first */
3304 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
3305
3306 if (ret == ENOMEM)
3307 ibmvnic_close(netdev);
3308
3309 return -ret;
3310}
3311
3312static void ibmvnic_tx_timeout(struct net_device *dev, unsigned int txqueue)
3313{
3314 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3315
3316 if (test_bit(0, &adapter->resetting)) {
3317 netdev_err(adapter->netdev,
3318 "Adapter is resetting, skip timeout reset\n");
3319 return;
3320 }
3321 /* No queuing up reset until at least 5 seconds (default watchdog val)
3322 * after last reset
3323 */
3324 if (time_before(jiffies, (adapter->last_reset_time + dev->watchdog_timeo))) {
3325 netdev_dbg(dev, "Not yet time to tx timeout.\n");
3326 return;
3327 }
3328 ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
3329}
3330
3331static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
3332 struct ibmvnic_rx_buff *rx_buff)
3333{
3334 struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
3335
3336 rx_buff->skb = NULL;
3337
3338 pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
3339 pool->next_alloc = (pool->next_alloc + 1) % pool->size;
3340
3341 atomic_dec(&pool->available);
3342}
3343
3344static int ibmvnic_poll(struct napi_struct *napi, int budget)
3345{
3346 struct ibmvnic_sub_crq_queue *rx_scrq;
3347 struct ibmvnic_adapter *adapter;
3348 struct net_device *netdev;
3349 int frames_processed;
3350 int scrq_num;
3351
3352 netdev = napi->dev;
3353 adapter = netdev_priv(netdev);
3354 scrq_num = (int)(napi - adapter->napi);
3355 frames_processed = 0;
3356 rx_scrq = adapter->rx_scrq[scrq_num];
3357
3358restart_poll:
3359 while (frames_processed < budget) {
3360 struct sk_buff *skb;
3361 struct ibmvnic_rx_buff *rx_buff;
3362 union sub_crq *next;
3363 u32 length;
3364 u16 offset;
3365 u8 flags = 0;
3366
3367 if (unlikely(test_bit(0, &adapter->resetting) &&
3368 adapter->reset_reason != VNIC_RESET_NON_FATAL)) {
3369 enable_scrq_irq(adapter, rx_scrq);
3370 napi_complete_done(napi, frames_processed);
3371 return frames_processed;
3372 }
3373
3374 if (!pending_scrq(adapter, rx_scrq))
3375 break;
3376 next = ibmvnic_next_scrq(adapter, rx_scrq);
3377 rx_buff = (struct ibmvnic_rx_buff *)
3378 be64_to_cpu(next->rx_comp.correlator);
3379 /* do error checking */
3380 if (next->rx_comp.rc) {
3381 netdev_dbg(netdev, "rx buffer returned with rc %x\n",
3382 be16_to_cpu(next->rx_comp.rc));
3383 /* free the entry */
3384 next->rx_comp.first = 0;
3385 dev_kfree_skb_any(rx_buff->skb);
3386 remove_buff_from_pool(adapter, rx_buff);
3387 continue;
3388 } else if (!rx_buff->skb) {
3389 /* free the entry */
3390 next->rx_comp.first = 0;
3391 remove_buff_from_pool(adapter, rx_buff);
3392 continue;
3393 }
3394
3395 length = be32_to_cpu(next->rx_comp.len);
3396 offset = be16_to_cpu(next->rx_comp.off_frame_data);
3397 flags = next->rx_comp.flags;
3398 skb = rx_buff->skb;
3399 /* load long_term_buff before copying to skb */
3400 dma_rmb();
3401 skb_copy_to_linear_data(skb, rx_buff->data + offset,
3402 length);
3403
3404 /* VLAN Header has been stripped by the system firmware and
3405 * needs to be inserted by the driver
3406 */
3407 if (adapter->rx_vlan_header_insertion &&
3408 (flags & IBMVNIC_VLAN_STRIPPED))
3409 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
3410 ntohs(next->rx_comp.vlan_tci));
3411
3412 /* free the entry */
3413 next->rx_comp.first = 0;
3414 remove_buff_from_pool(adapter, rx_buff);
3415
3416 skb_put(skb, length);
3417 skb->protocol = eth_type_trans(skb, netdev);
3418 skb_record_rx_queue(skb, scrq_num);
3419
3420 if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
3421 flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
3422 skb->ip_summed = CHECKSUM_UNNECESSARY;
3423 }
3424
3425 length = skb->len;
3426 napi_gro_receive(napi, skb); /* send it up */
3427 netdev->stats.rx_packets++;
3428 netdev->stats.rx_bytes += length;
3429 adapter->rx_stats_buffers[scrq_num].packets++;
3430 adapter->rx_stats_buffers[scrq_num].bytes += length;
3431 frames_processed++;
3432 }
3433
3434 if (adapter->state != VNIC_CLOSING &&
3435 ((atomic_read(&adapter->rx_pool[scrq_num].available) <
3436 adapter->req_rx_add_entries_per_subcrq / 2) ||
3437 frames_processed < budget))
3438 replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
3439 if (frames_processed < budget) {
3440 if (napi_complete_done(napi, frames_processed)) {
3441 enable_scrq_irq(adapter, rx_scrq);
3442 if (pending_scrq(adapter, rx_scrq)) {
3443 if (napi_reschedule(napi)) {
3444 disable_scrq_irq(adapter, rx_scrq);
3445 goto restart_poll;
3446 }
3447 }
3448 }
3449 }
3450 return frames_processed;
3451}
3452
3453static int wait_for_reset(struct ibmvnic_adapter *adapter)
3454{
3455 int rc, ret;
3456
3457 adapter->fallback.mtu = adapter->req_mtu;
3458 adapter->fallback.rx_queues = adapter->req_rx_queues;
3459 adapter->fallback.tx_queues = adapter->req_tx_queues;
3460 adapter->fallback.rx_entries = adapter->req_rx_add_entries_per_subcrq;
3461 adapter->fallback.tx_entries = adapter->req_tx_entries_per_subcrq;
3462
3463 reinit_completion(&adapter->reset_done);
3464 adapter->wait_for_reset = true;
3465 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
3466
3467 if (rc) {
3468 ret = rc;
3469 goto out;
3470 }
3471 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done, 60000);
3472 if (rc) {
3473 ret = -ENODEV;
3474 goto out;
3475 }
3476
3477 ret = 0;
3478 if (adapter->reset_done_rc) {
3479 ret = -EIO;
3480 adapter->desired.mtu = adapter->fallback.mtu;
3481 adapter->desired.rx_queues = adapter->fallback.rx_queues;
3482 adapter->desired.tx_queues = adapter->fallback.tx_queues;
3483 adapter->desired.rx_entries = adapter->fallback.rx_entries;
3484 adapter->desired.tx_entries = adapter->fallback.tx_entries;
3485
3486 reinit_completion(&adapter->reset_done);
3487 adapter->wait_for_reset = true;
3488 rc = ibmvnic_reset(adapter, VNIC_RESET_CHANGE_PARAM);
3489 if (rc) {
3490 ret = rc;
3491 goto out;
3492 }
3493 rc = ibmvnic_wait_for_completion(adapter, &adapter->reset_done,
3494 60000);
3495 if (rc) {
3496 ret = -ENODEV;
3497 goto out;
3498 }
3499 }
3500out:
3501 adapter->wait_for_reset = false;
3502
3503 return ret;
3504}
3505
3506static int ibmvnic_change_mtu(struct net_device *netdev, int new_mtu)
3507{
3508 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3509
3510 adapter->desired.mtu = new_mtu + ETH_HLEN;
3511
3512 return wait_for_reset(adapter);
3513}
3514
3515static netdev_features_t ibmvnic_features_check(struct sk_buff *skb,
3516 struct net_device *dev,
3517 netdev_features_t features)
3518{
3519 /* Some backing hardware adapters can not
3520 * handle packets with a MSS less than 224
3521 * or with only one segment.
3522 */
3523 if (skb_is_gso(skb)) {
3524 if (skb_shinfo(skb)->gso_size < 224 ||
3525 skb_shinfo(skb)->gso_segs == 1)
3526 features &= ~NETIF_F_GSO_MASK;
3527 }
3528
3529 return features;
3530}
3531
3532static const struct net_device_ops ibmvnic_netdev_ops = {
3533 .ndo_open = ibmvnic_open,
3534 .ndo_stop = ibmvnic_close,
3535 .ndo_start_xmit = ibmvnic_xmit,
3536 .ndo_set_rx_mode = ibmvnic_set_multi,
3537 .ndo_set_mac_address = ibmvnic_set_mac,
3538 .ndo_validate_addr = eth_validate_addr,
3539 .ndo_tx_timeout = ibmvnic_tx_timeout,
3540 .ndo_change_mtu = ibmvnic_change_mtu,
3541 .ndo_features_check = ibmvnic_features_check,
3542};
3543
3544/* ethtool functions */
3545
3546static int ibmvnic_get_link_ksettings(struct net_device *netdev,
3547 struct ethtool_link_ksettings *cmd)
3548{
3549 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3550 int rc;
3551
3552 rc = send_query_phys_parms(adapter);
3553 if (rc) {
3554 adapter->speed = SPEED_UNKNOWN;
3555 adapter->duplex = DUPLEX_UNKNOWN;
3556 }
3557 cmd->base.speed = adapter->speed;
3558 cmd->base.duplex = adapter->duplex;
3559 cmd->base.port = PORT_FIBRE;
3560 cmd->base.phy_address = 0;
3561 cmd->base.autoneg = AUTONEG_ENABLE;
3562
3563 return 0;
3564}
3565
3566static void ibmvnic_get_drvinfo(struct net_device *netdev,
3567 struct ethtool_drvinfo *info)
3568{
3569 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3570
3571 strscpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
3572 strscpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
3573 strscpy(info->fw_version, adapter->fw_version,
3574 sizeof(info->fw_version));
3575}
3576
3577static u32 ibmvnic_get_msglevel(struct net_device *netdev)
3578{
3579 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3580
3581 return adapter->msg_enable;
3582}
3583
3584static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
3585{
3586 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3587
3588 adapter->msg_enable = data;
3589}
3590
3591static u32 ibmvnic_get_link(struct net_device *netdev)
3592{
3593 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3594
3595 /* Don't need to send a query because we request a logical link up at
3596 * init and then we wait for link state indications
3597 */
3598 return adapter->logical_link_state;
3599}
3600
3601static void ibmvnic_get_ringparam(struct net_device *netdev,
3602 struct ethtool_ringparam *ring,
3603 struct kernel_ethtool_ringparam *kernel_ring,
3604 struct netlink_ext_ack *extack)
3605{
3606 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3607
3608 ring->rx_max_pending = adapter->max_rx_add_entries_per_subcrq;
3609 ring->tx_max_pending = adapter->max_tx_entries_per_subcrq;
3610 ring->rx_mini_max_pending = 0;
3611 ring->rx_jumbo_max_pending = 0;
3612 ring->rx_pending = adapter->req_rx_add_entries_per_subcrq;
3613 ring->tx_pending = adapter->req_tx_entries_per_subcrq;
3614 ring->rx_mini_pending = 0;
3615 ring->rx_jumbo_pending = 0;
3616}
3617
3618static int ibmvnic_set_ringparam(struct net_device *netdev,
3619 struct ethtool_ringparam *ring,
3620 struct kernel_ethtool_ringparam *kernel_ring,
3621 struct netlink_ext_ack *extack)
3622{
3623 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3624
3625 if (ring->rx_pending > adapter->max_rx_add_entries_per_subcrq ||
3626 ring->tx_pending > adapter->max_tx_entries_per_subcrq) {
3627 netdev_err(netdev, "Invalid request.\n");
3628 netdev_err(netdev, "Max tx buffers = %llu\n",
3629 adapter->max_rx_add_entries_per_subcrq);
3630 netdev_err(netdev, "Max rx buffers = %llu\n",
3631 adapter->max_tx_entries_per_subcrq);
3632 return -EINVAL;
3633 }
3634
3635 adapter->desired.rx_entries = ring->rx_pending;
3636 adapter->desired.tx_entries = ring->tx_pending;
3637
3638 return wait_for_reset(adapter);
3639}
3640
3641static void ibmvnic_get_channels(struct net_device *netdev,
3642 struct ethtool_channels *channels)
3643{
3644 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3645
3646 channels->max_rx = adapter->max_rx_queues;
3647 channels->max_tx = adapter->max_tx_queues;
3648 channels->max_other = 0;
3649 channels->max_combined = 0;
3650 channels->rx_count = adapter->req_rx_queues;
3651 channels->tx_count = adapter->req_tx_queues;
3652 channels->other_count = 0;
3653 channels->combined_count = 0;
3654}
3655
3656static int ibmvnic_set_channels(struct net_device *netdev,
3657 struct ethtool_channels *channels)
3658{
3659 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
3660
3661 adapter->desired.rx_queues = channels->rx_count;
3662 adapter->desired.tx_queues = channels->tx_count;
3663
3664 return wait_for_reset(adapter);
3665}
3666
3667static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3668{
3669 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3670 int i;
3671
3672 if (stringset != ETH_SS_STATS)
3673 return;
3674
3675 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
3676 memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
3677
3678 for (i = 0; i < adapter->req_tx_queues; i++) {
3679 snprintf(data, ETH_GSTRING_LEN, "tx%d_packets", i);
3680 data += ETH_GSTRING_LEN;
3681
3682 snprintf(data, ETH_GSTRING_LEN, "tx%d_bytes", i);
3683 data += ETH_GSTRING_LEN;
3684
3685 snprintf(data, ETH_GSTRING_LEN, "tx%d_dropped_packets", i);
3686 data += ETH_GSTRING_LEN;
3687 }
3688
3689 for (i = 0; i < adapter->req_rx_queues; i++) {
3690 snprintf(data, ETH_GSTRING_LEN, "rx%d_packets", i);
3691 data += ETH_GSTRING_LEN;
3692
3693 snprintf(data, ETH_GSTRING_LEN, "rx%d_bytes", i);
3694 data += ETH_GSTRING_LEN;
3695
3696 snprintf(data, ETH_GSTRING_LEN, "rx%d_interrupts", i);
3697 data += ETH_GSTRING_LEN;
3698 }
3699}
3700
3701static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
3702{
3703 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3704
3705 switch (sset) {
3706 case ETH_SS_STATS:
3707 return ARRAY_SIZE(ibmvnic_stats) +
3708 adapter->req_tx_queues * NUM_TX_STATS +
3709 adapter->req_rx_queues * NUM_RX_STATS;
3710 default:
3711 return -EOPNOTSUPP;
3712 }
3713}
3714
3715static void ibmvnic_get_ethtool_stats(struct net_device *dev,
3716 struct ethtool_stats *stats, u64 *data)
3717{
3718 struct ibmvnic_adapter *adapter = netdev_priv(dev);
3719 union ibmvnic_crq crq;
3720 int i, j;
3721 int rc;
3722
3723 memset(&crq, 0, sizeof(crq));
3724 crq.request_statistics.first = IBMVNIC_CRQ_CMD;
3725 crq.request_statistics.cmd = REQUEST_STATISTICS;
3726 crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
3727 crq.request_statistics.len =
3728 cpu_to_be32(sizeof(struct ibmvnic_statistics));
3729
3730 /* Wait for data to be written */
3731 reinit_completion(&adapter->stats_done);
3732 rc = ibmvnic_send_crq(adapter, &crq);
3733 if (rc)
3734 return;
3735 rc = ibmvnic_wait_for_completion(adapter, &adapter->stats_done, 10000);
3736 if (rc)
3737 return;
3738
3739 for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
3740 data[i] = be64_to_cpu(IBMVNIC_GET_STAT
3741 (adapter, ibmvnic_stats[i].offset));
3742
3743 for (j = 0; j < adapter->req_tx_queues; j++) {
3744 data[i] = adapter->tx_stats_buffers[j].packets;
3745 i++;
3746 data[i] = adapter->tx_stats_buffers[j].bytes;
3747 i++;
3748 data[i] = adapter->tx_stats_buffers[j].dropped_packets;
3749 i++;
3750 }
3751
3752 for (j = 0; j < adapter->req_rx_queues; j++) {
3753 data[i] = adapter->rx_stats_buffers[j].packets;
3754 i++;
3755 data[i] = adapter->rx_stats_buffers[j].bytes;
3756 i++;
3757 data[i] = adapter->rx_stats_buffers[j].interrupts;
3758 i++;
3759 }
3760}
3761
3762static const struct ethtool_ops ibmvnic_ethtool_ops = {
3763 .get_drvinfo = ibmvnic_get_drvinfo,
3764 .get_msglevel = ibmvnic_get_msglevel,
3765 .set_msglevel = ibmvnic_set_msglevel,
3766 .get_link = ibmvnic_get_link,
3767 .get_ringparam = ibmvnic_get_ringparam,
3768 .set_ringparam = ibmvnic_set_ringparam,
3769 .get_channels = ibmvnic_get_channels,
3770 .set_channels = ibmvnic_set_channels,
3771 .get_strings = ibmvnic_get_strings,
3772 .get_sset_count = ibmvnic_get_sset_count,
3773 .get_ethtool_stats = ibmvnic_get_ethtool_stats,
3774 .get_link_ksettings = ibmvnic_get_link_ksettings,
3775};
3776
3777/* Routines for managing CRQs/sCRQs */
3778
3779static int reset_one_sub_crq_queue(struct ibmvnic_adapter *adapter,
3780 struct ibmvnic_sub_crq_queue *scrq)
3781{
3782 int rc;
3783
3784 if (!scrq) {
3785 netdev_dbg(adapter->netdev, "Invalid scrq reset.\n");
3786 return -EINVAL;
3787 }
3788
3789 if (scrq->irq) {
3790 free_irq(scrq->irq, scrq);
3791 irq_dispose_mapping(scrq->irq);
3792 scrq->irq = 0;
3793 }
3794
3795 if (scrq->msgs) {
3796 memset(scrq->msgs, 0, 4 * PAGE_SIZE);
3797 atomic_set(&scrq->used, 0);
3798 scrq->cur = 0;
3799 scrq->ind_buf.index = 0;
3800 } else {
3801 netdev_dbg(adapter->netdev, "Invalid scrq reset\n");
3802 return -EINVAL;
3803 }
3804
3805 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3806 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3807 return rc;
3808}
3809
3810static int reset_sub_crq_queues(struct ibmvnic_adapter *adapter)
3811{
3812 int i, rc;
3813
3814 if (!adapter->tx_scrq || !adapter->rx_scrq)
3815 return -EINVAL;
3816
3817 ibmvnic_clean_affinity(adapter);
3818
3819 for (i = 0; i < adapter->req_tx_queues; i++) {
3820 netdev_dbg(adapter->netdev, "Re-setting tx_scrq[%d]\n", i);
3821 rc = reset_one_sub_crq_queue(adapter, adapter->tx_scrq[i]);
3822 if (rc)
3823 return rc;
3824 }
3825
3826 for (i = 0; i < adapter->req_rx_queues; i++) {
3827 netdev_dbg(adapter->netdev, "Re-setting rx_scrq[%d]\n", i);
3828 rc = reset_one_sub_crq_queue(adapter, adapter->rx_scrq[i]);
3829 if (rc)
3830 return rc;
3831 }
3832
3833 return rc;
3834}
3835
3836static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
3837 struct ibmvnic_sub_crq_queue *scrq,
3838 bool do_h_free)
3839{
3840 struct device *dev = &adapter->vdev->dev;
3841 long rc;
3842
3843 netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
3844
3845 if (do_h_free) {
3846 /* Close the sub-crqs */
3847 do {
3848 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3849 adapter->vdev->unit_address,
3850 scrq->crq_num);
3851 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3852
3853 if (rc) {
3854 netdev_err(adapter->netdev,
3855 "Failed to release sub-CRQ %16lx, rc = %ld\n",
3856 scrq->crq_num, rc);
3857 }
3858 }
3859
3860 dma_free_coherent(dev,
3861 IBMVNIC_IND_ARR_SZ,
3862 scrq->ind_buf.indir_arr,
3863 scrq->ind_buf.indir_dma);
3864
3865 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3866 DMA_BIDIRECTIONAL);
3867 free_pages((unsigned long)scrq->msgs, 2);
3868 free_cpumask_var(scrq->affinity_mask);
3869 kfree(scrq);
3870}
3871
3872static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
3873 *adapter)
3874{
3875 struct device *dev = &adapter->vdev->dev;
3876 struct ibmvnic_sub_crq_queue *scrq;
3877 int rc;
3878
3879 scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
3880 if (!scrq)
3881 return NULL;
3882
3883 scrq->msgs =
3884 (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
3885 if (!scrq->msgs) {
3886 dev_warn(dev, "Couldn't allocate crq queue messages page\n");
3887 goto zero_page_failed;
3888 }
3889 if (!zalloc_cpumask_var(&scrq->affinity_mask, GFP_KERNEL))
3890 goto cpumask_alloc_failed;
3891
3892 scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
3893 DMA_BIDIRECTIONAL);
3894 if (dma_mapping_error(dev, scrq->msg_token)) {
3895 dev_warn(dev, "Couldn't map crq queue messages page\n");
3896 goto map_failed;
3897 }
3898
3899 rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
3900 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
3901
3902 if (rc == H_RESOURCE)
3903 rc = ibmvnic_reset_crq(adapter);
3904
3905 if (rc == H_CLOSED) {
3906 dev_warn(dev, "Partner adapter not ready, waiting.\n");
3907 } else if (rc) {
3908 dev_warn(dev, "Error %d registering sub-crq\n", rc);
3909 goto reg_failed;
3910 }
3911
3912 scrq->adapter = adapter;
3913 scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
3914 scrq->ind_buf.index = 0;
3915
3916 scrq->ind_buf.indir_arr =
3917 dma_alloc_coherent(dev,
3918 IBMVNIC_IND_ARR_SZ,
3919 &scrq->ind_buf.indir_dma,
3920 GFP_KERNEL);
3921
3922 if (!scrq->ind_buf.indir_arr)
3923 goto indir_failed;
3924
3925 spin_lock_init(&scrq->lock);
3926
3927 netdev_dbg(adapter->netdev,
3928 "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
3929 scrq->crq_num, scrq->hw_irq, scrq->irq);
3930
3931 return scrq;
3932
3933indir_failed:
3934 do {
3935 rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
3936 adapter->vdev->unit_address,
3937 scrq->crq_num);
3938 } while (rc == H_BUSY || rc == H_IS_LONG_BUSY(rc));
3939reg_failed:
3940 dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
3941 DMA_BIDIRECTIONAL);
3942map_failed:
3943 free_cpumask_var(scrq->affinity_mask);
3944cpumask_alloc_failed:
3945 free_pages((unsigned long)scrq->msgs, 2);
3946zero_page_failed:
3947 kfree(scrq);
3948
3949 return NULL;
3950}
3951
3952static void release_sub_crqs(struct ibmvnic_adapter *adapter, bool do_h_free)
3953{
3954 int i;
3955
3956 ibmvnic_clean_affinity(adapter);
3957 if (adapter->tx_scrq) {
3958 for (i = 0; i < adapter->num_active_tx_scrqs; i++) {
3959 if (!adapter->tx_scrq[i])
3960 continue;
3961
3962 netdev_dbg(adapter->netdev, "Releasing tx_scrq[%d]\n",
3963 i);
3964 ibmvnic_tx_scrq_clean_buffer(adapter, adapter->tx_scrq[i]);
3965 if (adapter->tx_scrq[i]->irq) {
3966 free_irq(adapter->tx_scrq[i]->irq,
3967 adapter->tx_scrq[i]);
3968 irq_dispose_mapping(adapter->tx_scrq[i]->irq);
3969 adapter->tx_scrq[i]->irq = 0;
3970 }
3971
3972 release_sub_crq_queue(adapter, adapter->tx_scrq[i],
3973 do_h_free);
3974 }
3975
3976 kfree(adapter->tx_scrq);
3977 adapter->tx_scrq = NULL;
3978 adapter->num_active_tx_scrqs = 0;
3979 }
3980
3981 if (adapter->rx_scrq) {
3982 for (i = 0; i < adapter->num_active_rx_scrqs; i++) {
3983 if (!adapter->rx_scrq[i])
3984 continue;
3985
3986 netdev_dbg(adapter->netdev, "Releasing rx_scrq[%d]\n",
3987 i);
3988 if (adapter->rx_scrq[i]->irq) {
3989 free_irq(adapter->rx_scrq[i]->irq,
3990 adapter->rx_scrq[i]);
3991 irq_dispose_mapping(adapter->rx_scrq[i]->irq);
3992 adapter->rx_scrq[i]->irq = 0;
3993 }
3994
3995 release_sub_crq_queue(adapter, adapter->rx_scrq[i],
3996 do_h_free);
3997 }
3998
3999 kfree(adapter->rx_scrq);
4000 adapter->rx_scrq = NULL;
4001 adapter->num_active_rx_scrqs = 0;
4002 }
4003}
4004
4005static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
4006 struct ibmvnic_sub_crq_queue *scrq)
4007{
4008 struct device *dev = &adapter->vdev->dev;
4009 unsigned long rc;
4010
4011 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4012 H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
4013 if (rc)
4014 dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
4015 scrq->hw_irq, rc);
4016 return rc;
4017}
4018
4019/* We can not use the IRQ chip EOI handler because that has the
4020 * unintended effect of changing the interrupt priority.
4021 */
4022static void ibmvnic_xics_eoi(struct device *dev, struct ibmvnic_sub_crq_queue *scrq)
4023{
4024 u64 val = 0xff000000 | scrq->hw_irq;
4025 unsigned long rc;
4026
4027 rc = plpar_hcall_norets(H_EOI, val);
4028 if (rc)
4029 dev_err(dev, "H_EOI FAILED irq 0x%llx. rc=%ld\n", val, rc);
4030}
4031
4032/* Due to a firmware bug, the hypervisor can send an interrupt to a
4033 * transmit or receive queue just prior to a partition migration.
4034 * Force an EOI after migration.
4035 */
4036static void ibmvnic_clear_pending_interrupt(struct device *dev,
4037 struct ibmvnic_sub_crq_queue *scrq)
4038{
4039 if (!xive_enabled())
4040 ibmvnic_xics_eoi(dev, scrq);
4041}
4042
4043static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
4044 struct ibmvnic_sub_crq_queue *scrq)
4045{
4046 struct device *dev = &adapter->vdev->dev;
4047 unsigned long rc;
4048
4049 if (scrq->hw_irq > 0x100000000ULL) {
4050 dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
4051 return 1;
4052 }
4053
4054 if (test_bit(0, &adapter->resetting) &&
4055 adapter->reset_reason == VNIC_RESET_MOBILITY) {
4056 ibmvnic_clear_pending_interrupt(dev, scrq);
4057 }
4058
4059 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
4060 H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
4061 if (rc)
4062 dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
4063 scrq->hw_irq, rc);
4064 return rc;
4065}
4066
4067static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
4068 struct ibmvnic_sub_crq_queue *scrq)
4069{
4070 struct device *dev = &adapter->vdev->dev;
4071 struct ibmvnic_tx_pool *tx_pool;
4072 struct ibmvnic_tx_buff *txbuff;
4073 struct netdev_queue *txq;
4074 union sub_crq *next;
4075 int index;
4076 int i;
4077
4078restart_loop:
4079 while (pending_scrq(adapter, scrq)) {
4080 unsigned int pool = scrq->pool_index;
4081 int num_entries = 0;
4082 int total_bytes = 0;
4083 int num_packets = 0;
4084
4085 next = ibmvnic_next_scrq(adapter, scrq);
4086 for (i = 0; i < next->tx_comp.num_comps; i++) {
4087 index = be32_to_cpu(next->tx_comp.correlators[i]);
4088 if (index & IBMVNIC_TSO_POOL_MASK) {
4089 tx_pool = &adapter->tso_pool[pool];
4090 index &= ~IBMVNIC_TSO_POOL_MASK;
4091 } else {
4092 tx_pool = &adapter->tx_pool[pool];
4093 }
4094
4095 txbuff = &tx_pool->tx_buff[index];
4096 num_packets++;
4097 num_entries += txbuff->num_entries;
4098 if (txbuff->skb) {
4099 total_bytes += txbuff->skb->len;
4100 if (next->tx_comp.rcs[i]) {
4101 dev_err(dev, "tx error %x\n",
4102 next->tx_comp.rcs[i]);
4103 dev_kfree_skb_irq(txbuff->skb);
4104 } else {
4105 dev_consume_skb_irq(txbuff->skb);
4106 }
4107 txbuff->skb = NULL;
4108 } else {
4109 netdev_warn(adapter->netdev,
4110 "TX completion received with NULL socket buffer\n");
4111 }
4112 tx_pool->free_map[tx_pool->producer_index] = index;
4113 tx_pool->producer_index =
4114 (tx_pool->producer_index + 1) %
4115 tx_pool->num_buffers;
4116 }
4117 /* remove tx_comp scrq*/
4118 next->tx_comp.first = 0;
4119
4120 txq = netdev_get_tx_queue(adapter->netdev, scrq->pool_index);
4121 netdev_tx_completed_queue(txq, num_packets, total_bytes);
4122
4123 if (atomic_sub_return(num_entries, &scrq->used) <=
4124 (adapter->req_tx_entries_per_subcrq / 2) &&
4125 __netif_subqueue_stopped(adapter->netdev,
4126 scrq->pool_index)) {
4127 rcu_read_lock();
4128 if (adapter->tx_queues_active) {
4129 netif_wake_subqueue(adapter->netdev,
4130 scrq->pool_index);
4131 netdev_dbg(adapter->netdev,
4132 "Started queue %d\n",
4133 scrq->pool_index);
4134 }
4135 rcu_read_unlock();
4136 }
4137 }
4138
4139 enable_scrq_irq(adapter, scrq);
4140
4141 if (pending_scrq(adapter, scrq)) {
4142 disable_scrq_irq(adapter, scrq);
4143 goto restart_loop;
4144 }
4145
4146 return 0;
4147}
4148
4149static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
4150{
4151 struct ibmvnic_sub_crq_queue *scrq = instance;
4152 struct ibmvnic_adapter *adapter = scrq->adapter;
4153
4154 disable_scrq_irq(adapter, scrq);
4155 ibmvnic_complete_tx(adapter, scrq);
4156
4157 return IRQ_HANDLED;
4158}
4159
4160static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
4161{
4162 struct ibmvnic_sub_crq_queue *scrq = instance;
4163 struct ibmvnic_adapter *adapter = scrq->adapter;
4164
4165 /* When booting a kdump kernel we can hit pending interrupts
4166 * prior to completing driver initialization.
4167 */
4168 if (unlikely(adapter->state != VNIC_OPEN))
4169 return IRQ_NONE;
4170
4171 adapter->rx_stats_buffers[scrq->scrq_num].interrupts++;
4172
4173 if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
4174 disable_scrq_irq(adapter, scrq);
4175 __napi_schedule(&adapter->napi[scrq->scrq_num]);
4176 }
4177
4178 return IRQ_HANDLED;
4179}
4180
4181static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
4182{
4183 struct device *dev = &adapter->vdev->dev;
4184 struct ibmvnic_sub_crq_queue *scrq;
4185 int i = 0, j = 0;
4186 int rc = 0;
4187
4188 for (i = 0; i < adapter->req_tx_queues; i++) {
4189 netdev_dbg(adapter->netdev, "Initializing tx_scrq[%d] irq\n",
4190 i);
4191 scrq = adapter->tx_scrq[i];
4192 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
4193
4194 if (!scrq->irq) {
4195 rc = -EINVAL;
4196 dev_err(dev, "Error mapping irq\n");
4197 goto req_tx_irq_failed;
4198 }
4199
4200 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-tx%d",
4201 adapter->vdev->unit_address, i);
4202 rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
4203 0, scrq->name, scrq);
4204
4205 if (rc) {
4206 dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
4207 scrq->irq, rc);
4208 irq_dispose_mapping(scrq->irq);
4209 goto req_tx_irq_failed;
4210 }
4211 }
4212
4213 for (i = 0; i < adapter->req_rx_queues; i++) {
4214 netdev_dbg(adapter->netdev, "Initializing rx_scrq[%d] irq\n",
4215 i);
4216 scrq = adapter->rx_scrq[i];
4217 scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
4218 if (!scrq->irq) {
4219 rc = -EINVAL;
4220 dev_err(dev, "Error mapping irq\n");
4221 goto req_rx_irq_failed;
4222 }
4223 snprintf(scrq->name, sizeof(scrq->name), "ibmvnic-%x-rx%d",
4224 adapter->vdev->unit_address, i);
4225 rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
4226 0, scrq->name, scrq);
4227 if (rc) {
4228 dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
4229 scrq->irq, rc);
4230 irq_dispose_mapping(scrq->irq);
4231 goto req_rx_irq_failed;
4232 }
4233 }
4234
4235 cpus_read_lock();
4236 ibmvnic_set_affinity(adapter);
4237 cpus_read_unlock();
4238
4239 return rc;
4240
4241req_rx_irq_failed:
4242 for (j = 0; j < i; j++) {
4243 free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
4244 irq_dispose_mapping(adapter->rx_scrq[j]->irq);
4245 }
4246 i = adapter->req_tx_queues;
4247req_tx_irq_failed:
4248 for (j = 0; j < i; j++) {
4249 free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
4250 irq_dispose_mapping(adapter->tx_scrq[j]->irq);
4251 }
4252 release_sub_crqs(adapter, 1);
4253 return rc;
4254}
4255
4256static int init_sub_crqs(struct ibmvnic_adapter *adapter)
4257{
4258 struct device *dev = &adapter->vdev->dev;
4259 struct ibmvnic_sub_crq_queue **allqueues;
4260 int registered_queues = 0;
4261 int total_queues;
4262 int more = 0;
4263 int i;
4264
4265 total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
4266
4267 allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
4268 if (!allqueues)
4269 return -ENOMEM;
4270
4271 for (i = 0; i < total_queues; i++) {
4272 allqueues[i] = init_sub_crq_queue(adapter);
4273 if (!allqueues[i]) {
4274 dev_warn(dev, "Couldn't allocate all sub-crqs\n");
4275 break;
4276 }
4277 registered_queues++;
4278 }
4279
4280 /* Make sure we were able to register the minimum number of queues */
4281 if (registered_queues <
4282 adapter->min_tx_queues + adapter->min_rx_queues) {
4283 dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
4284 goto tx_failed;
4285 }
4286
4287 /* Distribute the failed allocated queues*/
4288 for (i = 0; i < total_queues - registered_queues + more ; i++) {
4289 netdev_dbg(adapter->netdev, "Reducing number of queues\n");
4290 switch (i % 3) {
4291 case 0:
4292 if (adapter->req_rx_queues > adapter->min_rx_queues)
4293 adapter->req_rx_queues--;
4294 else
4295 more++;
4296 break;
4297 case 1:
4298 if (adapter->req_tx_queues > adapter->min_tx_queues)
4299 adapter->req_tx_queues--;
4300 else
4301 more++;
4302 break;
4303 }
4304 }
4305
4306 adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
4307 sizeof(*adapter->tx_scrq), GFP_KERNEL);
4308 if (!adapter->tx_scrq)
4309 goto tx_failed;
4310
4311 for (i = 0; i < adapter->req_tx_queues; i++) {
4312 adapter->tx_scrq[i] = allqueues[i];
4313 adapter->tx_scrq[i]->pool_index = i;
4314 adapter->num_active_tx_scrqs++;
4315 }
4316
4317 adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
4318 sizeof(*adapter->rx_scrq), GFP_KERNEL);
4319 if (!adapter->rx_scrq)
4320 goto rx_failed;
4321
4322 for (i = 0; i < adapter->req_rx_queues; i++) {
4323 adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
4324 adapter->rx_scrq[i]->scrq_num = i;
4325 adapter->num_active_rx_scrqs++;
4326 }
4327
4328 kfree(allqueues);
4329 return 0;
4330
4331rx_failed:
4332 kfree(adapter->tx_scrq);
4333 adapter->tx_scrq = NULL;
4334tx_failed:
4335 for (i = 0; i < registered_queues; i++)
4336 release_sub_crq_queue(adapter, allqueues[i], 1);
4337 kfree(allqueues);
4338 return -ENOMEM;
4339}
4340
4341static void send_request_cap(struct ibmvnic_adapter *adapter, int retry)
4342{
4343 struct device *dev = &adapter->vdev->dev;
4344 union ibmvnic_crq crq;
4345 int max_entries;
4346 int cap_reqs;
4347
4348 /* We send out 6 or 7 REQUEST_CAPABILITY CRQs below (depending on
4349 * the PROMISC flag). Initialize this count upfront. When the tasklet
4350 * receives a response to all of these, it will send the next protocol
4351 * message (QUERY_IP_OFFLOAD).
4352 */
4353 if (!(adapter->netdev->flags & IFF_PROMISC) ||
4354 adapter->promisc_supported)
4355 cap_reqs = 7;
4356 else
4357 cap_reqs = 6;
4358
4359 if (!retry) {
4360 /* Sub-CRQ entries are 32 byte long */
4361 int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
4362
4363 atomic_set(&adapter->running_cap_crqs, cap_reqs);
4364
4365 if (adapter->min_tx_entries_per_subcrq > entries_page ||
4366 adapter->min_rx_add_entries_per_subcrq > entries_page) {
4367 dev_err(dev, "Fatal, invalid entries per sub-crq\n");
4368 return;
4369 }
4370
4371 if (adapter->desired.mtu)
4372 adapter->req_mtu = adapter->desired.mtu;
4373 else
4374 adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
4375
4376 if (!adapter->desired.tx_entries)
4377 adapter->desired.tx_entries =
4378 adapter->max_tx_entries_per_subcrq;
4379 if (!adapter->desired.rx_entries)
4380 adapter->desired.rx_entries =
4381 adapter->max_rx_add_entries_per_subcrq;
4382
4383 max_entries = IBMVNIC_LTB_SET_SIZE /
4384 (adapter->req_mtu + IBMVNIC_BUFFER_HLEN);
4385
4386 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
4387 adapter->desired.tx_entries > IBMVNIC_LTB_SET_SIZE) {
4388 adapter->desired.tx_entries = max_entries;
4389 }
4390
4391 if ((adapter->req_mtu + IBMVNIC_BUFFER_HLEN) *
4392 adapter->desired.rx_entries > IBMVNIC_LTB_SET_SIZE) {
4393 adapter->desired.rx_entries = max_entries;
4394 }
4395
4396 if (adapter->desired.tx_entries)
4397 adapter->req_tx_entries_per_subcrq =
4398 adapter->desired.tx_entries;
4399 else
4400 adapter->req_tx_entries_per_subcrq =
4401 adapter->max_tx_entries_per_subcrq;
4402
4403 if (adapter->desired.rx_entries)
4404 adapter->req_rx_add_entries_per_subcrq =
4405 adapter->desired.rx_entries;
4406 else
4407 adapter->req_rx_add_entries_per_subcrq =
4408 adapter->max_rx_add_entries_per_subcrq;
4409
4410 if (adapter->desired.tx_queues)
4411 adapter->req_tx_queues =
4412 adapter->desired.tx_queues;
4413 else
4414 adapter->req_tx_queues =
4415 adapter->opt_tx_comp_sub_queues;
4416
4417 if (adapter->desired.rx_queues)
4418 adapter->req_rx_queues =
4419 adapter->desired.rx_queues;
4420 else
4421 adapter->req_rx_queues =
4422 adapter->opt_rx_comp_queues;
4423
4424 adapter->req_rx_add_queues = adapter->max_rx_add_queues;
4425 } else {
4426 atomic_add(cap_reqs, &adapter->running_cap_crqs);
4427 }
4428 memset(&crq, 0, sizeof(crq));
4429 crq.request_capability.first = IBMVNIC_CRQ_CMD;
4430 crq.request_capability.cmd = REQUEST_CAPABILITY;
4431
4432 crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
4433 crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
4434 cap_reqs--;
4435 ibmvnic_send_crq(adapter, &crq);
4436
4437 crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
4438 crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
4439 cap_reqs--;
4440 ibmvnic_send_crq(adapter, &crq);
4441
4442 crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
4443 crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
4444 cap_reqs--;
4445 ibmvnic_send_crq(adapter, &crq);
4446
4447 crq.request_capability.capability =
4448 cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
4449 crq.request_capability.number =
4450 cpu_to_be64(adapter->req_tx_entries_per_subcrq);
4451 cap_reqs--;
4452 ibmvnic_send_crq(adapter, &crq);
4453
4454 crq.request_capability.capability =
4455 cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
4456 crq.request_capability.number =
4457 cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
4458 cap_reqs--;
4459 ibmvnic_send_crq(adapter, &crq);
4460
4461 crq.request_capability.capability = cpu_to_be16(REQ_MTU);
4462 crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
4463 cap_reqs--;
4464 ibmvnic_send_crq(adapter, &crq);
4465
4466 if (adapter->netdev->flags & IFF_PROMISC) {
4467 if (adapter->promisc_supported) {
4468 crq.request_capability.capability =
4469 cpu_to_be16(PROMISC_REQUESTED);
4470 crq.request_capability.number = cpu_to_be64(1);
4471 cap_reqs--;
4472 ibmvnic_send_crq(adapter, &crq);
4473 }
4474 } else {
4475 crq.request_capability.capability =
4476 cpu_to_be16(PROMISC_REQUESTED);
4477 crq.request_capability.number = cpu_to_be64(0);
4478 cap_reqs--;
4479 ibmvnic_send_crq(adapter, &crq);
4480 }
4481
4482 /* Keep at end to catch any discrepancy between expected and actual
4483 * CRQs sent.
4484 */
4485 WARN_ON(cap_reqs != 0);
4486}
4487
4488static int pending_scrq(struct ibmvnic_adapter *adapter,
4489 struct ibmvnic_sub_crq_queue *scrq)
4490{
4491 union sub_crq *entry = &scrq->msgs[scrq->cur];
4492 int rc;
4493
4494 rc = !!(entry->generic.first & IBMVNIC_CRQ_CMD_RSP);
4495
4496 /* Ensure that the SCRQ valid flag is loaded prior to loading the
4497 * contents of the SCRQ descriptor
4498 */
4499 dma_rmb();
4500
4501 return rc;
4502}
4503
4504static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
4505 struct ibmvnic_sub_crq_queue *scrq)
4506{
4507 union sub_crq *entry;
4508 unsigned long flags;
4509
4510 spin_lock_irqsave(&scrq->lock, flags);
4511 entry = &scrq->msgs[scrq->cur];
4512 if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4513 if (++scrq->cur == scrq->size)
4514 scrq->cur = 0;
4515 } else {
4516 entry = NULL;
4517 }
4518 spin_unlock_irqrestore(&scrq->lock, flags);
4519
4520 /* Ensure that the SCRQ valid flag is loaded prior to loading the
4521 * contents of the SCRQ descriptor
4522 */
4523 dma_rmb();
4524
4525 return entry;
4526}
4527
4528static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
4529{
4530 struct ibmvnic_crq_queue *queue = &adapter->crq;
4531 union ibmvnic_crq *crq;
4532
4533 crq = &queue->msgs[queue->cur];
4534 if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
4535 if (++queue->cur == queue->size)
4536 queue->cur = 0;
4537 } else {
4538 crq = NULL;
4539 }
4540
4541 return crq;
4542}
4543
4544static void print_subcrq_error(struct device *dev, int rc, const char *func)
4545{
4546 switch (rc) {
4547 case H_PARAMETER:
4548 dev_warn_ratelimited(dev,
4549 "%s failed: Send request is malformed or adapter failover pending. (rc=%d)\n",
4550 func, rc);
4551 break;
4552 case H_CLOSED:
4553 dev_warn_ratelimited(dev,
4554 "%s failed: Backing queue closed. Adapter is down or failover pending. (rc=%d)\n",
4555 func, rc);
4556 break;
4557 default:
4558 dev_err_ratelimited(dev, "%s failed: (rc=%d)\n", func, rc);
4559 break;
4560 }
4561}
4562
4563static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
4564 u64 remote_handle, u64 ioba, u64 num_entries)
4565{
4566 unsigned int ua = adapter->vdev->unit_address;
4567 struct device *dev = &adapter->vdev->dev;
4568 int rc;
4569
4570 /* Make sure the hypervisor sees the complete request */
4571 dma_wmb();
4572 rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
4573 cpu_to_be64(remote_handle),
4574 ioba, num_entries);
4575
4576 if (rc)
4577 print_subcrq_error(dev, rc, __func__);
4578
4579 return rc;
4580}
4581
4582static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
4583 union ibmvnic_crq *crq)
4584{
4585 unsigned int ua = adapter->vdev->unit_address;
4586 struct device *dev = &adapter->vdev->dev;
4587 u64 *u64_crq = (u64 *)crq;
4588 int rc;
4589
4590 netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
4591 (unsigned long)cpu_to_be64(u64_crq[0]),
4592 (unsigned long)cpu_to_be64(u64_crq[1]));
4593
4594 if (!adapter->crq.active &&
4595 crq->generic.first != IBMVNIC_CRQ_INIT_CMD) {
4596 dev_warn(dev, "Invalid request detected while CRQ is inactive, possible device state change during reset\n");
4597 return -EINVAL;
4598 }
4599
4600 /* Make sure the hypervisor sees the complete request */
4601 dma_wmb();
4602
4603 rc = plpar_hcall_norets(H_SEND_CRQ, ua,
4604 cpu_to_be64(u64_crq[0]),
4605 cpu_to_be64(u64_crq[1]));
4606
4607 if (rc) {
4608 if (rc == H_CLOSED) {
4609 dev_warn(dev, "CRQ Queue closed\n");
4610 /* do not reset, report the fail, wait for passive init from server */
4611 }
4612
4613 dev_warn(dev, "Send error (rc=%d)\n", rc);
4614 }
4615
4616 return rc;
4617}
4618
4619static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
4620{
4621 struct device *dev = &adapter->vdev->dev;
4622 union ibmvnic_crq crq;
4623 int retries = 100;
4624 int rc;
4625
4626 memset(&crq, 0, sizeof(crq));
4627 crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
4628 crq.generic.cmd = IBMVNIC_CRQ_INIT;
4629 netdev_dbg(adapter->netdev, "Sending CRQ init\n");
4630
4631 do {
4632 rc = ibmvnic_send_crq(adapter, &crq);
4633 if (rc != H_CLOSED)
4634 break;
4635 retries--;
4636 msleep(50);
4637
4638 } while (retries > 0);
4639
4640 if (rc) {
4641 dev_err(dev, "Failed to send init request, rc = %d\n", rc);
4642 return rc;
4643 }
4644
4645 return 0;
4646}
4647
4648struct vnic_login_client_data {
4649 u8 type;
4650 __be16 len;
4651 char name[];
4652} __packed;
4653
4654static int vnic_client_data_len(struct ibmvnic_adapter *adapter)
4655{
4656 int len;
4657
4658 /* Calculate the amount of buffer space needed for the
4659 * vnic client data in the login buffer. There are four entries,
4660 * OS name, LPAR name, device name, and a null last entry.
4661 */
4662 len = 4 * sizeof(struct vnic_login_client_data);
4663 len += 6; /* "Linux" plus NULL */
4664 len += strlen(utsname()->nodename) + 1;
4665 len += strlen(adapter->netdev->name) + 1;
4666
4667 return len;
4668}
4669
4670static void vnic_add_client_data(struct ibmvnic_adapter *adapter,
4671 struct vnic_login_client_data *vlcd)
4672{
4673 const char *os_name = "Linux";
4674 int len;
4675
4676 /* Type 1 - LPAR OS */
4677 vlcd->type = 1;
4678 len = strlen(os_name) + 1;
4679 vlcd->len = cpu_to_be16(len);
4680 strscpy(vlcd->name, os_name, len);
4681 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
4682
4683 /* Type 2 - LPAR name */
4684 vlcd->type = 2;
4685 len = strlen(utsname()->nodename) + 1;
4686 vlcd->len = cpu_to_be16(len);
4687 strscpy(vlcd->name, utsname()->nodename, len);
4688 vlcd = (struct vnic_login_client_data *)(vlcd->name + len);
4689
4690 /* Type 3 - device name */
4691 vlcd->type = 3;
4692 len = strlen(adapter->netdev->name) + 1;
4693 vlcd->len = cpu_to_be16(len);
4694 strscpy(vlcd->name, adapter->netdev->name, len);
4695}
4696
4697static int send_login(struct ibmvnic_adapter *adapter)
4698{
4699 struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
4700 struct ibmvnic_login_buffer *login_buffer;
4701 struct device *dev = &adapter->vdev->dev;
4702 struct vnic_login_client_data *vlcd;
4703 dma_addr_t rsp_buffer_token;
4704 dma_addr_t buffer_token;
4705 size_t rsp_buffer_size;
4706 union ibmvnic_crq crq;
4707 int client_data_len;
4708 size_t buffer_size;
4709 __be64 *tx_list_p;
4710 __be64 *rx_list_p;
4711 int rc;
4712 int i;
4713
4714 if (!adapter->tx_scrq || !adapter->rx_scrq) {
4715 netdev_err(adapter->netdev,
4716 "RX or TX queues are not allocated, device login failed\n");
4717 return -ENOMEM;
4718 }
4719
4720 release_login_buffer(adapter);
4721 release_login_rsp_buffer(adapter);
4722
4723 client_data_len = vnic_client_data_len(adapter);
4724
4725 buffer_size =
4726 sizeof(struct ibmvnic_login_buffer) +
4727 sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues) +
4728 client_data_len;
4729
4730 login_buffer = kzalloc(buffer_size, GFP_ATOMIC);
4731 if (!login_buffer)
4732 goto buf_alloc_failed;
4733
4734 buffer_token = dma_map_single(dev, login_buffer, buffer_size,
4735 DMA_TO_DEVICE);
4736 if (dma_mapping_error(dev, buffer_token)) {
4737 dev_err(dev, "Couldn't map login buffer\n");
4738 goto buf_map_failed;
4739 }
4740
4741 rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
4742 sizeof(u64) * adapter->req_tx_queues +
4743 sizeof(u64) * adapter->req_rx_queues +
4744 sizeof(u64) * adapter->req_rx_queues +
4745 sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
4746
4747 login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
4748 if (!login_rsp_buffer)
4749 goto buf_rsp_alloc_failed;
4750
4751 rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
4752 rsp_buffer_size, DMA_FROM_DEVICE);
4753 if (dma_mapping_error(dev, rsp_buffer_token)) {
4754 dev_err(dev, "Couldn't map login rsp buffer\n");
4755 goto buf_rsp_map_failed;
4756 }
4757
4758 adapter->login_buf = login_buffer;
4759 adapter->login_buf_token = buffer_token;
4760 adapter->login_buf_sz = buffer_size;
4761 adapter->login_rsp_buf = login_rsp_buffer;
4762 adapter->login_rsp_buf_token = rsp_buffer_token;
4763 adapter->login_rsp_buf_sz = rsp_buffer_size;
4764
4765 login_buffer->len = cpu_to_be32(buffer_size);
4766 login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
4767 login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
4768 login_buffer->off_txcomp_subcrqs =
4769 cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
4770 login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
4771 login_buffer->off_rxcomp_subcrqs =
4772 cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
4773 sizeof(u64) * adapter->req_tx_queues);
4774 login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
4775 login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
4776
4777 tx_list_p = (__be64 *)((char *)login_buffer +
4778 sizeof(struct ibmvnic_login_buffer));
4779 rx_list_p = (__be64 *)((char *)login_buffer +
4780 sizeof(struct ibmvnic_login_buffer) +
4781 sizeof(u64) * adapter->req_tx_queues);
4782
4783 for (i = 0; i < adapter->req_tx_queues; i++) {
4784 if (adapter->tx_scrq[i]) {
4785 tx_list_p[i] =
4786 cpu_to_be64(adapter->tx_scrq[i]->crq_num);
4787 }
4788 }
4789
4790 for (i = 0; i < adapter->req_rx_queues; i++) {
4791 if (adapter->rx_scrq[i]) {
4792 rx_list_p[i] =
4793 cpu_to_be64(adapter->rx_scrq[i]->crq_num);
4794 }
4795 }
4796
4797 /* Insert vNIC login client data */
4798 vlcd = (struct vnic_login_client_data *)
4799 ((char *)rx_list_p + (sizeof(u64) * adapter->req_rx_queues));
4800 login_buffer->client_data_offset =
4801 cpu_to_be32((char *)vlcd - (char *)login_buffer);
4802 login_buffer->client_data_len = cpu_to_be32(client_data_len);
4803
4804 vnic_add_client_data(adapter, vlcd);
4805
4806 netdev_dbg(adapter->netdev, "Login Buffer:\n");
4807 for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
4808 netdev_dbg(adapter->netdev, "%016lx\n",
4809 ((unsigned long *)(adapter->login_buf))[i]);
4810 }
4811
4812 memset(&crq, 0, sizeof(crq));
4813 crq.login.first = IBMVNIC_CRQ_CMD;
4814 crq.login.cmd = LOGIN;
4815 crq.login.ioba = cpu_to_be32(buffer_token);
4816 crq.login.len = cpu_to_be32(buffer_size);
4817
4818 adapter->login_pending = true;
4819 rc = ibmvnic_send_crq(adapter, &crq);
4820 if (rc) {
4821 adapter->login_pending = false;
4822 netdev_err(adapter->netdev, "Failed to send login, rc=%d\n", rc);
4823 goto buf_rsp_map_failed;
4824 }
4825
4826 return 0;
4827
4828buf_rsp_map_failed:
4829 kfree(login_rsp_buffer);
4830 adapter->login_rsp_buf = NULL;
4831buf_rsp_alloc_failed:
4832 dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
4833buf_map_failed:
4834 kfree(login_buffer);
4835 adapter->login_buf = NULL;
4836buf_alloc_failed:
4837 return -ENOMEM;
4838}
4839
4840static int send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
4841 u32 len, u8 map_id)
4842{
4843 union ibmvnic_crq crq;
4844
4845 memset(&crq, 0, sizeof(crq));
4846 crq.request_map.first = IBMVNIC_CRQ_CMD;
4847 crq.request_map.cmd = REQUEST_MAP;
4848 crq.request_map.map_id = map_id;
4849 crq.request_map.ioba = cpu_to_be32(addr);
4850 crq.request_map.len = cpu_to_be32(len);
4851 return ibmvnic_send_crq(adapter, &crq);
4852}
4853
4854static int send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
4855{
4856 union ibmvnic_crq crq;
4857
4858 memset(&crq, 0, sizeof(crq));
4859 crq.request_unmap.first = IBMVNIC_CRQ_CMD;
4860 crq.request_unmap.cmd = REQUEST_UNMAP;
4861 crq.request_unmap.map_id = map_id;
4862 return ibmvnic_send_crq(adapter, &crq);
4863}
4864
4865static void send_query_map(struct ibmvnic_adapter *adapter)
4866{
4867 union ibmvnic_crq crq;
4868
4869 memset(&crq, 0, sizeof(crq));
4870 crq.query_map.first = IBMVNIC_CRQ_CMD;
4871 crq.query_map.cmd = QUERY_MAP;
4872 ibmvnic_send_crq(adapter, &crq);
4873}
4874
4875/* Send a series of CRQs requesting various capabilities of the VNIC server */
4876static void send_query_cap(struct ibmvnic_adapter *adapter)
4877{
4878 union ibmvnic_crq crq;
4879 int cap_reqs;
4880
4881 /* We send out 25 QUERY_CAPABILITY CRQs below. Initialize this count
4882 * upfront. When the tasklet receives a response to all of these, it
4883 * can send out the next protocol messaage (REQUEST_CAPABILITY).
4884 */
4885 cap_reqs = 25;
4886
4887 atomic_set(&adapter->running_cap_crqs, cap_reqs);
4888
4889 memset(&crq, 0, sizeof(crq));
4890 crq.query_capability.first = IBMVNIC_CRQ_CMD;
4891 crq.query_capability.cmd = QUERY_CAPABILITY;
4892
4893 crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
4894 ibmvnic_send_crq(adapter, &crq);
4895 cap_reqs--;
4896
4897 crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
4898 ibmvnic_send_crq(adapter, &crq);
4899 cap_reqs--;
4900
4901 crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
4902 ibmvnic_send_crq(adapter, &crq);
4903 cap_reqs--;
4904
4905 crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
4906 ibmvnic_send_crq(adapter, &crq);
4907 cap_reqs--;
4908
4909 crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
4910 ibmvnic_send_crq(adapter, &crq);
4911 cap_reqs--;
4912
4913 crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
4914 ibmvnic_send_crq(adapter, &crq);
4915 cap_reqs--;
4916
4917 crq.query_capability.capability =
4918 cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
4919 ibmvnic_send_crq(adapter, &crq);
4920 cap_reqs--;
4921
4922 crq.query_capability.capability =
4923 cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
4924 ibmvnic_send_crq(adapter, &crq);
4925 cap_reqs--;
4926
4927 crq.query_capability.capability =
4928 cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
4929 ibmvnic_send_crq(adapter, &crq);
4930 cap_reqs--;
4931
4932 crq.query_capability.capability =
4933 cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
4934 ibmvnic_send_crq(adapter, &crq);
4935 cap_reqs--;
4936
4937 crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
4938 ibmvnic_send_crq(adapter, &crq);
4939 cap_reqs--;
4940
4941 crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
4942 ibmvnic_send_crq(adapter, &crq);
4943 cap_reqs--;
4944
4945 crq.query_capability.capability = cpu_to_be16(MIN_MTU);
4946 ibmvnic_send_crq(adapter, &crq);
4947 cap_reqs--;
4948
4949 crq.query_capability.capability = cpu_to_be16(MAX_MTU);
4950 ibmvnic_send_crq(adapter, &crq);
4951 cap_reqs--;
4952
4953 crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
4954 ibmvnic_send_crq(adapter, &crq);
4955 cap_reqs--;
4956
4957 crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
4958 ibmvnic_send_crq(adapter, &crq);
4959 cap_reqs--;
4960
4961 crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
4962 ibmvnic_send_crq(adapter, &crq);
4963 cap_reqs--;
4964
4965 crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
4966 ibmvnic_send_crq(adapter, &crq);
4967 cap_reqs--;
4968
4969 crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
4970 ibmvnic_send_crq(adapter, &crq);
4971 cap_reqs--;
4972
4973 crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
4974 ibmvnic_send_crq(adapter, &crq);
4975 cap_reqs--;
4976
4977 crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
4978 ibmvnic_send_crq(adapter, &crq);
4979 cap_reqs--;
4980
4981 crq.query_capability.capability =
4982 cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
4983 ibmvnic_send_crq(adapter, &crq);
4984 cap_reqs--;
4985
4986 crq.query_capability.capability =
4987 cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
4988 ibmvnic_send_crq(adapter, &crq);
4989 cap_reqs--;
4990
4991 crq.query_capability.capability =
4992 cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
4993 ibmvnic_send_crq(adapter, &crq);
4994 cap_reqs--;
4995
4996 crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
4997
4998 ibmvnic_send_crq(adapter, &crq);
4999 cap_reqs--;
5000
5001 /* Keep at end to catch any discrepancy between expected and actual
5002 * CRQs sent.
5003 */
5004 WARN_ON(cap_reqs != 0);
5005}
5006
5007static void send_query_ip_offload(struct ibmvnic_adapter *adapter)
5008{
5009 int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
5010 struct device *dev = &adapter->vdev->dev;
5011 union ibmvnic_crq crq;
5012
5013 adapter->ip_offload_tok =
5014 dma_map_single(dev,
5015 &adapter->ip_offload_buf,
5016 buf_sz,
5017 DMA_FROM_DEVICE);
5018
5019 if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
5020 if (!firmware_has_feature(FW_FEATURE_CMO))
5021 dev_err(dev, "Couldn't map offload buffer\n");
5022 return;
5023 }
5024
5025 memset(&crq, 0, sizeof(crq));
5026 crq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
5027 crq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
5028 crq.query_ip_offload.len = cpu_to_be32(buf_sz);
5029 crq.query_ip_offload.ioba =
5030 cpu_to_be32(adapter->ip_offload_tok);
5031
5032 ibmvnic_send_crq(adapter, &crq);
5033}
5034
5035static void send_control_ip_offload(struct ibmvnic_adapter *adapter)
5036{
5037 struct ibmvnic_control_ip_offload_buffer *ctrl_buf = &adapter->ip_offload_ctrl;
5038 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
5039 struct device *dev = &adapter->vdev->dev;
5040 netdev_features_t old_hw_features = 0;
5041 union ibmvnic_crq crq;
5042
5043 adapter->ip_offload_ctrl_tok =
5044 dma_map_single(dev,
5045 ctrl_buf,
5046 sizeof(adapter->ip_offload_ctrl),
5047 DMA_TO_DEVICE);
5048
5049 if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
5050 dev_err(dev, "Couldn't map ip offload control buffer\n");
5051 return;
5052 }
5053
5054 ctrl_buf->len = cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
5055 ctrl_buf->version = cpu_to_be32(INITIAL_VERSION_IOB);
5056 ctrl_buf->ipv4_chksum = buf->ipv4_chksum;
5057 ctrl_buf->ipv6_chksum = buf->ipv6_chksum;
5058 ctrl_buf->tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
5059 ctrl_buf->udp_ipv4_chksum = buf->udp_ipv4_chksum;
5060 ctrl_buf->tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
5061 ctrl_buf->udp_ipv6_chksum = buf->udp_ipv6_chksum;
5062 ctrl_buf->large_tx_ipv4 = buf->large_tx_ipv4;
5063 ctrl_buf->large_tx_ipv6 = buf->large_tx_ipv6;
5064
5065 /* large_rx disabled for now, additional features needed */
5066 ctrl_buf->large_rx_ipv4 = 0;
5067 ctrl_buf->large_rx_ipv6 = 0;
5068
5069 if (adapter->state != VNIC_PROBING) {
5070 old_hw_features = adapter->netdev->hw_features;
5071 adapter->netdev->hw_features = 0;
5072 }
5073
5074 adapter->netdev->hw_features = NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO;
5075
5076 if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
5077 adapter->netdev->hw_features |= NETIF_F_IP_CSUM;
5078
5079 if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
5080 adapter->netdev->hw_features |= NETIF_F_IPV6_CSUM;
5081
5082 if ((adapter->netdev->features &
5083 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
5084 adapter->netdev->hw_features |= NETIF_F_RXCSUM;
5085
5086 if (buf->large_tx_ipv4)
5087 adapter->netdev->hw_features |= NETIF_F_TSO;
5088 if (buf->large_tx_ipv6)
5089 adapter->netdev->hw_features |= NETIF_F_TSO6;
5090
5091 if (adapter->state == VNIC_PROBING) {
5092 adapter->netdev->features |= adapter->netdev->hw_features;
5093 } else if (old_hw_features != adapter->netdev->hw_features) {
5094 netdev_features_t tmp = 0;
5095
5096 /* disable features no longer supported */
5097 adapter->netdev->features &= adapter->netdev->hw_features;
5098 /* turn on features now supported if previously enabled */
5099 tmp = (old_hw_features ^ adapter->netdev->hw_features) &
5100 adapter->netdev->hw_features;
5101 adapter->netdev->features |=
5102 tmp & adapter->netdev->wanted_features;
5103 }
5104
5105 memset(&crq, 0, sizeof(crq));
5106 crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
5107 crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
5108 crq.control_ip_offload.len =
5109 cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
5110 crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
5111 ibmvnic_send_crq(adapter, &crq);
5112}
5113
5114static void handle_vpd_size_rsp(union ibmvnic_crq *crq,
5115 struct ibmvnic_adapter *adapter)
5116{
5117 struct device *dev = &adapter->vdev->dev;
5118
5119 if (crq->get_vpd_size_rsp.rc.code) {
5120 dev_err(dev, "Error retrieving VPD size, rc=%x\n",
5121 crq->get_vpd_size_rsp.rc.code);
5122 complete(&adapter->fw_done);
5123 return;
5124 }
5125
5126 adapter->vpd->len = be64_to_cpu(crq->get_vpd_size_rsp.len);
5127 complete(&adapter->fw_done);
5128}
5129
5130static void handle_vpd_rsp(union ibmvnic_crq *crq,
5131 struct ibmvnic_adapter *adapter)
5132{
5133 struct device *dev = &adapter->vdev->dev;
5134 unsigned char *substr = NULL;
5135 u8 fw_level_len = 0;
5136
5137 memset(adapter->fw_version, 0, 32);
5138
5139 dma_unmap_single(dev, adapter->vpd->dma_addr, adapter->vpd->len,
5140 DMA_FROM_DEVICE);
5141
5142 if (crq->get_vpd_rsp.rc.code) {
5143 dev_err(dev, "Error retrieving VPD from device, rc=%x\n",
5144 crq->get_vpd_rsp.rc.code);
5145 goto complete;
5146 }
5147
5148 /* get the position of the firmware version info
5149 * located after the ASCII 'RM' substring in the buffer
5150 */
5151 substr = strnstr(adapter->vpd->buff, "RM", adapter->vpd->len);
5152 if (!substr) {
5153 dev_info(dev, "Warning - No FW level has been provided in the VPD buffer by the VIOS Server\n");
5154 goto complete;
5155 }
5156
5157 /* get length of firmware level ASCII substring */
5158 if ((substr + 2) < (adapter->vpd->buff + adapter->vpd->len)) {
5159 fw_level_len = *(substr + 2);
5160 } else {
5161 dev_info(dev, "Length of FW substr extrapolated VDP buff\n");
5162 goto complete;
5163 }
5164
5165 /* copy firmware version string from vpd into adapter */
5166 if ((substr + 3 + fw_level_len) <
5167 (adapter->vpd->buff + adapter->vpd->len)) {
5168 strncpy((char *)adapter->fw_version, substr + 3, fw_level_len);
5169 } else {
5170 dev_info(dev, "FW substr extrapolated VPD buff\n");
5171 }
5172
5173complete:
5174 if (adapter->fw_version[0] == '\0')
5175 strscpy((char *)adapter->fw_version, "N/A", sizeof(adapter->fw_version));
5176 complete(&adapter->fw_done);
5177}
5178
5179static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
5180{
5181 struct device *dev = &adapter->vdev->dev;
5182 struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
5183 int i;
5184
5185 dma_unmap_single(dev, adapter->ip_offload_tok,
5186 sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
5187
5188 netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
5189 for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
5190 netdev_dbg(adapter->netdev, "%016lx\n",
5191 ((unsigned long *)(buf))[i]);
5192
5193 netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
5194 netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
5195 netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
5196 buf->tcp_ipv4_chksum);
5197 netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
5198 buf->tcp_ipv6_chksum);
5199 netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
5200 buf->udp_ipv4_chksum);
5201 netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
5202 buf->udp_ipv6_chksum);
5203 netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
5204 buf->large_tx_ipv4);
5205 netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
5206 buf->large_tx_ipv6);
5207 netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
5208 buf->large_rx_ipv4);
5209 netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
5210 buf->large_rx_ipv6);
5211 netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
5212 buf->max_ipv4_header_size);
5213 netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
5214 buf->max_ipv6_header_size);
5215 netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
5216 buf->max_tcp_header_size);
5217 netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
5218 buf->max_udp_header_size);
5219 netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
5220 buf->max_large_tx_size);
5221 netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
5222 buf->max_large_rx_size);
5223 netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
5224 buf->ipv6_extension_header);
5225 netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
5226 buf->tcp_pseudosum_req);
5227 netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
5228 buf->num_ipv6_ext_headers);
5229 netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
5230 buf->off_ipv6_ext_headers);
5231
5232 send_control_ip_offload(adapter);
5233}
5234
5235static const char *ibmvnic_fw_err_cause(u16 cause)
5236{
5237 switch (cause) {
5238 case ADAPTER_PROBLEM:
5239 return "adapter problem";
5240 case BUS_PROBLEM:
5241 return "bus problem";
5242 case FW_PROBLEM:
5243 return "firmware problem";
5244 case DD_PROBLEM:
5245 return "device driver problem";
5246 case EEH_RECOVERY:
5247 return "EEH recovery";
5248 case FW_UPDATED:
5249 return "firmware updated";
5250 case LOW_MEMORY:
5251 return "low Memory";
5252 default:
5253 return "unknown";
5254 }
5255}
5256
5257static void handle_error_indication(union ibmvnic_crq *crq,
5258 struct ibmvnic_adapter *adapter)
5259{
5260 struct device *dev = &adapter->vdev->dev;
5261 u16 cause;
5262
5263 cause = be16_to_cpu(crq->error_indication.error_cause);
5264
5265 dev_warn_ratelimited(dev,
5266 "Firmware reports %serror, cause: %s. Starting recovery...\n",
5267 crq->error_indication.flags
5268 & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
5269 ibmvnic_fw_err_cause(cause));
5270
5271 if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
5272 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5273 else
5274 ibmvnic_reset(adapter, VNIC_RESET_NON_FATAL);
5275}
5276
5277static int handle_change_mac_rsp(union ibmvnic_crq *crq,
5278 struct ibmvnic_adapter *adapter)
5279{
5280 struct net_device *netdev = adapter->netdev;
5281 struct device *dev = &adapter->vdev->dev;
5282 long rc;
5283
5284 rc = crq->change_mac_addr_rsp.rc.code;
5285 if (rc) {
5286 dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
5287 goto out;
5288 }
5289 /* crq->change_mac_addr.mac_addr is the requested one
5290 * crq->change_mac_addr_rsp.mac_addr is the returned valid one.
5291 */
5292 eth_hw_addr_set(netdev, &crq->change_mac_addr_rsp.mac_addr[0]);
5293 ether_addr_copy(adapter->mac_addr,
5294 &crq->change_mac_addr_rsp.mac_addr[0]);
5295out:
5296 complete(&adapter->fw_done);
5297 return rc;
5298}
5299
5300static void handle_request_cap_rsp(union ibmvnic_crq *crq,
5301 struct ibmvnic_adapter *adapter)
5302{
5303 struct device *dev = &adapter->vdev->dev;
5304 u64 *req_value;
5305 char *name;
5306
5307 atomic_dec(&adapter->running_cap_crqs);
5308 netdev_dbg(adapter->netdev, "Outstanding request-caps: %d\n",
5309 atomic_read(&adapter->running_cap_crqs));
5310 switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
5311 case REQ_TX_QUEUES:
5312 req_value = &adapter->req_tx_queues;
5313 name = "tx";
5314 break;
5315 case REQ_RX_QUEUES:
5316 req_value = &adapter->req_rx_queues;
5317 name = "rx";
5318 break;
5319 case REQ_RX_ADD_QUEUES:
5320 req_value = &adapter->req_rx_add_queues;
5321 name = "rx_add";
5322 break;
5323 case REQ_TX_ENTRIES_PER_SUBCRQ:
5324 req_value = &adapter->req_tx_entries_per_subcrq;
5325 name = "tx_entries_per_subcrq";
5326 break;
5327 case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
5328 req_value = &adapter->req_rx_add_entries_per_subcrq;
5329 name = "rx_add_entries_per_subcrq";
5330 break;
5331 case REQ_MTU:
5332 req_value = &adapter->req_mtu;
5333 name = "mtu";
5334 break;
5335 case PROMISC_REQUESTED:
5336 req_value = &adapter->promisc;
5337 name = "promisc";
5338 break;
5339 default:
5340 dev_err(dev, "Got invalid cap request rsp %d\n",
5341 crq->request_capability.capability);
5342 return;
5343 }
5344
5345 switch (crq->request_capability_rsp.rc.code) {
5346 case SUCCESS:
5347 break;
5348 case PARTIALSUCCESS:
5349 dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
5350 *req_value,
5351 (long)be64_to_cpu(crq->request_capability_rsp.number),
5352 name);
5353
5354 if (be16_to_cpu(crq->request_capability_rsp.capability) ==
5355 REQ_MTU) {
5356 pr_err("mtu of %llu is not supported. Reverting.\n",
5357 *req_value);
5358 *req_value = adapter->fallback.mtu;
5359 } else {
5360 *req_value =
5361 be64_to_cpu(crq->request_capability_rsp.number);
5362 }
5363
5364 send_request_cap(adapter, 1);
5365 return;
5366 default:
5367 dev_err(dev, "Error %d in request cap rsp\n",
5368 crq->request_capability_rsp.rc.code);
5369 return;
5370 }
5371
5372 /* Done receiving requested capabilities, query IP offload support */
5373 if (atomic_read(&adapter->running_cap_crqs) == 0)
5374 send_query_ip_offload(adapter);
5375}
5376
5377static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
5378 struct ibmvnic_adapter *adapter)
5379{
5380 struct device *dev = &adapter->vdev->dev;
5381 struct net_device *netdev = adapter->netdev;
5382 struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
5383 struct ibmvnic_login_buffer *login = adapter->login_buf;
5384 u64 *tx_handle_array;
5385 u64 *rx_handle_array;
5386 int num_tx_pools;
5387 int num_rx_pools;
5388 u64 *size_array;
5389 int i;
5390
5391 /* CHECK: Test/set of login_pending does not need to be atomic
5392 * because only ibmvnic_tasklet tests/clears this.
5393 */
5394 if (!adapter->login_pending) {
5395 netdev_warn(netdev, "Ignoring unexpected login response\n");
5396 return 0;
5397 }
5398 adapter->login_pending = false;
5399
5400 dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
5401 DMA_TO_DEVICE);
5402 dma_unmap_single(dev, adapter->login_rsp_buf_token,
5403 adapter->login_rsp_buf_sz, DMA_FROM_DEVICE);
5404
5405 /* If the number of queues requested can't be allocated by the
5406 * server, the login response will return with code 1. We will need
5407 * to resend the login buffer with fewer queues requested.
5408 */
5409 if (login_rsp_crq->generic.rc.code) {
5410 adapter->init_done_rc = login_rsp_crq->generic.rc.code;
5411 complete(&adapter->init_done);
5412 return 0;
5413 }
5414
5415 if (adapter->failover_pending) {
5416 adapter->init_done_rc = -EAGAIN;
5417 netdev_dbg(netdev, "Failover pending, ignoring login response\n");
5418 complete(&adapter->init_done);
5419 /* login response buffer will be released on reset */
5420 return 0;
5421 }
5422
5423 netdev->mtu = adapter->req_mtu - ETH_HLEN;
5424
5425 netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
5426 for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
5427 netdev_dbg(adapter->netdev, "%016lx\n",
5428 ((unsigned long *)(adapter->login_rsp_buf))[i]);
5429 }
5430
5431 /* Sanity checks */
5432 if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
5433 (be32_to_cpu(login->num_rxcomp_subcrqs) *
5434 adapter->req_rx_add_queues !=
5435 be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
5436 dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
5437 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5438 return -EIO;
5439 }
5440 size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
5441 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
5442 /* variable buffer sizes are not supported, so just read the
5443 * first entry.
5444 */
5445 adapter->cur_rx_buf_sz = be64_to_cpu(size_array[0]);
5446
5447 num_tx_pools = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
5448 num_rx_pools = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
5449
5450 tx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
5451 be32_to_cpu(adapter->login_rsp_buf->off_txsubm_subcrqs));
5452 rx_handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
5453 be32_to_cpu(adapter->login_rsp_buf->off_rxadd_subcrqs));
5454
5455 for (i = 0; i < num_tx_pools; i++)
5456 adapter->tx_scrq[i]->handle = tx_handle_array[i];
5457
5458 for (i = 0; i < num_rx_pools; i++)
5459 adapter->rx_scrq[i]->handle = rx_handle_array[i];
5460
5461 adapter->num_active_tx_scrqs = num_tx_pools;
5462 adapter->num_active_rx_scrqs = num_rx_pools;
5463 release_login_rsp_buffer(adapter);
5464 release_login_buffer(adapter);
5465 complete(&adapter->init_done);
5466
5467 return 0;
5468}
5469
5470static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
5471 struct ibmvnic_adapter *adapter)
5472{
5473 struct device *dev = &adapter->vdev->dev;
5474 long rc;
5475
5476 rc = crq->request_unmap_rsp.rc.code;
5477 if (rc)
5478 dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
5479}
5480
5481static void handle_query_map_rsp(union ibmvnic_crq *crq,
5482 struct ibmvnic_adapter *adapter)
5483{
5484 struct net_device *netdev = adapter->netdev;
5485 struct device *dev = &adapter->vdev->dev;
5486 long rc;
5487
5488 rc = crq->query_map_rsp.rc.code;
5489 if (rc) {
5490 dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
5491 return;
5492 }
5493 netdev_dbg(netdev, "page_size = %d\ntot_pages = %u\nfree_pages = %u\n",
5494 crq->query_map_rsp.page_size,
5495 __be32_to_cpu(crq->query_map_rsp.tot_pages),
5496 __be32_to_cpu(crq->query_map_rsp.free_pages));
5497}
5498
5499static void handle_query_cap_rsp(union ibmvnic_crq *crq,
5500 struct ibmvnic_adapter *adapter)
5501{
5502 struct net_device *netdev = adapter->netdev;
5503 struct device *dev = &adapter->vdev->dev;
5504 long rc;
5505
5506 atomic_dec(&adapter->running_cap_crqs);
5507 netdev_dbg(netdev, "Outstanding queries: %d\n",
5508 atomic_read(&adapter->running_cap_crqs));
5509 rc = crq->query_capability.rc.code;
5510 if (rc) {
5511 dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
5512 goto out;
5513 }
5514
5515 switch (be16_to_cpu(crq->query_capability.capability)) {
5516 case MIN_TX_QUEUES:
5517 adapter->min_tx_queues =
5518 be64_to_cpu(crq->query_capability.number);
5519 netdev_dbg(netdev, "min_tx_queues = %lld\n",
5520 adapter->min_tx_queues);
5521 break;
5522 case MIN_RX_QUEUES:
5523 adapter->min_rx_queues =
5524 be64_to_cpu(crq->query_capability.number);
5525 netdev_dbg(netdev, "min_rx_queues = %lld\n",
5526 adapter->min_rx_queues);
5527 break;
5528 case MIN_RX_ADD_QUEUES:
5529 adapter->min_rx_add_queues =
5530 be64_to_cpu(crq->query_capability.number);
5531 netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
5532 adapter->min_rx_add_queues);
5533 break;
5534 case MAX_TX_QUEUES:
5535 adapter->max_tx_queues =
5536 be64_to_cpu(crq->query_capability.number);
5537 netdev_dbg(netdev, "max_tx_queues = %lld\n",
5538 adapter->max_tx_queues);
5539 break;
5540 case MAX_RX_QUEUES:
5541 adapter->max_rx_queues =
5542 be64_to_cpu(crq->query_capability.number);
5543 netdev_dbg(netdev, "max_rx_queues = %lld\n",
5544 adapter->max_rx_queues);
5545 break;
5546 case MAX_RX_ADD_QUEUES:
5547 adapter->max_rx_add_queues =
5548 be64_to_cpu(crq->query_capability.number);
5549 netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
5550 adapter->max_rx_add_queues);
5551 break;
5552 case MIN_TX_ENTRIES_PER_SUBCRQ:
5553 adapter->min_tx_entries_per_subcrq =
5554 be64_to_cpu(crq->query_capability.number);
5555 netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
5556 adapter->min_tx_entries_per_subcrq);
5557 break;
5558 case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
5559 adapter->min_rx_add_entries_per_subcrq =
5560 be64_to_cpu(crq->query_capability.number);
5561 netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
5562 adapter->min_rx_add_entries_per_subcrq);
5563 break;
5564 case MAX_TX_ENTRIES_PER_SUBCRQ:
5565 adapter->max_tx_entries_per_subcrq =
5566 be64_to_cpu(crq->query_capability.number);
5567 netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
5568 adapter->max_tx_entries_per_subcrq);
5569 break;
5570 case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
5571 adapter->max_rx_add_entries_per_subcrq =
5572 be64_to_cpu(crq->query_capability.number);
5573 netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
5574 adapter->max_rx_add_entries_per_subcrq);
5575 break;
5576 case TCP_IP_OFFLOAD:
5577 adapter->tcp_ip_offload =
5578 be64_to_cpu(crq->query_capability.number);
5579 netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
5580 adapter->tcp_ip_offload);
5581 break;
5582 case PROMISC_SUPPORTED:
5583 adapter->promisc_supported =
5584 be64_to_cpu(crq->query_capability.number);
5585 netdev_dbg(netdev, "promisc_supported = %lld\n",
5586 adapter->promisc_supported);
5587 break;
5588 case MIN_MTU:
5589 adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
5590 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
5591 netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
5592 break;
5593 case MAX_MTU:
5594 adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
5595 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
5596 netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
5597 break;
5598 case MAX_MULTICAST_FILTERS:
5599 adapter->max_multicast_filters =
5600 be64_to_cpu(crq->query_capability.number);
5601 netdev_dbg(netdev, "max_multicast_filters = %lld\n",
5602 adapter->max_multicast_filters);
5603 break;
5604 case VLAN_HEADER_INSERTION:
5605 adapter->vlan_header_insertion =
5606 be64_to_cpu(crq->query_capability.number);
5607 if (adapter->vlan_header_insertion)
5608 netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
5609 netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
5610 adapter->vlan_header_insertion);
5611 break;
5612 case RX_VLAN_HEADER_INSERTION:
5613 adapter->rx_vlan_header_insertion =
5614 be64_to_cpu(crq->query_capability.number);
5615 netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
5616 adapter->rx_vlan_header_insertion);
5617 break;
5618 case MAX_TX_SG_ENTRIES:
5619 adapter->max_tx_sg_entries =
5620 be64_to_cpu(crq->query_capability.number);
5621 netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
5622 adapter->max_tx_sg_entries);
5623 break;
5624 case RX_SG_SUPPORTED:
5625 adapter->rx_sg_supported =
5626 be64_to_cpu(crq->query_capability.number);
5627 netdev_dbg(netdev, "rx_sg_supported = %lld\n",
5628 adapter->rx_sg_supported);
5629 break;
5630 case OPT_TX_COMP_SUB_QUEUES:
5631 adapter->opt_tx_comp_sub_queues =
5632 be64_to_cpu(crq->query_capability.number);
5633 netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
5634 adapter->opt_tx_comp_sub_queues);
5635 break;
5636 case OPT_RX_COMP_QUEUES:
5637 adapter->opt_rx_comp_queues =
5638 be64_to_cpu(crq->query_capability.number);
5639 netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
5640 adapter->opt_rx_comp_queues);
5641 break;
5642 case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
5643 adapter->opt_rx_bufadd_q_per_rx_comp_q =
5644 be64_to_cpu(crq->query_capability.number);
5645 netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
5646 adapter->opt_rx_bufadd_q_per_rx_comp_q);
5647 break;
5648 case OPT_TX_ENTRIES_PER_SUBCRQ:
5649 adapter->opt_tx_entries_per_subcrq =
5650 be64_to_cpu(crq->query_capability.number);
5651 netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
5652 adapter->opt_tx_entries_per_subcrq);
5653 break;
5654 case OPT_RXBA_ENTRIES_PER_SUBCRQ:
5655 adapter->opt_rxba_entries_per_subcrq =
5656 be64_to_cpu(crq->query_capability.number);
5657 netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
5658 adapter->opt_rxba_entries_per_subcrq);
5659 break;
5660 case TX_RX_DESC_REQ:
5661 adapter->tx_rx_desc_req = crq->query_capability.number;
5662 netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
5663 adapter->tx_rx_desc_req);
5664 break;
5665
5666 default:
5667 netdev_err(netdev, "Got invalid cap rsp %d\n",
5668 crq->query_capability.capability);
5669 }
5670
5671out:
5672 if (atomic_read(&adapter->running_cap_crqs) == 0)
5673 send_request_cap(adapter, 0);
5674}
5675
5676static int send_query_phys_parms(struct ibmvnic_adapter *adapter)
5677{
5678 union ibmvnic_crq crq;
5679 int rc;
5680
5681 memset(&crq, 0, sizeof(crq));
5682 crq.query_phys_parms.first = IBMVNIC_CRQ_CMD;
5683 crq.query_phys_parms.cmd = QUERY_PHYS_PARMS;
5684
5685 mutex_lock(&adapter->fw_lock);
5686 adapter->fw_done_rc = 0;
5687 reinit_completion(&adapter->fw_done);
5688
5689 rc = ibmvnic_send_crq(adapter, &crq);
5690 if (rc) {
5691 mutex_unlock(&adapter->fw_lock);
5692 return rc;
5693 }
5694
5695 rc = ibmvnic_wait_for_completion(adapter, &adapter->fw_done, 10000);
5696 if (rc) {
5697 mutex_unlock(&adapter->fw_lock);
5698 return rc;
5699 }
5700
5701 mutex_unlock(&adapter->fw_lock);
5702 return adapter->fw_done_rc ? -EIO : 0;
5703}
5704
5705static int handle_query_phys_parms_rsp(union ibmvnic_crq *crq,
5706 struct ibmvnic_adapter *adapter)
5707{
5708 struct net_device *netdev = adapter->netdev;
5709 int rc;
5710 __be32 rspeed = cpu_to_be32(crq->query_phys_parms_rsp.speed);
5711
5712 rc = crq->query_phys_parms_rsp.rc.code;
5713 if (rc) {
5714 netdev_err(netdev, "Error %d in QUERY_PHYS_PARMS\n", rc);
5715 return rc;
5716 }
5717 switch (rspeed) {
5718 case IBMVNIC_10MBPS:
5719 adapter->speed = SPEED_10;
5720 break;
5721 case IBMVNIC_100MBPS:
5722 adapter->speed = SPEED_100;
5723 break;
5724 case IBMVNIC_1GBPS:
5725 adapter->speed = SPEED_1000;
5726 break;
5727 case IBMVNIC_10GBPS:
5728 adapter->speed = SPEED_10000;
5729 break;
5730 case IBMVNIC_25GBPS:
5731 adapter->speed = SPEED_25000;
5732 break;
5733 case IBMVNIC_40GBPS:
5734 adapter->speed = SPEED_40000;
5735 break;
5736 case IBMVNIC_50GBPS:
5737 adapter->speed = SPEED_50000;
5738 break;
5739 case IBMVNIC_100GBPS:
5740 adapter->speed = SPEED_100000;
5741 break;
5742 case IBMVNIC_200GBPS:
5743 adapter->speed = SPEED_200000;
5744 break;
5745 default:
5746 if (netif_carrier_ok(netdev))
5747 netdev_warn(netdev, "Unknown speed 0x%08x\n", rspeed);
5748 adapter->speed = SPEED_UNKNOWN;
5749 }
5750 if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_FULL_DUPLEX)
5751 adapter->duplex = DUPLEX_FULL;
5752 else if (crq->query_phys_parms_rsp.flags1 & IBMVNIC_HALF_DUPLEX)
5753 adapter->duplex = DUPLEX_HALF;
5754 else
5755 adapter->duplex = DUPLEX_UNKNOWN;
5756
5757 return rc;
5758}
5759
5760static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
5761 struct ibmvnic_adapter *adapter)
5762{
5763 struct ibmvnic_generic_crq *gen_crq = &crq->generic;
5764 struct net_device *netdev = adapter->netdev;
5765 struct device *dev = &adapter->vdev->dev;
5766 u64 *u64_crq = (u64 *)crq;
5767 long rc;
5768
5769 netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
5770 (unsigned long)cpu_to_be64(u64_crq[0]),
5771 (unsigned long)cpu_to_be64(u64_crq[1]));
5772 switch (gen_crq->first) {
5773 case IBMVNIC_CRQ_INIT_RSP:
5774 switch (gen_crq->cmd) {
5775 case IBMVNIC_CRQ_INIT:
5776 dev_info(dev, "Partner initialized\n");
5777 adapter->from_passive_init = true;
5778 /* Discard any stale login responses from prev reset.
5779 * CHECK: should we clear even on INIT_COMPLETE?
5780 */
5781 adapter->login_pending = false;
5782
5783 if (adapter->state == VNIC_DOWN)
5784 rc = ibmvnic_reset(adapter, VNIC_RESET_PASSIVE_INIT);
5785 else
5786 rc = ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
5787
5788 if (rc && rc != -EBUSY) {
5789 /* We were unable to schedule the failover
5790 * reset either because the adapter was still
5791 * probing (eg: during kexec) or we could not
5792 * allocate memory. Clear the failover_pending
5793 * flag since no one else will. We ignore
5794 * EBUSY because it means either FAILOVER reset
5795 * is already scheduled or the adapter is
5796 * being removed.
5797 */
5798 netdev_err(netdev,
5799 "Error %ld scheduling failover reset\n",
5800 rc);
5801 adapter->failover_pending = false;
5802 }
5803
5804 if (!completion_done(&adapter->init_done)) {
5805 if (!adapter->init_done_rc)
5806 adapter->init_done_rc = -EAGAIN;
5807 complete(&adapter->init_done);
5808 }
5809
5810 break;
5811 case IBMVNIC_CRQ_INIT_COMPLETE:
5812 dev_info(dev, "Partner initialization complete\n");
5813 adapter->crq.active = true;
5814 send_version_xchg(adapter);
5815 break;
5816 default:
5817 dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
5818 }
5819 return;
5820 case IBMVNIC_CRQ_XPORT_EVENT:
5821 netif_carrier_off(netdev);
5822 adapter->crq.active = false;
5823 /* terminate any thread waiting for a response
5824 * from the device
5825 */
5826 if (!completion_done(&adapter->fw_done)) {
5827 adapter->fw_done_rc = -EIO;
5828 complete(&adapter->fw_done);
5829 }
5830
5831 /* if we got here during crq-init, retry crq-init */
5832 if (!completion_done(&adapter->init_done)) {
5833 adapter->init_done_rc = -EAGAIN;
5834 complete(&adapter->init_done);
5835 }
5836
5837 if (!completion_done(&adapter->stats_done))
5838 complete(&adapter->stats_done);
5839 if (test_bit(0, &adapter->resetting))
5840 adapter->force_reset_recovery = true;
5841 if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
5842 dev_info(dev, "Migrated, re-enabling adapter\n");
5843 ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
5844 } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
5845 dev_info(dev, "Backing device failover detected\n");
5846 adapter->failover_pending = true;
5847 } else {
5848 /* The adapter lost the connection */
5849 dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
5850 gen_crq->cmd);
5851 ibmvnic_reset(adapter, VNIC_RESET_FATAL);
5852 }
5853 return;
5854 case IBMVNIC_CRQ_CMD_RSP:
5855 break;
5856 default:
5857 dev_err(dev, "Got an invalid msg type 0x%02x\n",
5858 gen_crq->first);
5859 return;
5860 }
5861
5862 switch (gen_crq->cmd) {
5863 case VERSION_EXCHANGE_RSP:
5864 rc = crq->version_exchange_rsp.rc.code;
5865 if (rc) {
5866 dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
5867 break;
5868 }
5869 ibmvnic_version =
5870 be16_to_cpu(crq->version_exchange_rsp.version);
5871 dev_info(dev, "Partner protocol version is %d\n",
5872 ibmvnic_version);
5873 send_query_cap(adapter);
5874 break;
5875 case QUERY_CAPABILITY_RSP:
5876 handle_query_cap_rsp(crq, adapter);
5877 break;
5878 case QUERY_MAP_RSP:
5879 handle_query_map_rsp(crq, adapter);
5880 break;
5881 case REQUEST_MAP_RSP:
5882 adapter->fw_done_rc = crq->request_map_rsp.rc.code;
5883 complete(&adapter->fw_done);
5884 break;
5885 case REQUEST_UNMAP_RSP:
5886 handle_request_unmap_rsp(crq, adapter);
5887 break;
5888 case REQUEST_CAPABILITY_RSP:
5889 handle_request_cap_rsp(crq, adapter);
5890 break;
5891 case LOGIN_RSP:
5892 netdev_dbg(netdev, "Got Login Response\n");
5893 handle_login_rsp(crq, adapter);
5894 break;
5895 case LOGICAL_LINK_STATE_RSP:
5896 netdev_dbg(netdev,
5897 "Got Logical Link State Response, state: %d rc: %d\n",
5898 crq->logical_link_state_rsp.link_state,
5899 crq->logical_link_state_rsp.rc.code);
5900 adapter->logical_link_state =
5901 crq->logical_link_state_rsp.link_state;
5902 adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
5903 complete(&adapter->init_done);
5904 break;
5905 case LINK_STATE_INDICATION:
5906 netdev_dbg(netdev, "Got Logical Link State Indication\n");
5907 adapter->phys_link_state =
5908 crq->link_state_indication.phys_link_state;
5909 adapter->logical_link_state =
5910 crq->link_state_indication.logical_link_state;
5911 if (adapter->phys_link_state && adapter->logical_link_state)
5912 netif_carrier_on(netdev);
5913 else
5914 netif_carrier_off(netdev);
5915 break;
5916 case CHANGE_MAC_ADDR_RSP:
5917 netdev_dbg(netdev, "Got MAC address change Response\n");
5918 adapter->fw_done_rc = handle_change_mac_rsp(crq, adapter);
5919 break;
5920 case ERROR_INDICATION:
5921 netdev_dbg(netdev, "Got Error Indication\n");
5922 handle_error_indication(crq, adapter);
5923 break;
5924 case REQUEST_STATISTICS_RSP:
5925 netdev_dbg(netdev, "Got Statistics Response\n");
5926 complete(&adapter->stats_done);
5927 break;
5928 case QUERY_IP_OFFLOAD_RSP:
5929 netdev_dbg(netdev, "Got Query IP offload Response\n");
5930 handle_query_ip_offload_rsp(adapter);
5931 break;
5932 case MULTICAST_CTRL_RSP:
5933 netdev_dbg(netdev, "Got multicast control Response\n");
5934 break;
5935 case CONTROL_IP_OFFLOAD_RSP:
5936 netdev_dbg(netdev, "Got Control IP offload Response\n");
5937 dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
5938 sizeof(adapter->ip_offload_ctrl),
5939 DMA_TO_DEVICE);
5940 complete(&adapter->init_done);
5941 break;
5942 case COLLECT_FW_TRACE_RSP:
5943 netdev_dbg(netdev, "Got Collect firmware trace Response\n");
5944 complete(&adapter->fw_done);
5945 break;
5946 case GET_VPD_SIZE_RSP:
5947 handle_vpd_size_rsp(crq, adapter);
5948 break;
5949 case GET_VPD_RSP:
5950 handle_vpd_rsp(crq, adapter);
5951 break;
5952 case QUERY_PHYS_PARMS_RSP:
5953 adapter->fw_done_rc = handle_query_phys_parms_rsp(crq, adapter);
5954 complete(&adapter->fw_done);
5955 break;
5956 default:
5957 netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
5958 gen_crq->cmd);
5959 }
5960}
5961
5962static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
5963{
5964 struct ibmvnic_adapter *adapter = instance;
5965
5966 tasklet_schedule(&adapter->tasklet);
5967 return IRQ_HANDLED;
5968}
5969
5970static void ibmvnic_tasklet(struct tasklet_struct *t)
5971{
5972 struct ibmvnic_adapter *adapter = from_tasklet(adapter, t, tasklet);
5973 struct ibmvnic_crq_queue *queue = &adapter->crq;
5974 union ibmvnic_crq *crq;
5975 unsigned long flags;
5976
5977 spin_lock_irqsave(&queue->lock, flags);
5978
5979 /* Pull all the valid messages off the CRQ */
5980 while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
5981 /* This barrier makes sure ibmvnic_next_crq()'s
5982 * crq->generic.first & IBMVNIC_CRQ_CMD_RSP is loaded
5983 * before ibmvnic_handle_crq()'s
5984 * switch(gen_crq->first) and switch(gen_crq->cmd).
5985 */
5986 dma_rmb();
5987 ibmvnic_handle_crq(crq, adapter);
5988 crq->generic.first = 0;
5989 }
5990
5991 spin_unlock_irqrestore(&queue->lock, flags);
5992}
5993
5994static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
5995{
5996 struct vio_dev *vdev = adapter->vdev;
5997 int rc;
5998
5999 do {
6000 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
6001 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
6002
6003 if (rc)
6004 dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
6005
6006 return rc;
6007}
6008
6009static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
6010{
6011 struct ibmvnic_crq_queue *crq = &adapter->crq;
6012 struct device *dev = &adapter->vdev->dev;
6013 struct vio_dev *vdev = adapter->vdev;
6014 int rc;
6015
6016 /* Close the CRQ */
6017 do {
6018 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6019 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6020
6021 /* Clean out the queue */
6022 if (!crq->msgs)
6023 return -EINVAL;
6024
6025 memset(crq->msgs, 0, PAGE_SIZE);
6026 crq->cur = 0;
6027 crq->active = false;
6028
6029 /* And re-open it again */
6030 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
6031 crq->msg_token, PAGE_SIZE);
6032
6033 if (rc == H_CLOSED)
6034 /* Adapter is good, but other end is not ready */
6035 dev_warn(dev, "Partner adapter not ready\n");
6036 else if (rc != 0)
6037 dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
6038
6039 return rc;
6040}
6041
6042static void release_crq_queue(struct ibmvnic_adapter *adapter)
6043{
6044 struct ibmvnic_crq_queue *crq = &adapter->crq;
6045 struct vio_dev *vdev = adapter->vdev;
6046 long rc;
6047
6048 if (!crq->msgs)
6049 return;
6050
6051 netdev_dbg(adapter->netdev, "Releasing CRQ\n");
6052 free_irq(vdev->irq, adapter);
6053 tasklet_kill(&adapter->tasklet);
6054 do {
6055 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6056 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6057
6058 dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
6059 DMA_BIDIRECTIONAL);
6060 free_page((unsigned long)crq->msgs);
6061 crq->msgs = NULL;
6062 crq->active = false;
6063}
6064
6065static int init_crq_queue(struct ibmvnic_adapter *adapter)
6066{
6067 struct ibmvnic_crq_queue *crq = &adapter->crq;
6068 struct device *dev = &adapter->vdev->dev;
6069 struct vio_dev *vdev = adapter->vdev;
6070 int rc, retrc = -ENOMEM;
6071
6072 if (crq->msgs)
6073 return 0;
6074
6075 crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
6076 /* Should we allocate more than one page? */
6077
6078 if (!crq->msgs)
6079 return -ENOMEM;
6080
6081 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
6082 crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
6083 DMA_BIDIRECTIONAL);
6084 if (dma_mapping_error(dev, crq->msg_token))
6085 goto map_failed;
6086
6087 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
6088 crq->msg_token, PAGE_SIZE);
6089
6090 if (rc == H_RESOURCE)
6091 /* maybe kexecing and resource is busy. try a reset */
6092 rc = ibmvnic_reset_crq(adapter);
6093 retrc = rc;
6094
6095 if (rc == H_CLOSED) {
6096 dev_warn(dev, "Partner adapter not ready\n");
6097 } else if (rc) {
6098 dev_warn(dev, "Error %d opening adapter\n", rc);
6099 goto reg_crq_failed;
6100 }
6101
6102 retrc = 0;
6103
6104 tasklet_setup(&adapter->tasklet, (void *)ibmvnic_tasklet);
6105
6106 netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
6107 snprintf(crq->name, sizeof(crq->name), "ibmvnic-%x",
6108 adapter->vdev->unit_address);
6109 rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, crq->name, adapter);
6110 if (rc) {
6111 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
6112 vdev->irq, rc);
6113 goto req_irq_failed;
6114 }
6115
6116 rc = vio_enable_interrupts(vdev);
6117 if (rc) {
6118 dev_err(dev, "Error %d enabling interrupts\n", rc);
6119 goto req_irq_failed;
6120 }
6121
6122 crq->cur = 0;
6123 spin_lock_init(&crq->lock);
6124
6125 /* process any CRQs that were queued before we enabled interrupts */
6126 tasklet_schedule(&adapter->tasklet);
6127
6128 return retrc;
6129
6130req_irq_failed:
6131 tasklet_kill(&adapter->tasklet);
6132 do {
6133 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
6134 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
6135reg_crq_failed:
6136 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
6137map_failed:
6138 free_page((unsigned long)crq->msgs);
6139 crq->msgs = NULL;
6140 return retrc;
6141}
6142
6143static int ibmvnic_reset_init(struct ibmvnic_adapter *adapter, bool reset)
6144{
6145 struct device *dev = &adapter->vdev->dev;
6146 unsigned long timeout = msecs_to_jiffies(20000);
6147 u64 old_num_rx_queues = adapter->req_rx_queues;
6148 u64 old_num_tx_queues = adapter->req_tx_queues;
6149 int rc;
6150
6151 adapter->from_passive_init = false;
6152
6153 rc = ibmvnic_send_crq_init(adapter);
6154 if (rc) {
6155 dev_err(dev, "Send crq init failed with error %d\n", rc);
6156 return rc;
6157 }
6158
6159 if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
6160 dev_err(dev, "Initialization sequence timed out\n");
6161 return -ETIMEDOUT;
6162 }
6163
6164 if (adapter->init_done_rc) {
6165 release_crq_queue(adapter);
6166 dev_err(dev, "CRQ-init failed, %d\n", adapter->init_done_rc);
6167 return adapter->init_done_rc;
6168 }
6169
6170 if (adapter->from_passive_init) {
6171 adapter->state = VNIC_OPEN;
6172 adapter->from_passive_init = false;
6173 dev_err(dev, "CRQ-init failed, passive-init\n");
6174 return -EINVAL;
6175 }
6176
6177 if (reset &&
6178 test_bit(0, &adapter->resetting) && !adapter->wait_for_reset &&
6179 adapter->reset_reason != VNIC_RESET_MOBILITY) {
6180 if (adapter->req_rx_queues != old_num_rx_queues ||
6181 adapter->req_tx_queues != old_num_tx_queues) {
6182 release_sub_crqs(adapter, 0);
6183 rc = init_sub_crqs(adapter);
6184 } else {
6185 /* no need to reinitialize completely, but we do
6186 * need to clean up transmits that were in flight
6187 * when we processed the reset. Failure to do so
6188 * will confound the upper layer, usually TCP, by
6189 * creating the illusion of transmits that are
6190 * awaiting completion.
6191 */
6192 clean_tx_pools(adapter);
6193
6194 rc = reset_sub_crq_queues(adapter);
6195 }
6196 } else {
6197 rc = init_sub_crqs(adapter);
6198 }
6199
6200 if (rc) {
6201 dev_err(dev, "Initialization of sub crqs failed\n");
6202 release_crq_queue(adapter);
6203 return rc;
6204 }
6205
6206 rc = init_sub_crq_irqs(adapter);
6207 if (rc) {
6208 dev_err(dev, "Failed to initialize sub crq irqs\n");
6209 release_crq_queue(adapter);
6210 }
6211
6212 return rc;
6213}
6214
6215static struct device_attribute dev_attr_failover;
6216
6217static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
6218{
6219 struct ibmvnic_adapter *adapter;
6220 struct net_device *netdev;
6221 unsigned char *mac_addr_p;
6222 unsigned long flags;
6223 bool init_success;
6224 int rc;
6225
6226 dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
6227 dev->unit_address);
6228
6229 mac_addr_p = (unsigned char *)vio_get_attribute(dev,
6230 VETH_MAC_ADDR, NULL);
6231 if (!mac_addr_p) {
6232 dev_err(&dev->dev,
6233 "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
6234 __FILE__, __LINE__);
6235 return 0;
6236 }
6237
6238 netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
6239 IBMVNIC_MAX_QUEUES);
6240 if (!netdev)
6241 return -ENOMEM;
6242
6243 adapter = netdev_priv(netdev);
6244 adapter->state = VNIC_PROBING;
6245 dev_set_drvdata(&dev->dev, netdev);
6246 adapter->vdev = dev;
6247 adapter->netdev = netdev;
6248 adapter->login_pending = false;
6249 memset(&adapter->map_ids, 0, sizeof(adapter->map_ids));
6250 /* map_ids start at 1, so ensure map_id 0 is always "in-use" */
6251 bitmap_set(adapter->map_ids, 0, 1);
6252
6253 ether_addr_copy(adapter->mac_addr, mac_addr_p);
6254 eth_hw_addr_set(netdev, adapter->mac_addr);
6255 netdev->irq = dev->irq;
6256 netdev->netdev_ops = &ibmvnic_netdev_ops;
6257 netdev->ethtool_ops = &ibmvnic_ethtool_ops;
6258 SET_NETDEV_DEV(netdev, &dev->dev);
6259
6260 INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
6261 INIT_DELAYED_WORK(&adapter->ibmvnic_delayed_reset,
6262 __ibmvnic_delayed_reset);
6263 INIT_LIST_HEAD(&adapter->rwi_list);
6264 spin_lock_init(&adapter->rwi_lock);
6265 spin_lock_init(&adapter->state_lock);
6266 mutex_init(&adapter->fw_lock);
6267 init_completion(&adapter->probe_done);
6268 init_completion(&adapter->init_done);
6269 init_completion(&adapter->fw_done);
6270 init_completion(&adapter->reset_done);
6271 init_completion(&adapter->stats_done);
6272 clear_bit(0, &adapter->resetting);
6273 adapter->prev_rx_buf_sz = 0;
6274 adapter->prev_mtu = 0;
6275
6276 init_success = false;
6277 do {
6278 reinit_init_done(adapter);
6279
6280 /* clear any failovers we got in the previous pass
6281 * since we are reinitializing the CRQ
6282 */
6283 adapter->failover_pending = false;
6284
6285 /* If we had already initialized CRQ, we may have one or
6286 * more resets queued already. Discard those and release
6287 * the CRQ before initializing the CRQ again.
6288 */
6289 release_crq_queue(adapter);
6290
6291 /* Since we are still in PROBING state, __ibmvnic_reset()
6292 * will not access the ->rwi_list and since we released CRQ,
6293 * we won't get _new_ transport events. But there maybe an
6294 * ongoing ibmvnic_reset() call. So serialize access to
6295 * rwi_list. If we win the race, ibvmnic_reset() could add
6296 * a reset after we purged but thats ok - we just may end
6297 * up with an extra reset (i.e similar to having two or more
6298 * resets in the queue at once).
6299 * CHECK.
6300 */
6301 spin_lock_irqsave(&adapter->rwi_lock, flags);
6302 flush_reset_queue(adapter);
6303 spin_unlock_irqrestore(&adapter->rwi_lock, flags);
6304
6305 rc = init_crq_queue(adapter);
6306 if (rc) {
6307 dev_err(&dev->dev, "Couldn't initialize crq. rc=%d\n",
6308 rc);
6309 goto ibmvnic_init_fail;
6310 }
6311
6312 rc = ibmvnic_reset_init(adapter, false);
6313 } while (rc == -EAGAIN);
6314
6315 /* We are ignoring the error from ibmvnic_reset_init() assuming that the
6316 * partner is not ready. CRQ is not active. When the partner becomes
6317 * ready, we will do the passive init reset.
6318 */
6319
6320 if (!rc)
6321 init_success = true;
6322
6323 rc = init_stats_buffers(adapter);
6324 if (rc)
6325 goto ibmvnic_init_fail;
6326
6327 rc = init_stats_token(adapter);
6328 if (rc)
6329 goto ibmvnic_stats_fail;
6330
6331 rc = device_create_file(&dev->dev, &dev_attr_failover);
6332 if (rc)
6333 goto ibmvnic_dev_file_err;
6334
6335 netif_carrier_off(netdev);
6336
6337 if (init_success) {
6338 adapter->state = VNIC_PROBED;
6339 netdev->mtu = adapter->req_mtu - ETH_HLEN;
6340 netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
6341 netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
6342 } else {
6343 adapter->state = VNIC_DOWN;
6344 }
6345
6346 adapter->wait_for_reset = false;
6347 adapter->last_reset_time = jiffies;
6348
6349 rc = register_netdev(netdev);
6350 if (rc) {
6351 dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
6352 goto ibmvnic_register_fail;
6353 }
6354 dev_info(&dev->dev, "ibmvnic registered\n");
6355
6356 rc = ibmvnic_cpu_notif_add(adapter);
6357 if (rc) {
6358 netdev_err(netdev, "Registering cpu notifier failed\n");
6359 goto cpu_notif_add_failed;
6360 }
6361
6362 complete(&adapter->probe_done);
6363
6364 return 0;
6365
6366cpu_notif_add_failed:
6367 unregister_netdev(netdev);
6368
6369ibmvnic_register_fail:
6370 device_remove_file(&dev->dev, &dev_attr_failover);
6371
6372ibmvnic_dev_file_err:
6373 release_stats_token(adapter);
6374
6375ibmvnic_stats_fail:
6376 release_stats_buffers(adapter);
6377
6378ibmvnic_init_fail:
6379 release_sub_crqs(adapter, 1);
6380 release_crq_queue(adapter);
6381
6382 /* cleanup worker thread after releasing CRQ so we don't get
6383 * transport events (i.e new work items for the worker thread).
6384 */
6385 adapter->state = VNIC_REMOVING;
6386 complete(&adapter->probe_done);
6387 flush_work(&adapter->ibmvnic_reset);
6388 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
6389
6390 flush_reset_queue(adapter);
6391
6392 mutex_destroy(&adapter->fw_lock);
6393 free_netdev(netdev);
6394
6395 return rc;
6396}
6397
6398static void ibmvnic_remove(struct vio_dev *dev)
6399{
6400 struct net_device *netdev = dev_get_drvdata(&dev->dev);
6401 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
6402 unsigned long flags;
6403
6404 spin_lock_irqsave(&adapter->state_lock, flags);
6405
6406 /* If ibmvnic_reset() is scheduling a reset, wait for it to
6407 * finish. Then, set the state to REMOVING to prevent it from
6408 * scheduling any more work and to have reset functions ignore
6409 * any resets that have already been scheduled. Drop the lock
6410 * after setting state, so __ibmvnic_reset() which is called
6411 * from the flush_work() below, can make progress.
6412 */
6413 spin_lock(&adapter->rwi_lock);
6414 adapter->state = VNIC_REMOVING;
6415 spin_unlock(&adapter->rwi_lock);
6416
6417 spin_unlock_irqrestore(&adapter->state_lock, flags);
6418
6419 ibmvnic_cpu_notif_remove(adapter);
6420
6421 flush_work(&adapter->ibmvnic_reset);
6422 flush_delayed_work(&adapter->ibmvnic_delayed_reset);
6423
6424 rtnl_lock();
6425 unregister_netdevice(netdev);
6426
6427 release_resources(adapter);
6428 release_rx_pools(adapter);
6429 release_tx_pools(adapter);
6430 release_sub_crqs(adapter, 1);
6431 release_crq_queue(adapter);
6432
6433 release_stats_token(adapter);
6434 release_stats_buffers(adapter);
6435
6436 adapter->state = VNIC_REMOVED;
6437
6438 rtnl_unlock();
6439 mutex_destroy(&adapter->fw_lock);
6440 device_remove_file(&dev->dev, &dev_attr_failover);
6441 free_netdev(netdev);
6442 dev_set_drvdata(&dev->dev, NULL);
6443}
6444
6445static ssize_t failover_store(struct device *dev, struct device_attribute *attr,
6446 const char *buf, size_t count)
6447{
6448 struct net_device *netdev = dev_get_drvdata(dev);
6449 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
6450 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
6451 __be64 session_token;
6452 long rc;
6453
6454 if (!sysfs_streq(buf, "1"))
6455 return -EINVAL;
6456
6457 rc = plpar_hcall(H_VIOCTL, retbuf, adapter->vdev->unit_address,
6458 H_GET_SESSION_TOKEN, 0, 0, 0);
6459 if (rc) {
6460 netdev_err(netdev, "Couldn't retrieve session token, rc %ld\n",
6461 rc);
6462 goto last_resort;
6463 }
6464
6465 session_token = (__be64)retbuf[0];
6466 netdev_dbg(netdev, "Initiating client failover, session id %llx\n",
6467 be64_to_cpu(session_token));
6468 rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
6469 H_SESSION_ERR_DETECTED, session_token, 0, 0);
6470 if (rc) {
6471 netdev_err(netdev,
6472 "H_VIOCTL initiated failover failed, rc %ld\n",
6473 rc);
6474 goto last_resort;
6475 }
6476
6477 return count;
6478
6479last_resort:
6480 netdev_dbg(netdev, "Trying to send CRQ_CMD, the last resort\n");
6481 ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
6482
6483 return count;
6484}
6485static DEVICE_ATTR_WO(failover);
6486
6487static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
6488{
6489 struct net_device *netdev = dev_get_drvdata(&vdev->dev);
6490 struct ibmvnic_adapter *adapter;
6491 struct iommu_table *tbl;
6492 unsigned long ret = 0;
6493 int i;
6494
6495 tbl = get_iommu_table_base(&vdev->dev);
6496
6497 /* netdev inits at probe time along with the structures we need below*/
6498 if (!netdev)
6499 return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
6500
6501 adapter = netdev_priv(netdev);
6502
6503 ret += PAGE_SIZE; /* the crq message queue */
6504 ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
6505
6506 for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
6507 ret += 4 * PAGE_SIZE; /* the scrq message queue */
6508
6509 for (i = 0; i < adapter->num_active_rx_pools; i++)
6510 ret += adapter->rx_pool[i].size *
6511 IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
6512
6513 return ret;
6514}
6515
6516static int ibmvnic_resume(struct device *dev)
6517{
6518 struct net_device *netdev = dev_get_drvdata(dev);
6519 struct ibmvnic_adapter *adapter = netdev_priv(netdev);
6520
6521 if (adapter->state != VNIC_OPEN)
6522 return 0;
6523
6524 tasklet_schedule(&adapter->tasklet);
6525
6526 return 0;
6527}
6528
6529static const struct vio_device_id ibmvnic_device_table[] = {
6530 {"network", "IBM,vnic"},
6531 {"", "" }
6532};
6533MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
6534
6535static const struct dev_pm_ops ibmvnic_pm_ops = {
6536 .resume = ibmvnic_resume
6537};
6538
6539static struct vio_driver ibmvnic_driver = {
6540 .id_table = ibmvnic_device_table,
6541 .probe = ibmvnic_probe,
6542 .remove = ibmvnic_remove,
6543 .get_desired_dma = ibmvnic_get_desired_dma,
6544 .name = ibmvnic_driver_name,
6545 .pm = &ibmvnic_pm_ops,
6546};
6547
6548/* module functions */
6549static int __init ibmvnic_module_init(void)
6550{
6551 int ret;
6552
6553 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/ibmvnic:online",
6554 ibmvnic_cpu_online,
6555 ibmvnic_cpu_down_prep);
6556 if (ret < 0)
6557 goto out;
6558 ibmvnic_online = ret;
6559 ret = cpuhp_setup_state_multi(CPUHP_IBMVNIC_DEAD, "net/ibmvnic:dead",
6560 NULL, ibmvnic_cpu_dead);
6561 if (ret)
6562 goto err_dead;
6563
6564 ret = vio_register_driver(&ibmvnic_driver);
6565 if (ret)
6566 goto err_vio_register;
6567
6568 pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
6569 IBMVNIC_DRIVER_VERSION);
6570
6571 return 0;
6572err_vio_register:
6573 cpuhp_remove_multi_state(CPUHP_IBMVNIC_DEAD);
6574err_dead:
6575 cpuhp_remove_multi_state(ibmvnic_online);
6576out:
6577 return ret;
6578}
6579
6580static void __exit ibmvnic_module_exit(void)
6581{
6582 vio_unregister_driver(&ibmvnic_driver);
6583 cpuhp_remove_multi_state(CPUHP_IBMVNIC_DEAD);
6584 cpuhp_remove_multi_state(ibmvnic_online);
6585}
6586
6587module_init(ibmvnic_module_init);
6588module_exit(ibmvnic_module_exit);