2 * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
4 * Based on skelton.c by Donald Becker.
6 * This driver is a replacement of older and less maintained version.
7 * This is a header of the older version:
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";
17 * 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
21 * (C) Copyright TOSHIBA CORPORATION 2004-2005
22 * All Rights Reserved.
26 #define DRV_VERSION "1.36-NAPI"
28 #define DRV_VERSION "1.36"
30 static const char *version = "tc35815.c:v" DRV_VERSION "\n";
31 #define MODNAME "tc35815"
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
38 #include <linux/ioport.h>
40 #include <linux/slab.h>
41 #include <linux/string.h>
42 #include <linux/spinlock.h>
43 #include <linux/errno.h>
44 #include <linux/init.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/delay.h>
49 #include <linux/pci.h>
50 #include <linux/mii.h>
51 #include <linux/ethtool.h>
52 #include <linux/platform_device.h>
54 #include <asm/byteorder.h>
56 /* First, a few definitions that the brave might change. */
58 #define GATHER_TXINT /* On-Demand Tx Interrupt */
59 #define WORKAROUND_LOSTCAR
60 #define WORKAROUND_100HALF_PROMISC
61 /* #define TC35815_USE_PACKEDBUFFER */
69 /* indexed by board_t, above */
72 } board_info[] __devinitdata = {
73 { "TOSHIBA TC35815CF 10/100BaseTX" },
74 { "TOSHIBA TC35815 with Wake on LAN" },
75 { "TOSHIBA TC35815/TX4939" },
78 static const struct pci_device_id tc35815_pci_tbl[] = {
79 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
80 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
81 {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
84 MODULE_DEVICE_TABLE (pci, tc35815_pci_tbl);
86 /* see MODULE_PARM_DESC */
87 static struct tc35815_options {
97 volatile __u32 DMA_Ctl; /* 0x00 */
98 volatile __u32 TxFrmPtr;
99 volatile __u32 TxThrsh;
100 volatile __u32 TxPollCtr;
101 volatile __u32 BLFrmPtr;
102 volatile __u32 RxFragSize;
103 volatile __u32 Int_En;
104 volatile __u32 FDA_Bas;
105 volatile __u32 FDA_Lim; /* 0x20 */
106 volatile __u32 Int_Src;
107 volatile __u32 unused0[2];
108 volatile __u32 PauseCnt;
109 volatile __u32 RemPauCnt;
110 volatile __u32 TxCtlFrmStat;
111 volatile __u32 unused1;
112 volatile __u32 MAC_Ctl; /* 0x40 */
113 volatile __u32 CAM_Ctl;
114 volatile __u32 Tx_Ctl;
115 volatile __u32 Tx_Stat;
116 volatile __u32 Rx_Ctl;
117 volatile __u32 Rx_Stat;
118 volatile __u32 MD_Data;
119 volatile __u32 MD_CA;
120 volatile __u32 CAM_Adr; /* 0x60 */
121 volatile __u32 CAM_Data;
122 volatile __u32 CAM_Ena;
123 volatile __u32 PROM_Ctl;
124 volatile __u32 PROM_Data;
125 volatile __u32 Algn_Cnt;
126 volatile __u32 CRC_Cnt;
127 volatile __u32 Miss_Cnt;
133 /* DMA_Ctl bit asign ------------------------------------------------------- */
134 #define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
135 #define DMA_RxAlign_1 0x00400000
136 #define DMA_RxAlign_2 0x00800000
137 #define DMA_RxAlign_3 0x00c00000
138 #define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
139 #define DMA_IntMask 0x00040000 /* 1:Interupt mask */
140 #define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
141 #define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
142 #define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
143 #define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
144 #define DMA_TestMode 0x00002000 /* 1:Test Mode */
145 #define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
146 #define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
148 /* RxFragSize bit asign ---------------------------------------------------- */
149 #define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
150 #define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
152 /* MAC_Ctl bit asign ------------------------------------------------------- */
153 #define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
154 #define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
155 #define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
156 #define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
157 #define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
158 #define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
159 #define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
160 #define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
161 #define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
162 #define MAC_Reset 0x00000004 /* 1:Software Reset */
163 #define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
164 #define MAC_HaltReq 0x00000001 /* 1:Halt request */
166 /* PROM_Ctl bit asign ------------------------------------------------------ */
167 #define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
168 #define PROM_Read 0x00004000 /*10:Read operation */
169 #define PROM_Write 0x00002000 /*01:Write operation */
170 #define PROM_Erase 0x00006000 /*11:Erase operation */
171 /*00:Enable or Disable Writting, */
172 /* as specified in PROM_Addr. */
173 #define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
176 /* CAM_Ctl bit asign ------------------------------------------------------- */
177 #define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
178 #define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
180 #define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
181 #define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
182 #define CAM_StationAcc 0x00000001 /* 1:unicast accept */
184 /* CAM_Ena bit asign ------------------------------------------------------- */
185 #define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
186 #define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
187 #define CAM_Ena_Bit(index) (1<<(index))
188 #define CAM_ENTRY_DESTINATION 0
189 #define CAM_ENTRY_SOURCE 1
190 #define CAM_ENTRY_MACCTL 20
192 /* Tx_Ctl bit asign -------------------------------------------------------- */
193 #define Tx_En 0x00000001 /* 1:Transmit enable */
194 #define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
195 #define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
196 #define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
197 #define Tx_FBack 0x00000010 /* 1:Fast Back-off */
198 #define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
199 #define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
200 #define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
201 #define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
202 #define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
203 #define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
204 #define Tx_EnComp 0x00004000 /* 1:Enable Completion */
206 /* Tx_Stat bit asign ------------------------------------------------------- */
207 #define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
208 #define Tx_ExColl 0x00000010 /* Excessive Collision */
209 #define Tx_TXDefer 0x00000020 /* Transmit Defered */
210 #define Tx_Paused 0x00000040 /* Transmit Paused */
211 #define Tx_IntTx 0x00000080 /* Interrupt on Tx */
212 #define Tx_Under 0x00000100 /* Underrun */
213 #define Tx_Defer 0x00000200 /* Deferral */
214 #define Tx_NCarr 0x00000400 /* No Carrier */
215 #define Tx_10Stat 0x00000800 /* 10Mbps Status */
216 #define Tx_LateColl 0x00001000 /* Late Collision */
217 #define Tx_TxPar 0x00002000 /* Tx Parity Error */
218 #define Tx_Comp 0x00004000 /* Completion */
219 #define Tx_Halted 0x00008000 /* Tx Halted */
220 #define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
222 /* Rx_Ctl bit asign -------------------------------------------------------- */
223 #define Rx_EnGood 0x00004000 /* 1:Enable Good */
224 #define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
225 #define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
226 #define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
227 #define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
228 #define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
229 #define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
230 #define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
231 #define Rx_ShortEn 0x00000008 /* 1:Short Enable */
232 #define Rx_LongEn 0x00000004 /* 1:Long Enable */
233 #define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
234 #define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
236 /* Rx_Stat bit asign ------------------------------------------------------- */
237 #define Rx_Halted 0x00008000 /* Rx Halted */
238 #define Rx_Good 0x00004000 /* Rx Good */
239 #define Rx_RxPar 0x00002000 /* Rx Parity Error */
240 /* 0x00001000 not use */
241 #define Rx_LongErr 0x00000800 /* Rx Long Error */
242 #define Rx_Over 0x00000400 /* Rx Overflow */
243 #define Rx_CRCErr 0x00000200 /* Rx CRC Error */
244 #define Rx_Align 0x00000100 /* Rx Alignment Error */
245 #define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
246 #define Rx_IntRx 0x00000040 /* Rx Interrupt */
247 #define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
249 #define Rx_Stat_Mask 0x0000EFC0 /* Rx All Status Mask */
251 /* Int_En bit asign -------------------------------------------------------- */
252 #define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
253 #define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Control Complete Enable */
254 #define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
255 #define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
256 #define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
257 #define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
258 #define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
259 #define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
260 #define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
261 #define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
262 #define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
263 #define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
264 /* Exhausted Enable */
266 /* Int_Src bit asign ------------------------------------------------------- */
267 #define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
268 #define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
269 #define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
270 #define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
271 #define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
272 #define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
273 #define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
274 #define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
275 #define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
276 #define Int_SWInt 0x00000020 /* 1:Software request & Clear */
277 #define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
278 #define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
279 #define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
280 #define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
281 #define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
283 /* MD_CA bit asign --------------------------------------------------------- */
284 #define MD_CA_PreSup 0x00001000 /* 1:Preamble Supress */
285 #define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
286 #define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
293 /* Frame descripter */
295 volatile __u32 FDNext;
296 volatile __u32 FDSystem;
297 volatile __u32 FDStat;
298 volatile __u32 FDCtl;
301 /* Buffer descripter */
303 volatile __u32 BuffData;
304 volatile __u32 BDCtl;
309 /* Frame Descripter bit asign ---------------------------------------------- */
310 #define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
311 #define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
312 #define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
313 #define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
314 #define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
315 #define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
316 #define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
317 #define FD_FrmOpt_Packing 0x04000000 /* Rx only */
318 #define FD_CownsFD 0x80000000 /* FD Controller owner bit */
319 #define FD_Next_EOL 0x00000001 /* FD EOL indicator */
320 #define FD_BDCnt_SHIFT 16
322 /* Buffer Descripter bit asign --------------------------------------------- */
323 #define BD_BuffLength_MASK 0x0000FFFF /* Recieve Data Size */
324 #define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
325 #define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
326 #define BD_CownsBD 0x80000000 /* BD Controller owner bit */
327 #define BD_RxBDID_SHIFT 16
328 #define BD_RxBDSeqN_SHIFT 24
331 /* Some useful constants. */
332 #undef NO_CHECK_CARRIER /* Does not check No-Carrier with TP */
334 #ifdef NO_CHECK_CARRIER
335 #define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
336 Tx_EnExColl | Tx_EnExDefer | Tx_EnUnder | \
337 Tx_En) /* maybe 0x7b01 */
339 #define TX_CTL_CMD (Tx_EnComp | Tx_EnTxPar | Tx_EnLateColl | \
340 Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
341 Tx_En) /* maybe 0x7b01 */
343 #define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
344 | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
345 #define INT_EN_CMD (Int_NRAbtEn | \
346 Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
347 Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
349 Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
350 #define DMA_CTL_CMD DMA_BURST_SIZE
351 #define HAVE_DMA_RXALIGN(lp) likely((lp)->boardtype != TC35815CF)
353 /* Tuning parameters */
354 #define DMA_BURST_SIZE 32
355 #define TX_THRESHOLD 1024
356 #define TX_THRESHOLD_MAX 1536 /* used threshold with packet max byte for low pci transfer ability.*/
357 #define TX_THRESHOLD_KEEP_LIMIT 10 /* setting threshold max value when overrun error occured this count. */
359 /* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
360 #ifdef TC35815_USE_PACKEDBUFFER
361 #define FD_PAGE_NUM 2
362 #define RX_BUF_NUM 8 /* >= 2 */
363 #define RX_FD_NUM 250 /* >= 32 */
364 #define TX_FD_NUM 128
365 #define RX_BUF_SIZE PAGE_SIZE
366 #else /* TC35815_USE_PACKEDBUFFER */
367 #define FD_PAGE_NUM 4
368 #define RX_BUF_NUM 128 /* < 256 */
369 #define RX_FD_NUM 256 /* >= 32 */
370 #define TX_FD_NUM 128
371 #if RX_CTL_CMD & Rx_LongEn
372 #define RX_BUF_SIZE PAGE_SIZE
373 #elif RX_CTL_CMD & Rx_StripCRC
374 #define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 4 + 2, 32) /* +2: reserve */
376 #define RX_BUF_SIZE ALIGN(ETH_FRAME_LEN + 2, 32) /* +2: reserve */
378 #endif /* TC35815_USE_PACKEDBUFFER */
379 #define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
380 #define NAPI_WEIGHT 16
390 struct BDesc bd[0]; /* variable length */
395 struct BDesc bd[RX_BUF_NUM];
399 #define tc_readl(addr) readl(addr)
400 #define tc_writel(d, addr) writel(d, addr)
402 #define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
404 /* Timer state engine. */
405 enum tc35815_timer_state {
406 arbwait = 0, /* Waiting for auto negotiation to complete. */
407 lupwait = 1, /* Auto-neg complete, awaiting link-up status. */
408 ltrywait = 2, /* Forcing try of all modes, from fastest to slowest. */
409 asleep = 3, /* Time inactive. */
410 lcheck = 4, /* Check link status. */
413 /* Information that need to be kept for each board. */
414 struct tc35815_local {
415 struct pci_dev *pci_dev;
417 struct net_device *dev;
418 struct napi_struct napi;
428 /* Tx control lock. This protects the transmit buffer ring
429 * state along with the "tx full" state of the driver. This
430 * means all netif_queue flow control actions are protected
431 * by this lock as well.
437 unsigned short saved_lpa;
438 struct timer_list timer;
439 enum tc35815_timer_state timer_state; /* State of auto-neg timer. */
440 unsigned int timer_ticks; /* Number of clicks at each state */
443 * Transmitting: Batch Mode.
445 * Receiving: Packing Mode. (TC35815_USE_PACKEDBUFFER)
446 * 1 circular FD for Free Buffer List.
447 * RX_BUF_NUM BD in Free Buffer FD.
448 * One Free Buffer BD has PAGE_SIZE data buffer.
449 * Or Non-Packing Mode.
450 * 1 circular FD for Free Buffer List.
451 * RX_BUF_NUM BD in Free Buffer FD.
452 * One Free Buffer BD has ETH_FRAME_LEN data buffer.
454 void * fd_buf; /* for TxFD, RxFD, FrFD */
455 dma_addr_t fd_buf_dma;
456 struct TxFD *tfd_base;
457 unsigned int tfd_start;
458 unsigned int tfd_end;
459 struct RxFD *rfd_base;
460 struct RxFD *rfd_limit;
461 struct RxFD *rfd_cur;
462 struct FrFD *fbl_ptr;
463 #ifdef TC35815_USE_PACKEDBUFFER
464 unsigned char fbl_curid;
465 void * data_buf[RX_BUF_NUM]; /* packing */
466 dma_addr_t data_buf_dma[RX_BUF_NUM];
470 } tx_skbs[TX_FD_NUM];
472 unsigned int fbl_count;
476 } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
478 struct mii_if_info mii;
479 unsigned short mii_id[2];
484 static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
486 return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
489 static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
491 return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
494 #ifdef TC35815_USE_PACKEDBUFFER
495 static inline void *rxbuf_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
498 for (i = 0; i < RX_BUF_NUM; i++) {
499 if (bus >= lp->data_buf_dma[i] &&
500 bus < lp->data_buf_dma[i] + PAGE_SIZE)
501 return (void *)((u8 *)lp->data_buf[i] +
502 (bus - lp->data_buf_dma[i]));
507 #define TC35815_DMA_SYNC_ONDEMAND
508 static void* alloc_rxbuf_page(struct pci_dev *hwdev, dma_addr_t *dma_handle)
510 #ifdef TC35815_DMA_SYNC_ONDEMAND
512 /* pci_map + pci_dma_sync will be more effective than
513 * pci_alloc_consistent on some archs. */
514 if ((buf = (void *)__get_free_page(GFP_ATOMIC)) == NULL)
516 *dma_handle = pci_map_single(hwdev, buf, PAGE_SIZE,
518 if (pci_dma_mapping_error(*dma_handle)) {
519 free_page((unsigned long)buf);
524 return pci_alloc_consistent(hwdev, PAGE_SIZE, dma_handle);
528 static void free_rxbuf_page(struct pci_dev *hwdev, void *buf, dma_addr_t dma_handle)
530 #ifdef TC35815_DMA_SYNC_ONDEMAND
531 pci_unmap_single(hwdev, dma_handle, PAGE_SIZE, PCI_DMA_FROMDEVICE);
532 free_page((unsigned long)buf);
534 pci_free_consistent(hwdev, PAGE_SIZE, buf, dma_handle);
537 #else /* TC35815_USE_PACKEDBUFFER */
538 static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
539 struct pci_dev *hwdev,
540 dma_addr_t *dma_handle)
543 skb = dev_alloc_skb(RX_BUF_SIZE);
546 *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
548 if (pci_dma_mapping_error(*dma_handle)) {
549 dev_kfree_skb_any(skb);
552 skb_reserve(skb, 2); /* make IP header 4byte aligned */
556 static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
558 pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
560 dev_kfree_skb_any(skb);
562 #endif /* TC35815_USE_PACKEDBUFFER */
564 /* Index to functions, as function prototypes. */
566 static int tc35815_open(struct net_device *dev);
567 static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
568 static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
570 static int tc35815_rx(struct net_device *dev, int limit);
571 static int tc35815_poll(struct napi_struct *napi, int budget);
573 static void tc35815_rx(struct net_device *dev);
575 static void tc35815_txdone(struct net_device *dev);
576 static int tc35815_close(struct net_device *dev);
577 static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
578 static void tc35815_set_multicast_list(struct net_device *dev);
579 static void tc35815_tx_timeout(struct net_device *dev);
580 static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
581 #ifdef CONFIG_NET_POLL_CONTROLLER
582 static void tc35815_poll_controller(struct net_device *dev);
584 static const struct ethtool_ops tc35815_ethtool_ops;
586 /* Example routines you must write ;->. */
587 static void tc35815_chip_reset(struct net_device *dev);
588 static void tc35815_chip_init(struct net_device *dev);
589 static void tc35815_find_phy(struct net_device *dev);
590 static void tc35815_phy_chip_init(struct net_device *dev);
593 static void panic_queues(struct net_device *dev);
596 static void tc35815_timer(unsigned long data);
597 static void tc35815_start_auto_negotiation(struct net_device *dev,
598 struct ethtool_cmd *ep);
599 static int tc_mdio_read(struct net_device *dev, int phy_id, int location);
600 static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
603 #ifdef CONFIG_CPU_TX49XX
605 * Find a platform_device providing a MAC address. The platform code
606 * should provide a "tc35815-mac" device with a MAC address in its
609 static int __devinit tc35815_mac_match(struct device *dev, void *data)
611 struct platform_device *plat_dev = to_platform_device(dev);
612 struct pci_dev *pci_dev = data;
613 unsigned int id = pci_dev->irq;
614 return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
617 static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
619 struct tc35815_local *lp = dev->priv;
620 struct device *pd = bus_find_device(&platform_bus_type, NULL,
621 lp->pci_dev, tc35815_mac_match);
623 if (pd->platform_data)
624 memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
626 return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
631 static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
637 static int __devinit tc35815_init_dev_addr (struct net_device *dev)
639 struct tc35815_regs __iomem *tr =
640 (struct tc35815_regs __iomem *)dev->base_addr;
643 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
645 for (i = 0; i < 6; i += 2) {
647 tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
648 while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
650 data = tc_readl(&tr->PROM_Data);
651 dev->dev_addr[i] = data & 0xff;
652 dev->dev_addr[i+1] = data >> 8;
654 if (!is_valid_ether_addr(dev->dev_addr))
655 return tc35815_read_plat_dev_addr(dev);
659 static int __devinit tc35815_init_one (struct pci_dev *pdev,
660 const struct pci_device_id *ent)
662 void __iomem *ioaddr = NULL;
663 struct net_device *dev;
664 struct tc35815_local *lp;
666 unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
667 DECLARE_MAC_BUF(mac);
669 static int printed_version;
670 if (!printed_version++) {
672 dev_printk(KERN_DEBUG, &pdev->dev,
673 "speed:%d duplex:%d doforce:%d\n",
674 options.speed, options.duplex, options.doforce);
678 dev_warn(&pdev->dev, "no IRQ assigned.\n");
682 /* dev zeroed in alloc_etherdev */
683 dev = alloc_etherdev (sizeof (*lp));
685 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
688 SET_NETDEV_DEV(dev, &pdev->dev);
692 /* enable device (incl. PCI PM wakeup), and bus-mastering */
693 rc = pci_enable_device (pdev);
697 mmio_start = pci_resource_start (pdev, 1);
698 mmio_end = pci_resource_end (pdev, 1);
699 mmio_flags = pci_resource_flags (pdev, 1);
700 mmio_len = pci_resource_len (pdev, 1);
702 /* set this immediately, we need to know before
703 * we talk to the chip directly */
705 /* make sure PCI base addr 1 is MMIO */
706 if (!(mmio_flags & IORESOURCE_MEM)) {
707 dev_err(&pdev->dev, "region #1 not an MMIO resource, aborting\n");
712 /* check for weird/broken PCI region reporting */
713 if ((mmio_len < sizeof(struct tc35815_regs))) {
714 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
719 rc = pci_request_regions (pdev, MODNAME);
723 pci_set_master (pdev);
725 /* ioremap MMIO region */
726 ioaddr = ioremap (mmio_start, mmio_len);
727 if (ioaddr == NULL) {
728 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
730 goto err_out_free_res;
733 /* Initialize the device structure. */
734 dev->open = tc35815_open;
735 dev->hard_start_xmit = tc35815_send_packet;
736 dev->stop = tc35815_close;
737 dev->get_stats = tc35815_get_stats;
738 dev->set_multicast_list = tc35815_set_multicast_list;
739 dev->do_ioctl = tc35815_ioctl;
740 dev->ethtool_ops = &tc35815_ethtool_ops;
741 dev->tx_timeout = tc35815_tx_timeout;
742 dev->watchdog_timeo = TC35815_TX_TIMEOUT;
744 netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
746 #ifdef CONFIG_NET_POLL_CONTROLLER
747 dev->poll_controller = tc35815_poll_controller;
750 dev->irq = pdev->irq;
751 dev->base_addr = (unsigned long) ioaddr;
753 spin_lock_init(&lp->lock);
755 lp->boardtype = ent->driver_data;
757 lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
758 pci_set_drvdata(pdev, dev);
760 /* Soft reset the chip. */
761 tc35815_chip_reset(dev);
763 /* Retrieve the ethernet address. */
764 if (tc35815_init_dev_addr(dev)) {
765 dev_warn(&pdev->dev, "not valid ether addr\n");
766 random_ether_addr(dev->dev_addr);
769 rc = register_netdev (dev);
773 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
774 printk(KERN_INFO "%s: %s at 0x%lx, %s, IRQ %d\n",
776 board_info[ent->driver_data].name,
778 print_mac(mac, dev->dev_addr),
781 setup_timer(&lp->timer, tc35815_timer, (unsigned long) dev);
783 lp->mii.mdio_read = tc_mdio_read;
784 lp->mii.mdio_write = tc_mdio_write;
785 lp->mii.phy_id_mask = 0x1f;
786 lp->mii.reg_num_mask = 0x1f;
787 tc35815_find_phy(dev);
788 lp->mii.phy_id = lp->phy_addr;
789 lp->mii.full_duplex = 0;
790 lp->mii.force_media = 0;
797 pci_release_regions (pdev);
804 static void __devexit tc35815_remove_one (struct pci_dev *pdev)
806 struct net_device *dev = pci_get_drvdata (pdev);
807 unsigned long mmio_addr;
809 mmio_addr = dev->base_addr;
811 unregister_netdev (dev);
814 iounmap ((void __iomem *)mmio_addr);
815 pci_release_regions (pdev);
820 pci_set_drvdata (pdev, NULL);
824 tc35815_init_queues(struct net_device *dev)
826 struct tc35815_local *lp = dev->priv;
828 unsigned long fd_addr;
831 BUG_ON(sizeof(struct FDesc) +
832 sizeof(struct BDesc) * RX_BUF_NUM +
833 sizeof(struct FDesc) * RX_FD_NUM +
834 sizeof(struct TxFD) * TX_FD_NUM >
835 PAGE_SIZE * FD_PAGE_NUM);
837 if ((lp->fd_buf = pci_alloc_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM, &lp->fd_buf_dma)) == 0)
839 for (i = 0; i < RX_BUF_NUM; i++) {
840 #ifdef TC35815_USE_PACKEDBUFFER
841 if ((lp->data_buf[i] = alloc_rxbuf_page(lp->pci_dev, &lp->data_buf_dma[i])) == NULL) {
843 free_rxbuf_page(lp->pci_dev,
845 lp->data_buf_dma[i]);
846 lp->data_buf[i] = NULL;
848 pci_free_consistent(lp->pci_dev,
849 PAGE_SIZE * FD_PAGE_NUM,
857 alloc_rxbuf_skb(dev, lp->pci_dev,
858 &lp->rx_skbs[i].skb_dma);
859 if (!lp->rx_skbs[i].skb) {
861 free_rxbuf_skb(lp->pci_dev,
863 lp->rx_skbs[i].skb_dma);
864 lp->rx_skbs[i].skb = NULL;
866 pci_free_consistent(lp->pci_dev,
867 PAGE_SIZE * FD_PAGE_NUM,
875 printk(KERN_DEBUG "%s: FD buf %p DataBuf",
876 dev->name, lp->fd_buf);
877 #ifdef TC35815_USE_PACKEDBUFFER
879 for (i = 0; i < RX_BUF_NUM; i++)
880 printk(" %p", lp->data_buf[i]);
884 for (i = 0; i < FD_PAGE_NUM; i++) {
885 clear_page((void *)((unsigned long)lp->fd_buf + i * PAGE_SIZE));
888 fd_addr = (unsigned long)lp->fd_buf;
890 /* Free Descriptors (for Receive) */
891 lp->rfd_base = (struct RxFD *)fd_addr;
892 fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
893 for (i = 0; i < RX_FD_NUM; i++) {
894 lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
896 lp->rfd_cur = lp->rfd_base;
897 lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
899 /* Transmit Descriptors */
900 lp->tfd_base = (struct TxFD *)fd_addr;
901 fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
902 for (i = 0; i < TX_FD_NUM; i++) {
903 lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
904 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
905 lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
907 lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0]));
911 /* Buffer List (for Receive) */
912 lp->fbl_ptr = (struct FrFD *)fd_addr;
913 lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
914 lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
915 #ifndef TC35815_USE_PACKEDBUFFER
917 * move all allocated skbs to head of rx_skbs[] array.
918 * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
919 * tc35815_rx() had failed.
922 for (i = 0; i < RX_BUF_NUM; i++) {
923 if (lp->rx_skbs[i].skb) {
924 if (i != lp->fbl_count) {
925 lp->rx_skbs[lp->fbl_count].skb =
927 lp->rx_skbs[lp->fbl_count].skb_dma =
928 lp->rx_skbs[i].skb_dma;
934 for (i = 0; i < RX_BUF_NUM; i++) {
935 #ifdef TC35815_USE_PACKEDBUFFER
936 lp->fbl_ptr->bd[i].BuffData = cpu_to_le32(lp->data_buf_dma[i]);
938 if (i >= lp->fbl_count) {
939 lp->fbl_ptr->bd[i].BuffData = 0;
940 lp->fbl_ptr->bd[i].BDCtl = 0;
943 lp->fbl_ptr->bd[i].BuffData =
944 cpu_to_le32(lp->rx_skbs[i].skb_dma);
946 /* BDID is index of FrFD.bd[] */
947 lp->fbl_ptr->bd[i].BDCtl =
948 cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
951 #ifdef TC35815_USE_PACKEDBUFFER
955 printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
956 dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
961 tc35815_clear_queues(struct net_device *dev)
963 struct tc35815_local *lp = dev->priv;
966 for (i = 0; i < TX_FD_NUM; i++) {
967 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
968 struct sk_buff *skb =
969 fdsystem != 0xffffffff ?
970 lp->tx_skbs[fdsystem].skb : NULL;
972 if (lp->tx_skbs[i].skb != skb) {
973 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
977 BUG_ON(lp->tx_skbs[i].skb != skb);
980 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
981 lp->tx_skbs[i].skb = NULL;
982 lp->tx_skbs[i].skb_dma = 0;
983 dev_kfree_skb_any(skb);
985 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
988 tc35815_init_queues(dev);
992 tc35815_free_queues(struct net_device *dev)
994 struct tc35815_local *lp = dev->priv;
998 for (i = 0; i < TX_FD_NUM; i++) {
999 u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
1000 struct sk_buff *skb =
1001 fdsystem != 0xffffffff ?
1002 lp->tx_skbs[fdsystem].skb : NULL;
1004 if (lp->tx_skbs[i].skb != skb) {
1005 printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
1009 BUG_ON(lp->tx_skbs[i].skb != skb);
1013 pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
1014 lp->tx_skbs[i].skb = NULL;
1015 lp->tx_skbs[i].skb_dma = 0;
1017 lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
1021 lp->rfd_base = NULL;
1022 lp->rfd_limit = NULL;
1026 for (i = 0; i < RX_BUF_NUM; i++) {
1027 #ifdef TC35815_USE_PACKEDBUFFER
1028 if (lp->data_buf[i]) {
1029 free_rxbuf_page(lp->pci_dev,
1030 lp->data_buf[i], lp->data_buf_dma[i]);
1031 lp->data_buf[i] = NULL;
1034 if (lp->rx_skbs[i].skb) {
1035 free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
1036 lp->rx_skbs[i].skb_dma);
1037 lp->rx_skbs[i].skb = NULL;
1042 pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
1043 lp->fd_buf, lp->fd_buf_dma);
1049 dump_txfd(struct TxFD *fd)
1051 printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
1052 le32_to_cpu(fd->fd.FDNext),
1053 le32_to_cpu(fd->fd.FDSystem),
1054 le32_to_cpu(fd->fd.FDStat),
1055 le32_to_cpu(fd->fd.FDCtl));
1057 printk(" %08x %08x",
1058 le32_to_cpu(fd->bd.BuffData),
1059 le32_to_cpu(fd->bd.BDCtl));
1064 dump_rxfd(struct RxFD *fd)
1066 int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1069 printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
1070 le32_to_cpu(fd->fd.FDNext),
1071 le32_to_cpu(fd->fd.FDSystem),
1072 le32_to_cpu(fd->fd.FDStat),
1073 le32_to_cpu(fd->fd.FDCtl));
1074 if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
1077 for (i = 0; i < bd_count; i++)
1078 printk(" %08x %08x",
1079 le32_to_cpu(fd->bd[i].BuffData),
1080 le32_to_cpu(fd->bd[i].BDCtl));
1085 #if defined(DEBUG) || defined(TC35815_USE_PACKEDBUFFER)
1087 dump_frfd(struct FrFD *fd)
1090 printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
1091 le32_to_cpu(fd->fd.FDNext),
1092 le32_to_cpu(fd->fd.FDSystem),
1093 le32_to_cpu(fd->fd.FDStat),
1094 le32_to_cpu(fd->fd.FDCtl));
1096 for (i = 0; i < RX_BUF_NUM; i++)
1097 printk(" %08x %08x",
1098 le32_to_cpu(fd->bd[i].BuffData),
1099 le32_to_cpu(fd->bd[i].BDCtl));
1106 panic_queues(struct net_device *dev)
1108 struct tc35815_local *lp = dev->priv;
1111 printk("TxFD base %p, start %u, end %u\n",
1112 lp->tfd_base, lp->tfd_start, lp->tfd_end);
1113 printk("RxFD base %p limit %p cur %p\n",
1114 lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
1115 printk("FrFD %p\n", lp->fbl_ptr);
1116 for (i = 0; i < TX_FD_NUM; i++)
1117 dump_txfd(&lp->tfd_base[i]);
1118 for (i = 0; i < RX_FD_NUM; i++) {
1119 int bd_count = dump_rxfd(&lp->rfd_base[i]);
1120 i += (bd_count + 1) / 2; /* skip BDs */
1122 dump_frfd(lp->fbl_ptr);
1123 panic("%s: Illegal queue state.", dev->name);
1127 static void print_eth(const u8 *add)
1129 DECLARE_MAC_BUF(mac);
1131 printk(KERN_DEBUG "print_eth(%p)\n", add);
1132 printk(KERN_DEBUG " %s =>", print_mac(mac, add + 6));
1133 printk(KERN_CONT " %s : %02x%02x\n",
1134 print_mac(mac, add), add[12], add[13]);
1137 static int tc35815_tx_full(struct net_device *dev)
1139 struct tc35815_local *lp = dev->priv;
1140 return ((lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end);
1143 static void tc35815_restart(struct net_device *dev)
1145 struct tc35815_local *lp = dev->priv;
1146 int pid = lp->phy_addr;
1147 int do_phy_reset = 1;
1148 del_timer(&lp->timer); /* Kill if running */
1150 if (lp->mii_id[0] == 0x0016 && (lp->mii_id[1] & 0xfc00) == 0xf800) {
1151 /* Resetting PHY cause problem on some chip... (SEEQ 80221) */
1156 tc_mdio_write(dev, pid, MII_BMCR, BMCR_RESET);
1159 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_RESET))
1164 printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
1167 tc35815_chip_reset(dev);
1168 tc35815_clear_queues(dev);
1169 tc35815_chip_init(dev);
1170 /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
1171 tc35815_set_multicast_list(dev);
1174 static void tc35815_tx_timeout(struct net_device *dev)
1176 struct tc35815_local *lp = dev->priv;
1177 struct tc35815_regs __iomem *tr =
1178 (struct tc35815_regs __iomem *)dev->base_addr;
1180 printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
1181 dev->name, tc_readl(&tr->Tx_Stat));
1183 /* Try to restart the adaptor. */
1184 spin_lock_irq(&lp->lock);
1185 tc35815_restart(dev);
1186 spin_unlock_irq(&lp->lock);
1188 dev->stats.tx_errors++;
1190 /* If we have space available to accept new transmit
1191 * requests, wake up the queueing layer. This would
1192 * be the case if the chipset_init() call above just
1193 * flushes out the tx queue and empties it.
1195 * If instead, the tx queue is retained then the
1196 * netif_wake_queue() call should be placed in the
1197 * TX completion interrupt handler of the driver instead
1200 if (!tc35815_tx_full(dev))
1201 netif_wake_queue(dev);
1205 * Open/initialize the board. This is called (in the current kernel)
1206 * sometime after booting when the 'ifconfig' program is run.
1208 * This routine should set everything up anew at each open, even
1209 * registers that "should" only need to be set once at boot, so that
1210 * there is non-reboot way to recover if something goes wrong.
1213 tc35815_open(struct net_device *dev)
1215 struct tc35815_local *lp = dev->priv;
1218 * This is used if the interrupt line can turned off (shared).
1219 * See 3c503.c for an example of selecting the IRQ at config-time.
1221 if (request_irq(dev->irq, &tc35815_interrupt, IRQF_SHARED, dev->name, dev)) {
1225 del_timer(&lp->timer); /* Kill if running */
1226 tc35815_chip_reset(dev);
1228 if (tc35815_init_queues(dev) != 0) {
1229 free_irq(dev->irq, dev);
1234 napi_enable(&lp->napi);
1237 /* Reset the hardware here. Don't forget to set the station address. */
1238 spin_lock_irq(&lp->lock);
1239 tc35815_chip_init(dev);
1240 spin_unlock_irq(&lp->lock);
1242 /* We are now ready to accept transmit requeusts from
1243 * the queueing layer of the networking.
1245 netif_start_queue(dev);
1250 /* This will only be invoked if your driver is _not_ in XOFF state.
1251 * What this means is that you need not check it, and that this
1252 * invariant will hold if you make sure that the netif_*_queue()
1253 * calls are done at the proper times.
1255 static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
1257 struct tc35815_local *lp = dev->priv;
1259 unsigned long flags;
1261 /* If some error occurs while trying to transmit this
1262 * packet, you should return '1' from this function.
1263 * In such a case you _may not_ do anything to the
1264 * SKB, it is still owned by the network queueing
1265 * layer when an error is returned. This means you
1266 * may not modify any SKB fields, you may not free
1270 /* This is the most common case for modern hardware.
1271 * The spinlock protects this code from the TX complete
1272 * hardware interrupt handler. Queue flow control is
1273 * thus managed under this lock as well.
1275 spin_lock_irqsave(&lp->lock, flags);
1277 /* failsafe... (handle txdone now if half of FDs are used) */
1278 if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
1280 tc35815_txdone(dev);
1282 if (netif_msg_pktdata(lp))
1283 print_eth(skb->data);
1285 if (lp->tx_skbs[lp->tfd_start].skb) {
1286 printk("%s: tx_skbs conflict.\n", dev->name);
1290 BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
1292 lp->tx_skbs[lp->tfd_start].skb = skb;
1293 lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
1296 txfd = &lp->tfd_base[lp->tfd_start];
1297 txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
1298 txfd->bd.BDCtl = cpu_to_le32(skb->len);
1299 txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
1300 txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
1302 if (lp->tfd_start == lp->tfd_end) {
1303 struct tc35815_regs __iomem *tr =
1304 (struct tc35815_regs __iomem *)dev->base_addr;
1305 /* Start DMA Transmitter. */
1306 txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
1308 txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
1310 if (netif_msg_tx_queued(lp)) {
1311 printk("%s: starting TxFD.\n", dev->name);
1314 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1316 txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
1317 if (netif_msg_tx_queued(lp)) {
1318 printk("%s: queueing TxFD.\n", dev->name);
1322 lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
1324 dev->trans_start = jiffies;
1326 /* If we just used up the very last entry in the
1327 * TX ring on this device, tell the queueing
1328 * layer to send no more.
1330 if (tc35815_tx_full(dev)) {
1331 if (netif_msg_tx_queued(lp))
1332 printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
1333 netif_stop_queue(dev);
1336 /* When the TX completion hw interrupt arrives, this
1337 * is when the transmit statistics are updated.
1340 spin_unlock_irqrestore(&lp->lock, flags);
1344 #define FATAL_ERROR_INT \
1345 (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
1346 static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
1349 printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
1351 if (status & Int_IntPCI)
1353 if (status & Int_DmParErr)
1354 printk(" DmParErr");
1355 if (status & Int_IntNRAbt)
1356 printk(" IntNRAbt");
1359 panic("%s: Too many fatal errors.", dev->name);
1360 printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
1361 /* Try to restart the adaptor. */
1362 tc35815_restart(dev);
1366 static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
1368 static int tc35815_do_interrupt(struct net_device *dev, u32 status)
1371 struct tc35815_local *lp = dev->priv;
1372 struct tc35815_regs __iomem *tr =
1373 (struct tc35815_regs __iomem *)dev->base_addr;
1376 /* Fatal errors... */
1377 if (status & FATAL_ERROR_INT) {
1378 tc35815_fatal_error_interrupt(dev, status);
1381 /* recoverable errors */
1382 if (status & Int_IntFDAEx) {
1383 /* disable FDAEx int. (until we make rooms...) */
1384 tc_writel(tc_readl(&tr->Int_En) & ~Int_FDAExEn, &tr->Int_En);
1386 "%s: Free Descriptor Area Exhausted (%#x).\n",
1388 dev->stats.rx_dropped++;
1391 if (status & Int_IntBLEx) {
1392 /* disable BLEx int. (until we make rooms...) */
1393 tc_writel(tc_readl(&tr->Int_En) & ~Int_BLExEn, &tr->Int_En);
1395 "%s: Buffer List Exhausted (%#x).\n",
1397 dev->stats.rx_dropped++;
1400 if (status & Int_IntExBD) {
1402 "%s: Excessive Buffer Descriptiors (%#x).\n",
1404 dev->stats.rx_length_errors++;
1408 /* normal notification */
1409 if (status & Int_IntMacRx) {
1410 /* Got a packet(s). */
1412 ret = tc35815_rx(dev, limit);
1417 lp->lstats.rx_ints++;
1419 if (status & Int_IntMacTx) {
1420 /* Transmit complete. */
1421 lp->lstats.tx_ints++;
1422 tc35815_txdone(dev);
1423 netif_wake_queue(dev);
1430 * The typical workload of the driver:
1431 * Handle the network interface interrupts.
1433 static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
1435 struct net_device *dev = dev_id;
1436 struct tc35815_local *lp = netdev_priv(dev);
1437 struct tc35815_regs __iomem *tr =
1438 (struct tc35815_regs __iomem *)dev->base_addr;
1440 u32 dmactl = tc_readl(&tr->DMA_Ctl);
1442 if (!(dmactl & DMA_IntMask)) {
1443 /* disable interrupts */
1444 tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
1445 if (netif_rx_schedule_prep(dev, &lp->napi))
1446 __netif_rx_schedule(dev, &lp->napi);
1448 printk(KERN_ERR "%s: interrupt taken in poll\n",
1452 (void)tc_readl(&tr->Int_Src); /* flush */
1460 spin_lock(&lp->lock);
1461 status = tc_readl(&tr->Int_Src);
1462 tc_writel(status, &tr->Int_Src); /* write to clear */
1463 handled = tc35815_do_interrupt(dev, status);
1464 (void)tc_readl(&tr->Int_Src); /* flush */
1465 spin_unlock(&lp->lock);
1466 return IRQ_RETVAL(handled >= 0);
1467 #endif /* TC35815_NAPI */
1470 #ifdef CONFIG_NET_POLL_CONTROLLER
1471 static void tc35815_poll_controller(struct net_device *dev)
1473 disable_irq(dev->irq);
1474 tc35815_interrupt(dev->irq, dev);
1475 enable_irq(dev->irq);
1479 /* We have a good packet(s), get it/them out of the buffers. */
1482 tc35815_rx(struct net_device *dev, int limit)
1485 tc35815_rx(struct net_device *dev)
1488 struct tc35815_local *lp = dev->priv;
1491 int buf_free_count = 0;
1492 int fd_free_count = 0;
1497 while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
1498 int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
1499 int pkt_len = fdctl & FD_FDLength_MASK;
1500 int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
1502 struct RxFD *next_rfd;
1504 #if (RX_CTL_CMD & Rx_StripCRC) == 0
1508 if (netif_msg_rx_status(lp))
1509 dump_rxfd(lp->rfd_cur);
1510 if (status & Rx_Good) {
1511 struct sk_buff *skb;
1512 unsigned char *data;
1514 #ifdef TC35815_USE_PACKEDBUFFER
1522 #ifdef TC35815_USE_PACKEDBUFFER
1523 BUG_ON(bd_count > 2);
1524 skb = dev_alloc_skb(pkt_len + 2); /* +2: for reserve */
1526 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
1528 dev->stats.rx_dropped++;
1531 skb_reserve(skb, 2); /* 16 bit alignment */
1533 data = skb_put(skb, pkt_len);
1535 /* copy from receive buffer */
1538 while (offset < pkt_len && cur_bd < bd_count) {
1539 int len = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BDCtl) &
1541 dma_addr_t dma = le32_to_cpu(lp->rfd_cur->bd[cur_bd].BuffData);
1542 void *rxbuf = rxbuf_bus_to_virt(lp, dma);
1543 if (offset + len > pkt_len)
1544 len = pkt_len - offset;
1545 #ifdef TC35815_DMA_SYNC_ONDEMAND
1546 pci_dma_sync_single_for_cpu(lp->pci_dev,
1548 PCI_DMA_FROMDEVICE);
1550 memcpy(data + offset, rxbuf, len);
1551 #ifdef TC35815_DMA_SYNC_ONDEMAND
1552 pci_dma_sync_single_for_device(lp->pci_dev,
1554 PCI_DMA_FROMDEVICE);
1559 #else /* TC35815_USE_PACKEDBUFFER */
1560 BUG_ON(bd_count > 1);
1561 cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
1562 & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1564 if (cur_bd >= RX_BUF_NUM) {
1565 printk("%s: invalid BDID.\n", dev->name);
1568 BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
1569 (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
1570 if (!lp->rx_skbs[cur_bd].skb) {
1571 printk("%s: NULL skb.\n", dev->name);
1575 BUG_ON(cur_bd >= RX_BUF_NUM);
1577 skb = lp->rx_skbs[cur_bd].skb;
1578 prefetch(skb->data);
1579 lp->rx_skbs[cur_bd].skb = NULL;
1581 pci_unmap_single(lp->pci_dev,
1582 lp->rx_skbs[cur_bd].skb_dma,
1583 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1584 if (!HAVE_DMA_RXALIGN(lp))
1585 memmove(skb->data, skb->data - 2, pkt_len);
1586 data = skb_put(skb, pkt_len);
1587 #endif /* TC35815_USE_PACKEDBUFFER */
1588 if (netif_msg_pktdata(lp))
1590 skb->protocol = eth_type_trans(skb, dev);
1592 netif_receive_skb(skb);
1597 dev->last_rx = jiffies;
1598 dev->stats.rx_packets++;
1599 dev->stats.rx_bytes += pkt_len;
1601 dev->stats.rx_errors++;
1602 printk(KERN_DEBUG "%s: Rx error (status %x)\n",
1603 dev->name, status & Rx_Stat_Mask);
1604 /* WORKAROUND: LongErr and CRCErr means Overflow. */
1605 if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
1606 status &= ~(Rx_LongErr|Rx_CRCErr);
1609 if (status & Rx_LongErr)
1610 dev->stats.rx_length_errors++;
1611 if (status & Rx_Over)
1612 dev->stats.rx_fifo_errors++;
1613 if (status & Rx_CRCErr)
1614 dev->stats.rx_crc_errors++;
1615 if (status & Rx_Align)
1616 dev->stats.rx_frame_errors++;
1620 /* put Free Buffer back to controller */
1621 int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
1623 (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
1625 if (id >= RX_BUF_NUM) {
1626 printk("%s: invalid BDID.\n", dev->name);
1630 BUG_ON(id >= RX_BUF_NUM);
1632 /* free old buffers */
1633 #ifdef TC35815_USE_PACKEDBUFFER
1634 while (lp->fbl_curid != id)
1636 while (lp->fbl_count < RX_BUF_NUM)
1639 #ifdef TC35815_USE_PACKEDBUFFER
1640 unsigned char curid = lp->fbl_curid;
1642 unsigned char curid =
1643 (id + 1 + lp->fbl_count) % RX_BUF_NUM;
1645 struct BDesc *bd = &lp->fbl_ptr->bd[curid];
1647 bdctl = le32_to_cpu(bd->BDCtl);
1648 if (bdctl & BD_CownsBD) {
1649 printk("%s: Freeing invalid BD.\n",
1654 /* pass BD to controller */
1655 #ifndef TC35815_USE_PACKEDBUFFER
1656 if (!lp->rx_skbs[curid].skb) {
1657 lp->rx_skbs[curid].skb =
1658 alloc_rxbuf_skb(dev,
1660 &lp->rx_skbs[curid].skb_dma);
1661 if (!lp->rx_skbs[curid].skb)
1662 break; /* try on next reception */
1663 bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
1665 #endif /* TC35815_USE_PACKEDBUFFER */
1666 /* Note: BDLength was modified by chip. */
1667 bd->BDCtl = cpu_to_le32(BD_CownsBD |
1668 (curid << BD_RxBDID_SHIFT) |
1670 #ifdef TC35815_USE_PACKEDBUFFER
1671 lp->fbl_curid = (curid + 1) % RX_BUF_NUM;
1672 if (netif_msg_rx_status(lp)) {
1673 printk("%s: Entering new FBD %d\n",
1674 dev->name, lp->fbl_curid);
1675 dump_frfd(lp->fbl_ptr);
1684 /* put RxFD back to controller */
1686 next_rfd = fd_bus_to_virt(lp,
1687 le32_to_cpu(lp->rfd_cur->fd.FDNext));
1688 if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
1689 printk("%s: RxFD FDNext invalid.\n", dev->name);
1693 for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
1694 /* pass FD to controller */
1696 lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
1698 lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
1700 lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
1704 if (lp->rfd_cur > lp->rfd_limit)
1705 lp->rfd_cur = lp->rfd_base;
1707 if (lp->rfd_cur != next_rfd)
1708 printk("rfd_cur = %p, next_rfd %p\n",
1709 lp->rfd_cur, next_rfd);
1713 /* re-enable BL/FDA Exhaust interrupts. */
1714 if (fd_free_count) {
1715 struct tc35815_regs __iomem *tr =
1716 (struct tc35815_regs __iomem *)dev->base_addr;
1717 u32 en, en_old = tc_readl(&tr->Int_En);
1718 en = en_old | Int_FDAExEn;
1722 tc_writel(en, &tr->Int_En);
1730 static int tc35815_poll(struct napi_struct *napi, int budget)
1732 struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
1733 struct net_device *dev = lp->dev;
1734 struct tc35815_regs __iomem *tr =
1735 (struct tc35815_regs __iomem *)dev->base_addr;
1736 int received = 0, handled;
1739 spin_lock(&lp->lock);
1740 status = tc_readl(&tr->Int_Src);
1742 tc_writel(status, &tr->Int_Src); /* write to clear */
1744 handled = tc35815_do_interrupt(dev, status, limit);
1746 received += handled;
1747 if (received >= budget)
1750 status = tc_readl(&tr->Int_Src);
1752 spin_unlock(&lp->lock);
1754 if (received < budget) {
1755 netif_rx_complete(dev, napi);
1756 /* enable interrupts */
1757 tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
1763 #ifdef NO_CHECK_CARRIER
1764 #define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1766 #define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
1770 tc35815_check_tx_stat(struct net_device *dev, int status)
1772 struct tc35815_local *lp = dev->priv;
1773 const char *msg = NULL;
1775 /* count collisions */
1776 if (status & Tx_ExColl)
1777 dev->stats.collisions += 16;
1778 if (status & Tx_TxColl_MASK)
1779 dev->stats.collisions += status & Tx_TxColl_MASK;
1781 #ifndef NO_CHECK_CARRIER
1782 /* TX4939 does not have NCarr */
1783 if (lp->boardtype == TC35815_TX4939)
1784 status &= ~Tx_NCarr;
1785 #ifdef WORKAROUND_LOSTCAR
1786 /* WORKAROUND: ignore LostCrS in full duplex operation */
1787 if ((lp->timer_state != asleep && lp->timer_state != lcheck)
1789 status &= ~Tx_NCarr;
1793 if (!(status & TX_STA_ERR)) {
1795 dev->stats.tx_packets++;
1799 dev->stats.tx_errors++;
1800 if (status & Tx_ExColl) {
1801 dev->stats.tx_aborted_errors++;
1802 msg = "Excessive Collision.";
1804 if (status & Tx_Under) {
1805 dev->stats.tx_fifo_errors++;
1806 msg = "Tx FIFO Underrun.";
1807 if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
1808 lp->lstats.tx_underrun++;
1809 if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
1810 struct tc35815_regs __iomem *tr =
1811 (struct tc35815_regs __iomem *)dev->base_addr;
1812 tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
1813 msg = "Tx FIFO Underrun.Change Tx threshold to max.";
1817 if (status & Tx_Defer) {
1818 dev->stats.tx_fifo_errors++;
1819 msg = "Excessive Deferral.";
1821 #ifndef NO_CHECK_CARRIER
1822 if (status & Tx_NCarr) {
1823 dev->stats.tx_carrier_errors++;
1824 msg = "Lost Carrier Sense.";
1827 if (status & Tx_LateColl) {
1828 dev->stats.tx_aborted_errors++;
1829 msg = "Late Collision.";
1831 if (status & Tx_TxPar) {
1832 dev->stats.tx_fifo_errors++;
1833 msg = "Transmit Parity Error.";
1835 if (status & Tx_SQErr) {
1836 dev->stats.tx_heartbeat_errors++;
1837 msg = "Signal Quality Error.";
1839 if (msg && netif_msg_tx_err(lp))
1840 printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
1843 /* This handles TX complete events posted by the device
1847 tc35815_txdone(struct net_device *dev)
1849 struct tc35815_local *lp = dev->priv;
1853 txfd = &lp->tfd_base[lp->tfd_end];
1854 while (lp->tfd_start != lp->tfd_end &&
1855 !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
1856 int status = le32_to_cpu(txfd->fd.FDStat);
1857 struct sk_buff *skb;
1858 unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
1859 u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
1861 if (netif_msg_tx_done(lp)) {
1862 printk("%s: complete TxFD.\n", dev->name);
1865 tc35815_check_tx_stat(dev, status);
1867 skb = fdsystem != 0xffffffff ?
1868 lp->tx_skbs[fdsystem].skb : NULL;
1870 if (lp->tx_skbs[lp->tfd_end].skb != skb) {
1871 printk("%s: tx_skbs mismatch.\n", dev->name);
1875 BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
1878 dev->stats.tx_bytes += skb->len;
1879 pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
1880 lp->tx_skbs[lp->tfd_end].skb = NULL;
1881 lp->tx_skbs[lp->tfd_end].skb_dma = 0;
1883 dev_kfree_skb_any(skb);
1885 dev_kfree_skb_irq(skb);
1888 txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
1890 lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
1891 txfd = &lp->tfd_base[lp->tfd_end];
1893 if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
1894 printk("%s: TxFD FDNext invalid.\n", dev->name);
1898 if (fdnext & FD_Next_EOL) {
1899 /* DMA Transmitter has been stopping... */
1900 if (lp->tfd_end != lp->tfd_start) {
1901 struct tc35815_regs __iomem *tr =
1902 (struct tc35815_regs __iomem *)dev->base_addr;
1903 int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
1904 struct TxFD* txhead = &lp->tfd_base[head];
1905 int qlen = (lp->tfd_start + TX_FD_NUM
1906 - lp->tfd_end) % TX_FD_NUM;
1909 if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
1910 printk("%s: TxFD FDCtl invalid.\n", dev->name);
1914 /* log max queue length */
1915 if (lp->lstats.max_tx_qlen < qlen)
1916 lp->lstats.max_tx_qlen = qlen;
1919 /* start DMA Transmitter again */
1920 txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
1922 txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
1924 if (netif_msg_tx_queued(lp)) {
1925 printk("%s: start TxFD on queue.\n",
1929 tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
1935 /* If we had stopped the queue due to a "tx full"
1936 * condition, and space has now been made available,
1937 * wake up the queue.
1939 if (netif_queue_stopped(dev) && ! tc35815_tx_full(dev))
1940 netif_wake_queue(dev);
1943 /* The inverse routine to tc35815_open(). */
1945 tc35815_close(struct net_device *dev)
1947 struct tc35815_local *lp = dev->priv;
1949 netif_stop_queue(dev);
1951 napi_disable(&lp->napi);
1954 /* Flush the Tx and disable Rx here. */
1956 del_timer(&lp->timer); /* Kill if running */
1957 tc35815_chip_reset(dev);
1958 free_irq(dev->irq, dev);
1960 tc35815_free_queues(dev);
1967 * Get the current statistics.
1968 * This may be called with the card open or closed.
1970 static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
1972 struct tc35815_regs __iomem *tr =
1973 (struct tc35815_regs __iomem *)dev->base_addr;
1974 if (netif_running(dev))
1975 /* Update the statistics from the device registers. */
1976 dev->stats.rx_missed_errors = tc_readl(&tr->Miss_Cnt);
1981 static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
1983 struct tc35815_local *lp = dev->priv;
1984 struct tc35815_regs __iomem *tr =
1985 (struct tc35815_regs __iomem *)dev->base_addr;
1986 int cam_index = index * 6;
1989 DECLARE_MAC_BUF(mac);
1991 saved_addr = tc_readl(&tr->CAM_Adr);
1993 if (netif_msg_hw(lp))
1994 printk(KERN_DEBUG "%s: CAM %d: %s\n",
1995 dev->name, index, print_mac(mac, addr));
1997 /* read modify write */
1998 tc_writel(cam_index - 2, &tr->CAM_Adr);
1999 cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
2000 cam_data |= addr[0] << 8 | addr[1];
2001 tc_writel(cam_data, &tr->CAM_Data);
2002 /* write whole word */
2003 tc_writel(cam_index + 2, &tr->CAM_Adr);
2004 cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
2005 tc_writel(cam_data, &tr->CAM_Data);
2007 /* write whole word */
2008 tc_writel(cam_index, &tr->CAM_Adr);
2009 cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
2010 tc_writel(cam_data, &tr->CAM_Data);
2011 /* read modify write */
2012 tc_writel(cam_index + 4, &tr->CAM_Adr);
2013 cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
2014 cam_data |= addr[4] << 24 | (addr[5] << 16);
2015 tc_writel(cam_data, &tr->CAM_Data);
2018 tc_writel(saved_addr, &tr->CAM_Adr);
2023 * Set or clear the multicast filter for this adaptor.
2024 * num_addrs == -1 Promiscuous mode, receive all packets
2025 * num_addrs == 0 Normal mode, clear multicast list
2026 * num_addrs > 0 Multicast mode, receive normal and MC packets,
2027 * and do best-effort filtering.
2030 tc35815_set_multicast_list(struct net_device *dev)
2032 struct tc35815_regs __iomem *tr =
2033 (struct tc35815_regs __iomem *)dev->base_addr;
2035 if (dev->flags&IFF_PROMISC)
2037 #ifdef WORKAROUND_100HALF_PROMISC
2038 /* With some (all?) 100MHalf HUB, controller will hang
2039 * if we enabled promiscuous mode before linkup... */
2040 struct tc35815_local *lp = dev->priv;
2041 int pid = lp->phy_addr;
2042 if (!(tc_mdio_read(dev, pid, MII_BMSR) & BMSR_LSTATUS))
2045 /* Enable promiscuous mode */
2046 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
2048 else if((dev->flags&IFF_ALLMULTI) || dev->mc_count > CAM_ENTRY_MAX - 3)
2050 /* CAM 0, 1, 20 are reserved. */
2051 /* Disable promiscuous mode, use normal mode. */
2052 tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
2054 else if(dev->mc_count)
2056 struct dev_mc_list* cur_addr = dev->mc_list;
2058 int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
2060 tc_writel(0, &tr->CAM_Ctl);
2061 /* Walk the address list, and load the filter */
2062 for (i = 0; i < dev->mc_count; i++, cur_addr = cur_addr->next) {
2065 /* entry 0,1 is reserved. */
2066 tc35815_set_cam_entry(dev, i + 2, cur_addr->dmi_addr);
2067 ena_bits |= CAM_Ena_Bit(i + 2);
2069 tc_writel(ena_bits, &tr->CAM_Ena);
2070 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2073 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2074 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2078 static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2080 struct tc35815_local *lp = dev->priv;
2081 strcpy(info->driver, MODNAME);
2082 strcpy(info->version, DRV_VERSION);
2083 strcpy(info->bus_info, pci_name(lp->pci_dev));
2086 static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2088 struct tc35815_local *lp = dev->priv;
2089 spin_lock_irq(&lp->lock);
2090 mii_ethtool_gset(&lp->mii, cmd);
2091 spin_unlock_irq(&lp->lock);
2095 static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2097 struct tc35815_local *lp = dev->priv;
2099 #if 1 /* use our negotiation method... */
2100 /* Verify the settings we care about. */
2101 if (cmd->autoneg != AUTONEG_ENABLE &&
2102 cmd->autoneg != AUTONEG_DISABLE)
2104 if (cmd->autoneg == AUTONEG_DISABLE &&
2105 ((cmd->speed != SPEED_100 &&
2106 cmd->speed != SPEED_10) ||
2107 (cmd->duplex != DUPLEX_HALF &&
2108 cmd->duplex != DUPLEX_FULL)))
2111 /* Ok, do it to it. */
2112 spin_lock_irq(&lp->lock);
2113 del_timer(&lp->timer);
2114 tc35815_start_auto_negotiation(dev, cmd);
2115 spin_unlock_irq(&lp->lock);
2118 spin_lock_irq(&lp->lock);
2119 rc = mii_ethtool_sset(&lp->mii, cmd);
2120 spin_unlock_irq(&lp->lock);
2125 static int tc35815_nway_reset(struct net_device *dev)
2127 struct tc35815_local *lp = dev->priv;
2129 spin_lock_irq(&lp->lock);
2130 rc = mii_nway_restart(&lp->mii);
2131 spin_unlock_irq(&lp->lock);
2135 static u32 tc35815_get_link(struct net_device *dev)
2137 struct tc35815_local *lp = dev->priv;
2139 spin_lock_irq(&lp->lock);
2140 rc = mii_link_ok(&lp->mii);
2141 spin_unlock_irq(&lp->lock);
2145 static u32 tc35815_get_msglevel(struct net_device *dev)
2147 struct tc35815_local *lp = dev->priv;
2148 return lp->msg_enable;
2151 static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
2153 struct tc35815_local *lp = dev->priv;
2154 lp->msg_enable = datum;
2157 static int tc35815_get_sset_count(struct net_device *dev, int sset)
2159 struct tc35815_local *lp = dev->priv;
2163 return sizeof(lp->lstats) / sizeof(int);
2169 static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
2171 struct tc35815_local *lp = dev->priv;
2172 data[0] = lp->lstats.max_tx_qlen;
2173 data[1] = lp->lstats.tx_ints;
2174 data[2] = lp->lstats.rx_ints;
2175 data[3] = lp->lstats.tx_underrun;
2179 const char str[ETH_GSTRING_LEN];
2180 } ethtool_stats_keys[] = {
2187 static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2189 memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
2192 static const struct ethtool_ops tc35815_ethtool_ops = {
2193 .get_drvinfo = tc35815_get_drvinfo,
2194 .get_settings = tc35815_get_settings,
2195 .set_settings = tc35815_set_settings,
2196 .nway_reset = tc35815_nway_reset,
2197 .get_link = tc35815_get_link,
2198 .get_msglevel = tc35815_get_msglevel,
2199 .set_msglevel = tc35815_set_msglevel,
2200 .get_strings = tc35815_get_strings,
2201 .get_sset_count = tc35815_get_sset_count,
2202 .get_ethtool_stats = tc35815_get_ethtool_stats,
2205 static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2207 struct tc35815_local *lp = dev->priv;
2210 if (!netif_running(dev))
2213 spin_lock_irq(&lp->lock);
2214 rc = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
2215 spin_unlock_irq(&lp->lock);
2220 static int tc_mdio_read(struct net_device *dev, int phy_id, int location)
2222 struct tc35815_regs __iomem *tr =
2223 (struct tc35815_regs __iomem *)dev->base_addr;
2225 tc_writel(MD_CA_Busy | (phy_id << 5) | location, &tr->MD_CA);
2226 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2228 data = tc_readl(&tr->MD_Data);
2229 return data & 0xffff;
2232 static void tc_mdio_write(struct net_device *dev, int phy_id, int location,
2235 struct tc35815_regs __iomem *tr =
2236 (struct tc35815_regs __iomem *)dev->base_addr;
2237 tc_writel(val, &tr->MD_Data);
2238 tc_writel(MD_CA_Busy | MD_CA_Wr | (phy_id << 5) | location, &tr->MD_CA);
2239 while (tc_readl(&tr->MD_CA) & MD_CA_Busy)
2243 /* Auto negotiation. The scheme is very simple. We have a timer routine
2244 * that keeps watching the auto negotiation process as it progresses.
2245 * The DP83840 is first told to start doing it's thing, we set up the time
2246 * and place the timer state machine in it's initial state.
2248 * Here the timer peeks at the DP83840 status registers at each click to see
2249 * if the auto negotiation has completed, we assume here that the DP83840 PHY
2250 * will time out at some point and just tell us what (didn't) happen. For
2251 * complete coverage we only allow so many of the ticks at this level to run,
2252 * when this has expired we print a warning message and try another strategy.
2253 * This "other" strategy is to force the interface into various speed/duplex
2254 * configurations and we stop when we see a link-up condition before the
2255 * maximum number of "peek" ticks have occurred.
2257 * Once a valid link status has been detected we configure the BigMAC and
2258 * the rest of the Happy Meal to speak the most efficient protocol we could
2259 * get a clean link for. The priority for link configurations, highest first
2261 * 100 Base-T Full Duplex
2262 * 100 Base-T Half Duplex
2263 * 10 Base-T Full Duplex
2264 * 10 Base-T Half Duplex
2266 * We start a new timer now, after a successful auto negotiation status has
2267 * been detected. This timer just waits for the link-up bit to get set in
2268 * the BMCR of the DP83840. When this occurs we print a kernel log message
2269 * describing the link type in use and the fact that it is up.
2271 * If a fatal error of some sort is signalled and detected in the interrupt
2272 * service routine, and the chip is reset, or the link is ifconfig'd down
2273 * and then back up, this entire process repeats itself all over again.
2275 /* Note: Above comments are come from sunhme driver. */
2277 static int tc35815_try_next_permutation(struct net_device *dev)
2279 struct tc35815_local *lp = dev->priv;
2280 int pid = lp->phy_addr;
2281 unsigned short bmcr;
2283 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2285 /* Downgrade from full to half duplex. Only possible via ethtool. */
2286 if (bmcr & BMCR_FULLDPLX) {
2287 bmcr &= ~BMCR_FULLDPLX;
2288 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2289 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2293 /* Downgrade from 100 to 10. */
2294 if (bmcr & BMCR_SPEED100) {
2295 bmcr &= ~BMCR_SPEED100;
2296 printk(KERN_DEBUG "%s: try next permutation (BMCR %x)\n", dev->name, bmcr);
2297 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2301 /* We've tried everything. */
2306 tc35815_display_link_mode(struct net_device *dev)
2308 struct tc35815_local *lp = dev->priv;
2309 int pid = lp->phy_addr;
2310 unsigned short lpa, bmcr;
2311 char *speed = "", *duplex = "";
2313 lpa = tc_mdio_read(dev, pid, MII_LPA);
2314 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2315 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2319 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2320 duplex = "Full Duplex";
2322 duplex = "Half Duplex";
2324 if (netif_msg_link(lp))
2325 printk(KERN_INFO "%s: Link is up at %s, %s.\n",
2326 dev->name, speed, duplex);
2327 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2329 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2332 static void tc35815_display_forced_link_mode(struct net_device *dev)
2334 struct tc35815_local *lp = dev->priv;
2335 int pid = lp->phy_addr;
2336 unsigned short bmcr;
2337 char *speed = "", *duplex = "";
2339 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2340 if (bmcr & BMCR_SPEED100)
2344 if (bmcr & BMCR_FULLDPLX)
2345 duplex = "Full Duplex.\n";
2347 duplex = "Half Duplex.\n";
2349 if (netif_msg_link(lp))
2350 printk(KERN_INFO "%s: Link has been forced up at %s, %s",
2351 dev->name, speed, duplex);
2354 static void tc35815_set_link_modes(struct net_device *dev)
2356 struct tc35815_local *lp = dev->priv;
2357 struct tc35815_regs __iomem *tr =
2358 (struct tc35815_regs __iomem *)dev->base_addr;
2359 int pid = lp->phy_addr;
2360 unsigned short bmcr, lpa;
2363 if (lp->timer_state == arbwait) {
2364 lpa = tc_mdio_read(dev, pid, MII_LPA);
2365 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2366 printk(KERN_DEBUG "%s: MII BMCR %04x BMSR %04x LPA %04x\n",
2368 bmcr, tc_mdio_read(dev, pid, MII_BMSR), lpa);
2369 if (!(lpa & (LPA_10HALF | LPA_10FULL |
2370 LPA_100HALF | LPA_100FULL))) {
2371 /* fall back to 10HALF */
2372 printk(KERN_INFO "%s: bad ability %04x - falling back to 10HD.\n",
2376 if (options.duplex ? (bmcr & BMCR_FULLDPLX) : (lpa & (LPA_100FULL | LPA_10FULL)))
2380 if (options.speed ? (bmcr & BMCR_SPEED100) : (lpa & (LPA_100HALF | LPA_100FULL)))
2385 /* Forcing a link mode. */
2386 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2387 if (bmcr & BMCR_FULLDPLX)
2391 if (bmcr & BMCR_SPEED100)
2397 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_HaltReq, &tr->MAC_Ctl);
2398 if (lp->fullduplex) {
2399 tc_writel(tc_readl(&tr->MAC_Ctl) | MAC_FullDup, &tr->MAC_Ctl);
2401 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_FullDup, &tr->MAC_Ctl);
2403 tc_writel(tc_readl(&tr->MAC_Ctl) & ~MAC_HaltReq, &tr->MAC_Ctl);
2405 /* TX4939 PCFG.SPEEDn bit will be changed on NETDEV_CHANGE event. */
2407 #ifndef NO_CHECK_CARRIER
2408 /* TX4939 does not have EnLCarr */
2409 if (lp->boardtype != TC35815_TX4939) {
2410 #ifdef WORKAROUND_LOSTCAR
2411 /* WORKAROUND: enable LostCrS only if half duplex operation */
2412 if (!lp->fullduplex && lp->boardtype != TC35815_TX4939)
2413 tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr, &tr->Tx_Ctl);
2417 lp->mii.full_duplex = lp->fullduplex;
2420 static void tc35815_timer(unsigned long data)
2422 struct net_device *dev = (struct net_device *)data;
2423 struct tc35815_local *lp = dev->priv;
2424 int pid = lp->phy_addr;
2425 unsigned short bmsr, bmcr, lpa;
2426 int restart_timer = 0;
2428 spin_lock_irq(&lp->lock);
2431 switch (lp->timer_state) {
2434 * Only allow for 5 ticks, thats 10 seconds and much too
2435 * long to wait for arbitration to complete.
2437 /* TC35815 need more times... */
2438 if (lp->timer_ticks >= 10) {
2439 /* Enter force mode. */
2440 if (!options.doforce) {
2441 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2442 " cable probblem?\n", dev->name);
2443 /* Try to restart the adaptor. */
2444 tc35815_restart(dev);
2447 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful,"
2448 " trying force link mode\n", dev->name);
2449 printk(KERN_DEBUG "%s: BMCR %x BMSR %x\n", dev->name,
2450 tc_mdio_read(dev, pid, MII_BMCR),
2451 tc_mdio_read(dev, pid, MII_BMSR));
2452 bmcr = BMCR_SPEED100;
2453 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2456 * OK, seems we need do disable the transceiver
2457 * for the first tick to make sure we get an
2458 * accurate link state at the second tick.
2461 lp->timer_state = ltrywait;
2462 lp->timer_ticks = 0;
2465 /* Anything interesting happen? */
2466 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2467 if (bmsr & BMSR_ANEGCOMPLETE) {
2468 /* Just what we've been waiting for... */
2469 tc35815_set_link_modes(dev);
2472 * Success, at least so far, advance our state
2475 lp->timer_state = lupwait;
2485 * Auto negotiation was successful and we are awaiting a
2486 * link up status. I have decided to let this timer run
2487 * forever until some sort of error is signalled, reporting
2488 * a message to the user at 10 second intervals.
2490 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2491 if (bmsr & BMSR_LSTATUS) {
2493 * Wheee, it's up, display the link mode in use and put
2494 * the timer to sleep.
2496 tc35815_display_link_mode(dev);
2497 netif_carrier_on(dev);
2498 #ifdef WORKAROUND_100HALF_PROMISC
2499 /* delayed promiscuous enabling */
2500 if (dev->flags & IFF_PROMISC)
2501 tc35815_set_multicast_list(dev);
2504 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2505 lp->timer_state = lcheck;
2508 lp->timer_state = asleep;
2512 if (lp->timer_ticks >= 10) {
2513 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
2514 "not completely up.\n", dev->name);
2515 lp->timer_ticks = 0;
2525 * Making the timeout here too long can make it take
2526 * annoyingly long to attempt all of the link mode
2527 * permutations, but then again this is essentially
2528 * error recovery code for the most part.
2530 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2531 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2532 if (lp->timer_ticks == 1) {
2534 * Re-enable transceiver, we'll re-enable the
2535 * transceiver next tick, then check link state
2536 * on the following tick.
2541 if (lp->timer_ticks == 2) {
2545 if (bmsr & BMSR_LSTATUS) {
2546 /* Force mode selection success. */
2547 tc35815_display_forced_link_mode(dev);
2548 netif_carrier_on(dev);
2549 tc35815_set_link_modes(dev);
2550 #ifdef WORKAROUND_100HALF_PROMISC
2551 /* delayed promiscuous enabling */
2552 if (dev->flags & IFF_PROMISC)
2553 tc35815_set_multicast_list(dev);
2556 lp->saved_lpa = tc_mdio_read(dev, pid, MII_LPA);
2557 lp->timer_state = lcheck;
2560 lp->timer_state = asleep;
2564 if (lp->timer_ticks >= 4) { /* 6 seconds or so... */
2567 ret = tc35815_try_next_permutation(dev);
2570 * Aieee, tried them all, reset the
2571 * chip and try all over again.
2573 printk(KERN_NOTICE "%s: Link down, "
2577 /* Try to restart the adaptor. */
2578 tc35815_restart(dev);
2581 lp->timer_ticks = 0;
2590 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2591 lpa = tc_mdio_read(dev, pid, MII_LPA);
2592 if (bmcr & (BMCR_PDOWN | BMCR_ISOLATE | BMCR_RESET)) {
2593 printk(KERN_ERR "%s: PHY down? (BMCR %x)\n", dev->name,
2595 } else if ((lp->saved_lpa ^ lpa) &
2596 (LPA_100FULL|LPA_100HALF|LPA_10FULL|LPA_10HALF)) {
2597 printk(KERN_NOTICE "%s: link status changed"
2598 " (BMCR %x LPA %x->%x)\n", dev->name,
2599 bmcr, lp->saved_lpa, lpa);
2605 /* Try to restart the adaptor. */
2606 tc35815_restart(dev);
2611 /* Can't happens.... */
2612 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got "
2613 "one anyways!\n", dev->name);
2615 lp->timer_ticks = 0;
2616 lp->timer_state = asleep; /* foo on you */
2620 if (restart_timer) {
2621 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2622 add_timer(&lp->timer);
2625 spin_unlock_irq(&lp->lock);
2628 static void tc35815_start_auto_negotiation(struct net_device *dev,
2629 struct ethtool_cmd *ep)
2631 struct tc35815_local *lp = dev->priv;
2632 int pid = lp->phy_addr;
2633 unsigned short bmsr, bmcr, advertize;
2636 netif_carrier_off(dev);
2637 bmsr = tc_mdio_read(dev, pid, MII_BMSR);
2638 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2639 advertize = tc_mdio_read(dev, pid, MII_ADVERTISE);
2641 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2642 if (options.speed || options.duplex) {
2643 /* Advertise only specified configuration. */
2644 advertize &= ~(ADVERTISE_10HALF |
2648 if (options.speed != 10) {
2649 if (options.duplex != 1)
2650 advertize |= ADVERTISE_100FULL;
2651 if (options.duplex != 2)
2652 advertize |= ADVERTISE_100HALF;
2654 if (options.speed != 100) {
2655 if (options.duplex != 1)
2656 advertize |= ADVERTISE_10FULL;
2657 if (options.duplex != 2)
2658 advertize |= ADVERTISE_10HALF;
2660 if (options.speed == 100)
2661 bmcr |= BMCR_SPEED100;
2662 else if (options.speed == 10)
2663 bmcr &= ~BMCR_SPEED100;
2664 if (options.duplex == 2)
2665 bmcr |= BMCR_FULLDPLX;
2666 else if (options.duplex == 1)
2667 bmcr &= ~BMCR_FULLDPLX;
2669 /* Advertise everything we can support. */
2670 if (bmsr & BMSR_10HALF)
2671 advertize |= ADVERTISE_10HALF;
2673 advertize &= ~ADVERTISE_10HALF;
2674 if (bmsr & BMSR_10FULL)
2675 advertize |= ADVERTISE_10FULL;
2677 advertize &= ~ADVERTISE_10FULL;
2678 if (bmsr & BMSR_100HALF)
2679 advertize |= ADVERTISE_100HALF;
2681 advertize &= ~ADVERTISE_100HALF;
2682 if (bmsr & BMSR_100FULL)
2683 advertize |= ADVERTISE_100FULL;
2685 advertize &= ~ADVERTISE_100FULL;
2688 tc_mdio_write(dev, pid, MII_ADVERTISE, advertize);
2690 /* Enable Auto-Negotiation, this is usually on already... */
2691 bmcr |= BMCR_ANENABLE;
2692 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2694 /* Restart it to make sure it is going. */
2695 bmcr |= BMCR_ANRESTART;
2696 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2697 printk(KERN_DEBUG "%s: ADVERTISE %x BMCR %x\n", dev->name, advertize, bmcr);
2699 /* BMCR_ANRESTART self clears when the process has begun. */
2700 timeout = 64; /* More than enough. */
2702 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2703 if (!(bmcr & BMCR_ANRESTART))
2704 break; /* got it. */
2708 printk(KERN_ERR "%s: TC35815 would not start auto "
2709 "negotiation BMCR=0x%04x\n",
2711 printk(KERN_NOTICE "%s: Performing force link "
2712 "detection.\n", dev->name);
2715 printk(KERN_DEBUG "%s: auto negotiation started.\n", dev->name);
2716 lp->timer_state = arbwait;
2720 /* Force the link up, trying first a particular mode.
2721 * Either we are here at the request of ethtool or
2722 * because the Happy Meal would not start to autoneg.
2725 /* Disable auto-negotiation in BMCR, enable the duplex and
2726 * speed setting, init the timer state machine, and fire it off.
2728 if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
2729 bmcr = BMCR_SPEED100;
2731 if (ep->speed == SPEED_100)
2732 bmcr = BMCR_SPEED100;
2735 if (ep->duplex == DUPLEX_FULL)
2736 bmcr |= BMCR_FULLDPLX;
2738 tc_mdio_write(dev, pid, MII_BMCR, bmcr);
2740 /* OK, seems we need do disable the transceiver for the first
2741 * tick to make sure we get an accurate link state at the
2744 lp->timer_state = ltrywait;
2747 del_timer(&lp->timer);
2748 lp->timer_ticks = 0;
2749 lp->timer.expires = jiffies + msecs_to_jiffies(1200);
2750 add_timer(&lp->timer);
2753 static void tc35815_find_phy(struct net_device *dev)
2755 struct tc35815_local *lp = dev->priv;
2756 int pid = lp->phy_addr;
2760 for (pid = 31; pid >= 0; pid--) {
2761 id0 = tc_mdio_read(dev, pid, MII_BMSR);
2762 if (id0 != 0xffff && id0 != 0x0000 &&
2763 (id0 & BMSR_RESV) != (0xffff & BMSR_RESV) /* paranoia? */
2770 printk(KERN_ERR "%s: No MII Phy found.\n",
2772 lp->phy_addr = pid = 0;
2775 lp->mii_id[0] = tc_mdio_read(dev, pid, MII_PHYSID1);
2776 lp->mii_id[1] = tc_mdio_read(dev, pid, MII_PHYSID2);
2777 if (netif_msg_hw(lp))
2778 printk(KERN_INFO "%s: PHY(%02x) ID %04x %04x\n", dev->name,
2779 pid, lp->mii_id[0], lp->mii_id[1]);
2782 static void tc35815_phy_chip_init(struct net_device *dev)
2784 struct tc35815_local *lp = dev->priv;
2785 int pid = lp->phy_addr;
2786 unsigned short bmcr;
2787 struct ethtool_cmd ecmd, *ep;
2789 /* dis-isolate if needed. */
2790 bmcr = tc_mdio_read(dev, pid, MII_BMCR);
2791 if (bmcr & BMCR_ISOLATE) {
2793 printk(KERN_DEBUG "%s: unisolating...", dev->name);
2794 tc_mdio_write(dev, pid, MII_BMCR, bmcr & ~BMCR_ISOLATE);
2796 if (!(tc_mdio_read(dev, pid, MII_BMCR) & BMCR_ISOLATE))
2800 printk(" %s.\n", count ? "done" : "failed");
2803 if (options.speed && options.duplex) {
2804 ecmd.autoneg = AUTONEG_DISABLE;
2805 ecmd.speed = options.speed == 10 ? SPEED_10 : SPEED_100;
2806 ecmd.duplex = options.duplex == 1 ? DUPLEX_HALF : DUPLEX_FULL;
2811 tc35815_start_auto_negotiation(dev, ep);
2814 static void tc35815_chip_reset(struct net_device *dev)
2816 struct tc35815_regs __iomem *tr =
2817 (struct tc35815_regs __iomem *)dev->base_addr;
2819 /* reset the controller */
2820 tc_writel(MAC_Reset, &tr->MAC_Ctl);
2821 udelay(4); /* 3200ns */
2823 while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
2825 printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
2830 tc_writel(0, &tr->MAC_Ctl);
2832 /* initialize registers to default value */
2833 tc_writel(0, &tr->DMA_Ctl);
2834 tc_writel(0, &tr->TxThrsh);
2835 tc_writel(0, &tr->TxPollCtr);
2836 tc_writel(0, &tr->RxFragSize);
2837 tc_writel(0, &tr->Int_En);
2838 tc_writel(0, &tr->FDA_Bas);
2839 tc_writel(0, &tr->FDA_Lim);
2840 tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
2841 tc_writel(0, &tr->CAM_Ctl);
2842 tc_writel(0, &tr->Tx_Ctl);
2843 tc_writel(0, &tr->Rx_Ctl);
2844 tc_writel(0, &tr->CAM_Ena);
2845 (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
2847 /* initialize internal SRAM */
2848 tc_writel(DMA_TestMode, &tr->DMA_Ctl);
2849 for (i = 0; i < 0x1000; i += 4) {
2850 tc_writel(i, &tr->CAM_Adr);
2851 tc_writel(0, &tr->CAM_Data);
2853 tc_writel(0, &tr->DMA_Ctl);
2856 static void tc35815_chip_init(struct net_device *dev)
2858 struct tc35815_local *lp = dev->priv;
2859 struct tc35815_regs __iomem *tr =
2860 (struct tc35815_regs __iomem *)dev->base_addr;
2861 unsigned long txctl = TX_CTL_CMD;
2863 tc35815_phy_chip_init(dev);
2865 /* load station address to CAM */
2866 tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
2868 /* Enable CAM (broadcast and unicast) */
2869 tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
2870 tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
2872 /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
2873 if (HAVE_DMA_RXALIGN(lp))
2874 tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
2876 tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
2877 #ifdef TC35815_USE_PACKEDBUFFER
2878 tc_writel(RxFrag_EnPack | ETH_ZLEN, &tr->RxFragSize); /* Packing */
2880 tc_writel(ETH_ZLEN, &tr->RxFragSize);
2882 tc_writel(0, &tr->TxPollCtr); /* Batch mode */
2883 tc_writel(TX_THRESHOLD, &tr->TxThrsh);
2884 tc_writel(INT_EN_CMD, &tr->Int_En);
2887 tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
2888 tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
2891 * Activation method:
2892 * First, enable the MAC Transmitter and the DMA Receive circuits.
2893 * Then enable the DMA Transmitter and the MAC Receive circuits.
2895 tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
2896 tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
2898 /* start MAC transmitter */
2899 #ifndef NO_CHECK_CARRIER
2900 /* TX4939 does not have EnLCarr */
2901 if (lp->boardtype == TC35815_TX4939)
2902 txctl &= ~Tx_EnLCarr;
2903 #ifdef WORKAROUND_LOSTCAR
2904 /* WORKAROUND: ignore LostCrS in full duplex operation */
2905 if ((lp->timer_state != asleep && lp->timer_state != lcheck) ||
2907 txctl &= ~Tx_EnLCarr;
2909 #endif /* !NO_CHECK_CARRIER */
2911 txctl &= ~Tx_EnComp; /* disable global tx completion int. */
2913 tc_writel(txctl, &tr->Tx_Ctl);
2917 static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
2919 struct net_device *dev = pci_get_drvdata(pdev);
2920 struct tc35815_local *lp = dev->priv;
2921 unsigned long flags;
2923 pci_save_state(pdev);
2924 if (!netif_running(dev))
2926 netif_device_detach(dev);
2927 spin_lock_irqsave(&lp->lock, flags);
2928 del_timer(&lp->timer); /* Kill if running */
2929 tc35815_chip_reset(dev);
2930 spin_unlock_irqrestore(&lp->lock, flags);
2931 pci_set_power_state(pdev, PCI_D3hot);
2935 static int tc35815_resume(struct pci_dev *pdev)
2937 struct net_device *dev = pci_get_drvdata(pdev);
2938 struct tc35815_local *lp = dev->priv;
2939 unsigned long flags;
2941 pci_restore_state(pdev);
2942 if (!netif_running(dev))
2944 pci_set_power_state(pdev, PCI_D0);
2945 spin_lock_irqsave(&lp->lock, flags);
2946 tc35815_restart(dev);
2947 spin_unlock_irqrestore(&lp->lock, flags);
2948 netif_device_attach(dev);
2951 #endif /* CONFIG_PM */
2953 static struct pci_driver tc35815_pci_driver = {
2955 .id_table = tc35815_pci_tbl,
2956 .probe = tc35815_init_one,
2957 .remove = __devexit_p(tc35815_remove_one),
2959 .suspend = tc35815_suspend,
2960 .resume = tc35815_resume,
2964 module_param_named(speed, options.speed, int, 0);
2965 MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
2966 module_param_named(duplex, options.duplex, int, 0);
2967 MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
2968 module_param_named(doforce, options.doforce, int, 0);
2969 MODULE_PARM_DESC(doforce, "try force link mode if auto-negotiation failed");
2971 static int __init tc35815_init_module(void)
2973 return pci_register_driver(&tc35815_pci_driver);
2976 static void __exit tc35815_cleanup_module(void)
2978 pci_unregister_driver(&tc35815_pci_driver);
2981 module_init(tc35815_init_module);
2982 module_exit(tc35815_cleanup_module);
2984 MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
2985 MODULE_LICENSE("GPL");