ns83820.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  1. #define VERSION "0.23"
  2. /* ns83820.c by Benjamin LaHaise with contributions.
  3. *
  4. * Questions/comments/discussion to linux-ns83820@kvack.org.
  5. *
  6. * $Revision: 1.34.2.23 $
  7. *
  8. * Copyright 2001 Benjamin LaHaise.
  9. * Copyright 2001, 2002 Red Hat.
  10. *
  11. * Mmmm, chocolate vanilla mocha...
  12. *
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. *
  29. * ChangeLog
  30. * =========
  31. * 20010414 0.1 - created
  32. * 20010622 0.2 - basic rx and tx.
  33. * 20010711 0.3 - added duplex and link state detection support.
  34. * 20010713 0.4 - zero copy, no hangs.
  35. * 0.5 - 64 bit dma support (davem will hate me for this)
  36. * - disable jumbo frames to avoid tx hangs
  37. * - work around tx deadlocks on my 1.02 card via
  38. * fiddling with TXCFG
  39. * 20010810 0.6 - use pci dma api for ringbuffers, work on ia64
  40. * 20010816 0.7 - misc cleanups
  41. * 20010826 0.8 - fix critical zero copy bugs
  42. * 0.9 - internal experiment
  43. * 20010827 0.10 - fix ia64 unaligned access.
  44. * 20010906 0.11 - accept all packets with checksum errors as
  45. * otherwise fragments get lost
  46. * - fix >> 32 bugs
  47. * 0.12 - add statistics counters
  48. * - add allmulti/promisc support
  49. * 20011009 0.13 - hotplug support, other smaller pci api cleanups
  50. * 20011204 0.13a - optical transceiver support added
  51. * by Michael Clark <michael@metaparadigm.com>
  52. * 20011205 0.13b - call register_netdev earlier in initialization
  53. * suppress duplicate link status messages
  54. * 20011117 0.14 - ethtool GDRVINFO, GLINK support from jgarzik
  55. * 20011204 0.15 get ppc (big endian) working
  56. * 20011218 0.16 various cleanups
  57. * 20020310 0.17 speedups
  58. * 20020610 0.18 - actually use the pci dma api for highmem
  59. * - remove pci latency register fiddling
  60. * 0.19 - better bist support
  61. * - add ihr and reset_phy parameters
  62. * - gmii bus probing
  63. * - fix missed txok introduced during performance
  64. * tuning
  65. * 0.20 - fix stupid RFEN thinko. i am such a smurf.
  66. * 20040828 0.21 - add hardware vlan accleration
  67. * by Neil Horman <nhorman@redhat.com>
  68. * 20050406 0.22 - improved DAC ifdefs from Andi Kleen
  69. * - removal of dead code from Adrian Bunk
  70. * - fix half duplex collision behaviour
  71. * Driver Overview
  72. * ===============
  73. *
  74. * This driver was originally written for the National Semiconductor
  75. * 83820 chip, a 10/100/1000 Mbps 64 bit PCI ethernet NIC. Hopefully
  76. * this code will turn out to be a) clean, b) correct, and c) fast.
  77. * With that in mind, I'm aiming to split the code up as much as
  78. * reasonably possible. At present there are X major sections that
  79. * break down into a) packet receive, b) packet transmit, c) link
  80. * management, d) initialization and configuration. Where possible,
  81. * these code paths are designed to run in parallel.
  82. *
  83. * This driver has been tested and found to work with the following
  84. * cards (in no particular order):
  85. *
  86. * Cameo SOHO-GA2000T SOHO-GA2500T
  87. * D-Link DGE-500T
  88. * PureData PDP8023Z-TG
  89. * SMC SMC9452TX SMC9462TX
  90. * Netgear GA621
  91. *
  92. * Special thanks to SMC for providing hardware to test this driver on.
  93. *
  94. * Reports of success or failure would be greatly appreciated.
  95. */
  96. //#define dprintk printk
  97. #define dprintk(x...) do { } while (0)
  98. #include <linux/module.h>
  99. #include <linux/moduleparam.h>
  100. #include <linux/types.h>
  101. #include <linux/pci.h>
  102. #include <linux/dma-mapping.h>
  103. #include <linux/netdevice.h>
  104. #include <linux/etherdevice.h>
  105. #include <linux/delay.h>
  106. #include <linux/workqueue.h>
  107. #include <linux/init.h>
  108. #include <linux/ip.h> /* for iph */
  109. #include <linux/in.h> /* for IPPROTO_... */
  110. #include <linux/compiler.h>
  111. #include <linux/prefetch.h>
  112. #include <linux/ethtool.h>
  113. #include <linux/sched.h>
  114. #include <linux/timer.h>
  115. #include <linux/if_vlan.h>
  116. #include <linux/rtnetlink.h>
  117. #include <linux/jiffies.h>
  118. #include <linux/slab.h>
  119. #include <asm/io.h>
  120. #include <asm/uaccess.h>
  121. #include <asm/system.h>
  122. #define DRV_NAME "ns83820"
  123. /* Global parameters. See module_param near the bottom. */
  124. static int ihr = 2;
  125. static int reset_phy = 0;
  126. static int lnksts = 0; /* CFG_LNKSTS bit polarity */
  127. /* Dprintk is used for more interesting debug events */
  128. #undef Dprintk
  129. #define Dprintk dprintk
  130. /* tunables */
  131. #define RX_BUF_SIZE 1500 /* 8192 */
  132. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  133. #define NS83820_VLAN_ACCEL_SUPPORT
  134. #endif
  135. /* Must not exceed ~65000. */
  136. #define NR_RX_DESC 64
  137. #define NR_TX_DESC 128
  138. /* not tunable */
  139. #define REAL_RX_BUF_SIZE (RX_BUF_SIZE + 14) /* rx/tx mac addr + type */
  140. #define MIN_TX_DESC_FREE 8
  141. /* register defines */
  142. #define CFGCS 0x04
  143. #define CR_TXE 0x00000001
  144. #define CR_TXD 0x00000002
  145. /* Ramit : Here's a tip, don't do a RXD immediately followed by an RXE
  146. * The Receive engine skips one descriptor and moves
  147. * onto the next one!! */
  148. #define CR_RXE 0x00000004
  149. #define CR_RXD 0x00000008
  150. #define CR_TXR 0x00000010
  151. #define CR_RXR 0x00000020
  152. #define CR_SWI 0x00000080
  153. #define CR_RST 0x00000100
  154. #define PTSCR_EEBIST_FAIL 0x00000001
  155. #define PTSCR_EEBIST_EN 0x00000002
  156. #define PTSCR_EELOAD_EN 0x00000004
  157. #define PTSCR_RBIST_FAIL 0x000001b8
  158. #define PTSCR_RBIST_DONE 0x00000200
  159. #define PTSCR_RBIST_EN 0x00000400
  160. #define PTSCR_RBIST_RST 0x00002000
  161. #define MEAR_EEDI 0x00000001
  162. #define MEAR_EEDO 0x00000002
  163. #define MEAR_EECLK 0x00000004
  164. #define MEAR_EESEL 0x00000008
  165. #define MEAR_MDIO 0x00000010
  166. #define MEAR_MDDIR 0x00000020
  167. #define MEAR_MDC 0x00000040
  168. #define ISR_TXDESC3 0x40000000
  169. #define ISR_TXDESC2 0x20000000
  170. #define ISR_TXDESC1 0x10000000
  171. #define ISR_TXDESC0 0x08000000
  172. #define ISR_RXDESC3 0x04000000
  173. #define ISR_RXDESC2 0x02000000
  174. #define ISR_RXDESC1 0x01000000
  175. #define ISR_RXDESC0 0x00800000
  176. #define ISR_TXRCMP 0x00400000
  177. #define ISR_RXRCMP 0x00200000
  178. #define ISR_DPERR 0x00100000
  179. #define ISR_SSERR 0x00080000
  180. #define ISR_RMABT 0x00040000
  181. #define ISR_RTABT 0x00020000
  182. #define ISR_RXSOVR 0x00010000
  183. #define ISR_HIBINT 0x00008000
  184. #define ISR_PHY 0x00004000
  185. #define ISR_PME 0x00002000
  186. #define ISR_SWI 0x00001000
  187. #define ISR_MIB 0x00000800
  188. #define ISR_TXURN 0x00000400
  189. #define ISR_TXIDLE 0x00000200
  190. #define ISR_TXERR 0x00000100
  191. #define ISR_TXDESC 0x00000080
  192. #define ISR_TXOK 0x00000040
  193. #define ISR_RXORN 0x00000020
  194. #define ISR_RXIDLE 0x00000010
  195. #define ISR_RXEARLY 0x00000008
  196. #define ISR_RXERR 0x00000004
  197. #define ISR_RXDESC 0x00000002
  198. #define ISR_RXOK 0x00000001
  199. #define TXCFG_CSI 0x80000000
  200. #define TXCFG_HBI 0x40000000
  201. #define TXCFG_MLB 0x20000000
  202. #define TXCFG_ATP 0x10000000
  203. #define TXCFG_ECRETRY 0x00800000
  204. #define TXCFG_BRST_DIS 0x00080000
  205. #define TXCFG_MXDMA1024 0x00000000
  206. #define TXCFG_MXDMA512 0x00700000
  207. #define TXCFG_MXDMA256 0x00600000
  208. #define TXCFG_MXDMA128 0x00500000
  209. #define TXCFG_MXDMA64 0x00400000
  210. #define TXCFG_MXDMA32 0x00300000
  211. #define TXCFG_MXDMA16 0x00200000
  212. #define TXCFG_MXDMA8 0x00100000
  213. #define CFG_LNKSTS 0x80000000
  214. #define CFG_SPDSTS 0x60000000
  215. #define CFG_SPDSTS1 0x40000000
  216. #define CFG_SPDSTS0 0x20000000
  217. #define CFG_DUPSTS 0x10000000
  218. #define CFG_TBI_EN 0x01000000
  219. #define CFG_MODE_1000 0x00400000
  220. /* Ramit : Dont' ever use AUTO_1000, it never works and is buggy.
  221. * Read the Phy response and then configure the MAC accordingly */
  222. #define CFG_AUTO_1000 0x00200000
  223. #define CFG_PINT_CTL 0x001c0000
  224. #define CFG_PINT_DUPSTS 0x00100000
  225. #define CFG_PINT_LNKSTS 0x00080000
  226. #define CFG_PINT_SPDSTS 0x00040000
  227. #define CFG_TMRTEST 0x00020000
  228. #define CFG_MRM_DIS 0x00010000
  229. #define CFG_MWI_DIS 0x00008000
  230. #define CFG_T64ADDR 0x00004000
  231. #define CFG_PCI64_DET 0x00002000
  232. #define CFG_DATA64_EN 0x00001000
  233. #define CFG_M64ADDR 0x00000800
  234. #define CFG_PHY_RST 0x00000400
  235. #define CFG_PHY_DIS 0x00000200
  236. #define CFG_EXTSTS_EN 0x00000100
  237. #define CFG_REQALG 0x00000080
  238. #define CFG_SB 0x00000040
  239. #define CFG_POW 0x00000020
  240. #define CFG_EXD 0x00000010
  241. #define CFG_PESEL 0x00000008
  242. #define CFG_BROM_DIS 0x00000004
  243. #define CFG_EXT_125 0x00000002
  244. #define CFG_BEM 0x00000001
  245. #define EXTSTS_UDPPKT 0x00200000
  246. #define EXTSTS_TCPPKT 0x00080000
  247. #define EXTSTS_IPPKT 0x00020000
  248. #define EXTSTS_VPKT 0x00010000
  249. #define EXTSTS_VTG_MASK 0x0000ffff
  250. #define SPDSTS_POLARITY (CFG_SPDSTS1 | CFG_SPDSTS0 | CFG_DUPSTS | (lnksts ? CFG_LNKSTS : 0))
  251. #define MIBC_MIBS 0x00000008
  252. #define MIBC_ACLR 0x00000004
  253. #define MIBC_FRZ 0x00000002
  254. #define MIBC_WRN 0x00000001
  255. #define PCR_PSEN (1 << 31)
  256. #define PCR_PS_MCAST (1 << 30)
  257. #define PCR_PS_DA (1 << 29)
  258. #define PCR_STHI_8 (3 << 23)
  259. #define PCR_STLO_4 (1 << 23)
  260. #define PCR_FFHI_8K (3 << 21)
  261. #define PCR_FFLO_4K (1 << 21)
  262. #define PCR_PAUSE_CNT 0xFFFE
  263. #define RXCFG_AEP 0x80000000
  264. #define RXCFG_ARP 0x40000000
  265. #define RXCFG_STRIPCRC 0x20000000
  266. #define RXCFG_RX_FD 0x10000000
  267. #define RXCFG_ALP 0x08000000
  268. #define RXCFG_AIRL 0x04000000
  269. #define RXCFG_MXDMA512 0x00700000
  270. #define RXCFG_DRTH 0x0000003e
  271. #define RXCFG_DRTH0 0x00000002
  272. #define RFCR_RFEN 0x80000000
  273. #define RFCR_AAB 0x40000000
  274. #define RFCR_AAM 0x20000000
  275. #define RFCR_AAU 0x10000000
  276. #define RFCR_APM 0x08000000
  277. #define RFCR_APAT 0x07800000
  278. #define RFCR_APAT3 0x04000000
  279. #define RFCR_APAT2 0x02000000
  280. #define RFCR_APAT1 0x01000000
  281. #define RFCR_APAT0 0x00800000
  282. #define RFCR_AARP 0x00400000
  283. #define RFCR_MHEN 0x00200000
  284. #define RFCR_UHEN 0x00100000
  285. #define RFCR_ULM 0x00080000
  286. #define VRCR_RUDPE 0x00000080
  287. #define VRCR_RTCPE 0x00000040
  288. #define VRCR_RIPE 0x00000020
  289. #define VRCR_IPEN 0x00000010
  290. #define VRCR_DUTF 0x00000008
  291. #define VRCR_DVTF 0x00000004
  292. #define VRCR_VTREN 0x00000002
  293. #define VRCR_VTDEN 0x00000001
  294. #define VTCR_PPCHK 0x00000008
  295. #define VTCR_GCHK 0x00000004
  296. #define VTCR_VPPTI 0x00000002
  297. #define VTCR_VGTI 0x00000001
  298. #define CR 0x00
  299. #define CFG 0x04
  300. #define MEAR 0x08
  301. #define PTSCR 0x0c
  302. #define ISR 0x10
  303. #define IMR 0x14
  304. #define IER 0x18
  305. #define IHR 0x1c
  306. #define TXDP 0x20
  307. #define TXDP_HI 0x24
  308. #define TXCFG 0x28
  309. #define GPIOR 0x2c
  310. #define RXDP 0x30
  311. #define RXDP_HI 0x34
  312. #define RXCFG 0x38
  313. #define PQCR 0x3c
  314. #define WCSR 0x40
  315. #define PCR 0x44
  316. #define RFCR 0x48
  317. #define RFDR 0x4c
  318. #define SRR 0x58
  319. #define VRCR 0xbc
  320. #define VTCR 0xc0
  321. #define VDR 0xc4
  322. #define CCSR 0xcc
  323. #define TBICR 0xe0
  324. #define TBISR 0xe4
  325. #define TANAR 0xe8
  326. #define TANLPAR 0xec
  327. #define TANER 0xf0
  328. #define TESR 0xf4
  329. #define TBICR_MR_AN_ENABLE 0x00001000
  330. #define TBICR_MR_RESTART_AN 0x00000200
  331. #define TBISR_MR_LINK_STATUS 0x00000020
  332. #define TBISR_MR_AN_COMPLETE 0x00000004
  333. #define TANAR_PS2 0x00000100
  334. #define TANAR_PS1 0x00000080
  335. #define TANAR_HALF_DUP 0x00000040
  336. #define TANAR_FULL_DUP 0x00000020
  337. #define GPIOR_GP5_OE 0x00000200
  338. #define GPIOR_GP4_OE 0x00000100
  339. #define GPIOR_GP3_OE 0x00000080
  340. #define GPIOR_GP2_OE 0x00000040
  341. #define GPIOR_GP1_OE 0x00000020
  342. #define GPIOR_GP3_OUT 0x00000004
  343. #define GPIOR_GP1_OUT 0x00000001
  344. #define LINK_AUTONEGOTIATE 0x01
  345. #define LINK_DOWN 0x02
  346. #define LINK_UP 0x04
  347. #define HW_ADDR_LEN sizeof(dma_addr_t)
  348. #define desc_addr_set(desc, addr) \
  349. do { \
  350. ((desc)[0] = cpu_to_le32(addr)); \
  351. if (HW_ADDR_LEN == 8) \
  352. (desc)[1] = cpu_to_le32(((u64)addr) >> 32); \
  353. } while(0)
  354. #define desc_addr_get(desc) \
  355. (le32_to_cpu((desc)[0]) | \
  356. (HW_ADDR_LEN == 8 ? ((dma_addr_t)le32_to_cpu((desc)[1]))<<32 : 0))
  357. #define DESC_LINK 0
  358. #define DESC_BUFPTR (DESC_LINK + HW_ADDR_LEN/4)
  359. #define DESC_CMDSTS (DESC_BUFPTR + HW_ADDR_LEN/4)
  360. #define DESC_EXTSTS (DESC_CMDSTS + 4/4)
  361. #define CMDSTS_OWN 0x80000000
  362. #define CMDSTS_MORE 0x40000000
  363. #define CMDSTS_INTR 0x20000000
  364. #define CMDSTS_ERR 0x10000000
  365. #define CMDSTS_OK 0x08000000
  366. #define CMDSTS_RUNT 0x00200000
  367. #define CMDSTS_LEN_MASK 0x0000ffff
  368. #define CMDSTS_DEST_MASK 0x01800000
  369. #define CMDSTS_DEST_SELF 0x00800000
  370. #define CMDSTS_DEST_MULTI 0x01000000
  371. #define DESC_SIZE 8 /* Should be cache line sized */
  372. struct rx_info {
  373. spinlock_t lock;
  374. int up;
  375. unsigned long idle;
  376. struct sk_buff *skbs[NR_RX_DESC];
  377. __le32 *next_rx_desc;
  378. u16 next_rx, next_empty;
  379. __le32 *descs;
  380. dma_addr_t phy_descs;
  381. };
  382. struct ns83820 {
  383. u8 __iomem *base;
  384. struct pci_dev *pci_dev;
  385. struct net_device *ndev;
  386. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  387. struct vlan_group *vlgrp;
  388. #endif
  389. struct rx_info rx_info;
  390. struct tasklet_struct rx_tasklet;
  391. unsigned ihr;
  392. struct work_struct tq_refill;
  393. /* protects everything below. irqsave when using. */
  394. spinlock_t misc_lock;
  395. u32 CFG_cache;
  396. u32 MEAR_cache;
  397. u32 IMR_cache;
  398. unsigned linkstate;
  399. spinlock_t tx_lock;
  400. u16 tx_done_idx;
  401. u16 tx_idx;
  402. volatile u16 tx_free_idx; /* idx of free desc chain */
  403. u16 tx_intr_idx;
  404. atomic_t nr_tx_skbs;
  405. struct sk_buff *tx_skbs[NR_TX_DESC];
  406. char pad[16] __attribute__((aligned(16)));
  407. __le32 *tx_descs;
  408. dma_addr_t tx_phy_descs;
  409. struct timer_list tx_watchdog;
  410. };
  411. static inline struct ns83820 *PRIV(struct net_device *dev)
  412. {
  413. return netdev_priv(dev);
  414. }
  415. #define __kick_rx(dev) writel(CR_RXE, dev->base + CR)
  416. static inline void kick_rx(struct net_device *ndev)
  417. {
  418. struct ns83820 *dev = PRIV(ndev);
  419. dprintk("kick_rx: maybe kicking\n");
  420. if (test_and_clear_bit(0, &dev->rx_info.idle)) {
  421. dprintk("actually kicking\n");
  422. writel(dev->rx_info.phy_descs +
  423. (4 * DESC_SIZE * dev->rx_info.next_rx),
  424. dev->base + RXDP);
  425. if (dev->rx_info.next_rx == dev->rx_info.next_empty)
  426. printk(KERN_DEBUG "%s: uh-oh: next_rx == next_empty???\n",
  427. ndev->name);
  428. __kick_rx(dev);
  429. }
  430. }
  431. //free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC
  432. #define start_tx_okay(dev) \
  433. (((NR_TX_DESC-2 + dev->tx_done_idx - dev->tx_free_idx) % NR_TX_DESC) > MIN_TX_DESC_FREE)
  434. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  435. static void ns83820_vlan_rx_register(struct net_device *ndev, struct vlan_group *grp)
  436. {
  437. struct ns83820 *dev = PRIV(ndev);
  438. spin_lock_irq(&dev->misc_lock);
  439. spin_lock(&dev->tx_lock);
  440. dev->vlgrp = grp;
  441. spin_unlock(&dev->tx_lock);
  442. spin_unlock_irq(&dev->misc_lock);
  443. }
  444. #endif
  445. /* Packet Receiver
  446. *
  447. * The hardware supports linked lists of receive descriptors for
  448. * which ownership is transferred back and forth by means of an
  449. * ownership bit. While the hardware does support the use of a
  450. * ring for receive descriptors, we only make use of a chain in
  451. * an attempt to reduce bus traffic under heavy load scenarios.
  452. * This will also make bugs a bit more obvious. The current code
  453. * only makes use of a single rx chain; I hope to implement
  454. * priority based rx for version 1.0. Goal: even under overload
  455. * conditions, still route realtime traffic with as low jitter as
  456. * possible.
  457. */
  458. static inline void build_rx_desc(struct ns83820 *dev, __le32 *desc, dma_addr_t link, dma_addr_t buf, u32 cmdsts, u32 extsts)
  459. {
  460. desc_addr_set(desc + DESC_LINK, link);
  461. desc_addr_set(desc + DESC_BUFPTR, buf);
  462. desc[DESC_EXTSTS] = cpu_to_le32(extsts);
  463. mb();
  464. desc[DESC_CMDSTS] = cpu_to_le32(cmdsts);
  465. }
  466. #define nr_rx_empty(dev) ((NR_RX_DESC-2 + dev->rx_info.next_rx - dev->rx_info.next_empty) % NR_RX_DESC)
  467. static inline int ns83820_add_rx_skb(struct ns83820 *dev, struct sk_buff *skb)
  468. {
  469. unsigned next_empty;
  470. u32 cmdsts;
  471. __le32 *sg;
  472. dma_addr_t buf;
  473. next_empty = dev->rx_info.next_empty;
  474. /* don't overrun last rx marker */
  475. if (unlikely(nr_rx_empty(dev) <= 2)) {
  476. kfree_skb(skb);
  477. return 1;
  478. }
  479. #if 0
  480. dprintk("next_empty[%d] nr_used[%d] next_rx[%d]\n",
  481. dev->rx_info.next_empty,
  482. dev->rx_info.nr_used,
  483. dev->rx_info.next_rx
  484. );
  485. #endif
  486. sg = dev->rx_info.descs + (next_empty * DESC_SIZE);
  487. BUG_ON(NULL != dev->rx_info.skbs[next_empty]);
  488. dev->rx_info.skbs[next_empty] = skb;
  489. dev->rx_info.next_empty = (next_empty + 1) % NR_RX_DESC;
  490. cmdsts = REAL_RX_BUF_SIZE | CMDSTS_INTR;
  491. buf = pci_map_single(dev->pci_dev, skb->data,
  492. REAL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  493. build_rx_desc(dev, sg, 0, buf, cmdsts, 0);
  494. /* update link of previous rx */
  495. if (likely(next_empty != dev->rx_info.next_rx))
  496. dev->rx_info.descs[((NR_RX_DESC + next_empty - 1) % NR_RX_DESC) * DESC_SIZE] = cpu_to_le32(dev->rx_info.phy_descs + (next_empty * DESC_SIZE * 4));
  497. return 0;
  498. }
  499. static inline int rx_refill(struct net_device *ndev, gfp_t gfp)
  500. {
  501. struct ns83820 *dev = PRIV(ndev);
  502. unsigned i;
  503. unsigned long flags = 0;
  504. if (unlikely(nr_rx_empty(dev) <= 2))
  505. return 0;
  506. dprintk("rx_refill(%p)\n", ndev);
  507. if (gfp == GFP_ATOMIC)
  508. spin_lock_irqsave(&dev->rx_info.lock, flags);
  509. for (i=0; i<NR_RX_DESC; i++) {
  510. struct sk_buff *skb;
  511. long res;
  512. /* extra 16 bytes for alignment */
  513. skb = __netdev_alloc_skb(ndev, REAL_RX_BUF_SIZE+16, gfp);
  514. if (unlikely(!skb))
  515. break;
  516. skb_reserve(skb, skb->data - PTR_ALIGN(skb->data, 16));
  517. if (gfp != GFP_ATOMIC)
  518. spin_lock_irqsave(&dev->rx_info.lock, flags);
  519. res = ns83820_add_rx_skb(dev, skb);
  520. if (gfp != GFP_ATOMIC)
  521. spin_unlock_irqrestore(&dev->rx_info.lock, flags);
  522. if (res) {
  523. i = 1;
  524. break;
  525. }
  526. }
  527. if (gfp == GFP_ATOMIC)
  528. spin_unlock_irqrestore(&dev->rx_info.lock, flags);
  529. return i ? 0 : -ENOMEM;
  530. }
  531. static void rx_refill_atomic(struct net_device *ndev)
  532. {
  533. rx_refill(ndev, GFP_ATOMIC);
  534. }
  535. /* REFILL */
  536. static inline void queue_refill(struct work_struct *work)
  537. {
  538. struct ns83820 *dev = container_of(work, struct ns83820, tq_refill);
  539. struct net_device *ndev = dev->ndev;
  540. rx_refill(ndev, GFP_KERNEL);
  541. if (dev->rx_info.up)
  542. kick_rx(ndev);
  543. }
  544. static inline void clear_rx_desc(struct ns83820 *dev, unsigned i)
  545. {
  546. build_rx_desc(dev, dev->rx_info.descs + (DESC_SIZE * i), 0, 0, CMDSTS_OWN, 0);
  547. }
  548. static void phy_intr(struct net_device *ndev)
  549. {
  550. struct ns83820 *dev = PRIV(ndev);
  551. static const char *speeds[] = { "10", "100", "1000", "1000(?)", "1000F" };
  552. u32 cfg, new_cfg;
  553. u32 tbisr, tanar, tanlpar;
  554. int speed, fullduplex, newlinkstate;
  555. cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  556. if (dev->CFG_cache & CFG_TBI_EN) {
  557. /* we have an optical transceiver */
  558. tbisr = readl(dev->base + TBISR);
  559. tanar = readl(dev->base + TANAR);
  560. tanlpar = readl(dev->base + TANLPAR);
  561. dprintk("phy_intr: tbisr=%08x, tanar=%08x, tanlpar=%08x\n",
  562. tbisr, tanar, tanlpar);
  563. if ( (fullduplex = (tanlpar & TANAR_FULL_DUP) &&
  564. (tanar & TANAR_FULL_DUP)) ) {
  565. /* both of us are full duplex */
  566. writel(readl(dev->base + TXCFG)
  567. | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP,
  568. dev->base + TXCFG);
  569. writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
  570. dev->base + RXCFG);
  571. /* Light up full duplex LED */
  572. writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT,
  573. dev->base + GPIOR);
  574. } else if (((tanlpar & TANAR_HALF_DUP) &&
  575. (tanar & TANAR_HALF_DUP)) ||
  576. ((tanlpar & TANAR_FULL_DUP) &&
  577. (tanar & TANAR_HALF_DUP)) ||
  578. ((tanlpar & TANAR_HALF_DUP) &&
  579. (tanar & TANAR_FULL_DUP))) {
  580. /* one or both of us are half duplex */
  581. writel((readl(dev->base + TXCFG)
  582. & ~(TXCFG_CSI | TXCFG_HBI)) | TXCFG_ATP,
  583. dev->base + TXCFG);
  584. writel(readl(dev->base + RXCFG) & ~RXCFG_RX_FD,
  585. dev->base + RXCFG);
  586. /* Turn off full duplex LED */
  587. writel(readl(dev->base + GPIOR) & ~GPIOR_GP1_OUT,
  588. dev->base + GPIOR);
  589. }
  590. speed = 4; /* 1000F */
  591. } else {
  592. /* we have a copper transceiver */
  593. new_cfg = dev->CFG_cache & ~(CFG_SB | CFG_MODE_1000 | CFG_SPDSTS);
  594. if (cfg & CFG_SPDSTS1)
  595. new_cfg |= CFG_MODE_1000;
  596. else
  597. new_cfg &= ~CFG_MODE_1000;
  598. speed = ((cfg / CFG_SPDSTS0) & 3);
  599. fullduplex = (cfg & CFG_DUPSTS);
  600. if (fullduplex) {
  601. new_cfg |= CFG_SB;
  602. writel(readl(dev->base + TXCFG)
  603. | TXCFG_CSI | TXCFG_HBI,
  604. dev->base + TXCFG);
  605. writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
  606. dev->base + RXCFG);
  607. } else {
  608. writel(readl(dev->base + TXCFG)
  609. & ~(TXCFG_CSI | TXCFG_HBI),
  610. dev->base + TXCFG);
  611. writel(readl(dev->base + RXCFG) & ~(RXCFG_RX_FD),
  612. dev->base + RXCFG);
  613. }
  614. if ((cfg & CFG_LNKSTS) &&
  615. ((new_cfg ^ dev->CFG_cache) != 0)) {
  616. writel(new_cfg, dev->base + CFG);
  617. dev->CFG_cache = new_cfg;
  618. }
  619. dev->CFG_cache &= ~CFG_SPDSTS;
  620. dev->CFG_cache |= cfg & CFG_SPDSTS;
  621. }
  622. newlinkstate = (cfg & CFG_LNKSTS) ? LINK_UP : LINK_DOWN;
  623. if (newlinkstate & LINK_UP &&
  624. dev->linkstate != newlinkstate) {
  625. netif_start_queue(ndev);
  626. netif_wake_queue(ndev);
  627. printk(KERN_INFO "%s: link now %s mbps, %s duplex and up.\n",
  628. ndev->name,
  629. speeds[speed],
  630. fullduplex ? "full" : "half");
  631. } else if (newlinkstate & LINK_DOWN &&
  632. dev->linkstate != newlinkstate) {
  633. netif_stop_queue(ndev);
  634. printk(KERN_INFO "%s: link now down.\n", ndev->name);
  635. }
  636. dev->linkstate = newlinkstate;
  637. }
  638. static int ns83820_setup_rx(struct net_device *ndev)
  639. {
  640. struct ns83820 *dev = PRIV(ndev);
  641. unsigned i;
  642. int ret;
  643. dprintk("ns83820_setup_rx(%p)\n", ndev);
  644. dev->rx_info.idle = 1;
  645. dev->rx_info.next_rx = 0;
  646. dev->rx_info.next_rx_desc = dev->rx_info.descs;
  647. dev->rx_info.next_empty = 0;
  648. for (i=0; i<NR_RX_DESC; i++)
  649. clear_rx_desc(dev, i);
  650. writel(0, dev->base + RXDP_HI);
  651. writel(dev->rx_info.phy_descs, dev->base + RXDP);
  652. ret = rx_refill(ndev, GFP_KERNEL);
  653. if (!ret) {
  654. dprintk("starting receiver\n");
  655. /* prevent the interrupt handler from stomping on us */
  656. spin_lock_irq(&dev->rx_info.lock);
  657. writel(0x0001, dev->base + CCSR);
  658. writel(0, dev->base + RFCR);
  659. writel(0x7fc00000, dev->base + RFCR);
  660. writel(0xffc00000, dev->base + RFCR);
  661. dev->rx_info.up = 1;
  662. phy_intr(ndev);
  663. /* Okay, let it rip */
  664. spin_lock(&dev->misc_lock);
  665. dev->IMR_cache |= ISR_PHY;
  666. dev->IMR_cache |= ISR_RXRCMP;
  667. //dev->IMR_cache |= ISR_RXERR;
  668. //dev->IMR_cache |= ISR_RXOK;
  669. dev->IMR_cache |= ISR_RXORN;
  670. dev->IMR_cache |= ISR_RXSOVR;
  671. dev->IMR_cache |= ISR_RXDESC;
  672. dev->IMR_cache |= ISR_RXIDLE;
  673. dev->IMR_cache |= ISR_TXDESC;
  674. dev->IMR_cache |= ISR_TXIDLE;
  675. writel(dev->IMR_cache, dev->base + IMR);
  676. writel(1, dev->base + IER);
  677. spin_unlock(&dev->misc_lock);
  678. kick_rx(ndev);
  679. spin_unlock_irq(&dev->rx_info.lock);
  680. }
  681. return ret;
  682. }
  683. static void ns83820_cleanup_rx(struct ns83820 *dev)
  684. {
  685. unsigned i;
  686. unsigned long flags;
  687. dprintk("ns83820_cleanup_rx(%p)\n", dev);
  688. /* disable receive interrupts */
  689. spin_lock_irqsave(&dev->misc_lock, flags);
  690. dev->IMR_cache &= ~(ISR_RXOK | ISR_RXDESC | ISR_RXERR | ISR_RXEARLY | ISR_RXIDLE);
  691. writel(dev->IMR_cache, dev->base + IMR);
  692. spin_unlock_irqrestore(&dev->misc_lock, flags);
  693. /* synchronize with the interrupt handler and kill it */
  694. dev->rx_info.up = 0;
  695. synchronize_irq(dev->pci_dev->irq);
  696. /* touch the pci bus... */
  697. readl(dev->base + IMR);
  698. /* assumes the transmitter is already disabled and reset */
  699. writel(0, dev->base + RXDP_HI);
  700. writel(0, dev->base + RXDP);
  701. for (i=0; i<NR_RX_DESC; i++) {
  702. struct sk_buff *skb = dev->rx_info.skbs[i];
  703. dev->rx_info.skbs[i] = NULL;
  704. clear_rx_desc(dev, i);
  705. kfree_skb(skb);
  706. }
  707. }
  708. static void ns83820_rx_kick(struct net_device *ndev)
  709. {
  710. struct ns83820 *dev = PRIV(ndev);
  711. /*if (nr_rx_empty(dev) >= NR_RX_DESC/4)*/ {
  712. if (dev->rx_info.up) {
  713. rx_refill_atomic(ndev);
  714. kick_rx(ndev);
  715. }
  716. }
  717. if (dev->rx_info.up && nr_rx_empty(dev) > NR_RX_DESC*3/4)
  718. schedule_work(&dev->tq_refill);
  719. else
  720. kick_rx(ndev);
  721. if (dev->rx_info.idle)
  722. printk(KERN_DEBUG "%s: BAD\n", ndev->name);
  723. }
  724. /* rx_irq
  725. *
  726. */
  727. static void rx_irq(struct net_device *ndev)
  728. {
  729. struct ns83820 *dev = PRIV(ndev);
  730. struct rx_info *info = &dev->rx_info;
  731. unsigned next_rx;
  732. int rx_rc, len;
  733. u32 cmdsts;
  734. __le32 *desc;
  735. unsigned long flags;
  736. int nr = 0;
  737. dprintk("rx_irq(%p)\n", ndev);
  738. dprintk("rxdp: %08x, descs: %08lx next_rx[%d]: %p next_empty[%d]: %p\n",
  739. readl(dev->base + RXDP),
  740. (long)(dev->rx_info.phy_descs),
  741. (int)dev->rx_info.next_rx,
  742. (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_rx)),
  743. (int)dev->rx_info.next_empty,
  744. (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_empty))
  745. );
  746. spin_lock_irqsave(&info->lock, flags);
  747. if (!info->up)
  748. goto out;
  749. dprintk("walking descs\n");
  750. next_rx = info->next_rx;
  751. desc = info->next_rx_desc;
  752. while ((CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) &&
  753. (cmdsts != CMDSTS_OWN)) {
  754. struct sk_buff *skb;
  755. u32 extsts = le32_to_cpu(desc[DESC_EXTSTS]);
  756. dma_addr_t bufptr = desc_addr_get(desc + DESC_BUFPTR);
  757. dprintk("cmdsts: %08x\n", cmdsts);
  758. dprintk("link: %08x\n", cpu_to_le32(desc[DESC_LINK]));
  759. dprintk("extsts: %08x\n", extsts);
  760. skb = info->skbs[next_rx];
  761. info->skbs[next_rx] = NULL;
  762. info->next_rx = (next_rx + 1) % NR_RX_DESC;
  763. mb();
  764. clear_rx_desc(dev, next_rx);
  765. pci_unmap_single(dev->pci_dev, bufptr,
  766. RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  767. len = cmdsts & CMDSTS_LEN_MASK;
  768. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  769. /* NH: As was mentioned below, this chip is kinda
  770. * brain dead about vlan tag stripping. Frames
  771. * that are 64 bytes with a vlan header appended
  772. * like arp frames, or pings, are flagged as Runts
  773. * when the tag is stripped and hardware. This
  774. * also means that the OK bit in the descriptor
  775. * is cleared when the frame comes in so we have
  776. * to do a specific length check here to make sure
  777. * the frame would have been ok, had we not stripped
  778. * the tag.
  779. */
  780. if (likely((CMDSTS_OK & cmdsts) ||
  781. ((cmdsts & CMDSTS_RUNT) && len >= 56))) {
  782. #else
  783. if (likely(CMDSTS_OK & cmdsts)) {
  784. #endif
  785. skb_put(skb, len);
  786. if (unlikely(!skb))
  787. goto netdev_mangle_me_harder_failed;
  788. if (cmdsts & CMDSTS_DEST_MULTI)
  789. ndev->stats.multicast++;
  790. ndev->stats.rx_packets++;
  791. ndev->stats.rx_bytes += len;
  792. if ((extsts & 0x002a0000) && !(extsts & 0x00540000)) {
  793. skb->ip_summed = CHECKSUM_UNNECESSARY;
  794. } else {
  795. skb_checksum_none_assert(skb);
  796. }
  797. skb->protocol = eth_type_trans(skb, ndev);
  798. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  799. if(extsts & EXTSTS_VPKT) {
  800. unsigned short tag;
  801. tag = ntohs(extsts & EXTSTS_VTG_MASK);
  802. rx_rc = vlan_hwaccel_rx(skb,dev->vlgrp,tag);
  803. } else {
  804. rx_rc = netif_rx(skb);
  805. }
  806. #else
  807. rx_rc = netif_rx(skb);
  808. #endif
  809. if (NET_RX_DROP == rx_rc) {
  810. netdev_mangle_me_harder_failed:
  811. ndev->stats.rx_dropped++;
  812. }
  813. } else {
  814. kfree_skb(skb);
  815. }
  816. nr++;
  817. next_rx = info->next_rx;
  818. desc = info->descs + (DESC_SIZE * next_rx);
  819. }
  820. info->next_rx = next_rx;
  821. info->next_rx_desc = info->descs + (DESC_SIZE * next_rx);
  822. out:
  823. if (0 && !nr) {
  824. Dprintk("dazed: cmdsts_f: %08x\n", cmdsts);
  825. }
  826. spin_unlock_irqrestore(&info->lock, flags);
  827. }
  828. static void rx_action(unsigned long _dev)
  829. {
  830. struct net_device *ndev = (void *)_dev;
  831. struct ns83820 *dev = PRIV(ndev);
  832. rx_irq(ndev);
  833. writel(ihr, dev->base + IHR);
  834. spin_lock_irq(&dev->misc_lock);
  835. dev->IMR_cache |= ISR_RXDESC;
  836. writel(dev->IMR_cache, dev->base + IMR);
  837. spin_unlock_irq(&dev->misc_lock);
  838. rx_irq(ndev);
  839. ns83820_rx_kick(ndev);
  840. }
  841. /* Packet Transmit code
  842. */
  843. static inline void kick_tx(struct ns83820 *dev)
  844. {
  845. dprintk("kick_tx(%p): tx_idx=%d free_idx=%d\n",
  846. dev, dev->tx_idx, dev->tx_free_idx);
  847. writel(CR_TXE, dev->base + CR);
  848. }
  849. /* No spinlock needed on the transmit irq path as the interrupt handler is
  850. * serialized.
  851. */
  852. static void do_tx_done(struct net_device *ndev)
  853. {
  854. struct ns83820 *dev = PRIV(ndev);
  855. u32 cmdsts, tx_done_idx;
  856. __le32 *desc;
  857. dprintk("do_tx_done(%p)\n", ndev);
  858. tx_done_idx = dev->tx_done_idx;
  859. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  860. dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  861. tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
  862. while ((tx_done_idx != dev->tx_free_idx) &&
  863. !(CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) ) {
  864. struct sk_buff *skb;
  865. unsigned len;
  866. dma_addr_t addr;
  867. if (cmdsts & CMDSTS_ERR)
  868. ndev->stats.tx_errors++;
  869. if (cmdsts & CMDSTS_OK)
  870. ndev->stats.tx_packets++;
  871. if (cmdsts & CMDSTS_OK)
  872. ndev->stats.tx_bytes += cmdsts & 0xffff;
  873. dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  874. tx_done_idx, dev->tx_free_idx, cmdsts);
  875. skb = dev->tx_skbs[tx_done_idx];
  876. dev->tx_skbs[tx_done_idx] = NULL;
  877. dprintk("done(%p)\n", skb);
  878. len = cmdsts & CMDSTS_LEN_MASK;
  879. addr = desc_addr_get(desc + DESC_BUFPTR);
  880. if (skb) {
  881. pci_unmap_single(dev->pci_dev,
  882. addr,
  883. len,
  884. PCI_DMA_TODEVICE);
  885. dev_kfree_skb_irq(skb);
  886. atomic_dec(&dev->nr_tx_skbs);
  887. } else
  888. pci_unmap_page(dev->pci_dev,
  889. addr,
  890. len,
  891. PCI_DMA_TODEVICE);
  892. tx_done_idx = (tx_done_idx + 1) % NR_TX_DESC;
  893. dev->tx_done_idx = tx_done_idx;
  894. desc[DESC_CMDSTS] = cpu_to_le32(0);
  895. mb();
  896. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  897. }
  898. /* Allow network stack to resume queueing packets after we've
  899. * finished transmitting at least 1/4 of the packets in the queue.
  900. */
  901. if (netif_queue_stopped(ndev) && start_tx_okay(dev)) {
  902. dprintk("start_queue(%p)\n", ndev);
  903. netif_start_queue(ndev);
  904. netif_wake_queue(ndev);
  905. }
  906. }
  907. static void ns83820_cleanup_tx(struct ns83820 *dev)
  908. {
  909. unsigned i;
  910. for (i=0; i<NR_TX_DESC; i++) {
  911. struct sk_buff *skb = dev->tx_skbs[i];
  912. dev->tx_skbs[i] = NULL;
  913. if (skb) {
  914. __le32 *desc = dev->tx_descs + (i * DESC_SIZE);
  915. pci_unmap_single(dev->pci_dev,
  916. desc_addr_get(desc + DESC_BUFPTR),
  917. le32_to_cpu(desc[DESC_CMDSTS]) & CMDSTS_LEN_MASK,
  918. PCI_DMA_TODEVICE);
  919. dev_kfree_skb_irq(skb);
  920. atomic_dec(&dev->nr_tx_skbs);
  921. }
  922. }
  923. memset(dev->tx_descs, 0, NR_TX_DESC * DESC_SIZE * 4);
  924. }
  925. /* transmit routine. This code relies on the network layer serializing
  926. * its calls in, but will run happily in parallel with the interrupt
  927. * handler. This code currently has provisions for fragmenting tx buffers
  928. * while trying to track down a bug in either the zero copy code or
  929. * the tx fifo (hence the MAX_FRAG_LEN).
  930. */
  931. static netdev_tx_t ns83820_hard_start_xmit(struct sk_buff *skb,
  932. struct net_device *ndev)
  933. {
  934. struct ns83820 *dev = PRIV(ndev);
  935. u32 free_idx, cmdsts, extsts;
  936. int nr_free, nr_frags;
  937. unsigned tx_done_idx, last_idx;
  938. dma_addr_t buf;
  939. unsigned len;
  940. skb_frag_t *frag;
  941. int stopped = 0;
  942. int do_intr = 0;
  943. volatile __le32 *first_desc;
  944. dprintk("ns83820_hard_start_xmit\n");
  945. nr_frags = skb_shinfo(skb)->nr_frags;
  946. again:
  947. if (unlikely(dev->CFG_cache & CFG_LNKSTS)) {
  948. netif_stop_queue(ndev);
  949. if (unlikely(dev->CFG_cache & CFG_LNKSTS))
  950. return NETDEV_TX_BUSY;
  951. netif_start_queue(ndev);
  952. }
  953. last_idx = free_idx = dev->tx_free_idx;
  954. tx_done_idx = dev->tx_done_idx;
  955. nr_free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC;
  956. nr_free -= 1;
  957. if (nr_free <= nr_frags) {
  958. dprintk("stop_queue - not enough(%p)\n", ndev);
  959. netif_stop_queue(ndev);
  960. /* Check again: we may have raced with a tx done irq */
  961. if (dev->tx_done_idx != tx_done_idx) {
  962. dprintk("restart queue(%p)\n", ndev);
  963. netif_start_queue(ndev);
  964. goto again;
  965. }
  966. return NETDEV_TX_BUSY;
  967. }
  968. if (free_idx == dev->tx_intr_idx) {
  969. do_intr = 1;
  970. dev->tx_intr_idx = (dev->tx_intr_idx + NR_TX_DESC/4) % NR_TX_DESC;
  971. }
  972. nr_free -= nr_frags;
  973. if (nr_free < MIN_TX_DESC_FREE) {
  974. dprintk("stop_queue - last entry(%p)\n", ndev);
  975. netif_stop_queue(ndev);
  976. stopped = 1;
  977. }
  978. frag = skb_shinfo(skb)->frags;
  979. if (!nr_frags)
  980. frag = NULL;
  981. extsts = 0;
  982. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  983. extsts |= EXTSTS_IPPKT;
  984. if (IPPROTO_TCP == ip_hdr(skb)->protocol)
  985. extsts |= EXTSTS_TCPPKT;
  986. else if (IPPROTO_UDP == ip_hdr(skb)->protocol)
  987. extsts |= EXTSTS_UDPPKT;
  988. }
  989. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  990. if(vlan_tx_tag_present(skb)) {
  991. /* fetch the vlan tag info out of the
  992. * ancillary data if the vlan code
  993. * is using hw vlan acceleration
  994. */
  995. short tag = vlan_tx_tag_get(skb);
  996. extsts |= (EXTSTS_VPKT | htons(tag));
  997. }
  998. #endif
  999. len = skb->len;
  1000. if (nr_frags)
  1001. len -= skb->data_len;
  1002. buf = pci_map_single(dev->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  1003. first_desc = dev->tx_descs + (free_idx * DESC_SIZE);
  1004. for (;;) {
  1005. volatile __le32 *desc = dev->tx_descs + (free_idx * DESC_SIZE);
  1006. dprintk("frag[%3u]: %4u @ 0x%08Lx\n", free_idx, len,
  1007. (unsigned long long)buf);
  1008. last_idx = free_idx;
  1009. free_idx = (free_idx + 1) % NR_TX_DESC;
  1010. desc[DESC_LINK] = cpu_to_le32(dev->tx_phy_descs + (free_idx * DESC_SIZE * 4));
  1011. desc_addr_set(desc + DESC_BUFPTR, buf);
  1012. desc[DESC_EXTSTS] = cpu_to_le32(extsts);
  1013. cmdsts = ((nr_frags) ? CMDSTS_MORE : do_intr ? CMDSTS_INTR : 0);
  1014. cmdsts |= (desc == first_desc) ? 0 : CMDSTS_OWN;
  1015. cmdsts |= len;
  1016. desc[DESC_CMDSTS] = cpu_to_le32(cmdsts);
  1017. if (!nr_frags)
  1018. break;
  1019. buf = pci_map_page(dev->pci_dev, frag->page,
  1020. frag->page_offset,
  1021. frag->size, PCI_DMA_TODEVICE);
  1022. dprintk("frag: buf=%08Lx page=%08lx offset=%08lx\n",
  1023. (long long)buf, (long) page_to_pfn(frag->page),
  1024. frag->page_offset);
  1025. len = frag->size;
  1026. frag++;
  1027. nr_frags--;
  1028. }
  1029. dprintk("done pkt\n");
  1030. spin_lock_irq(&dev->tx_lock);
  1031. dev->tx_skbs[last_idx] = skb;
  1032. first_desc[DESC_CMDSTS] |= cpu_to_le32(CMDSTS_OWN);
  1033. dev->tx_free_idx = free_idx;
  1034. atomic_inc(&dev->nr_tx_skbs);
  1035. spin_unlock_irq(&dev->tx_lock);
  1036. kick_tx(dev);
  1037. /* Check again: we may have raced with a tx done irq */
  1038. if (stopped && (dev->tx_done_idx != tx_done_idx) && start_tx_okay(dev))
  1039. netif_start_queue(ndev);
  1040. return NETDEV_TX_OK;
  1041. }
  1042. static void ns83820_update_stats(struct ns83820 *dev)
  1043. {
  1044. struct net_device *ndev = dev->ndev;
  1045. u8 __iomem *base = dev->base;
  1046. /* the DP83820 will freeze counters, so we need to read all of them */
  1047. ndev->stats.rx_errors += readl(base + 0x60) & 0xffff;
  1048. ndev->stats.rx_crc_errors += readl(base + 0x64) & 0xffff;
  1049. ndev->stats.rx_missed_errors += readl(base + 0x68) & 0xffff;
  1050. ndev->stats.rx_frame_errors += readl(base + 0x6c) & 0xffff;
  1051. /*ndev->stats.rx_symbol_errors +=*/ readl(base + 0x70);
  1052. ndev->stats.rx_length_errors += readl(base + 0x74) & 0xffff;
  1053. ndev->stats.rx_length_errors += readl(base + 0x78) & 0xffff;
  1054. /*ndev->stats.rx_badopcode_errors += */ readl(base + 0x7c);
  1055. /*ndev->stats.rx_pause_count += */ readl(base + 0x80);
  1056. /*ndev->stats.tx_pause_count += */ readl(base + 0x84);
  1057. ndev->stats.tx_carrier_errors += readl(base + 0x88) & 0xff;
  1058. }
  1059. static struct net_device_stats *ns83820_get_stats(struct net_device *ndev)
  1060. {
  1061. struct ns83820 *dev = PRIV(ndev);
  1062. /* somewhat overkill */
  1063. spin_lock_irq(&dev->misc_lock);
  1064. ns83820_update_stats(dev);
  1065. spin_unlock_irq(&dev->misc_lock);
  1066. return &ndev->stats;
  1067. }
  1068. /* Let ethtool retrieve info */
  1069. static int ns83820_get_settings(struct net_device *ndev,
  1070. struct ethtool_cmd *cmd)
  1071. {
  1072. struct ns83820 *dev = PRIV(ndev);
  1073. u32 cfg, tanar, tbicr;
  1074. int fullduplex = 0;
  1075. /*
  1076. * Here's the list of available ethtool commands from other drivers:
  1077. * cmd->advertising =
  1078. * ethtool_cmd_speed_set(cmd, ...)
  1079. * cmd->duplex =
  1080. * cmd->port = 0;
  1081. * cmd->phy_address =
  1082. * cmd->transceiver = 0;
  1083. * cmd->autoneg =
  1084. * cmd->maxtxpkt = 0;
  1085. * cmd->maxrxpkt = 0;
  1086. */
  1087. /* read current configuration */
  1088. cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  1089. tanar = readl(dev->base + TANAR);
  1090. tbicr = readl(dev->base + TBICR);
  1091. fullduplex = (cfg & CFG_DUPSTS) ? 1 : 0;
  1092. cmd->supported = SUPPORTED_Autoneg;
  1093. if (dev->CFG_cache & CFG_TBI_EN) {
  1094. /* we have optical interface */
  1095. cmd->supported |= SUPPORTED_1000baseT_Half |
  1096. SUPPORTED_1000baseT_Full |
  1097. SUPPORTED_FIBRE;
  1098. cmd->port = PORT_FIBRE;
  1099. } else {
  1100. /* we have copper */
  1101. cmd->supported |= SUPPORTED_10baseT_Half |
  1102. SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half |
  1103. SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Half |
  1104. SUPPORTED_1000baseT_Full |
  1105. SUPPORTED_MII;
  1106. cmd->port = PORT_MII;
  1107. }
  1108. cmd->duplex = fullduplex ? DUPLEX_FULL : DUPLEX_HALF;
  1109. switch (cfg / CFG_SPDSTS0 & 3) {
  1110. case 2:
  1111. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1112. break;
  1113. case 1:
  1114. ethtool_cmd_speed_set(cmd, SPEED_100);
  1115. break;
  1116. default:
  1117. ethtool_cmd_speed_set(cmd, SPEED_10);
  1118. break;
  1119. }
  1120. cmd->autoneg = (tbicr & TBICR_MR_AN_ENABLE)
  1121. ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  1122. return 0;
  1123. }
  1124. /* Let ethool change settings*/
  1125. static int ns83820_set_settings(struct net_device *ndev,
  1126. struct ethtool_cmd *cmd)
  1127. {
  1128. struct ns83820 *dev = PRIV(ndev);
  1129. u32 cfg, tanar;
  1130. int have_optical = 0;
  1131. int fullduplex = 0;
  1132. /* read current configuration */
  1133. cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  1134. tanar = readl(dev->base + TANAR);
  1135. if (dev->CFG_cache & CFG_TBI_EN) {
  1136. /* we have optical */
  1137. have_optical = 1;
  1138. fullduplex = (tanar & TANAR_FULL_DUP);
  1139. } else {
  1140. /* we have copper */
  1141. fullduplex = cfg & CFG_DUPSTS;
  1142. }
  1143. spin_lock_irq(&dev->misc_lock);
  1144. spin_lock(&dev->tx_lock);
  1145. /* Set duplex */
  1146. if (cmd->duplex != fullduplex) {
  1147. if (have_optical) {
  1148. /*set full duplex*/
  1149. if (cmd->duplex == DUPLEX_FULL) {
  1150. /* force full duplex */
  1151. writel(readl(dev->base + TXCFG)
  1152. | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP,
  1153. dev->base + TXCFG);
  1154. writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
  1155. dev->base + RXCFG);
  1156. /* Light up full duplex LED */
  1157. writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT,
  1158. dev->base + GPIOR);
  1159. } else {
  1160. /*TODO: set half duplex */
  1161. }
  1162. } else {
  1163. /*we have copper*/
  1164. /* TODO: Set duplex for copper cards */
  1165. }
  1166. printk(KERN_INFO "%s: Duplex set via ethtool\n",
  1167. ndev->name);
  1168. }
  1169. /* Set autonegotiation */
  1170. if (1) {
  1171. if (cmd->autoneg == AUTONEG_ENABLE) {
  1172. /* restart auto negotiation */
  1173. writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN,
  1174. dev->base + TBICR);
  1175. writel(TBICR_MR_AN_ENABLE, dev->base + TBICR);
  1176. dev->linkstate = LINK_AUTONEGOTIATE;
  1177. printk(KERN_INFO "%s: autoneg enabled via ethtool\n",
  1178. ndev->name);
  1179. } else {
  1180. /* disable auto negotiation */
  1181. writel(0x00000000, dev->base + TBICR);
  1182. }
  1183. printk(KERN_INFO "%s: autoneg %s via ethtool\n", ndev->name,
  1184. cmd->autoneg ? "ENABLED" : "DISABLED");
  1185. }
  1186. phy_intr(ndev);
  1187. spin_unlock(&dev->tx_lock);
  1188. spin_unlock_irq(&dev->misc_lock);
  1189. return 0;
  1190. }
  1191. /* end ethtool get/set support -df */
  1192. static void ns83820_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
  1193. {
  1194. struct ns83820 *dev = PRIV(ndev);
  1195. strcpy(info->driver, "ns83820");
  1196. strcpy(info->version, VERSION);
  1197. strcpy(info->bus_info, pci_name(dev->pci_dev));
  1198. }
  1199. static u32 ns83820_get_link(struct net_device *ndev)
  1200. {
  1201. struct ns83820 *dev = PRIV(ndev);
  1202. u32 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  1203. return cfg & CFG_LNKSTS ? 1 : 0;
  1204. }
  1205. static const struct ethtool_ops ops = {
  1206. .get_settings = ns83820_get_settings,
  1207. .set_settings = ns83820_set_settings,
  1208. .get_drvinfo = ns83820_get_drvinfo,
  1209. .get_link = ns83820_get_link
  1210. };
  1211. static inline void ns83820_disable_interrupts(struct ns83820 *dev)
  1212. {
  1213. writel(0, dev->base + IMR);
  1214. writel(0, dev->base + IER);
  1215. readl(dev->base + IER);
  1216. }
  1217. /* this function is called in irq context from the ISR */
  1218. static void ns83820_mib_isr(struct ns83820 *dev)
  1219. {
  1220. unsigned long flags;
  1221. spin_lock_irqsave(&dev->misc_lock, flags);
  1222. ns83820_update_stats(dev);
  1223. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1224. }
  1225. static void ns83820_do_isr(struct net_device *ndev, u32 isr);
  1226. static irqreturn_t ns83820_irq(int foo, void *data)
  1227. {
  1228. struct net_device *ndev = data;
  1229. struct ns83820 *dev = PRIV(ndev);
  1230. u32 isr;
  1231. dprintk("ns83820_irq(%p)\n", ndev);
  1232. dev->ihr = 0;
  1233. isr = readl(dev->base + ISR);
  1234. dprintk("irq: %08x\n", isr);
  1235. ns83820_do_isr(ndev, isr);
  1236. return IRQ_HANDLED;
  1237. }
  1238. static void ns83820_do_isr(struct net_device *ndev, u32 isr)
  1239. {
  1240. struct ns83820 *dev = PRIV(ndev);
  1241. unsigned long flags;
  1242. #ifdef DEBUG
  1243. if (isr & ~(ISR_PHY | ISR_RXDESC | ISR_RXEARLY | ISR_RXOK | ISR_RXERR | ISR_TXIDLE | ISR_TXOK | ISR_TXDESC))
  1244. Dprintk("odd isr? 0x%08x\n", isr);
  1245. #endif
  1246. if (ISR_RXIDLE & isr) {
  1247. dev->rx_info.idle = 1;
  1248. Dprintk("oh dear, we are idle\n");
  1249. ns83820_rx_kick(ndev);
  1250. }
  1251. if ((ISR_RXDESC | ISR_RXOK) & isr) {
  1252. prefetch(dev->rx_info.next_rx_desc);
  1253. spin_lock_irqsave(&dev->misc_lock, flags);
  1254. dev->IMR_cache &= ~(ISR_RXDESC | ISR_RXOK);
  1255. writel(dev->IMR_cache, dev->base + IMR);
  1256. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1257. tasklet_schedule(&dev->rx_tasklet);
  1258. //rx_irq(ndev);
  1259. //writel(4, dev->base + IHR);
  1260. }
  1261. if ((ISR_RXIDLE | ISR_RXORN | ISR_RXDESC | ISR_RXOK | ISR_RXERR) & isr)
  1262. ns83820_rx_kick(ndev);
  1263. if (unlikely(ISR_RXSOVR & isr)) {
  1264. //printk("overrun: rxsovr\n");
  1265. ndev->stats.rx_fifo_errors++;
  1266. }
  1267. if (unlikely(ISR_RXORN & isr)) {
  1268. //printk("overrun: rxorn\n");
  1269. ndev->stats.rx_fifo_errors++;
  1270. }
  1271. if ((ISR_RXRCMP & isr) && dev->rx_info.up)
  1272. writel(CR_RXE, dev->base + CR);
  1273. if (ISR_TXIDLE & isr) {
  1274. u32 txdp;
  1275. txdp = readl(dev->base + TXDP);
  1276. dprintk("txdp: %08x\n", txdp);
  1277. txdp -= dev->tx_phy_descs;
  1278. dev->tx_idx = txdp / (DESC_SIZE * 4);
  1279. if (dev->tx_idx >= NR_TX_DESC) {
  1280. printk(KERN_ALERT "%s: BUG -- txdp out of range\n", ndev->name);
  1281. dev->tx_idx = 0;
  1282. }
  1283. /* The may have been a race between a pci originated read
  1284. * and the descriptor update from the cpu. Just in case,
  1285. * kick the transmitter if the hardware thinks it is on a
  1286. * different descriptor than we are.
  1287. */
  1288. if (dev->tx_idx != dev->tx_free_idx)
  1289. kick_tx(dev);
  1290. }
  1291. /* Defer tx ring processing until more than a minimum amount of
  1292. * work has accumulated
  1293. */
  1294. if ((ISR_TXDESC | ISR_TXIDLE | ISR_TXOK | ISR_TXERR) & isr) {
  1295. spin_lock_irqsave(&dev->tx_lock, flags);
  1296. do_tx_done(ndev);
  1297. spin_unlock_irqrestore(&dev->tx_lock, flags);
  1298. /* Disable TxOk if there are no outstanding tx packets.
  1299. */
  1300. if ((dev->tx_done_idx == dev->tx_free_idx) &&
  1301. (dev->IMR_cache & ISR_TXOK)) {
  1302. spin_lock_irqsave(&dev->misc_lock, flags);
  1303. dev->IMR_cache &= ~ISR_TXOK;
  1304. writel(dev->IMR_cache, dev->base + IMR);
  1305. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1306. }
  1307. }
  1308. /* The TxIdle interrupt can come in before the transmit has
  1309. * completed. Normally we reap packets off of the combination
  1310. * of TxDesc and TxIdle and leave TxOk disabled (since it
  1311. * occurs on every packet), but when no further irqs of this
  1312. * nature are expected, we must enable TxOk.
  1313. */
  1314. if ((ISR_TXIDLE & isr) && (dev->tx_done_idx != dev->tx_free_idx)) {
  1315. spin_lock_irqsave(&dev->misc_lock, flags);
  1316. dev->IMR_cache |= ISR_TXOK;
  1317. writel(dev->IMR_cache, dev->base + IMR);
  1318. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1319. }
  1320. /* MIB interrupt: one of the statistics counters is about to overflow */
  1321. if (unlikely(ISR_MIB & isr))
  1322. ns83820_mib_isr(dev);
  1323. /* PHY: Link up/down/negotiation state change */
  1324. if (unlikely(ISR_PHY & isr))
  1325. phy_intr(ndev);
  1326. #if 0 /* Still working on the interrupt mitigation strategy */
  1327. if (dev->ihr)
  1328. writel(dev->ihr, dev->base + IHR);
  1329. #endif
  1330. }
  1331. static void ns83820_do_reset(struct ns83820 *dev, u32 which)
  1332. {
  1333. Dprintk("resetting chip...\n");
  1334. writel(which, dev->base + CR);
  1335. do {
  1336. schedule();
  1337. } while (readl(dev->base + CR) & which);
  1338. Dprintk("okay!\n");
  1339. }
  1340. static int ns83820_stop(struct net_device *ndev)
  1341. {
  1342. struct ns83820 *dev = PRIV(ndev);
  1343. /* FIXME: protect against interrupt handler? */
  1344. del_timer_sync(&dev->tx_watchdog);
  1345. ns83820_disable_interrupts(dev);
  1346. dev->rx_info.up = 0;
  1347. synchronize_irq(dev->pci_dev->irq);
  1348. ns83820_do_reset(dev, CR_RST);
  1349. synchronize_irq(dev->pci_dev->irq);
  1350. spin_lock_irq(&dev->misc_lock);
  1351. dev->IMR_cache &= ~(ISR_TXURN | ISR_TXIDLE | ISR_TXERR | ISR_TXDESC | ISR_TXOK);
  1352. spin_unlock_irq(&dev->misc_lock);
  1353. ns83820_cleanup_rx(dev);
  1354. ns83820_cleanup_tx(dev);
  1355. return 0;
  1356. }
  1357. static void ns83820_tx_timeout(struct net_device *ndev)
  1358. {
  1359. struct ns83820 *dev = PRIV(ndev);
  1360. u32 tx_done_idx;
  1361. __le32 *desc;
  1362. unsigned long flags;
  1363. spin_lock_irqsave(&dev->tx_lock, flags);
  1364. tx_done_idx = dev->tx_done_idx;
  1365. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  1366. printk(KERN_INFO "%s: tx_timeout: tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  1367. ndev->name,
  1368. tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
  1369. #if defined(DEBUG)
  1370. {
  1371. u32 isr;
  1372. isr = readl(dev->base + ISR);
  1373. printk("irq: %08x imr: %08x\n", isr, dev->IMR_cache);
  1374. ns83820_do_isr(ndev, isr);
  1375. }
  1376. #endif
  1377. do_tx_done(ndev);
  1378. tx_done_idx = dev->tx_done_idx;
  1379. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  1380. printk(KERN_INFO "%s: after: tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  1381. ndev->name,
  1382. tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
  1383. spin_unlock_irqrestore(&dev->tx_lock, flags);
  1384. }
  1385. static void ns83820_tx_watch(unsigned long data)
  1386. {
  1387. struct net_device *ndev = (void *)data;
  1388. struct ns83820 *dev = PRIV(ndev);
  1389. #if defined(DEBUG)
  1390. printk("ns83820_tx_watch: %u %u %d\n",
  1391. dev->tx_done_idx, dev->tx_free_idx, atomic_read(&dev->nr_tx_skbs)
  1392. );
  1393. #endif
  1394. if (time_after(jiffies, dev_trans_start(ndev) + 1*HZ) &&
  1395. dev->tx_done_idx != dev->tx_free_idx) {
  1396. printk(KERN_DEBUG "%s: ns83820_tx_watch: %u %u %d\n",
  1397. ndev->name,
  1398. dev->tx_done_idx, dev->tx_free_idx,
  1399. atomic_read(&dev->nr_tx_skbs));
  1400. ns83820_tx_timeout(ndev);
  1401. }
  1402. mod_timer(&dev->tx_watchdog, jiffies + 2*HZ);
  1403. }
  1404. static int ns83820_open(struct net_device *ndev)
  1405. {
  1406. struct ns83820 *dev = PRIV(ndev);
  1407. unsigned i;
  1408. u32 desc;
  1409. int ret;
  1410. dprintk("ns83820_open\n");
  1411. writel(0, dev->base + PQCR);
  1412. ret = ns83820_setup_rx(ndev);
  1413. if (ret)
  1414. goto failed;
  1415. memset(dev->tx_descs, 0, 4 * NR_TX_DESC * DESC_SIZE);
  1416. for (i=0; i<NR_TX_DESC; i++) {
  1417. dev->tx_descs[(i * DESC_SIZE) + DESC_LINK]
  1418. = cpu_to_le32(
  1419. dev->tx_phy_descs
  1420. + ((i+1) % NR_TX_DESC) * DESC_SIZE * 4);
  1421. }
  1422. dev->tx_idx = 0;
  1423. dev->tx_done_idx = 0;
  1424. desc = dev->tx_phy_descs;
  1425. writel(0, dev->base + TXDP_HI);
  1426. writel(desc, dev->base + TXDP);
  1427. init_timer(&dev->tx_watchdog);
  1428. dev->tx_watchdog.data = (unsigned long)ndev;
  1429. dev->tx_watchdog.function = ns83820_tx_watch;
  1430. mod_timer(&dev->tx_watchdog, jiffies + 2*HZ);
  1431. netif_start_queue(ndev); /* FIXME: wait for phy to come up */
  1432. return 0;
  1433. failed:
  1434. ns83820_stop(ndev);
  1435. return ret;
  1436. }
  1437. static void ns83820_getmac(struct ns83820 *dev, u8 *mac)
  1438. {
  1439. unsigned i;
  1440. for (i=0; i<3; i++) {
  1441. u32 data;
  1442. /* Read from the perfect match memory: this is loaded by
  1443. * the chip from the EEPROM via the EELOAD self test.
  1444. */
  1445. writel(i*2, dev->base + RFCR);
  1446. data = readl(dev->base + RFDR);
  1447. *mac++ = data;
  1448. *mac++ = data >> 8;
  1449. }
  1450. }
  1451. static int ns83820_change_mtu(struct net_device *ndev, int new_mtu)
  1452. {
  1453. if (new_mtu > RX_BUF_SIZE)
  1454. return -EINVAL;
  1455. ndev->mtu = new_mtu;
  1456. return 0;
  1457. }
  1458. static void ns83820_set_multicast(struct net_device *ndev)
  1459. {
  1460. struct ns83820 *dev = PRIV(ndev);
  1461. u8 __iomem *rfcr = dev->base + RFCR;
  1462. u32 and_mask = 0xffffffff;
  1463. u32 or_mask = 0;
  1464. u32 val;
  1465. if (ndev->flags & IFF_PROMISC)
  1466. or_mask |= RFCR_AAU | RFCR_AAM;
  1467. else
  1468. and_mask &= ~(RFCR_AAU | RFCR_AAM);
  1469. if (ndev->flags & IFF_ALLMULTI || netdev_mc_count(ndev))
  1470. or_mask |= RFCR_AAM;
  1471. else
  1472. and_mask &= ~RFCR_AAM;
  1473. spin_lock_irq(&dev->misc_lock);
  1474. val = (readl(rfcr) & and_mask) | or_mask;
  1475. /* Ramit : RFCR Write Fix doc says RFEN must be 0 modify other bits */
  1476. writel(val & ~RFCR_RFEN, rfcr);
  1477. writel(val, rfcr);
  1478. spin_unlock_irq(&dev->misc_lock);
  1479. }
  1480. static void ns83820_run_bist(struct net_device *ndev, const char *name, u32 enable, u32 done, u32 fail)
  1481. {
  1482. struct ns83820 *dev = PRIV(ndev);
  1483. int timed_out = 0;
  1484. unsigned long start;
  1485. u32 status;
  1486. int loops = 0;
  1487. dprintk("%s: start %s\n", ndev->name, name);
  1488. start = jiffies;
  1489. writel(enable, dev->base + PTSCR);
  1490. for (;;) {
  1491. loops++;
  1492. status = readl(dev->base + PTSCR);
  1493. if (!(status & enable))
  1494. break;
  1495. if (status & done)
  1496. break;
  1497. if (status & fail)
  1498. break;
  1499. if (time_after_eq(jiffies, start + HZ)) {
  1500. timed_out = 1;
  1501. break;
  1502. }
  1503. schedule_timeout_uninterruptible(1);
  1504. }
  1505. if (status & fail)
  1506. printk(KERN_INFO "%s: %s failed! (0x%08x & 0x%08x)\n",
  1507. ndev->name, name, status, fail);
  1508. else if (timed_out)
  1509. printk(KERN_INFO "%s: run_bist %s timed out! (%08x)\n",
  1510. ndev->name, name, status);
  1511. dprintk("%s: done %s in %d loops\n", ndev->name, name, loops);
  1512. }
  1513. #ifdef PHY_CODE_IS_FINISHED
  1514. static void ns83820_mii_write_bit(struct ns83820 *dev, int bit)
  1515. {
  1516. /* drive MDC low */
  1517. dev->MEAR_cache &= ~MEAR_MDC;
  1518. writel(dev->MEAR_cache, dev->base + MEAR);
  1519. readl(dev->base + MEAR);
  1520. /* enable output, set bit */
  1521. dev->MEAR_cache |= MEAR_MDDIR;
  1522. if (bit)
  1523. dev->MEAR_cache |= MEAR_MDIO;
  1524. else
  1525. dev->MEAR_cache &= ~MEAR_MDIO;
  1526. /* set the output bit */
  1527. writel(dev->MEAR_cache, dev->base + MEAR);
  1528. readl(dev->base + MEAR);
  1529. /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */
  1530. udelay(1);
  1531. /* drive MDC high causing the data bit to be latched */
  1532. dev->MEAR_cache |= MEAR_MDC;
  1533. writel(dev->MEAR_cache, dev->base + MEAR);
  1534. readl(dev->base + MEAR);
  1535. /* Wait again... */
  1536. udelay(1);
  1537. }
  1538. static int ns83820_mii_read_bit(struct ns83820 *dev)
  1539. {
  1540. int bit;
  1541. /* drive MDC low, disable output */
  1542. dev->MEAR_cache &= ~MEAR_MDC;
  1543. dev->MEAR_cache &= ~MEAR_MDDIR;
  1544. writel(dev->MEAR_cache, dev->base + MEAR);
  1545. readl(dev->base + MEAR);
  1546. /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */
  1547. udelay(1);
  1548. /* drive MDC high causing the data bit to be latched */
  1549. bit = (readl(dev->base + MEAR) & MEAR_MDIO) ? 1 : 0;
  1550. dev->MEAR_cache |= MEAR_MDC;
  1551. writel(dev->MEAR_cache, dev->base + MEAR);
  1552. /* Wait again... */
  1553. udelay(1);
  1554. return bit;
  1555. }
  1556. static unsigned ns83820_mii_read_reg(struct ns83820 *dev, unsigned phy, unsigned reg)
  1557. {
  1558. unsigned data = 0;
  1559. int i;
  1560. /* read some garbage so that we eventually sync up */
  1561. for (i=0; i<64; i++)
  1562. ns83820_mii_read_bit(dev);
  1563. ns83820_mii_write_bit(dev, 0); /* start */
  1564. ns83820_mii_write_bit(dev, 1);
  1565. ns83820_mii_write_bit(dev, 1); /* opcode read */
  1566. ns83820_mii_write_bit(dev, 0);
  1567. /* write out the phy address: 5 bits, msb first */
  1568. for (i=0; i<5; i++)
  1569. ns83820_mii_write_bit(dev, phy & (0x10 >> i));
  1570. /* write out the register address, 5 bits, msb first */
  1571. for (i=0; i<5; i++)
  1572. ns83820_mii_write_bit(dev, reg & (0x10 >> i));
  1573. ns83820_mii_read_bit(dev); /* turn around cycles */
  1574. ns83820_mii_read_bit(dev);
  1575. /* read in the register data, 16 bits msb first */
  1576. for (i=0; i<16; i++) {
  1577. data <<= 1;
  1578. data |= ns83820_mii_read_bit(dev);
  1579. }
  1580. return data;
  1581. }
  1582. static unsigned ns83820_mii_write_reg(struct ns83820 *dev, unsigned phy, unsigned reg, unsigned data)
  1583. {
  1584. int i;
  1585. /* read some garbage so that we eventually sync up */
  1586. for (i=0; i<64; i++)
  1587. ns83820_mii_read_bit(dev);
  1588. ns83820_mii_write_bit(dev, 0); /* start */
  1589. ns83820_mii_write_bit(dev, 1);
  1590. ns83820_mii_write_bit(dev, 0); /* opcode read */
  1591. ns83820_mii_write_bit(dev, 1);
  1592. /* write out the phy address: 5 bits, msb first */
  1593. for (i=0; i<5; i++)
  1594. ns83820_mii_write_bit(dev, phy & (0x10 >> i));
  1595. /* write out the register address, 5 bits, msb first */
  1596. for (i=0; i<5; i++)
  1597. ns83820_mii_write_bit(dev, reg & (0x10 >> i));
  1598. ns83820_mii_read_bit(dev); /* turn around cycles */
  1599. ns83820_mii_read_bit(dev);
  1600. /* read in the register data, 16 bits msb first */
  1601. for (i=0; i<16; i++)
  1602. ns83820_mii_write_bit(dev, (data >> (15 - i)) & 1);
  1603. return data;
  1604. }
  1605. static void ns83820_probe_phy(struct net_device *ndev)
  1606. {
  1607. struct ns83820 *dev = PRIV(ndev);
  1608. static int first;
  1609. int i;
  1610. #define MII_PHYIDR1 0x02
  1611. #define MII_PHYIDR2 0x03
  1612. #if 0
  1613. if (!first) {
  1614. unsigned tmp;
  1615. ns83820_mii_read_reg(dev, 1, 0x09);
  1616. ns83820_mii_write_reg(dev, 1, 0x10, 0x0d3e);
  1617. tmp = ns83820_mii_read_reg(dev, 1, 0x00);
  1618. ns83820_mii_write_reg(dev, 1, 0x00, tmp | 0x8000);
  1619. udelay(1300);
  1620. ns83820_mii_read_reg(dev, 1, 0x09);
  1621. }
  1622. #endif
  1623. first = 1;
  1624. for (i=1; i<2; i++) {
  1625. int j;
  1626. unsigned a, b;
  1627. a = ns83820_mii_read_reg(dev, i, MII_PHYIDR1);
  1628. b = ns83820_mii_read_reg(dev, i, MII_PHYIDR2);
  1629. //printk("%s: phy %d: 0x%04x 0x%04x\n",
  1630. // ndev->name, i, a, b);
  1631. for (j=0; j<0x16; j+=4) {
  1632. dprintk("%s: [0x%02x] %04x %04x %04x %04x\n",
  1633. ndev->name, j,
  1634. ns83820_mii_read_reg(dev, i, 0 + j),
  1635. ns83820_mii_read_reg(dev, i, 1 + j),
  1636. ns83820_mii_read_reg(dev, i, 2 + j),
  1637. ns83820_mii_read_reg(dev, i, 3 + j)
  1638. );
  1639. }
  1640. }
  1641. {
  1642. unsigned a, b;
  1643. /* read firmware version: memory addr is 0x8402 and 0x8403 */
  1644. ns83820_mii_write_reg(dev, 1, 0x16, 0x000d);
  1645. ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e);
  1646. a = ns83820_mii_read_reg(dev, 1, 0x1d);
  1647. ns83820_mii_write_reg(dev, 1, 0x16, 0x000d);
  1648. ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e);
  1649. b = ns83820_mii_read_reg(dev, 1, 0x1d);
  1650. dprintk("version: 0x%04x 0x%04x\n", a, b);
  1651. }
  1652. }
  1653. #endif
  1654. static const struct net_device_ops netdev_ops = {
  1655. .ndo_open = ns83820_open,
  1656. .ndo_stop = ns83820_stop,
  1657. .ndo_start_xmit = ns83820_hard_start_xmit,
  1658. .ndo_get_stats = ns83820_get_stats,
  1659. .ndo_change_mtu = ns83820_change_mtu,
  1660. .ndo_set_multicast_list = ns83820_set_multicast,
  1661. .ndo_validate_addr = eth_validate_addr,
  1662. .ndo_set_mac_address = eth_mac_addr,
  1663. .ndo_tx_timeout = ns83820_tx_timeout,
  1664. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1665. .ndo_vlan_rx_register = ns83820_vlan_rx_register,
  1666. #endif
  1667. };
  1668. static int __devinit ns83820_init_one(struct pci_dev *pci_dev,
  1669. const struct pci_device_id *id)
  1670. {
  1671. struct net_device *ndev;
  1672. struct ns83820 *dev;
  1673. long addr;
  1674. int err;
  1675. int using_dac = 0;
  1676. /* See if we can set the dma mask early on; failure is fatal. */
  1677. if (sizeof(dma_addr_t) == 8 &&
  1678. !pci_set_dma_mask(pci_dev, DMA_BIT_MASK(64))) {
  1679. using_dac = 1;
  1680. } else if (!pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) {
  1681. using_dac = 0;
  1682. } else {
  1683. dev_warn(&pci_dev->dev, "pci_set_dma_mask failed!\n");
  1684. return -ENODEV;
  1685. }
  1686. ndev = alloc_etherdev(sizeof(struct ns83820));
  1687. err = -ENOMEM;
  1688. if (!ndev)
  1689. goto out;
  1690. dev = PRIV(ndev);
  1691. dev->ndev = ndev;
  1692. spin_lock_init(&dev->rx_info.lock);
  1693. spin_lock_init(&dev->tx_lock);
  1694. spin_lock_init(&dev->misc_lock);
  1695. dev->pci_dev = pci_dev;
  1696. SET_NETDEV_DEV(ndev, &pci_dev->dev);
  1697. INIT_WORK(&dev->tq_refill, queue_refill);
  1698. tasklet_init(&dev->rx_tasklet, rx_action, (unsigned long)ndev);
  1699. err = pci_enable_device(pci_dev);
  1700. if (err) {
  1701. dev_info(&pci_dev->dev, "pci_enable_dev failed: %d\n", err);
  1702. goto out_free;
  1703. }
  1704. pci_set_master(pci_dev);
  1705. addr = pci_resource_start(pci_dev, 1);
  1706. dev->base = ioremap_nocache(addr, PAGE_SIZE);
  1707. dev->tx_descs = pci_alloc_consistent(pci_dev,
  1708. 4 * DESC_SIZE * NR_TX_DESC, &dev->tx_phy_descs);
  1709. dev->rx_info.descs = pci_alloc_consistent(pci_dev,
  1710. 4 * DESC_SIZE * NR_RX_DESC, &dev->rx_info.phy_descs);
  1711. err = -ENOMEM;
  1712. if (!dev->base || !dev->tx_descs || !dev->rx_info.descs)
  1713. goto out_disable;
  1714. dprintk("%p: %08lx %p: %08lx\n",
  1715. dev->tx_descs, (long)dev->tx_phy_descs,
  1716. dev->rx_info.descs, (long)dev->rx_info.phy_descs);
  1717. ns83820_disable_interrupts(dev);
  1718. dev->IMR_cache = 0;
  1719. err = request_irq(pci_dev->irq, ns83820_irq, IRQF_SHARED,
  1720. DRV_NAME, ndev);
  1721. if (err) {
  1722. dev_info(&pci_dev->dev, "unable to register irq %d, err %d\n",
  1723. pci_dev->irq, err);
  1724. goto out_disable;
  1725. }
  1726. /*
  1727. * FIXME: we are holding rtnl_lock() over obscenely long area only
  1728. * because some of the setup code uses dev->name. It's Wrong(tm) -
  1729. * we should be using driver-specific names for all that stuff.
  1730. * For now that will do, but we really need to come back and kill
  1731. * most of the dev_alloc_name() users later.
  1732. */
  1733. rtnl_lock();
  1734. err = dev_alloc_name(ndev, ndev->name);
  1735. if (err < 0) {
  1736. dev_info(&pci_dev->dev, "unable to get netdev name: %d\n", err);
  1737. goto out_free_irq;
  1738. }
  1739. printk("%s: ns83820.c: 0x22c: %08x, subsystem: %04x:%04x\n",
  1740. ndev->name, le32_to_cpu(readl(dev->base + 0x22c)),
  1741. pci_dev->subsystem_vendor, pci_dev->subsystem_device);
  1742. ndev->netdev_ops = &netdev_ops;
  1743. SET_ETHTOOL_OPS(ndev, &ops);
  1744. ndev->watchdog_timeo = 5 * HZ;
  1745. pci_set_drvdata(pci_dev, ndev);
  1746. ns83820_do_reset(dev, CR_RST);
  1747. /* Must reset the ram bist before running it */
  1748. writel(PTSCR_RBIST_RST, dev->base + PTSCR);
  1749. ns83820_run_bist(ndev, "sram bist", PTSCR_RBIST_EN,
  1750. PTSCR_RBIST_DONE, PTSCR_RBIST_FAIL);
  1751. ns83820_run_bist(ndev, "eeprom bist", PTSCR_EEBIST_EN, 0,
  1752. PTSCR_EEBIST_FAIL);
  1753. ns83820_run_bist(ndev, "eeprom load", PTSCR_EELOAD_EN, 0, 0);
  1754. /* I love config registers */
  1755. dev->CFG_cache = readl(dev->base + CFG);
  1756. if ((dev->CFG_cache & CFG_PCI64_DET)) {
  1757. printk(KERN_INFO "%s: detected 64 bit PCI data bus.\n",
  1758. ndev->name);
  1759. /*dev->CFG_cache |= CFG_DATA64_EN;*/
  1760. if (!(dev->CFG_cache & CFG_DATA64_EN))
  1761. printk(KERN_INFO "%s: EEPROM did not enable 64 bit bus. Disabled.\n",
  1762. ndev->name);
  1763. } else
  1764. dev->CFG_cache &= ~(CFG_DATA64_EN);
  1765. dev->CFG_cache &= (CFG_TBI_EN | CFG_MRM_DIS | CFG_MWI_DIS |
  1766. CFG_T64ADDR | CFG_DATA64_EN | CFG_EXT_125 |
  1767. CFG_M64ADDR);
  1768. dev->CFG_cache |= CFG_PINT_DUPSTS | CFG_PINT_LNKSTS | CFG_PINT_SPDSTS |
  1769. CFG_EXTSTS_EN | CFG_EXD | CFG_PESEL;
  1770. dev->CFG_cache |= CFG_REQALG;
  1771. dev->CFG_cache |= CFG_POW;
  1772. dev->CFG_cache |= CFG_TMRTEST;
  1773. /* When compiled with 64 bit addressing, we must always enable
  1774. * the 64 bit descriptor format.
  1775. */
  1776. if (sizeof(dma_addr_t) == 8)
  1777. dev->CFG_cache |= CFG_M64ADDR;
  1778. if (using_dac)
  1779. dev->CFG_cache |= CFG_T64ADDR;
  1780. /* Big endian mode does not seem to do what the docs suggest */
  1781. dev->CFG_cache &= ~CFG_BEM;
  1782. /* setup optical transceiver if we have one */
  1783. if (dev->CFG_cache & CFG_TBI_EN) {
  1784. printk(KERN_INFO "%s: enabling optical transceiver\n",
  1785. ndev->name);
  1786. writel(readl(dev->base + GPIOR) | 0x3e8, dev->base + GPIOR);
  1787. /* setup auto negotiation feature advertisement */
  1788. writel(readl(dev->base + TANAR)
  1789. | TANAR_HALF_DUP | TANAR_FULL_DUP,
  1790. dev->base + TANAR);
  1791. /* start auto negotiation */
  1792. writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN,
  1793. dev->base + TBICR);
  1794. writel(TBICR_MR_AN_ENABLE, dev->base + TBICR);
  1795. dev->linkstate = LINK_AUTONEGOTIATE;
  1796. dev->CFG_cache |= CFG_MODE_1000;
  1797. }
  1798. writel(dev->CFG_cache, dev->base + CFG);
  1799. dprintk("CFG: %08x\n", dev->CFG_cache);
  1800. if (reset_phy) {
  1801. printk(KERN_INFO "%s: resetting phy\n", ndev->name);
  1802. writel(dev->CFG_cache | CFG_PHY_RST, dev->base + CFG);
  1803. msleep(10);
  1804. writel(dev->CFG_cache, dev->base + CFG);
  1805. }
  1806. #if 0 /* Huh? This sets the PCI latency register. Should be done via
  1807. * the PCI layer. FIXME.
  1808. */
  1809. if (readl(dev->base + SRR))
  1810. writel(readl(dev->base+0x20c) | 0xfe00, dev->base + 0x20c);
  1811. #endif
  1812. /* Note! The DMA burst size interacts with packet
  1813. * transmission, such that the largest packet that
  1814. * can be transmitted is 8192 - FLTH - burst size.
  1815. * If only the transmit fifo was larger...
  1816. */
  1817. /* Ramit : 1024 DMA is not a good idea, it ends up banging
  1818. * some DELL and COMPAQ SMP systems */
  1819. writel(TXCFG_CSI | TXCFG_HBI | TXCFG_ATP | TXCFG_MXDMA512
  1820. | ((1600 / 32) * 0x100),
  1821. dev->base + TXCFG);
  1822. /* Flush the interrupt holdoff timer */
  1823. writel(0x000, dev->base + IHR);
  1824. writel(0x100, dev->base + IHR);
  1825. writel(0x000, dev->base + IHR);
  1826. /* Set Rx to full duplex, don't accept runt, errored, long or length
  1827. * range errored packets. Use 512 byte DMA.
  1828. */
  1829. /* Ramit : 1024 DMA is not a good idea, it ends up banging
  1830. * some DELL and COMPAQ SMP systems
  1831. * Turn on ALP, only we are accpeting Jumbo Packets */
  1832. writel(RXCFG_AEP | RXCFG_ARP | RXCFG_AIRL | RXCFG_RX_FD
  1833. | RXCFG_STRIPCRC
  1834. //| RXCFG_ALP
  1835. | (RXCFG_MXDMA512) | 0, dev->base + RXCFG);
  1836. /* Disable priority queueing */
  1837. writel(0, dev->base + PQCR);
  1838. /* Enable IP checksum validation and detetion of VLAN headers.
  1839. * Note: do not set the reject options as at least the 0x102
  1840. * revision of the chip does not properly accept IP fragments
  1841. * at least for UDP.
  1842. */
  1843. /* Ramit : Be sure to turn on RXCFG_ARP if VLAN's are enabled, since
  1844. * the MAC it calculates the packetsize AFTER stripping the VLAN
  1845. * header, and if a VLAN Tagged packet of 64 bytes is received (like
  1846. * a ping with a VLAN header) then the card, strips the 4 byte VLAN
  1847. * tag and then checks the packet size, so if RXCFG_ARP is not enabled,
  1848. * it discrards it!. These guys......
  1849. * also turn on tag stripping if hardware acceleration is enabled
  1850. */
  1851. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1852. #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN|VRCR_VTREN)
  1853. #else
  1854. #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN)
  1855. #endif
  1856. writel(VRCR_INIT_VALUE, dev->base + VRCR);
  1857. /* Enable per-packet TCP/UDP/IP checksumming
  1858. * and per packet vlan tag insertion if
  1859. * vlan hardware acceleration is enabled
  1860. */
  1861. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1862. #define VTCR_INIT_VALUE (VTCR_PPCHK|VTCR_VPPTI)
  1863. #else
  1864. #define VTCR_INIT_VALUE VTCR_PPCHK
  1865. #endif
  1866. writel(VTCR_INIT_VALUE, dev->base + VTCR);
  1867. /* Ramit : Enable async and sync pause frames */
  1868. /* writel(0, dev->base + PCR); */
  1869. writel((PCR_PS_MCAST | PCR_PS_DA | PCR_PSEN | PCR_FFLO_4K |
  1870. PCR_FFHI_8K | PCR_STLO_4 | PCR_STHI_8 | PCR_PAUSE_CNT),
  1871. dev->base + PCR);
  1872. /* Disable Wake On Lan */
  1873. writel(0, dev->base + WCSR);
  1874. ns83820_getmac(dev, ndev->dev_addr);
  1875. /* Yes, we support dumb IP checksum on transmit */
  1876. ndev->features |= NETIF_F_SG;
  1877. ndev->features |= NETIF_F_IP_CSUM;
  1878. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1879. /* We also support hardware vlan acceleration */
  1880. ndev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1881. #endif
  1882. if (using_dac) {
  1883. printk(KERN_INFO "%s: using 64 bit addressing.\n",
  1884. ndev->name);
  1885. ndev->features |= NETIF_F_HIGHDMA;
  1886. }
  1887. printk(KERN_INFO "%s: ns83820 v" VERSION ": DP83820 v%u.%u: %pM io=0x%08lx irq=%d f=%s\n",
  1888. ndev->name,
  1889. (unsigned)readl(dev->base + SRR) >> 8,
  1890. (unsigned)readl(dev->base + SRR) & 0xff,
  1891. ndev->dev_addr, addr, pci_dev->irq,
  1892. (ndev->features & NETIF_F_HIGHDMA) ? "h,sg" : "sg"
  1893. );
  1894. #ifdef PHY_CODE_IS_FINISHED
  1895. ns83820_probe_phy(ndev);
  1896. #endif
  1897. err = register_netdevice(ndev);
  1898. if (err) {
  1899. printk(KERN_INFO "ns83820: unable to register netdev: %d\n", err);
  1900. goto out_cleanup;
  1901. }
  1902. rtnl_unlock();
  1903. return 0;
  1904. out_cleanup:
  1905. ns83820_disable_interrupts(dev); /* paranoia */
  1906. out_free_irq:
  1907. rtnl_unlock();
  1908. free_irq(pci_dev->irq, ndev);
  1909. out_disable:
  1910. if (dev->base)
  1911. iounmap(dev->base);
  1912. pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_TX_DESC, dev->tx_descs, dev->tx_phy_descs);
  1913. pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_RX_DESC, dev->rx_info.descs, dev->rx_info.phy_descs);
  1914. pci_disable_device(pci_dev);
  1915. out_free:
  1916. free_netdev(ndev);
  1917. pci_set_drvdata(pci_dev, NULL);
  1918. out:
  1919. return err;
  1920. }
  1921. static void __devexit ns83820_remove_one(struct pci_dev *pci_dev)
  1922. {
  1923. struct net_device *ndev = pci_get_drvdata(pci_dev);
  1924. struct ns83820 *dev = PRIV(ndev); /* ok even if NULL */
  1925. if (!ndev) /* paranoia */
  1926. return;
  1927. ns83820_disable_interrupts(dev); /* paranoia */
  1928. unregister_netdev(ndev);
  1929. free_irq(dev->pci_dev->irq, ndev);
  1930. iounmap(dev->base);
  1931. pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_TX_DESC,
  1932. dev->tx_descs, dev->tx_phy_descs);
  1933. pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_RX_DESC,
  1934. dev->rx_info.descs, dev->rx_info.phy_descs);
  1935. pci_disable_device(dev->pci_dev);
  1936. free_netdev(ndev);
  1937. pci_set_drvdata(pci_dev, NULL);
  1938. }
  1939. static DEFINE_PCI_DEVICE_TABLE(ns83820_pci_tbl) = {
  1940. { 0x100b, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, .driver_data = 0, },
  1941. { 0, },
  1942. };
  1943. static struct pci_driver driver = {
  1944. .name = "ns83820",
  1945. .id_table = ns83820_pci_tbl,
  1946. .probe = ns83820_init_one,
  1947. .remove = __devexit_p(ns83820_remove_one),
  1948. #if 0 /* FIXME: implement */
  1949. .suspend = ,
  1950. .resume = ,
  1951. #endif
  1952. };
  1953. static int __init ns83820_init(void)
  1954. {
  1955. printk(KERN_INFO "ns83820.c: National Semiconductor DP83820 10/100/1000 driver.\n");
  1956. return pci_register_driver(&driver);
  1957. }
  1958. static void __exit ns83820_exit(void)
  1959. {
  1960. pci_unregister_driver(&driver);
  1961. }
  1962. MODULE_AUTHOR("Benjamin LaHaise <bcrl@kvack.org>");
  1963. MODULE_DESCRIPTION("National Semiconductor DP83820 10/100/1000 driver");
  1964. MODULE_LICENSE("GPL");
  1965. MODULE_DEVICE_TABLE(pci, ns83820_pci_tbl);
  1966. module_param(lnksts, int, 0);
  1967. MODULE_PARM_DESC(lnksts, "Polarity of LNKSTS bit");
  1968. module_param(ihr, int, 0);
  1969. MODULE_PARM_DESC(ihr, "Time in 100 us increments to delay interrupts (range 0-127)");
  1970. module_param(reset_phy, int, 0);
  1971. MODULE_PARM_DESC(reset_phy, "Set to 1 to reset the PHY on startup");
  1972. module_init(ns83820_init);
  1973. module_exit(ns83820_exit);