blob: c666448fc7e36f3af41c7e431492e05eea5d7d7a [file] [log] [blame]
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001/*
2 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Based on skelton.c by Donald Becker.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09006 * This driver is a replacement of older and less maintained version.
7 * This is a header of the older version:
8 * -----<snip>-----
9 * Copyright 2001 MontaVista Software Inc.
10 * Author: MontaVista Software, Inc.
11 * ahennessy@mvista.com
12 * Copyright (C) 2000-2001 Toshiba Corporation
13 * static const char *version =
14 * "tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n";
15 * -----<snip>-----
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090017 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090021 * (C) Copyright TOSHIBA CORPORATION 2004-2005
22 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090025#ifdef TC35815_NAPI
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090026#define DRV_VERSION "1.37-NAPI"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090027#else
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090028#define DRV_VERSION "1.37"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090029#endif
30static const char *version = "tc35815.c:v" DRV_VERSION "\n";
31#define MODNAME "tc35815"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/fcntl.h>
37#include <linux/interrupt.h>
38#include <linux/ioport.h>
39#include <linux/in.h>
40#include <linux/slab.h>
41#include <linux/string.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090042#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/init.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/skbuff.h>
48#include <linux/delay.h>
49#include <linux/pci.h>
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090050#include <linux/phy.h>
51#include <linux/workqueue.h>
Atsushi Nemotobd43da82007-06-29 22:34:53 +090052#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* First, a few definitions that the brave might change. */
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define GATHER_TXINT /* On-Demand Tx Interrupt */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090059#define WORKAROUND_LOSTCAR
60#define WORKAROUND_100HALF_PROMISC
61/* #define TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090063enum tc35815_chiptype {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090064 TC35815CF = 0,
65 TC35815_NWU,
66 TC35815_TX4939,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090067};
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090068
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090069/* indexed by tc35815_chiptype, above */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090070static const struct {
71 const char *name;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090072} chip_info[] __devinitdata = {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090073 { "TOSHIBA TC35815CF 10/100BaseTX" },
74 { "TOSHIBA TC35815 with Wake on LAN" },
75 { "TOSHIBA TC35815/TX4939" },
76};
77
78static const struct pci_device_id tc35815_pci_tbl[] = {
79 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
80 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
81 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
82 {0,}
83};
Atsushi Nemoto7f225b42008-04-11 00:25:31 +090084MODULE_DEVICE_TABLE(pci, tc35815_pci_tbl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090085
86/* see MODULE_PARM_DESC */
87static struct tc35815_options {
88 int speed;
89 int duplex;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090090} options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * Registers
94 */
95struct tc35815_regs {
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +090096 __u32 DMA_Ctl; /* 0x00 */
97 __u32 TxFrmPtr;
98 __u32 TxThrsh;
99 __u32 TxPollCtr;
100 __u32 BLFrmPtr;
101 __u32 RxFragSize;
102 __u32 Int_En;
103 __u32 FDA_Bas;
104 __u32 FDA_Lim; /* 0x20 */
105 __u32 Int_Src;
106 __u32 unused0[2];
107 __u32 PauseCnt;
108 __u32 RemPauCnt;
109 __u32 TxCtlFrmStat;
110 __u32 unused1;
111 __u32 MAC_Ctl; /* 0x40 */
112 __u32 CAM_Ctl;
113 __u32 Tx_Ctl;
114 __u32 Tx_Stat;
115 __u32 Rx_Ctl;
116 __u32 Rx_Stat;
117 __u32 MD_Data;
118 __u32 MD_CA;
119 __u32 CAM_Adr; /* 0x60 */
120 __u32 CAM_Data;
121 __u32 CAM_Ena;
122 __u32 PROM_Ctl;
123 __u32 PROM_Data;
124 __u32 Algn_Cnt;
125 __u32 CRC_Cnt;
126 __u32 Miss_Cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129/*
130 * Bit assignments
131 */
132/* DMA_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900133#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
134#define DMA_RxAlign_1 0x00400000
135#define DMA_RxAlign_2 0x00800000
136#define DMA_RxAlign_3 0x00c00000
137#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
138#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
139#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
140#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
141#define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
142#define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
143#define DMA_TestMode 0x00002000 /* 1:Test Mode */
144#define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
145#define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* RxFragSize bit asign ---------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900148#define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
149#define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/* MAC_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900152#define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
153#define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
154#define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
155#define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
156#define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
157#define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
158#define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
159#define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
160#define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
161#define MAC_Reset 0x00000004 /* 1:Software Reset */
162#define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
163#define MAC_HaltReq 0x00000001 /* 1:Halt request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* PROM_Ctl bit asign ------------------------------------------------------ */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900166#define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
167#define PROM_Read 0x00004000 /*10:Read operation */
168#define PROM_Write 0x00002000 /*01:Write operation */
169#define PROM_Erase 0x00006000 /*11:Erase operation */
170 /*00:Enable or Disable Writting, */
171 /* as specified in PROM_Addr. */
172#define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
173 /*00xxxx: disable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* CAM_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900176#define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
177#define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
178 /* accept other */
179#define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
180#define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
181#define CAM_StationAcc 0x00000001 /* 1:unicast accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* CAM_Ena bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900184#define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900186#define CAM_Ena_Bit(index) (1 << (index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#define CAM_ENTRY_DESTINATION 0
188#define CAM_ENTRY_SOURCE 1
189#define CAM_ENTRY_MACCTL 20
190
191/* Tx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900192#define Tx_En 0x00000001 /* 1:Transmit enable */
193#define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
194#define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
195#define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
196#define Tx_FBack 0x00000010 /* 1:Fast Back-off */
197#define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
198#define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
199#define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
200#define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
201#define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
202#define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
203#define Tx_EnComp 0x00004000 /* 1:Enable Completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/* Tx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900206#define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
207#define Tx_ExColl 0x00000010 /* Excessive Collision */
208#define Tx_TXDefer 0x00000020 /* Transmit Defered */
209#define Tx_Paused 0x00000040 /* Transmit Paused */
210#define Tx_IntTx 0x00000080 /* Interrupt on Tx */
211#define Tx_Under 0x00000100 /* Underrun */
212#define Tx_Defer 0x00000200 /* Deferral */
213#define Tx_NCarr 0x00000400 /* No Carrier */
214#define Tx_10Stat 0x00000800 /* 10Mbps Status */
215#define Tx_LateColl 0x00001000 /* Late Collision */
216#define Tx_TxPar 0x00002000 /* Tx Parity Error */
217#define Tx_Comp 0x00004000 /* Completion */
218#define Tx_Halted 0x00008000 /* Tx Halted */
219#define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/* Rx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900222#define Rx_EnGood 0x00004000 /* 1:Enable Good */
223#define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
224#define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
225#define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
226#define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
227#define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
228#define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
229#define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
230#define Rx_ShortEn 0x00000008 /* 1:Short Enable */
231#define Rx_LongEn 0x00000004 /* 1:Long Enable */
232#define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
233#define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/* Rx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900236#define Rx_Halted 0x00008000 /* Rx Halted */
237#define Rx_Good 0x00004000 /* Rx Good */
238#define Rx_RxPar 0x00002000 /* Rx Parity Error */
239 /* 0x00001000 not use */
240#define Rx_LongErr 0x00000800 /* Rx Long Error */
241#define Rx_Over 0x00000400 /* Rx Overflow */
242#define Rx_CRCErr 0x00000200 /* Rx CRC Error */
243#define Rx_Align 0x00000100 /* Rx Alignment Error */
244#define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
245#define Rx_IntRx 0x00000040 /* Rx Interrupt */
246#define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900248#define Rx_Stat_Mask 0x0000EFC0 /* Rx All Status Mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/* Int_En bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900251#define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
252#define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Ctl Complete Enable */
253#define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
254#define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
255#define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
256#define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
257#define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
258#define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
259#define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
260#define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
261#define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
262#define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
263 /* Exhausted Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265/* Int_Src bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900266#define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
267#define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
268#define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
269#define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
270#define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
271#define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
272#define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
273#define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
274#define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
275#define Int_SWInt 0x00000020 /* 1:Software request & Clear */
276#define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
277#define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
278#define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
279#define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
280#define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/* MD_CA bit asign --------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900283#define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
284#define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
285#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
289 * Descriptors
290 */
291
292/* Frame descripter */
293struct FDesc {
294 volatile __u32 FDNext;
295 volatile __u32 FDSystem;
296 volatile __u32 FDStat;
297 volatile __u32 FDCtl;
298};
299
300/* Buffer descripter */
301struct BDesc {
302 volatile __u32 BuffData;
303 volatile __u32 BDCtl;
304};
305
306#define FD_ALIGN 16
307
308/* Frame Descripter bit asign ---------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900309#define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
310#define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
311#define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900313#define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
314#define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
316#define FD_FrmOpt_Packing 0x04000000 /* Rx only */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900317#define FD_CownsFD 0x80000000 /* FD Controller owner bit */
318#define FD_Next_EOL 0x00000001 /* FD EOL indicator */
319#define FD_BDCnt_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321/* Buffer Descripter bit asign --------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900322#define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
323#define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
324#define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
325#define BD_CownsBD 0x80000000 /* BD Controller owner bit */
326#define BD_RxBDID_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#define BD_RxBDSeqN_SHIFT 24
328
329
330/* Some useful constants. */
331#undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */
332
333#ifdef NO_CHECK_CARRIER
334#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900335 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
336 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#else
338#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900339 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
340 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif
342#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
343 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#define INT_EN_CMD (Int_NRAbtEn | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900345 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
347 Int_STargAbtEn | \
348 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900349#define DMA_CTL_CMD DMA_BURST_SIZE
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900350#define HAVE_DMA_RXALIGN(lp) likely((lp)->chiptype != TC35815CF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352/* Tuning parameters */
353#define DMA_BURST_SIZE 32
354#define TX_THRESHOLD 1024
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900355/* used threshold with packet max byte for low pci transfer ability.*/
356#define TX_THRESHOLD_MAX 1536
357/* setting threshold max value when overrun error occured this count. */
358#define TX_THRESHOLD_KEEP_LIMIT 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900360/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
361#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#define FD_PAGE_NUM 2
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900363#define RX_BUF_NUM 8 /* >= 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#define RX_FD_NUM 250 /* >= 32 */
365#define TX_FD_NUM 128
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900366#define RX_BUF_SIZE PAGE_SIZE
367#else /* TC35815_USE_PACKEDBUFFER */
368#define FD_PAGE_NUM 4
369#define RX_BUF_NUM 128 /* < 256 */
370#define RX_FD_NUM 256 /* >= 32 */
371#define TX_FD_NUM 128
372#if RX_CTL_CMD & Rx_LongEn
373#define RX_BUF_SIZE PAGE_SIZE
374#elif RX_CTL_CMD & Rx_StripCRC
375#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */
376#else
377#define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */
378#endif
379#endif /* TC35815_USE_PACKEDBUFFER */
380#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
381#define NAPI_WEIGHT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383struct TxFD {
384 struct FDesc fd;
385 struct BDesc bd;
386 struct BDesc unused;
387};
388
389struct RxFD {
390 struct FDesc fd;
391 struct BDesc bd[0]; /* variable length */
392};
393
394struct FrFD {
395 struct FDesc fd;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900396 struct BDesc bd[RX_BUF_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397};
398
399
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900400#define tc_readl(addr) ioread32(addr)
401#define tc_writel(d, addr) iowrite32(d, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900403#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
404
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900405/* Information that need to be kept for each controller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406struct tc35815_local {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900407 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700409 struct net_device *dev;
410 struct napi_struct napi;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct {
414 int max_tx_qlen;
415 int tx_ints;
416 int rx_ints;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900417 int tx_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } lstats;
419
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900420 /* Tx control lock. This protects the transmit buffer ring
421 * state along with the "tx full" state of the driver. This
422 * means all netif_queue flow control actions are protected
423 * by this lock as well.
424 */
425 spinlock_t lock;
426
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700427 struct mii_bus *mii_bus;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900428 struct phy_device *phy_dev;
429 int duplex;
430 int speed;
431 int link;
432 struct work_struct restart_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
435 * Transmitting: Batch Mode.
436 * 1 BD in 1 TxFD.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900437 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * 1 circular FD for Free Buffer List.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900439 * RX_BUF_NUM BD in Free Buffer FD.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * One Free Buffer BD has PAGE_SIZE data buffer.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900441 * Or Non-Packing Mode.
442 * 1 circular FD for Free Buffer List.
443 * RX_BUF_NUM BD in Free Buffer FD.
444 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900446 void *fd_buf; /* for TxFD, RxFD, FrFD */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900447 dma_addr_t fd_buf_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct TxFD *tfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900449 unsigned int tfd_start;
450 unsigned int tfd_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct RxFD *rfd_base;
452 struct RxFD *rfd_limit;
453 struct RxFD *rfd_cur;
454 struct FrFD *fbl_ptr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900455#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 unsigned char fbl_curid;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900457 void *data_buf[RX_BUF_NUM]; /* packing */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900458 dma_addr_t data_buf_dma[RX_BUF_NUM];
459 struct {
460 struct sk_buff *skb;
461 dma_addr_t skb_dma;
462 } tx_skbs[TX_FD_NUM];
463#else
464 unsigned int fbl_count;
465 struct {
466 struct sk_buff *skb;
467 dma_addr_t skb_dma;
468 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
469#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900470 u32 msg_enable;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900471 enum tc35815_chiptype chiptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472};
473
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900474static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
475{
476 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
477}
478#ifdef DEBUG
479static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
480{
481 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
482}
483#endif
484#ifdef TC35815_USE_PACKEDBUFFER
485static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
486{
487 int i;
488 for (i = 0; i < RX_BUF_NUM; i++) {
489 if (bus >= lp->data_buf_dma[i] &&
490 bus < lp->data_buf_dma[i] + PAGE_SIZE)
491 return (void *)((u8 *)lp->data_buf[i] +
492 (bus - lp->data_buf_dma[i]));
493 }
494 return NULL;
495}
496
497#define TC35815_DMA_SYNC_ONDEMAND
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900498static void *alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900499{
500#ifdef TC35815_DMA_SYNC_ONDEMAND
501 void *buf;
502 /* pci_map + pci_dma_sync will be more effective than
503 * pci_alloc_consistent on some archs. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900504 buf = (void *)__get_free_page(GFP_ATOMIC);
505 if (!buf)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900506 return NULL;
507 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
508 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700509 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900510 free_page((unsigned long)buf);
511 return NULL;
512 }
513 return buf;
514#else
515 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
516#endif
517}
518
519static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
520{
521#ifdef TC35815_DMA_SYNC_ONDEMAND
522 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
523 free_page((unsigned long)buf);
524#else
525 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
526#endif
527}
528#else /* TC35815_USE_PACKEDBUFFER */
529static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
530 struct pci_dev *hwdev,
531 dma_addr_t *dma_handle)
532{
533 struct sk_buff *skb;
534 skb = dev_alloc_skb(RX_BUF_SIZE);
535 if (!skb)
536 return NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900537 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
538 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700539 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900540 dev_kfree_skb_any(skb);
541 return NULL;
542 }
543 skb_reserve(skb, 2); /* make IP header 4byte aligned */
544 return skb;
545}
546
547static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
548{
549 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
550 PCI_DMA_FROMDEVICE);
551 dev_kfree_skb_any(skb);
552}
553#endif /* TC35815_USE_PACKEDBUFFER */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/* Index to functions, as function prototypes. */
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557static int tc35815_open(struct net_device *dev);
558static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900559static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
560#ifdef TC35815_NAPI
561static int tc35815_rx(struct net_device *dev, int limit);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700562static int tc35815_poll(struct napi_struct *napi, int budget);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900563#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static void tc35815_rx(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static void tc35815_txdone(struct net_device *dev);
567static int tc35815_close(struct net_device *dev);
568static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
569static void tc35815_set_multicast_list(struct net_device *dev);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900570static void tc35815_tx_timeout(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900571static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
572#ifdef CONFIG_NET_POLL_CONTROLLER
573static void tc35815_poll_controller(struct net_device *dev);
574#endif
575static const struct ethtool_ops tc35815_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900577/* Example routines you must write ;->. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900578static void tc35815_chip_reset(struct net_device *dev);
579static void tc35815_chip_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900581#ifdef DEBUG
582static void panic_queues(struct net_device *dev);
583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900585static void tc35815_restart_work(struct work_struct *work);
586
587static int tc_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
588{
589 struct net_device *dev = bus->priv;
590 struct tc35815_regs __iomem *tr =
591 (struct tc35815_regs __iomem *)dev->base_addr;
592 unsigned long timeout = jiffies + 10;
593
594 tc_writel(MD_CA_Busy | (mii_id << 5) | (regnum & 0x1f), &tr->MD_CA);
595 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
596 if (time_after(jiffies, timeout))
597 return -EIO;
598 cpu_relax();
599 }
600 return tc_readl(&tr->MD_Data) & 0xffff;
601}
602
603static int tc_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 val)
604{
605 struct net_device *dev = bus->priv;
606 struct tc35815_regs __iomem *tr =
607 (struct tc35815_regs __iomem *)dev->base_addr;
608 unsigned long timeout = jiffies + 10;
609
610 tc_writel(val, &tr->MD_Data);
611 tc_writel(MD_CA_Busy | MD_CA_Wr | (mii_id << 5) | (regnum & 0x1f),
612 &tr->MD_CA);
613 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
614 if (time_after(jiffies, timeout))
615 return -EIO;
616 cpu_relax();
617 }
618 return 0;
619}
620
621static void tc_handle_link_change(struct net_device *dev)
622{
623 struct tc35815_local *lp = netdev_priv(dev);
624 struct phy_device *phydev = lp->phy_dev;
625 unsigned long flags;
626 int status_change = 0;
627
628 spin_lock_irqsave(&lp->lock, flags);
629 if (phydev->link &&
630 (lp->speed != phydev->speed || lp->duplex != phydev->duplex)) {
631 struct tc35815_regs __iomem *tr =
632 (struct tc35815_regs __iomem *)dev->base_addr;
633 u32 reg;
634
635 reg = tc_readl(&tr->MAC_Ctl);
636 reg |= MAC_HaltReq;
637 tc_writel(reg, &tr->MAC_Ctl);
638 if (phydev->duplex == DUPLEX_FULL)
639 reg |= MAC_FullDup;
640 else
641 reg &= ~MAC_FullDup;
642 tc_writel(reg, &tr->MAC_Ctl);
643 reg &= ~MAC_HaltReq;
644 tc_writel(reg, &tr->MAC_Ctl);
645
646 /*
647 * TX4939 PCFG.SPEEDn bit will be changed on
648 * NETDEV_CHANGE event.
649 */
650
651#if !defined(NO_CHECK_CARRIER) && defined(WORKAROUND_LOSTCAR)
652 /*
653 * WORKAROUND: enable LostCrS only if half duplex
654 * operation.
655 * (TX4939 does not have EnLCarr)
656 */
657 if (phydev->duplex == DUPLEX_HALF &&
658 lp->chiptype != TC35815_TX4939)
659 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr,
660 &tr->Tx_Ctl);
661#endif
662
663 lp->speed = phydev->speed;
664 lp->duplex = phydev->duplex;
665 status_change = 1;
666 }
667
668 if (phydev->link != lp->link) {
669 if (phydev->link) {
670#ifdef WORKAROUND_100HALF_PROMISC
671 /* delayed promiscuous enabling */
672 if (dev->flags & IFF_PROMISC)
673 tc35815_set_multicast_list(dev);
674#endif
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900675 } else {
676 lp->speed = 0;
677 lp->duplex = -1;
678 }
679 lp->link = phydev->link;
680
681 status_change = 1;
682 }
683 spin_unlock_irqrestore(&lp->lock, flags);
684
685 if (status_change && netif_msg_link(lp)) {
686 phy_print_status(phydev);
687#ifdef DEBUG
688 printk(KERN_DEBUG
689 "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
690 dev->name,
691 phy_read(phydev, MII_BMCR),
692 phy_read(phydev, MII_BMSR),
693 phy_read(phydev, MII_LPA));
694#endif
695 }
696}
697
698static int tc_mii_probe(struct net_device *dev)
699{
700 struct tc35815_local *lp = netdev_priv(dev);
701 struct phy_device *phydev = NULL;
702 int phy_addr;
703 u32 dropmask;
704
705 /* find the first phy */
706 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700707 if (lp->mii_bus->phy_map[phy_addr]) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900708 if (phydev) {
709 printk(KERN_ERR "%s: multiple PHYs found\n",
710 dev->name);
711 return -EINVAL;
712 }
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700713 phydev = lp->mii_bus->phy_map[phy_addr];
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900714 break;
715 }
716 }
717
718 if (!phydev) {
719 printk(KERN_ERR "%s: no PHY found\n", dev->name);
720 return -ENODEV;
721 }
722
723 /* attach the mac to the phy */
724 phydev = phy_connect(dev, phydev->dev.bus_id,
725 &tc_handle_link_change, 0,
726 lp->chiptype == TC35815_TX4939 ?
727 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII);
728 if (IS_ERR(phydev)) {
729 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
730 return PTR_ERR(phydev);
731 }
732 printk(KERN_INFO "%s: attached PHY driver [%s] "
733 "(mii_bus:phy_addr=%s, id=%x)\n",
734 dev->name, phydev->drv->name, phydev->dev.bus_id,
735 phydev->phy_id);
736
737 /* mask with MAC supported features */
738 phydev->supported &= PHY_BASIC_FEATURES;
739 dropmask = 0;
740 if (options.speed == 10)
741 dropmask |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
742 else if (options.speed == 100)
743 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
744 if (options.duplex == 1)
745 dropmask |= SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full;
746 else if (options.duplex == 2)
747 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_100baseT_Half;
748 phydev->supported &= ~dropmask;
749 phydev->advertising = phydev->supported;
750
751 lp->link = 0;
752 lp->speed = 0;
753 lp->duplex = -1;
754 lp->phy_dev = phydev;
755
756 return 0;
757}
758
759static int tc_mii_init(struct net_device *dev)
760{
761 struct tc35815_local *lp = netdev_priv(dev);
762 int err;
763 int i;
764
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700765 lp->mii_bus = mdiobus_alloc();
766 if (lp->mii_bus == NULL) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900767 err = -ENOMEM;
768 goto err_out;
769 }
770
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700771 lp->mii_bus->name = "tc35815_mii_bus";
772 lp->mii_bus->read = tc_mdio_read;
773 lp->mii_bus->write = tc_mdio_write;
774 snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x",
775 (lp->pci_dev->bus->number << 8) | lp->pci_dev->devfn);
776 lp->mii_bus->priv = dev;
777 lp->mii_bus->parent = &lp->pci_dev->dev;
778 lp->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
779 if (!lp->mii_bus->irq) {
780 err = -ENOMEM;
781 goto err_out_free_mii_bus;
782 }
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900783
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700784 for (i = 0; i < PHY_MAX_ADDR; i++)
785 lp->mii_bus->irq[i] = PHY_POLL;
786
787 err = mdiobus_register(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900788 if (err)
789 goto err_out_free_mdio_irq;
790 err = tc_mii_probe(dev);
791 if (err)
792 goto err_out_unregister_bus;
793 return 0;
794
795err_out_unregister_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700796 mdiobus_unregister(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900797err_out_free_mdio_irq:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700798 kfree(lp->mii_bus->irq);
Adrian Bunk51cf7562008-10-12 21:01:53 -0700799err_out_free_mii_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700800 mdiobus_free(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900801err_out:
802 return err;
803}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900805#ifdef CONFIG_CPU_TX49XX
806/*
807 * Find a platform_device providing a MAC address. The platform code
808 * should provide a "tc35815-mac" device with a MAC address in its
809 * platform_data.
810 */
811static int __devinit tc35815_mac_match(struct device *dev, void *data)
812{
813 struct platform_device *plat_dev = to_platform_device(dev);
814 struct pci_dev *pci_dev = data;
Atsushi Nemoto06675e62008-01-19 01:15:52 +0900815 unsigned int id = pci_dev->irq;
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900816 return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
817}
818
819static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
820{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900821 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900822 struct device *pd = bus_find_device(&platform_bus_type, NULL,
823 lp->pci_dev, tc35815_mac_match);
824 if (pd) {
825 if (pd->platform_data)
826 memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
827 put_device(pd);
828 return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
829 }
830 return -ENODEV;
831}
832#else
Yoichi Yuasa308a9062007-07-18 11:13:42 +0900833static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900834{
835 return -ENODEV;
836}
837#endif
838
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900839static int __devinit tc35815_init_dev_addr(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900841 struct tc35815_regs __iomem *tr =
842 (struct tc35815_regs __iomem *)dev->base_addr;
843 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
846 ;
847 for (i = 0; i < 6; i += 2) {
848 unsigned short data;
849 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
850 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
851 ;
852 data = tc_readl(&tr->PROM_Data);
853 dev->dev_addr[i] = data & 0xff;
854 dev->dev_addr[i+1] = data >> 8;
855 }
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900856 if (!is_valid_ether_addr(dev->dev_addr))
857 return tc35815_read_plat_dev_addr(dev);
858 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900859}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900861static int __devinit tc35815_init_one(struct pci_dev *pdev,
862 const struct pci_device_id *ent)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900863{
864 void __iomem *ioaddr = NULL;
865 struct net_device *dev;
866 struct tc35815_local *lp;
867 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900869 static int printed_version;
870 if (!printed_version++) {
871 printk(version);
872 dev_printk(KERN_DEBUG, &pdev->dev,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900873 "speed:%d duplex:%d\n",
874 options.speed, options.duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900877 if (!pdev->irq) {
878 dev_warn(&pdev->dev, "no IRQ assigned.\n");
879 return -ENODEV;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900882 /* dev zeroed in alloc_etherdev */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900883 dev = alloc_etherdev(sizeof(*lp));
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900884 if (dev == NULL) {
885 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
886 return -ENOMEM;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 SET_NETDEV_DEV(dev, &pdev->dev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900889 lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700890 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900892 /* enable device (incl. PCI PM wakeup), and bus-mastering */
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900893 rc = pcim_enable_device(pdev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900894 if (rc)
895 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900896 rc = pcim_iomap_regions(pdev, 1 << 1, MODNAME);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900897 if (rc)
898 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900899 pci_set_master(pdev);
900 ioaddr = pcim_iomap_table(pdev)[1];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900901
902 /* Initialize the device structure. */
903 dev->open = tc35815_open;
904 dev->hard_start_xmit = tc35815_send_packet;
905 dev->stop = tc35815_close;
906 dev->get_stats = tc35815_get_stats;
907 dev->set_multicast_list = tc35815_set_multicast_list;
908 dev->do_ioctl = tc35815_ioctl;
909 dev->ethtool_ops = &tc35815_ethtool_ops;
910 dev->tx_timeout = tc35815_tx_timeout;
911 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
912#ifdef TC35815_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700913 netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900914#endif
915#ifdef CONFIG_NET_POLL_CONTROLLER
916 dev->poll_controller = tc35815_poll_controller;
917#endif
918
919 dev->irq = pdev->irq;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900920 dev->base_addr = (unsigned long)ioaddr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900921
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900922 INIT_WORK(&lp->restart_work, tc35815_restart_work);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900923 spin_lock_init(&lp->lock);
924 lp->pci_dev = pdev;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900925 lp->chiptype = ent->driver_data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900926
927 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
928 pci_set_drvdata(pdev, dev);
929
930 /* Soft reset the chip. */
931 tc35815_chip_reset(dev);
932
933 /* Retrieve the ethernet address. */
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900934 if (tc35815_init_dev_addr(dev)) {
935 dev_warn(&pdev->dev, "not valid ether addr\n");
936 random_ether_addr(dev->dev_addr);
937 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900938
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900939 rc = register_netdev(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900940 if (rc)
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900941 goto err_out;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900942
943 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Johannes Berge1749612008-10-27 15:59:26 -0700944 printk(KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900945 dev->name,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900946 chip_info[ent->driver_data].name,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900947 dev->base_addr,
Johannes Berge1749612008-10-27 15:59:26 -0700948 dev->dev_addr,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900949 dev->irq);
950
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900951 rc = tc_mii_init(dev);
952 if (rc)
953 goto err_out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 return 0;
956
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900957err_out_unregister:
958 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959err_out:
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900960 free_netdev(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900961 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900965static void __devexit tc35815_remove_one(struct pci_dev *pdev)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900966{
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900967 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900968 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900969
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900970 phy_disconnect(lp->phy_dev);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700971 mdiobus_unregister(lp->mii_bus);
972 kfree(lp->mii_bus->irq);
973 mdiobus_free(lp->mii_bus);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900974 unregister_netdev(dev);
975 free_netdev(dev);
976 pci_set_drvdata(pdev, NULL);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static int
980tc35815_init_queues(struct net_device *dev)
981{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900982 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int i;
984 unsigned long fd_addr;
985
986 if (!lp->fd_buf) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900987 BUG_ON(sizeof(struct FDesc) +
988 sizeof(struct BDesc) * RX_BUF_NUM +
989 sizeof(struct FDesc) * RX_FD_NUM +
990 sizeof(struct TxFD) * TX_FD_NUM >
991 PAGE_SIZE * FD_PAGE_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900993 lp->fd_buf = pci_alloc_consistent(lp->pci_dev,
994 PAGE_SIZE * FD_PAGE_NUM,
995 &lp->fd_buf_dma);
996 if (!lp->fd_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -ENOMEM;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900998 for (i = 0; i < RX_BUF_NUM; i++) {
999#ifdef TC35815_USE_PACKEDBUFFER
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001000 lp->data_buf[i] =
1001 alloc_rxbuf_page(lp->pci_dev,
1002 &lp->data_buf_dma[i]);
1003 if (!lp->data_buf[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 while (--i >= 0) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001005 free_rxbuf_page(lp->pci_dev,
1006 lp->data_buf[i],
1007 lp->data_buf_dma[i]);
1008 lp->data_buf[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001010 pci_free_consistent(lp->pci_dev,
1011 PAGE_SIZE * FD_PAGE_NUM,
1012 lp->fd_buf,
1013 lp->fd_buf_dma);
1014 lp->fd_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return -ENOMEM;
1016 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001017#else
1018 lp->rx_skbs[i].skb =
1019 alloc_rxbuf_skb(dev, lp->pci_dev,
1020 &lp->rx_skbs[i].skb_dma);
1021 if (!lp->rx_skbs[i].skb) {
1022 while (--i >= 0) {
1023 free_rxbuf_skb(lp->pci_dev,
1024 lp->rx_skbs[i].skb,
1025 lp->rx_skbs[i].skb_dma);
1026 lp->rx_skbs[i].skb = NULL;
1027 }
1028 pci_free_consistent(lp->pci_dev,
1029 PAGE_SIZE * FD_PAGE_NUM,
1030 lp->fd_buf,
1031 lp->fd_buf_dma);
1032 lp->fd_buf = NULL;
1033 return -ENOMEM;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#endif
1036 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001037 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
1038 dev->name, lp->fd_buf);
1039#ifdef TC35815_USE_PACKEDBUFFER
1040 printk(" DataBuf");
1041 for (i = 0; i < RX_BUF_NUM; i++)
1042 printk(" %p", lp->data_buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001044 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 } else {
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001046 for (i = 0; i < FD_PAGE_NUM; i++)
1047 clear_page((void *)((unsigned long)lp->fd_buf +
1048 i * PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 fd_addr = (unsigned long)lp->fd_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* Free Descriptors (for Receive) */
1053 lp->rfd_base = (struct RxFD *)fd_addr;
1054 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001055 for (i = 0; i < RX_FD_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 lp->rfd_cur = lp->rfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001058 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 /* Transmit Descriptors */
1061 lp->tfd_base = (struct TxFD *)fd_addr;
1062 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
1063 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001064 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
1065 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
1067 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001068 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 -07001069 lp->tfd_start = 0;
1070 lp->tfd_end = 0;
1071
1072 /* Buffer List (for Receive) */
1073 lp->fbl_ptr = (struct FrFD *)fd_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001074 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
1075 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
1076#ifndef TC35815_USE_PACKEDBUFFER
1077 /*
1078 * move all allocated skbs to head of rx_skbs[] array.
1079 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
1080 * tc35815_rx() had failed.
1081 */
1082 lp->fbl_count = 0;
1083 for (i = 0; i < RX_BUF_NUM; i++) {
1084 if (lp->rx_skbs[i].skb) {
1085 if (i != lp->fbl_count) {
1086 lp->rx_skbs[lp->fbl_count].skb =
1087 lp->rx_skbs[i].skb;
1088 lp->rx_skbs[lp->fbl_count].skb_dma =
1089 lp->rx_skbs[i].skb_dma;
1090 }
1091 lp->fbl_count++;
1092 }
1093 }
1094#endif
1095 for (i = 0; i < RX_BUF_NUM; i++) {
1096#ifdef TC35815_USE_PACKEDBUFFER
1097 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
1098#else
1099 if (i >= lp->fbl_count) {
1100 lp->fbl_ptr->bd[i].BuffData = 0;
1101 lp->fbl_ptr->bd[i].BDCtl = 0;
1102 continue;
1103 }
1104 lp->fbl_ptr->bd[i].BuffData =
1105 cpu_to_le32(lp->rx_skbs[i].skb_dma);
1106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* BDID is index of FrFD.bd[] */
1108 lp->fbl_ptr->bd[i].BDCtl =
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001109 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
1110 RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001112#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 lp->fbl_curid = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001116 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
1117 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119}
1120
1121static void
1122tc35815_clear_queues(struct net_device *dev)
1123{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001124 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int i;
1126
1127 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001128 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1129 struct sk_buff *skb =
1130 fdsystem != 0xffffffff ?
1131 lp->tx_skbs[fdsystem].skb : NULL;
1132#ifdef DEBUG
1133 if (lp->tx_skbs[i].skb != skb) {
1134 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1135 panic_queues(dev);
1136 }
1137#else
1138 BUG_ON(lp->tx_skbs[i].skb != skb);
1139#endif
1140 if (skb) {
1141 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1142 lp->tx_skbs[i].skb = NULL;
1143 lp->tx_skbs[i].skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001145 }
1146 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
1149 tc35815_init_queues(dev);
1150}
1151
1152static void
1153tc35815_free_queues(struct net_device *dev)
1154{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001155 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int i;
1157
1158 if (lp->tfd_base) {
1159 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001160 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1161 struct sk_buff *skb =
1162 fdsystem != 0xffffffff ?
1163 lp->tx_skbs[fdsystem].skb : NULL;
1164#ifdef DEBUG
1165 if (lp->tx_skbs[i].skb != skb) {
1166 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1167 panic_queues(dev);
1168 }
1169#else
1170 BUG_ON(lp->tx_skbs[i].skb != skb);
1171#endif
1172 if (skb) {
1173 dev_kfree_skb(skb);
1174 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1175 lp->tx_skbs[i].skb = NULL;
1176 lp->tx_skbs[i].skb_dma = 0;
1177 }
1178 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 }
1181
1182 lp->rfd_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 lp->rfd_limit = NULL;
1184 lp->rfd_cur = NULL;
1185 lp->fbl_ptr = NULL;
1186
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001187 for (i = 0; i < RX_BUF_NUM; i++) {
1188#ifdef TC35815_USE_PACKEDBUFFER
1189 if (lp->data_buf[i]) {
1190 free_rxbuf_page(lp->pci_dev,
1191 lp->data_buf[i], lp->data_buf_dma[i]);
1192 lp->data_buf[i] = NULL;
1193 }
1194#else
1195 if (lp->rx_skbs[i].skb) {
1196 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1197 lp->rx_skbs[i].skb_dma);
1198 lp->rx_skbs[i].skb = NULL;
1199 }
1200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001202 if (lp->fd_buf) {
1203 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1204 lp->fd_buf, lp->fd_buf_dma);
1205 lp->fd_buf = NULL;
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static void
1210dump_txfd(struct TxFD *fd)
1211{
1212 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1213 le32_to_cpu(fd->fd.FDNext),
1214 le32_to_cpu(fd->fd.FDSystem),
1215 le32_to_cpu(fd->fd.FDStat),
1216 le32_to_cpu(fd->fd.FDCtl));
1217 printk("BD: ");
1218 printk(" %08x %08x",
1219 le32_to_cpu(fd->bd.BuffData),
1220 le32_to_cpu(fd->bd.BDCtl));
1221 printk("\n");
1222}
1223
1224static int
1225dump_rxfd(struct RxFD *fd)
1226{
1227 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1228 if (bd_count > 8)
1229 bd_count = 8;
1230 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1231 le32_to_cpu(fd->fd.FDNext),
1232 le32_to_cpu(fd->fd.FDSystem),
1233 le32_to_cpu(fd->fd.FDStat),
1234 le32_to_cpu(fd->fd.FDCtl));
1235 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 printk("BD: ");
1238 for (i = 0; i < bd_count; i++)
1239 printk(" %08x %08x",
1240 le32_to_cpu(fd->bd[i].BuffData),
1241 le32_to_cpu(fd->bd[i].BDCtl));
1242 printk("\n");
1243 return bd_count;
1244}
1245
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001246#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247static void
1248dump_frfd(struct FrFD *fd)
1249{
1250 int i;
1251 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1252 le32_to_cpu(fd->fd.FDNext),
1253 le32_to_cpu(fd->fd.FDSystem),
1254 le32_to_cpu(fd->fd.FDStat),
1255 le32_to_cpu(fd->fd.FDCtl));
1256 printk("BD: ");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001257 for (i = 0; i < RX_BUF_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 printk(" %08x %08x",
1259 le32_to_cpu(fd->bd[i].BuffData),
1260 le32_to_cpu(fd->bd[i].BDCtl));
1261 printk("\n");
1262}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001265#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266static void
1267panic_queues(struct net_device *dev)
1268{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001269 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int i;
1271
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001272 printk("TxFD base %p, start %u, end %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1274 printk("RxFD base %p limit %p cur %p\n",
1275 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1276 printk("FrFD %p\n", lp->fbl_ptr);
1277 for (i = 0; i < TX_FD_NUM; i++)
1278 dump_txfd(&lp->tfd_base[i]);
1279 for (i = 0; i < RX_FD_NUM; i++) {
1280 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1281 i += (bd_count + 1) / 2; /* skip BDs */
1282 }
1283 dump_frfd(lp->fbl_ptr);
1284 panic("%s: Illegal queue state.", dev->name);
1285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#endif
1287
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001288static void print_eth(const u8 *add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001290 printk(KERN_DEBUG "print_eth(%p)\n", add);
Johannes Berge1749612008-10-27 15:59:26 -07001291 printk(KERN_DEBUG " %pM => %pM : %02x%02x\n",
1292 add + 6, add, add[12], add[13]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001295static int tc35815_tx_full(struct net_device *dev)
1296{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001297 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001298 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1299}
1300
1301static void tc35815_restart(struct net_device *dev)
1302{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001303 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001304
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001305 if (lp->phy_dev) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001306 int timeout;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001307
1308 phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001309 timeout = 100;
1310 while (--timeout) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001311 if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET))
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001312 break;
1313 udelay(1);
1314 }
1315 if (!timeout)
1316 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1317 }
1318
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001319 spin_lock_irq(&lp->lock);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001320 tc35815_chip_reset(dev);
1321 tc35815_clear_queues(dev);
1322 tc35815_chip_init(dev);
1323 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1324 tc35815_set_multicast_list(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001325 spin_unlock_irq(&lp->lock);
1326
1327 netif_wake_queue(dev);
1328}
1329
1330static void tc35815_restart_work(struct work_struct *work)
1331{
1332 struct tc35815_local *lp =
1333 container_of(work, struct tc35815_local, restart_work);
1334 struct net_device *dev = lp->dev;
1335
1336 tc35815_restart(dev);
1337}
1338
1339static void tc35815_schedule_restart(struct net_device *dev)
1340{
1341 struct tc35815_local *lp = netdev_priv(dev);
1342 struct tc35815_regs __iomem *tr =
1343 (struct tc35815_regs __iomem *)dev->base_addr;
1344
1345 /* disable interrupts */
1346 tc_writel(0, &tr->Int_En);
1347 tc_writel(tc_readl(&tr->DMA_Ctl) | DMA_IntMask, &tr->DMA_Ctl);
1348 schedule_work(&lp->restart_work);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001349}
1350
1351static void tc35815_tx_timeout(struct net_device *dev)
1352{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001353 struct tc35815_regs __iomem *tr =
1354 (struct tc35815_regs __iomem *)dev->base_addr;
1355
1356 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1357 dev->name, tc_readl(&tr->Tx_Stat));
1358
1359 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001360 tc35815_schedule_restart(dev);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001361 dev->stats.tx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/*
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001365 * Open/initialize the controller. This is called (in the current kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * sometime after booting when the 'ifconfig' program is run.
1367 *
1368 * This routine should set everything up anew at each open, even
1369 * registers that "should" only need to be set once at boot, so that
1370 * there is non-reboot way to recover if something goes wrong.
1371 */
1372static int
1373tc35815_open(struct net_device *dev)
1374{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001375 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /*
1378 * This is used if the interrupt line can turned off (shared).
1379 * See 3c503.c for an example of selecting the IRQ at config-time.
1380 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001381 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED,
1382 dev->name, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 tc35815_chip_reset(dev);
1386
1387 if (tc35815_init_queues(dev) != 0) {
1388 free_irq(dev->irq, dev);
1389 return -EAGAIN;
1390 }
1391
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001392#ifdef TC35815_NAPI
1393 napi_enable(&lp->napi);
1394#endif
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* Reset the hardware here. Don't forget to set the station address. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001397 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 tc35815_chip_init(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001399 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Atsushi Nemoto59524a32008-06-25 11:41:01 +09001401 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001402 /* schedule a link state check */
1403 phy_start(lp->phy_dev);
1404
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001405 /* We are now ready to accept transmit requeusts from
1406 * the queueing layer of the networking.
1407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 netif_start_queue(dev);
1409
1410 return 0;
1411}
1412
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001413/* This will only be invoked if your driver is _not_ in XOFF state.
1414 * What this means is that you need not check it, and that this
1415 * invariant will hold if you make sure that the netif_*_queue()
1416 * calls are done at the proper times.
1417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1419{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001420 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001421 struct TxFD *txfd;
1422 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001424 /* If some error occurs while trying to transmit this
1425 * packet, you should return '1' from this function.
1426 * In such a case you _may not_ do anything to the
1427 * SKB, it is still owned by the network queueing
1428 * layer when an error is returned. This means you
1429 * may not modify any SKB fields, you may not free
1430 * the SKB, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001433 /* This is the most common case for modern hardware.
1434 * The spinlock protects this code from the TX complete
1435 * hardware interrupt handler. Queue flow control is
1436 * thus managed under this lock as well.
1437 */
1438 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001440 /* failsafe... (handle txdone now if half of FDs are used) */
1441 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1442 TX_FD_NUM / 2)
1443 tc35815_txdone(dev);
1444
1445 if (netif_msg_pktdata(lp))
1446 print_eth(skb->data);
1447#ifdef DEBUG
1448 if (lp->tx_skbs[lp->tfd_start].skb) {
1449 printk("%s: tx_skbs conflict.\n", dev->name);
1450 panic_queues(dev);
1451 }
1452#else
1453 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001455 lp->tx_skbs[lp->tfd_start].skb = skb;
1456 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 -07001457
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001458 /*add to ring */
1459 txfd = &lp->tfd_base[lp->tfd_start];
1460 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1461 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1462 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1463 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001465 if (lp->tfd_start == lp->tfd_end) {
1466 struct tc35815_regs __iomem *tr =
1467 (struct tc35815_regs __iomem *)dev->base_addr;
1468 /* Start DMA Transmitter. */
1469 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#ifdef GATHER_TXINT
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001471 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001473 if (netif_msg_tx_queued(lp)) {
1474 printk("%s: starting TxFD.\n", dev->name);
1475 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001477 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1478 } else {
1479 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1480 if (netif_msg_tx_queued(lp)) {
1481 printk("%s: queueing TxFD.\n", dev->name);
1482 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001484 }
1485 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1486
1487 dev->trans_start = jiffies;
1488
1489 /* If we just used up the very last entry in the
1490 * TX ring on this device, tell the queueing
1491 * layer to send no more.
1492 */
1493 if (tc35815_tx_full(dev)) {
1494 if (netif_msg_tx_queued(lp))
1495 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1496 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001499 /* When the TX completion hw interrupt arrives, this
1500 * is when the transmit statistics are updated.
1501 */
1502
1503 spin_unlock_irqrestore(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
1505}
1506
1507#define FATAL_ERROR_INT \
1508 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001509static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 static int count;
1512 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1513 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (status & Int_IntPCI)
1515 printk(" IntPCI");
1516 if (status & Int_DmParErr)
1517 printk(" DmParErr");
1518 if (status & Int_IntNRAbt)
1519 printk(" IntNRAbt");
1520 printk("\n");
1521 if (count++ > 100)
1522 panic("%s: Too many fatal errors.", dev->name);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001523 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001525 tc35815_schedule_restart(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001526}
1527
1528#ifdef TC35815_NAPI
1529static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
1530#else
1531static int tc35815_do_interrupt(struct net_device *dev, u32 status)
1532#endif
1533{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001534 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001535 struct tc35815_regs __iomem *tr =
1536 (struct tc35815_regs __iomem *)dev->base_addr;
1537 int ret = -1;
1538
1539 /* Fatal errors... */
1540 if (status & FATAL_ERROR_INT) {
1541 tc35815_fatal_error_interrupt(dev, status);
1542 return 0;
1543 }
1544 /* recoverable errors */
1545 if (status & Int_IntFDAEx) {
1546 /* disable FDAEx int. (until we make rooms...) */
1547 tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
1548 printk(KERN_WARNING
1549 "%s: Free Descriptor Area Exhausted (%#x).\n",
1550 dev->name, status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001551 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001552 ret = 0;
1553 }
1554 if (status & Int_IntBLEx) {
1555 /* disable BLEx int. (until we make rooms...) */
1556 tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
1557 printk(KERN_WARNING
1558 "%s: Buffer List Exhausted (%#x).\n",
1559 dev->name, status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001560 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001561 ret = 0;
1562 }
1563 if (status & Int_IntExBD) {
1564 printk(KERN_WARNING
1565 "%s: Excessive Buffer Descriptiors (%#x).\n",
1566 dev->name, status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001567 dev->stats.rx_length_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001568 ret = 0;
1569 }
1570
1571 /* normal notification */
1572 if (status & Int_IntMacRx) {
1573 /* Got a packet(s). */
1574#ifdef TC35815_NAPI
1575 ret = tc35815_rx(dev, limit);
1576#else
1577 tc35815_rx(dev);
1578 ret = 0;
1579#endif
1580 lp->lstats.rx_ints++;
1581 }
1582 if (status & Int_IntMacTx) {
1583 /* Transmit complete. */
1584 lp->lstats.tx_ints++;
1585 tc35815_txdone(dev);
1586 netif_wake_queue(dev);
1587 ret = 0;
1588 }
1589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
1592/*
1593 * The typical workload of the driver:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001594 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
David Howells7d12e782006-10-05 14:55:46 +01001596static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct net_device *dev = dev_id;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001599 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001600 struct tc35815_regs __iomem *tr =
1601 (struct tc35815_regs __iomem *)dev->base_addr;
1602#ifdef TC35815_NAPI
1603 u32 dmactl = tc_readl(&tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001605 if (!(dmactl & DMA_IntMask)) {
1606 /* disable interrupts */
1607 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001608 if (netif_rx_schedule_prep(dev, &lp->napi))
1609 __netif_rx_schedule(dev, &lp->napi);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001610 else {
1611 printk(KERN_ERR "%s: interrupt taken in poll\n",
1612 dev->name);
1613 BUG();
1614 }
1615 (void)tc_readl(&tr->Int_Src); /* flush */
1616 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001618 return IRQ_NONE;
1619#else
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001620 int handled;
1621 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001623 spin_lock(&lp->lock);
1624 status = tc_readl(&tr->Int_Src);
1625 tc_writel(status, &tr->Int_Src); /* write to clear */
1626 handled = tc35815_do_interrupt(dev, status);
1627 (void)tc_readl(&tr->Int_Src); /* flush */
1628 spin_unlock(&lp->lock);
1629 return IRQ_RETVAL(handled >= 0);
1630#endif /* TC35815_NAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001633#ifdef CONFIG_NET_POLL_CONTROLLER
1634static void tc35815_poll_controller(struct net_device *dev)
1635{
1636 disable_irq(dev->irq);
1637 tc35815_interrupt(dev->irq, dev);
1638 enable_irq(dev->irq);
1639}
1640#endif
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/* We have a good packet(s), get it/them out of the buffers. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001643#ifdef TC35815_NAPI
1644static int
1645tc35815_rx(struct net_device *dev, int limit)
1646#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static void
1648tc35815_rx(struct net_device *dev)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001649#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001651 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 unsigned int fdctl;
1653 int i;
1654 int buf_free_count = 0;
1655 int fd_free_count = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001656#ifdef TC35815_NAPI
1657 int received = 0;
1658#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1661 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1662 int pkt_len = fdctl & FD_FDLength_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001664#ifdef DEBUG
1665 struct RxFD *next_rfd;
1666#endif
1667#if (RX_CTL_CMD & Rx_StripCRC) == 0
1668 pkt_len -= 4;
1669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001671 if (netif_msg_rx_status(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 dump_rxfd(lp->rfd_cur);
1673 if (status & Rx_Good) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct sk_buff *skb;
1675 unsigned char *data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001676 int cur_bd;
1677#ifdef TC35815_USE_PACKEDBUFFER
1678 int offset;
1679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001681#ifdef TC35815_NAPI
1682 if (--limit < 0)
1683 break;
1684#endif
1685#ifdef TC35815_USE_PACKEDBUFFER
1686 BUG_ON(bd_count > 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */
1688 if (skb == NULL) {
1689 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
1690 dev->name);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001691 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693 }
1694 skb_reserve(skb, 2); /* 16 bit alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 data = skb_put(skb, pkt_len);
1697
1698 /* copy from receive buffer */
1699 cur_bd = 0;
1700 offset = 0;
1701 while (offset < pkt_len && cur_bd < bd_count) {
1702 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1703 BD_BuffLength_MASK;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001704 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1705 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1706 if (offset + len > pkt_len)
1707 len = pkt_len - offset;
1708#ifdef TC35815_DMA_SYNC_ONDEMAND
1709 pci_dma_sync_single_for_cpu(lp->pci_dev,
1710 dma, len,
1711 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#endif
1713 memcpy(data + offset, rxbuf, len);
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +09001714#ifdef TC35815_DMA_SYNC_ONDEMAND
1715 pci_dma_sync_single_for_device(lp->pci_dev,
1716 dma, len,
1717 PCI_DMA_FROMDEVICE);
1718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 offset += len;
1720 cur_bd++;
1721 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001722#else /* TC35815_USE_PACKEDBUFFER */
1723 BUG_ON(bd_count > 1);
1724 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1725 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1726#ifdef DEBUG
1727 if (cur_bd >= RX_BUF_NUM) {
1728 printk("%s: invalid BDID.\n", dev->name);
1729 panic_queues(dev);
1730 }
1731 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1732 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1733 if (!lp->rx_skbs[cur_bd].skb) {
1734 printk("%s: NULL skb.\n", dev->name);
1735 panic_queues(dev);
1736 }
1737#else
1738 BUG_ON(cur_bd >= RX_BUF_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001740 skb = lp->rx_skbs[cur_bd].skb;
1741 prefetch(skb->data);
1742 lp->rx_skbs[cur_bd].skb = NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001743 pci_unmap_single(lp->pci_dev,
1744 lp->rx_skbs[cur_bd].skb_dma,
1745 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1746 if (!HAVE_DMA_RXALIGN(lp))
1747 memmove(skb->data, skb->data - 2, pkt_len);
1748 data = skb_put(skb, pkt_len);
1749#endif /* TC35815_USE_PACKEDBUFFER */
1750 if (netif_msg_pktdata(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 print_eth(data);
1752 skb->protocol = eth_type_trans(skb, dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001753#ifdef TC35815_NAPI
1754 netif_receive_skb(skb);
1755 received++;
1756#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 netif_rx(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001758#endif
1759 dev->last_rx = jiffies;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001760 dev->stats.rx_packets++;
1761 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 } else {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001763 dev->stats.rx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001764 printk(KERN_DEBUG "%s: Rx error (status %x)\n",
1765 dev->name, status & Rx_Stat_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1767 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1768 status &= ~(Rx_LongErr|Rx_CRCErr);
1769 status |= Rx_Over;
1770 }
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001771 if (status & Rx_LongErr)
1772 dev->stats.rx_length_errors++;
1773 if (status & Rx_Over)
1774 dev->stats.rx_fifo_errors++;
1775 if (status & Rx_CRCErr)
1776 dev->stats.rx_crc_errors++;
1777 if (status & Rx_Align)
1778 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
1781 if (bd_count > 0) {
1782 /* put Free Buffer back to controller */
1783 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1784 unsigned char id =
1785 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001786#ifdef DEBUG
1787 if (id >= RX_BUF_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 printk("%s: invalid BDID.\n", dev->name);
1789 panic_queues(dev);
1790 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001791#else
1792 BUG_ON(id >= RX_BUF_NUM);
1793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* free old buffers */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001795#ifdef TC35815_USE_PACKEDBUFFER
1796 while (lp->fbl_curid != id)
1797#else
Atsushi Nemotoccc57aa2008-06-26 17:14:15 +09001798 lp->fbl_count--;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001799 while (lp->fbl_count < RX_BUF_NUM)
1800#endif
1801 {
1802#ifdef TC35815_USE_PACKEDBUFFER
1803 unsigned char curid = lp->fbl_curid;
1804#else
1805 unsigned char curid =
1806 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1807#endif
1808 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1809#ifdef DEBUG
1810 bdctl = le32_to_cpu(bd->BDCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (bdctl & BD_CownsBD) {
1812 printk("%s: Freeing invalid BD.\n",
1813 dev->name);
1814 panic_queues(dev);
1815 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001816#endif
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001817 /* pass BD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001818#ifndef TC35815_USE_PACKEDBUFFER
1819 if (!lp->rx_skbs[curid].skb) {
1820 lp->rx_skbs[curid].skb =
1821 alloc_rxbuf_skb(dev,
1822 lp->pci_dev,
1823 &lp->rx_skbs[curid].skb_dma);
1824 if (!lp->rx_skbs[curid].skb)
1825 break; /* try on next reception */
1826 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1827 }
1828#endif /* TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /* Note: BDLength was modified by chip. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001830 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1831 (curid << BD_RxBDID_SHIFT) |
1832 RX_BUF_SIZE);
1833#ifdef TC35815_USE_PACKEDBUFFER
1834 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1835 if (netif_msg_rx_status(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 printk("%s: Entering new FBD %d\n",
1837 dev->name, lp->fbl_curid);
1838 dump_frfd(lp->fbl_ptr);
1839 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001840#else
1841 lp->fbl_count++;
1842#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 buf_free_count++;
1844 }
1845 }
1846
1847 /* put RxFD back to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001848#ifdef DEBUG
1849 next_rfd = fd_bus_to_virt(lp,
1850 le32_to_cpu(lp->rfd_cur->fd.FDNext));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1852 printk("%s: RxFD FDNext invalid.\n", dev->name);
1853 panic_queues(dev);
1854 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001857 /* pass FD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001858#ifdef DEBUG
1859 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1860#else
1861 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1862#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1864 lp->rfd_cur++;
1865 fd_free_count++;
1866 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001867 if (lp->rfd_cur > lp->rfd_limit)
1868 lp->rfd_cur = lp->rfd_base;
1869#ifdef DEBUG
1870 if (lp->rfd_cur != next_rfd)
1871 printk("rfd_cur = %p, next_rfd %p\n",
1872 lp->rfd_cur, next_rfd);
1873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 /* re-enable BL/FDA Exhaust interrupts. */
1877 if (fd_free_count) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001878 struct tc35815_regs __iomem *tr =
1879 (struct tc35815_regs __iomem *)dev->base_addr;
1880 u32 en, en_old = tc_readl(&tr->Int_En);
1881 en = en_old | Int_FDAExEn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (buf_free_count)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001883 en |= Int_BLExEn;
1884 if (en != en_old)
1885 tc_writel(en, &tr->Int_En);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001887#ifdef TC35815_NAPI
1888 return received;
1889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001892#ifdef TC35815_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001893static int tc35815_poll(struct napi_struct *napi, int budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001894{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001895 struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
1896 struct net_device *dev = lp->dev;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001897 struct tc35815_regs __iomem *tr =
1898 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001899 int received = 0, handled;
1900 u32 status;
1901
1902 spin_lock(&lp->lock);
1903 status = tc_readl(&tr->Int_Src);
1904 do {
1905 tc_writel(status, &tr->Int_Src); /* write to clear */
1906
1907 handled = tc35815_do_interrupt(dev, status, limit);
1908 if (handled >= 0) {
1909 received += handled;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001910 if (received >= budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001911 break;
1912 }
1913 status = tc_readl(&tr->Int_Src);
1914 } while (status);
1915 spin_unlock(&lp->lock);
1916
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001917 if (received < budget) {
1918 netif_rx_complete(dev, napi);
1919 /* enable interrupts */
1920 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1921 }
1922 return received;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001923}
1924#endif
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926#ifdef NO_CHECK_CARRIER
1927#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1928#else
1929#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1930#endif
1931
1932static void
1933tc35815_check_tx_stat(struct net_device *dev, int status)
1934{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001935 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 const char *msg = NULL;
1937
1938 /* count collisions */
1939 if (status & Tx_ExColl)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001940 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (status & Tx_TxColl_MASK)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001942 dev->stats.collisions += status & Tx_TxColl_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001944#ifndef NO_CHECK_CARRIER
1945 /* TX4939 does not have NCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001946 if (lp->chiptype == TC35815_TX4939)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 status &= ~Tx_NCarr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001948#ifdef WORKAROUND_LOSTCAR
1949 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001950 if (!lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001951 status &= ~Tx_NCarr;
1952#endif
1953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 if (!(status & TX_STA_ERR)) {
1956 /* no error. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001957 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return;
1959 }
1960
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001961 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (status & Tx_ExColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001963 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 msg = "Excessive Collision.";
1965 }
1966 if (status & Tx_Under) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001967 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 msg = "Tx FIFO Underrun.";
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001969 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1970 lp->lstats.tx_underrun++;
1971 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1972 struct tc35815_regs __iomem *tr =
1973 (struct tc35815_regs __iomem *)dev->base_addr;
1974 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1975 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1976 }
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979 if (status & Tx_Defer) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001980 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 msg = "Excessive Deferral.";
1982 }
1983#ifndef NO_CHECK_CARRIER
1984 if (status & Tx_NCarr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001985 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 msg = "Lost Carrier Sense.";
1987 }
1988#endif
1989 if (status & Tx_LateColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001990 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 msg = "Late Collision.";
1992 }
1993 if (status & Tx_TxPar) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001994 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 msg = "Transmit Parity Error.";
1996 }
1997 if (status & Tx_SQErr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001998 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 msg = "Signal Quality Error.";
2000 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002001 if (msg && netif_msg_tx_err(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
2003}
2004
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002005/* This handles TX complete events posted by the device
2006 * via interrupts.
2007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static void
2009tc35815_txdone(struct net_device *dev)
2010{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002011 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct TxFD *txfd;
2013 unsigned int fdctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 txfd = &lp->tfd_base[lp->tfd_end];
2016 while (lp->tfd_start != lp->tfd_end &&
2017 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
2018 int status = le32_to_cpu(txfd->fd.FDStat);
2019 struct sk_buff *skb;
2020 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002021 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002023 if (netif_msg_tx_done(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 printk("%s: complete TxFD.\n", dev->name);
2025 dump_txfd(txfd);
2026 }
2027 tc35815_check_tx_stat(dev, status);
2028
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002029 skb = fdsystem != 0xffffffff ?
2030 lp->tx_skbs[fdsystem].skb : NULL;
2031#ifdef DEBUG
2032 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
2033 printk("%s: tx_skbs mismatch.\n", dev->name);
2034 panic_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002036#else
2037 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
2038#endif
2039 if (skb) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002040 dev->stats.tx_bytes += skb->len;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002041 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
2042 lp->tx_skbs[lp->tfd_end].skb = NULL;
2043 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
2044#ifdef TC35815_NAPI
2045 dev_kfree_skb_any(skb);
2046#else
2047 dev_kfree_skb_irq(skb);
2048#endif
2049 }
2050 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
2053 txfd = &lp->tfd_base[lp->tfd_end];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002054#ifdef DEBUG
2055 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 printk("%s: TxFD FDNext invalid.\n", dev->name);
2057 panic_queues(dev);
2058 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (fdnext & FD_Next_EOL) {
2061 /* DMA Transmitter has been stopping... */
2062 if (lp->tfd_end != lp->tfd_start) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002063 struct tc35815_regs __iomem *tr =
2064 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002066 struct TxFD *txhead = &lp->tfd_base[head];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 int qlen = (lp->tfd_start + TX_FD_NUM
2068 - lp->tfd_end) % TX_FD_NUM;
2069
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002070#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
2072 printk("%s: TxFD FDCtl invalid.\n", dev->name);
2073 panic_queues(dev);
2074 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /* log max queue length */
2077 if (lp->lstats.max_tx_qlen < qlen)
2078 lp->lstats.max_tx_qlen = qlen;
2079
2080
2081 /* start DMA Transmitter again */
2082 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
2083#ifdef GATHER_TXINT
2084 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
2085#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002086 if (netif_msg_tx_queued(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 printk("%s: start TxFD on queue.\n",
2088 dev->name);
2089 dump_txfd(txfd);
2090 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002091 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 break;
2094 }
2095 }
2096
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002097 /* If we had stopped the queue due to a "tx full"
2098 * condition, and space has now been made available,
2099 * wake up the queue.
2100 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002101 if (netif_queue_stopped(dev) && !tc35815_tx_full(dev))
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002102 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
2105/* The inverse routine to tc35815_open(). */
2106static int
2107tc35815_close(struct net_device *dev)
2108{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002109 struct tc35815_local *lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002112#ifdef TC35815_NAPI
2113 napi_disable(&lp->napi);
2114#endif
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002115 if (lp->phy_dev)
2116 phy_stop(lp->phy_dev);
2117 cancel_work_sync(&lp->restart_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 /* Flush the Tx and disable Rx here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 tc35815_chip_reset(dev);
2121 free_irq(dev->irq, dev);
2122
2123 tc35815_free_queues(dev);
2124
2125 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129/*
2130 * Get the current statistics.
2131 * This may be called with the card open or closed.
2132 */
2133static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
2134{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002135 struct tc35815_regs __iomem *tr =
2136 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002137 if (netif_running(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /* Update the statistics from the device registers. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002139 dev->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002141 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002144static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002146 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002147 struct tc35815_regs __iomem *tr =
2148 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 int cam_index = index * 6;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002150 u32 cam_data;
2151 u32 saved_addr;
Atsushi Nemoto958eb802008-04-11 00:24:24 +09002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 saved_addr = tc_readl(&tr->CAM_Adr);
2154
Atsushi Nemoto958eb802008-04-11 00:24:24 +09002155 if (netif_msg_hw(lp))
Johannes Berge1749612008-10-27 15:59:26 -07002156 printk(KERN_DEBUG "%s: CAM %d: %pM\n",
2157 dev->name, index, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (index & 1) {
2159 /* read modify write */
2160 tc_writel(cam_index - 2, &tr->CAM_Adr);
2161 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
2162 cam_data |= addr[0] << 8 | addr[1];
2163 tc_writel(cam_data, &tr->CAM_Data);
2164 /* write whole word */
2165 tc_writel(cam_index + 2, &tr->CAM_Adr);
2166 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
2167 tc_writel(cam_data, &tr->CAM_Data);
2168 } else {
2169 /* write whole word */
2170 tc_writel(cam_index, &tr->CAM_Adr);
2171 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
2172 tc_writel(cam_data, &tr->CAM_Data);
2173 /* read modify write */
2174 tc_writel(cam_index + 4, &tr->CAM_Adr);
2175 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
2176 cam_data |= addr[4] << 24 | (addr[5] << 16);
2177 tc_writel(cam_data, &tr->CAM_Data);
2178 }
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 tc_writel(saved_addr, &tr->CAM_Adr);
2181}
2182
2183
2184/*
2185 * Set or clear the multicast filter for this adaptor.
2186 * num_addrs == -1 Promiscuous mode, receive all packets
2187 * num_addrs == 0 Normal mode, clear multicast list
2188 * num_addrs > 0 Multicast mode, receive normal and MC packets,
2189 * and do best-effort filtering.
2190 */
2191static void
2192tc35815_set_multicast_list(struct net_device *dev)
2193{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002194 struct tc35815_regs __iomem *tr =
2195 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002197 if (dev->flags & IFF_PROMISC) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002198#ifdef WORKAROUND_100HALF_PROMISC
2199 /* With some (all?) 100MHalf HUB, controller will hang
2200 * if we enabled promiscuous mode before linkup... */
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002201 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002202
2203 if (!lp->link)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002204 return;
2205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /* Enable promiscuous mode */
2207 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002208 } else if ((dev->flags & IFF_ALLMULTI) ||
2209 dev->mc_count > CAM_ENTRY_MAX - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* CAM 0, 1, 20 are reserved. */
2211 /* Disable promiscuous mode, use normal mode. */
2212 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002213 } else if (dev->mc_count) {
2214 struct dev_mc_list *cur_addr = dev->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 int i;
2216 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
2217
2218 tc_writel(0, &tr->CAM_Ctl);
2219 /* Walk the address list, and load the filter */
2220 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
2221 if (!cur_addr)
2222 break;
2223 /* entry 0,1 is reserved. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002224 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 ena_bits |= CAM_Ena_Bit(i + 2);
2226 }
2227 tc_writel(ena_bits, &tr->CAM_Ena);
2228 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002229 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2231 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2232 }
2233}
2234
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002235static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002237 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002238 strcpy(info->driver, MODNAME);
2239 strcpy(info->version, DRV_VERSION);
2240 strcpy(info->bus_info, pci_name(lp->pci_dev));
2241}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002242
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002243static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2244{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002245 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002246
2247 if (!lp->phy_dev)
2248 return -ENODEV;
2249 return phy_ethtool_gset(lp->phy_dev, cmd);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002252static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2253{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002254 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002255
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002256 if (!lp->phy_dev)
2257 return -ENODEV;
2258 return phy_ethtool_sset(lp->phy_dev, cmd);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002259}
2260
2261static u32 tc35815_get_msglevel(struct net_device *dev)
2262{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002263 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002264 return lp->msg_enable;
2265}
2266
2267static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2268{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002269 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002270 lp->msg_enable = datum;
2271}
2272
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002273static int tc35815_get_sset_count(struct net_device *dev, int sset)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002274{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002275 struct tc35815_local *lp = netdev_priv(dev);
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002276
2277 switch (sset) {
2278 case ETH_SS_STATS:
2279 return sizeof(lp->lstats) / sizeof(int);
2280 default:
2281 return -EOPNOTSUPP;
2282 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002283}
2284
2285static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2286{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002287 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002288 data[0] = lp->lstats.max_tx_qlen;
2289 data[1] = lp->lstats.tx_ints;
2290 data[2] = lp->lstats.rx_ints;
2291 data[3] = lp->lstats.tx_underrun;
2292}
2293
2294static struct {
2295 const char str[ETH_GSTRING_LEN];
2296} ethtool_stats_keys[] = {
2297 { "max_tx_qlen" },
2298 { "tx_ints" },
2299 { "rx_ints" },
2300 { "tx_underrun" },
2301};
2302
2303static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2304{
2305 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2306}
2307
2308static const struct ethtool_ops tc35815_ethtool_ops = {
2309 .get_drvinfo = tc35815_get_drvinfo,
2310 .get_settings = tc35815_get_settings,
2311 .set_settings = tc35815_set_settings,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002312 .get_link = ethtool_op_get_link,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002313 .get_msglevel = tc35815_get_msglevel,
2314 .set_msglevel = tc35815_set_msglevel,
2315 .get_strings = tc35815_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002316 .get_sset_count = tc35815_get_sset_count,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002317 .get_ethtool_stats = tc35815_get_ethtool_stats,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002318};
2319
2320static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2321{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002322 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002323
2324 if (!netif_running(dev))
2325 return -EINVAL;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002326 if (!lp->phy_dev)
2327 return -ENODEV;
2328 return phy_mii_ioctl(lp->phy_dev, if_mii(rq), cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
2331static void tc35815_chip_reset(struct net_device *dev)
2332{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002333 struct tc35815_regs __iomem *tr =
2334 (struct tc35815_regs __iomem *)dev->base_addr;
2335 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /* reset the controller */
2337 tc_writel(MAC_Reset, &tr->MAC_Ctl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002338 udelay(4); /* 3200ns */
2339 i = 0;
2340 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2341 if (i++ > 100) {
2342 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2343 break;
2344 }
2345 mdelay(1);
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 tc_writel(0, &tr->MAC_Ctl);
2348
2349 /* initialize registers to default value */
2350 tc_writel(0, &tr->DMA_Ctl);
2351 tc_writel(0, &tr->TxThrsh);
2352 tc_writel(0, &tr->TxPollCtr);
2353 tc_writel(0, &tr->RxFragSize);
2354 tc_writel(0, &tr->Int_En);
2355 tc_writel(0, &tr->FDA_Bas);
2356 tc_writel(0, &tr->FDA_Lim);
2357 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2358 tc_writel(0, &tr->CAM_Ctl);
2359 tc_writel(0, &tr->Tx_Ctl);
2360 tc_writel(0, &tr->Rx_Ctl);
2361 tc_writel(0, &tr->CAM_Ena);
2362 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2363
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002364 /* initialize internal SRAM */
2365 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2366 for (i = 0; i < 0x1000; i += 4) {
2367 tc_writel(i, &tr->CAM_Adr);
2368 tc_writel(0, &tr->CAM_Data);
2369 }
2370 tc_writel(0, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373static void tc35815_chip_init(struct net_device *dev)
2374{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002375 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002376 struct tc35815_regs __iomem *tr =
2377 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 unsigned long txctl = TX_CTL_CMD;
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* load station address to CAM */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002381 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* Enable CAM (broadcast and unicast) */
2384 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2385 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2386
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002387 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2388 if (HAVE_DMA_RXALIGN(lp))
2389 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2390 else
2391 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2392#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002394#else
2395 tc_writel(ETH_ZLEN, &tr->RxFragSize);
2396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2398 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2399 tc_writel(INT_EN_CMD, &tr->Int_En);
2400
2401 /* set queues */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002402 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2404 &tr->FDA_Lim);
2405 /*
2406 * Activation method:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002407 * First, enable the MAC Transmitter and the DMA Receive circuits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 * Then enable the DMA Transmitter and the MAC Receive circuits.
2409 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002410 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /* start MAC transmitter */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002414#ifndef NO_CHECK_CARRIER
2415 /* TX4939 does not have EnLCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002416 if (lp->chiptype == TC35815_TX4939)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002417 txctl &= ~Tx_EnLCarr;
2418#ifdef WORKAROUND_LOSTCAR
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002420 if (!lp->phy_dev || !lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002421 txctl &= ~Tx_EnLCarr;
2422#endif
2423#endif /* !NO_CHECK_CARRIER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424#ifdef GATHER_TXINT
2425 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
2426#endif
2427 tc_writel(txctl, &tr->Tx_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002430#ifdef CONFIG_PM
2431static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2432{
2433 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002434 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002435 unsigned long flags;
2436
2437 pci_save_state(pdev);
2438 if (!netif_running(dev))
2439 return 0;
2440 netif_device_detach(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002441 if (lp->phy_dev)
2442 phy_stop(lp->phy_dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002443 spin_lock_irqsave(&lp->lock, flags);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002444 tc35815_chip_reset(dev);
2445 spin_unlock_irqrestore(&lp->lock, flags);
2446 pci_set_power_state(pdev, PCI_D3hot);
2447 return 0;
2448}
2449
2450static int tc35815_resume(struct pci_dev *pdev)
2451{
2452 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002453 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002454
2455 pci_restore_state(pdev);
2456 if (!netif_running(dev))
2457 return 0;
2458 pci_set_power_state(pdev, PCI_D0);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002459 tc35815_restart(dev);
Atsushi Nemoto59524a32008-06-25 11:41:01 +09002460 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002461 if (lp->phy_dev)
2462 phy_start(lp->phy_dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002463 netif_device_attach(dev);
2464 return 0;
2465}
2466#endif /* CONFIG_PM */
2467
2468static struct pci_driver tc35815_pci_driver = {
2469 .name = MODNAME,
2470 .id_table = tc35815_pci_tbl,
2471 .probe = tc35815_init_one,
2472 .remove = __devexit_p(tc35815_remove_one),
2473#ifdef CONFIG_PM
2474 .suspend = tc35815_suspend,
2475 .resume = tc35815_resume,
2476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477};
2478
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002479module_param_named(speed, options.speed, int, 0);
2480MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2481module_param_named(duplex, options.duplex, int, 0);
2482MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484static int __init tc35815_init_module(void)
2485{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002486 return pci_register_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489static void __exit tc35815_cleanup_module(void)
2490{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002491 pci_unregister_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
Jeff Garzik420e8522007-02-24 17:02:16 -05002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494module_init(tc35815_init_module);
2495module_exit(tc35815_cleanup_module);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002496
2497MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2498MODULE_LICENSE("GPL");