tc35815.c 87.4 KB
Newer Older
A
Atsushi Nemoto 已提交
1 2
/*
 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
L
Linus Torvalds 已提交
3 4 5
 *
 * Based on skelton.c by Donald Becker.
 *
A
Atsushi Nemoto 已提交
6 7 8 9 10 11 12 13 14 15
 * This driver is a replacement of older and less maintained version.
 * This is a header of the older version:
 *	-----<snip>-----
 *	Copyright 2001 MontaVista Software Inc.
 *	Author: MontaVista Software, Inc.
 *		ahennessy@mvista.com
 *	Copyright (C) 2000-2001 Toshiba Corporation
 *	static const char *version =
 *		"tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n";
 *	-----<snip>-----
L
Linus Torvalds 已提交
16
 *
A
Atsushi Nemoto 已提交
17 18 19
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
L
Linus Torvalds 已提交
20
 *
A
Atsushi Nemoto 已提交
21 22
 * (C) Copyright TOSHIBA CORPORATION 2004-2005
 * All Rights Reserved.
L
Linus Torvalds 已提交
23 24
 */

A
Atsushi Nemoto 已提交
25
#ifdef TC35815_NAPI
26
#define DRV_VERSION	"1.36-NAPI"
A
Atsushi Nemoto 已提交
27
#else
28
#define DRV_VERSION	"1.36"
A
Atsushi Nemoto 已提交
29 30 31
#endif
static const char *version = "tc35815.c:v" DRV_VERSION "\n";
#define MODNAME			"tc35815"
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/string.h>
A
Atsushi Nemoto 已提交
42
#include <linux/spinlock.h>
L
Linus Torvalds 已提交
43 44 45 46 47 48 49
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/delay.h>
#include <linux/pci.h>
A
Atsushi Nemoto 已提交
50 51
#include <linux/mii.h>
#include <linux/ethtool.h>
52
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
53 54 55 56 57 58
#include <asm/io.h>
#include <asm/byteorder.h>

/* First, a few definitions that the brave might change. */

#define GATHER_TXINT	/* On-Demand Tx Interrupt */
A
Atsushi Nemoto 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#define WORKAROUND_LOSTCAR
#define WORKAROUND_100HALF_PROMISC
/* #define TC35815_USE_PACKEDBUFFER */

typedef enum {
	TC35815CF = 0,
	TC35815_NWU,
	TC35815_TX4939,
} board_t;

/* indexed by board_t, above */
static const struct {
	const char *name;
} board_info[] __devinitdata = {
	{ "TOSHIBA TC35815CF 10/100BaseTX" },
	{ "TOSHIBA TC35815 with Wake on LAN" },
	{ "TOSHIBA TC35815/TX4939" },
};

static const struct pci_device_id tc35815_pci_tbl[] = {
	{PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
	{PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
	{PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
	{0,}
};
MODULE_DEVICE_TABLE (pci, tc35815_pci_tbl);
L
Linus Torvalds 已提交
85

A
Atsushi Nemoto 已提交
86 87 88 89 90 91
/* see MODULE_PARM_DESC */
static struct tc35815_options {
	int speed;
	int duplex;
	int doforce;
} options;
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

/*
 * Registers
 */
struct tc35815_regs {
	volatile __u32 DMA_Ctl;		/* 0x00 */
	volatile __u32 TxFrmPtr;
	volatile __u32 TxThrsh;
	volatile __u32 TxPollCtr;
	volatile __u32 BLFrmPtr;
	volatile __u32 RxFragSize;
	volatile __u32 Int_En;
	volatile __u32 FDA_Bas;
	volatile __u32 FDA_Lim;		/* 0x20 */
	volatile __u32 Int_Src;
	volatile __u32 unused0[2];
	volatile __u32 PauseCnt;
	volatile __u32 RemPauCnt;
	volatile __u32 TxCtlFrmStat;
	volatile __u32 unused1;
	volatile __u32 MAC_Ctl;		/* 0x40 */
	volatile __u32 CAM_Ctl;
	volatile __u32 Tx_Ctl;
	volatile __u32 Tx_Stat;
	volatile __u32 Rx_Ctl;
	volatile __u32 Rx_Stat;
	volatile __u32 MD_Data;
	volatile __u32 MD_CA;
	volatile __u32 CAM_Adr;		/* 0x60 */
	volatile __u32 CAM_Data;
	volatile __u32 CAM_Ena;
	volatile __u32 PROM_Ctl;
	volatile __u32 PROM_Data;
	volatile __u32 Algn_Cnt;
	volatile __u32 CRC_Cnt;
	volatile __u32 Miss_Cnt;
};

/*
 * Bit assignments
 */
/* DMA_Ctl bit asign ------------------------------------------------------- */
A
Atsushi Nemoto 已提交
134 135 136 137 138
#define DMA_RxAlign            0x00c00000 /* 1:Reception Alignment           */
#define DMA_RxAlign_1          0x00400000
#define DMA_RxAlign_2          0x00800000
#define DMA_RxAlign_3          0x00c00000
#define DMA_M66EnStat          0x00080000 /* 1:66MHz Enable State            */
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
#define DMA_IntMask            0x00040000 /* 1:Interupt mask                 */
#define DMA_SWIntReq           0x00020000 /* 1:Software Interrupt request    */
#define DMA_TxWakeUp           0x00010000 /* 1:Transmit Wake Up              */
#define DMA_RxBigE             0x00008000 /* 1:Receive Big Endian            */
#define DMA_TxBigE             0x00004000 /* 1:Transmit Big Endian           */
#define DMA_TestMode           0x00002000 /* 1:Test Mode                     */
#define DMA_PowrMgmnt          0x00001000 /* 1:Power Management              */
#define DMA_DmBurst_Mask       0x000001fc /* DMA Burst size                  */

/* RxFragSize bit asign ---------------------------------------------------- */
#define RxFrag_EnPack          0x00008000 /* 1:Enable Packing                */
#define RxFrag_MinFragMask     0x00000ffc /* Minimum Fragment                */

/* MAC_Ctl bit asign ------------------------------------------------------- */
#define MAC_Link10             0x00008000 /* 1:Link Status 10Mbits           */
#define MAC_EnMissRoll         0x00002000 /* 1:Enable Missed Roll            */
#define MAC_MissRoll           0x00000400 /* 1:Missed Roll                   */
#define MAC_Loop10             0x00000080 /* 1:Loop 10 Mbps                  */
#define MAC_Conn_Auto          0x00000000 /*00:Connection mode (Automatic)   */
#define MAC_Conn_10M           0x00000020 /*01:                (10Mbps endec)*/
#define MAC_Conn_Mll           0x00000040 /*10:                (Mll clock)   */
#define MAC_MacLoop            0x00000010 /* 1:MAC Loopback                  */
#define MAC_FullDup            0x00000008 /* 1:Full Duplex 0:Half Duplex     */
#define MAC_Reset              0x00000004 /* 1:Software Reset                */
#define MAC_HaltImm            0x00000002 /* 1:Halt Immediate                */
#define MAC_HaltReq            0x00000001 /* 1:Halt request                  */

/* PROM_Ctl bit asign ------------------------------------------------------ */
#define PROM_Busy              0x00008000 /* 1:Busy (Start Operation)        */
#define PROM_Read              0x00004000 /*10:Read operation                */
#define PROM_Write             0x00002000 /*01:Write operation               */
#define PROM_Erase             0x00006000 /*11:Erase operation               */
                                          /*00:Enable or Disable Writting,   */
                                          /*      as specified in PROM_Addr. */
#define PROM_Addr_Ena          0x00000030 /*11xxxx:PROM Write enable         */
                                          /*00xxxx:           disable        */

/* CAM_Ctl bit asign ------------------------------------------------------- */
#define CAM_CompEn             0x00000010 /* 1:CAM Compare Enable            */
#define CAM_NegCAM             0x00000008 /* 1:Reject packets CAM recognizes,*/
                                          /*                    accept other */
#define CAM_BroadAcc           0x00000004 /* 1:Broadcast assept              */
#define CAM_GroupAcc           0x00000002 /* 1:Multicast assept              */
#define CAM_StationAcc         0x00000001 /* 1:unicast accept                */

/* CAM_Ena bit asign ------------------------------------------------------- */
#define CAM_ENTRY_MAX                  21   /* CAM Data entry max count      */
#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits)  */
#define CAM_Ena_Bit(index)         (1<<(index))
#define CAM_ENTRY_DESTINATION	0
#define CAM_ENTRY_SOURCE	1
#define CAM_ENTRY_MACCTL	20

/* Tx_Ctl bit asign -------------------------------------------------------- */
#define Tx_En                  0x00000001 /* 1:Transmit enable               */
#define Tx_TxHalt              0x00000002 /* 1:Transmit Halt Request         */
#define Tx_NoPad               0x00000004 /* 1:Suppress Padding              */
#define Tx_NoCRC               0x00000008 /* 1:Suppress Padding              */
#define Tx_FBack               0x00000010 /* 1:Fast Back-off                 */
#define Tx_EnUnder             0x00000100 /* 1:Enable Underrun               */
#define Tx_EnExDefer           0x00000200 /* 1:Enable Excessive Deferral     */
#define Tx_EnLCarr             0x00000400 /* 1:Enable Lost Carrier           */
#define Tx_EnExColl            0x00000800 /* 1:Enable Excessive Collision    */
#define Tx_EnLateColl          0x00001000 /* 1:Enable Late Collision         */
#define Tx_EnTxPar             0x00002000 /* 1:Enable Transmit Parity        */
#define Tx_EnComp              0x00004000 /* 1:Enable Completion             */

/* Tx_Stat bit asign ------------------------------------------------------- */
#define Tx_TxColl_MASK         0x0000000F /* Tx Collision Count              */
#define Tx_ExColl              0x00000010 /* Excessive Collision             */
#define Tx_TXDefer             0x00000020 /* Transmit Defered                */
#define Tx_Paused              0x00000040 /* Transmit Paused                 */
#define Tx_IntTx               0x00000080 /* Interrupt on Tx                 */
#define Tx_Under               0x00000100 /* Underrun                        */
#define Tx_Defer               0x00000200 /* Deferral                        */
#define Tx_NCarr               0x00000400 /* No Carrier                      */
#define Tx_10Stat              0x00000800 /* 10Mbps Status                   */
#define Tx_LateColl            0x00001000 /* Late Collision                  */
#define Tx_TxPar               0x00002000 /* Tx Parity Error                 */
#define Tx_Comp                0x00004000 /* Completion                      */
#define Tx_Halted              0x00008000 /* Tx Halted                       */
#define Tx_SQErr               0x00010000 /* Signal Quality Error(SQE)       */

/* Rx_Ctl bit asign -------------------------------------------------------- */
#define Rx_EnGood              0x00004000 /* 1:Enable Good                   */
#define Rx_EnRxPar             0x00002000 /* 1:Enable Receive Parity         */
#define Rx_EnLongErr           0x00000800 /* 1:Enable Long Error             */
#define Rx_EnOver              0x00000400 /* 1:Enable OverFlow               */
#define Rx_EnCRCErr            0x00000200 /* 1:Enable CRC Error              */
#define Rx_EnAlign             0x00000100 /* 1:Enable Alignment              */
#define Rx_IgnoreCRC           0x00000040 /* 1:Ignore CRC Value              */
#define Rx_StripCRC            0x00000010 /* 1:Strip CRC Value               */
#define Rx_ShortEn             0x00000008 /* 1:Short Enable                  */
#define Rx_LongEn              0x00000004 /* 1:Long Enable                   */
#define Rx_RxHalt              0x00000002 /* 1:Receive Halt Request          */
#define Rx_RxEn                0x00000001 /* 1:Receive Intrrupt Enable       */

/* Rx_Stat bit asign ------------------------------------------------------- */
#define Rx_Halted              0x00008000 /* Rx Halted                       */
#define Rx_Good                0x00004000 /* Rx Good                         */
#define Rx_RxPar               0x00002000 /* Rx Parity Error                 */
                            /* 0x00001000    not use                         */
#define Rx_LongErr             0x00000800 /* Rx Long Error                   */
#define Rx_Over                0x00000400 /* Rx Overflow                     */
#define Rx_CRCErr              0x00000200 /* Rx CRC Error                    */
#define Rx_Align               0x00000100 /* Rx Alignment Error              */
#define Rx_10Stat              0x00000080 /* Rx 10Mbps Status                */
#define Rx_IntRx               0x00000040 /* Rx Interrupt                    */
#define Rx_CtlRecd             0x00000020 /* Rx Control Receive              */

#define Rx_Stat_Mask           0x0000EFC0 /* Rx All Status Mask              */

/* Int_En bit asign -------------------------------------------------------- */
#define Int_NRAbtEn            0x00000800 /* 1:Non-recoverable Abort Enable  */
#define Int_TxCtlCmpEn         0x00000400 /* 1:Transmit Control Complete Enable */
#define Int_DmParErrEn         0x00000200 /* 1:DMA Parity Error Enable       */
#define Int_DParDEn            0x00000100 /* 1:Data Parity Error Enable      */
#define Int_EarNotEn           0x00000080 /* 1:Early Notify Enable           */
#define Int_DParErrEn          0x00000040 /* 1:Detected Parity Error Enable  */
#define Int_SSysErrEn          0x00000020 /* 1:Signalled System Error Enable */
#define Int_RMasAbtEn          0x00000010 /* 1:Received Master Abort Enable  */
#define Int_RTargAbtEn         0x00000008 /* 1:Received Target Abort Enable  */
#define Int_STargAbtEn         0x00000004 /* 1:Signalled Target Abort Enable */
#define Int_BLExEn             0x00000002 /* 1:Buffer List Exhausted Enable  */
#define Int_FDAExEn            0x00000001 /* 1:Free Descriptor Area          */
                                          /*               Exhausted Enable  */

/* Int_Src bit asign ------------------------------------------------------- */
#define Int_NRabt              0x00004000 /* 1:Non Recoverable error         */
#define Int_DmParErrStat       0x00002000 /* 1:DMA Parity Error & Clear      */
#define Int_BLEx               0x00001000 /* 1:Buffer List Empty & Clear     */
#define Int_FDAEx              0x00000800 /* 1:FDA Empty & Clear             */
#define Int_IntNRAbt           0x00000400 /* 1:Non Recoverable Abort         */
#define	Int_IntCmp             0x00000200 /* 1:MAC control packet complete   */
#define Int_IntExBD            0x00000100 /* 1:Interrupt Extra BD & Clear    */
#define Int_DmParErr           0x00000080 /* 1:DMA Parity Error & Clear      */
#define Int_IntEarNot          0x00000040 /* 1:Receive Data write & Clear    */
#define Int_SWInt              0x00000020 /* 1:Software request & Clear      */
#define Int_IntBLEx            0x00000010 /* 1:Buffer List Empty & Clear     */
#define Int_IntFDAEx           0x00000008 /* 1:FDA Empty & Clear             */
#define Int_IntPCI             0x00000004 /* 1:PCI controller & Clear        */
#define Int_IntMacRx           0x00000002 /* 1:Rx controller & Clear         */
#define Int_IntMacTx           0x00000001 /* 1:Tx controller & Clear         */

/* MD_CA bit asign --------------------------------------------------------- */
#define MD_CA_PreSup           0x00001000 /* 1:Preamble Supress              */
#define MD_CA_Busy             0x00000800 /* 1:Busy (Start Operation)        */
#define MD_CA_Wr               0x00000400 /* 1:Write 0:Read                  */


/*
 * Descriptors
 */

/* Frame descripter */
struct FDesc {
	volatile __u32 FDNext;
	volatile __u32 FDSystem;
	volatile __u32 FDStat;
	volatile __u32 FDCtl;
};

/* Buffer descripter */
struct BDesc {
	volatile __u32 BuffData;
	volatile __u32 BDCtl;
};

#define FD_ALIGN	16

/* Frame Descripter bit asign ---------------------------------------------- */
#define FD_FDLength_MASK       0x0000FFFF /* Length MASK                     */
#define FD_BDCnt_MASK          0x001F0000 /* BD count MASK in FD             */
#define FD_FrmOpt_MASK         0x7C000000 /* Frame option MASK               */
#define FD_FrmOpt_BigEndian    0x40000000 /* Tx/Rx */
#define FD_FrmOpt_IntTx        0x20000000 /* Tx only */
#define FD_FrmOpt_NoCRC        0x10000000 /* Tx only */
#define FD_FrmOpt_NoPadding    0x08000000 /* Tx only */
#define FD_FrmOpt_Packing      0x04000000 /* Rx only */
#define FD_CownsFD             0x80000000 /* FD Controller owner bit         */
#define FD_Next_EOL            0x00000001 /* FD EOL indicator                */
#define FD_BDCnt_SHIFT         16

/* Buffer Descripter bit asign --------------------------------------------- */
#define BD_BuffLength_MASK     0x0000FFFF /* Recieve Data Size               */
#define BD_RxBDID_MASK         0x00FF0000 /* BD ID Number MASK               */
#define BD_RxBDSeqN_MASK       0x7F000000 /* Rx BD Sequence Number           */
#define BD_CownsBD             0x80000000 /* BD Controller owner bit         */
#define BD_RxBDID_SHIFT        16
#define BD_RxBDSeqN_SHIFT      24


/* Some useful constants. */
#undef NO_CHECK_CARRIER	/* Does not check No-Carrier with TP */

#ifdef NO_CHECK_CARRIER
#define TX_CTL_CMD	(Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
A
Atsushi Nemoto 已提交
336 337
	Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
	Tx_En)	/* maybe  0x7b01 */
L
Linus Torvalds 已提交
338 339
#else
#define TX_CTL_CMD	(Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
A
Atsushi Nemoto 已提交
340 341
	Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
	Tx_En)	/* maybe  0x7b01 */
L
Linus Torvalds 已提交
342 343 344 345
#endif
#define RX_CTL_CMD	(Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
	| Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn)	/* maybe 0x6f01 */
