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