blob: 0d621ca5e27b9edf0f4347c50133ec5bb676785c [file] [log] [blame]
Atsushi Nemotoeea221c2007-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 Nemotoeea221c2007-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 Nemotoeea221c2007-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 Nemotoeea221c2007-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 Nemotob75c6db2009-10-26 03:46:22 +000025#define TC35815_NAPI
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090026#ifdef TC35815_NAPI
Atsushi Nemoto297713d2009-08-06 04:41:45 +000027#define DRV_VERSION "1.38-NAPI"
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090028#else
Atsushi Nemoto297713d2009-08-06 04:41:45 +000029#define DRV_VERSION "1.38"
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090030#endif
31static const char *version = "tc35815.c:v" DRV_VERSION "\n";
32#define MODNAME "tc35815"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/types.h>
37#include <linux/fcntl.h>
38#include <linux/interrupt.h>
39#include <linux/ioport.h>
40#include <linux/in.h>
Atsushi Nemoto82a99282008-12-11 20:58:04 -080041#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/slab.h>
43#include <linux/string.h>
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090044#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/errno.h>
46#include <linux/init.h>
47#include <linux/netdevice.h>
48#include <linux/etherdevice.h>
49#include <linux/skbuff.h>
50#include <linux/delay.h>
51#include <linux/pci.h>
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090052#include <linux/phy.h>
53#include <linux/workqueue.h>
Atsushi Nemotobd43da82007-06-29 22:34:53 +090054#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/byteorder.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* First, a few definitions that the brave might change. */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define GATHER_TXINT /* On-Demand Tx Interrupt */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090061#define WORKAROUND_LOSTCAR
62#define WORKAROUND_100HALF_PROMISC
63/* #define TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090065enum tc35815_chiptype {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090066 TC35815CF = 0,
67 TC35815_NWU,
68 TC35815_TX4939,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090069};
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090070
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090071/* indexed by tc35815_chiptype, above */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090072static const struct {
73 const char *name;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090074} chip_info[] __devinitdata = {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090075 { "TOSHIBA TC35815CF 10/100BaseTX" },
76 { "TOSHIBA TC35815 with Wake on LAN" },
77 { "TOSHIBA TC35815/TX4939" },
78};
79
80static const struct pci_device_id tc35815_pci_tbl[] = {
81 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
82 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
83 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
84 {0,}
85};
Atsushi Nemoto7f225b42008-04-11 00:25:31 +090086MODULE_DEVICE_TABLE(pci, tc35815_pci_tbl);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090087
88/* see MODULE_PARM_DESC */
89static struct tc35815_options {
90 int speed;
91 int duplex;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +090092} options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * Registers
96 */
97struct tc35815_regs {
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +090098 __u32 DMA_Ctl; /* 0x00 */
99 __u32 TxFrmPtr;
100 __u32 TxThrsh;
101 __u32 TxPollCtr;
102 __u32 BLFrmPtr;
103 __u32 RxFragSize;
104 __u32 Int_En;
105 __u32 FDA_Bas;
106 __u32 FDA_Lim; /* 0x20 */
107 __u32 Int_Src;
108 __u32 unused0[2];
109 __u32 PauseCnt;
110 __u32 RemPauCnt;
111 __u32 TxCtlFrmStat;
112 __u32 unused1;
113 __u32 MAC_Ctl; /* 0x40 */
114 __u32 CAM_Ctl;
115 __u32 Tx_Ctl;
116 __u32 Tx_Stat;
117 __u32 Rx_Ctl;
118 __u32 Rx_Stat;
119 __u32 MD_Data;
120 __u32 MD_CA;
121 __u32 CAM_Adr; /* 0x60 */
122 __u32 CAM_Data;
123 __u32 CAM_Ena;
124 __u32 PROM_Ctl;
125 __u32 PROM_Data;
126 __u32 Algn_Cnt;
127 __u32 CRC_Cnt;
128 __u32 Miss_Cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131/*
132 * Bit assignments
133 */
134/* DMA_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900135#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
136#define DMA_RxAlign_1 0x00400000
137#define DMA_RxAlign_2 0x00800000
138#define DMA_RxAlign_3 0x00c00000
139#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
140#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
141#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
142#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
143#define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
144#define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
145#define DMA_TestMode 0x00002000 /* 1:Test Mode */
146#define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
147#define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/* RxFragSize bit asign ---------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900150#define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
151#define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/* MAC_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900154#define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
155#define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
156#define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
157#define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
158#define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
159#define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
160#define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
161#define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
162#define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
163#define MAC_Reset 0x00000004 /* 1:Software Reset */
164#define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
165#define MAC_HaltReq 0x00000001 /* 1:Halt request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* PROM_Ctl bit asign ------------------------------------------------------ */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900168#define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
169#define PROM_Read 0x00004000 /*10:Read operation */
170#define PROM_Write 0x00002000 /*01:Write operation */
171#define PROM_Erase 0x00006000 /*11:Erase operation */
172 /*00:Enable or Disable Writting, */
173 /* as specified in PROM_Addr. */
174#define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
175 /*00xxxx: disable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* CAM_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900178#define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
179#define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
180 /* accept other */
181#define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
182#define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
183#define CAM_StationAcc 0x00000001 /* 1:unicast accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* CAM_Ena bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900186#define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900188#define CAM_Ena_Bit(index) (1 << (index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define CAM_ENTRY_DESTINATION 0
190#define CAM_ENTRY_SOURCE 1
191#define CAM_ENTRY_MACCTL 20
192
193/* Tx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900194#define Tx_En 0x00000001 /* 1:Transmit enable */
195#define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
196#define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
197#define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
198#define Tx_FBack 0x00000010 /* 1:Fast Back-off */
199#define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
200#define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
201#define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
202#define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
203#define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
204#define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
205#define Tx_EnComp 0x00004000 /* 1:Enable Completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/* Tx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900208#define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
209#define Tx_ExColl 0x00000010 /* Excessive Collision */
210#define Tx_TXDefer 0x00000020 /* Transmit Defered */
211#define Tx_Paused 0x00000040 /* Transmit Paused */
212#define Tx_IntTx 0x00000080 /* Interrupt on Tx */
213#define Tx_Under 0x00000100 /* Underrun */
214#define Tx_Defer 0x00000200 /* Deferral */
215#define Tx_NCarr 0x00000400 /* No Carrier */
216#define Tx_10Stat 0x00000800 /* 10Mbps Status */
217#define Tx_LateColl 0x00001000 /* Late Collision */
218#define Tx_TxPar 0x00002000 /* Tx Parity Error */
219#define Tx_Comp 0x00004000 /* Completion */
220#define Tx_Halted 0x00008000 /* Tx Halted */
221#define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223/* Rx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900224#define Rx_EnGood 0x00004000 /* 1:Enable Good */
225#define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
226#define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
227#define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
228#define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
229#define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
230#define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
231#define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
232#define Rx_ShortEn 0x00000008 /* 1:Short Enable */
233#define Rx_LongEn 0x00000004 /* 1:Long Enable */
234#define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
235#define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237/* Rx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900238#define Rx_Halted 0x00008000 /* Rx Halted */
239#define Rx_Good 0x00004000 /* Rx Good */
240#define Rx_RxPar 0x00002000 /* Rx Parity Error */
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900241#define Rx_TypePkt 0x00001000 /* Rx Type Packet */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900242#define Rx_LongErr 0x00000800 /* Rx Long Error */
243#define Rx_Over 0x00000400 /* Rx Overflow */
244#define Rx_CRCErr 0x00000200 /* Rx CRC Error */
245#define Rx_Align 0x00000100 /* Rx Alignment Error */
246#define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
247#define Rx_IntRx 0x00000040 /* Rx Interrupt */
248#define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900249#define Rx_InLenErr 0x00000010 /* Rx In Range Frame Length Error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900251#define Rx_Stat_Mask 0x0000FFF0 /* Rx All Status Mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/* Int_En bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900254#define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
255#define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Ctl Complete Enable */
256#define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
257#define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
258#define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
259#define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
260#define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
261#define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
262#define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
263#define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
264#define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
265#define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
266 /* Exhausted Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/* Int_Src bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900269#define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
270#define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
271#define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
272#define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
273#define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
274#define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
275#define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
276#define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
277#define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
278#define Int_SWInt 0x00000020 /* 1:Software request & Clear */
279#define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
280#define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
281#define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
282#define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
283#define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/* MD_CA bit asign --------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900286#define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
287#define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
288#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/*
292 * Descriptors
293 */
294
295/* Frame descripter */
296struct FDesc {
297 volatile __u32 FDNext;
298 volatile __u32 FDSystem;
299 volatile __u32 FDStat;
300 volatile __u32 FDCtl;
301};
302
303/* Buffer descripter */
304struct BDesc {
305 volatile __u32 BuffData;
306 volatile __u32 BDCtl;
307};
308
309#define FD_ALIGN 16
310
311/* Frame Descripter bit asign ---------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900312#define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
313#define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
314#define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900316#define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
317#define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
319#define FD_FrmOpt_Packing 0x04000000 /* Rx only */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900320#define FD_CownsFD 0x80000000 /* FD Controller owner bit */
321#define FD_Next_EOL 0x00000001 /* FD EOL indicator */
322#define FD_BDCnt_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324/* Buffer Descripter bit asign --------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900325#define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
326#define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
327#define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
328#define BD_CownsBD 0x80000000 /* BD Controller owner bit */
329#define BD_RxBDID_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#define BD_RxBDSeqN_SHIFT 24
331
332
333/* Some useful constants. */
334#undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */
335
336#ifdef NO_CHECK_CARRIER
337#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900338 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
339 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#else
341#define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900342 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
343 Tx_En) /* maybe 0x7b01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#endif
Atsushi Nemoto297713d2009-08-06 04:41:45 +0000345/* Do not use Rx_StripCRC -- it causes trouble on BLEx/FDAEx condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
Atsushi Nemoto297713d2009-08-06 04:41:45 +0000347 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#define INT_EN_CMD (Int_NRAbtEn | \
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900349 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
351 Int_STargAbtEn | \
352 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900353#define DMA_CTL_CMD DMA_BURST_SIZE
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900354#define HAVE_DMA_RXALIGN(lp) likely((lp)->chiptype != TC35815CF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356/* Tuning parameters */
357#define DMA_BURST_SIZE 32
358#define TX_THRESHOLD 1024
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900359/* used threshold with packet max byte for low pci transfer ability.*/
360#define TX_THRESHOLD_MAX 1536
361/* setting threshold max value when overrun error occured this count. */
362#define TX_THRESHOLD_KEEP_LIMIT 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900364/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
365#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#define FD_PAGE_NUM 2
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900367#define RX_BUF_NUM 8 /* >= 2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#define RX_FD_NUM 250 /* >= 32 */
369#define TX_FD_NUM 128
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900370#define RX_BUF_SIZE PAGE_SIZE
371#else /* TC35815_USE_PACKEDBUFFER */
372#define FD_PAGE_NUM 4
373#define RX_BUF_NUM 128 /* < 256 */
374#define RX_FD_NUM 256 /* >= 32 */
375#define TX_FD_NUM 128
376#if RX_CTL_CMD & Rx_LongEn
377#define RX_BUF_SIZE PAGE_SIZE
378#elif RX_CTL_CMD & Rx_StripCRC
Atsushi Nemoto82a99282008-12-11 20:58:04 -0800379#define RX_BUF_SIZE \
380 L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + NET_IP_ALIGN)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900381#else
Atsushi Nemoto82a99282008-12-11 20:58:04 -0800382#define RX_BUF_SIZE \
383 L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN + NET_IP_ALIGN)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900384#endif
385#endif /* TC35815_USE_PACKEDBUFFER */
386#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
387#define NAPI_WEIGHT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389struct TxFD {
390 struct FDesc fd;
391 struct BDesc bd;
392 struct BDesc unused;
393};
394
395struct RxFD {
396 struct FDesc fd;
397 struct BDesc bd[0]; /* variable length */
398};
399
400struct FrFD {
401 struct FDesc fd;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900402 struct BDesc bd[RX_BUF_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404
405
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900406#define tc_readl(addr) ioread32(addr)
407#define tc_writel(d, addr) iowrite32(d, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900409#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
410
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900411/* Information that need to be kept for each controller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412struct tc35815_local {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900413 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700415 struct net_device *dev;
416 struct napi_struct napi;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct {
420 int max_tx_qlen;
421 int tx_ints;
422 int rx_ints;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900423 int tx_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 } lstats;
425
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900426 /* Tx control lock. This protects the transmit buffer ring
427 * state along with the "tx full" state of the driver. This
428 * means all netif_queue flow control actions are protected
429 * by this lock as well.
430 */
431 spinlock_t lock;
432
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700433 struct mii_bus *mii_bus;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900434 struct phy_device *phy_dev;
435 int duplex;
436 int speed;
437 int link;
438 struct work_struct restart_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * Transmitting: Batch Mode.
442 * 1 BD in 1 TxFD.
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900443 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * 1 circular FD for Free Buffer List.
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900445 * RX_BUF_NUM BD in Free Buffer FD.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * One Free Buffer BD has PAGE_SIZE data buffer.
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900447 * Or Non-Packing Mode.
448 * 1 circular FD for Free Buffer List.
449 * RX_BUF_NUM BD in Free Buffer FD.
450 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900452 void *fd_buf; /* for TxFD, RxFD, FrFD */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900453 dma_addr_t fd_buf_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct TxFD *tfd_base;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900455 unsigned int tfd_start;
456 unsigned int tfd_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct RxFD *rfd_base;
458 struct RxFD *rfd_limit;
459 struct RxFD *rfd_cur;
460 struct FrFD *fbl_ptr;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900461#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 unsigned char fbl_curid;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900463 void *data_buf[RX_BUF_NUM]; /* packing */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900464 dma_addr_t data_buf_dma[RX_BUF_NUM];
465 struct {
466 struct sk_buff *skb;
467 dma_addr_t skb_dma;
468 } tx_skbs[TX_FD_NUM];
469#else
470 unsigned int fbl_count;
471 struct {
472 struct sk_buff *skb;
473 dma_addr_t skb_dma;
474 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
475#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900476 u32 msg_enable;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900477 enum tc35815_chiptype chiptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478};
479
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900480static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
481{
482 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
483}
484#ifdef DEBUG
485static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
486{
487 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
488}
489#endif
490#ifdef TC35815_USE_PACKEDBUFFER
491static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
492{
493 int i;
494 for (i = 0; i < RX_BUF_NUM; i++) {
495 if (bus >= lp->data_buf_dma[i] &&
496 bus < lp->data_buf_dma[i] + PAGE_SIZE)
497 return (void *)((u8 *)lp->data_buf[i] +
498 (bus - lp->data_buf_dma[i]));
499 }
500 return NULL;
501}
502
503#define TC35815_DMA_SYNC_ONDEMAND
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900504static void *alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900505{
506#ifdef TC35815_DMA_SYNC_ONDEMAND
507 void *buf;
508 /* pci_map + pci_dma_sync will be more effective than
509 * pci_alloc_consistent on some archs. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900510 buf = (void *)__get_free_page(GFP_ATOMIC);
511 if (!buf)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900512 return NULL;
513 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
514 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700515 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900516 free_page((unsigned long)buf);
517 return NULL;
518 }
519 return buf;
520#else
521 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
522#endif
523}
524
525static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
526{
527#ifdef TC35815_DMA_SYNC_ONDEMAND
528 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
529 free_page((unsigned long)buf);
530#else
531 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
532#endif
533}
534#else /* TC35815_USE_PACKEDBUFFER */
535static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
536 struct pci_dev *hwdev,
537 dma_addr_t *dma_handle)
538{
539 struct sk_buff *skb;
540 skb = dev_alloc_skb(RX_BUF_SIZE);
541 if (!skb)
542 return NULL;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900543 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
544 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700545 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900546 dev_kfree_skb_any(skb);
547 return NULL;
548 }
549 skb_reserve(skb, 2); /* make IP header 4byte aligned */
550 return skb;
551}
552
553static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
554{
555 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
556 PCI_DMA_FROMDEVICE);
557 dev_kfree_skb_any(skb);
558}
559#endif /* TC35815_USE_PACKEDBUFFER */
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/* Index to functions, as function prototypes. */
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static int tc35815_open(struct net_device *dev);
564static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900565static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
566#ifdef TC35815_NAPI
567static int tc35815_rx(struct net_device *dev, int limit);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700568static int tc35815_poll(struct napi_struct *napi, int budget);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900569#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static void tc35815_rx(struct net_device *dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572static void tc35815_txdone(struct net_device *dev);
573static int tc35815_close(struct net_device *dev);
574static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
575static void tc35815_set_multicast_list(struct net_device *dev);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900576static void tc35815_tx_timeout(struct net_device *dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900577static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
578#ifdef CONFIG_NET_POLL_CONTROLLER
579static void tc35815_poll_controller(struct net_device *dev);
580#endif
581static const struct ethtool_ops tc35815_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900583/* Example routines you must write ;->. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900584static void tc35815_chip_reset(struct net_device *dev);
585static void tc35815_chip_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900587#ifdef DEBUG
588static void panic_queues(struct net_device *dev);
589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900591static void tc35815_restart_work(struct work_struct *work);
592
593static int tc_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
594{
595 struct net_device *dev = bus->priv;
596 struct tc35815_regs __iomem *tr =
597 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000598 unsigned long timeout = jiffies + HZ;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900599
600 tc_writel(MD_CA_Busy | (mii_id << 5) | (regnum & 0x1f), &tr->MD_CA);
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000601 udelay(12); /* it takes 32 x 400ns at least */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900602 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
603 if (time_after(jiffies, timeout))
604 return -EIO;
605 cpu_relax();
606 }
607 return tc_readl(&tr->MD_Data) & 0xffff;
608}
609
610static int tc_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 val)
611{
612 struct net_device *dev = bus->priv;
613 struct tc35815_regs __iomem *tr =
614 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000615 unsigned long timeout = jiffies + HZ;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900616
617 tc_writel(val, &tr->MD_Data);
618 tc_writel(MD_CA_Busy | MD_CA_Wr | (mii_id << 5) | (regnum & 0x1f),
619 &tr->MD_CA);
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000620 udelay(12); /* it takes 32 x 400ns at least */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900621 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
622 if (time_after(jiffies, timeout))
623 return -EIO;
624 cpu_relax();
625 }
626 return 0;
627}
628
629static void tc_handle_link_change(struct net_device *dev)
630{
631 struct tc35815_local *lp = netdev_priv(dev);
632 struct phy_device *phydev = lp->phy_dev;
633 unsigned long flags;
634 int status_change = 0;
635
636 spin_lock_irqsave(&lp->lock, flags);
637 if (phydev->link &&
638 (lp->speed != phydev->speed || lp->duplex != phydev->duplex)) {
639 struct tc35815_regs __iomem *tr =
640 (struct tc35815_regs __iomem *)dev->base_addr;
641 u32 reg;
642
643 reg = tc_readl(&tr->MAC_Ctl);
644 reg |= MAC_HaltReq;
645 tc_writel(reg, &tr->MAC_Ctl);
646 if (phydev->duplex == DUPLEX_FULL)
647 reg |= MAC_FullDup;
648 else
649 reg &= ~MAC_FullDup;
650 tc_writel(reg, &tr->MAC_Ctl);
651 reg &= ~MAC_HaltReq;
652 tc_writel(reg, &tr->MAC_Ctl);
653
654 /*
655 * TX4939 PCFG.SPEEDn bit will be changed on
656 * NETDEV_CHANGE event.
657 */
658
659#if !defined(NO_CHECK_CARRIER) && defined(WORKAROUND_LOSTCAR)
660 /*
661 * WORKAROUND: enable LostCrS only if half duplex
662 * operation.
663 * (TX4939 does not have EnLCarr)
664 */
665 if (phydev->duplex == DUPLEX_HALF &&
666 lp->chiptype != TC35815_TX4939)
667 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr,
668 &tr->Tx_Ctl);
669#endif
670
671 lp->speed = phydev->speed;
672 lp->duplex = phydev->duplex;
673 status_change = 1;
674 }
675
676 if (phydev->link != lp->link) {
677 if (phydev->link) {
678#ifdef WORKAROUND_100HALF_PROMISC
679 /* delayed promiscuous enabling */
680 if (dev->flags & IFF_PROMISC)
681 tc35815_set_multicast_list(dev);
682#endif
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900683 } else {
684 lp->speed = 0;
685 lp->duplex = -1;
686 }
687 lp->link = phydev->link;
688
689 status_change = 1;
690 }
691 spin_unlock_irqrestore(&lp->lock, flags);
692
693 if (status_change && netif_msg_link(lp)) {
694 phy_print_status(phydev);
Joe Perches72903832009-05-15 07:59:42 +0000695 pr_debug("%s: MII BMCR %04x BMSR %04x LPA %04x\n",
696 dev->name,
697 phy_read(phydev, MII_BMCR),
698 phy_read(phydev, MII_BMSR),
699 phy_read(phydev, MII_LPA));
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900700 }
701}
702
703static int tc_mii_probe(struct net_device *dev)
704{
705 struct tc35815_local *lp = netdev_priv(dev);
706 struct phy_device *phydev = NULL;
707 int phy_addr;
708 u32 dropmask;
709
710 /* find the first phy */
711 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700712 if (lp->mii_bus->phy_map[phy_addr]) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900713 if (phydev) {
714 printk(KERN_ERR "%s: multiple PHYs found\n",
715 dev->name);
716 return -EINVAL;
717 }
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700718 phydev = lp->mii_bus->phy_map[phy_addr];
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900719 break;
720 }
721 }
722
723 if (!phydev) {
724 printk(KERN_ERR "%s: no PHY found\n", dev->name);
725 return -ENODEV;
726 }
727
728 /* attach the mac to the phy */
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800729 phydev = phy_connect(dev, dev_name(&phydev->dev),
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900730 &tc_handle_link_change, 0,
731 lp->chiptype == TC35815_TX4939 ?
732 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII);
733 if (IS_ERR(phydev)) {
734 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
735 return PTR_ERR(phydev);
736 }
737 printk(KERN_INFO "%s: attached PHY driver [%s] "
738 "(mii_bus:phy_addr=%s, id=%x)\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800739 dev->name, phydev->drv->name, dev_name(&phydev->dev),
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900740 phydev->phy_id);
741
742 /* mask with MAC supported features */
743 phydev->supported &= PHY_BASIC_FEATURES;
744 dropmask = 0;
745 if (options.speed == 10)
746 dropmask |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
747 else if (options.speed == 100)
748 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
749 if (options.duplex == 1)
750 dropmask |= SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full;
751 else if (options.duplex == 2)
752 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_100baseT_Half;
753 phydev->supported &= ~dropmask;
754 phydev->advertising = phydev->supported;
755
756 lp->link = 0;
757 lp->speed = 0;
758 lp->duplex = -1;
759 lp->phy_dev = phydev;
760
761 return 0;
762}
763
764static int tc_mii_init(struct net_device *dev)
765{
766 struct tc35815_local *lp = netdev_priv(dev);
767 int err;
768 int i;
769
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700770 lp->mii_bus = mdiobus_alloc();
771 if (lp->mii_bus == NULL) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900772 err = -ENOMEM;
773 goto err_out;
774 }
775
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700776 lp->mii_bus->name = "tc35815_mii_bus";
777 lp->mii_bus->read = tc_mdio_read;
778 lp->mii_bus->write = tc_mdio_write;
779 snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x",
780 (lp->pci_dev->bus->number << 8) | lp->pci_dev->devfn);
781 lp->mii_bus->priv = dev;
782 lp->mii_bus->parent = &lp->pci_dev->dev;
783 lp->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
784 if (!lp->mii_bus->irq) {
785 err = -ENOMEM;
786 goto err_out_free_mii_bus;
787 }
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900788
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700789 for (i = 0; i < PHY_MAX_ADDR; i++)
790 lp->mii_bus->irq[i] = PHY_POLL;
791
792 err = mdiobus_register(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900793 if (err)
794 goto err_out_free_mdio_irq;
795 err = tc_mii_probe(dev);
796 if (err)
797 goto err_out_unregister_bus;
798 return 0;
799
800err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700801 mdiobus_unregister(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900802err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700803 kfree(lp->mii_bus->irq);
Adrian Bunk51cf7562008-10-12 21:01:53 -0700804err_out_free_mii_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700805 mdiobus_free(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900806err_out:
807 return err;
808}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900810#ifdef CONFIG_CPU_TX49XX
811/*
812 * Find a platform_device providing a MAC address. The platform code
813 * should provide a "tc35815-mac" device with a MAC address in its
814 * platform_data.
815 */
816static int __devinit tc35815_mac_match(struct device *dev, void *data)
817{
818 struct platform_device *plat_dev = to_platform_device(dev);
819 struct pci_dev *pci_dev = data;
Atsushi Nemoto06675e62008-01-19 01:15:52 +0900820 unsigned int id = pci_dev->irq;
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900821 return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
822}
823
824static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
825{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900826 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900827 struct device *pd = bus_find_device(&platform_bus_type, NULL,
828 lp->pci_dev, tc35815_mac_match);
829 if (pd) {
830 if (pd->platform_data)
831 memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
832 put_device(pd);
833 return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
834 }
835 return -ENODEV;
836}
837#else
Yoichi Yuasa308a9062007-07-18 11:13:42 +0900838static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900839{
840 return -ENODEV;
841}
842#endif
843
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900844static int __devinit tc35815_init_dev_addr(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900846 struct tc35815_regs __iomem *tr =
847 (struct tc35815_regs __iomem *)dev->base_addr;
848 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
851 ;
852 for (i = 0; i < 6; i += 2) {
853 unsigned short data;
854 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
855 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
856 ;
857 data = tc_readl(&tr->PROM_Data);
858 dev->dev_addr[i] = data & 0xff;
859 dev->dev_addr[i+1] = data >> 8;
860 }
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900861 if (!is_valid_ether_addr(dev->dev_addr))
862 return tc35815_read_plat_dev_addr(dev);
863 return 0;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Alexander Beregalov5a1c28b2009-04-11 07:38:54 +0000866static const struct net_device_ops tc35815_netdev_ops = {
867 .ndo_open = tc35815_open,
868 .ndo_stop = tc35815_close,
869 .ndo_start_xmit = tc35815_send_packet,
870 .ndo_get_stats = tc35815_get_stats,
871 .ndo_set_multicast_list = tc35815_set_multicast_list,
872 .ndo_tx_timeout = tc35815_tx_timeout,
873 .ndo_do_ioctl = tc35815_ioctl,
874 .ndo_validate_addr = eth_validate_addr,
875 .ndo_change_mtu = eth_change_mtu,
876 .ndo_set_mac_address = eth_mac_addr,
877#ifdef CONFIG_NET_POLL_CONTROLLER
878 .ndo_poll_controller = tc35815_poll_controller,
879#endif
880};
881
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900882static int __devinit tc35815_init_one(struct pci_dev *pdev,
883 const struct pci_device_id *ent)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900884{
885 void __iomem *ioaddr = NULL;
886 struct net_device *dev;
887 struct tc35815_local *lp;
888 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900890 static int printed_version;
891 if (!printed_version++) {
892 printk(version);
893 dev_printk(KERN_DEBUG, &pdev->dev,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900894 "speed:%d duplex:%d\n",
895 options.speed, options.duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900898 if (!pdev->irq) {
899 dev_warn(&pdev->dev, "no IRQ assigned.\n");
900 return -ENODEV;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900903 /* dev zeroed in alloc_etherdev */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900904 dev = alloc_etherdev(sizeof(*lp));
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900905 if (dev == NULL) {
906 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
907 return -ENOMEM;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 SET_NETDEV_DEV(dev, &pdev->dev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900910 lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700911 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900913 /* enable device (incl. PCI PM wakeup), and bus-mastering */
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900914 rc = pcim_enable_device(pdev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900915 if (rc)
916 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900917 rc = pcim_iomap_regions(pdev, 1 << 1, MODNAME);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900918 if (rc)
919 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900920 pci_set_master(pdev);
921 ioaddr = pcim_iomap_table(pdev)[1];
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900922
923 /* Initialize the device structure. */
Alexander Beregalov5a1c28b2009-04-11 07:38:54 +0000924 dev->netdev_ops = &tc35815_netdev_ops;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900925 dev->ethtool_ops = &tc35815_ethtool_ops;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900926 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
927#ifdef TC35815_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700928 netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900929#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900930
931 dev->irq = pdev->irq;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900932 dev->base_addr = (unsigned long)ioaddr;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900933
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900934 INIT_WORK(&lp->restart_work, tc35815_restart_work);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900935 spin_lock_init(&lp->lock);
936 lp->pci_dev = pdev;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900937 lp->chiptype = ent->driver_data;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900938
939 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
940 pci_set_drvdata(pdev, dev);
941
942 /* Soft reset the chip. */
943 tc35815_chip_reset(dev);
944
945 /* Retrieve the ethernet address. */
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900946 if (tc35815_init_dev_addr(dev)) {
947 dev_warn(&pdev->dev, "not valid ether addr\n");
948 random_ether_addr(dev->dev_addr);
949 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900950
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900951 rc = register_netdev(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900952 if (rc)
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900953 goto err_out;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900954
955 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Johannes Berge1749612008-10-27 15:59:26 -0700956 printk(KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900957 dev->name,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900958 chip_info[ent->driver_data].name,
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900959 dev->base_addr,
Johannes Berge1749612008-10-27 15:59:26 -0700960 dev->dev_addr,
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900961 dev->irq);
962
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900963 rc = tc_mii_init(dev);
964 if (rc)
965 goto err_out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 return 0;
968
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900969err_out_unregister:
970 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971err_out:
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900972 free_netdev(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900973 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900977static void __devexit tc35815_remove_one(struct pci_dev *pdev)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900978{
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900979 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900980 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900981
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900982 phy_disconnect(lp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700983 mdiobus_unregister(lp->mii_bus);
984 kfree(lp->mii_bus->irq);
985 mdiobus_free(lp->mii_bus);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900986 unregister_netdev(dev);
987 free_netdev(dev);
988 pci_set_drvdata(pdev, NULL);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static int
992tc35815_init_queues(struct net_device *dev)
993{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900994 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int i;
996 unsigned long fd_addr;
997
998 if (!lp->fd_buf) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +0900999 BUG_ON(sizeof(struct FDesc) +
1000 sizeof(struct BDesc) * RX_BUF_NUM +
1001 sizeof(struct FDesc) * RX_FD_NUM +
1002 sizeof(struct TxFD) * TX_FD_NUM >
1003 PAGE_SIZE * FD_PAGE_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001005 lp->fd_buf = pci_alloc_consistent(lp->pci_dev,
1006 PAGE_SIZE * FD_PAGE_NUM,
1007 &lp->fd_buf_dma);
1008 if (!lp->fd_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return -ENOMEM;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001010 for (i = 0; i < RX_BUF_NUM; i++) {
1011#ifdef TC35815_USE_PACKEDBUFFER
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001012 lp->data_buf[i] =
1013 alloc_rxbuf_page(lp->pci_dev,
1014 &lp->data_buf_dma[i]);
1015 if (!lp->data_buf[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 while (--i >= 0) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001017 free_rxbuf_page(lp->pci_dev,
1018 lp->data_buf[i],
1019 lp->data_buf_dma[i]);
1020 lp->data_buf[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001022 pci_free_consistent(lp->pci_dev,
1023 PAGE_SIZE * FD_PAGE_NUM,
1024 lp->fd_buf,
1025 lp->fd_buf_dma);
1026 lp->fd_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -ENOMEM;
1028 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001029#else
1030 lp->rx_skbs[i].skb =
1031 alloc_rxbuf_skb(dev, lp->pci_dev,
1032 &lp->rx_skbs[i].skb_dma);
1033 if (!lp->rx_skbs[i].skb) {
1034 while (--i >= 0) {
1035 free_rxbuf_skb(lp->pci_dev,
1036 lp->rx_skbs[i].skb,
1037 lp->rx_skbs[i].skb_dma);
1038 lp->rx_skbs[i].skb = NULL;
1039 }
1040 pci_free_consistent(lp->pci_dev,
1041 PAGE_SIZE * FD_PAGE_NUM,
1042 lp->fd_buf,
1043 lp->fd_buf_dma);
1044 lp->fd_buf = NULL;
1045 return -ENOMEM;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047#endif
1048 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001049 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
1050 dev->name, lp->fd_buf);
1051#ifdef TC35815_USE_PACKEDBUFFER
1052 printk(" DataBuf");
1053 for (i = 0; i < RX_BUF_NUM; i++)
1054 printk(" %p", lp->data_buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001056 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 } else {
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001058 for (i = 0; i < FD_PAGE_NUM; i++)
1059 clear_page((void *)((unsigned long)lp->fd_buf +
1060 i * PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 fd_addr = (unsigned long)lp->fd_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /* Free Descriptors (for Receive) */
1065 lp->rfd_base = (struct RxFD *)fd_addr;
1066 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001067 for (i = 0; i < RX_FD_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 lp->rfd_cur = lp->rfd_base;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001070 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* Transmit Descriptors */
1073 lp->tfd_base = (struct TxFD *)fd_addr;
1074 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
1075 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001076 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
1077 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
1079 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001080 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 -07001081 lp->tfd_start = 0;
1082 lp->tfd_end = 0;
1083
1084 /* Buffer List (for Receive) */
1085 lp->fbl_ptr = (struct FrFD *)fd_addr;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001086 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
1087 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
1088#ifndef TC35815_USE_PACKEDBUFFER
1089 /*
1090 * move all allocated skbs to head of rx_skbs[] array.
1091 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
1092 * tc35815_rx() had failed.
1093 */
1094 lp->fbl_count = 0;
1095 for (i = 0; i < RX_BUF_NUM; i++) {
1096 if (lp->rx_skbs[i].skb) {
1097 if (i != lp->fbl_count) {
1098 lp->rx_skbs[lp->fbl_count].skb =
1099 lp->rx_skbs[i].skb;
1100 lp->rx_skbs[lp->fbl_count].skb_dma =
1101 lp->rx_skbs[i].skb_dma;
1102 }
1103 lp->fbl_count++;
1104 }
1105 }
1106#endif
1107 for (i = 0; i < RX_BUF_NUM; i++) {
1108#ifdef TC35815_USE_PACKEDBUFFER
1109 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
1110#else
1111 if (i >= lp->fbl_count) {
1112 lp->fbl_ptr->bd[i].BuffData = 0;
1113 lp->fbl_ptr->bd[i].BDCtl = 0;
1114 continue;
1115 }
1116 lp->fbl_ptr->bd[i].BuffData =
1117 cpu_to_le32(lp->rx_skbs[i].skb_dma);
1118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* BDID is index of FrFD.bd[] */
1120 lp->fbl_ptr->bd[i].BDCtl =
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001121 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
1122 RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001124#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 lp->fbl_curid = 0;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001128 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
1129 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return 0;
1131}
1132
1133static void
1134tc35815_clear_queues(struct net_device *dev)
1135{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001136 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int i;
1138
1139 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001140 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1141 struct sk_buff *skb =
1142 fdsystem != 0xffffffff ?
1143 lp->tx_skbs[fdsystem].skb : NULL;
1144#ifdef DEBUG
1145 if (lp->tx_skbs[i].skb != skb) {
1146 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1147 panic_queues(dev);
1148 }
1149#else
1150 BUG_ON(lp->tx_skbs[i].skb != skb);
1151#endif
1152 if (skb) {
1153 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1154 lp->tx_skbs[i].skb = NULL;
1155 lp->tx_skbs[i].skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001157 }
1158 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
1161 tc35815_init_queues(dev);
1162}
1163
1164static void
1165tc35815_free_queues(struct net_device *dev)
1166{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001167 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int i;
1169
1170 if (lp->tfd_base) {
1171 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001172 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1173 struct sk_buff *skb =
1174 fdsystem != 0xffffffff ?
1175 lp->tx_skbs[fdsystem].skb : NULL;
1176#ifdef DEBUG
1177 if (lp->tx_skbs[i].skb != skb) {
1178 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1179 panic_queues(dev);
1180 }
1181#else
1182 BUG_ON(lp->tx_skbs[i].skb != skb);
1183#endif
1184 if (skb) {
1185 dev_kfree_skb(skb);
1186 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1187 lp->tx_skbs[i].skb = NULL;
1188 lp->tx_skbs[i].skb_dma = 0;
1189 }
1190 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192 }
1193
1194 lp->rfd_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 lp->rfd_limit = NULL;
1196 lp->rfd_cur = NULL;
1197 lp->fbl_ptr = NULL;
1198
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001199 for (i = 0; i < RX_BUF_NUM; i++) {
1200#ifdef TC35815_USE_PACKEDBUFFER
1201 if (lp->data_buf[i]) {
1202 free_rxbuf_page(lp->pci_dev,
1203 lp->data_buf[i], lp->data_buf_dma[i]);
1204 lp->data_buf[i] = NULL;
1205 }
1206#else
1207 if (lp->rx_skbs[i].skb) {
1208 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1209 lp->rx_skbs[i].skb_dma);
1210 lp->rx_skbs[i].skb = NULL;
1211 }
1212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001214 if (lp->fd_buf) {
1215 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1216 lp->fd_buf, lp->fd_buf_dma);
1217 lp->fd_buf = NULL;
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221static void
1222dump_txfd(struct TxFD *fd)
1223{
1224 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1225 le32_to_cpu(fd->fd.FDNext),
1226 le32_to_cpu(fd->fd.FDSystem),
1227 le32_to_cpu(fd->fd.FDStat),
1228 le32_to_cpu(fd->fd.FDCtl));
1229 printk("BD: ");
1230 printk(" %08x %08x",
1231 le32_to_cpu(fd->bd.BuffData),
1232 le32_to_cpu(fd->bd.BDCtl));
1233 printk("\n");
1234}
1235
1236static int
1237dump_rxfd(struct RxFD *fd)
1238{
1239 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1240 if (bd_count > 8)
1241 bd_count = 8;
1242 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1243 le32_to_cpu(fd->fd.FDNext),
1244 le32_to_cpu(fd->fd.FDSystem),
1245 le32_to_cpu(fd->fd.FDStat),
1246 le32_to_cpu(fd->fd.FDCtl));
1247 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 printk("BD: ");
1250 for (i = 0; i < bd_count; i++)
1251 printk(" %08x %08x",
1252 le32_to_cpu(fd->bd[i].BuffData),
1253 le32_to_cpu(fd->bd[i].BDCtl));
1254 printk("\n");
1255 return bd_count;
1256}
1257
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001258#if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static void
1260dump_frfd(struct FrFD *fd)
1261{
1262 int i;
1263 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1264 le32_to_cpu(fd->fd.FDNext),
1265 le32_to_cpu(fd->fd.FDSystem),
1266 le32_to_cpu(fd->fd.FDStat),
1267 le32_to_cpu(fd->fd.FDCtl));
1268 printk("BD: ");
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001269 for (i = 0; i < RX_BUF_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 printk(" %08x %08x",
1271 le32_to_cpu(fd->bd[i].BuffData),
1272 le32_to_cpu(fd->bd[i].BDCtl));
1273 printk("\n");
1274}
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001277#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static void
1279panic_queues(struct net_device *dev)
1280{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001281 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int i;
1283
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001284 printk("TxFD base %p, start %u, end %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1286 printk("RxFD base %p limit %p cur %p\n",
1287 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1288 printk("FrFD %p\n", lp->fbl_ptr);
1289 for (i = 0; i < TX_FD_NUM; i++)
1290 dump_txfd(&lp->tfd_base[i]);
1291 for (i = 0; i < RX_FD_NUM; i++) {
1292 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1293 i += (bd_count + 1) / 2; /* skip BDs */
1294 }
1295 dump_frfd(lp->fbl_ptr);
1296 panic("%s: Illegal queue state.", dev->name);
1297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#endif
1299
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001300static void print_eth(const u8 *add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001302 printk(KERN_DEBUG "print_eth(%p)\n", add);
Johannes Berge1749612008-10-27 15:59:26 -07001303 printk(KERN_DEBUG " %pM => %pM : %02x%02x\n",
1304 add + 6, add, add[12], add[13]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001307static int tc35815_tx_full(struct net_device *dev)
1308{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001309 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001310 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1311}
1312
1313static void tc35815_restart(struct net_device *dev)
1314{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001315 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001316
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001317 if (lp->phy_dev) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001318 int timeout;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001319
1320 phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001321 timeout = 100;
1322 while (--timeout) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001323 if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET))
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001324 break;
1325 udelay(1);
1326 }
1327 if (!timeout)
1328 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1329 }
1330
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001331 spin_lock_irq(&lp->lock);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001332 tc35815_chip_reset(dev);
1333 tc35815_clear_queues(dev);
1334 tc35815_chip_init(dev);
1335 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1336 tc35815_set_multicast_list(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001337 spin_unlock_irq(&lp->lock);
1338
1339 netif_wake_queue(dev);
1340}
1341
1342static void tc35815_restart_work(struct work_struct *work)
1343{
1344 struct tc35815_local *lp =
1345 container_of(work, struct tc35815_local, restart_work);
1346 struct net_device *dev = lp->dev;
1347
1348 tc35815_restart(dev);
1349}
1350
1351static void tc35815_schedule_restart(struct net_device *dev)
1352{
1353 struct tc35815_local *lp = netdev_priv(dev);
1354 struct tc35815_regs __iomem *tr =
1355 (struct tc35815_regs __iomem *)dev->base_addr;
1356
1357 /* disable interrupts */
1358 tc_writel(0, &tr->Int_En);
1359 tc_writel(tc_readl(&tr->DMA_Ctl) | DMA_IntMask, &tr->DMA_Ctl);
1360 schedule_work(&lp->restart_work);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001361}
1362
1363static void tc35815_tx_timeout(struct net_device *dev)
1364{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001365 struct tc35815_regs __iomem *tr =
1366 (struct tc35815_regs __iomem *)dev->base_addr;
1367
1368 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1369 dev->name, tc_readl(&tr->Tx_Stat));
1370
1371 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001372 tc35815_schedule_restart(dev);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001373 dev->stats.tx_errors++;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/*
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001377 * Open/initialize the controller. This is called (in the current kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * sometime after booting when the 'ifconfig' program is run.
1379 *
1380 * This routine should set everything up anew at each open, even
1381 * registers that "should" only need to be set once at boot, so that
1382 * there is non-reboot way to recover if something goes wrong.
1383 */
1384static int
1385tc35815_open(struct net_device *dev)
1386{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001387 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /*
1390 * This is used if the interrupt line can turned off (shared).
1391 * See 3c503.c for an example of selecting the IRQ at config-time.
1392 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001393 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED,
1394 dev->name, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 tc35815_chip_reset(dev);
1398
1399 if (tc35815_init_queues(dev) != 0) {
1400 free_irq(dev->irq, dev);
1401 return -EAGAIN;
1402 }
1403
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001404#ifdef TC35815_NAPI
1405 napi_enable(&lp->napi);
1406#endif
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* Reset the hardware here. Don't forget to set the station address. */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001409 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 tc35815_chip_init(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001411 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Atsushi Nemoto59524a32008-06-25 11:41:01 +09001413 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001414 /* schedule a link state check */
1415 phy_start(lp->phy_dev);
1416
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001417 /* We are now ready to accept transmit requeusts from
1418 * the queueing layer of the networking.
1419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 netif_start_queue(dev);
1421
1422 return 0;
1423}
1424
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001425/* This will only be invoked if your driver is _not_ in XOFF state.
1426 * What this means is that you need not check it, and that this
1427 * invariant will hold if you make sure that the netif_*_queue()
1428 * calls are done at the proper times.
1429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1431{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001432 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001433 struct TxFD *txfd;
1434 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001436 /* If some error occurs while trying to transmit this
1437 * packet, you should return '1' from this function.
1438 * In such a case you _may not_ do anything to the
1439 * SKB, it is still owned by the network queueing
1440 * layer when an error is returned. This means you
1441 * may not modify any SKB fields, you may not free
1442 * the SKB, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001445 /* This is the most common case for modern hardware.
1446 * The spinlock protects this code from the TX complete
1447 * hardware interrupt handler. Queue flow control is
1448 * thus managed under this lock as well.
1449 */
1450 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001452 /* failsafe... (handle txdone now if half of FDs are used) */
1453 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1454 TX_FD_NUM / 2)
1455 tc35815_txdone(dev);
1456
1457 if (netif_msg_pktdata(lp))
1458 print_eth(skb->data);
1459#ifdef DEBUG
1460 if (lp->tx_skbs[lp->tfd_start].skb) {
1461 printk("%s: tx_skbs conflict.\n", dev->name);
1462 panic_queues(dev);
1463 }
1464#else
1465 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001467 lp->tx_skbs[lp->tfd_start].skb = skb;
1468 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 -07001469
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001470 /*add to ring */
1471 txfd = &lp->tfd_base[lp->tfd_start];
1472 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1473 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1474 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1475 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001477 if (lp->tfd_start == lp->tfd_end) {
1478 struct tc35815_regs __iomem *tr =
1479 (struct tc35815_regs __iomem *)dev->base_addr;
1480 /* Start DMA Transmitter. */
1481 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482#ifdef GATHER_TXINT
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001483 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001485 if (netif_msg_tx_queued(lp)) {
1486 printk("%s: starting TxFD.\n", dev->name);
1487 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001489 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1490 } else {
1491 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1492 if (netif_msg_tx_queued(lp)) {
1493 printk("%s: queueing TxFD.\n", dev->name);
1494 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001496 }
1497 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1498
1499 dev->trans_start = jiffies;
1500
1501 /* If we just used up the very last entry in the
1502 * TX ring on this device, tell the queueing
1503 * layer to send no more.
1504 */
1505 if (tc35815_tx_full(dev)) {
1506 if (netif_msg_tx_queued(lp))
1507 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1508 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001511 /* When the TX completion hw interrupt arrives, this
1512 * is when the transmit statistics are updated.
1513 */
1514
1515 spin_unlock_irqrestore(&lp->lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001516 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519#define FATAL_ERROR_INT \
1520 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001521static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 static int count;
1524 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1525 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (status & Int_IntPCI)
1527 printk(" IntPCI");
1528 if (status & Int_DmParErr)
1529 printk(" DmParErr");
1530 if (status & Int_IntNRAbt)
1531 printk(" IntNRAbt");
1532 printk("\n");
1533 if (count++ > 100)
1534 panic("%s: Too many fatal errors.", dev->name);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001535 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001537 tc35815_schedule_restart(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001538}
1539
1540#ifdef TC35815_NAPI
1541static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
1542#else
1543static int tc35815_do_interrupt(struct net_device *dev, u32 status)
1544#endif
1545{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001546 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001547 int ret = -1;
1548
1549 /* Fatal errors... */
1550 if (status & FATAL_ERROR_INT) {
1551 tc35815_fatal_error_interrupt(dev, status);
1552 return 0;
1553 }
1554 /* recoverable errors */
1555 if (status & Int_IntFDAEx) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001556 if (netif_msg_rx_err(lp))
1557 dev_warn(&dev->dev,
1558 "Free Descriptor Area Exhausted (%#x).\n",
1559 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001560 dev->stats.rx_dropped++;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001561 ret = 0;
1562 }
1563 if (status & Int_IntBLEx) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001564 if (netif_msg_rx_err(lp))
1565 dev_warn(&dev->dev,
1566 "Buffer List Exhausted (%#x).\n",
1567 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001568 dev->stats.rx_dropped++;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001569 ret = 0;
1570 }
1571 if (status & Int_IntExBD) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001572 if (netif_msg_rx_err(lp))
1573 dev_warn(&dev->dev,
1574 "Excessive Buffer Descriptiors (%#x).\n",
1575 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001576 dev->stats.rx_length_errors++;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001577 ret = 0;
1578 }
1579
1580 /* normal notification */
1581 if (status & Int_IntMacRx) {
1582 /* Got a packet(s). */
1583#ifdef TC35815_NAPI
1584 ret = tc35815_rx(dev, limit);
1585#else
1586 tc35815_rx(dev);
1587 ret = 0;
1588#endif
1589 lp->lstats.rx_ints++;
1590 }
1591 if (status & Int_IntMacTx) {
1592 /* Transmit complete. */
1593 lp->lstats.tx_ints++;
1594 tc35815_txdone(dev);
1595 netif_wake_queue(dev);
Atsushi Nemoto02c5c8e2009-10-26 03:46:21 +00001596#ifdef TC35815_NAPI
1597 if (ret < 0)
1598 ret = 0;
1599#else
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001600 ret = 0;
Atsushi Nemoto02c5c8e2009-10-26 03:46:21 +00001601#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001602 }
1603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606/*
1607 * The typical workload of the driver:
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001608 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
David Howells7d12e782006-10-05 14:55:46 +01001610static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct net_device *dev = dev_id;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001613 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001614 struct tc35815_regs __iomem *tr =
1615 (struct tc35815_regs __iomem *)dev->base_addr;
1616#ifdef TC35815_NAPI
1617 u32 dmactl = tc_readl(&tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001619 if (!(dmactl & DMA_IntMask)) {
1620 /* disable interrupts */
1621 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
Ben Hutchings288379f2009-01-19 16:43:59 -08001622 if (napi_schedule_prep(&lp->napi))
1623 __napi_schedule(&lp->napi);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001624 else {
1625 printk(KERN_ERR "%s: interrupt taken in poll\n",
1626 dev->name);
1627 BUG();
1628 }
1629 (void)tc_readl(&tr->Int_Src); /* flush */
1630 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001632 return IRQ_NONE;
1633#else
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001634 int handled;
1635 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001637 spin_lock(&lp->lock);
1638 status = tc_readl(&tr->Int_Src);
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001639 /* BLEx, FDAEx will be cleared later */
1640 tc_writel(status & ~(Int_BLEx | Int_FDAEx),
1641 &tr->Int_Src); /* write to clear */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001642 handled = tc35815_do_interrupt(dev, status);
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001643 if (status & (Int_BLEx | Int_FDAEx))
1644 tc_writel(status & (Int_BLEx | Int_FDAEx), &tr->Int_Src);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001645 (void)tc_readl(&tr->Int_Src); /* flush */
1646 spin_unlock(&lp->lock);
1647 return IRQ_RETVAL(handled >= 0);
1648#endif /* TC35815_NAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001651#ifdef CONFIG_NET_POLL_CONTROLLER
1652static void tc35815_poll_controller(struct net_device *dev)
1653{
1654 disable_irq(dev->irq);
1655 tc35815_interrupt(dev->irq, dev);
1656 enable_irq(dev->irq);
1657}
1658#endif
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/* We have a good packet(s), get it/them out of the buffers. */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001661#ifdef TC35815_NAPI
1662static int
1663tc35815_rx(struct net_device *dev, int limit)
1664#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665static void
1666tc35815_rx(struct net_device *dev)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001669 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 unsigned int fdctl;
1671 int i;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001672#ifdef TC35815_NAPI
1673 int received = 0;
1674#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1677 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1678 int pkt_len = fdctl & FD_FDLength_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001680#ifdef DEBUG
1681 struct RxFD *next_rfd;
1682#endif
1683#if (RX_CTL_CMD & Rx_StripCRC) == 0
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001684 pkt_len -= ETH_FCS_LEN;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001687 if (netif_msg_rx_status(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 dump_rxfd(lp->rfd_cur);
1689 if (status & Rx_Good) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct sk_buff *skb;
1691 unsigned char *data;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001692 int cur_bd;
1693#ifdef TC35815_USE_PACKEDBUFFER
1694 int offset;
1695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001697#ifdef TC35815_NAPI
1698 if (--limit < 0)
1699 break;
1700#endif
1701#ifdef TC35815_USE_PACKEDBUFFER
1702 BUG_ON(bd_count > 2);
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001703 skb = dev_alloc_skb(pkt_len + NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (skb == NULL) {
1705 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
1706 dev->name);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001707 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 }
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001710 skb_reserve(skb, NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 data = skb_put(skb, pkt_len);
1713
1714 /* copy from receive buffer */
1715 cur_bd = 0;
1716 offset = 0;
1717 while (offset < pkt_len && cur_bd < bd_count) {
1718 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1719 BD_BuffLength_MASK;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001720 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1721 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1722 if (offset + len > pkt_len)
1723 len = pkt_len - offset;
1724#ifdef TC35815_DMA_SYNC_ONDEMAND
1725 pci_dma_sync_single_for_cpu(lp->pci_dev,
1726 dma, len,
1727 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728#endif
1729 memcpy(data + offset, rxbuf, len);
Atsushi Nemoto793bc0a2007-03-14 01:02:20 +09001730#ifdef TC35815_DMA_SYNC_ONDEMAND
1731 pci_dma_sync_single_for_device(lp->pci_dev,
1732 dma, len,
1733 PCI_DMA_FROMDEVICE);
1734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 offset += len;
1736 cur_bd++;
1737 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001738#else /* TC35815_USE_PACKEDBUFFER */
1739 BUG_ON(bd_count > 1);
1740 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1741 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1742#ifdef DEBUG
1743 if (cur_bd >= RX_BUF_NUM) {
1744 printk("%s: invalid BDID.\n", dev->name);
1745 panic_queues(dev);
1746 }
1747 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1748 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1749 if (!lp->rx_skbs[cur_bd].skb) {
1750 printk("%s: NULL skb.\n", dev->name);
1751 panic_queues(dev);
1752 }
1753#else
1754 BUG_ON(cur_bd >= RX_BUF_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001756 skb = lp->rx_skbs[cur_bd].skb;
1757 prefetch(skb->data);
1758 lp->rx_skbs[cur_bd].skb = NULL;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001759 pci_unmap_single(lp->pci_dev,
1760 lp->rx_skbs[cur_bd].skb_dma,
1761 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001762 if (!HAVE_DMA_RXALIGN(lp) && NET_IP_ALIGN)
1763 memmove(skb->data, skb->data - NET_IP_ALIGN,
1764 pkt_len);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001765 data = skb_put(skb, pkt_len);
1766#endif /* TC35815_USE_PACKEDBUFFER */
1767 if (netif_msg_pktdata(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 print_eth(data);
1769 skb->protocol = eth_type_trans(skb, dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001770#ifdef TC35815_NAPI
1771 netif_receive_skb(skb);
1772 received++;
1773#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 netif_rx(skb);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001775#endif
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001776 dev->stats.rx_packets++;
1777 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 } else {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001779 dev->stats.rx_errors++;
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001780 if (netif_msg_rx_err(lp))
1781 dev_info(&dev->dev, "Rx error (status %x)\n",
1782 status & Rx_Stat_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1784 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1785 status &= ~(Rx_LongErr|Rx_CRCErr);
1786 status |= Rx_Over;
1787 }
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001788 if (status & Rx_LongErr)
1789 dev->stats.rx_length_errors++;
1790 if (status & Rx_Over)
1791 dev->stats.rx_fifo_errors++;
1792 if (status & Rx_CRCErr)
1793 dev->stats.rx_crc_errors++;
1794 if (status & Rx_Align)
1795 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 if (bd_count > 0) {
1799 /* put Free Buffer back to controller */
1800 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1801 unsigned char id =
1802 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001803#ifdef DEBUG
1804 if (id >= RX_BUF_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 printk("%s: invalid BDID.\n", dev->name);
1806 panic_queues(dev);
1807 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001808#else
1809 BUG_ON(id >= RX_BUF_NUM);
1810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* free old buffers */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001812#ifdef TC35815_USE_PACKEDBUFFER
1813 while (lp->fbl_curid != id)
1814#else
Atsushi Nemotoccc57aa2008-06-26 17:14:15 +09001815 lp->fbl_count--;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001816 while (lp->fbl_count < RX_BUF_NUM)
1817#endif
1818 {
1819#ifdef TC35815_USE_PACKEDBUFFER
1820 unsigned char curid = lp->fbl_curid;
1821#else
1822 unsigned char curid =
1823 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1824#endif
1825 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1826#ifdef DEBUG
1827 bdctl = le32_to_cpu(bd->BDCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (bdctl & BD_CownsBD) {
1829 printk("%s: Freeing invalid BD.\n",
1830 dev->name);
1831 panic_queues(dev);
1832 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001833#endif
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001834 /* pass BD to controller */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001835#ifndef TC35815_USE_PACKEDBUFFER
1836 if (!lp->rx_skbs[curid].skb) {
1837 lp->rx_skbs[curid].skb =
1838 alloc_rxbuf_skb(dev,
1839 lp->pci_dev,
1840 &lp->rx_skbs[curid].skb_dma);
1841 if (!lp->rx_skbs[curid].skb)
1842 break; /* try on next reception */
1843 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1844 }
1845#endif /* TC35815_USE_PACKEDBUFFER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /* Note: BDLength was modified by chip. */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001847 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1848 (curid << BD_RxBDID_SHIFT) |
1849 RX_BUF_SIZE);
1850#ifdef TC35815_USE_PACKEDBUFFER
1851 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1852 if (netif_msg_rx_status(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 printk("%s: Entering new FBD %d\n",
1854 dev->name, lp->fbl_curid);
1855 dump_frfd(lp->fbl_ptr);
1856 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001857#else
1858 lp->fbl_count++;
1859#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 }
1862
1863 /* put RxFD back to controller */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001864#ifdef DEBUG
1865 next_rfd = fd_bus_to_virt(lp,
1866 le32_to_cpu(lp->rfd_cur->fd.FDNext));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1868 printk("%s: RxFD FDNext invalid.\n", dev->name);
1869 panic_queues(dev);
1870 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001873 /* pass FD to controller */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001874#ifdef DEBUG
1875 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1876#else
1877 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1880 lp->rfd_cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001882 if (lp->rfd_cur > lp->rfd_limit)
1883 lp->rfd_cur = lp->rfd_base;
1884#ifdef DEBUG
1885 if (lp->rfd_cur != next_rfd)
1886 printk("rfd_cur = %p, next_rfd %p\n",
1887 lp->rfd_cur, next_rfd);
1888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001891#ifdef TC35815_NAPI
1892 return received;
1893#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001896#ifdef TC35815_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001897static int tc35815_poll(struct napi_struct *napi, int budget)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001898{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001899 struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
1900 struct net_device *dev = lp->dev;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001901 struct tc35815_regs __iomem *tr =
1902 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001903 int received = 0, handled;
1904 u32 status;
1905
1906 spin_lock(&lp->lock);
1907 status = tc_readl(&tr->Int_Src);
1908 do {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001909 /* BLEx, FDAEx will be cleared later */
1910 tc_writel(status & ~(Int_BLEx | Int_FDAEx),
1911 &tr->Int_Src); /* write to clear */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001912
Atsushi Nemotoa2c465d2009-04-02 01:17:36 -07001913 handled = tc35815_do_interrupt(dev, status, budget - received);
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001914 if (status & (Int_BLEx | Int_FDAEx))
1915 tc_writel(status & (Int_BLEx | Int_FDAEx),
1916 &tr->Int_Src);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001917 if (handled >= 0) {
1918 received += handled;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001919 if (received >= budget)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001920 break;
1921 }
1922 status = tc_readl(&tr->Int_Src);
1923 } while (status);
1924 spin_unlock(&lp->lock);
1925
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001926 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001927 napi_complete(napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001928 /* enable interrupts */
1929 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1930 }
1931 return received;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001932}
1933#endif
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935#ifdef NO_CHECK_CARRIER
1936#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1937#else
1938#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1939#endif
1940
1941static void
1942tc35815_check_tx_stat(struct net_device *dev, int status)
1943{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001944 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 const char *msg = NULL;
1946
1947 /* count collisions */
1948 if (status & Tx_ExColl)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001949 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (status & Tx_TxColl_MASK)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001951 dev->stats.collisions += status & Tx_TxColl_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001953#ifndef NO_CHECK_CARRIER
1954 /* TX4939 does not have NCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001955 if (lp->chiptype == TC35815_TX4939)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 status &= ~Tx_NCarr;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001957#ifdef WORKAROUND_LOSTCAR
1958 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001959 if (!lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001960 status &= ~Tx_NCarr;
1961#endif
1962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 if (!(status & TX_STA_ERR)) {
1965 /* no error. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001966 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return;
1968 }
1969
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001970 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (status & Tx_ExColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001972 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 msg = "Excessive Collision.";
1974 }
1975 if (status & Tx_Under) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001976 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 msg = "Tx FIFO Underrun.";
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09001978 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1979 lp->lstats.tx_underrun++;
1980 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1981 struct tc35815_regs __iomem *tr =
1982 (struct tc35815_regs __iomem *)dev->base_addr;
1983 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1984 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1985 }
1986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 if (status & Tx_Defer) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001989 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 msg = "Excessive Deferral.";
1991 }
1992#ifndef NO_CHECK_CARRIER
1993 if (status & Tx_NCarr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001994 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 msg = "Lost Carrier Sense.";
1996 }
1997#endif
1998 if (status & Tx_LateColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001999 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 msg = "Late Collision.";
2001 }
2002 if (status & Tx_TxPar) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002003 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 msg = "Transmit Parity Error.";
2005 }
2006 if (status & Tx_SQErr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002007 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 msg = "Signal Quality Error.";
2009 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002010 if (msg && netif_msg_tx_err(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
2012}
2013
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002014/* This handles TX complete events posted by the device
2015 * via interrupts.
2016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017static void
2018tc35815_txdone(struct net_device *dev)
2019{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002020 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct TxFD *txfd;
2022 unsigned int fdctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 txfd = &lp->tfd_base[lp->tfd_end];
2025 while (lp->tfd_start != lp->tfd_end &&
2026 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
2027 int status = le32_to_cpu(txfd->fd.FDStat);
2028 struct sk_buff *skb;
2029 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002030 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002032 if (netif_msg_tx_done(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 printk("%s: complete TxFD.\n", dev->name);
2034 dump_txfd(txfd);
2035 }
2036 tc35815_check_tx_stat(dev, status);
2037
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002038 skb = fdsystem != 0xffffffff ?
2039 lp->tx_skbs[fdsystem].skb : NULL;
2040#ifdef DEBUG
2041 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
2042 printk("%s: tx_skbs mismatch.\n", dev->name);
2043 panic_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002045#else
2046 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
2047#endif
2048 if (skb) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002049 dev->stats.tx_bytes += skb->len;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002050 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
2051 lp->tx_skbs[lp->tfd_end].skb = NULL;
2052 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
2053#ifdef TC35815_NAPI
2054 dev_kfree_skb_any(skb);
2055#else
2056 dev_kfree_skb_irq(skb);
2057#endif
2058 }
2059 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
2062 txfd = &lp->tfd_base[lp->tfd_end];
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002063#ifdef DEBUG
2064 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 printk("%s: TxFD FDNext invalid.\n", dev->name);
2066 panic_queues(dev);
2067 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (fdnext & FD_Next_EOL) {
2070 /* DMA Transmitter has been stopping... */
2071 if (lp->tfd_end != lp->tfd_start) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002072 struct tc35815_regs __iomem *tr =
2073 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002075 struct TxFD *txhead = &lp->tfd_base[head];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int qlen = (lp->tfd_start + TX_FD_NUM
2077 - lp->tfd_end) % TX_FD_NUM;
2078
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002079#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
2081 printk("%s: TxFD FDCtl invalid.\n", dev->name);
2082 panic_queues(dev);
2083 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* log max queue length */
2086 if (lp->lstats.max_tx_qlen < qlen)
2087 lp->lstats.max_tx_qlen = qlen;
2088
2089
2090 /* start DMA Transmitter again */
2091 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
2092#ifdef GATHER_TXINT
2093 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
2094#endif
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002095 if (netif_msg_tx_queued(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 printk("%s: start TxFD on queue.\n",
2097 dev->name);
2098 dump_txfd(txfd);
2099 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002100 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102 break;
2103 }
2104 }
2105
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002106 /* If we had stopped the queue due to a "tx full"
2107 * condition, and space has now been made available,
2108 * wake up the queue.
2109 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002110 if (netif_queue_stopped(dev) && !tc35815_tx_full(dev))
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002111 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114/* The inverse routine to tc35815_open(). */
2115static int
2116tc35815_close(struct net_device *dev)
2117{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002118 struct tc35815_local *lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002121#ifdef TC35815_NAPI
2122 napi_disable(&lp->napi);
2123#endif
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002124 if (lp->phy_dev)
2125 phy_stop(lp->phy_dev);
2126 cancel_work_sync(&lp->restart_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* Flush the Tx and disable Rx here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 tc35815_chip_reset(dev);
2130 free_irq(dev->irq, dev);
2131
2132 tc35815_free_queues(dev);
2133
2134 return 0;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
2138/*
2139 * Get the current statistics.
2140 * This may be called with the card open or closed.
2141 */
2142static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
2143{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002144 struct tc35815_regs __iomem *tr =
2145 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002146 if (netif_running(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /* Update the statistics from the device registers. */
Atsushi Nemoto7bb82e82009-08-06 04:41:48 +00002148 dev->stats.rx_missed_errors += tc_readl(&tr->Miss_Cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09002150 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002153static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002155 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002156 struct tc35815_regs __iomem *tr =
2157 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int cam_index = index * 6;
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002159 u32 cam_data;
2160 u32 saved_addr;
Atsushi Nemoto958eb802008-04-11 00:24:24 +09002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 saved_addr = tc_readl(&tr->CAM_Adr);
2163
Atsushi Nemoto958eb802008-04-11 00:24:24 +09002164 if (netif_msg_hw(lp))
Johannes Berge1749612008-10-27 15:59:26 -07002165 printk(KERN_DEBUG "%s: CAM %d: %pM\n",
2166 dev->name, index, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (index & 1) {
2168 /* read modify write */
2169 tc_writel(cam_index - 2, &tr->CAM_Adr);
2170 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
2171 cam_data |= addr[0] << 8 | addr[1];
2172 tc_writel(cam_data, &tr->CAM_Data);
2173 /* write whole word */
2174 tc_writel(cam_index + 2, &tr->CAM_Adr);
2175 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
2176 tc_writel(cam_data, &tr->CAM_Data);
2177 } else {
2178 /* write whole word */
2179 tc_writel(cam_index, &tr->CAM_Adr);
2180 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
2181 tc_writel(cam_data, &tr->CAM_Data);
2182 /* read modify write */
2183 tc_writel(cam_index + 4, &tr->CAM_Adr);
2184 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
2185 cam_data |= addr[4] << 24 | (addr[5] << 16);
2186 tc_writel(cam_data, &tr->CAM_Data);
2187 }
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 tc_writel(saved_addr, &tr->CAM_Adr);
2190}
2191
2192
2193/*
2194 * Set or clear the multicast filter for this adaptor.
2195 * num_addrs == -1 Promiscuous mode, receive all packets
2196 * num_addrs == 0 Normal mode, clear multicast list
2197 * num_addrs > 0 Multicast mode, receive normal and MC packets,
2198 * and do best-effort filtering.
2199 */
2200static void
2201tc35815_set_multicast_list(struct net_device *dev)
2202{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002203 struct tc35815_regs __iomem *tr =
2204 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002206 if (dev->flags & IFF_PROMISC) {
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002207#ifdef WORKAROUND_100HALF_PROMISC
2208 /* With some (all?) 100MHalf HUB, controller will hang
2209 * if we enabled promiscuous mode before linkup... */
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002210 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002211
2212 if (!lp->link)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002213 return;
2214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* Enable promiscuous mode */
2216 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002217 } else if ((dev->flags & IFF_ALLMULTI) ||
2218 dev->mc_count > CAM_ENTRY_MAX - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /* CAM 0, 1, 20 are reserved. */
2220 /* Disable promiscuous mode, use normal mode. */
2221 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002222 } else if (dev->mc_count) {
2223 struct dev_mc_list *cur_addr = dev->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 int i;
2225 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
2226
2227 tc_writel(0, &tr->CAM_Ctl);
2228 /* Walk the address list, and load the filter */
2229 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
2230 if (!cur_addr)
2231 break;
2232 /* entry 0,1 is reserved. */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002233 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 ena_bits |= CAM_Ena_Bit(i + 2);
2235 }
2236 tc_writel(ena_bits, &tr->CAM_Ena);
2237 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09002238 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2240 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2241 }
2242}
2243
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002244static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002246 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002247 strcpy(info->driver, MODNAME);
2248 strcpy(info->version, DRV_VERSION);
2249 strcpy(info->bus_info, pci_name(lp->pci_dev));
2250}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002251
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002252static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2253{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002254 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002255
2256 if (!lp->phy_dev)
2257 return -ENODEV;
2258 return phy_ethtool_gset(lp->phy_dev, cmd);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002261static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2262{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002263 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002264
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002265 if (!lp->phy_dev)
2266 return -ENODEV;
2267 return phy_ethtool_sset(lp->phy_dev, cmd);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002268}
2269
2270static u32 tc35815_get_msglevel(struct net_device *dev)
2271{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002272 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002273 return lp->msg_enable;
2274}
2275
2276static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2277{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002278 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002279 lp->msg_enable = datum;
2280}
2281
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002282static int tc35815_get_sset_count(struct net_device *dev, int sset)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002283{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002284 struct tc35815_local *lp = netdev_priv(dev);
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002285
2286 switch (sset) {
2287 case ETH_SS_STATS:
2288 return sizeof(lp->lstats) / sizeof(int);
2289 default:
2290 return -EOPNOTSUPP;
2291 }
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002292}
2293
2294static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2295{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002296 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002297 data[0] = lp->lstats.max_tx_qlen;
2298 data[1] = lp->lstats.tx_ints;
2299 data[2] = lp->lstats.rx_ints;
2300 data[3] = lp->lstats.tx_underrun;
2301}
2302
2303static struct {
2304 const char str[ETH_GSTRING_LEN];
2305} ethtool_stats_keys[] = {
2306 { "max_tx_qlen" },
2307 { "tx_ints" },
2308 { "rx_ints" },
2309 { "tx_underrun" },
2310};
2311
2312static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2313{
2314 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2315}
2316
2317static const struct ethtool_ops tc35815_ethtool_ops = {
2318 .get_drvinfo = tc35815_get_drvinfo,
2319 .get_settings = tc35815_get_settings,
2320 .set_settings = tc35815_set_settings,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002321 .get_link = ethtool_op_get_link,
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002322 .get_msglevel = tc35815_get_msglevel,
2323 .set_msglevel = tc35815_set_msglevel,
2324 .get_strings = tc35815_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002325 .get_sset_count = tc35815_get_sset_count,
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002326 .get_ethtool_stats = tc35815_get_ethtool_stats,
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002327};
2328
2329static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2330{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002331 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002332
2333 if (!netif_running(dev))
2334 return -EINVAL;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002335 if (!lp->phy_dev)
2336 return -ENODEV;
2337 return phy_mii_ioctl(lp->phy_dev, if_mii(rq), cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
2340static void tc35815_chip_reset(struct net_device *dev)
2341{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002342 struct tc35815_regs __iomem *tr =
2343 (struct tc35815_regs __iomem *)dev->base_addr;
2344 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* reset the controller */
2346 tc_writel(MAC_Reset, &tr->MAC_Ctl);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002347 udelay(4); /* 3200ns */
2348 i = 0;
2349 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2350 if (i++ > 100) {
2351 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2352 break;
2353 }
2354 mdelay(1);
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 tc_writel(0, &tr->MAC_Ctl);
2357
2358 /* initialize registers to default value */
2359 tc_writel(0, &tr->DMA_Ctl);
2360 tc_writel(0, &tr->TxThrsh);
2361 tc_writel(0, &tr->TxPollCtr);
2362 tc_writel(0, &tr->RxFragSize);
2363 tc_writel(0, &tr->Int_En);
2364 tc_writel(0, &tr->FDA_Bas);
2365 tc_writel(0, &tr->FDA_Lim);
2366 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2367 tc_writel(0, &tr->CAM_Ctl);
2368 tc_writel(0, &tr->Tx_Ctl);
2369 tc_writel(0, &tr->Rx_Ctl);
2370 tc_writel(0, &tr->CAM_Ena);
2371 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2372
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002373 /* initialize internal SRAM */
2374 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2375 for (i = 0; i < 0x1000; i += 4) {
2376 tc_writel(i, &tr->CAM_Adr);
2377 tc_writel(0, &tr->CAM_Data);
2378 }
2379 tc_writel(0, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
2382static void tc35815_chip_init(struct net_device *dev)
2383{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002384 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002385 struct tc35815_regs __iomem *tr =
2386 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 unsigned long txctl = TX_CTL_CMD;
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /* load station address to CAM */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002390 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 /* Enable CAM (broadcast and unicast) */
2393 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2394 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2395
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002396 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2397 if (HAVE_DMA_RXALIGN(lp))
2398 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2399 else
2400 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2401#ifdef TC35815_USE_PACKEDBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2405 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2406 tc_writel(INT_EN_CMD, &tr->Int_En);
2407
2408 /* set queues */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002409 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2411 &tr->FDA_Lim);
2412 /*
2413 * Activation method:
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002414 * First, enable the MAC Transmitter and the DMA Receive circuits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 * Then enable the DMA Transmitter and the MAC Receive circuits.
2416 */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002417 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* start MAC transmitter */
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002421#ifndef NO_CHECK_CARRIER
2422 /* TX4939 does not have EnLCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002423 if (lp->chiptype == TC35815_TX4939)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002424 txctl &= ~Tx_EnLCarr;
2425#ifdef WORKAROUND_LOSTCAR
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002427 if (!lp->phy_dev || !lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002428 txctl &= ~Tx_EnLCarr;
2429#endif
2430#endif /* !NO_CHECK_CARRIER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431#ifdef GATHER_TXINT
2432 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
2433#endif
2434 tc_writel(txctl, &tr->Tx_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002437#ifdef CONFIG_PM
2438static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2439{
2440 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002441 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002442 unsigned long flags;
2443
2444 pci_save_state(pdev);
2445 if (!netif_running(dev))
2446 return 0;
2447 netif_device_detach(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002448 if (lp->phy_dev)
2449 phy_stop(lp->phy_dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002450 spin_lock_irqsave(&lp->lock, flags);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002451 tc35815_chip_reset(dev);
2452 spin_unlock_irqrestore(&lp->lock, flags);
2453 pci_set_power_state(pdev, PCI_D3hot);
2454 return 0;
2455}
2456
2457static int tc35815_resume(struct pci_dev *pdev)
2458{
2459 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002460 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002461
2462 pci_restore_state(pdev);
2463 if (!netif_running(dev))
2464 return 0;
2465 pci_set_power_state(pdev, PCI_D0);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002466 tc35815_restart(dev);
Atsushi Nemoto59524a32008-06-25 11:41:01 +09002467 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002468 if (lp->phy_dev)
2469 phy_start(lp->phy_dev);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002470 netif_device_attach(dev);
2471 return 0;
2472}
2473#endif /* CONFIG_PM */
2474
2475static struct pci_driver tc35815_pci_driver = {
2476 .name = MODNAME,
2477 .id_table = tc35815_pci_tbl,
2478 .probe = tc35815_init_one,
2479 .remove = __devexit_p(tc35815_remove_one),
2480#ifdef CONFIG_PM
2481 .suspend = tc35815_suspend,
2482 .resume = tc35815_resume,
2483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484};
2485
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002486module_param_named(speed, options.speed, int, 0);
2487MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2488module_param_named(duplex, options.duplex, int, 0);
2489MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491static int __init tc35815_init_module(void)
2492{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002493 return pci_register_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
2496static void __exit tc35815_cleanup_module(void)
2497{
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002498 pci_unregister_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
Jeff Garzik420e8522007-02-24 17:02:16 -05002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501module_init(tc35815_init_module);
2502module_exit(tc35815_cleanup_module);
Atsushi Nemotoeea221c2007-03-03 23:54:59 +09002503
2504MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2505MODULE_LICENSE("GPL");