#define INT_EN_CMD  (Int_NRAbtEn | \
A
Atsushi Nemoto 已提交
346
	Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
L
Linus Torvalds 已提交
347 348 349
	Int_SSysErrEn  | Int_RMasAbtEn | Int_RTargAbtEn | \
	Int_STargAbtEn | \
	Int_BLExEn  | Int_FDAExEn) /* maybe 0xb7f*/
A
Atsushi Nemoto 已提交
350 351
#define DMA_CTL_CMD	DMA_BURST_SIZE
#define HAVE_DMA_RXALIGN(lp)	likely((lp)->boardtype != TC35815CF)
L
Linus Torvalds 已提交
352 353 354 355

/* Tuning parameters */
#define DMA_BURST_SIZE	32
#define TX_THRESHOLD	1024
A
Atsushi Nemoto 已提交
356 357
#define TX_THRESHOLD_MAX 1536       /* used threshold with packet max byte for low pci transfer ability.*/
#define TX_THRESHOLD_KEEP_LIMIT 10  /* setting threshold max value when overrun error occured this count. */
L
Linus Torvalds 已提交
358

A
Atsushi Nemoto 已提交
359 360
/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
#ifdef TC35815_USE_PACKEDBUFFER
L
Linus Torvalds 已提交
361
#define FD_PAGE_NUM 2
A
Atsushi Nemoto 已提交
362
#define RX_BUF_NUM	8	/* >= 2 */
L
Linus Torvalds 已提交
363 364
#define RX_FD_NUM	250	/* >= 32 */
#define TX_FD_NUM	128
A
Atsushi Nemoto 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
#define RX_BUF_SIZE	PAGE_SIZE
#else /* TC35815_USE_PACKEDBUFFER */
#define FD_PAGE_NUM 4
#define RX_BUF_NUM	128	/* < 256 */
#define RX_FD_NUM	256	/* >= 32 */
#define TX_FD_NUM	128
#if RX_CTL_CMD & Rx_LongEn
#define RX_BUF_SIZE	PAGE_SIZE
#elif RX_CTL_CMD & Rx_StripCRC
#define RX_BUF_SIZE	ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */
#else
#define RX_BUF_SIZE	ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */
#endif
#endif /* TC35815_USE_PACKEDBUFFER */
#define RX_FD_RESERVE	(2 / 2)	/* max 2 BD per RxFD */
#define NAPI_WEIGHT	16
L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394

struct TxFD {
	struct FDesc fd;
	struct BDesc bd;
	struct BDesc unused;
};

struct RxFD {
	struct FDesc fd;
	struct BDesc bd[0];	/* variable length */
};

struct FrFD {
	struct FDesc fd;
A
Atsushi Nemoto 已提交
395
	struct BDesc bd[RX_BUF_NUM];
L
Linus Torvalds 已提交
396 397 398
};


A
Atsushi Nemoto 已提交
399 400
#define tc_readl(addr)	readl(addr)
#define tc_writel(d, addr)	writel(d, addr)
L
Linus Torvalds 已提交
401

A
Atsushi Nemoto 已提交
402 403 404 405 406 407 408 409 410 411
#define TC35815_TX_TIMEOUT  msecs_to_jiffies(400)

/* Timer state engine. */
enum tc35815_timer_state {
	arbwait  = 0,	/* Waiting for auto negotiation to complete.          */
	lupwait  = 1,	/* Auto-neg complete, awaiting link-up status.        */
	ltrywait = 2,	/* Forcing try of all modes, from fastest to slowest. */
	asleep   = 3,	/* Time inactive.                                     */
	lcheck   = 4,	/* Check link status.                                 */
};
L
Linus Torvalds 已提交
412 413 414

/* Information that need to be kept for each board. */
struct tc35815_local {
A
Atsushi Nemoto 已提交
415
	struct pci_dev *pci_dev;
L
Linus Torvalds 已提交
416

417 418 419
	struct net_device *dev;
	struct napi_struct napi;

L
Linus Torvalds 已提交
420 421 422 423 424
	/* statistics */
	struct {
		int max_tx_qlen;
		int tx_ints;
		int rx_ints;
A
Atsushi Nemoto 已提交
425
	        int tx_underrun;
L
Linus Torvalds 已提交
426 427
	} lstats;

A
Atsushi Nemoto 已提交
428 429 430 431 432 433 434 435
	/* Tx control lock.  This protects the transmit buffer ring
	 * state along with the "tx full" state of the driver.  This
	 * means all netif_queue flow control actions are protected
	 * by this lock as well.
	 */
	spinlock_t lock;

	int phy_addr;
L
Linus Torvalds 已提交
436
	int fullduplex;
A
Atsushi Nemoto 已提交
437 438 439 440
	unsigned short saved_lpa;
	struct timer_list timer;
	enum tc35815_timer_state timer_state; /* State of auto-neg timer. */
	unsigned int timer_ticks;	/* Number of clicks at each state  */
L
Linus Torvalds 已提交
441 442 443 444

	/*
	 * Transmitting: Batch Mode.
	 *	1 BD in 1 TxFD.
A
Atsushi Nemoto 已提交
445
	 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
L
Linus Torvalds 已提交
446
	 *	1 circular FD for Free Buffer List.
A
Atsushi Nemoto 已提交
447
	 *	RX_BUF_NUM BD in Free Buffer FD.
L
Linus Torvalds 已提交
448
	 *	One Free Buffer BD has PAGE_SIZE data buffer.
A
Atsushi Nemoto 已提交
449 450 451 452
	 * Or Non-Packing Mode.
	 *	1 circular FD for Free Buffer List.
	 *	RX_BUF_NUM BD in Free Buffer FD.
	 *	One Free Buffer BD has ETH_FRAME_LEN data buffer.
L
Linus Torvalds 已提交
453
	 */
A
Atsushi Nemoto 已提交
454 455
	void * fd_buf;	/* for TxFD, RxFD, FrFD */
	dma_addr_t fd_buf_dma;
L
Linus Torvalds 已提交
456
	struct TxFD *tfd_base;
A
Atsushi Nemoto 已提交
457 458
	unsigned int tfd_start;
	unsigned int tfd_end;
L
Linus Torvalds 已提交
459 460 461 462
	struct RxFD *rfd_base;
	struct RxFD *rfd_limit;
	struct RxFD *rfd_cur;
	struct FrFD *fbl_ptr;
A
Atsushi Nemoto 已提交
463
#ifdef TC35815_USE_PACKEDBUFFER
L
Linus Torvalds 已提交
464
	unsigned char fbl_curid;
A
Atsushi Nemoto 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	void * data_buf[RX_BUF_NUM];		/* packing */
	dma_addr_t data_buf_dma[RX_BUF_NUM];
	struct {
		struct sk_buff *skb;
		dma_addr_t skb_dma;
	} tx_skbs[TX_FD_NUM];
#else
	unsigned int fbl_count;
	struct {
		struct sk_buff *skb;
		dma_addr_t skb_dma;
	} tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
#endif
	struct mii_if_info mii;
	unsigned short mii_id[2];
	u32 msg_enable;
	board_t boardtype;
L
Linus Torvalds 已提交
482 483
};

A
Atsushi Nemoto 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
{
	return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
}
#ifdef DEBUG
static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
{
	return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
}
#endif
#ifdef TC35815_USE_PACKEDBUFFER
static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
{
	int i;
	for (i = 0; i < RX_BUF_NUM; i++) {
		if (bus >= lp->data_buf_dma[i] &&
		    bus < lp->data_buf_dma[i] + PAGE_SIZE)
			return (void *)((u8 *)lp->data_buf[i] +
					(bus - lp->data_buf_dma[i]));
	}
	return NULL;
}

#define TC35815_DMA_SYNC_ONDEMAND
static void* alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
{
#ifdef TC35815_DMA_SYNC_ONDEMAND
	void *buf;
	/* pci_map + pci_dma_sync will be more effective than
	 * pci_alloc_consistent on some archs. */
	if ((buf = (void *)__get_free_page(GFP_ATOMIC)) == NULL)
		return NULL;
	*dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
				     PCI_DMA_FROMDEVICE);
	if (pci_dma_mapping_error(*dma_handle)) {
		free_page((unsigned long)buf);
		return NULL;
	}
	return buf;
#else
	return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
#endif
}

static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
{
#ifdef TC35815_DMA_SYNC_ONDEMAND
	pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
	free_page((unsigned long)buf);
#else
	pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
#endif
}
#else /* TC35815_USE_PACKEDBUFFER */
static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
				       struct pci_dev *hwdev,
				       dma_addr_t *dma_handle)
{
	struct sk_buff *skb;
	skb = dev_alloc_skb(RX_BUF_SIZE);
	if (!skb)
		return NULL;
	*dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
				     PCI_DMA_FROMDEVICE);
	if (pci_dma_mapping_error(*dma_handle)) {
		dev_kfree_skb_any(skb);
		return NULL;
	}
	skb_reserve(skb, 2);	/* make IP header 4byte aligned */
	return skb;
}

static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
{
	pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
			 PCI_DMA_FROMDEVICE);
	dev_kfree_skb_any(skb);
}
#endif /* TC35815_USE_PACKEDBUFFER */
L
Linus Torvalds 已提交
563

A
Atsushi Nemoto 已提交
564
/* Index to functions, as function prototypes. */
L
Linus Torvalds 已提交
565 566 567

static int	tc35815_open(struct net_device *dev);
static int	tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
A
Atsushi Nemoto 已提交
568 569 570
static irqreturn_t	tc35815_interrupt(int irq, void *dev_id);
#ifdef TC35815_NAPI
static int	tc35815_rx(struct net_device *dev, int limit);
571
static int	tc35815_poll(struct napi_struct *napi, int budget);
A
Atsushi Nemoto 已提交
572
#else
L
Linus Torvalds 已提交
573
static void	tc35815_rx(struct net_device *dev);
A
Atsushi Nemoto 已提交
574
#endif
L
Linus Torvalds 已提交
575 576 577 578
static void	tc35815_txdone(struct net_device *dev);
static int	tc35815_close(struct net_device *dev);
static struct	net_device_stats *tc35815_get_stats(struct net_device *dev);
static void	tc35815_set_multicast_list(struct net_device *dev);
A
Atsushi Nemoto 已提交
579 580 581 582 583 584
static void     tc35815_tx_timeout(struct net_device *dev);
static int	tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
#ifdef CONFIG_NET_POLL_CONTROLLER
static void	tc35815_poll_controller(struct net_device *dev);
#endif
static const struct ethtool_ops tc35815_ethtool_ops;
L
Linus Torvalds 已提交
585

A
Atsushi Nemoto 已提交
586
/* Example routines you must write ;->. */
L
Linus Torvalds 已提交
587 588
static void 	tc35815_chip_reset(struct net_device *dev);
static void 	tc35815_chip_init(struct net_device *dev);
A
Atsushi Nemoto 已提交
589
static void	tc35815_find_phy(struct net_device *dev);
L
Linus Torvalds 已提交
590 591
static void 	tc35815_phy_chip_init(struct net_device *dev);

A
Atsushi Nemoto 已提交
592 593 594
#ifdef DEBUG
static void	panic_queues(struct net_device *dev);
#endif
L
Linus Torvalds 已提交
595

A
Atsushi Nemoto 已提交
596 597 598 599 600 601
static void tc35815_timer(unsigned long data);
static void tc35815_start_auto_negotiation(struct net_device *dev,
					   struct ethtool_cmd *ep);
static int tc_mdio_read(struct net_device *dev, int phy_id, int location);
static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
			  int val);
L
Linus Torvalds 已提交
602

603 604 605 606 607 608 609 610 611 612
#ifdef CONFIG_CPU_TX49XX
/*
 * Find a platform_device providing a MAC address.  The platform code
 * should provide a "tc35815-mac" device with a MAC address in its
 * platform_data.
 */
static int __devinit tc35815_mac_match(struct device *dev, void *data)
{
	struct platform_device *plat_dev = to_platform_device(dev);
	struct pci_dev *pci_dev = data;
613
	unsigned int id = pci_dev->irq;
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
}

static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	struct device *pd = bus_find_device(&platform_bus_type, NULL,
					    lp->pci_dev, tc35815_mac_match);
	if (pd) {
		if (pd->platform_data)
			memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
		put_device(pd);
		return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
	}
	return -ENODEV;
}
#else
631
static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
632 633 634 635 636 637
{
	return -ENODEV;
}
#endif

static int __devinit tc35815_init_dev_addr (struct net_device *dev)
A
Atsushi Nemoto 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
{
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	int i;

	while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
		;
	for (i = 0; i < 6; i += 2) {
		unsigned short data;
		tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
		while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
			;
		data = tc_readl(&tr->PROM_Data);
		dev->dev_addr[i] = data & 0xff;
		dev->dev_addr[i+1] = data >> 8;
	}
654 655 656
	if (!is_valid_ether_addr(dev->dev_addr))
		return tc35815_read_plat_dev_addr(dev);
	return 0;
A
Atsushi Nemoto 已提交
657
}
L
Linus Torvalds 已提交
658

