Commit | Line | Data |
---|---|---|
eea221ce AN |
1 | /* |
2 | * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux. | |
1da177e4 LT |
3 | * |
4 | * Based on skelton.c by Donald Becker. | |
1da177e4 | 5 | * |
eea221ce AN |
6 | * 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>----- | |
1da177e4 | 16 | * |
eea221ce AN |
17 | * This file is subject to the terms and conditions of the GNU General Public |
18 | * License. See the file "COPYING" in the main directory of this archive | |
19 | * for more details. | |
1da177e4 | 20 | * |
eea221ce AN |
21 | * (C) Copyright TOSHIBA CORPORATION 2004-2005 |
22 | * All Rights Reserved. | |
1da177e4 LT |
23 | */ |
24 | ||
eea221ce | 25 | #ifdef TC35815_NAPI |
c6686fe3 | 26 | #define DRV_VERSION "1.37-NAPI" |
eea221ce | 27 | #else |
c6686fe3 | 28 | #define DRV_VERSION "1.37" |
eea221ce AN |
29 | #endif |
30 | static const char *version = "tc35815.c:v" DRV_VERSION "\n"; | |
31 | #define MODNAME "tc35815" | |
1da177e4 LT |
32 | |
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> | |
eea221ce | 42 | #include <linux/spinlock.h> |
1da177e4 LT |
43 | #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> | |
c6686fe3 AN |
50 | #include <linux/phy.h> |
51 | #include <linux/workqueue.h> | |
bd43da8f | 52 | #include <linux/platform_device.h> |
1da177e4 | 53 | #include <asm/io.h> |
1da177e4 LT |
54 | #include <asm/byteorder.h> |
55 | ||
1da177e4 LT |
56 | /* First, a few definitions that the brave might change. */ |
57 | ||
1da177e4 | 58 | #define GATHER_TXINT /* On-Demand Tx Interrupt */ |
eea221ce AN |
59 | #define WORKAROUND_LOSTCAR |
60 | #define WORKAROUND_100HALF_PROMISC | |
61 | /* #define TC35815_USE_PACKEDBUFFER */ | |
62 | ||
c6686fe3 | 63 | enum tc35815_chiptype { |
eea221ce AN |
64 | TC35815CF = 0, |
65 | TC35815_NWU, | |
66 | TC35815_TX4939, | |
c6686fe3 | 67 | }; |
eea221ce | 68 | |
c6686fe3 | 69 | /* indexed by tc35815_chiptype, above */ |
eea221ce AN |
70 | static const struct { |
71 | const char *name; | |
c6686fe3 | 72 | } chip_info[] __devinitdata = { |
eea221ce AN |
73 | { "TOSHIBA TC35815CF 10/100BaseTX" }, |
74 | { "TOSHIBA TC35815 with Wake on LAN" }, | |
75 | { "TOSHIBA TC35815/TX4939" }, | |
76 | }; | |
77 | ||
78 | static 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 | }; | |
7f225b42 | 84 | MODULE_DEVICE_TABLE(pci, tc35815_pci_tbl); |
1da177e4 | 85 | |
eea221ce AN |
86 | /* see MODULE_PARM_DESC */ |
87 | static struct tc35815_options { | |
88 | int speed; | |
89 | int duplex; | |
eea221ce | 90 | } options; |
1da177e4 LT |
91 | |
92 | /* | |
93 | * Registers | |
94 | */ | |
95 | struct tc35815_regs { | |
22adf7e5 AN |
96 | __u32 DMA_Ctl; /* 0x00 */ |
97 | __u32 TxFrmPtr; | |
98 | __u32 TxThrsh; | |
99 | __u32 TxPollCtr; | |
100 | __u32 BLFrmPtr; | |
101 | __u32 RxFragSize; | |
102 | __u32 Int_En; | |
103 | __u32 FDA_Bas; | |
104 | __u32 FDA_Lim; /* 0x20 */ | |
105 | __u32 Int_Src; | |
106 | __u32 unused0[2]; | |
107 | __u32 PauseCnt; | |
108 | __u32 RemPauCnt; | |
109 | __u32 TxCtlFrmStat; | |
110 | __u32 unused1; | |
111 | __u32 MAC_Ctl; /* 0x40 */ | |
112 | __u32 CAM_Ctl; | |
113 | __u32 Tx_Ctl; | |
114 | __u32 Tx_Stat; | |
115 | __u32 Rx_Ctl; | |
116 | __u32 Rx_Stat; | |
117 | __u32 MD_Data; | |
118 | __u32 MD_CA; | |
119 | __u32 CAM_Adr; /* 0x60 */ | |
120 | __u32 CAM_Data; | |
121 | __u32 CAM_Ena; | |
122 | __u32 PROM_Ctl; | |
123 | __u32 PROM_Data; | |
124 | __u32 Algn_Cnt; | |
125 | __u32 CRC_Cnt; | |
126 | __u32 Miss_Cnt; | |
1da177e4 LT |
127 | }; |
128 | ||
129 | /* | |
130 | * Bit assignments | |
131 | */ | |
132 | /* DMA_Ctl bit asign ------------------------------------------------------- */ | |
7f225b42 AN |
133 | #define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */ |
134 | #define DMA_RxAlign_1 0x00400000 | |
135 | #define DMA_RxAlign_2 0x00800000 | |
136 | #define DMA_RxAlign_3 0x00c00000 | |
137 | #define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */ | |
138 | #define DMA_IntMask 0x00040000 /* 1:Interupt mask */ | |
139 | #define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */ | |
140 | #define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */ | |
141 | #define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */ | |
142 | #define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */ | |
143 | #define DMA_TestMode 0x00002000 /* 1:Test Mode */ | |
144 | #define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */ | |
145 | #define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */ | |
1da177e4 LT |
146 | |
147 | /* RxFragSize bit asign ---------------------------------------------------- */ | |
7f225b42 AN |
148 | #define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */ |
149 | #define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */ | |
1da177e4 LT |
150 | |
151 | /* MAC_Ctl bit asign ------------------------------------------------------- */ | |
7f225b42 AN |
152 | #define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */ |
153 | #define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */ | |
154 | #define MAC_MissRoll 0x00000400 /* 1:Missed Roll */ | |
155 | #define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */ | |
156 | #define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */ | |
157 | #define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/ | |
158 | #define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */ | |
159 | #define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */ | |
160 | #define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */ | |
161 | #define MAC_Reset 0x00000004 /* 1:Software Reset */ | |
162 | #define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */ | |
163 | #define MAC_HaltReq 0x00000001 /* 1:Halt request */ | |
1da177e4 LT |
164 | |
165 | /* PROM_Ctl bit asign ------------------------------------------------------ */ | |
7f225b42 AN |
166 | #define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */ |
167 | #define PROM_Read 0x00004000 /*10:Read operation */ | |
168 | #define PROM_Write 0x00002000 /*01:Write operation */ | |
169 | #define PROM_Erase 0x00006000 /*11:Erase operation */ | |
170 | /*00:Enable or Disable Writting, */ | |
171 | /* as specified in PROM_Addr. */ | |
172 | #define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */ | |
173 | /*00xxxx: disable */ | |
1da177e4 LT |
174 | |
175 | /* CAM_Ctl bit asign ------------------------------------------------------- */ | |
7f225b42 AN |
176 | #define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */ |
177 | #define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/ | |
178 | /* accept other */ | |
179 | #define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */ | |
180 | #define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */ | |
181 | #define CAM_StationAcc 0x00000001 /* 1:unicast accept */ | |
1da177e4 LT |
182 | |
183 | /* CAM_Ena bit asign ------------------------------------------------------- */ | |
7f225b42 | 184 | #define CAM_ENTRY_MAX 21 /* CAM Data entry max count */ |
1da177e4 | 185 | #define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */ |
7f225b42 | 186 | #define CAM_Ena_Bit(index) (1 << (index)) |
1da177e4 LT |
187 | #define CAM_ENTRY_DESTINATION 0 |
188 | #define CAM_ENTRY_SOURCE 1 | |
189 | #define CAM_ENTRY_MACCTL 20 | |
190 | ||
191 | /* Tx_Ctl bit asign -------------------------------------------------------- */ | |
7f225b42 AN |
192 | #define Tx_En 0x00000001 /* 1:Transmit enable */ |
193 | #define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */ | |
194 | #define Tx_NoPad 0x00000004 /* 1:Suppress Padding */ | |
195 | #define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */ | |
196 | #define Tx_FBack 0x00000010 /* 1:Fast Back-off */ | |
197 | #define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */ | |
198 | #define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */ | |
199 | #define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */ | |
200 | #define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */ | |
201 | #define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */ | |
202 | #define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */ | |
203 | #define Tx_EnComp 0x00004000 /* 1:Enable Completion */ | |
1da177e4 LT |
204 | |
205 | /* Tx_Stat bit asign ------------------------------------------------------- */ | |
7f225b42 AN |
206 | #define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */ |
207 | #define Tx_ExColl 0x00000010 /* Excessive Collision */ | |
208 | #define Tx_TXDefer 0x00000020 /* Transmit Defered */ | |
209 | #define Tx_Paused 0x00000040 /* Transmit Paused */ | |
210 | #define Tx_IntTx 0x00000080 /* Interrupt on Tx */ | |
211 | #define Tx_Under 0x00000100 /* Underrun */ | |
212 | #define Tx_Defer 0x00000200 /* Deferral */ | |
213 | #define Tx_NCarr 0x00000400 /* No Carrier */ | |
214 | #define Tx_10Stat 0x00000800 /* 10Mbps Status */ | |
215 | #define Tx_LateColl 0x00001000 /* Late Collision */ | |
216 | #define Tx_TxPar 0x00002000 /* Tx Parity Error */ | |
217 | #define Tx_Comp 0x00004000 /* Completion */ | |
218 | #define Tx_Halted 0x00008000 /* Tx Halted */ | |
219 | #define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */ | |
1da177e4 LT |
220 | |
221 | /* Rx_Ctl bit asign -------------------------------------------------------- */ | |
7f225b42 AN |
222 | #define Rx_EnGood 0x00004000 /* 1:Enable Good */ |
223 | #define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */ | |
224 | #define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */ | |
225 | #define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */ | |
226 | #define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */ | |
227 | #define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */ | |
228 | #define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */ | |
229 | #define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */ | |
230 | #define Rx_ShortEn 0x00000008 /* 1:Short Enable */ | |
231 | #define Rx_LongEn 0x00000004 /* 1:Long Enable */ | |
232 | #define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */ | |
233 | #define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */ | |
1da177e4 LT |
234 | |
235 | /* Rx_Stat bit asign ------------------------------------------------------- */ | |
7f225b42 AN |
236 | #define Rx_Halted 0x00008000 /* Rx Halted */ |
237 | #define Rx_Good 0x00004000 /* Rx Good */ | |
238 | #define Rx_RxPar 0x00002000 /* Rx Parity Error */ | |
239 | /* 0x00001000 not use */ | |
240 | #define Rx_LongErr 0x00000800 /* Rx Long Error */ | |
241 | #define Rx_Over 0x00000400 /* Rx Overflow */ | |
242 | #define Rx_CRCErr 0x00000200 /* Rx CRC Error */ | |
243 | #define Rx_Align 0x00000100 /* Rx Alignment Error */ | |
244 | #define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */ | |
245 | #define Rx_IntRx 0x00000040 /* Rx Interrupt */ | |
246 | #define Rx_CtlRecd 0x00000020 /* Rx Control Receive */ | |
247 | ||
248 | #define Rx_Stat_Mask 0x0000EFC0 /* Rx All Status Mask */ | |
1da177e4 LT |
249 | |
250 | /* Int_En bit asign -------------------------------------------------------- */ | |
7f225b42 AN |
251 | #define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */ |
252 | #define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Ctl Complete Enable */ | |
253 | #define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */ | |
254 | #define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */ | |
255 | #define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */ | |
256 | #define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */ | |
257 | #define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */ | |
258 | #define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */ | |
259 | #define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */ | |
260 | #define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */ | |
261 | #define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */ | |
262 | #define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */ | |
263 | /* Exhausted Enable */ | |
1da177e4 LT |
264 | |
265 | /* Int_Src bit asign ------------------------------------------------------- */ | |
7f225b42 AN |
266 | #define Int_NRabt 0x00004000 /* 1:Non Recoverable error */ |
267 | #define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */ | |
268 | #define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */ | |
269 | #define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */ | |
270 | #define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */ | |
271 | #define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */ | |
272 | #define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */ | |
273 | #define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */ | |
274 | #define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */ | |
275 | #define Int_SWInt 0x00000020 /* 1:Software request & Clear */ | |
276 | #define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */ | |
277 | #define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */ | |
278 | #define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */ | |
279 | #define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */ | |
280 | #define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */ | |
1da177e4 LT |
281 | |
282 | /* MD_CA bit asign --------------------------------------------------------- */ | |
7f225b42 AN |
283 | #define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */ |
284 | #define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */ | |
285 | #define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */ | |
1da177e4 LT |
286 | |
287 | ||
1da177e4 LT |
288 | /* |
289 | * Descriptors | |
290 | */ | |
291 | ||
292 | /* Frame descripter */ | |
293 | struct FDesc { | |
294 | volatile __u32 FDNext; | |
295 | volatile __u32 FDSystem; | |
296 | volatile __u32 FDStat; | |
297 | volatile __u32 FDCtl; | |
298 | }; | |
299 | ||
300 | /* Buffer descripter */ | |
301 | struct BDesc { | |
302 | volatile __u32 BuffData; | |
303 | volatile __u32 BDCtl; | |
304 | }; | |
305 | ||
306 | #define FD_ALIGN 16 | |
307 | ||
308 | /* Frame Descripter bit asign ---------------------------------------------- */ | |
7f225b42 AN |
309 | #define FD_FDLength_MASK 0x0000FFFF /* Length MASK */ |
310 | #define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */ | |
311 | #define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */ | |
1da177e4 | 312 | #define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */ |
7f225b42 AN |
313 | #define FD_FrmOpt_IntTx 0x20000000 /* Tx only */ |
314 | #define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */ | |
1da177e4 LT |
315 | #define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */ |
316 | #define FD_FrmOpt_Packing 0x04000000 /* Rx only */ | |
7f225b42 AN |
317 | #define FD_CownsFD 0x80000000 /* FD Controller owner bit */ |
318 | #define FD_Next_EOL 0x00000001 /* FD EOL indicator */ | |
319 | #define FD_BDCnt_SHIFT 16 | |
1da177e4 LT |
320 | |
321 | /* Buffer Descripter bit asign --------------------------------------------- */ | |
7f225b42 AN |
322 | #define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */ |
323 | #define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */ | |
324 | #define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */ | |
325 | #define BD_CownsBD 0x80000000 /* BD Controller owner bit */ | |
326 | #define BD_RxBDID_SHIFT 16 | |
1da177e4 LT |
327 | #define BD_RxBDSeqN_SHIFT 24 |
328 | ||
329 | ||
330 | /* Some useful constants. */ | |
331 | #undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */ | |
332 | ||
333 | #ifdef NO_CHECK_CARRIER | |
334 | #define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \ | |
eea221ce AN |
335 | Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \ |
336 | Tx_En) /* maybe 0x7b01 */ | |
1da177e4 LT |
337 | #else |
338 | #define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \ | |
eea221ce AN |
339 | Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \ |
340 | Tx_En) /* maybe 0x7b01 */ | |
1da177e4 LT |
341 | #endif |
342 | #define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \ | |
343 | | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */ | |
1da177e4 | 344 | #define INT_EN_CMD (Int_NRAbtEn | \ |
eea221ce | 345 | Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \ |
1da177e4 LT |
346 | Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \ |
347 | Int_STargAbtEn | \ | |
348 | Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/ | |
eea221ce | 349 | #define DMA_CTL_CMD DMA_BURST_SIZE |
c6686fe3 | 350 | #define HAVE_DMA_RXALIGN(lp) likely((lp)->chiptype != TC35815CF) |
1da177e4 LT |
351 | |
352 | /* Tuning parameters */ | |
353 | #define DMA_BURST_SIZE 32 | |
354 | #define TX_THRESHOLD 1024 | |
7f225b42 AN |
355 | /* used threshold with packet max byte for low pci transfer ability.*/ |
356 | #define TX_THRESHOLD_MAX 1536 | |
357 | /* setting threshold max value when overrun error occured this count. */ | |
358 | #define TX_THRESHOLD_KEEP_LIMIT 10 | |
1da177e4 | 359 | |
eea221ce AN |
360 | /* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */ |
361 | #ifdef TC35815_USE_PACKEDBUFFER | |
1da177e4 | 362 | #define FD_PAGE_NUM 2 |
eea221ce | 363 | #define RX_BUF_NUM 8 /* >= 2 */ |
1da177e4 LT |
364 | #define RX_FD_NUM 250 /* >= 32 */ |
365 | #define TX_FD_NUM 128 | |
eea221ce AN |
366 | #define RX_BUF_SIZE PAGE_SIZE |
367 | #else /* TC35815_USE_PACKEDBUFFER */ | |
368 | #define FD_PAGE_NUM 4 | |
369 | #define RX_BUF_NUM 128 /* < 256 */ | |
370 | #define RX_FD_NUM 256 /* >= 32 */ | |
371 | #define TX_FD_NUM 128 | |
372 | #if RX_CTL_CMD & Rx_LongEn | |
373 | #define RX_BUF_SIZE PAGE_SIZE | |
374 | #elif RX_CTL_CMD & Rx_StripCRC | |
375 | #define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */ | |
376 | #else | |
377 | #define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */ | |
378 | #endif | |
379 | #endif /* TC35815_USE_PACKEDBUFFER */ | |
380 | #define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */ | |
381 | #define NAPI_WEIGHT 16 | |
1da177e4 LT |
382 | |
383 | struct TxFD { | |
384 | struct FDesc fd; | |
385 | struct BDesc bd; | |
386 | struct BDesc unused; | |
387 | }; | |
388 | ||
389 | struct RxFD { | |
390 | struct FDesc fd; | |
391 | struct BDesc bd[0]; /* variable length */ | |
392 | }; | |
393 | ||
394 | struct FrFD { | |
395 | struct FDesc fd; | |
eea221ce | 396 | struct BDesc bd[RX_BUF_NUM]; |
1da177e4 LT |
397 | }; |
398 | ||
399 | ||
22adf7e5 AN |
400 | #define tc_readl(addr) ioread32(addr) |
401 | #define tc_writel(d, addr) iowrite32(d, addr) | |
1da177e4 | 402 | |
eea221ce AN |
403 | #define TC35815_TX_TIMEOUT msecs_to_jiffies(400) |
404 | ||
c6686fe3 | 405 | /* Information that need to be kept for each controller. */ |
1da177e4 | 406 | struct tc35815_local { |
eea221ce | 407 | struct pci_dev *pci_dev; |
1da177e4 | 408 | |
bea3348e SH |
409 | struct net_device *dev; |
410 | struct napi_struct napi; | |
411 | ||
1da177e4 | 412 | /* statistics */ |
1da177e4 LT |
413 | struct { |
414 | int max_tx_qlen; | |
415 | int tx_ints; | |
416 | int rx_ints; | |
7f225b42 | 417 | int tx_underrun; |
1da177e4 LT |
418 | } lstats; |
419 | ||
eea221ce AN |
420 | /* Tx control lock. This protects the transmit buffer ring |
421 | * state along with the "tx full" state of the driver. This | |
422 | * means all netif_queue flow control actions are protected | |
423 | * by this lock as well. | |
424 | */ | |
425 | spinlock_t lock; | |
426 | ||
c6686fe3 AN |
427 | struct mii_bus mii_bus; |
428 | struct phy_device *phy_dev; | |
429 | int duplex; | |
430 | int speed; | |
431 | int link; | |
432 | struct work_struct restart_work; | |
1da177e4 LT |
433 | |
434 | /* | |
435 | * Transmitting: Batch Mode. | |
436 | * 1 BD in 1 TxFD. | |
eea221ce | 437 | * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER) |
1da177e4 | 438 | * 1 circular FD for Free Buffer List. |
eea221ce | 439 | * RX_BUF_NUM BD in Free Buffer FD. |
1da177e4 | 440 | * One Free Buffer BD has PAGE_SIZE data buffer. |
eea221ce AN |
441 | * Or Non-Packing Mode. |
442 | * 1 circular FD for Free Buffer List. | |
443 | * RX_BUF_NUM BD in Free Buffer FD. | |
444 | * One Free Buffer BD has ETH_FRAME_LEN data buffer. | |
1da177e4 | 445 | */ |
7f225b42 | 446 | void *fd_buf; /* for TxFD, RxFD, FrFD */ |
eea221ce | 447 | dma_addr_t fd_buf_dma; |
1da177e4 | 448 | struct TxFD *tfd_base; |
eea221ce AN |
449 | unsigned int tfd_start; |
450 | unsigned int tfd_end; | |
1da177e4 LT |
451 | struct RxFD *rfd_base; |
452 | struct RxFD *rfd_limit; | |
453 | struct RxFD *rfd_cur; | |
454 | struct FrFD *fbl_ptr; | |
eea221ce | 455 | #ifdef TC35815_USE_PACKEDBUFFER |
1da177e4 | 456 | unsigned char fbl_curid; |
7f225b42 | 457 | void *data_buf[RX_BUF_NUM]; /* packing */ |
eea221ce AN |
458 | dma_addr_t data_buf_dma[RX_BUF_NUM]; |
459 | struct { | |
460 | struct sk_buff *skb; | |
461 | dma_addr_t skb_dma; | |
462 | } tx_skbs[TX_FD_NUM]; | |
463 | #else | |
464 | unsigned int fbl_count; | |
465 | struct { | |
466 | struct sk_buff *skb; | |
467 | dma_addr_t skb_dma; | |
468 | } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM]; | |
469 | #endif | |
eea221ce | 470 | u32 msg_enable; |
c6686fe3 | 471 | enum tc35815_chiptype chiptype; |
1da177e4 LT |
472 | }; |
473 | ||
eea221ce AN |
474 | static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt) |
475 | { | |
476 | return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf); | |
477 | } | |
478 | #ifdef DEBUG | |
479 | static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus) | |
480 | { | |
481 | return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma)); | |
482 | } | |
483 | #endif | |
484 | #ifdef TC35815_USE_PACKEDBUFFER | |
485 | static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus) | |
486 | { | |
487 | int i; | |
488 | for (i = 0; i < RX_BUF_NUM; i++) { | |
489 | if (bus >= lp->data_buf_dma[i] && | |
490 | bus < lp->data_buf_dma[i] + PAGE_SIZE) | |
491 | return (void *)((u8 *)lp->data_buf[i] + | |
492 | (bus - lp->data_buf_dma[i])); | |
493 | } | |
494 | return NULL; | |
495 | } | |
496 | ||
497 | #define TC35815_DMA_SYNC_ONDEMAND | |
7f225b42 | 498 | static void *alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle) |
eea221ce AN |
499 | { |
500 | #ifdef TC35815_DMA_SYNC_ONDEMAND | |
501 | void *buf; | |
502 | /* pci_map + pci_dma_sync will be more effective than | |
503 | * pci_alloc_consistent on some archs. */ | |
7f225b42 AN |
504 | buf = (void *)__get_free_page(GFP_ATOMIC); |
505 | if (!buf) | |
eea221ce AN |
506 | return NULL; |
507 | *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE, | |
508 | PCI_DMA_FROMDEVICE); | |
509 | if (pci_dma_mapping_error(*dma_handle)) { | |
510 | free_page((unsigned long)buf); | |
511 | return NULL; | |
512 | } | |
513 | return buf; | |
514 | #else | |
515 | return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle); | |
516 | #endif | |
517 | } | |
518 | ||
519 | static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle) | |
520 | { | |
521 | #ifdef TC35815_DMA_SYNC_ONDEMAND | |
522 | pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE); | |
523 | free_page((unsigned long)buf); | |
524 | #else | |
525 | pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle); | |
526 | #endif | |
527 | } | |
528 | #else /* TC35815_USE_PACKEDBUFFER */ | |
529 | static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev, | |
530 | struct pci_dev *hwdev, | |
531 | dma_addr_t *dma_handle) | |
532 | { | |
533 | struct sk_buff *skb; | |
534 | skb = dev_alloc_skb(RX_BUF_SIZE); | |
535 | if (!skb) | |
536 | return NULL; | |
eea221ce AN |
537 | *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE, |
538 | PCI_DMA_FROMDEVICE); | |
539 | if (pci_dma_mapping_error(*dma_handle)) { | |
540 | dev_kfree_skb_any(skb); | |
541 | return NULL; | |
542 | } | |
543 | skb_reserve(skb, 2); /* make IP header 4byte aligned */ | |
544 | return skb; | |
545 | } | |
546 | ||
547 | static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle) | |
548 | { | |
549 | pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE, | |
550 | PCI_DMA_FROMDEVICE); | |
551 | dev_kfree_skb_any(skb); | |
552 | } | |
553 | #endif /* TC35815_USE_PACKEDBUFFER */ | |
1da177e4 | 554 | |
eea221ce | 555 | /* Index to functions, as function prototypes. */ |
1da177e4 LT |
556 | |
557 | static int tc35815_open(struct net_device *dev); | |
558 | static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev); | |
eea221ce AN |
559 | static irqreturn_t tc35815_interrupt(int irq, void *dev_id); |
560 | #ifdef TC35815_NAPI | |
561 | static int tc35815_rx(struct net_device *dev, int limit); | |
bea3348e | 562 | static int tc35815_poll(struct napi_struct *napi, int budget); |
eea221ce | 563 | #else |
1da177e4 | 564 | static void tc35815_rx(struct net_device *dev); |
eea221ce | 565 | #endif |
1da177e4 LT |
566 | static void tc35815_txdone(struct net_device *dev); |
567 | static int tc35815_close(struct net_device *dev); | |
568 | static struct net_device_stats *tc35815_get_stats(struct net_device *dev); | |
569 | static void tc35815_set_multicast_list(struct net_device *dev); | |
7f225b42 | 570 | static void tc35815_tx_timeout(struct net_device *dev); |
eea221ce AN |
571 | static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); |
572 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
573 | static void tc35815_poll_controller(struct net_device *dev); | |
574 | #endif | |
575 | static const struct ethtool_ops tc35815_ethtool_ops; | |
1da177e4 | 576 | |
eea221ce | 577 | /* Example routines you must write ;->. */ |
7f225b42 AN |
578 | static void tc35815_chip_reset(struct net_device *dev); |
579 | static void tc35815_chip_init(struct net_device *dev); | |
1da177e4 | 580 | |
eea221ce AN |
581 | #ifdef DEBUG |
582 | static void panic_queues(struct net_device *dev); | |
583 | #endif | |
1da177e4 | 584 | |
c6686fe3 AN |
585 | static void tc35815_restart_work(struct work_struct *work); |
586 | ||
587 | static int tc_mdio_read(struct mii_bus *bus, int mii_id, int regnum) | |
588 | { | |
589 | struct net_device *dev = bus->priv; | |
590 | struct tc35815_regs __iomem *tr = | |
591 | (struct tc35815_regs __iomem *)dev->base_addr; | |
592 | unsigned long timeout = jiffies + 10; | |
593 | ||
594 | tc_writel(MD_CA_Busy | (mii_id << 5) | (regnum & 0x1f), &tr->MD_CA); | |
595 | while (tc_readl(&tr->MD_CA) & MD_CA_Busy) { | |
596 | if (time_after(jiffies, timeout)) | |
597 | return -EIO; | |
598 | cpu_relax(); | |
599 | } | |
600 | return tc_readl(&tr->MD_Data) & 0xffff; | |
601 | } | |
602 | ||
603 | static int tc_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 val) | |
604 | { | |
605 | struct net_device *dev = bus->priv; | |
606 | struct tc35815_regs __iomem *tr = | |
607 | (struct tc35815_regs __iomem *)dev->base_addr; | |
608 | unsigned long timeout = jiffies + 10; | |
609 | ||
610 | tc_writel(val, &tr->MD_Data); | |
611 | tc_writel(MD_CA_Busy | MD_CA_Wr | (mii_id << 5) | (regnum & 0x1f), | |
612 | &tr->MD_CA); | |
613 | while (tc_readl(&tr->MD_CA) & MD_CA_Busy) { | |
614 | if (time_after(jiffies, timeout)) | |
615 | return -EIO; | |
616 | cpu_relax(); | |
617 | } | |
618 | return 0; | |
619 | } | |
620 | ||
621 | static void tc_handle_link_change(struct net_device *dev) | |
622 | { | |
623 | struct tc35815_local *lp = netdev_priv(dev); | |
624 | struct phy_device *phydev = lp->phy_dev; | |
625 | unsigned long flags; | |
626 | int status_change = 0; | |
627 | ||
628 | spin_lock_irqsave(&lp->lock, flags); | |
629 | if (phydev->link && | |
630 | (lp->speed != phydev->speed || lp->duplex != phydev->duplex)) { | |
631 | struct tc35815_regs __iomem *tr = | |
632 | (struct tc35815_regs __iomem *)dev->base_addr; | |
633 | u32 reg; | |
634 | ||
635 | reg = tc_readl(&tr->MAC_Ctl); | |
636 | reg |= MAC_HaltReq; | |
637 | tc_writel(reg, &tr->MAC_Ctl); | |
638 | if (phydev->duplex == DUPLEX_FULL) | |
639 | reg |= MAC_FullDup; | |
640 | else | |
641 | reg &= ~MAC_FullDup; | |
642 | tc_writel(reg, &tr->MAC_Ctl); | |
643 | reg &= ~MAC_HaltReq; | |
644 | tc_writel(reg, &tr->MAC_Ctl); | |
645 | ||
646 | /* | |
647 | * TX4939 PCFG.SPEEDn bit will be changed on | |
648 | * NETDEV_CHANGE event. | |
649 | */ | |
650 | ||
651 | #if !defined(NO_CHECK_CARRIER) && defined(WORKAROUND_LOSTCAR) | |
652 | /* | |
653 | * WORKAROUND: enable LostCrS only if half duplex | |
654 | * operation. | |
655 | * (TX4939 does not have EnLCarr) | |
656 | */ | |
657 | if (phydev->duplex == DUPLEX_HALF && | |
658 | lp->chiptype != TC35815_TX4939) | |
659 | tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, | |
660 | &tr->Tx_Ctl); | |
661 | #endif | |
662 | ||
663 | lp->speed = phydev->speed; | |
664 | lp->duplex = phydev->duplex; | |
665 | status_change = 1; | |
666 | } | |
667 | ||
668 | if (phydev->link != lp->link) { | |
669 | if (phydev->link) { | |
670 | #ifdef WORKAROUND_100HALF_PROMISC | |
671 | /* delayed promiscuous enabling */ | |
672 | if (dev->flags & IFF_PROMISC) | |
673 | tc35815_set_multicast_list(dev); | |
674 | #endif | |
675 | netif_schedule(dev); | |
676 | } else { | |
677 | lp->speed = 0; | |
678 | lp->duplex = -1; | |
679 | } | |
680 | lp->link = phydev->link; | |
681 | ||
682 | status_change = 1; | |
683 | } | |
684 | spin_unlock_irqrestore(&lp->lock, flags); | |
685 | ||
686 | if (status_change && netif_msg_link(lp)) { | |
687 | phy_print_status(phydev); | |
688 | #ifdef DEBUG | |
689 | printk(KERN_DEBUG | |
690 | "%s: MII BMCR %04x BMSR %04x LPA %04x\n", | |
691 | dev->name, | |
692 | phy_read(phydev, MII_BMCR), | |
693 | phy_read(phydev, MII_BMSR), | |
694 | phy_read(phydev, MII_LPA)); | |
695 | #endif | |
696 | } | |
697 | } | |
698 | ||
699 | static int tc_mii_probe(struct net_device *dev) | |
700 | { | |
701 | struct tc35815_local *lp = netdev_priv(dev); | |
702 | struct phy_device *phydev = NULL; | |
703 | int phy_addr; | |
704 | u32 dropmask; | |
705 | ||
706 | /* find the first phy */ | |
707 | for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) { | |
708 | if (lp->mii_bus.phy_map[phy_addr]) { | |
709 | if (phydev) { | |
710 | printk(KERN_ERR "%s: multiple PHYs found\n", | |
711 | dev->name); | |
712 | return -EINVAL; | |
713 | } | |
714 | phydev = lp->mii_bus.phy_map[phy_addr]; | |
715 | break; | |
716 | } | |
717 | } | |
718 | ||
719 | if (!phydev) { | |
720 | printk(KERN_ERR "%s: no PHY found\n", dev->name); | |
721 | return -ENODEV; | |
722 | } | |
723 | ||
724 | /* attach the mac to the phy */ | |
725 | phydev = phy_connect(dev, phydev->dev.bus_id, | |
726 | &tc_handle_link_change, 0, | |
727 | lp->chiptype == TC35815_TX4939 ? | |
728 | PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII); | |
729 | if (IS_ERR(phydev)) { | |
730 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); | |
731 | return PTR_ERR(phydev); | |
732 | } | |
733 | printk(KERN_INFO "%s: attached PHY driver [%s] " | |
734 | "(mii_bus:phy_addr=%s, id=%x)\n", | |
735 | dev->name, phydev->drv->name, phydev->dev.bus_id, | |
736 | phydev->phy_id); | |
737 | ||
738 | /* mask with MAC supported features */ | |
739 | phydev->supported &= PHY_BASIC_FEATURES; | |
740 | dropmask = 0; | |
741 | if (options.speed == 10) | |
742 | dropmask |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full; | |
743 | else if (options.speed == 100) | |
744 | dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full; | |
745 | if (options.duplex == 1) | |
746 | dropmask |= SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full; | |
747 | else if (options.duplex == 2) | |
748 | dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_100baseT_Half; | |
749 | phydev->supported &= ~dropmask; | |
750 | phydev->advertising = phydev->supported; | |
751 | ||
752 | lp->link = 0; | |
753 | lp->speed = 0; | |
754 | lp->duplex = -1; | |
755 | lp->phy_dev = phydev; | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | static int tc_mii_init(struct net_device *dev) | |
761 | { | |
762 | struct tc35815_local *lp = netdev_priv(dev); | |
763 | int err; | |
764 | int i; | |
765 | ||
766 | lp->mii_bus.name = "tc35815_mii_bus"; | |
767 | lp->mii_bus.read = tc_mdio_read; | |
768 | lp->mii_bus.write = tc_mdio_write; | |
769 | lp->mii_bus.id = (lp->pci_dev->bus->number << 8) | lp->pci_dev->devfn; | |
770 | lp->mii_bus.priv = dev; | |
771 | lp->mii_bus.dev = &lp->pci_dev->dev; | |
772 | lp->mii_bus.irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); | |
773 | if (!lp->mii_bus.irq) { | |
774 | err = -ENOMEM; | |
775 | goto err_out; | |
776 | } | |
777 | ||
778 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
779 | lp->mii_bus.irq[i] = PHY_POLL; | |
780 | ||
781 | err = mdiobus_register(&lp->mii_bus); | |
782 | if (err) | |
783 | goto err_out_free_mdio_irq; | |
784 | err = tc_mii_probe(dev); | |
785 | if (err) | |
786 | goto err_out_unregister_bus; | |
787 | return 0; | |
788 | ||
789 | err_out_unregister_bus: | |
790 | mdiobus_unregister(&lp->mii_bus); | |
791 | err_out_free_mdio_irq: | |
792 | kfree(lp->mii_bus.irq); | |
793 | err_out: | |
794 | return err; | |
795 | } | |
1da177e4 | 796 | |
bd43da8f AN |
797 | #ifdef CONFIG_CPU_TX49XX |
798 | /* | |
799 | * Find a platform_device providing a MAC address. The platform code | |
800 | * should provide a "tc35815-mac" device with a MAC address in its | |
801 | * platform_data. | |
802 | */ | |
803 | static int __devinit tc35815_mac_match(struct device *dev, void *data) | |
804 | { | |
805 | struct platform_device *plat_dev = to_platform_device(dev); | |
806 | struct pci_dev *pci_dev = data; | |
06675e6f | 807 | unsigned int id = pci_dev->irq; |
bd43da8f AN |
808 | return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id; |
809 | } | |
810 | ||
811 | static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev) | |
812 | { | |
ee79b7fb | 813 | struct tc35815_local *lp = netdev_priv(dev); |
bd43da8f AN |
814 | struct device *pd = bus_find_device(&platform_bus_type, NULL, |
815 | lp->pci_dev, tc35815_mac_match); | |
816 | if (pd) { | |
817 | if (pd->platform_data) | |
818 | memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN); | |
819 | put_device(pd); | |
820 | return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV; | |
821 | } | |
822 | return -ENODEV; | |
823 | } | |
824 | #else | |
308a9068 | 825 | static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev) |
bd43da8f AN |
826 | { |
827 | return -ENODEV; | |
828 | } | |
829 | #endif | |
830 | ||
7f225b42 | 831 | static int __devinit tc35815_init_dev_addr(struct net_device *dev) |
eea221ce AN |
832 | { |
833 | struct tc35815_regs __iomem *tr = | |
834 | (struct tc35815_regs __iomem *)dev->base_addr; | |
835 | int i; | |
836 | ||
eea221ce AN |
837 | while (tc_readl(&tr->PROM_Ctl) & PROM_Busy) |
838 | ; | |
839 | for (i = 0; i < 6; i += 2) { | |
840 | unsigned short data; | |
841 | tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl); | |
842 | while (tc_readl(&tr->PROM_Ctl) & PROM_Busy) | |
843 | ; | |
844 | data = tc_readl(&tr->PROM_Data); | |
845 | dev->dev_addr[i] = data & 0xff; | |
846 | dev->dev_addr[i+1] = data >> 8; | |
847 | } | |
bd43da8f AN |
848 | if (!is_valid_ether_addr(dev->dev_addr)) |
849 | return tc35815_read_plat_dev_addr(dev); | |
850 | return 0; | |
eea221ce | 851 | } |
1da177e4 | 852 | |
7f225b42 AN |
853 | static int __devinit tc35815_init_one(struct pci_dev *pdev, |
854 | const struct pci_device_id *ent) | |
1da177e4 | 855 | { |
eea221ce AN |
856 | void __iomem *ioaddr = NULL; |
857 | struct net_device *dev; | |
858 | struct tc35815_local *lp; | |
859 | int rc; | |
958eb80b | 860 | DECLARE_MAC_BUF(mac); |
eea221ce AN |
861 | |
862 | static int printed_version; | |
863 | if (!printed_version++) { | |
864 | printk(version); | |
865 | dev_printk(KERN_DEBUG, &pdev->dev, | |
c6686fe3 AN |
866 | "speed:%d duplex:%d\n", |
867 | options.speed, options.duplex); | |
eea221ce AN |
868 | } |
869 | ||
870 | if (!pdev->irq) { | |
871 | dev_warn(&pdev->dev, "no IRQ assigned.\n"); | |
872 | return -ENODEV; | |
873 | } | |
1da177e4 | 874 | |
eea221ce | 875 | /* dev zeroed in alloc_etherdev */ |
7f225b42 | 876 | dev = alloc_etherdev(sizeof(*lp)); |
eea221ce AN |
877 | if (dev == NULL) { |
878 | dev_err(&pdev->dev, "unable to alloc new ethernet\n"); | |
879 | return -ENOMEM; | |
880 | } | |
eea221ce | 881 | SET_NETDEV_DEV(dev, &pdev->dev); |
ee79b7fb | 882 | lp = netdev_priv(dev); |
bea3348e | 883 | lp->dev = dev; |
1da177e4 | 884 | |
eea221ce | 885 | /* enable device (incl. PCI PM wakeup), and bus-mastering */ |
22adf7e5 | 886 | rc = pcim_enable_device(pdev); |
eea221ce AN |
887 | if (rc) |
888 | goto err_out; | |
22adf7e5 | 889 | rc = pcim_iomap_regions(pdev, 1 << 1, MODNAME); |
eea221ce | 890 | if (rc) |
1da177e4 | 891 | goto err_out; |
22adf7e5 AN |
892 | pci_set_master(pdev); |
893 | ioaddr = pcim_iomap_table(pdev)[1]; | |
1da177e4 | 894 | |
eea221ce AN |
895 | /* Initialize the device structure. */ |
896 | dev->open = tc35815_open; | |
897 | dev->hard_start_xmit = tc35815_send_packet; | |
898 | dev->stop = tc35815_close; | |
899 | dev->get_stats = tc35815_get_stats; | |
900 | dev->set_multicast_list = tc35815_set_multicast_list; | |
901 | dev->do_ioctl = tc35815_ioctl; | |
902 | dev->ethtool_ops = &tc35815_ethtool_ops; | |
903 | dev->tx_timeout = tc35815_tx_timeout; | |
904 | dev->watchdog_timeo = TC35815_TX_TIMEOUT; | |
905 | #ifdef TC35815_NAPI | |
bea3348e | 906 | netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT); |
eea221ce AN |
907 | #endif |
908 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
909 | dev->poll_controller = tc35815_poll_controller; | |
910 | #endif | |
1da177e4 | 911 | |
eea221ce | 912 | dev->irq = pdev->irq; |
7f225b42 | 913 | dev->base_addr = (unsigned long)ioaddr; |
1da177e4 | 914 | |
c6686fe3 | 915 | INIT_WORK(&lp->restart_work, tc35815_restart_work); |
eea221ce AN |
916 | spin_lock_init(&lp->lock); |
917 | lp->pci_dev = pdev; | |
c6686fe3 | 918 | lp->chiptype = ent->driver_data; |
1da177e4 | 919 | |
eea221ce AN |
920 | lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK; |
921 | pci_set_drvdata(pdev, dev); | |
1da177e4 | 922 | |
eea221ce | 923 | /* Soft reset the chip. */ |
1da177e4 LT |
924 | tc35815_chip_reset(dev); |
925 | ||
eea221ce | 926 | /* Retrieve the ethernet address. */ |
bd43da8f AN |
927 | if (tc35815_init_dev_addr(dev)) { |
928 | dev_warn(&pdev->dev, "not valid ether addr\n"); | |
929 | random_ether_addr(dev->dev_addr); | |
930 | } | |
eea221ce | 931 | |
7f225b42 | 932 | rc = register_netdev(dev); |
eea221ce | 933 | if (rc) |
22adf7e5 | 934 | goto err_out; |
eea221ce AN |
935 | |
936 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
958eb80b | 937 | printk(KERN_INFO "%s: %s at 0x%lx, %s, IRQ %d\n", |
eea221ce | 938 | dev->name, |
c6686fe3 | 939 | chip_info[ent->driver_data].name, |
eea221ce | 940 | dev->base_addr, |
958eb80b | 941 | print_mac(mac, dev->dev_addr), |
eea221ce AN |
942 | dev->irq); |
943 | ||
c6686fe3 AN |
944 | rc = tc_mii_init(dev); |
945 | if (rc) | |
946 | goto err_out_unregister; | |
1da177e4 | 947 | |
eea221ce | 948 | return 0; |
1da177e4 | 949 | |
c6686fe3 AN |
950 | err_out_unregister: |
951 | unregister_netdev(dev); | |
eea221ce | 952 | err_out: |
7f225b42 | 953 | free_netdev(dev); |
eea221ce AN |
954 | return rc; |
955 | } | |
1da177e4 | 956 | |
1da177e4 | 957 | |
7f225b42 | 958 | static void __devexit tc35815_remove_one(struct pci_dev *pdev) |
eea221ce | 959 | { |
7f225b42 | 960 | struct net_device *dev = pci_get_drvdata(pdev); |
c6686fe3 | 961 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 | 962 | |
c6686fe3 AN |
963 | phy_disconnect(lp->phy_dev); |
964 | mdiobus_unregister(&lp->mii_bus); | |
965 | kfree(lp->mii_bus.irq); | |
7f225b42 AN |
966 | unregister_netdev(dev); |
967 | free_netdev(dev); | |
968 | pci_set_drvdata(pdev, NULL); | |
1da177e4 LT |
969 | } |
970 | ||
1da177e4 LT |
971 | static int |
972 | tc35815_init_queues(struct net_device *dev) | |
973 | { | |
ee79b7fb | 974 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
975 | int i; |
976 | unsigned long fd_addr; | |
977 | ||
978 | if (!lp->fd_buf) { | |
eea221ce AN |
979 | BUG_ON(sizeof(struct FDesc) + |
980 | sizeof(struct BDesc) * RX_BUF_NUM + | |
981 | sizeof(struct FDesc) * RX_FD_NUM + | |
982 | sizeof(struct TxFD) * TX_FD_NUM > | |
983 | PAGE_SIZE * FD_PAGE_NUM); | |
1da177e4 | 984 | |
7f225b42 AN |
985 | lp->fd_buf = pci_alloc_consistent(lp->pci_dev, |
986 | PAGE_SIZE * FD_PAGE_NUM, | |
987 | &lp->fd_buf_dma); | |
988 | if (!lp->fd_buf) | |
1da177e4 | 989 | return -ENOMEM; |
eea221ce AN |
990 | for (i = 0; i < RX_BUF_NUM; i++) { |
991 | #ifdef TC35815_USE_PACKEDBUFFER | |
7f225b42 AN |
992 | lp->data_buf[i] = |
993 | alloc_rxbuf_page(lp->pci_dev, | |
994 | &lp->data_buf_dma[i]); | |
995 | if (!lp->data_buf[i]) { | |
1da177e4 | 996 | while (--i >= 0) { |
eea221ce AN |
997 | free_rxbuf_page(lp->pci_dev, |
998 | lp->data_buf[i], | |
999 | lp->data_buf_dma[i]); | |
1000 | lp->data_buf[i] = NULL; | |
1da177e4 | 1001 | } |
eea221ce AN |
1002 | pci_free_consistent(lp->pci_dev, |
1003 | PAGE_SIZE * FD_PAGE_NUM, | |
1004 | lp->fd_buf, | |
1005 | lp->fd_buf_dma); | |
1006 | lp->fd_buf = NULL; | |
1007 | return -ENOMEM; | |
1008 | } | |
1009 | #else | |
1010 | lp->rx_skbs[i].skb = | |
1011 | alloc_rxbuf_skb(dev, lp->pci_dev, | |
1012 | &lp->rx_skbs[i].skb_dma); | |
1013 | if (!lp->rx_skbs[i].skb) { | |
1014 | while (--i >= 0) { | |
1015 | free_rxbuf_skb(lp->pci_dev, | |
1016 | lp->rx_skbs[i].skb, | |
1017 | lp->rx_skbs[i].skb_dma); | |
1018 | lp->rx_skbs[i].skb = NULL; | |
1019 | } | |
1020 | pci_free_consistent(lp->pci_dev, | |
1021 | PAGE_SIZE * FD_PAGE_NUM, | |
1022 | lp->fd_buf, | |
1023 | lp->fd_buf_dma); | |
1024 | lp->fd_buf = NULL; | |
1da177e4 LT |
1025 | return -ENOMEM; |
1026 | } | |
1da177e4 LT |
1027 | #endif |
1028 | } | |
eea221ce AN |
1029 | printk(KERN_DEBUG "%s: FD buf %p DataBuf", |
1030 | dev->name, lp->fd_buf); | |
1031 | #ifdef TC35815_USE_PACKEDBUFFER | |
1032 | printk(" DataBuf"); | |
1033 | for (i = 0; i < RX_BUF_NUM; i++) | |
1034 | printk(" %p", lp->data_buf[i]); | |
1da177e4 | 1035 | #endif |
eea221ce | 1036 | printk("\n"); |
1da177e4 | 1037 | } else { |
7f225b42 AN |
1038 | for (i = 0; i < FD_PAGE_NUM; i++) |
1039 | clear_page((void *)((unsigned long)lp->fd_buf + | |
1040 | i * PAGE_SIZE)); | |
1da177e4 | 1041 | } |
1da177e4 | 1042 | fd_addr = (unsigned long)lp->fd_buf; |
1da177e4 LT |
1043 | |
1044 | /* Free Descriptors (for Receive) */ | |
1045 | lp->rfd_base = (struct RxFD *)fd_addr; | |
1046 | fd_addr += sizeof(struct RxFD) * RX_FD_NUM; | |
7f225b42 | 1047 | for (i = 0; i < RX_FD_NUM; i++) |
1da177e4 | 1048 | lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD); |
1da177e4 | 1049 | lp->rfd_cur = lp->rfd_base; |
eea221ce | 1050 | lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1); |
1da177e4 LT |
1051 | |
1052 | /* Transmit Descriptors */ | |
1053 | lp->tfd_base = (struct TxFD *)fd_addr; | |
1054 | fd_addr += sizeof(struct TxFD) * TX_FD_NUM; | |
1055 | for (i = 0; i < TX_FD_NUM; i++) { | |
eea221ce AN |
1056 | lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1])); |
1057 | lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff); | |
1da177e4 LT |
1058 | lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0); |
1059 | } | |
eea221ce | 1060 | lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0])); |
1da177e4 LT |
1061 | lp->tfd_start = 0; |
1062 | lp->tfd_end = 0; | |
1063 | ||
1064 | /* Buffer List (for Receive) */ | |
1065 | lp->fbl_ptr = (struct FrFD *)fd_addr; | |
eea221ce AN |
1066 | lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr)); |
1067 | lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD); | |
1068 | #ifndef TC35815_USE_PACKEDBUFFER | |
1069 | /* | |
1070 | * move all allocated skbs to head of rx_skbs[] array. | |
1071 | * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in | |
1072 | * tc35815_rx() had failed. | |
1073 | */ | |
1074 | lp->fbl_count = 0; | |
1075 | for (i = 0; i < RX_BUF_NUM; i++) { | |
1076 | if (lp->rx_skbs[i].skb) { | |
1077 | if (i != lp->fbl_count) { | |
1078 | lp->rx_skbs[lp->fbl_count].skb = | |
1079 | lp->rx_skbs[i].skb; | |
1080 | lp->rx_skbs[lp->fbl_count].skb_dma = | |
1081 | lp->rx_skbs[i].skb_dma; | |
1082 | } | |
1083 | lp->fbl_count++; | |
1084 | } | |
1085 | } | |
1086 | #endif | |
1087 | for (i = 0; i < RX_BUF_NUM; i++) { | |
1088 | #ifdef TC35815_USE_PACKEDBUFFER | |
1089 | lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]); | |
1090 | #else | |
1091 | if (i >= lp->fbl_count) { | |
1092 | lp->fbl_ptr->bd[i].BuffData = 0; | |
1093 | lp->fbl_ptr->bd[i].BDCtl = 0; | |
1094 | continue; | |
1095 | } | |
1096 | lp->fbl_ptr->bd[i].BuffData = | |
1097 | cpu_to_le32(lp->rx_skbs[i].skb_dma); | |
1098 | #endif | |
1da177e4 LT |
1099 | /* BDID is index of FrFD.bd[] */ |
1100 | lp->fbl_ptr->bd[i].BDCtl = | |
eea221ce AN |
1101 | cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) | |
1102 | RX_BUF_SIZE); | |
1da177e4 | 1103 | } |
eea221ce | 1104 | #ifdef TC35815_USE_PACKEDBUFFER |
1da177e4 | 1105 | lp->fbl_curid = 0; |
eea221ce | 1106 | #endif |
1da177e4 | 1107 | |
eea221ce AN |
1108 | printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n", |
1109 | dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr); | |
1da177e4 LT |
1110 | return 0; |
1111 | } | |
1112 | ||
1113 | static void | |
1114 | tc35815_clear_queues(struct net_device *dev) | |
1115 | { | |
ee79b7fb | 1116 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
1117 | int i; |
1118 | ||
1119 | for (i = 0; i < TX_FD_NUM; i++) { | |
eea221ce AN |
1120 | u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem); |
1121 | struct sk_buff *skb = | |
1122 | fdsystem != 0xffffffff ? | |
1123 | lp->tx_skbs[fdsystem].skb : NULL; | |
1124 | #ifdef DEBUG | |
1125 | if (lp->tx_skbs[i].skb != skb) { | |
1126 | printk("%s: tx_skbs mismatch(%d).\n", dev->name, i); | |
1127 | panic_queues(dev); | |
1128 | } | |
1129 | #else | |
1130 | BUG_ON(lp->tx_skbs[i].skb != skb); | |
1131 | #endif | |
1132 | if (skb) { | |
1133 | pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE); | |
1134 | lp->tx_skbs[i].skb = NULL; | |
1135 | lp->tx_skbs[i].skb_dma = 0; | |
1da177e4 | 1136 | dev_kfree_skb_any(skb); |
eea221ce AN |
1137 | } |
1138 | lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff); | |
1da177e4 LT |
1139 | } |
1140 | ||
1141 | tc35815_init_queues(dev); | |
1142 | } | |
1143 | ||
1144 | static void | |
1145 | tc35815_free_queues(struct net_device *dev) | |
1146 | { | |
ee79b7fb | 1147 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
1148 | int i; |
1149 | ||
1150 | if (lp->tfd_base) { | |
1151 | for (i = 0; i < TX_FD_NUM; i++) { | |
eea221ce AN |
1152 | u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem); |
1153 | struct sk_buff *skb = | |
1154 | fdsystem != 0xffffffff ? | |
1155 | lp->tx_skbs[fdsystem].skb : NULL; | |
1156 | #ifdef DEBUG | |
1157 | if (lp->tx_skbs[i].skb != skb) { | |
1158 | printk("%s: tx_skbs mismatch(%d).\n", dev->name, i); | |
1159 | panic_queues(dev); | |
1160 | } | |
1161 | #else | |
1162 | BUG_ON(lp->tx_skbs[i].skb != skb); | |
1163 | #endif | |
1164 | if (skb) { | |
1165 | dev_kfree_skb(skb); | |
1166 | pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE); | |
1167 | lp->tx_skbs[i].skb = NULL; | |
1168 | lp->tx_skbs[i].skb_dma = 0; | |
1169 | } | |
1170 | lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff); | |
1da177e4 LT |
1171 | } |
1172 | } | |
1173 | ||
1da177e4 LT |
1174 | lp->rfd_base = NULL; |
1175 | lp->rfd_limit = NULL; | |
1176 | lp->rfd_cur = NULL; | |
1177 | lp->fbl_ptr = NULL; | |
1178 | ||
eea221ce AN |
1179 | for (i = 0; i < RX_BUF_NUM; i++) { |
1180 | #ifdef TC35815_USE_PACKEDBUFFER | |
1181 | if (lp->data_buf[i]) { | |
1182 | free_rxbuf_page(lp->pci_dev, | |
1183 | lp->data_buf[i], lp->data_buf_dma[i]); | |
1184 | lp->data_buf[i] = NULL; | |
1185 | } | |
1186 | #else | |
1187 | if (lp->rx_skbs[i].skb) { | |
1188 | free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb, | |
1189 | lp->rx_skbs[i].skb_dma); | |
1190 | lp->rx_skbs[i].skb = NULL; | |
1191 | } | |
1192 | #endif | |
1193 | } | |
1194 | if (lp->fd_buf) { | |
1195 | pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM, | |
1196 | lp->fd_buf, lp->fd_buf_dma); | |
1197 | lp->fd_buf = NULL; | |
1da177e4 | 1198 | } |
1da177e4 LT |
1199 | } |
1200 | ||
1201 | static void | |
1202 | dump_txfd(struct TxFD *fd) | |
1203 | { | |
1204 | printk("TxFD(%p): %08x %08x %08x %08x\n", fd, | |
1205 | le32_to_cpu(fd->fd.FDNext), | |
1206 | le32_to_cpu(fd->fd.FDSystem), | |
1207 | le32_to_cpu(fd->fd.FDStat), | |
1208 | le32_to_cpu(fd->fd.FDCtl)); | |
1209 | printk("BD: "); | |
1210 | printk(" %08x %08x", | |
1211 | le32_to_cpu(fd->bd.BuffData), | |
1212 | le32_to_cpu(fd->bd.BDCtl)); | |
1213 | printk("\n"); | |
1214 | } | |
1215 | ||
1216 | static int | |
1217 | dump_rxfd(struct RxFD *fd) | |
1218 | { | |
1219 | int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT; | |
1220 | if (bd_count > 8) | |
1221 | bd_count = 8; | |
1222 | printk("RxFD(%p): %08x %08x %08x %08x\n", fd, | |
1223 | le32_to_cpu(fd->fd.FDNext), | |
1224 | le32_to_cpu(fd->fd.FDSystem), | |
1225 | le32_to_cpu(fd->fd.FDStat), | |
1226 | le32_to_cpu(fd->fd.FDCtl)); | |
1227 | if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD) | |
7f225b42 | 1228 | return 0; |
1da177e4 LT |
1229 | printk("BD: "); |
1230 | for (i = 0; i < bd_count; i++) | |
1231 | printk(" %08x %08x", | |
1232 | le32_to_cpu(fd->bd[i].BuffData), | |
1233 | le32_to_cpu(fd->bd[i].BDCtl)); | |
1234 | printk("\n"); | |
1235 | return bd_count; | |
1236 | } | |
1237 | ||
eea221ce | 1238 | #if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER) |
1da177e4 LT |
1239 | static void |
1240 | dump_frfd(struct FrFD *fd) | |
1241 | { | |
1242 | int i; | |
1243 | printk("FrFD(%p): %08x %08x %08x %08x\n", fd, | |
1244 | le32_to_cpu(fd->fd.FDNext), | |
1245 | le32_to_cpu(fd->fd.FDSystem), | |
1246 | le32_to_cpu(fd->fd.FDStat), | |
1247 | le32_to_cpu(fd->fd.FDCtl)); | |
1248 | printk("BD: "); | |
eea221ce | 1249 | for (i = 0; i < RX_BUF_NUM; i++) |
1da177e4 LT |
1250 | printk(" %08x %08x", |
1251 | le32_to_cpu(fd->bd[i].BuffData), | |
1252 | le32_to_cpu(fd->bd[i].BDCtl)); | |
1253 | printk("\n"); | |
1254 | } | |
eea221ce | 1255 | #endif |
1da177e4 | 1256 | |
eea221ce | 1257 | #ifdef DEBUG |
1da177e4 LT |
1258 | static void |
1259 | panic_queues(struct net_device *dev) | |
1260 | { | |
ee79b7fb | 1261 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
1262 | int i; |
1263 | ||
eea221ce | 1264 | printk("TxFD base %p, start %u, end %u\n", |
1da177e4 LT |
1265 | lp->tfd_base, lp->tfd_start, lp->tfd_end); |
1266 | printk("RxFD base %p limit %p cur %p\n", | |
1267 | lp->rfd_base, lp->rfd_limit, lp->rfd_cur); | |
1268 | printk("FrFD %p\n", lp->fbl_ptr); | |
1269 | for (i = 0; i < TX_FD_NUM; i++) | |
1270 | dump_txfd(&lp->tfd_base[i]); | |
1271 | for (i = 0; i < RX_FD_NUM; i++) { | |
1272 | int bd_count = dump_rxfd(&lp->rfd_base[i]); | |
1273 | i += (bd_count + 1) / 2; /* skip BDs */ | |
1274 | } | |
1275 | dump_frfd(lp->fbl_ptr); | |
1276 | panic("%s: Illegal queue state.", dev->name); | |
1277 | } | |
1da177e4 LT |
1278 | #endif |
1279 | ||
958eb80b | 1280 | static void print_eth(const u8 *add) |
1da177e4 | 1281 | { |
958eb80b | 1282 | DECLARE_MAC_BUF(mac); |
1da177e4 | 1283 | |
958eb80b AN |
1284 | printk(KERN_DEBUG "print_eth(%p)\n", add); |
1285 | printk(KERN_DEBUG " %s =>", print_mac(mac, add + 6)); | |
1286 | printk(KERN_CONT " %s : %02x%02x\n", | |
1287 | print_mac(mac, add), add[12], add[13]); | |
1da177e4 LT |
1288 | } |
1289 | ||
eea221ce AN |
1290 | static int tc35815_tx_full(struct net_device *dev) |
1291 | { | |
ee79b7fb | 1292 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
1293 | return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end); |
1294 | } | |
1295 | ||
1296 | static void tc35815_restart(struct net_device *dev) | |
1297 | { | |
ee79b7fb | 1298 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce | 1299 | |
c6686fe3 | 1300 | if (lp->phy_dev) { |
eea221ce | 1301 | int timeout; |
c6686fe3 AN |
1302 | |
1303 | phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET); | |
eea221ce AN |
1304 | timeout = 100; |
1305 | while (--timeout) { | |
c6686fe3 | 1306 | if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET)) |
eea221ce AN |
1307 | break; |
1308 | udelay(1); | |
1309 | } | |
1310 | if (!timeout) | |
1311 | printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name); | |
1312 | } | |
1313 | ||
c6686fe3 | 1314 | spin_lock_irq(&lp->lock); |
eea221ce AN |
1315 | tc35815_chip_reset(dev); |
1316 | tc35815_clear_queues(dev); | |
1317 | tc35815_chip_init(dev); | |
1318 | /* Reconfigure CAM again since tc35815_chip_init() initialize it. */ | |
1319 | tc35815_set_multicast_list(dev); | |
c6686fe3 AN |
1320 | spin_unlock_irq(&lp->lock); |
1321 | ||
1322 | netif_wake_queue(dev); | |
eea221ce AN |
1323 | } |
1324 | ||
c6686fe3 AN |
1325 | static void tc35815_restart_work(struct work_struct *work) |
1326 | { | |
1327 | struct tc35815_local *lp = | |
1328 | container_of(work, struct tc35815_local, restart_work); | |
1329 | struct net_device *dev = lp->dev; | |
1330 | ||
1331 | tc35815_restart(dev); | |
1332 | } | |
1333 | ||
1334 | static void tc35815_schedule_restart(struct net_device *dev) | |
eea221ce | 1335 | { |
ee79b7fb | 1336 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
1337 | struct tc35815_regs __iomem *tr = |
1338 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1339 | ||
c6686fe3 AN |
1340 | /* disable interrupts */ |
1341 | tc_writel(0, &tr->Int_En); | |
1342 | tc_writel(tc_readl(&tr->DMA_Ctl) | DMA_IntMask, &tr->DMA_Ctl); | |
1343 | schedule_work(&lp->restart_work); | |
1344 | } | |
1345 | ||
1346 | static void tc35815_tx_timeout(struct net_device *dev) | |
1347 | { | |
1348 | struct tc35815_regs __iomem *tr = | |
1349 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1350 | ||
eea221ce AN |
1351 | printk(KERN_WARNING "%s: transmit timed out, status %#x\n", |
1352 | dev->name, tc_readl(&tr->Tx_Stat)); | |
1353 | ||
1354 | /* Try to restart the adaptor. */ | |
c6686fe3 | 1355 | tc35815_schedule_restart(dev); |
c201abd9 | 1356 | dev->stats.tx_errors++; |
eea221ce AN |
1357 | } |
1358 | ||
1da177e4 | 1359 | /* |
c6686fe3 | 1360 | * Open/initialize the controller. This is called (in the current kernel) |
1da177e4 LT |
1361 | * sometime after booting when the 'ifconfig' program is run. |
1362 | * | |
1363 | * This routine should set everything up anew at each open, even | |
1364 | * registers that "should" only need to be set once at boot, so that | |
1365 | * there is non-reboot way to recover if something goes wrong. | |
1366 | */ | |
1367 | static int | |
1368 | tc35815_open(struct net_device *dev) | |
1369 | { | |
ee79b7fb | 1370 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce | 1371 | |
1da177e4 LT |
1372 | /* |
1373 | * This is used if the interrupt line can turned off (shared). | |
1374 | * See 3c503.c for an example of selecting the IRQ at config-time. | |
1375 | */ | |
7f225b42 AN |
1376 | if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, |
1377 | dev->name, dev)) | |
1da177e4 | 1378 | return -EAGAIN; |
1da177e4 LT |
1379 | |
1380 | tc35815_chip_reset(dev); | |
1381 | ||
1382 | if (tc35815_init_queues(dev) != 0) { | |
1383 | free_irq(dev->irq, dev); | |
1384 | return -EAGAIN; | |
1385 | } | |
1386 | ||
bea3348e SH |
1387 | #ifdef TC35815_NAPI |
1388 | napi_enable(&lp->napi); | |
1389 | #endif | |
1390 | ||
1da177e4 | 1391 | /* Reset the hardware here. Don't forget to set the station address. */ |
eea221ce | 1392 | spin_lock_irq(&lp->lock); |
1da177e4 | 1393 | tc35815_chip_init(dev); |
eea221ce | 1394 | spin_unlock_irq(&lp->lock); |
1da177e4 | 1395 | |
c6686fe3 AN |
1396 | /* schedule a link state check */ |
1397 | phy_start(lp->phy_dev); | |
1398 | ||
eea221ce AN |
1399 | /* We are now ready to accept transmit requeusts from |
1400 | * the queueing layer of the networking. | |
1401 | */ | |
1da177e4 LT |
1402 | netif_start_queue(dev); |
1403 | ||
1404 | return 0; | |
1405 | } | |
1406 | ||
eea221ce AN |
1407 | /* This will only be invoked if your driver is _not_ in XOFF state. |
1408 | * What this means is that you need not check it, and that this | |
1409 | * invariant will hold if you make sure that the netif_*_queue() | |
1410 | * calls are done at the proper times. | |
1411 | */ | |
1412 | static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev) | |
1da177e4 | 1413 | { |
ee79b7fb | 1414 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce | 1415 | struct TxFD *txfd; |
1da177e4 LT |
1416 | unsigned long flags; |
1417 | ||
eea221ce AN |
1418 | /* If some error occurs while trying to transmit this |
1419 | * packet, you should return '1' from this function. | |
1420 | * In such a case you _may not_ do anything to the | |
1421 | * SKB, it is still owned by the network queueing | |
1422 | * layer when an error is returned. This means you | |
1423 | * may not modify any SKB fields, you may not free | |
1424 | * the SKB, etc. | |
1425 | */ | |
1426 | ||
1427 | /* This is the most common case for modern hardware. | |
1428 | * The spinlock protects this code from the TX complete | |
1429 | * hardware interrupt handler. Queue flow control is | |
1430 | * thus managed under this lock as well. | |
1431 | */ | |
1da177e4 | 1432 | spin_lock_irqsave(&lp->lock, flags); |
1da177e4 | 1433 | |
eea221ce AN |
1434 | /* failsafe... (handle txdone now if half of FDs are used) */ |
1435 | if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM > | |
1436 | TX_FD_NUM / 2) | |
1437 | tc35815_txdone(dev); | |
1438 | ||
1439 | if (netif_msg_pktdata(lp)) | |
1440 | print_eth(skb->data); | |
1441 | #ifdef DEBUG | |
1442 | if (lp->tx_skbs[lp->tfd_start].skb) { | |
1443 | printk("%s: tx_skbs conflict.\n", dev->name); | |
1444 | panic_queues(dev); | |
1da177e4 | 1445 | } |
eea221ce AN |
1446 | #else |
1447 | BUG_ON(lp->tx_skbs[lp->tfd_start].skb); | |
1da177e4 | 1448 | #endif |
eea221ce AN |
1449 | lp->tx_skbs[lp->tfd_start].skb = skb; |
1450 | lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE); | |
1451 | ||
1452 | /*add to ring */ | |
1453 | txfd = &lp->tfd_base[lp->tfd_start]; | |
1454 | txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma); | |
1455 | txfd->bd.BDCtl = cpu_to_le32(skb->len); | |
1456 | txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start); | |
1457 | txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT)); | |
1458 | ||
1459 | if (lp->tfd_start == lp->tfd_end) { | |
1460 | struct tc35815_regs __iomem *tr = | |
1461 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1462 | /* Start DMA Transmitter. */ | |
1463 | txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL); | |
1da177e4 | 1464 | #ifdef GATHER_TXINT |
eea221ce | 1465 | txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx); |
1da177e4 | 1466 | #endif |
eea221ce AN |
1467 | if (netif_msg_tx_queued(lp)) { |
1468 | printk("%s: starting TxFD.\n", dev->name); | |
1469 | dump_txfd(txfd); | |
1470 | } | |
1471 | tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr); | |
1472 | } else { | |
1473 | txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL); | |
1474 | if (netif_msg_tx_queued(lp)) { | |
1475 | printk("%s: queueing TxFD.\n", dev->name); | |
1476 | dump_txfd(txfd); | |
1da177e4 | 1477 | } |
eea221ce AN |
1478 | } |
1479 | lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM; | |
1da177e4 | 1480 | |
eea221ce | 1481 | dev->trans_start = jiffies; |
1da177e4 | 1482 | |
eea221ce AN |
1483 | /* If we just used up the very last entry in the |
1484 | * TX ring on this device, tell the queueing | |
1485 | * layer to send no more. | |
1486 | */ | |
1487 | if (tc35815_tx_full(dev)) { | |
1488 | if (netif_msg_tx_queued(lp)) | |
1489 | printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name); | |
1490 | netif_stop_queue(dev); | |
1da177e4 LT |
1491 | } |
1492 | ||
eea221ce AN |
1493 | /* When the TX completion hw interrupt arrives, this |
1494 | * is when the transmit statistics are updated. | |
1495 | */ | |
1496 | ||
1497 | spin_unlock_irqrestore(&lp->lock, flags); | |
1da177e4 LT |
1498 | return 0; |
1499 | } | |
1500 | ||
1501 | #define FATAL_ERROR_INT \ | |
1502 | (Int_IntPCI | Int_DmParErr | Int_IntNRAbt) | |
eea221ce | 1503 | static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status) |
1da177e4 LT |
1504 | { |
1505 | static int count; | |
1506 | printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):", | |
1507 | dev->name, status); | |
1da177e4 LT |
1508 | if (status & Int_IntPCI) |
1509 | printk(" IntPCI"); | |
1510 | if (status & Int_DmParErr) | |
1511 | printk(" DmParErr"); | |
1512 | if (status & Int_IntNRAbt) | |
1513 | printk(" IntNRAbt"); | |
1514 | printk("\n"); | |
1515 | if (count++ > 100) | |
1516 | panic("%s: Too many fatal errors.", dev->name); | |
eea221ce | 1517 | printk(KERN_WARNING "%s: Resetting ...\n", dev->name); |
1da177e4 | 1518 | /* Try to restart the adaptor. */ |
c6686fe3 | 1519 | tc35815_schedule_restart(dev); |
eea221ce AN |
1520 | } |
1521 | ||
1522 | #ifdef TC35815_NAPI | |
1523 | static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit) | |
1524 | #else | |
1525 | static int tc35815_do_interrupt(struct net_device *dev, u32 status) | |
1526 | #endif | |
1527 | { | |
ee79b7fb | 1528 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
1529 | struct tc35815_regs __iomem *tr = |
1530 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1531 | int ret = -1; | |
1532 | ||
1533 | /* Fatal errors... */ | |
1534 | if (status & FATAL_ERROR_INT) { | |
1535 | tc35815_fatal_error_interrupt(dev, status); | |
1536 | return 0; | |
1537 | } | |
1538 | /* recoverable errors */ | |
1539 | if (status & Int_IntFDAEx) { | |
1540 | /* disable FDAEx int. (until we make rooms...) */ | |
1541 | tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En); | |
1542 | printk(KERN_WARNING | |
1543 | "%s: Free Descriptor Area Exhausted (%#x).\n", | |
1544 | dev->name, status); | |
c201abd9 | 1545 | dev->stats.rx_dropped++; |
eea221ce AN |
1546 | ret = 0; |
1547 | } | |
1548 | if (status & Int_IntBLEx) { | |
1549 | /* disable BLEx int. (until we make rooms...) */ | |
1550 | tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En); | |
1551 | printk(KERN_WARNING | |
1552 | "%s: Buffer List Exhausted (%#x).\n", | |
1553 | dev->name, status); | |
c201abd9 | 1554 | dev->stats.rx_dropped++; |
eea221ce AN |
1555 | ret = 0; |
1556 | } | |
1557 | if (status & Int_IntExBD) { | |
1558 | printk(KERN_WARNING | |
1559 | "%s: Excessive Buffer Descriptiors (%#x).\n", | |
1560 | dev->name, status); | |
c201abd9 | 1561 | dev->stats.rx_length_errors++; |
eea221ce AN |
1562 | ret = 0; |
1563 | } | |
1564 | ||
1565 | /* normal notification */ | |
1566 | if (status & Int_IntMacRx) { | |
1567 | /* Got a packet(s). */ | |
1568 | #ifdef TC35815_NAPI | |
1569 | ret = tc35815_rx(dev, limit); | |
1570 | #else | |
1571 | tc35815_rx(dev); | |
1572 | ret = 0; | |
1573 | #endif | |
1574 | lp->lstats.rx_ints++; | |
1575 | } | |
1576 | if (status & Int_IntMacTx) { | |
1577 | /* Transmit complete. */ | |
1578 | lp->lstats.tx_ints++; | |
1579 | tc35815_txdone(dev); | |
1580 | netif_wake_queue(dev); | |
1581 | ret = 0; | |
1582 | } | |
1583 | return ret; | |
1da177e4 LT |
1584 | } |
1585 | ||
1586 | /* | |
1587 | * The typical workload of the driver: | |
eea221ce | 1588 | * Handle the network interface interrupts. |
1da177e4 | 1589 | */ |
7d12e780 | 1590 | static irqreturn_t tc35815_interrupt(int irq, void *dev_id) |
1da177e4 LT |
1591 | { |
1592 | struct net_device *dev = dev_id; | |
bea3348e | 1593 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
1594 | struct tc35815_regs __iomem *tr = |
1595 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1596 | #ifdef TC35815_NAPI | |
1597 | u32 dmactl = tc_readl(&tr->DMA_Ctl); | |
1598 | ||
1599 | if (!(dmactl & DMA_IntMask)) { | |
1600 | /* disable interrupts */ | |
1601 | tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl); | |
bea3348e SH |
1602 | if (netif_rx_schedule_prep(dev, &lp->napi)) |
1603 | __netif_rx_schedule(dev, &lp->napi); | |
eea221ce AN |
1604 | else { |
1605 | printk(KERN_ERR "%s: interrupt taken in poll\n", | |
1606 | dev->name); | |
1607 | BUG(); | |
1da177e4 | 1608 | } |
eea221ce AN |
1609 | (void)tc_readl(&tr->Int_Src); /* flush */ |
1610 | return IRQ_HANDLED; | |
1611 | } | |
1612 | return IRQ_NONE; | |
1613 | #else | |
eea221ce AN |
1614 | int handled; |
1615 | u32 status; | |
1616 | ||
1617 | spin_lock(&lp->lock); | |
1618 | status = tc_readl(&tr->Int_Src); | |
1619 | tc_writel(status, &tr->Int_Src); /* write to clear */ | |
1620 | handled = tc35815_do_interrupt(dev, status); | |
1621 | (void)tc_readl(&tr->Int_Src); /* flush */ | |
1622 | spin_unlock(&lp->lock); | |
1623 | return IRQ_RETVAL(handled >= 0); | |
1624 | #endif /* TC35815_NAPI */ | |
1625 | } | |
1da177e4 | 1626 | |
eea221ce AN |
1627 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1628 | static void tc35815_poll_controller(struct net_device *dev) | |
1629 | { | |
1630 | disable_irq(dev->irq); | |
1631 | tc35815_interrupt(dev->irq, dev); | |
1632 | enable_irq(dev->irq); | |
1da177e4 | 1633 | } |
eea221ce | 1634 | #endif |
1da177e4 LT |
1635 | |
1636 | /* We have a good packet(s), get it/them out of the buffers. */ | |
eea221ce AN |
1637 | #ifdef TC35815_NAPI |
1638 | static int | |
1639 | tc35815_rx(struct net_device *dev, int limit) | |
1640 | #else | |
1da177e4 LT |
1641 | static void |
1642 | tc35815_rx(struct net_device *dev) | |
eea221ce | 1643 | #endif |
1da177e4 | 1644 | { |
ee79b7fb | 1645 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
1646 | unsigned int fdctl; |
1647 | int i; | |
1648 | int buf_free_count = 0; | |
1649 | int fd_free_count = 0; | |
eea221ce AN |
1650 | #ifdef TC35815_NAPI |
1651 | int received = 0; | |
1652 | #endif | |
1da177e4 LT |
1653 | |
1654 | while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) { | |
1655 | int status = le32_to_cpu(lp->rfd_cur->fd.FDStat); | |
1656 | int pkt_len = fdctl & FD_FDLength_MASK; | |
1da177e4 | 1657 | int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT; |
eea221ce AN |
1658 | #ifdef DEBUG |
1659 | struct RxFD *next_rfd; | |
1660 | #endif | |
1661 | #if (RX_CTL_CMD & Rx_StripCRC) == 0 | |
1662 | pkt_len -= 4; | |
1663 | #endif | |
1da177e4 | 1664 | |
eea221ce | 1665 | if (netif_msg_rx_status(lp)) |
1da177e4 LT |
1666 | dump_rxfd(lp->rfd_cur); |
1667 | if (status & Rx_Good) { | |
1da177e4 LT |
1668 | struct sk_buff *skb; |
1669 | unsigned char *data; | |
eea221ce AN |
1670 | int cur_bd; |
1671 | #ifdef TC35815_USE_PACKEDBUFFER | |
1672 | int offset; | |
1673 | #endif | |
6aa20a22 | 1674 | |
eea221ce AN |
1675 | #ifdef TC35815_NAPI |
1676 | if (--limit < 0) | |
1677 | break; | |
1678 | #endif | |
1679 | #ifdef TC35815_USE_PACKEDBUFFER | |
1680 | BUG_ON(bd_count > 2); | |
1da177e4 LT |
1681 | skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */ |
1682 | if (skb == NULL) { | |
1683 | printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", | |
1684 | dev->name); | |
c201abd9 | 1685 | dev->stats.rx_dropped++; |
1da177e4 LT |
1686 | break; |
1687 | } | |
1688 | skb_reserve(skb, 2); /* 16 bit alignment */ | |
1da177e4 LT |
1689 | |
1690 | data = skb_put(skb, pkt_len); | |
1691 | ||
1692 | /* copy from receive buffer */ | |
1693 | cur_bd = 0; | |
1694 | offset = 0; | |
1695 | while (offset < pkt_len && cur_bd < bd_count) { | |
1696 | int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) & | |
1697 | BD_BuffLength_MASK; | |
eea221ce AN |
1698 | dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData); |
1699 | void *rxbuf = rxbuf_bus_to_virt(lp, dma); | |
1700 | if (offset + len > pkt_len) | |
1701 | len = pkt_len - offset; | |
1702 | #ifdef TC35815_DMA_SYNC_ONDEMAND | |
1703 | pci_dma_sync_single_for_cpu(lp->pci_dev, | |
1704 | dma, len, | |
1705 | PCI_DMA_FROMDEVICE); | |
1da177e4 LT |
1706 | #endif |
1707 | memcpy(data + offset, rxbuf, len); | |
793bc0af AN |
1708 | #ifdef TC35815_DMA_SYNC_ONDEMAND |
1709 | pci_dma_sync_single_for_device(lp->pci_dev, | |
1710 | dma, len, | |
1711 | PCI_DMA_FROMDEVICE); | |
1712 | #endif | |
1da177e4 LT |
1713 | offset += len; |
1714 | cur_bd++; | |
1715 | } | |
eea221ce AN |
1716 | #else /* TC35815_USE_PACKEDBUFFER */ |
1717 | BUG_ON(bd_count > 1); | |
1718 | cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl) | |
1719 | & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT; | |
1720 | #ifdef DEBUG | |
1721 | if (cur_bd >= RX_BUF_NUM) { | |
1722 | printk("%s: invalid BDID.\n", dev->name); | |
1723 | panic_queues(dev); | |
1724 | } | |
1725 | BUG_ON(lp->rx_skbs[cur_bd].skb_dma != | |
1726 | (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3)); | |
1727 | if (!lp->rx_skbs[cur_bd].skb) { | |
1728 | printk("%s: NULL skb.\n", dev->name); | |
1729 | panic_queues(dev); | |
1730 | } | |
1731 | #else | |
1732 | BUG_ON(cur_bd >= RX_BUF_NUM); | |
1da177e4 | 1733 | #endif |
eea221ce AN |
1734 | skb = lp->rx_skbs[cur_bd].skb; |
1735 | prefetch(skb->data); | |
1736 | lp->rx_skbs[cur_bd].skb = NULL; | |
1737 | lp->fbl_count--; | |
1738 | pci_unmap_single(lp->pci_dev, | |
1739 | lp->rx_skbs[cur_bd].skb_dma, | |
1740 | RX_BUF_SIZE, PCI_DMA_FROMDEVICE); | |
1741 | if (!HAVE_DMA_RXALIGN(lp)) | |
1742 | memmove(skb->data, skb->data - 2, pkt_len); | |
1743 | data = skb_put(skb, pkt_len); | |
1744 | #endif /* TC35815_USE_PACKEDBUFFER */ | |
1745 | if (netif_msg_pktdata(lp)) | |
1da177e4 LT |
1746 | print_eth(data); |
1747 | skb->protocol = eth_type_trans(skb, dev); | |
eea221ce AN |
1748 | #ifdef TC35815_NAPI |
1749 | netif_receive_skb(skb); | |
1750 | received++; | |
1751 | #else | |
1da177e4 | 1752 | netif_rx(skb); |
eea221ce AN |
1753 | #endif |
1754 | dev->last_rx = jiffies; | |
c201abd9 AN |
1755 | dev->stats.rx_packets++; |
1756 | dev->stats.rx_bytes += pkt_len; | |
1da177e4 | 1757 | } else { |
c201abd9 | 1758 | dev->stats.rx_errors++; |
eea221ce AN |
1759 | printk(KERN_DEBUG "%s: Rx error (status %x)\n", |
1760 | dev->name, status & Rx_Stat_Mask); | |
1da177e4 LT |
1761 | /* WORKAROUND: LongErr and CRCErr means Overflow. */ |
1762 | if ((status & Rx_LongErr) && (status & Rx_CRCErr)) { | |
1763 | status &= ~(Rx_LongErr|Rx_CRCErr); | |
1764 | status |= Rx_Over; | |
1765 | } | |
c201abd9 AN |
1766 | if (status & Rx_LongErr) |
1767 | dev->stats.rx_length_errors++; | |
1768 | if (status & Rx_Over) | |
1769 | dev->stats.rx_fifo_errors++; | |
1770 | if (status & Rx_CRCErr) | |
1771 | dev->stats.rx_crc_errors++; | |
1772 | if (status & Rx_Align) | |
1773 | dev->stats.rx_frame_errors++; | |
1da177e4 LT |
1774 | } |
1775 | ||
1776 | if (bd_count > 0) { | |
1777 | /* put Free Buffer back to controller */ | |
1778 | int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl); | |
1779 | unsigned char id = | |
1780 | (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT; | |
eea221ce AN |
1781 | #ifdef DEBUG |
1782 | if (id >= RX_BUF_NUM) { | |
1da177e4 LT |
1783 | printk("%s: invalid BDID.\n", dev->name); |
1784 | panic_queues(dev); | |
1785 | } | |
eea221ce AN |
1786 | #else |
1787 | BUG_ON(id >= RX_BUF_NUM); | |
1788 | #endif | |
1da177e4 | 1789 | /* free old buffers */ |
eea221ce AN |
1790 | #ifdef TC35815_USE_PACKEDBUFFER |
1791 | while (lp->fbl_curid != id) | |
1792 | #else | |
1793 | while (lp->fbl_count < RX_BUF_NUM) | |
1794 | #endif | |
1795 | { | |
1796 | #ifdef TC35815_USE_PACKEDBUFFER | |
1797 | unsigned char curid = lp->fbl_curid; | |
1798 | #else | |
1799 | unsigned char curid = | |
1800 | (id + 1 + lp->fbl_count) % RX_BUF_NUM; | |
1801 | #endif | |
1802 | struct BDesc *bd = &lp->fbl_ptr->bd[curid]; | |
1803 | #ifdef DEBUG | |
1804 | bdctl = le32_to_cpu(bd->BDCtl); | |
1da177e4 LT |
1805 | if (bdctl & BD_CownsBD) { |
1806 | printk("%s: Freeing invalid BD.\n", | |
1807 | dev->name); | |
1808 | panic_queues(dev); | |
1809 | } | |
eea221ce | 1810 | #endif |
3a4fa0a2 | 1811 | /* pass BD to controller */ |
eea221ce AN |
1812 | #ifndef TC35815_USE_PACKEDBUFFER |
1813 | if (!lp->rx_skbs[curid].skb) { | |
1814 | lp->rx_skbs[curid].skb = | |
1815 | alloc_rxbuf_skb(dev, | |
1816 | lp->pci_dev, | |
1817 | &lp->rx_skbs[curid].skb_dma); | |
1818 | if (!lp->rx_skbs[curid].skb) | |
1819 | break; /* try on next reception */ | |
1820 | bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma); | |
1821 | } | |
1822 | #endif /* TC35815_USE_PACKEDBUFFER */ | |
1da177e4 | 1823 | /* Note: BDLength was modified by chip. */ |
eea221ce AN |
1824 | bd->BDCtl = cpu_to_le32(BD_CownsBD | |
1825 | (curid << BD_RxBDID_SHIFT) | | |
1826 | RX_BUF_SIZE); | |
1827 | #ifdef TC35815_USE_PACKEDBUFFER | |
1828 | lp->fbl_curid = (curid + 1) % RX_BUF_NUM; | |
1829 | if (netif_msg_rx_status(lp)) { | |
1da177e4 LT |
1830 | printk("%s: Entering new FBD %d\n", |
1831 | dev->name, lp->fbl_curid); | |
1832 | dump_frfd(lp->fbl_ptr); | |
1833 | } | |
eea221ce AN |
1834 | #else |
1835 | lp->fbl_count++; | |
1836 | #endif | |
1da177e4 LT |
1837 | buf_free_count++; |
1838 | } | |
1839 | } | |
1840 | ||
1841 | /* put RxFD back to controller */ | |
eea221ce AN |
1842 | #ifdef DEBUG |
1843 | next_rfd = fd_bus_to_virt(lp, | |
1844 | le32_to_cpu(lp->rfd_cur->fd.FDNext)); | |
1da177e4 LT |
1845 | if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) { |
1846 | printk("%s: RxFD FDNext invalid.\n", dev->name); | |
1847 | panic_queues(dev); | |
1848 | } | |
eea221ce | 1849 | #endif |
1da177e4 | 1850 | for (i = 0; i < (bd_count + 1) / 2 + 1; i++) { |
3a4fa0a2 | 1851 | /* pass FD to controller */ |
eea221ce AN |
1852 | #ifdef DEBUG |
1853 | lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead); | |
1854 | #else | |
1855 | lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL); | |
1856 | #endif | |
1da177e4 LT |
1857 | lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD); |
1858 | lp->rfd_cur++; | |
1859 | fd_free_count++; | |
1860 | } | |
eea221ce AN |
1861 | if (lp->rfd_cur > lp->rfd_limit) |
1862 | lp->rfd_cur = lp->rfd_base; | |
1863 | #ifdef DEBUG | |
1864 | if (lp->rfd_cur != next_rfd) | |
1865 | printk("rfd_cur = %p, next_rfd %p\n", | |
1866 | lp->rfd_cur, next_rfd); | |
1867 | #endif | |
1da177e4 LT |
1868 | } |
1869 | ||
1870 | /* re-enable BL/FDA Exhaust interrupts. */ | |
1871 | if (fd_free_count) { | |
eea221ce AN |
1872 | struct tc35815_regs __iomem *tr = |
1873 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1874 | u32 en, en_old = tc_readl(&tr->Int_En); | |
1875 | en = en_old | Int_FDAExEn; | |
1da177e4 | 1876 | if (buf_free_count) |
eea221ce AN |
1877 | en |= Int_BLExEn; |
1878 | if (en != en_old) | |
1879 | tc_writel(en, &tr->Int_En); | |
1da177e4 | 1880 | } |
eea221ce AN |
1881 | #ifdef TC35815_NAPI |
1882 | return received; | |
1883 | #endif | |
1da177e4 LT |
1884 | } |
1885 | ||
eea221ce | 1886 | #ifdef TC35815_NAPI |
bea3348e | 1887 | static int tc35815_poll(struct napi_struct *napi, int budget) |
eea221ce | 1888 | { |
bea3348e SH |
1889 | struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi); |
1890 | struct net_device *dev = lp->dev; | |
eea221ce AN |
1891 | struct tc35815_regs __iomem *tr = |
1892 | (struct tc35815_regs __iomem *)dev->base_addr; | |
eea221ce AN |
1893 | int received = 0, handled; |
1894 | u32 status; | |
1895 | ||
1896 | spin_lock(&lp->lock); | |
1897 | status = tc_readl(&tr->Int_Src); | |
1898 | do { | |
1899 | tc_writel(status, &tr->Int_Src); /* write to clear */ | |
1900 | ||
1901 | handled = tc35815_do_interrupt(dev, status, limit); | |
1902 | if (handled >= 0) { | |
1903 | received += handled; | |
bea3348e | 1904 | if (received >= budget) |
eea221ce AN |
1905 | break; |
1906 | } | |
1907 | status = tc_readl(&tr->Int_Src); | |
1908 | } while (status); | |
1909 | spin_unlock(&lp->lock); | |
1910 | ||
bea3348e SH |
1911 | if (received < budget) { |
1912 | netif_rx_complete(dev, napi); | |
1913 | /* enable interrupts */ | |
1914 | tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl); | |
1915 | } | |
1916 | return received; | |
eea221ce AN |
1917 | } |
1918 | #endif | |
1919 | ||
1da177e4 LT |
1920 | #ifdef NO_CHECK_CARRIER |
1921 | #define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr) | |
1922 | #else | |
1923 | #define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr) | |
1924 | #endif | |
1925 | ||
1926 | static void | |
1927 | tc35815_check_tx_stat(struct net_device *dev, int status) | |
1928 | { | |
ee79b7fb | 1929 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
1930 | const char *msg = NULL; |
1931 | ||
1932 | /* count collisions */ | |
1933 | if (status & Tx_ExColl) | |
c201abd9 | 1934 | dev->stats.collisions += 16; |
1da177e4 | 1935 | if (status & Tx_TxColl_MASK) |
c201abd9 | 1936 | dev->stats.collisions += status & Tx_TxColl_MASK; |
1da177e4 | 1937 | |
eea221ce AN |
1938 | #ifndef NO_CHECK_CARRIER |
1939 | /* TX4939 does not have NCarr */ | |
c6686fe3 | 1940 | if (lp->chiptype == TC35815_TX4939) |
eea221ce AN |
1941 | status &= ~Tx_NCarr; |
1942 | #ifdef WORKAROUND_LOSTCAR | |
1da177e4 | 1943 | /* WORKAROUND: ignore LostCrS in full duplex operation */ |
c6686fe3 | 1944 | if (!lp->link || lp->duplex == DUPLEX_FULL) |
1da177e4 | 1945 | status &= ~Tx_NCarr; |
eea221ce AN |
1946 | #endif |
1947 | #endif | |
1da177e4 LT |
1948 | |
1949 | if (!(status & TX_STA_ERR)) { | |
1950 | /* no error. */ | |
c201abd9 | 1951 | dev->stats.tx_packets++; |
1da177e4 LT |
1952 | return; |
1953 | } | |
1954 | ||
c201abd9 | 1955 | dev->stats.tx_errors++; |
1da177e4 | 1956 | if (status & Tx_ExColl) { |
c201abd9 | 1957 | dev->stats.tx_aborted_errors++; |
1da177e4 LT |
1958 | msg = "Excessive Collision."; |
1959 | } | |
1960 | if (status & Tx_Under) { | |
c201abd9 | 1961 | dev->stats.tx_fifo_errors++; |
1da177e4 | 1962 | msg = "Tx FIFO Underrun."; |
eea221ce AN |
1963 | if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) { |
1964 | lp->lstats.tx_underrun++; | |
1965 | if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) { | |
1966 | struct tc35815_regs __iomem *tr = | |
1967 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1968 | tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh); | |
1969 | msg = "Tx FIFO Underrun.Change Tx threshold to max."; | |
1970 | } | |
1971 | } | |
1da177e4 LT |
1972 | } |
1973 | if (status & Tx_Defer) { | |
c201abd9 | 1974 | dev->stats.tx_fifo_errors++; |
1da177e4 LT |
1975 | msg = "Excessive Deferral."; |
1976 | } | |
1977 | #ifndef NO_CHECK_CARRIER | |
1978 | if (status & Tx_NCarr) { | |
c201abd9 | 1979 | dev->stats.tx_carrier_errors++; |
1da177e4 LT |
1980 | msg = "Lost Carrier Sense."; |
1981 | } | |
1982 | #endif | |
1983 | if (status & Tx_LateColl) { | |
c201abd9 | 1984 | dev->stats.tx_aborted_errors++; |
1da177e4 LT |
1985 | msg = "Late Collision."; |
1986 | } | |
1987 | if (status & Tx_TxPar) { | |
c201abd9 | 1988 | dev->stats.tx_fifo_errors++; |
1da177e4 LT |
1989 | msg = "Transmit Parity Error."; |
1990 | } | |
1991 | if (status & Tx_SQErr) { | |
c201abd9 | 1992 | dev->stats.tx_heartbeat_errors++; |
1da177e4 LT |
1993 | msg = "Signal Quality Error."; |
1994 | } | |
eea221ce | 1995 | if (msg && netif_msg_tx_err(lp)) |
1da177e4 LT |
1996 | printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status); |
1997 | } | |
1998 | ||
eea221ce AN |
1999 | /* This handles TX complete events posted by the device |
2000 | * via interrupts. | |
2001 | */ | |
1da177e4 LT |
2002 | static void |
2003 | tc35815_txdone(struct net_device *dev) | |
2004 | { | |
ee79b7fb | 2005 | struct tc35815_local *lp = netdev_priv(dev); |
1da177e4 LT |
2006 | struct TxFD *txfd; |
2007 | unsigned int fdctl; | |
1da177e4 LT |
2008 | |
2009 | txfd = &lp->tfd_base[lp->tfd_end]; | |
2010 | while (lp->tfd_start != lp->tfd_end && | |
2011 | !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) { | |
2012 | int status = le32_to_cpu(txfd->fd.FDStat); | |
2013 | struct sk_buff *skb; | |
2014 | unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext); | |
eea221ce | 2015 | u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem); |
1da177e4 | 2016 | |
eea221ce | 2017 | if (netif_msg_tx_done(lp)) { |
1da177e4 LT |
2018 | printk("%s: complete TxFD.\n", dev->name); |
2019 | dump_txfd(txfd); | |
2020 | } | |
2021 | tc35815_check_tx_stat(dev, status); | |
2022 | ||
eea221ce AN |
2023 | skb = fdsystem != 0xffffffff ? |
2024 | lp->tx_skbs[fdsystem].skb : NULL; | |
2025 | #ifdef DEBUG | |
2026 | if (lp->tx_skbs[lp->tfd_end].skb != skb) { | |
2027 | printk("%s: tx_skbs mismatch.\n", dev->name); | |
2028 | panic_queues(dev); | |
2029 | } | |
2030 | #else | |
2031 | BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb); | |
2032 | #endif | |
1da177e4 | 2033 | if (skb) { |
c201abd9 | 2034 | dev->stats.tx_bytes += skb->len; |
eea221ce AN |
2035 | pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE); |
2036 | lp->tx_skbs[lp->tfd_end].skb = NULL; | |
2037 | lp->tx_skbs[lp->tfd_end].skb_dma = 0; | |
2038 | #ifdef TC35815_NAPI | |
1da177e4 | 2039 | dev_kfree_skb_any(skb); |
eea221ce AN |
2040 | #else |
2041 | dev_kfree_skb_irq(skb); | |
2042 | #endif | |
1da177e4 | 2043 | } |
eea221ce | 2044 | txfd->fd.FDSystem = cpu_to_le32(0xffffffff); |
1da177e4 | 2045 | |
1da177e4 LT |
2046 | lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM; |
2047 | txfd = &lp->tfd_base[lp->tfd_end]; | |
eea221ce AN |
2048 | #ifdef DEBUG |
2049 | if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) { | |
1da177e4 LT |
2050 | printk("%s: TxFD FDNext invalid.\n", dev->name); |
2051 | panic_queues(dev); | |
2052 | } | |
eea221ce | 2053 | #endif |
1da177e4 LT |
2054 | if (fdnext & FD_Next_EOL) { |
2055 | /* DMA Transmitter has been stopping... */ | |
2056 | if (lp->tfd_end != lp->tfd_start) { | |
eea221ce AN |
2057 | struct tc35815_regs __iomem *tr = |
2058 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1da177e4 | 2059 | int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM; |
7f225b42 | 2060 | struct TxFD *txhead = &lp->tfd_base[head]; |
1da177e4 LT |
2061 | int qlen = (lp->tfd_start + TX_FD_NUM |
2062 | - lp->tfd_end) % TX_FD_NUM; | |
2063 | ||
eea221ce | 2064 | #ifdef DEBUG |
1da177e4 LT |
2065 | if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) { |
2066 | printk("%s: TxFD FDCtl invalid.\n", dev->name); | |
2067 | panic_queues(dev); | |
2068 | } | |
eea221ce | 2069 | #endif |
1da177e4 LT |
2070 | /* log max queue length */ |
2071 | if (lp->lstats.max_tx_qlen < qlen) | |
2072 | lp->lstats.max_tx_qlen = qlen; | |
2073 | ||
2074 | ||
2075 | /* start DMA Transmitter again */ | |
2076 | txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL); | |
2077 | #ifdef GATHER_TXINT | |
2078 | txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx); | |
2079 | #endif | |
eea221ce | 2080 | if (netif_msg_tx_queued(lp)) { |
1da177e4 LT |
2081 | printk("%s: start TxFD on queue.\n", |
2082 | dev->name); | |
2083 | dump_txfd(txfd); | |
2084 | } | |
eea221ce | 2085 | tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr); |
1da177e4 LT |
2086 | } |
2087 | break; | |
2088 | } | |
2089 | } | |
2090 | ||
eea221ce AN |
2091 | /* If we had stopped the queue due to a "tx full" |
2092 | * condition, and space has now been made available, | |
2093 | * wake up the queue. | |
2094 | */ | |
7f225b42 | 2095 | if (netif_queue_stopped(dev) && !tc35815_tx_full(dev)) |
eea221ce | 2096 | netif_wake_queue(dev); |
1da177e4 LT |
2097 | } |
2098 | ||
2099 | /* The inverse routine to tc35815_open(). */ | |
2100 | static int | |
2101 | tc35815_close(struct net_device *dev) | |
2102 | { | |
ee79b7fb | 2103 | struct tc35815_local *lp = netdev_priv(dev); |
bea3348e | 2104 | |
1da177e4 | 2105 | netif_stop_queue(dev); |
bea3348e SH |
2106 | #ifdef TC35815_NAPI |
2107 | napi_disable(&lp->napi); | |
2108 | #endif | |
c6686fe3 AN |
2109 | if (lp->phy_dev) |
2110 | phy_stop(lp->phy_dev); | |
2111 | cancel_work_sync(&lp->restart_work); | |
1da177e4 LT |
2112 | |
2113 | /* Flush the Tx and disable Rx here. */ | |
1da177e4 LT |
2114 | tc35815_chip_reset(dev); |
2115 | free_irq(dev->irq, dev); | |
2116 | ||
2117 | tc35815_free_queues(dev); | |
2118 | ||
2119 | return 0; | |
eea221ce | 2120 | |
1da177e4 LT |
2121 | } |
2122 | ||
2123 | /* | |
2124 | * Get the current statistics. | |
2125 | * This may be called with the card open or closed. | |
2126 | */ | |
2127 | static struct net_device_stats *tc35815_get_stats(struct net_device *dev) | |
2128 | { | |
eea221ce AN |
2129 | struct tc35815_regs __iomem *tr = |
2130 | (struct tc35815_regs __iomem *)dev->base_addr; | |
c201abd9 | 2131 | if (netif_running(dev)) |
1da177e4 | 2132 | /* Update the statistics from the device registers. */ |
c201abd9 | 2133 | dev->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt); |
1da177e4 | 2134 | |
c201abd9 | 2135 | return &dev->stats; |
1da177e4 LT |
2136 | } |
2137 | ||
eea221ce | 2138 | static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr) |
1da177e4 | 2139 | { |
ee79b7fb | 2140 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2141 | struct tc35815_regs __iomem *tr = |
2142 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1da177e4 | 2143 | int cam_index = index * 6; |
eea221ce AN |
2144 | u32 cam_data; |
2145 | u32 saved_addr; | |
958eb80b AN |
2146 | DECLARE_MAC_BUF(mac); |
2147 | ||
1da177e4 LT |
2148 | saved_addr = tc_readl(&tr->CAM_Adr); |
2149 | ||
958eb80b AN |
2150 | if (netif_msg_hw(lp)) |
2151 | printk(KERN_DEBUG "%s: CAM %d: %s\n", | |
2152 | dev->name, index, print_mac(mac, addr)); | |
1da177e4 LT |
2153 | if (index & 1) { |
2154 | /* read modify write */ | |
2155 | tc_writel(cam_index - 2, &tr->CAM_Adr); | |
2156 | cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000; | |
2157 | cam_data |= addr[0] << 8 | addr[1]; | |
2158 | tc_writel(cam_data, &tr->CAM_Data); | |
2159 | /* write whole word */ | |
2160 | tc_writel(cam_index + 2, &tr->CAM_Adr); | |
2161 | cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5]; | |
2162 | tc_writel(cam_data, &tr->CAM_Data); | |
2163 | } else { | |
2164 | /* write whole word */ | |
2165 | tc_writel(cam_index, &tr->CAM_Adr); | |
2166 | cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]; | |
2167 | tc_writel(cam_data, &tr->CAM_Data); | |
2168 | /* read modify write */ | |
2169 | tc_writel(cam_index + 4, &tr->CAM_Adr); | |
2170 | cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff; | |
2171 | cam_data |= addr[4] << 24 | (addr[5] << 16); | |
2172 | tc_writel(cam_data, &tr->CAM_Data); | |
2173 | } | |
2174 | ||
1da177e4 LT |
2175 | tc_writel(saved_addr, &tr->CAM_Adr); |
2176 | } | |
2177 | ||
2178 | ||
2179 | /* | |
2180 | * Set or clear the multicast filter for this adaptor. | |
2181 | * num_addrs == -1 Promiscuous mode, receive all packets | |
2182 | * num_addrs == 0 Normal mode, clear multicast list | |
2183 | * num_addrs > 0 Multicast mode, receive normal and MC packets, | |
2184 | * and do best-effort filtering. | |
2185 | */ | |
2186 | static void | |
2187 | tc35815_set_multicast_list(struct net_device *dev) | |
2188 | { | |
eea221ce AN |
2189 | struct tc35815_regs __iomem *tr = |
2190 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1da177e4 | 2191 | |
7f225b42 | 2192 | if (dev->flags & IFF_PROMISC) { |
eea221ce AN |
2193 | #ifdef WORKAROUND_100HALF_PROMISC |
2194 | /* With some (all?) 100MHalf HUB, controller will hang | |
2195 | * if we enabled promiscuous mode before linkup... */ | |
ee79b7fb | 2196 | struct tc35815_local *lp = netdev_priv(dev); |
c6686fe3 AN |
2197 | |
2198 | if (!lp->link) | |
eea221ce AN |
2199 | return; |
2200 | #endif | |
1da177e4 LT |
2201 | /* Enable promiscuous mode */ |
2202 | tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl); | |
7f225b42 AN |
2203 | } else if ((dev->flags & IFF_ALLMULTI) || |
2204 | dev->mc_count > CAM_ENTRY_MAX - 3) { | |
1da177e4 LT |
2205 | /* CAM 0, 1, 20 are reserved. */ |
2206 | /* Disable promiscuous mode, use normal mode. */ | |
2207 | tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl); | |
7f225b42 AN |
2208 | } else if (dev->mc_count) { |
2209 | struct dev_mc_list *cur_addr = dev->mc_list; | |
1da177e4 LT |
2210 | int i; |
2211 | int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE); | |
2212 | ||
2213 | tc_writel(0, &tr->CAM_Ctl); | |
2214 | /* Walk the address list, and load the filter */ | |
2215 | for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) { | |
2216 | if (!cur_addr) | |
2217 | break; | |
2218 | /* entry 0,1 is reserved. */ | |
eea221ce | 2219 | tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr); |
1da177e4 LT |
2220 | ena_bits |= CAM_Ena_Bit(i + 2); |
2221 | } | |
2222 | tc_writel(ena_bits, &tr->CAM_Ena); | |
2223 | tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl); | |
7f225b42 | 2224 | } else { |
1da177e4 LT |
2225 | tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena); |
2226 | tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl); | |
2227 | } | |
2228 | } | |
2229 | ||
eea221ce | 2230 | static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
1da177e4 | 2231 | { |
ee79b7fb | 2232 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2233 | strcpy(info->driver, MODNAME); |
2234 | strcpy(info->version, DRV_VERSION); | |
2235 | strcpy(info->bus_info, pci_name(lp->pci_dev)); | |
2236 | } | |
6aa20a22 | 2237 | |
eea221ce AN |
2238 | static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
2239 | { | |
ee79b7fb | 2240 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce | 2241 | |
c6686fe3 AN |
2242 | if (!lp->phy_dev) |
2243 | return -ENODEV; | |
2244 | return phy_ethtool_gset(lp->phy_dev, cmd); | |
eea221ce AN |
2245 | } |
2246 | ||
c6686fe3 | 2247 | static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
eea221ce | 2248 | { |
ee79b7fb | 2249 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce | 2250 | |
c6686fe3 AN |
2251 | if (!lp->phy_dev) |
2252 | return -ENODEV; | |
2253 | return phy_ethtool_sset(lp->phy_dev, cmd); | |
eea221ce AN |
2254 | } |
2255 | ||
2256 | static u32 tc35815_get_msglevel(struct net_device *dev) | |
2257 | { | |
ee79b7fb | 2258 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2259 | return lp->msg_enable; |
2260 | } | |
2261 | ||
2262 | static void tc35815_set_msglevel(struct net_device *dev, u32 datum) | |
2263 | { | |
ee79b7fb | 2264 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2265 | lp->msg_enable = datum; |
2266 | } | |
2267 | ||
b9f2c044 | 2268 | static int tc35815_get_sset_count(struct net_device *dev, int sset) |
eea221ce | 2269 | { |
ee79b7fb | 2270 | struct tc35815_local *lp = netdev_priv(dev); |
b9f2c044 JG |
2271 | |
2272 | switch (sset) { | |
2273 | case ETH_SS_STATS: | |
2274 | return sizeof(lp->lstats) / sizeof(int); | |
2275 | default: | |
2276 | return -EOPNOTSUPP; | |
2277 | } | |
eea221ce | 2278 | } |
1da177e4 | 2279 | |
eea221ce AN |
2280 | static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data) |
2281 | { | |
ee79b7fb | 2282 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2283 | data[0] = lp->lstats.max_tx_qlen; |
2284 | data[1] = lp->lstats.tx_ints; | |
2285 | data[2] = lp->lstats.rx_ints; | |
2286 | data[3] = lp->lstats.tx_underrun; | |
2287 | } | |
2288 | ||
2289 | static struct { | |
2290 | const char str[ETH_GSTRING_LEN]; | |
2291 | } ethtool_stats_keys[] = { | |
2292 | { "max_tx_qlen" }, | |
2293 | { "tx_ints" }, | |
2294 | { "rx_ints" }, | |
2295 | { "tx_underrun" }, | |
2296 | }; | |
2297 | ||
2298 | static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data) | |
2299 | { | |
2300 | memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys)); | |
2301 | } | |
2302 | ||
2303 | static const struct ethtool_ops tc35815_ethtool_ops = { | |
2304 | .get_drvinfo = tc35815_get_drvinfo, | |
2305 | .get_settings = tc35815_get_settings, | |
2306 | .set_settings = tc35815_set_settings, | |
c6686fe3 | 2307 | .get_link = ethtool_op_get_link, |
eea221ce AN |
2308 | .get_msglevel = tc35815_get_msglevel, |
2309 | .set_msglevel = tc35815_set_msglevel, | |
2310 | .get_strings = tc35815_get_strings, | |
b9f2c044 | 2311 | .get_sset_count = tc35815_get_sset_count, |
eea221ce | 2312 | .get_ethtool_stats = tc35815_get_ethtool_stats, |
eea221ce AN |
2313 | }; |
2314 | ||
2315 | static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) | |
2316 | { | |
ee79b7fb | 2317 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2318 | |
2319 | if (!netif_running(dev)) | |
2320 | return -EINVAL; | |
c6686fe3 AN |
2321 | if (!lp->phy_dev) |
2322 | return -ENODEV; | |
2323 | return phy_mii_ioctl(lp->phy_dev, if_mii(rq), cmd); | |
eea221ce AN |
2324 | } |
2325 | ||
2326 | static void tc35815_chip_reset(struct net_device *dev) | |
2327 | { | |
2328 | struct tc35815_regs __iomem *tr = | |
2329 | (struct tc35815_regs __iomem *)dev->base_addr; | |
2330 | int i; | |
1da177e4 LT |
2331 | /* reset the controller */ |
2332 | tc_writel(MAC_Reset, &tr->MAC_Ctl); | |
eea221ce AN |
2333 | udelay(4); /* 3200ns */ |
2334 | i = 0; | |
2335 | while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) { | |
2336 | if (i++ > 100) { | |
2337 | printk(KERN_ERR "%s: MAC reset failed.\n", dev->name); | |
2338 | break; | |
2339 | } | |
2340 | mdelay(1); | |
2341 | } | |
1da177e4 LT |
2342 | tc_writel(0, &tr->MAC_Ctl); |
2343 | ||
2344 | /* initialize registers to default value */ | |
2345 | tc_writel(0, &tr->DMA_Ctl); | |
2346 | tc_writel(0, &tr->TxThrsh); | |
2347 | tc_writel(0, &tr->TxPollCtr); | |
2348 | tc_writel(0, &tr->RxFragSize); | |
2349 | tc_writel(0, &tr->Int_En); | |
2350 | tc_writel(0, &tr->FDA_Bas); | |
2351 | tc_writel(0, &tr->FDA_Lim); | |
2352 | tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */ | |
2353 | tc_writel(0, &tr->CAM_Ctl); | |
2354 | tc_writel(0, &tr->Tx_Ctl); | |
2355 | tc_writel(0, &tr->Rx_Ctl); | |
2356 | tc_writel(0, &tr->CAM_Ena); | |
2357 | (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */ | |
2358 | ||
eea221ce AN |
2359 | /* initialize internal SRAM */ |
2360 | tc_writel(DMA_TestMode, &tr->DMA_Ctl); | |
2361 | for (i = 0; i < 0x1000; i += 4) { | |
2362 | tc_writel(i, &tr->CAM_Adr); | |
2363 | tc_writel(0, &tr->CAM_Data); | |
2364 | } | |
2365 | tc_writel(0, &tr->DMA_Ctl); | |
1da177e4 LT |
2366 | } |
2367 | ||
2368 | static void tc35815_chip_init(struct net_device *dev) | |
2369 | { | |
ee79b7fb | 2370 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2371 | struct tc35815_regs __iomem *tr = |
2372 | (struct tc35815_regs __iomem *)dev->base_addr; | |
1da177e4 LT |
2373 | unsigned long txctl = TX_CTL_CMD; |
2374 | ||
1da177e4 | 2375 | /* load station address to CAM */ |
eea221ce | 2376 | tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr); |
1da177e4 LT |
2377 | |
2378 | /* Enable CAM (broadcast and unicast) */ | |
2379 | tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena); | |
2380 | tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl); | |
2381 | ||
eea221ce AN |
2382 | /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */ |
2383 | if (HAVE_DMA_RXALIGN(lp)) | |
2384 | tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl); | |
2385 | else | |
2386 | tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl); | |
2387 | #ifdef TC35815_USE_PACKEDBUFFER | |
1da177e4 | 2388 | tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */ |
eea221ce AN |
2389 | #else |
2390 | tc_writel(ETH_ZLEN, &tr->RxFragSize); | |
2391 | #endif | |
1da177e4 LT |
2392 | tc_writel(0, &tr->TxPollCtr); /* Batch mode */ |
2393 | tc_writel(TX_THRESHOLD, &tr->TxThrsh); | |
2394 | tc_writel(INT_EN_CMD, &tr->Int_En); | |
2395 | ||
2396 | /* set queues */ | |
eea221ce | 2397 | tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas); |
1da177e4 LT |
2398 | tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base, |
2399 | &tr->FDA_Lim); | |
2400 | /* | |
2401 | * Activation method: | |
eea221ce | 2402 | * First, enable the MAC Transmitter and the DMA Receive circuits. |
1da177e4 LT |
2403 | * Then enable the DMA Transmitter and the MAC Receive circuits. |
2404 | */ | |
eea221ce | 2405 | tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */ |
1da177e4 | 2406 | tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */ |
eea221ce | 2407 | |
1da177e4 | 2408 | /* start MAC transmitter */ |
eea221ce AN |
2409 | #ifndef NO_CHECK_CARRIER |
2410 | /* TX4939 does not have EnLCarr */ | |
c6686fe3 | 2411 | if (lp->chiptype == TC35815_TX4939) |
eea221ce AN |
2412 | txctl &= ~Tx_EnLCarr; |
2413 | #ifdef WORKAROUND_LOSTCAR | |
1da177e4 | 2414 | /* WORKAROUND: ignore LostCrS in full duplex operation */ |
c6686fe3 | 2415 | if (!lp->phy_dev || !lp->link || lp->duplex == DUPLEX_FULL) |
eea221ce AN |
2416 | txctl &= ~Tx_EnLCarr; |
2417 | #endif | |
2418 | #endif /* !NO_CHECK_CARRIER */ | |
1da177e4 LT |
2419 | #ifdef GATHER_TXINT |
2420 | txctl &= ~Tx_EnComp; /* disable global tx completion int. */ | |
2421 | #endif | |
2422 | tc_writel(txctl, &tr->Tx_Ctl); | |
eea221ce AN |
2423 | } |
2424 | ||
2425 | #ifdef CONFIG_PM | |
2426 | static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state) | |
2427 | { | |
2428 | struct net_device *dev = pci_get_drvdata(pdev); | |
ee79b7fb | 2429 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2430 | unsigned long flags; |
2431 | ||
2432 | pci_save_state(pdev); | |
2433 | if (!netif_running(dev)) | |
2434 | return 0; | |
2435 | netif_device_detach(dev); | |
c6686fe3 AN |
2436 | if (lp->phy_dev) |
2437 | phy_stop(lp->phy_dev); | |
eea221ce | 2438 | spin_lock_irqsave(&lp->lock, flags); |
eea221ce | 2439 | tc35815_chip_reset(dev); |
1da177e4 | 2440 | spin_unlock_irqrestore(&lp->lock, flags); |
eea221ce AN |
2441 | pci_set_power_state(pdev, PCI_D3hot); |
2442 | return 0; | |
1da177e4 LT |
2443 | } |
2444 | ||
eea221ce AN |
2445 | static int tc35815_resume(struct pci_dev *pdev) |
2446 | { | |
2447 | struct net_device *dev = pci_get_drvdata(pdev); | |
ee79b7fb | 2448 | struct tc35815_local *lp = netdev_priv(dev); |
eea221ce AN |
2449 | |
2450 | pci_restore_state(pdev); | |
2451 | if (!netif_running(dev)) | |
2452 | return 0; | |
2453 | pci_set_power_state(pdev, PCI_D0); | |
eea221ce | 2454 | tc35815_restart(dev); |
c6686fe3 AN |
2455 | if (lp->phy_dev) |
2456 | phy_start(lp->phy_dev); | |
eea221ce AN |
2457 | netif_device_attach(dev); |
2458 | return 0; | |
2459 | } | |
2460 | #endif /* CONFIG_PM */ | |
2461 | ||
2462 | static struct pci_driver tc35815_pci_driver = { | |
2463 | .name = MODNAME, | |
2464 | .id_table = tc35815_pci_tbl, | |
2465 | .probe = tc35815_init_one, | |
2466 | .remove = __devexit_p(tc35815_remove_one), | |
2467 | #ifdef CONFIG_PM | |
2468 | .suspend = tc35815_suspend, | |
2469 | .resume = tc35815_resume, | |
2470 | #endif | |
1da177e4 LT |
2471 | }; |
2472 | ||
eea221ce AN |
2473 | module_param_named(speed, options.speed, int, 0); |
2474 | MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps"); | |
2475 | module_param_named(duplex, options.duplex, int, 0); | |
2476 | MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full"); | |
eea221ce | 2477 | |
1da177e4 LT |
2478 | static int __init tc35815_init_module(void) |
2479 | { | |
eea221ce | 2480 | return pci_register_driver(&tc35815_pci_driver); |
1da177e4 LT |
2481 | } |
2482 | ||
2483 | static void __exit tc35815_cleanup_module(void) | |
2484 | { | |
eea221ce | 2485 | pci_unregister_driver(&tc35815_pci_driver); |
1da177e4 | 2486 | } |
420e8524 | 2487 | |
1da177e4 LT |
2488 | module_init(tc35815_init_module); |
2489 | module_exit(tc35815_cleanup_module); | |
eea221ce AN |
2490 | |
2491 | MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver"); | |
2492 | MODULE_LICENSE("GPL"); |