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