A
Atsushi Nemoto 已提交
659 660
static int __devinit tc35815_init_one (struct pci_dev *pdev,
				       const struct pci_device_id *ent)
L
Linus Torvalds 已提交
661
{
A
Atsushi Nemoto 已提交
662 663 664 665 666
	void __iomem *ioaddr = NULL;
	struct net_device *dev;
	struct tc35815_local *lp;
	int rc;
	unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
A
Atsushi Nemoto 已提交
667
	DECLARE_MAC_BUF(mac);
A
Atsushi Nemoto 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680

	static int printed_version;
	if (!printed_version++) {
		printk(version);
		dev_printk(KERN_DEBUG, &pdev->dev,
			   "speed:%d duplex:%d doforce:%d\n",
			   options.speed, options.duplex, options.doforce);
	}

	if (!pdev->irq) {
		dev_warn(&pdev->dev, "no IRQ assigned.\n");
		return -ENODEV;
	}
L
Linus Torvalds 已提交
681

A
Atsushi Nemoto 已提交
682 683 684 685 686 687 688 689
	/* dev zeroed in alloc_etherdev */
	dev = alloc_etherdev (sizeof (*lp));
	if (dev == NULL) {
		dev_err(&pdev->dev, "unable to alloc new ethernet\n");
		return -ENOMEM;
	}
	SET_NETDEV_DEV(dev, &pdev->dev);
	lp = dev->priv;
690
	lp->dev = dev;
L
Linus Torvalds 已提交
691

A
Atsushi Nemoto 已提交
692 693 694 695
	/* enable device (incl. PCI PM wakeup), and bus-mastering */
	rc = pci_enable_device (pdev);
	if (rc)
		goto err_out;
L
Linus Torvalds 已提交
696

A
Atsushi Nemoto 已提交
697 698 699 700
	mmio_start = pci_resource_start (pdev, 1);
	mmio_end = pci_resource_end (pdev, 1);
	mmio_flags = pci_resource_flags (pdev, 1);
	mmio_len = pci_resource_len (pdev, 1);
L
Linus Torvalds 已提交
701

A
Atsushi Nemoto 已提交
702 703
	/* set this immediately, we need to know before
	 * we talk to the chip directly */
L
Linus Torvalds 已提交
704

A
Atsushi Nemoto 已提交
705 706 707 708
	/* make sure PCI base addr 1 is MMIO */
	if (!(mmio_flags & IORESOURCE_MEM)) {
		dev_err(&pdev->dev, "region #1 not an MMIO resource, aborting\n");
		rc = -ENODEV;
L
Linus Torvalds 已提交
709 710
		goto err_out;
	}
A
Atsushi Nemoto 已提交
711 712 713 714 715

	/* check for weird/broken PCI region reporting */
	if ((mmio_len < sizeof(struct tc35815_regs))) {
		dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
		rc = -ENODEV;
L
Linus Torvalds 已提交
716 717 718
		goto err_out;
	}

A
Atsushi Nemoto 已提交
719 720
	rc = pci_request_regions (pdev, MODNAME);
	if (rc)
L
Linus Torvalds 已提交
721 722
		goto err_out;

A
Atsushi Nemoto 已提交
723
	pci_set_master (pdev);
L
Linus Torvalds 已提交
724

A
Atsushi Nemoto 已提交
725 726 727 728 729 730 731
	/* ioremap MMIO region */
	ioaddr = ioremap (mmio_start, mmio_len);
	if (ioaddr == NULL) {
		dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
		rc = -EIO;
		goto err_out_free_res;
	}
L
Linus Torvalds 已提交
732

A
Atsushi Nemoto 已提交
733 734 735 736 737 738 739 740 741 742 743
	/* Initialize the device structure. */
	dev->open = tc35815_open;
	dev->hard_start_xmit = tc35815_send_packet;
	dev->stop = tc35815_close;
	dev->get_stats = tc35815_get_stats;
	dev->set_multicast_list = tc35815_set_multicast_list;
	dev->do_ioctl = tc35815_ioctl;
	dev->ethtool_ops = &tc35815_ethtool_ops;
	dev->tx_timeout = tc35815_tx_timeout;
	dev->watchdog_timeo = TC35815_TX_TIMEOUT;
#ifdef TC35815_NAPI
744
	netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
A
Atsushi Nemoto 已提交
745 746 747 748
#endif
#ifdef CONFIG_NET_POLL_CONTROLLER
	dev->poll_controller = tc35815_poll_controller;
#endif
L
Linus Torvalds 已提交
749

A
Atsushi Nemoto 已提交
750 751
	dev->irq = pdev->irq;
	dev->base_addr = (unsigned long) ioaddr;
L
Linus Torvalds 已提交
752

A
Atsushi Nemoto 已提交
753 754 755
	spin_lock_init(&lp->lock);
	lp->pci_dev = pdev;
	lp->boardtype = ent->driver_data;
L
Linus Torvalds 已提交
756

A
Atsushi Nemoto 已提交
757 758
	lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
	pci_set_drvdata(pdev, dev);
L
Linus Torvalds 已提交
759

A
Atsushi Nemoto 已提交
760
	/* Soft reset the chip. */
L
Linus Torvalds 已提交
761 762
	tc35815_chip_reset(dev);

A
Atsushi Nemoto 已提交
763
	/* Retrieve the ethernet address. */
764 765 766 767
	if (tc35815_init_dev_addr(dev)) {
		dev_warn(&pdev->dev, "not valid ether addr\n");
		random_ether_addr(dev->dev_addr);
	}
A
Atsushi Nemoto 已提交
768 769 770 771 772 773

	rc = register_netdev (dev);
	if (rc)
		goto err_out_unmap;

	memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
A
Atsushi Nemoto 已提交
774
	printk(KERN_INFO "%s: %s at 0x%lx, %s, IRQ %d\n",
A
Atsushi Nemoto 已提交
775 776 777
		dev->name,
		board_info[ent->driver_data].name,
		dev->base_addr,
A
Atsushi Nemoto 已提交
778
		print_mac(mac, dev->dev_addr),
A
Atsushi Nemoto 已提交
779 780 781 782 783 784 785 786 787 788 789 790
		dev->irq);

	setup_timer(&lp->timer, tc35815_timer, (unsigned long) dev);
	lp->mii.dev = dev;
	lp->mii.mdio_read = tc_mdio_read;
	lp->mii.mdio_write = tc_mdio_write;
	lp->mii.phy_id_mask = 0x1f;
	lp->mii.reg_num_mask = 0x1f;
	tc35815_find_phy(dev);
	lp->mii.phy_id = lp->phy_addr;
	lp->mii.full_duplex = 0;
	lp->mii.force_media = 0;
L
Linus Torvalds 已提交
791

A
Atsushi Nemoto 已提交
792
	return 0;
L
Linus Torvalds 已提交
793

A
Atsushi Nemoto 已提交
794 795 796 797 798 799 800 801
err_out_unmap:
	iounmap(ioaddr);
err_out_free_res:
	pci_release_regions (pdev);
err_out:
	free_netdev (dev);
	return rc;
}
L
Linus Torvalds 已提交
802 803


A
Atsushi Nemoto 已提交
804 805 806 807
static void __devexit tc35815_remove_one (struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata (pdev);
	unsigned long mmio_addr;
L
Linus Torvalds 已提交
808

A
Atsushi Nemoto 已提交
809
	mmio_addr = dev->base_addr;
L
Linus Torvalds 已提交
810

A
Atsushi Nemoto 已提交
811
	unregister_netdev (dev);
L
Linus Torvalds 已提交
812

A
Atsushi Nemoto 已提交
813 814 815 816
	if (mmio_addr) {
		iounmap ((void __iomem *)mmio_addr);
		pci_release_regions (pdev);
	}
L
Linus Torvalds 已提交
817

A
Atsushi Nemoto 已提交
818
	free_netdev (dev);
L
Linus Torvalds 已提交
819

A
Atsushi Nemoto 已提交
820
	pci_set_drvdata (pdev, NULL);
L
Linus Torvalds 已提交
821 822 823 824 825 826 827 828 829 830
}

static int
tc35815_init_queues(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int i;
	unsigned long fd_addr;

	if (!lp->fd_buf) {
A
Atsushi Nemoto 已提交
831 832 833 834 835
		BUG_ON(sizeof(struct FDesc) +
		       sizeof(struct BDesc) * RX_BUF_NUM +
		       sizeof(struct FDesc) * RX_FD_NUM +
		       sizeof(struct TxFD) * TX_FD_NUM >
		       PAGE_SIZE * FD_PAGE_NUM);
L
Linus Torvalds 已提交
836

A
Atsushi Nemoto 已提交
837
		if ((lp->fd_buf = pci_alloc_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM, &lp->fd_buf_dma)) == 0)
L
Linus Torvalds 已提交
838
			return -ENOMEM;
A
Atsushi Nemoto 已提交
839 840 841
		for (i = 0; i < RX_BUF_NUM; i++) {
#ifdef TC35815_USE_PACKEDBUFFER
			if ((lp->data_buf[i] = alloc_rxbuf_page(lp->pci_dev, &lp->data_buf_dma[i])) == NULL) {
L
Linus Torvalds 已提交
842
				while (--i >= 0) {
A
Atsushi Nemoto 已提交
843 844 845 846
					free_rxbuf_page(lp->pci_dev,
							lp->data_buf[i],
							lp->data_buf_dma[i]);
					lp->data_buf[i] = NULL;
L
Linus Torvalds 已提交
847
				}
A
Atsushi Nemoto 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
				pci_free_consistent(lp->pci_dev,
						    PAGE_SIZE * FD_PAGE_NUM,
						    lp->fd_buf,
						    lp->fd_buf_dma);
				lp->fd_buf = NULL;
				return -ENOMEM;
			}
#else
			lp->rx_skbs[i].skb =
				alloc_rxbuf_skb(dev, lp->pci_dev,
						&lp->rx_skbs[i].skb_dma);
			if (!lp->rx_skbs[i].skb) {
				while (--i >= 0) {
					free_rxbuf_skb(lp->pci_dev,
						       lp->rx_skbs[i].skb,
						       lp->rx_skbs[i].skb_dma);
					lp->rx_skbs[i].skb = NULL;
				}
				pci_free_consistent(lp->pci_dev,
						    PAGE_SIZE * FD_PAGE_NUM,
						    lp->fd_buf,
						    lp->fd_buf_dma);
				lp->fd_buf = NULL;
L
Linus Torvalds 已提交
871 872 873 874
				return -ENOMEM;
			}
#endif
		}
A
Atsushi Nemoto 已提交
875 876 877 878 879 880
		printk(KERN_DEBUG "%s: FD buf %p DataBuf",
		       dev->name, lp->fd_buf);
#ifdef TC35815_USE_PACKEDBUFFER
		printk(" DataBuf");
		for (i = 0; i < RX_BUF_NUM; i++)
			printk(" %p", lp->data_buf[i]);
L
Linus Torvalds 已提交
881
#endif
A
Atsushi Nemoto 已提交
882
		printk("\n");
L
Linus Torvalds 已提交
883
	} else {
A
Atsushi Nemoto 已提交
884 885 886
		for (i = 0; i < FD_PAGE_NUM; i++) {
			clear_page((void *)((unsigned long)lp->fd_buf + i * PAGE_SIZE));
		}
L
Linus Torvalds 已提交
887 888 889 890 891 892 893 894 895 896
	}
	fd_addr = (unsigned long)lp->fd_buf;

	/* Free Descriptors (for Receive) */
	lp->rfd_base = (struct RxFD *)fd_addr;
	fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
	for (i = 0; i < RX_FD_NUM; i++) {
		lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
	}
	lp->rfd_cur = lp->rfd_base;
A
Atsushi Nemoto 已提交
897
	lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
L
Linus Torvalds 已提交
898 899 900 901 902

	/* Transmit Descriptors */
	lp->tfd_base = (struct TxFD *)fd_addr;
	fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
	for (i = 0; i < TX_FD_NUM; i++) {
A
Atsushi Nemoto 已提交
903 904
		lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
		lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
L
Linus Torvalds 已提交
905 906
		lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
	}
A
Atsushi Nemoto 已提交
907
	lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0]));
L
Linus Torvalds 已提交
908 909 910 911 912
	lp->tfd_start = 0;
	lp->tfd_end = 0;

	/* Buffer List (for Receive) */
	lp->fbl_ptr = (struct FrFD *)fd_addr;
A
Atsushi Nemoto 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
	lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
#ifndef TC35815_USE_PACKEDBUFFER
	/*
	 * move all allocated skbs to head of rx_skbs[] array.
	 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
	 * tc35815_rx() had failed.
	 */
	lp->fbl_count = 0;
	for (i = 0; i < RX_BUF_NUM; i++) {
		if (lp->rx_skbs[i].skb) {
			if (i != lp->fbl_count) {
				lp->rx_skbs[lp->fbl_count].skb =
					lp->rx_skbs[i].skb;
				lp->rx_skbs[lp->fbl_count].skb_dma =
					lp->rx_skbs[i].skb_dma;
			}
			lp->fbl_count++;
		}
	}
#endif
	for (i = 0; i < RX_BUF_NUM; i++) {
#ifdef TC35815_USE_PACKEDBUFFER
		lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
#else
		if (i >= lp->fbl_count) {
			lp->fbl_ptr->bd[i].BuffData = 0;
			lp->fbl_ptr->bd[i].BDCtl = 0;
			continue;
		}
		lp->fbl_ptr->bd[i].BuffData =
			cpu_to_le32(lp->rx_skbs[i].skb_dma);
#endif
L
Linus Torvalds 已提交
946 947
		/* BDID is index of FrFD.bd[] */
		lp->fbl_ptr->bd[i].BDCtl =
A
Atsushi Nemoto 已提交
948 949
			cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
				    RX_BUF_SIZE);
L
Linus Torvalds 已提交
950
	}
A
Atsushi Nemoto 已提交
951
#ifdef TC35815_USE_PACKEDBUFFER
L
Linus Torvalds 已提交
952
	lp->fbl_curid = 0;
A
Atsushi Nemoto 已提交
953
#endif
L
Linus Torvalds 已提交
954

A
Atsushi Nemoto 已提交
955 956
	printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
	       dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
L
Linus Torvalds 已提交
957 958 959 960 961 962 963 964 965 966
	return 0;
}

static void
tc35815_clear_queues(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int i;

	for (i = 0; i < TX_FD_NUM; i++) {
A
Atsushi Nemoto 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
		u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
		struct sk_buff *skb =
			fdsystem != 0xffffffff ?
			lp->tx_skbs[fdsystem].skb : NULL;
#ifdef DEBUG
		if (lp->tx_skbs[i].skb != skb) {
			printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
			panic_queues(dev);
		}
#else
		BUG_ON(lp->tx_skbs[i].skb != skb);
#endif
		if (skb) {
			pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
			lp->tx_skbs[i].skb = NULL;
			lp->tx_skbs[i].skb_dma = 0;
L
Linus Torvalds 已提交
983
			dev_kfree_skb_any(skb);
A
Atsushi Nemoto 已提交
984 985
		}
		lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
L
Linus Torvalds 已提交
986 987 988 989 990 991 992 993 994 995 996 997 998
	}

	tc35815_init_queues(dev);
}

