blob: f0e9566f787e1a97353e5fa8a734758c7fa1f8cf [file] [log] [blame]
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001/*
2 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Based on skelton.c by Donald Becker.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09006 * This driver is a replacement of older and less maintained version.
7 * This is a header of the older version:
8 * -----<snip>-----
9 * Copyright 2001 MontaVista Software Inc.
10 * Author: MontaVista Software, Inc.
11 * ahennessy@mvista.com
12 * Copyright (C) 2000-2001 Toshiba Corporation
13 * static const char *version =
14 * "tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n";
15 * -----<snip>-----
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090017 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090021 * (C) Copyright TOSHIBA CORPORATION 2004-2005
22 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090025#ifdef TC35815_NAPI
Atsushi Nemotobd43da82007-06-29 22:34:53 +090026#define DRV_VERSION "1.36-NAPI"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090027#else
Atsushi Nemotobd43da82007-06-29 22:34:53 +090028#define DRV_VERSION "1.36"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090029#endif
30static const char *version = "tc35815.c:v" DRV_VERSION "\n";
31#define MODNAME "tc35815"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/fcntl.h>
37#include <linux/interrupt.h>
38#include <linux/ioport.h>
39#include <linux/in.h>
40#include <linux/slab.h>
41#include <linux/string.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090042#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/init.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/skbuff.h>
48#include <linux/delay.h>
49#include <linux/pci.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090050#include <linux/mii.h>
51#include <linux/ethtool.h>
Atsushi Nemotobd43da82007-06-29 22:34:53 +090052#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* First, a few definitions that the brave might change. */
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define GATHER_TXINT /* On-Demand Tx Interrupt */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090059#define WORKAROUND_LOSTCAR
60#define WORKAROUND_100HALF_PROMISC
61/* #define TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090063typedef enum {
64 TC35815CF = 0,
65 TC35815_NWU,
66 TC35815_TX4939,
67} board_t;
68
69/* indexed by board_t, above */
70static const struct {
71 const char *name;
72} board_info[] __devinitdata = {
73 { "TOSHIBA TC35815CF 10/100BaseTX" },
74 { "TOSHIBA TC35815 with Wake on LAN" },
75 { "TOSHIBA TC35815/TX4939" },
76};
77
78static const struct pci_device_id tc35815_pci_tbl[] = {
79 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
80 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
81 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
82 {0,}
83};
84MODULE_DEVICE_TABLE (pci, tc35815_pci_tbl);
85
86/* see MODULE_PARM_DESC */
87static struct tc35815_options {
88 int speed;
89 int duplex;
90 int doforce;
91} options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * Registers
95 */
96struct tc35815_regs {
97 volatile __u32 DMA_Ctl; /* 0x00 */
98 volatile __u32 TxFrmPtr;
99 volatile __u32 TxThrsh;
100 volatile __u32 TxPollCtr;
101 volatile __u32 BLFrmPtr;
102 volatile __u32 RxFragSize;
103 volatile __u32 Int_En;
104 volatile __u32 FDA_Bas;
105 volatile __u32 FDA_Lim; /* 0x20 */
106 volatile __u32 Int_Src;
107 volatile __u32 unused0[2];
108 volatile __u32 PauseCnt;
109 volatile __u32 RemPauCnt;
110 volatile __u32 TxCtlFrmStat;
111 volatile __u32 unused1;
112 volatile __u32 MAC_Ctl; /* 0x40 */
113 volatile __u32 CAM_Ctl;
114 volatile __u32 Tx_Ctl;
115 volatile __u32 Tx_Stat;
116 volatile __u32 Rx_Ctl;
117 volatile __u32 Rx_Stat;
118 volatile __u32 MD_Data;
119 volatile __u32 MD_CA;
120 volatile __u32 CAM_Adr; /* 0x60 */
121 volatile __u32 CAM_Data;
122 volatile __u32 CAM_Ena;
123 volatile __u32 PROM_Ctl;
124 volatile __u32 PROM_Data;
125 volatile __u32 Algn_Cnt;
126 volatile __u32 CRC_Cnt;
127 volatile __u32 Miss_Cnt;
128};
129
130/*
131 * Bit assignments
132 */
133/* DMA_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900134#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
135#define DMA_RxAlign_1 0x00400000
136#define DMA_RxAlign_2 0x00800000
137#define DMA_RxAlign_3 0x00c00000
138#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
140#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
141#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
142#define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
143#define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
144#define DMA_TestMode 0x00002000 /* 1:Test Mode */
145#define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
146#define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
147
148/* RxFragSize bit asign ---------------------------------------------------- */
149#define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
150#define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
151
152/* MAC_Ctl bit asign ------------------------------------------------------- */
153#define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
154#define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
155#define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
156#define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
157#define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
158#define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
159#define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
160#define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
161#define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
162#define MAC_Reset 0x00000004 /* 1:Software Reset */
163#define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
164#define MAC_HaltReq 0x00000001 /* 1:Halt request */
165
166/* PROM_Ctl bit asign ------------------------------------------------------ */
167#define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
168#define PROM_Read 0x00004000 /*10:Read operation */
169#define PROM_Write 0x00002000 /*01:Write operation */
170#define PROM_Erase 0x00006000 /*11:Erase operation */
171 /*00:Enable or Disable Writting, */
172 /* as specified in PROM_Addr. */
173#define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
174 /*00xxxx: disable */
175
176/* CAM_Ctl bit asign ------------------------------------------------------- */
177#define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
178#define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
179 /* accept other */
180#define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
181#define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
182#define CAM_StationAcc 0x00000001 /* 1:unicast accept */
183
184/* CAM_Ena bit asign ------------------------------------------------------- */
185#define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
186#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
187#define CAM_Ena_Bit(index) (1<<(index))
188#define CAM_ENTRY_DESTINATION 0
189#define CAM_ENTRY_SOURCE 1
190#define CAM_ENTRY_MACCTL 20
191
192/* Tx_Ctl bit asign -------------------------------------------------------- */
193#define Tx_En 0x00000001 /* 1:Transmit enable */
194#define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
195#define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
196#define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
197#define Tx_FBack 0x00000010 /* 1:Fast Back-off */
198#define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
199#define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
200#define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
201#define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
202#define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
203#define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
204#define Tx_EnComp 0x00004000 /* 1:Enable Completion */
205
206/* Tx_Stat bit asign ------------------------------------------------------- */
207#define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
208#define Tx_ExColl 0x00000010 /* Excessive Collision */
209#define Tx_TXDefer 0x00000020 /* Transmit Defered */
210#define Tx_Paused 0x00000040 /* Transmit Paused */
211#define Tx_IntTx 0x00000080 /* Interrupt on Tx */
212#define Tx_Under 0x00000100 /* Underrun */
213#define Tx_Defer 0x00000200 /* Deferral */
214#define Tx_NCarr 0x00000400 /* No Carrier */
215#define Tx_10Stat 0x00000800 /* 10Mbps Status */
216#define Tx_LateColl 0x00001000 /* Late Collision */
217#define Tx_TxPar 0x00002000 /* Tx Parity Error */
218#define Tx_Comp 0x00004000 /* Completion */
219#define Tx_Halted 0x00008000 /* Tx Halted */
220#define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
221
222/* Rx_Ctl bit asign -------------------------------------------------------- */
223#define Rx_EnGood 0x00004000 /* 1:Enable Good */
224#define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
225#define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
226#define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
227#define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
228#define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
229#define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
230#define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
231#define Rx_ShortEn 0x00000008 /* 1:Short Enable */
232#define Rx_LongEn 0x00000004 /* 1:Long Enable */
233#define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
234#define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
235
236/* Rx_Stat bit asign ------------------------------------------------------- */
237#define Rx_Halted 0x00008000 /* Rx Halted */
238#define Rx_Good 0x00004000 /* Rx Good */
239#define Rx_RxPar 0x00002000 /* Rx Parity Error */
240 /* 0x00001000 not use */
241#define Rx_LongErr 0x00000800 /* Rx Long Error */
242#define Rx_Over 0x00000400 /* Rx Overflow */
243#define Rx_CRCErr 0x00000200 /* Rx CRC Error */
244#define Rx_Align 0x00000100 /* Rx Alignment Error */
245#define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
246#define Rx_IntRx 0x00000040 /* Rx Interrupt */
247#define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
248
249#define Rx_Stat_Mask 0x0000EFC0 /* Rx All Status Mask */
250
251/* Int_En bit asign -------------------------------------------------------- */
252#define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
253#define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Control Complete Enable */
254#define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
255#define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
256#define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
257#define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
258#define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
259#define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
260#define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
261#define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
262#define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
263#define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
264 /* Exhausted Enable */
265
266/* Int_Src bit asign ------------------------------------------------------- */
267#define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
268#define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
269#define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
270#define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
271#define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
272#define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
273#define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
274#define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
275#define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
276#define Int_SWInt 0x00000020 /* 1:Software request & Clear */
277#define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
278#define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
279#define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
280#define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
281#define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
282
283/* MD_CA bit asign --------------------------------------------------------- */
284#define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
285#define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
286#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
287
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
290 * Descriptors
291 */
292
293/* Frame descripter */
294struct FDesc {
295 volatile __u32 FDNext;
296 volatile __u32 FDSystem;
297 volatile __u32 FDStat;
298 volatile __u32 FDCtl;
299};
300
301/* Buffer descripter */
302struct BDesc {
303 volatile __u32 BuffData;
304 volatile __u32 BDCtl;
305};
306
307#define FD_ALIGN 16
308
309/* Frame Descripter bit asign ---------------------------------------------- */
310#define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
311#define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
312#define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
313#define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
314#define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
315#define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
316#define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
317#define FD_FrmOpt_Packing 0x04000000 /* Rx only */
318#define FD_CownsFD 0x80000000 /* FD Controller owner bit */
319#define FD_Next_EOL 0x00000001 /* FD EOL indicator */
320#define FD_BDCnt_SHIFT 16
321
322/* Buffer Descripter bit asign --------------------------------------------- */
323#define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
324#define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
325#define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
326#define BD_CownsBD 0x80000000 /* BD Controller owner bit */
327#define BD_RxBDID_SHIFT 16
328#define BD_RxBDSeqN_SHIFT 24
329
330
331/* Some useful constants. */
332#undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */
333
334#ifdef NO_CHECK_CARRIER
335#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900336 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
337 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#else
339#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900340 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
341 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#endif
343#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
344 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#define INT_EN_CMD (Int_NRAbtEn | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900346 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
348 Int_STargAbtEn | \
349 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900350#define DMA_CTL_CMD DMA_BURST_SIZE
351#define HAVE_DMA_RXALIGN(lp) likely((lp)->boardtype != TC35815CF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/* Tuning parameters */
354#define DMA_BURST_SIZE 32
355#define TX_THRESHOLD 1024
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900356#define TX_THRESHOLD_MAX 1536 /* used threshold with packet max byte for low pci transfer ability.*/
357#define TX_THRESHOLD_KEEP_LIMIT 10 /* setting threshold max value when overrun error occured this count. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900359/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
360#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define FD_PAGE_NUM 2
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900362#define RX_BUF_NUM 8 /* >= 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#define RX_FD_NUM 250 /* >= 32 */
364#define TX_FD_NUM 128
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900365#define RX_BUF_SIZE PAGE_SIZE
366#else /* TC35815_USE_PACKEDBUFFER */
367#define FD_PAGE_NUM 4
368#define RX_BUF_NUM 128 /* < 256 */
369#define RX_FD_NUM 256 /* >= 32 */
370#define TX_FD_NUM 128
371#if RX_CTL_CMD & Rx_LongEn
372#define RX_BUF_SIZE PAGE_SIZE
373#elif RX_CTL_CMD & Rx_StripCRC
374#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */
375#else
376#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */
377#endif
378#endif /* TC35815_USE_PACKEDBUFFER */
379#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
380#define NAPI_WEIGHT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382struct TxFD {
383 struct FDesc fd;
384 struct BDesc bd;
385 struct BDesc unused;
386};
387
388struct RxFD {
389 struct FDesc fd;
390 struct BDesc bd[0]; /* variable length */
391};
392
393struct FrFD {
394 struct FDesc fd;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900395 struct BDesc bd[RX_BUF_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
398
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900399#define tc_readl(addr) readl(addr)
400#define tc_writel(d, addr) writel(d, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900402#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
403
404/* Timer state engine. */
405enum tc35815_timer_state {
406 arbwait = 0, /* Waiting for auto negotiation to complete. */
407 lupwait = 1, /* Auto-neg complete, awaiting link-up status. */
408 ltrywait = 2, /* Forcing try of all modes, from fastest to slowest. */
409 asleep = 3, /* Time inactive. */
410 lcheck = 4, /* Check link status. */
411};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/* Information that need to be kept for each board. */
414struct tc35815_local {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900415 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700417 struct net_device *dev;
418 struct napi_struct napi;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct {
422 int max_tx_qlen;
423 int tx_ints;
424 int rx_ints;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900425 int tx_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 } lstats;
427
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900428 /* Tx control lock. This protects the transmit buffer ring
429 * state along with the "tx full" state of the driver. This
430 * means all netif_queue flow control actions are protected
431 * by this lock as well.
432 */
433 spinlock_t lock;
434
435 int phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int fullduplex;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900437 unsigned short saved_lpa;
438 struct timer_list timer;
439 enum tc35815_timer_state timer_state; /* State of auto-neg timer. */
440 unsigned int timer_ticks; /* Number of clicks at each state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /*
443 * Transmitting: Batch Mode.
444 * 1 BD in 1 TxFD.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900445 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * 1 circular FD for Free Buffer List.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900447 * RX_BUF_NUM BD in Free Buffer FD.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * One Free Buffer BD has PAGE_SIZE data buffer.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900449 * Or Non-Packing Mode.
450 * 1 circular FD for Free Buffer List.
451 * RX_BUF_NUM BD in Free Buffer FD.
452 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900454 void * fd_buf; /* for TxFD, RxFD, FrFD */
455 dma_addr_t fd_buf_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct TxFD *tfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900457 unsigned int tfd_start;
458 unsigned int tfd_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct RxFD *rfd_base;
460 struct RxFD *rfd_limit;
461 struct RxFD *rfd_cur;
462 struct FrFD *fbl_ptr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900463#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned char fbl_curid;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900465 void * data_buf[RX_BUF_NUM]; /* packing */
466 dma_addr_t data_buf_dma[RX_BUF_NUM];
467 struct {
468 struct sk_buff *skb;
469 dma_addr_t skb_dma;
470 } tx_skbs[TX_FD_NUM];
471#else
472 unsigned int fbl_count;
473 struct {
474 struct sk_buff *skb;
475 dma_addr_t skb_dma;
476 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
477#endif
478 struct mii_if_info mii;
479 unsigned short mii_id[2];
480 u32 msg_enable;
481 board_t boardtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482};
483
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900484static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
485{
486 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
487}
488#ifdef DEBUG
489static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
490{
491 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
492}
493#endif
494#ifdef TC35815_USE_PACKEDBUFFER
495static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
496{
497 int i;
498 for (i = 0; i < RX_BUF_NUM; i++) {
499 if (bus >= lp->data_buf_dma[i] &&
500 bus < lp->data_buf_dma[i] + PAGE_SIZE)
501 return (void *)((u8 *)lp->data_buf[i] +
502 (bus - lp->data_buf_dma[i]));
503 }
504 return NULL;
505}
506
507#define TC35815_DMA_SYNC_ONDEMAND
508static void* alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
509{
510#ifdef TC35815_DMA_SYNC_ONDEMAND
511 void *buf;
512 /* pci_map + pci_dma_sync will be more effective than
513 * pci_alloc_consistent on some archs. */
514 if ((buf = (void *)__get_free_page(GFP_ATOMIC)) == NULL)
515 return NULL;
516 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
517 PCI_DMA_FROMDEVICE);
518 if (pci_dma_mapping_error(*dma_handle)) {
519 free_page((unsigned long)buf);
520 return NULL;
521 }
522 return buf;
523#else
524 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
525#endif
526}
527
528static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
529{
530#ifdef TC35815_DMA_SYNC_ONDEMAND
531 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
532 free_page((unsigned long)buf);
533#else
534 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
535#endif
536}
537#else /* TC35815_USE_PACKEDBUFFER */
538static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
539 struct pci_dev *hwdev,
540 dma_addr_t *dma_handle)
541{
542 struct sk_buff *skb;
543 skb = dev_alloc_skb(RX_BUF_SIZE);
544 if (!skb)
545 return NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900546 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
547 PCI_DMA_FROMDEVICE);
548 if (pci_dma_mapping_error(*dma_handle)) {
549 dev_kfree_skb_any(skb);
550 return NULL;
551 }
552 skb_reserve(skb, 2); /* make IP header 4byte aligned */
553 return skb;
554}
555
556static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
557{
558 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
559 PCI_DMA_FROMDEVICE);
560 dev_kfree_skb_any(skb);
561}
562#endif /* TC35815_USE_PACKEDBUFFER */
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* Index to functions, as function prototypes. */
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static int tc35815_open(struct net_device *dev);
567static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900568static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
569#ifdef TC35815_NAPI
570static int tc35815_rx(struct net_device *dev, int limit);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700571static int tc35815_poll(struct napi_struct *napi, int budget);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900572#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static void tc35815_rx(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static void tc35815_txdone(struct net_device *dev);
576static int tc35815_close(struct net_device *dev);
577static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
578static void tc35815_set_multicast_list(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900579static void tc35815_tx_timeout(struct net_device *dev);
580static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
581#ifdef CONFIG_NET_POLL_CONTROLLER
582static void tc35815_poll_controller(struct net_device *dev);
583#endif
584static const struct ethtool_ops tc35815_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900586/* Example routines you must write ;->. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static void tc35815_chip_reset(struct net_device *dev);
588static void tc35815_chip_init(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900589static void tc35815_find_phy(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static void tc35815_phy_chip_init(struct net_device *dev);
591
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900592#ifdef DEBUG
593static void panic_queues(struct net_device *dev);
594#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900596static void tc35815_timer(unsigned long data);
597static void tc35815_start_auto_negotiation(struct net_device *dev,
598 struct ethtool_cmd *ep);
599static int tc_mdio_read(struct net_device *dev, int phy_id, int location);
600static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
601 int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900603#ifdef CONFIG_CPU_TX49XX
604/*
605 * Find a platform_device providing a MAC address. The platform code
606 * should provide a "tc35815-mac" device with a MAC address in its
607 * platform_data.
608 */
609static int __devinit tc35815_mac_match(struct device *dev, void *data)
610{
611 struct platform_device *plat_dev = to_platform_device(dev);
612 struct pci_dev *pci_dev = data;
Atsushi Nemoto06675e62008-01-19 01:15:52 +0900613 unsigned int id = pci_dev->irq;
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900614 return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
615}
616
617static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
618{
619 struct tc35815_local *lp = dev->priv;
620 struct device *pd = bus_find_device(&platform_bus_type, NULL,
621 lp->pci_dev, tc35815_mac_match);
622 if (pd) {
623 if (pd->platform_data)
624 memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
625 put_device(pd);
626 return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
627 }
628 return -ENODEV;
629}
630#else
Yoichi Yuasa308a9062007-07-18 11:13:42 +0900631static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900632{
633 return -ENODEV;
634}
635#endif
636
637static int __devinit tc35815_init_dev_addr (struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900639 struct tc35815_regs __iomem *tr =
640 (struct tc35815_regs __iomem *)dev->base_addr;
641 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
644 ;
645 for (i = 0; i < 6; i += 2) {
646 unsigned short data;
647 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
648 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
649 ;
650 data = tc_readl(&tr->PROM_Data);
651 dev->dev_addr[i] = data & 0xff;
652 dev->dev_addr[i+1] = data >> 8;
653 }
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900654 if (!is_valid_ether_addr(dev->dev_addr))
655 return tc35815_read_plat_dev_addr(dev);
656 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900657}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900659static int __devinit tc35815_init_one (struct pci_dev *pdev,
660 const struct pci_device_id *ent)
661{
662 void __iomem *ioaddr = NULL;
663 struct net_device *dev;
664 struct tc35815_local *lp;
665 int rc;
666 unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900668 static int printed_version;
669 if (!printed_version++) {
670 printk(version);
671 dev_printk(KERN_DEBUG, &pdev->dev,
672 "speed:%d duplex:%d doforce:%d\n",
673 options.speed, options.duplex, options.doforce);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900676 if (!pdev->irq) {
677 dev_warn(&pdev->dev, "no IRQ assigned.\n");
678 return -ENODEV;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900681 /* dev zeroed in alloc_etherdev */
682 dev = alloc_etherdev (sizeof (*lp));
683 if (dev == NULL) {
684 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
685 return -ENOMEM;
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 SET_NETDEV_DEV(dev, &pdev->dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900688 lp = dev->priv;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700689 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900691 /* enable device (incl. PCI PM wakeup), and bus-mastering */
692 rc = pci_enable_device (pdev);
693 if (rc)
694 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900696 mmio_start = pci_resource_start (pdev, 1);
697 mmio_end = pci_resource_end (pdev, 1);
698 mmio_flags = pci_resource_flags (pdev, 1);
699 mmio_len = pci_resource_len (pdev, 1);
700
701 /* set this immediately, we need to know before
702 * we talk to the chip directly */
703
704 /* make sure PCI base addr 1 is MMIO */
705 if (!(mmio_flags & IORESOURCE_MEM)) {
706 dev_err(&pdev->dev, "region #1 not an MMIO resource, aborting\n");
707 rc = -ENODEV;
708 goto err_out;
709 }
710
711 /* check for weird/broken PCI region reporting */
712 if ((mmio_len < sizeof(struct tc35815_regs))) {
713 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
714 rc = -ENODEV;
715 goto err_out;
716 }
717
718 rc = pci_request_regions (pdev, MODNAME);
719 if (rc)
720 goto err_out;
721
722 pci_set_master (pdev);
723
724 /* ioremap MMIO region */
725 ioaddr = ioremap (mmio_start, mmio_len);
726 if (ioaddr == NULL) {
727 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
728 rc = -EIO;
729 goto err_out_free_res;
730 }
731
732 /* Initialize the device structure. */
733 dev->open = tc35815_open;
734 dev->hard_start_xmit = tc35815_send_packet;
735 dev->stop = tc35815_close;
736 dev->get_stats = tc35815_get_stats;
737 dev->set_multicast_list = tc35815_set_multicast_list;
738 dev->do_ioctl = tc35815_ioctl;
739 dev->ethtool_ops = &tc35815_ethtool_ops;
740 dev->tx_timeout = tc35815_tx_timeout;
741 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
742#ifdef TC35815_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700743 netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900744#endif
745#ifdef CONFIG_NET_POLL_CONTROLLER
746 dev->poll_controller = tc35815_poll_controller;
747#endif
748
749 dev->irq = pdev->irq;
750 dev->base_addr = (unsigned long) ioaddr;
751
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900752 spin_lock_init(&lp->lock);
753 lp->pci_dev = pdev;
754 lp->boardtype = ent->driver_data;
755
756 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
757 pci_set_drvdata(pdev, dev);
758
759 /* Soft reset the chip. */
760 tc35815_chip_reset(dev);
761
762 /* Retrieve the ethernet address. */
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900763 if (tc35815_init_dev_addr(dev)) {
764 dev_warn(&pdev->dev, "not valid ether addr\n");
765 random_ether_addr(dev->dev_addr);
766 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900767
768 rc = register_netdev (dev);
769 if (rc)
770 goto err_out_unmap;
771
772 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
773 printk(KERN_INFO "%s: %s at 0x%lx, "
774 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
775 "IRQ %d\n",
776 dev->name,
777 board_info[ent->driver_data].name,
778 dev->base_addr,
779 dev->dev_addr[0], dev->dev_addr[1],
780 dev->dev_addr[2], dev->dev_addr[3],
781 dev->dev_addr[4], dev->dev_addr[5],
782 dev->irq);
783
784 setup_timer(&lp->timer, tc35815_timer, (unsigned long) dev);
785 lp->mii.dev = dev;
786 lp->mii.mdio_read = tc_mdio_read;
787 lp->mii.mdio_write = tc_mdio_write;
788 lp->mii.phy_id_mask = 0x1f;
789 lp->mii.reg_num_mask = 0x1f;
790 tc35815_find_phy(dev);
791 lp->mii.phy_id = lp->phy_addr;
792 lp->mii.full_duplex = 0;
793 lp->mii.force_media = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 return 0;
796
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900797err_out_unmap:
798 iounmap(ioaddr);
799err_out_free_res:
800 pci_release_regions (pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801err_out:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900802 free_netdev (dev);
803 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900807static void __devexit tc35815_remove_one (struct pci_dev *pdev)
808{
809 struct net_device *dev = pci_get_drvdata (pdev);
810 unsigned long mmio_addr;
811
812 mmio_addr = dev->base_addr;
813
814 unregister_netdev (dev);
815
816 if (mmio_addr) {
817 iounmap ((void __iomem *)mmio_addr);
818 pci_release_regions (pdev);
819 }
820
821 free_netdev (dev);
822
823 pci_set_drvdata (pdev, NULL);
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int
827tc35815_init_queues(struct net_device *dev)
828{
829 struct tc35815_local *lp = dev->priv;
830 int i;
831 unsigned long fd_addr;
832
833 if (!lp->fd_buf) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900834 BUG_ON(sizeof(struct FDesc) +
835 sizeof(struct BDesc) * RX_BUF_NUM +
836 sizeof(struct FDesc) * RX_FD_NUM +
837 sizeof(struct TxFD) * TX_FD_NUM >
838 PAGE_SIZE * FD_PAGE_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900840 if ((lp->fd_buf = pci_alloc_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM, &lp->fd_buf_dma)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -ENOMEM;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900842 for (i = 0; i < RX_BUF_NUM; i++) {
843#ifdef TC35815_USE_PACKEDBUFFER
844 if ((lp->data_buf[i] = alloc_rxbuf_page(lp->pci_dev, &lp->data_buf_dma[i])) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 while (--i >= 0) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900846 free_rxbuf_page(lp->pci_dev,
847 lp->data_buf[i],
848 lp->data_buf_dma[i]);
849 lp->data_buf[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900851 pci_free_consistent(lp->pci_dev,
852 PAGE_SIZE * FD_PAGE_NUM,
853 lp->fd_buf,
854 lp->fd_buf_dma);
855 lp->fd_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -ENOMEM;
857 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900858#else
859 lp->rx_skbs[i].skb =
860 alloc_rxbuf_skb(dev, lp->pci_dev,
861 &lp->rx_skbs[i].skb_dma);
862 if (!lp->rx_skbs[i].skb) {
863 while (--i >= 0) {
864 free_rxbuf_skb(lp->pci_dev,
865 lp->rx_skbs[i].skb,
866 lp->rx_skbs[i].skb_dma);
867 lp->rx_skbs[i].skb = NULL;
868 }
869 pci_free_consistent(lp->pci_dev,
870 PAGE_SIZE * FD_PAGE_NUM,
871 lp->fd_buf,
872 lp->fd_buf_dma);
873 lp->fd_buf = NULL;
874 return -ENOMEM;
875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#endif
877 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900878 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
879 dev->name, lp->fd_buf);
880#ifdef TC35815_USE_PACKEDBUFFER
881 printk(" DataBuf");
882 for (i = 0; i < RX_BUF_NUM; i++)
883 printk(" %p", lp->data_buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900885 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 } else {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900887 for (i = 0; i < FD_PAGE_NUM; i++) {
888 clear_page((void *)((unsigned long)lp->fd_buf + i * PAGE_SIZE));
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 fd_addr = (unsigned long)lp->fd_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* Free Descriptors (for Receive) */
894 lp->rfd_base = (struct RxFD *)fd_addr;
895 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
896 for (i = 0; i < RX_FD_NUM; i++) {
897 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
898 }
899 lp->rfd_cur = lp->rfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900900 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* Transmit Descriptors */
903 lp->tfd_base = (struct TxFD *)fd_addr;
904 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
905 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900906 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
907 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
909 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900910 lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 lp->tfd_start = 0;
912 lp->tfd_end = 0;
913
914 /* Buffer List (for Receive) */
915 lp->fbl_ptr = (struct FrFD *)fd_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900916 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
917 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
918#ifndef TC35815_USE_PACKEDBUFFER
919 /*
920 * move all allocated skbs to head of rx_skbs[] array.
921 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
922 * tc35815_rx() had failed.
923 */
924 lp->fbl_count = 0;
925 for (i = 0; i < RX_BUF_NUM; i++) {
926 if (lp->rx_skbs[i].skb) {
927 if (i != lp->fbl_count) {
928 lp->rx_skbs[lp->fbl_count].skb =
929 lp->rx_skbs[i].skb;
930 lp->rx_skbs[lp->fbl_count].skb_dma =
931 lp->rx_skbs[i].skb_dma;
932 }
933 lp->fbl_count++;
934 }
935 }
936#endif
937 for (i = 0; i < RX_BUF_NUM; i++) {
938#ifdef TC35815_USE_PACKEDBUFFER
939 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
940#else
941 if (i >= lp->fbl_count) {
942 lp->fbl_ptr->bd[i].BuffData = 0;
943 lp->fbl_ptr->bd[i].BDCtl = 0;
944 continue;
945 }
946 lp->fbl_ptr->bd[i].BuffData =
947 cpu_to_le32(lp->rx_skbs[i].skb_dma);
948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* BDID is index of FrFD.bd[] */
950 lp->fbl_ptr->bd[i].BDCtl =
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900951 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
952 RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900954#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 lp->fbl_curid = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900958 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
959 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961}
962
963static void
964tc35815_clear_queues(struct net_device *dev)
965{
966 struct tc35815_local *lp = dev->priv;
967 int i;
968
969 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900970 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
971 struct sk_buff *skb =
972 fdsystem != 0xffffffff ?
973 lp->tx_skbs[fdsystem].skb : NULL;
974#ifdef DEBUG
975 if (lp->tx_skbs[i].skb != skb) {
976 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
977 panic_queues(dev);
978 }
979#else
980 BUG_ON(lp->tx_skbs[i].skb != skb);
981#endif
982 if (skb) {
983 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
984 lp->tx_skbs[i].skb = NULL;
985 lp->tx_skbs[i].skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900987 }
988 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 tc35815_init_queues(dev);
992}
993
994static void
995tc35815_free_queues(struct net_device *dev)
996{
997 struct tc35815_local *lp = dev->priv;
998 int i;
999
1000 if (lp->tfd_base) {
1001 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001002 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1003 struct sk_buff *skb =
1004 fdsystem != 0xffffffff ?
1005 lp->tx_skbs[fdsystem].skb : NULL;
1006#ifdef DEBUG
1007 if (lp->tx_skbs[i].skb != skb) {
1008 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1009 panic_queues(dev);
1010 }
1011#else
1012 BUG_ON(lp->tx_skbs[i].skb != skb);
1013#endif
1014 if (skb) {
1015 dev_kfree_skb(skb);
1016 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1017 lp->tx_skbs[i].skb = NULL;
1018 lp->tx_skbs[i].skb_dma = 0;
1019 }
1020 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 }
1023
1024 lp->rfd_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 lp->rfd_limit = NULL;
1026 lp->rfd_cur = NULL;
1027 lp->fbl_ptr = NULL;
1028
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001029 for (i = 0; i < RX_BUF_NUM; i++) {
1030#ifdef TC35815_USE_PACKEDBUFFER
1031 if (lp->data_buf[i]) {
1032 free_rxbuf_page(lp->pci_dev,
1033 lp->data_buf[i], lp->data_buf_dma[i]);
1034 lp->data_buf[i] = NULL;
1035 }
1036#else
1037 if (lp->rx_skbs[i].skb) {
1038 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1039 lp->rx_skbs[i].skb_dma);
1040 lp->rx_skbs[i].skb = NULL;
1041 }
1042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001044 if (lp->fd_buf) {
1045 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1046 lp->fd_buf, lp->fd_buf_dma);
1047 lp->fd_buf = NULL;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static void
1052dump_txfd(struct TxFD *fd)
1053{
1054 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1055 le32_to_cpu(fd->fd.FDNext),
1056 le32_to_cpu(fd->fd.FDSystem),
1057 le32_to_cpu(fd->fd.FDStat),
1058 le32_to_cpu(fd->fd.FDCtl));
1059 printk("BD: ");
1060 printk(" %08x %08x",
1061 le32_to_cpu(fd->bd.BuffData),
1062 le32_to_cpu(fd->bd.BDCtl));
1063 printk("\n");
1064}
1065
1066static int
1067dump_rxfd(struct RxFD *fd)
1068{
1069 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1070 if (bd_count > 8)
1071 bd_count = 8;
1072 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1073 le32_to_cpu(fd->fd.FDNext),
1074 le32_to_cpu(fd->fd.FDSystem),
1075 le32_to_cpu(fd->fd.FDStat),
1076 le32_to_cpu(fd->fd.FDCtl));
1077 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
1078 return 0;
1079 printk("BD: ");
1080 for (i = 0; i < bd_count; i++)
1081 printk(" %08x %08x",
1082 le32_to_cpu(fd->bd[i].BuffData),
1083 le32_to_cpu(fd->bd[i].BDCtl));
1084 printk("\n");
1085 return bd_count;
1086}
1087
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001088#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089static void
1090dump_frfd(struct FrFD *fd)
1091{
1092 int i;
1093 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1094 le32_to_cpu(fd->fd.FDNext),
1095 le32_to_cpu(fd->fd.FDSystem),
1096 le32_to_cpu(fd->fd.FDStat),
1097 le32_to_cpu(fd->fd.FDCtl));
1098 printk("BD: ");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001099 for (i = 0; i < RX_BUF_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(" %08x %08x",
1101 le32_to_cpu(fd->bd[i].BuffData),
1102 le32_to_cpu(fd->bd[i].BDCtl));
1103 printk("\n");
1104}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001107#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108static void
1109panic_queues(struct net_device *dev)
1110{
1111 struct tc35815_local *lp = dev->priv;
1112 int i;
1113
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001114 printk("TxFD base %p, start %u, end %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1116 printk("RxFD base %p limit %p cur %p\n",
1117 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1118 printk("FrFD %p\n", lp->fbl_ptr);
1119 for (i = 0; i < TX_FD_NUM; i++)
1120 dump_txfd(&lp->tfd_base[i]);
1121 for (i = 0; i < RX_FD_NUM; i++) {
1122 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1123 i += (bd_count + 1) / 2; /* skip BDs */
1124 }
1125 dump_frfd(lp->fbl_ptr);
1126 panic("%s: Illegal queue state.", dev->name);
1127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129
1130static void print_eth(char *add)
1131{
1132 int i;
1133
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001134 printk("print_eth(%p)\n", add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 for (i = 0; i < 6; i++)
1136 printk(" %2.2X", (unsigned char) add[i + 6]);
1137 printk(" =>");
1138 for (i = 0; i < 6; i++)
1139 printk(" %2.2X", (unsigned char) add[i]);
1140 printk(" : %2.2X%2.2X\n", (unsigned char) add[12], (unsigned char) add[13]);
1141}
1142
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001143static int tc35815_tx_full(struct net_device *dev)
1144{
1145 struct tc35815_local *lp = dev->priv;
1146 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1147}
1148
1149static void tc35815_restart(struct net_device *dev)
1150{
1151 struct tc35815_local *lp = dev->priv;
1152 int pid = lp->phy_addr;
1153 int do_phy_reset = 1;
1154 del_timer(&lp->timer); /* Kill if running */
1155
1156 if (lp->mii_id[0] == 0x0016 && (lp->mii_id[1] & 0xfc00) == 0xf800) {
1157 /* Resetting PHY cause problem on some chip... (SEEQ 80221) */
1158 do_phy_reset = 0;
1159 }
1160 if (do_phy_reset) {
1161 int timeout;
1162 tc_mdio_write(dev, pid, MII_BMCR, BMCR_RESET);
1163 timeout = 100;
1164 while (--timeout) {
1165 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_RESET))
1166 break;
1167 udelay(1);
1168 }
1169 if (!timeout)
1170 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1171 }
1172
1173 tc35815_chip_reset(dev);
1174 tc35815_clear_queues(dev);
1175 tc35815_chip_init(dev);
1176 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1177 tc35815_set_multicast_list(dev);
1178}
1179
1180static void tc35815_tx_timeout(struct net_device *dev)
1181{
1182 struct tc35815_local *lp = dev->priv;
1183 struct tc35815_regs __iomem *tr =
1184 (struct tc35815_regs __iomem *)dev->base_addr;
1185
1186 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1187 dev->name, tc_readl(&tr->Tx_Stat));
1188
1189 /* Try to restart the adaptor. */
1190 spin_lock_irq(&lp->lock);
1191 tc35815_restart(dev);
1192 spin_unlock_irq(&lp->lock);
1193
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001194 dev->stats.tx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001195
1196 /* If we have space available to accept new transmit
1197 * requests, wake up the queueing layer. This would
1198 * be the case if the chipset_init() call above just
1199 * flushes out the tx queue and empties it.
1200 *
1201 * If instead, the tx queue is retained then the
1202 * netif_wake_queue() call should be placed in the
1203 * TX completion interrupt handler of the driver instead
1204 * of here.
1205 */
1206 if (!tc35815_tx_full(dev))
1207 netif_wake_queue(dev);
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/*
1211 * Open/initialize the board. This is called (in the current kernel)
1212 * sometime after booting when the 'ifconfig' program is run.
1213 *
1214 * This routine should set everything up anew at each open, even
1215 * registers that "should" only need to be set once at boot, so that
1216 * there is non-reboot way to recover if something goes wrong.
1217 */
1218static int
1219tc35815_open(struct net_device *dev)
1220{
1221 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /*
1224 * This is used if the interrupt line can turned off (shared).
1225 * See 3c503.c for an example of selecting the IRQ at config-time.
1226 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001227 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, dev->name, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return -EAGAIN;
1229 }
1230
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001231 del_timer(&lp->timer); /* Kill if running */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 tc35815_chip_reset(dev);
1233
1234 if (tc35815_init_queues(dev) != 0) {
1235 free_irq(dev->irq, dev);
1236 return -EAGAIN;
1237 }
1238
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001239#ifdef TC35815_NAPI
1240 napi_enable(&lp->napi);
1241#endif
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Reset the hardware here. Don't forget to set the station address. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001244 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 tc35815_chip_init(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001246 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001248 /* We are now ready to accept transmit requeusts from
1249 * the queueing layer of the networking.
1250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 netif_start_queue(dev);
1252
1253 return 0;
1254}
1255
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001256/* This will only be invoked if your driver is _not_ in XOFF state.
1257 * What this means is that you need not check it, and that this
1258 * invariant will hold if you make sure that the netif_*_queue()
1259 * calls are done at the proper times.
1260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1262{
1263 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001264 struct TxFD *txfd;
1265 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001267 /* If some error occurs while trying to transmit this
1268 * packet, you should return '1' from this function.
1269 * In such a case you _may not_ do anything to the
1270 * SKB, it is still owned by the network queueing
1271 * layer when an error is returned. This means you
1272 * may not modify any SKB fields, you may not free
1273 * the SKB, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001276 /* This is the most common case for modern hardware.
1277 * The spinlock protects this code from the TX complete
1278 * hardware interrupt handler. Queue flow control is
1279 * thus managed under this lock as well.
1280 */
1281 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001283 /* failsafe... (handle txdone now if half of FDs are used) */
1284 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1285 TX_FD_NUM / 2)
1286 tc35815_txdone(dev);
1287
1288 if (netif_msg_pktdata(lp))
1289 print_eth(skb->data);
1290#ifdef DEBUG
1291 if (lp->tx_skbs[lp->tfd_start].skb) {
1292 printk("%s: tx_skbs conflict.\n", dev->name);
1293 panic_queues(dev);
1294 }
1295#else
1296 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001298 lp->tx_skbs[lp->tfd_start].skb = skb;
1299 lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001301 /*add to ring */
1302 txfd = &lp->tfd_base[lp->tfd_start];
1303 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1304 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1305 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1306 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001308 if (lp->tfd_start == lp->tfd_end) {
1309 struct tc35815_regs __iomem *tr =
1310 (struct tc35815_regs __iomem *)dev->base_addr;
1311 /* Start DMA Transmitter. */
1312 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#ifdef GATHER_TXINT
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001314 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001316 if (netif_msg_tx_queued(lp)) {
1317 printk("%s: starting TxFD.\n", dev->name);
1318 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001320 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1321 } else {
1322 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1323 if (netif_msg_tx_queued(lp)) {
1324 printk("%s: queueing TxFD.\n", dev->name);
1325 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001327 }
1328 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1329
1330 dev->trans_start = jiffies;
1331
1332 /* If we just used up the very last entry in the
1333 * TX ring on this device, tell the queueing
1334 * layer to send no more.
1335 */
1336 if (tc35815_tx_full(dev)) {
1337 if (netif_msg_tx_queued(lp))
1338 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1339 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001342 /* When the TX completion hw interrupt arrives, this
1343 * is when the transmit statistics are updated.
1344 */
1345
1346 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348}
1349
1350#define FATAL_ERROR_INT \
1351 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001352static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 static int count;
1355 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1356 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (status & Int_IntPCI)
1358 printk(" IntPCI");
1359 if (status & Int_DmParErr)
1360 printk(" DmParErr");
1361 if (status & Int_IntNRAbt)
1362 printk(" IntNRAbt");
1363 printk("\n");
1364 if (count++ > 100)
1365 panic("%s: Too many fatal errors.", dev->name);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001366 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Try to restart the adaptor. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001368 tc35815_restart(dev);
1369}
1370
1371#ifdef TC35815_NAPI
1372static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
1373#else
1374static int tc35815_do_interrupt(struct net_device *dev, u32 status)
1375#endif
1376{
1377 struct tc35815_local *lp = dev->priv;
1378 struct tc35815_regs __iomem *tr =
1379 (struct tc35815_regs __iomem *)dev->base_addr;
1380 int ret = -1;
1381
1382 /* Fatal errors... */
1383 if (status & FATAL_ERROR_INT) {
1384 tc35815_fatal_error_interrupt(dev, status);
1385 return 0;
1386 }
1387 /* recoverable errors */
1388 if (status & Int_IntFDAEx) {
1389 /* disable FDAEx int. (until we make rooms...) */
1390 tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
1391 printk(KERN_WARNING
1392 "%s: Free Descriptor Area Exhausted (%#x).\n",
1393 dev->name, status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001394 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001395 ret = 0;
1396 }
1397 if (status & Int_IntBLEx) {
1398 /* disable BLEx int. (until we make rooms...) */
1399 tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
1400 printk(KERN_WARNING
1401 "%s: Buffer List Exhausted (%#x).\n",
1402 dev->name, status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001403 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001404 ret = 0;
1405 }
1406 if (status & Int_IntExBD) {
1407 printk(KERN_WARNING
1408 "%s: Excessive Buffer Descriptiors (%#x).\n",
1409 dev->name, status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001410 dev->stats.rx_length_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001411 ret = 0;
1412 }
1413
1414 /* normal notification */
1415 if (status & Int_IntMacRx) {
1416 /* Got a packet(s). */
1417#ifdef TC35815_NAPI
1418 ret = tc35815_rx(dev, limit);
1419#else
1420 tc35815_rx(dev);
1421 ret = 0;
1422#endif
1423 lp->lstats.rx_ints++;
1424 }
1425 if (status & Int_IntMacTx) {
1426 /* Transmit complete. */
1427 lp->lstats.tx_ints++;
1428 tc35815_txdone(dev);
1429 netif_wake_queue(dev);
1430 ret = 0;
1431 }
1432 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435/*
1436 * The typical workload of the driver:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001437 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
David Howells7d12e782006-10-05 14:55:46 +01001439static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct net_device *dev = dev_id;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001442 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001443 struct tc35815_regs __iomem *tr =
1444 (struct tc35815_regs __iomem *)dev->base_addr;
1445#ifdef TC35815_NAPI
1446 u32 dmactl = tc_readl(&tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001448 if (!(dmactl & DMA_IntMask)) {
1449 /* disable interrupts */
1450 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001451 if (netif_rx_schedule_prep(dev, &lp->napi))
1452 __netif_rx_schedule(dev, &lp->napi);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001453 else {
1454 printk(KERN_ERR "%s: interrupt taken in poll\n",
1455 dev->name);
1456 BUG();
1457 }
1458 (void)tc_readl(&tr->Int_Src); /* flush */
1459 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001461 return IRQ_NONE;
1462#else
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001463 int handled;
1464 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001466 spin_lock(&lp->lock);
1467 status = tc_readl(&tr->Int_Src);
1468 tc_writel(status, &tr->Int_Src); /* write to clear */
1469 handled = tc35815_do_interrupt(dev, status);
1470 (void)tc_readl(&tr->Int_Src); /* flush */
1471 spin_unlock(&lp->lock);
1472 return IRQ_RETVAL(handled >= 0);
1473#endif /* TC35815_NAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001476#ifdef CONFIG_NET_POLL_CONTROLLER
1477static void tc35815_poll_controller(struct net_device *dev)
1478{
1479 disable_irq(dev->irq);
1480 tc35815_interrupt(dev->irq, dev);
1481 enable_irq(dev->irq);
1482}
1483#endif
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/* We have a good packet(s), get it/them out of the buffers. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001486#ifdef TC35815_NAPI
1487static int
1488tc35815_rx(struct net_device *dev, int limit)
1489#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490static void
1491tc35815_rx(struct net_device *dev)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001492#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct tc35815_local *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 unsigned int fdctl;
1496 int i;
1497 int buf_free_count = 0;
1498 int fd_free_count = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001499#ifdef TC35815_NAPI
1500 int received = 0;
1501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1504 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1505 int pkt_len = fdctl & FD_FDLength_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001507#ifdef DEBUG
1508 struct RxFD *next_rfd;
1509#endif
1510#if (RX_CTL_CMD & Rx_StripCRC) == 0
1511 pkt_len -= 4;
1512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001514 if (netif_msg_rx_status(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dump_rxfd(lp->rfd_cur);
1516 if (status & Rx_Good) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 struct sk_buff *skb;
1518 unsigned char *data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001519 int cur_bd;
1520#ifdef TC35815_USE_PACKEDBUFFER
1521 int offset;
1522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001524#ifdef TC35815_NAPI
1525 if (--limit < 0)
1526 break;
1527#endif
1528#ifdef TC35815_USE_PACKEDBUFFER
1529 BUG_ON(bd_count > 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */
1531 if (skb == NULL) {
1532 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
1533 dev->name);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001534 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536 }
1537 skb_reserve(skb, 2); /* 16 bit alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 data = skb_put(skb, pkt_len);
1540
1541 /* copy from receive buffer */
1542 cur_bd = 0;
1543 offset = 0;
1544 while (offset < pkt_len && cur_bd < bd_count) {
1545 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1546 BD_BuffLength_MASK;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001547 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1548 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1549 if (offset + len > pkt_len)
1550 len = pkt_len - offset;
1551#ifdef TC35815_DMA_SYNC_ONDEMAND
1552 pci_dma_sync_single_for_cpu(lp->pci_dev,
1553 dma, len,
1554 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555#endif
1556 memcpy(data + offset, rxbuf, len);
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +09001557#ifdef TC35815_DMA_SYNC_ONDEMAND
1558 pci_dma_sync_single_for_device(lp->pci_dev,
1559 dma, len,
1560 PCI_DMA_FROMDEVICE);
1561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 offset += len;
1563 cur_bd++;
1564 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001565#else /* TC35815_USE_PACKEDBUFFER */
1566 BUG_ON(bd_count > 1);
1567 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1568 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1569#ifdef DEBUG
1570 if (cur_bd >= RX_BUF_NUM) {
1571 printk("%s: invalid BDID.\n", dev->name);
1572 panic_queues(dev);
1573 }
1574 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1575 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1576 if (!lp->rx_skbs[cur_bd].skb) {
1577 printk("%s: NULL skb.\n", dev->name);
1578 panic_queues(dev);
1579 }
1580#else
1581 BUG_ON(cur_bd >= RX_BUF_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001583 skb = lp->rx_skbs[cur_bd].skb;
1584 prefetch(skb->data);
1585 lp->rx_skbs[cur_bd].skb = NULL;
1586 lp->fbl_count--;
1587 pci_unmap_single(lp->pci_dev,
1588 lp->rx_skbs[cur_bd].skb_dma,
1589 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1590 if (!HAVE_DMA_RXALIGN(lp))
1591 memmove(skb->data, skb->data - 2, pkt_len);
1592 data = skb_put(skb, pkt_len);
1593#endif /* TC35815_USE_PACKEDBUFFER */
1594 if (netif_msg_pktdata(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 print_eth(data);
1596 skb->protocol = eth_type_trans(skb, dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001597#ifdef TC35815_NAPI
1598 netif_receive_skb(skb);
1599 received++;
1600#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 netif_rx(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001602#endif
1603 dev->last_rx = jiffies;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001604 dev->stats.rx_packets++;
1605 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 } else {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001607 dev->stats.rx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001608 printk(KERN_DEBUG "%s: Rx error (status %x)\n",
1609 dev->name, status & Rx_Stat_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1611 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1612 status &= ~(Rx_LongErr|Rx_CRCErr);
1613 status |= Rx_Over;
1614 }
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001615 if (status & Rx_LongErr)
1616 dev->stats.rx_length_errors++;
1617 if (status & Rx_Over)
1618 dev->stats.rx_fifo_errors++;
1619 if (status & Rx_CRCErr)
1620 dev->stats.rx_crc_errors++;
1621 if (status & Rx_Align)
1622 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
1625 if (bd_count > 0) {
1626 /* put Free Buffer back to controller */
1627 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1628 unsigned char id =
1629 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001630#ifdef DEBUG
1631 if (id >= RX_BUF_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 printk("%s: invalid BDID.\n", dev->name);
1633 panic_queues(dev);
1634 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001635#else
1636 BUG_ON(id >= RX_BUF_NUM);
1637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /* free old buffers */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001639#ifdef TC35815_USE_PACKEDBUFFER
1640 while (lp->fbl_curid != id)
1641#else
1642 while (lp->fbl_count < RX_BUF_NUM)
1643#endif
1644 {
1645#ifdef TC35815_USE_PACKEDBUFFER
1646 unsigned char curid = lp->fbl_curid;
1647#else
1648 unsigned char curid =
1649 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1650#endif
1651 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1652#ifdef DEBUG
1653 bdctl = le32_to_cpu(bd->BDCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (bdctl & BD_CownsBD) {
1655 printk("%s: Freeing invalid BD.\n",
1656 dev->name);
1657 panic_queues(dev);
1658 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001659#endif
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001660 /* pass BD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001661#ifndef TC35815_USE_PACKEDBUFFER
1662 if (!lp->rx_skbs[curid].skb) {
1663 lp->rx_skbs[curid].skb =
1664 alloc_rxbuf_skb(dev,
1665 lp->pci_dev,
1666 &lp->rx_skbs[curid].skb_dma);
1667 if (!lp->rx_skbs[curid].skb)
1668 break; /* try on next reception */
1669 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1670 }
1671#endif /* TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /* Note: BDLength was modified by chip. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001673 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1674 (curid << BD_RxBDID_SHIFT) |
1675 RX_BUF_SIZE);
1676#ifdef TC35815_USE_PACKEDBUFFER
1677 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1678 if (netif_msg_rx_status(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 printk("%s: Entering new FBD %d\n",
1680 dev->name, lp->fbl_curid);
1681 dump_frfd(lp->fbl_ptr);
1682 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001683#else
1684 lp->fbl_count++;
1685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 buf_free_count++;
1687 }
1688 }
1689
1690 /* put RxFD back to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001691#ifdef DEBUG
1692 next_rfd = fd_bus_to_virt(lp,
1693 le32_to_cpu(lp->rfd_cur->fd.FDNext));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1695 printk("%s: RxFD FDNext invalid.\n", dev->name);
1696 panic_queues(dev);
1697 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001700 /* pass FD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001701#ifdef DEBUG
1702 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1703#else
1704 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1707 lp->rfd_cur++;
1708 fd_free_count++;
1709 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001710 if (lp->rfd_cur > lp->rfd_limit)
1711 lp->rfd_cur = lp->rfd_base;
1712#ifdef DEBUG
1713 if (lp->rfd_cur != next_rfd)
1714 printk("rfd_cur = %p, next_rfd %p\n",
1715 lp->rfd_cur, next_rfd);
1716#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
1719 /* re-enable BL/FDA Exhaust interrupts. */
1720 if (fd_free_count) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001721 struct tc35815_regs __iomem *tr =
1722 (struct tc35815_regs __iomem *)dev->base_addr;
1723 u32 en, en_old = tc_readl(&tr->Int_En);
1724 en = en_old | Int_FDAExEn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (buf_free_count)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001726 en |= Int_BLExEn;
1727 if (en != en_old)
1728 tc_writel(en, &tr->Int_En);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001730#ifdef TC35815_NAPI
1731 return received;
1732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001735#ifdef TC35815_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001736static int tc35815_poll(struct napi_struct *napi, int budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001737{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001738 struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
1739 struct net_device *dev = lp->dev;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001740 struct tc35815_regs __iomem *tr =
1741 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001742 int received = 0, handled;
1743 u32 status;
1744
1745 spin_lock(&lp->lock);
1746 status = tc_readl(&tr->Int_Src);
1747 do {
1748 tc_writel(status, &tr->Int_Src); /* write to clear */
1749
1750 handled = tc35815_do_interrupt(dev, status, limit);
1751 if (handled >= 0) {
1752 received += handled;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001753 if (received >= budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001754 break;
1755 }
1756 status = tc_readl(&tr->Int_Src);
1757 } while (status);
1758 spin_unlock(&lp->lock);
1759
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001760 if (received < budget) {
1761 netif_rx_complete(dev, napi);
1762 /* enable interrupts */
1763 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1764 }
1765 return received;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001766}
1767#endif
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769#ifdef NO_CHECK_CARRIER
1770#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1771#else
1772#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1773#endif
1774
1775static void
1776tc35815_check_tx_stat(struct net_device *dev, int status)
1777{
1778 struct tc35815_local *lp = dev->priv;
1779 const char *msg = NULL;
1780
1781 /* count collisions */
1782 if (status & Tx_ExColl)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001783 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (status & Tx_TxColl_MASK)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001785 dev->stats.collisions += status & Tx_TxColl_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001787#ifndef NO_CHECK_CARRIER
1788 /* TX4939 does not have NCarr */
1789 if (lp->boardtype == TC35815_TX4939)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 status &= ~Tx_NCarr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001791#ifdef WORKAROUND_LOSTCAR
1792 /* WORKAROUND: ignore LostCrS in full duplex operation */
1793 if ((lp->timer_state != asleep && lp->timer_state != lcheck)
1794 || lp->fullduplex)
1795 status &= ~Tx_NCarr;
1796#endif
1797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 if (!(status & TX_STA_ERR)) {
1800 /* no error. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001801 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return;
1803 }
1804
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001805 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (status & Tx_ExColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001807 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 msg = "Excessive Collision.";
1809 }
1810 if (status & Tx_Under) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001811 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 msg = "Tx FIFO Underrun.";
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001813 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1814 lp->lstats.tx_underrun++;
1815 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1816 struct tc35815_regs __iomem *tr =
1817 (struct tc35815_regs __iomem *)dev->base_addr;
1818 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1819 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1820 }
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 if (status & Tx_Defer) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001824 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 msg = "Excessive Deferral.";
1826 }
1827#ifndef NO_CHECK_CARRIER
1828 if (status & Tx_NCarr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001829 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 msg = "Lost Carrier Sense.";
1831 }
1832#endif
1833 if (status & Tx_LateColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001834 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 msg = "Late Collision.";
1836 }
1837 if (status & Tx_TxPar) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001838 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 msg = "Transmit Parity Error.";
1840 }
1841 if (status & Tx_SQErr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001842 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 msg = "Signal Quality Error.";
1844 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001845 if (msg && netif_msg_tx_err(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
1847}
1848
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001849/* This handles TX complete events posted by the device
1850 * via interrupts.
1851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852static void
1853tc35815_txdone(struct net_device *dev)
1854{
1855 struct tc35815_local *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct TxFD *txfd;
1857 unsigned int fdctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 txfd = &lp->tfd_base[lp->tfd_end];
1860 while (lp->tfd_start != lp->tfd_end &&
1861 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
1862 int status = le32_to_cpu(txfd->fd.FDStat);
1863 struct sk_buff *skb;
1864 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001865 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001867 if (netif_msg_tx_done(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 printk("%s: complete TxFD.\n", dev->name);
1869 dump_txfd(txfd);
1870 }
1871 tc35815_check_tx_stat(dev, status);
1872
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001873 skb = fdsystem != 0xffffffff ?
1874 lp->tx_skbs[fdsystem].skb : NULL;
1875#ifdef DEBUG
1876 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
1877 printk("%s: tx_skbs mismatch.\n", dev->name);
1878 panic_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001880#else
1881 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
1882#endif
1883 if (skb) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001884 dev->stats.tx_bytes += skb->len;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001885 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
1886 lp->tx_skbs[lp->tfd_end].skb = NULL;
1887 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
1888#ifdef TC35815_NAPI
1889 dev_kfree_skb_any(skb);
1890#else
1891 dev_kfree_skb_irq(skb);
1892#endif
1893 }
1894 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
1897 txfd = &lp->tfd_base[lp->tfd_end];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001898#ifdef DEBUG
1899 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 printk("%s: TxFD FDNext invalid.\n", dev->name);
1901 panic_queues(dev);
1902 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (fdnext & FD_Next_EOL) {
1905 /* DMA Transmitter has been stopping... */
1906 if (lp->tfd_end != lp->tfd_start) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001907 struct tc35815_regs __iomem *tr =
1908 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
1910 struct TxFD* txhead = &lp->tfd_base[head];
1911 int qlen = (lp->tfd_start + TX_FD_NUM
1912 - lp->tfd_end) % TX_FD_NUM;
1913
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001914#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
1916 printk("%s: TxFD FDCtl invalid.\n", dev->name);
1917 panic_queues(dev);
1918 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001919#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /* log max queue length */
1921 if (lp->lstats.max_tx_qlen < qlen)
1922 lp->lstats.max_tx_qlen = qlen;
1923
1924
1925 /* start DMA Transmitter again */
1926 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
1927#ifdef GATHER_TXINT
1928 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
1929#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001930 if (netif_msg_tx_queued(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 printk("%s: start TxFD on queue.\n",
1932 dev->name);
1933 dump_txfd(txfd);
1934 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001935 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 break;
1938 }
1939 }
1940
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001941 /* If we had stopped the queue due to a "tx full"
1942 * condition, and space has now been made available,
1943 * wake up the queue.
1944 */
1945 if (netif_queue_stopped(dev) && ! tc35815_tx_full(dev))
1946 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
1949/* The inverse routine to tc35815_open(). */
1950static int
1951tc35815_close(struct net_device *dev)
1952{
1953 struct tc35815_local *lp = dev->priv;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001956#ifdef TC35815_NAPI
1957 napi_disable(&lp->napi);
1958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* Flush the Tx and disable Rx here. */
1961
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001962 del_timer(&lp->timer); /* Kill if running */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 tc35815_chip_reset(dev);
1964 free_irq(dev->irq, dev);
1965
1966 tc35815_free_queues(dev);
1967
1968 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972/*
1973 * Get the current statistics.
1974 * This may be called with the card open or closed.
1975 */
1976static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
1977{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001978 struct tc35815_regs __iomem *tr =
1979 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001980 if (netif_running(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* Update the statistics from the device registers. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001982 dev->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001984 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001987static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001989 struct tc35815_local *lp = dev->priv;
1990 struct tc35815_regs __iomem *tr =
1991 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int cam_index = index * 6;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001993 u32 cam_data;
1994 u32 saved_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 saved_addr = tc_readl(&tr->CAM_Adr);
1996
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001997 if (netif_msg_hw(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int i;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001999 printk(KERN_DEBUG "%s: CAM %d:", dev->name, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 for (i = 0; i < 6; i++)
2001 printk(" %02x", addr[i]);
2002 printk("\n");
2003 }
2004 if (index & 1) {
2005 /* read modify write */
2006 tc_writel(cam_index - 2, &tr->CAM_Adr);
2007 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
2008 cam_data |= addr[0] << 8 | addr[1];
2009 tc_writel(cam_data, &tr->CAM_Data);
2010 /* write whole word */
2011 tc_writel(cam_index + 2, &tr->CAM_Adr);
2012 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
2013 tc_writel(cam_data, &tr->CAM_Data);
2014 } else {
2015 /* write whole word */
2016 tc_writel(cam_index, &tr->CAM_Adr);
2017 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
2018 tc_writel(cam_data, &tr->CAM_Data);
2019 /* read modify write */
2020 tc_writel(cam_index + 4, &tr->CAM_Adr);
2021 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
2022 cam_data |= addr[4] << 24 | (addr[5] << 16);
2023 tc_writel(cam_data, &tr->CAM_Data);
2024 }
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 tc_writel(saved_addr, &tr->CAM_Adr);
2027}
2028
2029
2030/*
2031 * Set or clear the multicast filter for this adaptor.
2032 * num_addrs == -1 Promiscuous mode, receive all packets
2033 * num_addrs == 0 Normal mode, clear multicast list
2034 * num_addrs > 0 Multicast mode, receive normal and MC packets,
2035 * and do best-effort filtering.
2036 */
2037static void
2038tc35815_set_multicast_list(struct net_device *dev)
2039{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002040 struct tc35815_regs __iomem *tr =
2041 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 if (dev->flags&IFF_PROMISC)
2044 {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002045#ifdef WORKAROUND_100HALF_PROMISC
2046 /* With some (all?) 100MHalf HUB, controller will hang
2047 * if we enabled promiscuous mode before linkup... */
2048 struct tc35815_local *lp = dev->priv;
2049 int pid = lp->phy_addr;
2050 if (!(tc_mdio_read(dev, pid, MII_BMSR) & BMSR_LSTATUS))
2051 return;
2052#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /* Enable promiscuous mode */
2054 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
2055 }
2056 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > CAM_ENTRY_MAX - 3)
2057 {
2058 /* CAM 0, 1, 20 are reserved. */
2059 /* Disable promiscuous mode, use normal mode. */
2060 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
2061 }
2062 else if(dev->mc_count)
2063 {
2064 struct dev_mc_list* cur_addr = dev->mc_list;
2065 int i;
2066 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
2067
2068 tc_writel(0, &tr->CAM_Ctl);
2069 /* Walk the address list, and load the filter */
2070 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
2071 if (!cur_addr)
2072 break;
2073 /* entry 0,1 is reserved. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002074 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 ena_bits |= CAM_Ena_Bit(i + 2);
2076 }
2077 tc_writel(ena_bits, &tr->CAM_Ena);
2078 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2079 }
2080 else {
2081 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2082 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2083 }
2084}
2085
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002086static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002089 strcpy(info->driver, MODNAME);
2090 strcpy(info->version, DRV_VERSION);
2091 strcpy(info->bus_info, pci_name(lp->pci_dev));
2092}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002093
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002094static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2095{
2096 struct tc35815_local *lp = dev->priv;
2097 spin_lock_irq(&lp->lock);
2098 mii_ethtool_gset(&lp->mii, cmd);
2099 spin_unlock_irq(&lp->lock);
2100 return 0;
2101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002103static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2104{
2105 struct tc35815_local *lp = dev->priv;
2106 int rc;
2107#if 1 /* use our negotiation method... */
2108 /* Verify the settings we care about. */
2109 if (cmd->autoneg != AUTONEG_ENABLE &&
2110 cmd->autoneg != AUTONEG_DISABLE)
2111 return -EINVAL;
2112 if (cmd->autoneg == AUTONEG_DISABLE &&
2113 ((cmd->speed != SPEED_100 &&
2114 cmd->speed != SPEED_10) ||
2115 (cmd->duplex != DUPLEX_HALF &&
2116 cmd->duplex != DUPLEX_FULL)))
2117 return -EINVAL;
2118
2119 /* Ok, do it to it. */
2120 spin_lock_irq(&lp->lock);
2121 del_timer(&lp->timer);
2122 tc35815_start_auto_negotiation(dev, cmd);
2123 spin_unlock_irq(&lp->lock);
2124 rc = 0;
2125#else
2126 spin_lock_irq(&lp->lock);
2127 rc = mii_ethtool_sset(&lp->mii, cmd);
2128 spin_unlock_irq(&lp->lock);
2129#endif
2130 return rc;
2131}
2132
2133static int tc35815_nway_reset(struct net_device *dev)
2134{
2135 struct tc35815_local *lp = dev->priv;
2136 int rc;
2137 spin_lock_irq(&lp->lock);
2138 rc = mii_nway_restart(&lp->mii);
2139 spin_unlock_irq(&lp->lock);
2140 return rc;
2141}
2142
2143static u32 tc35815_get_link(struct net_device *dev)
2144{
2145 struct tc35815_local *lp = dev->priv;
2146 int rc;
2147 spin_lock_irq(&lp->lock);
2148 rc = mii_link_ok(&lp->mii);
2149 spin_unlock_irq(&lp->lock);
2150 return rc;
2151}
2152
2153static u32 tc35815_get_msglevel(struct net_device *dev)
2154{
2155 struct tc35815_local *lp = dev->priv;
2156 return lp->msg_enable;
2157}
2158
2159static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2160{
2161 struct tc35815_local *lp = dev->priv;
2162 lp->msg_enable = datum;
2163}
2164
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002165static int tc35815_get_sset_count(struct net_device *dev, int sset)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002166{
2167 struct tc35815_local *lp = dev->priv;
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002168
2169 switch (sset) {
2170 case ETH_SS_STATS:
2171 return sizeof(lp->lstats) / sizeof(int);
2172 default:
2173 return -EOPNOTSUPP;
2174 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002175}
2176
2177static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2178{
2179 struct tc35815_local *lp = dev->priv;
2180 data[0] = lp->lstats.max_tx_qlen;
2181 data[1] = lp->lstats.tx_ints;
2182 data[2] = lp->lstats.rx_ints;
2183 data[3] = lp->lstats.tx_underrun;
2184}
2185
2186static struct {
2187 const char str[ETH_GSTRING_LEN];
2188} ethtool_stats_keys[] = {
2189 { "max_tx_qlen" },
2190 { "tx_ints" },
2191 { "rx_ints" },
2192 { "tx_underrun" },
2193};
2194
2195static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2196{
2197 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2198}
2199
2200static const struct ethtool_ops tc35815_ethtool_ops = {
2201 .get_drvinfo = tc35815_get_drvinfo,
2202 .get_settings = tc35815_get_settings,
2203 .set_settings = tc35815_set_settings,
2204 .nway_reset = tc35815_nway_reset,
2205 .get_link = tc35815_get_link,
2206 .get_msglevel = tc35815_get_msglevel,
2207 .set_msglevel = tc35815_set_msglevel,
2208 .get_strings = tc35815_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002209 .get_sset_count = tc35815_get_sset_count,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002210 .get_ethtool_stats = tc35815_get_ethtool_stats,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002211};
2212
2213static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2214{
2215 struct tc35815_local *lp = dev->priv;
2216 int rc;
2217
2218 if (!netif_running(dev))
2219 return -EINVAL;
2220
2221 spin_lock_irq(&lp->lock);
2222 rc = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
2223 spin_unlock_irq(&lp->lock);
2224
2225 return rc;
2226}
2227
2228static int tc_mdio_read(struct net_device *dev, int phy_id, int location)
2229{
2230 struct tc35815_regs __iomem *tr =
2231 (struct tc35815_regs __iomem *)dev->base_addr;
2232 u32 data;
2233 tc_writel(MD_CA_Busy | (phy_id << 5) | location, &tr->MD_CA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2235 ;
2236 data = tc_readl(&tr->MD_Data);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002237 return data & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002240static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
2241 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002243 struct tc35815_regs __iomem *tr =
2244 (struct tc35815_regs __iomem *)dev->base_addr;
2245 tc_writel(val, &tr->MD_Data);
2246 tc_writel(MD_CA_Busy | MD_CA_Wr | (phy_id << 5) | location, &tr->MD_CA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2248 ;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002249}
2250
2251/* Auto negotiation. The scheme is very simple. We have a timer routine
2252 * that keeps watching the auto negotiation process as it progresses.
2253 * The DP83840 is first told to start doing it's thing, we set up the time
2254 * and place the timer state machine in it's initial state.
2255 *
2256 * Here the timer peeks at the DP83840 status registers at each click to see
2257 * if the auto negotiation has completed, we assume here that the DP83840 PHY
2258 * will time out at some point and just tell us what (didn't) happen. For
2259 * complete coverage we only allow so many of the ticks at this level to run,
2260 * when this has expired we print a warning message and try another strategy.
2261 * This "other" strategy is to force the interface into various speed/duplex
2262 * configurations and we stop when we see a link-up condition before the
2263 * maximum number of "peek" ticks have occurred.
2264 *
2265 * Once a valid link status has been detected we configure the BigMAC and
2266 * the rest of the Happy Meal to speak the most efficient protocol we could
2267 * get a clean link for. The priority for link configurations, highest first
2268 * is:
2269 * 100 Base-T Full Duplex
2270 * 100 Base-T Half Duplex
2271 * 10 Base-T Full Duplex
2272 * 10 Base-T Half Duplex
2273 *
2274 * We start a new timer now, after a successful auto negotiation status has
2275 * been detected. This timer just waits for the link-up bit to get set in
2276 * the BMCR of the DP83840. When this occurs we print a kernel log message
2277 * describing the link type in use and the fact that it is up.
2278 *
2279 * If a fatal error of some sort is signalled and detected in the interrupt
2280 * service routine, and the chip is reset, or the link is ifconfig'd down
2281 * and then back up, this entire process repeats itself all over again.
2282 */
2283/* Note: Above comments are come from sunhme driver. */
2284
2285static int tc35815_try_next_permutation(struct net_device *dev)
2286{
2287 struct tc35815_local *lp = dev->priv;
2288 int pid = lp->phy_addr;
2289 unsigned short bmcr;
2290
2291 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2292
2293 /* Downgrade from full to half duplex. Only possible via ethtool. */
2294 if (bmcr & BMCR_FULLDPLX) {
2295 bmcr &= ~BMCR_FULLDPLX;
2296 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2297 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2298 return 0;
2299 }
2300
2301 /* Downgrade from 100 to 10. */
2302 if (bmcr & BMCR_SPEED100) {
2303 bmcr &= ~BMCR_SPEED100;
2304 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2305 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2306 return 0;
2307 }
2308
2309 /* We've tried everything. */
2310 return -1;
2311}
2312
2313static void
2314tc35815_display_link_mode(struct net_device *dev)
2315{
2316 struct tc35815_local *lp = dev->priv;
2317 int pid = lp->phy_addr;
2318 unsigned short lpa, bmcr;
2319 char *speed = "", *duplex = "";
2320
2321 lpa = tc_mdio_read(dev, pid, MII_LPA);
2322 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2323 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2324 speed = "100Mb/s";
2325 else
2326 speed = "10Mb/s";
2327 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2328 duplex = "Full Duplex";
2329 else
2330 duplex = "Half Duplex";
2331
2332 if (netif_msg_link(lp))
2333 printk(KERN_INFO "%s: Link is up at %s, %s.\n",
2334 dev->name, speed, duplex);
2335 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2336 dev->name,
2337 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2338}
2339
2340static void tc35815_display_forced_link_mode(struct net_device *dev)
2341{
2342 struct tc35815_local *lp = dev->priv;
2343 int pid = lp->phy_addr;
2344 unsigned short bmcr;
2345 char *speed = "", *duplex = "";
2346
2347 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2348 if (bmcr & BMCR_SPEED100)
2349 speed = "100Mb/s";
2350 else
2351 speed = "10Mb/s";
2352 if (bmcr & BMCR_FULLDPLX)
2353 duplex = "Full Duplex.\n";
2354 else
2355 duplex = "Half Duplex.\n";
2356
2357 if (netif_msg_link(lp))
2358 printk(KERN_INFO "%s: Link has been forced up at %s, %s",
2359 dev->name, speed, duplex);
2360}
2361
2362static void tc35815_set_link_modes(struct net_device *dev)
2363{
2364 struct tc35815_local *lp = dev->priv;
2365 struct tc35815_regs __iomem *tr =
2366 (struct tc35815_regs __iomem *)dev->base_addr;
2367 int pid = lp->phy_addr;
2368 unsigned short bmcr, lpa;
2369 int speed;
2370
2371 if (lp->timer_state == arbwait) {
2372 lpa = tc_mdio_read(dev, pid, MII_LPA);
2373 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2374 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2375 dev->name,
2376 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2377 if (!(lpa & (LPA_10HALF | LPA_10FULL |
2378 LPA_100HALF | LPA_100FULL))) {
2379 /* fall back to 10HALF */
2380 printk(KERN_INFO "%s: bad ability %04x - falling back to 10HD.\n",
2381 dev->name, lpa);
2382 lpa = LPA_10HALF;
2383 }
2384 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2385 lp->fullduplex = 1;
2386 else
2387 lp->fullduplex = 0;
2388 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2389 speed = 100;
2390 else
2391 speed = 10;
2392 } else {
2393 /* Forcing a link mode. */
2394 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2395 if (bmcr & BMCR_FULLDPLX)
2396 lp->fullduplex = 1;
2397 else
2398 lp->fullduplex = 0;
2399 if (bmcr & BMCR_SPEED100)
2400 speed = 100;
2401 else
2402 speed = 10;
2403 }
2404
2405 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_HaltReq, &tr->MAC_Ctl);
2406 if (lp->fullduplex) {
2407 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl);
2408 } else {
2409 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_FullDup, &tr->MAC_Ctl);
2410 }
2411 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_HaltReq, &tr->MAC_Ctl);
2412
2413 /* TX4939 PCFG.SPEEDn bit will be changed on NETDEV_CHANGE event. */
2414
2415#ifndef NO_CHECK_CARRIER
2416 /* TX4939 does not have EnLCarr */
2417 if (lp->boardtype != TC35815_TX4939) {
2418#ifdef WORKAROUND_LOSTCAR
2419 /* WORKAROUND: enable LostCrS only if half duplex operation */
2420 if (!lp->fullduplex && lp->boardtype != TC35815_TX4939)
2421 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, &tr->Tx_Ctl);
2422#endif
2423 }
2424#endif
2425 lp->mii.full_duplex = lp->fullduplex;
2426}
2427
2428static void tc35815_timer(unsigned long data)
2429{
2430 struct net_device *dev = (struct net_device *)data;
2431 struct tc35815_local *lp = dev->priv;
2432 int pid = lp->phy_addr;
2433 unsigned short bmsr, bmcr, lpa;
2434 int restart_timer = 0;
2435
2436 spin_lock_irq(&lp->lock);
2437
2438 lp->timer_ticks++;
2439 switch (lp->timer_state) {
2440 case arbwait:
2441 /*
2442 * Only allow for 5 ticks, thats 10 seconds and much too
2443 * long to wait for arbitration to complete.
2444 */
2445 /* TC35815 need more times... */
2446 if (lp->timer_ticks >= 10) {
2447 /* Enter force mode. */
2448 if (!options.doforce) {
2449 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2450 " cable probblem?\n", dev->name);
2451 /* Try to restart the adaptor. */
2452 tc35815_restart(dev);
2453 goto out;
2454 }
2455 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2456 " trying force link mode\n", dev->name);
2457 printk(KERN_DEBUG "%s: BMCR %x BMSR %x\n", dev->name,
2458 tc_mdio_read(dev, pid, MII_BMCR),
2459 tc_mdio_read(dev, pid, MII_BMSR));
2460 bmcr = BMCR_SPEED100;
2461 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2462
2463 /*
2464 * OK, seems we need do disable the transceiver
2465 * for the first tick to make sure we get an
2466 * accurate link state at the second tick.
2467 */
2468
2469 lp->timer_state = ltrywait;
2470 lp->timer_ticks = 0;
2471 restart_timer = 1;
2472 } else {
2473 /* Anything interesting happen? */
2474 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2475 if (bmsr & BMSR_ANEGCOMPLETE) {
2476 /* Just what we've been waiting for... */
2477 tc35815_set_link_modes(dev);
2478
2479 /*
2480 * Success, at least so far, advance our state
2481 * engine.
2482 */
2483 lp->timer_state = lupwait;
2484 restart_timer = 1;
2485 } else {
2486 restart_timer = 1;
2487 }
2488 }
2489 break;
2490
2491 case lupwait:
2492 /*
2493 * Auto negotiation was successful and we are awaiting a
2494 * link up status. I have decided to let this timer run
2495 * forever until some sort of error is signalled, reporting
2496 * a message to the user at 10 second intervals.
2497 */
2498 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2499 if (bmsr & BMSR_LSTATUS) {
2500 /*
2501 * Wheee, it's up, display the link mode in use and put
2502 * the timer to sleep.
2503 */
2504 tc35815_display_link_mode(dev);
2505 netif_carrier_on(dev);
2506#ifdef WORKAROUND_100HALF_PROMISC
2507 /* delayed promiscuous enabling */
2508 if (dev->flags & IFF_PROMISC)
2509 tc35815_set_multicast_list(dev);
2510#endif
2511#if 1
2512 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2513 lp->timer_state = lcheck;
2514 restart_timer = 1;
2515#else
2516 lp->timer_state = asleep;
2517 restart_timer = 0;
2518#endif
2519 } else {
2520 if (lp->timer_ticks >= 10) {
2521 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
2522 "not completely up.\n", dev->name);
2523 lp->timer_ticks = 0;
2524 restart_timer = 1;
2525 } else {
2526 restart_timer = 1;
2527 }
2528 }
2529 break;
2530
2531 case ltrywait:
2532 /*
2533 * Making the timeout here too long can make it take
2534 * annoyingly long to attempt all of the link mode
2535 * permutations, but then again this is essentially
2536 * error recovery code for the most part.
2537 */
2538 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2539 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2540 if (lp->timer_ticks == 1) {
2541 /*
2542 * Re-enable transceiver, we'll re-enable the
2543 * transceiver next tick, then check link state
2544 * on the following tick.
2545 */
2546 restart_timer = 1;
2547 break;
2548 }
2549 if (lp->timer_ticks == 2) {
2550 restart_timer = 1;
2551 break;
2552 }
2553 if (bmsr & BMSR_LSTATUS) {
2554 /* Force mode selection success. */
2555 tc35815_display_forced_link_mode(dev);
2556 netif_carrier_on(dev);
2557 tc35815_set_link_modes(dev);
2558#ifdef WORKAROUND_100HALF_PROMISC
2559 /* delayed promiscuous enabling */
2560 if (dev->flags & IFF_PROMISC)
2561 tc35815_set_multicast_list(dev);
2562#endif
2563#if 1
2564 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2565 lp->timer_state = lcheck;
2566 restart_timer = 1;
2567#else
2568 lp->timer_state = asleep;
2569 restart_timer = 0;
2570#endif
2571 } else {
2572 if (lp->timer_ticks >= 4) { /* 6 seconds or so... */
2573 int ret;
2574
2575 ret = tc35815_try_next_permutation(dev);
2576 if (ret == -1) {
2577 /*
2578 * Aieee, tried them all, reset the
2579 * chip and try all over again.
2580 */
2581 printk(KERN_NOTICE "%s: Link down, "
2582 "cable problem?\n",
2583 dev->name);
2584
2585 /* Try to restart the adaptor. */
2586 tc35815_restart(dev);
2587 goto out;
2588 }
2589 lp->timer_ticks = 0;
2590 restart_timer = 1;
2591 } else {
2592 restart_timer = 1;
2593 }
2594 }
2595 break;
2596
2597 case lcheck:
2598 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2599 lpa = tc_mdio_read(dev, pid, MII_LPA);
2600 if (bmcr & (BMCR_PDOWN | BMCR_ISOLATE | BMCR_RESET)) {
2601 printk(KERN_ERR "%s: PHY down? (BMCR %x)\n", dev->name,
2602 bmcr);
2603 } else if ((lp->saved_lpa ^ lpa) &
2604 (LPA_100FULL|LPA_100HALF|LPA_10FULL|LPA_10HALF)) {
2605 printk(KERN_NOTICE "%s: link status changed"
2606 " (BMCR %x LPA %x->%x)\n", dev->name,
2607 bmcr, lp->saved_lpa, lpa);
2608 } else {
2609 /* go on */
2610 restart_timer = 1;
2611 break;
2612 }
2613 /* Try to restart the adaptor. */
2614 tc35815_restart(dev);
2615 goto out;
2616
2617 case asleep:
2618 default:
2619 /* Can't happens.... */
2620 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
2621 "one anyways!\n", dev->name);
2622 restart_timer = 0;
2623 lp->timer_ticks = 0;
2624 lp->timer_state = asleep; /* foo on you */
2625 break;
2626 }
2627
2628 if (restart_timer) {
2629 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2630 add_timer(&lp->timer);
2631 }
2632out:
2633 spin_unlock_irq(&lp->lock);
2634}
2635
2636static void tc35815_start_auto_negotiation(struct net_device *dev,
2637 struct ethtool_cmd *ep)
2638{
2639 struct tc35815_local *lp = dev->priv;
2640 int pid = lp->phy_addr;
2641 unsigned short bmsr, bmcr, advertize;
2642 int timeout;
2643
2644 netif_carrier_off(dev);
2645 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2646 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2647 advertize = tc_mdio_read(dev, pid, MII_ADVERTISE);
2648
2649 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2650 if (options.speed || options.duplex) {
2651 /* Advertise only specified configuration. */
2652 advertize &= ~(ADVERTISE_10HALF |
2653 ADVERTISE_10FULL |
2654 ADVERTISE_100HALF |
2655 ADVERTISE_100FULL);
2656 if (options.speed != 10) {
2657 if (options.duplex != 1)
2658 advertize |= ADVERTISE_100FULL;
2659 if (options.duplex != 2)
2660 advertize |= ADVERTISE_100HALF;
2661 }
2662 if (options.speed != 100) {
2663 if (options.duplex != 1)
2664 advertize |= ADVERTISE_10FULL;
2665 if (options.duplex != 2)
2666 advertize |= ADVERTISE_10HALF;
2667 }
2668 if (options.speed == 100)
2669 bmcr |= BMCR_SPEED100;
2670 else if (options.speed == 10)
2671 bmcr &= ~BMCR_SPEED100;
2672 if (options.duplex == 2)
2673 bmcr |= BMCR_FULLDPLX;
2674 else if (options.duplex == 1)
2675 bmcr &= ~BMCR_FULLDPLX;
2676 } else {
2677 /* Advertise everything we can support. */
2678 if (bmsr & BMSR_10HALF)
2679 advertize |= ADVERTISE_10HALF;
2680 else
2681 advertize &= ~ADVERTISE_10HALF;
2682 if (bmsr & BMSR_10FULL)
2683 advertize |= ADVERTISE_10FULL;
2684 else
2685 advertize &= ~ADVERTISE_10FULL;
2686 if (bmsr & BMSR_100HALF)
2687 advertize |= ADVERTISE_100HALF;
2688 else
2689 advertize &= ~ADVERTISE_100HALF;
2690 if (bmsr & BMSR_100FULL)
2691 advertize |= ADVERTISE_100FULL;
2692 else
2693 advertize &= ~ADVERTISE_100FULL;
2694 }
2695
2696 tc_mdio_write(dev, pid, MII_ADVERTISE, advertize);
2697
2698 /* Enable Auto-Negotiation, this is usually on already... */
2699 bmcr |= BMCR_ANENABLE;
2700 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2701
2702 /* Restart it to make sure it is going. */
2703 bmcr |= BMCR_ANRESTART;
2704 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2705 printk(KERN_DEBUG "%s: ADVERTISE %x BMCR %x\n", dev->name, advertize, bmcr);
2706
2707 /* BMCR_ANRESTART self clears when the process has begun. */
2708 timeout = 64; /* More than enough. */
2709 while (--timeout) {
2710 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2711 if (!(bmcr & BMCR_ANRESTART))
2712 break; /* got it. */
2713 udelay(10);
2714 }
2715 if (!timeout) {
2716 printk(KERN_ERR "%s: TC35815 would not start auto "
2717 "negotiation BMCR=0x%04x\n",
2718 dev->name, bmcr);
2719 printk(KERN_NOTICE "%s: Performing force link "
2720 "detection.\n", dev->name);
2721 goto force_link;
2722 } else {
2723 printk(KERN_DEBUG "%s: auto negotiation started.\n", dev->name);
2724 lp->timer_state = arbwait;
2725 }
2726 } else {
2727force_link:
2728 /* Force the link up, trying first a particular mode.
2729 * Either we are here at the request of ethtool or
2730 * because the Happy Meal would not start to autoneg.
2731 */
2732
2733 /* Disable auto-negotiation in BMCR, enable the duplex and
2734 * speed setting, init the timer state machine, and fire it off.
2735 */
2736 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2737 bmcr = BMCR_SPEED100;
2738 } else {
2739 if (ep->speed == SPEED_100)
2740 bmcr = BMCR_SPEED100;
2741 else
2742 bmcr = 0;
2743 if (ep->duplex == DUPLEX_FULL)
2744 bmcr |= BMCR_FULLDPLX;
2745 }
2746 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2747
2748 /* OK, seems we need do disable the transceiver for the first
2749 * tick to make sure we get an accurate link state at the
2750 * second tick.
2751 */
2752 lp->timer_state = ltrywait;
2753 }
2754
2755 del_timer(&lp->timer);
2756 lp->timer_ticks = 0;
2757 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2758 add_timer(&lp->timer);
2759}
2760
2761static void tc35815_find_phy(struct net_device *dev)
2762{
2763 struct tc35815_local *lp = dev->priv;
2764 int pid = lp->phy_addr;
2765 unsigned short id0;
2766
2767 /* find MII phy */
2768 for (pid = 31; pid >= 0; pid--) {
2769 id0 = tc_mdio_read(dev, pid, MII_BMSR);
2770 if (id0 != 0xffff && id0 != 0x0000 &&
2771 (id0 & BMSR_RESV) != (0xffff & BMSR_RESV) /* paranoia? */
2772 ) {
2773 lp->phy_addr = pid;
2774 break;
2775 }
2776 }
2777 if (pid < 0) {
2778 printk(KERN_ERR "%s: No MII Phy found.\n",
2779 dev->name);
2780 lp->phy_addr = pid = 0;
2781 }
2782
2783 lp->mii_id[0] = tc_mdio_read(dev, pid, MII_PHYSID1);
2784 lp->mii_id[1] = tc_mdio_read(dev, pid, MII_PHYSID2);
2785 if (netif_msg_hw(lp))
2786 printk(KERN_INFO "%s: PHY(%02x) ID %04x %04x\n", dev->name,
2787 pid, lp->mii_id[0], lp->mii_id[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
2790static void tc35815_phy_chip_init(struct net_device *dev)
2791{
2792 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002793 int pid = lp->phy_addr;
2794 unsigned short bmcr;
2795 struct ethtool_cmd ecmd, *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002797 /* dis-isolate if needed. */
2798 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2799 if (bmcr & BMCR_ISOLATE) {
2800 int count = 32;
2801 printk(KERN_DEBUG "%s: unisolating...", dev->name);
2802 tc_mdio_write(dev, pid, MII_BMCR, bmcr & ~BMCR_ISOLATE);
2803 while (--count) {
2804 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_ISOLATE))
2805 break;
2806 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002808 printk(" %s.\n", count ? "done" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
2810
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002811 if (options.speed && options.duplex) {
2812 ecmd.autoneg = AUTONEG_DISABLE;
2813 ecmd.speed = options.speed == 10 ? SPEED_10 : SPEED_100;
2814 ecmd.duplex = options.duplex == 1 ? DUPLEX_HALF : DUPLEX_FULL;
2815 ep = &ecmd;
2816 } else {
2817 ep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002819 tc35815_start_auto_negotiation(dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
2822static void tc35815_chip_reset(struct net_device *dev)
2823{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002824 struct tc35815_regs __iomem *tr =
2825 (struct tc35815_regs __iomem *)dev->base_addr;
2826 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /* reset the controller */
2828 tc_writel(MAC_Reset, &tr->MAC_Ctl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002829 udelay(4); /* 3200ns */
2830 i = 0;
2831 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2832 if (i++ > 100) {
2833 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2834 break;
2835 }
2836 mdelay(1);
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 tc_writel(0, &tr->MAC_Ctl);
2839
2840 /* initialize registers to default value */
2841 tc_writel(0, &tr->DMA_Ctl);
2842 tc_writel(0, &tr->TxThrsh);
2843 tc_writel(0, &tr->TxPollCtr);
2844 tc_writel(0, &tr->RxFragSize);
2845 tc_writel(0, &tr->Int_En);
2846 tc_writel(0, &tr->FDA_Bas);
2847 tc_writel(0, &tr->FDA_Lim);
2848 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2849 tc_writel(0, &tr->CAM_Ctl);
2850 tc_writel(0, &tr->Tx_Ctl);
2851 tc_writel(0, &tr->Rx_Ctl);
2852 tc_writel(0, &tr->CAM_Ena);
2853 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2854
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002855 /* initialize internal SRAM */
2856 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2857 for (i = 0; i < 0x1000; i += 4) {
2858 tc_writel(i, &tr->CAM_Adr);
2859 tc_writel(0, &tr->CAM_Data);
2860 }
2861 tc_writel(0, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
2863
2864static void tc35815_chip_init(struct net_device *dev)
2865{
2866 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002867 struct tc35815_regs __iomem *tr =
2868 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 unsigned long txctl = TX_CTL_CMD;
2870
2871 tc35815_phy_chip_init(dev);
2872
2873 /* load station address to CAM */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002874 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 /* Enable CAM (broadcast and unicast) */
2877 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2878 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2879
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002880 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2881 if (HAVE_DMA_RXALIGN(lp))
2882 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2883 else
2884 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2885#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002887#else
2888 tc_writel(ETH_ZLEN, &tr->RxFragSize);
2889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2891 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2892 tc_writel(INT_EN_CMD, &tr->Int_En);
2893
2894 /* set queues */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002895 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2897 &tr->FDA_Lim);
2898 /*
2899 * Activation method:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002900 * First, enable the MAC Transmitter and the DMA Receive circuits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 * Then enable the DMA Transmitter and the MAC Receive circuits.
2902 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002903 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 /* start MAC transmitter */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002907#ifndef NO_CHECK_CARRIER
2908 /* TX4939 does not have EnLCarr */
2909 if (lp->boardtype == TC35815_TX4939)
2910 txctl &= ~Tx_EnLCarr;
2911#ifdef WORKAROUND_LOSTCAR
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002913 if ((lp->timer_state != asleep && lp->timer_state != lcheck) ||
2914 lp->fullduplex)
2915 txctl &= ~Tx_EnLCarr;
2916#endif
2917#endif /* !NO_CHECK_CARRIER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918#ifdef GATHER_TXINT
2919 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
2920#endif
2921 tc_writel(txctl, &tr->Tx_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002924#ifdef CONFIG_PM
2925static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2926{
2927 struct net_device *dev = pci_get_drvdata(pdev);
2928 struct tc35815_local *lp = dev->priv;
2929 unsigned long flags;
2930
2931 pci_save_state(pdev);
2932 if (!netif_running(dev))
2933 return 0;
2934 netif_device_detach(dev);
2935 spin_lock_irqsave(&lp->lock, flags);
2936 del_timer(&lp->timer); /* Kill if running */
2937 tc35815_chip_reset(dev);
2938 spin_unlock_irqrestore(&lp->lock, flags);
2939 pci_set_power_state(pdev, PCI_D3hot);
2940 return 0;
2941}
2942
2943static int tc35815_resume(struct pci_dev *pdev)
2944{
2945 struct net_device *dev = pci_get_drvdata(pdev);
2946 struct tc35815_local *lp = dev->priv;
2947 unsigned long flags;
2948
2949 pci_restore_state(pdev);
2950 if (!netif_running(dev))
2951 return 0;
2952 pci_set_power_state(pdev, PCI_D0);
2953 spin_lock_irqsave(&lp->lock, flags);
2954 tc35815_restart(dev);
2955 spin_unlock_irqrestore(&lp->lock, flags);
2956 netif_device_attach(dev);
2957 return 0;
2958}
2959#endif /* CONFIG_PM */
2960
2961static struct pci_driver tc35815_pci_driver = {
2962 .name = MODNAME,
2963 .id_table = tc35815_pci_tbl,
2964 .probe = tc35815_init_one,
2965 .remove = __devexit_p(tc35815_remove_one),
2966#ifdef CONFIG_PM
2967 .suspend = tc35815_suspend,
2968 .resume = tc35815_resume,
2969#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970};
2971
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002972module_param_named(speed, options.speed, int, 0);
2973MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2974module_param_named(duplex, options.duplex, int, 0);
2975MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
2976module_param_named(doforce, options.doforce, int, 0);
2977MODULE_PARM_DESC(doforce, "try force link mode if auto-negotiation failed");
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979static int __init tc35815_init_module(void)
2980{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002981 return pci_register_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984static void __exit tc35815_cleanup_module(void)
2985{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002986 pci_unregister_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
Jeff Garzik420e8522007-02-24 17:02:16 -05002988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989module_init(tc35815_init_module);
2990module_exit(tc35815_cleanup_module);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002991
2992MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2993MODULE_LICENSE("GPL");