1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2006-2012 Solarflare Communications Inc.
6 */
7
8 #include <linux/netdevice.h>
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/pci.h>
13 #include <linux/ethtool.h>
14 #include <linux/ip.h>
15 #include <linux/in.h>
16 #include <linux/udp.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/slab.h>
19 #include "net_driver.h"
20 #include "efx.h"
21 #include "efx_common.h"
22 #include "efx_channels.h"
23 #include "nic.h"
24 #include "mcdi_port_common.h"
25 #include "selftest.h"
26 #include "workarounds.h"
27
28 /* IRQ latency can be enormous because:
29 * - All IRQs may be disabled on a CPU for a *long* time by e.g. a
30 * slow serial console or an old IDE driver doing error recovery
31 * - The PREEMPT_RT patches mostly deal with this, but also allow a
32 * tasklet or normal task to be given higher priority than our IRQ
33 * threads
34 * Try to avoid blaming the hardware for this.
35 */
36 #define IRQ_TIMEOUT HZ
37
38 /*
39 * Loopback test packet structure
40 *
41 * The self-test should stress every RSS vector, and unfortunately
42 * Falcon only performs RSS on TCP/UDP packets.
43 */
44 struct efx_loopback_payload {
45 char pad[2]; /* Ensures ip is 4-byte aligned */
46 struct_group_attr(packet, __packed,
47 struct ethhdr header;
48 struct iphdr ip;
49 struct udphdr udp;
50 __be16 iteration;
51 char msg[64];
52 );
53 } __packed __aligned(4);
54 #define EFX_LOOPBACK_PAYLOAD_LEN \
55 sizeof_field(struct efx_loopback_payload, packet)
56
57 /* Loopback test source MAC address */
58 static const u8 payload_source[ETH_ALEN] __aligned(2) = {
59 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b,
60 };
61
62 static const char payload_msg[] =
63 "Hello world! This is an Efx loopback test in progress!";
64
65 /* Interrupt mode names */
66 static const unsigned int efx_siena_interrupt_mode_max = EFX_INT_MODE_MAX;
67 static const char *const efx_siena_interrupt_mode_names[] = {
68 [EFX_INT_MODE_MSIX] = "MSI-X",
69 [EFX_INT_MODE_MSI] = "MSI",
70 [EFX_INT_MODE_LEGACY] = "legacy",
71 };
72 #define INT_MODE(efx) \
73 STRING_TABLE_LOOKUP(efx->interrupt_mode, efx_siena_interrupt_mode)
74
75 /**
76 * struct efx_loopback_state - persistent state during a loopback selftest
77 * @flush: Drop all packets in efx_siena_loopback_rx_packet
78 * @packet_count: Number of packets being used in this test
79 * @skbs: An array of skbs transmitted
80 * @offload_csum: Checksums are being offloaded
81 * @rx_good: RX good packet count
82 * @rx_bad: RX bad packet count
83 * @payload: Payload used in tests
84 */
85 struct efx_loopback_state {
86 bool flush;
87 int packet_count;
88 struct sk_buff **skbs;
89 bool offload_csum;
90 atomic_t rx_good;
91 atomic_t rx_bad;
92 struct efx_loopback_payload payload;
93 };
94
95 /* How long to wait for all the packets to arrive (in ms) */
96 #define LOOPBACK_TIMEOUT_MS 1000
97
98 /**************************************************************************
99 *
100 * MII, NVRAM and register tests
101 *
102 **************************************************************************/
103
efx_test_phy_alive(struct efx_nic * efx,struct efx_self_tests * tests)104 static int efx_test_phy_alive(struct efx_nic *efx, struct efx_self_tests *tests)
105 {
106 int rc = 0;
107
108 rc = efx_siena_mcdi_phy_test_alive(efx);
109 tests->phy_alive = rc ? -1 : 1;
110
111 return rc;
112 }
113
efx_test_nvram(struct efx_nic * efx,struct efx_self_tests * tests)114 static int efx_test_nvram(struct efx_nic *efx, struct efx_self_tests *tests)
115 {
116 int rc = 0;
117
118 if (efx->type->test_nvram) {
119 rc = efx->type->test_nvram(efx);
120 if (rc == -EPERM)
121 rc = 0;
122 else
123 tests->nvram = rc ? -1 : 1;
124 }
125
126 return rc;
127 }
128
129 /**************************************************************************
130 *
131 * Interrupt and event queue testing
132 *
133 **************************************************************************/
134
135 /* Test generation and receipt of interrupts */
efx_test_interrupts(struct efx_nic * efx,struct efx_self_tests * tests)136 static int efx_test_interrupts(struct efx_nic *efx,
137 struct efx_self_tests *tests)
138 {
139 unsigned long timeout, wait;
140 int cpu;
141 int rc;
142
143 netif_dbg(efx, drv, efx->net_dev, "testing interrupts\n");
144 tests->interrupt = -1;
145
146 rc = efx_siena_irq_test_start(efx);
147 if (rc == -ENOTSUPP) {
148 netif_dbg(efx, drv, efx->net_dev,
149 "direct interrupt testing not supported\n");
150 tests->interrupt = 0;
151 return 0;
152 }
153
154 timeout = jiffies + IRQ_TIMEOUT;
155 wait = 1;
156
157 /* Wait for arrival of test interrupt. */
158 netif_dbg(efx, drv, efx->net_dev, "waiting for test interrupt\n");
159 do {
160 schedule_timeout_uninterruptible(wait);
161 cpu = efx_nic_irq_test_irq_cpu(efx);
162 if (cpu >= 0)
163 goto success;
164 wait *= 2;
165 } while (time_before(jiffies, timeout));
166
167 netif_err(efx, drv, efx->net_dev, "timed out waiting for interrupt\n");
168 return -ETIMEDOUT;
169
170 success:
171 netif_dbg(efx, drv, efx->net_dev, "%s test interrupt seen on CPU%d\n",
172 INT_MODE(efx), cpu);
173 tests->interrupt = 1;
174 return 0;
175 }
176
177 /* Test generation and receipt of interrupting events */
efx_test_eventq_irq(struct efx_nic * efx,struct efx_self_tests * tests)178 static int efx_test_eventq_irq(struct efx_nic *efx,
179 struct efx_self_tests *tests)
180 {
181 struct efx_channel *channel;
182 unsigned int read_ptr[EFX_MAX_CHANNELS];
183 unsigned long napi_ran = 0, dma_pend = 0, int_pend = 0;
184 unsigned long timeout, wait;
185
186 BUILD_BUG_ON(EFX_MAX_CHANNELS > BITS_PER_LONG);
187
188 efx_for_each_channel(channel, efx) {
189 read_ptr[channel->channel] = channel->eventq_read_ptr;
190 set_bit(channel->channel, &dma_pend);
191 set_bit(channel->channel, &int_pend);
192 efx_siena_event_test_start(channel);
193 }
194
195 timeout = jiffies + IRQ_TIMEOUT;
196 wait = 1;
197
198 /* Wait for arrival of interrupts. NAPI processing may or may
199 * not complete in time, but we can cope in any case.
200 */
201 do {
202 schedule_timeout_uninterruptible(wait);
203
204 efx_for_each_channel(channel, efx) {
205 efx_siena_stop_eventq(channel);
206 if (channel->eventq_read_ptr !=
207 read_ptr[channel->channel]) {
208 set_bit(channel->channel, &napi_ran);
209 clear_bit(channel->channel, &dma_pend);
210 clear_bit(channel->channel, &int_pend);
211 } else {
212 if (efx_siena_event_present(channel))
213 clear_bit(channel->channel, &dma_pend);
214 if (efx_nic_event_test_irq_cpu(channel) >= 0)
215 clear_bit(channel->channel, &int_pend);
216 }
217 efx_siena_start_eventq(channel);
218 }
219
220 wait *= 2;
221 } while ((dma_pend || int_pend) && time_before(jiffies, timeout));
222
223 efx_for_each_channel(channel, efx) {
224 bool dma_seen = !test_bit(channel->channel, &dma_pend);
225 bool int_seen = !test_bit(channel->channel, &int_pend);
226
227 tests->eventq_dma[channel->channel] = dma_seen ? 1 : -1;
228 tests->eventq_int[channel->channel] = int_seen ? 1 : -1;
229
230 if (dma_seen && int_seen) {
231 netif_dbg(efx, drv, efx->net_dev,
232 "channel %d event queue passed (with%s NAPI)\n",
233 channel->channel,
234 test_bit(channel->channel, &napi_ran) ?
235 "" : "out");
236 } else {
237 /* Report failure and whether either interrupt or DMA
238 * worked
239 */
240 netif_err(efx, drv, efx->net_dev,
241 "channel %d timed out waiting for event queue\n",
242 channel->channel);
243 if (int_seen)
244 netif_err(efx, drv, efx->net_dev,
245 "channel %d saw interrupt "
246 "during event queue test\n",
247 channel->channel);
248 if (dma_seen)
249 netif_err(efx, drv, efx->net_dev,
250 "channel %d event was generated, but "
251 "failed to trigger an interrupt\n",
252 channel->channel);
253 }
254 }
255
256 return (dma_pend || int_pend) ? -ETIMEDOUT : 0;
257 }
258
efx_test_phy(struct efx_nic * efx,struct efx_self_tests * tests,unsigned flags)259 static int efx_test_phy(struct efx_nic *efx, struct efx_self_tests *tests,
260 unsigned flags)
261 {
262 int rc;
263
264 mutex_lock(&efx->mac_lock);
265 rc = efx_siena_mcdi_phy_run_tests(efx, tests->phy_ext, flags);
266 mutex_unlock(&efx->mac_lock);
267 if (rc == -EPERM)
268 rc = 0;
269 else
270 netif_info(efx, drv, efx->net_dev,
271 "%s phy selftest\n", rc ? "Failed" : "Passed");
272
273 return rc;
274 }
275
276 /**************************************************************************
277 *
278 * Loopback testing
279 * NB Only one loopback test can be executing concurrently.
280 *
281 **************************************************************************/
282
283 /* Loopback test RX callback
284 * This is called for each received packet during loopback testing.
285 */
efx_siena_loopback_rx_packet(struct efx_nic * efx,const char * buf_ptr,int pkt_len)286 void efx_siena_loopback_rx_packet(struct efx_nic *efx,
287 const char *buf_ptr, int pkt_len)
288 {
289 struct efx_loopback_state *state = efx->loopback_selftest;
290 struct efx_loopback_payload received;
291 struct efx_loopback_payload *payload;
292
293 BUG_ON(!buf_ptr);
294
295 /* If we are just flushing, then drop the packet */
296 if ((state == NULL) || state->flush)
297 return;
298
299 payload = &state->payload;
300
301 memcpy(&received.packet, buf_ptr,
302 min_t(int, pkt_len, EFX_LOOPBACK_PAYLOAD_LEN));
303 received.ip.saddr = payload->ip.saddr;
304 if (state->offload_csum)
305 received.ip.check = payload->ip.check;
306
307 /* Check that header exists */
308 if (pkt_len < sizeof(received.header)) {
309 netif_err(efx, drv, efx->net_dev,
310 "saw runt RX packet (length %d) in %s loopback "
311 "test\n", pkt_len, LOOPBACK_MODE(efx));
312 goto err;
313 }
314
315 /* Check that the ethernet header exists */
316 if (memcmp(&received.header, &payload->header, ETH_HLEN) != 0) {
317 netif_err(efx, drv, efx->net_dev,
318 "saw non-loopback RX packet in %s loopback test\n",
319 LOOPBACK_MODE(efx));
320 goto err;
321 }
322
323 /* Check packet length */
324 if (pkt_len != EFX_LOOPBACK_PAYLOAD_LEN) {
325 netif_err(efx, drv, efx->net_dev,
326 "saw incorrect RX packet length %d (wanted %d) in "
327 "%s loopback test\n", pkt_len,
328 (int)EFX_LOOPBACK_PAYLOAD_LEN, LOOPBACK_MODE(efx));
329 goto err;
330 }
331
332 /* Check that IP header matches */
333 if (memcmp(&received.ip, &payload->ip, sizeof(payload->ip)) != 0) {
334 netif_err(efx, drv, efx->net_dev,
335 "saw corrupted IP header in %s loopback test\n",
336 LOOPBACK_MODE(efx));
337 goto err;
338 }
339
340 /* Check that msg and padding matches */
341 if (memcmp(&received.msg, &payload->msg, sizeof(received.msg)) != 0) {
342 netif_err(efx, drv, efx->net_dev,
343 "saw corrupted RX packet in %s loopback test\n",
344 LOOPBACK_MODE(efx));
345 goto err;
346 }
347
348 /* Check that iteration matches */
349 if (received.iteration != payload->iteration) {
350 netif_err(efx, drv, efx->net_dev,
351 "saw RX packet from iteration %d (wanted %d) in "
352 "%s loopback test\n", ntohs(received.iteration),
353 ntohs(payload->iteration), LOOPBACK_MODE(efx));
354 goto err;
355 }
356
357 /* Increase correct RX count */
358 netif_vdbg(efx, drv, efx->net_dev,
359 "got loopback RX in %s loopback test\n", LOOPBACK_MODE(efx));
360
361 atomic_inc(&state->rx_good);
362 return;
363
364 err:
365 #ifdef DEBUG
366 if (atomic_read(&state->rx_bad) == 0) {
367 netif_err(efx, drv, efx->net_dev, "received packet:\n");
368 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
369 buf_ptr, pkt_len, 0);
370 netif_err(efx, drv, efx->net_dev, "expected packet:\n");
371 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1,
372 &state->payload.packet, EFX_LOOPBACK_PAYLOAD_LEN,
373 0);
374 }
375 #endif
376 atomic_inc(&state->rx_bad);
377 }
378
379 /* Initialise an efx_siena_selftest_state for a new iteration */
efx_iterate_state(struct efx_nic * efx)380 static void efx_iterate_state(struct efx_nic *efx)
381 {
382 struct efx_loopback_state *state = efx->loopback_selftest;
383 struct net_device *net_dev = efx->net_dev;
384 struct efx_loopback_payload *payload = &state->payload;
385
386 /* Initialise the layerII header */
387 ether_addr_copy((u8 *)&payload->header.h_dest, net_dev->dev_addr);
388 ether_addr_copy((u8 *)&payload->header.h_source, payload_source);
389 payload->header.h_proto = htons(ETH_P_IP);
390
391 /* saddr set later and used as incrementing count */
392 payload->ip.daddr = htonl(INADDR_LOOPBACK);
393 payload->ip.ihl = 5;
394 payload->ip.check = (__force __sum16) htons(0xdead);
395 payload->ip.tot_len = htons(sizeof(*payload) -
396 offsetof(struct efx_loopback_payload, ip));
397 payload->ip.version = IPVERSION;
398 payload->ip.protocol = IPPROTO_UDP;
399
400 /* Initialise udp header */
401 payload->udp.source = 0;
402 payload->udp.len = htons(sizeof(*payload) -
403 offsetof(struct efx_loopback_payload, udp));
404 payload->udp.check = 0; /* checksum ignored */
405
406 /* Fill out payload */
407 payload->iteration = htons(ntohs(payload->iteration) + 1);
408 memcpy(&payload->msg, payload_msg, sizeof(payload_msg));
409
410 /* Fill out remaining state members */
411 atomic_set(&state->rx_good, 0);
412 atomic_set(&state->rx_bad, 0);
413 smp_wmb();
414 }
415
efx_begin_loopback(struct efx_tx_queue * tx_queue)416 static int efx_begin_loopback(struct efx_tx_queue *tx_queue)
417 {
418 struct efx_nic *efx = tx_queue->efx;
419 struct efx_loopback_state *state = efx->loopback_selftest;
420 struct efx_loopback_payload *payload;
421 struct sk_buff *skb;
422 int i;
423 netdev_tx_t rc;
424
425 /* Transmit N copies of buffer */
426 for (i = 0; i < state->packet_count; i++) {
427 /* Allocate an skb, holding an extra reference for
428 * transmit completion counting */
429 skb = alloc_skb(sizeof(state->payload), GFP_KERNEL);
430 if (!skb)
431 return -ENOMEM;
432 state->skbs[i] = skb;
433 skb_get(skb);
434
435 /* Copy the payload in, incrementing the source address to
436 * exercise the rss vectors */
437 payload = skb_put(skb, sizeof(state->payload));
438 memcpy(payload, &state->payload, sizeof(state->payload));
439 payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2));
440 /* Strip off the leading padding */
441 skb_pull(skb, offsetof(struct efx_loopback_payload, header));
442 /* Strip off the trailing padding */
443 skb_trim(skb, EFX_LOOPBACK_PAYLOAD_LEN);
444
445 /* Ensure everything we've written is visible to the
446 * interrupt handler. */
447 smp_wmb();
448
449 netif_tx_lock_bh(efx->net_dev);
450 rc = efx_enqueue_skb(tx_queue, skb);
451 netif_tx_unlock_bh(efx->net_dev);
452
453 if (rc != NETDEV_TX_OK) {
454 netif_err(efx, drv, efx->net_dev,
455 "TX queue %d could not transmit packet %d of "
456 "%d in %s loopback test\n", tx_queue->label,
457 i + 1, state->packet_count,
458 LOOPBACK_MODE(efx));
459
460 /* Defer cleaning up the other skbs for the caller */
461 kfree_skb(skb);
462 return -EPIPE;
463 }
464 }
465
466 return 0;
467 }
468
efx_poll_loopback(struct efx_nic * efx)469 static int efx_poll_loopback(struct efx_nic *efx)
470 {
471 struct efx_loopback_state *state = efx->loopback_selftest;
472
473 return atomic_read(&state->rx_good) == state->packet_count;
474 }
475
efx_end_loopback(struct efx_tx_queue * tx_queue,struct efx_loopback_self_tests * lb_tests)476 static int efx_end_loopback(struct efx_tx_queue *tx_queue,
477 struct efx_loopback_self_tests *lb_tests)
478 {
479 struct efx_nic *efx = tx_queue->efx;
480 struct efx_loopback_state *state = efx->loopback_selftest;
481 struct sk_buff *skb;
482 int tx_done = 0, rx_good, rx_bad;
483 int i, rc = 0;
484
485 netif_tx_lock_bh(efx->net_dev);
486
487 /* Count the number of tx completions, and decrement the refcnt. Any
488 * skbs not already completed will be free'd when the queue is flushed */
489 for (i = 0; i < state->packet_count; i++) {
490 skb = state->skbs[i];
491 if (skb && !skb_shared(skb))
492 ++tx_done;
493 dev_kfree_skb(skb);
494 }
495
496 netif_tx_unlock_bh(efx->net_dev);
497
498 /* Check TX completion and received packet counts */
499 rx_good = atomic_read(&state->rx_good);
500 rx_bad = atomic_read(&state->rx_bad);
501 if (tx_done != state->packet_count) {
502 /* Don't free the skbs; they will be picked up on TX
503 * overflow or channel teardown.
504 */
505 netif_err(efx, drv, efx->net_dev,
506 "TX queue %d saw only %d out of an expected %d "
507 "TX completion events in %s loopback test\n",
508 tx_queue->label, tx_done, state->packet_count,
509 LOOPBACK_MODE(efx));
510 rc = -ETIMEDOUT;
511 /* Allow to fall through so we see the RX errors as well */
512 }
513
514 /* We may always be up to a flush away from our desired packet total */
515 if (rx_good != state->packet_count) {
516 netif_dbg(efx, drv, efx->net_dev,
517 "TX queue %d saw only %d out of an expected %d "
518 "received packets in %s loopback test\n",
519 tx_queue->label, rx_good, state->packet_count,
520 LOOPBACK_MODE(efx));
521 rc = -ETIMEDOUT;
522 /* Fall through */
523 }
524
525 /* Update loopback test structure */
526 lb_tests->tx_sent[tx_queue->label] += state->packet_count;
527 lb_tests->tx_done[tx_queue->label] += tx_done;
528 lb_tests->rx_good += rx_good;
529 lb_tests->rx_bad += rx_bad;
530
531 return rc;
532 }
533
534 static int
efx_test_loopback(struct efx_tx_queue * tx_queue,struct efx_loopback_self_tests * lb_tests)535 efx_test_loopback(struct efx_tx_queue *tx_queue,
536 struct efx_loopback_self_tests *lb_tests)
537 {
538 struct efx_nic *efx = tx_queue->efx;
539 struct efx_loopback_state *state = efx->loopback_selftest;
540 int i, begin_rc, end_rc;
541
542 for (i = 0; i < 3; i++) {
543 /* Determine how many packets to send */
544 state->packet_count = efx->txq_entries / 3;
545 state->packet_count = min(1 << (i << 2), state->packet_count);
546 state->skbs = kzalloc_objs(state->skbs[0], state->packet_count);
547 if (!state->skbs)
548 return -ENOMEM;
549 state->flush = false;
550
551 netif_dbg(efx, drv, efx->net_dev,
552 "TX queue %d (hw %d) testing %s loopback with %d packets\n",
553 tx_queue->label, tx_queue->queue, LOOPBACK_MODE(efx),
554 state->packet_count);
555
556 efx_iterate_state(efx);
557 begin_rc = efx_begin_loopback(tx_queue);
558
559 /* This will normally complete very quickly, but be
560 * prepared to wait much longer. */
561 msleep(1);
562 if (!efx_poll_loopback(efx)) {
563 msleep(LOOPBACK_TIMEOUT_MS);
564 efx_poll_loopback(efx);
565 }
566
567 end_rc = efx_end_loopback(tx_queue, lb_tests);
568 kfree(state->skbs);
569
570 if (begin_rc || end_rc) {
571 /* Wait a while to ensure there are no packets
572 * floating around after a failure. */
573 schedule_timeout_uninterruptible(HZ / 10);
574 return begin_rc ? begin_rc : end_rc;
575 }
576 }
577
578 netif_dbg(efx, drv, efx->net_dev,
579 "TX queue %d passed %s loopback test with a burst length "
580 "of %d packets\n", tx_queue->label, LOOPBACK_MODE(efx),
581 state->packet_count);
582
583 return 0;
584 }
585
586 /* Wait for link up. On Falcon, we would prefer to rely on efx_monitor, but
587 * any contention on the mac lock (via e.g. efx_mac_mcast_work) causes it
588 * to delay and retry. Therefore, it's safer to just poll directly. Wait
589 * for link up and any faults to dissipate. */
efx_wait_for_link(struct efx_nic * efx)590 static int efx_wait_for_link(struct efx_nic *efx)
591 {
592 struct efx_link_state *link_state = &efx->link_state;
593 int count, link_up_count = 0;
594 bool link_up;
595
596 for (count = 0; count < 40; count++) {
597 schedule_timeout_uninterruptible(HZ / 10);
598
599 if (efx->type->monitor != NULL) {
600 mutex_lock(&efx->mac_lock);
601 efx->type->monitor(efx);
602 mutex_unlock(&efx->mac_lock);
603 }
604
605 mutex_lock(&efx->mac_lock);
606 link_up = link_state->up;
607 if (link_up)
608 link_up = !efx->type->check_mac_fault(efx);
609 mutex_unlock(&efx->mac_lock);
610
611 if (link_up) {
612 if (++link_up_count == 2)
613 return 0;
614 } else {
615 link_up_count = 0;
616 }
617 }
618
619 return -ETIMEDOUT;
620 }
621
efx_test_loopbacks(struct efx_nic * efx,struct efx_self_tests * tests,unsigned int loopback_modes)622 static int efx_test_loopbacks(struct efx_nic *efx, struct efx_self_tests *tests,
623 unsigned int loopback_modes)
624 {
625 enum efx_loopback_mode mode;
626 struct efx_loopback_state *state;
627 struct efx_channel *channel =
628 efx_get_channel(efx, efx->tx_channel_offset);
629 struct efx_tx_queue *tx_queue;
630 int rc = 0;
631
632 /* Set the port loopback_selftest member. From this point on
633 * all received packets will be dropped. Mark the state as
634 * "flushing" so all inflight packets are dropped */
635 state = kzalloc_obj(*state);
636 if (state == NULL)
637 return -ENOMEM;
638 BUG_ON(efx->loopback_selftest);
639 state->flush = true;
640 efx->loopback_selftest = state;
641
642 /* Test all supported loopback modes */
643 for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
644 if (!(loopback_modes & (1 << mode)))
645 continue;
646
647 /* Move the port into the specified loopback mode. */
648 state->flush = true;
649 mutex_lock(&efx->mac_lock);
650 efx->loopback_mode = mode;
651 rc = __efx_siena_reconfigure_port(efx);
652 mutex_unlock(&efx->mac_lock);
653 if (rc) {
654 netif_err(efx, drv, efx->net_dev,
655 "unable to move into %s loopback\n",
656 LOOPBACK_MODE(efx));
657 goto out;
658 }
659
660 rc = efx_wait_for_link(efx);
661 if (rc) {
662 netif_err(efx, drv, efx->net_dev,
663 "loopback %s never came up\n",
664 LOOPBACK_MODE(efx));
665 goto out;
666 }
667
668 /* Test all enabled types of TX queue */
669 efx_for_each_channel_tx_queue(tx_queue, channel) {
670 state->offload_csum = (tx_queue->type &
671 EFX_TXQ_TYPE_OUTER_CSUM);
672 rc = efx_test_loopback(tx_queue,
673 &tests->loopback[mode]);
674 if (rc)
675 goto out;
676 }
677 }
678
679 out:
680 /* Remove the flush. The caller will remove the loopback setting */
681 state->flush = true;
682 efx->loopback_selftest = NULL;
683 wmb();
684 kfree(state);
685
686 if (rc == -EPERM)
687 rc = 0;
688
689 return rc;
690 }
691
692 /**************************************************************************
693 *
694 * Entry point
695 *
696 *************************************************************************/
697
efx_siena_selftest(struct efx_nic * efx,struct efx_self_tests * tests,unsigned int flags)698 int efx_siena_selftest(struct efx_nic *efx, struct efx_self_tests *tests,
699 unsigned int flags)
700 {
701 enum efx_loopback_mode loopback_mode = efx->loopback_mode;
702 int phy_mode = efx->phy_mode;
703 int rc_test = 0, rc_reset, rc;
704
705 efx_siena_selftest_async_cancel(efx);
706
707 /* Online (i.e. non-disruptive) testing
708 * This checks interrupt generation, event delivery and PHY presence. */
709
710 rc = efx_test_phy_alive(efx, tests);
711 if (rc && !rc_test)
712 rc_test = rc;
713
714 rc = efx_test_nvram(efx, tests);
715 if (rc && !rc_test)
716 rc_test = rc;
717
718 rc = efx_test_interrupts(efx, tests);
719 if (rc && !rc_test)
720 rc_test = rc;
721
722 rc = efx_test_eventq_irq(efx, tests);
723 if (rc && !rc_test)
724 rc_test = rc;
725
726 if (rc_test)
727 return rc_test;
728
729 if (!(flags & ETH_TEST_FL_OFFLINE))
730 return efx_test_phy(efx, tests, flags);
731
732 /* Offline (i.e. disruptive) testing
733 * This checks MAC and PHY loopback on the specified port. */
734
735 /* Detach the device so the kernel doesn't transmit during the
736 * loopback test and the watchdog timeout doesn't fire.
737 */
738 efx_device_detach_sync(efx);
739
740 if (efx->type->test_chip) {
741 rc_reset = efx->type->test_chip(efx, tests);
742 if (rc_reset) {
743 netif_err(efx, hw, efx->net_dev,
744 "Unable to recover from chip test\n");
745 efx_siena_schedule_reset(efx, RESET_TYPE_DISABLE);
746 return rc_reset;
747 }
748
749 if ((tests->memory < 0 || tests->registers < 0) && !rc_test)
750 rc_test = -EIO;
751 }
752
753 /* Ensure that the phy is powered and out of loopback
754 * for the bist and loopback tests */
755 mutex_lock(&efx->mac_lock);
756 efx->phy_mode &= ~PHY_MODE_LOW_POWER;
757 efx->loopback_mode = LOOPBACK_NONE;
758 __efx_siena_reconfigure_port(efx);
759 mutex_unlock(&efx->mac_lock);
760
761 rc = efx_test_phy(efx, tests, flags);
762 if (rc && !rc_test)
763 rc_test = rc;
764
765 rc = efx_test_loopbacks(efx, tests, efx->loopback_modes);
766 if (rc && !rc_test)
767 rc_test = rc;
768
769 /* restore the PHY to the previous state */
770 mutex_lock(&efx->mac_lock);
771 efx->phy_mode = phy_mode;
772 efx->loopback_mode = loopback_mode;
773 __efx_siena_reconfigure_port(efx);
774 mutex_unlock(&efx->mac_lock);
775
776 efx_device_attach_if_not_resetting(efx);
777
778 return rc_test;
779 }
780
efx_siena_selftest_async_start(struct efx_nic * efx)781 void efx_siena_selftest_async_start(struct efx_nic *efx)
782 {
783 struct efx_channel *channel;
784
785 efx_for_each_channel(channel, efx)
786 efx_siena_event_test_start(channel);
787 schedule_delayed_work(&efx->selftest_work, IRQ_TIMEOUT);
788 }
789
efx_siena_selftest_async_cancel(struct efx_nic * efx)790 void efx_siena_selftest_async_cancel(struct efx_nic *efx)
791 {
792 cancel_delayed_work_sync(&efx->selftest_work);
793 }
794
efx_siena_selftest_async_work(struct work_struct * data)795 static void efx_siena_selftest_async_work(struct work_struct *data)
796 {
797 struct efx_nic *efx = container_of(data, struct efx_nic,
798 selftest_work.work);
799 struct efx_channel *channel;
800 int cpu;
801
802 efx_for_each_channel(channel, efx) {
803 cpu = efx_nic_event_test_irq_cpu(channel);
804 if (cpu < 0)
805 netif_err(efx, ifup, efx->net_dev,
806 "channel %d failed to trigger an interrupt\n",
807 channel->channel);
808 else
809 netif_dbg(efx, ifup, efx->net_dev,
810 "channel %d triggered interrupt on CPU %d\n",
811 channel->channel, cpu);
812 }
813 }
814
efx_siena_selftest_async_init(struct efx_nic * efx)815 void efx_siena_selftest_async_init(struct efx_nic *efx)
816 {
817 INIT_DELAYED_WORK(&efx->selftest_work, efx_siena_selftest_async_work);
818 }
819