static void
tc35815_free_queues(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int i;

	if (lp->tfd_base) {
		for (i = 0; i < TX_FD_NUM; i++) {
A
Atsushi Nemoto 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
			u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
			struct sk_buff *skb =
				fdsystem != 0xffffffff ?
				lp->tx_skbs[fdsystem].skb : NULL;
#ifdef DEBUG
			if (lp->tx_skbs[i].skb != skb) {
				printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
				panic_queues(dev);
			}
#else
			BUG_ON(lp->tx_skbs[i].skb != skb);
#endif
			if (skb) {
				dev_kfree_skb(skb);
				pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
				lp->tx_skbs[i].skb = NULL;
				lp->tx_skbs[i].skb_dma = 0;
			}
			lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022 1023 1024 1025
		}
	}

	lp->rfd_base = NULL;
	lp->rfd_limit = NULL;
	lp->rfd_cur = NULL;
	lp->fbl_ptr = NULL;

A
Atsushi Nemoto 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	for (i = 0; i < RX_BUF_NUM; i++) {
#ifdef TC35815_USE_PACKEDBUFFER
		if (lp->data_buf[i]) {
			free_rxbuf_page(lp->pci_dev,
					lp->data_buf[i], lp->data_buf_dma[i]);
			lp->data_buf[i] = NULL;
		}
#else
		if (lp->rx_skbs[i].skb) {
			free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
				       lp->rx_skbs[i].skb_dma);
			lp->rx_skbs[i].skb = NULL;
		}
#endif
	}
	if (lp->fd_buf) {
		pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
				    lp->fd_buf, lp->fd_buf_dma);
		lp->fd_buf = NULL;
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	}
}

static void
dump_txfd(struct TxFD *fd)
{
	printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
	       le32_to_cpu(fd->fd.FDNext),
	       le32_to_cpu(fd->fd.FDSystem),
	       le32_to_cpu(fd->fd.FDStat),
	       le32_to_cpu(fd->fd.FDCtl));
	printk("BD: ");
	printk(" %08x %08x",
	       le32_to_cpu(fd->bd.BuffData),
	       le32_to_cpu(fd->bd.BDCtl));
	printk("\n");
}

static int
dump_rxfd(struct RxFD *fd)
{
	int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
	if (bd_count > 8)
		bd_count = 8;
	printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
	       le32_to_cpu(fd->fd.FDNext),
	       le32_to_cpu(fd->fd.FDSystem),
	       le32_to_cpu(fd->fd.FDStat),
	       le32_to_cpu(fd->fd.FDCtl));
	if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
	    return 0;
	printk("BD: ");
	for (i = 0; i < bd_count; i++)
		printk(" %08x %08x",
		       le32_to_cpu(fd->bd[i].BuffData),
		       le32_to_cpu(fd->bd[i].BDCtl));
	printk("\n");
	return bd_count;
}

A
Atsushi Nemoto 已提交
1085
#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
static void
dump_frfd(struct FrFD *fd)
{
	int i;
	printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
	       le32_to_cpu(fd->fd.FDNext),
	       le32_to_cpu(fd->fd.FDSystem),
	       le32_to_cpu(fd->fd.FDStat),
	       le32_to_cpu(fd->fd.FDCtl));
	printk("BD: ");
A
Atsushi Nemoto 已提交
1096
	for (i = 0; i < RX_BUF_NUM; i++)
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101
		printk(" %08x %08x",
		       le32_to_cpu(fd->bd[i].BuffData),
		       le32_to_cpu(fd->bd[i].BDCtl));
	printk("\n");
}
A
Atsushi Nemoto 已提交
1102
#endif
L
Linus Torvalds 已提交
1103

A
Atsushi Nemoto 已提交
1104
#ifdef DEBUG
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110
static void
panic_queues(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int i;

A
Atsushi Nemoto 已提交
1111
	printk("TxFD base %p, start %u, end %u\n",
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	       lp->tfd_base, lp->tfd_start, lp->tfd_end);
	printk("RxFD base %p limit %p cur %p\n",
	       lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
	printk("FrFD %p\n", lp->fbl_ptr);
	for (i = 0; i < TX_FD_NUM; i++)
		dump_txfd(&lp->tfd_base[i]);
	for (i = 0; i < RX_FD_NUM; i++) {
		int bd_count = dump_rxfd(&lp->rfd_base[i]);
		i += (bd_count + 1) / 2;	/* skip BDs */
	}
	dump_frfd(lp->fbl_ptr);
	panic("%s: Illegal queue state.", dev->name);
}
#endif

A
Atsushi Nemoto 已提交
1127
static void print_eth(const u8 *add)
L
Linus Torvalds 已提交
1128
{
A
Atsushi Nemoto 已提交
1129
	DECLARE_MAC_BUF(mac);
L
Linus Torvalds 已提交
1130

A
Atsushi Nemoto 已提交
1131 1132 1133 1134
	printk(KERN_DEBUG "print_eth(%p)\n", add);
	printk(KERN_DEBUG " %s =>", print_mac(mac, add + 6));
	printk(KERN_CONT " %s : %02x%02x\n",
		print_mac(mac, add), add[12], add[13]);
L
Linus Torvalds 已提交
1135 1136
}

A
Atsushi Nemoto 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static int tc35815_tx_full(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
}

static void tc35815_restart(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int pid = lp->phy_addr;
	int do_phy_reset = 1;
	del_timer(&lp->timer);		/* Kill if running	*/

	if (lp->mii_id[0] == 0x0016 && (lp->mii_id[1] & 0xfc00) == 0xf800) {
		/* Resetting PHY cause problem on some chip... (SEEQ 80221) */
		do_phy_reset = 0;
	}
	if (do_phy_reset) {
		int timeout;
		tc_mdio_write(dev, pid, MII_BMCR, BMCR_RESET);
		timeout = 100;
		while (--timeout) {
			if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_RESET))
				break;
			udelay(1);
		}
		if (!timeout)
			printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
	}

	tc35815_chip_reset(dev);
	tc35815_clear_queues(dev);
	tc35815_chip_init(dev);
	/* Reconfigure CAM again since tc35815_chip_init() initialize it. */
	tc35815_set_multicast_list(dev);
}

static void tc35815_tx_timeout(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;

	printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
	       dev->name, tc_readl(&tr->Tx_Stat));

	/* Try to restart the adaptor. */
	spin_lock_irq(&lp->lock);
	tc35815_restart(dev);
	spin_unlock_irq(&lp->lock);

A
Atsushi Nemoto 已提交
1188
	dev->stats.tx_errors++;
A
Atsushi Nemoto 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203

	/* If we have space available to accept new transmit
	 * requests, wake up the queueing layer.  This would
	 * be the case if the chipset_init() call above just
	 * flushes out the tx queue and empties it.
	 *
	 * If instead, the tx queue is retained then the
	 * netif_wake_queue() call should be placed in the
	 * TX completion interrupt handler of the driver instead
	 * of here.
	 */
	if (!tc35815_tx_full(dev))
		netif_wake_queue(dev);
}

L
Linus Torvalds 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
/*
 * Open/initialize the board. This is called (in the current kernel)
 * sometime after booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */
static int
tc35815_open(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
A
Atsushi Nemoto 已提交
1216

L
Linus Torvalds 已提交
1217 1218 1219 1220
	/*
	 * This is used if the interrupt line can turned off (shared).
	 * See 3c503.c for an example of selecting the IRQ at config-time.
	 */
A
Atsushi Nemoto 已提交
1221
	if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, dev->name, dev)) {
L
Linus Torvalds 已提交
1222 1223 1224
		return -EAGAIN;
	}

A
Atsushi Nemoto 已提交
1225
	del_timer(&lp->timer);		/* Kill if running	*/
L
Linus Torvalds 已提交
1226 1227 1228 1229 1230 1231 1232
	tc35815_chip_reset(dev);

	if (tc35815_init_queues(dev) != 0) {
		free_irq(dev->irq, dev);
		return -EAGAIN;
	}

1233 1234 1235 1236
#ifdef TC35815_NAPI
	napi_enable(&lp->napi);
#endif

L
Linus Torvalds 已提交
1237
	/* Reset the hardware here. Don't forget to set the station address. */
A
Atsushi Nemoto 已提交
1238
	spin_lock_irq(&lp->lock);
L
Linus Torvalds 已提交
1239
	tc35815_chip_init(dev);
A
Atsushi Nemoto 已提交
1240
	spin_unlock_irq(&lp->lock);
L
Linus Torvalds 已提交
1241

A
Atsushi Nemoto 已提交
1242 1243 1244
	/* We are now ready to accept transmit requeusts from
	 * the queueing layer of the networking.
	 */
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249
	netif_start_queue(dev);

	return 0;
}

A
Atsushi Nemoto 已提交
1250 1251 1252 1253 1254 1255
/* This will only be invoked if your driver is _not_ in XOFF state.
 * What this means is that you need not check it, and that this
 * invariant will hold if you make sure that the netif_*_queue()
 * calls are done at the proper times.
 */
static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
1256 1257
{
	struct tc35815_local *lp = dev->priv;
A
Atsushi Nemoto 已提交
1258
	struct TxFD *txfd;
L
Linus Torvalds 已提交
1259 1260
	unsigned long flags;

A
Atsushi Nemoto 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
	/* If some error occurs while trying to transmit this
	 * packet, you should return '1' from this function.
	 * In such a case you _may not_ do anything to the
	 * SKB, it is still owned by the network queueing
	 * layer when an error is returned.  This means you
	 * may not modify any SKB fields, you may not free
	 * the SKB, etc.
	 */

	/* This is the most common case for modern hardware.
	 * The spinlock protects this code from the TX complete
	 * hardware interrupt handler.  Queue flow control is
	 * thus managed under this lock as well.
	 */
L
Linus Torvalds 已提交
1275 1276
	spin_lock_irqsave(&lp->lock, flags);

A
Atsushi Nemoto 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	/* failsafe... (handle txdone now if half of FDs are used) */
	if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
	    TX_FD_NUM / 2)
		tc35815_txdone(dev);

	if (netif_msg_pktdata(lp))
		print_eth(skb->data);
#ifdef DEBUG
	if (lp->tx_skbs[lp->tfd_start].skb) {
		printk("%s: tx_skbs conflict.\n", dev->name);
		panic_queues(dev);
L
Linus Torvalds 已提交
1288
	}
A
Atsushi Nemoto 已提交
1289 1290
#else
	BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
L
Linus Torvalds 已提交
1291
#endif
A
Atsushi Nemoto 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	lp->tx_skbs[lp->tfd_start].skb = skb;
	lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);

	/*add to ring */
	txfd = &lp->tfd_base[lp->tfd_start];
	txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
	txfd->bd.BDCtl = cpu_to_le32(skb->len);
	txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
	txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));

	if (lp->tfd_start == lp->tfd_end) {
		struct tc35815_regs __iomem *tr =
			(struct tc35815_regs __iomem *)dev->base_addr;
		/* Start DMA Transmitter. */
		txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
L
Linus Torvalds 已提交
1307
#ifdef GATHER_TXINT
A
Atsushi Nemoto 已提交
1308
		txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
L
Linus Torvalds 已提交
1309
#endif
A
Atsushi Nemoto 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
		if (netif_msg_tx_queued(lp)) {
			printk("%s: starting TxFD.\n", dev->name);
			dump_txfd(txfd);
		}
		tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
	} else {
		txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
		if (netif_msg_tx_queued(lp)) {
			printk("%s: queueing TxFD.\n", dev->name);
			dump_txfd(txfd);
L
Linus Torvalds 已提交
1320
		}
A
Atsushi Nemoto 已提交
1321 1322
	}
	lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
L
Linus Torvalds 已提交
1323

A
Atsushi Nemoto 已提交
1324
	dev->trans_start = jiffies;
L
Linus Torvalds 已提交
1325

A
Atsushi Nemoto 已提交
1326 1327 1328 1329 1330 1331 1332 1333
	/* If we just used up the very last entry in the
	 * TX ring on this device, tell the queueing
	 * layer to send no more.
	 */
	if (tc35815_tx_full(dev)) {
		if (netif_msg_tx_queued(lp))
			printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
		netif_stop_queue(dev);
L
Linus Torvalds 已提交
1334 1335
	}

A
Atsushi Nemoto 已提交
1336 1337 1338 1339 1340
	/* When the TX completion hw interrupt arrives, this
	 * is when the transmit statistics are updated.
	 */

	spin_unlock_irqrestore(&lp->lock, flags);
L
Linus Torvalds 已提交
1341 1342 1343 1344 1345
	return 0;
}

#define FATAL_ERROR_INT \
	(Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
A
Atsushi Nemoto 已提交
1346
static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
{
	static int count;
	printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
	       dev->name, status);
	if (status & Int_IntPCI)
		printk(" IntPCI");
	if (status & Int_DmParErr)
		printk(" DmParErr");
	if (status & Int_IntNRAbt)
		printk(" IntNRAbt");
	printk("\n");
	if (count++ > 100)
		panic("%s: Too many fatal errors.", dev->name);
A
Atsushi Nemoto 已提交
1360
	printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
L
Linus Torvalds 已提交
1361
	/* Try to restart the adaptor. */
A
Atsushi Nemoto 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
	tc35815_restart(dev);
}

#ifdef TC35815_NAPI
static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
#else
static int tc35815_do_interrupt(struct net_device *dev, u32 status)
#endif
{
	struct tc35815_local *lp = dev->priv;
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	int ret = -1;

	/* Fatal errors... */
	if (status & FATAL_ERROR_INT) {
		tc35815_fatal_error_interrupt(dev, status);
		return 0;
	}
	/* recoverable errors */
	if (status & Int_IntFDAEx) {
		/* disable FDAEx int. (until we make rooms...) */
		tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
		printk(KERN_WARNING
		       "%s: Free Descriptor Area Exhausted (%#x).\n",
		       dev->name, status);
A
Atsushi Nemoto 已提交
1388
		dev->stats.rx_dropped++;
A
Atsushi Nemoto 已提交
1389 1390 1391 1392 1393 1394 1395 1396
		ret = 0;
	}
	if (status & Int_IntBLEx) {
		/* disable BLEx int. (until we make rooms...) */
		tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
		printk(KERN_WARNING
		       "%s: Buffer List Exhausted (%#x).\n",
		       dev->name, status);
A
Atsushi Nemoto 已提交
1397
		dev->stats.rx_dropped++;
A
Atsushi Nemoto 已提交
1398 1399 1400 1401 1402 1403
		ret = 0;
	}
	if (status & Int_IntExBD) {
		printk(KERN_WARNING
		       "%s: Excessive Buffer Descriptiors (%#x).\n",
		       dev->name, status);
A
Atsushi Nemoto 已提交
1404
		dev->stats.rx_length_errors++;
A
Atsushi Nemoto 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
		ret = 0;
	}

	/* normal notification */
	if (status & Int_IntMacRx) {
		/* Got a packet(s). */
#ifdef TC35815_NAPI
		ret = tc35815_rx(dev, limit);
#else
		tc35815_rx(dev);
		ret = 0;
#endif
		lp->lstats.rx_ints++;
	}
	if (status & Int_IntMacTx) {
		/* Transmit complete. */
		lp->lstats.tx_ints++;
		tc35815_txdone(dev);
		netif_wake_queue(dev);
		ret = 0;
	}
	return ret;
L
Linus Torvalds 已提交
1427 1428 1429 1430
}

