Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Driver for Solarflare Solarstorm network controllers and boards |
| 3 | * Copyright 2005-2006 Fen Systems Ltd. |
| 4 | * Copyright 2006-2008 Solarflare Communications Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation, incorporated herein by reference. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/netdevice.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/kernel_stat.h> |
| 15 | #include <linux/pci.h> |
| 16 | #include <linux/ethtool.h> |
| 17 | #include <linux/ip.h> |
| 18 | #include <linux/in.h> |
| 19 | #include <linux/udp.h> |
| 20 | #include <linux/rtnetlink.h> |
| 21 | #include <asm/io.h> |
| 22 | #include "net_driver.h" |
| 23 | #include "ethtool.h" |
| 24 | #include "efx.h" |
| 25 | #include "falcon.h" |
| 26 | #include "selftest.h" |
| 27 | #include "boards.h" |
| 28 | #include "workarounds.h" |
| 29 | #include "mac.h" |
| 30 | |
| 31 | /* |
| 32 | * Loopback test packet structure |
| 33 | * |
| 34 | * The self-test should stress every RSS vector, and unfortunately |
| 35 | * Falcon only performs RSS on TCP/UDP packets. |
| 36 | */ |
| 37 | struct efx_loopback_payload { |
| 38 | struct ethhdr header; |
| 39 | struct iphdr ip; |
| 40 | struct udphdr udp; |
| 41 | __be16 iteration; |
| 42 | const char msg[64]; |
| 43 | } __attribute__ ((packed)); |
| 44 | |
| 45 | /* Loopback test source MAC address */ |
| 46 | static const unsigned char payload_source[ETH_ALEN] = { |
| 47 | 0x00, 0x0f, 0x53, 0x1b, 0x1b, 0x1b, |
| 48 | }; |
| 49 | |
| 50 | static const char *payload_msg = |
| 51 | "Hello world! This is an Efx loopback test in progress!"; |
| 52 | |
| 53 | /** |
| 54 | * efx_selftest_state - persistent state during a selftest |
| 55 | * @flush: Drop all packets in efx_loopback_rx_packet |
| 56 | * @packet_count: Number of packets being used in this test |
| 57 | * @skbs: An array of skbs transmitted |
| 58 | * @rx_good: RX good packet count |
| 59 | * @rx_bad: RX bad packet count |
| 60 | * @payload: Payload used in tests |
| 61 | */ |
| 62 | struct efx_selftest_state { |
| 63 | int flush; |
| 64 | int packet_count; |
| 65 | struct sk_buff **skbs; |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 66 | |
| 67 | /* Checksums are being offloaded */ |
| 68 | int offload_csum; |
| 69 | |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 70 | atomic_t rx_good; |
| 71 | atomic_t rx_bad; |
| 72 | struct efx_loopback_payload payload; |
| 73 | }; |
| 74 | |
| 75 | /************************************************************************** |
| 76 | * |
| 77 | * Configurable values |
| 78 | * |
| 79 | **************************************************************************/ |
| 80 | |
| 81 | /* Level of loopback testing |
| 82 | * |
| 83 | * The maximum packet burst length is 16**(n-1), i.e. |
| 84 | * |
| 85 | * - Level 0 : no packets |
| 86 | * - Level 1 : 1 packet |
| 87 | * - Level 2 : 17 packets (1 * 1 packet, 1 * 16 packets) |
| 88 | * - Level 3 : 273 packets (1 * 1 packet, 1 * 16 packet, 1 * 256 packets) |
| 89 | * |
| 90 | */ |
| 91 | static unsigned int loopback_test_level = 3; |
| 92 | |
| 93 | /************************************************************************** |
| 94 | * |
| 95 | * Interrupt and event queue testing |
| 96 | * |
| 97 | **************************************************************************/ |
| 98 | |
| 99 | /* Test generation and receipt of interrupts */ |
| 100 | static int efx_test_interrupts(struct efx_nic *efx, |
| 101 | struct efx_self_tests *tests) |
| 102 | { |
| 103 | struct efx_channel *channel; |
| 104 | |
| 105 | EFX_LOG(efx, "testing interrupts\n"); |
| 106 | tests->interrupt = -1; |
| 107 | |
| 108 | /* Reset interrupt flag */ |
| 109 | efx->last_irq_cpu = -1; |
| 110 | smp_wmb(); |
| 111 | |
| 112 | /* ACK each interrupting event queue. Receiving an interrupt due to |
| 113 | * traffic before a test event is raised is considered a pass */ |
| 114 | efx_for_each_channel_with_interrupt(channel, efx) { |
| 115 | if (channel->work_pending) |
| 116 | efx_process_channel_now(channel); |
| 117 | if (efx->last_irq_cpu >= 0) |
| 118 | goto success; |
| 119 | } |
| 120 | |
| 121 | falcon_generate_interrupt(efx); |
| 122 | |
| 123 | /* Wait for arrival of test interrupt. */ |
| 124 | EFX_LOG(efx, "waiting for test interrupt\n"); |
| 125 | schedule_timeout_uninterruptible(HZ / 10); |
| 126 | if (efx->last_irq_cpu >= 0) |
| 127 | goto success; |
| 128 | |
| 129 | EFX_ERR(efx, "timed out waiting for interrupt\n"); |
| 130 | return -ETIMEDOUT; |
| 131 | |
| 132 | success: |
| 133 | EFX_LOG(efx, "test interrupt (mode %d) seen on CPU%d\n", |
| 134 | efx->interrupt_mode, efx->last_irq_cpu); |
| 135 | tests->interrupt = 1; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* Test generation and receipt of non-interrupting events */ |
| 140 | static int efx_test_eventq(struct efx_channel *channel, |
| 141 | struct efx_self_tests *tests) |
| 142 | { |
| 143 | unsigned int magic; |
| 144 | |
| 145 | /* Channel specific code, limited to 20 bits */ |
| 146 | magic = (0x00010150 + channel->channel); |
| 147 | EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n", |
| 148 | channel->channel, magic); |
| 149 | |
| 150 | tests->eventq_dma[channel->channel] = -1; |
| 151 | tests->eventq_int[channel->channel] = 1; /* fake pass */ |
| 152 | tests->eventq_poll[channel->channel] = 1; /* fake pass */ |
| 153 | |
| 154 | /* Reset flag and zero magic word */ |
| 155 | channel->efx->last_irq_cpu = -1; |
| 156 | channel->eventq_magic = 0; |
| 157 | smp_wmb(); |
| 158 | |
| 159 | falcon_generate_test_event(channel, magic); |
| 160 | udelay(1); |
| 161 | |
| 162 | efx_process_channel_now(channel); |
| 163 | if (channel->eventq_magic != magic) { |
| 164 | EFX_ERR(channel->efx, "channel %d failed to see test event\n", |
| 165 | channel->channel); |
| 166 | return -ETIMEDOUT; |
| 167 | } else { |
| 168 | tests->eventq_dma[channel->channel] = 1; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* Test generation and receipt of interrupting events */ |
| 175 | static int efx_test_eventq_irq(struct efx_channel *channel, |
| 176 | struct efx_self_tests *tests) |
| 177 | { |
| 178 | unsigned int magic, count; |
| 179 | |
| 180 | /* Channel specific code, limited to 20 bits */ |
| 181 | magic = (0x00010150 + channel->channel); |
| 182 | EFX_LOG(channel->efx, "channel %d testing event queue with code %x\n", |
| 183 | channel->channel, magic); |
| 184 | |
| 185 | tests->eventq_dma[channel->channel] = -1; |
| 186 | tests->eventq_int[channel->channel] = -1; |
| 187 | tests->eventq_poll[channel->channel] = -1; |
| 188 | |
| 189 | /* Reset flag and zero magic word */ |
| 190 | channel->efx->last_irq_cpu = -1; |
| 191 | channel->eventq_magic = 0; |
| 192 | smp_wmb(); |
| 193 | |
| 194 | falcon_generate_test_event(channel, magic); |
| 195 | |
| 196 | /* Wait for arrival of interrupt */ |
| 197 | count = 0; |
| 198 | do { |
| 199 | schedule_timeout_uninterruptible(HZ / 100); |
| 200 | |
| 201 | if (channel->work_pending) |
| 202 | efx_process_channel_now(channel); |
| 203 | |
| 204 | if (channel->eventq_magic == magic) |
| 205 | goto eventq_ok; |
| 206 | } while (++count < 2); |
| 207 | |
| 208 | EFX_ERR(channel->efx, "channel %d timed out waiting for event queue\n", |
| 209 | channel->channel); |
| 210 | |
| 211 | /* See if interrupt arrived */ |
| 212 | if (channel->efx->last_irq_cpu >= 0) { |
| 213 | EFX_ERR(channel->efx, "channel %d saw interrupt on CPU%d " |
| 214 | "during event queue test\n", channel->channel, |
| 215 | raw_smp_processor_id()); |
| 216 | tests->eventq_int[channel->channel] = 1; |
| 217 | } |
| 218 | |
| 219 | /* Check to see if event was received even if interrupt wasn't */ |
| 220 | efx_process_channel_now(channel); |
| 221 | if (channel->eventq_magic == magic) { |
| 222 | EFX_ERR(channel->efx, "channel %d event was generated, but " |
| 223 | "failed to trigger an interrupt\n", channel->channel); |
| 224 | tests->eventq_dma[channel->channel] = 1; |
| 225 | } |
| 226 | |
| 227 | return -ETIMEDOUT; |
| 228 | eventq_ok: |
| 229 | EFX_LOG(channel->efx, "channel %d event queue passed\n", |
| 230 | channel->channel); |
| 231 | tests->eventq_dma[channel->channel] = 1; |
| 232 | tests->eventq_int[channel->channel] = 1; |
| 233 | tests->eventq_poll[channel->channel] = 1; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /************************************************************************** |
| 238 | * |
| 239 | * PHY testing |
| 240 | * |
| 241 | **************************************************************************/ |
| 242 | |
| 243 | /* Check PHY presence by reading the PHY ID registers */ |
| 244 | static int efx_test_phy(struct efx_nic *efx, |
| 245 | struct efx_self_tests *tests) |
| 246 | { |
| 247 | u16 physid1, physid2; |
| 248 | struct mii_if_info *mii = &efx->mii; |
| 249 | struct net_device *net_dev = efx->net_dev; |
| 250 | |
| 251 | if (efx->phy_type == PHY_TYPE_NONE) |
| 252 | return 0; |
| 253 | |
| 254 | EFX_LOG(efx, "testing PHY presence\n"); |
| 255 | tests->phy_ok = -1; |
| 256 | |
| 257 | physid1 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID1); |
| 258 | physid2 = mii->mdio_read(net_dev, mii->phy_id, MII_PHYSID2); |
| 259 | |
| 260 | if ((physid1 != 0x0000) && (physid1 != 0xffff) && |
| 261 | (physid2 != 0x0000) && (physid2 != 0xffff)) { |
| 262 | EFX_LOG(efx, "found MII PHY %d ID 0x%x:%x\n", |
| 263 | mii->phy_id, physid1, physid2); |
| 264 | tests->phy_ok = 1; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | EFX_ERR(efx, "no MII PHY present with ID %d\n", mii->phy_id); |
| 269 | return -ENODEV; |
| 270 | } |
| 271 | |
| 272 | /************************************************************************** |
| 273 | * |
| 274 | * Loopback testing |
| 275 | * NB Only one loopback test can be executing concurrently. |
| 276 | * |
| 277 | **************************************************************************/ |
| 278 | |
| 279 | /* Loopback test RX callback |
| 280 | * This is called for each received packet during loopback testing. |
| 281 | */ |
| 282 | void efx_loopback_rx_packet(struct efx_nic *efx, |
| 283 | const char *buf_ptr, int pkt_len) |
| 284 | { |
| 285 | struct efx_selftest_state *state = efx->loopback_selftest; |
| 286 | struct efx_loopback_payload *received; |
| 287 | struct efx_loopback_payload *payload; |
| 288 | |
| 289 | BUG_ON(!buf_ptr); |
| 290 | |
| 291 | /* If we are just flushing, then drop the packet */ |
| 292 | if ((state == NULL) || state->flush) |
| 293 | return; |
| 294 | |
| 295 | payload = &state->payload; |
| 296 | |
Ben Hutchings | d3208b5 | 2008-05-16 21:20:00 +0100 | [diff] [blame] | 297 | received = (struct efx_loopback_payload *) buf_ptr; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 298 | received->ip.saddr = payload->ip.saddr; |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 299 | if (state->offload_csum) |
| 300 | received->ip.check = payload->ip.check; |
| 301 | |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 302 | /* Check that header exists */ |
| 303 | if (pkt_len < sizeof(received->header)) { |
| 304 | EFX_ERR(efx, "saw runt RX packet (length %d) in %s loopback " |
| 305 | "test\n", pkt_len, LOOPBACK_MODE(efx)); |
| 306 | goto err; |
| 307 | } |
| 308 | |
| 309 | /* Check that the ethernet header exists */ |
| 310 | if (memcmp(&received->header, &payload->header, ETH_HLEN) != 0) { |
| 311 | EFX_ERR(efx, "saw non-loopback RX packet in %s loopback test\n", |
| 312 | LOOPBACK_MODE(efx)); |
| 313 | goto err; |
| 314 | } |
| 315 | |
| 316 | /* Check packet length */ |
| 317 | if (pkt_len != sizeof(*payload)) { |
| 318 | EFX_ERR(efx, "saw incorrect RX packet length %d (wanted %d) in " |
| 319 | "%s loopback test\n", pkt_len, (int)sizeof(*payload), |
| 320 | LOOPBACK_MODE(efx)); |
| 321 | goto err; |
| 322 | } |
| 323 | |
| 324 | /* Check that IP header matches */ |
| 325 | if (memcmp(&received->ip, &payload->ip, sizeof(payload->ip)) != 0) { |
| 326 | EFX_ERR(efx, "saw corrupted IP header in %s loopback test\n", |
| 327 | LOOPBACK_MODE(efx)); |
| 328 | goto err; |
| 329 | } |
| 330 | |
| 331 | /* Check that msg and padding matches */ |
| 332 | if (memcmp(&received->msg, &payload->msg, sizeof(received->msg)) != 0) { |
| 333 | EFX_ERR(efx, "saw corrupted RX packet in %s loopback test\n", |
| 334 | LOOPBACK_MODE(efx)); |
| 335 | goto err; |
| 336 | } |
| 337 | |
| 338 | /* Check that iteration matches */ |
| 339 | if (received->iteration != payload->iteration) { |
| 340 | EFX_ERR(efx, "saw RX packet from iteration %d (wanted %d) in " |
| 341 | "%s loopback test\n", ntohs(received->iteration), |
| 342 | ntohs(payload->iteration), LOOPBACK_MODE(efx)); |
| 343 | goto err; |
| 344 | } |
| 345 | |
| 346 | /* Increase correct RX count */ |
| 347 | EFX_TRACE(efx, "got loopback RX in %s loopback test\n", |
| 348 | LOOPBACK_MODE(efx)); |
| 349 | |
| 350 | atomic_inc(&state->rx_good); |
| 351 | return; |
| 352 | |
| 353 | err: |
| 354 | #ifdef EFX_ENABLE_DEBUG |
| 355 | if (atomic_read(&state->rx_bad) == 0) { |
| 356 | EFX_ERR(efx, "received packet:\n"); |
| 357 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1, |
| 358 | buf_ptr, pkt_len, 0); |
| 359 | EFX_ERR(efx, "expected packet:\n"); |
| 360 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 0x10, 1, |
| 361 | &state->payload, sizeof(state->payload), 0); |
| 362 | } |
| 363 | #endif |
| 364 | atomic_inc(&state->rx_bad); |
| 365 | } |
| 366 | |
| 367 | /* Initialise an efx_selftest_state for a new iteration */ |
| 368 | static void efx_iterate_state(struct efx_nic *efx) |
| 369 | { |
| 370 | struct efx_selftest_state *state = efx->loopback_selftest; |
| 371 | struct net_device *net_dev = efx->net_dev; |
| 372 | struct efx_loopback_payload *payload = &state->payload; |
| 373 | |
| 374 | /* Initialise the layerII header */ |
| 375 | memcpy(&payload->header.h_dest, net_dev->dev_addr, ETH_ALEN); |
| 376 | memcpy(&payload->header.h_source, &payload_source, ETH_ALEN); |
| 377 | payload->header.h_proto = htons(ETH_P_IP); |
| 378 | |
| 379 | /* saddr set later and used as incrementing count */ |
| 380 | payload->ip.daddr = htonl(INADDR_LOOPBACK); |
| 381 | payload->ip.ihl = 5; |
| 382 | payload->ip.check = htons(0xdead); |
| 383 | payload->ip.tot_len = htons(sizeof(*payload) - sizeof(struct ethhdr)); |
| 384 | payload->ip.version = IPVERSION; |
| 385 | payload->ip.protocol = IPPROTO_UDP; |
| 386 | |
| 387 | /* Initialise udp header */ |
| 388 | payload->udp.source = 0; |
| 389 | payload->udp.len = htons(sizeof(*payload) - sizeof(struct ethhdr) - |
| 390 | sizeof(struct iphdr)); |
| 391 | payload->udp.check = 0; /* checksum ignored */ |
| 392 | |
| 393 | /* Fill out payload */ |
| 394 | payload->iteration = htons(ntohs(payload->iteration) + 1); |
| 395 | memcpy(&payload->msg, payload_msg, sizeof(payload_msg)); |
| 396 | |
| 397 | /* Fill out remaining state members */ |
| 398 | atomic_set(&state->rx_good, 0); |
| 399 | atomic_set(&state->rx_bad, 0); |
| 400 | smp_wmb(); |
| 401 | } |
| 402 | |
| 403 | static int efx_tx_loopback(struct efx_tx_queue *tx_queue) |
| 404 | { |
| 405 | struct efx_nic *efx = tx_queue->efx; |
| 406 | struct efx_selftest_state *state = efx->loopback_selftest; |
| 407 | struct efx_loopback_payload *payload; |
| 408 | struct sk_buff *skb; |
| 409 | int i, rc; |
| 410 | |
| 411 | /* Transmit N copies of buffer */ |
| 412 | for (i = 0; i < state->packet_count; i++) { |
| 413 | /* Allocate an skb, holding an extra reference for |
| 414 | * transmit completion counting */ |
| 415 | skb = alloc_skb(sizeof(state->payload), GFP_KERNEL); |
| 416 | if (!skb) |
| 417 | return -ENOMEM; |
| 418 | state->skbs[i] = skb; |
| 419 | skb_get(skb); |
| 420 | |
| 421 | /* Copy the payload in, incrementing the source address to |
| 422 | * exercise the rss vectors */ |
| 423 | payload = ((struct efx_loopback_payload *) |
| 424 | skb_put(skb, sizeof(state->payload))); |
| 425 | memcpy(payload, &state->payload, sizeof(state->payload)); |
| 426 | payload->ip.saddr = htonl(INADDR_LOOPBACK | (i << 2)); |
| 427 | |
| 428 | /* Ensure everything we've written is visible to the |
| 429 | * interrupt handler. */ |
| 430 | smp_wmb(); |
| 431 | |
Ben Hutchings | 5566861 | 2008-05-16 21:16:10 +0100 | [diff] [blame] | 432 | if (efx_dev_registered(efx)) |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 433 | netif_tx_lock_bh(efx->net_dev); |
| 434 | rc = efx_xmit(efx, tx_queue, skb); |
Ben Hutchings | 5566861 | 2008-05-16 21:16:10 +0100 | [diff] [blame] | 435 | if (efx_dev_registered(efx)) |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 436 | netif_tx_unlock_bh(efx->net_dev); |
| 437 | |
| 438 | if (rc != NETDEV_TX_OK) { |
| 439 | EFX_ERR(efx, "TX queue %d could not transmit packet %d " |
| 440 | "of %d in %s loopback test\n", tx_queue->queue, |
| 441 | i + 1, state->packet_count, LOOPBACK_MODE(efx)); |
| 442 | |
| 443 | /* Defer cleaning up the other skbs for the caller */ |
| 444 | kfree_skb(skb); |
| 445 | return -EPIPE; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int efx_rx_loopback(struct efx_tx_queue *tx_queue, |
| 453 | struct efx_loopback_self_tests *lb_tests) |
| 454 | { |
| 455 | struct efx_nic *efx = tx_queue->efx; |
| 456 | struct efx_selftest_state *state = efx->loopback_selftest; |
| 457 | struct sk_buff *skb; |
| 458 | int tx_done = 0, rx_good, rx_bad; |
| 459 | int i, rc = 0; |
| 460 | |
Ben Hutchings | 5566861 | 2008-05-16 21:16:10 +0100 | [diff] [blame] | 461 | if (efx_dev_registered(efx)) |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 462 | netif_tx_lock_bh(efx->net_dev); |
| 463 | |
| 464 | /* Count the number of tx completions, and decrement the refcnt. Any |
| 465 | * skbs not already completed will be free'd when the queue is flushed */ |
| 466 | for (i=0; i < state->packet_count; i++) { |
| 467 | skb = state->skbs[i]; |
| 468 | if (skb && !skb_shared(skb)) |
| 469 | ++tx_done; |
| 470 | dev_kfree_skb_any(skb); |
| 471 | } |
| 472 | |
Ben Hutchings | 5566861 | 2008-05-16 21:16:10 +0100 | [diff] [blame] | 473 | if (efx_dev_registered(efx)) |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 474 | netif_tx_unlock_bh(efx->net_dev); |
| 475 | |
| 476 | /* Check TX completion and received packet counts */ |
| 477 | rx_good = atomic_read(&state->rx_good); |
| 478 | rx_bad = atomic_read(&state->rx_bad); |
| 479 | if (tx_done != state->packet_count) { |
| 480 | /* Don't free the skbs; they will be picked up on TX |
| 481 | * overflow or channel teardown. |
| 482 | */ |
| 483 | EFX_ERR(efx, "TX queue %d saw only %d out of an expected %d " |
| 484 | "TX completion events in %s loopback test\n", |
| 485 | tx_queue->queue, tx_done, state->packet_count, |
| 486 | LOOPBACK_MODE(efx)); |
| 487 | rc = -ETIMEDOUT; |
| 488 | /* Allow to fall through so we see the RX errors as well */ |
| 489 | } |
| 490 | |
| 491 | /* We may always be up to a flush away from our desired packet total */ |
| 492 | if (rx_good != state->packet_count) { |
| 493 | EFX_LOG(efx, "TX queue %d saw only %d out of an expected %d " |
| 494 | "received packets in %s loopback test\n", |
| 495 | tx_queue->queue, rx_good, state->packet_count, |
| 496 | LOOPBACK_MODE(efx)); |
| 497 | rc = -ETIMEDOUT; |
| 498 | /* Fall through */ |
| 499 | } |
| 500 | |
| 501 | /* Update loopback test structure */ |
| 502 | lb_tests->tx_sent[tx_queue->queue] += state->packet_count; |
| 503 | lb_tests->tx_done[tx_queue->queue] += tx_done; |
| 504 | lb_tests->rx_good += rx_good; |
| 505 | lb_tests->rx_bad += rx_bad; |
| 506 | |
| 507 | return rc; |
| 508 | } |
| 509 | |
| 510 | static int |
| 511 | efx_test_loopback(struct efx_tx_queue *tx_queue, |
| 512 | struct efx_loopback_self_tests *lb_tests) |
| 513 | { |
| 514 | struct efx_nic *efx = tx_queue->efx; |
| 515 | struct efx_selftest_state *state = efx->loopback_selftest; |
| 516 | struct efx_channel *channel; |
Ben Hutchings | a0c2c19 | 2008-09-01 12:45:08 +0100 | [diff] [blame^] | 517 | int i, tx_rc, rx_rc; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 518 | |
| 519 | for (i = 0; i < loopback_test_level; i++) { |
| 520 | /* Determine how many packets to send */ |
| 521 | state->packet_count = (efx->type->txd_ring_mask + 1) / 3; |
| 522 | state->packet_count = min(1 << (i << 2), state->packet_count); |
| 523 | state->skbs = kzalloc(sizeof(state->skbs[0]) * |
| 524 | state->packet_count, GFP_KERNEL); |
Ben Hutchings | 9b7bfc4 | 2008-05-16 21:20:20 +0100 | [diff] [blame] | 525 | if (!state->skbs) |
| 526 | return -ENOMEM; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 527 | state->flush = 0; |
| 528 | |
| 529 | EFX_LOG(efx, "TX queue %d testing %s loopback with %d " |
| 530 | "packets\n", tx_queue->queue, LOOPBACK_MODE(efx), |
| 531 | state->packet_count); |
| 532 | |
| 533 | efx_iterate_state(efx); |
Ben Hutchings | a0c2c19 | 2008-09-01 12:45:08 +0100 | [diff] [blame^] | 534 | tx_rc = efx_tx_loopback(tx_queue); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 535 | |
| 536 | /* NAPI polling is not enabled, so process channels synchronously */ |
| 537 | schedule_timeout_uninterruptible(HZ / 50); |
| 538 | efx_for_each_channel_with_interrupt(channel, efx) { |
| 539 | if (channel->work_pending) |
| 540 | efx_process_channel_now(channel); |
| 541 | } |
| 542 | |
Ben Hutchings | a0c2c19 | 2008-09-01 12:45:08 +0100 | [diff] [blame^] | 543 | rx_rc = efx_rx_loopback(tx_queue, lb_tests); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 544 | kfree(state->skbs); |
| 545 | |
Ben Hutchings | a0c2c19 | 2008-09-01 12:45:08 +0100 | [diff] [blame^] | 546 | if (tx_rc || rx_rc) { |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 547 | /* Wait a while to ensure there are no packets |
| 548 | * floating around after a failure. */ |
| 549 | schedule_timeout_uninterruptible(HZ / 10); |
Ben Hutchings | a0c2c19 | 2008-09-01 12:45:08 +0100 | [diff] [blame^] | 550 | return tx_rc ? tx_rc : rx_rc; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | EFX_LOG(efx, "TX queue %d passed %s loopback test with a burst length " |
| 555 | "of %d packets\n", tx_queue->queue, LOOPBACK_MODE(efx), |
| 556 | state->packet_count); |
| 557 | |
Ben Hutchings | a0c2c19 | 2008-09-01 12:45:08 +0100 | [diff] [blame^] | 558 | return 0; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static int efx_test_loopbacks(struct efx_nic *efx, |
| 562 | struct efx_self_tests *tests, |
| 563 | unsigned int loopback_modes) |
| 564 | { |
| 565 | struct efx_selftest_state *state = efx->loopback_selftest; |
| 566 | struct ethtool_cmd ecmd, ecmd_loopback; |
| 567 | struct efx_tx_queue *tx_queue; |
| 568 | enum efx_loopback_mode old_mode, mode; |
| 569 | int count, rc = 0, link_up; |
| 570 | |
| 571 | rc = efx_ethtool_get_settings(efx->net_dev, &ecmd); |
| 572 | if (rc) { |
| 573 | EFX_ERR(efx, "could not get GMII settings\n"); |
| 574 | return rc; |
| 575 | } |
| 576 | old_mode = efx->loopback_mode; |
| 577 | |
| 578 | /* Disable autonegotiation for the purposes of loopback */ |
| 579 | memcpy(&ecmd_loopback, &ecmd, sizeof(ecmd_loopback)); |
| 580 | if (ecmd_loopback.autoneg == AUTONEG_ENABLE) { |
| 581 | ecmd_loopback.autoneg = AUTONEG_DISABLE; |
| 582 | ecmd_loopback.duplex = DUPLEX_FULL; |
| 583 | ecmd_loopback.speed = SPEED_10000; |
| 584 | } |
| 585 | |
| 586 | rc = efx_ethtool_set_settings(efx->net_dev, &ecmd_loopback); |
| 587 | if (rc) { |
| 588 | EFX_ERR(efx, "could not disable autonegotiation\n"); |
| 589 | goto out; |
| 590 | } |
| 591 | tests->loopback_speed = ecmd_loopback.speed; |
| 592 | tests->loopback_full_duplex = ecmd_loopback.duplex; |
| 593 | |
| 594 | /* Test all supported loopback modes */ |
| 595 | for (mode = LOOPBACK_NONE; mode < LOOPBACK_TEST_MAX; mode++) { |
| 596 | if (!(loopback_modes & (1 << mode))) |
| 597 | continue; |
| 598 | |
| 599 | /* Move the port into the specified loopback mode. */ |
| 600 | state->flush = 1; |
| 601 | efx->loopback_mode = mode; |
| 602 | efx_reconfigure_port(efx); |
| 603 | |
| 604 | /* Wait for the PHY to signal the link is up */ |
| 605 | count = 0; |
| 606 | do { |
| 607 | struct efx_channel *channel = &efx->channel[0]; |
| 608 | |
| 609 | falcon_check_xmac(efx); |
| 610 | schedule_timeout_uninterruptible(HZ / 10); |
| 611 | if (channel->work_pending) |
| 612 | efx_process_channel_now(channel); |
| 613 | /* Wait for PHY events to be processed */ |
| 614 | flush_workqueue(efx->workqueue); |
| 615 | rmb(); |
| 616 | |
| 617 | /* efx->link_up can be 1 even if the XAUI link is down, |
| 618 | * (bug5762). Usually, it's not worth bothering with the |
| 619 | * difference, but for selftests, we need that extra |
| 620 | * guarantee that the link is really, really, up. |
| 621 | */ |
| 622 | link_up = efx->link_up; |
| 623 | if (!falcon_xaui_link_ok(efx)) |
| 624 | link_up = 0; |
| 625 | |
| 626 | } while ((++count < 20) && !link_up); |
| 627 | |
| 628 | /* The link should now be up. If it isn't, there is no point |
| 629 | * in attempting a loopback test */ |
| 630 | if (!link_up) { |
| 631 | EFX_ERR(efx, "loopback %s never came up\n", |
| 632 | LOOPBACK_MODE(efx)); |
| 633 | rc = -EIO; |
| 634 | goto out; |
| 635 | } |
| 636 | |
| 637 | EFX_LOG(efx, "link came up in %s loopback in %d iterations\n", |
| 638 | LOOPBACK_MODE(efx), count); |
| 639 | |
| 640 | /* Test every TX queue */ |
| 641 | efx_for_each_tx_queue(tx_queue, efx) { |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 642 | state->offload_csum = (tx_queue->queue == |
| 643 | EFX_TX_QUEUE_OFFLOAD_CSUM); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 644 | rc |= efx_test_loopback(tx_queue, |
| 645 | &tests->loopback[mode]); |
| 646 | if (rc) |
| 647 | goto out; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | out: |
| 652 | /* Take out of loopback and restore PHY settings */ |
| 653 | state->flush = 1; |
| 654 | efx->loopback_mode = old_mode; |
| 655 | efx_ethtool_set_settings(efx->net_dev, &ecmd); |
| 656 | |
| 657 | return rc; |
| 658 | } |
| 659 | |
| 660 | /************************************************************************** |
| 661 | * |
| 662 | * Entry points |
| 663 | * |
| 664 | *************************************************************************/ |
| 665 | |
| 666 | /* Online (i.e. non-disruptive) testing |
| 667 | * This checks interrupt generation, event delivery and PHY presence. */ |
| 668 | int efx_online_test(struct efx_nic *efx, struct efx_self_tests *tests) |
| 669 | { |
| 670 | struct efx_channel *channel; |
| 671 | int rc = 0; |
| 672 | |
| 673 | EFX_LOG(efx, "performing online self-tests\n"); |
| 674 | |
| 675 | rc |= efx_test_interrupts(efx, tests); |
| 676 | efx_for_each_channel(channel, efx) { |
| 677 | if (channel->has_interrupt) |
| 678 | rc |= efx_test_eventq_irq(channel, tests); |
| 679 | else |
| 680 | rc |= efx_test_eventq(channel, tests); |
| 681 | } |
| 682 | rc |= efx_test_phy(efx, tests); |
| 683 | |
| 684 | if (rc) |
| 685 | EFX_ERR(efx, "failed online self-tests\n"); |
| 686 | |
| 687 | return rc; |
| 688 | } |
| 689 | |
| 690 | /* Offline (i.e. disruptive) testing |
| 691 | * This checks MAC and PHY loopback on the specified port. */ |
| 692 | int efx_offline_test(struct efx_nic *efx, |
| 693 | struct efx_self_tests *tests, unsigned int loopback_modes) |
| 694 | { |
| 695 | struct efx_selftest_state *state; |
| 696 | int rc = 0; |
| 697 | |
| 698 | EFX_LOG(efx, "performing offline self-tests\n"); |
| 699 | |
| 700 | /* Create a selftest_state structure to hold state for the test */ |
| 701 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 702 | if (state == NULL) { |
| 703 | rc = -ENOMEM; |
| 704 | goto out; |
| 705 | } |
| 706 | |
| 707 | /* Set the port loopback_selftest member. From this point on |
| 708 | * all received packets will be dropped. Mark the state as |
| 709 | * "flushing" so all inflight packets are dropped */ |
| 710 | BUG_ON(efx->loopback_selftest); |
| 711 | state->flush = 1; |
Ben Hutchings | d3208b5 | 2008-05-16 21:20:00 +0100 | [diff] [blame] | 712 | efx->loopback_selftest = state; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 713 | |
| 714 | rc = efx_test_loopbacks(efx, tests, loopback_modes); |
| 715 | |
| 716 | efx->loopback_selftest = NULL; |
| 717 | wmb(); |
| 718 | kfree(state); |
| 719 | |
| 720 | out: |
| 721 | if (rc) |
| 722 | EFX_ERR(efx, "failed offline self-tests\n"); |
| 723 | |
| 724 | return rc; |
| 725 | } |
| 726 | |