1 /* typhoon.c: A Linux Ethernet device driver for 3Com 3CR990 family of NICs */
3 Written 2002-2004 by David Dillow <dave@thedillows.org>
4 Based on code written 1998-2000 by Donald Becker <becker@scyld.com> and
5 Linux 2.2.x driver by David P. McLean <davidpmclean@yahoo.com>.
7 This software may be used and distributed according to the terms of
8 the GNU General Public License (GPL), incorporated herein by reference.
9 Drivers based on or derived from this code fall under the GPL and must
10 retain the authorship, copyright and license notice. This file is not
11 a complete program and may only be used when the entire operating
12 system is licensed under the GPL.
14 This software is available on a public web site. It may enable
15 cryptographic capabilities of the 3Com hardware, and may be
16 exported from the United States under License Exception "TSU"
17 pursuant to 15 C.F.R. Section 740.13(e).
19 This work was funded by the National Library of Medicine under
20 the Department of Energy project number 0274DD06D1 and NLM project
23 This driver is designed for the 3Com 3CR990 Family of cards with the
24 3XP Processor. It has been tested on x86 and sparc64.
27 *) Cannot DMA Rx packets to a 2 byte aligned address. Also firmware
28 issue. Hopefully 3Com will fix it.
29 *) Waiting for a command response takes 8ms due to non-preemptable
30 polling. Only significant for getting stats and creating
31 SAs, but an ugly wart never the less.
34 *) Doesn't do IPSEC offloading. Yet. Keep yer pants on, it's coming.
35 *) Add more support for ethtool (especially for NIC stats)
36 *) Allow disabling of RX checksum offloading
37 *) Fix MAC changing to work while the interface is up
38 (Need to put commands on the TX ring, which changes
40 *) Add in FCS to {rx,tx}_bytes, since the hardware doesn't. See
41 http://oss.sgi.com/cgi-bin/mesg.cgi?a=netdev&i=20031215152211.7003fe8e.rddunlap%40osdl.org
44 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
45 * Setting to > 1518 effectively disables this feature.
47 static int rx_copybreak = 200;
49 /* Should we use MMIO or Port IO?
52 * 2: Try MMIO, fallback to Port IO
54 static unsigned int use_mmio = 2;
56 /* end user-configurable values */
58 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
60 static const int multicast_filter_limit = 32;
62 /* Operational parameters that are set at compile time. */
64 /* Keep the ring sizes a power of two for compile efficiency.
65 * The compiler will convert <unsigned>'%'<2^N> into a bit mask.
66 * Making the Tx ring too large decreases the effectiveness of channel
67 * bonding and packet priority.
68 * There are no ill effects from too-large receive rings.
70 * We don't currently use the Hi Tx ring so, don't make it very big.
72 * Beware that if we start using the Hi Tx ring, we will need to change
73 * typhoon_num_free_tx() and typhoon_tx_complete() to account for that.
75 #define TXHI_ENTRIES 2
76 #define TXLO_ENTRIES 128
78 #define COMMAND_ENTRIES 16
79 #define RESPONSE_ENTRIES 32
81 #define COMMAND_RING_SIZE (COMMAND_ENTRIES * sizeof(struct cmd_desc))
82 #define RESPONSE_RING_SIZE (RESPONSE_ENTRIES * sizeof(struct resp_desc))
84 /* The 3XP will preload and remove 64 entries from the free buffer
85 * list, and we need one entry to keep the ring from wrapping, so
86 * to keep this a power of two, we use 128 entries.
88 #define RXFREE_ENTRIES 128
89 #define RXENT_ENTRIES (RXFREE_ENTRIES - 1)
91 /* Operational parameters that usually are not changed. */
93 /* Time in jiffies before concluding the transmitter is hung. */
94 #define TX_TIMEOUT (2*HZ)
96 #define PKT_BUF_SZ 1536
97 #define FIRMWARE_NAME "3com/typhoon.bin"
99 #define pr_fmt(fmt) KBUILD_MODNAME " " fmt
101 #include <linux/module.h>
102 #include <linux/kernel.h>
103 #include <linux/sched.h>
104 #include <linux/string.h>
105 #include <linux/timer.h>
106 #include <linux/errno.h>
107 #include <linux/ioport.h>
108 #include <linux/interrupt.h>
109 #include <linux/pci.h>
110 #include <linux/netdevice.h>
111 #include <linux/etherdevice.h>
112 #include <linux/skbuff.h>
113 #include <linux/mm.h>
114 #include <linux/init.h>
115 #include <linux/delay.h>
116 #include <linux/ethtool.h>
117 #include <linux/if_vlan.h>
118 #include <linux/crc32.h>
119 #include <linux/bitops.h>
120 #include <asm/processor.h>
122 #include <linux/uaccess.h>
123 #include <linux/in6.h>
124 #include <linux/dma-mapping.h>
125 #include <linux/firmware.h>
129 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
130 MODULE_LICENSE("GPL");
131 MODULE_FIRMWARE(FIRMWARE_NAME);
132 MODULE_DESCRIPTION("3Com Typhoon Family (3C990, 3CR990, and variants)");
133 MODULE_PARM_DESC(rx_copybreak, "Packets smaller than this are copied and "
134 "the buffer given back to the NIC. Default "
136 MODULE_PARM_DESC(use_mmio, "Use MMIO (1) or PIO(0) to access the NIC. "
137 "Default is to try MMIO and fallback to PIO.");
138 module_param(rx_copybreak, int, 0);
139 module_param(use_mmio, int, 0);
141 #if defined(NETIF_F_TSO) && MAX_SKB_FRAGS > 32
142 #warning Typhoon only supports 32 entries in its SG list for TSO, disabling TSO
146 #if TXLO_ENTRIES <= (2 * MAX_SKB_FRAGS)
147 #error TX ring too small!
150 struct typhoon_card_info {
152 const int capabilities;
155 #define TYPHOON_CRYPTO_NONE 0x00
156 #define TYPHOON_CRYPTO_DES 0x01
157 #define TYPHOON_CRYPTO_3DES 0x02
158 #define TYPHOON_CRYPTO_VARIABLE 0x04
159 #define TYPHOON_FIBER 0x08
160 #define TYPHOON_WAKEUP_NEEDS_RESET 0x10
163 TYPHOON_TX = 0, TYPHOON_TX95, TYPHOON_TX97, TYPHOON_SVR,
164 TYPHOON_SVR95, TYPHOON_SVR97, TYPHOON_TXM, TYPHOON_BSVR,
165 TYPHOON_FX95, TYPHOON_FX97, TYPHOON_FX95SVR, TYPHOON_FX97SVR,
169 /* directly indexed by enum typhoon_cards, above */
170 static struct typhoon_card_info typhoon_card_info[] = {
171 { "3Com Typhoon (3C990-TX)",
172 TYPHOON_CRYPTO_NONE},
173 { "3Com Typhoon (3CR990-TX-95)",
175 { "3Com Typhoon (3CR990-TX-97)",
176 TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES},
177 { "3Com Typhoon (3C990SVR)",
178 TYPHOON_CRYPTO_NONE},
179 { "3Com Typhoon (3CR990SVR95)",
181 { "3Com Typhoon (3CR990SVR97)",
182 TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES},
183 { "3Com Typhoon2 (3C990B-TX-M)",
184 TYPHOON_CRYPTO_VARIABLE},
185 { "3Com Typhoon2 (3C990BSVR)",
186 TYPHOON_CRYPTO_VARIABLE},
187 { "3Com Typhoon (3CR990-FX-95)",
188 TYPHOON_CRYPTO_DES | TYPHOON_FIBER},
189 { "3Com Typhoon (3CR990-FX-97)",
190 TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES | TYPHOON_FIBER},
191 { "3Com Typhoon (3CR990-FX-95 Server)",
192 TYPHOON_CRYPTO_DES | TYPHOON_FIBER},
193 { "3Com Typhoon (3CR990-FX-97 Server)",
194 TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES | TYPHOON_FIBER},
195 { "3Com Typhoon2 (3C990B-FX-97)",
196 TYPHOON_CRYPTO_VARIABLE | TYPHOON_FIBER},
199 /* Notes on the new subsystem numbering scheme:
200 * bits 0-1 indicate crypto capabilities: (0) variable, (1) DES, or (2) 3DES
201 * bit 4 indicates if this card has secured firmware (we don't support it)
202 * bit 8 indicates if this is a (0) copper or (1) fiber card
203 * bits 12-16 indicate card type: (0) client and (1) server
205 static const struct pci_device_id typhoon_pci_tbl[] = {
206 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990,
207 PCI_ANY_ID, PCI_ANY_ID, 0, 0,TYPHOON_TX },
208 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_TX_95,
209 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_TX95 },
210 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_TX_97,
211 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_TX97 },
212 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990B,
213 PCI_ANY_ID, 0x1000, 0, 0, TYPHOON_TXM },
214 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990B,
215 PCI_ANY_ID, 0x1102, 0, 0, TYPHOON_FXM },
216 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990B,
217 PCI_ANY_ID, 0x2000, 0, 0, TYPHOON_BSVR },
218 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
219 PCI_ANY_ID, 0x1101, 0, 0, TYPHOON_FX95 },
220 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
221 PCI_ANY_ID, 0x1102, 0, 0, TYPHOON_FX97 },
222 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
223 PCI_ANY_ID, 0x2101, 0, 0, TYPHOON_FX95SVR },
224 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
225 PCI_ANY_ID, 0x2102, 0, 0, TYPHOON_FX97SVR },
226 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990SVR95,
227 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_SVR95 },
228 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990SVR97,
229 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_SVR97 },
230 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990SVR,
231 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_SVR },
234 MODULE_DEVICE_TABLE(pci, typhoon_pci_tbl);
236 /* Define the shared memory area
237 * Align everything the 3XP will normally be using.
238 * We'll need to move/align txHi if we start using that ring.
240 #define __3xp_aligned ____cacheline_aligned
241 struct typhoon_shared {
242 struct typhoon_interface iface;
243 struct typhoon_indexes indexes __3xp_aligned;
244 struct tx_desc txLo[TXLO_ENTRIES] __3xp_aligned;
245 struct rx_desc rxLo[RX_ENTRIES] __3xp_aligned;
246 struct rx_desc rxHi[RX_ENTRIES] __3xp_aligned;
247 struct cmd_desc cmd[COMMAND_ENTRIES] __3xp_aligned;
248 struct resp_desc resp[RESPONSE_ENTRIES] __3xp_aligned;
249 struct rx_free rxBuff[RXFREE_ENTRIES] __3xp_aligned;
251 struct tx_desc txHi[TXHI_ENTRIES];
260 /* Tx cache line section */
261 struct transmit_ring txLoRing ____cacheline_aligned;
262 struct pci_dev * tx_pdev;
263 void __iomem *tx_ioaddr;
266 /* Irq/Rx cache line section */
267 void __iomem *ioaddr ____cacheline_aligned;
268 struct typhoon_indexes *indexes;
273 struct basic_ring rxLoRing;
274 struct pci_dev * pdev;
275 struct net_device * dev;
276 struct napi_struct napi;
277 struct basic_ring rxHiRing;
278 struct basic_ring rxBuffRing;
279 struct rxbuff_ent rxbuffers[RXENT_ENTRIES];
281 /* general section */
282 spinlock_t command_lock ____cacheline_aligned;
283 struct basic_ring cmdRing;
284 struct basic_ring respRing;
285 struct net_device_stats stats_saved;
286 struct typhoon_shared * shared;
287 dma_addr_t shared_dma;
292 /* unused stuff (future use) */
294 struct transmit_ring txHiRing;
297 enum completion_wait_values {
298 NoWait = 0, WaitNoSleep, WaitSleep,
301 /* These are the values for the typhoon.card_state variable.
302 * These determine where the statistics will come from in get_stats().
303 * The sleep image does not support the statistics we need.
306 Sleeping = 0, Running,
309 /* PCI writes are not guaranteed to be posted in order, but outstanding writes
310 * cannot pass a read, so this forces current writes to post.
312 #define typhoon_post_pci_writes(x) \
313 do { if (likely(use_mmio)) ioread32(x+TYPHOON_REG_HEARTBEAT); } while (0)
315 /* We'll wait up to six seconds for a reset, and half a second normally.
317 #define TYPHOON_UDELAY 50
318 #define TYPHOON_RESET_TIMEOUT_SLEEP (6 * HZ)
319 #define TYPHOON_RESET_TIMEOUT_NOSLEEP ((6 * 1000000) / TYPHOON_UDELAY)
320 #define TYPHOON_WAIT_TIMEOUT ((1000000 / 2) / TYPHOON_UDELAY)
322 #if defined(NETIF_F_TSO)
323 #define skb_tso_size(x) (skb_shinfo(x)->gso_size)
324 #define TSO_NUM_DESCRIPTORS 2
325 #define TSO_OFFLOAD_ON TYPHOON_OFFLOAD_TCP_SEGMENT
327 #define NETIF_F_TSO 0
328 #define skb_tso_size(x) 0
329 #define TSO_NUM_DESCRIPTORS 0
330 #define TSO_OFFLOAD_ON 0
334 typhoon_inc_index(u32 *index, const int count, const int num_entries)
336 /* Increment a ring index -- we can use this for all rings execept
337 * the Rx rings, as they use different size descriptors
338 * otherwise, everything is the same size as a cmd_desc
340 *index += count * sizeof(struct cmd_desc);
341 *index %= num_entries * sizeof(struct cmd_desc);
345 typhoon_inc_cmd_index(u32 *index, const int count)
347 typhoon_inc_index(index, count, COMMAND_ENTRIES);
351 typhoon_inc_resp_index(u32 *index, const int count)
353 typhoon_inc_index(index, count, RESPONSE_ENTRIES);
357 typhoon_inc_rxfree_index(u32 *index, const int count)
359 typhoon_inc_index(index, count, RXFREE_ENTRIES);
363 typhoon_inc_tx_index(u32 *index, const int count)
365 /* if we start using the Hi Tx ring, this needs updating */
366 typhoon_inc_index(index, count, TXLO_ENTRIES);
370 typhoon_inc_rx_index(u32 *index, const int count)
372 /* sizeof(struct rx_desc) != sizeof(struct cmd_desc) */
373 *index += count * sizeof(struct rx_desc);
374 *index %= RX_ENTRIES * sizeof(struct rx_desc);
378 typhoon_reset(void __iomem *ioaddr, int wait_type)
383 if (wait_type == WaitNoSleep)
384 timeout = TYPHOON_RESET_TIMEOUT_NOSLEEP;
386 timeout = TYPHOON_RESET_TIMEOUT_SLEEP;
388 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
389 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
391 iowrite32(TYPHOON_RESET_ALL, ioaddr + TYPHOON_REG_SOFT_RESET);
392 typhoon_post_pci_writes(ioaddr);
394 iowrite32(TYPHOON_RESET_NONE, ioaddr + TYPHOON_REG_SOFT_RESET);
396 if (wait_type != NoWait) {
397 for (i = 0; i < timeout; i++) {
398 if (ioread32(ioaddr + TYPHOON_REG_STATUS) ==
399 TYPHOON_STATUS_WAITING_FOR_HOST)
402 if (wait_type == WaitSleep)
403 schedule_timeout_uninterruptible(1);
405 udelay(TYPHOON_UDELAY);
412 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
413 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
415 /* The 3XP seems to need a little extra time to complete the load
416 * of the sleep image before we can reliably boot it. Failure to
417 * do this occasionally results in a hung adapter after boot in
418 * typhoon_init_one() while trying to read the MAC address or
419 * putting the card to sleep. 3Com's driver waits 5ms, but
420 * that seems to be overkill. However, if we can sleep, we might
421 * as well give it that much time. Otherwise, we'll give it 500us,
422 * which should be enough (I've see it work well at 100us, but still
423 * saw occasional problems.)
425 if (wait_type == WaitSleep)
433 typhoon_wait_status(void __iomem *ioaddr, u32 wait_value)
437 for (i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
438 if (ioread32(ioaddr + TYPHOON_REG_STATUS) == wait_value)
440 udelay(TYPHOON_UDELAY);
450 typhoon_media_status(struct net_device *dev, struct resp_desc *resp)
452 if (resp->parm1 & TYPHOON_MEDIA_STAT_NO_LINK)
453 netif_carrier_off(dev);
455 netif_carrier_on(dev);
459 typhoon_hello(struct typhoon *tp)
461 struct basic_ring *ring = &tp->cmdRing;
462 struct cmd_desc *cmd;
464 /* We only get a hello request if we've not sent anything to the
465 * card in a long while. If the lock is held, then we're in the
466 * process of issuing a command, so we don't need to respond.
468 if (spin_trylock(&tp->command_lock)) {
469 cmd = (struct cmd_desc *)(ring->ringBase + ring->lastWrite);
470 typhoon_inc_cmd_index(&ring->lastWrite, 1);
472 INIT_COMMAND_NO_RESPONSE(cmd, TYPHOON_CMD_HELLO_RESP);
474 iowrite32(ring->lastWrite, tp->ioaddr + TYPHOON_REG_CMD_READY);
475 spin_unlock(&tp->command_lock);
480 typhoon_process_response(struct typhoon *tp, int resp_size,
481 struct resp_desc *resp_save)
483 struct typhoon_indexes *indexes = tp->indexes;
484 struct resp_desc *resp;
485 u8 *base = tp->respRing.ringBase;
486 int count, len, wrap_len;
490 cleared = le32_to_cpu(indexes->respCleared);
491 ready = le32_to_cpu(indexes->respReady);
492 while (cleared != ready) {
493 resp = (struct resp_desc *)(base + cleared);
494 count = resp->numDesc + 1;
495 if (resp_save && resp->seqNo) {
496 if (count > resp_size) {
497 resp_save->flags = TYPHOON_RESP_ERROR;
502 len = count * sizeof(*resp);
503 if (unlikely(cleared + len > RESPONSE_RING_SIZE)) {
504 wrap_len = cleared + len - RESPONSE_RING_SIZE;
505 len = RESPONSE_RING_SIZE - cleared;
508 memcpy(resp_save, resp, len);
509 if (unlikely(wrap_len)) {
510 resp_save += len / sizeof(*resp);
511 memcpy(resp_save, base, wrap_len);
515 } else if (resp->cmd == TYPHOON_CMD_READ_MEDIA_STATUS) {
516 typhoon_media_status(tp->dev, resp);
517 } else if (resp->cmd == TYPHOON_CMD_HELLO_RESP) {
521 "dumping unexpected response 0x%04x:%d:0x%02x:0x%04x:%08x:%08x\n",
522 le16_to_cpu(resp->cmd),
523 resp->numDesc, resp->flags,
524 le16_to_cpu(resp->parm1),
525 le32_to_cpu(resp->parm2),
526 le32_to_cpu(resp->parm3));
530 typhoon_inc_resp_index(&cleared, count);
533 indexes->respCleared = cpu_to_le32(cleared);
535 return resp_save == NULL;
539 typhoon_num_free(int lastWrite, int lastRead, int ringSize)
541 /* this works for all descriptors but rx_desc, as they are a
542 * different size than the cmd_desc -- everyone else is the same
544 lastWrite /= sizeof(struct cmd_desc);
545 lastRead /= sizeof(struct cmd_desc);
546 return (ringSize + lastRead - lastWrite - 1) % ringSize;
550 typhoon_num_free_cmd(struct typhoon *tp)
552 int lastWrite = tp->cmdRing.lastWrite;
553 int cmdCleared = le32_to_cpu(tp->indexes->cmdCleared);
555 return typhoon_num_free(lastWrite, cmdCleared, COMMAND_ENTRIES);
559 typhoon_num_free_resp(struct typhoon *tp)
561 int respReady = le32_to_cpu(tp->indexes->respReady);
562 int respCleared = le32_to_cpu(tp->indexes->respCleared);
564 return typhoon_num_free(respReady, respCleared, RESPONSE_ENTRIES);
568 typhoon_num_free_tx(struct transmit_ring *ring)
570 /* if we start using the Hi Tx ring, this needs updating */
571 return typhoon_num_free(ring->lastWrite, ring->lastRead, TXLO_ENTRIES);
575 typhoon_issue_command(struct typhoon *tp, int num_cmd, struct cmd_desc *cmd,
576 int num_resp, struct resp_desc *resp)
578 struct typhoon_indexes *indexes = tp->indexes;
579 struct basic_ring *ring = &tp->cmdRing;
580 struct resp_desc local_resp;
583 int freeCmd, freeResp;
586 spin_lock(&tp->command_lock);
588 freeCmd = typhoon_num_free_cmd(tp);
589 freeResp = typhoon_num_free_resp(tp);
591 if (freeCmd < num_cmd || freeResp < num_resp) {
592 netdev_err(tp->dev, "no descs for cmd, had (needed) %d (%d) cmd, %d (%d) resp\n",
593 freeCmd, num_cmd, freeResp, num_resp);
598 if (cmd->flags & TYPHOON_CMD_RESPOND) {
599 /* If we're expecting a response, but the caller hasn't given
600 * us a place to put it, we'll provide one.
602 tp->awaiting_resp = 1;
610 len = num_cmd * sizeof(*cmd);
611 if (unlikely(ring->lastWrite + len > COMMAND_RING_SIZE)) {
612 wrap_len = ring->lastWrite + len - COMMAND_RING_SIZE;
613 len = COMMAND_RING_SIZE - ring->lastWrite;
616 memcpy(ring->ringBase + ring->lastWrite, cmd, len);
617 if (unlikely(wrap_len)) {
618 struct cmd_desc *wrap_ptr = cmd;
619 wrap_ptr += len / sizeof(*cmd);
620 memcpy(ring->ringBase, wrap_ptr, wrap_len);
623 typhoon_inc_cmd_index(&ring->lastWrite, num_cmd);
625 /* "I feel a presence... another warrior is on the mesa."
628 iowrite32(ring->lastWrite, tp->ioaddr + TYPHOON_REG_CMD_READY);
629 typhoon_post_pci_writes(tp->ioaddr);
631 if ((cmd->flags & TYPHOON_CMD_RESPOND) == 0)
634 /* Ugh. We'll be here about 8ms, spinning our thumbs, unable to
635 * preempt or do anything other than take interrupts. So, don't
636 * wait for a response unless you have to.
638 * I've thought about trying to sleep here, but we're called
639 * from many contexts that don't allow that. Also, given the way
640 * 3Com has implemented irq coalescing, we would likely timeout --
641 * this has been observed in real life!
643 * The big killer is we have to wait to get stats from the card,
644 * though we could go to a periodic refresh of those if we don't
645 * mind them getting somewhat stale. The rest of the waiting
646 * commands occur during open/close/suspend/resume, so they aren't
647 * time critical. Creating SAs in the future will also have to
651 for (i = 0; i < TYPHOON_WAIT_TIMEOUT && !got_resp; i++) {
652 if (indexes->respCleared != indexes->respReady)
653 got_resp = typhoon_process_response(tp, num_resp,
655 udelay(TYPHOON_UDELAY);
663 /* Collect the error response even if we don't care about the
664 * rest of the response
666 if (resp->flags & TYPHOON_RESP_ERROR)
670 if (tp->awaiting_resp) {
671 tp->awaiting_resp = 0;
674 /* Ugh. If a response was added to the ring between
675 * the call to typhoon_process_response() and the clearing
676 * of tp->awaiting_resp, we could have missed the interrupt
677 * and it could hang in the ring an indeterminate amount of
678 * time. So, check for it, and interrupt ourselves if this
681 if (indexes->respCleared != indexes->respReady)
682 iowrite32(1, tp->ioaddr + TYPHOON_REG_SELF_INTERRUPT);
685 spin_unlock(&tp->command_lock);
690 typhoon_tso_fill(struct sk_buff *skb, struct transmit_ring *txRing,
693 struct tcpopt_desc *tcpd;
694 u32 tcpd_offset = ring_dma;
696 tcpd = (struct tcpopt_desc *) (txRing->ringBase + txRing->lastWrite);
697 tcpd_offset += txRing->lastWrite;
698 tcpd_offset += offsetof(struct tcpopt_desc, bytesTx);
699 typhoon_inc_tx_index(&txRing->lastWrite, 1);
701 tcpd->flags = TYPHOON_OPT_DESC | TYPHOON_OPT_TCP_SEG;
703 tcpd->mss_flags = cpu_to_le16(skb_tso_size(skb));
704 tcpd->mss_flags |= TYPHOON_TSO_FIRST | TYPHOON_TSO_LAST;
705 tcpd->respAddrLo = cpu_to_le32(tcpd_offset);
706 tcpd->bytesTx = cpu_to_le32(skb->len);
711 typhoon_start_tx(struct sk_buff *skb, struct net_device *dev)
713 struct typhoon *tp = netdev_priv(dev);
714 struct transmit_ring *txRing;
715 struct tx_desc *txd, *first_txd;
719 /* we have two rings to choose from, but we only use txLo for now
720 * If we start using the Hi ring as well, we'll need to update
721 * typhoon_stop_runtime(), typhoon_interrupt(), typhoon_num_free_tx(),
722 * and TXHI_ENTRIES to match, as well as update the TSO code below
723 * to get the right DMA address
725 txRing = &tp->txLoRing;
727 /* We need one descriptor for each fragment of the sk_buff, plus the
728 * one for the ->data area of it.
730 * The docs say a maximum of 16 fragment descriptors per TCP option
731 * descriptor, then make a new packet descriptor and option descriptor
732 * for the next 16 fragments. The engineers say just an option
733 * descriptor is needed. I've tested up to 26 fragments with a single
734 * packet descriptor/option descriptor combo, so I use that for now.
736 * If problems develop with TSO, check this first.
738 numDesc = skb_shinfo(skb)->nr_frags + 1;
742 /* When checking for free space in the ring, we need to also
743 * account for the initial Tx descriptor, and we always must leave
744 * at least one descriptor unused in the ring so that it doesn't
745 * wrap and look empty.
747 * The only time we should loop here is when we hit the race
748 * between marking the queue awake and updating the cleared index.
749 * Just loop and it will appear. This comes from the acenic driver.
751 while (unlikely(typhoon_num_free_tx(txRing) < (numDesc + 2)))
754 first_txd = (struct tx_desc *) (txRing->ringBase + txRing->lastWrite);
755 typhoon_inc_tx_index(&txRing->lastWrite, 1);
757 first_txd->flags = TYPHOON_TX_DESC | TYPHOON_DESC_VALID;
758 first_txd->numDesc = 0;
760 first_txd->tx_addr = (u64)((unsigned long) skb);
761 first_txd->processFlags = 0;
763 if (skb->ip_summed == CHECKSUM_PARTIAL) {
764 /* The 3XP will figure out if this is UDP/TCP */
765 first_txd->processFlags |= TYPHOON_TX_PF_TCP_CHKSUM;
766 first_txd->processFlags |= TYPHOON_TX_PF_UDP_CHKSUM;
767 first_txd->processFlags |= TYPHOON_TX_PF_IP_CHKSUM;
770 if (skb_vlan_tag_present(skb)) {
771 first_txd->processFlags |=
772 TYPHOON_TX_PF_INSERT_VLAN | TYPHOON_TX_PF_VLAN_PRIORITY;
773 first_txd->processFlags |=
774 cpu_to_le32(htons(skb_vlan_tag_get(skb)) <<
775 TYPHOON_TX_PF_VLAN_TAG_SHIFT);
778 if (skb_is_gso(skb)) {
779 first_txd->processFlags |= TYPHOON_TX_PF_TCP_SEGMENT;
780 first_txd->numDesc++;
782 typhoon_tso_fill(skb, txRing, tp->txlo_dma_addr);
785 txd = (struct tx_desc *) (txRing->ringBase + txRing->lastWrite);
786 typhoon_inc_tx_index(&txRing->lastWrite, 1);
788 /* No need to worry about padding packet -- the firmware pads
789 * it with zeros to ETH_ZLEN for us.
791 if (skb_shinfo(skb)->nr_frags == 0) {
792 skb_dma = dma_map_single(&tp->tx_pdev->dev, skb->data,
793 skb->len, DMA_TO_DEVICE);
794 txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
795 txd->len = cpu_to_le16(skb->len);
796 txd->frag.addr = cpu_to_le32(skb_dma);
797 txd->frag.addrHi = 0;
798 first_txd->numDesc++;
802 len = skb_headlen(skb);
803 skb_dma = dma_map_single(&tp->tx_pdev->dev, skb->data, len,
805 txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
806 txd->len = cpu_to_le16(len);
807 txd->frag.addr = cpu_to_le32(skb_dma);
808 txd->frag.addrHi = 0;
809 first_txd->numDesc++;
811 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
812 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
815 txd = (struct tx_desc *) (txRing->ringBase +
817 typhoon_inc_tx_index(&txRing->lastWrite, 1);
819 len = skb_frag_size(frag);
820 frag_addr = skb_frag_address(frag);
821 skb_dma = dma_map_single(&tp->tx_pdev->dev, frag_addr,
823 txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
824 txd->len = cpu_to_le16(len);
825 txd->frag.addr = cpu_to_le32(skb_dma);
826 txd->frag.addrHi = 0;
827 first_txd->numDesc++;
834 iowrite32(txRing->lastWrite, tp->tx_ioaddr + txRing->writeRegister);
836 /* If we don't have room to put the worst case packet on the
837 * queue, then we must stop the queue. We need 2 extra
838 * descriptors -- one to prevent ring wrap, and one for the
841 numDesc = MAX_SKB_FRAGS + TSO_NUM_DESCRIPTORS + 1;
843 if (typhoon_num_free_tx(txRing) < (numDesc + 2)) {
844 netif_stop_queue(dev);
846 /* A Tx complete IRQ could have gotten between, making
847 * the ring free again. Only need to recheck here, since
850 if (typhoon_num_free_tx(txRing) >= (numDesc + 2))
851 netif_wake_queue(dev);
858 typhoon_set_rx_mode(struct net_device *dev)
860 struct typhoon *tp = netdev_priv(dev);
861 struct cmd_desc xp_cmd;
865 filter = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
866 if (dev->flags & IFF_PROMISC) {
867 filter |= TYPHOON_RX_FILTER_PROMISCOUS;
868 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
869 (dev->flags & IFF_ALLMULTI)) {
870 /* Too many to match, or accept all multicasts. */
871 filter |= TYPHOON_RX_FILTER_ALL_MCAST;
872 } else if (!netdev_mc_empty(dev)) {
873 struct netdev_hw_addr *ha;
875 memset(mc_filter, 0, sizeof(mc_filter));
876 netdev_for_each_mc_addr(ha, dev) {
877 int bit = ether_crc(ETH_ALEN, ha->addr) & 0x3f;
878 mc_filter[bit >> 5] |= 1 << (bit & 0x1f);
881 INIT_COMMAND_NO_RESPONSE(&xp_cmd,
882 TYPHOON_CMD_SET_MULTICAST_HASH);
883 xp_cmd.parm1 = TYPHOON_MCAST_HASH_SET;
884 xp_cmd.parm2 = cpu_to_le32(mc_filter[0]);
885 xp_cmd.parm3 = cpu_to_le32(mc_filter[1]);
886 typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
888 filter |= TYPHOON_RX_FILTER_MCAST_HASH;
891 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
892 xp_cmd.parm1 = filter;
893 typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
897 typhoon_do_get_stats(struct typhoon *tp)
899 struct net_device_stats *stats = &tp->dev->stats;
900 struct net_device_stats *saved = &tp->stats_saved;
901 struct cmd_desc xp_cmd;
902 struct resp_desc xp_resp[7];
903 struct stats_resp *s = (struct stats_resp *) xp_resp;
906 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_STATS);
907 err = typhoon_issue_command(tp, 1, &xp_cmd, 7, xp_resp);
911 /* 3Com's Linux driver uses txMultipleCollisions as it's
912 * collisions value, but there is some other collision info as well...
914 * The extra status reported would be a good candidate for
915 * ethtool_ops->get_{strings,stats}()
917 stats->tx_packets = le32_to_cpu(s->txPackets) +
919 stats->tx_bytes = le64_to_cpu(s->txBytes) +
921 stats->tx_errors = le32_to_cpu(s->txCarrierLost) +
923 stats->tx_carrier_errors = le32_to_cpu(s->txCarrierLost) +
924 saved->tx_carrier_errors;
925 stats->collisions = le32_to_cpu(s->txMultipleCollisions) +
927 stats->rx_packets = le32_to_cpu(s->rxPacketsGood) +
929 stats->rx_bytes = le64_to_cpu(s->rxBytesGood) +
931 stats->rx_fifo_errors = le32_to_cpu(s->rxFifoOverruns) +
932 saved->rx_fifo_errors;
933 stats->rx_errors = le32_to_cpu(s->rxFifoOverruns) +
934 le32_to_cpu(s->BadSSD) + le32_to_cpu(s->rxCrcErrors) +
936 stats->rx_crc_errors = le32_to_cpu(s->rxCrcErrors) +
937 saved->rx_crc_errors;
938 stats->rx_length_errors = le32_to_cpu(s->rxOversized) +
939 saved->rx_length_errors;
940 tp->speed = (s->linkStatus & TYPHOON_LINK_100MBPS) ?
941 SPEED_100 : SPEED_10;
942 tp->duplex = (s->linkStatus & TYPHOON_LINK_FULL_DUPLEX) ?
943 DUPLEX_FULL : DUPLEX_HALF;
948 static struct net_device_stats *
949 typhoon_get_stats(struct net_device *dev)
951 struct typhoon *tp = netdev_priv(dev);
952 struct net_device_stats *stats = &tp->dev->stats;
953 struct net_device_stats *saved = &tp->stats_saved;
956 if (tp->card_state == Sleeping)
959 if (typhoon_do_get_stats(tp) < 0) {
960 netdev_err(dev, "error getting stats\n");
968 typhoon_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
970 struct typhoon *tp = netdev_priv(dev);
971 struct pci_dev *pci_dev = tp->pdev;
972 struct cmd_desc xp_cmd;
973 struct resp_desc xp_resp[3];
976 if (tp->card_state == Sleeping) {
977 strlcpy(info->fw_version, "Sleep image",
978 sizeof(info->fw_version));
980 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
981 if (typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp) < 0) {
982 strlcpy(info->fw_version, "Unknown runtime",
983 sizeof(info->fw_version));
985 u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
986 snprintf(info->fw_version, sizeof(info->fw_version),
987 "%02x.%03x.%03x", sleep_ver >> 24,
988 (sleep_ver >> 12) & 0xfff, sleep_ver & 0xfff);
992 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
993 strlcpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
997 typhoon_get_link_ksettings(struct net_device *dev,
998 struct ethtool_link_ksettings *cmd)
1000 struct typhoon *tp = netdev_priv(dev);
1001 u32 supported, advertising = 0;
1003 supported = SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1006 switch (tp->xcvr_select) {
1007 case TYPHOON_XCVR_10HALF:
1008 advertising = ADVERTISED_10baseT_Half;
1010 case TYPHOON_XCVR_10FULL:
1011 advertising = ADVERTISED_10baseT_Full;
1013 case TYPHOON_XCVR_100HALF:
1014 advertising = ADVERTISED_100baseT_Half;
1016 case TYPHOON_XCVR_100FULL:
1017 advertising = ADVERTISED_100baseT_Full;
1019 case TYPHOON_XCVR_AUTONEG:
1020 advertising = ADVERTISED_10baseT_Half |
1021 ADVERTISED_10baseT_Full |
1022 ADVERTISED_100baseT_Half |
1023 ADVERTISED_100baseT_Full |
1028 if (tp->capabilities & TYPHOON_FIBER) {
1029 supported |= SUPPORTED_FIBRE;
1030 advertising |= ADVERTISED_FIBRE;
1031 cmd->base.port = PORT_FIBRE;
1033 supported |= SUPPORTED_10baseT_Half |
1034 SUPPORTED_10baseT_Full |
1036 advertising |= ADVERTISED_TP;
1037 cmd->base.port = PORT_TP;
1040 /* need to get stats to make these link speed/duplex valid */
1041 typhoon_do_get_stats(tp);
1042 cmd->base.speed = tp->speed;
1043 cmd->base.duplex = tp->duplex;
1044 cmd->base.phy_address = 0;
1045 if (tp->xcvr_select == TYPHOON_XCVR_AUTONEG)
1046 cmd->base.autoneg = AUTONEG_ENABLE;
1048 cmd->base.autoneg = AUTONEG_DISABLE;
1050 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1052 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1059 typhoon_set_link_ksettings(struct net_device *dev,
1060 const struct ethtool_link_ksettings *cmd)
1062 struct typhoon *tp = netdev_priv(dev);
1063 u32 speed = cmd->base.speed;
1064 struct cmd_desc xp_cmd;
1069 if (cmd->base.autoneg == AUTONEG_ENABLE) {
1070 xcvr = TYPHOON_XCVR_AUTONEG;
1072 if (cmd->base.duplex == DUPLEX_HALF) {
1073 if (speed == SPEED_10)
1074 xcvr = TYPHOON_XCVR_10HALF;
1075 else if (speed == SPEED_100)
1076 xcvr = TYPHOON_XCVR_100HALF;
1079 } else if (cmd->base.duplex == DUPLEX_FULL) {
1080 if (speed == SPEED_10)
1081 xcvr = TYPHOON_XCVR_10FULL;
1082 else if (speed == SPEED_100)
1083 xcvr = TYPHOON_XCVR_100FULL;
1090 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
1091 xp_cmd.parm1 = xcvr;
1092 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1096 tp->xcvr_select = xcvr;
1097 if (cmd->base.autoneg == AUTONEG_ENABLE) {
1098 tp->speed = 0xff; /* invalid */
1099 tp->duplex = 0xff; /* invalid */
1102 tp->duplex = cmd->base.duplex;
1110 typhoon_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1112 struct typhoon *tp = netdev_priv(dev);
1114 wol->supported = WAKE_PHY | WAKE_MAGIC;
1116 if (tp->wol_events & TYPHOON_WAKE_LINK_EVENT)
1117 wol->wolopts |= WAKE_PHY;
1118 if (tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
1119 wol->wolopts |= WAKE_MAGIC;
1120 memset(&wol->sopass, 0, sizeof(wol->sopass));
1124 typhoon_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1126 struct typhoon *tp = netdev_priv(dev);
1128 if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
1132 if (wol->wolopts & WAKE_PHY)
1133 tp->wol_events |= TYPHOON_WAKE_LINK_EVENT;
1134 if (wol->wolopts & WAKE_MAGIC)
1135 tp->wol_events |= TYPHOON_WAKE_MAGIC_PKT;
1141 typhoon_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering,
1142 struct kernel_ethtool_ringparam *kernel_ering,
1143 struct netlink_ext_ack *extack)
1145 ering->rx_max_pending = RXENT_ENTRIES;
1146 ering->tx_max_pending = TXLO_ENTRIES - 1;
1148 ering->rx_pending = RXENT_ENTRIES;
1149 ering->tx_pending = TXLO_ENTRIES - 1;
1152 static const struct ethtool_ops typhoon_ethtool_ops = {
1153 .get_drvinfo = typhoon_get_drvinfo,
1154 .get_wol = typhoon_get_wol,
1155 .set_wol = typhoon_set_wol,
1156 .get_link = ethtool_op_get_link,
1157 .get_ringparam = typhoon_get_ringparam,
1158 .get_link_ksettings = typhoon_get_link_ksettings,
1159 .set_link_ksettings = typhoon_set_link_ksettings,
1163 typhoon_wait_interrupt(void __iomem *ioaddr)
1167 for (i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
1168 if (ioread32(ioaddr + TYPHOON_REG_INTR_STATUS) &
1169 TYPHOON_INTR_BOOTCMD)
1171 udelay(TYPHOON_UDELAY);
1177 iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
1181 #define shared_offset(x) offsetof(struct typhoon_shared, x)
1184 typhoon_init_interface(struct typhoon *tp)
1186 struct typhoon_interface *iface = &tp->shared->iface;
1187 dma_addr_t shared_dma;
1189 memset(tp->shared, 0, sizeof(struct typhoon_shared));
1191 /* The *Hi members of iface are all init'd to zero by the memset().
1193 shared_dma = tp->shared_dma + shared_offset(indexes);
1194 iface->ringIndex = cpu_to_le32(shared_dma);
1196 shared_dma = tp->shared_dma + shared_offset(txLo);
1197 iface->txLoAddr = cpu_to_le32(shared_dma);
1198 iface->txLoSize = cpu_to_le32(TXLO_ENTRIES * sizeof(struct tx_desc));
1200 shared_dma = tp->shared_dma + shared_offset(txHi);
1201 iface->txHiAddr = cpu_to_le32(shared_dma);
1202 iface->txHiSize = cpu_to_le32(TXHI_ENTRIES * sizeof(struct tx_desc));
1204 shared_dma = tp->shared_dma + shared_offset(rxBuff);
1205 iface->rxBuffAddr = cpu_to_le32(shared_dma);
1206 iface->rxBuffSize = cpu_to_le32(RXFREE_ENTRIES *
1207 sizeof(struct rx_free));
1209 shared_dma = tp->shared_dma + shared_offset(rxLo);
1210 iface->rxLoAddr = cpu_to_le32(shared_dma);
1211 iface->rxLoSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
1213 shared_dma = tp->shared_dma + shared_offset(rxHi);
1214 iface->rxHiAddr = cpu_to_le32(shared_dma);
1215 iface->rxHiSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
1217 shared_dma = tp->shared_dma + shared_offset(cmd);
1218 iface->cmdAddr = cpu_to_le32(shared_dma);
1219 iface->cmdSize = cpu_to_le32(COMMAND_RING_SIZE);
1221 shared_dma = tp->shared_dma + shared_offset(resp);
1222 iface->respAddr = cpu_to_le32(shared_dma);
1223 iface->respSize = cpu_to_le32(RESPONSE_RING_SIZE);
1225 shared_dma = tp->shared_dma + shared_offset(zeroWord);
1226 iface->zeroAddr = cpu_to_le32(shared_dma);
1228 tp->indexes = &tp->shared->indexes;
1229 tp->txLoRing.ringBase = (u8 *) tp->shared->txLo;
1230 tp->txHiRing.ringBase = (u8 *) tp->shared->txHi;
1231 tp->rxLoRing.ringBase = (u8 *) tp->shared->rxLo;
1232 tp->rxHiRing.ringBase = (u8 *) tp->shared->rxHi;
1233 tp->rxBuffRing.ringBase = (u8 *) tp->shared->rxBuff;
1234 tp->cmdRing.ringBase = (u8 *) tp->shared->cmd;
1235 tp->respRing.ringBase = (u8 *) tp->shared->resp;
1237 tp->txLoRing.writeRegister = TYPHOON_REG_TX_LO_READY;
1238 tp->txHiRing.writeRegister = TYPHOON_REG_TX_HI_READY;
1240 tp->txlo_dma_addr = le32_to_cpu(iface->txLoAddr);
1241 tp->card_state = Sleeping;
1243 tp->offload = TYPHOON_OFFLOAD_IP_CHKSUM | TYPHOON_OFFLOAD_TCP_CHKSUM;
1244 tp->offload |= TYPHOON_OFFLOAD_UDP_CHKSUM | TSO_OFFLOAD_ON;
1245 tp->offload |= TYPHOON_OFFLOAD_VLAN;
1247 spin_lock_init(&tp->command_lock);
1249 /* Force the writes to the shared memory area out before continuing. */
1254 typhoon_init_rings(struct typhoon *tp)
1256 memset(tp->indexes, 0, sizeof(struct typhoon_indexes));
1258 tp->txLoRing.lastWrite = 0;
1259 tp->txHiRing.lastWrite = 0;
1260 tp->rxLoRing.lastWrite = 0;
1261 tp->rxHiRing.lastWrite = 0;
1262 tp->rxBuffRing.lastWrite = 0;
1263 tp->cmdRing.lastWrite = 0;
1264 tp->respRing.lastWrite = 0;
1266 tp->txLoRing.lastRead = 0;
1267 tp->txHiRing.lastRead = 0;
1270 static const struct firmware *typhoon_fw;
1273 typhoon_request_firmware(struct typhoon *tp)
1275 const struct typhoon_file_header *fHdr;
1276 const struct typhoon_section_header *sHdr;
1277 const u8 *image_data;
1286 err = request_firmware(&typhoon_fw, FIRMWARE_NAME, &tp->pdev->dev);
1288 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
1293 image_data = typhoon_fw->data;
1294 remaining = typhoon_fw->size;
1295 if (remaining < sizeof(struct typhoon_file_header))
1298 fHdr = (struct typhoon_file_header *) image_data;
1299 if (memcmp(fHdr->tag, "TYPHOON", 8))
1302 numSections = le32_to_cpu(fHdr->numSections);
1303 image_data += sizeof(struct typhoon_file_header);
1304 remaining -= sizeof(struct typhoon_file_header);
1306 while (numSections--) {
1307 if (remaining < sizeof(struct typhoon_section_header))
1310 sHdr = (struct typhoon_section_header *) image_data;
1311 image_data += sizeof(struct typhoon_section_header);
1312 section_len = le32_to_cpu(sHdr->len);
1314 if (remaining < section_len)
1317 image_data += section_len;
1318 remaining -= section_len;
1324 netdev_err(tp->dev, "Invalid firmware image\n");
1325 release_firmware(typhoon_fw);
1331 typhoon_download_firmware(struct typhoon *tp)
1333 void __iomem *ioaddr = tp->ioaddr;
1334 struct pci_dev *pdev = tp->pdev;
1335 const struct typhoon_file_header *fHdr;
1336 const struct typhoon_section_header *sHdr;
1337 const u8 *image_data;
1339 dma_addr_t dpage_dma;
1351 image_data = typhoon_fw->data;
1352 fHdr = (struct typhoon_file_header *) image_data;
1354 /* Cannot just map the firmware image using dma_map_single() as
1355 * the firmware is vmalloc()'d and may not be physically contiguous,
1356 * so we allocate some coherent memory to copy the sections into.
1359 dpage = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &dpage_dma, GFP_ATOMIC);
1361 netdev_err(tp->dev, "no DMA mem for firmware\n");
1365 irqEnabled = ioread32(ioaddr + TYPHOON_REG_INTR_ENABLE);
1366 iowrite32(irqEnabled | TYPHOON_INTR_BOOTCMD,
1367 ioaddr + TYPHOON_REG_INTR_ENABLE);
1368 irqMasked = ioread32(ioaddr + TYPHOON_REG_INTR_MASK);
1369 iowrite32(irqMasked | TYPHOON_INTR_BOOTCMD,
1370 ioaddr + TYPHOON_REG_INTR_MASK);
1373 if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
1374 netdev_err(tp->dev, "card ready timeout\n");
1378 numSections = le32_to_cpu(fHdr->numSections);
1379 load_addr = le32_to_cpu(fHdr->startAddr);
1381 iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
1382 iowrite32(load_addr, ioaddr + TYPHOON_REG_DOWNLOAD_BOOT_ADDR);
1383 hmac = le32_to_cpu(fHdr->hmacDigest[0]);
1384 iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_0);
1385 hmac = le32_to_cpu(fHdr->hmacDigest[1]);
1386 iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_1);
1387 hmac = le32_to_cpu(fHdr->hmacDigest[2]);
1388 iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_2);
1389 hmac = le32_to_cpu(fHdr->hmacDigest[3]);
1390 iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_3);
1391 hmac = le32_to_cpu(fHdr->hmacDigest[4]);
1392 iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_4);
1393 typhoon_post_pci_writes(ioaddr);
1394 iowrite32(TYPHOON_BOOTCMD_RUNTIME_IMAGE, ioaddr + TYPHOON_REG_COMMAND);
1396 image_data += sizeof(struct typhoon_file_header);
1398 /* The ioread32() in typhoon_wait_interrupt() will force the
1399 * last write to the command register to post, so
1400 * we don't need a typhoon_post_pci_writes() after it.
1402 for (i = 0; i < numSections; i++) {
1403 sHdr = (struct typhoon_section_header *) image_data;
1404 image_data += sizeof(struct typhoon_section_header);
1405 load_addr = le32_to_cpu(sHdr->startAddr);
1406 section_len = le32_to_cpu(sHdr->len);
1408 while (section_len) {
1409 len = min_t(u32, section_len, PAGE_SIZE);
1411 if (typhoon_wait_interrupt(ioaddr) < 0 ||
1412 ioread32(ioaddr + TYPHOON_REG_STATUS) !=
1413 TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
1414 netdev_err(tp->dev, "segment ready timeout\n");
1418 /* Do an pseudo IPv4 checksum on the data -- first
1419 * need to convert each u16 to cpu order before
1420 * summing. Fortunately, due to the properties of
1421 * the checksum, we can do this once, at the end.
1423 csum = csum_fold(csum_partial_copy_nocheck(image_data,
1426 iowrite32(len, ioaddr + TYPHOON_REG_BOOT_LENGTH);
1427 iowrite32(le16_to_cpu((__force __le16)csum),
1428 ioaddr + TYPHOON_REG_BOOT_CHECKSUM);
1429 iowrite32(load_addr,
1430 ioaddr + TYPHOON_REG_BOOT_DEST_ADDR);
1431 iowrite32(0, ioaddr + TYPHOON_REG_BOOT_DATA_HI);
1432 iowrite32(dpage_dma, ioaddr + TYPHOON_REG_BOOT_DATA_LO);
1433 typhoon_post_pci_writes(ioaddr);
1434 iowrite32(TYPHOON_BOOTCMD_SEG_AVAILABLE,
1435 ioaddr + TYPHOON_REG_COMMAND);
1443 if (typhoon_wait_interrupt(ioaddr) < 0 ||
1444 ioread32(ioaddr + TYPHOON_REG_STATUS) !=
1445 TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
1446 netdev_err(tp->dev, "final segment ready timeout\n");
1450 iowrite32(TYPHOON_BOOTCMD_DNLD_COMPLETE, ioaddr + TYPHOON_REG_COMMAND);
1452 if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
1453 netdev_err(tp->dev, "boot ready timeout, status 0x%0x\n",
1454 ioread32(ioaddr + TYPHOON_REG_STATUS));
1461 iowrite32(irqMasked, ioaddr + TYPHOON_REG_INTR_MASK);
1462 iowrite32(irqEnabled, ioaddr + TYPHOON_REG_INTR_ENABLE);
1464 dma_free_coherent(&pdev->dev, PAGE_SIZE, dpage, dpage_dma);
1471 typhoon_boot_3XP(struct typhoon *tp, u32 initial_status)
1473 void __iomem *ioaddr = tp->ioaddr;
1475 if (typhoon_wait_status(ioaddr, initial_status) < 0) {
1476 netdev_err(tp->dev, "boot ready timeout\n");
1480 iowrite32(0, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_HI);
1481 iowrite32(tp->shared_dma, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_LO);
1482 typhoon_post_pci_writes(ioaddr);
1483 iowrite32(TYPHOON_BOOTCMD_REG_BOOT_RECORD,
1484 ioaddr + TYPHOON_REG_COMMAND);
1486 if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_RUNNING) < 0) {
1487 netdev_err(tp->dev, "boot finish timeout (status 0x%x)\n",
1488 ioread32(ioaddr + TYPHOON_REG_STATUS));
1492 /* Clear the Transmit and Command ready registers
1494 iowrite32(0, ioaddr + TYPHOON_REG_TX_HI_READY);
1495 iowrite32(0, ioaddr + TYPHOON_REG_CMD_READY);
1496 iowrite32(0, ioaddr + TYPHOON_REG_TX_LO_READY);
1497 typhoon_post_pci_writes(ioaddr);
1498 iowrite32(TYPHOON_BOOTCMD_BOOT, ioaddr + TYPHOON_REG_COMMAND);
1507 typhoon_clean_tx(struct typhoon *tp, struct transmit_ring *txRing,
1508 volatile __le32 * index)
1510 u32 lastRead = txRing->lastRead;
1516 while (lastRead != le32_to_cpu(*index)) {
1517 tx = (struct tx_desc *) (txRing->ringBase + lastRead);
1518 type = tx->flags & TYPHOON_TYPE_MASK;
1520 if (type == TYPHOON_TX_DESC) {
1521 /* This tx_desc describes a packet.
1523 unsigned long ptr = tx->tx_addr;
1524 struct sk_buff *skb = (struct sk_buff *) ptr;
1525 dev_kfree_skb_irq(skb);
1526 } else if (type == TYPHOON_FRAG_DESC) {
1527 /* This tx_desc describes a memory mapping. Free it.
1529 skb_dma = (dma_addr_t) le32_to_cpu(tx->frag.addr);
1530 dma_len = le16_to_cpu(tx->len);
1531 dma_unmap_single(&tp->pdev->dev, skb_dma, dma_len,
1536 typhoon_inc_tx_index(&lastRead, 1);
1543 typhoon_tx_complete(struct typhoon *tp, struct transmit_ring *txRing,
1544 volatile __le32 * index)
1547 int numDesc = MAX_SKB_FRAGS + 1;
1549 /* This will need changing if we start to use the Hi Tx ring. */
1550 lastRead = typhoon_clean_tx(tp, txRing, index);
1551 if (netif_queue_stopped(tp->dev) && typhoon_num_free(txRing->lastWrite,
1552 lastRead, TXLO_ENTRIES) > (numDesc + 2))
1553 netif_wake_queue(tp->dev);
1555 txRing->lastRead = lastRead;
1560 typhoon_recycle_rx_skb(struct typhoon *tp, u32 idx)
1562 struct typhoon_indexes *indexes = tp->indexes;
1563 struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
1564 struct basic_ring *ring = &tp->rxBuffRing;
1567 if ((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
1568 le32_to_cpu(indexes->rxBuffCleared)) {
1569 /* no room in ring, just drop the skb
1571 dev_kfree_skb_any(rxb->skb);
1576 r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
1577 typhoon_inc_rxfree_index(&ring->lastWrite, 1);
1579 r->physAddr = cpu_to_le32(rxb->dma_addr);
1581 /* Tell the card about it */
1583 indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
1587 typhoon_alloc_rx_skb(struct typhoon *tp, u32 idx)
1589 struct typhoon_indexes *indexes = tp->indexes;
1590 struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
1591 struct basic_ring *ring = &tp->rxBuffRing;
1593 struct sk_buff *skb;
1594 dma_addr_t dma_addr;
1598 if ((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
1599 le32_to_cpu(indexes->rxBuffCleared))
1602 skb = netdev_alloc_skb(tp->dev, PKT_BUF_SZ);
1607 /* Please, 3com, fix the firmware to allow DMA to a unaligned
1608 * address! Pretty please?
1610 skb_reserve(skb, 2);
1613 dma_addr = dma_map_single(&tp->pdev->dev, skb->data, PKT_BUF_SZ,
1616 /* Since no card does 64 bit DAC, the high bits will never
1619 r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
1620 typhoon_inc_rxfree_index(&ring->lastWrite, 1);
1622 r->physAddr = cpu_to_le32(dma_addr);
1624 rxb->dma_addr = dma_addr;
1626 /* Tell the card about it */
1628 indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
1633 typhoon_rx(struct typhoon *tp, struct basic_ring *rxRing, volatile __le32 * ready,
1634 volatile __le32 * cleared, int budget)
1637 struct sk_buff *skb, *new_skb;
1638 struct rxbuff_ent *rxb;
1639 dma_addr_t dma_addr;
1648 local_ready = le32_to_cpu(*ready);
1649 rxaddr = le32_to_cpu(*cleared);
1650 while (rxaddr != local_ready && budget > 0) {
1651 rx = (struct rx_desc *) (rxRing->ringBase + rxaddr);
1653 rxb = &tp->rxbuffers[idx];
1655 dma_addr = rxb->dma_addr;
1657 typhoon_inc_rx_index(&rxaddr, 1);
1659 if (rx->flags & TYPHOON_RX_ERROR) {
1660 typhoon_recycle_rx_skb(tp, idx);
1664 pkt_len = le16_to_cpu(rx->frameLen);
1666 if (pkt_len < rx_copybreak &&
1667 (new_skb = netdev_alloc_skb(tp->dev, pkt_len + 2)) != NULL) {
1668 skb_reserve(new_skb, 2);
1669 dma_sync_single_for_cpu(&tp->pdev->dev, dma_addr,
1670 PKT_BUF_SZ, DMA_FROM_DEVICE);
1671 skb_copy_to_linear_data(new_skb, skb->data, pkt_len);
1672 dma_sync_single_for_device(&tp->pdev->dev, dma_addr,
1675 skb_put(new_skb, pkt_len);
1676 typhoon_recycle_rx_skb(tp, idx);
1679 skb_put(new_skb, pkt_len);
1680 dma_unmap_single(&tp->pdev->dev, dma_addr, PKT_BUF_SZ,
1682 typhoon_alloc_rx_skb(tp, idx);
1684 new_skb->protocol = eth_type_trans(new_skb, tp->dev);
1685 csum_bits = rx->rxStatus & (TYPHOON_RX_IP_CHK_GOOD |
1686 TYPHOON_RX_UDP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD);
1688 (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD) ||
1690 (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_UDP_CHK_GOOD)) {
1691 new_skb->ip_summed = CHECKSUM_UNNECESSARY;
1693 skb_checksum_none_assert(new_skb);
1695 if (rx->rxStatus & TYPHOON_RX_VLAN)
1696 __vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
1697 ntohl(rx->vlanTag) & 0xffff);
1698 netif_receive_skb(new_skb);
1703 *cleared = cpu_to_le32(rxaddr);
1709 typhoon_fill_free_ring(struct typhoon *tp)
1713 for (i = 0; i < RXENT_ENTRIES; i++) {
1714 struct rxbuff_ent *rxb = &tp->rxbuffers[i];
1717 if (typhoon_alloc_rx_skb(tp, i) < 0)
1723 typhoon_poll(struct napi_struct *napi, int budget)
1725 struct typhoon *tp = container_of(napi, struct typhoon, napi);
1726 struct typhoon_indexes *indexes = tp->indexes;
1730 if (!tp->awaiting_resp && indexes->respReady != indexes->respCleared)
1731 typhoon_process_response(tp, 0, NULL);
1733 if (le32_to_cpu(indexes->txLoCleared) != tp->txLoRing.lastRead)
1734 typhoon_tx_complete(tp, &tp->txLoRing, &indexes->txLoCleared);
1738 if (indexes->rxHiCleared != indexes->rxHiReady) {
1739 work_done += typhoon_rx(tp, &tp->rxHiRing, &indexes->rxHiReady,
1740 &indexes->rxHiCleared, budget);
1743 if (indexes->rxLoCleared != indexes->rxLoReady) {
1744 work_done += typhoon_rx(tp, &tp->rxLoRing, &indexes->rxLoReady,
1745 &indexes->rxLoCleared, budget - work_done);
1748 if (le32_to_cpu(indexes->rxBuffCleared) == tp->rxBuffRing.lastWrite) {
1749 /* rxBuff ring is empty, try to fill it. */
1750 typhoon_fill_free_ring(tp);
1753 if (work_done < budget) {
1754 napi_complete_done(napi, work_done);
1755 iowrite32(TYPHOON_INTR_NONE,
1756 tp->ioaddr + TYPHOON_REG_INTR_MASK);
1757 typhoon_post_pci_writes(tp->ioaddr);
1764 typhoon_interrupt(int irq, void *dev_instance)
1766 struct net_device *dev = dev_instance;
1767 struct typhoon *tp = netdev_priv(dev);
1768 void __iomem *ioaddr = tp->ioaddr;
1771 intr_status = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
1772 if (!(intr_status & TYPHOON_INTR_HOST_INT))
1775 iowrite32(intr_status, ioaddr + TYPHOON_REG_INTR_STATUS);
1777 if (napi_schedule_prep(&tp->napi)) {
1778 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
1779 typhoon_post_pci_writes(ioaddr);
1780 __napi_schedule(&tp->napi);
1782 netdev_err(dev, "Error, poll already scheduled\n");
1788 typhoon_free_rx_rings(struct typhoon *tp)
1792 for (i = 0; i < RXENT_ENTRIES; i++) {
1793 struct rxbuff_ent *rxb = &tp->rxbuffers[i];
1795 dma_unmap_single(&tp->pdev->dev, rxb->dma_addr,
1796 PKT_BUF_SZ, DMA_FROM_DEVICE);
1797 dev_kfree_skb(rxb->skb);
1804 typhoon_sleep_early(struct typhoon *tp, __le16 events)
1806 void __iomem *ioaddr = tp->ioaddr;
1807 struct cmd_desc xp_cmd;
1810 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_ENABLE_WAKE_EVENTS);
1811 xp_cmd.parm1 = events;
1812 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1814 netdev_err(tp->dev, "typhoon_sleep(): wake events cmd err %d\n",
1819 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_GOTO_SLEEP);
1820 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1822 netdev_err(tp->dev, "typhoon_sleep(): sleep cmd err %d\n", err);
1826 if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_SLEEPING) < 0)
1829 /* Since we cannot monitor the status of the link while sleeping,
1830 * tell the world it went away.
1832 netif_carrier_off(tp->dev);
1838 typhoon_sleep(struct typhoon *tp, pci_power_t state, __le16 events)
1842 err = typhoon_sleep_early(tp, events);
1847 pci_enable_wake(tp->pdev, state, 1);
1848 pci_disable_device(tp->pdev);
1849 return pci_set_power_state(tp->pdev, state);
1853 typhoon_wakeup(struct typhoon *tp, int wait_type)
1855 void __iomem *ioaddr = tp->ioaddr;
1857 /* Post 2.x.x versions of the Sleep Image require a reset before
1858 * we can download the Runtime Image. But let's not make users of
1859 * the old firmware pay for the reset.
1861 iowrite32(TYPHOON_BOOTCMD_WAKEUP, ioaddr + TYPHOON_REG_COMMAND);
1862 if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0 ||
1863 (tp->capabilities & TYPHOON_WAKEUP_NEEDS_RESET))
1864 return typhoon_reset(ioaddr, wait_type);
1870 typhoon_start_runtime(struct typhoon *tp)
1872 struct net_device *dev = tp->dev;
1873 void __iomem *ioaddr = tp->ioaddr;
1874 struct cmd_desc xp_cmd;
1877 typhoon_init_rings(tp);
1878 typhoon_fill_free_ring(tp);
1880 err = typhoon_download_firmware(tp);
1882 netdev_err(tp->dev, "cannot load runtime on 3XP\n");
1886 if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
1887 netdev_err(tp->dev, "cannot boot 3XP\n");
1892 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAX_PKT_SIZE);
1893 xp_cmd.parm1 = cpu_to_le16(PKT_BUF_SZ);
1894 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1898 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
1899 xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
1900 xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
1901 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1905 /* Disable IRQ coalescing -- we can reenable it when 3Com gives
1906 * us some more information on how to control it.
1908 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_IRQ_COALESCE_CTRL);
1910 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1914 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
1915 xp_cmd.parm1 = tp->xcvr_select;
1916 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1920 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_VLAN_TYPE_WRITE);
1921 xp_cmd.parm1 = cpu_to_le16(ETH_P_8021Q);
1922 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1926 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_OFFLOAD_TASKS);
1927 xp_cmd.parm2 = tp->offload;
1928 xp_cmd.parm3 = tp->offload;
1929 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1933 typhoon_set_rx_mode(dev);
1935 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_ENABLE);
1936 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1940 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_ENABLE);
1941 err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1945 tp->card_state = Running;
1948 iowrite32(TYPHOON_INTR_ENABLE_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
1949 iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_MASK);
1950 typhoon_post_pci_writes(ioaddr);
1955 typhoon_reset(ioaddr, WaitNoSleep);
1956 typhoon_free_rx_rings(tp);
1957 typhoon_init_rings(tp);
1962 typhoon_stop_runtime(struct typhoon *tp, int wait_type)
1964 struct typhoon_indexes *indexes = tp->indexes;
1965 struct transmit_ring *txLo = &tp->txLoRing;
1966 void __iomem *ioaddr = tp->ioaddr;
1967 struct cmd_desc xp_cmd;
1970 /* Disable interrupts early, since we can't schedule a poll
1971 * when called with !netif_running(). This will be posted
1972 * when we force the posting of the command.
1974 iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
1976 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_DISABLE);
1977 typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1979 /* Wait 1/2 sec for any outstanding transmits to occur
1980 * We'll cleanup after the reset if this times out.
1982 for (i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
1983 if (indexes->txLoCleared == cpu_to_le32(txLo->lastWrite))
1985 udelay(TYPHOON_UDELAY);
1988 if (i == TYPHOON_WAIT_TIMEOUT)
1989 netdev_err(tp->dev, "halt timed out waiting for Tx to complete\n");
1991 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_DISABLE);
1992 typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1994 /* save the statistics so when we bring the interface up again,
1995 * the values reported to userspace are correct.
1997 tp->card_state = Sleeping;
1999 typhoon_do_get_stats(tp);
2000 memcpy(&tp->stats_saved, &tp->dev->stats, sizeof(struct net_device_stats));
2002 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_HALT);
2003 typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
2005 if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_HALTED) < 0)
2006 netdev_err(tp->dev, "timed out waiting for 3XP to halt\n");
2008 if (typhoon_reset(ioaddr, wait_type) < 0) {
2009 netdev_err(tp->dev, "unable to reset 3XP\n");
2013 /* cleanup any outstanding Tx packets */
2014 if (indexes->txLoCleared != cpu_to_le32(txLo->lastWrite)) {
2015 indexes->txLoCleared = cpu_to_le32(txLo->lastWrite);
2016 typhoon_clean_tx(tp, &tp->txLoRing, &indexes->txLoCleared);
2023 typhoon_tx_timeout(struct net_device *dev, unsigned int txqueue)
2025 struct typhoon *tp = netdev_priv(dev);
2027 if (typhoon_reset(tp->ioaddr, WaitNoSleep) < 0) {
2028 netdev_warn(dev, "could not reset in tx timeout\n");
2032 /* If we ever start using the Hi ring, it will need cleaning too */
2033 typhoon_clean_tx(tp, &tp->txLoRing, &tp->indexes->txLoCleared);
2034 typhoon_free_rx_rings(tp);
2036 if (typhoon_start_runtime(tp) < 0) {
2037 netdev_err(dev, "could not start runtime in tx timeout\n");
2041 netif_wake_queue(dev);
2045 /* Reset the hardware, and turn off carrier to avoid more timeouts */
2046 typhoon_reset(tp->ioaddr, NoWait);
2047 netif_carrier_off(dev);
2051 typhoon_open(struct net_device *dev)
2053 struct typhoon *tp = netdev_priv(dev);
2056 err = typhoon_request_firmware(tp);
2060 pci_set_power_state(tp->pdev, PCI_D0);
2061 pci_restore_state(tp->pdev);
2063 err = typhoon_wakeup(tp, WaitSleep);
2065 netdev_err(dev, "unable to wakeup device\n");
2069 err = request_irq(dev->irq, typhoon_interrupt, IRQF_SHARED,
2074 napi_enable(&tp->napi);
2076 err = typhoon_start_runtime(tp);
2078 napi_disable(&tp->napi);
2082 netif_start_queue(dev);
2086 free_irq(dev->irq, dev);
2089 if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
2090 netdev_err(dev, "unable to reboot into sleep img\n");
2091 typhoon_reset(tp->ioaddr, NoWait);
2095 if (typhoon_sleep(tp, PCI_D3hot, 0) < 0)
2096 netdev_err(dev, "unable to go back to sleep\n");
2103 typhoon_close(struct net_device *dev)
2105 struct typhoon *tp = netdev_priv(dev);
2107 netif_stop_queue(dev);
2108 napi_disable(&tp->napi);
2110 if (typhoon_stop_runtime(tp, WaitSleep) < 0)
2111 netdev_err(dev, "unable to stop runtime\n");
2113 /* Make sure there is no irq handler running on a different CPU. */
2114 free_irq(dev->irq, dev);
2116 typhoon_free_rx_rings(tp);
2117 typhoon_init_rings(tp);
2119 if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0)
2120 netdev_err(dev, "unable to boot sleep image\n");
2122 if (typhoon_sleep(tp, PCI_D3hot, 0) < 0)
2123 netdev_err(dev, "unable to put card to sleep\n");
2128 static int __maybe_unused
2129 typhoon_resume(struct device *dev_d)
2131 struct net_device *dev = dev_get_drvdata(dev_d);
2132 struct typhoon *tp = netdev_priv(dev);
2134 /* If we're down, resume when we are upped.
2136 if (!netif_running(dev))
2139 if (typhoon_wakeup(tp, WaitNoSleep) < 0) {
2140 netdev_err(dev, "critical: could not wake up in resume\n");
2144 if (typhoon_start_runtime(tp) < 0) {
2145 netdev_err(dev, "critical: could not start runtime in resume\n");
2149 netif_device_attach(dev);
2153 typhoon_reset(tp->ioaddr, NoWait);
2157 static int __maybe_unused
2158 typhoon_suspend(struct device *dev_d)
2160 struct pci_dev *pdev = to_pci_dev(dev_d);
2161 struct net_device *dev = pci_get_drvdata(pdev);
2162 struct typhoon *tp = netdev_priv(dev);
2163 struct cmd_desc xp_cmd;
2165 /* If we're down, we're already suspended.
2167 if (!netif_running(dev))
2170 /* TYPHOON_OFFLOAD_VLAN is always on now, so this doesn't work */
2171 if (tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
2172 netdev_warn(dev, "cannot do WAKE_MAGIC with VLAN offloading\n");
2174 netif_device_detach(dev);
2176 if (typhoon_stop_runtime(tp, WaitNoSleep) < 0) {
2177 netdev_err(dev, "unable to stop runtime\n");
2181 typhoon_free_rx_rings(tp);
2182 typhoon_init_rings(tp);
2184 if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
2185 netdev_err(dev, "unable to boot sleep image\n");
2189 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
2190 xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
2191 xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
2192 if (typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
2193 netdev_err(dev, "unable to set mac address in suspend\n");
2197 INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
2198 xp_cmd.parm1 = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
2199 if (typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
2200 netdev_err(dev, "unable to set rx filter in suspend\n");
2204 if (typhoon_sleep_early(tp, tp->wol_events) < 0) {
2205 netdev_err(dev, "unable to put card to sleep\n");
2209 device_wakeup_enable(dev_d);
2214 typhoon_resume(dev_d);
2219 typhoon_test_mmio(struct pci_dev *pdev)
2221 void __iomem *ioaddr = pci_iomap(pdev, 1, 128);
2228 if (ioread32(ioaddr + TYPHOON_REG_STATUS) !=
2229 TYPHOON_STATUS_WAITING_FOR_HOST)
2232 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
2233 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
2234 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
2236 /* Ok, see if we can change our interrupt status register by
2237 * sending ourselves an interrupt. If so, then MMIO works.
2238 * The 50usec delay is arbitrary -- it could probably be smaller.
2240 val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2241 if ((val & TYPHOON_INTR_SELF) == 0) {
2242 iowrite32(1, ioaddr + TYPHOON_REG_SELF_INTERRUPT);
2243 ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2245 val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2246 if (val & TYPHOON_INTR_SELF)
2250 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
2251 iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
2252 iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
2253 ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2256 pci_iounmap(pdev, ioaddr);
2260 pr_info("%s: falling back to port IO\n", pci_name(pdev));
2264 static const struct net_device_ops typhoon_netdev_ops = {
2265 .ndo_open = typhoon_open,
2266 .ndo_stop = typhoon_close,
2267 .ndo_start_xmit = typhoon_start_tx,
2268 .ndo_set_rx_mode = typhoon_set_rx_mode,
2269 .ndo_tx_timeout = typhoon_tx_timeout,
2270 .ndo_get_stats = typhoon_get_stats,
2271 .ndo_validate_addr = eth_validate_addr,
2272 .ndo_set_mac_address = eth_mac_addr,
2276 typhoon_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2278 struct net_device *dev;
2280 int card_id = (int) ent->driver_data;
2281 void __iomem *ioaddr;
2283 dma_addr_t shared_dma;
2284 struct cmd_desc xp_cmd;
2285 struct resp_desc xp_resp[3];
2287 const char *err_msg;
2289 dev = alloc_etherdev(sizeof(*tp));
2291 err_msg = "unable to alloc new net device";
2295 SET_NETDEV_DEV(dev, &pdev->dev);
2297 err = pci_enable_device(pdev);
2299 err_msg = "unable to enable device";
2303 err = pci_set_mwi(pdev);
2305 err_msg = "unable to set MWI";
2306 goto error_out_disable;
2309 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
2311 err_msg = "No usable DMA configuration";
2315 /* sanity checks on IO and MMIO BARs
2317 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
2318 err_msg = "region #1 not a PCI IO resource, aborting";
2322 if (pci_resource_len(pdev, 0) < 128) {
2323 err_msg = "Invalid PCI IO region size, aborting";
2327 if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
2328 err_msg = "region #1 not a PCI MMIO resource, aborting";
2332 if (pci_resource_len(pdev, 1) < 128) {
2333 err_msg = "Invalid PCI MMIO region size, aborting";
2338 err = pci_request_regions(pdev, KBUILD_MODNAME);
2340 err_msg = "could not request regions";
2344 /* map our registers
2346 if (use_mmio != 0 && use_mmio != 1)
2347 use_mmio = typhoon_test_mmio(pdev);
2349 ioaddr = pci_iomap(pdev, use_mmio, 128);
2351 err_msg = "cannot remap registers, aborting";
2353 goto error_out_regions;
2356 /* allocate pci dma space for rx and tx descriptor rings
2358 shared = dma_alloc_coherent(&pdev->dev, sizeof(struct typhoon_shared),
2359 &shared_dma, GFP_KERNEL);
2361 err_msg = "could not allocate DMA memory";
2363 goto error_out_remap;
2366 dev->irq = pdev->irq;
2367 tp = netdev_priv(dev);
2368 tp->shared = shared;
2369 tp->shared_dma = shared_dma;
2372 tp->ioaddr = ioaddr;
2373 tp->tx_ioaddr = ioaddr;
2377 * 1) Reset the adapter to clear any bad juju
2378 * 2) Reload the sleep image
2379 * 3) Boot the sleep image
2380 * 4) Get the hardware address.
2381 * 5) Put the card to sleep.
2383 err = typhoon_reset(ioaddr, WaitSleep);
2385 err_msg = "could not reset 3XP";
2389 /* Now that we've reset the 3XP and are sure it's not going to
2390 * write all over memory, enable bus mastering, and save our
2391 * state for resuming after a suspend.
2393 pci_set_master(pdev);
2394 pci_save_state(pdev);
2396 typhoon_init_interface(tp);
2397 typhoon_init_rings(tp);
2399 err = typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST);
2401 err_msg = "cannot boot 3XP sleep image";
2402 goto error_out_reset;
2405 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_MAC_ADDRESS);
2406 err = typhoon_issue_command(tp, 1, &xp_cmd, 1, xp_resp);
2408 err_msg = "cannot read MAC address";
2409 goto error_out_reset;
2412 *(__be16 *)&dev->dev_addr[0] = htons(le16_to_cpu(xp_resp[0].parm1));
2413 *(__be32 *)&dev->dev_addr[2] = htonl(le32_to_cpu(xp_resp[0].parm2));
2415 if (!is_valid_ether_addr(dev->dev_addr)) {
2416 err_msg = "Could not obtain valid ethernet address, aborting";
2418 goto error_out_reset;
2421 /* Read the Sleep Image version last, so the response is valid
2422 * later when we print out the version reported.
2424 INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
2425 err = typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp);
2427 err_msg = "Could not get Sleep Image version";
2428 goto error_out_reset;
2431 tp->capabilities = typhoon_card_info[card_id].capabilities;
2432 tp->xcvr_select = TYPHOON_XCVR_AUTONEG;
2434 /* Typhoon 1.0 Sleep Images return one response descriptor to the
2435 * READ_VERSIONS command. Those versions are OK after waking up
2436 * from sleep without needing a reset. Typhoon 1.1+ Sleep Images
2437 * seem to need a little extra help to get started. Since we don't
2438 * know how to nudge it along, just kick it.
2440 if (xp_resp[0].numDesc != 0)
2441 tp->capabilities |= TYPHOON_WAKEUP_NEEDS_RESET;
2443 err = typhoon_sleep(tp, PCI_D3hot, 0);
2445 err_msg = "cannot put adapter to sleep";
2446 goto error_out_reset;
2449 /* The chip-specific entries in the device structure. */
2450 dev->netdev_ops = &typhoon_netdev_ops;
2451 netif_napi_add(dev, &tp->napi, typhoon_poll, 16);
2452 dev->watchdog_timeo = TX_TIMEOUT;
2454 dev->ethtool_ops = &typhoon_ethtool_ops;
2456 /* We can handle scatter gather, up to 16 entries, and
2457 * we can do IP checksumming (only version 4, doh...)
2459 * There's no way to turn off the RX VLAN offloading and stripping
2460 * on the current 3XP firmware -- it does not respect the offload
2461 * settings -- so we only allow the user to toggle the TX processing.
2463 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
2464 NETIF_F_HW_VLAN_CTAG_TX;
2465 dev->features = dev->hw_features |
2466 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_RXCSUM;
2468 err = register_netdev(dev);
2470 err_msg = "unable to register netdev";
2471 goto error_out_reset;
2474 pci_set_drvdata(pdev, dev);
2476 netdev_info(dev, "%s at %s 0x%llx, %pM\n",
2477 typhoon_card_info[card_id].name,
2478 use_mmio ? "MMIO" : "IO",
2479 (unsigned long long)pci_resource_start(pdev, use_mmio),
2482 /* xp_resp still contains the response to the READ_VERSIONS command.
2483 * For debugging, let the user know what version he has.
2485 if (xp_resp[0].numDesc == 0) {
2486 /* This is the Typhoon 1.0 type Sleep Image, last 16 bits
2487 * of version is Month/Day of build.
2489 u16 monthday = le32_to_cpu(xp_resp[0].parm2) & 0xffff;
2490 netdev_info(dev, "Typhoon 1.0 Sleep Image built %02u/%02u/2000\n",
2491 monthday >> 8, monthday & 0xff);
2492 } else if (xp_resp[0].numDesc == 2) {
2493 /* This is the Typhoon 1.1+ type Sleep Image
2495 u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
2496 u8 *ver_string = (u8 *) &xp_resp[1];
2498 netdev_info(dev, "Typhoon 1.1+ Sleep Image version %02x.%03x.%03x %s\n",
2499 sleep_ver >> 24, (sleep_ver >> 12) & 0xfff,
2500 sleep_ver & 0xfff, ver_string);
2502 netdev_warn(dev, "Unknown Sleep Image version (%u:%04x)\n",
2503 xp_resp[0].numDesc, le32_to_cpu(xp_resp[0].parm2));
2509 typhoon_reset(ioaddr, NoWait);
2512 dma_free_coherent(&pdev->dev, sizeof(struct typhoon_shared), shared,
2515 pci_iounmap(pdev, ioaddr);
2517 pci_release_regions(pdev);
2519 pci_clear_mwi(pdev);
2521 pci_disable_device(pdev);
2525 pr_err("%s: %s\n", pci_name(pdev), err_msg);
2530 typhoon_remove_one(struct pci_dev *pdev)
2532 struct net_device *dev = pci_get_drvdata(pdev);
2533 struct typhoon *tp = netdev_priv(dev);
2535 unregister_netdev(dev);
2536 pci_set_power_state(pdev, PCI_D0);
2537 pci_restore_state(pdev);
2538 typhoon_reset(tp->ioaddr, NoWait);
2539 pci_iounmap(pdev, tp->ioaddr);
2540 dma_free_coherent(&pdev->dev, sizeof(struct typhoon_shared),
2541 tp->shared, tp->shared_dma);
2542 pci_release_regions(pdev);
2543 pci_clear_mwi(pdev);
2544 pci_disable_device(pdev);
2548 static SIMPLE_DEV_PM_OPS(typhoon_pm_ops, typhoon_suspend, typhoon_resume);
2550 static struct pci_driver typhoon_driver = {
2551 .name = KBUILD_MODNAME,
2552 .id_table = typhoon_pci_tbl,
2553 .probe = typhoon_init_one,
2554 .remove = typhoon_remove_one,
2555 .driver.pm = &typhoon_pm_ops,
2561 return pci_register_driver(&typhoon_driver);
2565 typhoon_cleanup(void)
2567 release_firmware(typhoon_fw);
2568 pci_unregister_driver(&typhoon_driver);
2571 module_init(typhoon_init);
2572 module_exit(typhoon_cleanup);