/*
 * The typical workload of the driver:
A
Atsushi Nemoto 已提交
1431
 * Handle the network interface interrupts.
L
Linus Torvalds 已提交
1432
 */
1433
static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1434 1435
{
	struct net_device *dev = dev_id;
1436
	struct tc35815_local *lp = netdev_priv(dev);
A
Atsushi Nemoto 已提交
1437 1438 1439 1440 1441 1442 1443 1444
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
#ifdef TC35815_NAPI
	u32 dmactl = tc_readl(&tr->DMA_Ctl);

	if (!(dmactl & DMA_IntMask)) {
		/* disable interrupts */
		tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
1445 1446
		if (netif_rx_schedule_prep(dev, &lp->napi))
			__netif_rx_schedule(dev, &lp->napi);
A
Atsushi Nemoto 已提交
1447 1448 1449 1450
		else {
			printk(KERN_ERR "%s: interrupt taken in poll\n",
			       dev->name);
			BUG();
L
Linus Torvalds 已提交
1451
		}
A
Atsushi Nemoto 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
		(void)tc_readl(&tr->Int_Src);	/* flush */
		return IRQ_HANDLED;
	}
	return IRQ_NONE;
#else
	int handled;
	u32 status;

	spin_lock(&lp->lock);
	status = tc_readl(&tr->Int_Src);
	tc_writel(status, &tr->Int_Src);	/* write to clear */
	handled = tc35815_do_interrupt(dev, status);
	(void)tc_readl(&tr->Int_Src);	/* flush */
	spin_unlock(&lp->lock);
	return IRQ_RETVAL(handled >= 0);
#endif /* TC35815_NAPI */
}
L
Linus Torvalds 已提交
1469

A
Atsushi Nemoto 已提交
1470 1471 1472 1473 1474 1475
#ifdef CONFIG_NET_POLL_CONTROLLER
static void tc35815_poll_controller(struct net_device *dev)
{
	disable_irq(dev->irq);
	tc35815_interrupt(dev->irq, dev);
	enable_irq(dev->irq);
L
Linus Torvalds 已提交
1476
}
A
Atsushi Nemoto 已提交
1477
#endif
L
Linus Torvalds 已提交
1478 1479

/* We have a good packet(s), get it/them out of the buffers. */
A
Atsushi Nemoto 已提交
1480 1481 1482 1483
#ifdef TC35815_NAPI
static int
tc35815_rx(struct net_device *dev, int limit)
#else
L
Linus Torvalds 已提交
1484 1485
static void
tc35815_rx(struct net_device *dev)
A
Atsushi Nemoto 已提交
1486
#endif
L
Linus Torvalds 已提交
1487 1488 1489 1490 1491 1492
{
	struct tc35815_local *lp = dev->priv;
	unsigned int fdctl;
	int i;
	int buf_free_count = 0;
	int fd_free_count = 0;
A
Atsushi Nemoto 已提交
1493 1494 1495
#ifdef TC35815_NAPI
	int received = 0;
#endif
L
Linus Torvalds 已提交
1496 1497 1498 1499 1500

	while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
		int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
		int pkt_len = fdctl & FD_FDLength_MASK;
		int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
A
Atsushi Nemoto 已提交
1501 1502 1503 1504 1505 1506
#ifdef DEBUG
		struct RxFD *next_rfd;
#endif
#if (RX_CTL_CMD & Rx_StripCRC) == 0
		pkt_len -= 4;
#endif
L
Linus Torvalds 已提交
1507

A
Atsushi Nemoto 已提交
1508
		if (netif_msg_rx_status(lp))
L
Linus Torvalds 已提交
1509 1510 1511 1512
			dump_rxfd(lp->rfd_cur);
		if (status & Rx_Good) {
			struct sk_buff *skb;
			unsigned char *data;
A
Atsushi Nemoto 已提交
1513 1514 1515 1516
			int cur_bd;
#ifdef TC35815_USE_PACKEDBUFFER
			int offset;
#endif
1517

A
Atsushi Nemoto 已提交
1518 1519 1520 1521 1522 1523
#ifdef TC35815_NAPI
			if (--limit < 0)
				break;
#endif
#ifdef TC35815_USE_PACKEDBUFFER
			BUG_ON(bd_count > 2);
L
Linus Torvalds 已提交
1524 1525 1526 1527
			skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */
			if (skb == NULL) {
				printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
				       dev->name);
A
Atsushi Nemoto 已提交
1528
				dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
				break;
			}
			skb_reserve(skb, 2);   /* 16 bit alignment */

			data = skb_put(skb, pkt_len);

			/* copy from receive buffer */
			cur_bd = 0;
			offset = 0;
			while (offset < pkt_len && cur_bd < bd_count) {
				int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
					BD_BuffLength_MASK;
A
Atsushi Nemoto 已提交
1541 1542 1543 1544 1545 1546 1547 1548
				dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
				void *rxbuf = rxbuf_bus_to_virt(lp, dma);
				if (offset + len > pkt_len)
					len = pkt_len - offset;
#ifdef TC35815_DMA_SYNC_ONDEMAND
				pci_dma_sync_single_for_cpu(lp->pci_dev,
							    dma, len,
							    PCI_DMA_FROMDEVICE);
L
Linus Torvalds 已提交
1549 1550
#endif
				memcpy(data + offset, rxbuf, len);
1551 1552 1553 1554 1555
#ifdef TC35815_DMA_SYNC_ONDEMAND
				pci_dma_sync_single_for_device(lp->pci_dev,
							       dma, len,
							       PCI_DMA_FROMDEVICE);
#endif
L
Linus Torvalds 已提交
1556 1557 1558
				offset += len;
				cur_bd++;
			}
A
Atsushi Nemoto 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
#else /* TC35815_USE_PACKEDBUFFER */
			BUG_ON(bd_count > 1);
			cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
				  & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
#ifdef DEBUG
			if (cur_bd >= RX_BUF_NUM) {
				printk("%s: invalid BDID.\n", dev->name);
				panic_queues(dev);
			}
			BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
			       (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
			if (!lp->rx_skbs[cur_bd].skb) {
				printk("%s: NULL skb.\n", dev->name);
				panic_queues(dev);
			}
#else
			BUG_ON(cur_bd >= RX_BUF_NUM);
L
Linus Torvalds 已提交
1576
#endif
A
Atsushi Nemoto 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
			skb = lp->rx_skbs[cur_bd].skb;
			prefetch(skb->data);
			lp->rx_skbs[cur_bd].skb = NULL;
			lp->fbl_count--;
			pci_unmap_single(lp->pci_dev,
					 lp->rx_skbs[cur_bd].skb_dma,
					 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
			if (!HAVE_DMA_RXALIGN(lp))
				memmove(skb->data, skb->data - 2, pkt_len);
			data = skb_put(skb, pkt_len);
#endif /* TC35815_USE_PACKEDBUFFER */
			if (netif_msg_pktdata(lp))
L
Linus Torvalds 已提交
1589 1590
				print_eth(data);
			skb->protocol = eth_type_trans(skb, dev);
A
Atsushi Nemoto 已提交
1591 1592 1593 1594
#ifdef TC35815_NAPI
			netif_receive_skb(skb);
			received++;
#else
L
Linus Torvalds 已提交
1595
			netif_rx(skb);
A
Atsushi Nemoto 已提交
1596 1597
#endif
			dev->last_rx = jiffies;
A
Atsushi Nemoto 已提交
1598 1599
			dev->stats.rx_packets++;
			dev->stats.rx_bytes += pkt_len;
L
Linus Torvalds 已提交
1600
		} else {
A
Atsushi Nemoto 已提交
1601
			dev->stats.rx_errors++;
A
Atsushi Nemoto 已提交
1602 1603
			printk(KERN_DEBUG "%s: Rx error (status %x)\n",
			       dev->name, status & Rx_Stat_Mask);
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608
			/* WORKAROUND: LongErr and CRCErr means Overflow. */
			if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
				status &= ~(Rx_LongErr|Rx_CRCErr);
				status |= Rx_Over;
			}
A
Atsushi Nemoto 已提交
1609 1610 1611 1612 1613 1614 1615 1616
			if (status & Rx_LongErr)
				dev->stats.rx_length_errors++;
			if (status & Rx_Over)
				dev->stats.rx_fifo_errors++;
			if (status & Rx_CRCErr)
				dev->stats.rx_crc_errors++;
			if (status & Rx_Align)
				dev->stats.rx_frame_errors++;
L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622 1623
		}

		if (bd_count > 0) {
			/* put Free Buffer back to controller */
			int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
			unsigned char id =
				(bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
A
Atsushi Nemoto 已提交
1624 1625
#ifdef DEBUG
			if (id >= RX_BUF_NUM) {
L
Linus Torvalds 已提交
1626 1627 1628
				printk("%s: invalid BDID.\n", dev->name);
				panic_queues(dev);
			}
A
Atsushi Nemoto 已提交
1629 1630 1631
#else
			BUG_ON(id >= RX_BUF_NUM);
#endif
L
Linus Torvalds 已提交
1632
			/* free old buffers */
A
Atsushi Nemoto 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
#ifdef TC35815_USE_PACKEDBUFFER
			while (lp->fbl_curid != id)
#else
			while (lp->fbl_count < RX_BUF_NUM)
#endif
			{
#ifdef TC35815_USE_PACKEDBUFFER
				unsigned char curid = lp->fbl_curid;
#else
				unsigned char curid =
					(id + 1 + lp->fbl_count) % RX_BUF_NUM;
#endif
				struct BDesc *bd = &lp->fbl_ptr->bd[curid];
#ifdef DEBUG
				bdctl = le32_to_cpu(bd->BDCtl);
L
Linus Torvalds 已提交
1648 1649 1650 1651 1652
				if (bdctl & BD_CownsBD) {
					printk("%s: Freeing invalid BD.\n",
					       dev->name);
					panic_queues(dev);
				}
A
Atsushi Nemoto 已提交
1653
#endif
1654
				/* pass BD to controller */
A
Atsushi Nemoto 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
#ifndef TC35815_USE_PACKEDBUFFER
				if (!lp->rx_skbs[curid].skb) {
					lp->rx_skbs[curid].skb =
						alloc_rxbuf_skb(dev,
								lp->pci_dev,
								&lp->rx_skbs[curid].skb_dma);
					if (!lp->rx_skbs[curid].skb)
						break; /* try on next reception */
					bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
				}
#endif /* TC35815_USE_PACKEDBUFFER */
L
Linus Torvalds 已提交
1666
				/* Note: BDLength was modified by chip. */
A
Atsushi Nemoto 已提交
1667 1668 1669 1670 1671 1672
				bd->BDCtl = cpu_to_le32(BD_CownsBD |
							(curid << BD_RxBDID_SHIFT) |
							RX_BUF_SIZE);
#ifdef TC35815_USE_PACKEDBUFFER
				lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
				if (netif_msg_rx_status(lp)) {
L
Linus Torvalds 已提交
1673 1674 1675 1676
					printk("%s: Entering new FBD %d\n",
					       dev->name, lp->fbl_curid);
					dump_frfd(lp->fbl_ptr);
				}
A
Atsushi Nemoto 已提交
1677 1678 1679
#else
				lp->fbl_count++;
#endif
L
Linus Torvalds 已提交
1680 1681 1682 1683 1684
				buf_free_count++;
			}
		}

		/* put RxFD back to controller */
A
Atsushi Nemoto 已提交
1685 1686 1687
#ifdef DEBUG
		next_rfd = fd_bus_to_virt(lp,
					  le32_to_cpu(lp->rfd_cur->fd.FDNext));
L
Linus Torvalds 已提交
1688 1689 1690 1691
		if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
			printk("%s: RxFD FDNext invalid.\n", dev->name);
			panic_queues(dev);
		}
A
Atsushi Nemoto 已提交
1692
#endif
L
Linus Torvalds 已提交
1693
		for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
1694
			/* pass FD to controller */
A
Atsushi Nemoto 已提交
1695 1696 1697 1698 1699
#ifdef DEBUG
			lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
#else
			lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
#endif
L
Linus Torvalds 已提交
1700 1701 1702 1703
			lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
			lp->rfd_cur++;
			fd_free_count++;
		}
A
Atsushi Nemoto 已提交
1704 1705 1706 1707 1708 1709 1710
		if (lp->rfd_cur > lp->rfd_limit)
			lp->rfd_cur = lp->rfd_base;
#ifdef DEBUG
		if (lp->rfd_cur != next_rfd)
			printk("rfd_cur = %p, next_rfd %p\n",
			       lp->rfd_cur, next_rfd);
#endif
L
Linus Torvalds 已提交
1711 1712 1713 1714
	}

	/* re-enable BL/FDA Exhaust interrupts. */
	if (fd_free_count) {
A
Atsushi Nemoto 已提交
1715 1716 1717 1718
		struct tc35815_regs __iomem *tr =
			(struct tc35815_regs __iomem *)dev->base_addr;
		u32 en, en_old = tc_readl(&tr->Int_En);
		en = en_old | Int_FDAExEn;
L
Linus Torvalds 已提交
1719
		if (buf_free_count)
A
Atsushi Nemoto 已提交
1720 1721 1722
			en |= Int_BLExEn;
		if (en != en_old)
			tc_writel(en, &tr->Int_En);
L
Linus Torvalds 已提交
1723
	}
A
Atsushi Nemoto 已提交
1724 1725 1726
#ifdef TC35815_NAPI
	return received;
#endif
L
Linus Torvalds 已提交
1727 1728
}

A
Atsushi Nemoto 已提交
1729
#ifdef TC35815_NAPI
1730
static int tc35815_poll(struct napi_struct *napi, int budget)
A
Atsushi Nemoto 已提交
1731
{
1732 1733
	struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
	struct net_device *dev = lp->dev;
A
Atsushi Nemoto 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	int received = 0, handled;
	u32 status;

	spin_lock(&lp->lock);
	status = tc_readl(&tr->Int_Src);
	do {
		tc_writel(status, &tr->Int_Src);	/* write to clear */

		handled = tc35815_do_interrupt(dev, status, limit);
		if (handled >= 0) {
			received += handled;
1747
			if (received >= budget)
A
Atsushi Nemoto 已提交
1748 1749 1750 1751 1752 1753
				break;
		}
		status = tc_readl(&tr->Int_Src);
	} while (status);
	spin_unlock(&lp->lock);

1754 1755 1756 1757 1758 1759
	if (received < budget) {
		netif_rx_complete(dev, napi);
		/* enable interrupts */
		tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
	}
	return received;
A
Atsushi Nemoto 已提交
1760 1761 1762
}
#endif

L
Linus Torvalds 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
#ifdef NO_CHECK_CARRIER
#define TX_STA_ERR	(Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
#else
#define TX_STA_ERR	(Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
#endif

