aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/net/Kconfig3
-rw-r--r--drivers/net/tc35815.c2587
-rw-r--r--include/linux/pci_ids.h2
3 files changed, 1917 insertions, 675 deletions
diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index a3d46ea37126..3e9b37055ea4 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -1444,7 +1444,8 @@ config CS89x0
1444 1444
1445config TC35815 1445config TC35815
1446 tristate "TOSHIBA TC35815 Ethernet support" 1446 tristate "TOSHIBA TC35815 Ethernet support"
1447 depends on NET_PCI && PCI && TOSHIBA_JMR3927 1447 depends on NET_PCI && PCI && MIPS
1448 select MII
1448 1449
1449config DGRS 1450config DGRS
1450 tristate "Digi Intl. RightSwitch SE-X support" 1451 tristate "Digi Intl. RightSwitch SE-X support"
diff --git a/drivers/net/tc35815.c b/drivers/net/tc35815.c
index d7741e23f8de..1a1b74c956bd 100644
--- a/drivers/net/tc35815.c
+++ b/drivers/net/tc35815.c
@@ -1,35 +1,72 @@
1/* tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux. 1/*
2 * 2 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
3 * Copyright 2001 MontaVista Software Inc.
4 * Author: MontaVista Software, Inc.
5 * ahennessy@mvista.com
6 * 3 *
7 * Based on skelton.c by Donald Becker. 4 * Based on skelton.c by Donald Becker.
8 * Copyright (C) 2000-2001 Toshiba Corporation
9 * 5 *
10 * This program is free software; you can redistribute it and/or modify it 6 * This driver is a replacement of older and less maintained version.
11 * under the terms of the GNU General Public License as published by the 7 * This is a header of the older version:
12 * Free Software Foundation; either version 2 of the License, or (at your 8 * -----<snip>-----
13 * option) any later version. 9 * Copyright 2001 MontaVista Software Inc.
10 * Author: MontaVista Software, Inc.
11 * ahennessy@mvista.com
12 * Copyright (C) 2000-2001 Toshiba Corporation
13 * static const char *version =
14 * "tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n";
15 * -----<snip>-----
16 *
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
14 * 20 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 21 * (C) Copyright TOSHIBA CORPORATION 2004-2005
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 * All Rights Reserved.
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
21 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * 23 *
26 * You should have received a copy of the GNU General Public License along 24 * Revision History:
27 * with this program; if not, write to the Free Software Foundation, Inc., 25 * 1.13 64-bit proof.
28 * 675 Mass Ave, Cambridge, MA 02139, USA. 26 * 1.14 Do not round-up transmit length.
27 * 1.15 Define TC35815_DMA_SYNC_ONDEMAND, cleanup.
28 * 1.16 Fix free_page bug introduced in 1.15
29 * 1.17 Add mii/ethtool ioctl support.
30 * Remove workaround for early TX4938. Cleanup.
31 * 1.20 Kernel 2.6.
32 * 1.21 Fix receive packet length (omit CRC).
33 * Call netif_carrier_on/netif_carrier_off.
34 * Add kernel/module options (speed, duplex, doforce).
35 * Do not try "force link mode" by default.
36 * Reconfigure CAM on restarting.
37 * Reset PHY on restarting.
38 * Add workaround for 100MHalf HUB.
39 * 1.22 Minor fix.
40 * 1.23 Minor cleanup.
41 * 1.24 Remove tc35815_setup since new stype option
42 * ("tc35815.speed=10", etc.) can be used for 2.6 kernel.
43 * 1.25 TX4939 support.
44 * 1.26 Minor cleanup.
45 * 1.27 Move TX4939 PCFG.SPEEDn control code out from this driver.
46 * Cleanup init_dev_addr. (NETDEV_REGISTER event notifier
47 * can overwrite dev_addr)
48 * support ETHTOOL_GPERMADDR.
49 * 1.28 Minor cleanup.
50 * 1.29 support netpoll.
51 * 1.30 Minor cleanup.
52 * 1.31 NAPI support. (disabled by default)
53 * Use DMA_RxAlign_2 if possible.
54 * Do not use PackedBuffer.
55 * Cleanup.
56 * 1.32 Fix free buffer management on non-PackedBuffer mode.
57 * 1.33 Fix netpoll build.
58 * 1.34 Fix netpoll locking. "BH rule" for NAPI is not enough with
59 * netpoll, hard_start_xmit might be called from irq context.
60 * PM support.
29 */ 61 */
30 62
31static const char *version = 63#ifdef TC35815_NAPI
32 "tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n"; 64#define DRV_VERSION "1.34-NAPI"
65#else
66#define DRV_VERSION "1.34"
67#endif
68static const char *version = "tc35815.c:v" DRV_VERSION "\n";
69#define MODNAME "tc35815"
33 70
34#include <linux/module.h> 71#include <linux/module.h>
35#include <linux/kernel.h> 72#include <linux/kernel.h>
@@ -40,6 +77,7 @@ static const char *version =
40#include <linux/in.h> 77#include <linux/in.h>
41#include <linux/slab.h> 78#include <linux/slab.h>
42#include <linux/string.h> 79#include <linux/string.h>
80#include <linux/spinlock.h>
43#include <linux/errno.h> 81#include <linux/errno.h>
44#include <linux/init.h> 82#include <linux/init.h>
45#include <linux/netdevice.h> 83#include <linux/netdevice.h>
@@ -47,36 +85,47 @@ static const char *version =
47#include <linux/skbuff.h> 85#include <linux/skbuff.h>
48#include <linux/delay.h> 86#include <linux/delay.h>
49#include <linux/pci.h> 87#include <linux/pci.h>
50#include <linux/proc_fs.h> 88#include <linux/mii.h>
51#include <linux/spinlock.h> 89#include <linux/ethtool.h>
52#include <linux/bitops.h>
53
54#include <asm/system.h>
55#include <asm/io.h> 90#include <asm/io.h>
56#include <asm/dma.h>
57#include <asm/byteorder.h> 91#include <asm/byteorder.h>
58 92
59/*
60 * The name of the card. Is used for messages and in the requests for
61 * io regions, irqs and dma channels
62 */
63static const char* cardname = "TC35815CF";
64#define TC35815_PROC_ENTRY "net/tc35815"
65
66#define TC35815_MODULE_NAME "TC35815CF"
67#define TX_TIMEOUT (4*HZ)
68
69/* First, a few definitions that the brave might change. */ 93/* First, a few definitions that the brave might change. */
70 94
71/* use 0 for production, 1 for verification, >2 for debug */
72#ifndef TC35815_DEBUG
73#define TC35815_DEBUG 1
74#endif
75static unsigned int tc35815_debug = TC35815_DEBUG;
76
77#define GATHER_TXINT /* On-Demand Tx Interrupt */ 95#define GATHER_TXINT /* On-Demand Tx Interrupt */
96#define WORKAROUND_LOSTCAR
97#define WORKAROUND_100HALF_PROMISC
98/* #define TC35815_USE_PACKEDBUFFER */
99
100typedef enum {
101 TC35815CF = 0,
102 TC35815_NWU,
103 TC35815_TX4939,
104} board_t;
105
106/* indexed by board_t, above */
107static const struct {
108 const char *name;
109} board_info[] __devinitdata = {
110 { "TOSHIBA TC35815CF 10/100BaseTX" },
111 { "TOSHIBA TC35815 with Wake on LAN" },
112 { "TOSHIBA TC35815/TX4939" },
113};
114
115static const struct pci_device_id tc35815_pci_tbl[] = {
116 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
117 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
118 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
119 {0,}
120};
121MODULE_DEVICE_TABLE (pci, tc35815_pci_tbl);
78 122
79#define vtonocache(p) KSEG1ADDR(virt_to_phys(p)) 123/* see MODULE_PARM_DESC */
124static struct tc35815_options {
125 int speed;
126 int duplex;
127 int doforce;
128} options;
80 129
81/* 130/*
82 * Registers 131 * Registers
@@ -119,6 +168,11 @@ struct tc35815_regs {
119 * Bit assignments 168 * Bit assignments
120 */ 169 */
121/* DMA_Ctl bit asign ------------------------------------------------------- */ 170/* DMA_Ctl bit asign ------------------------------------------------------- */
171#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
172#define DMA_RxAlign_1 0x00400000
173#define DMA_RxAlign_2 0x00800000
174#define DMA_RxAlign_3 0x00c00000
175#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
122#define DMA_IntMask 0x00040000 /* 1:Interupt mask */ 176#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
123#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */ 177#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
124#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */ 178#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
@@ -269,42 +323,6 @@ struct tc35815_regs {
269#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */ 323#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
270 324
271 325
272/* MII register offsets */
273#define MII_CONTROL 0x0000
274#define MII_STATUS 0x0001
275#define MII_PHY_ID0 0x0002
276#define MII_PHY_ID1 0x0003
277#define MII_ANAR 0x0004
278#define MII_ANLPAR 0x0005
279#define MII_ANER 0x0006
280/* MII Control register bit definitions. */
281#define MIICNTL_FDX 0x0100
282#define MIICNTL_RST_AUTO 0x0200
283#define MIICNTL_ISOLATE 0x0400
284#define MIICNTL_PWRDWN 0x0800
285#define MIICNTL_AUTO 0x1000
286#define MIICNTL_SPEED 0x2000
287#define MIICNTL_LPBK 0x4000
288#define MIICNTL_RESET 0x8000
289/* MII Status register bit significance. */
290#define MIISTAT_EXT 0x0001
291#define MIISTAT_JAB 0x0002
292#define MIISTAT_LINK 0x0004
293#define MIISTAT_CAN_AUTO 0x0008
294#define MIISTAT_FAULT 0x0010
295#define MIISTAT_AUTO_DONE 0x0020
296#define MIISTAT_CAN_T 0x0800
297#define MIISTAT_CAN_T_FDX 0x1000
298#define MIISTAT_CAN_TX 0x2000
299#define MIISTAT_CAN_TX_FDX 0x4000
300#define MIISTAT_CAN_T4 0x8000
301/* MII Auto-Negotiation Expansion/RemoteEnd Register Bits */
302#define MII_AN_TX_FDX 0x0100
303#define MII_AN_TX_HDX 0x0080
304#define MII_AN_10_FDX 0x0040
305#define MII_AN_10_HDX 0x0020
306
307
308/* 326/*
309 * Descriptors 327 * Descriptors
310 */ 328 */
@@ -352,32 +370,51 @@ struct BDesc {
352 370
353#ifdef NO_CHECK_CARRIER 371#ifdef NO_CHECK_CARRIER
354#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \ 372#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
355 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \ 373 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
356 Tx_En) /* maybe 0x7d01 */ 374 Tx_En) /* maybe 0x7b01 */
357#else 375#else
358#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \ 376#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
359 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \ 377 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
360 Tx_En) /* maybe 0x7f01 */ 378 Tx_En) /* maybe 0x7b01 */
361#endif 379#endif
362#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \ 380#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
363 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */ 381 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
364
365#define INT_EN_CMD (Int_NRAbtEn | \ 382#define INT_EN_CMD (Int_NRAbtEn | \
366 Int_DParDEn | Int_DParErrEn | \ 383 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
367 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \ 384 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
368 Int_STargAbtEn | \ 385 Int_STargAbtEn | \
369 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/ 386 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
387#define DMA_CTL_CMD DMA_BURST_SIZE
388#define HAVE_DMA_RXALIGN(lp) likely((lp)->boardtype != TC35815CF)
370 389
371/* Tuning parameters */ 390/* Tuning parameters */
372#define DMA_BURST_SIZE 32 391#define DMA_BURST_SIZE 32
373#define TX_THRESHOLD 1024 392#define TX_THRESHOLD 1024
393#define TX_THRESHOLD_MAX 1536 /* used threshold with packet max byte for low pci transfer ability.*/
394#define TX_THRESHOLD_KEEP_LIMIT 10 /* setting threshold max value when overrun error occured this count. */
374 395
396/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
397#ifdef TC35815_USE_PACKEDBUFFER
375#define FD_PAGE_NUM 2 398#define FD_PAGE_NUM 2
376#define FD_PAGE_ORDER 1 399#define RX_BUF_NUM 8 /* >= 2 */
377/* 16 + RX_BUF_PAGES * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*2 */
378#define RX_BUF_PAGES 8 /* >= 2 */
379#define RX_FD_NUM 250 /* >= 32 */ 400#define RX_FD_NUM 250 /* >= 32 */
380#define TX_FD_NUM 128 401#define TX_FD_NUM 128
402#define RX_BUF_SIZE PAGE_SIZE
403#else /* TC35815_USE_PACKEDBUFFER */
404#define FD_PAGE_NUM 4
405#define RX_BUF_NUM 128 /* < 256 */
406#define RX_FD_NUM 256 /* >= 32 */
407#define TX_FD_NUM 128
408#if RX_CTL_CMD & Rx_LongEn
409#define RX_BUF_SIZE PAGE_SIZE
410#elif RX_CTL_CMD & Rx_StripCRC
411#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */
412#else
413#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */
414#endif
415#endif /* TC35815_USE_PACKEDBUFFER */
416#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
417#define NAPI_WEIGHT 16
381 418
382struct TxFD { 419struct TxFD {
383 struct FDesc fd; 420 struct FDesc fd;
@@ -392,18 +429,27 @@ struct RxFD {
392 429
393struct FrFD { 430struct FrFD {
394 struct FDesc fd; 431 struct FDesc fd;
395 struct BDesc bd[RX_BUF_PAGES]; 432 struct BDesc bd[RX_BUF_NUM];
396}; 433};
397 434
398 435
399extern unsigned long tc_readl(volatile __u32 *addr); 436#define tc_readl(addr) readl(addr)
400extern void tc_writel(unsigned long data, volatile __u32 *addr); 437#define tc_writel(d, addr) writel(d, addr)
401 438
402dma_addr_t priv_dma_handle; 439#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
440
441/* Timer state engine. */
442enum tc35815_timer_state {
443 arbwait = 0, /* Waiting for auto negotiation to complete. */
444 lupwait = 1, /* Auto-neg complete, awaiting link-up status. */
445 ltrywait = 2, /* Forcing try of all modes, from fastest to slowest. */
446 asleep = 3, /* Time inactive. */
447 lcheck = 4, /* Check link status. */
448};
403 449
404/* Information that need to be kept for each board. */ 450/* Information that need to be kept for each board. */
405struct tc35815_local { 451struct tc35815_local {
406 struct net_device *next_module; 452 struct pci_dev *pci_dev;
407 453
408 /* statistics */ 454 /* statistics */
409 struct net_device_stats stats; 455 struct net_device_stats stats;
@@ -411,216 +457,372 @@ struct tc35815_local {
411 int max_tx_qlen; 457 int max_tx_qlen;
412 int tx_ints; 458 int tx_ints;
413 int rx_ints; 459 int rx_ints;
460 int tx_underrun;
414 } lstats; 461 } lstats;
415 462
416 int tbusy; 463 /* Tx control lock. This protects the transmit buffer ring
417 int option; 464 * state along with the "tx full" state of the driver. This
418#define TC35815_OPT_AUTO 0x00 465 * means all netif_queue flow control actions are protected
419#define TC35815_OPT_10M 0x01 466 * by this lock as well.
420#define TC35815_OPT_100M 0x02 467 */
421#define TC35815_OPT_FULLDUP 0x04 468 spinlock_t lock;
422 int linkspeed; /* 10 or 100 */ 469
470 int phy_addr;
423 int fullduplex; 471 int fullduplex;
472 unsigned short saved_lpa;
473 struct timer_list timer;
474 enum tc35815_timer_state timer_state; /* State of auto-neg timer. */
475 unsigned int timer_ticks; /* Number of clicks at each state */
424 476
425 /* 477 /*
426 * Transmitting: Batch Mode. 478 * Transmitting: Batch Mode.
427 * 1 BD in 1 TxFD. 479 * 1 BD in 1 TxFD.
428 * Receiving: Packing Mode. 480 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
429 * 1 circular FD for Free Buffer List. 481 * 1 circular FD for Free Buffer List.
430 * RX_BUG_PAGES BD in Free Buffer FD. 482 * RX_BUF_NUM BD in Free Buffer FD.
431 * One Free Buffer BD has PAGE_SIZE data buffer. 483 * One Free Buffer BD has PAGE_SIZE data buffer.
484 * Or Non-Packing Mode.
485 * 1 circular FD for Free Buffer List.
486 * RX_BUF_NUM BD in Free Buffer FD.
487 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
432 */ 488 */
433 struct pci_dev *pdev; 489 void * fd_buf; /* for TxFD, RxFD, FrFD */
434 dma_addr_t fd_buf_dma_handle; 490 dma_addr_t fd_buf_dma;
435 void * fd_buf; /* for TxFD, TxFD, FrFD */
436 struct TxFD *tfd_base; 491 struct TxFD *tfd_base;
437 int tfd_start; 492 unsigned int tfd_start;
438 int tfd_end; 493 unsigned int tfd_end;
439 struct RxFD *rfd_base; 494 struct RxFD *rfd_base;
440 struct RxFD *rfd_limit; 495 struct RxFD *rfd_limit;
441 struct RxFD *rfd_cur; 496 struct RxFD *rfd_cur;
442 struct FrFD *fbl_ptr; 497 struct FrFD *fbl_ptr;
498#ifdef TC35815_USE_PACKEDBUFFER
443 unsigned char fbl_curid; 499 unsigned char fbl_curid;
444 dma_addr_t data_buf_dma_handle[RX_BUF_PAGES]; 500 void * data_buf[RX_BUF_NUM]; /* packing */
445 void * data_buf[RX_BUF_PAGES]; /* packing */ 501 dma_addr_t data_buf_dma[RX_BUF_NUM];
446 spinlock_t lock; 502 struct {
503 struct sk_buff *skb;
504 dma_addr_t skb_dma;
505 } tx_skbs[TX_FD_NUM];
506#else
507 unsigned int fbl_count;
508 struct {
509 struct sk_buff *skb;
510 dma_addr_t skb_dma;
511 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
512#endif
513 struct mii_if_info mii;
514 unsigned short mii_id[2];
515 u32 msg_enable;
516 board_t boardtype;
447}; 517};
448 518
449/* Index to functions, as function prototypes. */ 519static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
520{
521 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
522}
523#ifdef DEBUG
524static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
525{
526 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
527}
528#endif
529#ifdef TC35815_USE_PACKEDBUFFER
530static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
531{
532 int i;
533 for (i = 0; i < RX_BUF_NUM; i++) {
534 if (bus >= lp->data_buf_dma[i] &&
535 bus < lp->data_buf_dma[i] + PAGE_SIZE)
536 return (void *)((u8 *)lp->data_buf[i] +
537 (bus - lp->data_buf_dma[i]));
538 }
539 return NULL;
540}
450 541
451static int __devinit tc35815_probe1(struct pci_dev *pdev, unsigned int base_addr, unsigned int irq); 542#define TC35815_DMA_SYNC_ONDEMAND
543static void* alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
544{
545#ifdef TC35815_DMA_SYNC_ONDEMAND
546 void *buf;
547 /* pci_map + pci_dma_sync will be more effective than
548 * pci_alloc_consistent on some archs. */
549 if ((buf = (void *)__get_free_page(GFP_ATOMIC)) == NULL)
550 return NULL;
551 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
552 PCI_DMA_FROMDEVICE);
553 if (pci_dma_mapping_error(*dma_handle)) {
554 free_page((unsigned long)buf);
555 return NULL;
556 }
557 return buf;
558#else
559 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
560#endif
561}
562
563static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
564{
565#ifdef TC35815_DMA_SYNC_ONDEMAND
566 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
567 free_page((unsigned long)buf);
568#else
569 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
570#endif
571}
572#else /* TC35815_USE_PACKEDBUFFER */
573static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
574 struct pci_dev *hwdev,
575 dma_addr_t *dma_handle)
576{
577 struct sk_buff *skb;
578 skb = dev_alloc_skb(RX_BUF_SIZE);
579 if (!skb)
580 return NULL;
581 skb->dev = dev;
582 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
583 PCI_DMA_FROMDEVICE);
584 if (pci_dma_mapping_error(*dma_handle)) {
585 dev_kfree_skb_any(skb);
586 return NULL;
587 }
588 skb_reserve(skb, 2); /* make IP header 4byte aligned */
589 return skb;
590}
591
592static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
593{
594 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
595 PCI_DMA_FROMDEVICE);
596 dev_kfree_skb_any(skb);
597}
598#endif /* TC35815_USE_PACKEDBUFFER */
599
600/* Index to functions, as function prototypes. */
452 601
453static int tc35815_open(struct net_device *dev); 602static int tc35815_open(struct net_device *dev);
454static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev); 603static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
455static void tc35815_tx_timeout(struct net_device *dev); 604static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
456static irqreturn_t tc35815_interrupt(int irq, void *dev_id); 605#ifdef TC35815_NAPI
606static int tc35815_rx(struct net_device *dev, int limit);
607static int tc35815_poll(struct net_device *dev, int *budget);
608#else
457static void tc35815_rx(struct net_device *dev); 609static void tc35815_rx(struct net_device *dev);
610#endif
458static void tc35815_txdone(struct net_device *dev); 611static void tc35815_txdone(struct net_device *dev);
459static int tc35815_close(struct net_device *dev); 612static int tc35815_close(struct net_device *dev);
460static struct net_device_stats *tc35815_get_stats(struct net_device *dev); 613static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
461static void tc35815_set_multicast_list(struct net_device *dev); 614static void tc35815_set_multicast_list(struct net_device *dev);
615static void tc35815_tx_timeout(struct net_device *dev);
616static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
617#ifdef CONFIG_NET_POLL_CONTROLLER
618static void tc35815_poll_controller(struct net_device *dev);
619#endif
620static const struct ethtool_ops tc35815_ethtool_ops;
462 621
622/* Example routines you must write ;->. */
463static void tc35815_chip_reset(struct net_device *dev); 623static void tc35815_chip_reset(struct net_device *dev);
464static void tc35815_chip_init(struct net_device *dev); 624static void tc35815_chip_init(struct net_device *dev);
625static void tc35815_find_phy(struct net_device *dev);
465static void tc35815_phy_chip_init(struct net_device *dev); 626static void tc35815_phy_chip_init(struct net_device *dev);
466 627
467/* A list of all installed tc35815 devices. */ 628#ifdef DEBUG
468static struct net_device *root_tc35815_dev = NULL; 629static void panic_queues(struct net_device *dev);
630#endif
469 631
470/* 632static void tc35815_timer(unsigned long data);
471 * PCI device identifiers for "new style" Linux PCI Device Drivers 633static void tc35815_start_auto_negotiation(struct net_device *dev,
472 */ 634 struct ethtool_cmd *ep);
473static struct pci_device_id tc35815_pci_tbl[] = { 635static int tc_mdio_read(struct net_device *dev, int phy_id, int location);
474 { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, 636static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
475 { 0, } 637 int val);
476};
477 638
478MODULE_DEVICE_TABLE (pci, tc35815_pci_tbl); 639static void __devinit tc35815_init_dev_addr (struct net_device *dev)
640{
641 struct tc35815_regs __iomem *tr =
642 (struct tc35815_regs __iomem *)dev->base_addr;
643 int i;
479 644
480int 645 /* dev_addr will be overwritten on NETDEV_REGISTER event */
481tc35815_probe(struct pci_dev *pdev, 646 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
482 const struct pci_device_id *ent) 647 ;
648 for (i = 0; i < 6; i += 2) {
649 unsigned short data;
650 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
651 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
652 ;
653 data = tc_readl(&tr->PROM_Data);
654 dev->dev_addr[i] = data & 0xff;
655 dev->dev_addr[i+1] = data >> 8;
656 }
657}
658
659static int __devinit tc35815_init_one (struct pci_dev *pdev,
660 const struct pci_device_id *ent)
483{ 661{
484 int err = 0; 662 void __iomem *ioaddr = NULL;
485 int ret; 663 struct net_device *dev;
486 unsigned long pci_memaddr; 664 struct tc35815_local *lp;
487 unsigned int pci_irq_line; 665 int rc;
666 unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
667
668 static int printed_version;
669 if (!printed_version++) {
670 printk(version);
671 dev_printk(KERN_DEBUG, &pdev->dev,
672 "speed:%d duplex:%d doforce:%d\n",
673 options.speed, options.duplex, options.doforce);
674 }
488 675
489 printk(KERN_INFO "tc35815_probe: found device %#08x.%#08x\n", ent->vendor, ent->device); 676 if (!pdev->irq) {
677 dev_warn(&pdev->dev, "no IRQ assigned.\n");
678 return -ENODEV;
679 }
490 680
491 err = pci_enable_device(pdev); 681 /* dev zeroed in alloc_etherdev */
492 if (err) 682 dev = alloc_etherdev (sizeof (*lp));
493 return err; 683 if (dev == NULL) {
684 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
685 return -ENOMEM;
686 }
687 SET_MODULE_OWNER(dev);
688 SET_NETDEV_DEV(dev, &pdev->dev);
689 lp = dev->priv;
494 690
495 pci_memaddr = pci_resource_start (pdev, 1); 691 /* enable device (incl. PCI PM wakeup), and bus-mastering */
692 rc = pci_enable_device (pdev);
693 if (rc)
694 goto err_out;
496 695
497 printk(KERN_INFO " pci_memaddr=%#08lx resource_flags=%#08lx\n", pci_memaddr, pci_resource_flags (pdev, 0)); 696 mmio_start = pci_resource_start (pdev, 1);
697 mmio_end = pci_resource_end (pdev, 1);
698 mmio_flags = pci_resource_flags (pdev, 1);
699 mmio_len = pci_resource_len (pdev, 1);
498 700
499 if (!pci_memaddr) { 701 /* set this immediately, we need to know before
500 printk(KERN_WARNING "no PCI MEM resources, aborting\n"); 702 * we talk to the chip directly */
501 ret = -ENODEV; 703
704 /* make sure PCI base addr 1 is MMIO */
705 if (!(mmio_flags & IORESOURCE_MEM)) {
706 dev_err(&pdev->dev, "region #1 not an MMIO resource, aborting\n");
707 rc = -ENODEV;
502 goto err_out; 708 goto err_out;
503 } 709 }
504 pci_irq_line = pdev->irq; 710
505 /* irq disabled. */ 711 /* check for weird/broken PCI region reporting */
506 if (pci_irq_line == 0) { 712 if ((mmio_len < sizeof(struct tc35815_regs))) {
507 printk(KERN_WARNING "no PCI irq, aborting\n"); 713 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
508 ret = -ENODEV; 714 rc = -ENODEV;
509 goto err_out; 715 goto err_out;
510 } 716 }
511 717
512 ret = tc35815_probe1(pdev, pci_memaddr, pci_irq_line); 718 rc = pci_request_regions (pdev, MODNAME);
513 if (ret) 719 if (rc)
514 goto err_out; 720 goto err_out;
515 721
516 pci_set_master(pdev); 722 pci_set_master (pdev);
517 723
518 return 0; 724 /* ioremap MMIO region */
519 725 ioaddr = ioremap (mmio_start, mmio_len);
520err_out: 726 if (ioaddr == NULL) {
521 pci_disable_device(pdev); 727 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
522 return ret; 728 rc = -EIO;
523} 729 goto err_out_free_res;
730 }
524 731
525static int __devinit tc35815_probe1(struct pci_dev *pdev, unsigned int base_addr, unsigned int irq) 732 /* Initialize the device structure. */
526{ 733 dev->open = tc35815_open;
527 static unsigned version_printed = 0; 734 dev->hard_start_xmit = tc35815_send_packet;
528 int i, ret; 735 dev->stop = tc35815_close;
529 struct tc35815_local *lp; 736 dev->get_stats = tc35815_get_stats;
530 struct tc35815_regs *tr; 737 dev->set_multicast_list = tc35815_set_multicast_list;
531 struct net_device *dev; 738 dev->do_ioctl = tc35815_ioctl;
739 dev->ethtool_ops = &tc35815_ethtool_ops;
740 dev->tx_timeout = tc35815_tx_timeout;
741 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
742#ifdef TC35815_NAPI
743 dev->poll = tc35815_poll;
744 dev->weight = NAPI_WEIGHT;
745#endif
746#ifdef CONFIG_NET_POLL_CONTROLLER
747 dev->poll_controller = tc35815_poll_controller;
748#endif
532 749
533 /* Allocate a new 'dev' if needed. */ 750 dev->irq = pdev->irq;
534 dev = alloc_etherdev(sizeof(struct tc35815_local)); 751 dev->base_addr = (unsigned long) ioaddr;
535 if (dev == NULL)
536 return -ENOMEM;
537 752
538 /* 753 /* dev->priv/lp zeroed and aligned in alloc_etherdev */
539 * alloc_etherdev allocs and zeros dev->priv
540 */
541 lp = dev->priv; 754 lp = dev->priv;
755 spin_lock_init(&lp->lock);
756 lp->pci_dev = pdev;
757 lp->boardtype = ent->driver_data;
542 758
543 if (tc35815_debug && version_printed++ == 0) 759 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
544 printk(KERN_DEBUG "%s", version); 760 pci_set_drvdata(pdev, dev);
545
546 /* Fill in the 'dev' fields. */
547 dev->irq = irq;
548 dev->base_addr = (unsigned long)ioremap(base_addr,
549 sizeof(struct tc35815_regs));
550 if (!dev->base_addr) {
551 ret = -ENOMEM;
552 goto err_out;
553 }
554 tr = (struct tc35815_regs*)dev->base_addr;
555 761
762 /* Soft reset the chip. */
556 tc35815_chip_reset(dev); 763 tc35815_chip_reset(dev);
557 764
558 /* Retrieve and print the ethernet address. */ 765 /* Retrieve the ethernet address. */
559 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy) 766 tc35815_init_dev_addr(dev);
560 ; 767
561 for (i = 0; i < 6; i += 2) { 768 rc = register_netdev (dev);
562 unsigned short data; 769 if (rc)
563 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl); 770 goto err_out_unmap;
564 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy) 771
565 ; 772 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
566 data = tc_readl(&tr->PROM_Data); 773 printk(KERN_INFO "%s: %s at 0x%lx, "
567 dev->dev_addr[i] = data & 0xff; 774 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
568 dev->dev_addr[i+1] = data >> 8; 775 "IRQ %d\n",
569 } 776 dev->name,
777 board_info[ent->driver_data].name,
778 dev->base_addr,
779 dev->dev_addr[0], dev->dev_addr[1],
780 dev->dev_addr[2], dev->dev_addr[3],
781 dev->dev_addr[4], dev->dev_addr[5],
782 dev->irq);
783
784 setup_timer(&lp->timer, tc35815_timer, (unsigned long) dev);
785 lp->mii.dev = dev;
786 lp->mii.mdio_read = tc_mdio_read;
787 lp->mii.mdio_write = tc_mdio_write;
788 lp->mii.phy_id_mask = 0x1f;
789 lp->mii.reg_num_mask = 0x1f;
790 tc35815_find_phy(dev);
791 lp->mii.phy_id = lp->phy_addr;
792 lp->mii.full_duplex = 0;
793 lp->mii.force_media = 0;
570 794
571 /* Initialize the device structure. */ 795 return 0;
572 lp->pdev = pdev;
573 lp->next_module = root_tc35815_dev;
574 root_tc35815_dev = dev;
575 796
576 spin_lock_init(&lp->lock); 797err_out_unmap:
798 iounmap(ioaddr);
799err_out_free_res:
800 pci_release_regions (pdev);
801err_out:
802 free_netdev (dev);
803 return rc;
804}
577 805
578 if (dev->mem_start > 0) {
579 lp->option = dev->mem_start;
580 if ((lp->option & TC35815_OPT_10M) &&
581 (lp->option & TC35815_OPT_100M)) {
582 /* if both speed speficied, auto select. */
583 lp->option &= ~(TC35815_OPT_10M | TC35815_OPT_100M);
584 }
585 }
586 //XXX fixme
587 lp->option |= TC35815_OPT_10M;
588 806
589 /* do auto negotiation */ 807static void __devexit tc35815_remove_one (struct pci_dev *pdev)
590 tc35815_phy_chip_init(dev); 808{
809 struct net_device *dev = pci_get_drvdata (pdev);
810 unsigned long mmio_addr;
591 811
592 dev->open = tc35815_open; 812 mmio_addr = dev->base_addr;
593 dev->stop = tc35815_close;
594 dev->tx_timeout = tc35815_tx_timeout;
595 dev->watchdog_timeo = TX_TIMEOUT;
596 dev->hard_start_xmit = tc35815_send_packet;
597 dev->get_stats = tc35815_get_stats;
598 dev->set_multicast_list = tc35815_set_multicast_list;
599 SET_MODULE_OWNER(dev);
600 SET_NETDEV_DEV(dev, &pdev->dev);
601 813
602 ret = register_netdev(dev); 814 unregister_netdev (dev);
603 if (ret)
604 goto err_out_iounmap;
605 815
606 printk(KERN_INFO "%s: %s found at %#x, irq %d, MAC", 816 if (mmio_addr) {
607 dev->name, cardname, base_addr, irq); 817 iounmap ((void __iomem *)mmio_addr);
608 for (i = 0; i < 6; i++) 818 pci_release_regions (pdev);
609 printk(" %2.2x", dev->dev_addr[i]); 819 }
610 printk("\n");
611 printk(KERN_INFO "%s: linkspeed %dMbps, %s Duplex\n",
612 dev->name, lp->linkspeed, lp->fullduplex ? "Full" : "Half");
613 820
614 return 0; 821 free_netdev (dev);
615 822
616err_out_iounmap: 823 pci_set_drvdata (pdev, NULL);
617 iounmap((void *) dev->base_addr);
618err_out:
619 free_netdev(dev);
620 return ret;
621} 824}
622 825
623
624static int 826static int
625tc35815_init_queues(struct net_device *dev) 827tc35815_init_queues(struct net_device *dev)
626{ 828{
@@ -629,44 +831,64 @@ tc35815_init_queues(struct net_device *dev)
629 unsigned long fd_addr; 831 unsigned long fd_addr;
630 832
631 if (!lp->fd_buf) { 833 if (!lp->fd_buf) {
632 if (sizeof(struct FDesc) + 834 BUG_ON(sizeof(struct FDesc) +
633 sizeof(struct BDesc) * RX_BUF_PAGES + 835 sizeof(struct BDesc) * RX_BUF_NUM +
634 sizeof(struct FDesc) * RX_FD_NUM + 836 sizeof(struct FDesc) * RX_FD_NUM +
635 sizeof(struct TxFD) * TX_FD_NUM > PAGE_SIZE * FD_PAGE_NUM) { 837 sizeof(struct TxFD) * TX_FD_NUM >
636 printk(KERN_WARNING "%s: Invalid Queue Size.\n", dev->name); 838 PAGE_SIZE * FD_PAGE_NUM);
637 return -ENOMEM;
638 }
639 839
640 if ((lp->fd_buf = (void *)__get_free_pages(GFP_KERNEL, FD_PAGE_ORDER)) == 0) 840 if ((lp->fd_buf = pci_alloc_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM, &lp->fd_buf_dma)) == 0)
641 return -ENOMEM; 841 return -ENOMEM;
642 for (i = 0; i < RX_BUF_PAGES; i++) { 842 for (i = 0; i < RX_BUF_NUM; i++) {
643 if ((lp->data_buf[i] = (void *)get_zeroed_page(GFP_KERNEL)) == 0) { 843#ifdef TC35815_USE_PACKEDBUFFER
844 if ((lp->data_buf[i] = alloc_rxbuf_page(lp->pci_dev, &lp->data_buf_dma[i])) == NULL) {
644 while (--i >= 0) { 845 while (--i >= 0) {
645 free_page((unsigned long)lp->data_buf[i]); 846 free_rxbuf_page(lp->pci_dev,
646 lp->data_buf[i] = 0; 847 lp->data_buf[i],
848 lp->data_buf_dma[i]);
849 lp->data_buf[i] = NULL;
647 } 850 }
648 free_page((unsigned long)lp->fd_buf); 851 pci_free_consistent(lp->pci_dev,
649 lp->fd_buf = 0; 852 PAGE_SIZE * FD_PAGE_NUM,
853 lp->fd_buf,
854 lp->fd_buf_dma);
855 lp->fd_buf = NULL;
856 return -ENOMEM;
857 }
858#else
859 lp->rx_skbs[i].skb =
860 alloc_rxbuf_skb(dev, lp->pci_dev,
861 &lp->rx_skbs[i].skb_dma);
862 if (!lp->rx_skbs[i].skb) {
863 while (--i >= 0) {
864 free_rxbuf_skb(lp->pci_dev,
865 lp->rx_skbs[i].skb,
866 lp->rx_skbs[i].skb_dma);
867 lp->rx_skbs[i].skb = NULL;
868 }
869 pci_free_consistent(lp->pci_dev,
870 PAGE_SIZE * FD_PAGE_NUM,
871 lp->fd_buf,
872 lp->fd_buf_dma);
873 lp->fd_buf = NULL;
650 return -ENOMEM; 874 return -ENOMEM;
651 } 875 }
652#ifdef __mips__
653 dma_cache_wback_inv((unsigned long)lp->data_buf[i], PAGE_SIZE * FD_PAGE_NUM);
654#endif 876#endif
655 } 877 }
656#ifdef __mips__ 878 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
657 dma_cache_wback_inv((unsigned long)lp->fd_buf, PAGE_SIZE * FD_PAGE_NUM); 879 dev->name, lp->fd_buf);
880#ifdef TC35815_USE_PACKEDBUFFER
881 printk(" DataBuf");
882 for (i = 0; i < RX_BUF_NUM; i++)
883 printk(" %p", lp->data_buf[i]);
658#endif 884#endif
885 printk("\n");
659 } else { 886 } else {
660 memset(lp->fd_buf, 0, PAGE_SIZE * FD_PAGE_NUM); 887 for (i = 0; i < FD_PAGE_NUM; i++) {
661#ifdef __mips__ 888 clear_page((void *)((unsigned long)lp->fd_buf + i * PAGE_SIZE));
662 dma_cache_wback_inv((unsigned long)lp->fd_buf, PAGE_SIZE * FD_PAGE_NUM); 889 }
663#endif
664 } 890 }
665#ifdef __mips__
666 fd_addr = (unsigned long)vtonocache(lp->fd_buf);
667#else
668 fd_addr = (unsigned long)lp->fd_buf; 891 fd_addr = (unsigned long)lp->fd_buf;
669#endif
670 892
671 /* Free Descriptors (for Receive) */ 893 /* Free Descriptors (for Receive) */
672 lp->rfd_base = (struct RxFD *)fd_addr; 894 lp->rfd_base = (struct RxFD *)fd_addr;
@@ -675,34 +897,66 @@ tc35815_init_queues(struct net_device *dev)
675 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD); 897 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
676 } 898 }
677 lp->rfd_cur = lp->rfd_base; 899 lp->rfd_cur = lp->rfd_base;
678 lp->rfd_limit = (struct RxFD *)(fd_addr - 900 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
679 sizeof(struct FDesc) -
680 sizeof(struct BDesc) * 30);
681 901
682 /* Transmit Descriptors */ 902 /* Transmit Descriptors */
683 lp->tfd_base = (struct TxFD *)fd_addr; 903 lp->tfd_base = (struct TxFD *)fd_addr;
684 fd_addr += sizeof(struct TxFD) * TX_FD_NUM; 904 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
685 for (i = 0; i < TX_FD_NUM; i++) { 905 for (i = 0; i < TX_FD_NUM; i++) {
686 lp->tfd_base[i].fd.FDNext = cpu_to_le32(virt_to_bus(&lp->tfd_base[i+1])); 906 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
687 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0); 907 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
688 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0); 908 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
689 } 909 }
690 lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(virt_to_bus(&lp->tfd_base[0])); 910 lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0]));
691 lp->tfd_start = 0; 911 lp->tfd_start = 0;
692 lp->tfd_end = 0; 912 lp->tfd_end = 0;
693 913
694 /* Buffer List (for Receive) */ 914 /* Buffer List (for Receive) */
695 lp->fbl_ptr = (struct FrFD *)fd_addr; 915 lp->fbl_ptr = (struct FrFD *)fd_addr;
696 lp->fbl_ptr->fd.FDNext = cpu_to_le32(virt_to_bus(lp->fbl_ptr)); 916 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
697 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_PAGES | FD_CownsFD); 917 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
698 for (i = 0; i < RX_BUF_PAGES; i++) { 918#ifndef TC35815_USE_PACKEDBUFFER
699 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(virt_to_bus(lp->data_buf[i])); 919 /*
920 * move all allocated skbs to head of rx_skbs[] array.
921 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
922 * tc35815_rx() had failed.
923 */
924 lp->fbl_count = 0;
925 for (i = 0; i < RX_BUF_NUM; i++) {
926 if (lp->rx_skbs[i].skb) {
927 if (i != lp->fbl_count) {
928 lp->rx_skbs[lp->fbl_count].skb =
929 lp->rx_skbs[i].skb;
930 lp->rx_skbs[lp->fbl_count].skb_dma =
931 lp->rx_skbs[i].skb_dma;
932 }
933 lp->fbl_count++;
934 }
935 }
936#endif
937 for (i = 0; i < RX_BUF_NUM; i++) {
938#ifdef TC35815_USE_PACKEDBUFFER
939 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
940#else
941 if (i >= lp->fbl_count) {
942 lp->fbl_ptr->bd[i].BuffData = 0;
943 lp->fbl_ptr->bd[i].BDCtl = 0;
944 continue;
945 }
946 lp->fbl_ptr->bd[i].BuffData =
947 cpu_to_le32(lp->rx_skbs[i].skb_dma);
948#endif
700 /* BDID is index of FrFD.bd[] */ 949 /* BDID is index of FrFD.bd[] */
701 lp->fbl_ptr->bd[i].BDCtl = 950 lp->fbl_ptr->bd[i].BDCtl =
702 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) | PAGE_SIZE); 951 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
952 RX_BUF_SIZE);
703 } 953 }
954#ifdef TC35815_USE_PACKEDBUFFER
704 lp->fbl_curid = 0; 955 lp->fbl_curid = 0;
956#endif
705 957
958 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
959 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
706 return 0; 960 return 0;
707} 961}
708 962
@@ -713,11 +967,25 @@ tc35815_clear_queues(struct net_device *dev)
713 int i; 967 int i;
714 968
715 for (i = 0; i < TX_FD_NUM; i++) { 969 for (i = 0; i < TX_FD_NUM; i++) {
716 struct sk_buff *skb = (struct sk_buff *) 970 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
717 le32_to_cpu(lp->tfd_base[i].fd.FDSystem); 971 struct sk_buff *skb =
718 if (skb) 972 fdsystem != 0xffffffff ?
973 lp->tx_skbs[fdsystem].skb : NULL;
974#ifdef DEBUG
975 if (lp->tx_skbs[i].skb != skb) {
976 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
977 panic_queues(dev);
978 }
979#else
980 BUG_ON(lp->tx_skbs[i].skb != skb);
981#endif
982 if (skb) {
983 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
984 lp->tx_skbs[i].skb = NULL;
985 lp->tx_skbs[i].skb_dma = 0;
719 dev_kfree_skb_any(skb); 986 dev_kfree_skb_any(skb);
720 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0); 987 }
988 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
721 } 989 }
722 990
723 tc35815_init_queues(dev); 991 tc35815_init_queues(dev);
@@ -731,28 +999,53 @@ tc35815_free_queues(struct net_device *dev)
731 999
732 if (lp->tfd_base) { 1000 if (lp->tfd_base) {
733 for (i = 0; i < TX_FD_NUM; i++) { 1001 for (i = 0; i < TX_FD_NUM; i++) {
734 struct sk_buff *skb = (struct sk_buff *) 1002 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
735 le32_to_cpu(lp->tfd_base[i].fd.FDSystem); 1003 struct sk_buff *skb =
736 if (skb) 1004 fdsystem != 0xffffffff ?
737 dev_kfree_skb_any(skb); 1005 lp->tx_skbs[fdsystem].skb : NULL;
738 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0); 1006#ifdef DEBUG
1007 if (lp->tx_skbs[i].skb != skb) {
1008 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1009 panic_queues(dev);
1010 }
1011#else
1012 BUG_ON(lp->tx_skbs[i].skb != skb);
1013#endif
1014 if (skb) {
1015 dev_kfree_skb(skb);
1016 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1017 lp->tx_skbs[i].skb = NULL;
1018 lp->tx_skbs[i].skb_dma = 0;
1019 }
1020 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
739 } 1021 }
740 } 1022 }
741 1023
742 lp->rfd_base = NULL; 1024 lp->rfd_base = NULL;
743 lp->rfd_base = NULL;
744 lp->rfd_limit = NULL; 1025 lp->rfd_limit = NULL;
745 lp->rfd_cur = NULL; 1026 lp->rfd_cur = NULL;
746 lp->fbl_ptr = NULL; 1027 lp->fbl_ptr = NULL;
747 1028
748 for (i = 0; i < RX_BUF_PAGES; i++) { 1029 for (i = 0; i < RX_BUF_NUM; i++) {
749 if (lp->data_buf[i]) 1030#ifdef TC35815_USE_PACKEDBUFFER
750 free_page((unsigned long)lp->data_buf[i]); 1031 if (lp->data_buf[i]) {
751 lp->data_buf[i] = 0; 1032 free_rxbuf_page(lp->pci_dev,
1033 lp->data_buf[i], lp->data_buf_dma[i]);
1034 lp->data_buf[i] = NULL;
1035 }
1036#else
1037 if (lp->rx_skbs[i].skb) {
1038 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1039 lp->rx_skbs[i].skb_dma);
1040 lp->rx_skbs[i].skb = NULL;
1041 }
1042#endif
1043 }
1044 if (lp->fd_buf) {
1045 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1046 lp->fd_buf, lp->fd_buf_dma);
1047 lp->fd_buf = NULL;
752 } 1048 }
753 if (lp->fd_buf)
754 __free_pages(lp->fd_buf, FD_PAGE_ORDER);
755 lp->fd_buf = NULL;
756} 1049}
757 1050
758static void 1051static void
@@ -792,6 +1085,7 @@ dump_rxfd(struct RxFD *fd)
792 return bd_count; 1085 return bd_count;
793} 1086}
794 1087
1088#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
795static void 1089static void
796dump_frfd(struct FrFD *fd) 1090dump_frfd(struct FrFD *fd)
797{ 1091{
@@ -802,20 +1096,22 @@ dump_frfd(struct FrFD *fd)
802 le32_to_cpu(fd->fd.FDStat), 1096 le32_to_cpu(fd->fd.FDStat),
803 le32_to_cpu(fd->fd.FDCtl)); 1097 le32_to_cpu(fd->fd.FDCtl));
804 printk("BD: "); 1098 printk("BD: ");
805 for (i = 0; i < RX_BUF_PAGES; i++) 1099 for (i = 0; i < RX_BUF_NUM; i++)
806 printk(" %08x %08x", 1100 printk(" %08x %08x",
807 le32_to_cpu(fd->bd[i].BuffData), 1101 le32_to_cpu(fd->bd[i].BuffData),
808 le32_to_cpu(fd->bd[i].BDCtl)); 1102 le32_to_cpu(fd->bd[i].BDCtl));
809 printk("\n"); 1103 printk("\n");
810} 1104}
1105#endif
811 1106
1107#ifdef DEBUG
812static void 1108static void
813panic_queues(struct net_device *dev) 1109panic_queues(struct net_device *dev)
814{ 1110{
815 struct tc35815_local *lp = dev->priv; 1111 struct tc35815_local *lp = dev->priv;
816 int i; 1112 int i;
817 1113
818 printk("TxFD base %p, start %d, end %d\n", 1114 printk("TxFD base %p, start %u, end %u\n",
819 lp->tfd_base, lp->tfd_start, lp->tfd_end); 1115 lp->tfd_base, lp->tfd_start, lp->tfd_end);
820 printk("RxFD base %p limit %p cur %p\n", 1116 printk("RxFD base %p limit %p cur %p\n",
821 lp->rfd_base, lp->rfd_limit, lp->rfd_cur); 1117 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
@@ -829,31 +1125,13 @@ panic_queues(struct net_device *dev)
829 dump_frfd(lp->fbl_ptr); 1125 dump_frfd(lp->fbl_ptr);
830 panic("%s: Illegal queue state.", dev->name); 1126 panic("%s: Illegal queue state.", dev->name);
831} 1127}
832
833#if 0
834static void print_buf(char *add, int length)
835{
836 int i;
837 int len = length;
838
839 printk("print_buf(%08x)(%x)\n", (unsigned int) add,length);
840
841 if (len > 100)
842 len = 100;
843 for (i = 0; i < len; i++) {
844 printk(" %2.2X", (unsigned char) add[i]);
845 if (!(i % 16))
846 printk("\n");
847 }
848 printk("\n");
849}
850#endif 1128#endif
851 1129
852static void print_eth(char *add) 1130static void print_eth(char *add)
853{ 1131{
854 int i; 1132 int i;
855 1133
856 printk("print_eth(%08x)\n", (unsigned int) add); 1134 printk("print_eth(%p)\n", add);
857 for (i = 0; i < 6; i++) 1135 for (i = 0; i < 6; i++)
858 printk(" %2.2X", (unsigned char) add[i + 6]); 1136 printk(" %2.2X", (unsigned char) add[i + 6]);
859 printk(" =>"); 1137 printk(" =>");
@@ -862,6 +1140,73 @@ static void print_eth(char *add)
862 printk(" : %2.2X%2.2X\n", (unsigned char) add[12], (unsigned char) add[13]); 1140 printk(" : %2.2X%2.2X\n", (unsigned char) add[12], (unsigned char) add[13]);
863} 1141}
864 1142
1143static int tc35815_tx_full(struct net_device *dev)
1144{
1145 struct tc35815_local *lp = dev->priv;
1146 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1147}
1148
1149static void tc35815_restart(struct net_device *dev)
1150{
1151 struct tc35815_local *lp = dev->priv;
1152 int pid = lp->phy_addr;
1153 int do_phy_reset = 1;
1154 del_timer(&lp->timer); /* Kill if running */
1155
1156 if (lp->mii_id[0] == 0x0016 && (lp->mii_id[1] & 0xfc00) == 0xf800) {
1157 /* Resetting PHY cause problem on some chip... (SEEQ 80221) */
1158 do_phy_reset = 0;
1159 }
1160 if (do_phy_reset) {
1161 int timeout;
1162 tc_mdio_write(dev, pid, MII_BMCR, BMCR_RESET);
1163 timeout = 100;
1164 while (--timeout) {
1165 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_RESET))
1166 break;
1167 udelay(1);
1168 }
1169 if (!timeout)
1170 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1171 }
1172
1173 tc35815_chip_reset(dev);
1174 tc35815_clear_queues(dev);
1175 tc35815_chip_init(dev);
1176 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1177 tc35815_set_multicast_list(dev);
1178}
1179
1180static void tc35815_tx_timeout(struct net_device *dev)
1181{
1182 struct tc35815_local *lp = dev->priv;
1183 struct tc35815_regs __iomem *tr =
1184 (struct tc35815_regs __iomem *)dev->base_addr;
1185
1186 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1187 dev->name, tc_readl(&tr->Tx_Stat));
1188
1189 /* Try to restart the adaptor. */
1190 spin_lock_irq(&lp->lock);
1191 tc35815_restart(dev);
1192 spin_unlock_irq(&lp->lock);
1193
1194 lp->stats.tx_errors++;
1195
1196 /* If we have space available to accept new transmit
1197 * requests, wake up the queueing layer. This would
1198 * be the case if the chipset_init() call above just
1199 * flushes out the tx queue and empties it.
1200 *
1201 * If instead, the tx queue is retained then the
1202 * netif_wake_queue() call should be placed in the
1203 * TX completion interrupt handler of the driver instead
1204 * of here.
1205 */
1206 if (!tc35815_tx_full(dev))
1207 netif_wake_queue(dev);
1208}
1209
865/* 1210/*
866 * Open/initialize the board. This is called (in the current kernel) 1211 * Open/initialize the board. This is called (in the current kernel)
867 * sometime after booting when the 'ifconfig' program is run. 1212 * sometime after booting when the 'ifconfig' program is run.
@@ -874,16 +1219,16 @@ static int
874tc35815_open(struct net_device *dev) 1219tc35815_open(struct net_device *dev)
875{ 1220{
876 struct tc35815_local *lp = dev->priv; 1221 struct tc35815_local *lp = dev->priv;
1222
877 /* 1223 /*
878 * This is used if the interrupt line can turned off (shared). 1224 * This is used if the interrupt line can turned off (shared).
879 * See 3c503.c for an example of selecting the IRQ at config-time. 1225 * See 3c503.c for an example of selecting the IRQ at config-time.
880 */ 1226 */
881 1227 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, dev->name, dev)) {
882 if (dev->irq == 0 ||
883 request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, cardname, dev)) {
884 return -EAGAIN; 1228 return -EAGAIN;
885 } 1229 }
886 1230
1231 del_timer(&lp->timer); /* Kill if running */
887 tc35815_chip_reset(dev); 1232 tc35815_chip_reset(dev);
888 1233
889 if (tc35815_init_queues(dev) != 0) { 1234 if (tc35815_init_queues(dev) != 0) {
@@ -892,138 +1237,119 @@ tc35815_open(struct net_device *dev)
892 } 1237 }
893 1238
894 /* Reset the hardware here. Don't forget to set the station address. */ 1239 /* Reset the hardware here. Don't forget to set the station address. */
1240 spin_lock_irq(&lp->lock);
895 tc35815_chip_init(dev); 1241 tc35815_chip_init(dev);
1242 spin_unlock_irq(&lp->lock);
896 1243
897 lp->tbusy = 0; 1244 /* We are now ready to accept transmit requeusts from
1245 * the queueing layer of the networking.
1246 */
898 netif_start_queue(dev); 1247 netif_start_queue(dev);
899 1248
900 return 0; 1249 return 0;
901} 1250}
902 1251
903static void tc35815_tx_timeout(struct net_device *dev) 1252/* This will only be invoked if your driver is _not_ in XOFF state.
904{ 1253 * What this means is that you need not check it, and that this
905 struct tc35815_local *lp = dev->priv; 1254 * invariant will hold if you make sure that the netif_*_queue()
906 struct tc35815_regs *tr = (struct tc35815_regs *)dev->base_addr; 1255 * calls are done at the proper times.
907 unsigned long flags; 1256 */
908
909 spin_lock_irqsave(&lp->lock, flags);
910 printk(KERN_WARNING "%s: transmit timed out, status %#lx\n",
911 dev->name, tc_readl(&tr->Tx_Stat));
912 /* Try to restart the adaptor. */
913 tc35815_chip_reset(dev);
914 tc35815_clear_queues(dev);
915 tc35815_chip_init(dev);
916 lp->tbusy=0;
917 spin_unlock_irqrestore(&lp->lock, flags);
918 dev->trans_start = jiffies;
919 netif_wake_queue(dev);
920}
921
922static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev) 1257static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
923{ 1258{
924 struct tc35815_local *lp = dev->priv; 1259 struct tc35815_local *lp = dev->priv;
925 struct tc35815_regs *tr = (struct tc35815_regs *)dev->base_addr; 1260 struct TxFD *txfd;
926 1261 unsigned long flags;
927 if (netif_queue_stopped(dev)) {
928 /*
929 * If we get here, some higher level has decided we are broken.
930 * There should really be a "kick me" function call instead.
931 */
932 int tickssofar = jiffies - dev->trans_start;
933 if (tickssofar < 5)
934 return 1;
935 printk(KERN_WARNING "%s: transmit timed out, status %#lx\n",
936 dev->name, tc_readl(&tr->Tx_Stat));
937 /* Try to restart the adaptor. */
938 tc35815_chip_reset(dev);
939 tc35815_clear_queues(dev);
940 tc35815_chip_init(dev);
941 lp->tbusy=0;
942 dev->trans_start = jiffies;
943 netif_wake_queue(dev);
944 }
945 1262
946 /* 1263 /* If some error occurs while trying to transmit this
947 * Block a timer-based transmit from overlapping. This could better be 1264 * packet, you should return '1' from this function.
948 * done with atomic_swap(1, lp->tbusy), but set_bit() works as well. 1265 * In such a case you _may not_ do anything to the
1266 * SKB, it is still owned by the network queueing
1267 * layer when an error is returned. This means you
1268 * may not modify any SKB fields, you may not free
1269 * the SKB, etc.
949 */ 1270 */
950 if (test_and_set_bit(0, (void*)&lp->tbusy) != 0) {
951 printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name);
952 dev_kfree_skb_any(skb);
953 } else {
954 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
955 unsigned char *buf = skb->data;
956 struct TxFD *txfd = &lp->tfd_base[lp->tfd_start];
957 unsigned long flags;
958 lp->stats.tx_bytes += skb->len;
959 1271
1272 /* This is the most common case for modern hardware.
1273 * The spinlock protects this code from the TX complete
1274 * hardware interrupt handler. Queue flow control is
1275 * thus managed under this lock as well.
1276 */
1277 spin_lock_irqsave(&lp->lock, flags);
960 1278
961#ifdef __mips__ 1279 /* failsafe... (handle txdone now if half of FDs are used) */
962 dma_cache_wback_inv((unsigned long)buf, length); 1280 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1281 TX_FD_NUM / 2)
1282 tc35815_txdone(dev);
1283
1284 if (netif_msg_pktdata(lp))
1285 print_eth(skb->data);
1286#ifdef DEBUG
1287 if (lp->tx_skbs[lp->tfd_start].skb) {
1288 printk("%s: tx_skbs conflict.\n", dev->name);
1289 panic_queues(dev);
1290 }
1291#else
1292 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
963#endif 1293#endif
964 1294 lp->tx_skbs[lp->tfd_start].skb = skb;
965 spin_lock_irqsave(&lp->lock, flags); 1295 lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
966 1296
967 /* failsafe... */ 1297 /*add to ring */
968 if (lp->tfd_start != lp->tfd_end) 1298 txfd = &lp->tfd_base[lp->tfd_start];
969 tc35815_txdone(dev); 1299 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
970 1300 txfd->bd.BDCtl = cpu_to_le32(skb->len);
971 1301 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
972 txfd->bd.BuffData = cpu_to_le32(virt_to_bus(buf)); 1302 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
973 1303
974 txfd->bd.BDCtl = cpu_to_le32(length); 1304 if (lp->tfd_start == lp->tfd_end) {
975 txfd->fd.FDSystem = cpu_to_le32((__u32)skb); 1305 struct tc35815_regs __iomem *tr =
976 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT)); 1306 (struct tc35815_regs __iomem *)dev->base_addr;
977 1307 /* Start DMA Transmitter. */
978 if (lp->tfd_start == lp->tfd_end) { 1308 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
979 /* Start DMA Transmitter. */
980 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
981#ifdef GATHER_TXINT 1309#ifdef GATHER_TXINT
982 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx); 1310 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
983#endif 1311#endif
984 if (tc35815_debug > 2) { 1312 if (netif_msg_tx_queued(lp)) {
985 printk("%s: starting TxFD.\n", dev->name); 1313 printk("%s: starting TxFD.\n", dev->name);
986 dump_txfd(txfd); 1314 dump_txfd(txfd);
987 if (tc35815_debug > 3) 1315 }
988 print_eth(buf); 1316 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
989 } 1317 } else {
990 tc_writel(virt_to_bus(txfd), &tr->TxFrmPtr); 1318 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
991 } else { 1319 if (netif_msg_tx_queued(lp)) {
992 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL); 1320 printk("%s: queueing TxFD.\n", dev->name);
993 if (tc35815_debug > 2) { 1321 dump_txfd(txfd);
994 printk("%s: queueing TxFD.\n", dev->name);
995 dump_txfd(txfd);
996 if (tc35815_debug > 3)
997 print_eth(buf);
998 }
999 } 1322 }
1000 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM; 1323 }
1324 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1001 1325
1002 dev->trans_start = jiffies; 1326 dev->trans_start = jiffies;
1003 1327
1004 if ((lp->tfd_start + 1) % TX_FD_NUM != lp->tfd_end) { 1328 /* If we just used up the very last entry in the
1005 /* we can send another packet */ 1329 * TX ring on this device, tell the queueing
1006 lp->tbusy = 0; 1330 * layer to send no more.
1007 netif_start_queue(dev); 1331 */
1008 } else { 1332 if (tc35815_tx_full(dev)) {
1009 netif_stop_queue(dev); 1333 if (netif_msg_tx_queued(lp))
1010 if (tc35815_debug > 1) 1334 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1011 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name); 1335 netif_stop_queue(dev);
1012 }
1013 spin_unlock_irqrestore(&lp->lock, flags);
1014 } 1336 }
1015 1337
1338 /* When the TX completion hw interrupt arrives, this
1339 * is when the transmit statistics are updated.
1340 */
1341
1342 spin_unlock_irqrestore(&lp->lock, flags);
1016 return 0; 1343 return 0;
1017} 1344}
1018 1345
1019#define FATAL_ERROR_INT \ 1346#define FATAL_ERROR_INT \
1020 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt) 1347 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
1021static void tc35815_fatal_error_interrupt(struct net_device *dev, int status) 1348static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
1022{ 1349{
1023 static int count; 1350 static int count;
1024 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):", 1351 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1025 dev->name, status); 1352 dev->name, status);
1026
1027 if (status & Int_IntPCI) 1353 if (status & Int_IntPCI)
1028 printk(" IntPCI"); 1354 printk(" IntPCI");
1029 if (status & Int_DmParErr) 1355 if (status & Int_DmParErr)
@@ -1033,110 +1359,170 @@ static void tc35815_fatal_error_interrupt(struct net_device *dev, int status)
1033 printk("\n"); 1359 printk("\n");
1034 if (count++ > 100) 1360 if (count++ > 100)
1035 panic("%s: Too many fatal errors.", dev->name); 1361 panic("%s: Too many fatal errors.", dev->name);
1036 printk(KERN_WARNING "%s: Resetting %s...\n", dev->name, cardname); 1362 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
1037 /* Try to restart the adaptor. */ 1363 /* Try to restart the adaptor. */
1038 tc35815_chip_reset(dev); 1364 tc35815_restart(dev);
1039 tc35815_clear_queues(dev); 1365}
1040 tc35815_chip_init(dev); 1366
1367#ifdef TC35815_NAPI
1368static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
1369#else
1370static int tc35815_do_interrupt(struct net_device *dev, u32 status)
1371#endif
1372{
1373 struct tc35815_local *lp = dev->priv;
1374 struct tc35815_regs __iomem *tr =
1375 (struct tc35815_regs __iomem *)dev->base_addr;
1376 int ret = -1;
1377
1378 /* Fatal errors... */
1379 if (status & FATAL_ERROR_INT) {
1380 tc35815_fatal_error_interrupt(dev, status);
1381 return 0;
1382 }
1383 /* recoverable errors */
1384 if (status & Int_IntFDAEx) {
1385 /* disable FDAEx int. (until we make rooms...) */
1386 tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
1387 printk(KERN_WARNING
1388 "%s: Free Descriptor Area Exhausted (%#x).\n",
1389 dev->name, status);
1390 lp->stats.rx_dropped++;
1391 ret = 0;
1392 }
1393 if (status & Int_IntBLEx) {
1394 /* disable BLEx int. (until we make rooms...) */
1395 tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
1396 printk(KERN_WARNING
1397 "%s: Buffer List Exhausted (%#x).\n",
1398 dev->name, status);
1399 lp->stats.rx_dropped++;
1400 ret = 0;
1401 }
1402 if (status & Int_IntExBD) {
1403 printk(KERN_WARNING
1404 "%s: Excessive Buffer Descriptiors (%#x).\n",
1405 dev->name, status);
1406 lp->stats.rx_length_errors++;
1407 ret = 0;
1408 }
1409
1410 /* normal notification */
1411 if (status & Int_IntMacRx) {
1412 /* Got a packet(s). */
1413#ifdef TC35815_NAPI
1414 ret = tc35815_rx(dev, limit);
1415#else
1416 tc35815_rx(dev);
1417 ret = 0;
1418#endif
1419 lp->lstats.rx_ints++;
1420 }
1421 if (status & Int_IntMacTx) {
1422 /* Transmit complete. */
1423 lp->lstats.tx_ints++;
1424 tc35815_txdone(dev);
1425 netif_wake_queue(dev);
1426 ret = 0;
1427 }
1428 return ret;
1041} 1429}
1042 1430
1043/* 1431/*
1044 * The typical workload of the driver: 1432 * The typical workload of the driver:
1045 * Handle the network interface interrupts. 1433 * Handle the network interface interrupts.
1046 */ 1434 */
1047static irqreturn_t tc35815_interrupt(int irq, void *dev_id) 1435static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
1048{ 1436{
1049 struct net_device *dev = dev_id; 1437 struct net_device *dev = dev_id;
1050 struct tc35815_regs *tr; 1438 struct tc35815_regs __iomem *tr =
1051 struct tc35815_local *lp; 1439 (struct tc35815_regs __iomem *)dev->base_addr;
1052 int status, boguscount = 0; 1440#ifdef TC35815_NAPI
1053 int handled = 0; 1441 u32 dmactl = tc_readl(&tr->DMA_Ctl);
1054 1442
1055 if (dev == NULL) { 1443 if (!(dmactl & DMA_IntMask)) {
1056 printk(KERN_WARNING "%s: irq %d for unknown device.\n", cardname, irq); 1444 /* disable interrupts */
1057 return IRQ_NONE; 1445 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
1058 } 1446 if (netif_rx_schedule_prep(dev))
1059 1447 __netif_rx_schedule(dev);
1060 tr = (struct tc35815_regs*)dev->base_addr; 1448 else {
1061 lp = dev->priv; 1449 printk(KERN_ERR "%s: interrupt taken in poll\n",
1062 1450 dev->name);
1063 do { 1451 BUG();
1064 status = tc_readl(&tr->Int_Src);
1065 if (status == 0)
1066 break;
1067 handled = 1;
1068 tc_writel(status, &tr->Int_Src); /* write to clear */
1069
1070 /* Fatal errors... */
1071 if (status & FATAL_ERROR_INT) {
1072 tc35815_fatal_error_interrupt(dev, status);
1073 break;
1074 }
1075 /* recoverable errors */
1076 if (status & Int_IntFDAEx) {
1077 /* disable FDAEx int. (until we make rooms...) */
1078 tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
1079 printk(KERN_WARNING
1080 "%s: Free Descriptor Area Exhausted (%#x).\n",
1081 dev->name, status);
1082 lp->stats.rx_dropped++;
1083 }
1084 if (status & Int_IntBLEx) {
1085 /* disable BLEx int. (until we make rooms...) */
1086 tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
1087 printk(KERN_WARNING
1088 "%s: Buffer List Exhausted (%#x).\n",
1089 dev->name, status);
1090 lp->stats.rx_dropped++;
1091 }
1092 if (status & Int_IntExBD) {
1093 printk(KERN_WARNING
1094 "%s: Excessive Buffer Descriptiors (%#x).\n",
1095 dev->name, status);
1096 lp->stats.rx_length_errors++;
1097 }
1098 /* normal notification */
1099 if (status & Int_IntMacRx) {
1100 /* Got a packet(s). */
1101 lp->lstats.rx_ints++;
1102 tc35815_rx(dev);
1103 } 1452 }
1104 if (status & Int_IntMacTx) { 1453 (void)tc_readl(&tr->Int_Src); /* flush */
1105 lp->lstats.tx_ints++; 1454 return IRQ_HANDLED;
1106 tc35815_txdone(dev); 1455 }
1107 } 1456 return IRQ_NONE;
1108 } while (++boguscount < 20) ; 1457#else
1458 struct tc35815_local *lp = dev->priv;
1459 int handled;
1460 u32 status;
1461
1462 spin_lock(&lp->lock);
1463 status = tc_readl(&tr->Int_Src);
1464 tc_writel(status, &tr->Int_Src); /* write to clear */
1465 handled = tc35815_do_interrupt(dev, status);
1466 (void)tc_readl(&tr->Int_Src); /* flush */
1467 spin_unlock(&lp->lock);
1468 return IRQ_RETVAL(handled >= 0);
1469#endif /* TC35815_NAPI */
1470}
1109 1471
1110 return IRQ_RETVAL(handled); 1472#ifdef CONFIG_NET_POLL_CONTROLLER
1473static void tc35815_poll_controller(struct net_device *dev)
1474{
1475 disable_irq(dev->irq);
1476 tc35815_interrupt(dev->irq, dev);
1477 enable_irq(dev->irq);
1111} 1478}
1479#endif
1112 1480
1113/* We have a good packet(s), get it/them out of the buffers. */ 1481/* We have a good packet(s), get it/them out of the buffers. */
1482#ifdef TC35815_NAPI
1483static int
1484tc35815_rx(struct net_device *dev, int limit)
1485#else
1114static void 1486static void
1115tc35815_rx(struct net_device *dev) 1487tc35815_rx(struct net_device *dev)
1488#endif
1116{ 1489{
1117 struct tc35815_local *lp = dev->priv; 1490 struct tc35815_local *lp = dev->priv;
1118 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr;
1119 unsigned int fdctl; 1491 unsigned int fdctl;
1120 int i; 1492 int i;
1121 int buf_free_count = 0; 1493 int buf_free_count = 0;
1122 int fd_free_count = 0; 1494 int fd_free_count = 0;
1495#ifdef TC35815_NAPI
1496 int received = 0;
1497#endif
1123 1498
1124 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) { 1499 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1125 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat); 1500 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1126 int pkt_len = fdctl & FD_FDLength_MASK; 1501 int pkt_len = fdctl & FD_FDLength_MASK;
1127 struct RxFD *next_rfd;
1128 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT; 1502 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1503#ifdef DEBUG
1504 struct RxFD *next_rfd;
1505#endif
1506#if (RX_CTL_CMD & Rx_StripCRC) == 0
1507 pkt_len -= 4;
1508#endif
1129 1509
1130 if (tc35815_debug > 2) 1510 if (netif_msg_rx_status(lp))
1131 dump_rxfd(lp->rfd_cur); 1511 dump_rxfd(lp->rfd_cur);
1132 if (status & Rx_Good) { 1512 if (status & Rx_Good) {
1133 /* Malloc up new buffer. */
1134 struct sk_buff *skb; 1513 struct sk_buff *skb;
1135 unsigned char *data; 1514 unsigned char *data;
1136 int cur_bd, offset; 1515 int cur_bd;
1137 1516#ifdef TC35815_USE_PACKEDBUFFER
1138 lp->stats.rx_bytes += pkt_len; 1517 int offset;
1518#endif
1139 1519
1520#ifdef TC35815_NAPI
1521 if (--limit < 0)
1522 break;
1523#endif
1524#ifdef TC35815_USE_PACKEDBUFFER
1525 BUG_ON(bd_count > 2);
1140 skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */ 1526 skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */
1141 if (skb == NULL) { 1527 if (skb == NULL) {
1142 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", 1528 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
@@ -1154,25 +1540,64 @@ tc35815_rx(struct net_device *dev)
1154 while (offset < pkt_len && cur_bd < bd_count) { 1540 while (offset < pkt_len && cur_bd < bd_count) {
1155 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) & 1541 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1156 BD_BuffLength_MASK; 1542 BD_BuffLength_MASK;
1157 void *rxbuf = 1543 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1158 bus_to_virt(le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData)); 1544 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1159#ifdef __mips__ 1545 if (offset + len > pkt_len)
1160 dma_cache_inv((unsigned long)rxbuf, len); 1546 len = pkt_len - offset;
1547#ifdef TC35815_DMA_SYNC_ONDEMAND
1548 pci_dma_sync_single_for_cpu(lp->pci_dev,
1549 dma, len,
1550 PCI_DMA_FROMDEVICE);
1161#endif 1551#endif
1162 memcpy(data + offset, rxbuf, len); 1552 memcpy(data + offset, rxbuf, len);
1163 offset += len; 1553 offset += len;
1164 cur_bd++; 1554 cur_bd++;
1165 } 1555 }
1166#if 0 1556#else /* TC35815_USE_PACKEDBUFFER */
1167 print_buf(data,pkt_len); 1557 BUG_ON(bd_count > 1);
1558 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1559 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1560#ifdef DEBUG
1561 if (cur_bd >= RX_BUF_NUM) {
1562 printk("%s: invalid BDID.\n", dev->name);
1563 panic_queues(dev);
1564 }
1565 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1566 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1567 if (!lp->rx_skbs[cur_bd].skb) {
1568 printk("%s: NULL skb.\n", dev->name);
1569 panic_queues(dev);
1570 }
1571#else
1572 BUG_ON(cur_bd >= RX_BUF_NUM);
1168#endif 1573#endif
1169 if (tc35815_debug > 3) 1574 skb = lp->rx_skbs[cur_bd].skb;
1575 prefetch(skb->data);
1576 lp->rx_skbs[cur_bd].skb = NULL;
1577 lp->fbl_count--;
1578 pci_unmap_single(lp->pci_dev,
1579 lp->rx_skbs[cur_bd].skb_dma,
1580 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1581 if (!HAVE_DMA_RXALIGN(lp))
1582 memmove(skb->data, skb->data - 2, pkt_len);
1583 data = skb_put(skb, pkt_len);
1584#endif /* TC35815_USE_PACKEDBUFFER */
1585 if (netif_msg_pktdata(lp))
1170 print_eth(data); 1586 print_eth(data);
1171 skb->protocol = eth_type_trans(skb, dev); 1587 skb->protocol = eth_type_trans(skb, dev);
1588#ifdef TC35815_NAPI
1589 netif_receive_skb(skb);
1590 received++;
1591#else
1172 netif_rx(skb); 1592 netif_rx(skb);
1593#endif
1594 dev->last_rx = jiffies;
1173 lp->stats.rx_packets++; 1595 lp->stats.rx_packets++;
1596 lp->stats.rx_bytes += pkt_len;
1174 } else { 1597 } else {
1175 lp->stats.rx_errors++; 1598 lp->stats.rx_errors++;
1599 printk(KERN_DEBUG "%s: Rx error (status %x)\n",
1600 dev->name, status & Rx_Stat_Mask);
1176 /* WORKAROUND: LongErr and CRCErr means Overflow. */ 1601 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1177 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) { 1602 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1178 status &= ~(Rx_LongErr|Rx_CRCErr); 1603 status &= ~(Rx_LongErr|Rx_CRCErr);
@@ -1189,63 +1614,150 @@ tc35815_rx(struct net_device *dev)
1189 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl); 1614 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1190 unsigned char id = 1615 unsigned char id =
1191 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT; 1616 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1192 if (id >= RX_BUF_PAGES) { 1617#ifdef DEBUG
1618 if (id >= RX_BUF_NUM) {
1193 printk("%s: invalid BDID.\n", dev->name); 1619 printk("%s: invalid BDID.\n", dev->name);
1194 panic_queues(dev); 1620 panic_queues(dev);
1195 } 1621 }
1622#else
1623 BUG_ON(id >= RX_BUF_NUM);
1624#endif
1196 /* free old buffers */ 1625 /* free old buffers */
1197 while (lp->fbl_curid != id) { 1626#ifdef TC35815_USE_PACKEDBUFFER
1198 bdctl = le32_to_cpu(lp->fbl_ptr->bd[lp->fbl_curid].BDCtl); 1627 while (lp->fbl_curid != id)
1628#else
1629 while (lp->fbl_count < RX_BUF_NUM)
1630#endif
1631 {
1632#ifdef TC35815_USE_PACKEDBUFFER
1633 unsigned char curid = lp->fbl_curid;
1634#else
1635 unsigned char curid =
1636 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1637#endif
1638 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1639#ifdef DEBUG
1640 bdctl = le32_to_cpu(bd->BDCtl);
1199 if (bdctl & BD_CownsBD) { 1641 if (bdctl & BD_CownsBD) {
1200 printk("%s: Freeing invalid BD.\n", 1642 printk("%s: Freeing invalid BD.\n",
1201 dev->name); 1643 dev->name);
1202 panic_queues(dev); 1644 panic_queues(dev);
1203 } 1645 }
1646#endif
1204 /* pass BD to controler */ 1647 /* pass BD to controler */
1648#ifndef TC35815_USE_PACKEDBUFFER
1649 if (!lp->rx_skbs[curid].skb) {
1650 lp->rx_skbs[curid].skb =
1651 alloc_rxbuf_skb(dev,
1652 lp->pci_dev,
1653 &lp->rx_skbs[curid].skb_dma);
1654 if (!lp->rx_skbs[curid].skb)
1655 break; /* try on next reception */
1656 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1657 }
1658#endif /* TC35815_USE_PACKEDBUFFER */
1205 /* Note: BDLength was modified by chip. */ 1659 /* Note: BDLength was modified by chip. */
1206 lp->fbl_ptr->bd[lp->fbl_curid].BDCtl = 1660 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1207 cpu_to_le32(BD_CownsBD | 1661 (curid << BD_RxBDID_SHIFT) |
1208 (lp->fbl_curid << BD_RxBDID_SHIFT) | 1662 RX_BUF_SIZE);
1209 PAGE_SIZE); 1663#ifdef TC35815_USE_PACKEDBUFFER
1210 lp->fbl_curid = 1664 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1211 (lp->fbl_curid + 1) % RX_BUF_PAGES; 1665 if (netif_msg_rx_status(lp)) {
1212 if (tc35815_debug > 2) {
1213 printk("%s: Entering new FBD %d\n", 1666 printk("%s: Entering new FBD %d\n",
1214 dev->name, lp->fbl_curid); 1667 dev->name, lp->fbl_curid);
1215 dump_frfd(lp->fbl_ptr); 1668 dump_frfd(lp->fbl_ptr);
1216 } 1669 }
1670#else
1671 lp->fbl_count++;
1672#endif
1217 buf_free_count++; 1673 buf_free_count++;
1218 } 1674 }
1219 } 1675 }
1220 1676
1221 /* put RxFD back to controller */ 1677 /* put RxFD back to controller */
1222 next_rfd = bus_to_virt(le32_to_cpu(lp->rfd_cur->fd.FDNext)); 1678#ifdef DEBUG
1223#ifdef __mips__ 1679 next_rfd = fd_bus_to_virt(lp,
1224 next_rfd = (struct RxFD *)vtonocache(next_rfd); 1680 le32_to_cpu(lp->rfd_cur->fd.FDNext));
1225#endif
1226 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) { 1681 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1227 printk("%s: RxFD FDNext invalid.\n", dev->name); 1682 printk("%s: RxFD FDNext invalid.\n", dev->name);
1228 panic_queues(dev); 1683 panic_queues(dev);
1229 } 1684 }
1685#endif
1230 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) { 1686 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
1231 /* pass FD to controler */ 1687 /* pass FD to controler */
1232 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead); /* for debug */ 1688#ifdef DEBUG
1689 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1690#else
1691 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1692#endif
1233 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD); 1693 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1234 lp->rfd_cur++; 1694 lp->rfd_cur++;
1235 fd_free_count++; 1695 fd_free_count++;
1236 } 1696 }
1237 1697 if (lp->rfd_cur > lp->rfd_limit)
1238 lp->rfd_cur = next_rfd; 1698 lp->rfd_cur = lp->rfd_base;
1699#ifdef DEBUG
1700 if (lp->rfd_cur != next_rfd)
1701 printk("rfd_cur = %p, next_rfd %p\n",
1702 lp->rfd_cur, next_rfd);
1703#endif
1239 } 1704 }
1240 1705
1241 /* re-enable BL/FDA Exhaust interrupts. */ 1706 /* re-enable BL/FDA Exhaust interrupts. */
1242 if (fd_free_count) { 1707 if (fd_free_count) {
1243 tc_writel(tc_readl(&tr->Int_En) | Int_FDAExEn, &tr->Int_En); 1708 struct tc35815_regs __iomem *tr =
1709 (struct tc35815_regs __iomem *)dev->base_addr;
1710 u32 en, en_old = tc_readl(&tr->Int_En);
1711 en = en_old | Int_FDAExEn;
1244 if (buf_free_count) 1712 if (buf_free_count)
1245 tc_writel(tc_readl(&tr->Int_En) | Int_BLExEn, &tr->Int_En); 1713 en |= Int_BLExEn;
1714 if (en != en_old)
1715 tc_writel(en, &tr->Int_En);
1246 } 1716 }
1717#ifdef TC35815_NAPI
1718 return received;
1719#endif
1247} 1720}
1248 1721
1722#ifdef TC35815_NAPI
1723static int
1724tc35815_poll(struct net_device *dev, int *budget)
1725{
1726 struct tc35815_local *lp = dev->priv;
1727 struct tc35815_regs __iomem *tr =
1728 (struct tc35815_regs __iomem *)dev->base_addr;
1729 int limit = min(*budget, dev->quota);
1730 int received = 0, handled;
1731 u32 status;
1732
1733 spin_lock(&lp->lock);
1734 status = tc_readl(&tr->Int_Src);
1735 do {
1736 tc_writel(status, &tr->Int_Src); /* write to clear */
1737
1738 handled = tc35815_do_interrupt(dev, status, limit);
1739 if (handled >= 0) {
1740 received += handled;
1741 limit -= handled;
1742 if (limit <= 0)
1743 break;
1744 }
1745 status = tc_readl(&tr->Int_Src);
1746 } while (status);
1747 spin_unlock(&lp->lock);
1748
1749 dev->quota -= received;
1750 *budget -= received;
1751 if (limit <= 0)
1752 return 1;
1753
1754 netif_rx_complete(dev);
1755 /* enable interrupts */
1756 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1757 return 0;
1758}
1759#endif
1760
1249#ifdef NO_CHECK_CARRIER 1761#ifdef NO_CHECK_CARRIER
1250#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr) 1762#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1251#else 1763#else
@@ -1264,9 +1776,17 @@ tc35815_check_tx_stat(struct net_device *dev, int status)
1264 if (status & Tx_TxColl_MASK) 1776 if (status & Tx_TxColl_MASK)
1265 lp->stats.collisions += status & Tx_TxColl_MASK; 1777 lp->stats.collisions += status & Tx_TxColl_MASK;
1266 1778
1779#ifndef NO_CHECK_CARRIER
1780 /* TX4939 does not have NCarr */
1781 if (lp->boardtype == TC35815_TX4939)
1782 status &= ~Tx_NCarr;
1783#ifdef WORKAROUND_LOSTCAR
1267 /* WORKAROUND: ignore LostCrS in full duplex operation */ 1784 /* WORKAROUND: ignore LostCrS in full duplex operation */
1268 if (lp->fullduplex) 1785 if ((lp->timer_state != asleep && lp->timer_state != lcheck)
1786 || lp->fullduplex)
1269 status &= ~Tx_NCarr; 1787 status &= ~Tx_NCarr;
1788#endif
1789#endif
1270 1790
1271 if (!(status & TX_STA_ERR)) { 1791 if (!(status & TX_STA_ERR)) {
1272 /* no error. */ 1792 /* no error. */
@@ -1282,6 +1802,15 @@ tc35815_check_tx_stat(struct net_device *dev, int status)
1282 if (status & Tx_Under) { 1802 if (status & Tx_Under) {
1283 lp->stats.tx_fifo_errors++; 1803 lp->stats.tx_fifo_errors++;
1284 msg = "Tx FIFO Underrun."; 1804 msg = "Tx FIFO Underrun.";
1805 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1806 lp->lstats.tx_underrun++;
1807 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1808 struct tc35815_regs __iomem *tr =
1809 (struct tc35815_regs __iomem *)dev->base_addr;
1810 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1811 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1812 }
1813 }
1285 } 1814 }
1286 if (status & Tx_Defer) { 1815 if (status & Tx_Defer) {
1287 lp->stats.tx_fifo_errors++; 1816 lp->stats.tx_fifo_errors++;
@@ -1305,18 +1834,19 @@ tc35815_check_tx_stat(struct net_device *dev, int status)
1305 lp->stats.tx_heartbeat_errors++; 1834 lp->stats.tx_heartbeat_errors++;
1306 msg = "Signal Quality Error."; 1835 msg = "Signal Quality Error.";
1307 } 1836 }
1308 if (msg) 1837 if (msg && netif_msg_tx_err(lp))
1309 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status); 1838 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
1310} 1839}
1311 1840
1841/* This handles TX complete events posted by the device
1842 * via interrupts.
1843 */
1312static void 1844static void
1313tc35815_txdone(struct net_device *dev) 1845tc35815_txdone(struct net_device *dev)
1314{ 1846{
1315 struct tc35815_local *lp = dev->priv; 1847 struct tc35815_local *lp = dev->priv;
1316 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr;
1317 struct TxFD *txfd; 1848 struct TxFD *txfd;
1318 unsigned int fdctl; 1849 unsigned int fdctl;
1319 int num_done = 0;
1320 1850
1321 txfd = &lp->tfd_base[lp->tfd_end]; 1851 txfd = &lp->tfd_base[lp->tfd_end];
1322 while (lp->tfd_start != lp->tfd_end && 1852 while (lp->tfd_start != lp->tfd_end &&
@@ -1324,38 +1854,61 @@ tc35815_txdone(struct net_device *dev)
1324 int status = le32_to_cpu(txfd->fd.FDStat); 1854 int status = le32_to_cpu(txfd->fd.FDStat);
1325 struct sk_buff *skb; 1855 struct sk_buff *skb;
1326 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext); 1856 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
1857 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
1327 1858
1328 if (tc35815_debug > 2) { 1859 if (netif_msg_tx_done(lp)) {
1329 printk("%s: complete TxFD.\n", dev->name); 1860 printk("%s: complete TxFD.\n", dev->name);
1330 dump_txfd(txfd); 1861 dump_txfd(txfd);
1331 } 1862 }
1332 tc35815_check_tx_stat(dev, status); 1863 tc35815_check_tx_stat(dev, status);
1333 1864
1334 skb = (struct sk_buff *)le32_to_cpu(txfd->fd.FDSystem); 1865 skb = fdsystem != 0xffffffff ?
1866 lp->tx_skbs[fdsystem].skb : NULL;
1867#ifdef DEBUG
1868 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
1869 printk("%s: tx_skbs mismatch.\n", dev->name);
1870 panic_queues(dev);
1871 }
1872#else
1873 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
1874#endif
1335 if (skb) { 1875 if (skb) {
1876 lp->stats.tx_bytes += skb->len;
1877 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
1878 lp->tx_skbs[lp->tfd_end].skb = NULL;
1879 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
1880#ifdef TC35815_NAPI
1336 dev_kfree_skb_any(skb); 1881 dev_kfree_skb_any(skb);
1882#else
1883 dev_kfree_skb_irq(skb);
1884#endif
1337 } 1885 }
1338 txfd->fd.FDSystem = cpu_to_le32(0); 1886 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
1339 1887
1340 num_done++;
1341 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM; 1888 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
1342 txfd = &lp->tfd_base[lp->tfd_end]; 1889 txfd = &lp->tfd_base[lp->tfd_end];
1343 if ((fdnext & ~FD_Next_EOL) != virt_to_bus(txfd)) { 1890#ifdef DEBUG
1891 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
1344 printk("%s: TxFD FDNext invalid.\n", dev->name); 1892 printk("%s: TxFD FDNext invalid.\n", dev->name);
1345 panic_queues(dev); 1893 panic_queues(dev);
1346 } 1894 }
1895#endif
1347 if (fdnext & FD_Next_EOL) { 1896 if (fdnext & FD_Next_EOL) {
1348 /* DMA Transmitter has been stopping... */ 1897 /* DMA Transmitter has been stopping... */
1349 if (lp->tfd_end != lp->tfd_start) { 1898 if (lp->tfd_end != lp->tfd_start) {
1899 struct tc35815_regs __iomem *tr =
1900 (struct tc35815_regs __iomem *)dev->base_addr;
1350 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM; 1901 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
1351 struct TxFD* txhead = &lp->tfd_base[head]; 1902 struct TxFD* txhead = &lp->tfd_base[head];
1352 int qlen = (lp->tfd_start + TX_FD_NUM 1903 int qlen = (lp->tfd_start + TX_FD_NUM
1353 - lp->tfd_end) % TX_FD_NUM; 1904 - lp->tfd_end) % TX_FD_NUM;
1354 1905
1906#ifdef DEBUG
1355 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) { 1907 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
1356 printk("%s: TxFD FDCtl invalid.\n", dev->name); 1908 printk("%s: TxFD FDCtl invalid.\n", dev->name);
1357 panic_queues(dev); 1909 panic_queues(dev);
1358 } 1910 }
1911#endif
1359 /* log max queue length */ 1912 /* log max queue length */
1360 if (lp->lstats.max_tx_qlen < qlen) 1913 if (lp->lstats.max_tx_qlen < qlen)
1361 lp->lstats.max_tx_qlen = qlen; 1914 lp->lstats.max_tx_qlen = qlen;
@@ -1366,21 +1919,23 @@ tc35815_txdone(struct net_device *dev)
1366#ifdef GATHER_TXINT 1919#ifdef GATHER_TXINT
1367 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx); 1920 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
1368#endif 1921#endif
1369 if (tc35815_debug > 2) { 1922 if (netif_msg_tx_queued(lp)) {
1370 printk("%s: start TxFD on queue.\n", 1923 printk("%s: start TxFD on queue.\n",
1371 dev->name); 1924 dev->name);
1372 dump_txfd(txfd); 1925 dump_txfd(txfd);
1373 } 1926 }
1374 tc_writel(virt_to_bus(txfd), &tr->TxFrmPtr); 1927 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1375 } 1928 }
1376 break; 1929 break;
1377 } 1930 }
1378 } 1931 }
1379 1932
1380 if (num_done > 0 && lp->tbusy) { 1933 /* If we had stopped the queue due to a "tx full"
1381 lp->tbusy = 0; 1934 * condition, and space has now been made available,
1382 netif_start_queue(dev); 1935 * wake up the queue.
1383 } 1936 */
1937 if (netif_queue_stopped(dev) && ! tc35815_tx_full(dev))
1938 netif_wake_queue(dev);
1384} 1939}
1385 1940
1386/* The inverse routine to tc35815_open(). */ 1941/* The inverse routine to tc35815_open(). */
@@ -1388,18 +1943,18 @@ static int
1388tc35815_close(struct net_device *dev) 1943tc35815_close(struct net_device *dev)
1389{ 1944{
1390 struct tc35815_local *lp = dev->priv; 1945 struct tc35815_local *lp = dev->priv;
1391
1392 lp->tbusy = 1;
1393 netif_stop_queue(dev); 1946 netif_stop_queue(dev);
1394 1947
1395 /* Flush the Tx and disable Rx here. */ 1948 /* Flush the Tx and disable Rx here. */
1396 1949
1950 del_timer(&lp->timer); /* Kill if running */
1397 tc35815_chip_reset(dev); 1951 tc35815_chip_reset(dev);
1398 free_irq(dev->irq, dev); 1952 free_irq(dev->irq, dev);
1399 1953
1400 tc35815_free_queues(dev); 1954 tc35815_free_queues(dev);
1401 1955
1402 return 0; 1956 return 0;
1957
1403} 1958}
1404 1959
1405/* 1960/*
@@ -1409,29 +1964,29 @@ tc35815_close(struct net_device *dev)
1409static struct net_device_stats *tc35815_get_stats(struct net_device *dev) 1964static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
1410{ 1965{
1411 struct tc35815_local *lp = dev->priv; 1966 struct tc35815_local *lp = dev->priv;
1412 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr; 1967 struct tc35815_regs __iomem *tr =
1413 unsigned long flags; 1968 (struct tc35815_regs __iomem *)dev->base_addr;
1414
1415 if (netif_running(dev)) { 1969 if (netif_running(dev)) {
1416 spin_lock_irqsave(&lp->lock, flags);
1417 /* Update the statistics from the device registers. */ 1970 /* Update the statistics from the device registers. */
1418 lp->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt); 1971 lp->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt);
1419 spin_unlock_irqrestore(&lp->lock, flags);
1420 } 1972 }
1421 1973
1422 return &lp->stats; 1974 return &lp->stats;
1423} 1975}
1424 1976
1425static void tc35815_set_cam_entry(struct tc35815_regs *tr, int index, unsigned char *addr) 1977static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
1426{ 1978{
1979 struct tc35815_local *lp = dev->priv;
1980 struct tc35815_regs __iomem *tr =
1981 (struct tc35815_regs __iomem *)dev->base_addr;
1427 int cam_index = index * 6; 1982 int cam_index = index * 6;
1428 unsigned long cam_data; 1983 u32 cam_data;
1429 unsigned long saved_addr; 1984 u32 saved_addr;
1430 saved_addr = tc_readl(&tr->CAM_Adr); 1985 saved_addr = tc_readl(&tr->CAM_Adr);
1431 1986
1432 if (tc35815_debug > 1) { 1987 if (netif_msg_hw(lp)) {
1433 int i; 1988 int i;
1434 printk(KERN_DEBUG "%s: CAM %d:", cardname, index); 1989 printk(KERN_DEBUG "%s: CAM %d:", dev->name, index);
1435 for (i = 0; i < 6; i++) 1990 for (i = 0; i < 6; i++)
1436 printk(" %02x", addr[i]); 1991 printk(" %02x", addr[i]);
1437 printk("\n"); 1992 printk("\n");
@@ -1458,14 +2013,6 @@ static void tc35815_set_cam_entry(struct tc35815_regs *tr, int index, unsigned c
1458 tc_writel(cam_data, &tr->CAM_Data); 2013 tc_writel(cam_data, &tr->CAM_Data);
1459 } 2014 }
1460 2015
1461 if (tc35815_debug > 2) {
1462 int i;
1463 for (i = cam_index / 4; i < cam_index / 4 + 2; i++) {
1464 tc_writel(i * 4, &tr->CAM_Adr);
1465 printk("CAM 0x%x: %08lx",
1466 i * 4, tc_readl(&tr->CAM_Data));
1467 }
1468 }
1469 tc_writel(saved_addr, &tr->CAM_Adr); 2016 tc_writel(saved_addr, &tr->CAM_Adr);
1470} 2017}
1471 2018
@@ -1480,10 +2027,19 @@ static void tc35815_set_cam_entry(struct tc35815_regs *tr, int index, unsigned c
1480static void 2027static void
1481tc35815_set_multicast_list(struct net_device *dev) 2028tc35815_set_multicast_list(struct net_device *dev)
1482{ 2029{
1483 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr; 2030 struct tc35815_regs __iomem *tr =
2031 (struct tc35815_regs __iomem *)dev->base_addr;
1484 2032
1485 if (dev->flags&IFF_PROMISC) 2033 if (dev->flags&IFF_PROMISC)
1486 { 2034 {
2035#ifdef WORKAROUND_100HALF_PROMISC
2036 /* With some (all?) 100MHalf HUB, controller will hang
2037 * if we enabled promiscuous mode before linkup... */
2038 struct tc35815_local *lp = dev->priv;
2039 int pid = lp->phy_addr;
2040 if (!(tc_mdio_read(dev, pid, MII_BMSR) & BMSR_LSTATUS))
2041 return;
2042#endif
1487 /* Enable promiscuous mode */ 2043 /* Enable promiscuous mode */
1488 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl); 2044 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
1489 } 2045 }
@@ -1505,7 +2061,7 @@ tc35815_set_multicast_list(struct net_device *dev)
1505 if (!cur_addr) 2061 if (!cur_addr)
1506 break; 2062 break;
1507 /* entry 0,1 is reserved. */ 2063 /* entry 0,1 is reserved. */
1508 tc35815_set_cam_entry(tr, i + 2, cur_addr->dmi_addr); 2064 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
1509 ena_bits |= CAM_Ena_Bit(i + 2); 2065 ena_bits |= CAM_Ena_Bit(i + 2);
1510 } 2066 }
1511 tc_writel(ena_bits, &tr->CAM_Ena); 2067 tc_writel(ena_bits, &tr->CAM_Ena);
@@ -1517,122 +2073,753 @@ tc35815_set_multicast_list(struct net_device *dev)
1517 } 2073 }
1518} 2074}
1519 2075
1520static unsigned long tc_phy_read(struct net_device *dev, struct tc35815_regs *tr, int phy, int phy_reg) 2076static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1521{ 2077{
1522 struct tc35815_local *lp = dev->priv; 2078 struct tc35815_local *lp = dev->priv;
1523 unsigned long data; 2079 strcpy(info->driver, MODNAME);
1524 unsigned long flags; 2080 strcpy(info->version, DRV_VERSION);
2081 strcpy(info->bus_info, pci_name(lp->pci_dev));
2082}
1525 2083
1526 spin_lock_irqsave(&lp->lock, flags); 2084static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2085{
2086 struct tc35815_local *lp = dev->priv;
2087 spin_lock_irq(&lp->lock);
2088 mii_ethtool_gset(&lp->mii, cmd);
2089 spin_unlock_irq(&lp->lock);
2090 return 0;
2091}
1527 2092
1528 tc_writel(MD_CA_Busy | (phy << 5) | phy_reg, &tr->MD_CA); 2093static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2094{
2095 struct tc35815_local *lp = dev->priv;
2096 int rc;
2097#if 1 /* use our negotiation method... */
2098 /* Verify the settings we care about. */
2099 if (cmd->autoneg != AUTONEG_ENABLE &&
2100 cmd->autoneg != AUTONEG_DISABLE)
2101 return -EINVAL;
2102 if (cmd->autoneg == AUTONEG_DISABLE &&
2103 ((cmd->speed != SPEED_100 &&
2104 cmd->speed != SPEED_10) ||
2105 (cmd->duplex != DUPLEX_HALF &&
2106 cmd->duplex != DUPLEX_FULL)))
2107 return -EINVAL;
2108
2109 /* Ok, do it to it. */
2110 spin_lock_irq(&lp->lock);
2111 del_timer(&lp->timer);
2112 tc35815_start_auto_negotiation(dev, cmd);
2113 spin_unlock_irq(&lp->lock);
2114 rc = 0;
2115#else
2116 spin_lock_irq(&lp->lock);
2117 rc = mii_ethtool_sset(&lp->mii, cmd);
2118 spin_unlock_irq(&lp->lock);
2119#endif
2120 return rc;
2121}
2122
2123static int tc35815_nway_reset(struct net_device *dev)
2124{
2125 struct tc35815_local *lp = dev->priv;
2126 int rc;
2127 spin_lock_irq(&lp->lock);
2128 rc = mii_nway_restart(&lp->mii);
2129 spin_unlock_irq(&lp->lock);
2130 return rc;
2131}
2132
2133static u32 tc35815_get_link(struct net_device *dev)
2134{
2135 struct tc35815_local *lp = dev->priv;
2136 int rc;
2137 spin_lock_irq(&lp->lock);
2138 rc = mii_link_ok(&lp->mii);
2139 spin_unlock_irq(&lp->lock);
2140 return rc;
2141}
2142
2143static u32 tc35815_get_msglevel(struct net_device *dev)
2144{
2145 struct tc35815_local *lp = dev->priv;
2146 return lp->msg_enable;
2147}
2148
2149static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2150{
2151 struct tc35815_local *lp = dev->priv;
2152 lp->msg_enable = datum;
2153}
2154
2155static int tc35815_get_stats_count(struct net_device *dev)
2156{
2157 struct tc35815_local *lp = dev->priv;
2158 return sizeof(lp->lstats) / sizeof(int);
2159}
2160
2161static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2162{
2163 struct tc35815_local *lp = dev->priv;
2164 data[0] = lp->lstats.max_tx_qlen;
2165 data[1] = lp->lstats.tx_ints;
2166 data[2] = lp->lstats.rx_ints;
2167 data[3] = lp->lstats.tx_underrun;
2168}
2169
2170static struct {
2171 const char str[ETH_GSTRING_LEN];
2172} ethtool_stats_keys[] = {
2173 { "max_tx_qlen" },
2174 { "tx_ints" },
2175 { "rx_ints" },
2176 { "tx_underrun" },
2177};
2178
2179static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2180{
2181 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2182}
2183
2184static const struct ethtool_ops tc35815_ethtool_ops = {
2185 .get_drvinfo = tc35815_get_drvinfo,
2186 .get_settings = tc35815_get_settings,
2187 .set_settings = tc35815_set_settings,
2188 .nway_reset = tc35815_nway_reset,
2189 .get_link = tc35815_get_link,
2190 .get_msglevel = tc35815_get_msglevel,
2191 .set_msglevel = tc35815_set_msglevel,
2192 .get_strings = tc35815_get_strings,
2193 .get_stats_count = tc35815_get_stats_count,
2194 .get_ethtool_stats = tc35815_get_ethtool_stats,
2195 .get_perm_addr = ethtool_op_get_perm_addr,
2196};
2197
2198static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2199{
2200 struct tc35815_local *lp = dev->priv;
2201 int rc;
2202
2203 if (!netif_running(dev))
2204 return -EINVAL;
2205
2206 spin_lock_irq(&lp->lock);
2207 rc = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
2208 spin_unlock_irq(&lp->lock);
2209
2210 return rc;
2211}
2212
2213static int tc_mdio_read(struct net_device *dev, int phy_id, int location)
2214{
2215 struct tc35815_regs __iomem *tr =
2216 (struct tc35815_regs __iomem *)dev->base_addr;
2217 u32 data;
2218 tc_writel(MD_CA_Busy | (phy_id << 5) | location, &tr->MD_CA);
1529 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) 2219 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
1530 ; 2220 ;
1531 data = tc_readl(&tr->MD_Data); 2221 data = tc_readl(&tr->MD_Data);
1532 spin_unlock_irqrestore(&lp->lock, flags); 2222 return data & 0xffff;
1533 return data; 2223}
2224
2225static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
2226 int val)
2227{
2228 struct tc35815_regs __iomem *tr =
2229 (struct tc35815_regs __iomem *)dev->base_addr;
2230 tc_writel(val, &tr->MD_Data);
2231 tc_writel(MD_CA_Busy | MD_CA_Wr | (phy_id << 5) | location, &tr->MD_CA);
2232 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2233 ;
1534} 2234}
1535 2235
1536static void tc_phy_write(struct net_device *dev, unsigned long d, struct tc35815_regs *tr, int phy, int phy_reg) 2236/* Auto negotiation. The scheme is very simple. We have a timer routine
2237 * that keeps watching the auto negotiation process as it progresses.
2238 * The DP83840 is first told to start doing it's thing, we set up the time
2239 * and place the timer state machine in it's initial state.
2240 *
2241 * Here the timer peeks at the DP83840 status registers at each click to see
2242 * if the auto negotiation has completed, we assume here that the DP83840 PHY
2243 * will time out at some point and just tell us what (didn't) happen. For
2244 * complete coverage we only allow so many of the ticks at this level to run,
2245 * when this has expired we print a warning message and try another strategy.
2246 * This "other" strategy is to force the interface into various speed/duplex
2247 * configurations and we stop when we see a link-up condition before the
2248 * maximum number of "peek" ticks have occurred.
2249 *
2250 * Once a valid link status has been detected we configure the BigMAC and
2251 * the rest of the Happy Meal to speak the most efficient protocol we could
2252 * get a clean link for. The priority for link configurations, highest first
2253 * is:
2254 * 100 Base-T Full Duplex
2255 * 100 Base-T Half Duplex
2256 * 10 Base-T Full Duplex
2257 * 10 Base-T Half Duplex
2258 *
2259 * We start a new timer now, after a successful auto negotiation status has
2260 * been detected. This timer just waits for the link-up bit to get set in
2261 * the BMCR of the DP83840. When this occurs we print a kernel log message
2262 * describing the link type in use and the fact that it is up.
2263 *
2264 * If a fatal error of some sort is signalled and detected in the interrupt
2265 * service routine, and the chip is reset, or the link is ifconfig'd down
2266 * and then back up, this entire process repeats itself all over again.
2267 */
2268/* Note: Above comments are come from sunhme driver. */
2269
2270static int tc35815_try_next_permutation(struct net_device *dev)
1537{ 2271{
1538 struct tc35815_local *lp = dev->priv; 2272 struct tc35815_local *lp = dev->priv;
1539 unsigned long flags; 2273 int pid = lp->phy_addr;
2274 unsigned short bmcr;
1540 2275
1541 spin_lock_irqsave(&lp->lock, flags); 2276 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
1542 2277
1543 tc_writel(d, &tr->MD_Data); 2278 /* Downgrade from full to half duplex. Only possible via ethtool. */
1544 tc_writel(MD_CA_Busy | MD_CA_Wr | (phy << 5) | phy_reg, &tr->MD_CA); 2279 if (bmcr & BMCR_FULLDPLX) {
1545 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) 2280 bmcr &= ~BMCR_FULLDPLX;
1546 ; 2281 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
1547 spin_unlock_irqrestore(&lp->lock, flags); 2282 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2283 return 0;
2284 }
2285
2286 /* Downgrade from 100 to 10. */
2287 if (bmcr & BMCR_SPEED100) {
2288 bmcr &= ~BMCR_SPEED100;
2289 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2290 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2291 return 0;
2292 }
2293
2294 /* We've tried everything. */
2295 return -1;
1548} 2296}
1549 2297
1550static void tc35815_phy_chip_init(struct net_device *dev) 2298static void
2299tc35815_display_link_mode(struct net_device *dev)
1551{ 2300{
1552 struct tc35815_local *lp = dev->priv; 2301 struct tc35815_local *lp = dev->priv;
1553 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr; 2302 int pid = lp->phy_addr;
1554 static int first = 1; 2303 unsigned short lpa, bmcr;
1555 unsigned short ctl; 2304 char *speed = "", *duplex = "";
1556 2305
1557 if (first) { 2306 lpa = tc_mdio_read(dev, pid, MII_LPA);
1558 unsigned short id0, id1; 2307 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
1559 int count; 2308 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
1560 first = 0; 2309 speed = "100Mb/s";
1561 2310 else
1562 /* first data written to the PHY will be an ID number */ 2311 speed = "10Mb/s";
1563 tc_phy_write(dev, 0, tr, 0, MII_CONTROL); /* ID:0 */ 2312 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
1564#if 0 2313 duplex = "Full Duplex";
1565 tc_phy_write(dev, MIICNTL_RESET, tr, 0, MII_CONTROL); 2314 else
1566 printk(KERN_INFO "%s: Resetting PHY...", dev->name); 2315 duplex = "Half Duplex";
1567 while (tc_phy_read(dev, tr, 0, MII_CONTROL) & MIICNTL_RESET) 2316
1568 ; 2317 if (netif_msg_link(lp))
1569 printk("\n"); 2318 printk(KERN_INFO "%s: Link is up at %s, %s.\n",
1570 tc_phy_write(dev, MIICNTL_AUTO|MIICNTL_SPEED|MIICNTL_FDX, tr, 0, 2319 dev->name, speed, duplex);
1571 MII_CONTROL); 2320 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
1572#endif 2321 dev->name,
1573 id0 = tc_phy_read(dev, tr, 0, MII_PHY_ID0); 2322 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
1574 id1 = tc_phy_read(dev, tr, 0, MII_PHY_ID1); 2323}
1575 printk(KERN_DEBUG "%s: PHY ID %04x %04x\n", dev->name, 2324
1576 id0, id1); 2325static void tc35815_display_forced_link_mode(struct net_device *dev)
1577 if (lp->option & TC35815_OPT_10M) { 2326{
1578 lp->linkspeed = 10; 2327 struct tc35815_local *lp = dev->priv;
1579 lp->fullduplex = (lp->option & TC35815_OPT_FULLDUP) != 0; 2328 int pid = lp->phy_addr;
1580 } else if (lp->option & TC35815_OPT_100M) { 2329 unsigned short bmcr;
1581 lp->linkspeed = 100; 2330 char *speed = "", *duplex = "";
1582 lp->fullduplex = (lp->option & TC35815_OPT_FULLDUP) != 0; 2331
2332 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2333 if (bmcr & BMCR_SPEED100)
2334 speed = "100Mb/s";
2335 else
2336 speed = "10Mb/s";
2337 if (bmcr & BMCR_FULLDPLX)
2338 duplex = "Full Duplex.\n";
2339 else
2340 duplex = "Half Duplex.\n";
2341
2342 if (netif_msg_link(lp))
2343 printk(KERN_INFO "%s: Link has been forced up at %s, %s",
2344 dev->name, speed, duplex);
2345}
2346
2347static void tc35815_set_link_modes(struct net_device *dev)
2348{
2349 struct tc35815_local *lp = dev->priv;
2350 struct tc35815_regs __iomem *tr =
2351 (struct tc35815_regs __iomem *)dev->base_addr;
2352 int pid = lp->phy_addr;
2353 unsigned short bmcr, lpa;
2354 int speed;
2355
2356 if (lp->timer_state == arbwait) {
2357 lpa = tc_mdio_read(dev, pid, MII_LPA);
2358 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2359 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2360 dev->name,
2361 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2362 if (!(lpa & (LPA_10HALF | LPA_10FULL |
2363 LPA_100HALF | LPA_100FULL))) {
2364 /* fall back to 10HALF */
2365 printk(KERN_INFO "%s: bad ability %04x - falling back to 10HD.\n",
2366 dev->name, lpa);
2367 lpa = LPA_10HALF;
2368 }
2369 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2370 lp->fullduplex = 1;
2371 else
2372 lp->fullduplex = 0;
2373 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2374 speed = 100;
2375 else
2376 speed = 10;
2377 } else {
2378 /* Forcing a link mode. */
2379 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2380 if (bmcr & BMCR_FULLDPLX)
2381 lp->fullduplex = 1;
2382 else
2383 lp->fullduplex = 0;
2384 if (bmcr & BMCR_SPEED100)
2385 speed = 100;
2386 else
2387 speed = 10;
2388 }
2389
2390 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_HaltReq, &tr->MAC_Ctl);
2391 if (lp->fullduplex) {
2392 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl);
2393 } else {
2394 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_FullDup, &tr->MAC_Ctl);
2395 }
2396 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_HaltReq, &tr->MAC_Ctl);
2397
2398 /* TX4939 PCFG.SPEEDn bit will be changed on NETDEV_CHANGE event. */
2399
2400#ifndef NO_CHECK_CARRIER
2401 /* TX4939 does not have EnLCarr */
2402 if (lp->boardtype != TC35815_TX4939) {
2403#ifdef WORKAROUND_LOSTCAR
2404 /* WORKAROUND: enable LostCrS only if half duplex operation */
2405 if (!lp->fullduplex && lp->boardtype != TC35815_TX4939)
2406 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, &tr->Tx_Ctl);
2407#endif
2408 }
2409#endif
2410 lp->mii.full_duplex = lp->fullduplex;
2411}
2412
2413static void tc35815_timer(unsigned long data)
2414{
2415 struct net_device *dev = (struct net_device *)data;
2416 struct tc35815_local *lp = dev->priv;
2417 int pid = lp->phy_addr;
2418 unsigned short bmsr, bmcr, lpa;
2419 int restart_timer = 0;
2420
2421 spin_lock_irq(&lp->lock);
2422
2423 lp->timer_ticks++;
2424 switch (lp->timer_state) {
2425 case arbwait:
2426 /*
2427 * Only allow for 5 ticks, thats 10 seconds and much too
2428 * long to wait for arbitration to complete.
2429 */
2430 /* TC35815 need more times... */
2431 if (lp->timer_ticks >= 10) {
2432 /* Enter force mode. */
2433 if (!options.doforce) {
2434 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2435 " cable probblem?\n", dev->name);
2436 /* Try to restart the adaptor. */
2437 tc35815_restart(dev);
2438 goto out;
2439 }
2440 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2441 " trying force link mode\n", dev->name);
2442 printk(KERN_DEBUG "%s: BMCR %x BMSR %x\n", dev->name,
2443 tc_mdio_read(dev, pid, MII_BMCR),
2444 tc_mdio_read(dev, pid, MII_BMSR));
2445 bmcr = BMCR_SPEED100;
2446 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2447
2448 /*
2449 * OK, seems we need do disable the transceiver
2450 * for the first tick to make sure we get an
2451 * accurate link state at the second tick.
2452 */
2453
2454 lp->timer_state = ltrywait;
2455 lp->timer_ticks = 0;
2456 restart_timer = 1;
1583 } else { 2457 } else {
1584 /* auto negotiation */ 2458 /* Anything interesting happen? */
1585 unsigned long neg_result; 2459 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
1586 tc_phy_write(dev, MIICNTL_AUTO | MIICNTL_RST_AUTO, tr, 0, MII_CONTROL); 2460 if (bmsr & BMSR_ANEGCOMPLETE) {
1587 printk(KERN_INFO "%s: Auto Negotiation...", dev->name); 2461 /* Just what we've been waiting for... */
1588 count = 0; 2462 tc35815_set_link_modes(dev);
1589 while (!(tc_phy_read(dev, tr, 0, MII_STATUS) & MIISTAT_AUTO_DONE)) { 2463
1590 if (count++ > 5000) { 2464 /*
1591 printk(" failed. Assume 10Mbps\n"); 2465 * Success, at least so far, advance our state
1592 lp->linkspeed = 10; 2466 * engine.
1593 lp->fullduplex = 0; 2467 */
1594 goto done; 2468 lp->timer_state = lupwait;
2469 restart_timer = 1;
2470 } else {
2471 restart_timer = 1;
2472 }
2473 }
2474 break;
2475
2476 case lupwait:
2477 /*
2478 * Auto negotiation was successful and we are awaiting a
2479 * link up status. I have decided to let this timer run
2480 * forever until some sort of error is signalled, reporting
2481 * a message to the user at 10 second intervals.
2482 */
2483 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2484 if (bmsr & BMSR_LSTATUS) {
2485 /*
2486 * Wheee, it's up, display the link mode in use and put
2487 * the timer to sleep.
2488 */
2489 tc35815_display_link_mode(dev);
2490 netif_carrier_on(dev);
2491#ifdef WORKAROUND_100HALF_PROMISC
2492 /* delayed promiscuous enabling */
2493 if (dev->flags & IFF_PROMISC)
2494 tc35815_set_multicast_list(dev);
2495#endif
2496#if 1
2497 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2498 lp->timer_state = lcheck;
2499 restart_timer = 1;
2500#else
2501 lp->timer_state = asleep;
2502 restart_timer = 0;
2503#endif
2504 } else {
2505 if (lp->timer_ticks >= 10) {
2506 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
2507 "not completely up.\n", dev->name);
2508 lp->timer_ticks = 0;
2509 restart_timer = 1;
2510 } else {
2511 restart_timer = 1;
2512 }
2513 }
2514 break;
2515
2516 case ltrywait:
2517 /*
2518 * Making the timeout here too long can make it take
2519 * annoyingly long to attempt all of the link mode
2520 * permutations, but then again this is essentially
2521 * error recovery code for the most part.
2522 */
2523 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2524 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2525 if (lp->timer_ticks == 1) {
2526 /*
2527 * Re-enable transceiver, we'll re-enable the
2528 * transceiver next tick, then check link state
2529 * on the following tick.
2530 */
2531 restart_timer = 1;
2532 break;
2533 }
2534 if (lp->timer_ticks == 2) {
2535 restart_timer = 1;
2536 break;
2537 }
2538 if (bmsr & BMSR_LSTATUS) {
2539 /* Force mode selection success. */
2540 tc35815_display_forced_link_mode(dev);
2541 netif_carrier_on(dev);
2542 tc35815_set_link_modes(dev);
2543#ifdef WORKAROUND_100HALF_PROMISC
2544 /* delayed promiscuous enabling */
2545 if (dev->flags & IFF_PROMISC)
2546 tc35815_set_multicast_list(dev);
2547#endif
2548#if 1
2549 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2550 lp->timer_state = lcheck;
2551 restart_timer = 1;
2552#else
2553 lp->timer_state = asleep;
2554 restart_timer = 0;
2555#endif
2556 } else {
2557 if (lp->timer_ticks >= 4) { /* 6 seconds or so... */
2558 int ret;
2559
2560 ret = tc35815_try_next_permutation(dev);
2561 if (ret == -1) {
2562 /*
2563 * Aieee, tried them all, reset the
2564 * chip and try all over again.
2565 */
2566 printk(KERN_NOTICE "%s: Link down, "
2567 "cable problem?\n",
2568 dev->name);
2569
2570 /* Try to restart the adaptor. */
2571 tc35815_restart(dev);
2572 goto out;
1595 } 2573 }
1596 if (count % 512 == 0) 2574 lp->timer_ticks = 0;
1597 printk("."); 2575 restart_timer = 1;
1598 mdelay(1); 2576 } else {
2577 restart_timer = 1;
2578 }
2579 }
2580 break;
2581
2582 case lcheck:
2583 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2584 lpa = tc_mdio_read(dev, pid, MII_LPA);
2585 if (bmcr & (BMCR_PDOWN | BMCR_ISOLATE | BMCR_RESET)) {
2586 printk(KERN_ERR "%s: PHY down? (BMCR %x)\n", dev->name,
2587 bmcr);
2588 } else if ((lp->saved_lpa ^ lpa) &
2589 (LPA_100FULL|LPA_100HALF|LPA_10FULL|LPA_10HALF)) {
2590 printk(KERN_NOTICE "%s: link status changed"
2591 " (BMCR %x LPA %x->%x)\n", dev->name,
2592 bmcr, lp->saved_lpa, lpa);
2593 } else {
2594 /* go on */
2595 restart_timer = 1;
2596 break;
2597 }
2598 /* Try to restart the adaptor. */
2599 tc35815_restart(dev);
2600 goto out;
2601
2602 case asleep:
2603 default:
2604 /* Can't happens.... */
2605 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
2606 "one anyways!\n", dev->name);
2607 restart_timer = 0;
2608 lp->timer_ticks = 0;
2609 lp->timer_state = asleep; /* foo on you */
2610 break;
2611 }
2612
2613 if (restart_timer) {
2614 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2615 add_timer(&lp->timer);
2616 }
2617out:
2618 spin_unlock_irq(&lp->lock);
2619}
2620
2621static void tc35815_start_auto_negotiation(struct net_device *dev,
2622 struct ethtool_cmd *ep)
2623{
2624 struct tc35815_local *lp = dev->priv;
2625 int pid = lp->phy_addr;
2626 unsigned short bmsr, bmcr, advertize;
2627 int timeout;
2628
2629 netif_carrier_off(dev);
2630 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2631 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2632 advertize = tc_mdio_read(dev, pid, MII_ADVERTISE);
2633
2634 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2635 if (options.speed || options.duplex) {
2636 /* Advertise only specified configuration. */
2637 advertize &= ~(ADVERTISE_10HALF |
2638 ADVERTISE_10FULL |
2639 ADVERTISE_100HALF |
2640 ADVERTISE_100FULL);
2641 if (options.speed != 10) {
2642 if (options.duplex != 1)
2643 advertize |= ADVERTISE_100FULL;
2644 if (options.duplex != 2)
2645 advertize |= ADVERTISE_100HALF;
2646 }
2647 if (options.speed != 100) {
2648 if (options.duplex != 1)
2649 advertize |= ADVERTISE_10FULL;
2650 if (options.duplex != 2)
2651 advertize |= ADVERTISE_10HALF;
1599 } 2652 }
1600 printk(" done.\n"); 2653 if (options.speed == 100)
1601 neg_result = tc_phy_read(dev, tr, 0, MII_ANLPAR); 2654 bmcr |= BMCR_SPEED100;
1602 if (neg_result & (MII_AN_TX_FDX | MII_AN_TX_HDX)) 2655 else if (options.speed == 10)
1603 lp->linkspeed = 100; 2656 bmcr &= ~BMCR_SPEED100;
2657 if (options.duplex == 2)
2658 bmcr |= BMCR_FULLDPLX;
2659 else if (options.duplex == 1)
2660 bmcr &= ~BMCR_FULLDPLX;
2661 } else {
2662 /* Advertise everything we can support. */
2663 if (bmsr & BMSR_10HALF)
2664 advertize |= ADVERTISE_10HALF;
1604 else 2665 else
1605 lp->linkspeed = 10; 2666 advertize &= ~ADVERTISE_10HALF;
1606 if (neg_result & (MII_AN_TX_FDX | MII_AN_10_FDX)) 2667 if (bmsr & BMSR_10FULL)
1607 lp->fullduplex = 1; 2668 advertize |= ADVERTISE_10FULL;
1608 else 2669 else
1609 lp->fullduplex = 0; 2670 advertize &= ~ADVERTISE_10FULL;
1610 done: 2671 if (bmsr & BMSR_100HALF)
1611 ; 2672 advertize |= ADVERTISE_100HALF;
2673 else
2674 advertize &= ~ADVERTISE_100HALF;
2675 if (bmsr & BMSR_100FULL)
2676 advertize |= ADVERTISE_100FULL;
2677 else
2678 advertize &= ~ADVERTISE_100FULL;
2679 }
2680
2681 tc_mdio_write(dev, pid, MII_ADVERTISE, advertize);
2682
2683 /* Enable Auto-Negotiation, this is usually on already... */
2684 bmcr |= BMCR_ANENABLE;
2685 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2686
2687 /* Restart it to make sure it is going. */
2688 bmcr |= BMCR_ANRESTART;
2689 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2690 printk(KERN_DEBUG "%s: ADVERTISE %x BMCR %x\n", dev->name, advertize, bmcr);
2691
2692 /* BMCR_ANRESTART self clears when the process has begun. */
2693 timeout = 64; /* More than enough. */
2694 while (--timeout) {
2695 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2696 if (!(bmcr & BMCR_ANRESTART))
2697 break; /* got it. */
2698 udelay(10);
1612 } 2699 }
2700 if (!timeout) {
2701 printk(KERN_ERR "%s: TC35815 would not start auto "
2702 "negotiation BMCR=0x%04x\n",
2703 dev->name, bmcr);
2704 printk(KERN_NOTICE "%s: Performing force link "
2705 "detection.\n", dev->name);
2706 goto force_link;
2707 } else {
2708 printk(KERN_DEBUG "%s: auto negotiation started.\n", dev->name);
2709 lp->timer_state = arbwait;
2710 }
2711 } else {
2712force_link:
2713 /* Force the link up, trying first a particular mode.
2714 * Either we are here at the request of ethtool or
2715 * because the Happy Meal would not start to autoneg.
2716 */
2717
2718 /* Disable auto-negotiation in BMCR, enable the duplex and
2719 * speed setting, init the timer state machine, and fire it off.
2720 */
2721 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2722 bmcr = BMCR_SPEED100;
2723 } else {
2724 if (ep->speed == SPEED_100)
2725 bmcr = BMCR_SPEED100;
2726 else
2727 bmcr = 0;
2728 if (ep->duplex == DUPLEX_FULL)
2729 bmcr |= BMCR_FULLDPLX;
2730 }
2731 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2732
2733 /* OK, seems we need do disable the transceiver for the first
2734 * tick to make sure we get an accurate link state at the
2735 * second tick.
2736 */
2737 lp->timer_state = ltrywait;
1613 } 2738 }
1614 2739
1615 ctl = 0; 2740 del_timer(&lp->timer);
1616 if (lp->linkspeed == 100) 2741 lp->timer_ticks = 0;
1617 ctl |= MIICNTL_SPEED; 2742 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
1618 if (lp->fullduplex) 2743 add_timer(&lp->timer);
1619 ctl |= MIICNTL_FDX; 2744}
1620 tc_phy_write(dev, ctl, tr, 0, MII_CONTROL);
1621 2745
1622 if (lp->fullduplex) { 2746static void tc35815_find_phy(struct net_device *dev)
1623 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl); 2747{
2748 struct tc35815_local *lp = dev->priv;
2749 int pid = lp->phy_addr;
2750 unsigned short id0;
2751
2752 /* find MII phy */
2753 for (pid = 31; pid >= 0; pid--) {
2754 id0 = tc_mdio_read(dev, pid, MII_BMSR);
2755 if (id0 != 0xffff && id0 != 0x0000 &&
2756 (id0 & BMSR_RESV) != (0xffff & BMSR_RESV) /* paranoia? */
2757 ) {
2758 lp->phy_addr = pid;
2759 break;
2760 }
1624 } 2761 }
2762 if (pid < 0) {
2763 printk(KERN_ERR "%s: No MII Phy found.\n",
2764 dev->name);
2765 lp->phy_addr = pid = 0;
2766 }
2767
2768 lp->mii_id[0] = tc_mdio_read(dev, pid, MII_PHYSID1);
2769 lp->mii_id[1] = tc_mdio_read(dev, pid, MII_PHYSID2);
2770 if (netif_msg_hw(lp))
2771 printk(KERN_INFO "%s: PHY(%02x) ID %04x %04x\n", dev->name,
2772 pid, lp->mii_id[0], lp->mii_id[1]);
1625} 2773}
1626 2774
1627static void tc35815_chip_reset(struct net_device *dev) 2775static void tc35815_phy_chip_init(struct net_device *dev)
1628{ 2776{
1629 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr; 2777 struct tc35815_local *lp = dev->priv;
2778 int pid = lp->phy_addr;
2779 unsigned short bmcr;
2780 struct ethtool_cmd ecmd, *ep;
2781
2782 /* dis-isolate if needed. */
2783 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2784 if (bmcr & BMCR_ISOLATE) {
2785 int count = 32;
2786 printk(KERN_DEBUG "%s: unisolating...", dev->name);
2787 tc_mdio_write(dev, pid, MII_BMCR, bmcr & ~BMCR_ISOLATE);
2788 while (--count) {
2789 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_ISOLATE))
2790 break;
2791 udelay(20);
2792 }
2793 printk(" %s.\n", count ? "done" : "failed");
2794 }
2795
2796 if (options.speed && options.duplex) {
2797 ecmd.autoneg = AUTONEG_DISABLE;
2798 ecmd.speed = options.speed == 10 ? SPEED_10 : SPEED_100;
2799 ecmd.duplex = options.duplex == 1 ? DUPLEX_HALF : DUPLEX_FULL;
2800 ep = &ecmd;
2801 } else {
2802 ep = NULL;
2803 }
2804 tc35815_start_auto_negotiation(dev, ep);
2805}
1630 2806
2807static void tc35815_chip_reset(struct net_device *dev)
2808{
2809 struct tc35815_regs __iomem *tr =
2810 (struct tc35815_regs __iomem *)dev->base_addr;
2811 int i;
1631 /* reset the controller */ 2812 /* reset the controller */
1632 tc_writel(MAC_Reset, &tr->MAC_Ctl); 2813 tc_writel(MAC_Reset, &tr->MAC_Ctl);
1633 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) 2814 udelay(4); /* 3200ns */
1634 ; 2815 i = 0;
1635 2816 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2817 if (i++ > 100) {
2818 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2819 break;
2820 }
2821 mdelay(1);
2822 }
1636 tc_writel(0, &tr->MAC_Ctl); 2823 tc_writel(0, &tr->MAC_Ctl);
1637 2824
1638 /* initialize registers to default value */ 2825 /* initialize registers to default value */
@@ -1650,90 +2837,142 @@ static void tc35815_chip_reset(struct net_device *dev)
1650 tc_writel(0, &tr->CAM_Ena); 2837 tc_writel(0, &tr->CAM_Ena);
1651 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */ 2838 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
1652 2839
2840 /* initialize internal SRAM */
2841 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2842 for (i = 0; i < 0x1000; i += 4) {
2843 tc_writel(i, &tr->CAM_Adr);
2844 tc_writel(0, &tr->CAM_Data);
2845 }
2846 tc_writel(0, &tr->DMA_Ctl);
1653} 2847}
1654 2848
1655static void tc35815_chip_init(struct net_device *dev) 2849static void tc35815_chip_init(struct net_device *dev)
1656{ 2850{
1657 struct tc35815_local *lp = dev->priv; 2851 struct tc35815_local *lp = dev->priv;
1658 struct tc35815_regs *tr = (struct tc35815_regs*)dev->base_addr; 2852 struct tc35815_regs __iomem *tr =
1659 unsigned long flags; 2853 (struct tc35815_regs __iomem *)dev->base_addr;
1660 unsigned long txctl = TX_CTL_CMD; 2854 unsigned long txctl = TX_CTL_CMD;
1661 2855
1662 tc35815_phy_chip_init(dev); 2856 tc35815_phy_chip_init(dev);
1663 2857
1664 /* load station address to CAM */ 2858 /* load station address to CAM */
1665 tc35815_set_cam_entry(tr, CAM_ENTRY_SOURCE, dev->dev_addr); 2859 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
1666 2860
1667 /* Enable CAM (broadcast and unicast) */ 2861 /* Enable CAM (broadcast and unicast) */
1668 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena); 2862 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
1669 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl); 2863 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
1670 2864
1671 spin_lock_irqsave(&lp->lock, flags); 2865 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
1672 2866 if (HAVE_DMA_RXALIGN(lp))
1673 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl); 2867 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
1674 2868 else
2869 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2870#ifdef TC35815_USE_PACKEDBUFFER
1675 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */ 2871 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
2872#else
2873 tc_writel(ETH_ZLEN, &tr->RxFragSize);
2874#endif
1676 tc_writel(0, &tr->TxPollCtr); /* Batch mode */ 2875 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
1677 tc_writel(TX_THRESHOLD, &tr->TxThrsh); 2876 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
1678 tc_writel(INT_EN_CMD, &tr->Int_En); 2877 tc_writel(INT_EN_CMD, &tr->Int_En);
1679 2878
1680 /* set queues */ 2879 /* set queues */
1681 tc_writel(virt_to_bus(lp->rfd_base), &tr->FDA_Bas); 2880 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
1682 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base, 2881 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
1683 &tr->FDA_Lim); 2882 &tr->FDA_Lim);
1684 /* 2883 /*
1685 * Activation method: 2884 * Activation method:
1686 * First, enable eht MAC Transmitter and the DMA Receive circuits. 2885 * First, enable the MAC Transmitter and the DMA Receive circuits.
1687 * Then enable the DMA Transmitter and the MAC Receive circuits. 2886 * Then enable the DMA Transmitter and the MAC Receive circuits.
1688 */ 2887 */
1689 tc_writel(virt_to_bus(lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */ 2888 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
1690 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */ 2889 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
2890
1691 /* start MAC transmitter */ 2891 /* start MAC transmitter */
2892#ifndef NO_CHECK_CARRIER
2893 /* TX4939 does not have EnLCarr */
2894 if (lp->boardtype == TC35815_TX4939)
2895 txctl &= ~Tx_EnLCarr;
2896#ifdef WORKAROUND_LOSTCAR
1692 /* WORKAROUND: ignore LostCrS in full duplex operation */ 2897 /* WORKAROUND: ignore LostCrS in full duplex operation */
1693 if (lp->fullduplex) 2898 if ((lp->timer_state != asleep && lp->timer_state != lcheck) ||
1694 txctl = TX_CTL_CMD & ~Tx_EnLCarr; 2899 lp->fullduplex)
2900 txctl &= ~Tx_EnLCarr;
2901#endif
2902#endif /* !NO_CHECK_CARRIER */
1695#ifdef GATHER_TXINT 2903#ifdef GATHER_TXINT
1696 txctl &= ~Tx_EnComp; /* disable global tx completion int. */ 2904 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
1697#endif 2905#endif
1698 tc_writel(txctl, &tr->Tx_Ctl); 2906 tc_writel(txctl, &tr->Tx_Ctl);
1699#if 0 /* No need to polling */ 2907}
1700 tc_writel(virt_to_bus(lp->tfd_base), &tr->TxFrmPtr); /* start DMA transmitter */ 2908
1701#endif 2909#ifdef CONFIG_PM
2910static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2911{
2912 struct net_device *dev = pci_get_drvdata(pdev);
2913 struct tc35815_local *lp = dev->priv;
2914 unsigned long flags;
2915
2916 pci_save_state(pdev);
2917 if (!netif_running(dev))
2918 return 0;
2919 netif_device_detach(dev);
2920 spin_lock_irqsave(&lp->lock, flags);
2921 del_timer(&lp->timer); /* Kill if running */
2922 tc35815_chip_reset(dev);
1702 spin_unlock_irqrestore(&lp->lock, flags); 2923 spin_unlock_irqrestore(&lp->lock, flags);
2924 pci_set_power_state(pdev, PCI_D3hot);
2925 return 0;
1703} 2926}
1704 2927
1705static struct pci_driver tc35815_driver = { 2928static int tc35815_resume(struct pci_dev *pdev)
1706 .name = TC35815_MODULE_NAME, 2929{
1707 .probe = tc35815_probe, 2930 struct net_device *dev = pci_get_drvdata(pdev);
1708 .remove = NULL, 2931 struct tc35815_local *lp = dev->priv;
1709 .id_table = tc35815_pci_tbl, 2932 unsigned long flags;
2933
2934 pci_restore_state(pdev);
2935 if (!netif_running(dev))
2936 return 0;
2937 pci_set_power_state(pdev, PCI_D0);
2938 spin_lock_irqsave(&lp->lock, flags);
2939 tc35815_restart(dev);
2940 spin_unlock_irqrestore(&lp->lock, flags);
2941 netif_device_attach(dev);
2942 return 0;
2943}
2944#endif /* CONFIG_PM */
2945
2946static struct pci_driver tc35815_pci_driver = {
2947 .name = MODNAME,
2948 .id_table = tc35815_pci_tbl,
2949 .probe = tc35815_init_one,
2950 .remove = __devexit_p(tc35815_remove_one),
2951#ifdef CONFIG_PM
2952 .suspend = tc35815_suspend,
2953 .resume = tc35815_resume,
2954#endif
1710}; 2955};
1711 2956
2957module_param_named(speed, options.speed, int, 0);
2958MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2959module_param_named(duplex, options.duplex, int, 0);
2960MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
2961module_param_named(doforce, options.doforce, int, 0);
2962MODULE_PARM_DESC(doforce, "try force link mode if auto-negotiation failed");
2963
1712static int __init tc35815_init_module(void) 2964static int __init tc35815_init_module(void)
1713{ 2965{
1714 return pci_register_driver(&tc35815_driver); 2966 return pci_register_driver(&tc35815_pci_driver);
1715} 2967}
1716 2968
1717static void __exit tc35815_cleanup_module(void) 2969static void __exit tc35815_cleanup_module(void)
1718{ 2970{
1719 struct net_device *next_dev; 2971 pci_unregister_driver(&tc35815_pci_driver);
1720
1721 /*
1722 * TODO: implement a tc35815_driver.remove hook, and
1723 * move this code into that function. Then, delete
1724 * all root_tc35815_dev list handling code.
1725 */
1726 while (root_tc35815_dev) {
1727 struct net_device *dev = root_tc35815_dev;
1728 next_dev = ((struct tc35815_local *)dev->priv)->next_module;
1729 iounmap((void *)(dev->base_addr));
1730 unregister_netdev(dev);
1731 free_netdev(dev);
1732 root_tc35815_dev = next_dev;
1733 }
1734
1735 pci_unregister_driver(&tc35815_driver);
1736} 2972}
1737 2973
1738module_init(tc35815_init_module); 2974module_init(tc35815_init_module);
1739module_exit(tc35815_cleanup_module); 2975module_exit(tc35815_cleanup_module);
2976
2977MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2978MODULE_LICENSE("GPL");
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 600308fdf9ce..247b5e63f6c2 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -1459,6 +1459,8 @@
1459 1459
1460#define PCI_VENDOR_ID_TOSHIBA_2 0x102f 1460#define PCI_VENDOR_ID_TOSHIBA_2 0x102f
1461#define PCI_DEVICE_ID_TOSHIBA_TC35815CF 0x0030 1461#define PCI_DEVICE_ID_TOSHIBA_TC35815CF 0x0030
1462#define PCI_DEVICE_ID_TOSHIBA_TC35815_NWU 0x0031
1463#define PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939 0x0032
1462#define PCI_DEVICE_ID_TOSHIBA_TC86C001_IDE 0x0105 1464#define PCI_DEVICE_ID_TOSHIBA_TC86C001_IDE 0x0105
1463#define PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC 0x0108 1465#define PCI_DEVICE_ID_TOSHIBA_TC86C001_MISC 0x0108
1464#define PCI_DEVICE_ID_TOSHIBA_SPIDER_NET 0x01b3 1466#define PCI_DEVICE_ID_TOSHIBA_SPIDER_NET 0x01b3