blob: 75a669d48e5ebc4be3d79b84ee09ff7a50d85845 [file] [log] [blame]
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001/*
2 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Based on skelton.c by Donald Becker.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09006 * This driver is a replacement of older and less maintained version.
7 * This is a header of the older version:
8 * -----<snip>-----
9 * Copyright 2001 MontaVista Software Inc.
10 * Author: MontaVista Software, Inc.
11 * ahennessy@mvista.com
12 * Copyright (C) 2000-2001 Toshiba Corporation
13 * static const char *version =
14 * "tc35815.c:v0.00 26/07/2000 by Toshiba Corporation\n";
15 * -----<snip>-----
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090017 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090021 * (C) Copyright TOSHIBA CORPORATION 2004-2005
22 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Atsushi Nemotoc6a2dbb2009-11-02 04:34:46 +000025#define DRV_VERSION "1.39"
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090026static const char *version = "tc35815.c:v" DRV_VERSION "\n";
27#define MODNAME "tc35815"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/fcntl.h>
33#include <linux/interrupt.h>
34#include <linux/ioport.h>
35#include <linux/in.h>
Atsushi Nemoto82a99282008-12-11 20:58:04 -080036#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/string.h>
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090039#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/init.h>
42#include <linux/netdevice.h>
43#include <linux/etherdevice.h>
44#include <linux/skbuff.h>
45#include <linux/delay.h>
46#include <linux/pci.h>
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090047#include <linux/phy.h>
48#include <linux/workqueue.h>
Atsushi Nemotobd43da82007-06-29 22:34:53 +090049#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/byteorder.h>
52
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090053enum tc35815_chiptype {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090054 TC35815CF = 0,
55 TC35815_NWU,
56 TC35815_TX4939,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090057};
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090058
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090059/* indexed by tc35815_chiptype, above */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090060static const struct {
61 const char *name;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +090062} chip_info[] __devinitdata = {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090063 { "TOSHIBA TC35815CF 10/100BaseTX" },
64 { "TOSHIBA TC35815 with Wake on LAN" },
65 { "TOSHIBA TC35815/TX4939" },
66};
67
68static const struct pci_device_id tc35815_pci_tbl[] = {
69 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
70 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
71 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
72 {0,}
73};
Atsushi Nemoto7f225b42008-04-11 00:25:31 +090074MODULE_DEVICE_TABLE(pci, tc35815_pci_tbl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090075
76/* see MODULE_PARM_DESC */
77static struct tc35815_options {
78 int speed;
79 int duplex;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +090080} options;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * Registers
84 */
85struct tc35815_regs {
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +090086 __u32 DMA_Ctl; /* 0x00 */
87 __u32 TxFrmPtr;
88 __u32 TxThrsh;
89 __u32 TxPollCtr;
90 __u32 BLFrmPtr;
91 __u32 RxFragSize;
92 __u32 Int_En;
93 __u32 FDA_Bas;
94 __u32 FDA_Lim; /* 0x20 */
95 __u32 Int_Src;
96 __u32 unused0[2];
97 __u32 PauseCnt;
98 __u32 RemPauCnt;
99 __u32 TxCtlFrmStat;
100 __u32 unused1;
101 __u32 MAC_Ctl; /* 0x40 */
102 __u32 CAM_Ctl;
103 __u32 Tx_Ctl;
104 __u32 Tx_Stat;
105 __u32 Rx_Ctl;
106 __u32 Rx_Stat;
107 __u32 MD_Data;
108 __u32 MD_CA;
109 __u32 CAM_Adr; /* 0x60 */
110 __u32 CAM_Data;
111 __u32 CAM_Ena;
112 __u32 PROM_Ctl;
113 __u32 PROM_Data;
114 __u32 Algn_Cnt;
115 __u32 CRC_Cnt;
116 __u32 Miss_Cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119/*
120 * Bit assignments
121 */
122/* DMA_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900123#define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
124#define DMA_RxAlign_1 0x00400000
125#define DMA_RxAlign_2 0x00800000
126#define DMA_RxAlign_3 0x00c00000
127#define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
128#define DMA_IntMask 0x00040000 /* 1:Interupt mask */
129#define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
130#define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
131#define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
132#define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
133#define DMA_TestMode 0x00002000 /* 1:Test Mode */
134#define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
135#define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* RxFragSize bit asign ---------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900138#define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
139#define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/* MAC_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900142#define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
143#define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
144#define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
145#define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
146#define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
147#define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
148#define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
149#define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
150#define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
151#define MAC_Reset 0x00000004 /* 1:Software Reset */
152#define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
153#define MAC_HaltReq 0x00000001 /* 1:Halt request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/* PROM_Ctl bit asign ------------------------------------------------------ */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900156#define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
157#define PROM_Read 0x00004000 /*10:Read operation */
158#define PROM_Write 0x00002000 /*01:Write operation */
159#define PROM_Erase 0x00006000 /*11:Erase operation */
160 /*00:Enable or Disable Writting, */
161 /* as specified in PROM_Addr. */
162#define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
163 /*00xxxx: disable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/* CAM_Ctl bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900166#define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
167#define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
168 /* accept other */
169#define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
170#define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
171#define CAM_StationAcc 0x00000001 /* 1:unicast accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* CAM_Ena bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900174#define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900176#define CAM_Ena_Bit(index) (1 << (index))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#define CAM_ENTRY_DESTINATION 0
178#define CAM_ENTRY_SOURCE 1
179#define CAM_ENTRY_MACCTL 20
180
181/* Tx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900182#define Tx_En 0x00000001 /* 1:Transmit enable */
183#define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
184#define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
185#define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
186#define Tx_FBack 0x00000010 /* 1:Fast Back-off */
187#define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
188#define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
189#define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
190#define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
191#define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
192#define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
193#define Tx_EnComp 0x00004000 /* 1:Enable Completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/* Tx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900196#define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
197#define Tx_ExColl 0x00000010 /* Excessive Collision */
198#define Tx_TXDefer 0x00000020 /* Transmit Defered */
199#define Tx_Paused 0x00000040 /* Transmit Paused */
200#define Tx_IntTx 0x00000080 /* Interrupt on Tx */
201#define Tx_Under 0x00000100 /* Underrun */
202#define Tx_Defer 0x00000200 /* Deferral */
203#define Tx_NCarr 0x00000400 /* No Carrier */
204#define Tx_10Stat 0x00000800 /* 10Mbps Status */
205#define Tx_LateColl 0x00001000 /* Late Collision */
206#define Tx_TxPar 0x00002000 /* Tx Parity Error */
207#define Tx_Comp 0x00004000 /* Completion */
208#define Tx_Halted 0x00008000 /* Tx Halted */
209#define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/* Rx_Ctl bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900212#define Rx_EnGood 0x00004000 /* 1:Enable Good */
213#define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
214#define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
215#define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
216#define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
217#define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
218#define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
219#define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
220#define Rx_ShortEn 0x00000008 /* 1:Short Enable */
221#define Rx_LongEn 0x00000004 /* 1:Long Enable */
222#define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
223#define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225/* Rx_Stat bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900226#define Rx_Halted 0x00008000 /* Rx Halted */
227#define Rx_Good 0x00004000 /* Rx Good */
228#define Rx_RxPar 0x00002000 /* Rx Parity Error */
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900229#define Rx_TypePkt 0x00001000 /* Rx Type Packet */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900230#define Rx_LongErr 0x00000800 /* Rx Long Error */
231#define Rx_Over 0x00000400 /* Rx Overflow */
232#define Rx_CRCErr 0x00000200 /* Rx CRC Error */
233#define Rx_Align 0x00000100 /* Rx Alignment Error */
234#define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
235#define Rx_IntRx 0x00000040 /* Rx Interrupt */
236#define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900237#define Rx_InLenErr 0x00000010 /* Rx In Range Frame Length Error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Atsushi Nemoto842e08b2008-10-28 22:30:23 +0900239#define Rx_Stat_Mask 0x0000FFF0 /* Rx All Status Mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241/* Int_En bit asign -------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900242#define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
243#define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Ctl Complete Enable */
244#define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
245#define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
246#define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
247#define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
248#define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
249#define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
250#define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
251#define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
252#define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
253#define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
254 /* Exhausted Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256/* Int_Src bit asign ------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900257#define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
258#define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
259#define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
260#define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
261#define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
262#define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
263#define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
264#define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
265#define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
266#define Int_SWInt 0x00000020 /* 1:Software request & Clear */
267#define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
268#define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
269#define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
270#define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
271#define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/* MD_CA bit asign --------------------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900274#define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
275#define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
276#define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * Descriptors
281 */
282
283/* Frame descripter */
284struct FDesc {
285 volatile __u32 FDNext;
286 volatile __u32 FDSystem;
287 volatile __u32 FDStat;
288 volatile __u32 FDCtl;
289};
290
291/* Buffer descripter */
292struct BDesc {
293 volatile __u32 BuffData;
294 volatile __u32 BDCtl;
295};
296
297#define FD_ALIGN 16
298
299/* Frame Descripter bit asign ---------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900300#define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
301#define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
302#define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900304#define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
305#define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
307#define FD_FrmOpt_Packing 0x04000000 /* Rx only */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900308#define FD_CownsFD 0x80000000 /* FD Controller owner bit */
309#define FD_Next_EOL 0x00000001 /* FD EOL indicator */
310#define FD_BDCnt_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/* Buffer Descripter bit asign --------------------------------------------- */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900313#define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
314#define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
315#define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
316#define BD_CownsBD 0x80000000 /* BD Controller owner bit */
317#define BD_RxBDID_SHIFT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define BD_RxBDSeqN_SHIFT 24
319
320
321/* Some useful constants. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Atsushi Nemotoa02b7b72009-11-02 04:34:47 +0000323#define TX_CTL_CMD (Tx_EnTxPar | Tx_EnLateColl | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900324 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
325 Tx_En) /* maybe 0x7b01 */
Atsushi Nemoto297713d2009-08-06 04:41:45 +0000326/* Do not use Rx_StripCRC -- it causes trouble on BLEx/FDAEx condition */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
Atsushi Nemoto297713d2009-08-06 04:41:45 +0000328 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#define INT_EN_CMD (Int_NRAbtEn | \
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900330 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
332 Int_STargAbtEn | \
333 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900334#define DMA_CTL_CMD DMA_BURST_SIZE
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900335#define HAVE_DMA_RXALIGN(lp) likely((lp)->chiptype != TC35815CF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337/* Tuning parameters */
338#define DMA_BURST_SIZE 32
339#define TX_THRESHOLD 1024
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900340/* used threshold with packet max byte for low pci transfer ability.*/
341#define TX_THRESHOLD_MAX 1536
342/* setting threshold max value when overrun error occured this count. */
343#define TX_THRESHOLD_KEEP_LIMIT 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900345/* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900346#define FD_PAGE_NUM 4
347#define RX_BUF_NUM 128 /* < 256 */
348#define RX_FD_NUM 256 /* >= 32 */
349#define TX_FD_NUM 128
350#if RX_CTL_CMD & Rx_LongEn
351#define RX_BUF_SIZE PAGE_SIZE
352#elif RX_CTL_CMD & Rx_StripCRC
Atsushi Nemoto82a99282008-12-11 20:58:04 -0800353#define RX_BUF_SIZE \
354 L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + NET_IP_ALIGN)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900355#else
Atsushi Nemoto82a99282008-12-11 20:58:04 -0800356#define RX_BUF_SIZE \
357 L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN + NET_IP_ALIGN)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900358#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900359#define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
360#define NAPI_WEIGHT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362struct TxFD {
363 struct FDesc fd;
364 struct BDesc bd;
365 struct BDesc unused;
366};
367
368struct RxFD {
369 struct FDesc fd;
370 struct BDesc bd[0]; /* variable length */
371};
372
373struct FrFD {
374 struct FDesc fd;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900375 struct BDesc bd[RX_BUF_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
378
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900379#define tc_readl(addr) ioread32(addr)
380#define tc_writel(d, addr) iowrite32(d, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900382#define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
383
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900384/* Information that need to be kept for each controller. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385struct tc35815_local {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900386 struct pci_dev *pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700388 struct net_device *dev;
389 struct napi_struct napi;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct {
393 int max_tx_qlen;
394 int tx_ints;
395 int rx_ints;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900396 int tx_underrun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } lstats;
398
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900399 /* Tx control lock. This protects the transmit buffer ring
400 * state along with the "tx full" state of the driver. This
401 * means all netif_queue flow control actions are protected
402 * by this lock as well.
403 */
404 spinlock_t lock;
405
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700406 struct mii_bus *mii_bus;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900407 struct phy_device *phy_dev;
408 int duplex;
409 int speed;
410 int link;
411 struct work_struct restart_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /*
414 * Transmitting: Batch Mode.
415 * 1 BD in 1 TxFD.
Atsushi Nemotoa02b7b72009-11-02 04:34:47 +0000416 * Receiving: Non-Packing Mode.
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900417 * 1 circular FD for Free Buffer List.
418 * RX_BUF_NUM BD in Free Buffer FD.
419 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900421 void *fd_buf; /* for TxFD, RxFD, FrFD */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900422 dma_addr_t fd_buf_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct TxFD *tfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900424 unsigned int tfd_start;
425 unsigned int tfd_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct RxFD *rfd_base;
427 struct RxFD *rfd_limit;
428 struct RxFD *rfd_cur;
429 struct FrFD *fbl_ptr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900430 unsigned int fbl_count;
431 struct {
432 struct sk_buff *skb;
433 dma_addr_t skb_dma;
434 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900435 u32 msg_enable;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900436 enum tc35815_chiptype chiptype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437};
438
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900439static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
440{
441 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
442}
443#ifdef DEBUG
444static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
445{
446 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
447}
448#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900449static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
450 struct pci_dev *hwdev,
451 dma_addr_t *dma_handle)
452{
453 struct sk_buff *skb;
454 skb = dev_alloc_skb(RX_BUF_SIZE);
455 if (!skb)
456 return NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900457 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
458 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700459 if (pci_dma_mapping_error(hwdev, *dma_handle)) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900460 dev_kfree_skb_any(skb);
461 return NULL;
462 }
463 skb_reserve(skb, 2); /* make IP header 4byte aligned */
464 return skb;
465}
466
467static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
468{
469 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
470 PCI_DMA_FROMDEVICE);
471 dev_kfree_skb_any(skb);
472}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/* Index to functions, as function prototypes. */
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static int tc35815_open(struct net_device *dev);
477static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900478static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900479static int tc35815_rx(struct net_device *dev, int limit);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700480static int tc35815_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static void tc35815_txdone(struct net_device *dev);
482static int tc35815_close(struct net_device *dev);
483static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
484static void tc35815_set_multicast_list(struct net_device *dev);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900485static void tc35815_tx_timeout(struct net_device *dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900486static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
487#ifdef CONFIG_NET_POLL_CONTROLLER
488static void tc35815_poll_controller(struct net_device *dev);
489#endif
490static const struct ethtool_ops tc35815_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900492/* Example routines you must write ;->. */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900493static void tc35815_chip_reset(struct net_device *dev);
494static void tc35815_chip_init(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900496#ifdef DEBUG
497static void panic_queues(struct net_device *dev);
498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900500static void tc35815_restart_work(struct work_struct *work);
501
502static int tc_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
503{
504 struct net_device *dev = bus->priv;
505 struct tc35815_regs __iomem *tr =
506 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000507 unsigned long timeout = jiffies + HZ;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900508
509 tc_writel(MD_CA_Busy | (mii_id << 5) | (regnum & 0x1f), &tr->MD_CA);
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000510 udelay(12); /* it takes 32 x 400ns at least */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900511 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
512 if (time_after(jiffies, timeout))
513 return -EIO;
514 cpu_relax();
515 }
516 return tc_readl(&tr->MD_Data) & 0xffff;
517}
518
519static int tc_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 val)
520{
521 struct net_device *dev = bus->priv;
522 struct tc35815_regs __iomem *tr =
523 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000524 unsigned long timeout = jiffies + HZ;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900525
526 tc_writel(val, &tr->MD_Data);
527 tc_writel(MD_CA_Busy | MD_CA_Wr | (mii_id << 5) | (regnum & 0x1f),
528 &tr->MD_CA);
Atsushi Nemotoc60a5cf2009-08-06 04:41:47 +0000529 udelay(12); /* it takes 32 x 400ns at least */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900530 while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
531 if (time_after(jiffies, timeout))
532 return -EIO;
533 cpu_relax();
534 }
535 return 0;
536}
537
538static void tc_handle_link_change(struct net_device *dev)
539{
540 struct tc35815_local *lp = netdev_priv(dev);
541 struct phy_device *phydev = lp->phy_dev;
542 unsigned long flags;
543 int status_change = 0;
544
545 spin_lock_irqsave(&lp->lock, flags);
546 if (phydev->link &&
547 (lp->speed != phydev->speed || lp->duplex != phydev->duplex)) {
548 struct tc35815_regs __iomem *tr =
549 (struct tc35815_regs __iomem *)dev->base_addr;
550 u32 reg;
551
552 reg = tc_readl(&tr->MAC_Ctl);
553 reg |= MAC_HaltReq;
554 tc_writel(reg, &tr->MAC_Ctl);
555 if (phydev->duplex == DUPLEX_FULL)
556 reg |= MAC_FullDup;
557 else
558 reg &= ~MAC_FullDup;
559 tc_writel(reg, &tr->MAC_Ctl);
560 reg &= ~MAC_HaltReq;
561 tc_writel(reg, &tr->MAC_Ctl);
562
563 /*
564 * TX4939 PCFG.SPEEDn bit will be changed on
565 * NETDEV_CHANGE event.
566 */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900567 /*
568 * WORKAROUND: enable LostCrS only if half duplex
569 * operation.
570 * (TX4939 does not have EnLCarr)
571 */
572 if (phydev->duplex == DUPLEX_HALF &&
573 lp->chiptype != TC35815_TX4939)
574 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr,
575 &tr->Tx_Ctl);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900576
577 lp->speed = phydev->speed;
578 lp->duplex = phydev->duplex;
579 status_change = 1;
580 }
581
582 if (phydev->link != lp->link) {
583 if (phydev->link) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900584 /* delayed promiscuous enabling */
585 if (dev->flags & IFF_PROMISC)
586 tc35815_set_multicast_list(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900587 } else {
588 lp->speed = 0;
589 lp->duplex = -1;
590 }
591 lp->link = phydev->link;
592
593 status_change = 1;
594 }
595 spin_unlock_irqrestore(&lp->lock, flags);
596
597 if (status_change && netif_msg_link(lp)) {
598 phy_print_status(phydev);
Joe Perches72903832009-05-15 07:59:42 +0000599 pr_debug("%s: MII BMCR %04x BMSR %04x LPA %04x\n",
600 dev->name,
601 phy_read(phydev, MII_BMCR),
602 phy_read(phydev, MII_BMSR),
603 phy_read(phydev, MII_LPA));
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900604 }
605}
606
607static int tc_mii_probe(struct net_device *dev)
608{
609 struct tc35815_local *lp = netdev_priv(dev);
610 struct phy_device *phydev = NULL;
611 int phy_addr;
612 u32 dropmask;
613
614 /* find the first phy */
615 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700616 if (lp->mii_bus->phy_map[phy_addr]) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900617 if (phydev) {
618 printk(KERN_ERR "%s: multiple PHYs found\n",
619 dev->name);
620 return -EINVAL;
621 }
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700622 phydev = lp->mii_bus->phy_map[phy_addr];
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900623 break;
624 }
625 }
626
627 if (!phydev) {
628 printk(KERN_ERR "%s: no PHY found\n", dev->name);
629 return -ENODEV;
630 }
631
632 /* attach the mac to the phy */
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800633 phydev = phy_connect(dev, dev_name(&phydev->dev),
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900634 &tc_handle_link_change, 0,
635 lp->chiptype == TC35815_TX4939 ?
636 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII);
637 if (IS_ERR(phydev)) {
638 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
639 return PTR_ERR(phydev);
640 }
641 printk(KERN_INFO "%s: attached PHY driver [%s] "
642 "(mii_bus:phy_addr=%s, id=%x)\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -0800643 dev->name, phydev->drv->name, dev_name(&phydev->dev),
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900644 phydev->phy_id);
645
646 /* mask with MAC supported features */
647 phydev->supported &= PHY_BASIC_FEATURES;
648 dropmask = 0;
649 if (options.speed == 10)
650 dropmask |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
651 else if (options.speed == 100)
652 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
653 if (options.duplex == 1)
654 dropmask |= SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full;
655 else if (options.duplex == 2)
656 dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_100baseT_Half;
657 phydev->supported &= ~dropmask;
658 phydev->advertising = phydev->supported;
659
660 lp->link = 0;
661 lp->speed = 0;
662 lp->duplex = -1;
663 lp->phy_dev = phydev;
664
665 return 0;
666}
667
668static int tc_mii_init(struct net_device *dev)
669{
670 struct tc35815_local *lp = netdev_priv(dev);
671 int err;
672 int i;
673
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700674 lp->mii_bus = mdiobus_alloc();
675 if (lp->mii_bus == NULL) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900676 err = -ENOMEM;
677 goto err_out;
678 }
679
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700680 lp->mii_bus->name = "tc35815_mii_bus";
681 lp->mii_bus->read = tc_mdio_read;
682 lp->mii_bus->write = tc_mdio_write;
683 snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x",
684 (lp->pci_dev->bus->number << 8) | lp->pci_dev->devfn);
685 lp->mii_bus->priv = dev;
686 lp->mii_bus->parent = &lp->pci_dev->dev;
687 lp->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
688 if (!lp->mii_bus->irq) {
689 err = -ENOMEM;
690 goto err_out_free_mii_bus;
691 }
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900692
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700693 for (i = 0; i < PHY_MAX_ADDR; i++)
694 lp->mii_bus->irq[i] = PHY_POLL;
695
696 err = mdiobus_register(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900697 if (err)
698 goto err_out_free_mdio_irq;
699 err = tc_mii_probe(dev);
700 if (err)
701 goto err_out_unregister_bus;
702 return 0;
703
704err_out_unregister_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700705 mdiobus_unregister(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900706err_out_free_mdio_irq:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700707 kfree(lp->mii_bus->irq);
Adrian Bunk51cf7562008-10-12 21:01:53 -0700708err_out_free_mii_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700709 mdiobus_free(lp->mii_bus);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900710err_out:
711 return err;
712}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900714#ifdef CONFIG_CPU_TX49XX
715/*
716 * Find a platform_device providing a MAC address. The platform code
717 * should provide a "tc35815-mac" device with a MAC address in its
718 * platform_data.
719 */
720static int __devinit tc35815_mac_match(struct device *dev, void *data)
721{
722 struct platform_device *plat_dev = to_platform_device(dev);
723 struct pci_dev *pci_dev = data;
Atsushi Nemoto06675e62008-01-19 01:15:52 +0900724 unsigned int id = pci_dev->irq;
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900725 return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
726}
727
728static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
729{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900730 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900731 struct device *pd = bus_find_device(&platform_bus_type, NULL,
732 lp->pci_dev, tc35815_mac_match);
733 if (pd) {
734 if (pd->platform_data)
735 memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
736 put_device(pd);
737 return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
738 }
739 return -ENODEV;
740}
741#else
Yoichi Yuasa308a9062007-07-18 11:13:42 +0900742static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900743{
744 return -ENODEV;
745}
746#endif
747
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900748static int __devinit tc35815_init_dev_addr(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900750 struct tc35815_regs __iomem *tr =
751 (struct tc35815_regs __iomem *)dev->base_addr;
752 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
755 ;
756 for (i = 0; i < 6; i += 2) {
757 unsigned short data;
758 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
759 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
760 ;
761 data = tc_readl(&tr->PROM_Data);
762 dev->dev_addr[i] = data & 0xff;
763 dev->dev_addr[i+1] = data >> 8;
764 }
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900765 if (!is_valid_ether_addr(dev->dev_addr))
766 return tc35815_read_plat_dev_addr(dev);
767 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900768}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alexander Beregalov5a1c28b2009-04-11 07:38:54 +0000770static const struct net_device_ops tc35815_netdev_ops = {
771 .ndo_open = tc35815_open,
772 .ndo_stop = tc35815_close,
773 .ndo_start_xmit = tc35815_send_packet,
774 .ndo_get_stats = tc35815_get_stats,
775 .ndo_set_multicast_list = tc35815_set_multicast_list,
776 .ndo_tx_timeout = tc35815_tx_timeout,
777 .ndo_do_ioctl = tc35815_ioctl,
778 .ndo_validate_addr = eth_validate_addr,
779 .ndo_change_mtu = eth_change_mtu,
780 .ndo_set_mac_address = eth_mac_addr,
781#ifdef CONFIG_NET_POLL_CONTROLLER
782 .ndo_poll_controller = tc35815_poll_controller,
783#endif
784};
785
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900786static int __devinit tc35815_init_one(struct pci_dev *pdev,
787 const struct pci_device_id *ent)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900788{
789 void __iomem *ioaddr = NULL;
790 struct net_device *dev;
791 struct tc35815_local *lp;
792 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900794 static int printed_version;
795 if (!printed_version++) {
796 printk(version);
797 dev_printk(KERN_DEBUG, &pdev->dev,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900798 "speed:%d duplex:%d\n",
799 options.speed, options.duplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900802 if (!pdev->irq) {
803 dev_warn(&pdev->dev, "no IRQ assigned.\n");
804 return -ENODEV;
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900807 /* dev zeroed in alloc_etherdev */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900808 dev = alloc_etherdev(sizeof(*lp));
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900809 if (dev == NULL) {
810 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
811 return -ENOMEM;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 SET_NETDEV_DEV(dev, &pdev->dev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900814 lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700815 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900817 /* enable device (incl. PCI PM wakeup), and bus-mastering */
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900818 rc = pcim_enable_device(pdev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900819 if (rc)
820 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900821 rc = pcim_iomap_regions(pdev, 1 << 1, MODNAME);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900822 if (rc)
823 goto err_out;
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900824 pci_set_master(pdev);
825 ioaddr = pcim_iomap_table(pdev)[1];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900826
827 /* Initialize the device structure. */
Alexander Beregalov5a1c28b2009-04-11 07:38:54 +0000828 dev->netdev_ops = &tc35815_netdev_ops;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900829 dev->ethtool_ops = &tc35815_ethtool_ops;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900830 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700831 netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900832
833 dev->irq = pdev->irq;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900834 dev->base_addr = (unsigned long)ioaddr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900835
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900836 INIT_WORK(&lp->restart_work, tc35815_restart_work);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900837 spin_lock_init(&lp->lock);
838 lp->pci_dev = pdev;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900839 lp->chiptype = ent->driver_data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900840
841 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
842 pci_set_drvdata(pdev, dev);
843
844 /* Soft reset the chip. */
845 tc35815_chip_reset(dev);
846
847 /* Retrieve the ethernet address. */
Atsushi Nemotobd43da82007-06-29 22:34:53 +0900848 if (tc35815_init_dev_addr(dev)) {
849 dev_warn(&pdev->dev, "not valid ether addr\n");
850 random_ether_addr(dev->dev_addr);
851 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900852
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900853 rc = register_netdev(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900854 if (rc)
Atsushi Nemoto22adf7e2008-04-11 00:24:45 +0900855 goto err_out;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900856
857 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Johannes Berge1749612008-10-27 15:59:26 -0700858 printk(KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900859 dev->name,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900860 chip_info[ent->driver_data].name,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900861 dev->base_addr,
Johannes Berge1749612008-10-27 15:59:26 -0700862 dev->dev_addr,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900863 dev->irq);
864
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900865 rc = tc_mii_init(dev);
866 if (rc)
867 goto err_out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 return 0;
870
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900871err_out_unregister:
872 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873err_out:
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900874 free_netdev(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900875 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900879static void __devexit tc35815_remove_one(struct pci_dev *pdev)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900880{
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900881 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900882 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900883
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +0900884 phy_disconnect(lp->phy_dev);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700885 mdiobus_unregister(lp->mii_bus);
886 kfree(lp->mii_bus->irq);
887 mdiobus_free(lp->mii_bus);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900888 unregister_netdev(dev);
889 free_netdev(dev);
890 pci_set_drvdata(pdev, NULL);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static int
894tc35815_init_queues(struct net_device *dev)
895{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +0900896 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 int i;
898 unsigned long fd_addr;
899
900 if (!lp->fd_buf) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900901 BUG_ON(sizeof(struct FDesc) +
902 sizeof(struct BDesc) * RX_BUF_NUM +
903 sizeof(struct FDesc) * RX_FD_NUM +
904 sizeof(struct TxFD) * TX_FD_NUM >
905 PAGE_SIZE * FD_PAGE_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900907 lp->fd_buf = pci_alloc_consistent(lp->pci_dev,
908 PAGE_SIZE * FD_PAGE_NUM,
909 &lp->fd_buf_dma);
910 if (!lp->fd_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENOMEM;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900912 for (i = 0; i < RX_BUF_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900913 lp->rx_skbs[i].skb =
914 alloc_rxbuf_skb(dev, lp->pci_dev,
915 &lp->rx_skbs[i].skb_dma);
916 if (!lp->rx_skbs[i].skb) {
917 while (--i >= 0) {
918 free_rxbuf_skb(lp->pci_dev,
919 lp->rx_skbs[i].skb,
920 lp->rx_skbs[i].skb_dma);
921 lp->rx_skbs[i].skb = NULL;
922 }
923 pci_free_consistent(lp->pci_dev,
924 PAGE_SIZE * FD_PAGE_NUM,
925 lp->fd_buf,
926 lp->fd_buf_dma);
927 lp->fd_buf = NULL;
928 return -ENOMEM;
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900931 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
932 dev->name, lp->fd_buf);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900933 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 } else {
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900935 for (i = 0; i < FD_PAGE_NUM; i++)
936 clear_page((void *)((unsigned long)lp->fd_buf +
937 i * PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 fd_addr = (unsigned long)lp->fd_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /* Free Descriptors (for Receive) */
942 lp->rfd_base = (struct RxFD *)fd_addr;
943 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +0900944 for (i = 0; i < RX_FD_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 lp->rfd_cur = lp->rfd_base;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900947 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* Transmit Descriptors */
950 lp->tfd_base = (struct TxFD *)fd_addr;
951 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
952 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900953 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
954 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
956 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900957 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 -0700958 lp->tfd_start = 0;
959 lp->tfd_end = 0;
960
961 /* Buffer List (for Receive) */
962 lp->fbl_ptr = (struct FrFD *)fd_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900963 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
964 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900965 /*
966 * move all allocated skbs to head of rx_skbs[] array.
967 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
968 * tc35815_rx() had failed.
969 */
970 lp->fbl_count = 0;
971 for (i = 0; i < RX_BUF_NUM; i++) {
972 if (lp->rx_skbs[i].skb) {
973 if (i != lp->fbl_count) {
974 lp->rx_skbs[lp->fbl_count].skb =
975 lp->rx_skbs[i].skb;
976 lp->rx_skbs[lp->fbl_count].skb_dma =
977 lp->rx_skbs[i].skb_dma;
978 }
979 lp->fbl_count++;
980 }
981 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900982 for (i = 0; i < RX_BUF_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900983 if (i >= lp->fbl_count) {
984 lp->fbl_ptr->bd[i].BuffData = 0;
985 lp->fbl_ptr->bd[i].BDCtl = 0;
986 continue;
987 }
988 lp->fbl_ptr->bd[i].BuffData =
989 cpu_to_le32(lp->rx_skbs[i].skb_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* BDID is index of FrFD.bd[] */
991 lp->fbl_ptr->bd[i].BDCtl =
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900992 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
993 RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +0900996 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
997 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
999}
1000
1001static void
1002tc35815_clear_queues(struct net_device *dev)
1003{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001004 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int i;
1006
1007 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001008 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1009 struct sk_buff *skb =
1010 fdsystem != 0xffffffff ?
1011 lp->tx_skbs[fdsystem].skb : NULL;
1012#ifdef DEBUG
1013 if (lp->tx_skbs[i].skb != skb) {
1014 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1015 panic_queues(dev);
1016 }
1017#else
1018 BUG_ON(lp->tx_skbs[i].skb != skb);
1019#endif
1020 if (skb) {
1021 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1022 lp->tx_skbs[i].skb = NULL;
1023 lp->tx_skbs[i].skb_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001025 }
1026 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
1029 tc35815_init_queues(dev);
1030}
1031
1032static void
1033tc35815_free_queues(struct net_device *dev)
1034{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001035 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int i;
1037
1038 if (lp->tfd_base) {
1039 for (i = 0; i < TX_FD_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001040 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1041 struct sk_buff *skb =
1042 fdsystem != 0xffffffff ?
1043 lp->tx_skbs[fdsystem].skb : NULL;
1044#ifdef DEBUG
1045 if (lp->tx_skbs[i].skb != skb) {
1046 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1047 panic_queues(dev);
1048 }
1049#else
1050 BUG_ON(lp->tx_skbs[i].skb != skb);
1051#endif
1052 if (skb) {
1053 dev_kfree_skb(skb);
1054 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1055 lp->tx_skbs[i].skb = NULL;
1056 lp->tx_skbs[i].skb_dma = 0;
1057 }
1058 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 }
1061
1062 lp->rfd_base = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 lp->rfd_limit = NULL;
1064 lp->rfd_cur = NULL;
1065 lp->fbl_ptr = NULL;
1066
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001067 for (i = 0; i < RX_BUF_NUM; i++) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001068 if (lp->rx_skbs[i].skb) {
1069 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1070 lp->rx_skbs[i].skb_dma);
1071 lp->rx_skbs[i].skb = NULL;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001074 if (lp->fd_buf) {
1075 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1076 lp->fd_buf, lp->fd_buf_dma);
1077 lp->fd_buf = NULL;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static void
1082dump_txfd(struct TxFD *fd)
1083{
1084 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1085 le32_to_cpu(fd->fd.FDNext),
1086 le32_to_cpu(fd->fd.FDSystem),
1087 le32_to_cpu(fd->fd.FDStat),
1088 le32_to_cpu(fd->fd.FDCtl));
1089 printk("BD: ");
1090 printk(" %08x %08x",
1091 le32_to_cpu(fd->bd.BuffData),
1092 le32_to_cpu(fd->bd.BDCtl));
1093 printk("\n");
1094}
1095
1096static int
1097dump_rxfd(struct RxFD *fd)
1098{
1099 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1100 if (bd_count > 8)
1101 bd_count = 8;
1102 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1103 le32_to_cpu(fd->fd.FDNext),
1104 le32_to_cpu(fd->fd.FDSystem),
1105 le32_to_cpu(fd->fd.FDStat),
1106 le32_to_cpu(fd->fd.FDCtl));
1107 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 printk("BD: ");
1110 for (i = 0; i < bd_count; i++)
1111 printk(" %08x %08x",
1112 le32_to_cpu(fd->bd[i].BuffData),
1113 le32_to_cpu(fd->bd[i].BDCtl));
1114 printk("\n");
1115 return bd_count;
1116}
1117
Atsushi Nemotoa02b7b72009-11-02 04:34:47 +00001118#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119static void
1120dump_frfd(struct FrFD *fd)
1121{
1122 int i;
1123 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1124 le32_to_cpu(fd->fd.FDNext),
1125 le32_to_cpu(fd->fd.FDSystem),
1126 le32_to_cpu(fd->fd.FDStat),
1127 le32_to_cpu(fd->fd.FDCtl));
1128 printk("BD: ");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001129 for (i = 0; i < RX_BUF_NUM; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 printk(" %08x %08x",
1131 le32_to_cpu(fd->bd[i].BuffData),
1132 le32_to_cpu(fd->bd[i].BDCtl));
1133 printk("\n");
1134}
1135
1136static void
1137panic_queues(struct net_device *dev)
1138{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001139 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int i;
1141
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001142 printk("TxFD base %p, start %u, end %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1144 printk("RxFD base %p limit %p cur %p\n",
1145 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1146 printk("FrFD %p\n", lp->fbl_ptr);
1147 for (i = 0; i < TX_FD_NUM; i++)
1148 dump_txfd(&lp->tfd_base[i]);
1149 for (i = 0; i < RX_FD_NUM; i++) {
1150 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1151 i += (bd_count + 1) / 2; /* skip BDs */
1152 }
1153 dump_frfd(lp->fbl_ptr);
1154 panic("%s: Illegal queue state.", dev->name);
1155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156#endif
1157
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001158static void print_eth(const u8 *add)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001160 printk(KERN_DEBUG "print_eth(%p)\n", add);
Johannes Berge1749612008-10-27 15:59:26 -07001161 printk(KERN_DEBUG " %pM => %pM : %02x%02x\n",
1162 add + 6, add, add[12], add[13]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001165static int tc35815_tx_full(struct net_device *dev)
1166{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001167 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001168 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1169}
1170
1171static void tc35815_restart(struct net_device *dev)
1172{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001173 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001174
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001175 if (lp->phy_dev) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001176 int timeout;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001177
1178 phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001179 timeout = 100;
1180 while (--timeout) {
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001181 if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET))
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001182 break;
1183 udelay(1);
1184 }
1185 if (!timeout)
1186 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1187 }
1188
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001189 spin_lock_irq(&lp->lock);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001190 tc35815_chip_reset(dev);
1191 tc35815_clear_queues(dev);
1192 tc35815_chip_init(dev);
1193 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1194 tc35815_set_multicast_list(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001195 spin_unlock_irq(&lp->lock);
1196
1197 netif_wake_queue(dev);
1198}
1199
1200static void tc35815_restart_work(struct work_struct *work)
1201{
1202 struct tc35815_local *lp =
1203 container_of(work, struct tc35815_local, restart_work);
1204 struct net_device *dev = lp->dev;
1205
1206 tc35815_restart(dev);
1207}
1208
1209static void tc35815_schedule_restart(struct net_device *dev)
1210{
1211 struct tc35815_local *lp = netdev_priv(dev);
1212 struct tc35815_regs __iomem *tr =
1213 (struct tc35815_regs __iomem *)dev->base_addr;
1214
1215 /* disable interrupts */
1216 tc_writel(0, &tr->Int_En);
1217 tc_writel(tc_readl(&tr->DMA_Ctl) | DMA_IntMask, &tr->DMA_Ctl);
1218 schedule_work(&lp->restart_work);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001219}
1220
1221static void tc35815_tx_timeout(struct net_device *dev)
1222{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001223 struct tc35815_regs __iomem *tr =
1224 (struct tc35815_regs __iomem *)dev->base_addr;
1225
1226 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1227 dev->name, tc_readl(&tr->Tx_Stat));
1228
1229 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001230 tc35815_schedule_restart(dev);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001231 dev->stats.tx_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/*
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001235 * Open/initialize the controller. This is called (in the current kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * sometime after booting when the 'ifconfig' program is run.
1237 *
1238 * This routine should set everything up anew at each open, even
1239 * registers that "should" only need to be set once at boot, so that
1240 * there is non-reboot way to recover if something goes wrong.
1241 */
1242static int
1243tc35815_open(struct net_device *dev)
1244{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001245 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
1248 * This is used if the interrupt line can turned off (shared).
1249 * See 3c503.c for an example of selecting the IRQ at config-time.
1250 */
Joe Perchesa0607fd2009-11-18 23:29:17 -08001251 if (request_irq(dev->irq, tc35815_interrupt, IRQF_SHARED,
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001252 dev->name, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 tc35815_chip_reset(dev);
1256
1257 if (tc35815_init_queues(dev) != 0) {
1258 free_irq(dev->irq, dev);
1259 return -EAGAIN;
1260 }
1261
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001262 napi_enable(&lp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* Reset the hardware here. Don't forget to set the station address. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001265 spin_lock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 tc35815_chip_init(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001267 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Atsushi Nemoto59524a32008-06-25 11:41:01 +09001269 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001270 /* schedule a link state check */
1271 phy_start(lp->phy_dev);
1272
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001273 /* We are now ready to accept transmit requeusts from
1274 * the queueing layer of the networking.
1275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 netif_start_queue(dev);
1277
1278 return 0;
1279}
1280
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001281/* This will only be invoked if your driver is _not_ in XOFF state.
1282 * What this means is that you need not check it, and that this
1283 * invariant will hold if you make sure that the netif_*_queue()
1284 * calls are done at the proper times.
1285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1287{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001288 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001289 struct TxFD *txfd;
1290 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001292 /* If some error occurs while trying to transmit this
1293 * packet, you should return '1' from this function.
1294 * In such a case you _may not_ do anything to the
1295 * SKB, it is still owned by the network queueing
1296 * layer when an error is returned. This means you
1297 * may not modify any SKB fields, you may not free
1298 * the SKB, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001301 /* This is the most common case for modern hardware.
1302 * The spinlock protects this code from the TX complete
1303 * hardware interrupt handler. Queue flow control is
1304 * thus managed under this lock as well.
1305 */
1306 spin_lock_irqsave(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001308 /* failsafe... (handle txdone now if half of FDs are used) */
1309 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1310 TX_FD_NUM / 2)
1311 tc35815_txdone(dev);
1312
1313 if (netif_msg_pktdata(lp))
1314 print_eth(skb->data);
1315#ifdef DEBUG
1316 if (lp->tx_skbs[lp->tfd_start].skb) {
1317 printk("%s: tx_skbs conflict.\n", dev->name);
1318 panic_queues(dev);
1319 }
1320#else
1321 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001323 lp->tx_skbs[lp->tfd_start].skb = skb;
1324 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 -07001325
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001326 /*add to ring */
1327 txfd = &lp->tfd_base[lp->tfd_start];
1328 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1329 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1330 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1331 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001333 if (lp->tfd_start == lp->tfd_end) {
1334 struct tc35815_regs __iomem *tr =
1335 (struct tc35815_regs __iomem *)dev->base_addr;
1336 /* Start DMA Transmitter. */
1337 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001338 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001339 if (netif_msg_tx_queued(lp)) {
1340 printk("%s: starting TxFD.\n", dev->name);
1341 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001343 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1344 } else {
1345 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1346 if (netif_msg_tx_queued(lp)) {
1347 printk("%s: queueing TxFD.\n", dev->name);
1348 dump_txfd(txfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001350 }
1351 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1352
1353 dev->trans_start = jiffies;
1354
1355 /* If we just used up the very last entry in the
1356 * TX ring on this device, tell the queueing
1357 * layer to send no more.
1358 */
1359 if (tc35815_tx_full(dev)) {
1360 if (netif_msg_tx_queued(lp))
1361 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1362 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001365 /* When the TX completion hw interrupt arrives, this
1366 * is when the transmit statistics are updated.
1367 */
1368
1369 spin_unlock_irqrestore(&lp->lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001370 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373#define FATAL_ERROR_INT \
1374 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001375static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 static int count;
1378 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1379 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (status & Int_IntPCI)
1381 printk(" IntPCI");
1382 if (status & Int_DmParErr)
1383 printk(" DmParErr");
1384 if (status & Int_IntNRAbt)
1385 printk(" IntNRAbt");
1386 printk("\n");
1387 if (count++ > 100)
1388 panic("%s: Too many fatal errors.", dev->name);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001389 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Try to restart the adaptor. */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001391 tc35815_schedule_restart(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001392}
1393
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001394static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001395{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001396 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001397 int ret = -1;
1398
1399 /* Fatal errors... */
1400 if (status & FATAL_ERROR_INT) {
1401 tc35815_fatal_error_interrupt(dev, status);
1402 return 0;
1403 }
1404 /* recoverable errors */
1405 if (status & Int_IntFDAEx) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001406 if (netif_msg_rx_err(lp))
1407 dev_warn(&dev->dev,
1408 "Free Descriptor Area Exhausted (%#x).\n",
1409 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001410 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001411 ret = 0;
1412 }
1413 if (status & Int_IntBLEx) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001414 if (netif_msg_rx_err(lp))
1415 dev_warn(&dev->dev,
1416 "Buffer List Exhausted (%#x).\n",
1417 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001418 dev->stats.rx_dropped++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001419 ret = 0;
1420 }
1421 if (status & Int_IntExBD) {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001422 if (netif_msg_rx_err(lp))
1423 dev_warn(&dev->dev,
1424 "Excessive Buffer Descriptiors (%#x).\n",
1425 status);
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001426 dev->stats.rx_length_errors++;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001427 ret = 0;
1428 }
1429
1430 /* normal notification */
1431 if (status & Int_IntMacRx) {
1432 /* Got a packet(s). */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001433 ret = tc35815_rx(dev, limit);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001434 lp->lstats.rx_ints++;
1435 }
1436 if (status & Int_IntMacTx) {
1437 /* Transmit complete. */
1438 lp->lstats.tx_ints++;
1439 tc35815_txdone(dev);
1440 netif_wake_queue(dev);
Atsushi Nemoto02c5c8e2009-10-26 03:46:21 +00001441 if (ret < 0)
1442 ret = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001443 }
1444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447/*
1448 * The typical workload of the driver:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001449 * Handle the network interface interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
David Howells7d12e782006-10-05 14:55:46 +01001451static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 struct net_device *dev = dev_id;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001454 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001455 struct tc35815_regs __iomem *tr =
1456 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001457 u32 dmactl = tc_readl(&tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001459 if (!(dmactl & DMA_IntMask)) {
1460 /* disable interrupts */
1461 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
Ben Hutchings288379f2009-01-19 16:43:59 -08001462 if (napi_schedule_prep(&lp->napi))
1463 __napi_schedule(&lp->napi);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001464 else {
1465 printk(KERN_ERR "%s: interrupt taken in poll\n",
1466 dev->name);
1467 BUG();
1468 }
1469 (void)tc_readl(&tr->Int_Src); /* flush */
1470 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001472 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001475#ifdef CONFIG_NET_POLL_CONTROLLER
1476static void tc35815_poll_controller(struct net_device *dev)
1477{
1478 disable_irq(dev->irq);
1479 tc35815_interrupt(dev->irq, dev);
1480 enable_irq(dev->irq);
1481}
1482#endif
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/* We have a good packet(s), get it/them out of the buffers. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001485static int
1486tc35815_rx(struct net_device *dev, int limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001488 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 unsigned int fdctl;
1490 int i;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001491 int received = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1494 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1495 int pkt_len = fdctl & FD_FDLength_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001497#ifdef DEBUG
1498 struct RxFD *next_rfd;
1499#endif
1500#if (RX_CTL_CMD & Rx_StripCRC) == 0
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001501 pkt_len -= ETH_FCS_LEN;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001504 if (netif_msg_rx_status(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 dump_rxfd(lp->rfd_cur);
1506 if (status & Rx_Good) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct sk_buff *skb;
1508 unsigned char *data;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001509 int cur_bd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001511 if (--limit < 0)
1512 break;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001513 BUG_ON(bd_count > 1);
1514 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1515 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1516#ifdef DEBUG
1517 if (cur_bd >= RX_BUF_NUM) {
1518 printk("%s: invalid BDID.\n", dev->name);
1519 panic_queues(dev);
1520 }
1521 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1522 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1523 if (!lp->rx_skbs[cur_bd].skb) {
1524 printk("%s: NULL skb.\n", dev->name);
1525 panic_queues(dev);
1526 }
1527#else
1528 BUG_ON(cur_bd >= RX_BUF_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529#endif
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001530 skb = lp->rx_skbs[cur_bd].skb;
1531 prefetch(skb->data);
1532 lp->rx_skbs[cur_bd].skb = NULL;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001533 pci_unmap_single(lp->pci_dev,
1534 lp->rx_skbs[cur_bd].skb_dma,
1535 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
Atsushi Nemoto82a99282008-12-11 20:58:04 -08001536 if (!HAVE_DMA_RXALIGN(lp) && NET_IP_ALIGN)
1537 memmove(skb->data, skb->data - NET_IP_ALIGN,
1538 pkt_len);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001539 data = skb_put(skb, pkt_len);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001540 if (netif_msg_pktdata(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 print_eth(data);
1542 skb->protocol = eth_type_trans(skb, dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001543 netif_receive_skb(skb);
1544 received++;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001545 dev->stats.rx_packets++;
1546 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 } else {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001548 dev->stats.rx_errors++;
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001549 if (netif_msg_rx_err(lp))
1550 dev_info(&dev->dev, "Rx error (status %x)\n",
1551 status & Rx_Stat_Mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1553 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1554 status &= ~(Rx_LongErr|Rx_CRCErr);
1555 status |= Rx_Over;
1556 }
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001557 if (status & Rx_LongErr)
1558 dev->stats.rx_length_errors++;
1559 if (status & Rx_Over)
1560 dev->stats.rx_fifo_errors++;
1561 if (status & Rx_CRCErr)
1562 dev->stats.rx_crc_errors++;
1563 if (status & Rx_Align)
1564 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
1567 if (bd_count > 0) {
1568 /* put Free Buffer back to controller */
1569 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1570 unsigned char id =
1571 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001572#ifdef DEBUG
1573 if (id >= RX_BUF_NUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 printk("%s: invalid BDID.\n", dev->name);
1575 panic_queues(dev);
1576 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001577#else
1578 BUG_ON(id >= RX_BUF_NUM);
1579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /* free old buffers */
Atsushi Nemotoccc57aa2008-06-26 17:14:15 +09001581 lp->fbl_count--;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001582 while (lp->fbl_count < RX_BUF_NUM)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001583 {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001584 unsigned char curid =
1585 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001586 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1587#ifdef DEBUG
1588 bdctl = le32_to_cpu(bd->BDCtl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (bdctl & BD_CownsBD) {
1590 printk("%s: Freeing invalid BD.\n",
1591 dev->name);
1592 panic_queues(dev);
1593 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001594#endif
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001595 /* pass BD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001596 if (!lp->rx_skbs[curid].skb) {
1597 lp->rx_skbs[curid].skb =
1598 alloc_rxbuf_skb(dev,
1599 lp->pci_dev,
1600 &lp->rx_skbs[curid].skb_dma);
1601 if (!lp->rx_skbs[curid].skb)
1602 break; /* try on next reception */
1603 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /* Note: BDLength was modified by chip. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001606 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1607 (curid << BD_RxBDID_SHIFT) |
1608 RX_BUF_SIZE);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001609 lp->fbl_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611 }
1612
1613 /* put RxFD back to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001614#ifdef DEBUG
1615 next_rfd = fd_bus_to_virt(lp,
1616 le32_to_cpu(lp->rfd_cur->fd.FDNext));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1618 printk("%s: RxFD FDNext invalid.\n", dev->name);
1619 panic_queues(dev);
1620 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001623 /* pass FD to controller */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001624#ifdef DEBUG
1625 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1626#else
1627 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1630 lp->rfd_cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001632 if (lp->rfd_cur > lp->rfd_limit)
1633 lp->rfd_cur = lp->rfd_base;
1634#ifdef DEBUG
1635 if (lp->rfd_cur != next_rfd)
1636 printk("rfd_cur = %p, next_rfd %p\n",
1637 lp->rfd_cur, next_rfd);
1638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001641 return received;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001644static int tc35815_poll(struct napi_struct *napi, int budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001645{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001646 struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
1647 struct net_device *dev = lp->dev;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001648 struct tc35815_regs __iomem *tr =
1649 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001650 int received = 0, handled;
1651 u32 status;
1652
1653 spin_lock(&lp->lock);
1654 status = tc_readl(&tr->Int_Src);
1655 do {
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001656 /* BLEx, FDAEx will be cleared later */
1657 tc_writel(status & ~(Int_BLEx | Int_FDAEx),
1658 &tr->Int_Src); /* write to clear */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001659
Atsushi Nemotoa2c465d2009-04-02 01:17:36 -07001660 handled = tc35815_do_interrupt(dev, status, budget - received);
Atsushi Nemotodb30f5e2009-08-06 04:41:46 +00001661 if (status & (Int_BLEx | Int_FDAEx))
1662 tc_writel(status & (Int_BLEx | Int_FDAEx),
1663 &tr->Int_Src);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001664 if (handled >= 0) {
1665 received += handled;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001666 if (received >= budget)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001667 break;
1668 }
1669 status = tc_readl(&tr->Int_Src);
1670 } while (status);
1671 spin_unlock(&lp->lock);
1672
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001673 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001674 napi_complete(napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001675 /* enable interrupts */
1676 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1677 }
1678 return received;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001679}
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681#define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683static void
1684tc35815_check_tx_stat(struct net_device *dev, int status)
1685{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001686 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 const char *msg = NULL;
1688
1689 /* count collisions */
1690 if (status & Tx_ExColl)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001691 dev->stats.collisions += 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (status & Tx_TxColl_MASK)
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001693 dev->stats.collisions += status & Tx_TxColl_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001695 /* TX4939 does not have NCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001696 if (lp->chiptype == TC35815_TX4939)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 status &= ~Tx_NCarr;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001698 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001699 if (!lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001700 status &= ~Tx_NCarr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 if (!(status & TX_STA_ERR)) {
1703 /* no error. */
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001704 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return;
1706 }
1707
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001708 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (status & Tx_ExColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001710 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 msg = "Excessive Collision.";
1712 }
1713 if (status & Tx_Under) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001714 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 msg = "Tx FIFO Underrun.";
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001716 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1717 lp->lstats.tx_underrun++;
1718 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1719 struct tc35815_regs __iomem *tr =
1720 (struct tc35815_regs __iomem *)dev->base_addr;
1721 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1722 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1723 }
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 if (status & Tx_Defer) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001727 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 msg = "Excessive Deferral.";
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (status & Tx_NCarr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001731 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 msg = "Lost Carrier Sense.";
1733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (status & Tx_LateColl) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001735 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 msg = "Late Collision.";
1737 }
1738 if (status & Tx_TxPar) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001739 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 msg = "Transmit Parity Error.";
1741 }
1742 if (status & Tx_SQErr) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001743 dev->stats.tx_heartbeat_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 msg = "Signal Quality Error.";
1745 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001746 if (msg && netif_msg_tx_err(lp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
1748}
1749
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001750/* This handles TX complete events posted by the device
1751 * via interrupts.
1752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753static void
1754tc35815_txdone(struct net_device *dev)
1755{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001756 struct tc35815_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct TxFD *txfd;
1758 unsigned int fdctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 txfd = &lp->tfd_base[lp->tfd_end];
1761 while (lp->tfd_start != lp->tfd_end &&
1762 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
1763 int status = le32_to_cpu(txfd->fd.FDStat);
1764 struct sk_buff *skb;
1765 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001766 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001768 if (netif_msg_tx_done(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 printk("%s: complete TxFD.\n", dev->name);
1770 dump_txfd(txfd);
1771 }
1772 tc35815_check_tx_stat(dev, status);
1773
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001774 skb = fdsystem != 0xffffffff ?
1775 lp->tx_skbs[fdsystem].skb : NULL;
1776#ifdef DEBUG
1777 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
1778 printk("%s: tx_skbs mismatch.\n", dev->name);
1779 panic_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001781#else
1782 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
1783#endif
1784 if (skb) {
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001785 dev->stats.tx_bytes += skb->len;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001786 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
1787 lp->tx_skbs[lp->tfd_end].skb = NULL;
1788 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001789 dev_kfree_skb_any(skb);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001790 }
1791 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
1794 txfd = &lp->tfd_base[lp->tfd_end];
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001795#ifdef DEBUG
1796 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 printk("%s: TxFD FDNext invalid.\n", dev->name);
1798 panic_queues(dev);
1799 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001800#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (fdnext & FD_Next_EOL) {
1802 /* DMA Transmitter has been stopping... */
1803 if (lp->tfd_end != lp->tfd_start) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001804 struct tc35815_regs __iomem *tr =
1805 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001807 struct TxFD *txhead = &lp->tfd_base[head];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 int qlen = (lp->tfd_start + TX_FD_NUM
1809 - lp->tfd_end) % TX_FD_NUM;
1810
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001811#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
1813 printk("%s: TxFD FDCtl invalid.\n", dev->name);
1814 panic_queues(dev);
1815 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* log max queue length */
1818 if (lp->lstats.max_tx_qlen < qlen)
1819 lp->lstats.max_tx_qlen = qlen;
1820
1821
1822 /* start DMA Transmitter again */
1823 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001825 if (netif_msg_tx_queued(lp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 printk("%s: start TxFD on queue.\n",
1827 dev->name);
1828 dump_txfd(txfd);
1829 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001830 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832 break;
1833 }
1834 }
1835
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001836 /* If we had stopped the queue due to a "tx full"
1837 * condition, and space has now been made available,
1838 * wake up the queue.
1839 */
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001840 if (netif_queue_stopped(dev) && !tc35815_tx_full(dev))
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001841 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844/* The inverse routine to tc35815_open(). */
1845static int
1846tc35815_close(struct net_device *dev)
1847{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001848 struct tc35815_local *lp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001851 napi_disable(&lp->napi);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001852 if (lp->phy_dev)
1853 phy_stop(lp->phy_dev);
1854 cancel_work_sync(&lp->restart_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* Flush the Tx and disable Rx here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 tc35815_chip_reset(dev);
1858 free_irq(dev->irq, dev);
1859
1860 tc35815_free_queues(dev);
1861
1862 return 0;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
1866/*
1867 * Get the current statistics.
1868 * This may be called with the card open or closed.
1869 */
1870static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
1871{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001872 struct tc35815_regs __iomem *tr =
1873 (struct tc35815_regs __iomem *)dev->base_addr;
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001874 if (netif_running(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /* Update the statistics from the device registers. */
Atsushi Nemoto7bb82e82009-08-06 04:41:48 +00001876 dev->stats.rx_missed_errors += tc_readl(&tr->Miss_Cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Atsushi Nemotoc201abd92008-04-11 00:24:12 +09001878 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001881static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001883 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001884 struct tc35815_regs __iomem *tr =
1885 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 int cam_index = index * 6;
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001887 u32 cam_data;
1888 u32 saved_addr;
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 saved_addr = tc_readl(&tr->CAM_Adr);
1891
Atsushi Nemoto958eb802008-04-11 00:24:24 +09001892 if (netif_msg_hw(lp))
Johannes Berge1749612008-10-27 15:59:26 -07001893 printk(KERN_DEBUG "%s: CAM %d: %pM\n",
1894 dev->name, index, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (index & 1) {
1896 /* read modify write */
1897 tc_writel(cam_index - 2, &tr->CAM_Adr);
1898 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
1899 cam_data |= addr[0] << 8 | addr[1];
1900 tc_writel(cam_data, &tr->CAM_Data);
1901 /* write whole word */
1902 tc_writel(cam_index + 2, &tr->CAM_Adr);
1903 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
1904 tc_writel(cam_data, &tr->CAM_Data);
1905 } else {
1906 /* write whole word */
1907 tc_writel(cam_index, &tr->CAM_Adr);
1908 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
1909 tc_writel(cam_data, &tr->CAM_Data);
1910 /* read modify write */
1911 tc_writel(cam_index + 4, &tr->CAM_Adr);
1912 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
1913 cam_data |= addr[4] << 24 | (addr[5] << 16);
1914 tc_writel(cam_data, &tr->CAM_Data);
1915 }
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 tc_writel(saved_addr, &tr->CAM_Adr);
1918}
1919
1920
1921/*
1922 * Set or clear the multicast filter for this adaptor.
1923 * num_addrs == -1 Promiscuous mode, receive all packets
1924 * num_addrs == 0 Normal mode, clear multicast list
1925 * num_addrs > 0 Multicast mode, receive normal and MC packets,
1926 * and do best-effort filtering.
1927 */
1928static void
1929tc35815_set_multicast_list(struct net_device *dev)
1930{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001931 struct tc35815_regs __iomem *tr =
1932 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001934 if (dev->flags & IFF_PROMISC) {
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001935 /* With some (all?) 100MHalf HUB, controller will hang
1936 * if we enabled promiscuous mode before linkup... */
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001937 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001938
1939 if (!lp->link)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001940 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* Enable promiscuous mode */
1942 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001943 } else if ((dev->flags & IFF_ALLMULTI) ||
1944 dev->mc_count > CAM_ENTRY_MAX - 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* CAM 0, 1, 20 are reserved. */
1946 /* Disable promiscuous mode, use normal mode. */
1947 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001948 } else if (dev->mc_count) {
1949 struct dev_mc_list *cur_addr = dev->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int i;
1951 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
1952
1953 tc_writel(0, &tr->CAM_Ctl);
1954 /* Walk the address list, and load the filter */
1955 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
1956 if (!cur_addr)
1957 break;
1958 /* entry 0,1 is reserved. */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001959 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 ena_bits |= CAM_Ena_Bit(i + 2);
1961 }
1962 tc_writel(ena_bits, &tr->CAM_Ena);
1963 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
Atsushi Nemoto7f225b42008-04-11 00:25:31 +09001964 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
1966 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
1967 }
1968}
1969
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001970static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001972 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001973 strcpy(info->driver, MODNAME);
1974 strcpy(info->version, DRV_VERSION);
1975 strcpy(info->bus_info, pci_name(lp->pci_dev));
1976}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001977
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001978static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1979{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001980 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001981
1982 if (!lp->phy_dev)
1983 return -ENODEV;
1984 return phy_ethtool_gset(lp->phy_dev, cmd);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001987static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1988{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001989 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001990
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09001991 if (!lp->phy_dev)
1992 return -ENODEV;
1993 return phy_ethtool_sset(lp->phy_dev, cmd);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001994}
1995
1996static u32 tc35815_get_msglevel(struct net_device *dev)
1997{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09001998 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09001999 return lp->msg_enable;
2000}
2001
2002static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2003{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002004 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002005 lp->msg_enable = datum;
2006}
2007
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002008static int tc35815_get_sset_count(struct net_device *dev, int sset)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002009{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002010 struct tc35815_local *lp = netdev_priv(dev);
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002011
2012 switch (sset) {
2013 case ETH_SS_STATS:
2014 return sizeof(lp->lstats) / sizeof(int);
2015 default:
2016 return -EOPNOTSUPP;
2017 }
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002018}
2019
2020static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2021{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002022 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002023 data[0] = lp->lstats.max_tx_qlen;
2024 data[1] = lp->lstats.tx_ints;
2025 data[2] = lp->lstats.rx_ints;
2026 data[3] = lp->lstats.tx_underrun;
2027}
2028
2029static struct {
2030 const char str[ETH_GSTRING_LEN];
2031} ethtool_stats_keys[] = {
2032 { "max_tx_qlen" },
2033 { "tx_ints" },
2034 { "rx_ints" },
2035 { "tx_underrun" },
2036};
2037
2038static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2039{
2040 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2041}
2042
2043static const struct ethtool_ops tc35815_ethtool_ops = {
2044 .get_drvinfo = tc35815_get_drvinfo,
2045 .get_settings = tc35815_get_settings,
2046 .set_settings = tc35815_set_settings,
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002047 .get_link = ethtool_op_get_link,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002048 .get_msglevel = tc35815_get_msglevel,
2049 .set_msglevel = tc35815_set_msglevel,
2050 .get_strings = tc35815_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002051 .get_sset_count = tc35815_get_sset_count,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002052 .get_ethtool_stats = tc35815_get_ethtool_stats,
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002053};
2054
2055static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2056{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002057 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002058
2059 if (!netif_running(dev))
2060 return -EINVAL;
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002061 if (!lp->phy_dev)
2062 return -ENODEV;
2063 return phy_mii_ioctl(lp->phy_dev, if_mii(rq), cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066static void tc35815_chip_reset(struct net_device *dev)
2067{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002068 struct tc35815_regs __iomem *tr =
2069 (struct tc35815_regs __iomem *)dev->base_addr;
2070 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* reset the controller */
2072 tc_writel(MAC_Reset, &tr->MAC_Ctl);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002073 udelay(4); /* 3200ns */
2074 i = 0;
2075 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2076 if (i++ > 100) {
2077 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2078 break;
2079 }
2080 mdelay(1);
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 tc_writel(0, &tr->MAC_Ctl);
2083
2084 /* initialize registers to default value */
2085 tc_writel(0, &tr->DMA_Ctl);
2086 tc_writel(0, &tr->TxThrsh);
2087 tc_writel(0, &tr->TxPollCtr);
2088 tc_writel(0, &tr->RxFragSize);
2089 tc_writel(0, &tr->Int_En);
2090 tc_writel(0, &tr->FDA_Bas);
2091 tc_writel(0, &tr->FDA_Lim);
2092 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2093 tc_writel(0, &tr->CAM_Ctl);
2094 tc_writel(0, &tr->Tx_Ctl);
2095 tc_writel(0, &tr->Rx_Ctl);
2096 tc_writel(0, &tr->CAM_Ena);
2097 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2098
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002099 /* initialize internal SRAM */
2100 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2101 for (i = 0; i < 0x1000; i += 4) {
2102 tc_writel(i, &tr->CAM_Adr);
2103 tc_writel(0, &tr->CAM_Data);
2104 }
2105 tc_writel(0, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
2108static void tc35815_chip_init(struct net_device *dev)
2109{
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002110 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002111 struct tc35815_regs __iomem *tr =
2112 (struct tc35815_regs __iomem *)dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 unsigned long txctl = TX_CTL_CMD;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /* load station address to CAM */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002116 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /* Enable CAM (broadcast and unicast) */
2119 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2120 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2121
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002122 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2123 if (HAVE_DMA_RXALIGN(lp))
2124 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2125 else
2126 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2128 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2129 tc_writel(INT_EN_CMD, &tr->Int_En);
2130
2131 /* set queues */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002132 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2134 &tr->FDA_Lim);
2135 /*
2136 * Activation method:
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002137 * First, enable the MAC Transmitter and the DMA Receive circuits.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 * Then enable the DMA Transmitter and the MAC Receive circuits.
2139 */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002140 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /* start MAC transmitter */
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002144 /* TX4939 does not have EnLCarr */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002145 if (lp->chiptype == TC35815_TX4939)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002146 txctl &= ~Tx_EnLCarr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /* WORKAROUND: ignore LostCrS in full duplex operation */
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002148 if (!lp->phy_dev || !lp->link || lp->duplex == DUPLEX_FULL)
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002149 txctl &= ~Tx_EnLCarr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 tc_writel(txctl, &tr->Tx_Ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002153#ifdef CONFIG_PM
2154static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2155{
2156 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002157 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002158 unsigned long flags;
2159
2160 pci_save_state(pdev);
2161 if (!netif_running(dev))
2162 return 0;
2163 netif_device_detach(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002164 if (lp->phy_dev)
2165 phy_stop(lp->phy_dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002166 spin_lock_irqsave(&lp->lock, flags);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002167 tc35815_chip_reset(dev);
2168 spin_unlock_irqrestore(&lp->lock, flags);
2169 pci_set_power_state(pdev, PCI_D3hot);
2170 return 0;
2171}
2172
2173static int tc35815_resume(struct pci_dev *pdev)
2174{
2175 struct net_device *dev = pci_get_drvdata(pdev);
Atsushi Nemotoee79b7f2008-04-11 00:24:36 +09002176 struct tc35815_local *lp = netdev_priv(dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002177
2178 pci_restore_state(pdev);
2179 if (!netif_running(dev))
2180 return 0;
2181 pci_set_power_state(pdev, PCI_D0);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002182 tc35815_restart(dev);
Atsushi Nemoto59524a32008-06-25 11:41:01 +09002183 netif_carrier_off(dev);
Atsushi Nemotoc6686fe2008-04-12 00:47:46 +09002184 if (lp->phy_dev)
2185 phy_start(lp->phy_dev);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002186 netif_device_attach(dev);
2187 return 0;
2188}
2189#endif /* CONFIG_PM */
2190
2191static struct pci_driver tc35815_pci_driver = {
2192 .name = MODNAME,
2193 .id_table = tc35815_pci_tbl,
2194 .probe = tc35815_init_one,
2195 .remove = __devexit_p(tc35815_remove_one),
2196#ifdef CONFIG_PM
2197 .suspend = tc35815_suspend,
2198 .resume = tc35815_resume,
2199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200};
2201
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002202module_param_named(speed, options.speed, int, 0);
2203MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2204module_param_named(duplex, options.duplex, int, 0);
2205MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static int __init tc35815_init_module(void)
2208{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002209 return pci_register_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212static void __exit tc35815_cleanup_module(void)
2213{
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002214 pci_unregister_driver(&tc35815_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
Jeff Garzik420e8522007-02-24 17:02:16 -05002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217module_init(tc35815_init_module);
2218module_exit(tc35815_cleanup_module);
Atsushi Nemotoeea221ce2007-03-03 23:54:59 +09002219
2220MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2221MODULE_LICENSE("GPL");