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