static void
tc35815_check_tx_stat(struct net_device *dev, int status)
{
	struct tc35815_local *lp = dev->priv;
	const char *msg = NULL;

	/* count collisions */
	if (status & Tx_ExColl)
A
Atsushi Nemoto 已提交
1777
		dev->stats.collisions += 16;
L
Linus Torvalds 已提交
1778
	if (status & Tx_TxColl_MASK)
A
Atsushi Nemoto 已提交
1779
		dev->stats.collisions += status & Tx_TxColl_MASK;
L
Linus Torvalds 已提交
1780

A
Atsushi Nemoto 已提交
1781 1782 1783 1784 1785
#ifndef NO_CHECK_CARRIER
	/* TX4939 does not have NCarr */
	if (lp->boardtype == TC35815_TX4939)
		status &= ~Tx_NCarr;
#ifdef WORKAROUND_LOSTCAR
L
Linus Torvalds 已提交
1786
	/* WORKAROUND: ignore LostCrS in full duplex operation */
A
Atsushi Nemoto 已提交
1787 1788
	if ((lp->timer_state != asleep && lp->timer_state != lcheck)
	    || lp->fullduplex)
L
Linus Torvalds 已提交
1789
		status &= ~Tx_NCarr;
A
Atsushi Nemoto 已提交
1790 1791
#endif
#endif
L
Linus Torvalds 已提交
1792 1793 1794

	if (!(status & TX_STA_ERR)) {
		/* no error. */
A
Atsushi Nemoto 已提交
1795
		dev->stats.tx_packets++;
L
Linus Torvalds 已提交
1796 1797 1798
		return;
	}

A
Atsushi Nemoto 已提交
1799
	dev->stats.tx_errors++;
L
Linus Torvalds 已提交
1800
	if (status & Tx_ExColl) {
A
Atsushi Nemoto 已提交
1801
		dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
1802 1803 1804
		msg = "Excessive Collision.";
	}
	if (status & Tx_Under) {
A
Atsushi Nemoto 已提交
1805
		dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
1806
		msg = "Tx FIFO Underrun.";
A
Atsushi Nemoto 已提交
1807 1808 1809 1810 1811 1812 1813 1814 1815
		if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
			lp->lstats.tx_underrun++;
			if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
				struct tc35815_regs __iomem *tr =
					(struct tc35815_regs __iomem *)dev->base_addr;
				tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
				msg = "Tx FIFO Underrun.Change Tx threshold to max.";
			}
		}
L
Linus Torvalds 已提交
1816 1817
	}
	if (status & Tx_Defer) {
A
Atsushi Nemoto 已提交
1818
		dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
1819 1820 1821 1822
		msg = "Excessive Deferral.";
	}
#ifndef NO_CHECK_CARRIER
	if (status & Tx_NCarr) {
A
Atsushi Nemoto 已提交
1823
		dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
1824 1825 1826 1827
		msg = "Lost Carrier Sense.";
	}
#endif
	if (status & Tx_LateColl) {
A
Atsushi Nemoto 已提交
1828
		dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
1829 1830 1831
		msg = "Late Collision.";
	}
	if (status & Tx_TxPar) {
A
Atsushi Nemoto 已提交
1832
		dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
1833 1834 1835
		msg = "Transmit Parity Error.";
	}
	if (status & Tx_SQErr) {
A
Atsushi Nemoto 已提交
1836
		dev->stats.tx_heartbeat_errors++;
L
Linus Torvalds 已提交
1837 1838
		msg = "Signal Quality Error.";
	}
A
Atsushi Nemoto 已提交
1839
	if (msg && netif_msg_tx_err(lp))
L
Linus Torvalds 已提交
1840 1841 1842
		printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
}

A
Atsushi Nemoto 已提交
1843 1844 1845
/* This handles TX complete events posted by the device
 * via interrupts.
 */
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
static void
tc35815_txdone(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	struct TxFD *txfd;
	unsigned int fdctl;

	txfd = &lp->tfd_base[lp->tfd_end];
	while (lp->tfd_start != lp->tfd_end &&
	       !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
		int status = le32_to_cpu(txfd->fd.FDStat);
		struct sk_buff *skb;
		unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
A
Atsushi Nemoto 已提交
1859
		u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
L
Linus Torvalds 已提交
1860

A
Atsushi Nemoto 已提交
1861
		if (netif_msg_tx_done(lp)) {
L
Linus Torvalds 已提交
1862 1863 1864 1865 1866
			printk("%s: complete TxFD.\n", dev->name);
			dump_txfd(txfd);
		}
		tc35815_check_tx_stat(dev, status);

A
Atsushi Nemoto 已提交
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
		skb = fdsystem != 0xffffffff ?
			lp->tx_skbs[fdsystem].skb : NULL;
#ifdef DEBUG
		if (lp->tx_skbs[lp->tfd_end].skb != skb) {
			printk("%s: tx_skbs mismatch.\n", dev->name);
			panic_queues(dev);
		}
#else
		BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
#endif
L
Linus Torvalds 已提交
1877
		if (skb) {
A
Atsushi Nemoto 已提交
1878
			dev->stats.tx_bytes += skb->len;
A
Atsushi Nemoto 已提交
1879 1880 1881 1882
			pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
			lp->tx_skbs[lp->tfd_end].skb = NULL;
			lp->tx_skbs[lp->tfd_end].skb_dma = 0;
#ifdef TC35815_NAPI
L
Linus Torvalds 已提交
1883
			dev_kfree_skb_any(skb);
A
Atsushi Nemoto 已提交
1884 1885 1886
#else
			dev_kfree_skb_irq(skb);
#endif
L
Linus Torvalds 已提交
1887
		}
A
Atsushi Nemoto 已提交
1888
		txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
L
Linus Torvalds 已提交
1889 1890 1891

		lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
		txfd = &lp->tfd_base[lp->tfd_end];
A
Atsushi Nemoto 已提交
1892 1893
#ifdef DEBUG
		if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
L
Linus Torvalds 已提交
1894 1895 1896
			printk("%s: TxFD FDNext invalid.\n", dev->name);
			panic_queues(dev);
		}
A
Atsushi Nemoto 已提交
1897
#endif
L
Linus Torvalds 已提交
1898 1899 1900
		if (fdnext & FD_Next_EOL) {
			/* DMA Transmitter has been stopping... */
			if (lp->tfd_end != lp->tfd_start) {
A
Atsushi Nemoto 已提交
1901 1902
				struct tc35815_regs __iomem *tr =
					(struct tc35815_regs __iomem *)dev->base_addr;
L
Linus Torvalds 已提交
1903 1904 1905 1906 1907
				int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
				struct TxFD* txhead = &lp->tfd_base[head];
				int qlen = (lp->tfd_start + TX_FD_NUM
					    - lp->tfd_end) % TX_FD_NUM;

A
Atsushi Nemoto 已提交
1908
#ifdef DEBUG
L
Linus Torvalds 已提交
1909 1910 1911 1912
				if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
					printk("%s: TxFD FDCtl invalid.\n", dev->name);
					panic_queues(dev);
				}
A
Atsushi Nemoto 已提交
1913
#endif
L
Linus Torvalds 已提交
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
				/* log max queue length */
				if (lp->lstats.max_tx_qlen < qlen)
					lp->lstats.max_tx_qlen = qlen;


				/* start DMA Transmitter again */
				txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
#ifdef GATHER_TXINT
				txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
#endif
A
Atsushi Nemoto 已提交
1924
				if (netif_msg_tx_queued(lp)) {
L
Linus Torvalds 已提交
1925 1926 1927 1928
					printk("%s: start TxFD on queue.\n",
					       dev->name);
					dump_txfd(txfd);
				}
A
Atsushi Nemoto 已提交
1929
				tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934
			}
			break;
		}
	}

A
Atsushi Nemoto 已提交
1935 1936 1937 1938 1939 1940
	/* If we had stopped the queue due to a "tx full"
	 * condition, and space has now been made available,
	 * wake up the queue.
	 */
	if (netif_queue_stopped(dev) && ! tc35815_tx_full(dev))
		netif_wake_queue(dev);
L
Linus Torvalds 已提交
1941 1942 1943 1944 1945 1946 1947
}

/* The inverse routine to tc35815_open(). */
static int
tc35815_close(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
1948

L
Linus Torvalds 已提交
1949
	netif_stop_queue(dev);
1950 1951 1952
#ifdef TC35815_NAPI
	napi_disable(&lp->napi);
#endif
L
Linus Torvalds 已提交
1953 1954 1955

	/* Flush the Tx and disable Rx here. */

A
Atsushi Nemoto 已提交
1956
	del_timer(&lp->timer);		/* Kill if running	*/
L
Linus Torvalds 已提交
1957 1958 1959 1960 1961 1962
	tc35815_chip_reset(dev);
	free_irq(dev->irq, dev);

	tc35815_free_queues(dev);

	return 0;
A
Atsushi Nemoto 已提交
1963

L
Linus Torvalds 已提交
1964 1965 1966 1967 1968 1969 1970 1971
}

/*
 * Get the current statistics.
 * This may be called with the card open or closed.
 */
static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
{
A
Atsushi Nemoto 已提交
1972 1973
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
A
Atsushi Nemoto 已提交
1974
	if (netif_running(dev))
L
Linus Torvalds 已提交
1975
		/* Update the statistics from the device registers. */
A
Atsushi Nemoto 已提交
1976
		dev->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt);
L
Linus Torvalds 已提交
1977

A
Atsushi Nemoto 已提交
1978
	return &dev->stats;
L
Linus Torvalds 已提交
1979 1980
}

A
Atsushi Nemoto 已提交
1981
static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
L
Linus Torvalds 已提交
1982
{
A
Atsushi Nemoto 已提交
1983 1984 1985
	struct tc35815_local *lp = dev->priv;
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
L
Linus Torvalds 已提交
1986
	int cam_index = index * 6;
A
Atsushi Nemoto 已提交
1987 1988
	u32 cam_data;
	u32 saved_addr;
A
Atsushi Nemoto 已提交
1989 1990
	DECLARE_MAC_BUF(mac);

L
Linus Torvalds 已提交
1991 1992
	saved_addr = tc_readl(&tr->CAM_Adr);

A
Atsushi Nemoto 已提交
1993 1994 1995
	if (netif_msg_hw(lp))
		printk(KERN_DEBUG "%s: CAM %d: %s\n",
			dev->name, index, print_mac(mac, addr));
L
Linus Torvalds 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
	if (index & 1) {
		/* read modify write */
		tc_writel(cam_index - 2, &tr->CAM_Adr);
		cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
		cam_data |= addr[0] << 8 | addr[1];
		tc_writel(cam_data, &tr->CAM_Data);
		/* write whole word */
		tc_writel(cam_index + 2, &tr->CAM_Adr);
		cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
		tc_writel(cam_data, &tr->CAM_Data);
	} else {
		/* write whole word */
		tc_writel(cam_index, &tr->CAM_Adr);
		cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
		tc_writel(cam_data, &tr->CAM_Data);
		/* read modify write */
		tc_writel(cam_index + 4, &tr->CAM_Adr);
		cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
		cam_data |= addr[4] << 24 | (addr[5] << 16);
		tc_writel(cam_data, &tr->CAM_Data);
	}

	tc_writel(saved_addr, &tr->CAM_Adr);
}


/*
 * Set or clear the multicast filter for this adaptor.
 * num_addrs == -1	Promiscuous mode, receive all packets
 * num_addrs == 0	Normal mode, clear multicast list
 * num_addrs > 0	Multicast mode, receive normal and MC packets,
 *			and do best-effort filtering.
 */
static void
tc35815_set_multicast_list(struct net_device *dev)
{
A
Atsushi Nemoto 已提交
2032 2033
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
L
Linus Torvalds 已提交
2034 2035 2036

	if (dev->flags&IFF_PROMISC)
	{
A
Atsushi Nemoto 已提交
2037 2038 2039 2040 2041 2042 2043 2044
#ifdef WORKAROUND_100HALF_PROMISC
		/* With some (all?) 100MHalf HUB, controller will hang
		 * if we enabled promiscuous mode before linkup... */
		struct tc35815_local *lp = dev->priv;
		int pid = lp->phy_addr;
		if (!(tc_mdio_read(dev, pid, MII_BMSR) & BMSR_LSTATUS))
			return;
#endif
L
Linus Torvalds 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
		/* Enable promiscuous mode */
		tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
	}
	else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > CAM_ENTRY_MAX - 3)
	{
		/* CAM 0, 1, 20 are reserved. */
		/* Disable promiscuous mode, use normal mode. */
		tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
	}
	else if(dev->mc_count)
	{
		struct dev_mc_list* cur_addr = dev->mc_list;
		int i;
		int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);

		tc_writel(0, &tr->CAM_Ctl);
		/* Walk the address list, and load the filter */
		for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
			if (!cur_addr)
				break;
			/* entry 0,1 is reserved. */
A
Atsushi Nemoto 已提交
2066
			tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
L
Linus Torvalds 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
			ena_bits |= CAM_Ena_Bit(i + 2);
		}
		tc_writel(ena_bits, &tr->CAM_Ena);
		tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
	}
	else {
		tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
		tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
	}
}

A
Atsushi Nemoto 已提交
2078
static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
L
Linus Torvalds 已提交
2079 2080
{
	struct tc35815_local *lp = dev->priv;
A
Atsushi Nemoto 已提交
2081 2082 2083 2084
	strcpy(info->driver, MODNAME);
	strcpy(info->version, DRV_VERSION);
	strcpy(info->bus_info, pci_name(lp->pci_dev));
}
2085

A
Atsushi Nemoto 已提交
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct tc35815_local *lp = dev->priv;
	spin_lock_irq(&lp->lock);
	mii_ethtool_gset(&lp->mii, cmd);
	spin_unlock_irq(&lp->lock);
	return 0;
}

static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct tc35815_local *lp = dev->priv;
	int rc;
#if 1	/* use our negotiation method... */
	/* Verify the settings we care about. */
	if (cmd->autoneg != AUTONEG_ENABLE &&
	    cmd->autoneg != AUTONEG_DISABLE)
		return -EINVAL;
	if (cmd->autoneg == AUTONEG_DISABLE &&
	    ((cmd->speed != SPEED_100 &&
	      cmd->speed != SPEED_10) ||
	     (cmd->duplex != DUPLEX_HALF &&
	      cmd->duplex != DUPLEX_FULL)))
		return -EINVAL;

	/* Ok, do it to it. */
	spin_lock_irq(&lp->lock);
	del_timer(&lp->timer);
	tc35815_start_auto_negotiation(dev, cmd);
	spin_unlock_irq(&lp->lock);
	rc = 0;
#else
	spin_lock_irq(&lp->lock);
	rc = mii_ethtool_sset(&lp->mii, cmd);
	spin_unlock_irq(&lp->lock);
#endif
	return rc;
}

static int tc35815_nway_reset(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int rc;
	spin_lock_irq(&lp->lock);
	rc = mii_nway_restart(&lp->mii);
	spin_unlock_irq(&lp->lock);
	return rc;
}

static u32 tc35815_get_link(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int rc;
	spin_lock_irq(&lp->lock);
	rc = mii_link_ok(&lp->mii);
	spin_unlock_irq(&lp->lock);
	return rc;
}

static u32 tc35815_get_msglevel(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	return lp->msg_enable;
}

static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
{
	struct tc35815_local *lp = dev->priv;
	lp->msg_enable = datum;
}

2157
static int tc35815_get_sset_count(struct net_device *dev, int sset)
A
Atsushi Nemoto 已提交
2158 2159
{
	struct tc35815_local *lp = dev->priv;
2160 2161 2162 2163 2164 2165 2166

	switch (sset) {
	case ETH_SS_STATS:
		return sizeof(lp->lstats) / sizeof(int);
	default:
		return -EOPNOTSUPP;
	}
A
Atsushi Nemoto 已提交
2167
}
L
Linus Torvalds 已提交
2168

