blob: b269b187b55fb8c3de874cba446a86222f4c36d7 [file] [log] [blame]
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001/*
2 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Based on skelton.c by Donald Becker.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09006 * This driver is a replacement of older and less maintained version.
7 * This is a header of the older version:
8 * -----<snip>-----
9 * Copyright 2001 MontaVista Software Inc.
10 * Author: MontaVista Software, Inc.
11 * ahennessy@mvista.com
12 * Copyright (C) 2000-2001 Toshiba Corporation
13 * static const char *version =
14 * "tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n";
15 * -----<snip>-----
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090017 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090021 * (C) Copyright TOSHIBA CORPORATION 2004-2005
22 * All Rights Reserved.
23 *
24 * Revision History:
25 * 1.13 64-bit proof.
26 * 1.14 Do not round-up transmit length.
27 * 1.15 Define TC35815_DMA_SYNC_ONDEMAND, cleanup.
28 * 1.16 Fix free_page bug introduced in 1.15
29 * 1.17 Add mii/ethtool ioctl support.
30 * Remove workaround for early TX4938. Cleanup.
31 * 1.20 Kernel 2.6.
32 * 1.21 Fix receive packet length (omit CRC).
33 * Call netif_carrier_on/netif_carrier_off.
34 * Add kernel/module options (speed, duplex, doforce).
35 * Do not try "force link mode" by default.
36 * Reconfigure CAM on restarting.
37 * Reset PHY on restarting.
38 * Add workaround for 100MHalf HUB.
39 * 1.22 Minor fix.
40 * 1.23 Minor cleanup.
41 * 1.24 Remove tc35815_setup since new stype option
42 * ("tc35815.speed=10", etc.) can be used for 2.6 kernel.
43 * 1.25 TX4939 support.
44 * 1.26 Minor cleanup.
45 * 1.27 Move TX4939 PCFG.SPEEDn control code out from this driver.
46 * Cleanup init_dev_addr. (NETDEV_REGISTER event notifier
47 * can overwrite dev_addr)
48 * support ETHTOOL_GPERMADDR.
49 * 1.28 Minor cleanup.
50 * 1.29 support netpoll.
51 * 1.30 Minor cleanup.
52 * 1.31 NAPI support. (disabled by default)
53 * Use DMA_RxAlign_2 if possible.
54 * Do not use PackedBuffer.
55 * Cleanup.
56 * 1.32 Fix free buffer management on non-PackedBuffer mode.
57 * 1.33 Fix netpoll build.
58 * 1.34 Fix netpoll locking. "BH rule" for NAPI is not enough with
59 * netpoll, hard_start_xmit might be called from irq context.
60 * PM support.
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +090061 * 1.35 Fix an usage of streaming DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090064#ifdef TC35815_NAPI
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +090065#define DRV_VERSION "1.35-NAPI"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090066#else
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +090067#define DRV_VERSION "1.35"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090068#endif
69static const char *version = "tc35815.c:v" DRV_VERSION "\n";
70#define MODNAME "tc35815"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#include <linux/module.h>
73#include <linux/kernel.h>
74#include <linux/types.h>
75#include <linux/fcntl.h>
76#include <linux/interrupt.h>
77#include <linux/ioport.h>
78#include <linux/in.h>
79#include <linux/slab.h>
80#include <linux/string.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090081#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/errno.h>
83#include <linux/init.h>
84#include <linux/netdevice.h>
85#include <linux/etherdevice.h>
86#include <linux/skbuff.h>
87#include <linux/delay.h>
88#include <linux/pci.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090089#include <linux/mii.h>
90#include <linux/ethtool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <asm/byteorder.h>
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* First, a few definitions that the brave might change. */
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define GATHER_TXINT /* On-Demand Tx Interrupt */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090097#define WORKAROUND_LOSTCAR
98#define WORKAROUND_100HALF_PROMISC
99/* #define TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900101typedef enum {
102 TC35815CF = 0,
103 TC35815_NWU,
104 TC35815_TX4939,
105} board_t;
106
107/* indexed by board_t, above */
108static const struct {
109 const char *name;
110} board_info[] __devinitdata = {
111 { "TOSHIBA TC35815CF 10/100BaseTX" },
112 { "TOSHIBA TC35815 with Wake on LAN" },
113 { "TOSHIBA TC35815/TX4939" },
114};
115
116static const struct pci_device_id tc35815_pci_tbl[] = {
117 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
118 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
119 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
120 {0,}
121};
122MODULE_DEVICE_TABLE (pci, tc35815_pci_tbl);
123
124/* see MODULE_PARM_DESC */
125static struct tc35815_options {
126 int speed;
127 int duplex;
128 int doforce;
129} options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
132 * Registers
133 */
134struct tc35815_regs {
135 volatile __u32 DMA_Ctl; /* 0x00 */
136 volatile __u32 TxFrmPtr;
137 volatile __u32 TxThrsh;
138 volatile __u32 TxPollCtr;
139 volatile __u32 BLFrmPtr;
140 volatile __u32 RxFragSize;
141 volatile __u32 Int_En;
142 volatile __u32 FDA_Bas;
143 volatile __u32 FDA_Lim; /* 0x20 */
144 volatile __u32 Int_Src;
145 volatile __u32 unused0[2];
146 volatile __u32 PauseCnt;
147 volatile __u32 RemPauCnt;
148 volatile __u32 TxCtlFrmStat;
149 volatile __u32 unused1;
150 volatile __u32 MAC_Ctl; /* 0x40 */
151 volatile __u32 CAM_Ctl;
152 volatile __u32 Tx_Ctl;
153 volatile __u32 Tx_Stat;
154 volatile __u32 Rx_Ctl;
155 volatile __u32 Rx_Stat;
156 volatile __u32 MD_Data;
157 volatile __u32 MD_CA;
158 volatile __u32 CAM_Adr; /* 0x60 */
159 volatile __u32 CAM_Data;
160 volatile __u32 CAM_Ena;
161 volatile __u32 PROM_Ctl;
162 volatile __u32 PROM_Data;
163 volatile __u32 Algn_Cnt;
164 volatile __u32 CRC_Cnt;
165 volatile __u32 Miss_Cnt;
166};
167
168/*
169 * Bit assignments
170 */
171/* DMA_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900172#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
173#define DMA_RxAlign_1 0x00400000
174#define DMA_RxAlign_2 0x00800000
175#define DMA_RxAlign_3 0x00c00000
176#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
178#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
179#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
180#define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
181#define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
182#define DMA_TestMode 0x00002000 /* 1:Test Mode */
183#define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
184#define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
185
186/* RxFragSize bit asign ---------------------------------------------------- */
187#define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
188#define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
189
190/* MAC_Ctl bit asign ------------------------------------------------------- */
191#define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
192#define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
193#define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
194#define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
195#define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
196#define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
197#define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
198#define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
199#define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
200#define MAC_Reset 0x00000004 /* 1:Software Reset */
201#define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
202#define MAC_HaltReq 0x00000001 /* 1:Halt request */
203
204/* PROM_Ctl bit asign ------------------------------------------------------ */
205#define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
206#define PROM_Read 0x00004000 /*10:Read operation */
207#define PROM_Write 0x00002000 /*01:Write operation */
208#define PROM_Erase 0x00006000 /*11:Erase operation */
209 /*00:Enable or Disable Writting, */
210 /* as specified in PROM_Addr. */
211#define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
212 /*00xxxx: disable */
213
214/* CAM_Ctl bit asign ------------------------------------------------------- */
215#define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
216#define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
217 /* accept other */
218#define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
219#define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
220#define CAM_StationAcc 0x00000001 /* 1:unicast accept */
221
222/* CAM_Ena bit asign ------------------------------------------------------- */
223#define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
224#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
225#define CAM_Ena_Bit(index) (1<<(index))
226#define CAM_ENTRY_DESTINATION 0
227#define CAM_ENTRY_SOURCE 1
228#define CAM_ENTRY_MACCTL 20
229
230/* Tx_Ctl bit asign -------------------------------------------------------- */
231#define Tx_En 0x00000001 /* 1:Transmit enable */
232#define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
233#define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
234#define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
235#define Tx_FBack 0x00000010 /* 1:Fast Back-off */
236#define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
237#define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
238#define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
239#define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
240#define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
241#define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
242#define Tx_EnComp 0x00004000 /* 1:Enable Completion */
243
244/* Tx_Stat bit asign ------------------------------------------------------- */
245#define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
246#define Tx_ExColl 0x00000010 /* Excessive Collision */
247#define Tx_TXDefer 0x00000020 /* Transmit Defered */
248#define Tx_Paused 0x00000040 /* Transmit Paused */
249#define Tx_IntTx 0x00000080 /* Interrupt on Tx */
250#define Tx_Under 0x00000100 /* Underrun */
251#define Tx_Defer 0x00000200 /* Deferral */
252#define Tx_NCarr 0x00000400 /* No Carrier */
253#define Tx_10Stat 0x00000800 /* 10Mbps Status */
254#define Tx_LateColl 0x00001000 /* Late Collision */
255#define Tx_TxPar 0x00002000 /* Tx Parity Error */
256#define Tx_Comp 0x00004000 /* Completion */
257#define Tx_Halted 0x00008000 /* Tx Halted */
258#define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
259
260/* Rx_Ctl bit asign -------------------------------------------------------- */
261#define Rx_EnGood 0x00004000 /* 1:Enable Good */
262#define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
263#define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
264#define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
265#define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
266#define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
267#define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
268#define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
269#define Rx_ShortEn 0x00000008 /* 1:Short Enable */
270#define Rx_LongEn 0x00000004 /* 1:Long Enable */
271#define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
272#define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
273
274/* Rx_Stat bit asign ------------------------------------------------------- */
275#define Rx_Halted 0x00008000 /* Rx Halted */
276#define Rx_Good 0x00004000 /* Rx Good */
277#define Rx_RxPar 0x00002000 /* Rx Parity Error */
278 /* 0x00001000 not use */
279#define Rx_LongErr 0x00000800 /* Rx Long Error */
280#define Rx_Over 0x00000400 /* Rx Overflow */
281#define Rx_CRCErr 0x00000200 /* Rx CRC Error */
282#define Rx_Align 0x00000100 /* Rx Alignment Error */
283#define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
284#define Rx_IntRx 0x00000040 /* Rx Interrupt */
285#define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
286
287#define Rx_Stat_Mask 0x0000EFC0 /* Rx All Status Mask */
288
289/* Int_En bit asign -------------------------------------------------------- */
290#define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
291#define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Control Complete Enable */
292#define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
293#define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
294#define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
295#define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
296#define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
297#define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
298#define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
299#define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
300#define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
301#define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
302 /* Exhausted Enable */
303
304/* Int_Src bit asign ------------------------------------------------------- */
305#define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
306#define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
307#define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
308#define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
309#define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
310#define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
311#define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
312#define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
313#define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
314#define Int_SWInt 0x00000020 /* 1:Software request & Clear */
315#define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
316#define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
317#define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
318#define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
319#define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
320
321/* MD_CA bit asign --------------------------------------------------------- */
322#define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
323#define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
324#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
325
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * Descriptors
329 */
330
331/* Frame descripter */
332struct FDesc {
333 volatile __u32 FDNext;
334 volatile __u32 FDSystem;
335 volatile __u32 FDStat;
336 volatile __u32 FDCtl;
337};
338
339/* Buffer descripter */
340struct BDesc {
341 volatile __u32 BuffData;
342 volatile __u32 BDCtl;
343};
344
345#define FD_ALIGN 16
346
347/* Frame Descripter bit asign ---------------------------------------------- */
348#define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
349#define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
350#define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
351#define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
352#define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
353#define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
354#define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
355#define FD_FrmOpt_Packing 0x04000000 /* Rx only */
356#define FD_CownsFD 0x80000000 /* FD Controller owner bit */
357#define FD_Next_EOL 0x00000001 /* FD EOL indicator */
358#define FD_BDCnt_SHIFT 16
359
360/* Buffer Descripter bit asign --------------------------------------------- */
361#define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
362#define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
363#define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
364#define BD_CownsBD 0x80000000 /* BD Controller owner bit */
365#define BD_RxBDID_SHIFT 16
366#define BD_RxBDSeqN_SHIFT 24
367
368
369/* Some useful constants. */
370#undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */
371
372#ifdef NO_CHECK_CARRIER
373#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900374 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
375 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#else
377#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900378 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
379 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#endif
381#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
382 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#define INT_EN_CMD (Int_NRAbtEn | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900384 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
386 Int_STargAbtEn | \
387 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900388#define DMA_CTL_CMD DMA_BURST_SIZE
389#define HAVE_DMA_RXALIGN(lp) likely((lp)->boardtype != TC35815CF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391/* Tuning parameters */
392#define DMA_BURST_SIZE 32
393#define TX_THRESHOLD 1024
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900394#define TX_THRESHOLD_MAX 1536 /* used threshold with packet max byte for low pci transfer ability.*/
395#define TX_THRESHOLD_KEEP_LIMIT 10 /* setting threshold max value when overrun error occured this count. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900397/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
398#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#define FD_PAGE_NUM 2
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900400#define RX_BUF_NUM 8 /* >= 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#define RX_FD_NUM 250 /* >= 32 */
402#define TX_FD_NUM 128
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900403#define RX_BUF_SIZE PAGE_SIZE
404#else /* TC35815_USE_PACKEDBUFFER */
405#define FD_PAGE_NUM 4
406#define RX_BUF_NUM 128 /* < 256 */
407#define RX_FD_NUM 256 /* >= 32 */
408#define TX_FD_NUM 128
409#if RX_CTL_CMD & Rx_LongEn
410#define RX_BUF_SIZE PAGE_SIZE
411#elif RX_CTL_CMD & Rx_StripCRC
412#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */
413#else
414#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */
415#endif
416#endif /* TC35815_USE_PACKEDBUFFER */
417#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
418#define NAPI_WEIGHT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420struct TxFD {
421 struct FDesc fd;
422 struct BDesc bd;
423 struct BDesc unused;
424};
425
426struct RxFD {
427 struct FDesc fd;
428 struct BDesc bd[0]; /* variable length */
429};
430
431struct FrFD {
432 struct FDesc fd;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900433 struct BDesc bd[RX_BUF_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
436
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900437#define tc_readl(addr) readl(addr)
438#define tc_writel(d, addr) writel(d, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900440#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
441
442/* Timer state engine. */
443enum tc35815_timer_state {
444 arbwait = 0, /* Waiting for auto negotiation to complete. */
445 lupwait = 1, /* Auto-neg complete, awaiting link-up status. */
446 ltrywait = 2, /* Forcing try of all modes, from fastest to slowest. */
447 asleep = 3, /* Time inactive. */
448 lcheck = 4, /* Check link status. */
449};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451/* Information that need to be kept for each board. */
452struct tc35815_local {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900453 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* statistics */
456 struct net_device_stats stats;
457 struct {
458 int max_tx_qlen;
459 int tx_ints;
460 int rx_ints;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900461 int tx_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } lstats;
463
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900464 /* Tx control lock. This protects the transmit buffer ring
465 * state along with the "tx full" state of the driver. This
466 * means all netif_queue flow control actions are protected
467 * by this lock as well.
468 */
469 spinlock_t lock;
470
471 int phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int fullduplex;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900473 unsigned short saved_lpa;
474 struct timer_list timer;
475 enum tc35815_timer_state timer_state; /* State of auto-neg timer. */
476 unsigned int timer_ticks; /* Number of clicks at each state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * Transmitting: Batch Mode.
480 * 1 BD in 1 TxFD.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900481 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * 1 circular FD for Free Buffer List.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900483 * RX_BUF_NUM BD in Free Buffer FD.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * One Free Buffer BD has PAGE_SIZE data buffer.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900485 * Or Non-Packing Mode.
486 * 1 circular FD for Free Buffer List.
487 * RX_BUF_NUM BD in Free Buffer FD.
488 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900490 void * fd_buf; /* for TxFD, RxFD, FrFD */
491 dma_addr_t fd_buf_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct TxFD *tfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900493 unsigned int tfd_start;
494 unsigned int tfd_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct RxFD *rfd_base;
496 struct RxFD *rfd_limit;
497 struct RxFD *rfd_cur;
498 struct FrFD *fbl_ptr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900499#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 unsigned char fbl_curid;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900501 void * data_buf[RX_BUF_NUM]; /* packing */
502 dma_addr_t data_buf_dma[RX_BUF_NUM];
503 struct {
504 struct sk_buff *skb;
505 dma_addr_t skb_dma;
506 } tx_skbs[TX_FD_NUM];
507#else
508 unsigned int fbl_count;
509 struct {
510 struct sk_buff *skb;
511 dma_addr_t skb_dma;
512 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
513#endif
514 struct mii_if_info mii;
515 unsigned short mii_id[2];
516 u32 msg_enable;
517 board_t boardtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518};
519
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900520static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
521{
522 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
523}
524#ifdef DEBUG
525static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
526{
527 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
528}
529#endif
530#ifdef TC35815_USE_PACKEDBUFFER
531static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
532{
533 int i;
534 for (i = 0; i < RX_BUF_NUM; i++) {
535 if (bus >= lp->data_buf_dma[i] &&
536 bus < lp->data_buf_dma[i] + PAGE_SIZE)
537 return (void *)((u8 *)lp->data_buf[i] +
538 (bus - lp->data_buf_dma[i]));
539 }
540 return NULL;
541}
542
543#define TC35815_DMA_SYNC_ONDEMAND
544static void* alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
545{
546#ifdef TC35815_DMA_SYNC_ONDEMAND
547 void *buf;
548 /* pci_map + pci_dma_sync will be more effective than
549 * pci_alloc_consistent on some archs. */
550 if ((buf = (void *)__get_free_page(GFP_ATOMIC)) == NULL)
551 return NULL;
552 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
553 PCI_DMA_FROMDEVICE);
554 if (pci_dma_mapping_error(*dma_handle)) {
555 free_page((unsigned long)buf);
556 return NULL;
557 }
558 return buf;
559#else
560 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
561#endif
562}
563
564static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
565{
566#ifdef TC35815_DMA_SYNC_ONDEMAND
567 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
568 free_page((unsigned long)buf);
569#else
570 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
571#endif
572}
573#else /* TC35815_USE_PACKEDBUFFER */
574static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
575 struct pci_dev *hwdev,
576 dma_addr_t *dma_handle)
577{
578 struct sk_buff *skb;
579 skb = dev_alloc_skb(RX_BUF_SIZE);
580 if (!skb)
581 return NULL;
582 skb->dev = dev;
583 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
584 PCI_DMA_FROMDEVICE);
585 if (pci_dma_mapping_error(*dma_handle)) {
586 dev_kfree_skb_any(skb);
587 return NULL;
588 }
589 skb_reserve(skb, 2); /* make IP header 4byte aligned */
590 return skb;
591}
592
593static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
594{
595 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
596 PCI_DMA_FROMDEVICE);
597 dev_kfree_skb_any(skb);
598}
599#endif /* TC35815_USE_PACKEDBUFFER */
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/* Index to functions, as function prototypes. */
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int tc35815_open(struct net_device *dev);
604static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900605static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
606#ifdef TC35815_NAPI
607static int tc35815_rx(struct net_device *dev, int limit);
608static int tc35815_poll(struct net_device *dev, int *budget);
609#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static void tc35815_rx(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900611#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static void tc35815_txdone(struct net_device *dev);
613static int tc35815_close(struct net_device *dev);
614static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
615static void tc35815_set_multicast_list(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900616static void tc35815_tx_timeout(struct net_device *dev);
617static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
618#ifdef CONFIG_NET_POLL_CONTROLLER
619static void tc35815_poll_controller(struct net_device *dev);
620#endif
621static const struct ethtool_ops tc35815_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900623/* Example routines you must write ;->. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static void tc35815_chip_reset(struct net_device *dev);
625static void tc35815_chip_init(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900626static void tc35815_find_phy(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void tc35815_phy_chip_init(struct net_device *dev);
628
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900629#ifdef DEBUG
630static void panic_queues(struct net_device *dev);
631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900633static void tc35815_timer(unsigned long data);
634static void tc35815_start_auto_negotiation(struct net_device *dev,
635 struct ethtool_cmd *ep);
636static int tc_mdio_read(struct net_device *dev, int phy_id, int location);
637static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
638 int val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900640static void __devinit tc35815_init_dev_addr (struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900642 struct tc35815_regs __iomem *tr =
643 (struct tc35815_regs __iomem *)dev->base_addr;
644 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900646 /* dev_addr will be overwritten on NETDEV_REGISTER event */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
648 ;
649 for (i = 0; i < 6; i += 2) {
650 unsigned short data;
651 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
652 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
653 ;
654 data = tc_readl(&tr->PROM_Data);
655 dev->dev_addr[i] = data & 0xff;
656 dev->dev_addr[i+1] = data >> 8;
657 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900658}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900660static int __devinit tc35815_init_one (struct pci_dev *pdev,
661 const struct pci_device_id *ent)
662{
663 void __iomem *ioaddr = NULL;
664 struct net_device *dev;
665 struct tc35815_local *lp;
666 int rc;
667 unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900669 static int printed_version;
670 if (!printed_version++) {
671 printk(version);
672 dev_printk(KERN_DEBUG, &pdev->dev,
673 "speed:%d duplex:%d doforce:%d\n",
674 options.speed, options.duplex, options.doforce);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900677 if (!pdev->irq) {
678 dev_warn(&pdev->dev, "no IRQ assigned.\n");
679 return -ENODEV;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900682 /* dev zeroed in alloc_etherdev */
683 dev = alloc_etherdev (sizeof (*lp));
684 if (dev == NULL) {
685 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
686 return -ENOMEM;
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 SET_MODULE_OWNER(dev);
689 SET_NETDEV_DEV(dev, &pdev->dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900690 lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900692 /* enable device (incl. PCI PM wakeup), and bus-mastering */
693 rc = pci_enable_device (pdev);
694 if (rc)
695 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900697 mmio_start = pci_resource_start (pdev, 1);
698 mmio_end = pci_resource_end (pdev, 1);
699 mmio_flags = pci_resource_flags (pdev, 1);
700 mmio_len = pci_resource_len (pdev, 1);
701
702 /* set this immediately, we need to know before
703 * we talk to the chip directly */
704
705 /* make sure PCI base addr 1 is MMIO */
706 if (!(mmio_flags & IORESOURCE_MEM)) {
707 dev_err(&pdev->dev, "region #1 not an MMIO resource, aborting\n");
708 rc = -ENODEV;
709 goto err_out;
710 }
711
712 /* check for weird/broken PCI region reporting */
713 if ((mmio_len < sizeof(struct tc35815_regs))) {
714 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
715 rc = -ENODEV;
716 goto err_out;
717 }
718
719 rc = pci_request_regions (pdev, MODNAME);
720 if (rc)
721 goto err_out;
722
723 pci_set_master (pdev);
724
725 /* ioremap MMIO region */
726 ioaddr = ioremap (mmio_start, mmio_len);
727 if (ioaddr == NULL) {
728 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
729 rc = -EIO;
730 goto err_out_free_res;
731 }
732
733 /* Initialize the device structure. */
734 dev->open = tc35815_open;
735 dev->hard_start_xmit = tc35815_send_packet;
736 dev->stop = tc35815_close;
737 dev->get_stats = tc35815_get_stats;
738 dev->set_multicast_list = tc35815_set_multicast_list;
739 dev->do_ioctl = tc35815_ioctl;
740 dev->ethtool_ops = &tc35815_ethtool_ops;
741 dev->tx_timeout = tc35815_tx_timeout;
742 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
743#ifdef TC35815_NAPI
744 dev->poll = tc35815_poll;
745 dev->weight = NAPI_WEIGHT;
746#endif
747#ifdef CONFIG_NET_POLL_CONTROLLER
748 dev->poll_controller = tc35815_poll_controller;
749#endif
750
751 dev->irq = pdev->irq;
752 dev->base_addr = (unsigned long) ioaddr;
753
754 /* dev->priv/lp zeroed and aligned in alloc_etherdev */
755 lp = dev->priv;
756 spin_lock_init(&lp->lock);
757 lp->pci_dev = pdev;
758 lp->boardtype = ent->driver_data;
759
760 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
761 pci_set_drvdata(pdev, dev);
762
763 /* Soft reset the chip. */
764 tc35815_chip_reset(dev);
765
766 /* Retrieve the ethernet address. */
767 tc35815_init_dev_addr(dev);
768
769 rc = register_netdev (dev);
770 if (rc)
771 goto err_out_unmap;
772
773 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
774 printk(KERN_INFO "%s: %s at 0x%lx, "
775 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
776 "IRQ %d\n",
777 dev->name,
778 board_info[ent->driver_data].name,
779 dev->base_addr,
780 dev->dev_addr[0], dev->dev_addr[1],
781 dev->dev_addr[2], dev->dev_addr[3],
782 dev->dev_addr[4], dev->dev_addr[5],
783 dev->irq);
784
785 setup_timer(&lp->timer, tc35815_timer, (unsigned long) dev);
786 lp->mii.dev = dev;
787 lp->mii.mdio_read = tc_mdio_read;
788 lp->mii.mdio_write = tc_mdio_write;
789 lp->mii.phy_id_mask = 0x1f;
790 lp->mii.reg_num_mask = 0x1f;
791 tc35815_find_phy(dev);
792 lp->mii.phy_id = lp->phy_addr;
793 lp->mii.full_duplex = 0;
794 lp->mii.force_media = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 return 0;
797
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900798err_out_unmap:
799 iounmap(ioaddr);
800err_out_free_res:
801 pci_release_regions (pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802err_out:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900803 free_netdev (dev);
804 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900808static void __devexit tc35815_remove_one (struct pci_dev *pdev)
809{
810 struct net_device *dev = pci_get_drvdata (pdev);
811 unsigned long mmio_addr;
812
813 mmio_addr = dev->base_addr;
814
815 unregister_netdev (dev);
816
817 if (mmio_addr) {
818 iounmap ((void __iomem *)mmio_addr);
819 pci_release_regions (pdev);
820 }
821
822 free_netdev (dev);
823
824 pci_set_drvdata (pdev, NULL);
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int
828tc35815_init_queues(struct net_device *dev)
829{
830 struct tc35815_local *lp = dev->priv;
831 int i;
832 unsigned long fd_addr;
833
834 if (!lp->fd_buf) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900835 BUG_ON(sizeof(struct FDesc) +
836 sizeof(struct BDesc) * RX_BUF_NUM +
837 sizeof(struct FDesc) * RX_FD_NUM +
838 sizeof(struct TxFD) * TX_FD_NUM >
839 PAGE_SIZE * FD_PAGE_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900841 if ((lp->fd_buf = pci_alloc_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM, &lp->fd_buf_dma)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENOMEM;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900843 for (i = 0; i < RX_BUF_NUM; i++) {
844#ifdef TC35815_USE_PACKEDBUFFER
845 if ((lp->data_buf[i] = alloc_rxbuf_page(lp->pci_dev, &lp->data_buf_dma[i])) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 while (--i >= 0) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900847 free_rxbuf_page(lp->pci_dev,
848 lp->data_buf[i],
849 lp->data_buf_dma[i]);
850 lp->data_buf[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900852 pci_free_consistent(lp->pci_dev,
853 PAGE_SIZE * FD_PAGE_NUM,
854 lp->fd_buf,
855 lp->fd_buf_dma);
856 lp->fd_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -ENOMEM;
858 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900859#else
860 lp->rx_skbs[i].skb =
861 alloc_rxbuf_skb(dev, lp->pci_dev,
862 &lp->rx_skbs[i].skb_dma);
863 if (!lp->rx_skbs[i].skb) {
864 while (--i >= 0) {
865 free_rxbuf_skb(lp->pci_dev,
866 lp->rx_skbs[i].skb,
867 lp->rx_skbs[i].skb_dma);
868 lp->rx_skbs[i].skb = NULL;
869 }
870 pci_free_consistent(lp->pci_dev,
871 PAGE_SIZE * FD_PAGE_NUM,
872 lp->fd_buf,
873 lp->fd_buf_dma);
874 lp->fd_buf = NULL;
875 return -ENOMEM;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#endif
878 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900879 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
880 dev->name, lp->fd_buf);
881#ifdef TC35815_USE_PACKEDBUFFER
882 printk(" DataBuf");
883 for (i = 0; i < RX_BUF_NUM; i++)
884 printk(" %p", lp->data_buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900886 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900888 for (i = 0; i < FD_PAGE_NUM; i++) {
889 clear_page((void *)((unsigned long)lp->fd_buf + i * PAGE_SIZE));
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 fd_addr = (unsigned long)lp->fd_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Free Descriptors (for Receive) */
895 lp->rfd_base = (struct RxFD *)fd_addr;
896 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
897 for (i = 0; i < RX_FD_NUM; i++) {
898 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
899 }
900 lp->rfd_cur = lp->rfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900901 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* Transmit Descriptors */
904 lp->tfd_base = (struct TxFD *)fd_addr;
905 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
906 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900907 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
908 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
910 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900911 lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 lp->tfd_start = 0;
913 lp->tfd_end = 0;
914
915 /* Buffer List (for Receive) */
916 lp->fbl_ptr = (struct FrFD *)fd_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900917 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
918 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
919#ifndef TC35815_USE_PACKEDBUFFER
920 /*
921 * move all allocated skbs to head of rx_skbs[] array.
922 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
923 * tc35815_rx() had failed.
924 */
925 lp->fbl_count = 0;
926 for (i = 0; i < RX_BUF_NUM; i++) {
927 if (lp->rx_skbs[i].skb) {
928 if (i != lp->fbl_count) {
929 lp->rx_skbs[lp->fbl_count].skb =
930 lp->rx_skbs[i].skb;
931 lp->rx_skbs[lp->fbl_count].skb_dma =
932 lp->rx_skbs[i].skb_dma;
933 }
934 lp->fbl_count++;
935 }
936 }
937#endif
938 for (i = 0; i < RX_BUF_NUM; i++) {
939#ifdef TC35815_USE_PACKEDBUFFER
940 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
941#else
942 if (i >= lp->fbl_count) {
943 lp->fbl_ptr->bd[i].BuffData = 0;
944 lp->fbl_ptr->bd[i].BDCtl = 0;
945 continue;
946 }
947 lp->fbl_ptr->bd[i].BuffData =
948 cpu_to_le32(lp->rx_skbs[i].skb_dma);
949#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* BDID is index of FrFD.bd[] */
951 lp->fbl_ptr->bd[i].BDCtl =
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900952 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
953 RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900955#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 lp->fbl_curid = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900959 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
960 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962}
963
964static void
965tc35815_clear_queues(struct net_device *dev)
966{
967 struct tc35815_local *lp = dev->priv;
968 int i;
969
970 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900971 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
972 struct sk_buff *skb =
973 fdsystem != 0xffffffff ?
974 lp->tx_skbs[fdsystem].skb : NULL;
975#ifdef DEBUG
976 if (lp->tx_skbs[i].skb != skb) {
977 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
978 panic_queues(dev);
979 }
980#else
981 BUG_ON(lp->tx_skbs[i].skb != skb);
982#endif
983 if (skb) {
984 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
985 lp->tx_skbs[i].skb = NULL;
986 lp->tx_skbs[i].skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900988 }
989 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 tc35815_init_queues(dev);
993}
994
995static void
996tc35815_free_queues(struct net_device *dev)
997{
998 struct tc35815_local *lp = dev->priv;
999 int i;
1000
1001 if (lp->tfd_base) {
1002 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001003 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1004 struct sk_buff *skb =
1005 fdsystem != 0xffffffff ?
1006 lp->tx_skbs[fdsystem].skb : NULL;
1007#ifdef DEBUG
1008 if (lp->tx_skbs[i].skb != skb) {
1009 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1010 panic_queues(dev);
1011 }
1012#else
1013 BUG_ON(lp->tx_skbs[i].skb != skb);
1014#endif
1015 if (skb) {
1016 dev_kfree_skb(skb);
1017 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1018 lp->tx_skbs[i].skb = NULL;
1019 lp->tx_skbs[i].skb_dma = 0;
1020 }
1021 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 }
1024
1025 lp->rfd_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 lp->rfd_limit = NULL;
1027 lp->rfd_cur = NULL;
1028 lp->fbl_ptr = NULL;
1029
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001030 for (i = 0; i < RX_BUF_NUM; i++) {
1031#ifdef TC35815_USE_PACKEDBUFFER
1032 if (lp->data_buf[i]) {
1033 free_rxbuf_page(lp->pci_dev,
1034 lp->data_buf[i], lp->data_buf_dma[i]);
1035 lp->data_buf[i] = NULL;
1036 }
1037#else
1038 if (lp->rx_skbs[i].skb) {
1039 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1040 lp->rx_skbs[i].skb_dma);
1041 lp->rx_skbs[i].skb = NULL;
1042 }
1043#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001045 if (lp->fd_buf) {
1046 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1047 lp->fd_buf, lp->fd_buf_dma);
1048 lp->fd_buf = NULL;
1049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static void
1053dump_txfd(struct TxFD *fd)
1054{
1055 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1056 le32_to_cpu(fd->fd.FDNext),
1057 le32_to_cpu(fd->fd.FDSystem),
1058 le32_to_cpu(fd->fd.FDStat),
1059 le32_to_cpu(fd->fd.FDCtl));
1060 printk("BD: ");
1061 printk(" %08x %08x",
1062 le32_to_cpu(fd->bd.BuffData),
1063 le32_to_cpu(fd->bd.BDCtl));
1064 printk("\n");
1065}
1066
1067static int
1068dump_rxfd(struct RxFD *fd)
1069{
1070 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1071 if (bd_count > 8)
1072 bd_count = 8;
1073 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1074 le32_to_cpu(fd->fd.FDNext),
1075 le32_to_cpu(fd->fd.FDSystem),
1076 le32_to_cpu(fd->fd.FDStat),
1077 le32_to_cpu(fd->fd.FDCtl));
1078 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
1079 return 0;
1080 printk("BD: ");
1081 for (i = 0; i < bd_count; i++)
1082 printk(" %08x %08x",
1083 le32_to_cpu(fd->bd[i].BuffData),
1084 le32_to_cpu(fd->bd[i].BDCtl));
1085 printk("\n");
1086 return bd_count;
1087}
1088
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001089#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static void
1091dump_frfd(struct FrFD *fd)
1092{
1093 int i;
1094 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1095 le32_to_cpu(fd->fd.FDNext),
1096 le32_to_cpu(fd->fd.FDSystem),
1097 le32_to_cpu(fd->fd.FDStat),
1098 le32_to_cpu(fd->fd.FDCtl));
1099 printk("BD: ");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001100 for (i = 0; i < RX_BUF_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 printk(" %08x %08x",
1102 le32_to_cpu(fd->bd[i].BuffData),
1103 le32_to_cpu(fd->bd[i].BDCtl));
1104 printk("\n");
1105}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001108#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109static void
1110panic_queues(struct net_device *dev)
1111{
1112 struct tc35815_local *lp = dev->priv;
1113 int i;
1114
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001115 printk("TxFD base %p, start %u, end %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1117 printk("RxFD base %p limit %p cur %p\n",
1118 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1119 printk("FrFD %p\n", lp->fbl_ptr);
1120 for (i = 0; i < TX_FD_NUM; i++)
1121 dump_txfd(&lp->tfd_base[i]);
1122 for (i = 0; i < RX_FD_NUM; i++) {
1123 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1124 i += (bd_count + 1) / 2; /* skip BDs */
1125 }
1126 dump_frfd(lp->fbl_ptr);
1127 panic("%s: Illegal queue state.", dev->name);
1128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#endif
1130
1131static void print_eth(char *add)
1132{
1133 int i;
1134
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001135 printk("print_eth(%p)\n", add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 for (i = 0; i < 6; i++)
1137 printk(" %2.2X", (unsigned char) add[i + 6]);
1138 printk(" =>");
1139 for (i = 0; i < 6; i++)
1140 printk(" %2.2X", (unsigned char) add[i]);
1141 printk(" : %2.2X%2.2X\n", (unsigned char) add[12], (unsigned char) add[13]);
1142}
1143
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001144static int tc35815_tx_full(struct net_device *dev)
1145{
1146 struct tc35815_local *lp = dev->priv;
1147 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1148}
1149
1150static void tc35815_restart(struct net_device *dev)
1151{
1152 struct tc35815_local *lp = dev->priv;
1153 int pid = lp->phy_addr;
1154 int do_phy_reset = 1;
1155 del_timer(&lp->timer); /* Kill if running */
1156
1157 if (lp->mii_id[0] == 0x0016 && (lp->mii_id[1] & 0xfc00) == 0xf800) {
1158 /* Resetting PHY cause problem on some chip... (SEEQ 80221) */
1159 do_phy_reset = 0;
1160 }
1161 if (do_phy_reset) {
1162 int timeout;
1163 tc_mdio_write(dev, pid, MII_BMCR, BMCR_RESET);
1164 timeout = 100;
1165 while (--timeout) {
1166 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_RESET))
1167 break;
1168 udelay(1);
1169 }
1170 if (!timeout)
1171 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1172 }
1173
1174 tc35815_chip_reset(dev);
1175 tc35815_clear_queues(dev);
1176 tc35815_chip_init(dev);
1177 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1178 tc35815_set_multicast_list(dev);
1179}
1180
1181static void tc35815_tx_timeout(struct net_device *dev)
1182{
1183 struct tc35815_local *lp = dev->priv;
1184 struct tc35815_regs __iomem *tr =
1185 (struct tc35815_regs __iomem *)dev->base_addr;
1186
1187 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1188 dev->name, tc_readl(&tr->Tx_Stat));
1189
1190 /* Try to restart the adaptor. */
1191 spin_lock_irq(&lp->lock);
1192 tc35815_restart(dev);
1193 spin_unlock_irq(&lp->lock);
1194
1195 lp->stats.tx_errors++;
1196
1197 /* If we have space available to accept new transmit
1198 * requests, wake up the queueing layer. This would
1199 * be the case if the chipset_init() call above just
1200 * flushes out the tx queue and empties it.
1201 *
1202 * If instead, the tx queue is retained then the
1203 * netif_wake_queue() call should be placed in the
1204 * TX completion interrupt handler of the driver instead
1205 * of here.
1206 */
1207 if (!tc35815_tx_full(dev))
1208 netif_wake_queue(dev);
1209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/*
1212 * Open/initialize the board. This is called (in the current kernel)
1213 * sometime after booting when the 'ifconfig' program is run.
1214 *
1215 * This routine should set everything up anew at each open, even
1216 * registers that "should" only need to be set once at boot, so that
1217 * there is non-reboot way to recover if something goes wrong.
1218 */
1219static int
1220tc35815_open(struct net_device *dev)
1221{
1222 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
1225 * This is used if the interrupt line can turned off (shared).
1226 * See 3c503.c for an example of selecting the IRQ at config-time.
1227 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001228 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, dev->name, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -EAGAIN;
1230 }
1231
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001232 del_timer(&lp->timer); /* Kill if running */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 tc35815_chip_reset(dev);
1234
1235 if (tc35815_init_queues(dev) != 0) {
1236 free_irq(dev->irq, dev);
1237 return -EAGAIN;
1238 }
1239
1240 /* Reset the hardware here. Don't forget to set the station address. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001241 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 tc35815_chip_init(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001243 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001245 /* We are now ready to accept transmit requeusts from
1246 * the queueing layer of the networking.
1247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 netif_start_queue(dev);
1249
1250 return 0;
1251}
1252
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001253/* This will only be invoked if your driver is _not_ in XOFF state.
1254 * What this means is that you need not check it, and that this
1255 * invariant will hold if you make sure that the netif_*_queue()
1256 * calls are done at the proper times.
1257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1259{
1260 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001261 struct TxFD *txfd;
1262 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001264 /* If some error occurs while trying to transmit this
1265 * packet, you should return '1' from this function.
1266 * In such a case you _may not_ do anything to the
1267 * SKB, it is still owned by the network queueing
1268 * layer when an error is returned. This means you
1269 * may not modify any SKB fields, you may not free
1270 * the SKB, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001273 /* This is the most common case for modern hardware.
1274 * The spinlock protects this code from the TX complete
1275 * hardware interrupt handler. Queue flow control is
1276 * thus managed under this lock as well.
1277 */
1278 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001280 /* failsafe... (handle txdone now if half of FDs are used) */
1281 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1282 TX_FD_NUM / 2)
1283 tc35815_txdone(dev);
1284
1285 if (netif_msg_pktdata(lp))
1286 print_eth(skb->data);
1287#ifdef DEBUG
1288 if (lp->tx_skbs[lp->tfd_start].skb) {
1289 printk("%s: tx_skbs conflict.\n", dev->name);
1290 panic_queues(dev);
1291 }
1292#else
1293 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001295 lp->tx_skbs[lp->tfd_start].skb = skb;
1296 lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001298 /*add to ring */
1299 txfd = &lp->tfd_base[lp->tfd_start];
1300 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1301 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1302 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1303 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001305 if (lp->tfd_start == lp->tfd_end) {
1306 struct tc35815_regs __iomem *tr =
1307 (struct tc35815_regs __iomem *)dev->base_addr;
1308 /* Start DMA Transmitter. */
1309 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310#ifdef GATHER_TXINT
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001311 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001313 if (netif_msg_tx_queued(lp)) {
1314 printk("%s: starting TxFD.\n", dev->name);
1315 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001317 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1318 } else {
1319 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1320 if (netif_msg_tx_queued(lp)) {
1321 printk("%s: queueing TxFD.\n", dev->name);
1322 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001324 }
1325 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1326
1327 dev->trans_start = jiffies;
1328
1329 /* If we just used up the very last entry in the
1330 * TX ring on this device, tell the queueing
1331 * layer to send no more.
1332 */
1333 if (tc35815_tx_full(dev)) {
1334 if (netif_msg_tx_queued(lp))
1335 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1336 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001339 /* When the TX completion hw interrupt arrives, this
1340 * is when the transmit statistics are updated.
1341 */
1342
1343 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345}
1346
1347#define FATAL_ERROR_INT \
1348 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001349static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 static int count;
1352 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1353 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (status & Int_IntPCI)
1355 printk(" IntPCI");
1356 if (status & Int_DmParErr)
1357 printk(" DmParErr");
1358 if (status & Int_IntNRAbt)
1359 printk(" IntNRAbt");
1360 printk("\n");
1361 if (count++ > 100)
1362 panic("%s: Too many fatal errors.", dev->name);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001363 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* Try to restart the adaptor. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001365 tc35815_restart(dev);
1366}
1367
1368#ifdef TC35815_NAPI
1369static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
1370#else
1371static int tc35815_do_interrupt(struct net_device *dev, u32 status)
1372#endif
1373{
1374 struct tc35815_local *lp = dev->priv;
1375 struct tc35815_regs __iomem *tr =
1376 (struct tc35815_regs __iomem *)dev->base_addr;
1377 int ret = -1;
1378
1379 /* Fatal errors... */
1380 if (status & FATAL_ERROR_INT) {
1381 tc35815_fatal_error_interrupt(dev, status);
1382 return 0;
1383 }
1384 /* recoverable errors */
1385 if (status & Int_IntFDAEx) {
1386 /* disable FDAEx int. (until we make rooms...) */
1387 tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
1388 printk(KERN_WARNING
1389 "%s: Free Descriptor Area Exhausted (%#x).\n",
1390 dev->name, status);
1391 lp->stats.rx_dropped++;
1392 ret = 0;
1393 }
1394 if (status & Int_IntBLEx) {
1395 /* disable BLEx int. (until we make rooms...) */
1396 tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
1397 printk(KERN_WARNING
1398 "%s: Buffer List Exhausted (%#x).\n",
1399 dev->name, status);
1400 lp->stats.rx_dropped++;
1401 ret = 0;
1402 }
1403 if (status & Int_IntExBD) {
1404 printk(KERN_WARNING
1405 "%s: Excessive Buffer Descriptiors (%#x).\n",
1406 dev->name, status);
1407 lp->stats.rx_length_errors++;
1408 ret = 0;
1409 }
1410
1411 /* normal notification */
1412 if (status & Int_IntMacRx) {
1413 /* Got a packet(s). */
1414#ifdef TC35815_NAPI
1415 ret = tc35815_rx(dev, limit);
1416#else
1417 tc35815_rx(dev);
1418 ret = 0;
1419#endif
1420 lp->lstats.rx_ints++;
1421 }
1422 if (status & Int_IntMacTx) {
1423 /* Transmit complete. */
1424 lp->lstats.tx_ints++;
1425 tc35815_txdone(dev);
1426 netif_wake_queue(dev);
1427 ret = 0;
1428 }
1429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432/*
1433 * The typical workload of the driver:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001434 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
David Howells7d12e782006-10-05 14:55:46 +01001436static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct net_device *dev = dev_id;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001439 struct tc35815_regs __iomem *tr =
1440 (struct tc35815_regs __iomem *)dev->base_addr;
1441#ifdef TC35815_NAPI
1442 u32 dmactl = tc_readl(&tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001444 if (!(dmactl & DMA_IntMask)) {
1445 /* disable interrupts */
1446 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
1447 if (netif_rx_schedule_prep(dev))
1448 __netif_rx_schedule(dev);
1449 else {
1450 printk(KERN_ERR "%s: interrupt taken in poll\n",
1451 dev->name);
1452 BUG();
1453 }
1454 (void)tc_readl(&tr->Int_Src); /* flush */
1455 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001457 return IRQ_NONE;
1458#else
1459 struct tc35815_local *lp = dev->priv;
1460 int handled;
1461 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001463 spin_lock(&lp->lock);
1464 status = tc_readl(&tr->Int_Src);
1465 tc_writel(status, &tr->Int_Src); /* write to clear */
1466 handled = tc35815_do_interrupt(dev, status);
1467 (void)tc_readl(&tr->Int_Src); /* flush */
1468 spin_unlock(&lp->lock);
1469 return IRQ_RETVAL(handled >= 0);
1470#endif /* TC35815_NAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001473#ifdef CONFIG_NET_POLL_CONTROLLER
1474static void tc35815_poll_controller(struct net_device *dev)
1475{
1476 disable_irq(dev->irq);
1477 tc35815_interrupt(dev->irq, dev);
1478 enable_irq(dev->irq);
1479}
1480#endif
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482/* We have a good packet(s), get it/them out of the buffers. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001483#ifdef TC35815_NAPI
1484static int
1485tc35815_rx(struct net_device *dev, int limit)
1486#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static void
1488tc35815_rx(struct net_device *dev)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct tc35815_local *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 unsigned int fdctl;
1493 int i;
1494 int buf_free_count = 0;
1495 int fd_free_count = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001496#ifdef TC35815_NAPI
1497 int received = 0;
1498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1501 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1502 int pkt_len = fdctl & FD_FDLength_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001504#ifdef DEBUG
1505 struct RxFD *next_rfd;
1506#endif
1507#if (RX_CTL_CMD & Rx_StripCRC) == 0
1508 pkt_len -= 4;
1509#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001511 if (netif_msg_rx_status(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 dump_rxfd(lp->rfd_cur);
1513 if (status & Rx_Good) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 struct sk_buff *skb;
1515 unsigned char *data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001516 int cur_bd;
1517#ifdef TC35815_USE_PACKEDBUFFER
1518 int offset;
1519#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001521#ifdef TC35815_NAPI
1522 if (--limit < 0)
1523 break;
1524#endif
1525#ifdef TC35815_USE_PACKEDBUFFER
1526 BUG_ON(bd_count > 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */
1528 if (skb == NULL) {
1529 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
1530 dev->name);
1531 lp->stats.rx_dropped++;
1532 break;
1533 }
1534 skb_reserve(skb, 2); /* 16 bit alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 data = skb_put(skb, pkt_len);
1537
1538 /* copy from receive buffer */
1539 cur_bd = 0;
1540 offset = 0;
1541 while (offset < pkt_len && cur_bd < bd_count) {
1542 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1543 BD_BuffLength_MASK;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001544 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1545 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1546 if (offset + len > pkt_len)
1547 len = pkt_len - offset;
1548#ifdef TC35815_DMA_SYNC_ONDEMAND
1549 pci_dma_sync_single_for_cpu(lp->pci_dev,
1550 dma, len,
1551 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552#endif
1553 memcpy(data + offset, rxbuf, len);
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +09001554#ifdef TC35815_DMA_SYNC_ONDEMAND
1555 pci_dma_sync_single_for_device(lp->pci_dev,
1556 dma, len,
1557 PCI_DMA_FROMDEVICE);
1558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 offset += len;
1560 cur_bd++;
1561 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001562#else /* TC35815_USE_PACKEDBUFFER */
1563 BUG_ON(bd_count > 1);
1564 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1565 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1566#ifdef DEBUG
1567 if (cur_bd >= RX_BUF_NUM) {
1568 printk("%s: invalid BDID.\n", dev->name);
1569 panic_queues(dev);
1570 }
1571 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1572 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1573 if (!lp->rx_skbs[cur_bd].skb) {
1574 printk("%s: NULL skb.\n", dev->name);
1575 panic_queues(dev);
1576 }
1577#else
1578 BUG_ON(cur_bd >= RX_BUF_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001580 skb = lp->rx_skbs[cur_bd].skb;
1581 prefetch(skb->data);
1582 lp->rx_skbs[cur_bd].skb = NULL;
1583 lp->fbl_count--;
1584 pci_unmap_single(lp->pci_dev,
1585 lp->rx_skbs[cur_bd].skb_dma,
1586 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1587 if (!HAVE_DMA_RXALIGN(lp))
1588 memmove(skb->data, skb->data - 2, pkt_len);
1589 data = skb_put(skb, pkt_len);
1590#endif /* TC35815_USE_PACKEDBUFFER */
1591 if (netif_msg_pktdata(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 print_eth(data);
1593 skb->protocol = eth_type_trans(skb, dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001594#ifdef TC35815_NAPI
1595 netif_receive_skb(skb);
1596 received++;
1597#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 netif_rx(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001599#endif
1600 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 lp->stats.rx_packets++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001602 lp->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 } else {
1604 lp->stats.rx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001605 printk(KERN_DEBUG "%s: Rx error (status %x)\n",
1606 dev->name, status & Rx_Stat_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1608 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1609 status &= ~(Rx_LongErr|Rx_CRCErr);
1610 status |= Rx_Over;
1611 }
1612 if (status & Rx_LongErr) lp->stats.rx_length_errors++;
1613 if (status & Rx_Over) lp->stats.rx_fifo_errors++;
1614 if (status & Rx_CRCErr) lp->stats.rx_crc_errors++;
1615 if (status & Rx_Align) lp->stats.rx_frame_errors++;
1616 }
1617
1618 if (bd_count > 0) {
1619 /* put Free Buffer back to controller */
1620 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1621 unsigned char id =
1622 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001623#ifdef DEBUG
1624 if (id >= RX_BUF_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 printk("%s: invalid BDID.\n", dev->name);
1626 panic_queues(dev);
1627 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001628#else
1629 BUG_ON(id >= RX_BUF_NUM);
1630#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /* free old buffers */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001632#ifdef TC35815_USE_PACKEDBUFFER
1633 while (lp->fbl_curid != id)
1634#else
1635 while (lp->fbl_count < RX_BUF_NUM)
1636#endif
1637 {
1638#ifdef TC35815_USE_PACKEDBUFFER
1639 unsigned char curid = lp->fbl_curid;
1640#else
1641 unsigned char curid =
1642 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1643#endif
1644 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1645#ifdef DEBUG
1646 bdctl = le32_to_cpu(bd->BDCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (bdctl & BD_CownsBD) {
1648 printk("%s: Freeing invalid BD.\n",
1649 dev->name);
1650 panic_queues(dev);
1651 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* pass BD to controler */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001654#ifndef TC35815_USE_PACKEDBUFFER
1655 if (!lp->rx_skbs[curid].skb) {
1656 lp->rx_skbs[curid].skb =
1657 alloc_rxbuf_skb(dev,
1658 lp->pci_dev,
1659 &lp->rx_skbs[curid].skb_dma);
1660 if (!lp->rx_skbs[curid].skb)
1661 break; /* try on next reception */
1662 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1663 }
1664#endif /* TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* Note: BDLength was modified by chip. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001666 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1667 (curid << BD_RxBDID_SHIFT) |
1668 RX_BUF_SIZE);
1669#ifdef TC35815_USE_PACKEDBUFFER
1670 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1671 if (netif_msg_rx_status(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 printk("%s: Entering new FBD %d\n",
1673 dev->name, lp->fbl_curid);
1674 dump_frfd(lp->fbl_ptr);
1675 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001676#else
1677 lp->fbl_count++;
1678#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 buf_free_count++;
1680 }
1681 }
1682
1683 /* put RxFD back to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001684#ifdef DEBUG
1685 next_rfd = fd_bus_to_virt(lp,
1686 le32_to_cpu(lp->rfd_cur->fd.FDNext));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1688 printk("%s: RxFD FDNext invalid.\n", dev->name);
1689 panic_queues(dev);
1690 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
1693 /* pass FD to controler */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001694#ifdef DEBUG
1695 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1696#else
1697 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1700 lp->rfd_cur++;
1701 fd_free_count++;
1702 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001703 if (lp->rfd_cur > lp->rfd_limit)
1704 lp->rfd_cur = lp->rfd_base;
1705#ifdef DEBUG
1706 if (lp->rfd_cur != next_rfd)
1707 printk("rfd_cur = %p, next_rfd %p\n",
1708 lp->rfd_cur, next_rfd);
1709#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 /* re-enable BL/FDA Exhaust interrupts. */
1713 if (fd_free_count) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001714 struct tc35815_regs __iomem *tr =
1715 (struct tc35815_regs __iomem *)dev->base_addr;
1716 u32 en, en_old = tc_readl(&tr->Int_En);
1717 en = en_old | Int_FDAExEn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (buf_free_count)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001719 en |= Int_BLExEn;
1720 if (en != en_old)
1721 tc_writel(en, &tr->Int_En);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001723#ifdef TC35815_NAPI
1724 return received;
1725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001728#ifdef TC35815_NAPI
1729static int
1730tc35815_poll(struct net_device *dev, int *budget)
1731{
1732 struct tc35815_local *lp = dev->priv;
1733 struct tc35815_regs __iomem *tr =
1734 (struct tc35815_regs __iomem *)dev->base_addr;
1735 int limit = min(*budget, dev->quota);
1736 int received = 0, handled;
1737 u32 status;
1738
1739 spin_lock(&lp->lock);
1740 status = tc_readl(&tr->Int_Src);
1741 do {
1742 tc_writel(status, &tr->Int_Src); /* write to clear */
1743
1744 handled = tc35815_do_interrupt(dev, status, limit);
1745 if (handled >= 0) {
1746 received += handled;
1747 limit -= handled;
1748 if (limit <= 0)
1749 break;
1750 }
1751 status = tc_readl(&tr->Int_Src);
1752 } while (status);
1753 spin_unlock(&lp->lock);
1754
1755 dev->quota -= received;
1756 *budget -= received;
1757 if (limit <= 0)
1758 return 1;
1759
1760 netif_rx_complete(dev);
1761 /* enable interrupts */
1762 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1763 return 0;
1764}
1765#endif
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767#ifdef NO_CHECK_CARRIER
1768#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1769#else
1770#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1771#endif
1772
1773static void
1774tc35815_check_tx_stat(struct net_device *dev, int status)
1775{
1776 struct tc35815_local *lp = dev->priv;
1777 const char *msg = NULL;
1778
1779 /* count collisions */
1780 if (status & Tx_ExColl)
1781 lp->stats.collisions += 16;
1782 if (status & Tx_TxColl_MASK)
1783 lp->stats.collisions += status & Tx_TxColl_MASK;
1784
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001785#ifndef NO_CHECK_CARRIER
1786 /* TX4939 does not have NCarr */
1787 if (lp->boardtype == TC35815_TX4939)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 status &= ~Tx_NCarr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001789#ifdef WORKAROUND_LOSTCAR
1790 /* WORKAROUND: ignore LostCrS in full duplex operation */
1791 if ((lp->timer_state != asleep && lp->timer_state != lcheck)
1792 || lp->fullduplex)
1793 status &= ~Tx_NCarr;
1794#endif
1795#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (!(status & TX_STA_ERR)) {
1798 /* no error. */
1799 lp->stats.tx_packets++;
1800 return;
1801 }
1802
1803 lp->stats.tx_errors++;
1804 if (status & Tx_ExColl) {
1805 lp->stats.tx_aborted_errors++;
1806 msg = "Excessive Collision.";
1807 }
1808 if (status & Tx_Under) {
1809 lp->stats.tx_fifo_errors++;
1810 msg = "Tx FIFO Underrun.";
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001811 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1812 lp->lstats.tx_underrun++;
1813 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1814 struct tc35815_regs __iomem *tr =
1815 (struct tc35815_regs __iomem *)dev->base_addr;
1816 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1817 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1818 }
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821 if (status & Tx_Defer) {
1822 lp->stats.tx_fifo_errors++;
1823 msg = "Excessive Deferral.";
1824 }
1825#ifndef NO_CHECK_CARRIER
1826 if (status & Tx_NCarr) {
1827 lp->stats.tx_carrier_errors++;
1828 msg = "Lost Carrier Sense.";
1829 }
1830#endif
1831 if (status & Tx_LateColl) {
1832 lp->stats.tx_aborted_errors++;
1833 msg = "Late Collision.";
1834 }
1835 if (status & Tx_TxPar) {
1836 lp->stats.tx_fifo_errors++;
1837 msg = "Transmit Parity Error.";
1838 }
1839 if (status & Tx_SQErr) {
1840 lp->stats.tx_heartbeat_errors++;
1841 msg = "Signal Quality Error.";
1842 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001843 if (msg && netif_msg_tx_err(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
1845}
1846
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001847/* This handles TX complete events posted by the device
1848 * via interrupts.
1849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850static void
1851tc35815_txdone(struct net_device *dev)
1852{
1853 struct tc35815_local *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct TxFD *txfd;
1855 unsigned int fdctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 txfd = &lp->tfd_base[lp->tfd_end];
1858 while (lp->tfd_start != lp->tfd_end &&
1859 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
1860 int status = le32_to_cpu(txfd->fd.FDStat);
1861 struct sk_buff *skb;
1862 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001863 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001865 if (netif_msg_tx_done(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 printk("%s: complete TxFD.\n", dev->name);
1867 dump_txfd(txfd);
1868 }
1869 tc35815_check_tx_stat(dev, status);
1870
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001871 skb = fdsystem != 0xffffffff ?
1872 lp->tx_skbs[fdsystem].skb : NULL;
1873#ifdef DEBUG
1874 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
1875 printk("%s: tx_skbs mismatch.\n", dev->name);
1876 panic_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001878#else
1879 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
1880#endif
1881 if (skb) {
1882 lp->stats.tx_bytes += skb->len;
1883 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
1884 lp->tx_skbs[lp->tfd_end].skb = NULL;
1885 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
1886#ifdef TC35815_NAPI
1887 dev_kfree_skb_any(skb);
1888#else
1889 dev_kfree_skb_irq(skb);
1890#endif
1891 }
1892 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
1895 txfd = &lp->tfd_base[lp->tfd_end];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001896#ifdef DEBUG
1897 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 printk("%s: TxFD FDNext invalid.\n", dev->name);
1899 panic_queues(dev);
1900 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (fdnext & FD_Next_EOL) {
1903 /* DMA Transmitter has been stopping... */
1904 if (lp->tfd_end != lp->tfd_start) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001905 struct tc35815_regs __iomem *tr =
1906 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
1908 struct TxFD* txhead = &lp->tfd_base[head];
1909 int qlen = (lp->tfd_start + TX_FD_NUM
1910 - lp->tfd_end) % TX_FD_NUM;
1911
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001912#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
1914 printk("%s: TxFD FDCtl invalid.\n", dev->name);
1915 panic_queues(dev);
1916 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001917#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /* log max queue length */
1919 if (lp->lstats.max_tx_qlen < qlen)
1920 lp->lstats.max_tx_qlen = qlen;
1921
1922
1923 /* start DMA Transmitter again */
1924 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
1925#ifdef GATHER_TXINT
1926 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
1927#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001928 if (netif_msg_tx_queued(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 printk("%s: start TxFD on queue.\n",
1930 dev->name);
1931 dump_txfd(txfd);
1932 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001933 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935 break;
1936 }
1937 }
1938
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001939 /* If we had stopped the queue due to a "tx full"
1940 * condition, and space has now been made available,
1941 * wake up the queue.
1942 */
1943 if (netif_queue_stopped(dev) && ! tc35815_tx_full(dev))
1944 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
1947/* The inverse routine to tc35815_open(). */
1948static int
1949tc35815_close(struct net_device *dev)
1950{
1951 struct tc35815_local *lp = dev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 netif_stop_queue(dev);
1953
1954 /* Flush the Tx and disable Rx here. */
1955
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001956 del_timer(&lp->timer); /* Kill if running */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 tc35815_chip_reset(dev);
1958 free_irq(dev->irq, dev);
1959
1960 tc35815_free_queues(dev);
1961
1962 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966/*
1967 * Get the current statistics.
1968 * This may be called with the card open or closed.
1969 */
1970static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
1971{
1972 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001973 struct tc35815_regs __iomem *tr =
1974 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Update the statistics from the device registers. */
1977 lp->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979
1980 return &lp->stats;
1981}
1982
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001983static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001985 struct tc35815_local *lp = dev->priv;
1986 struct tc35815_regs __iomem *tr =
1987 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 int cam_index = index * 6;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001989 u32 cam_data;
1990 u32 saved_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 saved_addr = tc_readl(&tr->CAM_Adr);
1992
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001993 if (netif_msg_hw(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 int i;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001995 printk(KERN_DEBUG "%s: CAM %d:", dev->name, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 for (i = 0; i < 6; i++)
1997 printk(" %02x", addr[i]);
1998 printk("\n");
1999 }
2000 if (index & 1) {
2001 /* read modify write */
2002 tc_writel(cam_index - 2, &tr->CAM_Adr);
2003 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
2004 cam_data |= addr[0] << 8 | addr[1];
2005 tc_writel(cam_data, &tr->CAM_Data);
2006 /* write whole word */
2007 tc_writel(cam_index + 2, &tr->CAM_Adr);
2008 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
2009 tc_writel(cam_data, &tr->CAM_Data);
2010 } else {
2011 /* write whole word */
2012 tc_writel(cam_index, &tr->CAM_Adr);
2013 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
2014 tc_writel(cam_data, &tr->CAM_Data);
2015 /* read modify write */
2016 tc_writel(cam_index + 4, &tr->CAM_Adr);
2017 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
2018 cam_data |= addr[4] << 24 | (addr[5] << 16);
2019 tc_writel(cam_data, &tr->CAM_Data);
2020 }
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 tc_writel(saved_addr, &tr->CAM_Adr);
2023}
2024
2025
2026/*
2027 * Set or clear the multicast filter for this adaptor.
2028 * num_addrs == -1 Promiscuous mode, receive all packets
2029 * num_addrs == 0 Normal mode, clear multicast list
2030 * num_addrs > 0 Multicast mode, receive normal and MC packets,
2031 * and do best-effort filtering.
2032 */
2033static void
2034tc35815_set_multicast_list(struct net_device *dev)
2035{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002036 struct tc35815_regs __iomem *tr =
2037 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 if (dev->flags&IFF_PROMISC)
2040 {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002041#ifdef WORKAROUND_100HALF_PROMISC
2042 /* With some (all?) 100MHalf HUB, controller will hang
2043 * if we enabled promiscuous mode before linkup... */
2044 struct tc35815_local *lp = dev->priv;
2045 int pid = lp->phy_addr;
2046 if (!(tc_mdio_read(dev, pid, MII_BMSR) & BMSR_LSTATUS))
2047 return;
2048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* Enable promiscuous mode */
2050 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
2051 }
2052 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > CAM_ENTRY_MAX - 3)
2053 {
2054 /* CAM 0, 1, 20 are reserved. */
2055 /* Disable promiscuous mode, use normal mode. */
2056 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
2057 }
2058 else if(dev->mc_count)
2059 {
2060 struct dev_mc_list* cur_addr = dev->mc_list;
2061 int i;
2062 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
2063
2064 tc_writel(0, &tr->CAM_Ctl);
2065 /* Walk the address list, and load the filter */
2066 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
2067 if (!cur_addr)
2068 break;
2069 /* entry 0,1 is reserved. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002070 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 ena_bits |= CAM_Ena_Bit(i + 2);
2072 }
2073 tc_writel(ena_bits, &tr->CAM_Ena);
2074 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2075 }
2076 else {
2077 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2078 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2079 }
2080}
2081
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002082static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002085 strcpy(info->driver, MODNAME);
2086 strcpy(info->version, DRV_VERSION);
2087 strcpy(info->bus_info, pci_name(lp->pci_dev));
2088}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002089
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002090static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2091{
2092 struct tc35815_local *lp = dev->priv;
2093 spin_lock_irq(&lp->lock);
2094 mii_ethtool_gset(&lp->mii, cmd);
2095 spin_unlock_irq(&lp->lock);
2096 return 0;
2097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002099static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2100{
2101 struct tc35815_local *lp = dev->priv;
2102 int rc;
2103#if 1 /* use our negotiation method... */
2104 /* Verify the settings we care about. */
2105 if (cmd->autoneg != AUTONEG_ENABLE &&
2106 cmd->autoneg != AUTONEG_DISABLE)
2107 return -EINVAL;
2108 if (cmd->autoneg == AUTONEG_DISABLE &&
2109 ((cmd->speed != SPEED_100 &&
2110 cmd->speed != SPEED_10) ||
2111 (cmd->duplex != DUPLEX_HALF &&
2112 cmd->duplex != DUPLEX_FULL)))
2113 return -EINVAL;
2114
2115 /* Ok, do it to it. */
2116 spin_lock_irq(&lp->lock);
2117 del_timer(&lp->timer);
2118 tc35815_start_auto_negotiation(dev, cmd);
2119 spin_unlock_irq(&lp->lock);
2120 rc = 0;
2121#else
2122 spin_lock_irq(&lp->lock);
2123 rc = mii_ethtool_sset(&lp->mii, cmd);
2124 spin_unlock_irq(&lp->lock);
2125#endif
2126 return rc;
2127}
2128
2129static int tc35815_nway_reset(struct net_device *dev)
2130{
2131 struct tc35815_local *lp = dev->priv;
2132 int rc;
2133 spin_lock_irq(&lp->lock);
2134 rc = mii_nway_restart(&lp->mii);
2135 spin_unlock_irq(&lp->lock);
2136 return rc;
2137}
2138
2139static u32 tc35815_get_link(struct net_device *dev)
2140{
2141 struct tc35815_local *lp = dev->priv;
2142 int rc;
2143 spin_lock_irq(&lp->lock);
2144 rc = mii_link_ok(&lp->mii);
2145 spin_unlock_irq(&lp->lock);
2146 return rc;
2147}
2148
2149static u32 tc35815_get_msglevel(struct net_device *dev)
2150{
2151 struct tc35815_local *lp = dev->priv;
2152 return lp->msg_enable;
2153}
2154
2155static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2156{
2157 struct tc35815_local *lp = dev->priv;
2158 lp->msg_enable = datum;
2159}
2160
2161static int tc35815_get_stats_count(struct net_device *dev)
2162{
2163 struct tc35815_local *lp = dev->priv;
2164 return sizeof(lp->lstats) / sizeof(int);
2165}
2166
2167static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2168{
2169 struct tc35815_local *lp = dev->priv;
2170 data[0] = lp->lstats.max_tx_qlen;
2171 data[1] = lp->lstats.tx_ints;
2172 data[2] = lp->lstats.rx_ints;
2173 data[3] = lp->lstats.tx_underrun;
2174}
2175
2176static struct {
2177 const char str[ETH_GSTRING_LEN];
2178} ethtool_stats_keys[] = {
2179 { "max_tx_qlen" },
2180 { "tx_ints" },
2181 { "rx_ints" },
2182 { "tx_underrun" },
2183};
2184
2185static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2186{
2187 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2188}
2189
2190static const struct ethtool_ops tc35815_ethtool_ops = {
2191 .get_drvinfo = tc35815_get_drvinfo,
2192 .get_settings = tc35815_get_settings,
2193 .set_settings = tc35815_set_settings,
2194 .nway_reset = tc35815_nway_reset,
2195 .get_link = tc35815_get_link,
2196 .get_msglevel = tc35815_get_msglevel,
2197 .set_msglevel = tc35815_set_msglevel,
2198 .get_strings = tc35815_get_strings,
2199 .get_stats_count = tc35815_get_stats_count,
2200 .get_ethtool_stats = tc35815_get_ethtool_stats,
2201 .get_perm_addr = ethtool_op_get_perm_addr,
2202};
2203
2204static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2205{
2206 struct tc35815_local *lp = dev->priv;
2207 int rc;
2208
2209 if (!netif_running(dev))
2210 return -EINVAL;
2211
2212 spin_lock_irq(&lp->lock);
2213 rc = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
2214 spin_unlock_irq(&lp->lock);
2215
2216 return rc;
2217}
2218
2219static int tc_mdio_read(struct net_device *dev, int phy_id, int location)
2220{
2221 struct tc35815_regs __iomem *tr =
2222 (struct tc35815_regs __iomem *)dev->base_addr;
2223 u32 data;
2224 tc_writel(MD_CA_Busy | (phy_id << 5) | location, &tr->MD_CA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2226 ;
2227 data = tc_readl(&tr->MD_Data);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002228 return data & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002231static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
2232 int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002234 struct tc35815_regs __iomem *tr =
2235 (struct tc35815_regs __iomem *)dev->base_addr;
2236 tc_writel(val, &tr->MD_Data);
2237 tc_writel(MD_CA_Busy | MD_CA_Wr | (phy_id << 5) | location, &tr->MD_CA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2239 ;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002240}
2241
2242/* Auto negotiation. The scheme is very simple. We have a timer routine
2243 * that keeps watching the auto negotiation process as it progresses.
2244 * The DP83840 is first told to start doing it's thing, we set up the time
2245 * and place the timer state machine in it's initial state.
2246 *
2247 * Here the timer peeks at the DP83840 status registers at each click to see
2248 * if the auto negotiation has completed, we assume here that the DP83840 PHY
2249 * will time out at some point and just tell us what (didn't) happen. For
2250 * complete coverage we only allow so many of the ticks at this level to run,
2251 * when this has expired we print a warning message and try another strategy.
2252 * This "other" strategy is to force the interface into various speed/duplex
2253 * configurations and we stop when we see a link-up condition before the
2254 * maximum number of "peek" ticks have occurred.
2255 *
2256 * Once a valid link status has been detected we configure the BigMAC and
2257 * the rest of the Happy Meal to speak the most efficient protocol we could
2258 * get a clean link for. The priority for link configurations, highest first
2259 * is:
2260 * 100 Base-T Full Duplex
2261 * 100 Base-T Half Duplex
2262 * 10 Base-T Full Duplex
2263 * 10 Base-T Half Duplex
2264 *
2265 * We start a new timer now, after a successful auto negotiation status has
2266 * been detected. This timer just waits for the link-up bit to get set in
2267 * the BMCR of the DP83840. When this occurs we print a kernel log message
2268 * describing the link type in use and the fact that it is up.
2269 *
2270 * If a fatal error of some sort is signalled and detected in the interrupt
2271 * service routine, and the chip is reset, or the link is ifconfig'd down
2272 * and then back up, this entire process repeats itself all over again.
2273 */
2274/* Note: Above comments are come from sunhme driver. */
2275
2276static int tc35815_try_next_permutation(struct net_device *dev)
2277{
2278 struct tc35815_local *lp = dev->priv;
2279 int pid = lp->phy_addr;
2280 unsigned short bmcr;
2281
2282 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2283
2284 /* Downgrade from full to half duplex. Only possible via ethtool. */
2285 if (bmcr & BMCR_FULLDPLX) {
2286 bmcr &= ~BMCR_FULLDPLX;
2287 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2288 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2289 return 0;
2290 }
2291
2292 /* Downgrade from 100 to 10. */
2293 if (bmcr & BMCR_SPEED100) {
2294 bmcr &= ~BMCR_SPEED100;
2295 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2296 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2297 return 0;
2298 }
2299
2300 /* We've tried everything. */
2301 return -1;
2302}
2303
2304static void
2305tc35815_display_link_mode(struct net_device *dev)
2306{
2307 struct tc35815_local *lp = dev->priv;
2308 int pid = lp->phy_addr;
2309 unsigned short lpa, bmcr;
2310 char *speed = "", *duplex = "";
2311
2312 lpa = tc_mdio_read(dev, pid, MII_LPA);
2313 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2314 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2315 speed = "100Mb/s";
2316 else
2317 speed = "10Mb/s";
2318 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2319 duplex = "Full Duplex";
2320 else
2321 duplex = "Half Duplex";
2322
2323 if (netif_msg_link(lp))
2324 printk(KERN_INFO "%s: Link is up at %s, %s.\n",
2325 dev->name, speed, duplex);
2326 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2327 dev->name,
2328 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2329}
2330
2331static void tc35815_display_forced_link_mode(struct net_device *dev)
2332{
2333 struct tc35815_local *lp = dev->priv;
2334 int pid = lp->phy_addr;
2335 unsigned short bmcr;
2336 char *speed = "", *duplex = "";
2337
2338 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2339 if (bmcr & BMCR_SPEED100)
2340 speed = "100Mb/s";
2341 else
2342 speed = "10Mb/s";
2343 if (bmcr & BMCR_FULLDPLX)
2344 duplex = "Full Duplex.\n";
2345 else
2346 duplex = "Half Duplex.\n";
2347
2348 if (netif_msg_link(lp))
2349 printk(KERN_INFO "%s: Link has been forced up at %s, %s",
2350 dev->name, speed, duplex);
2351}
2352
2353static void tc35815_set_link_modes(struct net_device *dev)
2354{
2355 struct tc35815_local *lp = dev->priv;
2356 struct tc35815_regs __iomem *tr =
2357 (struct tc35815_regs __iomem *)dev->base_addr;
2358 int pid = lp->phy_addr;
2359 unsigned short bmcr, lpa;
2360 int speed;
2361
2362 if (lp->timer_state == arbwait) {
2363 lpa = tc_mdio_read(dev, pid, MII_LPA);
2364 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2365 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2366 dev->name,
2367 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2368 if (!(lpa & (LPA_10HALF | LPA_10FULL |
2369 LPA_100HALF | LPA_100FULL))) {
2370 /* fall back to 10HALF */
2371 printk(KERN_INFO "%s: bad ability %04x - falling back to 10HD.\n",
2372 dev->name, lpa);
2373 lpa = LPA_10HALF;
2374 }
2375 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2376 lp->fullduplex = 1;
2377 else
2378 lp->fullduplex = 0;
2379 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2380 speed = 100;
2381 else
2382 speed = 10;
2383 } else {
2384 /* Forcing a link mode. */
2385 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2386 if (bmcr & BMCR_FULLDPLX)
2387 lp->fullduplex = 1;
2388 else
2389 lp->fullduplex = 0;
2390 if (bmcr & BMCR_SPEED100)
2391 speed = 100;
2392 else
2393 speed = 10;
2394 }
2395
2396 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_HaltReq, &tr->MAC_Ctl);
2397 if (lp->fullduplex) {
2398 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl);
2399 } else {
2400 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_FullDup, &tr->MAC_Ctl);
2401 }
2402 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_HaltReq, &tr->MAC_Ctl);
2403
2404 /* TX4939 PCFG.SPEEDn bit will be changed on NETDEV_CHANGE event. */
2405
2406#ifndef NO_CHECK_CARRIER
2407 /* TX4939 does not have EnLCarr */
2408 if (lp->boardtype != TC35815_TX4939) {
2409#ifdef WORKAROUND_LOSTCAR
2410 /* WORKAROUND: enable LostCrS only if half duplex operation */
2411 if (!lp->fullduplex && lp->boardtype != TC35815_TX4939)
2412 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, &tr->Tx_Ctl);
2413#endif
2414 }
2415#endif
2416 lp->mii.full_duplex = lp->fullduplex;
2417}
2418
2419static void tc35815_timer(unsigned long data)
2420{
2421 struct net_device *dev = (struct net_device *)data;
2422 struct tc35815_local *lp = dev->priv;
2423 int pid = lp->phy_addr;
2424 unsigned short bmsr, bmcr, lpa;
2425 int restart_timer = 0;
2426
2427 spin_lock_irq(&lp->lock);
2428
2429 lp->timer_ticks++;
2430 switch (lp->timer_state) {
2431 case arbwait:
2432 /*
2433 * Only allow for 5 ticks, thats 10 seconds and much too
2434 * long to wait for arbitration to complete.
2435 */
2436 /* TC35815 need more times... */
2437 if (lp->timer_ticks >= 10) {
2438 /* Enter force mode. */
2439 if (!options.doforce) {
2440 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2441 " cable probblem?\n", dev->name);
2442 /* Try to restart the adaptor. */
2443 tc35815_restart(dev);
2444 goto out;
2445 }
2446 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2447 " trying force link mode\n", dev->name);
2448 printk(KERN_DEBUG "%s: BMCR %x BMSR %x\n", dev->name,
2449 tc_mdio_read(dev, pid, MII_BMCR),
2450 tc_mdio_read(dev, pid, MII_BMSR));
2451 bmcr = BMCR_SPEED100;
2452 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2453
2454 /*
2455 * OK, seems we need do disable the transceiver
2456 * for the first tick to make sure we get an
2457 * accurate link state at the second tick.
2458 */
2459
2460 lp->timer_state = ltrywait;
2461 lp->timer_ticks = 0;
2462 restart_timer = 1;
2463 } else {
2464 /* Anything interesting happen? */
2465 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2466 if (bmsr & BMSR_ANEGCOMPLETE) {
2467 /* Just what we've been waiting for... */
2468 tc35815_set_link_modes(dev);
2469
2470 /*
2471 * Success, at least so far, advance our state
2472 * engine.
2473 */
2474 lp->timer_state = lupwait;
2475 restart_timer = 1;
2476 } else {
2477 restart_timer = 1;
2478 }
2479 }
2480 break;
2481
2482 case lupwait:
2483 /*
2484 * Auto negotiation was successful and we are awaiting a
2485 * link up status. I have decided to let this timer run
2486 * forever until some sort of error is signalled, reporting
2487 * a message to the user at 10 second intervals.
2488 */
2489 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2490 if (bmsr & BMSR_LSTATUS) {
2491 /*
2492 * Wheee, it's up, display the link mode in use and put
2493 * the timer to sleep.
2494 */
2495 tc35815_display_link_mode(dev);
2496 netif_carrier_on(dev);
2497#ifdef WORKAROUND_100HALF_PROMISC
2498 /* delayed promiscuous enabling */
2499 if (dev->flags & IFF_PROMISC)
2500 tc35815_set_multicast_list(dev);
2501#endif
2502#if 1
2503 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2504 lp->timer_state = lcheck;
2505 restart_timer = 1;
2506#else
2507 lp->timer_state = asleep;
2508 restart_timer = 0;
2509#endif
2510 } else {
2511 if (lp->timer_ticks >= 10) {
2512 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
2513 "not completely up.\n", dev->name);
2514 lp->timer_ticks = 0;
2515 restart_timer = 1;
2516 } else {
2517 restart_timer = 1;
2518 }
2519 }
2520 break;
2521
2522 case ltrywait:
2523 /*
2524 * Making the timeout here too long can make it take
2525 * annoyingly long to attempt all of the link mode
2526 * permutations, but then again this is essentially
2527 * error recovery code for the most part.
2528 */
2529 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2530 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2531 if (lp->timer_ticks == 1) {
2532 /*
2533 * Re-enable transceiver, we'll re-enable the
2534 * transceiver next tick, then check link state
2535 * on the following tick.
2536 */
2537 restart_timer = 1;
2538 break;
2539 }
2540 if (lp->timer_ticks == 2) {
2541 restart_timer = 1;
2542 break;
2543 }
2544 if (bmsr & BMSR_LSTATUS) {
2545 /* Force mode selection success. */
2546 tc35815_display_forced_link_mode(dev);
2547 netif_carrier_on(dev);
2548 tc35815_set_link_modes(dev);
2549#ifdef WORKAROUND_100HALF_PROMISC
2550 /* delayed promiscuous enabling */
2551 if (dev->flags & IFF_PROMISC)
2552 tc35815_set_multicast_list(dev);
2553#endif
2554#if 1
2555 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2556 lp->timer_state = lcheck;
2557 restart_timer = 1;
2558#else
2559 lp->timer_state = asleep;
2560 restart_timer = 0;
2561#endif
2562 } else {
2563 if (lp->timer_ticks >= 4) { /* 6 seconds or so... */
2564 int ret;
2565
2566 ret = tc35815_try_next_permutation(dev);
2567 if (ret == -1) {
2568 /*
2569 * Aieee, tried them all, reset the
2570 * chip and try all over again.
2571 */
2572 printk(KERN_NOTICE "%s: Link down, "
2573 "cable problem?\n",
2574 dev->name);
2575
2576 /* Try to restart the adaptor. */
2577 tc35815_restart(dev);
2578 goto out;
2579 }
2580 lp->timer_ticks = 0;
2581 restart_timer = 1;
2582 } else {
2583 restart_timer = 1;
2584 }
2585 }
2586 break;
2587
2588 case lcheck:
2589 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2590 lpa = tc_mdio_read(dev, pid, MII_LPA);
2591 if (bmcr & (BMCR_PDOWN | BMCR_ISOLATE | BMCR_RESET)) {
2592 printk(KERN_ERR "%s: PHY down? (BMCR %x)\n", dev->name,
2593 bmcr);
2594 } else if ((lp->saved_lpa ^ lpa) &
2595 (LPA_100FULL|LPA_100HALF|LPA_10FULL|LPA_10HALF)) {
2596 printk(KERN_NOTICE "%s: link status changed"
2597 " (BMCR %x LPA %x->%x)\n", dev->name,
2598 bmcr, lp->saved_lpa, lpa);
2599 } else {
2600 /* go on */
2601 restart_timer = 1;
2602 break;
2603 }
2604 /* Try to restart the adaptor. */
2605 tc35815_restart(dev);
2606 goto out;
2607
2608 case asleep:
2609 default:
2610 /* Can't happens.... */
2611 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
2612 "one anyways!\n", dev->name);
2613 restart_timer = 0;
2614 lp->timer_ticks = 0;
2615 lp->timer_state = asleep; /* foo on you */
2616 break;
2617 }
2618
2619 if (restart_timer) {
2620 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2621 add_timer(&lp->timer);
2622 }
2623out:
2624 spin_unlock_irq(&lp->lock);
2625}
2626
2627static void tc35815_start_auto_negotiation(struct net_device *dev,
2628 struct ethtool_cmd *ep)
2629{
2630 struct tc35815_local *lp = dev->priv;
2631 int pid = lp->phy_addr;
2632 unsigned short bmsr, bmcr, advertize;
2633 int timeout;
2634
2635 netif_carrier_off(dev);
2636 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2637 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2638 advertize = tc_mdio_read(dev, pid, MII_ADVERTISE);
2639
2640 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2641 if (options.speed || options.duplex) {
2642 /* Advertise only specified configuration. */
2643 advertize &= ~(ADVERTISE_10HALF |
2644 ADVERTISE_10FULL |
2645 ADVERTISE_100HALF |
2646 ADVERTISE_100FULL);
2647 if (options.speed != 10) {
2648 if (options.duplex != 1)
2649 advertize |= ADVERTISE_100FULL;
2650 if (options.duplex != 2)
2651 advertize |= ADVERTISE_100HALF;
2652 }
2653 if (options.speed != 100) {
2654 if (options.duplex != 1)
2655 advertize |= ADVERTISE_10FULL;
2656 if (options.duplex != 2)
2657 advertize |= ADVERTISE_10HALF;
2658 }
2659 if (options.speed == 100)
2660 bmcr |= BMCR_SPEED100;
2661 else if (options.speed == 10)
2662 bmcr &= ~BMCR_SPEED100;
2663 if (options.duplex == 2)
2664 bmcr |= BMCR_FULLDPLX;
2665 else if (options.duplex == 1)
2666 bmcr &= ~BMCR_FULLDPLX;
2667 } else {
2668 /* Advertise everything we can support. */
2669 if (bmsr & BMSR_10HALF)
2670 advertize |= ADVERTISE_10HALF;
2671 else
2672 advertize &= ~ADVERTISE_10HALF;
2673 if (bmsr & BMSR_10FULL)
2674 advertize |= ADVERTISE_10FULL;
2675 else
2676 advertize &= ~ADVERTISE_10FULL;
2677 if (bmsr & BMSR_100HALF)
2678 advertize |= ADVERTISE_100HALF;
2679 else
2680 advertize &= ~ADVERTISE_100HALF;
2681 if (bmsr & BMSR_100FULL)
2682 advertize |= ADVERTISE_100FULL;
2683 else
2684 advertize &= ~ADVERTISE_100FULL;
2685 }
2686
2687 tc_mdio_write(dev, pid, MII_ADVERTISE, advertize);
2688
2689 /* Enable Auto-Negotiation, this is usually on already... */
2690 bmcr |= BMCR_ANENABLE;
2691 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2692
2693 /* Restart it to make sure it is going. */
2694 bmcr |= BMCR_ANRESTART;
2695 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2696 printk(KERN_DEBUG "%s: ADVERTISE %x BMCR %x\n", dev->name, advertize, bmcr);
2697
2698 /* BMCR_ANRESTART self clears when the process has begun. */
2699 timeout = 64; /* More than enough. */
2700 while (--timeout) {
2701 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2702 if (!(bmcr & BMCR_ANRESTART))
2703 break; /* got it. */
2704 udelay(10);
2705 }
2706 if (!timeout) {
2707 printk(KERN_ERR "%s: TC35815 would not start auto "
2708 "negotiation BMCR=0x%04x\n",
2709 dev->name, bmcr);
2710 printk(KERN_NOTICE "%s: Performing force link "
2711 "detection.\n", dev->name);
2712 goto force_link;
2713 } else {
2714 printk(KERN_DEBUG "%s: auto negotiation started.\n", dev->name);
2715 lp->timer_state = arbwait;
2716 }
2717 } else {
2718force_link:
2719 /* Force the link up, trying first a particular mode.
2720 * Either we are here at the request of ethtool or
2721 * because the Happy Meal would not start to autoneg.
2722 */
2723
2724 /* Disable auto-negotiation in BMCR, enable the duplex and
2725 * speed setting, init the timer state machine, and fire it off.
2726 */
2727 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2728 bmcr = BMCR_SPEED100;
2729 } else {
2730 if (ep->speed == SPEED_100)
2731 bmcr = BMCR_SPEED100;
2732 else
2733 bmcr = 0;
2734 if (ep->duplex == DUPLEX_FULL)
2735 bmcr |= BMCR_FULLDPLX;
2736 }
2737 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2738
2739 /* OK, seems we need do disable the transceiver for the first
2740 * tick to make sure we get an accurate link state at the
2741 * second tick.
2742 */
2743 lp->timer_state = ltrywait;
2744 }
2745
2746 del_timer(&lp->timer);
2747 lp->timer_ticks = 0;
2748 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2749 add_timer(&lp->timer);
2750}
2751
2752static void tc35815_find_phy(struct net_device *dev)
2753{
2754 struct tc35815_local *lp = dev->priv;
2755 int pid = lp->phy_addr;
2756 unsigned short id0;
2757
2758 /* find MII phy */
2759 for (pid = 31; pid >= 0; pid--) {
2760 id0 = tc_mdio_read(dev, pid, MII_BMSR);
2761 if (id0 != 0xffff && id0 != 0x0000 &&
2762 (id0 & BMSR_RESV) != (0xffff & BMSR_RESV) /* paranoia? */
2763 ) {
2764 lp->phy_addr = pid;
2765 break;
2766 }
2767 }
2768 if (pid < 0) {
2769 printk(KERN_ERR "%s: No MII Phy found.\n",
2770 dev->name);
2771 lp->phy_addr = pid = 0;
2772 }
2773
2774 lp->mii_id[0] = tc_mdio_read(dev, pid, MII_PHYSID1);
2775 lp->mii_id[1] = tc_mdio_read(dev, pid, MII_PHYSID2);
2776 if (netif_msg_hw(lp))
2777 printk(KERN_INFO "%s: PHY(%02x) ID %04x %04x\n", dev->name,
2778 pid, lp->mii_id[0], lp->mii_id[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781static void tc35815_phy_chip_init(struct net_device *dev)
2782{
2783 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002784 int pid = lp->phy_addr;
2785 unsigned short bmcr;
2786 struct ethtool_cmd ecmd, *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002788 /* dis-isolate if needed. */
2789 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2790 if (bmcr & BMCR_ISOLATE) {
2791 int count = 32;
2792 printk(KERN_DEBUG "%s: unisolating...", dev->name);
2793 tc_mdio_write(dev, pid, MII_BMCR, bmcr & ~BMCR_ISOLATE);
2794 while (--count) {
2795 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_ISOLATE))
2796 break;
2797 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002799 printk(" %s.\n", count ? "done" : "failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002802 if (options.speed && options.duplex) {
2803 ecmd.autoneg = AUTONEG_DISABLE;
2804 ecmd.speed = options.speed == 10 ? SPEED_10 : SPEED_100;
2805 ecmd.duplex = options.duplex == 1 ? DUPLEX_HALF : DUPLEX_FULL;
2806 ep = &ecmd;
2807 } else {
2808 ep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002810 tc35815_start_auto_negotiation(dev, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
2813static void tc35815_chip_reset(struct net_device *dev)
2814{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002815 struct tc35815_regs __iomem *tr =
2816 (struct tc35815_regs __iomem *)dev->base_addr;
2817 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /* reset the controller */
2819 tc_writel(MAC_Reset, &tr->MAC_Ctl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002820 udelay(4); /* 3200ns */
2821 i = 0;
2822 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2823 if (i++ > 100) {
2824 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2825 break;
2826 }
2827 mdelay(1);
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 tc_writel(0, &tr->MAC_Ctl);
2830
2831 /* initialize registers to default value */
2832 tc_writel(0, &tr->DMA_Ctl);
2833 tc_writel(0, &tr->TxThrsh);
2834 tc_writel(0, &tr->TxPollCtr);
2835 tc_writel(0, &tr->RxFragSize);
2836 tc_writel(0, &tr->Int_En);
2837 tc_writel(0, &tr->FDA_Bas);
2838 tc_writel(0, &tr->FDA_Lim);
2839 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2840 tc_writel(0, &tr->CAM_Ctl);
2841 tc_writel(0, &tr->Tx_Ctl);
2842 tc_writel(0, &tr->Rx_Ctl);
2843 tc_writel(0, &tr->CAM_Ena);
2844 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2845
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002846 /* initialize internal SRAM */
2847 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2848 for (i = 0; i < 0x1000; i += 4) {
2849 tc_writel(i, &tr->CAM_Adr);
2850 tc_writel(0, &tr->CAM_Data);
2851 }
2852 tc_writel(0, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
2855static void tc35815_chip_init(struct net_device *dev)
2856{
2857 struct tc35815_local *lp = dev->priv;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002858 struct tc35815_regs __iomem *tr =
2859 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 unsigned long txctl = TX_CTL_CMD;
2861
2862 tc35815_phy_chip_init(dev);
2863
2864 /* load station address to CAM */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002865 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 /* Enable CAM (broadcast and unicast) */
2868 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2869 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2870
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002871 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2872 if (HAVE_DMA_RXALIGN(lp))
2873 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2874 else
2875 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2876#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002878#else
2879 tc_writel(ETH_ZLEN, &tr->RxFragSize);
2880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2882 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2883 tc_writel(INT_EN_CMD, &tr->Int_En);
2884
2885 /* set queues */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002886 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2888 &tr->FDA_Lim);
2889 /*
2890 * Activation method:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002891 * First, enable the MAC Transmitter and the DMA Receive circuits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 * Then enable the DMA Transmitter and the MAC Receive circuits.
2893 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002894 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 /* start MAC transmitter */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002898#ifndef NO_CHECK_CARRIER
2899 /* TX4939 does not have EnLCarr */
2900 if (lp->boardtype == TC35815_TX4939)
2901 txctl &= ~Tx_EnLCarr;
2902#ifdef WORKAROUND_LOSTCAR
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002904 if ((lp->timer_state != asleep && lp->timer_state != lcheck) ||
2905 lp->fullduplex)
2906 txctl &= ~Tx_EnLCarr;
2907#endif
2908#endif /* !NO_CHECK_CARRIER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909#ifdef GATHER_TXINT
2910 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
2911#endif
2912 tc_writel(txctl, &tr->Tx_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002915#ifdef CONFIG_PM
2916static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2917{
2918 struct net_device *dev = pci_get_drvdata(pdev);
2919 struct tc35815_local *lp = dev->priv;
2920 unsigned long flags;
2921
2922 pci_save_state(pdev);
2923 if (!netif_running(dev))
2924 return 0;
2925 netif_device_detach(dev);
2926 spin_lock_irqsave(&lp->lock, flags);
2927 del_timer(&lp->timer); /* Kill if running */
2928 tc35815_chip_reset(dev);
2929 spin_unlock_irqrestore(&lp->lock, flags);
2930 pci_set_power_state(pdev, PCI_D3hot);
2931 return 0;
2932}
2933
2934static int tc35815_resume(struct pci_dev *pdev)
2935{
2936 struct net_device *dev = pci_get_drvdata(pdev);
2937 struct tc35815_local *lp = dev->priv;
2938 unsigned long flags;
2939
2940 pci_restore_state(pdev);
2941 if (!netif_running(dev))
2942 return 0;
2943 pci_set_power_state(pdev, PCI_D0);
2944 spin_lock_irqsave(&lp->lock, flags);
2945 tc35815_restart(dev);
2946 spin_unlock_irqrestore(&lp->lock, flags);
2947 netif_device_attach(dev);
2948 return 0;
2949}
2950#endif /* CONFIG_PM */
2951
2952static struct pci_driver tc35815_pci_driver = {
2953 .name = MODNAME,
2954 .id_table = tc35815_pci_tbl,
2955 .probe = tc35815_init_one,
2956 .remove = __devexit_p(tc35815_remove_one),
2957#ifdef CONFIG_PM
2958 .suspend = tc35815_suspend,
2959 .resume = tc35815_resume,
2960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961};
2962
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002963module_param_named(speed, options.speed, int, 0);
2964MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2965module_param_named(duplex, options.duplex, int, 0);
2966MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
2967module_param_named(doforce, options.doforce, int, 0);
2968MODULE_PARM_DESC(doforce, "try force link mode if auto-negotiation failed");
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970static int __init tc35815_init_module(void)
2971{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002972 return pci_register_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975static void __exit tc35815_cleanup_module(void)
2976{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002977 pci_unregister_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
Jeff Garzik420e8522007-02-24 17:02:16 -05002979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980module_init(tc35815_init_module);
2981module_exit(tc35815_cleanup_module);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002982
2983MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2984MODULE_LICENSE("GPL");