ns83820.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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/interrupt.h>
  109. #include <linux/ip.h> /* for iph */
  110. #include <linux/in.h> /* for IPPROTO_... */
  111. #include <linux/compiler.h>
  112. #include <linux/prefetch.h>
  113. #include <linux/ethtool.h>
  114. #include <linux/sched.h>
  115. #include <linux/timer.h>
  116. #include <linux/if_vlan.h>
  117. #include <linux/rtnetlink.h>
  118. #include <linux/jiffies.h>
  119. #include <linux/slab.h>
  120. #include <asm/io.h>
  121. #include <asm/uaccess.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. struct rx_info rx_info;
  387. struct tasklet_struct rx_tasklet;
  388. unsigned ihr;
  389. struct work_struct tq_refill;
  390. /* protects everything below. irqsave when using. */
  391. spinlock_t misc_lock;
  392. u32 CFG_cache;
  393. u32 MEAR_cache;
  394. u32 IMR_cache;
  395. unsigned linkstate;
  396. spinlock_t tx_lock;
  397. u16 tx_done_idx;
  398. u16 tx_idx;
  399. volatile u16 tx_free_idx; /* idx of free desc chain */
  400. u16 tx_intr_idx;
  401. atomic_t nr_tx_skbs;
  402. struct sk_buff *tx_skbs[NR_TX_DESC];
  403. char pad[16] __attribute__((aligned(16)));
  404. __le32 *tx_descs;
  405. dma_addr_t tx_phy_descs;
  406. struct timer_list tx_watchdog;
  407. };
  408. static inline struct ns83820 *PRIV(struct net_device *dev)
  409. {
  410. return netdev_priv(dev);
  411. }
  412. #define __kick_rx(dev) writel(CR_RXE, dev->base + CR)
  413. static inline void kick_rx(struct net_device *ndev)
  414. {
  415. struct ns83820 *dev = PRIV(ndev);
  416. dprintk("kick_rx: maybe kicking\n");
  417. if (test_and_clear_bit(0, &dev->rx_info.idle)) {
  418. dprintk("actually kicking\n");
  419. writel(dev->rx_info.phy_descs +
  420. (4 * DESC_SIZE * dev->rx_info.next_rx),
  421. dev->base + RXDP);
  422. if (dev->rx_info.next_rx == dev->rx_info.next_empty)
  423. printk(KERN_DEBUG "%s: uh-oh: next_rx == next_empty???\n",
  424. ndev->name);
  425. __kick_rx(dev);
  426. }
  427. }
  428. //free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC
  429. #define start_tx_okay(dev) \
  430. (((NR_TX_DESC-2 + dev->tx_done_idx - dev->tx_free_idx) % NR_TX_DESC) > MIN_TX_DESC_FREE)
  431. /* Packet Receiver
  432. *
  433. * The hardware supports linked lists of receive descriptors for
  434. * which ownership is transferred back and forth by means of an
  435. * ownership bit. While the hardware does support the use of a
  436. * ring for receive descriptors, we only make use of a chain in
  437. * an attempt to reduce bus traffic under heavy load scenarios.
  438. * This will also make bugs a bit more obvious. The current code
  439. * only makes use of a single rx chain; I hope to implement
  440. * priority based rx for version 1.0. Goal: even under overload
  441. * conditions, still route realtime traffic with as low jitter as
  442. * possible.
  443. */
  444. static inline void build_rx_desc(struct ns83820 *dev, __le32 *desc, dma_addr_t link, dma_addr_t buf, u32 cmdsts, u32 extsts)
  445. {
  446. desc_addr_set(desc + DESC_LINK, link);
  447. desc_addr_set(desc + DESC_BUFPTR, buf);
  448. desc[DESC_EXTSTS] = cpu_to_le32(extsts);
  449. mb();
  450. desc[DESC_CMDSTS] = cpu_to_le32(cmdsts);
  451. }
  452. #define nr_rx_empty(dev) ((NR_RX_DESC-2 + dev->rx_info.next_rx - dev->rx_info.next_empty) % NR_RX_DESC)
  453. static inline int ns83820_add_rx_skb(struct ns83820 *dev, struct sk_buff *skb)
  454. {
  455. unsigned next_empty;
  456. u32 cmdsts;
  457. __le32 *sg;
  458. dma_addr_t buf;
  459. next_empty = dev->rx_info.next_empty;
  460. /* don't overrun last rx marker */
  461. if (unlikely(nr_rx_empty(dev) <= 2)) {
  462. kfree_skb(skb);
  463. return 1;
  464. }
  465. #if 0
  466. dprintk("next_empty[%d] nr_used[%d] next_rx[%d]\n",
  467. dev->rx_info.next_empty,
  468. dev->rx_info.nr_used,
  469. dev->rx_info.next_rx
  470. );
  471. #endif
  472. sg = dev->rx_info.descs + (next_empty * DESC_SIZE);
  473. BUG_ON(NULL != dev->rx_info.skbs[next_empty]);
  474. dev->rx_info.skbs[next_empty] = skb;
  475. dev->rx_info.next_empty = (next_empty + 1) % NR_RX_DESC;
  476. cmdsts = REAL_RX_BUF_SIZE | CMDSTS_INTR;
  477. buf = pci_map_single(dev->pci_dev, skb->data,
  478. REAL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  479. build_rx_desc(dev, sg, 0, buf, cmdsts, 0);
  480. /* update link of previous rx */
  481. if (likely(next_empty != dev->rx_info.next_rx))
  482. 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));
  483. return 0;
  484. }
  485. static inline int rx_refill(struct net_device *ndev, gfp_t gfp)
  486. {
  487. struct ns83820 *dev = PRIV(ndev);
  488. unsigned i;
  489. unsigned long flags = 0;
  490. if (unlikely(nr_rx_empty(dev) <= 2))
  491. return 0;
  492. dprintk("rx_refill(%p)\n", ndev);
  493. if (gfp == GFP_ATOMIC)
  494. spin_lock_irqsave(&dev->rx_info.lock, flags);
  495. for (i=0; i<NR_RX_DESC; i++) {
  496. struct sk_buff *skb;
  497. long res;
  498. /* extra 16 bytes for alignment */
  499. skb = __netdev_alloc_skb(ndev, REAL_RX_BUF_SIZE+16, gfp);
  500. if (unlikely(!skb))
  501. break;
  502. skb_reserve(skb, skb->data - PTR_ALIGN(skb->data, 16));
  503. if (gfp != GFP_ATOMIC)
  504. spin_lock_irqsave(&dev->rx_info.lock, flags);
  505. res = ns83820_add_rx_skb(dev, skb);
  506. if (gfp != GFP_ATOMIC)
  507. spin_unlock_irqrestore(&dev->rx_info.lock, flags);
  508. if (res) {
  509. i = 1;
  510. break;
  511. }
  512. }
  513. if (gfp == GFP_ATOMIC)
  514. spin_unlock_irqrestore(&dev->rx_info.lock, flags);
  515. return i ? 0 : -ENOMEM;
  516. }
  517. static void rx_refill_atomic(struct net_device *ndev)
  518. {
  519. rx_refill(ndev, GFP_ATOMIC);
  520. }
  521. /* REFILL */
  522. static inline void queue_refill(struct work_struct *work)
  523. {
  524. struct ns83820 *dev = container_of(work, struct ns83820, tq_refill);
  525. struct net_device *ndev = dev->ndev;
  526. rx_refill(ndev, GFP_KERNEL);
  527. if (dev->rx_info.up)
  528. kick_rx(ndev);
  529. }
  530. static inline void clear_rx_desc(struct ns83820 *dev, unsigned i)
  531. {
  532. build_rx_desc(dev, dev->rx_info.descs + (DESC_SIZE * i), 0, 0, CMDSTS_OWN, 0);
  533. }
  534. static void phy_intr(struct net_device *ndev)
  535. {
  536. struct ns83820 *dev = PRIV(ndev);
  537. static const char *speeds[] = { "10", "100", "1000", "1000(?)", "1000F" };
  538. u32 cfg, new_cfg;
  539. u32 tbisr, tanar, tanlpar;
  540. int speed, fullduplex, newlinkstate;
  541. cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  542. if (dev->CFG_cache & CFG_TBI_EN) {
  543. /* we have an optical transceiver */
  544. tbisr = readl(dev->base + TBISR);
  545. tanar = readl(dev->base + TANAR);
  546. tanlpar = readl(dev->base + TANLPAR);
  547. dprintk("phy_intr: tbisr=%08x, tanar=%08x, tanlpar=%08x\n",
  548. tbisr, tanar, tanlpar);
  549. if ( (fullduplex = (tanlpar & TANAR_FULL_DUP) &&
  550. (tanar & TANAR_FULL_DUP)) ) {
  551. /* both of us are full duplex */
  552. writel(readl(dev->base + TXCFG)
  553. | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP,
  554. dev->base + TXCFG);
  555. writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
  556. dev->base + RXCFG);
  557. /* Light up full duplex LED */
  558. writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT,
  559. dev->base + GPIOR);
  560. } else if (((tanlpar & TANAR_HALF_DUP) &&
  561. (tanar & TANAR_HALF_DUP)) ||
  562. ((tanlpar & TANAR_FULL_DUP) &&
  563. (tanar & TANAR_HALF_DUP)) ||
  564. ((tanlpar & TANAR_HALF_DUP) &&
  565. (tanar & TANAR_FULL_DUP))) {
  566. /* one or both of us are half duplex */
  567. writel((readl(dev->base + TXCFG)
  568. & ~(TXCFG_CSI | TXCFG_HBI)) | TXCFG_ATP,
  569. dev->base + TXCFG);
  570. writel(readl(dev->base + RXCFG) & ~RXCFG_RX_FD,
  571. dev->base + RXCFG);
  572. /* Turn off full duplex LED */
  573. writel(readl(dev->base + GPIOR) & ~GPIOR_GP1_OUT,
  574. dev->base + GPIOR);
  575. }
  576. speed = 4; /* 1000F */
  577. } else {
  578. /* we have a copper transceiver */
  579. new_cfg = dev->CFG_cache & ~(CFG_SB | CFG_MODE_1000 | CFG_SPDSTS);
  580. if (cfg & CFG_SPDSTS1)
  581. new_cfg |= CFG_MODE_1000;
  582. else
  583. new_cfg &= ~CFG_MODE_1000;
  584. speed = ((cfg / CFG_SPDSTS0) & 3);
  585. fullduplex = (cfg & CFG_DUPSTS);
  586. if (fullduplex) {
  587. new_cfg |= CFG_SB;
  588. writel(readl(dev->base + TXCFG)
  589. | TXCFG_CSI | TXCFG_HBI,
  590. dev->base + TXCFG);
  591. writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
  592. dev->base + RXCFG);
  593. } else {
  594. writel(readl(dev->base + TXCFG)
  595. & ~(TXCFG_CSI | TXCFG_HBI),
  596. dev->base + TXCFG);
  597. writel(readl(dev->base + RXCFG) & ~(RXCFG_RX_FD),
  598. dev->base + RXCFG);
  599. }
  600. if ((cfg & CFG_LNKSTS) &&
  601. ((new_cfg ^ dev->CFG_cache) != 0)) {
  602. writel(new_cfg, dev->base + CFG);
  603. dev->CFG_cache = new_cfg;
  604. }
  605. dev->CFG_cache &= ~CFG_SPDSTS;
  606. dev->CFG_cache |= cfg & CFG_SPDSTS;
  607. }
  608. newlinkstate = (cfg & CFG_LNKSTS) ? LINK_UP : LINK_DOWN;
  609. if (newlinkstate & LINK_UP &&
  610. dev->linkstate != newlinkstate) {
  611. netif_start_queue(ndev);
  612. netif_wake_queue(ndev);
  613. printk(KERN_INFO "%s: link now %s mbps, %s duplex and up.\n",
  614. ndev->name,
  615. speeds[speed],
  616. fullduplex ? "full" : "half");
  617. } else if (newlinkstate & LINK_DOWN &&
  618. dev->linkstate != newlinkstate) {
  619. netif_stop_queue(ndev);
  620. printk(KERN_INFO "%s: link now down.\n", ndev->name);
  621. }
  622. dev->linkstate = newlinkstate;
  623. }
  624. static int ns83820_setup_rx(struct net_device *ndev)
  625. {
  626. struct ns83820 *dev = PRIV(ndev);
  627. unsigned i;
  628. int ret;
  629. dprintk("ns83820_setup_rx(%p)\n", ndev);
  630. dev->rx_info.idle = 1;
  631. dev->rx_info.next_rx = 0;
  632. dev->rx_info.next_rx_desc = dev->rx_info.descs;
  633. dev->rx_info.next_empty = 0;
  634. for (i=0; i<NR_RX_DESC; i++)
  635. clear_rx_desc(dev, i);
  636. writel(0, dev->base + RXDP_HI);
  637. writel(dev->rx_info.phy_descs, dev->base + RXDP);
  638. ret = rx_refill(ndev, GFP_KERNEL);
  639. if (!ret) {
  640. dprintk("starting receiver\n");
  641. /* prevent the interrupt handler from stomping on us */
  642. spin_lock_irq(&dev->rx_info.lock);
  643. writel(0x0001, dev->base + CCSR);
  644. writel(0, dev->base + RFCR);
  645. writel(0x7fc00000, dev->base + RFCR);
  646. writel(0xffc00000, dev->base + RFCR);
  647. dev->rx_info.up = 1;
  648. phy_intr(ndev);
  649. /* Okay, let it rip */
  650. spin_lock(&dev->misc_lock);
  651. dev->IMR_cache |= ISR_PHY;
  652. dev->IMR_cache |= ISR_RXRCMP;
  653. //dev->IMR_cache |= ISR_RXERR;
  654. //dev->IMR_cache |= ISR_RXOK;
  655. dev->IMR_cache |= ISR_RXORN;
  656. dev->IMR_cache |= ISR_RXSOVR;
  657. dev->IMR_cache |= ISR_RXDESC;
  658. dev->IMR_cache |= ISR_RXIDLE;
  659. dev->IMR_cache |= ISR_TXDESC;
  660. dev->IMR_cache |= ISR_TXIDLE;
  661. writel(dev->IMR_cache, dev->base + IMR);
  662. writel(1, dev->base + IER);
  663. spin_unlock(&dev->misc_lock);
  664. kick_rx(ndev);
  665. spin_unlock_irq(&dev->rx_info.lock);
  666. }
  667. return ret;
  668. }
  669. static void ns83820_cleanup_rx(struct ns83820 *dev)
  670. {
  671. unsigned i;
  672. unsigned long flags;
  673. dprintk("ns83820_cleanup_rx(%p)\n", dev);
  674. /* disable receive interrupts */
  675. spin_lock_irqsave(&dev->misc_lock, flags);
  676. dev->IMR_cache &= ~(ISR_RXOK | ISR_RXDESC | ISR_RXERR | ISR_RXEARLY | ISR_RXIDLE);
  677. writel(dev->IMR_cache, dev->base + IMR);
  678. spin_unlock_irqrestore(&dev->misc_lock, flags);
  679. /* synchronize with the interrupt handler and kill it */
  680. dev->rx_info.up = 0;
  681. synchronize_irq(dev->pci_dev->irq);
  682. /* touch the pci bus... */
  683. readl(dev->base + IMR);
  684. /* assumes the transmitter is already disabled and reset */
  685. writel(0, dev->base + RXDP_HI);
  686. writel(0, dev->base + RXDP);
  687. for (i=0; i<NR_RX_DESC; i++) {
  688. struct sk_buff *skb = dev->rx_info.skbs[i];
  689. dev->rx_info.skbs[i] = NULL;
  690. clear_rx_desc(dev, i);
  691. kfree_skb(skb);
  692. }
  693. }
  694. static void ns83820_rx_kick(struct net_device *ndev)
  695. {
  696. struct ns83820 *dev = PRIV(ndev);
  697. /*if (nr_rx_empty(dev) >= NR_RX_DESC/4)*/ {
  698. if (dev->rx_info.up) {
  699. rx_refill_atomic(ndev);
  700. kick_rx(ndev);
  701. }
  702. }
  703. if (dev->rx_info.up && nr_rx_empty(dev) > NR_RX_DESC*3/4)
  704. schedule_work(&dev->tq_refill);
  705. else
  706. kick_rx(ndev);
  707. if (dev->rx_info.idle)
  708. printk(KERN_DEBUG "%s: BAD\n", ndev->name);
  709. }
  710. /* rx_irq
  711. *
  712. */
  713. static void rx_irq(struct net_device *ndev)
  714. {
  715. struct ns83820 *dev = PRIV(ndev);
  716. struct rx_info *info = &dev->rx_info;
  717. unsigned next_rx;
  718. int rx_rc, len;
  719. u32 cmdsts;
  720. __le32 *desc;
  721. unsigned long flags;
  722. int nr = 0;
  723. dprintk("rx_irq(%p)\n", ndev);
  724. dprintk("rxdp: %08x, descs: %08lx next_rx[%d]: %p next_empty[%d]: %p\n",
  725. readl(dev->base + RXDP),
  726. (long)(dev->rx_info.phy_descs),
  727. (int)dev->rx_info.next_rx,
  728. (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_rx)),
  729. (int)dev->rx_info.next_empty,
  730. (dev->rx_info.descs + (DESC_SIZE * dev->rx_info.next_empty))
  731. );
  732. spin_lock_irqsave(&info->lock, flags);
  733. if (!info->up)
  734. goto out;
  735. dprintk("walking descs\n");
  736. next_rx = info->next_rx;
  737. desc = info->next_rx_desc;
  738. while ((CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) &&
  739. (cmdsts != CMDSTS_OWN)) {
  740. struct sk_buff *skb;
  741. u32 extsts = le32_to_cpu(desc[DESC_EXTSTS]);
  742. dma_addr_t bufptr = desc_addr_get(desc + DESC_BUFPTR);
  743. dprintk("cmdsts: %08x\n", cmdsts);
  744. dprintk("link: %08x\n", cpu_to_le32(desc[DESC_LINK]));
  745. dprintk("extsts: %08x\n", extsts);
  746. skb = info->skbs[next_rx];
  747. info->skbs[next_rx] = NULL;
  748. info->next_rx = (next_rx + 1) % NR_RX_DESC;
  749. mb();
  750. clear_rx_desc(dev, next_rx);
  751. pci_unmap_single(dev->pci_dev, bufptr,
  752. RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  753. len = cmdsts & CMDSTS_LEN_MASK;
  754. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  755. /* NH: As was mentioned below, this chip is kinda
  756. * brain dead about vlan tag stripping. Frames
  757. * that are 64 bytes with a vlan header appended
  758. * like arp frames, or pings, are flagged as Runts
  759. * when the tag is stripped and hardware. This
  760. * also means that the OK bit in the descriptor
  761. * is cleared when the frame comes in so we have
  762. * to do a specific length check here to make sure
  763. * the frame would have been ok, had we not stripped
  764. * the tag.
  765. */
  766. if (likely((CMDSTS_OK & cmdsts) ||
  767. ((cmdsts & CMDSTS_RUNT) && len >= 56))) {
  768. #else
  769. if (likely(CMDSTS_OK & cmdsts)) {
  770. #endif
  771. skb_put(skb, len);
  772. if (unlikely(!skb))
  773. goto netdev_mangle_me_harder_failed;
  774. if (cmdsts & CMDSTS_DEST_MULTI)
  775. ndev->stats.multicast++;
  776. ndev->stats.rx_packets++;
  777. ndev->stats.rx_bytes += len;
  778. if ((extsts & 0x002a0000) && !(extsts & 0x00540000)) {
  779. skb->ip_summed = CHECKSUM_UNNECESSARY;
  780. } else {
  781. skb_checksum_none_assert(skb);
  782. }
  783. skb->protocol = eth_type_trans(skb, ndev);
  784. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  785. if(extsts & EXTSTS_VPKT) {
  786. unsigned short tag;
  787. tag = ntohs(extsts & EXTSTS_VTG_MASK);
  788. __vlan_hwaccel_put_tag(skb, tag);
  789. }
  790. #endif
  791. rx_rc = netif_rx(skb);
  792. if (NET_RX_DROP == rx_rc) {
  793. netdev_mangle_me_harder_failed:
  794. ndev->stats.rx_dropped++;
  795. }
  796. } else {
  797. kfree_skb(skb);
  798. }
  799. nr++;
  800. next_rx = info->next_rx;
  801. desc = info->descs + (DESC_SIZE * next_rx);
  802. }
  803. info->next_rx = next_rx;
  804. info->next_rx_desc = info->descs + (DESC_SIZE * next_rx);
  805. out:
  806. if (0 && !nr) {
  807. Dprintk("dazed: cmdsts_f: %08x\n", cmdsts);
  808. }
  809. spin_unlock_irqrestore(&info->lock, flags);
  810. }
  811. static void rx_action(unsigned long _dev)
  812. {
  813. struct net_device *ndev = (void *)_dev;
  814. struct ns83820 *dev = PRIV(ndev);
  815. rx_irq(ndev);
  816. writel(ihr, dev->base + IHR);
  817. spin_lock_irq(&dev->misc_lock);
  818. dev->IMR_cache |= ISR_RXDESC;
  819. writel(dev->IMR_cache, dev->base + IMR);
  820. spin_unlock_irq(&dev->misc_lock);
  821. rx_irq(ndev);
  822. ns83820_rx_kick(ndev);
  823. }
  824. /* Packet Transmit code
  825. */
  826. static inline void kick_tx(struct ns83820 *dev)
  827. {
  828. dprintk("kick_tx(%p): tx_idx=%d free_idx=%d\n",
  829. dev, dev->tx_idx, dev->tx_free_idx);
  830. writel(CR_TXE, dev->base + CR);
  831. }
  832. /* No spinlock needed on the transmit irq path as the interrupt handler is
  833. * serialized.
  834. */
  835. static void do_tx_done(struct net_device *ndev)
  836. {
  837. struct ns83820 *dev = PRIV(ndev);
  838. u32 cmdsts, tx_done_idx;
  839. __le32 *desc;
  840. dprintk("do_tx_done(%p)\n", ndev);
  841. tx_done_idx = dev->tx_done_idx;
  842. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  843. dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  844. tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
  845. while ((tx_done_idx != dev->tx_free_idx) &&
  846. !(CMDSTS_OWN & (cmdsts = le32_to_cpu(desc[DESC_CMDSTS]))) ) {
  847. struct sk_buff *skb;
  848. unsigned len;
  849. dma_addr_t addr;
  850. if (cmdsts & CMDSTS_ERR)
  851. ndev->stats.tx_errors++;
  852. if (cmdsts & CMDSTS_OK)
  853. ndev->stats.tx_packets++;
  854. if (cmdsts & CMDSTS_OK)
  855. ndev->stats.tx_bytes += cmdsts & 0xffff;
  856. dprintk("tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  857. tx_done_idx, dev->tx_free_idx, cmdsts);
  858. skb = dev->tx_skbs[tx_done_idx];
  859. dev->tx_skbs[tx_done_idx] = NULL;
  860. dprintk("done(%p)\n", skb);
  861. len = cmdsts & CMDSTS_LEN_MASK;
  862. addr = desc_addr_get(desc + DESC_BUFPTR);
  863. if (skb) {
  864. pci_unmap_single(dev->pci_dev,
  865. addr,
  866. len,
  867. PCI_DMA_TODEVICE);
  868. dev_kfree_skb_irq(skb);
  869. atomic_dec(&dev->nr_tx_skbs);
  870. } else
  871. pci_unmap_page(dev->pci_dev,
  872. addr,
  873. len,
  874. PCI_DMA_TODEVICE);
  875. tx_done_idx = (tx_done_idx + 1) % NR_TX_DESC;
  876. dev->tx_done_idx = tx_done_idx;
  877. desc[DESC_CMDSTS] = cpu_to_le32(0);
  878. mb();
  879. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  880. }
  881. /* Allow network stack to resume queueing packets after we've
  882. * finished transmitting at least 1/4 of the packets in the queue.
  883. */
  884. if (netif_queue_stopped(ndev) && start_tx_okay(dev)) {
  885. dprintk("start_queue(%p)\n", ndev);
  886. netif_start_queue(ndev);
  887. netif_wake_queue(ndev);
  888. }
  889. }
  890. static void ns83820_cleanup_tx(struct ns83820 *dev)
  891. {
  892. unsigned i;
  893. for (i=0; i<NR_TX_DESC; i++) {
  894. struct sk_buff *skb = dev->tx_skbs[i];
  895. dev->tx_skbs[i] = NULL;
  896. if (skb) {
  897. __le32 *desc = dev->tx_descs + (i * DESC_SIZE);
  898. pci_unmap_single(dev->pci_dev,
  899. desc_addr_get(desc + DESC_BUFPTR),
  900. le32_to_cpu(desc[DESC_CMDSTS]) & CMDSTS_LEN_MASK,
  901. PCI_DMA_TODEVICE);
  902. dev_kfree_skb_irq(skb);
  903. atomic_dec(&dev->nr_tx_skbs);
  904. }
  905. }
  906. memset(dev->tx_descs, 0, NR_TX_DESC * DESC_SIZE * 4);
  907. }
  908. /* transmit routine. This code relies on the network layer serializing
  909. * its calls in, but will run happily in parallel with the interrupt
  910. * handler. This code currently has provisions for fragmenting tx buffers
  911. * while trying to track down a bug in either the zero copy code or
  912. * the tx fifo (hence the MAX_FRAG_LEN).
  913. */
  914. static netdev_tx_t ns83820_hard_start_xmit(struct sk_buff *skb,
  915. struct net_device *ndev)
  916. {
  917. struct ns83820 *dev = PRIV(ndev);
  918. u32 free_idx, cmdsts, extsts;
  919. int nr_free, nr_frags;
  920. unsigned tx_done_idx, last_idx;
  921. dma_addr_t buf;
  922. unsigned len;
  923. skb_frag_t *frag;
  924. int stopped = 0;
  925. int do_intr = 0;
  926. volatile __le32 *first_desc;
  927. dprintk("ns83820_hard_start_xmit\n");
  928. nr_frags = skb_shinfo(skb)->nr_frags;
  929. again:
  930. if (unlikely(dev->CFG_cache & CFG_LNKSTS)) {
  931. netif_stop_queue(ndev);
  932. if (unlikely(dev->CFG_cache & CFG_LNKSTS))
  933. return NETDEV_TX_BUSY;
  934. netif_start_queue(ndev);
  935. }
  936. last_idx = free_idx = dev->tx_free_idx;
  937. tx_done_idx = dev->tx_done_idx;
  938. nr_free = (tx_done_idx + NR_TX_DESC-2 - free_idx) % NR_TX_DESC;
  939. nr_free -= 1;
  940. if (nr_free <= nr_frags) {
  941. dprintk("stop_queue - not enough(%p)\n", ndev);
  942. netif_stop_queue(ndev);
  943. /* Check again: we may have raced with a tx done irq */
  944. if (dev->tx_done_idx != tx_done_idx) {
  945. dprintk("restart queue(%p)\n", ndev);
  946. netif_start_queue(ndev);
  947. goto again;
  948. }
  949. return NETDEV_TX_BUSY;
  950. }
  951. if (free_idx == dev->tx_intr_idx) {
  952. do_intr = 1;
  953. dev->tx_intr_idx = (dev->tx_intr_idx + NR_TX_DESC/4) % NR_TX_DESC;
  954. }
  955. nr_free -= nr_frags;
  956. if (nr_free < MIN_TX_DESC_FREE) {
  957. dprintk("stop_queue - last entry(%p)\n", ndev);
  958. netif_stop_queue(ndev);
  959. stopped = 1;
  960. }
  961. frag = skb_shinfo(skb)->frags;
  962. if (!nr_frags)
  963. frag = NULL;
  964. extsts = 0;
  965. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  966. extsts |= EXTSTS_IPPKT;
  967. if (IPPROTO_TCP == ip_hdr(skb)->protocol)
  968. extsts |= EXTSTS_TCPPKT;
  969. else if (IPPROTO_UDP == ip_hdr(skb)->protocol)
  970. extsts |= EXTSTS_UDPPKT;
  971. }
  972. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  973. if(vlan_tx_tag_present(skb)) {
  974. /* fetch the vlan tag info out of the
  975. * ancillary data if the vlan code
  976. * is using hw vlan acceleration
  977. */
  978. short tag = vlan_tx_tag_get(skb);
  979. extsts |= (EXTSTS_VPKT | htons(tag));
  980. }
  981. #endif
  982. len = skb->len;
  983. if (nr_frags)
  984. len -= skb->data_len;
  985. buf = pci_map_single(dev->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  986. first_desc = dev->tx_descs + (free_idx * DESC_SIZE);
  987. for (;;) {
  988. volatile __le32 *desc = dev->tx_descs + (free_idx * DESC_SIZE);
  989. dprintk("frag[%3u]: %4u @ 0x%08Lx\n", free_idx, len,
  990. (unsigned long long)buf);
  991. last_idx = free_idx;
  992. free_idx = (free_idx + 1) % NR_TX_DESC;
  993. desc[DESC_LINK] = cpu_to_le32(dev->tx_phy_descs + (free_idx * DESC_SIZE * 4));
  994. desc_addr_set(desc + DESC_BUFPTR, buf);
  995. desc[DESC_EXTSTS] = cpu_to_le32(extsts);
  996. cmdsts = ((nr_frags) ? CMDSTS_MORE : do_intr ? CMDSTS_INTR : 0);
  997. cmdsts |= (desc == first_desc) ? 0 : CMDSTS_OWN;
  998. cmdsts |= len;
  999. desc[DESC_CMDSTS] = cpu_to_le32(cmdsts);
  1000. if (!nr_frags)
  1001. break;
  1002. buf = skb_frag_dma_map(&dev->pci_dev->dev, frag, 0,
  1003. skb_frag_size(frag), DMA_TO_DEVICE);
  1004. dprintk("frag: buf=%08Lx page=%08lx offset=%08lx\n",
  1005. (long long)buf, (long) page_to_pfn(frag->page),
  1006. frag->page_offset);
  1007. len = skb_frag_size(frag);
  1008. frag++;
  1009. nr_frags--;
  1010. }
  1011. dprintk("done pkt\n");
  1012. spin_lock_irq(&dev->tx_lock);
  1013. dev->tx_skbs[last_idx] = skb;
  1014. first_desc[DESC_CMDSTS] |= cpu_to_le32(CMDSTS_OWN);
  1015. dev->tx_free_idx = free_idx;
  1016. atomic_inc(&dev->nr_tx_skbs);
  1017. spin_unlock_irq(&dev->tx_lock);
  1018. kick_tx(dev);
  1019. /* Check again: we may have raced with a tx done irq */
  1020. if (stopped && (dev->tx_done_idx != tx_done_idx) && start_tx_okay(dev))
  1021. netif_start_queue(ndev);
  1022. return NETDEV_TX_OK;
  1023. }
  1024. static void ns83820_update_stats(struct ns83820 *dev)
  1025. {
  1026. struct net_device *ndev = dev->ndev;
  1027. u8 __iomem *base = dev->base;
  1028. /* the DP83820 will freeze counters, so we need to read all of them */
  1029. ndev->stats.rx_errors += readl(base + 0x60) & 0xffff;
  1030. ndev->stats.rx_crc_errors += readl(base + 0x64) & 0xffff;
  1031. ndev->stats.rx_missed_errors += readl(base + 0x68) & 0xffff;
  1032. ndev->stats.rx_frame_errors += readl(base + 0x6c) & 0xffff;
  1033. /*ndev->stats.rx_symbol_errors +=*/ readl(base + 0x70);
  1034. ndev->stats.rx_length_errors += readl(base + 0x74) & 0xffff;
  1035. ndev->stats.rx_length_errors += readl(base + 0x78) & 0xffff;
  1036. /*ndev->stats.rx_badopcode_errors += */ readl(base + 0x7c);
  1037. /*ndev->stats.rx_pause_count += */ readl(base + 0x80);
  1038. /*ndev->stats.tx_pause_count += */ readl(base + 0x84);
  1039. ndev->stats.tx_carrier_errors += readl(base + 0x88) & 0xff;
  1040. }
  1041. static struct net_device_stats *ns83820_get_stats(struct net_device *ndev)
  1042. {
  1043. struct ns83820 *dev = PRIV(ndev);
  1044. /* somewhat overkill */
  1045. spin_lock_irq(&dev->misc_lock);
  1046. ns83820_update_stats(dev);
  1047. spin_unlock_irq(&dev->misc_lock);
  1048. return &ndev->stats;
  1049. }
  1050. /* Let ethtool retrieve info */
  1051. static int ns83820_get_settings(struct net_device *ndev,
  1052. struct ethtool_cmd *cmd)
  1053. {
  1054. struct ns83820 *dev = PRIV(ndev);
  1055. u32 cfg, tanar, tbicr;
  1056. int fullduplex = 0;
  1057. /*
  1058. * Here's the list of available ethtool commands from other drivers:
  1059. * cmd->advertising =
  1060. * ethtool_cmd_speed_set(cmd, ...)
  1061. * cmd->duplex =
  1062. * cmd->port = 0;
  1063. * cmd->phy_address =
  1064. * cmd->transceiver = 0;
  1065. * cmd->autoneg =
  1066. * cmd->maxtxpkt = 0;
  1067. * cmd->maxrxpkt = 0;
  1068. */
  1069. /* read current configuration */
  1070. cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  1071. tanar = readl(dev->base + TANAR);
  1072. tbicr = readl(dev->base + TBICR);
  1073. fullduplex = (cfg & CFG_DUPSTS) ? 1 : 0;
  1074. cmd->supported = SUPPORTED_Autoneg;
  1075. if (dev->CFG_cache & CFG_TBI_EN) {
  1076. /* we have optical interface */
  1077. cmd->supported |= SUPPORTED_1000baseT_Half |
  1078. SUPPORTED_1000baseT_Full |
  1079. SUPPORTED_FIBRE;
  1080. cmd->port = PORT_FIBRE;
  1081. } else {
  1082. /* we have copper */
  1083. cmd->supported |= SUPPORTED_10baseT_Half |
  1084. SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half |
  1085. SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Half |
  1086. SUPPORTED_1000baseT_Full |
  1087. SUPPORTED_MII;
  1088. cmd->port = PORT_MII;
  1089. }
  1090. cmd->duplex = fullduplex ? DUPLEX_FULL : DUPLEX_HALF;
  1091. switch (cfg / CFG_SPDSTS0 & 3) {
  1092. case 2:
  1093. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1094. break;
  1095. case 1:
  1096. ethtool_cmd_speed_set(cmd, SPEED_100);
  1097. break;
  1098. default:
  1099. ethtool_cmd_speed_set(cmd, SPEED_10);
  1100. break;
  1101. }
  1102. cmd->autoneg = (tbicr & TBICR_MR_AN_ENABLE)
  1103. ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  1104. return 0;
  1105. }
  1106. /* Let ethool change settings*/
  1107. static int ns83820_set_settings(struct net_device *ndev,
  1108. struct ethtool_cmd *cmd)
  1109. {
  1110. struct ns83820 *dev = PRIV(ndev);
  1111. u32 cfg, tanar;
  1112. int have_optical = 0;
  1113. int fullduplex = 0;
  1114. /* read current configuration */
  1115. cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  1116. tanar = readl(dev->base + TANAR);
  1117. if (dev->CFG_cache & CFG_TBI_EN) {
  1118. /* we have optical */
  1119. have_optical = 1;
  1120. fullduplex = (tanar & TANAR_FULL_DUP);
  1121. } else {
  1122. /* we have copper */
  1123. fullduplex = cfg & CFG_DUPSTS;
  1124. }
  1125. spin_lock_irq(&dev->misc_lock);
  1126. spin_lock(&dev->tx_lock);
  1127. /* Set duplex */
  1128. if (cmd->duplex != fullduplex) {
  1129. if (have_optical) {
  1130. /*set full duplex*/
  1131. if (cmd->duplex == DUPLEX_FULL) {
  1132. /* force full duplex */
  1133. writel(readl(dev->base + TXCFG)
  1134. | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP,
  1135. dev->base + TXCFG);
  1136. writel(readl(dev->base + RXCFG) | RXCFG_RX_FD,
  1137. dev->base + RXCFG);
  1138. /* Light up full duplex LED */
  1139. writel(readl(dev->base + GPIOR) | GPIOR_GP1_OUT,
  1140. dev->base + GPIOR);
  1141. } else {
  1142. /*TODO: set half duplex */
  1143. }
  1144. } else {
  1145. /*we have copper*/
  1146. /* TODO: Set duplex for copper cards */
  1147. }
  1148. printk(KERN_INFO "%s: Duplex set via ethtool\n",
  1149. ndev->name);
  1150. }
  1151. /* Set autonegotiation */
  1152. if (1) {
  1153. if (cmd->autoneg == AUTONEG_ENABLE) {
  1154. /* restart auto negotiation */
  1155. writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN,
  1156. dev->base + TBICR);
  1157. writel(TBICR_MR_AN_ENABLE, dev->base + TBICR);
  1158. dev->linkstate = LINK_AUTONEGOTIATE;
  1159. printk(KERN_INFO "%s: autoneg enabled via ethtool\n",
  1160. ndev->name);
  1161. } else {
  1162. /* disable auto negotiation */
  1163. writel(0x00000000, dev->base + TBICR);
  1164. }
  1165. printk(KERN_INFO "%s: autoneg %s via ethtool\n", ndev->name,
  1166. cmd->autoneg ? "ENABLED" : "DISABLED");
  1167. }
  1168. phy_intr(ndev);
  1169. spin_unlock(&dev->tx_lock);
  1170. spin_unlock_irq(&dev->misc_lock);
  1171. return 0;
  1172. }
  1173. /* end ethtool get/set support -df */
  1174. static void ns83820_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
  1175. {
  1176. struct ns83820 *dev = PRIV(ndev);
  1177. strlcpy(info->driver, "ns83820", sizeof(info->driver));
  1178. strlcpy(info->version, VERSION, sizeof(info->version));
  1179. strlcpy(info->bus_info, pci_name(dev->pci_dev), sizeof(info->bus_info));
  1180. }
  1181. static u32 ns83820_get_link(struct net_device *ndev)
  1182. {
  1183. struct ns83820 *dev = PRIV(ndev);
  1184. u32 cfg = readl(dev->base + CFG) ^ SPDSTS_POLARITY;
  1185. return cfg & CFG_LNKSTS ? 1 : 0;
  1186. }
  1187. static const struct ethtool_ops ops = {
  1188. .get_settings = ns83820_get_settings,
  1189. .set_settings = ns83820_set_settings,
  1190. .get_drvinfo = ns83820_get_drvinfo,
  1191. .get_link = ns83820_get_link
  1192. };
  1193. static inline void ns83820_disable_interrupts(struct ns83820 *dev)
  1194. {
  1195. writel(0, dev->base + IMR);
  1196. writel(0, dev->base + IER);
  1197. readl(dev->base + IER);
  1198. }
  1199. /* this function is called in irq context from the ISR */
  1200. static void ns83820_mib_isr(struct ns83820 *dev)
  1201. {
  1202. unsigned long flags;
  1203. spin_lock_irqsave(&dev->misc_lock, flags);
  1204. ns83820_update_stats(dev);
  1205. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1206. }
  1207. static void ns83820_do_isr(struct net_device *ndev, u32 isr);
  1208. static irqreturn_t ns83820_irq(int foo, void *data)
  1209. {
  1210. struct net_device *ndev = data;
  1211. struct ns83820 *dev = PRIV(ndev);
  1212. u32 isr;
  1213. dprintk("ns83820_irq(%p)\n", ndev);
  1214. dev->ihr = 0;
  1215. isr = readl(dev->base + ISR);
  1216. dprintk("irq: %08x\n", isr);
  1217. ns83820_do_isr(ndev, isr);
  1218. return IRQ_HANDLED;
  1219. }
  1220. static void ns83820_do_isr(struct net_device *ndev, u32 isr)
  1221. {
  1222. struct ns83820 *dev = PRIV(ndev);
  1223. unsigned long flags;
  1224. #ifdef DEBUG
  1225. if (isr & ~(ISR_PHY | ISR_RXDESC | ISR_RXEARLY | ISR_RXOK | ISR_RXERR | ISR_TXIDLE | ISR_TXOK | ISR_TXDESC))
  1226. Dprintk("odd isr? 0x%08x\n", isr);
  1227. #endif
  1228. if (ISR_RXIDLE & isr) {
  1229. dev->rx_info.idle = 1;
  1230. Dprintk("oh dear, we are idle\n");
  1231. ns83820_rx_kick(ndev);
  1232. }
  1233. if ((ISR_RXDESC | ISR_RXOK) & isr) {
  1234. prefetch(dev->rx_info.next_rx_desc);
  1235. spin_lock_irqsave(&dev->misc_lock, flags);
  1236. dev->IMR_cache &= ~(ISR_RXDESC | ISR_RXOK);
  1237. writel(dev->IMR_cache, dev->base + IMR);
  1238. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1239. tasklet_schedule(&dev->rx_tasklet);
  1240. //rx_irq(ndev);
  1241. //writel(4, dev->base + IHR);
  1242. }
  1243. if ((ISR_RXIDLE | ISR_RXORN | ISR_RXDESC | ISR_RXOK | ISR_RXERR) & isr)
  1244. ns83820_rx_kick(ndev);
  1245. if (unlikely(ISR_RXSOVR & isr)) {
  1246. //printk("overrun: rxsovr\n");
  1247. ndev->stats.rx_fifo_errors++;
  1248. }
  1249. if (unlikely(ISR_RXORN & isr)) {
  1250. //printk("overrun: rxorn\n");
  1251. ndev->stats.rx_fifo_errors++;
  1252. }
  1253. if ((ISR_RXRCMP & isr) && dev->rx_info.up)
  1254. writel(CR_RXE, dev->base + CR);
  1255. if (ISR_TXIDLE & isr) {
  1256. u32 txdp;
  1257. txdp = readl(dev->base + TXDP);
  1258. dprintk("txdp: %08x\n", txdp);
  1259. txdp -= dev->tx_phy_descs;
  1260. dev->tx_idx = txdp / (DESC_SIZE * 4);
  1261. if (dev->tx_idx >= NR_TX_DESC) {
  1262. printk(KERN_ALERT "%s: BUG -- txdp out of range\n", ndev->name);
  1263. dev->tx_idx = 0;
  1264. }
  1265. /* The may have been a race between a pci originated read
  1266. * and the descriptor update from the cpu. Just in case,
  1267. * kick the transmitter if the hardware thinks it is on a
  1268. * different descriptor than we are.
  1269. */
  1270. if (dev->tx_idx != dev->tx_free_idx)
  1271. kick_tx(dev);
  1272. }
  1273. /* Defer tx ring processing until more than a minimum amount of
  1274. * work has accumulated
  1275. */
  1276. if ((ISR_TXDESC | ISR_TXIDLE | ISR_TXOK | ISR_TXERR) & isr) {
  1277. spin_lock_irqsave(&dev->tx_lock, flags);
  1278. do_tx_done(ndev);
  1279. spin_unlock_irqrestore(&dev->tx_lock, flags);
  1280. /* Disable TxOk if there are no outstanding tx packets.
  1281. */
  1282. if ((dev->tx_done_idx == dev->tx_free_idx) &&
  1283. (dev->IMR_cache & ISR_TXOK)) {
  1284. spin_lock_irqsave(&dev->misc_lock, flags);
  1285. dev->IMR_cache &= ~ISR_TXOK;
  1286. writel(dev->IMR_cache, dev->base + IMR);
  1287. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1288. }
  1289. }
  1290. /* The TxIdle interrupt can come in before the transmit has
  1291. * completed. Normally we reap packets off of the combination
  1292. * of TxDesc and TxIdle and leave TxOk disabled (since it
  1293. * occurs on every packet), but when no further irqs of this
  1294. * nature are expected, we must enable TxOk.
  1295. */
  1296. if ((ISR_TXIDLE & isr) && (dev->tx_done_idx != dev->tx_free_idx)) {
  1297. spin_lock_irqsave(&dev->misc_lock, flags);
  1298. dev->IMR_cache |= ISR_TXOK;
  1299. writel(dev->IMR_cache, dev->base + IMR);
  1300. spin_unlock_irqrestore(&dev->misc_lock, flags);
  1301. }
  1302. /* MIB interrupt: one of the statistics counters is about to overflow */
  1303. if (unlikely(ISR_MIB & isr))
  1304. ns83820_mib_isr(dev);
  1305. /* PHY: Link up/down/negotiation state change */
  1306. if (unlikely(ISR_PHY & isr))
  1307. phy_intr(ndev);
  1308. #if 0 /* Still working on the interrupt mitigation strategy */
  1309. if (dev->ihr)
  1310. writel(dev->ihr, dev->base + IHR);
  1311. #endif
  1312. }
  1313. static void ns83820_do_reset(struct ns83820 *dev, u32 which)
  1314. {
  1315. Dprintk("resetting chip...\n");
  1316. writel(which, dev->base + CR);
  1317. do {
  1318. schedule();
  1319. } while (readl(dev->base + CR) & which);
  1320. Dprintk("okay!\n");
  1321. }
  1322. static int ns83820_stop(struct net_device *ndev)
  1323. {
  1324. struct ns83820 *dev = PRIV(ndev);
  1325. /* FIXME: protect against interrupt handler? */
  1326. del_timer_sync(&dev->tx_watchdog);
  1327. ns83820_disable_interrupts(dev);
  1328. dev->rx_info.up = 0;
  1329. synchronize_irq(dev->pci_dev->irq);
  1330. ns83820_do_reset(dev, CR_RST);
  1331. synchronize_irq(dev->pci_dev->irq);
  1332. spin_lock_irq(&dev->misc_lock);
  1333. dev->IMR_cache &= ~(ISR_TXURN | ISR_TXIDLE | ISR_TXERR | ISR_TXDESC | ISR_TXOK);
  1334. spin_unlock_irq(&dev->misc_lock);
  1335. ns83820_cleanup_rx(dev);
  1336. ns83820_cleanup_tx(dev);
  1337. return 0;
  1338. }
  1339. static void ns83820_tx_timeout(struct net_device *ndev)
  1340. {
  1341. struct ns83820 *dev = PRIV(ndev);
  1342. u32 tx_done_idx;
  1343. __le32 *desc;
  1344. unsigned long flags;
  1345. spin_lock_irqsave(&dev->tx_lock, flags);
  1346. tx_done_idx = dev->tx_done_idx;
  1347. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  1348. printk(KERN_INFO "%s: tx_timeout: tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  1349. ndev->name,
  1350. tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
  1351. #if defined(DEBUG)
  1352. {
  1353. u32 isr;
  1354. isr = readl(dev->base + ISR);
  1355. printk("irq: %08x imr: %08x\n", isr, dev->IMR_cache);
  1356. ns83820_do_isr(ndev, isr);
  1357. }
  1358. #endif
  1359. do_tx_done(ndev);
  1360. tx_done_idx = dev->tx_done_idx;
  1361. desc = dev->tx_descs + (tx_done_idx * DESC_SIZE);
  1362. printk(KERN_INFO "%s: after: tx_done_idx=%d free_idx=%d cmdsts=%08x\n",
  1363. ndev->name,
  1364. tx_done_idx, dev->tx_free_idx, le32_to_cpu(desc[DESC_CMDSTS]));
  1365. spin_unlock_irqrestore(&dev->tx_lock, flags);
  1366. }
  1367. static void ns83820_tx_watch(unsigned long data)
  1368. {
  1369. struct net_device *ndev = (void *)data;
  1370. struct ns83820 *dev = PRIV(ndev);
  1371. #if defined(DEBUG)
  1372. printk("ns83820_tx_watch: %u %u %d\n",
  1373. dev->tx_done_idx, dev->tx_free_idx, atomic_read(&dev->nr_tx_skbs)
  1374. );
  1375. #endif
  1376. if (time_after(jiffies, dev_trans_start(ndev) + 1*HZ) &&
  1377. dev->tx_done_idx != dev->tx_free_idx) {
  1378. printk(KERN_DEBUG "%s: ns83820_tx_watch: %u %u %d\n",
  1379. ndev->name,
  1380. dev->tx_done_idx, dev->tx_free_idx,
  1381. atomic_read(&dev->nr_tx_skbs));
  1382. ns83820_tx_timeout(ndev);
  1383. }
  1384. mod_timer(&dev->tx_watchdog, jiffies + 2*HZ);
  1385. }
  1386. static int ns83820_open(struct net_device *ndev)
  1387. {
  1388. struct ns83820 *dev = PRIV(ndev);
  1389. unsigned i;
  1390. u32 desc;
  1391. int ret;
  1392. dprintk("ns83820_open\n");
  1393. writel(0, dev->base + PQCR);
  1394. ret = ns83820_setup_rx(ndev);
  1395. if (ret)
  1396. goto failed;
  1397. memset(dev->tx_descs, 0, 4 * NR_TX_DESC * DESC_SIZE);
  1398. for (i=0; i<NR_TX_DESC; i++) {
  1399. dev->tx_descs[(i * DESC_SIZE) + DESC_LINK]
  1400. = cpu_to_le32(
  1401. dev->tx_phy_descs
  1402. + ((i+1) % NR_TX_DESC) * DESC_SIZE * 4);
  1403. }
  1404. dev->tx_idx = 0;
  1405. dev->tx_done_idx = 0;
  1406. desc = dev->tx_phy_descs;
  1407. writel(0, dev->base + TXDP_HI);
  1408. writel(desc, dev->base + TXDP);
  1409. init_timer(&dev->tx_watchdog);
  1410. dev->tx_watchdog.data = (unsigned long)ndev;
  1411. dev->tx_watchdog.function = ns83820_tx_watch;
  1412. mod_timer(&dev->tx_watchdog, jiffies + 2*HZ);
  1413. netif_start_queue(ndev); /* FIXME: wait for phy to come up */
  1414. return 0;
  1415. failed:
  1416. ns83820_stop(ndev);
  1417. return ret;
  1418. }
  1419. static void ns83820_getmac(struct ns83820 *dev, u8 *mac)
  1420. {
  1421. unsigned i;
  1422. for (i=0; i<3; i++) {
  1423. u32 data;
  1424. /* Read from the perfect match memory: this is loaded by
  1425. * the chip from the EEPROM via the EELOAD self test.
  1426. */
  1427. writel(i*2, dev->base + RFCR);
  1428. data = readl(dev->base + RFDR);
  1429. *mac++ = data;
  1430. *mac++ = data >> 8;
  1431. }
  1432. }
  1433. static int ns83820_change_mtu(struct net_device *ndev, int new_mtu)
  1434. {
  1435. if (new_mtu > RX_BUF_SIZE)
  1436. return -EINVAL;
  1437. ndev->mtu = new_mtu;
  1438. return 0;
  1439. }
  1440. static void ns83820_set_multicast(struct net_device *ndev)
  1441. {
  1442. struct ns83820 *dev = PRIV(ndev);
  1443. u8 __iomem *rfcr = dev->base + RFCR;
  1444. u32 and_mask = 0xffffffff;
  1445. u32 or_mask = 0;
  1446. u32 val;
  1447. if (ndev->flags & IFF_PROMISC)
  1448. or_mask |= RFCR_AAU | RFCR_AAM;
  1449. else
  1450. and_mask &= ~(RFCR_AAU | RFCR_AAM);
  1451. if (ndev->flags & IFF_ALLMULTI || netdev_mc_count(ndev))
  1452. or_mask |= RFCR_AAM;
  1453. else
  1454. and_mask &= ~RFCR_AAM;
  1455. spin_lock_irq(&dev->misc_lock);
  1456. val = (readl(rfcr) & and_mask) | or_mask;
  1457. /* Ramit : RFCR Write Fix doc says RFEN must be 0 modify other bits */
  1458. writel(val & ~RFCR_RFEN, rfcr);
  1459. writel(val, rfcr);
  1460. spin_unlock_irq(&dev->misc_lock);
  1461. }
  1462. static void ns83820_run_bist(struct net_device *ndev, const char *name, u32 enable, u32 done, u32 fail)
  1463. {
  1464. struct ns83820 *dev = PRIV(ndev);
  1465. int timed_out = 0;
  1466. unsigned long start;
  1467. u32 status;
  1468. int loops = 0;
  1469. dprintk("%s: start %s\n", ndev->name, name);
  1470. start = jiffies;
  1471. writel(enable, dev->base + PTSCR);
  1472. for (;;) {
  1473. loops++;
  1474. status = readl(dev->base + PTSCR);
  1475. if (!(status & enable))
  1476. break;
  1477. if (status & done)
  1478. break;
  1479. if (status & fail)
  1480. break;
  1481. if (time_after_eq(jiffies, start + HZ)) {
  1482. timed_out = 1;
  1483. break;
  1484. }
  1485. schedule_timeout_uninterruptible(1);
  1486. }
  1487. if (status & fail)
  1488. printk(KERN_INFO "%s: %s failed! (0x%08x & 0x%08x)\n",
  1489. ndev->name, name, status, fail);
  1490. else if (timed_out)
  1491. printk(KERN_INFO "%s: run_bist %s timed out! (%08x)\n",
  1492. ndev->name, name, status);
  1493. dprintk("%s: done %s in %d loops\n", ndev->name, name, loops);
  1494. }
  1495. #ifdef PHY_CODE_IS_FINISHED
  1496. static void ns83820_mii_write_bit(struct ns83820 *dev, int bit)
  1497. {
  1498. /* drive MDC low */
  1499. dev->MEAR_cache &= ~MEAR_MDC;
  1500. writel(dev->MEAR_cache, dev->base + MEAR);
  1501. readl(dev->base + MEAR);
  1502. /* enable output, set bit */
  1503. dev->MEAR_cache |= MEAR_MDDIR;
  1504. if (bit)
  1505. dev->MEAR_cache |= MEAR_MDIO;
  1506. else
  1507. dev->MEAR_cache &= ~MEAR_MDIO;
  1508. /* set the output bit */
  1509. writel(dev->MEAR_cache, dev->base + MEAR);
  1510. readl(dev->base + MEAR);
  1511. /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */
  1512. udelay(1);
  1513. /* drive MDC high causing the data bit to be latched */
  1514. dev->MEAR_cache |= MEAR_MDC;
  1515. writel(dev->MEAR_cache, dev->base + MEAR);
  1516. readl(dev->base + MEAR);
  1517. /* Wait again... */
  1518. udelay(1);
  1519. }
  1520. static int ns83820_mii_read_bit(struct ns83820 *dev)
  1521. {
  1522. int bit;
  1523. /* drive MDC low, disable output */
  1524. dev->MEAR_cache &= ~MEAR_MDC;
  1525. dev->MEAR_cache &= ~MEAR_MDDIR;
  1526. writel(dev->MEAR_cache, dev->base + MEAR);
  1527. readl(dev->base + MEAR);
  1528. /* Wait. Max clock rate is 2.5MHz, this way we come in under 1MHz */
  1529. udelay(1);
  1530. /* drive MDC high causing the data bit to be latched */
  1531. bit = (readl(dev->base + MEAR) & MEAR_MDIO) ? 1 : 0;
  1532. dev->MEAR_cache |= MEAR_MDC;
  1533. writel(dev->MEAR_cache, dev->base + MEAR);
  1534. /* Wait again... */
  1535. udelay(1);
  1536. return bit;
  1537. }
  1538. static unsigned ns83820_mii_read_reg(struct ns83820 *dev, unsigned phy, unsigned reg)
  1539. {
  1540. unsigned data = 0;
  1541. int i;
  1542. /* read some garbage so that we eventually sync up */
  1543. for (i=0; i<64; i++)
  1544. ns83820_mii_read_bit(dev);
  1545. ns83820_mii_write_bit(dev, 0); /* start */
  1546. ns83820_mii_write_bit(dev, 1);
  1547. ns83820_mii_write_bit(dev, 1); /* opcode read */
  1548. ns83820_mii_write_bit(dev, 0);
  1549. /* write out the phy address: 5 bits, msb first */
  1550. for (i=0; i<5; i++)
  1551. ns83820_mii_write_bit(dev, phy & (0x10 >> i));
  1552. /* write out the register address, 5 bits, msb first */
  1553. for (i=0; i<5; i++)
  1554. ns83820_mii_write_bit(dev, reg & (0x10 >> i));
  1555. ns83820_mii_read_bit(dev); /* turn around cycles */
  1556. ns83820_mii_read_bit(dev);
  1557. /* read in the register data, 16 bits msb first */
  1558. for (i=0; i<16; i++) {
  1559. data <<= 1;
  1560. data |= ns83820_mii_read_bit(dev);
  1561. }
  1562. return data;
  1563. }
  1564. static unsigned ns83820_mii_write_reg(struct ns83820 *dev, unsigned phy, unsigned reg, unsigned data)
  1565. {
  1566. int i;
  1567. /* read some garbage so that we eventually sync up */
  1568. for (i=0; i<64; i++)
  1569. ns83820_mii_read_bit(dev);
  1570. ns83820_mii_write_bit(dev, 0); /* start */
  1571. ns83820_mii_write_bit(dev, 1);
  1572. ns83820_mii_write_bit(dev, 0); /* opcode read */
  1573. ns83820_mii_write_bit(dev, 1);
  1574. /* write out the phy address: 5 bits, msb first */
  1575. for (i=0; i<5; i++)
  1576. ns83820_mii_write_bit(dev, phy & (0x10 >> i));
  1577. /* write out the register address, 5 bits, msb first */
  1578. for (i=0; i<5; i++)
  1579. ns83820_mii_write_bit(dev, reg & (0x10 >> i));
  1580. ns83820_mii_read_bit(dev); /* turn around cycles */
  1581. ns83820_mii_read_bit(dev);
  1582. /* read in the register data, 16 bits msb first */
  1583. for (i=0; i<16; i++)
  1584. ns83820_mii_write_bit(dev, (data >> (15 - i)) & 1);
  1585. return data;
  1586. }
  1587. static void ns83820_probe_phy(struct net_device *ndev)
  1588. {
  1589. struct ns83820 *dev = PRIV(ndev);
  1590. static int first;
  1591. int i;
  1592. #define MII_PHYIDR1 0x02
  1593. #define MII_PHYIDR2 0x03
  1594. #if 0
  1595. if (!first) {
  1596. unsigned tmp;
  1597. ns83820_mii_read_reg(dev, 1, 0x09);
  1598. ns83820_mii_write_reg(dev, 1, 0x10, 0x0d3e);
  1599. tmp = ns83820_mii_read_reg(dev, 1, 0x00);
  1600. ns83820_mii_write_reg(dev, 1, 0x00, tmp | 0x8000);
  1601. udelay(1300);
  1602. ns83820_mii_read_reg(dev, 1, 0x09);
  1603. }
  1604. #endif
  1605. first = 1;
  1606. for (i=1; i<2; i++) {
  1607. int j;
  1608. unsigned a, b;
  1609. a = ns83820_mii_read_reg(dev, i, MII_PHYIDR1);
  1610. b = ns83820_mii_read_reg(dev, i, MII_PHYIDR2);
  1611. //printk("%s: phy %d: 0x%04x 0x%04x\n",
  1612. // ndev->name, i, a, b);
  1613. for (j=0; j<0x16; j+=4) {
  1614. dprintk("%s: [0x%02x] %04x %04x %04x %04x\n",
  1615. ndev->name, j,
  1616. ns83820_mii_read_reg(dev, i, 0 + j),
  1617. ns83820_mii_read_reg(dev, i, 1 + j),
  1618. ns83820_mii_read_reg(dev, i, 2 + j),
  1619. ns83820_mii_read_reg(dev, i, 3 + j)
  1620. );
  1621. }
  1622. }
  1623. {
  1624. unsigned a, b;
  1625. /* read firmware version: memory addr is 0x8402 and 0x8403 */
  1626. ns83820_mii_write_reg(dev, 1, 0x16, 0x000d);
  1627. ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e);
  1628. a = ns83820_mii_read_reg(dev, 1, 0x1d);
  1629. ns83820_mii_write_reg(dev, 1, 0x16, 0x000d);
  1630. ns83820_mii_write_reg(dev, 1, 0x1e, 0x810e);
  1631. b = ns83820_mii_read_reg(dev, 1, 0x1d);
  1632. dprintk("version: 0x%04x 0x%04x\n", a, b);
  1633. }
  1634. }
  1635. #endif
  1636. static const struct net_device_ops netdev_ops = {
  1637. .ndo_open = ns83820_open,
  1638. .ndo_stop = ns83820_stop,
  1639. .ndo_start_xmit = ns83820_hard_start_xmit,
  1640. .ndo_get_stats = ns83820_get_stats,
  1641. .ndo_change_mtu = ns83820_change_mtu,
  1642. .ndo_set_rx_mode = ns83820_set_multicast,
  1643. .ndo_validate_addr = eth_validate_addr,
  1644. .ndo_set_mac_address = eth_mac_addr,
  1645. .ndo_tx_timeout = ns83820_tx_timeout,
  1646. };
  1647. static int __devinit ns83820_init_one(struct pci_dev *pci_dev,
  1648. const struct pci_device_id *id)
  1649. {
  1650. struct net_device *ndev;
  1651. struct ns83820 *dev;
  1652. long addr;
  1653. int err;
  1654. int using_dac = 0;
  1655. /* See if we can set the dma mask early on; failure is fatal. */
  1656. if (sizeof(dma_addr_t) == 8 &&
  1657. !pci_set_dma_mask(pci_dev, DMA_BIT_MASK(64))) {
  1658. using_dac = 1;
  1659. } else if (!pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32))) {
  1660. using_dac = 0;
  1661. } else {
  1662. dev_warn(&pci_dev->dev, "pci_set_dma_mask failed!\n");
  1663. return -ENODEV;
  1664. }
  1665. ndev = alloc_etherdev(sizeof(struct ns83820));
  1666. err = -ENOMEM;
  1667. if (!ndev)
  1668. goto out;
  1669. dev = PRIV(ndev);
  1670. dev->ndev = ndev;
  1671. spin_lock_init(&dev->rx_info.lock);
  1672. spin_lock_init(&dev->tx_lock);
  1673. spin_lock_init(&dev->misc_lock);
  1674. dev->pci_dev = pci_dev;
  1675. SET_NETDEV_DEV(ndev, &pci_dev->dev);
  1676. INIT_WORK(&dev->tq_refill, queue_refill);
  1677. tasklet_init(&dev->rx_tasklet, rx_action, (unsigned long)ndev);
  1678. err = pci_enable_device(pci_dev);
  1679. if (err) {
  1680. dev_info(&pci_dev->dev, "pci_enable_dev failed: %d\n", err);
  1681. goto out_free;
  1682. }
  1683. pci_set_master(pci_dev);
  1684. addr = pci_resource_start(pci_dev, 1);
  1685. dev->base = ioremap_nocache(addr, PAGE_SIZE);
  1686. dev->tx_descs = pci_alloc_consistent(pci_dev,
  1687. 4 * DESC_SIZE * NR_TX_DESC, &dev->tx_phy_descs);
  1688. dev->rx_info.descs = pci_alloc_consistent(pci_dev,
  1689. 4 * DESC_SIZE * NR_RX_DESC, &dev->rx_info.phy_descs);
  1690. err = -ENOMEM;
  1691. if (!dev->base || !dev->tx_descs || !dev->rx_info.descs)
  1692. goto out_disable;
  1693. dprintk("%p: %08lx %p: %08lx\n",
  1694. dev->tx_descs, (long)dev->tx_phy_descs,
  1695. dev->rx_info.descs, (long)dev->rx_info.phy_descs);
  1696. ns83820_disable_interrupts(dev);
  1697. dev->IMR_cache = 0;
  1698. err = request_irq(pci_dev->irq, ns83820_irq, IRQF_SHARED,
  1699. DRV_NAME, ndev);
  1700. if (err) {
  1701. dev_info(&pci_dev->dev, "unable to register irq %d, err %d\n",
  1702. pci_dev->irq, err);
  1703. goto out_disable;
  1704. }
  1705. /*
  1706. * FIXME: we are holding rtnl_lock() over obscenely long area only
  1707. * because some of the setup code uses dev->name. It's Wrong(tm) -
  1708. * we should be using driver-specific names for all that stuff.
  1709. * For now that will do, but we really need to come back and kill
  1710. * most of the dev_alloc_name() users later.
  1711. */
  1712. rtnl_lock();
  1713. err = dev_alloc_name(ndev, ndev->name);
  1714. if (err < 0) {
  1715. dev_info(&pci_dev->dev, "unable to get netdev name: %d\n", err);
  1716. goto out_free_irq;
  1717. }
  1718. printk("%s: ns83820.c: 0x22c: %08x, subsystem: %04x:%04x\n",
  1719. ndev->name, le32_to_cpu(readl(dev->base + 0x22c)),
  1720. pci_dev->subsystem_vendor, pci_dev->subsystem_device);
  1721. ndev->netdev_ops = &netdev_ops;
  1722. SET_ETHTOOL_OPS(ndev, &ops);
  1723. ndev->watchdog_timeo = 5 * HZ;
  1724. pci_set_drvdata(pci_dev, ndev);
  1725. ns83820_do_reset(dev, CR_RST);
  1726. /* Must reset the ram bist before running it */
  1727. writel(PTSCR_RBIST_RST, dev->base + PTSCR);
  1728. ns83820_run_bist(ndev, "sram bist", PTSCR_RBIST_EN,
  1729. PTSCR_RBIST_DONE, PTSCR_RBIST_FAIL);
  1730. ns83820_run_bist(ndev, "eeprom bist", PTSCR_EEBIST_EN, 0,
  1731. PTSCR_EEBIST_FAIL);
  1732. ns83820_run_bist(ndev, "eeprom load", PTSCR_EELOAD_EN, 0, 0);
  1733. /* I love config registers */
  1734. dev->CFG_cache = readl(dev->base + CFG);
  1735. if ((dev->CFG_cache & CFG_PCI64_DET)) {
  1736. printk(KERN_INFO "%s: detected 64 bit PCI data bus.\n",
  1737. ndev->name);
  1738. /*dev->CFG_cache |= CFG_DATA64_EN;*/
  1739. if (!(dev->CFG_cache & CFG_DATA64_EN))
  1740. printk(KERN_INFO "%s: EEPROM did not enable 64 bit bus. Disabled.\n",
  1741. ndev->name);
  1742. } else
  1743. dev->CFG_cache &= ~(CFG_DATA64_EN);
  1744. dev->CFG_cache &= (CFG_TBI_EN | CFG_MRM_DIS | CFG_MWI_DIS |
  1745. CFG_T64ADDR | CFG_DATA64_EN | CFG_EXT_125 |
  1746. CFG_M64ADDR);
  1747. dev->CFG_cache |= CFG_PINT_DUPSTS | CFG_PINT_LNKSTS | CFG_PINT_SPDSTS |
  1748. CFG_EXTSTS_EN | CFG_EXD | CFG_PESEL;
  1749. dev->CFG_cache |= CFG_REQALG;
  1750. dev->CFG_cache |= CFG_POW;
  1751. dev->CFG_cache |= CFG_TMRTEST;
  1752. /* When compiled with 64 bit addressing, we must always enable
  1753. * the 64 bit descriptor format.
  1754. */
  1755. if (sizeof(dma_addr_t) == 8)
  1756. dev->CFG_cache |= CFG_M64ADDR;
  1757. if (using_dac)
  1758. dev->CFG_cache |= CFG_T64ADDR;
  1759. /* Big endian mode does not seem to do what the docs suggest */
  1760. dev->CFG_cache &= ~CFG_BEM;
  1761. /* setup optical transceiver if we have one */
  1762. if (dev->CFG_cache & CFG_TBI_EN) {
  1763. printk(KERN_INFO "%s: enabling optical transceiver\n",
  1764. ndev->name);
  1765. writel(readl(dev->base + GPIOR) | 0x3e8, dev->base + GPIOR);
  1766. /* setup auto negotiation feature advertisement */
  1767. writel(readl(dev->base + TANAR)
  1768. | TANAR_HALF_DUP | TANAR_FULL_DUP,
  1769. dev->base + TANAR);
  1770. /* start auto negotiation */
  1771. writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN,
  1772. dev->base + TBICR);
  1773. writel(TBICR_MR_AN_ENABLE, dev->base + TBICR);
  1774. dev->linkstate = LINK_AUTONEGOTIATE;
  1775. dev->CFG_cache |= CFG_MODE_1000;
  1776. }
  1777. writel(dev->CFG_cache, dev->base + CFG);
  1778. dprintk("CFG: %08x\n", dev->CFG_cache);
  1779. if (reset_phy) {
  1780. printk(KERN_INFO "%s: resetting phy\n", ndev->name);
  1781. writel(dev->CFG_cache | CFG_PHY_RST, dev->base + CFG);
  1782. msleep(10);
  1783. writel(dev->CFG_cache, dev->base + CFG);
  1784. }
  1785. #if 0 /* Huh? This sets the PCI latency register. Should be done via
  1786. * the PCI layer. FIXME.
  1787. */
  1788. if (readl(dev->base + SRR))
  1789. writel(readl(dev->base+0x20c) | 0xfe00, dev->base + 0x20c);
  1790. #endif
  1791. /* Note! The DMA burst size interacts with packet
  1792. * transmission, such that the largest packet that
  1793. * can be transmitted is 8192 - FLTH - burst size.
  1794. * If only the transmit fifo was larger...
  1795. */
  1796. /* Ramit : 1024 DMA is not a good idea, it ends up banging
  1797. * some DELL and COMPAQ SMP systems */
  1798. writel(TXCFG_CSI | TXCFG_HBI | TXCFG_ATP | TXCFG_MXDMA512
  1799. | ((1600 / 32) * 0x100),
  1800. dev->base + TXCFG);
  1801. /* Flush the interrupt holdoff timer */
  1802. writel(0x000, dev->base + IHR);
  1803. writel(0x100, dev->base + IHR);
  1804. writel(0x000, dev->base + IHR);
  1805. /* Set Rx to full duplex, don't accept runt, errored, long or length
  1806. * range errored packets. Use 512 byte DMA.
  1807. */
  1808. /* Ramit : 1024 DMA is not a good idea, it ends up banging
  1809. * some DELL and COMPAQ SMP systems
  1810. * Turn on ALP, only we are accpeting Jumbo Packets */
  1811. writel(RXCFG_AEP | RXCFG_ARP | RXCFG_AIRL | RXCFG_RX_FD
  1812. | RXCFG_STRIPCRC
  1813. //| RXCFG_ALP
  1814. | (RXCFG_MXDMA512) | 0, dev->base + RXCFG);
  1815. /* Disable priority queueing */
  1816. writel(0, dev->base + PQCR);
  1817. /* Enable IP checksum validation and detetion of VLAN headers.
  1818. * Note: do not set the reject options as at least the 0x102
  1819. * revision of the chip does not properly accept IP fragments
  1820. * at least for UDP.
  1821. */
  1822. /* Ramit : Be sure to turn on RXCFG_ARP if VLAN's are enabled, since
  1823. * the MAC it calculates the packetsize AFTER stripping the VLAN
  1824. * header, and if a VLAN Tagged packet of 64 bytes is received (like
  1825. * a ping with a VLAN header) then the card, strips the 4 byte VLAN
  1826. * tag and then checks the packet size, so if RXCFG_ARP is not enabled,
  1827. * it discrards it!. These guys......
  1828. * also turn on tag stripping if hardware acceleration is enabled
  1829. */
  1830. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1831. #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN|VRCR_VTREN)
  1832. #else
  1833. #define VRCR_INIT_VALUE (VRCR_IPEN|VRCR_VTDEN)
  1834. #endif
  1835. writel(VRCR_INIT_VALUE, dev->base + VRCR);
  1836. /* Enable per-packet TCP/UDP/IP checksumming
  1837. * and per packet vlan tag insertion if
  1838. * vlan hardware acceleration is enabled
  1839. */
  1840. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1841. #define VTCR_INIT_VALUE (VTCR_PPCHK|VTCR_VPPTI)
  1842. #else
  1843. #define VTCR_INIT_VALUE VTCR_PPCHK
  1844. #endif
  1845. writel(VTCR_INIT_VALUE, dev->base + VTCR);
  1846. /* Ramit : Enable async and sync pause frames */
  1847. /* writel(0, dev->base + PCR); */
  1848. writel((PCR_PS_MCAST | PCR_PS_DA | PCR_PSEN | PCR_FFLO_4K |
  1849. PCR_FFHI_8K | PCR_STLO_4 | PCR_STHI_8 | PCR_PAUSE_CNT),
  1850. dev->base + PCR);
  1851. /* Disable Wake On Lan */
  1852. writel(0, dev->base + WCSR);
  1853. ns83820_getmac(dev, ndev->dev_addr);
  1854. /* Yes, we support dumb IP checksum on transmit */
  1855. ndev->features |= NETIF_F_SG;
  1856. ndev->features |= NETIF_F_IP_CSUM;
  1857. #ifdef NS83820_VLAN_ACCEL_SUPPORT
  1858. /* We also support hardware vlan acceleration */
  1859. ndev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1860. #endif
  1861. if (using_dac) {
  1862. printk(KERN_INFO "%s: using 64 bit addressing.\n",
  1863. ndev->name);
  1864. ndev->features |= NETIF_F_HIGHDMA;
  1865. }
  1866. printk(KERN_INFO "%s: ns83820 v" VERSION ": DP83820 v%u.%u: %pM io=0x%08lx irq=%d f=%s\n",
  1867. ndev->name,
  1868. (unsigned)readl(dev->base + SRR) >> 8,
  1869. (unsigned)readl(dev->base + SRR) & 0xff,
  1870. ndev->dev_addr, addr, pci_dev->irq,
  1871. (ndev->features & NETIF_F_HIGHDMA) ? "h,sg" : "sg"
  1872. );
  1873. #ifdef PHY_CODE_IS_FINISHED
  1874. ns83820_probe_phy(ndev);
  1875. #endif
  1876. err = register_netdevice(ndev);
  1877. if (err) {
  1878. printk(KERN_INFO "ns83820: unable to register netdev: %d\n", err);
  1879. goto out_cleanup;
  1880. }
  1881. rtnl_unlock();
  1882. return 0;
  1883. out_cleanup:
  1884. ns83820_disable_interrupts(dev); /* paranoia */
  1885. out_free_irq:
  1886. rtnl_unlock();
  1887. free_irq(pci_dev->irq, ndev);
  1888. out_disable:
  1889. if (dev->base)
  1890. iounmap(dev->base);
  1891. pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_TX_DESC, dev->tx_descs, dev->tx_phy_descs);
  1892. pci_free_consistent(pci_dev, 4 * DESC_SIZE * NR_RX_DESC, dev->rx_info.descs, dev->rx_info.phy_descs);
  1893. pci_disable_device(pci_dev);
  1894. out_free:
  1895. free_netdev(ndev);
  1896. pci_set_drvdata(pci_dev, NULL);
  1897. out:
  1898. return err;
  1899. }
  1900. static void __devexit ns83820_remove_one(struct pci_dev *pci_dev)
  1901. {
  1902. struct net_device *ndev = pci_get_drvdata(pci_dev);
  1903. struct ns83820 *dev = PRIV(ndev); /* ok even if NULL */
  1904. if (!ndev) /* paranoia */
  1905. return;
  1906. ns83820_disable_interrupts(dev); /* paranoia */
  1907. unregister_netdev(ndev);
  1908. free_irq(dev->pci_dev->irq, ndev);
  1909. iounmap(dev->base);
  1910. pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_TX_DESC,
  1911. dev->tx_descs, dev->tx_phy_descs);
  1912. pci_free_consistent(dev->pci_dev, 4 * DESC_SIZE * NR_RX_DESC,
  1913. dev->rx_info.descs, dev->rx_info.phy_descs);
  1914. pci_disable_device(dev->pci_dev);
  1915. free_netdev(ndev);
  1916. pci_set_drvdata(pci_dev, NULL);
  1917. }
  1918. static DEFINE_PCI_DEVICE_TABLE(ns83820_pci_tbl) = {
  1919. { 0x100b, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, .driver_data = 0, },
  1920. { 0, },
  1921. };
  1922. static struct pci_driver driver = {
  1923. .name = "ns83820",
  1924. .id_table = ns83820_pci_tbl,
  1925. .probe = ns83820_init_one,
  1926. .remove = __devexit_p(ns83820_remove_one),
  1927. #if 0 /* FIXME: implement */
  1928. .suspend = ,
  1929. .resume = ,
  1930. #endif
  1931. };
  1932. static int __init ns83820_init(void)
  1933. {
  1934. printk(KERN_INFO "ns83820.c: National Semiconductor DP83820 10/100/1000 driver.\n");
  1935. return pci_register_driver(&driver);
  1936. }
  1937. static void __exit ns83820_exit(void)
  1938. {
  1939. pci_unregister_driver(&driver);
  1940. }
  1941. MODULE_AUTHOR("Benjamin LaHaise <bcrl@kvack.org>");
  1942. MODULE_DESCRIPTION("National Semiconductor DP83820 10/100/1000 driver");
  1943. MODULE_LICENSE("GPL");
  1944. MODULE_DEVICE_TABLE(pci, ns83820_pci_tbl);
  1945. module_param(lnksts, int, 0);
  1946. MODULE_PARM_DESC(lnksts, "Polarity of LNKSTS bit");
  1947. module_param(ihr, int, 0);
  1948. MODULE_PARM_DESC(ihr, "Time in 100 us increments to delay interrupts (range 0-127)");
  1949. module_param(reset_phy, int, 0);
  1950. MODULE_PARM_DESC(reset_phy, "Set to 1 to reset the PHY on startup");
  1951. module_init(ns83820_init);
  1952. module_exit(ns83820_exit);