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