tc35815.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*
  2. * tc35815.c: A TOSHIBA TC35815CF PCI 10/100Mbps ethernet driver for linux.
  3. *
  4. * Based on skelton.c by Donald Becker.
  5. *
  6. * 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>-----
  16. *
  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
  19. * for more details.
  20. *
  21. * (C) Copyright TOSHIBA CORPORATION 2004-2005
  22. * All Rights Reserved.
  23. */
  24. #define DRV_VERSION "1.39"
  25. static const char *version = "tc35815.c:v" DRV_VERSION "\n";
  26. #define MODNAME "tc35815"
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/ioport.h>
  33. #include <linux/in.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/slab.h>
  36. #include <linux/string.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/errno.h>
  39. #include <linux/init.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/delay.h>
  44. #include <linux/pci.h>
  45. #include <linux/phy.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/platform_device.h>
  48. #include <linux/prefetch.h>
  49. #include <asm/io.h>
  50. #include <asm/byteorder.h>
  51. enum tc35815_chiptype {
  52. TC35815CF = 0,
  53. TC35815_NWU,
  54. TC35815_TX4939,
  55. };
  56. /* indexed by tc35815_chiptype, above */
  57. static const struct {
  58. const char *name;
  59. } chip_info[] __devinitdata = {
  60. { "TOSHIBA TC35815CF 10/100BaseTX" },
  61. { "TOSHIBA TC35815 with Wake on LAN" },
  62. { "TOSHIBA TC35815/TX4939" },
  63. };
  64. static DEFINE_PCI_DEVICE_TABLE(tc35815_pci_tbl) = {
  65. {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815CF), .driver_data = TC35815CF },
  66. {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_NWU), .driver_data = TC35815_NWU },
  67. {PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_TC35815_TX4939), .driver_data = TC35815_TX4939 },
  68. {0,}
  69. };
  70. MODULE_DEVICE_TABLE(pci, tc35815_pci_tbl);
  71. /* see MODULE_PARM_DESC */
  72. static struct tc35815_options {
  73. int speed;
  74. int duplex;
  75. } options;
  76. /*
  77. * Registers
  78. */
  79. struct tc35815_regs {
  80. __u32 DMA_Ctl; /* 0x00 */
  81. __u32 TxFrmPtr;
  82. __u32 TxThrsh;
  83. __u32 TxPollCtr;
  84. __u32 BLFrmPtr;
  85. __u32 RxFragSize;
  86. __u32 Int_En;
  87. __u32 FDA_Bas;
  88. __u32 FDA_Lim; /* 0x20 */
  89. __u32 Int_Src;
  90. __u32 unused0[2];
  91. __u32 PauseCnt;
  92. __u32 RemPauCnt;
  93. __u32 TxCtlFrmStat;
  94. __u32 unused1;
  95. __u32 MAC_Ctl; /* 0x40 */
  96. __u32 CAM_Ctl;
  97. __u32 Tx_Ctl;
  98. __u32 Tx_Stat;
  99. __u32 Rx_Ctl;
  100. __u32 Rx_Stat;
  101. __u32 MD_Data;
  102. __u32 MD_CA;
  103. __u32 CAM_Adr; /* 0x60 */
  104. __u32 CAM_Data;
  105. __u32 CAM_Ena;
  106. __u32 PROM_Ctl;
  107. __u32 PROM_Data;
  108. __u32 Algn_Cnt;
  109. __u32 CRC_Cnt;
  110. __u32 Miss_Cnt;
  111. };
  112. /*
  113. * Bit assignments
  114. */
  115. /* DMA_Ctl bit assign ------------------------------------------------------- */
  116. #define DMA_RxAlign 0x00c00000 /* 1:Reception Alignment */
  117. #define DMA_RxAlign_1 0x00400000
  118. #define DMA_RxAlign_2 0x00800000
  119. #define DMA_RxAlign_3 0x00c00000
  120. #define DMA_M66EnStat 0x00080000 /* 1:66MHz Enable State */
  121. #define DMA_IntMask 0x00040000 /* 1:Interrupt mask */
  122. #define DMA_SWIntReq 0x00020000 /* 1:Software Interrupt request */
  123. #define DMA_TxWakeUp 0x00010000 /* 1:Transmit Wake Up */
  124. #define DMA_RxBigE 0x00008000 /* 1:Receive Big Endian */
  125. #define DMA_TxBigE 0x00004000 /* 1:Transmit Big Endian */
  126. #define DMA_TestMode 0x00002000 /* 1:Test Mode */
  127. #define DMA_PowrMgmnt 0x00001000 /* 1:Power Management */
  128. #define DMA_DmBurst_Mask 0x000001fc /* DMA Burst size */
  129. /* RxFragSize bit assign ---------------------------------------------------- */
  130. #define RxFrag_EnPack 0x00008000 /* 1:Enable Packing */
  131. #define RxFrag_MinFragMask 0x00000ffc /* Minimum Fragment */
  132. /* MAC_Ctl bit assign ------------------------------------------------------- */
  133. #define MAC_Link10 0x00008000 /* 1:Link Status 10Mbits */
  134. #define MAC_EnMissRoll 0x00002000 /* 1:Enable Missed Roll */
  135. #define MAC_MissRoll 0x00000400 /* 1:Missed Roll */
  136. #define MAC_Loop10 0x00000080 /* 1:Loop 10 Mbps */
  137. #define MAC_Conn_Auto 0x00000000 /*00:Connection mode (Automatic) */
  138. #define MAC_Conn_10M 0x00000020 /*01: (10Mbps endec)*/
  139. #define MAC_Conn_Mll 0x00000040 /*10: (Mll clock) */
  140. #define MAC_MacLoop 0x00000010 /* 1:MAC Loopback */
  141. #define MAC_FullDup 0x00000008 /* 1:Full Duplex 0:Half Duplex */
  142. #define MAC_Reset 0x00000004 /* 1:Software Reset */
  143. #define MAC_HaltImm 0x00000002 /* 1:Halt Immediate */
  144. #define MAC_HaltReq 0x00000001 /* 1:Halt request */
  145. /* PROM_Ctl bit assign ------------------------------------------------------ */
  146. #define PROM_Busy 0x00008000 /* 1:Busy (Start Operation) */
  147. #define PROM_Read 0x00004000 /*10:Read operation */
  148. #define PROM_Write 0x00002000 /*01:Write operation */
  149. #define PROM_Erase 0x00006000 /*11:Erase operation */
  150. /*00:Enable or Disable Writting, */
  151. /* as specified in PROM_Addr. */
  152. #define PROM_Addr_Ena 0x00000030 /*11xxxx:PROM Write enable */
  153. /*00xxxx: disable */
  154. /* CAM_Ctl bit assign ------------------------------------------------------- */
  155. #define CAM_CompEn 0x00000010 /* 1:CAM Compare Enable */
  156. #define CAM_NegCAM 0x00000008 /* 1:Reject packets CAM recognizes,*/
  157. /* accept other */
  158. #define CAM_BroadAcc 0x00000004 /* 1:Broadcast assept */
  159. #define CAM_GroupAcc 0x00000002 /* 1:Multicast assept */
  160. #define CAM_StationAcc 0x00000001 /* 1:unicast accept */
  161. /* CAM_Ena bit assign ------------------------------------------------------- */
  162. #define CAM_ENTRY_MAX 21 /* CAM Data entry max count */
  163. #define CAM_Ena_Mask ((1<<CAM_ENTRY_MAX)-1) /* CAM Enable bits (Max 21bits) */
  164. #define CAM_Ena_Bit(index) (1 << (index))
  165. #define CAM_ENTRY_DESTINATION 0
  166. #define CAM_ENTRY_SOURCE 1
  167. #define CAM_ENTRY_MACCTL 20
  168. /* Tx_Ctl bit assign -------------------------------------------------------- */
  169. #define Tx_En 0x00000001 /* 1:Transmit enable */
  170. #define Tx_TxHalt 0x00000002 /* 1:Transmit Halt Request */
  171. #define Tx_NoPad 0x00000004 /* 1:Suppress Padding */
  172. #define Tx_NoCRC 0x00000008 /* 1:Suppress Padding */
  173. #define Tx_FBack 0x00000010 /* 1:Fast Back-off */
  174. #define Tx_EnUnder 0x00000100 /* 1:Enable Underrun */
  175. #define Tx_EnExDefer 0x00000200 /* 1:Enable Excessive Deferral */
  176. #define Tx_EnLCarr 0x00000400 /* 1:Enable Lost Carrier */
  177. #define Tx_EnExColl 0x00000800 /* 1:Enable Excessive Collision */
  178. #define Tx_EnLateColl 0x00001000 /* 1:Enable Late Collision */
  179. #define Tx_EnTxPar 0x00002000 /* 1:Enable Transmit Parity */
  180. #define Tx_EnComp 0x00004000 /* 1:Enable Completion */
  181. /* Tx_Stat bit assign ------------------------------------------------------- */
  182. #define Tx_TxColl_MASK 0x0000000F /* Tx Collision Count */
  183. #define Tx_ExColl 0x00000010 /* Excessive Collision */
  184. #define Tx_TXDefer 0x00000020 /* Transmit Defered */
  185. #define Tx_Paused 0x00000040 /* Transmit Paused */
  186. #define Tx_IntTx 0x00000080 /* Interrupt on Tx */
  187. #define Tx_Under 0x00000100 /* Underrun */
  188. #define Tx_Defer 0x00000200 /* Deferral */
  189. #define Tx_NCarr 0x00000400 /* No Carrier */
  190. #define Tx_10Stat 0x00000800 /* 10Mbps Status */
  191. #define Tx_LateColl 0x00001000 /* Late Collision */
  192. #define Tx_TxPar 0x00002000 /* Tx Parity Error */
  193. #define Tx_Comp 0x00004000 /* Completion */
  194. #define Tx_Halted 0x00008000 /* Tx Halted */
  195. #define Tx_SQErr 0x00010000 /* Signal Quality Error(SQE) */
  196. /* Rx_Ctl bit assign -------------------------------------------------------- */
  197. #define Rx_EnGood 0x00004000 /* 1:Enable Good */
  198. #define Rx_EnRxPar 0x00002000 /* 1:Enable Receive Parity */
  199. #define Rx_EnLongErr 0x00000800 /* 1:Enable Long Error */
  200. #define Rx_EnOver 0x00000400 /* 1:Enable OverFlow */
  201. #define Rx_EnCRCErr 0x00000200 /* 1:Enable CRC Error */
  202. #define Rx_EnAlign 0x00000100 /* 1:Enable Alignment */
  203. #define Rx_IgnoreCRC 0x00000040 /* 1:Ignore CRC Value */
  204. #define Rx_StripCRC 0x00000010 /* 1:Strip CRC Value */
  205. #define Rx_ShortEn 0x00000008 /* 1:Short Enable */
  206. #define Rx_LongEn 0x00000004 /* 1:Long Enable */
  207. #define Rx_RxHalt 0x00000002 /* 1:Receive Halt Request */
  208. #define Rx_RxEn 0x00000001 /* 1:Receive Intrrupt Enable */
  209. /* Rx_Stat bit assign ------------------------------------------------------- */
  210. #define Rx_Halted 0x00008000 /* Rx Halted */
  211. #define Rx_Good 0x00004000 /* Rx Good */
  212. #define Rx_RxPar 0x00002000 /* Rx Parity Error */
  213. #define Rx_TypePkt 0x00001000 /* Rx Type Packet */
  214. #define Rx_LongErr 0x00000800 /* Rx Long Error */
  215. #define Rx_Over 0x00000400 /* Rx Overflow */
  216. #define Rx_CRCErr 0x00000200 /* Rx CRC Error */
  217. #define Rx_Align 0x00000100 /* Rx Alignment Error */
  218. #define Rx_10Stat 0x00000080 /* Rx 10Mbps Status */
  219. #define Rx_IntRx 0x00000040 /* Rx Interrupt */
  220. #define Rx_CtlRecd 0x00000020 /* Rx Control Receive */
  221. #define Rx_InLenErr 0x00000010 /* Rx In Range Frame Length Error */
  222. #define Rx_Stat_Mask 0x0000FFF0 /* Rx All Status Mask */
  223. /* Int_En bit assign -------------------------------------------------------- */
  224. #define Int_NRAbtEn 0x00000800 /* 1:Non-recoverable Abort Enable */
  225. #define Int_TxCtlCmpEn 0x00000400 /* 1:Transmit Ctl Complete Enable */
  226. #define Int_DmParErrEn 0x00000200 /* 1:DMA Parity Error Enable */
  227. #define Int_DParDEn 0x00000100 /* 1:Data Parity Error Enable */
  228. #define Int_EarNotEn 0x00000080 /* 1:Early Notify Enable */
  229. #define Int_DParErrEn 0x00000040 /* 1:Detected Parity Error Enable */
  230. #define Int_SSysErrEn 0x00000020 /* 1:Signalled System Error Enable */
  231. #define Int_RMasAbtEn 0x00000010 /* 1:Received Master Abort Enable */
  232. #define Int_RTargAbtEn 0x00000008 /* 1:Received Target Abort Enable */
  233. #define Int_STargAbtEn 0x00000004 /* 1:Signalled Target Abort Enable */
  234. #define Int_BLExEn 0x00000002 /* 1:Buffer List Exhausted Enable */
  235. #define Int_FDAExEn 0x00000001 /* 1:Free Descriptor Area */
  236. /* Exhausted Enable */
  237. /* Int_Src bit assign ------------------------------------------------------- */
  238. #define Int_NRabt 0x00004000 /* 1:Non Recoverable error */
  239. #define Int_DmParErrStat 0x00002000 /* 1:DMA Parity Error & Clear */
  240. #define Int_BLEx 0x00001000 /* 1:Buffer List Empty & Clear */
  241. #define Int_FDAEx 0x00000800 /* 1:FDA Empty & Clear */
  242. #define Int_IntNRAbt 0x00000400 /* 1:Non Recoverable Abort */
  243. #define Int_IntCmp 0x00000200 /* 1:MAC control packet complete */
  244. #define Int_IntExBD 0x00000100 /* 1:Interrupt Extra BD & Clear */
  245. #define Int_DmParErr 0x00000080 /* 1:DMA Parity Error & Clear */
  246. #define Int_IntEarNot 0x00000040 /* 1:Receive Data write & Clear */
  247. #define Int_SWInt 0x00000020 /* 1:Software request & Clear */
  248. #define Int_IntBLEx 0x00000010 /* 1:Buffer List Empty & Clear */
  249. #define Int_IntFDAEx 0x00000008 /* 1:FDA Empty & Clear */
  250. #define Int_IntPCI 0x00000004 /* 1:PCI controller & Clear */
  251. #define Int_IntMacRx 0x00000002 /* 1:Rx controller & Clear */
  252. #define Int_IntMacTx 0x00000001 /* 1:Tx controller & Clear */
  253. /* MD_CA bit assign --------------------------------------------------------- */
  254. #define MD_CA_PreSup 0x00001000 /* 1:Preamble Suppress */
  255. #define MD_CA_Busy 0x00000800 /* 1:Busy (Start Operation) */
  256. #define MD_CA_Wr 0x00000400 /* 1:Write 0:Read */
  257. /*
  258. * Descriptors
  259. */
  260. /* Frame descripter */
  261. struct FDesc {
  262. volatile __u32 FDNext;
  263. volatile __u32 FDSystem;
  264. volatile __u32 FDStat;
  265. volatile __u32 FDCtl;
  266. };
  267. /* Buffer descripter */
  268. struct BDesc {
  269. volatile __u32 BuffData;
  270. volatile __u32 BDCtl;
  271. };
  272. #define FD_ALIGN 16
  273. /* Frame Descripter bit assign ---------------------------------------------- */
  274. #define FD_FDLength_MASK 0x0000FFFF /* Length MASK */
  275. #define FD_BDCnt_MASK 0x001F0000 /* BD count MASK in FD */
  276. #define FD_FrmOpt_MASK 0x7C000000 /* Frame option MASK */
  277. #define FD_FrmOpt_BigEndian 0x40000000 /* Tx/Rx */
  278. #define FD_FrmOpt_IntTx 0x20000000 /* Tx only */
  279. #define FD_FrmOpt_NoCRC 0x10000000 /* Tx only */
  280. #define FD_FrmOpt_NoPadding 0x08000000 /* Tx only */
  281. #define FD_FrmOpt_Packing 0x04000000 /* Rx only */
  282. #define FD_CownsFD 0x80000000 /* FD Controller owner bit */
  283. #define FD_Next_EOL 0x00000001 /* FD EOL indicator */
  284. #define FD_BDCnt_SHIFT 16
  285. /* Buffer Descripter bit assign --------------------------------------------- */
  286. #define BD_BuffLength_MASK 0x0000FFFF /* Receive Data Size */
  287. #define BD_RxBDID_MASK 0x00FF0000 /* BD ID Number MASK */
  288. #define BD_RxBDSeqN_MASK 0x7F000000 /* Rx BD Sequence Number */
  289. #define BD_CownsBD 0x80000000 /* BD Controller owner bit */
  290. #define BD_RxBDID_SHIFT 16
  291. #define BD_RxBDSeqN_SHIFT 24
  292. /* Some useful constants. */
  293. #define TX_CTL_CMD (Tx_EnTxPar | Tx_EnLateColl | \
  294. Tx_EnExColl | Tx_EnLCarr | Tx_EnExDefer | Tx_EnUnder | \
  295. Tx_En) /* maybe 0x7b01 */
  296. /* Do not use Rx_StripCRC -- it causes trouble on BLEx/FDAEx condition */
  297. #define RX_CTL_CMD (Rx_EnGood | Rx_EnRxPar | Rx_EnLongErr | Rx_EnOver \
  298. | Rx_EnCRCErr | Rx_EnAlign | Rx_RxEn) /* maybe 0x6f01 */
  299. #define INT_EN_CMD (Int_NRAbtEn | \
  300. Int_DmParErrEn | Int_DParDEn | Int_DParErrEn | \
  301. Int_SSysErrEn | Int_RMasAbtEn | Int_RTargAbtEn | \
  302. Int_STargAbtEn | \
  303. Int_BLExEn | Int_FDAExEn) /* maybe 0xb7f*/
  304. #define DMA_CTL_CMD DMA_BURST_SIZE
  305. #define HAVE_DMA_RXALIGN(lp) likely((lp)->chiptype != TC35815CF)
  306. /* Tuning parameters */
  307. #define DMA_BURST_SIZE 32
  308. #define TX_THRESHOLD 1024
  309. /* used threshold with packet max byte for low pci transfer ability.*/
  310. #define TX_THRESHOLD_MAX 1536
  311. /* setting threshold max value when overrun error occurred this count. */
  312. #define TX_THRESHOLD_KEEP_LIMIT 10
  313. /* 16 + RX_BUF_NUM * 8 + RX_FD_NUM * 16 + TX_FD_NUM * 32 <= PAGE_SIZE*FD_PAGE_NUM */
  314. #define FD_PAGE_NUM 4
  315. #define RX_BUF_NUM 128 /* < 256 */
  316. #define RX_FD_NUM 256 /* >= 32 */
  317. #define TX_FD_NUM 128
  318. #if RX_CTL_CMD & Rx_LongEn
  319. #define RX_BUF_SIZE PAGE_SIZE
  320. #elif RX_CTL_CMD & Rx_StripCRC
  321. #define RX_BUF_SIZE \
  322. L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + NET_IP_ALIGN)
  323. #else
  324. #define RX_BUF_SIZE \
  325. L1_CACHE_ALIGN(ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN + NET_IP_ALIGN)
  326. #endif
  327. #define RX_FD_RESERVE (2 / 2) /* max 2 BD per RxFD */
  328. #define NAPI_WEIGHT 16
  329. struct TxFD {
  330. struct FDesc fd;
  331. struct BDesc bd;
  332. struct BDesc unused;
  333. };
  334. struct RxFD {
  335. struct FDesc fd;
  336. struct BDesc bd[0]; /* variable length */
  337. };
  338. struct FrFD {
  339. struct FDesc fd;
  340. struct BDesc bd[RX_BUF_NUM];
  341. };
  342. #define tc_readl(addr) ioread32(addr)
  343. #define tc_writel(d, addr) iowrite32(d, addr)
  344. #define TC35815_TX_TIMEOUT msecs_to_jiffies(400)
  345. /* Information that need to be kept for each controller. */
  346. struct tc35815_local {
  347. struct pci_dev *pci_dev;
  348. struct net_device *dev;
  349. struct napi_struct napi;
  350. /* statistics */
  351. struct {
  352. int max_tx_qlen;
  353. int tx_ints;
  354. int rx_ints;
  355. int tx_underrun;
  356. } lstats;
  357. /* Tx control lock. This protects the transmit buffer ring
  358. * state along with the "tx full" state of the driver. This
  359. * means all netif_queue flow control actions are protected
  360. * by this lock as well.
  361. */
  362. spinlock_t lock;
  363. spinlock_t rx_lock;
  364. struct mii_bus *mii_bus;
  365. struct phy_device *phy_dev;
  366. int duplex;
  367. int speed;
  368. int link;
  369. struct work_struct restart_work;
  370. /*
  371. * Transmitting: Batch Mode.
  372. * 1 BD in 1 TxFD.
  373. * Receiving: Non-Packing Mode.
  374. * 1 circular FD for Free Buffer List.
  375. * RX_BUF_NUM BD in Free Buffer FD.
  376. * One Free Buffer BD has ETH_FRAME_LEN data buffer.
  377. */
  378. void *fd_buf; /* for TxFD, RxFD, FrFD */
  379. dma_addr_t fd_buf_dma;
  380. struct TxFD *tfd_base;
  381. unsigned int tfd_start;
  382. unsigned int tfd_end;
  383. struct RxFD *rfd_base;
  384. struct RxFD *rfd_limit;
  385. struct RxFD *rfd_cur;
  386. struct FrFD *fbl_ptr;
  387. unsigned int fbl_count;
  388. struct {
  389. struct sk_buff *skb;
  390. dma_addr_t skb_dma;
  391. } tx_skbs[TX_FD_NUM], rx_skbs[RX_BUF_NUM];
  392. u32 msg_enable;
  393. enum tc35815_chiptype chiptype;
  394. };
  395. static inline dma_addr_t fd_virt_to_bus(struct tc35815_local *lp, void *virt)
  396. {
  397. return lp->fd_buf_dma + ((u8 *)virt - (u8 *)lp->fd_buf);
  398. }
  399. #ifdef DEBUG
  400. static inline void *fd_bus_to_virt(struct tc35815_local *lp, dma_addr_t bus)
  401. {
  402. return (void *)((u8 *)lp->fd_buf + (bus - lp->fd_buf_dma));
  403. }
  404. #endif
  405. static struct sk_buff *alloc_rxbuf_skb(struct net_device *dev,
  406. struct pci_dev *hwdev,
  407. dma_addr_t *dma_handle)
  408. {
  409. struct sk_buff *skb;
  410. skb = dev_alloc_skb(RX_BUF_SIZE);
  411. if (!skb)
  412. return NULL;
  413. *dma_handle = pci_map_single(hwdev, skb->data, RX_BUF_SIZE,
  414. PCI_DMA_FROMDEVICE);
  415. if (pci_dma_mapping_error(hwdev, *dma_handle)) {
  416. dev_kfree_skb_any(skb);
  417. return NULL;
  418. }
  419. skb_reserve(skb, 2); /* make IP header 4byte aligned */
  420. return skb;
  421. }
  422. static void free_rxbuf_skb(struct pci_dev *hwdev, struct sk_buff *skb, dma_addr_t dma_handle)
  423. {
  424. pci_unmap_single(hwdev, dma_handle, RX_BUF_SIZE,
  425. PCI_DMA_FROMDEVICE);
  426. dev_kfree_skb_any(skb);
  427. }
  428. /* Index to functions, as function prototypes. */
  429. static int tc35815_open(struct net_device *dev);
  430. static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev);
  431. static irqreturn_t tc35815_interrupt(int irq, void *dev_id);
  432. static int tc35815_rx(struct net_device *dev, int limit);
  433. static int tc35815_poll(struct napi_struct *napi, int budget);
  434. static void tc35815_txdone(struct net_device *dev);
  435. static int tc35815_close(struct net_device *dev);
  436. static struct net_device_stats *tc35815_get_stats(struct net_device *dev);
  437. static void tc35815_set_multicast_list(struct net_device *dev);
  438. static void tc35815_tx_timeout(struct net_device *dev);
  439. static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  440. #ifdef CONFIG_NET_POLL_CONTROLLER
  441. static void tc35815_poll_controller(struct net_device *dev);
  442. #endif
  443. static const struct ethtool_ops tc35815_ethtool_ops;
  444. /* Example routines you must write ;->. */
  445. static void tc35815_chip_reset(struct net_device *dev);
  446. static void tc35815_chip_init(struct net_device *dev);
  447. #ifdef DEBUG
  448. static void panic_queues(struct net_device *dev);
  449. #endif
  450. static void tc35815_restart_work(struct work_struct *work);
  451. static int tc_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  452. {
  453. struct net_device *dev = bus->priv;
  454. struct tc35815_regs __iomem *tr =
  455. (struct tc35815_regs __iomem *)dev->base_addr;
  456. unsigned long timeout = jiffies + HZ;
  457. tc_writel(MD_CA_Busy | (mii_id << 5) | (regnum & 0x1f), &tr->MD_CA);
  458. udelay(12); /* it takes 32 x 400ns at least */
  459. while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
  460. if (time_after(jiffies, timeout))
  461. return -EIO;
  462. cpu_relax();
  463. }
  464. return tc_readl(&tr->MD_Data) & 0xffff;
  465. }
  466. static int tc_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 val)
  467. {
  468. struct net_device *dev = bus->priv;
  469. struct tc35815_regs __iomem *tr =
  470. (struct tc35815_regs __iomem *)dev->base_addr;
  471. unsigned long timeout = jiffies + HZ;
  472. tc_writel(val, &tr->MD_Data);
  473. tc_writel(MD_CA_Busy | MD_CA_Wr | (mii_id << 5) | (regnum & 0x1f),
  474. &tr->MD_CA);
  475. udelay(12); /* it takes 32 x 400ns at least */
  476. while (tc_readl(&tr->MD_CA) & MD_CA_Busy) {
  477. if (time_after(jiffies, timeout))
  478. return -EIO;
  479. cpu_relax();
  480. }
  481. return 0;
  482. }
  483. static void tc_handle_link_change(struct net_device *dev)
  484. {
  485. struct tc35815_local *lp = netdev_priv(dev);
  486. struct phy_device *phydev = lp->phy_dev;
  487. unsigned long flags;
  488. int status_change = 0;
  489. spin_lock_irqsave(&lp->lock, flags);
  490. if (phydev->link &&
  491. (lp->speed != phydev->speed || lp->duplex != phydev->duplex)) {
  492. struct tc35815_regs __iomem *tr =
  493. (struct tc35815_regs __iomem *)dev->base_addr;
  494. u32 reg;
  495. reg = tc_readl(&tr->MAC_Ctl);
  496. reg |= MAC_HaltReq;
  497. tc_writel(reg, &tr->MAC_Ctl);
  498. if (phydev->duplex == DUPLEX_FULL)
  499. reg |= MAC_FullDup;
  500. else
  501. reg &= ~MAC_FullDup;
  502. tc_writel(reg, &tr->MAC_Ctl);
  503. reg &= ~MAC_HaltReq;
  504. tc_writel(reg, &tr->MAC_Ctl);
  505. /*
  506. * TX4939 PCFG.SPEEDn bit will be changed on
  507. * NETDEV_CHANGE event.
  508. */
  509. /*
  510. * WORKAROUND: enable LostCrS only if half duplex
  511. * operation.
  512. * (TX4939 does not have EnLCarr)
  513. */
  514. if (phydev->duplex == DUPLEX_HALF &&
  515. lp->chiptype != TC35815_TX4939)
  516. tc_writel(tc_readl(&tr->Tx_Ctl) | Tx_EnLCarr,
  517. &tr->Tx_Ctl);
  518. lp->speed = phydev->speed;
  519. lp->duplex = phydev->duplex;
  520. status_change = 1;
  521. }
  522. if (phydev->link != lp->link) {
  523. if (phydev->link) {
  524. /* delayed promiscuous enabling */
  525. if (dev->flags & IFF_PROMISC)
  526. tc35815_set_multicast_list(dev);
  527. } else {
  528. lp->speed = 0;
  529. lp->duplex = -1;
  530. }
  531. lp->link = phydev->link;
  532. status_change = 1;
  533. }
  534. spin_unlock_irqrestore(&lp->lock, flags);
  535. if (status_change && netif_msg_link(lp)) {
  536. phy_print_status(phydev);
  537. pr_debug("%s: MII BMCR %04x BMSR %04x LPA %04x\n",
  538. dev->name,
  539. phy_read(phydev, MII_BMCR),
  540. phy_read(phydev, MII_BMSR),
  541. phy_read(phydev, MII_LPA));
  542. }
  543. }
  544. static int tc_mii_probe(struct net_device *dev)
  545. {
  546. struct tc35815_local *lp = netdev_priv(dev);
  547. struct phy_device *phydev = NULL;
  548. int phy_addr;
  549. u32 dropmask;
  550. /* find the first phy */
  551. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  552. if (lp->mii_bus->phy_map[phy_addr]) {
  553. if (phydev) {
  554. printk(KERN_ERR "%s: multiple PHYs found\n",
  555. dev->name);
  556. return -EINVAL;
  557. }
  558. phydev = lp->mii_bus->phy_map[phy_addr];
  559. break;
  560. }
  561. }
  562. if (!phydev) {
  563. printk(KERN_ERR "%s: no PHY found\n", dev->name);
  564. return -ENODEV;
  565. }
  566. /* attach the mac to the phy */
  567. phydev = phy_connect(dev, dev_name(&phydev->dev),
  568. &tc_handle_link_change, 0,
  569. lp->chiptype == TC35815_TX4939 ?
  570. PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII);
  571. if (IS_ERR(phydev)) {
  572. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  573. return PTR_ERR(phydev);
  574. }
  575. printk(KERN_INFO "%s: attached PHY driver [%s] "
  576. "(mii_bus:phy_addr=%s, id=%x)\n",
  577. dev->name, phydev->drv->name, dev_name(&phydev->dev),
  578. phydev->phy_id);
  579. /* mask with MAC supported features */
  580. phydev->supported &= PHY_BASIC_FEATURES;
  581. dropmask = 0;
  582. if (options.speed == 10)
  583. dropmask |= SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
  584. else if (options.speed == 100)
  585. dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full;
  586. if (options.duplex == 1)
  587. dropmask |= SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full;
  588. else if (options.duplex == 2)
  589. dropmask |= SUPPORTED_10baseT_Half | SUPPORTED_100baseT_Half;
  590. phydev->supported &= ~dropmask;
  591. phydev->advertising = phydev->supported;
  592. lp->link = 0;
  593. lp->speed = 0;
  594. lp->duplex = -1;
  595. lp->phy_dev = phydev;
  596. return 0;
  597. }
  598. static int tc_mii_init(struct net_device *dev)
  599. {
  600. struct tc35815_local *lp = netdev_priv(dev);
  601. int err;
  602. int i;
  603. lp->mii_bus = mdiobus_alloc();
  604. if (lp->mii_bus == NULL) {
  605. err = -ENOMEM;
  606. goto err_out;
  607. }
  608. lp->mii_bus->name = "tc35815_mii_bus";
  609. lp->mii_bus->read = tc_mdio_read;
  610. lp->mii_bus->write = tc_mdio_write;
  611. snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x",
  612. (lp->pci_dev->bus->number << 8) | lp->pci_dev->devfn);
  613. lp->mii_bus->priv = dev;
  614. lp->mii_bus->parent = &lp->pci_dev->dev;
  615. lp->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  616. if (!lp->mii_bus->irq) {
  617. err = -ENOMEM;
  618. goto err_out_free_mii_bus;
  619. }
  620. for (i = 0; i < PHY_MAX_ADDR; i++)
  621. lp->mii_bus->irq[i] = PHY_POLL;
  622. err = mdiobus_register(lp->mii_bus);
  623. if (err)
  624. goto err_out_free_mdio_irq;
  625. err = tc_mii_probe(dev);
  626. if (err)
  627. goto err_out_unregister_bus;
  628. return 0;
  629. err_out_unregister_bus:
  630. mdiobus_unregister(lp->mii_bus);
  631. err_out_free_mdio_irq:
  632. kfree(lp->mii_bus->irq);
  633. err_out_free_mii_bus:
  634. mdiobus_free(lp->mii_bus);
  635. err_out:
  636. return err;
  637. }
  638. #ifdef CONFIG_CPU_TX49XX
  639. /*
  640. * Find a platform_device providing a MAC address. The platform code
  641. * should provide a "tc35815-mac" device with a MAC address in its
  642. * platform_data.
  643. */
  644. static int __devinit tc35815_mac_match(struct device *dev, void *data)
  645. {
  646. struct platform_device *plat_dev = to_platform_device(dev);
  647. struct pci_dev *pci_dev = data;
  648. unsigned int id = pci_dev->irq;
  649. return !strcmp(plat_dev->name, "tc35815-mac") && plat_dev->id == id;
  650. }
  651. static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
  652. {
  653. struct tc35815_local *lp = netdev_priv(dev);
  654. struct device *pd = bus_find_device(&platform_bus_type, NULL,
  655. lp->pci_dev, tc35815_mac_match);
  656. if (pd) {
  657. if (pd->platform_data)
  658. memcpy(dev->dev_addr, pd->platform_data, ETH_ALEN);
  659. put_device(pd);
  660. return is_valid_ether_addr(dev->dev_addr) ? 0 : -ENODEV;
  661. }
  662. return -ENODEV;
  663. }
  664. #else
  665. static int __devinit tc35815_read_plat_dev_addr(struct net_device *dev)
  666. {
  667. return -ENODEV;
  668. }
  669. #endif
  670. static int __devinit tc35815_init_dev_addr(struct net_device *dev)
  671. {
  672. struct tc35815_regs __iomem *tr =
  673. (struct tc35815_regs __iomem *)dev->base_addr;
  674. int i;
  675. while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
  676. ;
  677. for (i = 0; i < 6; i += 2) {
  678. unsigned short data;
  679. tc_writel(PROM_Busy | PROM_Read | (i / 2 + 2), &tr->PROM_Ctl);
  680. while (tc_readl(&tr->PROM_Ctl) & PROM_Busy)
  681. ;
  682. data = tc_readl(&tr->PROM_Data);
  683. dev->dev_addr[i] = data & 0xff;
  684. dev->dev_addr[i+1] = data >> 8;
  685. }
  686. if (!is_valid_ether_addr(dev->dev_addr))
  687. return tc35815_read_plat_dev_addr(dev);
  688. return 0;
  689. }
  690. static const struct net_device_ops tc35815_netdev_ops = {
  691. .ndo_open = tc35815_open,
  692. .ndo_stop = tc35815_close,
  693. .ndo_start_xmit = tc35815_send_packet,
  694. .ndo_get_stats = tc35815_get_stats,
  695. .ndo_set_multicast_list = tc35815_set_multicast_list,
  696. .ndo_tx_timeout = tc35815_tx_timeout,
  697. .ndo_do_ioctl = tc35815_ioctl,
  698. .ndo_validate_addr = eth_validate_addr,
  699. .ndo_change_mtu = eth_change_mtu,
  700. .ndo_set_mac_address = eth_mac_addr,
  701. #ifdef CONFIG_NET_POLL_CONTROLLER
  702. .ndo_poll_controller = tc35815_poll_controller,
  703. #endif
  704. };
  705. static int __devinit tc35815_init_one(struct pci_dev *pdev,
  706. const struct pci_device_id *ent)
  707. {
  708. void __iomem *ioaddr = NULL;
  709. struct net_device *dev;
  710. struct tc35815_local *lp;
  711. int rc;
  712. static int printed_version;
  713. if (!printed_version++) {
  714. printk(version);
  715. dev_printk(KERN_DEBUG, &pdev->dev,
  716. "speed:%d duplex:%d\n",
  717. options.speed, options.duplex);
  718. }
  719. if (!pdev->irq) {
  720. dev_warn(&pdev->dev, "no IRQ assigned.\n");
  721. return -ENODEV;
  722. }
  723. /* dev zeroed in alloc_etherdev */
  724. dev = alloc_etherdev(sizeof(*lp));
  725. if (dev == NULL) {
  726. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  727. return -ENOMEM;
  728. }
  729. SET_NETDEV_DEV(dev, &pdev->dev);
  730. lp = netdev_priv(dev);
  731. lp->dev = dev;
  732. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  733. rc = pcim_enable_device(pdev);
  734. if (rc)
  735. goto err_out;
  736. rc = pcim_iomap_regions(pdev, 1 << 1, MODNAME);
  737. if (rc)
  738. goto err_out;
  739. pci_set_master(pdev);
  740. ioaddr = pcim_iomap_table(pdev)[1];
  741. /* Initialize the device structure. */
  742. dev->netdev_ops = &tc35815_netdev_ops;
  743. dev->ethtool_ops = &tc35815_ethtool_ops;
  744. dev->watchdog_timeo = TC35815_TX_TIMEOUT;
  745. netif_napi_add(dev, &lp->napi, tc35815_poll, NAPI_WEIGHT);
  746. dev->irq = pdev->irq;
  747. dev->base_addr = (unsigned long)ioaddr;
  748. INIT_WORK(&lp->restart_work, tc35815_restart_work);
  749. spin_lock_init(&lp->lock);
  750. spin_lock_init(&lp->rx_lock);
  751. lp->pci_dev = pdev;
  752. lp->chiptype = ent->driver_data;
  753. lp->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK;
  754. pci_set_drvdata(pdev, dev);
  755. /* Soft reset the chip. */
  756. tc35815_chip_reset(dev);
  757. /* Retrieve the ethernet address. */
  758. if (tc35815_init_dev_addr(dev)) {
  759. dev_warn(&pdev->dev, "not valid ether addr\n");
  760. random_ether_addr(dev->dev_addr);
  761. }
  762. rc = register_netdev(dev);
  763. if (rc)
  764. goto err_out;
  765. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  766. printk(KERN_INFO "%s: %s at 0x%lx, %pM, IRQ %d\n",
  767. dev->name,
  768. chip_info[ent->driver_data].name,
  769. dev->base_addr,
  770. dev->dev_addr,
  771. dev->irq);
  772. rc = tc_mii_init(dev);
  773. if (rc)
  774. goto err_out_unregister;
  775. return 0;
  776. err_out_unregister:
  777. unregister_netdev(dev);
  778. err_out:
  779. free_netdev(dev);
  780. return rc;
  781. }
  782. static void __devexit tc35815_remove_one(struct pci_dev *pdev)
  783. {
  784. struct net_device *dev = pci_get_drvdata(pdev);
  785. struct tc35815_local *lp = netdev_priv(dev);
  786. phy_disconnect(lp->phy_dev);
  787. mdiobus_unregister(lp->mii_bus);
  788. kfree(lp->mii_bus->irq);
  789. mdiobus_free(lp->mii_bus);
  790. unregister_netdev(dev);
  791. free_netdev(dev);
  792. pci_set_drvdata(pdev, NULL);
  793. }
  794. static int
  795. tc35815_init_queues(struct net_device *dev)
  796. {
  797. struct tc35815_local *lp = netdev_priv(dev);
  798. int i;
  799. unsigned long fd_addr;
  800. if (!lp->fd_buf) {
  801. BUG_ON(sizeof(struct FDesc) +
  802. sizeof(struct BDesc) * RX_BUF_NUM +
  803. sizeof(struct FDesc) * RX_FD_NUM +
  804. sizeof(struct TxFD) * TX_FD_NUM >
  805. PAGE_SIZE * FD_PAGE_NUM);
  806. lp->fd_buf = pci_alloc_consistent(lp->pci_dev,
  807. PAGE_SIZE * FD_PAGE_NUM,
  808. &lp->fd_buf_dma);
  809. if (!lp->fd_buf)
  810. return -ENOMEM;
  811. for (i = 0; i < RX_BUF_NUM; i++) {
  812. lp->rx_skbs[i].skb =
  813. alloc_rxbuf_skb(dev, lp->pci_dev,
  814. &lp->rx_skbs[i].skb_dma);
  815. if (!lp->rx_skbs[i].skb) {
  816. while (--i >= 0) {
  817. free_rxbuf_skb(lp->pci_dev,
  818. lp->rx_skbs[i].skb,
  819. lp->rx_skbs[i].skb_dma);
  820. lp->rx_skbs[i].skb = NULL;
  821. }
  822. pci_free_consistent(lp->pci_dev,
  823. PAGE_SIZE * FD_PAGE_NUM,
  824. lp->fd_buf,
  825. lp->fd_buf_dma);
  826. lp->fd_buf = NULL;
  827. return -ENOMEM;
  828. }
  829. }
  830. printk(KERN_DEBUG "%s: FD buf %p DataBuf",
  831. dev->name, lp->fd_buf);
  832. printk("\n");
  833. } else {
  834. for (i = 0; i < FD_PAGE_NUM; i++)
  835. clear_page((void *)((unsigned long)lp->fd_buf +
  836. i * PAGE_SIZE));
  837. }
  838. fd_addr = (unsigned long)lp->fd_buf;
  839. /* Free Descriptors (for Receive) */
  840. lp->rfd_base = (struct RxFD *)fd_addr;
  841. fd_addr += sizeof(struct RxFD) * RX_FD_NUM;
  842. for (i = 0; i < RX_FD_NUM; i++)
  843. lp->rfd_base[i].fd.FDCtl = cpu_to_le32(FD_CownsFD);
  844. lp->rfd_cur = lp->rfd_base;
  845. lp->rfd_limit = (struct RxFD *)fd_addr - (RX_FD_RESERVE + 1);
  846. /* Transmit Descriptors */
  847. lp->tfd_base = (struct TxFD *)fd_addr;
  848. fd_addr += sizeof(struct TxFD) * TX_FD_NUM;
  849. for (i = 0; i < TX_FD_NUM; i++) {
  850. lp->tfd_base[i].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[i+1]));
  851. lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
  852. lp->tfd_base[i].fd.FDCtl = cpu_to_le32(0);
  853. }
  854. lp->tfd_base[TX_FD_NUM-1].fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, &lp->tfd_base[0]));
  855. lp->tfd_start = 0;
  856. lp->tfd_end = 0;
  857. /* Buffer List (for Receive) */
  858. lp->fbl_ptr = (struct FrFD *)fd_addr;
  859. lp->fbl_ptr->fd.FDNext = cpu_to_le32(fd_virt_to_bus(lp, lp->fbl_ptr));
  860. lp->fbl_ptr->fd.FDCtl = cpu_to_le32(RX_BUF_NUM | FD_CownsFD);
  861. /*
  862. * move all allocated skbs to head of rx_skbs[] array.
  863. * fbl_count mighe not be RX_BUF_NUM if alloc_rxbuf_skb() in
  864. * tc35815_rx() had failed.
  865. */
  866. lp->fbl_count = 0;
  867. for (i = 0; i < RX_BUF_NUM; i++) {
  868. if (lp->rx_skbs[i].skb) {
  869. if (i != lp->fbl_count) {
  870. lp->rx_skbs[lp->fbl_count].skb =
  871. lp->rx_skbs[i].skb;
  872. lp->rx_skbs[lp->fbl_count].skb_dma =
  873. lp->rx_skbs[i].skb_dma;
  874. }
  875. lp->fbl_count++;
  876. }
  877. }
  878. for (i = 0; i < RX_BUF_NUM; i++) {
  879. if (i >= lp->fbl_count) {
  880. lp->fbl_ptr->bd[i].BuffData = 0;
  881. lp->fbl_ptr->bd[i].BDCtl = 0;
  882. continue;
  883. }
  884. lp->fbl_ptr->bd[i].BuffData =
  885. cpu_to_le32(lp->rx_skbs[i].skb_dma);
  886. /* BDID is index of FrFD.bd[] */
  887. lp->fbl_ptr->bd[i].BDCtl =
  888. cpu_to_le32(BD_CownsBD | (i << BD_RxBDID_SHIFT) |
  889. RX_BUF_SIZE);
  890. }
  891. printk(KERN_DEBUG "%s: TxFD %p RxFD %p FrFD %p\n",
  892. dev->name, lp->tfd_base, lp->rfd_base, lp->fbl_ptr);
  893. return 0;
  894. }
  895. static void
  896. tc35815_clear_queues(struct net_device *dev)
  897. {
  898. struct tc35815_local *lp = netdev_priv(dev);
  899. int i;
  900. for (i = 0; i < TX_FD_NUM; i++) {
  901. u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
  902. struct sk_buff *skb =
  903. fdsystem != 0xffffffff ?
  904. lp->tx_skbs[fdsystem].skb : NULL;
  905. #ifdef DEBUG
  906. if (lp->tx_skbs[i].skb != skb) {
  907. printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
  908. panic_queues(dev);
  909. }
  910. #else
  911. BUG_ON(lp->tx_skbs[i].skb != skb);
  912. #endif
  913. if (skb) {
  914. pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
  915. lp->tx_skbs[i].skb = NULL;
  916. lp->tx_skbs[i].skb_dma = 0;
  917. dev_kfree_skb_any(skb);
  918. }
  919. lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
  920. }
  921. tc35815_init_queues(dev);
  922. }
  923. static void
  924. tc35815_free_queues(struct net_device *dev)
  925. {
  926. struct tc35815_local *lp = netdev_priv(dev);
  927. int i;
  928. if (lp->tfd_base) {
  929. for (i = 0; i < TX_FD_NUM; i++) {
  930. u32 fdsystem = le32_to_cpu(lp->tfd_base[i].fd.FDSystem);
  931. struct sk_buff *skb =
  932. fdsystem != 0xffffffff ?
  933. lp->tx_skbs[fdsystem].skb : NULL;
  934. #ifdef DEBUG
  935. if (lp->tx_skbs[i].skb != skb) {
  936. printk("%s: tx_skbs mismatch(%d).\n", dev->name, i);
  937. panic_queues(dev);
  938. }
  939. #else
  940. BUG_ON(lp->tx_skbs[i].skb != skb);
  941. #endif
  942. if (skb) {
  943. dev_kfree_skb(skb);
  944. pci_unmap_single(lp->pci_dev, lp->tx_skbs[i].skb_dma, skb->len, PCI_DMA_TODEVICE);
  945. lp->tx_skbs[i].skb = NULL;
  946. lp->tx_skbs[i].skb_dma = 0;
  947. }
  948. lp->tfd_base[i].fd.FDSystem = cpu_to_le32(0xffffffff);
  949. }
  950. }
  951. lp->rfd_base = NULL;
  952. lp->rfd_limit = NULL;
  953. lp->rfd_cur = NULL;
  954. lp->fbl_ptr = NULL;
  955. for (i = 0; i < RX_BUF_NUM; i++) {
  956. if (lp->rx_skbs[i].skb) {
  957. free_rxbuf_skb(lp->pci_dev, lp->rx_skbs[i].skb,
  958. lp->rx_skbs[i].skb_dma);
  959. lp->rx_skbs[i].skb = NULL;
  960. }
  961. }
  962. if (lp->fd_buf) {
  963. pci_free_consistent(lp->pci_dev, PAGE_SIZE * FD_PAGE_NUM,
  964. lp->fd_buf, lp->fd_buf_dma);
  965. lp->fd_buf = NULL;
  966. }
  967. }
  968. static void
  969. dump_txfd(struct TxFD *fd)
  970. {
  971. printk("TxFD(%p): %08x %08x %08x %08x\n", fd,
  972. le32_to_cpu(fd->fd.FDNext),
  973. le32_to_cpu(fd->fd.FDSystem),
  974. le32_to_cpu(fd->fd.FDStat),
  975. le32_to_cpu(fd->fd.FDCtl));
  976. printk("BD: ");
  977. printk(" %08x %08x",
  978. le32_to_cpu(fd->bd.BuffData),
  979. le32_to_cpu(fd->bd.BDCtl));
  980. printk("\n");
  981. }
  982. static int
  983. dump_rxfd(struct RxFD *fd)
  984. {
  985. int i, bd_count = (le32_to_cpu(fd->fd.FDCtl) & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
  986. if (bd_count > 8)
  987. bd_count = 8;
  988. printk("RxFD(%p): %08x %08x %08x %08x\n", fd,
  989. le32_to_cpu(fd->fd.FDNext),
  990. le32_to_cpu(fd->fd.FDSystem),
  991. le32_to_cpu(fd->fd.FDStat),
  992. le32_to_cpu(fd->fd.FDCtl));
  993. if (le32_to_cpu(fd->fd.FDCtl) & FD_CownsFD)
  994. return 0;
  995. printk("BD: ");
  996. for (i = 0; i < bd_count; i++)
  997. printk(" %08x %08x",
  998. le32_to_cpu(fd->bd[i].BuffData),
  999. le32_to_cpu(fd->bd[i].BDCtl));
  1000. printk("\n");
  1001. return bd_count;
  1002. }
  1003. #ifdef DEBUG
  1004. static void
  1005. dump_frfd(struct FrFD *fd)
  1006. {
  1007. int i;
  1008. printk("FrFD(%p): %08x %08x %08x %08x\n", fd,
  1009. le32_to_cpu(fd->fd.FDNext),
  1010. le32_to_cpu(fd->fd.FDSystem),
  1011. le32_to_cpu(fd->fd.FDStat),
  1012. le32_to_cpu(fd->fd.FDCtl));
  1013. printk("BD: ");
  1014. for (i = 0; i < RX_BUF_NUM; i++)
  1015. printk(" %08x %08x",
  1016. le32_to_cpu(fd->bd[i].BuffData),
  1017. le32_to_cpu(fd->bd[i].BDCtl));
  1018. printk("\n");
  1019. }
  1020. static void
  1021. panic_queues(struct net_device *dev)
  1022. {
  1023. struct tc35815_local *lp = netdev_priv(dev);
  1024. int i;
  1025. printk("TxFD base %p, start %u, end %u\n",
  1026. lp->tfd_base, lp->tfd_start, lp->tfd_end);
  1027. printk("RxFD base %p limit %p cur %p\n",
  1028. lp->rfd_base, lp->rfd_limit, lp->rfd_cur);
  1029. printk("FrFD %p\n", lp->fbl_ptr);
  1030. for (i = 0; i < TX_FD_NUM; i++)
  1031. dump_txfd(&lp->tfd_base[i]);
  1032. for (i = 0; i < RX_FD_NUM; i++) {
  1033. int bd_count = dump_rxfd(&lp->rfd_base[i]);
  1034. i += (bd_count + 1) / 2; /* skip BDs */
  1035. }
  1036. dump_frfd(lp->fbl_ptr);
  1037. panic("%s: Illegal queue state.", dev->name);
  1038. }
  1039. #endif
  1040. static void print_eth(const u8 *add)
  1041. {
  1042. printk(KERN_DEBUG "print_eth(%p)\n", add);
  1043. printk(KERN_DEBUG " %pM => %pM : %02x%02x\n",
  1044. add + 6, add, add[12], add[13]);
  1045. }
  1046. static int tc35815_tx_full(struct net_device *dev)
  1047. {
  1048. struct tc35815_local *lp = netdev_priv(dev);
  1049. return (lp->tfd_start + 1) % TX_FD_NUM == lp->tfd_end;
  1050. }
  1051. static void tc35815_restart(struct net_device *dev)
  1052. {
  1053. struct tc35815_local *lp = netdev_priv(dev);
  1054. if (lp->phy_dev) {
  1055. int timeout;
  1056. phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET);
  1057. timeout = 100;
  1058. while (--timeout) {
  1059. if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET))
  1060. break;
  1061. udelay(1);
  1062. }
  1063. if (!timeout)
  1064. printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
  1065. }
  1066. spin_lock_bh(&lp->rx_lock);
  1067. spin_lock_irq(&lp->lock);
  1068. tc35815_chip_reset(dev);
  1069. tc35815_clear_queues(dev);
  1070. tc35815_chip_init(dev);
  1071. /* Reconfigure CAM again since tc35815_chip_init() initialize it. */
  1072. tc35815_set_multicast_list(dev);
  1073. spin_unlock_irq(&lp->lock);
  1074. spin_unlock_bh(&lp->rx_lock);
  1075. netif_wake_queue(dev);
  1076. }
  1077. static void tc35815_restart_work(struct work_struct *work)
  1078. {
  1079. struct tc35815_local *lp =
  1080. container_of(work, struct tc35815_local, restart_work);
  1081. struct net_device *dev = lp->dev;
  1082. tc35815_restart(dev);
  1083. }
  1084. static void tc35815_schedule_restart(struct net_device *dev)
  1085. {
  1086. struct tc35815_local *lp = netdev_priv(dev);
  1087. struct tc35815_regs __iomem *tr =
  1088. (struct tc35815_regs __iomem *)dev->base_addr;
  1089. unsigned long flags;
  1090. /* disable interrupts */
  1091. spin_lock_irqsave(&lp->lock, flags);
  1092. tc_writel(0, &tr->Int_En);
  1093. tc_writel(tc_readl(&tr->DMA_Ctl) | DMA_IntMask, &tr->DMA_Ctl);
  1094. schedule_work(&lp->restart_work);
  1095. spin_unlock_irqrestore(&lp->lock, flags);
  1096. }
  1097. static void tc35815_tx_timeout(struct net_device *dev)
  1098. {
  1099. struct tc35815_regs __iomem *tr =
  1100. (struct tc35815_regs __iomem *)dev->base_addr;
  1101. printk(KERN_WARNING "%s: transmit timed out, status %#x\n",
  1102. dev->name, tc_readl(&tr->Tx_Stat));
  1103. /* Try to restart the adaptor. */
  1104. tc35815_schedule_restart(dev);
  1105. dev->stats.tx_errors++;
  1106. }
  1107. /*
  1108. * Open/initialize the controller. This is called (in the current kernel)
  1109. * sometime after booting when the 'ifconfig' program is run.
  1110. *
  1111. * This routine should set everything up anew at each open, even
  1112. * registers that "should" only need to be set once at boot, so that
  1113. * there is non-reboot way to recover if something goes wrong.
  1114. */
  1115. static int
  1116. tc35815_open(struct net_device *dev)
  1117. {
  1118. struct tc35815_local *lp = netdev_priv(dev);
  1119. /*
  1120. * This is used if the interrupt line can turned off (shared).
  1121. * See 3c503.c for an example of selecting the IRQ at config-time.
  1122. */
  1123. if (request_irq(dev->irq, tc35815_interrupt, IRQF_SHARED,
  1124. dev->name, dev))
  1125. return -EAGAIN;
  1126. tc35815_chip_reset(dev);
  1127. if (tc35815_init_queues(dev) != 0) {
  1128. free_irq(dev->irq, dev);
  1129. return -EAGAIN;
  1130. }
  1131. napi_enable(&lp->napi);
  1132. /* Reset the hardware here. Don't forget to set the station address. */
  1133. spin_lock_irq(&lp->lock);
  1134. tc35815_chip_init(dev);
  1135. spin_unlock_irq(&lp->lock);
  1136. netif_carrier_off(dev);
  1137. /* schedule a link state check */
  1138. phy_start(lp->phy_dev);
  1139. /* We are now ready to accept transmit requeusts from
  1140. * the queueing layer of the networking.
  1141. */
  1142. netif_start_queue(dev);
  1143. return 0;
  1144. }
  1145. /* This will only be invoked if your driver is _not_ in XOFF state.
  1146. * What this means is that you need not check it, and that this
  1147. * invariant will hold if you make sure that the netif_*_queue()
  1148. * calls are done at the proper times.
  1149. */
  1150. static int tc35815_send_packet(struct sk_buff *skb, struct net_device *dev)
  1151. {
  1152. struct tc35815_local *lp = netdev_priv(dev);
  1153. struct TxFD *txfd;
  1154. unsigned long flags;
  1155. /* If some error occurs while trying to transmit this
  1156. * packet, you should return '1' from this function.
  1157. * In such a case you _may not_ do anything to the
  1158. * SKB, it is still owned by the network queueing
  1159. * layer when an error is returned. This means you
  1160. * may not modify any SKB fields, you may not free
  1161. * the SKB, etc.
  1162. */
  1163. /* This is the most common case for modern hardware.
  1164. * The spinlock protects this code from the TX complete
  1165. * hardware interrupt handler. Queue flow control is
  1166. * thus managed under this lock as well.
  1167. */
  1168. spin_lock_irqsave(&lp->lock, flags);
  1169. /* failsafe... (handle txdone now if half of FDs are used) */
  1170. if ((lp->tfd_start + TX_FD_NUM - lp->tfd_end) % TX_FD_NUM >
  1171. TX_FD_NUM / 2)
  1172. tc35815_txdone(dev);
  1173. if (netif_msg_pktdata(lp))
  1174. print_eth(skb->data);
  1175. #ifdef DEBUG
  1176. if (lp->tx_skbs[lp->tfd_start].skb) {
  1177. printk("%s: tx_skbs conflict.\n", dev->name);
  1178. panic_queues(dev);
  1179. }
  1180. #else
  1181. BUG_ON(lp->tx_skbs[lp->tfd_start].skb);
  1182. #endif
  1183. lp->tx_skbs[lp->tfd_start].skb = skb;
  1184. lp->tx_skbs[lp->tfd_start].skb_dma = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
  1185. /*add to ring */
  1186. txfd = &lp->tfd_base[lp->tfd_start];
  1187. txfd->bd.BuffData = cpu_to_le32(lp->tx_skbs[lp->tfd_start].skb_dma);
  1188. txfd->bd.BDCtl = cpu_to_le32(skb->len);
  1189. txfd->fd.FDSystem = cpu_to_le32(lp->tfd_start);
  1190. txfd->fd.FDCtl = cpu_to_le32(FD_CownsFD | (1 << FD_BDCnt_SHIFT));
  1191. if (lp->tfd_start == lp->tfd_end) {
  1192. struct tc35815_regs __iomem *tr =
  1193. (struct tc35815_regs __iomem *)dev->base_addr;
  1194. /* Start DMA Transmitter. */
  1195. txfd->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
  1196. txfd->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
  1197. if (netif_msg_tx_queued(lp)) {
  1198. printk("%s: starting TxFD.\n", dev->name);
  1199. dump_txfd(txfd);
  1200. }
  1201. tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
  1202. } else {
  1203. txfd->fd.FDNext &= cpu_to_le32(~FD_Next_EOL);
  1204. if (netif_msg_tx_queued(lp)) {
  1205. printk("%s: queueing TxFD.\n", dev->name);
  1206. dump_txfd(txfd);
  1207. }
  1208. }
  1209. lp->tfd_start = (lp->tfd_start + 1) % TX_FD_NUM;
  1210. /* If we just used up the very last entry in the
  1211. * TX ring on this device, tell the queueing
  1212. * layer to send no more.
  1213. */
  1214. if (tc35815_tx_full(dev)) {
  1215. if (netif_msg_tx_queued(lp))
  1216. printk(KERN_WARNING "%s: TxFD Exhausted.\n", dev->name);
  1217. netif_stop_queue(dev);
  1218. }
  1219. /* When the TX completion hw interrupt arrives, this
  1220. * is when the transmit statistics are updated.
  1221. */
  1222. spin_unlock_irqrestore(&lp->lock, flags);
  1223. return NETDEV_TX_OK;
  1224. }
  1225. #define FATAL_ERROR_INT \
  1226. (Int_IntPCI | Int_DmParErr | Int_IntNRAbt)
  1227. static void tc35815_fatal_error_interrupt(struct net_device *dev, u32 status)
  1228. {
  1229. static int count;
  1230. printk(KERN_WARNING "%s: Fatal Error Intterrupt (%#x):",
  1231. dev->name, status);
  1232. if (status & Int_IntPCI)
  1233. printk(" IntPCI");
  1234. if (status & Int_DmParErr)
  1235. printk(" DmParErr");
  1236. if (status & Int_IntNRAbt)
  1237. printk(" IntNRAbt");
  1238. printk("\n");
  1239. if (count++ > 100)
  1240. panic("%s: Too many fatal errors.", dev->name);
  1241. printk(KERN_WARNING "%s: Resetting ...\n", dev->name);
  1242. /* Try to restart the adaptor. */
  1243. tc35815_schedule_restart(dev);
  1244. }
  1245. static int tc35815_do_interrupt(struct net_device *dev, u32 status, int limit)
  1246. {
  1247. struct tc35815_local *lp = netdev_priv(dev);
  1248. int ret = -1;
  1249. /* Fatal errors... */
  1250. if (status & FATAL_ERROR_INT) {
  1251. tc35815_fatal_error_interrupt(dev, status);
  1252. return 0;
  1253. }
  1254. /* recoverable errors */
  1255. if (status & Int_IntFDAEx) {
  1256. if (netif_msg_rx_err(lp))
  1257. dev_warn(&dev->dev,
  1258. "Free Descriptor Area Exhausted (%#x).\n",
  1259. status);
  1260. dev->stats.rx_dropped++;
  1261. ret = 0;
  1262. }
  1263. if (status & Int_IntBLEx) {
  1264. if (netif_msg_rx_err(lp))
  1265. dev_warn(&dev->dev,
  1266. "Buffer List Exhausted (%#x).\n",
  1267. status);
  1268. dev->stats.rx_dropped++;
  1269. ret = 0;
  1270. }
  1271. if (status & Int_IntExBD) {
  1272. if (netif_msg_rx_err(lp))
  1273. dev_warn(&dev->dev,
  1274. "Excessive Buffer Descriptiors (%#x).\n",
  1275. status);
  1276. dev->stats.rx_length_errors++;
  1277. ret = 0;
  1278. }
  1279. /* normal notification */
  1280. if (status & Int_IntMacRx) {
  1281. /* Got a packet(s). */
  1282. ret = tc35815_rx(dev, limit);
  1283. lp->lstats.rx_ints++;
  1284. }
  1285. if (status & Int_IntMacTx) {
  1286. /* Transmit complete. */
  1287. lp->lstats.tx_ints++;
  1288. spin_lock_irq(&lp->lock);
  1289. tc35815_txdone(dev);
  1290. spin_unlock_irq(&lp->lock);
  1291. if (ret < 0)
  1292. ret = 0;
  1293. }
  1294. return ret;
  1295. }
  1296. /*
  1297. * The typical workload of the driver:
  1298. * Handle the network interface interrupts.
  1299. */
  1300. static irqreturn_t tc35815_interrupt(int irq, void *dev_id)
  1301. {
  1302. struct net_device *dev = dev_id;
  1303. struct tc35815_local *lp = netdev_priv(dev);
  1304. struct tc35815_regs __iomem *tr =
  1305. (struct tc35815_regs __iomem *)dev->base_addr;
  1306. u32 dmactl = tc_readl(&tr->DMA_Ctl);
  1307. if (!(dmactl & DMA_IntMask)) {
  1308. /* disable interrupts */
  1309. tc_writel(dmactl | DMA_IntMask, &tr->DMA_Ctl);
  1310. if (napi_schedule_prep(&lp->napi))
  1311. __napi_schedule(&lp->napi);
  1312. else {
  1313. printk(KERN_ERR "%s: interrupt taken in poll\n",
  1314. dev->name);
  1315. BUG();
  1316. }
  1317. (void)tc_readl(&tr->Int_Src); /* flush */
  1318. return IRQ_HANDLED;
  1319. }
  1320. return IRQ_NONE;
  1321. }
  1322. #ifdef CONFIG_NET_POLL_CONTROLLER
  1323. static void tc35815_poll_controller(struct net_device *dev)
  1324. {
  1325. disable_irq(dev->irq);
  1326. tc35815_interrupt(dev->irq, dev);
  1327. enable_irq(dev->irq);
  1328. }
  1329. #endif
  1330. /* We have a good packet(s), get it/them out of the buffers. */
  1331. static int
  1332. tc35815_rx(struct net_device *dev, int limit)
  1333. {
  1334. struct tc35815_local *lp = netdev_priv(dev);
  1335. unsigned int fdctl;
  1336. int i;
  1337. int received = 0;
  1338. while (!((fdctl = le32_to_cpu(lp->rfd_cur->fd.FDCtl)) & FD_CownsFD)) {
  1339. int status = le32_to_cpu(lp->rfd_cur->fd.FDStat);
  1340. int pkt_len = fdctl & FD_FDLength_MASK;
  1341. int bd_count = (fdctl & FD_BDCnt_MASK) >> FD_BDCnt_SHIFT;
  1342. #ifdef DEBUG
  1343. struct RxFD *next_rfd;
  1344. #endif
  1345. #if (RX_CTL_CMD & Rx_StripCRC) == 0
  1346. pkt_len -= ETH_FCS_LEN;
  1347. #endif
  1348. if (netif_msg_rx_status(lp))
  1349. dump_rxfd(lp->rfd_cur);
  1350. if (status & Rx_Good) {
  1351. struct sk_buff *skb;
  1352. unsigned char *data;
  1353. int cur_bd;
  1354. if (--limit < 0)
  1355. break;
  1356. BUG_ON(bd_count > 1);
  1357. cur_bd = (le32_to_cpu(lp->rfd_cur->bd[0].BDCtl)
  1358. & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
  1359. #ifdef DEBUG
  1360. if (cur_bd >= RX_BUF_NUM) {
  1361. printk("%s: invalid BDID.\n", dev->name);
  1362. panic_queues(dev);
  1363. }
  1364. BUG_ON(lp->rx_skbs[cur_bd].skb_dma !=
  1365. (le32_to_cpu(lp->rfd_cur->bd[0].BuffData) & ~3));
  1366. if (!lp->rx_skbs[cur_bd].skb) {
  1367. printk("%s: NULL skb.\n", dev->name);
  1368. panic_queues(dev);
  1369. }
  1370. #else
  1371. BUG_ON(cur_bd >= RX_BUF_NUM);
  1372. #endif
  1373. skb = lp->rx_skbs[cur_bd].skb;
  1374. prefetch(skb->data);
  1375. lp->rx_skbs[cur_bd].skb = NULL;
  1376. pci_unmap_single(lp->pci_dev,
  1377. lp->rx_skbs[cur_bd].skb_dma,
  1378. RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  1379. if (!HAVE_DMA_RXALIGN(lp) && NET_IP_ALIGN)
  1380. memmove(skb->data, skb->data - NET_IP_ALIGN,
  1381. pkt_len);
  1382. data = skb_put(skb, pkt_len);
  1383. if (netif_msg_pktdata(lp))
  1384. print_eth(data);
  1385. skb->protocol = eth_type_trans(skb, dev);
  1386. netif_receive_skb(skb);
  1387. received++;
  1388. dev->stats.rx_packets++;
  1389. dev->stats.rx_bytes += pkt_len;
  1390. } else {
  1391. dev->stats.rx_errors++;
  1392. if (netif_msg_rx_err(lp))
  1393. dev_info(&dev->dev, "Rx error (status %x)\n",
  1394. status & Rx_Stat_Mask);
  1395. /* WORKAROUND: LongErr and CRCErr means Overflow. */
  1396. if ((status & Rx_LongErr) && (status & Rx_CRCErr)) {
  1397. status &= ~(Rx_LongErr|Rx_CRCErr);
  1398. status |= Rx_Over;
  1399. }
  1400. if (status & Rx_LongErr)
  1401. dev->stats.rx_length_errors++;
  1402. if (status & Rx_Over)
  1403. dev->stats.rx_fifo_errors++;
  1404. if (status & Rx_CRCErr)
  1405. dev->stats.rx_crc_errors++;
  1406. if (status & Rx_Align)
  1407. dev->stats.rx_frame_errors++;
  1408. }
  1409. if (bd_count > 0) {
  1410. /* put Free Buffer back to controller */
  1411. int bdctl = le32_to_cpu(lp->rfd_cur->bd[bd_count - 1].BDCtl);
  1412. unsigned char id =
  1413. (bdctl & BD_RxBDID_MASK) >> BD_RxBDID_SHIFT;
  1414. #ifdef DEBUG
  1415. if (id >= RX_BUF_NUM) {
  1416. printk("%s: invalid BDID.\n", dev->name);
  1417. panic_queues(dev);
  1418. }
  1419. #else
  1420. BUG_ON(id >= RX_BUF_NUM);
  1421. #endif
  1422. /* free old buffers */
  1423. lp->fbl_count--;
  1424. while (lp->fbl_count < RX_BUF_NUM)
  1425. {
  1426. unsigned char curid =
  1427. (id + 1 + lp->fbl_count) % RX_BUF_NUM;
  1428. struct BDesc *bd = &lp->fbl_ptr->bd[curid];
  1429. #ifdef DEBUG
  1430. bdctl = le32_to_cpu(bd->BDCtl);
  1431. if (bdctl & BD_CownsBD) {
  1432. printk("%s: Freeing invalid BD.\n",
  1433. dev->name);
  1434. panic_queues(dev);
  1435. }
  1436. #endif
  1437. /* pass BD to controller */
  1438. if (!lp->rx_skbs[curid].skb) {
  1439. lp->rx_skbs[curid].skb =
  1440. alloc_rxbuf_skb(dev,
  1441. lp->pci_dev,
  1442. &lp->rx_skbs[curid].skb_dma);
  1443. if (!lp->rx_skbs[curid].skb)
  1444. break; /* try on next reception */
  1445. bd->BuffData = cpu_to_le32(lp->rx_skbs[curid].skb_dma);
  1446. }
  1447. /* Note: BDLength was modified by chip. */
  1448. bd->BDCtl = cpu_to_le32(BD_CownsBD |
  1449. (curid << BD_RxBDID_SHIFT) |
  1450. RX_BUF_SIZE);
  1451. lp->fbl_count++;
  1452. }
  1453. }
  1454. /* put RxFD back to controller */
  1455. #ifdef DEBUG
  1456. next_rfd = fd_bus_to_virt(lp,
  1457. le32_to_cpu(lp->rfd_cur->fd.FDNext));
  1458. if (next_rfd < lp->rfd_base || next_rfd > lp->rfd_limit) {
  1459. printk("%s: RxFD FDNext invalid.\n", dev->name);
  1460. panic_queues(dev);
  1461. }
  1462. #endif
  1463. for (i = 0; i < (bd_count + 1) / 2 + 1; i++) {
  1464. /* pass FD to controller */
  1465. #ifdef DEBUG
  1466. lp->rfd_cur->fd.FDNext = cpu_to_le32(0xdeaddead);
  1467. #else
  1468. lp->rfd_cur->fd.FDNext = cpu_to_le32(FD_Next_EOL);
  1469. #endif
  1470. lp->rfd_cur->fd.FDCtl = cpu_to_le32(FD_CownsFD);
  1471. lp->rfd_cur++;
  1472. }
  1473. if (lp->rfd_cur > lp->rfd_limit)
  1474. lp->rfd_cur = lp->rfd_base;
  1475. #ifdef DEBUG
  1476. if (lp->rfd_cur != next_rfd)
  1477. printk("rfd_cur = %p, next_rfd %p\n",
  1478. lp->rfd_cur, next_rfd);
  1479. #endif
  1480. }
  1481. return received;
  1482. }
  1483. static int tc35815_poll(struct napi_struct *napi, int budget)
  1484. {
  1485. struct tc35815_local *lp = container_of(napi, struct tc35815_local, napi);
  1486. struct net_device *dev = lp->dev;
  1487. struct tc35815_regs __iomem *tr =
  1488. (struct tc35815_regs __iomem *)dev->base_addr;
  1489. int received = 0, handled;
  1490. u32 status;
  1491. spin_lock(&lp->rx_lock);
  1492. status = tc_readl(&tr->Int_Src);
  1493. do {
  1494. /* BLEx, FDAEx will be cleared later */
  1495. tc_writel(status & ~(Int_BLEx | Int_FDAEx),
  1496. &tr->Int_Src); /* write to clear */
  1497. handled = tc35815_do_interrupt(dev, status, budget - received);
  1498. if (status & (Int_BLEx | Int_FDAEx))
  1499. tc_writel(status & (Int_BLEx | Int_FDAEx),
  1500. &tr->Int_Src);
  1501. if (handled >= 0) {
  1502. received += handled;
  1503. if (received >= budget)
  1504. break;
  1505. }
  1506. status = tc_readl(&tr->Int_Src);
  1507. } while (status);
  1508. spin_unlock(&lp->rx_lock);
  1509. if (received < budget) {
  1510. napi_complete(napi);
  1511. /* enable interrupts */
  1512. tc_writel(tc_readl(&tr->DMA_Ctl) & ~DMA_IntMask, &tr->DMA_Ctl);
  1513. }
  1514. return received;
  1515. }
  1516. #define TX_STA_ERR (Tx_ExColl|Tx_Under|Tx_Defer|Tx_NCarr|Tx_LateColl|Tx_TxPar|Tx_SQErr)
  1517. static void
  1518. tc35815_check_tx_stat(struct net_device *dev, int status)
  1519. {
  1520. struct tc35815_local *lp = netdev_priv(dev);
  1521. const char *msg = NULL;
  1522. /* count collisions */
  1523. if (status & Tx_ExColl)
  1524. dev->stats.collisions += 16;
  1525. if (status & Tx_TxColl_MASK)
  1526. dev->stats.collisions += status & Tx_TxColl_MASK;
  1527. /* TX4939 does not have NCarr */
  1528. if (lp->chiptype == TC35815_TX4939)
  1529. status &= ~Tx_NCarr;
  1530. /* WORKAROUND: ignore LostCrS in full duplex operation */
  1531. if (!lp->link || lp->duplex == DUPLEX_FULL)
  1532. status &= ~Tx_NCarr;
  1533. if (!(status & TX_STA_ERR)) {
  1534. /* no error. */
  1535. dev->stats.tx_packets++;
  1536. return;
  1537. }
  1538. dev->stats.tx_errors++;
  1539. if (status & Tx_ExColl) {
  1540. dev->stats.tx_aborted_errors++;
  1541. msg = "Excessive Collision.";
  1542. }
  1543. if (status & Tx_Under) {
  1544. dev->stats.tx_fifo_errors++;
  1545. msg = "Tx FIFO Underrun.";
  1546. if (lp->lstats.tx_underrun < TX_THRESHOLD_KEEP_LIMIT) {
  1547. lp->lstats.tx_underrun++;
  1548. if (lp->lstats.tx_underrun >= TX_THRESHOLD_KEEP_LIMIT) {
  1549. struct tc35815_regs __iomem *tr =
  1550. (struct tc35815_regs __iomem *)dev->base_addr;
  1551. tc_writel(TX_THRESHOLD_MAX, &tr->TxThrsh);
  1552. msg = "Tx FIFO Underrun.Change Tx threshold to max.";
  1553. }
  1554. }
  1555. }
  1556. if (status & Tx_Defer) {
  1557. dev->stats.tx_fifo_errors++;
  1558. msg = "Excessive Deferral.";
  1559. }
  1560. if (status & Tx_NCarr) {
  1561. dev->stats.tx_carrier_errors++;
  1562. msg = "Lost Carrier Sense.";
  1563. }
  1564. if (status & Tx_LateColl) {
  1565. dev->stats.tx_aborted_errors++;
  1566. msg = "Late Collision.";
  1567. }
  1568. if (status & Tx_TxPar) {
  1569. dev->stats.tx_fifo_errors++;
  1570. msg = "Transmit Parity Error.";
  1571. }
  1572. if (status & Tx_SQErr) {
  1573. dev->stats.tx_heartbeat_errors++;
  1574. msg = "Signal Quality Error.";
  1575. }
  1576. if (msg && netif_msg_tx_err(lp))
  1577. printk(KERN_WARNING "%s: %s (%#x)\n", dev->name, msg, status);
  1578. }
  1579. /* This handles TX complete events posted by the device
  1580. * via interrupts.
  1581. */
  1582. static void
  1583. tc35815_txdone(struct net_device *dev)
  1584. {
  1585. struct tc35815_local *lp = netdev_priv(dev);
  1586. struct TxFD *txfd;
  1587. unsigned int fdctl;
  1588. txfd = &lp->tfd_base[lp->tfd_end];
  1589. while (lp->tfd_start != lp->tfd_end &&
  1590. !((fdctl = le32_to_cpu(txfd->fd.FDCtl)) & FD_CownsFD)) {
  1591. int status = le32_to_cpu(txfd->fd.FDStat);
  1592. struct sk_buff *skb;
  1593. unsigned long fdnext = le32_to_cpu(txfd->fd.FDNext);
  1594. u32 fdsystem = le32_to_cpu(txfd->fd.FDSystem);
  1595. if (netif_msg_tx_done(lp)) {
  1596. printk("%s: complete TxFD.\n", dev->name);
  1597. dump_txfd(txfd);
  1598. }
  1599. tc35815_check_tx_stat(dev, status);
  1600. skb = fdsystem != 0xffffffff ?
  1601. lp->tx_skbs[fdsystem].skb : NULL;
  1602. #ifdef DEBUG
  1603. if (lp->tx_skbs[lp->tfd_end].skb != skb) {
  1604. printk("%s: tx_skbs mismatch.\n", dev->name);
  1605. panic_queues(dev);
  1606. }
  1607. #else
  1608. BUG_ON(lp->tx_skbs[lp->tfd_end].skb != skb);
  1609. #endif
  1610. if (skb) {
  1611. dev->stats.tx_bytes += skb->len;
  1612. pci_unmap_single(lp->pci_dev, lp->tx_skbs[lp->tfd_end].skb_dma, skb->len, PCI_DMA_TODEVICE);
  1613. lp->tx_skbs[lp->tfd_end].skb = NULL;
  1614. lp->tx_skbs[lp->tfd_end].skb_dma = 0;
  1615. dev_kfree_skb_any(skb);
  1616. }
  1617. txfd->fd.FDSystem = cpu_to_le32(0xffffffff);
  1618. lp->tfd_end = (lp->tfd_end + 1) % TX_FD_NUM;
  1619. txfd = &lp->tfd_base[lp->tfd_end];
  1620. #ifdef DEBUG
  1621. if ((fdnext & ~FD_Next_EOL) != fd_virt_to_bus(lp, txfd)) {
  1622. printk("%s: TxFD FDNext invalid.\n", dev->name);
  1623. panic_queues(dev);
  1624. }
  1625. #endif
  1626. if (fdnext & FD_Next_EOL) {
  1627. /* DMA Transmitter has been stopping... */
  1628. if (lp->tfd_end != lp->tfd_start) {
  1629. struct tc35815_regs __iomem *tr =
  1630. (struct tc35815_regs __iomem *)dev->base_addr;
  1631. int head = (lp->tfd_start + TX_FD_NUM - 1) % TX_FD_NUM;
  1632. struct TxFD *txhead = &lp->tfd_base[head];
  1633. int qlen = (lp->tfd_start + TX_FD_NUM
  1634. - lp->tfd_end) % TX_FD_NUM;
  1635. #ifdef DEBUG
  1636. if (!(le32_to_cpu(txfd->fd.FDCtl) & FD_CownsFD)) {
  1637. printk("%s: TxFD FDCtl invalid.\n", dev->name);
  1638. panic_queues(dev);
  1639. }
  1640. #endif
  1641. /* log max queue length */
  1642. if (lp->lstats.max_tx_qlen < qlen)
  1643. lp->lstats.max_tx_qlen = qlen;
  1644. /* start DMA Transmitter again */
  1645. txhead->fd.FDNext |= cpu_to_le32(FD_Next_EOL);
  1646. txhead->fd.FDCtl |= cpu_to_le32(FD_FrmOpt_IntTx);
  1647. if (netif_msg_tx_queued(lp)) {
  1648. printk("%s: start TxFD on queue.\n",
  1649. dev->name);
  1650. dump_txfd(txfd);
  1651. }
  1652. tc_writel(fd_virt_to_bus(lp, txfd), &tr->TxFrmPtr);
  1653. }
  1654. break;
  1655. }
  1656. }
  1657. /* If we had stopped the queue due to a "tx full"
  1658. * condition, and space has now been made available,
  1659. * wake up the queue.
  1660. */
  1661. if (netif_queue_stopped(dev) && !tc35815_tx_full(dev))
  1662. netif_wake_queue(dev);
  1663. }
  1664. /* The inverse routine to tc35815_open(). */
  1665. static int
  1666. tc35815_close(struct net_device *dev)
  1667. {
  1668. struct tc35815_local *lp = netdev_priv(dev);
  1669. netif_stop_queue(dev);
  1670. napi_disable(&lp->napi);
  1671. if (lp->phy_dev)
  1672. phy_stop(lp->phy_dev);
  1673. cancel_work_sync(&lp->restart_work);
  1674. /* Flush the Tx and disable Rx here. */
  1675. tc35815_chip_reset(dev);
  1676. free_irq(dev->irq, dev);
  1677. tc35815_free_queues(dev);
  1678. return 0;
  1679. }
  1680. /*
  1681. * Get the current statistics.
  1682. * This may be called with the card open or closed.
  1683. */
  1684. static struct net_device_stats *tc35815_get_stats(struct net_device *dev)
  1685. {
  1686. struct tc35815_regs __iomem *tr =
  1687. (struct tc35815_regs __iomem *)dev->base_addr;
  1688. if (netif_running(dev))
  1689. /* Update the statistics from the device registers. */
  1690. dev->stats.rx_missed_errors += tc_readl(&tr->Miss_Cnt);
  1691. return &dev->stats;
  1692. }
  1693. static void tc35815_set_cam_entry(struct net_device *dev, int index, unsigned char *addr)
  1694. {
  1695. struct tc35815_local *lp = netdev_priv(dev);
  1696. struct tc35815_regs __iomem *tr =
  1697. (struct tc35815_regs __iomem *)dev->base_addr;
  1698. int cam_index = index * 6;
  1699. u32 cam_data;
  1700. u32 saved_addr;
  1701. saved_addr = tc_readl(&tr->CAM_Adr);
  1702. if (netif_msg_hw(lp))
  1703. printk(KERN_DEBUG "%s: CAM %d: %pM\n",
  1704. dev->name, index, addr);
  1705. if (index & 1) {
  1706. /* read modify write */
  1707. tc_writel(cam_index - 2, &tr->CAM_Adr);
  1708. cam_data = tc_readl(&tr->CAM_Data) & 0xffff0000;
  1709. cam_data |= addr[0] << 8 | addr[1];
  1710. tc_writel(cam_data, &tr->CAM_Data);
  1711. /* write whole word */
  1712. tc_writel(cam_index + 2, &tr->CAM_Adr);
  1713. cam_data = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
  1714. tc_writel(cam_data, &tr->CAM_Data);
  1715. } else {
  1716. /* write whole word */
  1717. tc_writel(cam_index, &tr->CAM_Adr);
  1718. cam_data = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  1719. tc_writel(cam_data, &tr->CAM_Data);
  1720. /* read modify write */
  1721. tc_writel(cam_index + 4, &tr->CAM_Adr);
  1722. cam_data = tc_readl(&tr->CAM_Data) & 0x0000ffff;
  1723. cam_data |= addr[4] << 24 | (addr[5] << 16);
  1724. tc_writel(cam_data, &tr->CAM_Data);
  1725. }
  1726. tc_writel(saved_addr, &tr->CAM_Adr);
  1727. }
  1728. /*
  1729. * Set or clear the multicast filter for this adaptor.
  1730. * num_addrs == -1 Promiscuous mode, receive all packets
  1731. * num_addrs == 0 Normal mode, clear multicast list
  1732. * num_addrs > 0 Multicast mode, receive normal and MC packets,
  1733. * and do best-effort filtering.
  1734. */
  1735. static void
  1736. tc35815_set_multicast_list(struct net_device *dev)
  1737. {
  1738. struct tc35815_regs __iomem *tr =
  1739. (struct tc35815_regs __iomem *)dev->base_addr;
  1740. if (dev->flags & IFF_PROMISC) {
  1741. /* With some (all?) 100MHalf HUB, controller will hang
  1742. * if we enabled promiscuous mode before linkup... */
  1743. struct tc35815_local *lp = netdev_priv(dev);
  1744. if (!lp->link)
  1745. return;
  1746. /* Enable promiscuous mode */
  1747. tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc | CAM_StationAcc, &tr->CAM_Ctl);
  1748. } else if ((dev->flags & IFF_ALLMULTI) ||
  1749. netdev_mc_count(dev) > CAM_ENTRY_MAX - 3) {
  1750. /* CAM 0, 1, 20 are reserved. */
  1751. /* Disable promiscuous mode, use normal mode. */
  1752. tc_writel(CAM_CompEn | CAM_BroadAcc | CAM_GroupAcc, &tr->CAM_Ctl);
  1753. } else if (!netdev_mc_empty(dev)) {
  1754. struct netdev_hw_addr *ha;
  1755. int i;
  1756. int ena_bits = CAM_Ena_Bit(CAM_ENTRY_SOURCE);
  1757. tc_writel(0, &tr->CAM_Ctl);
  1758. /* Walk the address list, and load the filter */
  1759. i = 0;
  1760. netdev_for_each_mc_addr(ha, dev) {
  1761. /* entry 0,1 is reserved. */
  1762. tc35815_set_cam_entry(dev, i + 2, ha->addr);
  1763. ena_bits |= CAM_Ena_Bit(i + 2);
  1764. i++;
  1765. }
  1766. tc_writel(ena_bits, &tr->CAM_Ena);
  1767. tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
  1768. } else {
  1769. tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
  1770. tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
  1771. }
  1772. }
  1773. static void tc35815_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1774. {
  1775. struct tc35815_local *lp = netdev_priv(dev);
  1776. strcpy(info->driver, MODNAME);
  1777. strcpy(info->version, DRV_VERSION);
  1778. strcpy(info->bus_info, pci_name(lp->pci_dev));
  1779. }
  1780. static int tc35815_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1781. {
  1782. struct tc35815_local *lp = netdev_priv(dev);
  1783. if (!lp->phy_dev)
  1784. return -ENODEV;
  1785. return phy_ethtool_gset(lp->phy_dev, cmd);
  1786. }
  1787. static int tc35815_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1788. {
  1789. struct tc35815_local *lp = netdev_priv(dev);
  1790. if (!lp->phy_dev)
  1791. return -ENODEV;
  1792. return phy_ethtool_sset(lp->phy_dev, cmd);
  1793. }
  1794. static u32 tc35815_get_msglevel(struct net_device *dev)
  1795. {
  1796. struct tc35815_local *lp = netdev_priv(dev);
  1797. return lp->msg_enable;
  1798. }
  1799. static void tc35815_set_msglevel(struct net_device *dev, u32 datum)
  1800. {
  1801. struct tc35815_local *lp = netdev_priv(dev);
  1802. lp->msg_enable = datum;
  1803. }
  1804. static int tc35815_get_sset_count(struct net_device *dev, int sset)
  1805. {
  1806. struct tc35815_local *lp = netdev_priv(dev);
  1807. switch (sset) {
  1808. case ETH_SS_STATS:
  1809. return sizeof(lp->lstats) / sizeof(int);
  1810. default:
  1811. return -EOPNOTSUPP;
  1812. }
  1813. }
  1814. static void tc35815_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
  1815. {
  1816. struct tc35815_local *lp = netdev_priv(dev);
  1817. data[0] = lp->lstats.max_tx_qlen;
  1818. data[1] = lp->lstats.tx_ints;
  1819. data[2] = lp->lstats.rx_ints;
  1820. data[3] = lp->lstats.tx_underrun;
  1821. }
  1822. static struct {
  1823. const char str[ETH_GSTRING_LEN];
  1824. } ethtool_stats_keys[] = {
  1825. { "max_tx_qlen" },
  1826. { "tx_ints" },
  1827. { "rx_ints" },
  1828. { "tx_underrun" },
  1829. };
  1830. static void tc35815_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1831. {
  1832. memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
  1833. }
  1834. static const struct ethtool_ops tc35815_ethtool_ops = {
  1835. .get_drvinfo = tc35815_get_drvinfo,
  1836. .get_settings = tc35815_get_settings,
  1837. .set_settings = tc35815_set_settings,
  1838. .get_link = ethtool_op_get_link,
  1839. .get_msglevel = tc35815_get_msglevel,
  1840. .set_msglevel = tc35815_set_msglevel,
  1841. .get_strings = tc35815_get_strings,
  1842. .get_sset_count = tc35815_get_sset_count,
  1843. .get_ethtool_stats = tc35815_get_ethtool_stats,
  1844. };
  1845. static int tc35815_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1846. {
  1847. struct tc35815_local *lp = netdev_priv(dev);
  1848. if (!netif_running(dev))
  1849. return -EINVAL;
  1850. if (!lp->phy_dev)
  1851. return -ENODEV;
  1852. return phy_mii_ioctl(lp->phy_dev, rq, cmd);
  1853. }
  1854. static void tc35815_chip_reset(struct net_device *dev)
  1855. {
  1856. struct tc35815_regs __iomem *tr =
  1857. (struct tc35815_regs __iomem *)dev->base_addr;
  1858. int i;
  1859. /* reset the controller */
  1860. tc_writel(MAC_Reset, &tr->MAC_Ctl);
  1861. udelay(4); /* 3200ns */
  1862. i = 0;
  1863. while (tc_readl(&tr->MAC_Ctl) & MAC_Reset) {
  1864. if (i++ > 100) {
  1865. printk(KERN_ERR "%s: MAC reset failed.\n", dev->name);
  1866. break;
  1867. }
  1868. mdelay(1);
  1869. }
  1870. tc_writel(0, &tr->MAC_Ctl);
  1871. /* initialize registers to default value */
  1872. tc_writel(0, &tr->DMA_Ctl);
  1873. tc_writel(0, &tr->TxThrsh);
  1874. tc_writel(0, &tr->TxPollCtr);
  1875. tc_writel(0, &tr->RxFragSize);
  1876. tc_writel(0, &tr->Int_En);
  1877. tc_writel(0, &tr->FDA_Bas);
  1878. tc_writel(0, &tr->FDA_Lim);
  1879. tc_writel(0xffffffff, &tr->Int_Src); /* Write 1 to clear */
  1880. tc_writel(0, &tr->CAM_Ctl);
  1881. tc_writel(0, &tr->Tx_Ctl);
  1882. tc_writel(0, &tr->Rx_Ctl);
  1883. tc_writel(0, &tr->CAM_Ena);
  1884. (void)tc_readl(&tr->Miss_Cnt); /* Read to clear */
  1885. /* initialize internal SRAM */
  1886. tc_writel(DMA_TestMode, &tr->DMA_Ctl);
  1887. for (i = 0; i < 0x1000; i += 4) {
  1888. tc_writel(i, &tr->CAM_Adr);
  1889. tc_writel(0, &tr->CAM_Data);
  1890. }
  1891. tc_writel(0, &tr->DMA_Ctl);
  1892. }
  1893. static void tc35815_chip_init(struct net_device *dev)
  1894. {
  1895. struct tc35815_local *lp = netdev_priv(dev);
  1896. struct tc35815_regs __iomem *tr =
  1897. (struct tc35815_regs __iomem *)dev->base_addr;
  1898. unsigned long txctl = TX_CTL_CMD;
  1899. /* load station address to CAM */
  1900. tc35815_set_cam_entry(dev, CAM_ENTRY_SOURCE, dev->dev_addr);
  1901. /* Enable CAM (broadcast and unicast) */
  1902. tc_writel(CAM_Ena_Bit(CAM_ENTRY_SOURCE), &tr->CAM_Ena);
  1903. tc_writel(CAM_CompEn | CAM_BroadAcc, &tr->CAM_Ctl);
  1904. /* Use DMA_RxAlign_2 to make IP header 4-byte aligned. */
  1905. if (HAVE_DMA_RXALIGN(lp))
  1906. tc_writel(DMA_BURST_SIZE | DMA_RxAlign_2, &tr->DMA_Ctl);
  1907. else
  1908. tc_writel(DMA_BURST_SIZE, &tr->DMA_Ctl);
  1909. tc_writel(0, &tr->TxPollCtr); /* Batch mode */
  1910. tc_writel(TX_THRESHOLD, &tr->TxThrsh);
  1911. tc_writel(INT_EN_CMD, &tr->Int_En);
  1912. /* set queues */
  1913. tc_writel(fd_virt_to_bus(lp, lp->rfd_base), &tr->FDA_Bas);
  1914. tc_writel((unsigned long)lp->rfd_limit - (unsigned long)lp->rfd_base,
  1915. &tr->FDA_Lim);
  1916. /*
  1917. * Activation method:
  1918. * First, enable the MAC Transmitter and the DMA Receive circuits.
  1919. * Then enable the DMA Transmitter and the MAC Receive circuits.
  1920. */
  1921. tc_writel(fd_virt_to_bus(lp, lp->fbl_ptr), &tr->BLFrmPtr); /* start DMA receiver */
  1922. tc_writel(RX_CTL_CMD, &tr->Rx_Ctl); /* start MAC receiver */
  1923. /* start MAC transmitter */
  1924. /* TX4939 does not have EnLCarr */
  1925. if (lp->chiptype == TC35815_TX4939)
  1926. txctl &= ~Tx_EnLCarr;
  1927. /* WORKAROUND: ignore LostCrS in full duplex operation */
  1928. if (!lp->phy_dev || !lp->link || lp->duplex == DUPLEX_FULL)
  1929. txctl &= ~Tx_EnLCarr;
  1930. tc_writel(txctl, &tr->Tx_Ctl);
  1931. }
  1932. #ifdef CONFIG_PM
  1933. static int tc35815_suspend(struct pci_dev *pdev, pm_message_t state)
  1934. {
  1935. struct net_device *dev = pci_get_drvdata(pdev);
  1936. struct tc35815_local *lp = netdev_priv(dev);
  1937. unsigned long flags;
  1938. pci_save_state(pdev);
  1939. if (!netif_running(dev))
  1940. return 0;
  1941. netif_device_detach(dev);
  1942. if (lp->phy_dev)
  1943. phy_stop(lp->phy_dev);
  1944. spin_lock_irqsave(&lp->lock, flags);
  1945. tc35815_chip_reset(dev);
  1946. spin_unlock_irqrestore(&lp->lock, flags);
  1947. pci_set_power_state(pdev, PCI_D3hot);
  1948. return 0;
  1949. }
  1950. static int tc35815_resume(struct pci_dev *pdev)
  1951. {
  1952. struct net_device *dev = pci_get_drvdata(pdev);
  1953. struct tc35815_local *lp = netdev_priv(dev);
  1954. pci_restore_state(pdev);
  1955. if (!netif_running(dev))
  1956. return 0;
  1957. pci_set_power_state(pdev, PCI_D0);
  1958. tc35815_restart(dev);
  1959. netif_carrier_off(dev);
  1960. if (lp->phy_dev)
  1961. phy_start(lp->phy_dev);
  1962. netif_device_attach(dev);
  1963. return 0;
  1964. }
  1965. #endif /* CONFIG_PM */
  1966. static struct pci_driver tc35815_pci_driver = {
  1967. .name = MODNAME,
  1968. .id_table = tc35815_pci_tbl,
  1969. .probe = tc35815_init_one,
  1970. .remove = __devexit_p(tc35815_remove_one),
  1971. #ifdef CONFIG_PM
  1972. .suspend = tc35815_suspend,
  1973. .resume = tc35815_resume,
  1974. #endif
  1975. };
  1976. module_param_named(speed, options.speed, int, 0);
  1977. MODULE_PARM_DESC(speed, "0:auto, 10:10Mbps, 100:100Mbps");
  1978. module_param_named(duplex, options.duplex, int, 0);
  1979. MODULE_PARM_DESC(duplex, "0:auto, 1:half, 2:full");
  1980. static int __init tc35815_init_module(void)
  1981. {
  1982. return pci_register_driver(&tc35815_pci_driver);
  1983. }
  1984. static void __exit tc35815_cleanup_module(void)
  1985. {
  1986. pci_unregister_driver(&tc35815_pci_driver);
  1987. }
  1988. module_init(tc35815_init_module);
  1989. module_exit(tc35815_cleanup_module);
  1990. MODULE_DESCRIPTION("TOSHIBA TC35815 PCI 10M/100M Ethernet driver");
  1991. MODULE_LICENSE("GPL");