xref: /linux/drivers/net/ethernet/3com/typhoon.c (revision d603517771d8e08a2d8fc9e1f7682ce393d3973a)
1 /* typhoon.c: A Linux Ethernet device driver for 3Com 3CR990 family of NICs */
2 /*
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>.
6 
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.
13 
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).
18 
19 	This work was funded by the National Library of Medicine under
20 	the Department of Energy project number 0274DD06D1 and NLM project
21 	number Y1-LM-2015-01.
22 
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.
25 
26 	KNOWN ISSUES:
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.
32 
33 	TODO:
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
39 		the locking)
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
42 */
43 
44 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
45  * Setting to > 1518 effectively disables this feature.
46  */
47 static int rx_copybreak = 200;
48 
49 /* Should we use MMIO or Port IO?
50  * 0: Port IO
51  * 1: MMIO
52  * 2: Try MMIO, fallback to Port IO
53  */
54 static unsigned int use_mmio = 2;
55 
56 /* end user-configurable values */
57 
58 /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
59  */
60 static const int multicast_filter_limit = 32;
61 
62 /* Operational parameters that are set at compile time. */
63 
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.
69  *
70  * We don't currently use the Hi Tx ring so, don't make it very big.
71  *
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.
74  */
75 #define TXHI_ENTRIES		2
76 #define TXLO_ENTRIES		128
77 #define RX_ENTRIES		32
78 #define COMMAND_ENTRIES		16
79 #define RESPONSE_ENTRIES	32
80 
81 #define COMMAND_RING_SIZE	(COMMAND_ENTRIES * sizeof(struct cmd_desc))
82 #define RESPONSE_RING_SIZE	(RESPONSE_ENTRIES * sizeof(struct resp_desc))
83 
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.
87  */
88 #define RXFREE_ENTRIES		128
89 #define RXENT_ENTRIES		(RXFREE_ENTRIES - 1)
90 
91 /* Operational parameters that usually are not changed. */
92 
93 /* Time in jiffies before concluding the transmitter is hung. */
94 #define TX_TIMEOUT  (2*HZ)
95 
96 #define PKT_BUF_SZ		1536
97 #define FIRMWARE_NAME		"3com/typhoon.bin"
98 
99 #define pr_fmt(fmt)		KBUILD_MODNAME " " fmt
100 
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>
121 #include <asm/io.h>
122 #include <linux/uaccess.h>
123 #include <linux/in6.h>
124 #include <linux/dma-mapping.h>
125 #include <linux/firmware.h>
126 
127 #include "typhoon.h"
128 
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 "
135 			       "is 200.");
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);
140 
141 #if TXLO_ENTRIES <= (2 * MAX_SKB_FRAGS)
142 #error TX ring too small!
143 #endif
144 
145 struct typhoon_card_info {
146 	const char *name;
147 	const int capabilities;
148 };
149 
150 #define TYPHOON_CRYPTO_NONE		0x00
151 #define TYPHOON_CRYPTO_DES		0x01
152 #define TYPHOON_CRYPTO_3DES		0x02
153 #define	TYPHOON_CRYPTO_VARIABLE		0x04
154 #define TYPHOON_FIBER			0x08
155 #define TYPHOON_WAKEUP_NEEDS_RESET	0x10
156 
157 enum typhoon_cards {
158 	TYPHOON_TX = 0, TYPHOON_TX95, TYPHOON_TX97, TYPHOON_SVR,
159 	TYPHOON_SVR95, TYPHOON_SVR97, TYPHOON_TXM, TYPHOON_BSVR,
160 	TYPHOON_FX95, TYPHOON_FX97, TYPHOON_FX95SVR, TYPHOON_FX97SVR,
161 	TYPHOON_FXM,
162 };
163 
164 /* directly indexed by enum typhoon_cards, above */
165 static struct typhoon_card_info typhoon_card_info[] = {
166 	{ "3Com Typhoon (3C990-TX)",
167 		TYPHOON_CRYPTO_NONE},
168 	{ "3Com Typhoon (3CR990-TX-95)",
169 		TYPHOON_CRYPTO_DES},
170 	{ "3Com Typhoon (3CR990-TX-97)",
171 	 	TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES},
172 	{ "3Com Typhoon (3C990SVR)",
173 		TYPHOON_CRYPTO_NONE},
174 	{ "3Com Typhoon (3CR990SVR95)",
175 		TYPHOON_CRYPTO_DES},
176 	{ "3Com Typhoon (3CR990SVR97)",
177 	 	TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES},
178 	{ "3Com Typhoon2 (3C990B-TX-M)",
179 		TYPHOON_CRYPTO_VARIABLE},
180 	{ "3Com Typhoon2 (3C990BSVR)",
181 		TYPHOON_CRYPTO_VARIABLE},
182 	{ "3Com Typhoon (3CR990-FX-95)",
183 		TYPHOON_CRYPTO_DES | TYPHOON_FIBER},
184 	{ "3Com Typhoon (3CR990-FX-97)",
185 	 	TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES | TYPHOON_FIBER},
186 	{ "3Com Typhoon (3CR990-FX-95 Server)",
187 	 	TYPHOON_CRYPTO_DES | TYPHOON_FIBER},
188 	{ "3Com Typhoon (3CR990-FX-97 Server)",
189 	 	TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES | TYPHOON_FIBER},
190 	{ "3Com Typhoon2 (3C990B-FX-97)",
191 		TYPHOON_CRYPTO_VARIABLE | TYPHOON_FIBER},
192 };
193 
194 /* Notes on the new subsystem numbering scheme:
195  * bits 0-1 indicate crypto capabilities: (0) variable, (1) DES, or (2) 3DES
196  * bit 4 indicates if this card has secured firmware (we don't support it)
197  * bit 8 indicates if this is a (0) copper or (1) fiber card
198  * bits 12-16 indicate card type: (0) client and (1) server
199  */
200 static const struct pci_device_id typhoon_pci_tbl[] = {
201 	{
202 		PCI_VDEVICE(3COM, PCI_DEVICE_ID_3COM_3CR990),
203 		.driver_data = TYPHOON_TX,
204 	}, {
205 		PCI_VDEVICE(3COM, PCI_DEVICE_ID_3COM_3CR990_TX_95),
206 		.driver_data = TYPHOON_TX95,
207 	}, {
208 		PCI_VDEVICE(3COM, PCI_DEVICE_ID_3COM_3CR990_TX_97),
209 		.driver_data = TYPHOON_TX97,
210 	}, {
211 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990B,
212 				PCI_ANY_ID, 0x1000),
213 		.driver_data = TYPHOON_TXM,
214 	}, {
215 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990B,
216 				PCI_ANY_ID, 0x1102),
217 		.driver_data = TYPHOON_FXM,
218 	}, {
219 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990B,
220 				PCI_ANY_ID, 0x2000),
221 		.driver_data = TYPHOON_BSVR,
222 	}, {
223 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
224 				PCI_ANY_ID, 0x1101),
225 		.driver_data = TYPHOON_FX95,
226 	}, {
227 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
228 				PCI_ANY_ID, 0x1102),
229 		.driver_data = TYPHOON_FX97,
230 	}, {
231 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
232 				PCI_ANY_ID, 0x2101),
233 		.driver_data = TYPHOON_FX95SVR,
234 	}, {
235 		PCI_VDEVICE_SUB(3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
236 				PCI_ANY_ID, 0x2102),
237 		.driver_data = TYPHOON_FX97SVR,
238 	}, {
239 		PCI_VDEVICE(3COM, PCI_DEVICE_ID_3COM_3CR990SVR95),
240 		.driver_data = TYPHOON_SVR95,
241 	}, {
242 		PCI_VDEVICE(3COM, PCI_DEVICE_ID_3COM_3CR990SVR97),
243 		.driver_data = TYPHOON_SVR97,
244 	}, {
245 		PCI_VDEVICE(3COM, PCI_DEVICE_ID_3COM_3CR990SVR),
246 		.driver_data = TYPHOON_SVR,
247 	},
248 	{ }
249 };
250 MODULE_DEVICE_TABLE(pci, typhoon_pci_tbl);
251 
252 /* Define the shared memory area
253  * Align everything the 3XP will normally be using.
254  * We'll need to move/align txHi if we start using that ring.
255  */
256 #define __3xp_aligned	____cacheline_aligned
257 struct typhoon_shared {
258 	struct typhoon_interface	iface;
259 	struct typhoon_indexes		indexes			__3xp_aligned;
260 	struct tx_desc			txLo[TXLO_ENTRIES] 	__3xp_aligned;
261 	struct rx_desc			rxLo[RX_ENTRIES]	__3xp_aligned;
262 	struct rx_desc			rxHi[RX_ENTRIES]	__3xp_aligned;
263 	struct cmd_desc			cmd[COMMAND_ENTRIES]	__3xp_aligned;
264 	struct resp_desc		resp[RESPONSE_ENTRIES]	__3xp_aligned;
265 	struct rx_free			rxBuff[RXFREE_ENTRIES]	__3xp_aligned;
266 	u32				zeroWord;
267 	struct tx_desc			txHi[TXHI_ENTRIES];
268 } __packed;
269 
270 struct rxbuff_ent {
271 	struct sk_buff *skb;
272 	dma_addr_t	dma_addr;
273 };
274 
275 struct typhoon {
276 	/* Tx cache line section */
277 	struct transmit_ring 	txLoRing	____cacheline_aligned;
278 	struct pci_dev *	tx_pdev;
279 	void __iomem		*tx_ioaddr;
280 	u32			txlo_dma_addr;
281 
282 	/* Irq/Rx cache line section */
283 	void __iomem		*ioaddr		____cacheline_aligned;
284 	struct typhoon_indexes *indexes;
285 	u8			awaiting_resp;
286 	u8			duplex;
287 	u8			speed;
288 	u8			card_state;
289 	struct basic_ring	rxLoRing;
290 	struct pci_dev *	pdev;
291 	struct net_device *	dev;
292 	struct napi_struct	napi;
293 	struct basic_ring	rxHiRing;
294 	struct basic_ring	rxBuffRing;
295 	struct rxbuff_ent	rxbuffers[RXENT_ENTRIES];
296 
297 	/* general section */
298 	spinlock_t		command_lock	____cacheline_aligned;
299 	struct basic_ring	cmdRing;
300 	struct basic_ring	respRing;
301 	struct net_device_stats	stats_saved;
302 	struct typhoon_shared *	shared;
303 	dma_addr_t		shared_dma;
304 	__le16			xcvr_select;
305 	__le16			wol_events;
306 	__le32			offload;
307 
308 	/* unused stuff (future use) */
309 	int			capabilities;
310 	struct transmit_ring 	txHiRing;
311 };
312 
313 enum completion_wait_values {
314 	NoWait = 0, WaitNoSleep, WaitSleep,
315 };
316 
317 /* These are the values for the typhoon.card_state variable.
318  * These determine where the statistics will come from in get_stats().
319  * The sleep image does not support the statistics we need.
320  */
321 enum state_values {
322 	Sleeping = 0, Running,
323 };
324 
325 /* PCI writes are not guaranteed to be posted in order, but outstanding writes
326  * cannot pass a read, so this forces current writes to post.
327  */
328 #define typhoon_post_pci_writes(x) \
329 	do { if (likely(use_mmio)) ioread32(x+TYPHOON_REG_HEARTBEAT); } while (0)
330 
331 /* We'll wait up to six seconds for a reset, and half a second normally.
332  */
333 #define TYPHOON_UDELAY			50
334 #define TYPHOON_RESET_TIMEOUT_SLEEP	(6 * HZ)
335 #define TYPHOON_RESET_TIMEOUT_NOSLEEP	((6 * 1000000) / TYPHOON_UDELAY)
336 #define TYPHOON_WAIT_TIMEOUT		((1000000 / 2) / TYPHOON_UDELAY)
337 
338 #if defined(NETIF_F_TSO)
339 #define skb_tso_size(x)		(skb_shinfo(x)->gso_size)
340 #define TSO_NUM_DESCRIPTORS	2
341 #define TSO_OFFLOAD_ON		TYPHOON_OFFLOAD_TCP_SEGMENT
342 #else
343 #define NETIF_F_TSO 		0
344 #define skb_tso_size(x) 	0
345 #define TSO_NUM_DESCRIPTORS	0
346 #define TSO_OFFLOAD_ON		0
347 #endif
348 
349 static inline void
350 typhoon_inc_index(u32 *index, const int count, const int num_entries)
351 {
352 	/* Increment a ring index -- we can use this for all rings execept
353 	 * the Rx rings, as they use different size descriptors
354 	 * otherwise, everything is the same size as a cmd_desc
355 	 */
356 	*index += count * sizeof(struct cmd_desc);
357 	*index %= num_entries * sizeof(struct cmd_desc);
358 }
359 
360 static inline void
361 typhoon_inc_cmd_index(u32 *index, const int count)
362 {
363 	typhoon_inc_index(index, count, COMMAND_ENTRIES);
364 }
365 
366 static inline void
367 typhoon_inc_resp_index(u32 *index, const int count)
368 {
369 	typhoon_inc_index(index, count, RESPONSE_ENTRIES);
370 }
371 
372 static inline void
373 typhoon_inc_rxfree_index(u32 *index, const int count)
374 {
375 	typhoon_inc_index(index, count, RXFREE_ENTRIES);
376 }
377 
378 static inline void
379 typhoon_inc_tx_index(u32 *index, const int count)
380 {
381 	/* if we start using the Hi Tx ring, this needs updating */
382 	typhoon_inc_index(index, count, TXLO_ENTRIES);
383 }
384 
385 static inline void
386 typhoon_inc_rx_index(u32 *index, const int count)
387 {
388 	/* sizeof(struct rx_desc) != sizeof(struct cmd_desc) */
389 	*index += count * sizeof(struct rx_desc);
390 	*index %= RX_ENTRIES * sizeof(struct rx_desc);
391 }
392 
393 static int
394 typhoon_reset(void __iomem *ioaddr, int wait_type)
395 {
396 	int i, err = 0;
397 	int timeout;
398 
399 	if (wait_type == WaitNoSleep)
400 		timeout = TYPHOON_RESET_TIMEOUT_NOSLEEP;
401 	else
402 		timeout = TYPHOON_RESET_TIMEOUT_SLEEP;
403 
404 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
405 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
406 
407 	iowrite32(TYPHOON_RESET_ALL, ioaddr + TYPHOON_REG_SOFT_RESET);
408 	typhoon_post_pci_writes(ioaddr);
409 	udelay(1);
410 	iowrite32(TYPHOON_RESET_NONE, ioaddr + TYPHOON_REG_SOFT_RESET);
411 
412 	if (wait_type != NoWait) {
413 		for (i = 0; i < timeout; i++) {
414 			if (ioread32(ioaddr + TYPHOON_REG_STATUS) ==
415 			   TYPHOON_STATUS_WAITING_FOR_HOST)
416 				goto out;
417 
418 			if (wait_type == WaitSleep)
419 				schedule_timeout_uninterruptible(1);
420 			else
421 				udelay(TYPHOON_UDELAY);
422 		}
423 
424 		err = -ETIMEDOUT;
425 	}
426 
427 out:
428 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
429 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
430 
431 	/* The 3XP seems to need a little extra time to complete the load
432 	 * of the sleep image before we can reliably boot it. Failure to
433 	 * do this occasionally results in a hung adapter after boot in
434 	 * typhoon_init_one() while trying to read the MAC address or
435 	 * putting the card to sleep. 3Com's driver waits 5ms, but
436 	 * that seems to be overkill. However, if we can sleep, we might
437 	 * as well give it that much time. Otherwise, we'll give it 500us,
438 	 * which should be enough (I've see it work well at 100us, but still
439 	 * saw occasional problems.)
440 	 */
441 	if (wait_type == WaitSleep)
442 		msleep(5);
443 	else
444 		udelay(500);
445 	return err;
446 }
447 
448 static int
449 typhoon_wait_status(void __iomem *ioaddr, u32 wait_value)
450 {
451 	int i, err = 0;
452 
453 	for (i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
454 		if (ioread32(ioaddr + TYPHOON_REG_STATUS) == wait_value)
455 			goto out;
456 		udelay(TYPHOON_UDELAY);
457 	}
458 
459 	err = -ETIMEDOUT;
460 
461 out:
462 	return err;
463 }
464 
465 static inline void
466 typhoon_media_status(struct net_device *dev, struct resp_desc *resp)
467 {
468 	if (resp->parm1 & TYPHOON_MEDIA_STAT_NO_LINK)
469 		netif_carrier_off(dev);
470 	else
471 		netif_carrier_on(dev);
472 }
473 
474 static inline void
475 typhoon_hello(struct typhoon *tp)
476 {
477 	struct basic_ring *ring = &tp->cmdRing;
478 	struct cmd_desc *cmd;
479 
480 	/* We only get a hello request if we've not sent anything to the
481 	 * card in a long while. If the lock is held, then we're in the
482 	 * process of issuing a command, so we don't need to respond.
483 	 */
484 	if (spin_trylock(&tp->command_lock)) {
485 		cmd = (struct cmd_desc *)(ring->ringBase + ring->lastWrite);
486 		typhoon_inc_cmd_index(&ring->lastWrite, 1);
487 
488 		INIT_COMMAND_NO_RESPONSE(cmd, TYPHOON_CMD_HELLO_RESP);
489 		wmb();
490 		iowrite32(ring->lastWrite, tp->ioaddr + TYPHOON_REG_CMD_READY);
491 		spin_unlock(&tp->command_lock);
492 	}
493 }
494 
495 static int
496 typhoon_process_response(struct typhoon *tp, int resp_size,
497 				struct resp_desc *resp_save)
498 {
499 	struct typhoon_indexes *indexes = tp->indexes;
500 	struct resp_desc *resp;
501 	u8 *base = tp->respRing.ringBase;
502 	int count, len, wrap_len;
503 	u32 cleared;
504 	u32 ready;
505 
506 	cleared = le32_to_cpu(indexes->respCleared);
507 	ready = le32_to_cpu(indexes->respReady);
508 	while (cleared != ready) {
509 		resp = (struct resp_desc *)(base + cleared);
510 		count = resp->numDesc + 1;
511 		if (resp_save && resp->seqNo) {
512 			if (count > resp_size) {
513 				resp_save->flags = TYPHOON_RESP_ERROR;
514 				goto cleanup;
515 			}
516 
517 			wrap_len = 0;
518 			len = count * sizeof(*resp);
519 			if (unlikely(cleared + len > RESPONSE_RING_SIZE)) {
520 				wrap_len = cleared + len - RESPONSE_RING_SIZE;
521 				len = RESPONSE_RING_SIZE - cleared;
522 			}
523 
524 			memcpy(resp_save, resp, len);
525 			if (unlikely(wrap_len)) {
526 				resp_save += len / sizeof(*resp);
527 				memcpy(resp_save, base, wrap_len);
528 			}
529 
530 			resp_save = NULL;
531 		} else if (resp->cmd == TYPHOON_CMD_READ_MEDIA_STATUS) {
532 			typhoon_media_status(tp->dev, resp);
533 		} else if (resp->cmd == TYPHOON_CMD_HELLO_RESP) {
534 			typhoon_hello(tp);
535 		} else {
536 			netdev_err(tp->dev,
537 				   "dumping unexpected response 0x%04x:%d:0x%02x:0x%04x:%08x:%08x\n",
538 				   le16_to_cpu(resp->cmd),
539 				   resp->numDesc, resp->flags,
540 				   le16_to_cpu(resp->parm1),
541 				   le32_to_cpu(resp->parm2),
542 				   le32_to_cpu(resp->parm3));
543 		}
544 
545 cleanup:
546 		typhoon_inc_resp_index(&cleared, count);
547 	}
548 
549 	indexes->respCleared = cpu_to_le32(cleared);
550 	wmb();
551 	return resp_save == NULL;
552 }
553 
554 static inline int
555 typhoon_num_free(int lastWrite, int lastRead, int ringSize)
556 {
557 	/* this works for all descriptors but rx_desc, as they are a
558 	 * different size than the cmd_desc -- everyone else is the same
559 	 */
560 	lastWrite /= sizeof(struct cmd_desc);
561 	lastRead /= sizeof(struct cmd_desc);
562 	return (ringSize + lastRead - lastWrite - 1) % ringSize;
563 }
564 
565 static inline int
566 typhoon_num_free_cmd(struct typhoon *tp)
567 {
568 	int lastWrite = tp->cmdRing.lastWrite;
569 	int cmdCleared = le32_to_cpu(tp->indexes->cmdCleared);
570 
571 	return typhoon_num_free(lastWrite, cmdCleared, COMMAND_ENTRIES);
572 }
573 
574 static inline int
575 typhoon_num_free_resp(struct typhoon *tp)
576 {
577 	int respReady = le32_to_cpu(tp->indexes->respReady);
578 	int respCleared = le32_to_cpu(tp->indexes->respCleared);
579 
580 	return typhoon_num_free(respReady, respCleared, RESPONSE_ENTRIES);
581 }
582 
583 static inline int
584 typhoon_num_free_tx(struct transmit_ring *ring)
585 {
586 	/* if we start using the Hi Tx ring, this needs updating */
587 	return typhoon_num_free(ring->lastWrite, ring->lastRead, TXLO_ENTRIES);
588 }
589 
590 static int
591 typhoon_issue_command(struct typhoon *tp, int num_cmd, struct cmd_desc *cmd,
592 		      int num_resp, struct resp_desc *resp)
593 {
594 	struct typhoon_indexes *indexes = tp->indexes;
595 	struct basic_ring *ring = &tp->cmdRing;
596 	struct resp_desc local_resp;
597 	int i, err = 0;
598 	int got_resp;
599 	int freeCmd, freeResp;
600 	int len, wrap_len;
601 
602 	spin_lock(&tp->command_lock);
603 
604 	freeCmd = typhoon_num_free_cmd(tp);
605 	freeResp = typhoon_num_free_resp(tp);
606 
607 	if (freeCmd < num_cmd || freeResp < num_resp) {
608 		netdev_err(tp->dev, "no descs for cmd, had (needed) %d (%d) cmd, %d (%d) resp\n",
609 			   freeCmd, num_cmd, freeResp, num_resp);
610 		err = -ENOMEM;
611 		goto out;
612 	}
613 
614 	if (cmd->flags & TYPHOON_CMD_RESPOND) {
615 		/* If we're expecting a response, but the caller hasn't given
616 		 * us a place to put it, we'll provide one.
617 		 */
618 		tp->awaiting_resp = 1;
619 		if (resp == NULL) {
620 			resp = &local_resp;
621 			num_resp = 1;
622 		}
623 	}
624 
625 	wrap_len = 0;
626 	len = num_cmd * sizeof(*cmd);
627 	if (unlikely(ring->lastWrite + len > COMMAND_RING_SIZE)) {
628 		wrap_len = ring->lastWrite + len - COMMAND_RING_SIZE;
629 		len = COMMAND_RING_SIZE - ring->lastWrite;
630 	}
631 
632 	memcpy(ring->ringBase + ring->lastWrite, cmd, len);
633 	if (unlikely(wrap_len)) {
634 		struct cmd_desc *wrap_ptr = cmd;
635 		wrap_ptr += len / sizeof(*cmd);
636 		memcpy(ring->ringBase, wrap_ptr, wrap_len);
637 	}
638 
639 	typhoon_inc_cmd_index(&ring->lastWrite, num_cmd);
640 
641 	/* "I feel a presence... another warrior is on the mesa."
642 	 */
643 	wmb();
644 	iowrite32(ring->lastWrite, tp->ioaddr + TYPHOON_REG_CMD_READY);
645 	typhoon_post_pci_writes(tp->ioaddr);
646 
647 	if ((cmd->flags & TYPHOON_CMD_RESPOND) == 0)
648 		goto out;
649 
650 	/* Ugh. We'll be here about 8ms, spinning our thumbs, unable to
651 	 * preempt or do anything other than take interrupts. So, don't
652 	 * wait for a response unless you have to.
653 	 *
654 	 * I've thought about trying to sleep here, but we're called
655 	 * from many contexts that don't allow that. Also, given the way
656 	 * 3Com has implemented irq coalescing, we would likely timeout --
657 	 * this has been observed in real life!
658 	 *
659 	 * The big killer is we have to wait to get stats from the card,
660 	 * though we could go to a periodic refresh of those if we don't
661 	 * mind them getting somewhat stale. The rest of the waiting
662 	 * commands occur during open/close/suspend/resume, so they aren't
663 	 * time critical. Creating SAs in the future will also have to
664 	 * wait here.
665 	 */
666 	got_resp = 0;
667 	for (i = 0; i < TYPHOON_WAIT_TIMEOUT && !got_resp; i++) {
668 		if (indexes->respCleared != indexes->respReady)
669 			got_resp = typhoon_process_response(tp, num_resp,
670 								resp);
671 		udelay(TYPHOON_UDELAY);
672 	}
673 
674 	if (!got_resp) {
675 		err = -ETIMEDOUT;
676 		goto out;
677 	}
678 
679 	/* Collect the error response even if we don't care about the
680 	 * rest of the response
681 	 */
682 	if (resp->flags & TYPHOON_RESP_ERROR)
683 		err = -EIO;
684 
685 out:
686 	if (tp->awaiting_resp) {
687 		tp->awaiting_resp = 0;
688 		smp_wmb();
689 
690 		/* Ugh. If a response was added to the ring between
691 		 * the call to typhoon_process_response() and the clearing
692 		 * of tp->awaiting_resp, we could have missed the interrupt
693 		 * and it could hang in the ring an indeterminate amount of
694 		 * time. So, check for it, and interrupt ourselves if this
695 		 * is the case.
696 		 */
697 		if (indexes->respCleared != indexes->respReady)
698 			iowrite32(1, tp->ioaddr + TYPHOON_REG_SELF_INTERRUPT);
699 	}
700 
701 	spin_unlock(&tp->command_lock);
702 	return err;
703 }
704 
705 static inline void
706 typhoon_tso_fill(struct sk_buff *skb, struct transmit_ring *txRing,
707 			u32 ring_dma)
708 {
709 	struct tcpopt_desc *tcpd;
710 	u32 tcpd_offset = ring_dma;
711 
712 	tcpd = (struct tcpopt_desc *) (txRing->ringBase + txRing->lastWrite);
713 	tcpd_offset += txRing->lastWrite;
714 	tcpd_offset += offsetof(struct tcpopt_desc, bytesTx);
715 	typhoon_inc_tx_index(&txRing->lastWrite, 1);
716 
717 	tcpd->flags = TYPHOON_OPT_DESC | TYPHOON_OPT_TCP_SEG;
718 	tcpd->numDesc = 1;
719 	tcpd->mss_flags = cpu_to_le16(skb_tso_size(skb));
720 	tcpd->mss_flags |= TYPHOON_TSO_FIRST | TYPHOON_TSO_LAST;
721 	tcpd->respAddrLo = cpu_to_le32(tcpd_offset);
722 	tcpd->bytesTx = cpu_to_le32(skb->len);
723 	tcpd->status = 0;
724 }
725 
726 static netdev_tx_t
727 typhoon_start_tx(struct sk_buff *skb, struct net_device *dev)
728 {
729 	struct typhoon *tp = netdev_priv(dev);
730 	struct transmit_ring *txRing;
731 	struct tx_desc *txd, *first_txd;
732 	dma_addr_t skb_dma;
733 	int numDesc;
734 
735 	/* we have two rings to choose from, but we only use txLo for now
736 	 * If we start using the Hi ring as well, we'll need to update
737 	 * typhoon_stop_runtime(), typhoon_interrupt(), typhoon_num_free_tx(),
738 	 * and TXHI_ENTRIES to match, as well as update the TSO code below
739 	 * to get the right DMA address
740 	 */
741 	txRing = &tp->txLoRing;
742 
743 	/* We need one descriptor for each fragment of the sk_buff, plus the
744 	 * one for the ->data area of it.
745 	 *
746 	 * The docs say a maximum of 16 fragment descriptors per TCP option
747 	 * descriptor, then make a new packet descriptor and option descriptor
748 	 * for the next 16 fragments. The engineers say just an option
749 	 * descriptor is needed. I've tested up to 26 fragments with a single
750 	 * packet descriptor/option descriptor combo, so I use that for now.
751 	 *
752 	 * If problems develop with TSO, check this first.
753 	 */
754 	numDesc = skb_shinfo(skb)->nr_frags + 1;
755 	if (skb_is_gso(skb))
756 		numDesc++;
757 
758 	/* When checking for free space in the ring, we need to also
759 	 * account for the initial Tx descriptor, and we always must leave
760 	 * at least one descriptor unused in the ring so that it doesn't
761 	 * wrap and look empty.
762 	 *
763 	 * The only time we should loop here is when we hit the race
764 	 * between marking the queue awake and updating the cleared index.
765 	 * Just loop and it will appear. This comes from the acenic driver.
766 	 */
767 	while (unlikely(typhoon_num_free_tx(txRing) < (numDesc + 2)))
768 		smp_rmb();
769 
770 	first_txd = (struct tx_desc *) (txRing->ringBase + txRing->lastWrite);
771 	typhoon_inc_tx_index(&txRing->lastWrite, 1);
772 
773 	first_txd->flags = TYPHOON_TX_DESC | TYPHOON_DESC_VALID;
774 	first_txd->numDesc = 0;
775 	first_txd->len = 0;
776 	first_txd->tx_addr = (u64)((unsigned long) skb);
777 	first_txd->processFlags = 0;
778 
779 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
780 		/* The 3XP will figure out if this is UDP/TCP */
781 		first_txd->processFlags |= TYPHOON_TX_PF_TCP_CHKSUM;
782 		first_txd->processFlags |= TYPHOON_TX_PF_UDP_CHKSUM;
783 		first_txd->processFlags |= TYPHOON_TX_PF_IP_CHKSUM;
784 	}
785 
786 	if (skb_vlan_tag_present(skb)) {
787 		first_txd->processFlags |=
788 		    TYPHOON_TX_PF_INSERT_VLAN | TYPHOON_TX_PF_VLAN_PRIORITY;
789 		first_txd->processFlags |=
790 		    cpu_to_le32(htons(skb_vlan_tag_get(skb)) <<
791 				TYPHOON_TX_PF_VLAN_TAG_SHIFT);
792 	}
793 
794 	if (skb_is_gso(skb)) {
795 		first_txd->processFlags |= TYPHOON_TX_PF_TCP_SEGMENT;
796 		first_txd->numDesc++;
797 
798 		typhoon_tso_fill(skb, txRing, tp->txlo_dma_addr);
799 	}
800 
801 	txd = (struct tx_desc *) (txRing->ringBase + txRing->lastWrite);
802 	typhoon_inc_tx_index(&txRing->lastWrite, 1);
803 
804 	/* No need to worry about padding packet -- the firmware pads
805 	 * it with zeros to ETH_ZLEN for us.
806 	 */
807 	if (skb_shinfo(skb)->nr_frags == 0) {
808 		skb_dma = dma_map_single(&tp->tx_pdev->dev, skb->data,
809 					 skb->len, DMA_TO_DEVICE);
810 		txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
811 		txd->len = cpu_to_le16(skb->len);
812 		txd->frag.addr = cpu_to_le32(skb_dma);
813 		txd->frag.addrHi = 0;
814 		first_txd->numDesc++;
815 	} else {
816 		int i, len;
817 
818 		len = skb_headlen(skb);
819 		skb_dma = dma_map_single(&tp->tx_pdev->dev, skb->data, len,
820 					 DMA_TO_DEVICE);
821 		txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
822 		txd->len = cpu_to_le16(len);
823 		txd->frag.addr = cpu_to_le32(skb_dma);
824 		txd->frag.addrHi = 0;
825 		first_txd->numDesc++;
826 
827 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
828 			const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
829 			void *frag_addr;
830 
831 			txd = (struct tx_desc *) (txRing->ringBase +
832 						txRing->lastWrite);
833 			typhoon_inc_tx_index(&txRing->lastWrite, 1);
834 
835 			len = skb_frag_size(frag);
836 			frag_addr = skb_frag_address(frag);
837 			skb_dma = dma_map_single(&tp->tx_pdev->dev, frag_addr,
838 						 len, DMA_TO_DEVICE);
839 			txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
840 			txd->len = cpu_to_le16(len);
841 			txd->frag.addr = cpu_to_le32(skb_dma);
842 			txd->frag.addrHi = 0;
843 			first_txd->numDesc++;
844 		}
845 	}
846 
847 	/* Kick the 3XP
848 	 */
849 	wmb();
850 	iowrite32(txRing->lastWrite, tp->tx_ioaddr + txRing->writeRegister);
851 
852 	/* If we don't have room to put the worst case packet on the
853 	 * queue, then we must stop the queue. We need 2 extra
854 	 * descriptors -- one to prevent ring wrap, and one for the
855 	 * Tx header.
856 	 */
857 	numDesc = MAX_SKB_FRAGS + TSO_NUM_DESCRIPTORS + 1;
858 
859 	if (typhoon_num_free_tx(txRing) < (numDesc + 2)) {
860 		netif_stop_queue(dev);
861 
862 		/* A Tx complete IRQ could have gotten between, making
863 		 * the ring free again. Only need to recheck here, since
864 		 * Tx is serialized.
865 		 */
866 		if (typhoon_num_free_tx(txRing) >= (numDesc + 2))
867 			netif_wake_queue(dev);
868 	}
869 
870 	return NETDEV_TX_OK;
871 }
872 
873 static void
874 typhoon_set_rx_mode(struct net_device *dev)
875 {
876 	struct typhoon *tp = netdev_priv(dev);
877 	struct cmd_desc xp_cmd;
878 	u32 mc_filter[2];
879 	__le16 filter;
880 
881 	filter = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
882 	if (dev->flags & IFF_PROMISC) {
883 		filter |= TYPHOON_RX_FILTER_PROMISCOUS;
884 	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
885 		  (dev->flags & IFF_ALLMULTI)) {
886 		/* Too many to match, or accept all multicasts. */
887 		filter |= TYPHOON_RX_FILTER_ALL_MCAST;
888 	} else if (!netdev_mc_empty(dev)) {
889 		struct netdev_hw_addr *ha;
890 
891 		memset(mc_filter, 0, sizeof(mc_filter));
892 		netdev_for_each_mc_addr(ha, dev) {
893 			int bit = ether_crc(ETH_ALEN, ha->addr) & 0x3f;
894 			mc_filter[bit >> 5] |= 1 << (bit & 0x1f);
895 		}
896 
897 		INIT_COMMAND_NO_RESPONSE(&xp_cmd,
898 					 TYPHOON_CMD_SET_MULTICAST_HASH);
899 		xp_cmd.parm1 = TYPHOON_MCAST_HASH_SET;
900 		xp_cmd.parm2 = cpu_to_le32(mc_filter[0]);
901 		xp_cmd.parm3 = cpu_to_le32(mc_filter[1]);
902 		typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
903 
904 		filter |= TYPHOON_RX_FILTER_MCAST_HASH;
905 	}
906 
907 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
908 	xp_cmd.parm1 = filter;
909 	typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
910 }
911 
912 static int
913 typhoon_do_get_stats(struct typhoon *tp)
914 {
915 	struct net_device_stats *stats = &tp->dev->stats;
916 	struct net_device_stats *saved = &tp->stats_saved;
917 	struct cmd_desc xp_cmd;
918 	struct resp_desc xp_resp[7];
919 	struct stats_resp *s = (struct stats_resp *) xp_resp;
920 	int err;
921 
922 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_STATS);
923 	err = typhoon_issue_command(tp, 1, &xp_cmd, 7, xp_resp);
924 	if (err < 0)
925 		return err;
926 
927 	/* 3Com's Linux driver uses txMultipleCollisions as it's
928 	 * collisions value, but there is some other collision info as well...
929 	 *
930 	 * The extra status reported would be a good candidate for
931 	 * ethtool_ops->get_{strings,stats}()
932 	 */
933 	stats->tx_packets = le32_to_cpu(s->txPackets) +
934 			saved->tx_packets;
935 	stats->tx_bytes = le64_to_cpu(s->txBytes) +
936 			saved->tx_bytes;
937 	stats->tx_errors = le32_to_cpu(s->txCarrierLost) +
938 			saved->tx_errors;
939 	stats->tx_carrier_errors = le32_to_cpu(s->txCarrierLost) +
940 			saved->tx_carrier_errors;
941 	stats->collisions = le32_to_cpu(s->txMultipleCollisions) +
942 			saved->collisions;
943 	stats->rx_packets = le32_to_cpu(s->rxPacketsGood) +
944 			saved->rx_packets;
945 	stats->rx_bytes = le64_to_cpu(s->rxBytesGood) +
946 			saved->rx_bytes;
947 	stats->rx_fifo_errors = le32_to_cpu(s->rxFifoOverruns) +
948 			saved->rx_fifo_errors;
949 	stats->rx_errors = le32_to_cpu(s->rxFifoOverruns) +
950 			le32_to_cpu(s->BadSSD) + le32_to_cpu(s->rxCrcErrors) +
951 			saved->rx_errors;
952 	stats->rx_crc_errors = le32_to_cpu(s->rxCrcErrors) +
953 			saved->rx_crc_errors;
954 	stats->rx_length_errors = le32_to_cpu(s->rxOversized) +
955 			saved->rx_length_errors;
956 	tp->speed = (s->linkStatus & TYPHOON_LINK_100MBPS) ?
957 			SPEED_100 : SPEED_10;
958 	tp->duplex = (s->linkStatus & TYPHOON_LINK_FULL_DUPLEX) ?
959 			DUPLEX_FULL : DUPLEX_HALF;
960 
961 	return 0;
962 }
963 
964 static struct net_device_stats *
965 typhoon_get_stats(struct net_device *dev)
966 {
967 	struct typhoon *tp = netdev_priv(dev);
968 	struct net_device_stats *stats = &tp->dev->stats;
969 	struct net_device_stats *saved = &tp->stats_saved;
970 
971 	smp_rmb();
972 	if (tp->card_state == Sleeping)
973 		return saved;
974 
975 	if (typhoon_do_get_stats(tp) < 0) {
976 		netdev_err(dev, "error getting stats\n");
977 		return saved;
978 	}
979 
980 	return stats;
981 }
982 
983 static void
984 typhoon_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
985 {
986 	struct typhoon *tp = netdev_priv(dev);
987 	struct pci_dev *pci_dev = tp->pdev;
988 	struct cmd_desc xp_cmd;
989 	struct resp_desc xp_resp[3];
990 
991 	smp_rmb();
992 	if (tp->card_state == Sleeping) {
993 		strscpy(info->fw_version, "Sleep image",
994 			sizeof(info->fw_version));
995 	} else {
996 		INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
997 		if (typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp) < 0) {
998 			strscpy(info->fw_version, "Unknown runtime",
999 				sizeof(info->fw_version));
1000 		} else {
1001 			u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
1002 			snprintf(info->fw_version, sizeof(info->fw_version),
1003 				"%02x.%03x.%03x", sleep_ver >> 24,
1004 				(sleep_ver >> 12) & 0xfff, sleep_ver & 0xfff);
1005 		}
1006 	}
1007 
1008 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1009 	strscpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
1010 }
1011 
1012 static int
1013 typhoon_get_link_ksettings(struct net_device *dev,
1014 			   struct ethtool_link_ksettings *cmd)
1015 {
1016 	struct typhoon *tp = netdev_priv(dev);
1017 	u32 supported, advertising = 0;
1018 
1019 	supported = SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1020 				SUPPORTED_Autoneg;
1021 
1022 	switch (tp->xcvr_select) {
1023 	case TYPHOON_XCVR_10HALF:
1024 		advertising = ADVERTISED_10baseT_Half;
1025 		break;
1026 	case TYPHOON_XCVR_10FULL:
1027 		advertising = ADVERTISED_10baseT_Full;
1028 		break;
1029 	case TYPHOON_XCVR_100HALF:
1030 		advertising = ADVERTISED_100baseT_Half;
1031 		break;
1032 	case TYPHOON_XCVR_100FULL:
1033 		advertising = ADVERTISED_100baseT_Full;
1034 		break;
1035 	case TYPHOON_XCVR_AUTONEG:
1036 		advertising = ADVERTISED_10baseT_Half |
1037 					    ADVERTISED_10baseT_Full |
1038 					    ADVERTISED_100baseT_Half |
1039 					    ADVERTISED_100baseT_Full |
1040 					    ADVERTISED_Autoneg;
1041 		break;
1042 	}
1043 
1044 	if (tp->capabilities & TYPHOON_FIBER) {
1045 		supported |= SUPPORTED_FIBRE;
1046 		advertising |= ADVERTISED_FIBRE;
1047 		cmd->base.port = PORT_FIBRE;
1048 	} else {
1049 		supported |= SUPPORTED_10baseT_Half |
1050 		    			SUPPORTED_10baseT_Full |
1051 					SUPPORTED_TP;
1052 		advertising |= ADVERTISED_TP;
1053 		cmd->base.port = PORT_TP;
1054 	}
1055 
1056 	/* need to get stats to make these link speed/duplex valid */
1057 	typhoon_do_get_stats(tp);
1058 	cmd->base.speed = tp->speed;
1059 	cmd->base.duplex = tp->duplex;
1060 	cmd->base.phy_address = 0;
1061 	if (tp->xcvr_select == TYPHOON_XCVR_AUTONEG)
1062 		cmd->base.autoneg = AUTONEG_ENABLE;
1063 	else
1064 		cmd->base.autoneg = AUTONEG_DISABLE;
1065 
1066 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1067 						supported);
1068 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1069 						advertising);
1070 
1071 	return 0;
1072 }
1073 
1074 static int
1075 typhoon_set_link_ksettings(struct net_device *dev,
1076 			   const struct ethtool_link_ksettings *cmd)
1077 {
1078 	struct typhoon *tp = netdev_priv(dev);
1079 	u32 speed = cmd->base.speed;
1080 	struct cmd_desc xp_cmd;
1081 	__le16 xcvr;
1082 	int err;
1083 
1084 	err = -EINVAL;
1085 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1086 		xcvr = TYPHOON_XCVR_AUTONEG;
1087 	} else {
1088 		if (cmd->base.duplex == DUPLEX_HALF) {
1089 			if (speed == SPEED_10)
1090 				xcvr = TYPHOON_XCVR_10HALF;
1091 			else if (speed == SPEED_100)
1092 				xcvr = TYPHOON_XCVR_100HALF;
1093 			else
1094 				goto out;
1095 		} else if (cmd->base.duplex == DUPLEX_FULL) {
1096 			if (speed == SPEED_10)
1097 				xcvr = TYPHOON_XCVR_10FULL;
1098 			else if (speed == SPEED_100)
1099 				xcvr = TYPHOON_XCVR_100FULL;
1100 			else
1101 				goto out;
1102 		} else
1103 			goto out;
1104 	}
1105 
1106 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
1107 	xp_cmd.parm1 = xcvr;
1108 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1109 	if (err < 0)
1110 		goto out;
1111 
1112 	tp->xcvr_select = xcvr;
1113 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1114 		tp->speed = 0xff;	/* invalid */
1115 		tp->duplex = 0xff;	/* invalid */
1116 	} else {
1117 		tp->speed = speed;
1118 		tp->duplex = cmd->base.duplex;
1119 	}
1120 
1121 out:
1122 	return err;
1123 }
1124 
1125 static void
1126 typhoon_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1127 {
1128 	struct typhoon *tp = netdev_priv(dev);
1129 
1130 	wol->supported = WAKE_PHY | WAKE_MAGIC;
1131 	wol->wolopts = 0;
1132 	if (tp->wol_events & TYPHOON_WAKE_LINK_EVENT)
1133 		wol->wolopts |= WAKE_PHY;
1134 	if (tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
1135 		wol->wolopts |= WAKE_MAGIC;
1136 	memset(&wol->sopass, 0, sizeof(wol->sopass));
1137 }
1138 
1139 static int
1140 typhoon_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1141 {
1142 	struct typhoon *tp = netdev_priv(dev);
1143 
1144 	if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
1145 		return -EINVAL;
1146 
1147 	tp->wol_events = 0;
1148 	if (wol->wolopts & WAKE_PHY)
1149 		tp->wol_events |= TYPHOON_WAKE_LINK_EVENT;
1150 	if (wol->wolopts & WAKE_MAGIC)
1151 		tp->wol_events |= TYPHOON_WAKE_MAGIC_PKT;
1152 
1153 	return 0;
1154 }
1155 
1156 static void
1157 typhoon_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering,
1158 		      struct kernel_ethtool_ringparam *kernel_ering,
1159 		      struct netlink_ext_ack *extack)
1160 {
1161 	ering->rx_max_pending = RXENT_ENTRIES;
1162 	ering->tx_max_pending = TXLO_ENTRIES - 1;
1163 
1164 	ering->rx_pending = RXENT_ENTRIES;
1165 	ering->tx_pending = TXLO_ENTRIES - 1;
1166 }
1167 
1168 static const struct ethtool_ops typhoon_ethtool_ops = {
1169 	.get_drvinfo		= typhoon_get_drvinfo,
1170 	.get_wol		= typhoon_get_wol,
1171 	.set_wol		= typhoon_set_wol,
1172 	.get_link		= ethtool_op_get_link,
1173 	.get_ringparam		= typhoon_get_ringparam,
1174 	.get_link_ksettings	= typhoon_get_link_ksettings,
1175 	.set_link_ksettings	= typhoon_set_link_ksettings,
1176 };
1177 
1178 static int
1179 typhoon_wait_interrupt(void __iomem *ioaddr)
1180 {
1181 	int i, err = 0;
1182 
1183 	for (i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
1184 		if (ioread32(ioaddr + TYPHOON_REG_INTR_STATUS) &
1185 		   TYPHOON_INTR_BOOTCMD)
1186 			goto out;
1187 		udelay(TYPHOON_UDELAY);
1188 	}
1189 
1190 	err = -ETIMEDOUT;
1191 
1192 out:
1193 	iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
1194 	return err;
1195 }
1196 
1197 #define shared_offset(x)	offsetof(struct typhoon_shared, x)
1198 
1199 static void
1200 typhoon_init_interface(struct typhoon *tp)
1201 {
1202 	struct typhoon_interface *iface = &tp->shared->iface;
1203 	dma_addr_t shared_dma;
1204 
1205 	memset(tp->shared, 0, sizeof(struct typhoon_shared));
1206 
1207 	/* The *Hi members of iface are all init'd to zero by the memset().
1208 	 */
1209 	shared_dma = tp->shared_dma + shared_offset(indexes);
1210 	iface->ringIndex = cpu_to_le32(shared_dma);
1211 
1212 	shared_dma = tp->shared_dma + shared_offset(txLo);
1213 	iface->txLoAddr = cpu_to_le32(shared_dma);
1214 	iface->txLoSize = cpu_to_le32(TXLO_ENTRIES * sizeof(struct tx_desc));
1215 
1216 	shared_dma = tp->shared_dma + shared_offset(txHi);
1217 	iface->txHiAddr = cpu_to_le32(shared_dma);
1218 	iface->txHiSize = cpu_to_le32(TXHI_ENTRIES * sizeof(struct tx_desc));
1219 
1220 	shared_dma = tp->shared_dma + shared_offset(rxBuff);
1221 	iface->rxBuffAddr = cpu_to_le32(shared_dma);
1222 	iface->rxBuffSize = cpu_to_le32(RXFREE_ENTRIES *
1223 					sizeof(struct rx_free));
1224 
1225 	shared_dma = tp->shared_dma + shared_offset(rxLo);
1226 	iface->rxLoAddr = cpu_to_le32(shared_dma);
1227 	iface->rxLoSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
1228 
1229 	shared_dma = tp->shared_dma + shared_offset(rxHi);
1230 	iface->rxHiAddr = cpu_to_le32(shared_dma);
1231 	iface->rxHiSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
1232 
1233 	shared_dma = tp->shared_dma + shared_offset(cmd);
1234 	iface->cmdAddr = cpu_to_le32(shared_dma);
1235 	iface->cmdSize = cpu_to_le32(COMMAND_RING_SIZE);
1236 
1237 	shared_dma = tp->shared_dma + shared_offset(resp);
1238 	iface->respAddr = cpu_to_le32(shared_dma);
1239 	iface->respSize = cpu_to_le32(RESPONSE_RING_SIZE);
1240 
1241 	shared_dma = tp->shared_dma + shared_offset(zeroWord);
1242 	iface->zeroAddr = cpu_to_le32(shared_dma);
1243 
1244 	tp->indexes = &tp->shared->indexes;
1245 	tp->txLoRing.ringBase = (u8 *) tp->shared->txLo;
1246 	tp->txHiRing.ringBase = (u8 *) tp->shared->txHi;
1247 	tp->rxLoRing.ringBase = (u8 *) tp->shared->rxLo;
1248 	tp->rxHiRing.ringBase = (u8 *) tp->shared->rxHi;
1249 	tp->rxBuffRing.ringBase = (u8 *) tp->shared->rxBuff;
1250 	tp->cmdRing.ringBase = (u8 *) tp->shared->cmd;
1251 	tp->respRing.ringBase = (u8 *) tp->shared->resp;
1252 
1253 	tp->txLoRing.writeRegister = TYPHOON_REG_TX_LO_READY;
1254 	tp->txHiRing.writeRegister = TYPHOON_REG_TX_HI_READY;
1255 
1256 	tp->txlo_dma_addr = le32_to_cpu(iface->txLoAddr);
1257 	tp->card_state = Sleeping;
1258 
1259 	tp->offload = TYPHOON_OFFLOAD_IP_CHKSUM | TYPHOON_OFFLOAD_TCP_CHKSUM;
1260 	tp->offload |= TYPHOON_OFFLOAD_UDP_CHKSUM | TSO_OFFLOAD_ON;
1261 	tp->offload |= TYPHOON_OFFLOAD_VLAN;
1262 
1263 	spin_lock_init(&tp->command_lock);
1264 
1265 	/* Force the writes to the shared memory area out before continuing. */
1266 	wmb();
1267 }
1268 
1269 static void
1270 typhoon_init_rings(struct typhoon *tp)
1271 {
1272 	memset(tp->indexes, 0, sizeof(struct typhoon_indexes));
1273 
1274 	tp->txLoRing.lastWrite = 0;
1275 	tp->txHiRing.lastWrite = 0;
1276 	tp->rxLoRing.lastWrite = 0;
1277 	tp->rxHiRing.lastWrite = 0;
1278 	tp->rxBuffRing.lastWrite = 0;
1279 	tp->cmdRing.lastWrite = 0;
1280 	tp->respRing.lastWrite = 0;
1281 
1282 	tp->txLoRing.lastRead = 0;
1283 	tp->txHiRing.lastRead = 0;
1284 }
1285 
1286 static const struct firmware *typhoon_fw;
1287 
1288 static int
1289 typhoon_request_firmware(struct typhoon *tp)
1290 {
1291 	const struct typhoon_file_header *fHdr;
1292 	const struct typhoon_section_header *sHdr;
1293 	const u8 *image_data;
1294 	u32 numSections;
1295 	u32 section_len;
1296 	u32 remaining;
1297 	int err;
1298 
1299 	if (typhoon_fw)
1300 		return 0;
1301 
1302 	err = request_firmware(&typhoon_fw, FIRMWARE_NAME, &tp->pdev->dev);
1303 	if (err) {
1304 		netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
1305 			   FIRMWARE_NAME);
1306 		return err;
1307 	}
1308 
1309 	image_data = typhoon_fw->data;
1310 	remaining = typhoon_fw->size;
1311 	if (remaining < sizeof(struct typhoon_file_header))
1312 		goto invalid_fw;
1313 
1314 	fHdr = (struct typhoon_file_header *) image_data;
1315 	if (memcmp(fHdr->tag, "TYPHOON", 8))
1316 		goto invalid_fw;
1317 
1318 	numSections = le32_to_cpu(fHdr->numSections);
1319 	image_data += sizeof(struct typhoon_file_header);
1320 	remaining -= sizeof(struct typhoon_file_header);
1321 
1322 	while (numSections--) {
1323 		if (remaining < sizeof(struct typhoon_section_header))
1324 			goto invalid_fw;
1325 
1326 		sHdr = (struct typhoon_section_header *) image_data;
1327 		image_data += sizeof(struct typhoon_section_header);
1328 		section_len = le32_to_cpu(sHdr->len);
1329 
1330 		if (remaining < section_len)
1331 			goto invalid_fw;
1332 
1333 		image_data += section_len;
1334 		remaining -= section_len;
1335 	}
1336 
1337 	return 0;
1338 
1339 invalid_fw:
1340 	netdev_err(tp->dev, "Invalid firmware image\n");
1341 	release_firmware(typhoon_fw);
1342 	typhoon_fw = NULL;
1343 	return -EINVAL;
1344 }
1345 
1346 static int
1347 typhoon_download_firmware(struct typhoon *tp)
1348 {
1349 	void __iomem *ioaddr = tp->ioaddr;
1350 	struct pci_dev *pdev = tp->pdev;
1351 	const struct typhoon_file_header *fHdr;
1352 	const struct typhoon_section_header *sHdr;
1353 	const u8 *image_data;
1354 	void *dpage;
1355 	dma_addr_t dpage_dma;
1356 	__sum16 csum;
1357 	u32 irqEnabled;
1358 	u32 irqMasked;
1359 	u32 numSections;
1360 	u32 section_len;
1361 	u32 len;
1362 	u32 load_addr;
1363 	u32 hmac;
1364 	int i;
1365 	int err;
1366 
1367 	image_data = typhoon_fw->data;
1368 	fHdr = (struct typhoon_file_header *) image_data;
1369 
1370 	/* Cannot just map the firmware image using dma_map_single() as
1371 	 * the firmware is vmalloc()'d and may not be physically contiguous,
1372 	 * so we allocate some coherent memory to copy the sections into.
1373 	 */
1374 	err = -ENOMEM;
1375 	dpage = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &dpage_dma, GFP_ATOMIC);
1376 	if (!dpage) {
1377 		netdev_err(tp->dev, "no DMA mem for firmware\n");
1378 		goto err_out;
1379 	}
1380 
1381 	irqEnabled = ioread32(ioaddr + TYPHOON_REG_INTR_ENABLE);
1382 	iowrite32(irqEnabled | TYPHOON_INTR_BOOTCMD,
1383 	       ioaddr + TYPHOON_REG_INTR_ENABLE);
1384 	irqMasked = ioread32(ioaddr + TYPHOON_REG_INTR_MASK);
1385 	iowrite32(irqMasked | TYPHOON_INTR_BOOTCMD,
1386 	       ioaddr + TYPHOON_REG_INTR_MASK);
1387 
1388 	err = -ETIMEDOUT;
1389 	if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
1390 		netdev_err(tp->dev, "card ready timeout\n");
1391 		goto err_out_irq;
1392 	}
1393 
1394 	numSections = le32_to_cpu(fHdr->numSections);
1395 	load_addr = le32_to_cpu(fHdr->startAddr);
1396 
1397 	iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
1398 	iowrite32(load_addr, ioaddr + TYPHOON_REG_DOWNLOAD_BOOT_ADDR);
1399 	hmac = le32_to_cpu(fHdr->hmacDigest[0]);
1400 	iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_0);
1401 	hmac = le32_to_cpu(fHdr->hmacDigest[1]);
1402 	iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_1);
1403 	hmac = le32_to_cpu(fHdr->hmacDigest[2]);
1404 	iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_2);
1405 	hmac = le32_to_cpu(fHdr->hmacDigest[3]);
1406 	iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_3);
1407 	hmac = le32_to_cpu(fHdr->hmacDigest[4]);
1408 	iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_4);
1409 	typhoon_post_pci_writes(ioaddr);
1410 	iowrite32(TYPHOON_BOOTCMD_RUNTIME_IMAGE, ioaddr + TYPHOON_REG_COMMAND);
1411 
1412 	image_data += sizeof(struct typhoon_file_header);
1413 
1414 	/* The ioread32() in typhoon_wait_interrupt() will force the
1415 	 * last write to the command register to post, so
1416 	 * we don't need a typhoon_post_pci_writes() after it.
1417 	 */
1418 	for (i = 0; i < numSections; i++) {
1419 		sHdr = (struct typhoon_section_header *) image_data;
1420 		image_data += sizeof(struct typhoon_section_header);
1421 		load_addr = le32_to_cpu(sHdr->startAddr);
1422 		section_len = le32_to_cpu(sHdr->len);
1423 
1424 		while (section_len) {
1425 			len = min_t(u32, section_len, PAGE_SIZE);
1426 
1427 			if (typhoon_wait_interrupt(ioaddr) < 0 ||
1428 			   ioread32(ioaddr + TYPHOON_REG_STATUS) !=
1429 			   TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
1430 				netdev_err(tp->dev, "segment ready timeout\n");
1431 				goto err_out_irq;
1432 			}
1433 
1434 			/* Do an pseudo IPv4 checksum on the data -- first
1435 			 * need to convert each u16 to cpu order before
1436 			 * summing. Fortunately, due to the properties of
1437 			 * the checksum, we can do this once, at the end.
1438 			 */
1439 			csum = csum_fold(csum_partial_copy_nocheck(image_data,
1440 								   dpage, len));
1441 
1442 			iowrite32(len, ioaddr + TYPHOON_REG_BOOT_LENGTH);
1443 			iowrite32(le16_to_cpu((__force __le16)csum),
1444 					ioaddr + TYPHOON_REG_BOOT_CHECKSUM);
1445 			iowrite32(load_addr,
1446 					ioaddr + TYPHOON_REG_BOOT_DEST_ADDR);
1447 			iowrite32(0, ioaddr + TYPHOON_REG_BOOT_DATA_HI);
1448 			iowrite32(dpage_dma, ioaddr + TYPHOON_REG_BOOT_DATA_LO);
1449 			typhoon_post_pci_writes(ioaddr);
1450 			iowrite32(TYPHOON_BOOTCMD_SEG_AVAILABLE,
1451 					ioaddr + TYPHOON_REG_COMMAND);
1452 
1453 			image_data += len;
1454 			load_addr += len;
1455 			section_len -= len;
1456 		}
1457 	}
1458 
1459 	if (typhoon_wait_interrupt(ioaddr) < 0 ||
1460 	   ioread32(ioaddr + TYPHOON_REG_STATUS) !=
1461 	   TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
1462 		netdev_err(tp->dev, "final segment ready timeout\n");
1463 		goto err_out_irq;
1464 	}
1465 
1466 	iowrite32(TYPHOON_BOOTCMD_DNLD_COMPLETE, ioaddr + TYPHOON_REG_COMMAND);
1467 
1468 	if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
1469 		netdev_err(tp->dev, "boot ready timeout, status 0x%0x\n",
1470 			   ioread32(ioaddr + TYPHOON_REG_STATUS));
1471 		goto err_out_irq;
1472 	}
1473 
1474 	err = 0;
1475 
1476 err_out_irq:
1477 	iowrite32(irqMasked, ioaddr + TYPHOON_REG_INTR_MASK);
1478 	iowrite32(irqEnabled, ioaddr + TYPHOON_REG_INTR_ENABLE);
1479 
1480 	dma_free_coherent(&pdev->dev, PAGE_SIZE, dpage, dpage_dma);
1481 
1482 err_out:
1483 	return err;
1484 }
1485 
1486 static int
1487 typhoon_boot_3XP(struct typhoon *tp, u32 initial_status)
1488 {
1489 	void __iomem *ioaddr = tp->ioaddr;
1490 
1491 	if (typhoon_wait_status(ioaddr, initial_status) < 0) {
1492 		netdev_err(tp->dev, "boot ready timeout\n");
1493 		goto out_timeout;
1494 	}
1495 
1496 	iowrite32(0, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_HI);
1497 	iowrite32(tp->shared_dma, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_LO);
1498 	typhoon_post_pci_writes(ioaddr);
1499 	iowrite32(TYPHOON_BOOTCMD_REG_BOOT_RECORD,
1500 				ioaddr + TYPHOON_REG_COMMAND);
1501 
1502 	if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_RUNNING) < 0) {
1503 		netdev_err(tp->dev, "boot finish timeout (status 0x%x)\n",
1504 			   ioread32(ioaddr + TYPHOON_REG_STATUS));
1505 		goto out_timeout;
1506 	}
1507 
1508 	/* Clear the Transmit and Command ready registers
1509 	 */
1510 	iowrite32(0, ioaddr + TYPHOON_REG_TX_HI_READY);
1511 	iowrite32(0, ioaddr + TYPHOON_REG_CMD_READY);
1512 	iowrite32(0, ioaddr + TYPHOON_REG_TX_LO_READY);
1513 	typhoon_post_pci_writes(ioaddr);
1514 	iowrite32(TYPHOON_BOOTCMD_BOOT, ioaddr + TYPHOON_REG_COMMAND);
1515 
1516 	return 0;
1517 
1518 out_timeout:
1519 	return -ETIMEDOUT;
1520 }
1521 
1522 static u32
1523 typhoon_clean_tx(struct typhoon *tp, struct transmit_ring *txRing,
1524 			volatile __le32 * index)
1525 {
1526 	u32 lastRead = txRing->lastRead;
1527 	struct tx_desc *tx;
1528 	dma_addr_t skb_dma;
1529 	int dma_len;
1530 	int type;
1531 
1532 	while (lastRead != le32_to_cpu(*index)) {
1533 		tx = (struct tx_desc *) (txRing->ringBase + lastRead);
1534 		type = tx->flags & TYPHOON_TYPE_MASK;
1535 
1536 		if (type == TYPHOON_TX_DESC) {
1537 			/* This tx_desc describes a packet.
1538 			 */
1539 			unsigned long ptr = tx->tx_addr;
1540 			struct sk_buff *skb = (struct sk_buff *) ptr;
1541 			dev_kfree_skb_irq(skb);
1542 		} else if (type == TYPHOON_FRAG_DESC) {
1543 			/* This tx_desc describes a memory mapping. Free it.
1544 			 */
1545 			skb_dma = (dma_addr_t) le32_to_cpu(tx->frag.addr);
1546 			dma_len = le16_to_cpu(tx->len);
1547 			dma_unmap_single(&tp->pdev->dev, skb_dma, dma_len,
1548 					 DMA_TO_DEVICE);
1549 		}
1550 
1551 		tx->flags = 0;
1552 		typhoon_inc_tx_index(&lastRead, 1);
1553 	}
1554 
1555 	return lastRead;
1556 }
1557 
1558 static void
1559 typhoon_tx_complete(struct typhoon *tp, struct transmit_ring *txRing,
1560 			volatile __le32 * index)
1561 {
1562 	u32 lastRead;
1563 	int numDesc = MAX_SKB_FRAGS + 1;
1564 
1565 	/* This will need changing if we start to use the Hi Tx ring. */
1566 	lastRead = typhoon_clean_tx(tp, txRing, index);
1567 	if (netif_queue_stopped(tp->dev) && typhoon_num_free(txRing->lastWrite,
1568 				lastRead, TXLO_ENTRIES) > (numDesc + 2))
1569 		netif_wake_queue(tp->dev);
1570 
1571 	txRing->lastRead = lastRead;
1572 	smp_wmb();
1573 }
1574 
1575 static void
1576 typhoon_recycle_rx_skb(struct typhoon *tp, u32 idx)
1577 {
1578 	struct typhoon_indexes *indexes = tp->indexes;
1579 	struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
1580 	struct basic_ring *ring = &tp->rxBuffRing;
1581 	struct rx_free *r;
1582 
1583 	if ((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
1584 				le32_to_cpu(indexes->rxBuffCleared)) {
1585 		/* no room in ring, just drop the skb
1586 		 */
1587 		dev_kfree_skb_any(rxb->skb);
1588 		rxb->skb = NULL;
1589 		return;
1590 	}
1591 
1592 	r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
1593 	typhoon_inc_rxfree_index(&ring->lastWrite, 1);
1594 	r->virtAddr = idx;
1595 	r->physAddr = cpu_to_le32(rxb->dma_addr);
1596 
1597 	/* Tell the card about it */
1598 	wmb();
1599 	indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
1600 }
1601 
1602 static int
1603 typhoon_alloc_rx_skb(struct typhoon *tp, u32 idx)
1604 {
1605 	struct typhoon_indexes *indexes = tp->indexes;
1606 	struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
1607 	struct basic_ring *ring = &tp->rxBuffRing;
1608 	struct rx_free *r;
1609 	struct sk_buff *skb;
1610 	dma_addr_t dma_addr;
1611 
1612 	rxb->skb = NULL;
1613 
1614 	if ((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
1615 				le32_to_cpu(indexes->rxBuffCleared))
1616 		return -ENOMEM;
1617 
1618 	skb = netdev_alloc_skb(tp->dev, PKT_BUF_SZ);
1619 	if (!skb)
1620 		return -ENOMEM;
1621 
1622 #if 0
1623 	/* Please, 3com, fix the firmware to allow DMA to a unaligned
1624 	 * address! Pretty please?
1625 	 */
1626 	skb_reserve(skb, 2);
1627 #endif
1628 
1629 	dma_addr = dma_map_single(&tp->pdev->dev, skb->data, PKT_BUF_SZ,
1630 				  DMA_FROM_DEVICE);
1631 
1632 	/* Since no card does 64 bit DAC, the high bits will never
1633 	 * change from zero.
1634 	 */
1635 	r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
1636 	typhoon_inc_rxfree_index(&ring->lastWrite, 1);
1637 	r->virtAddr = idx;
1638 	r->physAddr = cpu_to_le32(dma_addr);
1639 	rxb->skb = skb;
1640 	rxb->dma_addr = dma_addr;
1641 
1642 	/* Tell the card about it */
1643 	wmb();
1644 	indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
1645 	return 0;
1646 }
1647 
1648 static int
1649 typhoon_rx(struct typhoon *tp, struct basic_ring *rxRing, volatile __le32 * ready,
1650 	   volatile __le32 * cleared, int budget)
1651 {
1652 	struct rx_desc *rx;
1653 	struct sk_buff *skb, *new_skb;
1654 	struct rxbuff_ent *rxb;
1655 	dma_addr_t dma_addr;
1656 	u32 local_ready;
1657 	u32 rxaddr;
1658 	int pkt_len;
1659 	u32 idx;
1660 	__le32 csum_bits;
1661 	int received;
1662 
1663 	received = 0;
1664 	local_ready = le32_to_cpu(*ready);
1665 	rxaddr = le32_to_cpu(*cleared);
1666 	while (rxaddr != local_ready && budget > 0) {
1667 		rx = (struct rx_desc *) (rxRing->ringBase + rxaddr);
1668 		idx = rx->addr;
1669 		rxb = &tp->rxbuffers[idx];
1670 		skb = rxb->skb;
1671 		dma_addr = rxb->dma_addr;
1672 
1673 		typhoon_inc_rx_index(&rxaddr, 1);
1674 
1675 		if (rx->flags & TYPHOON_RX_ERROR) {
1676 			typhoon_recycle_rx_skb(tp, idx);
1677 			continue;
1678 		}
1679 
1680 		pkt_len = le16_to_cpu(rx->frameLen);
1681 
1682 		if (pkt_len < rx_copybreak &&
1683 		   (new_skb = netdev_alloc_skb(tp->dev, pkt_len + 2)) != NULL) {
1684 			skb_reserve(new_skb, 2);
1685 			dma_sync_single_for_cpu(&tp->pdev->dev, dma_addr,
1686 						PKT_BUF_SZ, DMA_FROM_DEVICE);
1687 			skb_copy_to_linear_data(new_skb, skb->data, pkt_len);
1688 			dma_sync_single_for_device(&tp->pdev->dev, dma_addr,
1689 						   PKT_BUF_SZ,
1690 						   DMA_FROM_DEVICE);
1691 			skb_put(new_skb, pkt_len);
1692 			typhoon_recycle_rx_skb(tp, idx);
1693 		} else {
1694 			new_skb = skb;
1695 			skb_put(new_skb, pkt_len);
1696 			dma_unmap_single(&tp->pdev->dev, dma_addr, PKT_BUF_SZ,
1697 					 DMA_FROM_DEVICE);
1698 			typhoon_alloc_rx_skb(tp, idx);
1699 		}
1700 		new_skb->protocol = eth_type_trans(new_skb, tp->dev);
1701 		csum_bits = rx->rxStatus & (TYPHOON_RX_IP_CHK_GOOD |
1702 			TYPHOON_RX_UDP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD);
1703 		if (csum_bits ==
1704 		   (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD) ||
1705 		   csum_bits ==
1706 		   (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_UDP_CHK_GOOD)) {
1707 			new_skb->ip_summed = CHECKSUM_UNNECESSARY;
1708 		} else
1709 			skb_checksum_none_assert(new_skb);
1710 
1711 		if (rx->rxStatus & TYPHOON_RX_VLAN)
1712 			__vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
1713 					       ntohl(rx->vlanTag) & 0xffff);
1714 		netif_receive_skb(new_skb);
1715 
1716 		received++;
1717 		budget--;
1718 	}
1719 	*cleared = cpu_to_le32(rxaddr);
1720 
1721 	return received;
1722 }
1723 
1724 static void
1725 typhoon_fill_free_ring(struct typhoon *tp)
1726 {
1727 	u32 i;
1728 
1729 	for (i = 0; i < RXENT_ENTRIES; i++) {
1730 		struct rxbuff_ent *rxb = &tp->rxbuffers[i];
1731 		if (rxb->skb)
1732 			continue;
1733 		if (typhoon_alloc_rx_skb(tp, i) < 0)
1734 			break;
1735 	}
1736 }
1737 
1738 static int
1739 typhoon_poll(struct napi_struct *napi, int budget)
1740 {
1741 	struct typhoon *tp = container_of(napi, struct typhoon, napi);
1742 	struct typhoon_indexes *indexes = tp->indexes;
1743 	int work_done;
1744 
1745 	rmb();
1746 	if (!tp->awaiting_resp && indexes->respReady != indexes->respCleared)
1747 			typhoon_process_response(tp, 0, NULL);
1748 
1749 	if (le32_to_cpu(indexes->txLoCleared) != tp->txLoRing.lastRead)
1750 		typhoon_tx_complete(tp, &tp->txLoRing, &indexes->txLoCleared);
1751 
1752 	work_done = 0;
1753 
1754 	if (indexes->rxHiCleared != indexes->rxHiReady) {
1755 		work_done += typhoon_rx(tp, &tp->rxHiRing, &indexes->rxHiReady,
1756 			   		&indexes->rxHiCleared, budget);
1757 	}
1758 
1759 	if (indexes->rxLoCleared != indexes->rxLoReady) {
1760 		work_done += typhoon_rx(tp, &tp->rxLoRing, &indexes->rxLoReady,
1761 					&indexes->rxLoCleared, budget - work_done);
1762 	}
1763 
1764 	if (le32_to_cpu(indexes->rxBuffCleared) == tp->rxBuffRing.lastWrite) {
1765 		/* rxBuff ring is empty, try to fill it. */
1766 		typhoon_fill_free_ring(tp);
1767 	}
1768 
1769 	if (work_done < budget) {
1770 		napi_complete_done(napi, work_done);
1771 		iowrite32(TYPHOON_INTR_NONE,
1772 				tp->ioaddr + TYPHOON_REG_INTR_MASK);
1773 		typhoon_post_pci_writes(tp->ioaddr);
1774 	}
1775 
1776 	return work_done;
1777 }
1778 
1779 static irqreturn_t
1780 typhoon_interrupt(int irq, void *dev_instance)
1781 {
1782 	struct net_device *dev = dev_instance;
1783 	struct typhoon *tp = netdev_priv(dev);
1784 	void __iomem *ioaddr = tp->ioaddr;
1785 	u32 intr_status;
1786 
1787 	intr_status = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
1788 	if (!(intr_status & TYPHOON_INTR_HOST_INT))
1789 		return IRQ_NONE;
1790 
1791 	iowrite32(intr_status, ioaddr + TYPHOON_REG_INTR_STATUS);
1792 
1793 	if (napi_schedule_prep(&tp->napi)) {
1794 		iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
1795 		typhoon_post_pci_writes(ioaddr);
1796 		__napi_schedule(&tp->napi);
1797 	} else {
1798 		netdev_err(dev, "Error, poll already scheduled\n");
1799 	}
1800 	return IRQ_HANDLED;
1801 }
1802 
1803 static void
1804 typhoon_free_rx_rings(struct typhoon *tp)
1805 {
1806 	u32 i;
1807 
1808 	for (i = 0; i < RXENT_ENTRIES; i++) {
1809 		struct rxbuff_ent *rxb = &tp->rxbuffers[i];
1810 		if (rxb->skb) {
1811 			dma_unmap_single(&tp->pdev->dev, rxb->dma_addr,
1812 					 PKT_BUF_SZ, DMA_FROM_DEVICE);
1813 			dev_kfree_skb(rxb->skb);
1814 			rxb->skb = NULL;
1815 		}
1816 	}
1817 }
1818 
1819 static int
1820 typhoon_sleep_early(struct typhoon *tp, __le16 events)
1821 {
1822 	void __iomem *ioaddr = tp->ioaddr;
1823 	struct cmd_desc xp_cmd;
1824 	int err;
1825 
1826 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_ENABLE_WAKE_EVENTS);
1827 	xp_cmd.parm1 = events;
1828 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1829 	if (err < 0) {
1830 		netdev_err(tp->dev, "typhoon_sleep(): wake events cmd err %d\n",
1831 			   err);
1832 		return err;
1833 	}
1834 
1835 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_GOTO_SLEEP);
1836 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1837 	if (err < 0) {
1838 		netdev_err(tp->dev, "typhoon_sleep(): sleep cmd err %d\n", err);
1839 		return err;
1840 	}
1841 
1842 	if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_SLEEPING) < 0)
1843 		return -ETIMEDOUT;
1844 
1845 	/* Since we cannot monitor the status of the link while sleeping,
1846 	 * tell the world it went away.
1847 	 */
1848 	netif_carrier_off(tp->dev);
1849 
1850 	return 0;
1851 }
1852 
1853 static int
1854 typhoon_sleep(struct typhoon *tp, pci_power_t state, __le16 events)
1855 {
1856 	int err;
1857 
1858 	err = typhoon_sleep_early(tp, events);
1859 
1860 	if (err)
1861 		return err;
1862 
1863 	pci_enable_wake(tp->pdev, state, 1);
1864 	pci_disable_device(tp->pdev);
1865 	return pci_set_power_state(tp->pdev, state);
1866 }
1867 
1868 static int
1869 typhoon_wakeup(struct typhoon *tp, int wait_type)
1870 {
1871 	void __iomem *ioaddr = tp->ioaddr;
1872 
1873 	/* Post 2.x.x versions of the Sleep Image require a reset before
1874 	 * we can download the Runtime Image. But let's not make users of
1875 	 * the old firmware pay for the reset.
1876 	 */
1877 	iowrite32(TYPHOON_BOOTCMD_WAKEUP, ioaddr + TYPHOON_REG_COMMAND);
1878 	if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0 ||
1879 			(tp->capabilities & TYPHOON_WAKEUP_NEEDS_RESET))
1880 		return typhoon_reset(ioaddr, wait_type);
1881 
1882 	return 0;
1883 }
1884 
1885 static int
1886 typhoon_start_runtime(struct typhoon *tp)
1887 {
1888 	struct net_device *dev = tp->dev;
1889 	void __iomem *ioaddr = tp->ioaddr;
1890 	struct cmd_desc xp_cmd;
1891 	int err;
1892 
1893 	typhoon_init_rings(tp);
1894 	typhoon_fill_free_ring(tp);
1895 
1896 	err = typhoon_download_firmware(tp);
1897 	if (err < 0) {
1898 		netdev_err(tp->dev, "cannot load runtime on 3XP\n");
1899 		goto error_out;
1900 	}
1901 
1902 	if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
1903 		netdev_err(tp->dev, "cannot boot 3XP\n");
1904 		err = -EIO;
1905 		goto error_out;
1906 	}
1907 
1908 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAX_PKT_SIZE);
1909 	xp_cmd.parm1 = cpu_to_le16(PKT_BUF_SZ);
1910 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1911 	if (err < 0)
1912 		goto error_out;
1913 
1914 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
1915 	xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
1916 	xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
1917 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1918 	if (err < 0)
1919 		goto error_out;
1920 
1921 	/* Disable IRQ coalescing -- we can reenable it when 3Com gives
1922 	 * us some more information on how to control it.
1923 	 */
1924 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_IRQ_COALESCE_CTRL);
1925 	xp_cmd.parm1 = 0;
1926 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1927 	if (err < 0)
1928 		goto error_out;
1929 
1930 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
1931 	xp_cmd.parm1 = tp->xcvr_select;
1932 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1933 	if (err < 0)
1934 		goto error_out;
1935 
1936 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_VLAN_TYPE_WRITE);
1937 	xp_cmd.parm1 = cpu_to_le16(ETH_P_8021Q);
1938 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1939 	if (err < 0)
1940 		goto error_out;
1941 
1942 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_OFFLOAD_TASKS);
1943 	xp_cmd.parm2 = tp->offload;
1944 	xp_cmd.parm3 = tp->offload;
1945 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1946 	if (err < 0)
1947 		goto error_out;
1948 
1949 	typhoon_set_rx_mode(dev);
1950 
1951 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_ENABLE);
1952 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1953 	if (err < 0)
1954 		goto error_out;
1955 
1956 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_ENABLE);
1957 	err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1958 	if (err < 0)
1959 		goto error_out;
1960 
1961 	tp->card_state = Running;
1962 	smp_wmb();
1963 
1964 	iowrite32(TYPHOON_INTR_ENABLE_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
1965 	iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_MASK);
1966 	typhoon_post_pci_writes(ioaddr);
1967 
1968 	return 0;
1969 
1970 error_out:
1971 	typhoon_reset(ioaddr, WaitNoSleep);
1972 	typhoon_free_rx_rings(tp);
1973 	typhoon_init_rings(tp);
1974 	return err;
1975 }
1976 
1977 static int
1978 typhoon_stop_runtime(struct typhoon *tp, int wait_type)
1979 {
1980 	struct typhoon_indexes *indexes = tp->indexes;
1981 	struct transmit_ring *txLo = &tp->txLoRing;
1982 	void __iomem *ioaddr = tp->ioaddr;
1983 	struct cmd_desc xp_cmd;
1984 	int i;
1985 
1986 	/* Disable interrupts early, since we can't schedule a poll
1987 	 * when called with !netif_running(). This will be posted
1988 	 * when we force the posting of the command.
1989 	 */
1990 	iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
1991 
1992 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_DISABLE);
1993 	typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
1994 
1995 	/* Wait 1/2 sec for any outstanding transmits to occur
1996 	 * We'll cleanup after the reset if this times out.
1997 	 */
1998 	for (i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
1999 		if (indexes->txLoCleared == cpu_to_le32(txLo->lastWrite))
2000 			break;
2001 		udelay(TYPHOON_UDELAY);
2002 	}
2003 
2004 	if (i == TYPHOON_WAIT_TIMEOUT)
2005 		netdev_err(tp->dev, "halt timed out waiting for Tx to complete\n");
2006 
2007 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_DISABLE);
2008 	typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
2009 
2010 	/* save the statistics so when we bring the interface up again,
2011 	 * the values reported to userspace are correct.
2012 	 */
2013 	tp->card_state = Sleeping;
2014 	smp_wmb();
2015 	typhoon_do_get_stats(tp);
2016 	memcpy(&tp->stats_saved, &tp->dev->stats, sizeof(struct net_device_stats));
2017 
2018 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_HALT);
2019 	typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
2020 
2021 	if (typhoon_wait_status(ioaddr, TYPHOON_STATUS_HALTED) < 0)
2022 		netdev_err(tp->dev, "timed out waiting for 3XP to halt\n");
2023 
2024 	if (typhoon_reset(ioaddr, wait_type) < 0) {
2025 		netdev_err(tp->dev, "unable to reset 3XP\n");
2026 		return -ETIMEDOUT;
2027 	}
2028 
2029 	/* cleanup any outstanding Tx packets */
2030 	if (indexes->txLoCleared != cpu_to_le32(txLo->lastWrite)) {
2031 		indexes->txLoCleared = cpu_to_le32(txLo->lastWrite);
2032 		typhoon_clean_tx(tp, &tp->txLoRing, &indexes->txLoCleared);
2033 	}
2034 
2035 	return 0;
2036 }
2037 
2038 static void
2039 typhoon_tx_timeout(struct net_device *dev, unsigned int txqueue)
2040 {
2041 	struct typhoon *tp = netdev_priv(dev);
2042 
2043 	if (typhoon_reset(tp->ioaddr, WaitNoSleep) < 0) {
2044 		netdev_warn(dev, "could not reset in tx timeout\n");
2045 		goto truly_dead;
2046 	}
2047 
2048 	/* If we ever start using the Hi ring, it will need cleaning too */
2049 	typhoon_clean_tx(tp, &tp->txLoRing, &tp->indexes->txLoCleared);
2050 	typhoon_free_rx_rings(tp);
2051 
2052 	if (typhoon_start_runtime(tp) < 0) {
2053 		netdev_err(dev, "could not start runtime in tx timeout\n");
2054 		goto truly_dead;
2055         }
2056 
2057 	netif_wake_queue(dev);
2058 	return;
2059 
2060 truly_dead:
2061 	/* Reset the hardware, and turn off carrier to avoid more timeouts */
2062 	typhoon_reset(tp->ioaddr, NoWait);
2063 	netif_carrier_off(dev);
2064 }
2065 
2066 static int
2067 typhoon_open(struct net_device *dev)
2068 {
2069 	struct typhoon *tp = netdev_priv(dev);
2070 	int err;
2071 
2072 	err = typhoon_request_firmware(tp);
2073 	if (err)
2074 		goto out;
2075 
2076 	pci_set_power_state(tp->pdev, PCI_D0);
2077 	pci_restore_state(tp->pdev);
2078 
2079 	err = typhoon_wakeup(tp, WaitSleep);
2080 	if (err < 0) {
2081 		netdev_err(dev, "unable to wakeup device\n");
2082 		goto out_sleep;
2083 	}
2084 
2085 	err = request_irq(dev->irq, typhoon_interrupt, IRQF_SHARED,
2086 				dev->name, dev);
2087 	if (err < 0)
2088 		goto out_sleep;
2089 
2090 	napi_enable(&tp->napi);
2091 
2092 	err = typhoon_start_runtime(tp);
2093 	if (err < 0) {
2094 		napi_disable(&tp->napi);
2095 		goto out_irq;
2096 	}
2097 
2098 	netif_start_queue(dev);
2099 	return 0;
2100 
2101 out_irq:
2102 	free_irq(dev->irq, dev);
2103 
2104 out_sleep:
2105 	if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
2106 		netdev_err(dev, "unable to reboot into sleep img\n");
2107 		typhoon_reset(tp->ioaddr, NoWait);
2108 		goto out;
2109 	}
2110 
2111 	if (typhoon_sleep(tp, PCI_D3hot, 0) < 0)
2112 		netdev_err(dev, "unable to go back to sleep\n");
2113 
2114 out:
2115 	return err;
2116 }
2117 
2118 static int
2119 typhoon_close(struct net_device *dev)
2120 {
2121 	struct typhoon *tp = netdev_priv(dev);
2122 
2123 	netif_stop_queue(dev);
2124 	napi_disable(&tp->napi);
2125 
2126 	if (typhoon_stop_runtime(tp, WaitSleep) < 0)
2127 		netdev_err(dev, "unable to stop runtime\n");
2128 
2129 	/* Make sure there is no irq handler running on a different CPU. */
2130 	free_irq(dev->irq, dev);
2131 
2132 	typhoon_free_rx_rings(tp);
2133 	typhoon_init_rings(tp);
2134 
2135 	if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0)
2136 		netdev_err(dev, "unable to boot sleep image\n");
2137 
2138 	if (typhoon_sleep(tp, PCI_D3hot, 0) < 0)
2139 		netdev_err(dev, "unable to put card to sleep\n");
2140 
2141 	return 0;
2142 }
2143 
2144 static int __maybe_unused
2145 typhoon_resume(struct device *dev_d)
2146 {
2147 	struct net_device *dev = dev_get_drvdata(dev_d);
2148 	struct typhoon *tp = netdev_priv(dev);
2149 
2150 	/* If we're down, resume when we are upped.
2151 	 */
2152 	if (!netif_running(dev))
2153 		return 0;
2154 
2155 	if (typhoon_wakeup(tp, WaitNoSleep) < 0) {
2156 		netdev_err(dev, "critical: could not wake up in resume\n");
2157 		goto reset;
2158 	}
2159 
2160 	if (typhoon_start_runtime(tp) < 0) {
2161 		netdev_err(dev, "critical: could not start runtime in resume\n");
2162 		goto reset;
2163 	}
2164 
2165 	netif_device_attach(dev);
2166 	return 0;
2167 
2168 reset:
2169 	typhoon_reset(tp->ioaddr, NoWait);
2170 	return -EBUSY;
2171 }
2172 
2173 static int __maybe_unused
2174 typhoon_suspend(struct device *dev_d)
2175 {
2176 	struct pci_dev *pdev = to_pci_dev(dev_d);
2177 	struct net_device *dev = pci_get_drvdata(pdev);
2178 	struct typhoon *tp = netdev_priv(dev);
2179 	struct cmd_desc xp_cmd;
2180 
2181 	/* If we're down, we're already suspended.
2182 	 */
2183 	if (!netif_running(dev))
2184 		return 0;
2185 
2186 	/* TYPHOON_OFFLOAD_VLAN is always on now, so this doesn't work */
2187 	if (tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
2188 		netdev_warn(dev, "cannot do WAKE_MAGIC with VLAN offloading\n");
2189 
2190 	netif_device_detach(dev);
2191 
2192 	if (typhoon_stop_runtime(tp, WaitNoSleep) < 0) {
2193 		netdev_err(dev, "unable to stop runtime\n");
2194 		goto need_resume;
2195 	}
2196 
2197 	typhoon_free_rx_rings(tp);
2198 	typhoon_init_rings(tp);
2199 
2200 	if (typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
2201 		netdev_err(dev, "unable to boot sleep image\n");
2202 		goto need_resume;
2203 	}
2204 
2205 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
2206 	xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
2207 	xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
2208 	if (typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
2209 		netdev_err(dev, "unable to set mac address in suspend\n");
2210 		goto need_resume;
2211 	}
2212 
2213 	INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
2214 	xp_cmd.parm1 = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
2215 	if (typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
2216 		netdev_err(dev, "unable to set rx filter in suspend\n");
2217 		goto need_resume;
2218 	}
2219 
2220 	if (typhoon_sleep_early(tp, tp->wol_events) < 0) {
2221 		netdev_err(dev, "unable to put card to sleep\n");
2222 		goto need_resume;
2223 	}
2224 
2225 	device_wakeup_enable(dev_d);
2226 
2227 	return 0;
2228 
2229 need_resume:
2230 	typhoon_resume(dev_d);
2231 	return -EBUSY;
2232 }
2233 
2234 static int
2235 typhoon_test_mmio(struct pci_dev *pdev)
2236 {
2237 	void __iomem *ioaddr = pci_iomap(pdev, 1, 128);
2238 	int mode = 0;
2239 	u32 val;
2240 
2241 	if (!ioaddr)
2242 		goto out;
2243 
2244 	if (ioread32(ioaddr + TYPHOON_REG_STATUS) !=
2245 				TYPHOON_STATUS_WAITING_FOR_HOST)
2246 		goto out_unmap;
2247 
2248 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
2249 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
2250 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
2251 
2252 	/* Ok, see if we can change our interrupt status register by
2253 	 * sending ourselves an interrupt. If so, then MMIO works.
2254 	 * The 50usec delay is arbitrary -- it could probably be smaller.
2255 	 */
2256 	val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2257 	if ((val & TYPHOON_INTR_SELF) == 0) {
2258 		iowrite32(1, ioaddr + TYPHOON_REG_SELF_INTERRUPT);
2259 		ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2260 		udelay(50);
2261 		val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2262 		if (val & TYPHOON_INTR_SELF)
2263 			mode = 1;
2264 	}
2265 
2266 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
2267 	iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
2268 	iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
2269 	ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
2270 
2271 out_unmap:
2272 	pci_iounmap(pdev, ioaddr);
2273 
2274 out:
2275 	if (!mode)
2276 		pr_info("%s: falling back to port IO\n", pci_name(pdev));
2277 	return mode;
2278 }
2279 
2280 #if MAX_SKB_FRAGS > 32
2281 
2282 #include <net/vxlan.h>
2283 
2284 static netdev_features_t typhoon_features_check(struct sk_buff *skb,
2285 						struct net_device *dev,
2286 						netdev_features_t features)
2287 {
2288 	if (skb_shinfo(skb)->nr_frags > 32 && skb_is_gso(skb))
2289 		features &= ~NETIF_F_GSO_MASK;
2290 
2291 	features = vlan_features_check(skb, features);
2292 	return vxlan_features_check(skb, features);
2293 }
2294 #endif
2295 
2296 static const struct net_device_ops typhoon_netdev_ops = {
2297 	.ndo_open		= typhoon_open,
2298 	.ndo_stop		= typhoon_close,
2299 #if MAX_SKB_FRAGS > 32
2300 	.ndo_features_check	= typhoon_features_check,
2301 #endif
2302 	.ndo_start_xmit		= typhoon_start_tx,
2303 	.ndo_set_rx_mode	= typhoon_set_rx_mode,
2304 	.ndo_tx_timeout		= typhoon_tx_timeout,
2305 	.ndo_get_stats		= typhoon_get_stats,
2306 	.ndo_validate_addr	= eth_validate_addr,
2307 	.ndo_set_mac_address	= eth_mac_addr,
2308 };
2309 
2310 static int
2311 typhoon_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2312 {
2313 	struct net_device *dev;
2314 	struct typhoon *tp;
2315 	int card_id = (int) ent->driver_data;
2316 	u8 addr[ETH_ALEN] __aligned(4);
2317 	void __iomem *ioaddr;
2318 	void *shared;
2319 	dma_addr_t shared_dma;
2320 	struct cmd_desc xp_cmd;
2321 	struct resp_desc xp_resp[3];
2322 	int err = 0;
2323 	const char *err_msg;
2324 
2325 	dev = alloc_etherdev(sizeof(*tp));
2326 	if (dev == NULL) {
2327 		err_msg = "unable to alloc new net device";
2328 		err = -ENOMEM;
2329 		goto error_out;
2330 	}
2331 	SET_NETDEV_DEV(dev, &pdev->dev);
2332 
2333 	err = pci_enable_device(pdev);
2334 	if (err < 0) {
2335 		err_msg = "unable to enable device";
2336 		goto error_out_dev;
2337 	}
2338 
2339 	err = pci_set_mwi(pdev);
2340 	if (err < 0) {
2341 		err_msg = "unable to set MWI";
2342 		goto error_out_disable;
2343 	}
2344 
2345 	err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
2346 	if (err < 0) {
2347 		err_msg = "No usable DMA configuration";
2348 		goto error_out_mwi;
2349 	}
2350 
2351 	/* sanity checks on IO and MMIO BARs
2352 	 */
2353 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
2354 		err_msg = "region #1 not a PCI IO resource, aborting";
2355 		err = -ENODEV;
2356 		goto error_out_mwi;
2357 	}
2358 	if (pci_resource_len(pdev, 0) < 128) {
2359 		err_msg = "Invalid PCI IO region size, aborting";
2360 		err = -ENODEV;
2361 		goto error_out_mwi;
2362 	}
2363 	if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
2364 		err_msg = "region #1 not a PCI MMIO resource, aborting";
2365 		err = -ENODEV;
2366 		goto error_out_mwi;
2367 	}
2368 	if (pci_resource_len(pdev, 1) < 128) {
2369 		err_msg = "Invalid PCI MMIO region size, aborting";
2370 		err = -ENODEV;
2371 		goto error_out_mwi;
2372 	}
2373 
2374 	err = pci_request_regions(pdev, KBUILD_MODNAME);
2375 	if (err < 0) {
2376 		err_msg = "could not request regions";
2377 		goto error_out_mwi;
2378 	}
2379 
2380 	/* map our registers
2381 	 */
2382 	if (use_mmio != 0 && use_mmio != 1)
2383 		use_mmio = typhoon_test_mmio(pdev);
2384 
2385 	ioaddr = pci_iomap(pdev, use_mmio, 128);
2386 	if (!ioaddr) {
2387 		err_msg = "cannot remap registers, aborting";
2388 		err = -EIO;
2389 		goto error_out_regions;
2390 	}
2391 
2392 	/* allocate pci dma space for rx and tx descriptor rings
2393 	 */
2394 	shared = dma_alloc_coherent(&pdev->dev, sizeof(struct typhoon_shared),
2395 				    &shared_dma, GFP_KERNEL);
2396 	if (!shared) {
2397 		err_msg = "could not allocate DMA memory";
2398 		err = -ENOMEM;
2399 		goto error_out_remap;
2400 	}
2401 
2402 	dev->irq = pdev->irq;
2403 	tp = netdev_priv(dev);
2404 	tp->shared = shared;
2405 	tp->shared_dma = shared_dma;
2406 	tp->pdev = pdev;
2407 	tp->tx_pdev = pdev;
2408 	tp->ioaddr = ioaddr;
2409 	tp->tx_ioaddr = ioaddr;
2410 	tp->dev = dev;
2411 
2412 	/* Init sequence:
2413 	 * 1) Reset the adapter to clear any bad juju
2414 	 * 2) Reload the sleep image
2415 	 * 3) Boot the sleep image
2416 	 * 4) Get the hardware address.
2417 	 * 5) Put the card to sleep.
2418 	 */
2419 	err = typhoon_reset(ioaddr, WaitSleep);
2420 	if (err < 0) {
2421 		err_msg = "could not reset 3XP";
2422 		goto error_out_dma;
2423 	}
2424 
2425 	/* Now that we've reset the 3XP and are sure it's not going to
2426 	 * write all over memory, enable bus mastering, and save our
2427 	 * state for resuming after a suspend.
2428 	 */
2429 	pci_set_master(pdev);
2430 	pci_save_state(pdev);
2431 
2432 	typhoon_init_interface(tp);
2433 	typhoon_init_rings(tp);
2434 
2435 	err = typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST);
2436 	if (err < 0) {
2437 		err_msg = "cannot boot 3XP sleep image";
2438 		goto error_out_reset;
2439 	}
2440 
2441 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_MAC_ADDRESS);
2442 	err = typhoon_issue_command(tp, 1, &xp_cmd, 1, xp_resp);
2443 	if (err < 0) {
2444 		err_msg = "cannot read MAC address";
2445 		goto error_out_reset;
2446 	}
2447 
2448 	*(__be16 *)&addr[0] = htons(le16_to_cpu(xp_resp[0].parm1));
2449 	*(__be32 *)&addr[2] = htonl(le32_to_cpu(xp_resp[0].parm2));
2450 	eth_hw_addr_set(dev, addr);
2451 
2452 	if (!is_valid_ether_addr(dev->dev_addr)) {
2453 		err_msg = "Could not obtain valid ethernet address, aborting";
2454 		err = -EIO;
2455 		goto error_out_reset;
2456 	}
2457 
2458 	/* Read the Sleep Image version last, so the response is valid
2459 	 * later when we print out the version reported.
2460 	 */
2461 	INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
2462 	err = typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp);
2463 	if (err < 0) {
2464 		err_msg = "Could not get Sleep Image version";
2465 		goto error_out_reset;
2466 	}
2467 
2468 	tp->capabilities = typhoon_card_info[card_id].capabilities;
2469 	tp->xcvr_select = TYPHOON_XCVR_AUTONEG;
2470 
2471 	/* Typhoon 1.0 Sleep Images return one response descriptor to the
2472 	 * READ_VERSIONS command. Those versions are OK after waking up
2473 	 * from sleep without needing a reset. Typhoon 1.1+ Sleep Images
2474 	 * seem to need a little extra help to get started. Since we don't
2475 	 * know how to nudge it along, just kick it.
2476 	 */
2477 	if (xp_resp[0].numDesc != 0)
2478 		tp->capabilities |= TYPHOON_WAKEUP_NEEDS_RESET;
2479 
2480 	err = typhoon_sleep(tp, PCI_D3hot, 0);
2481 	if (err < 0) {
2482 		err_msg = "cannot put adapter to sleep";
2483 		goto error_out_reset;
2484 	}
2485 
2486 	/* The chip-specific entries in the device structure. */
2487 	dev->netdev_ops		= &typhoon_netdev_ops;
2488 	netif_napi_add_weight(dev, &tp->napi, typhoon_poll, 16);
2489 	dev->watchdog_timeo	= TX_TIMEOUT;
2490 
2491 	dev->ethtool_ops = &typhoon_ethtool_ops;
2492 
2493 	/* We can handle scatter gather, up to 16 entries, and
2494 	 * we can do IP checksumming (only version 4, doh...)
2495 	 *
2496 	 * There's no way to turn off the RX VLAN offloading and stripping
2497 	 * on the current 3XP firmware -- it does not respect the offload
2498 	 * settings -- so we only allow the user to toggle the TX processing.
2499 	 */
2500 	dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
2501 		NETIF_F_HW_VLAN_CTAG_TX;
2502 	dev->features = dev->hw_features |
2503 		NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_RXCSUM;
2504 
2505 	err = register_netdev(dev);
2506 	if (err < 0) {
2507 		err_msg = "unable to register netdev";
2508 		goto error_out_reset;
2509 	}
2510 
2511 	pci_set_drvdata(pdev, dev);
2512 
2513 	netdev_info(dev, "%s at %s 0x%llx, %pM\n",
2514 		    typhoon_card_info[card_id].name,
2515 		    use_mmio ? "MMIO" : "IO",
2516 		    (unsigned long long)pci_resource_start(pdev, use_mmio),
2517 		    dev->dev_addr);
2518 
2519 	/* xp_resp still contains the response to the READ_VERSIONS command.
2520 	 * For debugging, let the user know what version he has.
2521 	 */
2522 	if (xp_resp[0].numDesc == 0) {
2523 		/* This is the Typhoon 1.0 type Sleep Image, last 16 bits
2524 		 * of version is Month/Day of build.
2525 		 */
2526 		u16 monthday = le32_to_cpu(xp_resp[0].parm2) & 0xffff;
2527 		netdev_info(dev, "Typhoon 1.0 Sleep Image built %02u/%02u/2000\n",
2528 			    monthday >> 8, monthday & 0xff);
2529 	} else if (xp_resp[0].numDesc == 2) {
2530 		/* This is the Typhoon 1.1+ type Sleep Image
2531 		 */
2532 		u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
2533 		u8 *ver_string = (u8 *) &xp_resp[1];
2534 		ver_string[25] = 0;
2535 		netdev_info(dev, "Typhoon 1.1+ Sleep Image version %02x.%03x.%03x %s\n",
2536 			    sleep_ver >> 24, (sleep_ver >> 12) & 0xfff,
2537 			    sleep_ver & 0xfff, ver_string);
2538 	} else {
2539 		netdev_warn(dev, "Unknown Sleep Image version (%u:%04x)\n",
2540 			    xp_resp[0].numDesc, le32_to_cpu(xp_resp[0].parm2));
2541 	}
2542 
2543 	return 0;
2544 
2545 error_out_reset:
2546 	typhoon_reset(ioaddr, NoWait);
2547 
2548 error_out_dma:
2549 	dma_free_coherent(&pdev->dev, sizeof(struct typhoon_shared), shared,
2550 			  shared_dma);
2551 error_out_remap:
2552 	pci_iounmap(pdev, ioaddr);
2553 error_out_regions:
2554 	pci_release_regions(pdev);
2555 error_out_mwi:
2556 	pci_clear_mwi(pdev);
2557 error_out_disable:
2558 	pci_disable_device(pdev);
2559 error_out_dev:
2560 	free_netdev(dev);
2561 error_out:
2562 	pr_err("%s: %s\n", pci_name(pdev), err_msg);
2563 	return err;
2564 }
2565 
2566 static void
2567 typhoon_remove_one(struct pci_dev *pdev)
2568 {
2569 	struct net_device *dev = pci_get_drvdata(pdev);
2570 	struct typhoon *tp = netdev_priv(dev);
2571 
2572 	unregister_netdev(dev);
2573 	pci_set_power_state(pdev, PCI_D0);
2574 	pci_restore_state(pdev);
2575 	typhoon_reset(tp->ioaddr, NoWait);
2576 	pci_iounmap(pdev, tp->ioaddr);
2577 	dma_free_coherent(&pdev->dev, sizeof(struct typhoon_shared),
2578 			  tp->shared, tp->shared_dma);
2579 	pci_release_regions(pdev);
2580 	pci_clear_mwi(pdev);
2581 	pci_disable_device(pdev);
2582 	free_netdev(dev);
2583 }
2584 
2585 static SIMPLE_DEV_PM_OPS(typhoon_pm_ops, typhoon_suspend, typhoon_resume);
2586 
2587 static struct pci_driver typhoon_driver = {
2588 	.name		= KBUILD_MODNAME,
2589 	.id_table	= typhoon_pci_tbl,
2590 	.probe		= typhoon_init_one,
2591 	.remove		= typhoon_remove_one,
2592 	.driver.pm	= &typhoon_pm_ops,
2593 };
2594 
2595 static int __init
2596 typhoon_init(void)
2597 {
2598 	return pci_register_driver(&typhoon_driver);
2599 }
2600 
2601 static void __exit
2602 typhoon_cleanup(void)
2603 {
2604 	release_firmware(typhoon_fw);
2605 	pci_unregister_driver(&typhoon_driver);
2606 }
2607 
2608 module_init(typhoon_init);
2609 module_exit(typhoon_cleanup);
2610