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