A
Atsushi Nemoto 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
{
	struct tc35815_local *lp = dev->priv;
	data[0] = lp->lstats.max_tx_qlen;
	data[1] = lp->lstats.tx_ints;
	data[2] = lp->lstats.rx_ints;
	data[3] = lp->lstats.tx_underrun;
}

static struct {
	const char str[ETH_GSTRING_LEN];
} ethtool_stats_keys[] = {
	{ "max_tx_qlen" },
	{ "tx_ints" },
	{ "rx_ints" },
	{ "tx_underrun" },
};

static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
{
	memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
}

static const struct ethtool_ops tc35815_ethtool_ops = {
	.get_drvinfo		= tc35815_get_drvinfo,
	.get_settings		= tc35815_get_settings,
	.set_settings		= tc35815_set_settings,
	.nway_reset		= tc35815_nway_reset,
	.get_link		= tc35815_get_link,
	.get_msglevel		= tc35815_get_msglevel,
	.set_msglevel		= tc35815_set_msglevel,
	.get_strings		= tc35815_get_strings,
2201
	.get_sset_count		= tc35815_get_sset_count,
A
Atsushi Nemoto 已提交
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
	.get_ethtool_stats	= tc35815_get_ethtool_stats,
};

static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct tc35815_local *lp = dev->priv;
	int rc;

	if (!netif_running(dev))
		return -EINVAL;

	spin_lock_irq(&lp->lock);
	rc = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
	spin_unlock_irq(&lp->lock);

	return rc;
}

static int tc_mdio_read(struct net_device *dev, int phy_id, int location)
{
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	u32 data;
	tc_writel(MD_CA_Busy | (phy_id << 5) | location, &tr->MD_CA);
L
Linus Torvalds 已提交
2226 2227 2228
	while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
		;
	data = tc_readl(&tr->MD_Data);
A
Atsushi Nemoto 已提交
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	return data & 0xffff;
}

static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
			  int val)
{
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	tc_writel(val, &tr->MD_Data);
	tc_writel(MD_CA_Busy | MD_CA_Wr | (phy_id << 5) | location, &tr->MD_CA);
	while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
		;
L
Linus Torvalds 已提交
2241 2242
}

A
Atsushi Nemoto 已提交
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
/* Auto negotiation.  The scheme is very simple.  We have a timer routine
 * that keeps watching the auto negotiation process as it progresses.
 * The DP83840 is first told to start doing it's thing, we set up the time
 * and place the timer state machine in it's initial state.
 *
 * Here the timer peeks at the DP83840 status registers at each click to see
 * if the auto negotiation has completed, we assume here that the DP83840 PHY
 * will time out at some point and just tell us what (didn't) happen.  For
 * complete coverage we only allow so many of the ticks at this level to run,
 * when this has expired we print a warning message and try another strategy.
 * This "other" strategy is to force the interface into various speed/duplex
 * configurations and we stop when we see a link-up condition before the
 * maximum number of "peek" ticks have occurred.
 *
 * Once a valid link status has been detected we configure the BigMAC and
 * the rest of the Happy Meal to speak the most efficient protocol we could
 * get a clean link for.  The priority for link configurations, highest first
 * is:
 *                 100 Base-T Full Duplex
 *                 100 Base-T Half Duplex
 *                 10 Base-T Full Duplex
 *                 10 Base-T Half Duplex
 *
 * We start a new timer now, after a successful auto negotiation status has
 * been detected.  This timer just waits for the link-up bit to get set in
 * the BMCR of the DP83840.  When this occurs we print a kernel log message
 * describing the link type in use and the fact that it is up.
 *
 * If a fatal error of some sort is signalled and detected in the interrupt
 * service routine, and the chip is reset, or the link is ifconfig'd down
 * and then back up, this entire process repeats itself all over again.
 */
/* Note: Above comments are come from sunhme driver. */

static int tc35815_try_next_permutation(struct net_device *dev)
L
Linus Torvalds 已提交
2278 2279
{
	struct tc35815_local *lp = dev->priv;
A
Atsushi Nemoto 已提交
2280 2281
	int pid = lp->phy_addr;
	unsigned short bmcr;
L
Linus Torvalds 已提交
2282

A
Atsushi Nemoto 已提交
2283
	bmcr = tc_mdio_read(dev, pid, MII_BMCR);
L
Linus Torvalds 已提交
2284

A
Atsushi Nemoto 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
	/* Downgrade from full to half duplex.  Only possible via ethtool.  */
	if (bmcr & BMCR_FULLDPLX) {
		bmcr &= ~BMCR_FULLDPLX;
		printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
		tc_mdio_write(dev, pid, MII_BMCR, bmcr);
		return 0;
	}

	/* Downgrade from 100 to 10. */
	if (bmcr & BMCR_SPEED100) {
		bmcr &= ~BMCR_SPEED100;
		printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
		tc_mdio_write(dev, pid, MII_BMCR, bmcr);
		return 0;
	}

	/* We've tried everything. */
	return -1;
L
Linus Torvalds 已提交
2303 2304
}

A
Atsushi Nemoto 已提交
2305 2306
static void
tc35815_display_link_mode(struct net_device *dev)
L
Linus Torvalds 已提交
2307 2308
{
	struct tc35815_local *lp = dev->priv;
A
Atsushi Nemoto 已提交
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
	int pid = lp->phy_addr;
	unsigned short lpa, bmcr;
	char *speed = "", *duplex = "";

	lpa = tc_mdio_read(dev, pid, MII_LPA);
	bmcr = tc_mdio_read(dev, pid, MII_BMCR);
	if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
		speed = "100Mb/s";
	else
		speed = "10Mb/s";
	if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
		duplex = "Full Duplex";
	else
		duplex = "Half Duplex";

	if (netif_msg_link(lp))
		printk(KERN_INFO "%s: Link is up at %s, %s.\n",
		       dev->name, speed, duplex);
	printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
	       dev->name,
	       bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
}

static void tc35815_display_forced_link_mode(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int pid = lp->phy_addr;
	unsigned short bmcr;
	char *speed = "", *duplex = "";

	bmcr = tc_mdio_read(dev, pid, MII_BMCR);
	if (bmcr & BMCR_SPEED100)
		speed = "100Mb/s";
	else
		speed = "10Mb/s";
	if (bmcr & BMCR_FULLDPLX)
		duplex = "Full Duplex.\n";
	else
		duplex = "Half Duplex.\n";

	if (netif_msg_link(lp))
		printk(KERN_INFO "%s: Link has been forced up at %s, %s",
		       dev->name, speed, duplex);
}

static void tc35815_set_link_modes(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	int pid = lp->phy_addr;
	unsigned short bmcr, lpa;
	int speed;

	if (lp->timer_state == arbwait) {
		lpa = tc_mdio_read(dev, pid, MII_LPA);
		bmcr = tc_mdio_read(dev, pid, MII_BMCR);
		printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
		       dev->name,
		       bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
		if (!(lpa & (LPA_10HALF | LPA_10FULL |
			     LPA_100HALF | LPA_100FULL))) {
			/* fall back to 10HALF */
			printk(KERN_INFO "%s: bad ability %04x - falling back to 10HD.\n",
			       dev->name, lpa);
			lpa = LPA_10HALF;
		}
		if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
			lp->fullduplex = 1;
		else
			lp->fullduplex = 0;
		if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
			speed = 100;
		else
			speed = 10;
	} else {
		/* Forcing a link mode. */
		bmcr = tc_mdio_read(dev, pid, MII_BMCR);
		if (bmcr & BMCR_FULLDPLX)
			lp->fullduplex = 1;
		else
			lp->fullduplex = 0;
		if (bmcr & BMCR_SPEED100)
			speed = 100;
		else
			speed = 10;
	}

	tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_HaltReq, &tr->MAC_Ctl);
	if (lp->fullduplex) {
		tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl);
	} else {
		tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_FullDup, &tr->MAC_Ctl);
	}
	tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_HaltReq, &tr->MAC_Ctl);

	/* TX4939 PCFG.SPEEDn bit will be changed on NETDEV_CHANGE event. */

#ifndef NO_CHECK_CARRIER
	/* TX4939 does not have EnLCarr */
	if (lp->boardtype != TC35815_TX4939) {
#ifdef WORKAROUND_LOSTCAR
		/* WORKAROUND: enable LostCrS only if half duplex operation */
		if (!lp->fullduplex && lp->boardtype != TC35815_TX4939)
			tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, &tr->Tx_Ctl);
#endif
	}
#endif
	lp->mii.full_duplex = lp->fullduplex;
}

static void tc35815_timer(unsigned long data)
{
	struct net_device *dev = (struct net_device *)data;
	struct tc35815_local *lp = dev->priv;
	int pid = lp->phy_addr;
	unsigned short bmsr, bmcr, lpa;
	int restart_timer = 0;

	spin_lock_irq(&lp->lock);

	lp->timer_ticks++;
	switch (lp->timer_state) {
	case arbwait:
		/*
		 * Only allow for 5 ticks, thats 10 seconds and much too
		 * long to wait for arbitration to complete.
		 */
		/* TC35815 need more times... */
		if (lp->timer_ticks >= 10) {
			/* Enter force mode. */
			if (!options.doforce) {
				printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
				       " cable probblem?\n", dev->name);
				/* Try to restart the adaptor. */
				tc35815_restart(dev);
				goto out;
			}
			printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
			       " trying force link mode\n", dev->name);
			printk(KERN_DEBUG "%s: BMCR %x BMSR %x\n", dev->name,
			       tc_mdio_read(dev, pid, MII_BMCR),
			       tc_mdio_read(dev, pid, MII_BMSR));
			bmcr = BMCR_SPEED100;
			tc_mdio_write(dev, pid, MII_BMCR, bmcr);

			/*
			 * OK, seems we need do disable the transceiver
			 * for the first tick to make sure we get an
			 * accurate link state at the second tick.
			 */

			lp->timer_state = ltrywait;
			lp->timer_ticks = 0;
			restart_timer = 1;
L
Linus Torvalds 已提交
2464
		} else {
A
Atsushi Nemoto 已提交
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
			/* Anything interesting happen? */
			bmsr = tc_mdio_read(dev, pid, MII_BMSR);
			if (bmsr & BMSR_ANEGCOMPLETE) {
				/* Just what we've been waiting for... */
				tc35815_set_link_modes(dev);

				/*
				 * Success, at least so far, advance our state
				 * engine.
				 */
				lp->timer_state = lupwait;
				restart_timer = 1;
			} else {
				restart_timer = 1;
			}
		}
		break;

	case lupwait:
		/*
		 * Auto negotiation was successful and we are awaiting a
		 * link up status.  I have decided to let this timer run
		 * forever until some sort of error is signalled, reporting
		 * a message to the user at 10 second intervals.
		 */
		bmsr = tc_mdio_read(dev, pid, MII_BMSR);
		if (bmsr & BMSR_LSTATUS) {
			/*
			 * Wheee, it's up, display the link mode in use and put
			 * the timer to sleep.
			 */
			tc35815_display_link_mode(dev);
			netif_carrier_on(dev);
#ifdef WORKAROUND_100HALF_PROMISC
			/* delayed promiscuous enabling */
			if (dev->flags & IFF_PROMISC)
				tc35815_set_multicast_list(dev);
#endif
#if 1
			lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
			lp->timer_state = lcheck;
			restart_timer = 1;
#else
			lp->timer_state = asleep;
			restart_timer = 0;
#endif
		} else {
			if (lp->timer_ticks >= 10) {
				printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
				       "not completely up.\n", dev->name);
				lp->timer_ticks = 0;
				restart_timer = 1;
			} else {
				restart_timer = 1;
			}
		}
		break;

	case ltrywait:
		/*
		 * Making the timeout here too long can make it take
		 * annoyingly long to attempt all of the link mode
		 * permutations, but then again this is essentially
		 * error recovery code for the most part.
		 */
		bmsr = tc_mdio_read(dev, pid, MII_BMSR);
		bmcr = tc_mdio_read(dev, pid, MII_BMCR);
		if (lp->timer_ticks == 1) {
			/*
			 * Re-enable transceiver, we'll re-enable the
			 * transceiver next tick, then check link state
			 * on the following tick.
			 */
			restart_timer = 1;
			break;
		}
		if (lp->timer_ticks == 2) {
			restart_timer = 1;
			break;
		}
		if (bmsr & BMSR_LSTATUS) {
			/* Force mode selection success. */
			tc35815_display_forced_link_mode(dev);
			netif_carrier_on(dev);
			tc35815_set_link_modes(dev);
#ifdef WORKAROUND_100HALF_PROMISC
			/* delayed promiscuous enabling */
			if (dev->flags & IFF_PROMISC)
				tc35815_set_multicast_list(dev);
#endif
#if 1
			lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
			lp->timer_state = lcheck;
			restart_timer = 1;
#else
			lp->timer_state = asleep;
			restart_timer = 0;
#endif
		} else {
			if (lp->timer_ticks >= 4) { /* 6 seconds or so... */
				int ret;

				ret = tc35815_try_next_permutation(dev);
				if (ret == -1) {
					/*
					 * Aieee, tried them all, reset the
					 * chip and try all over again.
					 */
					printk(KERN_NOTICE "%s: Link down, "
					       "cable problem?\n",
					       dev->name);

					/* Try to restart the adaptor. */
					tc35815_restart(dev);
					goto out;
L
Linus Torvalds 已提交
2580
				}
A
Atsushi Nemoto 已提交
2581 2582 2583 2584
				lp->timer_ticks = 0;
				restart_timer = 1;
			} else {
				restart_timer = 1;
L
Linus Torvalds 已提交
2585
			}
A
Atsushi Nemoto 已提交
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
		}
		break;

	case lcheck:
		bmcr = tc_mdio_read(dev, pid, MII_BMCR);
		lpa = tc_mdio_read(dev, pid, MII_LPA);
		if (bmcr & (BMCR_PDOWN | BMCR_ISOLATE | BMCR_RESET)) {
			printk(KERN_ERR "%s: PHY down? (BMCR %x)\n", dev->name,
			       bmcr);
		} else if ((lp->saved_lpa ^ lpa) &
			   (LPA_100FULL|LPA_100HALF|LPA_10FULL|LPA_10HALF)) {
			printk(KERN_NOTICE "%s: link status changed"
			       " (BMCR %x LPA %x->%x)\n", dev->name,
			       bmcr, lp->saved_lpa, lpa);
		} else {
			/* go on */
			restart_timer = 1;
			break;
		}
		/* Try to restart the adaptor. */
		tc35815_restart(dev);
		goto out;

	case asleep:
	default:
		/* Can't happens.... */
		printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
		       "one anyways!\n", dev->name);
		restart_timer = 0;
		lp->timer_ticks = 0;
		lp->timer_state = asleep; /* foo on you */
		break;
	}

	if (restart_timer) {
		lp->timer.expires = jiffies + msecs_to_jiffies(1200);
		add_timer(&lp->timer);
	}
out:
	spin_unlock_irq(&lp->lock);
}

static void tc35815_start_auto_negotiation(struct net_device *dev,
					   struct ethtool_cmd *ep)
{
	struct tc35815_local *lp = dev->priv;
	int pid = lp->phy_addr;
	unsigned short bmsr, bmcr, advertize;
	int timeout;

