ns83820.c 61 KB

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