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