blob: 803eb64ffcc2beba9eb1508526a32669b295561d [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 Nemotoc6a2dbb2009-11-02 04:34:46 +000025#define DRV_VERSION "1.39"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090026static const char *version = "tc35815.c:v" DRV_VERSION "\n";
27#define MODNAME "tc35815"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/fcntl.h>
33#include <linux/interrupt.h>
34#include <linux/ioport.h>
35#include <linux/in.h>
Atsushi Nemoto82a99282008-12-11 20:58:04 -080036#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/string.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090039#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/init.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
45#include <linux/delay.h>
46#include <linux/pci.h>
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090047#include <linux/phy.h>
48#include <linux/workqueue.h>
Atsushi Nemotobd43da82007-06-29 22:34:53 +090049#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/byteorder.h>
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* First, a few definitions that the brave might change. */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define GATHER_TXINT /* On-Demand Tx Interrupt */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090056#define WORKAROUND_LOSTCAR
57#define WORKAROUND_100HALF_PROMISC
58/* #define TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090060enum tc35815_chiptype {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090061 TC35815CF = 0,
62 TC35815_NWU,
63 TC35815_TX4939,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090064};
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090065
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090066/* indexed by tc35815_chiptype, above */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090067static const struct {
68 const char *name;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090069} chip_info[] __devinitdata = {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090070 { "TOSHIBA TC35815CF 10/100BaseTX" },
71 { "TOSHIBA TC35815 with Wake on LAN" },
72 { "TOSHIBA TC35815/TX4939" },
73};
74
75static const struct pci_device_id tc35815_pci_tbl[] = {
76 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
77 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
78 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
79 {0,}
80};
Atsushi Nemoto7f225b42008-04-11 00:25:31 +090081MODULE_DEVICE_TABLE(pci, tc35815_pci_tbl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090082
83/* see MODULE_PARM_DESC */
84static struct tc35815_options {
85 int speed;
86 int duplex;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090087} options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/*
90 * Registers
91 */
92struct tc35815_regs {
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +090093 __u32 DMA_Ctl; /* 0x00 */
94 __u32 TxFrmPtr;
95 __u32 TxThrsh;
96 __u32 TxPollCtr;
97 __u32 BLFrmPtr;
98 __u32 RxFragSize;
99 __u32 Int_En;
100 __u32 FDA_Bas;
101 __u32 FDA_Lim; /* 0x20 */
102 __u32 Int_Src;
103 __u32 unused0[2];
104 __u32 PauseCnt;
105 __u32 RemPauCnt;
106 __u32 TxCtlFrmStat;
107 __u32 unused1;
108 __u32 MAC_Ctl; /* 0x40 */
109 __u32 CAM_Ctl;
110 __u32 Tx_Ctl;
111 __u32 Tx_Stat;
112 __u32 Rx_Ctl;
113 __u32 Rx_Stat;
114 __u32 MD_Data;
115 __u32 MD_CA;
116 __u32 CAM_Adr; /* 0x60 */
117 __u32 CAM_Data;
118 __u32 CAM_Ena;
119 __u32 PROM_Ctl;
120 __u32 PROM_Data;
121 __u32 Algn_Cnt;
122 __u32 CRC_Cnt;
123 __u32 Miss_Cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126/*
127 * Bit assignments
128 */
129/* DMA_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900130#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
131#define DMA_RxAlign_1 0x00400000
132#define DMA_RxAlign_2 0x00800000
133#define DMA_RxAlign_3 0x00c00000
134#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
135#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
136#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
137#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
138#define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
139#define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
140#define DMA_TestMode 0x00002000 /* 1:Test Mode */
141#define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
142#define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/* RxFragSize bit asign ---------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900145#define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
146#define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* MAC_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900149#define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
150#define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
151#define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
152#define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
153#define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
154#define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
155#define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
156#define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
157#define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
158#define MAC_Reset 0x00000004 /* 1:Software Reset */
159#define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
160#define MAC_HaltReq 0x00000001 /* 1:Halt request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/* PROM_Ctl bit asign ------------------------------------------------------ */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900163#define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
164#define PROM_Read 0x00004000 /*10:Read operation */
165#define PROM_Write 0x00002000 /*01:Write operation */
166#define PROM_Erase 0x00006000 /*11:Erase operation */
167 /*00:Enable or Disable Writting, */
168 /* as specified in PROM_Addr. */
169#define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
170 /*00xxxx: disable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* CAM_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900173#define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
174#define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
175 /* accept other */
176#define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
177#define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
178#define CAM_StationAcc 0x00000001 /* 1:unicast accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180/* CAM_Ena bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900181#define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900183#define CAM_Ena_Bit(index) (1 << (index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define CAM_ENTRY_DESTINATION 0
185#define CAM_ENTRY_SOURCE 1
186#define CAM_ENTRY_MACCTL 20
187
188/* Tx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900189#define Tx_En 0x00000001 /* 1:Transmit enable */
190#define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
191#define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
192#define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
193#define Tx_FBack 0x00000010 /* 1:Fast Back-off */
194#define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
195#define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
196#define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
197#define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
198#define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
199#define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
200#define Tx_EnComp 0x00004000 /* 1:Enable Completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/* Tx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900203#define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
204#define Tx_ExColl 0x00000010 /* Excessive Collision */
205#define Tx_TXDefer 0x00000020 /* Transmit Defered */
206#define Tx_Paused 0x00000040 /* Transmit Paused */
207#define Tx_IntTx 0x00000080 /* Interrupt on Tx */
208#define Tx_Under 0x00000100 /* Underrun */
209#define Tx_Defer 0x00000200 /* Deferral */
210#define Tx_NCarr 0x00000400 /* No Carrier */
211#define Tx_10Stat 0x00000800 /* 10Mbps Status */
212#define Tx_LateColl 0x00001000 /* Late Collision */
213#define Tx_TxPar 0x00002000 /* Tx Parity Error */
214#define Tx_Comp 0x00004000 /* Completion */
215#define Tx_Halted 0x00008000 /* Tx Halted */
216#define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/* Rx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900219#define Rx_EnGood 0x00004000 /* 1:Enable Good */
220#define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
221#define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
222#define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
223#define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
224#define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
225#define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
226#define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
227#define Rx_ShortEn 0x00000008 /* 1:Short Enable */
228#define Rx_LongEn 0x00000004 /* 1:Long Enable */
229#define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
230#define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/* Rx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900233#define Rx_Halted 0x00008000 /* Rx Halted */
234#define Rx_Good 0x00004000 /* Rx Good */
235#define Rx_RxPar 0x00002000 /* Rx Parity Error */
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900236#define Rx_TypePkt 0x00001000 /* Rx Type Packet */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900237#define Rx_LongErr 0x00000800 /* Rx Long Error */
238#define Rx_Over 0x00000400 /* Rx Overflow */
239#define Rx_CRCErr 0x00000200 /* Rx CRC Error */
240#define Rx_Align 0x00000100 /* Rx Alignment Error */
241#define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
242#define Rx_IntRx 0x00000040 /* Rx Interrupt */
243#define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900244#define Rx_InLenErr 0x00000010 /* Rx In Range Frame Length Error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900246#define Rx_Stat_Mask 0x0000FFF0 /* Rx All Status Mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248/* Int_En bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900249#define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
250#define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Ctl Complete Enable */
251#define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
252#define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
253#define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
254#define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
255#define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
256#define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
257#define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
258#define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
259#define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
260#define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
261 /* Exhausted Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/* Int_Src bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900264#define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
265#define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
266#define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
267#define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
268#define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
269#define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
270#define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
271#define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
272#define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
273#define Int_SWInt 0x00000020 /* 1:Software request & Clear */
274#define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
275#define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
276#define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
277#define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
278#define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/* MD_CA bit asign --------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900281#define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
282#define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
283#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * Descriptors
288 */
289
290/* Frame descripter */
291struct FDesc {
292 volatile __u32 FDNext;
293 volatile __u32 FDSystem;
294 volatile __u32 FDStat;
295 volatile __u32 FDCtl;
296};
297
298/* Buffer descripter */
299struct BDesc {
300 volatile __u32 BuffData;
301 volatile __u32 BDCtl;
302};
303
304#define FD_ALIGN 16
305
306/* Frame Descripter bit asign ---------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900307#define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
308#define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
309#define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900311#define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
312#define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
314#define FD_FrmOpt_Packing 0x04000000 /* Rx only */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900315#define FD_CownsFD 0x80000000 /* FD Controller owner bit */
316#define FD_Next_EOL 0x00000001 /* FD EOL indicator */
317#define FD_BDCnt_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/* Buffer Descripter bit asign --------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900320#define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
321#define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
322#define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
323#define BD_CownsBD 0x80000000 /* BD Controller owner bit */
324#define BD_RxBDID_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#define BD_RxBDSeqN_SHIFT 24
326
327
328/* Some useful constants. */
329#undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */
330
331#ifdef NO_CHECK_CARRIER
332#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900333 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
334 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#else
336#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900337 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
338 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif
Atsushi Nemoto297713d2009-08-06 04:41:45 +0000340/* Do not use Rx_StripCRC -- it causes trouble on BLEx/FDAEx condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
Atsushi Nemoto297713d2009-08-06 04:41:45 +0000342 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#define INT_EN_CMD (Int_NRAbtEn | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900344 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
346 Int_STargAbtEn | \
347 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900348#define DMA_CTL_CMD DMA_BURST_SIZE
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900349#define HAVE_DMA_RXALIGN(lp) likely((lp)->chiptype != TC35815CF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351/* Tuning parameters */
352#define DMA_BURST_SIZE 32
353#define TX_THRESHOLD 1024
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900354/* used threshold with packet max byte for low pci transfer ability.*/
355#define TX_THRESHOLD_MAX 1536
356/* setting threshold max value when overrun error occured this count. */
357#define TX_THRESHOLD_KEEP_LIMIT 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900359/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
360#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define FD_PAGE_NUM 2
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900362#define RX_BUF_NUM 8 /* >= 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#define RX_FD_NUM 250 /* >= 32 */
364#define TX_FD_NUM 128
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900365#define RX_BUF_SIZE PAGE_SIZE
366#else /* TC35815_USE_PACKEDBUFFER */
367#define FD_PAGE_NUM 4
368#define RX_BUF_NUM 128 /* < 256 */
369#define RX_FD_NUM 256 /* >= 32 */
370#define TX_FD_NUM 128
371#if RX_CTL_CMD & Rx_LongEn
372#define RX_BUF_SIZE PAGE_SIZE
373#elif RX_CTL_CMD & Rx_StripCRC
Atsushi Nemoto82a99282008-12-11 20:58:04 -0800374#define RX_BUF_SIZE \
375 L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + NET_IP_ALIGN)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900376#else
Atsushi Nemoto82a99282008-12-11 20:58:04 -0800377#define RX_BUF_SIZE \
378 L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN + NET_IP_ALIGN)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900379#endif
380#endif /* TC35815_USE_PACKEDBUFFER */
381#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
382#define NAPI_WEIGHT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384struct TxFD {
385 struct FDesc fd;
386 struct BDesc bd;
387 struct BDesc unused;
388};
389
390struct RxFD {
391 struct FDesc fd;
392 struct BDesc bd[0]; /* variable length */
393};
394
395struct FrFD {
396 struct FDesc fd;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900397 struct BDesc bd[RX_BUF_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
400
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900401#define tc_readl(addr) ioread32(addr)
402#define tc_writel(d, addr) iowrite32(d, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900404#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
405
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900406/* Information that need to be kept for each controller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407struct tc35815_local {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900408 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700410 struct net_device *dev;
411 struct napi_struct napi;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct {
415 int max_tx_qlen;
416 int tx_ints;
417 int rx_ints;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900418 int tx_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 } lstats;
420
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900421 /* Tx control lock. This protects the transmit buffer ring
422 * state along with the "tx full" state of the driver. This
423 * means all netif_queue flow control actions are protected
424 * by this lock as well.
425 */
426 spinlock_t lock;
427
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700428 struct mii_bus *mii_bus;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900429 struct phy_device *phy_dev;
430 int duplex;
431 int speed;
432 int link;
433 struct work_struct restart_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * Transmitting: Batch Mode.
437 * 1 BD in 1 TxFD.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900438 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * 1 circular FD for Free Buffer List.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900440 * RX_BUF_NUM BD in Free Buffer FD.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * One Free Buffer BD has PAGE_SIZE data buffer.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900442 * Or Non-Packing Mode.
443 * 1 circular FD for Free Buffer List.
444 * RX_BUF_NUM BD in Free Buffer FD.
445 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900447 void *fd_buf; /* for TxFD, RxFD, FrFD */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900448 dma_addr_t fd_buf_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct TxFD *tfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900450 unsigned int tfd_start;
451 unsigned int tfd_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct RxFD *rfd_base;
453 struct RxFD *rfd_limit;
454 struct RxFD *rfd_cur;
455 struct FrFD *fbl_ptr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900456#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 unsigned char fbl_curid;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900458 void *data_buf[RX_BUF_NUM]; /* packing */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900459 dma_addr_t data_buf_dma[RX_BUF_NUM];
460 struct {
461 struct sk_buff *skb;
462 dma_addr_t skb_dma;
463 } tx_skbs[TX_FD_NUM];
464#else
465 unsigned int fbl_count;
466 struct {
467 struct sk_buff *skb;
468 dma_addr_t skb_dma;
469 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
470#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900471 u32 msg_enable;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900472 enum tc35815_chiptype chiptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900475static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
476{
477 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
478}
479#ifdef DEBUG
480static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
481{
482 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
483}
484#endif
485#ifdef TC35815_USE_PACKEDBUFFER
486static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
487{
488 int i;
489 for (i = 0; i < RX_BUF_NUM; i++) {
490 if (bus >= lp->data_buf_dma[i] &&
491 bus < lp->data_buf_dma[i] + PAGE_SIZE)
492 return (void *)((u8 *)lp->data_buf[i] +
493 (bus - lp->data_buf_dma[i]));
494 }
495 return NULL;
496}
497
498#define TC35815_DMA_SYNC_ONDEMAND
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900499static void *alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900500{
501#ifdef TC35815_DMA_SYNC_ONDEMAND
502 void *buf;
503 /* pci_map + pci_dma_sync will be more effective than
504 * pci_alloc_consistent on some archs. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900505 buf = (void *)__get_free_page(GFP_ATOMIC);
506 if (!buf)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900507 return NULL;
508 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
509 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700510 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900511 free_page((unsigned long)buf);
512 return NULL;
513 }
514 return buf;
515#else
516 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
517#endif
518}
519
520static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
521{
522#ifdef TC35815_DMA_SYNC_ONDEMAND
523 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
524 free_page((unsigned long)buf);
525#else
526 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
527#endif
528}
529#else /* TC35815_USE_PACKEDBUFFER */
530static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
531 struct pci_dev *hwdev,
532 dma_addr_t *dma_handle)
533{
534 struct sk_buff *skb;
535 skb = dev_alloc_skb(RX_BUF_SIZE);
536 if (!skb)
537 return NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900538 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
539 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700540 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900541 dev_kfree_skb_any(skb);
542 return NULL;
543 }
544 skb_reserve(skb, 2); /* make IP header 4byte aligned */
545 return skb;
546}
547
548static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
549{
550 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
551 PCI_DMA_FROMDEVICE);
552 dev_kfree_skb_any(skb);
553}
554#endif /* TC35815_USE_PACKEDBUFFER */
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/* Index to functions, as function prototypes. */
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static int tc35815_open(struct net_device *dev);
559static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900560static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900561static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static void tc35815_txdone(struct net_device *dev);
564static int tc35815_close(struct net_device *dev);
565static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
566static void tc35815_set_multicast_list(struct net_device *dev);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900567static void tc35815_tx_timeout(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900568static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
569#ifdef CONFIG_NET_POLL_CONTROLLER
570static void tc35815_poll_controller(struct net_device *dev);
571#endif
572static const struct ethtool_ops tc35815_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900574/* Example routines you must write ;->. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900575static void tc35815_chip_reset(struct net_device *dev);
576static void tc35815_chip_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900578#ifdef DEBUG
579static void panic_queues(struct net_device *dev);
580#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900582static void tc35815_restart_work(struct work_struct *work);
583
584static int tc_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
585{
586 struct net_device *dev = bus->priv;
587 struct tc35815_regs __iomem *tr =
588 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000589 unsigned long timeout = jiffies + HZ;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900590
591 tc_writel(MD_CA_Busy | (mii_id << 5) | (regnum & 0x1f), &tr->MD_CA);
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000592 udelay(12); /* it takes 32 x 400ns at least */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900593 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
594 if (time_after(jiffies, timeout))
595 return -EIO;
596 cpu_relax();
597 }
598 return tc_readl(&tr->MD_Data) & 0xffff;
599}
600
601static int tc_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 val)
602{
603 struct net_device *dev = bus->priv;
604 struct tc35815_regs __iomem *tr =
605 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000606 unsigned long timeout = jiffies + HZ;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900607
608 tc_writel(val, &tr->MD_Data);
609 tc_writel(MD_CA_Busy | MD_CA_Wr | (mii_id << 5) | (regnum & 0x1f),
610 &tr->MD_CA);
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000611 udelay(12); /* it takes 32 x 400ns at least */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900612 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
613 if (time_after(jiffies, timeout))
614 return -EIO;
615 cpu_relax();
616 }
617 return 0;
618}
619
620static void tc_handle_link_change(struct net_device *dev)
621{
622 struct tc35815_local *lp = netdev_priv(dev);
623 struct phy_device *phydev = lp->phy_dev;
624 unsigned long flags;
625 int status_change = 0;
626
627 spin_lock_irqsave(&lp->lock, flags);
628 if (phydev->link &&
629 (lp->speed != phydev->speed || lp->duplex != phydev->duplex)) {
630 struct tc35815_regs __iomem *tr =
631 (struct tc35815_regs __iomem *)dev->base_addr;
632 u32 reg;
633
634 reg = tc_readl(&tr->MAC_Ctl);
635 reg |= MAC_HaltReq;
636 tc_writel(reg, &tr->MAC_Ctl);
637 if (phydev->duplex == DUPLEX_FULL)
638 reg |= MAC_FullDup;
639 else
640 reg &= ~MAC_FullDup;
641 tc_writel(reg, &tr->MAC_Ctl);
642 reg &= ~MAC_HaltReq;
643 tc_writel(reg, &tr->MAC_Ctl);
644
645 /*
646 * TX4939 PCFG.SPEEDn bit will be changed on
647 * NETDEV_CHANGE event.
648 */
649
650#if !defined(NO_CHECK_CARRIER) && defined(WORKAROUND_LOSTCAR)
651 /*
652 * WORKAROUND: enable LostCrS only if half duplex
653 * operation.
654 * (TX4939 does not have EnLCarr)
655 */
656 if (phydev->duplex == DUPLEX_HALF &&
657 lp->chiptype != TC35815_TX4939)
658 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr,
659 &tr->Tx_Ctl);
660#endif
661
662 lp->speed = phydev->speed;
663 lp->duplex = phydev->duplex;
664 status_change = 1;
665 }
666
667 if (phydev->link != lp->link) {
668 if (phydev->link) {
669#ifdef WORKAROUND_100HALF_PROMISC
670 /* delayed promiscuous enabling */
671 if (dev->flags & IFF_PROMISC)
672 tc35815_set_multicast_list(dev);
673#endif
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900674 } else {
675 lp->speed = 0;
676 lp->duplex = -1;
677 }
678 lp->link = phydev->link;
679
680 status_change = 1;
681 }
682 spin_unlock_irqrestore(&lp->lock, flags);
683
684 if (status_change && netif_msg_link(lp)) {
685 phy_print_status(phydev);
Joe Perches72903832009-05-15 07:59:42 +0000686 pr_debug("%s: MII BMCR %04x BMSR %04x LPA %04x\n",
687 dev->name,
688 phy_read(phydev, MII_BMCR),
689 phy_read(phydev, MII_BMSR),
690 phy_read(phydev, MII_LPA));
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900691 }
692}
693
694static int tc_mii_probe(struct net_device *dev)
695{
696 struct tc35815_local *lp = netdev_priv(dev);
697 struct phy_device *phydev = NULL;
698 int phy_addr;
699 u32 dropmask;
700
701 /* find the first phy */
702 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700703 if (lp->mii_bus->phy_map[phy_addr]) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900704 if (phydev) {
705 printk(KERN_ERR "%s: multiple PHYs found\n",
706 dev->name);
707 return -EINVAL;
708 }
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700709 phydev = lp->mii_bus->phy_map[phy_addr];
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900710 break;
711 }
712 }
713
714 if (!phydev) {
715 printk(KERN_ERR "%s: no PHY found\n", dev->name);
716 return -ENODEV;
717 }
718
719 /* attach the mac to the phy */
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800720 phydev = phy_connect(dev, dev_name(&phydev->dev),
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900721 &tc_handle_link_change, 0,
722 lp->chiptype == TC35815_TX4939 ?
723 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII);
724 if (IS_ERR(phydev)) {
725 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
726 return PTR_ERR(phydev);
727 }
728 printk(KERN_INFO "%s: attached PHY driver [%s] "
729 "(mii_bus:phy_addr=%s, id=%x)\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800730 dev->name, phydev->drv->name, dev_name(&phydev->dev),
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900731 phydev->phy_id);
732
733 /* mask with MAC supported features */
734 phydev->supported &= PHY_BASIC_FEATURES;
735 dropmask = 0;
736 if (options.speed == 10)
737 dropmask |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
738 else if (options.speed == 100)
739 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
740 if (options.duplex == 1)
741 dropmask |= SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full;
742 else if (options.duplex == 2)
743 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_100baseT_Half;
744 phydev->supported &= ~dropmask;
745 phydev->advertising = phydev->supported;
746
747 lp->link = 0;
748 lp->speed = 0;
749 lp->duplex = -1;
750 lp->phy_dev = phydev;
751
752 return 0;
753}
754
755static int tc_mii_init(struct net_device *dev)
756{
757 struct tc35815_local *lp = netdev_priv(dev);
758 int err;
759 int i;
760
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700761 lp->mii_bus = mdiobus_alloc();
762 if (lp->mii_bus == NULL) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900763 err = -ENOMEM;
764 goto err_out;
765 }
766
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700767 lp->mii_bus->name = "tc35815_mii_bus";
768 lp->mii_bus->read = tc_mdio_read;
769 lp->mii_bus->write = tc_mdio_write;
770 snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x",
771 (lp->pci_dev->bus->number << 8) | lp->pci_dev->devfn);
772 lp->mii_bus->priv = dev;
773 lp->mii_bus->parent = &lp->pci_dev->dev;
774 lp->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
775 if (!lp->mii_bus->irq) {
776 err = -ENOMEM;
777 goto err_out_free_mii_bus;
778 }
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900779
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700780 for (i = 0; i < PHY_MAX_ADDR; i++)
781 lp->mii_bus->irq[i] = PHY_POLL;
782
783 err = mdiobus_register(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900784 if (err)
785 goto err_out_free_mdio_irq;
786 err = tc_mii_probe(dev);
787 if (err)
788 goto err_out_unregister_bus;
789 return 0;
790
791err_out_unregister_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700792 mdiobus_unregister(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900793err_out_free_mdio_irq:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700794 kfree(lp->mii_bus->irq);
Adrian Bunk51cf7562008-10-12 21:01:53 -0700795err_out_free_mii_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700796 mdiobus_free(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900797err_out:
798 return err;
799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900801#ifdef CONFIG_CPU_TX49XX
802/*
803 * Find a platform_device providing a MAC address. The platform code
804 * should provide a "tc35815-mac" device with a MAC address in its
805 * platform_data.
806 */
807static int __devinit tc35815_mac_match(struct device *dev, void *data)
808{
809 struct platform_device *plat_dev = to_platform_device(dev);
810 struct pci_dev *pci_dev = data;
Atsushi Nemoto06675e62008-01-19 01:15:52 +0900811 unsigned int id = pci_dev->irq;
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900812 return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
813}
814
815static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
816{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900817 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900818 struct device *pd = bus_find_device(&platform_bus_type, NULL,
819 lp->pci_dev, tc35815_mac_match);
820 if (pd) {
821 if (pd->platform_data)
822 memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
823 put_device(pd);
824 return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
825 }
826 return -ENODEV;
827}
828#else
Yoichi Yuasa308a9062007-07-18 11:13:42 +0900829static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900830{
831 return -ENODEV;
832}
833#endif
834
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900835static int __devinit tc35815_init_dev_addr(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900837 struct tc35815_regs __iomem *tr =
838 (struct tc35815_regs __iomem *)dev->base_addr;
839 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
842 ;
843 for (i = 0; i < 6; i += 2) {
844 unsigned short data;
845 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
846 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
847 ;
848 data = tc_readl(&tr->PROM_Data);
849 dev->dev_addr[i] = data & 0xff;
850 dev->dev_addr[i+1] = data >> 8;
851 }
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900852 if (!is_valid_ether_addr(dev->dev_addr))
853 return tc35815_read_plat_dev_addr(dev);
854 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Alexander Beregalov5a1c28b2009-04-11 07:38:54 +0000857static const struct net_device_ops tc35815_netdev_ops = {
858 .ndo_open = tc35815_open,
859 .ndo_stop = tc35815_close,
860 .ndo_start_xmit = tc35815_send_packet,
861 .ndo_get_stats = tc35815_get_stats,
862 .ndo_set_multicast_list = tc35815_set_multicast_list,
863 .ndo_tx_timeout = tc35815_tx_timeout,
864 .ndo_do_ioctl = tc35815_ioctl,
865 .ndo_validate_addr = eth_validate_addr,
866 .ndo_change_mtu = eth_change_mtu,
867 .ndo_set_mac_address = eth_mac_addr,
868#ifdef CONFIG_NET_POLL_CONTROLLER
869 .ndo_poll_controller = tc35815_poll_controller,
870#endif
871};
872
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900873static int __devinit tc35815_init_one(struct pci_dev *pdev,
874 const struct pci_device_id *ent)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900875{
876 void __iomem *ioaddr = NULL;
877 struct net_device *dev;
878 struct tc35815_local *lp;
879 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900881 static int printed_version;
882 if (!printed_version++) {
883 printk(version);
884 dev_printk(KERN_DEBUG, &pdev->dev,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900885 "speed:%d duplex:%d\n",
886 options.speed, options.duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900889 if (!pdev->irq) {
890 dev_warn(&pdev->dev, "no IRQ assigned.\n");
891 return -ENODEV;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900894 /* dev zeroed in alloc_etherdev */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900895 dev = alloc_etherdev(sizeof(*lp));
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900896 if (dev == NULL) {
897 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
898 return -ENOMEM;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 SET_NETDEV_DEV(dev, &pdev->dev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900901 lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700902 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900904 /* enable device (incl. PCI PM wakeup), and bus-mastering */
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900905 rc = pcim_enable_device(pdev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900906 if (rc)
907 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900908 rc = pcim_iomap_regions(pdev, 1 << 1, MODNAME);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900909 if (rc)
910 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900911 pci_set_master(pdev);
912 ioaddr = pcim_iomap_table(pdev)[1];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900913
914 /* Initialize the device structure. */
Alexander Beregalov5a1c28b2009-04-11 07:38:54 +0000915 dev->netdev_ops = &tc35815_netdev_ops;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900916 dev->ethtool_ops = &tc35815_ethtool_ops;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900917 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700918 netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900919
920 dev->irq = pdev->irq;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900921 dev->base_addr = (unsigned long)ioaddr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900922
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900923 INIT_WORK(&lp->restart_work, tc35815_restart_work);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900924 spin_lock_init(&lp->lock);
925 lp->pci_dev = pdev;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900926 lp->chiptype = ent->driver_data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900927
928 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
929 pci_set_drvdata(pdev, dev);
930
931 /* Soft reset the chip. */
932 tc35815_chip_reset(dev);
933
934 /* Retrieve the ethernet address. */
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900935 if (tc35815_init_dev_addr(dev)) {
936 dev_warn(&pdev->dev, "not valid ether addr\n");
937 random_ether_addr(dev->dev_addr);
938 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900939
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900940 rc = register_netdev(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900941 if (rc)
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900942 goto err_out;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900943
944 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Johannes Berge1749612008-10-27 15:59:26 -0700945 printk(KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900946 dev->name,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900947 chip_info[ent->driver_data].name,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900948 dev->base_addr,
Johannes Berge1749612008-10-27 15:59:26 -0700949 dev->dev_addr,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900950 dev->irq);
951
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900952 rc = tc_mii_init(dev);
953 if (rc)
954 goto err_out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 return 0;
957
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900958err_out_unregister:
959 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960err_out:
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900961 free_netdev(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900962 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900966static void __devexit tc35815_remove_one(struct pci_dev *pdev)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900967{
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900968 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900969 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900970
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900971 phy_disconnect(lp->phy_dev);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700972 mdiobus_unregister(lp->mii_bus);
973 kfree(lp->mii_bus->irq);
974 mdiobus_free(lp->mii_bus);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900975 unregister_netdev(dev);
976 free_netdev(dev);
977 pci_set_drvdata(pdev, NULL);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int
981tc35815_init_queues(struct net_device *dev)
982{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900983 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int i;
985 unsigned long fd_addr;
986
987 if (!lp->fd_buf) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900988 BUG_ON(sizeof(struct FDesc) +
989 sizeof(struct BDesc) * RX_BUF_NUM +
990 sizeof(struct FDesc) * RX_FD_NUM +
991 sizeof(struct TxFD) * TX_FD_NUM >
992 PAGE_SIZE * FD_PAGE_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900994 lp->fd_buf = pci_alloc_consistent(lp->pci_dev,
995 PAGE_SIZE * FD_PAGE_NUM,
996 &lp->fd_buf_dma);
997 if (!lp->fd_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENOMEM;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900999 for (i = 0; i < RX_BUF_NUM; i++) {
1000#ifdef TC35815_USE_PACKEDBUFFER
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001001 lp->data_buf[i] =
1002 alloc_rxbuf_page(lp->pci_dev,
1003 &lp->data_buf_dma[i]);
1004 if (!lp->data_buf[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 while (--i >= 0) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001006 free_rxbuf_page(lp->pci_dev,
1007 lp->data_buf[i],
1008 lp->data_buf_dma[i]);
1009 lp->data_buf[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001011 pci_free_consistent(lp->pci_dev,
1012 PAGE_SIZE * FD_PAGE_NUM,
1013 lp->fd_buf,
1014 lp->fd_buf_dma);
1015 lp->fd_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -ENOMEM;
1017 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001018#else
1019 lp->rx_skbs[i].skb =
1020 alloc_rxbuf_skb(dev, lp->pci_dev,
1021 &lp->rx_skbs[i].skb_dma);
1022 if (!lp->rx_skbs[i].skb) {
1023 while (--i >= 0) {
1024 free_rxbuf_skb(lp->pci_dev,
1025 lp->rx_skbs[i].skb,
1026 lp->rx_skbs[i].skb_dma);
1027 lp->rx_skbs[i].skb = NULL;
1028 }
1029 pci_free_consistent(lp->pci_dev,
1030 PAGE_SIZE * FD_PAGE_NUM,
1031 lp->fd_buf,
1032 lp->fd_buf_dma);
1033 lp->fd_buf = NULL;
1034 return -ENOMEM;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036#endif
1037 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001038 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
1039 dev->name, lp->fd_buf);
1040#ifdef TC35815_USE_PACKEDBUFFER
1041 printk(" DataBuf");
1042 for (i = 0; i < RX_BUF_NUM; i++)
1043 printk(" %p", lp->data_buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001045 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 } else {
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001047 for (i = 0; i < FD_PAGE_NUM; i++)
1048 clear_page((void *)((unsigned long)lp->fd_buf +
1049 i * PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 fd_addr = (unsigned long)lp->fd_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* Free Descriptors (for Receive) */
1054 lp->rfd_base = (struct RxFD *)fd_addr;
1055 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001056 for (i = 0; i < RX_FD_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 lp->rfd_cur = lp->rfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001059 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /* Transmit Descriptors */
1062 lp->tfd_base = (struct TxFD *)fd_addr;
1063 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
1064 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001065 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
1066 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
1068 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001069 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 -07001070 lp->tfd_start = 0;
1071 lp->tfd_end = 0;
1072
1073 /* Buffer List (for Receive) */
1074 lp->fbl_ptr = (struct FrFD *)fd_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001075 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
1076 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
1077#ifndef TC35815_USE_PACKEDBUFFER
1078 /*
1079 * move all allocated skbs to head of rx_skbs[] array.
1080 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
1081 * tc35815_rx() had failed.
1082 */
1083 lp->fbl_count = 0;
1084 for (i = 0; i < RX_BUF_NUM; i++) {
1085 if (lp->rx_skbs[i].skb) {
1086 if (i != lp->fbl_count) {
1087 lp->rx_skbs[lp->fbl_count].skb =
1088 lp->rx_skbs[i].skb;
1089 lp->rx_skbs[lp->fbl_count].skb_dma =
1090 lp->rx_skbs[i].skb_dma;
1091 }
1092 lp->fbl_count++;
1093 }
1094 }
1095#endif
1096 for (i = 0; i < RX_BUF_NUM; i++) {
1097#ifdef TC35815_USE_PACKEDBUFFER
1098 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
1099#else
1100 if (i >= lp->fbl_count) {
1101 lp->fbl_ptr->bd[i].BuffData = 0;
1102 lp->fbl_ptr->bd[i].BDCtl = 0;
1103 continue;
1104 }
1105 lp->fbl_ptr->bd[i].BuffData =
1106 cpu_to_le32(lp->rx_skbs[i].skb_dma);
1107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* BDID is index of FrFD.bd[] */
1109 lp->fbl_ptr->bd[i].BDCtl =
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001110 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
1111 RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001113#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 lp->fbl_curid = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001117 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
1118 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120}
1121
1122static void
1123tc35815_clear_queues(struct net_device *dev)
1124{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001125 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int i;
1127
1128 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001129 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1130 struct sk_buff *skb =
1131 fdsystem != 0xffffffff ?
1132 lp->tx_skbs[fdsystem].skb : NULL;
1133#ifdef DEBUG
1134 if (lp->tx_skbs[i].skb != skb) {
1135 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1136 panic_queues(dev);
1137 }
1138#else
1139 BUG_ON(lp->tx_skbs[i].skb != skb);
1140#endif
1141 if (skb) {
1142 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1143 lp->tx_skbs[i].skb = NULL;
1144 lp->tx_skbs[i].skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001146 }
1147 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150 tc35815_init_queues(dev);
1151}
1152
1153static void
1154tc35815_free_queues(struct net_device *dev)
1155{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001156 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int i;
1158
1159 if (lp->tfd_base) {
1160 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001161 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1162 struct sk_buff *skb =
1163 fdsystem != 0xffffffff ?
1164 lp->tx_skbs[fdsystem].skb : NULL;
1165#ifdef DEBUG
1166 if (lp->tx_skbs[i].skb != skb) {
1167 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1168 panic_queues(dev);
1169 }
1170#else
1171 BUG_ON(lp->tx_skbs[i].skb != skb);
1172#endif
1173 if (skb) {
1174 dev_kfree_skb(skb);
1175 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1176 lp->tx_skbs[i].skb = NULL;
1177 lp->tx_skbs[i].skb_dma = 0;
1178 }
1179 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 }
1182
1183 lp->rfd_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 lp->rfd_limit = NULL;
1185 lp->rfd_cur = NULL;
1186 lp->fbl_ptr = NULL;
1187
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001188 for (i = 0; i < RX_BUF_NUM; i++) {
1189#ifdef TC35815_USE_PACKEDBUFFER
1190 if (lp->data_buf[i]) {
1191 free_rxbuf_page(lp->pci_dev,
1192 lp->data_buf[i], lp->data_buf_dma[i]);
1193 lp->data_buf[i] = NULL;
1194 }
1195#else
1196 if (lp->rx_skbs[i].skb) {
1197 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1198 lp->rx_skbs[i].skb_dma);
1199 lp->rx_skbs[i].skb = NULL;
1200 }
1201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001203 if (lp->fd_buf) {
1204 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1205 lp->fd_buf, lp->fd_buf_dma);
1206 lp->fd_buf = NULL;
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210static void
1211dump_txfd(struct TxFD *fd)
1212{
1213 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1214 le32_to_cpu(fd->fd.FDNext),
1215 le32_to_cpu(fd->fd.FDSystem),
1216 le32_to_cpu(fd->fd.FDStat),
1217 le32_to_cpu(fd->fd.FDCtl));
1218 printk("BD: ");
1219 printk(" %08x %08x",
1220 le32_to_cpu(fd->bd.BuffData),
1221 le32_to_cpu(fd->bd.BDCtl));
1222 printk("\n");
1223}
1224
1225static int
1226dump_rxfd(struct RxFD *fd)
1227{
1228 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1229 if (bd_count > 8)
1230 bd_count = 8;
1231 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1232 le32_to_cpu(fd->fd.FDNext),
1233 le32_to_cpu(fd->fd.FDSystem),
1234 le32_to_cpu(fd->fd.FDStat),
1235 le32_to_cpu(fd->fd.FDCtl));
1236 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 printk("BD: ");
1239 for (i = 0; i < bd_count; i++)
1240 printk(" %08x %08x",
1241 le32_to_cpu(fd->bd[i].BuffData),
1242 le32_to_cpu(fd->bd[i].BDCtl));
1243 printk("\n");
1244 return bd_count;
1245}
1246
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001247#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static void
1249dump_frfd(struct FrFD *fd)
1250{
1251 int i;
1252 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1253 le32_to_cpu(fd->fd.FDNext),
1254 le32_to_cpu(fd->fd.FDSystem),
1255 le32_to_cpu(fd->fd.FDStat),
1256 le32_to_cpu(fd->fd.FDCtl));
1257 printk("BD: ");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001258 for (i = 0; i < RX_BUF_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 printk(" %08x %08x",
1260 le32_to_cpu(fd->bd[i].BuffData),
1261 le32_to_cpu(fd->bd[i].BDCtl));
1262 printk("\n");
1263}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001266#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267static void
1268panic_queues(struct net_device *dev)
1269{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001270 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int i;
1272
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001273 printk("TxFD base %p, start %u, end %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1275 printk("RxFD base %p limit %p cur %p\n",
1276 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1277 printk("FrFD %p\n", lp->fbl_ptr);
1278 for (i = 0; i < TX_FD_NUM; i++)
1279 dump_txfd(&lp->tfd_base[i]);
1280 for (i = 0; i < RX_FD_NUM; i++) {
1281 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1282 i += (bd_count + 1) / 2; /* skip BDs */
1283 }
1284 dump_frfd(lp->fbl_ptr);
1285 panic("%s: Illegal queue state.", dev->name);
1286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#endif
1288
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001289static void print_eth(const u8 *add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001291 printk(KERN_DEBUG "print_eth(%p)\n", add);
Johannes Berge1749612008-10-27 15:59:26 -07001292 printk(KERN_DEBUG " %pM => %pM : %02x%02x\n",
1293 add + 6, add, add[12], add[13]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001296static int tc35815_tx_full(struct net_device *dev)
1297{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001298 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001299 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1300}
1301
1302static void tc35815_restart(struct net_device *dev)
1303{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001304 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001305
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001306 if (lp->phy_dev) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001307 int timeout;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001308
1309 phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001310 timeout = 100;
1311 while (--timeout) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001312 if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET))
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001313 break;
1314 udelay(1);
1315 }
1316 if (!timeout)
1317 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1318 }
1319
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001320 spin_lock_irq(&lp->lock);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001321 tc35815_chip_reset(dev);
1322 tc35815_clear_queues(dev);
1323 tc35815_chip_init(dev);
1324 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1325 tc35815_set_multicast_list(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001326 spin_unlock_irq(&lp->lock);
1327
1328 netif_wake_queue(dev);
1329}
1330
1331static void tc35815_restart_work(struct work_struct *work)
1332{
1333 struct tc35815_local *lp =
1334 container_of(work, struct tc35815_local, restart_work);
1335 struct net_device *dev = lp->dev;
1336
1337 tc35815_restart(dev);
1338}
1339
1340static void tc35815_schedule_restart(struct net_device *dev)
1341{
1342 struct tc35815_local *lp = netdev_priv(dev);
1343 struct tc35815_regs __iomem *tr =
1344 (struct tc35815_regs __iomem *)dev->base_addr;
1345
1346 /* disable interrupts */
1347 tc_writel(0, &tr->Int_En);
1348 tc_writel(tc_readl(&tr->DMA_Ctl) | DMA_IntMask, &tr->DMA_Ctl);
1349 schedule_work(&lp->restart_work);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001350}
1351
1352static void tc35815_tx_timeout(struct net_device *dev)
1353{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001354 struct tc35815_regs __iomem *tr =
1355 (struct tc35815_regs __iomem *)dev->base_addr;
1356
1357 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1358 dev->name, tc_readl(&tr->Tx_Stat));
1359
1360 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001361 tc35815_schedule_restart(dev);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001362 dev->stats.tx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/*
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001366 * Open/initialize the controller. This is called (in the current kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * sometime after booting when the 'ifconfig' program is run.
1368 *
1369 * This routine should set everything up anew at each open, even
1370 * registers that "should" only need to be set once at boot, so that
1371 * there is non-reboot way to recover if something goes wrong.
1372 */
1373static int
1374tc35815_open(struct net_device *dev)
1375{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001376 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * This is used if the interrupt line can turned off (shared).
1380 * See 3c503.c for an example of selecting the IRQ at config-time.
1381 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001382 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED,
1383 dev->name, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 tc35815_chip_reset(dev);
1387
1388 if (tc35815_init_queues(dev) != 0) {
1389 free_irq(dev->irq, dev);
1390 return -EAGAIN;
1391 }
1392
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001393 napi_enable(&lp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* Reset the hardware here. Don't forget to set the station address. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001396 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 tc35815_chip_init(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001398 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Atsushi Nemoto59524a32008-06-25 11:41:01 +09001400 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001401 /* schedule a link state check */
1402 phy_start(lp->phy_dev);
1403
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001404 /* We are now ready to accept transmit requeusts from
1405 * the queueing layer of the networking.
1406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 netif_start_queue(dev);
1408
1409 return 0;
1410}
1411
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001412/* This will only be invoked if your driver is _not_ in XOFF state.
1413 * What this means is that you need not check it, and that this
1414 * invariant will hold if you make sure that the netif_*_queue()
1415 * calls are done at the proper times.
1416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1418{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001419 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001420 struct TxFD *txfd;
1421 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001423 /* If some error occurs while trying to transmit this
1424 * packet, you should return '1' from this function.
1425 * In such a case you _may not_ do anything to the
1426 * SKB, it is still owned by the network queueing
1427 * layer when an error is returned. This means you
1428 * may not modify any SKB fields, you may not free
1429 * the SKB, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001432 /* This is the most common case for modern hardware.
1433 * The spinlock protects this code from the TX complete
1434 * hardware interrupt handler. Queue flow control is
1435 * thus managed under this lock as well.
1436 */
1437 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001439 /* failsafe... (handle txdone now if half of FDs are used) */
1440 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1441 TX_FD_NUM / 2)
1442 tc35815_txdone(dev);
1443
1444 if (netif_msg_pktdata(lp))
1445 print_eth(skb->data);
1446#ifdef DEBUG
1447 if (lp->tx_skbs[lp->tfd_start].skb) {
1448 printk("%s: tx_skbs conflict.\n", dev->name);
1449 panic_queues(dev);
1450 }
1451#else
1452 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001454 lp->tx_skbs[lp->tfd_start].skb = skb;
1455 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 -07001456
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001457 /*add to ring */
1458 txfd = &lp->tfd_base[lp->tfd_start];
1459 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1460 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1461 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1462 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001464 if (lp->tfd_start == lp->tfd_end) {
1465 struct tc35815_regs __iomem *tr =
1466 (struct tc35815_regs __iomem *)dev->base_addr;
1467 /* Start DMA Transmitter. */
1468 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#ifdef GATHER_TXINT
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001470 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001472 if (netif_msg_tx_queued(lp)) {
1473 printk("%s: starting TxFD.\n", dev->name);
1474 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001476 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1477 } else {
1478 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1479 if (netif_msg_tx_queued(lp)) {
1480 printk("%s: queueing TxFD.\n", dev->name);
1481 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001483 }
1484 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1485
1486 dev->trans_start = jiffies;
1487
1488 /* If we just used up the very last entry in the
1489 * TX ring on this device, tell the queueing
1490 * layer to send no more.
1491 */
1492 if (tc35815_tx_full(dev)) {
1493 if (netif_msg_tx_queued(lp))
1494 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1495 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001498 /* When the TX completion hw interrupt arrives, this
1499 * is when the transmit statistics are updated.
1500 */
1501
1502 spin_unlock_irqrestore(&lp->lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001503 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506#define FATAL_ERROR_INT \
1507 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001508static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 static int count;
1511 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1512 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (status & Int_IntPCI)
1514 printk(" IntPCI");
1515 if (status & Int_DmParErr)
1516 printk(" DmParErr");
1517 if (status & Int_IntNRAbt)
1518 printk(" IntNRAbt");
1519 printk("\n");
1520 if (count++ > 100)
1521 panic("%s: Too many fatal errors.", dev->name);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001522 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001524 tc35815_schedule_restart(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001525}
1526
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001527static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001528{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001529 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001530 int ret = -1;
1531
1532 /* Fatal errors... */
1533 if (status & FATAL_ERROR_INT) {
1534 tc35815_fatal_error_interrupt(dev, status);
1535 return 0;
1536 }
1537 /* recoverable errors */
1538 if (status & Int_IntFDAEx) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001539 if (netif_msg_rx_err(lp))
1540 dev_warn(&dev->dev,
1541 "Free Descriptor Area Exhausted (%#x).\n",
1542 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001543 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001544 ret = 0;
1545 }
1546 if (status & Int_IntBLEx) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001547 if (netif_msg_rx_err(lp))
1548 dev_warn(&dev->dev,
1549 "Buffer List Exhausted (%#x).\n",
1550 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_IntExBD) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001555 if (netif_msg_rx_err(lp))
1556 dev_warn(&dev->dev,
1557 "Excessive Buffer Descriptiors (%#x).\n",
1558 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001559 dev->stats.rx_length_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001560 ret = 0;
1561 }
1562
1563 /* normal notification */
1564 if (status & Int_IntMacRx) {
1565 /* Got a packet(s). */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001566 ret = tc35815_rx(dev, limit);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001567 lp->lstats.rx_ints++;
1568 }
1569 if (status & Int_IntMacTx) {
1570 /* Transmit complete. */
1571 lp->lstats.tx_ints++;
1572 tc35815_txdone(dev);
1573 netif_wake_queue(dev);
Atsushi Nemoto02c5c8e2009-10-26 03:46:21 +00001574 if (ret < 0)
1575 ret = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001576 }
1577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/*
1581 * The typical workload of the driver:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001582 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 */
David Howells7d12e782006-10-05 14:55:46 +01001584static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct net_device *dev = dev_id;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001587 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001588 struct tc35815_regs __iomem *tr =
1589 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001590 u32 dmactl = tc_readl(&tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001592 if (!(dmactl & DMA_IntMask)) {
1593 /* disable interrupts */
1594 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
Ben Hutchings288379f2009-01-19 16:43:59 -08001595 if (napi_schedule_prep(&lp->napi))
1596 __napi_schedule(&lp->napi);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001597 else {
1598 printk(KERN_ERR "%s: interrupt taken in poll\n",
1599 dev->name);
1600 BUG();
1601 }
1602 (void)tc_readl(&tr->Int_Src); /* flush */
1603 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001605 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001608#ifdef CONFIG_NET_POLL_CONTROLLER
1609static void tc35815_poll_controller(struct net_device *dev)
1610{
1611 disable_irq(dev->irq);
1612 tc35815_interrupt(dev->irq, dev);
1613 enable_irq(dev->irq);
1614}
1615#endif
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617/* We have a good packet(s), get it/them out of the buffers. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001618static int
1619tc35815_rx(struct net_device *dev, int limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001621 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 unsigned int fdctl;
1623 int i;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001624 int received = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1627 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1628 int pkt_len = fdctl & FD_FDLength_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001630#ifdef DEBUG
1631 struct RxFD *next_rfd;
1632#endif
1633#if (RX_CTL_CMD & Rx_StripCRC) == 0
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001634 pkt_len -= ETH_FCS_LEN;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001637 if (netif_msg_rx_status(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 dump_rxfd(lp->rfd_cur);
1639 if (status & Rx_Good) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct sk_buff *skb;
1641 unsigned char *data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001642 int cur_bd;
1643#ifdef TC35815_USE_PACKEDBUFFER
1644 int offset;
1645#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001647 if (--limit < 0)
1648 break;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001649#ifdef TC35815_USE_PACKEDBUFFER
1650 BUG_ON(bd_count > 2);
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001651 skb = dev_alloc_skb(pkt_len + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (skb == NULL) {
1653 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
1654 dev->name);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001655 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 break;
1657 }
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001658 skb_reserve(skb, NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 data = skb_put(skb, pkt_len);
1661
1662 /* copy from receive buffer */
1663 cur_bd = 0;
1664 offset = 0;
1665 while (offset < pkt_len && cur_bd < bd_count) {
1666 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1667 BD_BuffLength_MASK;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001668 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1669 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1670 if (offset + len > pkt_len)
1671 len = pkt_len - offset;
1672#ifdef TC35815_DMA_SYNC_ONDEMAND
1673 pci_dma_sync_single_for_cpu(lp->pci_dev,
1674 dma, len,
1675 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676#endif
1677 memcpy(data + offset, rxbuf, len);
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +09001678#ifdef TC35815_DMA_SYNC_ONDEMAND
1679 pci_dma_sync_single_for_device(lp->pci_dev,
1680 dma, len,
1681 PCI_DMA_FROMDEVICE);
1682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 offset += len;
1684 cur_bd++;
1685 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001686#else /* TC35815_USE_PACKEDBUFFER */
1687 BUG_ON(bd_count > 1);
1688 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1689 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1690#ifdef DEBUG
1691 if (cur_bd >= RX_BUF_NUM) {
1692 printk("%s: invalid BDID.\n", dev->name);
1693 panic_queues(dev);
1694 }
1695 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1696 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1697 if (!lp->rx_skbs[cur_bd].skb) {
1698 printk("%s: NULL skb.\n", dev->name);
1699 panic_queues(dev);
1700 }
1701#else
1702 BUG_ON(cur_bd >= RX_BUF_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001704 skb = lp->rx_skbs[cur_bd].skb;
1705 prefetch(skb->data);
1706 lp->rx_skbs[cur_bd].skb = NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001707 pci_unmap_single(lp->pci_dev,
1708 lp->rx_skbs[cur_bd].skb_dma,
1709 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001710 if (!HAVE_DMA_RXALIGN(lp) && NET_IP_ALIGN)
1711 memmove(skb->data, skb->data - NET_IP_ALIGN,
1712 pkt_len);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001713 data = skb_put(skb, pkt_len);
1714#endif /* TC35815_USE_PACKEDBUFFER */
1715 if (netif_msg_pktdata(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 print_eth(data);
1717 skb->protocol = eth_type_trans(skb, dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001718 netif_receive_skb(skb);
1719 received++;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001720 dev->stats.rx_packets++;
1721 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 } else {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001723 dev->stats.rx_errors++;
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001724 if (netif_msg_rx_err(lp))
1725 dev_info(&dev->dev, "Rx error (status %x)\n",
1726 status & Rx_Stat_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1728 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1729 status &= ~(Rx_LongErr|Rx_CRCErr);
1730 status |= Rx_Over;
1731 }
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001732 if (status & Rx_LongErr)
1733 dev->stats.rx_length_errors++;
1734 if (status & Rx_Over)
1735 dev->stats.rx_fifo_errors++;
1736 if (status & Rx_CRCErr)
1737 dev->stats.rx_crc_errors++;
1738 if (status & Rx_Align)
1739 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
1742 if (bd_count > 0) {
1743 /* put Free Buffer back to controller */
1744 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1745 unsigned char id =
1746 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001747#ifdef DEBUG
1748 if (id >= RX_BUF_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 printk("%s: invalid BDID.\n", dev->name);
1750 panic_queues(dev);
1751 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001752#else
1753 BUG_ON(id >= RX_BUF_NUM);
1754#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /* free old buffers */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001756#ifdef TC35815_USE_PACKEDBUFFER
1757 while (lp->fbl_curid != id)
1758#else
Atsushi Nemotoccc57aa2008-06-26 17:14:15 +09001759 lp->fbl_count--;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001760 while (lp->fbl_count < RX_BUF_NUM)
1761#endif
1762 {
1763#ifdef TC35815_USE_PACKEDBUFFER
1764 unsigned char curid = lp->fbl_curid;
1765#else
1766 unsigned char curid =
1767 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1768#endif
1769 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1770#ifdef DEBUG
1771 bdctl = le32_to_cpu(bd->BDCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (bdctl & BD_CownsBD) {
1773 printk("%s: Freeing invalid BD.\n",
1774 dev->name);
1775 panic_queues(dev);
1776 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001777#endif
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001778 /* pass BD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001779#ifndef TC35815_USE_PACKEDBUFFER
1780 if (!lp->rx_skbs[curid].skb) {
1781 lp->rx_skbs[curid].skb =
1782 alloc_rxbuf_skb(dev,
1783 lp->pci_dev,
1784 &lp->rx_skbs[curid].skb_dma);
1785 if (!lp->rx_skbs[curid].skb)
1786 break; /* try on next reception */
1787 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1788 }
1789#endif /* TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* Note: BDLength was modified by chip. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001791 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1792 (curid << BD_RxBDID_SHIFT) |
1793 RX_BUF_SIZE);
1794#ifdef TC35815_USE_PACKEDBUFFER
1795 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1796 if (netif_msg_rx_status(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 printk("%s: Entering new FBD %d\n",
1798 dev->name, lp->fbl_curid);
1799 dump_frfd(lp->fbl_ptr);
1800 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001801#else
1802 lp->fbl_count++;
1803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 }
1806
1807 /* put RxFD back to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001808#ifdef DEBUG
1809 next_rfd = fd_bus_to_virt(lp,
1810 le32_to_cpu(lp->rfd_cur->fd.FDNext));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1812 printk("%s: RxFD FDNext invalid.\n", dev->name);
1813 panic_queues(dev);
1814 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001817 /* pass FD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001818#ifdef DEBUG
1819 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1820#else
1821 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1824 lp->rfd_cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001826 if (lp->rfd_cur > lp->rfd_limit)
1827 lp->rfd_cur = lp->rfd_base;
1828#ifdef DEBUG
1829 if (lp->rfd_cur != next_rfd)
1830 printk("rfd_cur = %p, next_rfd %p\n",
1831 lp->rfd_cur, next_rfd);
1832#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001835 return received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001838static int tc35815_poll(struct napi_struct *napi, int budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001839{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001840 struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
1841 struct net_device *dev = lp->dev;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001842 struct tc35815_regs __iomem *tr =
1843 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001844 int received = 0, handled;
1845 u32 status;
1846
1847 spin_lock(&lp->lock);
1848 status = tc_readl(&tr->Int_Src);
1849 do {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001850 /* BLEx, FDAEx will be cleared later */
1851 tc_writel(status & ~(Int_BLEx | Int_FDAEx),
1852 &tr->Int_Src); /* write to clear */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001853
Atsushi Nemotoa2c465d2009-04-02 01:17:36 -07001854 handled = tc35815_do_interrupt(dev, status, budget - received);
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001855 if (status & (Int_BLEx | Int_FDAEx))
1856 tc_writel(status & (Int_BLEx | Int_FDAEx),
1857 &tr->Int_Src);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001858 if (handled >= 0) {
1859 received += handled;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001860 if (received >= budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001861 break;
1862 }
1863 status = tc_readl(&tr->Int_Src);
1864 } while (status);
1865 spin_unlock(&lp->lock);
1866
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001867 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001868 napi_complete(napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001869 /* enable interrupts */
1870 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1871 }
1872 return received;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001873}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875#ifdef NO_CHECK_CARRIER
1876#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1877#else
1878#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1879#endif
1880
1881static void
1882tc35815_check_tx_stat(struct net_device *dev, int status)
1883{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001884 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 const char *msg = NULL;
1886
1887 /* count collisions */
1888 if (status & Tx_ExColl)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001889 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (status & Tx_TxColl_MASK)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001891 dev->stats.collisions += status & Tx_TxColl_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001893#ifndef NO_CHECK_CARRIER
1894 /* TX4939 does not have NCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001895 if (lp->chiptype == TC35815_TX4939)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 status &= ~Tx_NCarr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001897#ifdef WORKAROUND_LOSTCAR
1898 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001899 if (!lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001900 status &= ~Tx_NCarr;
1901#endif
1902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 if (!(status & TX_STA_ERR)) {
1905 /* no error. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001906 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return;
1908 }
1909
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001910 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (status & Tx_ExColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001912 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 msg = "Excessive Collision.";
1914 }
1915 if (status & Tx_Under) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001916 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 msg = "Tx FIFO Underrun.";
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001918 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1919 lp->lstats.tx_underrun++;
1920 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1921 struct tc35815_regs __iomem *tr =
1922 (struct tc35815_regs __iomem *)dev->base_addr;
1923 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1924 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1925 }
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 if (status & Tx_Defer) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001929 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 msg = "Excessive Deferral.";
1931 }
1932#ifndef NO_CHECK_CARRIER
1933 if (status & Tx_NCarr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001934 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 msg = "Lost Carrier Sense.";
1936 }
1937#endif
1938 if (status & Tx_LateColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001939 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 msg = "Late Collision.";
1941 }
1942 if (status & Tx_TxPar) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001943 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 msg = "Transmit Parity Error.";
1945 }
1946 if (status & Tx_SQErr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001947 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 msg = "Signal Quality Error.";
1949 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001950 if (msg && netif_msg_tx_err(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
1952}
1953
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001954/* This handles TX complete events posted by the device
1955 * via interrupts.
1956 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957static void
1958tc35815_txdone(struct net_device *dev)
1959{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001960 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 struct TxFD *txfd;
1962 unsigned int fdctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 txfd = &lp->tfd_base[lp->tfd_end];
1965 while (lp->tfd_start != lp->tfd_end &&
1966 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
1967 int status = le32_to_cpu(txfd->fd.FDStat);
1968 struct sk_buff *skb;
1969 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001970 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001972 if (netif_msg_tx_done(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 printk("%s: complete TxFD.\n", dev->name);
1974 dump_txfd(txfd);
1975 }
1976 tc35815_check_tx_stat(dev, status);
1977
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001978 skb = fdsystem != 0xffffffff ?
1979 lp->tx_skbs[fdsystem].skb : NULL;
1980#ifdef DEBUG
1981 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
1982 printk("%s: tx_skbs mismatch.\n", dev->name);
1983 panic_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001985#else
1986 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
1987#endif
1988 if (skb) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001989 dev->stats.tx_bytes += skb->len;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001990 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
1991 lp->tx_skbs[lp->tfd_end].skb = NULL;
1992 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001993 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001994 }
1995 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
1998 txfd = &lp->tfd_base[lp->tfd_end];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001999#ifdef DEBUG
2000 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 printk("%s: TxFD FDNext invalid.\n", dev->name);
2002 panic_queues(dev);
2003 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (fdnext & FD_Next_EOL) {
2006 /* DMA Transmitter has been stopping... */
2007 if (lp->tfd_end != lp->tfd_start) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002008 struct tc35815_regs __iomem *tr =
2009 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002011 struct TxFD *txhead = &lp->tfd_base[head];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 int qlen = (lp->tfd_start + TX_FD_NUM
2013 - lp->tfd_end) % TX_FD_NUM;
2014
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002015#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
2017 printk("%s: TxFD FDCtl invalid.\n", dev->name);
2018 panic_queues(dev);
2019 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /* log max queue length */
2022 if (lp->lstats.max_tx_qlen < qlen)
2023 lp->lstats.max_tx_qlen = qlen;
2024
2025
2026 /* start DMA Transmitter again */
2027 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
2028#ifdef GATHER_TXINT
2029 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
2030#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002031 if (netif_msg_tx_queued(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 printk("%s: start TxFD on queue.\n",
2033 dev->name);
2034 dump_txfd(txfd);
2035 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002036 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038 break;
2039 }
2040 }
2041
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002042 /* If we had stopped the queue due to a "tx full"
2043 * condition, and space has now been made available,
2044 * wake up the queue.
2045 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002046 if (netif_queue_stopped(dev) && !tc35815_tx_full(dev))
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002047 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050/* The inverse routine to tc35815_open(). */
2051static int
2052tc35815_close(struct net_device *dev)
2053{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002054 struct tc35815_local *lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002057 napi_disable(&lp->napi);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002058 if (lp->phy_dev)
2059 phy_stop(lp->phy_dev);
2060 cancel_work_sync(&lp->restart_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 /* Flush the Tx and disable Rx here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 tc35815_chip_reset(dev);
2064 free_irq(dev->irq, dev);
2065
2066 tc35815_free_queues(dev);
2067
2068 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072/*
2073 * Get the current statistics.
2074 * This may be called with the card open or closed.
2075 */
2076static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
2077{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002078 struct tc35815_regs __iomem *tr =
2079 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002080 if (netif_running(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* Update the statistics from the device registers. */
Atsushi Nemoto7bb82e82009-08-06 04:41:48 +00002082 dev->stats.rx_missed_errors += tc_readl(&tr->Miss_Cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002084 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002087static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002089 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002090 struct tc35815_regs __iomem *tr =
2091 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 int cam_index = index * 6;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002093 u32 cam_data;
2094 u32 saved_addr;
Atsushi Nemoto958eb802008-04-11 00:24:24 +09002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 saved_addr = tc_readl(&tr->CAM_Adr);
2097
Atsushi Nemoto958eb802008-04-11 00:24:24 +09002098 if (netif_msg_hw(lp))
Johannes Berge1749612008-10-27 15:59:26 -07002099 printk(KERN_DEBUG "%s: CAM %d: %pM\n",
2100 dev->name, index, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (index & 1) {
2102 /* read modify write */
2103 tc_writel(cam_index - 2, &tr->CAM_Adr);
2104 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
2105 cam_data |= addr[0] << 8 | addr[1];
2106 tc_writel(cam_data, &tr->CAM_Data);
2107 /* write whole word */
2108 tc_writel(cam_index + 2, &tr->CAM_Adr);
2109 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
2110 tc_writel(cam_data, &tr->CAM_Data);
2111 } else {
2112 /* write whole word */
2113 tc_writel(cam_index, &tr->CAM_Adr);
2114 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
2115 tc_writel(cam_data, &tr->CAM_Data);
2116 /* read modify write */
2117 tc_writel(cam_index + 4, &tr->CAM_Adr);
2118 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
2119 cam_data |= addr[4] << 24 | (addr[5] << 16);
2120 tc_writel(cam_data, &tr->CAM_Data);
2121 }
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 tc_writel(saved_addr, &tr->CAM_Adr);
2124}
2125
2126
2127/*
2128 * Set or clear the multicast filter for this adaptor.
2129 * num_addrs == -1 Promiscuous mode, receive all packets
2130 * num_addrs == 0 Normal mode, clear multicast list
2131 * num_addrs > 0 Multicast mode, receive normal and MC packets,
2132 * and do best-effort filtering.
2133 */
2134static void
2135tc35815_set_multicast_list(struct net_device *dev)
2136{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002137 struct tc35815_regs __iomem *tr =
2138 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002140 if (dev->flags & IFF_PROMISC) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002141#ifdef WORKAROUND_100HALF_PROMISC
2142 /* With some (all?) 100MHalf HUB, controller will hang
2143 * if we enabled promiscuous mode before linkup... */
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002144 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002145
2146 if (!lp->link)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002147 return;
2148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* Enable promiscuous mode */
2150 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002151 } else if ((dev->flags & IFF_ALLMULTI) ||
2152 dev->mc_count > CAM_ENTRY_MAX - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /* CAM 0, 1, 20 are reserved. */
2154 /* Disable promiscuous mode, use normal mode. */
2155 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002156 } else if (dev->mc_count) {
2157 struct dev_mc_list *cur_addr = dev->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int i;
2159 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
2160
2161 tc_writel(0, &tr->CAM_Ctl);
2162 /* Walk the address list, and load the filter */
2163 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
2164 if (!cur_addr)
2165 break;
2166 /* entry 0,1 is reserved. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002167 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 ena_bits |= CAM_Ena_Bit(i + 2);
2169 }
2170 tc_writel(ena_bits, &tr->CAM_Ena);
2171 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002172 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2174 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2175 }
2176}
2177
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002178static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002180 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002181 strcpy(info->driver, MODNAME);
2182 strcpy(info->version, DRV_VERSION);
2183 strcpy(info->bus_info, pci_name(lp->pci_dev));
2184}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002185
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002186static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2187{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002188 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002189
2190 if (!lp->phy_dev)
2191 return -ENODEV;
2192 return phy_ethtool_gset(lp->phy_dev, cmd);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002193}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002195static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2196{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002197 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002198
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002199 if (!lp->phy_dev)
2200 return -ENODEV;
2201 return phy_ethtool_sset(lp->phy_dev, cmd);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002202}
2203
2204static u32 tc35815_get_msglevel(struct net_device *dev)
2205{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002206 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002207 return lp->msg_enable;
2208}
2209
2210static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2211{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002212 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002213 lp->msg_enable = datum;
2214}
2215
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002216static int tc35815_get_sset_count(struct net_device *dev, int sset)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002217{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002218 struct tc35815_local *lp = netdev_priv(dev);
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002219
2220 switch (sset) {
2221 case ETH_SS_STATS:
2222 return sizeof(lp->lstats) / sizeof(int);
2223 default:
2224 return -EOPNOTSUPP;
2225 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002226}
2227
2228static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2229{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002230 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002231 data[0] = lp->lstats.max_tx_qlen;
2232 data[1] = lp->lstats.tx_ints;
2233 data[2] = lp->lstats.rx_ints;
2234 data[3] = lp->lstats.tx_underrun;
2235}
2236
2237static struct {
2238 const char str[ETH_GSTRING_LEN];
2239} ethtool_stats_keys[] = {
2240 { "max_tx_qlen" },
2241 { "tx_ints" },
2242 { "rx_ints" },
2243 { "tx_underrun" },
2244};
2245
2246static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2247{
2248 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2249}
2250
2251static const struct ethtool_ops tc35815_ethtool_ops = {
2252 .get_drvinfo = tc35815_get_drvinfo,
2253 .get_settings = tc35815_get_settings,
2254 .set_settings = tc35815_set_settings,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002255 .get_link = ethtool_op_get_link,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002256 .get_msglevel = tc35815_get_msglevel,
2257 .set_msglevel = tc35815_set_msglevel,
2258 .get_strings = tc35815_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002259 .get_sset_count = tc35815_get_sset_count,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002260 .get_ethtool_stats = tc35815_get_ethtool_stats,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002261};
2262
2263static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2264{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002265 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002266
2267 if (!netif_running(dev))
2268 return -EINVAL;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002269 if (!lp->phy_dev)
2270 return -ENODEV;
2271 return phy_mii_ioctl(lp->phy_dev, if_mii(rq), cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
2274static void tc35815_chip_reset(struct net_device *dev)
2275{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002276 struct tc35815_regs __iomem *tr =
2277 (struct tc35815_regs __iomem *)dev->base_addr;
2278 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* reset the controller */
2280 tc_writel(MAC_Reset, &tr->MAC_Ctl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002281 udelay(4); /* 3200ns */
2282 i = 0;
2283 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2284 if (i++ > 100) {
2285 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2286 break;
2287 }
2288 mdelay(1);
2289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 tc_writel(0, &tr->MAC_Ctl);
2291
2292 /* initialize registers to default value */
2293 tc_writel(0, &tr->DMA_Ctl);
2294 tc_writel(0, &tr->TxThrsh);
2295 tc_writel(0, &tr->TxPollCtr);
2296 tc_writel(0, &tr->RxFragSize);
2297 tc_writel(0, &tr->Int_En);
2298 tc_writel(0, &tr->FDA_Bas);
2299 tc_writel(0, &tr->FDA_Lim);
2300 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2301 tc_writel(0, &tr->CAM_Ctl);
2302 tc_writel(0, &tr->Tx_Ctl);
2303 tc_writel(0, &tr->Rx_Ctl);
2304 tc_writel(0, &tr->CAM_Ena);
2305 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2306
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002307 /* initialize internal SRAM */
2308 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2309 for (i = 0; i < 0x1000; i += 4) {
2310 tc_writel(i, &tr->CAM_Adr);
2311 tc_writel(0, &tr->CAM_Data);
2312 }
2313 tc_writel(0, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
2316static void tc35815_chip_init(struct net_device *dev)
2317{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002318 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002319 struct tc35815_regs __iomem *tr =
2320 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 unsigned long txctl = TX_CTL_CMD;
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /* load station address to CAM */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002324 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 /* Enable CAM (broadcast and unicast) */
2327 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2328 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2329
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002330 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2331 if (HAVE_DMA_RXALIGN(lp))
2332 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2333 else
2334 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2335#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2339 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2340 tc_writel(INT_EN_CMD, &tr->Int_En);
2341
2342 /* set queues */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002343 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2345 &tr->FDA_Lim);
2346 /*
2347 * Activation method:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002348 * First, enable the MAC Transmitter and the DMA Receive circuits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 * Then enable the DMA Transmitter and the MAC Receive circuits.
2350 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002351 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /* start MAC transmitter */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002355#ifndef NO_CHECK_CARRIER
2356 /* TX4939 does not have EnLCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002357 if (lp->chiptype == TC35815_TX4939)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002358 txctl &= ~Tx_EnLCarr;
2359#ifdef WORKAROUND_LOSTCAR
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002361 if (!lp->phy_dev || !lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002362 txctl &= ~Tx_EnLCarr;
2363#endif
2364#endif /* !NO_CHECK_CARRIER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365#ifdef GATHER_TXINT
2366 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
2367#endif
2368 tc_writel(txctl, &tr->Tx_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002371#ifdef CONFIG_PM
2372static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2373{
2374 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002375 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002376 unsigned long flags;
2377
2378 pci_save_state(pdev);
2379 if (!netif_running(dev))
2380 return 0;
2381 netif_device_detach(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002382 if (lp->phy_dev)
2383 phy_stop(lp->phy_dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002384 spin_lock_irqsave(&lp->lock, flags);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002385 tc35815_chip_reset(dev);
2386 spin_unlock_irqrestore(&lp->lock, flags);
2387 pci_set_power_state(pdev, PCI_D3hot);
2388 return 0;
2389}
2390
2391static int tc35815_resume(struct pci_dev *pdev)
2392{
2393 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002394 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002395
2396 pci_restore_state(pdev);
2397 if (!netif_running(dev))
2398 return 0;
2399 pci_set_power_state(pdev, PCI_D0);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002400 tc35815_restart(dev);
Atsushi Nemoto59524a32008-06-25 11:41:01 +09002401 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002402 if (lp->phy_dev)
2403 phy_start(lp->phy_dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002404 netif_device_attach(dev);
2405 return 0;
2406}
2407#endif /* CONFIG_PM */
2408
2409static struct pci_driver tc35815_pci_driver = {
2410 .name = MODNAME,
2411 .id_table = tc35815_pci_tbl,
2412 .probe = tc35815_init_one,
2413 .remove = __devexit_p(tc35815_remove_one),
2414#ifdef CONFIG_PM
2415 .suspend = tc35815_suspend,
2416 .resume = tc35815_resume,
2417#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418};
2419
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002420module_param_named(speed, options.speed, int, 0);
2421MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2422module_param_named(duplex, options.duplex, int, 0);
2423MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425static int __init tc35815_init_module(void)
2426{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002427 return pci_register_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
2430static void __exit tc35815_cleanup_module(void)
2431{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002432 pci_unregister_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
Jeff Garzik420e8522007-02-24 17:02:16 -05002434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435module_init(tc35815_init_module);
2436module_exit(tc35815_cleanup_module);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002437
2438MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2439MODULE_LICENSE("GPL");