	netif_carrier_off(dev);
	bmsr = tc_mdio_read(dev, pid, MII_BMSR);
	bmcr = tc_mdio_read(dev, pid, MII_BMCR);
	advertize = tc_mdio_read(dev, pid, MII_ADVERTISE);

	if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
		if (options.speed || options.duplex) {
			/* Advertise only specified configuration. */
			advertize &= ~(ADVERTISE_10HALF |
				       ADVERTISE_10FULL |
				       ADVERTISE_100HALF |
				       ADVERTISE_100FULL);
			if (options.speed != 10) {
				if (options.duplex != 1)
					advertize |= ADVERTISE_100FULL;
				if (options.duplex != 2)
					advertize |= ADVERTISE_100HALF;
			}
			if (options.speed != 100) {
				if (options.duplex != 1)
					advertize |= ADVERTISE_10FULL;
				if (options.duplex != 2)
					advertize |= ADVERTISE_10HALF;
			}
			if (options.speed == 100)
				bmcr |= BMCR_SPEED100;
			else if (options.speed == 10)
				bmcr &= ~BMCR_SPEED100;
			if (options.duplex == 2)
				bmcr |= BMCR_FULLDPLX;
			else if (options.duplex == 1)
				bmcr &= ~BMCR_FULLDPLX;
		} else {
			/* Advertise everything we can support. */
			if (bmsr & BMSR_10HALF)
				advertize |= ADVERTISE_10HALF;
L
Linus Torvalds 已提交
2672
			else
A
Atsushi Nemoto 已提交
2673 2674 2675
				advertize &= ~ADVERTISE_10HALF;
			if (bmsr & BMSR_10FULL)
				advertize |= ADVERTISE_10FULL;
L
Linus Torvalds 已提交
2676
			else
A
Atsushi Nemoto 已提交
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
				advertize &= ~ADVERTISE_10FULL;
			if (bmsr & BMSR_100HALF)
				advertize |= ADVERTISE_100HALF;
			else
				advertize &= ~ADVERTISE_100HALF;
			if (bmsr & BMSR_100FULL)
				advertize |= ADVERTISE_100FULL;
			else
				advertize &= ~ADVERTISE_100FULL;
		}

		tc_mdio_write(dev, pid, MII_ADVERTISE, advertize);

		/* Enable Auto-Negotiation, this is usually on already... */
		bmcr |= BMCR_ANENABLE;
		tc_mdio_write(dev, pid, MII_BMCR, bmcr);

		/* Restart it to make sure it is going. */
		bmcr |= BMCR_ANRESTART;
		tc_mdio_write(dev, pid, MII_BMCR, bmcr);
		printk(KERN_DEBUG "%s: ADVERTISE %x BMCR %x\n", dev->name, advertize, bmcr);

		/* BMCR_ANRESTART self clears when the process has begun. */
		timeout = 64;  /* More than enough. */
		while (--timeout) {
			bmcr = tc_mdio_read(dev, pid, MII_BMCR);
			if (!(bmcr & BMCR_ANRESTART))
				break; /* got it. */
			udelay(10);
		}
		if (!timeout) {
			printk(KERN_ERR "%s: TC35815 would not start auto "
			       "negotiation BMCR=0x%04x\n",
			       dev->name, bmcr);
			printk(KERN_NOTICE "%s: Performing force link "
			       "detection.\n", dev->name);
			goto force_link;
		} else {
			printk(KERN_DEBUG "%s: auto negotiation started.\n", dev->name);
			lp->timer_state = arbwait;
L
Linus Torvalds 已提交
2717
		}
A
Atsushi Nemoto 已提交
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
	} else {
force_link:
		/* Force the link up, trying first a particular mode.
		 * Either we are here at the request of ethtool or
		 * because the Happy Meal would not start to autoneg.
		 */

		/* Disable auto-negotiation in BMCR, enable the duplex and
		 * speed setting, init the timer state machine, and fire it off.
		 */
		if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
			bmcr = BMCR_SPEED100;
		} else {
			if (ep->speed == SPEED_100)
				bmcr = BMCR_SPEED100;
			else
				bmcr = 0;
			if (ep->duplex == DUPLEX_FULL)
				bmcr |= BMCR_FULLDPLX;
		}
		tc_mdio_write(dev, pid, MII_BMCR, bmcr);

		/* OK, seems we need do disable the transceiver for the first
		 * tick to make sure we get an accurate link state at the
		 * second tick.
		 */
		lp->timer_state = ltrywait;
L
Linus Torvalds 已提交
2745 2746
	}

A
Atsushi Nemoto 已提交
2747 2748 2749 2750 2751
	del_timer(&lp->timer);
	lp->timer_ticks = 0;
	lp->timer.expires = jiffies + msecs_to_jiffies(1200);
	add_timer(&lp->timer);
}
L
Linus Torvalds 已提交
2752

A
Atsushi Nemoto 已提交
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
static void tc35815_find_phy(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
	int pid = lp->phy_addr;
	unsigned short id0;

	/* find MII phy */
	for (pid = 31; pid >= 0; pid--) {
		id0 = tc_mdio_read(dev, pid, MII_BMSR);
		if (id0 != 0xffff && id0 != 0x0000 &&
		    (id0 & BMSR_RESV) != (0xffff & BMSR_RESV) /* paranoia? */
			) {
			lp->phy_addr = pid;
			break;
		}
	}
	if (pid < 0) {
		printk(KERN_ERR "%s: No MII Phy found.\n",
		       dev->name);
		lp->phy_addr = pid = 0;
L
Linus Torvalds 已提交
2773
	}
A
Atsushi Nemoto 已提交
2774 2775 2776 2777 2778 2779

	lp->mii_id[0] = tc_mdio_read(dev, pid, MII_PHYSID1);
	lp->mii_id[1] = tc_mdio_read(dev, pid, MII_PHYSID2);
	if (netif_msg_hw(lp))
		printk(KERN_INFO "%s: PHY(%02x) ID %04x %04x\n", dev->name,
		       pid, lp->mii_id[0], lp->mii_id[1]);
L
Linus Torvalds 已提交
2780 2781
}

A
Atsushi Nemoto 已提交
2782
static void tc35815_phy_chip_init(struct net_device *dev)
L
Linus Torvalds 已提交
2783
{
A
Atsushi Nemoto 已提交
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
	struct tc35815_local *lp = dev->priv;
	int pid = lp->phy_addr;
	unsigned short bmcr;
	struct ethtool_cmd ecmd, *ep;

	/* dis-isolate if needed. */
	bmcr = tc_mdio_read(dev, pid, MII_BMCR);
	if (bmcr & BMCR_ISOLATE) {
		int count = 32;
		printk(KERN_DEBUG "%s: unisolating...", dev->name);
		tc_mdio_write(dev, pid, MII_BMCR, bmcr & ~BMCR_ISOLATE);
		while (--count) {
			if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_ISOLATE))
				break;
			udelay(20);
		}
		printk(" %s.\n", count ? "done" : "failed");
	}
L
Linus Torvalds 已提交
2802

A
Atsushi Nemoto 已提交
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
	if (options.speed && options.duplex) {
		ecmd.autoneg = AUTONEG_DISABLE;
		ecmd.speed = options.speed == 10 ? SPEED_10 : SPEED_100;
		ecmd.duplex = options.duplex == 1 ? DUPLEX_HALF : DUPLEX_FULL;
		ep = &ecmd;
	} else {
		ep = NULL;
	}
	tc35815_start_auto_negotiation(dev, ep);
}

static void tc35815_chip_reset(struct net_device *dev)
{
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
	int i;
L
Linus Torvalds 已提交
2819 2820
	/* reset the controller */
	tc_writel(MAC_Reset, &tr->MAC_Ctl);
A
Atsushi Nemoto 已提交
2821 2822 2823 2824 2825 2826 2827 2828 2829
	udelay(4); /* 3200ns */
	i = 0;
	while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
		if (i++ > 100) {
			printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
			break;
		}
		mdelay(1);
	}
L
Linus Torvalds 已提交
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
	tc_writel(0, &tr->MAC_Ctl);

	/* initialize registers to default value */
	tc_writel(0, &tr->DMA_Ctl);
	tc_writel(0, &tr->TxThrsh);
	tc_writel(0, &tr->TxPollCtr);
	tc_writel(0, &tr->RxFragSize);
	tc_writel(0, &tr->Int_En);
	tc_writel(0, &tr->FDA_Bas);
	tc_writel(0, &tr->FDA_Lim);
	tc_writel(0xffffffff, &tr->Int_Src);	/* Write 1 to clear */
	tc_writel(0, &tr->CAM_Ctl);
	tc_writel(0, &tr->Tx_Ctl);
	tc_writel(0, &tr->Rx_Ctl);
	tc_writel(0, &tr->CAM_Ena);
	(void)tc_readl(&tr->Miss_Cnt);	/* Read to clear */

A
Atsushi Nemoto 已提交
2847 2848 2849 2850 2851 2852 2853
	/* initialize internal SRAM */
	tc_writel(DMA_TestMode, &tr->DMA_Ctl);
	for (i = 0; i < 0x1000; i += 4) {
		tc_writel(i, &tr->CAM_Adr);
		tc_writel(0, &tr->CAM_Data);
	}
	tc_writel(0, &tr->DMA_Ctl);
L
Linus Torvalds 已提交
2854 2855 2856 2857 2858
}

static void tc35815_chip_init(struct net_device *dev)
{
	struct tc35815_local *lp = dev->priv;
A
Atsushi Nemoto 已提交
2859 2860
	struct tc35815_regs __iomem *tr =
		(struct tc35815_regs __iomem *)dev->base_addr;
L
Linus Torvalds 已提交
2861 2862 2863 2864 2865
	unsigned long txctl = TX_CTL_CMD;

	tc35815_phy_chip_init(dev);

	/* load station address to CAM */
A
Atsushi Nemoto 已提交
2866
	tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
L
Linus Torvalds 已提交
2867 2868 2869 2870 2871

	/* Enable CAM (broadcast and unicast) */
	tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
	tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);

A
Atsushi Nemoto 已提交
2872 2873 2874 2875 2876 2877
	/* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
	if (HAVE_DMA_RXALIGN(lp))
		tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
	else
		tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
#ifdef TC35815_USE_PACKEDBUFFER
L
Linus Torvalds 已提交
2878
	tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize);	/* Packing */
A
Atsushi Nemoto 已提交
2879 2880 2881
#else
	tc_writel(ETH_ZLEN, &tr->RxFragSize);
#endif
L
Linus Torvalds 已提交
2882 2883 2884 2885 2886
	tc_writel(0, &tr->TxPollCtr);	/* Batch mode */
	tc_writel(TX_THRESHOLD, &tr->TxThrsh);
	tc_writel(INT_EN_CMD, &tr->Int_En);

	/* set queues */
A
Atsushi Nemoto 已提交
2887
	tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
L
Linus Torvalds 已提交
2888 2889 2890 2891
	tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
		  &tr->FDA_Lim);
	/*
	 * Activation method:
A
Atsushi Nemoto 已提交
2892
	 * First, enable the MAC Transmitter and the DMA Receive circuits.
L
Linus Torvalds 已提交
2893 2894
	 * Then enable the DMA Transmitter and the MAC Receive circuits.
	 */
A
Atsushi Nemoto 已提交
2895
	tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr);	/* start DMA receiver */
L
Linus Torvalds 已提交
2896
	tc_writel(RX_CTL_CMD, &tr->Rx_Ctl);	/* start MAC receiver */
A
Atsushi Nemoto 已提交
2897

L
Linus Torvalds 已提交
2898
	/* start MAC transmitter */
A
Atsushi Nemoto 已提交
2899 2900 2901 2902 2903
#ifndef NO_CHECK_CARRIER
	/* TX4939 does not have EnLCarr */
	if (lp->boardtype == TC35815_TX4939)
		txctl &= ~Tx_EnLCarr;
#ifdef WORKAROUND_LOSTCAR
L
Linus Torvalds 已提交
2904
	/* WORKAROUND: ignore LostCrS in full duplex operation */
A
Atsushi Nemoto 已提交
2905 2906 2907 2908 2909
	if ((lp->timer_state != asleep && lp->timer_state != lcheck) ||
	    lp->fullduplex)
		txctl &= ~Tx_EnLCarr;
#endif
#endif /* !NO_CHECK_CARRIER */
L
Linus Torvalds 已提交
2910 2911 2912 2913
#ifdef GATHER_TXINT
	txctl &= ~Tx_EnComp;	/* disable global tx completion int. */
#endif
	tc_writel(txctl, &tr->Tx_Ctl);
A
Atsushi Nemoto 已提交
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
}

#ifdef CONFIG_PM
static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct tc35815_local *lp = dev->priv;
	unsigned long flags;

	pci_save_state(pdev);
	if (!netif_running(dev))
		return 0;
	netif_device_detach(dev);
	spin_lock_irqsave(&lp->lock, flags);
	del_timer(&lp->timer);		/* Kill if running	*/
	tc35815_chip_reset(dev);
L
Linus Torvalds 已提交
2930
	spin_unlock_irqrestore(&lp->lock, flags);
A
Atsushi Nemoto 已提交
2931 2932
	pci_set_power_state(pdev, PCI_D3hot);
	return 0;
L
Linus Torvalds 已提交
2933 2934
}

A
Atsushi Nemoto 已提交
2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
static int tc35815_resume(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct tc35815_local *lp = dev->priv;
	unsigned long flags;

	pci_restore_state(pdev);
	if (!netif_running(dev))
		return 0;
	pci_set_power_state(pdev, PCI_D0);
	spin_lock_irqsave(&lp->lock, flags);
	tc35815_restart(dev);
	spin_unlock_irqrestore(&lp->lock, flags);
	netif_device_attach(dev);
	return 0;
}
#endif /* CONFIG_PM */

static struct pci_driver tc35815_pci_driver = {
	.name		= MODNAME,
	.id_table	= tc35815_pci_tbl,
	.probe		= tc35815_init_one,
	.remove		= __devexit_p(tc35815_remove_one),
#ifdef CONFIG_PM
	.suspend	= tc35815_suspend,
	.resume		= tc35815_resume,
#endif
L
Linus Torvalds 已提交
2962 2963
};

A
Atsushi Nemoto 已提交
2964 2965 2966 2967 2968 2969 2970
module_param_named(speed, options.speed, int, 0);
MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
module_param_named(duplex, options.duplex, int, 0);
MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
module_param_named(doforce, options.doforce, int, 0);
MODULE_PARM_DESC(doforce, "try force link mode if auto-negotiation failed");

L
Linus Torvalds 已提交
2971 2972
static int __init tc35815_init_module(void)
{
A
Atsushi Nemoto 已提交
2973
	return pci_register_driver(&tc35815_pci_driver);
L
Linus Torvalds 已提交
2974 2975 2976 2977
}

static void __exit tc35815_cleanup_module(void)
{
A
Atsushi Nemoto 已提交
2978
	pci_unregister_driver(&tc35815_pci_driver);
L
Linus Torvalds 已提交
2979
}
2980

L
Linus Torvalds 已提交
2981 2982
module_init(tc35815_init_module);
module_exit(tc35815_cleanup_module);
A
Atsushi Nemoto 已提交
2983 2984 2985

MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
MODULE_LICENSE("GPL");