You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

forcedeth.c 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /**************************************************************************
  2. * forcedeth.c -- Etherboot device driver for the NVIDIA nForce
  3. * media access controllers.
  4. *
  5. * Note: This driver is based on the Linux driver that was based on
  6. * a cleanroom reimplementation which was based on reverse
  7. * engineered documentation written by Carl-Daniel Hailfinger
  8. * and Andrew de Quincey. It's neither supported nor endorsed
  9. * by NVIDIA Corp. Use at your own risk.
  10. *
  11. * Written 2004 by Timothy Legge <tlegge@rogers.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. *
  27. * Portions of this code based on:
  28. * forcedeth: Ethernet driver for NVIDIA nForce media access controllers:
  29. *
  30. * (C) 2003 Manfred Spraul
  31. * See Linux Driver for full information
  32. *
  33. * Linux Driver Version 0.30, 25 Sep 2004
  34. * Linux Kernel 2.6.10
  35. *
  36. *
  37. * REVISION HISTORY:
  38. * ================
  39. * v1.0 01-31-2004 timlegge Initial port of Linux driver
  40. * v1.1 02-03-2004 timlegge Large Clean up, first release
  41. * v1.2 05-14-2005 timlegge Add Linux 0.22 to .030 features
  42. *
  43. * Indent Options: indent -kr -i8
  44. ***************************************************************************/
  45. /* to get some global routines like printf */
  46. #include "etherboot.h"
  47. /* to get the interface to the body of the program */
  48. #include "nic.h"
  49. /* to get the PCI support functions, if this is a PCI NIC */
  50. #include <gpxe/pci.h>
  51. /* Include timer support functions */
  52. #include <gpxe/ethernet.h>
  53. #include "mii.h"
  54. #define drv_version "v1.2"
  55. #define drv_date "05-14-2005"
  56. //#define TFTM_DEBUG
  57. #ifdef TFTM_DEBUG
  58. #define dprintf(x) printf x
  59. #else
  60. #define dprintf(x)
  61. #endif
  62. #define ETH_DATA_LEN 1500
  63. /* Condensed operations for readability. */
  64. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  65. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  66. static unsigned long BASE;
  67. /* NIC specific static variables go here */
  68. #define PCI_DEVICE_ID_NVIDIA_NVENET_1 0x01c3
  69. #define PCI_DEVICE_ID_NVIDIA_NVENET_2 0x0066
  70. #define PCI_DEVICE_ID_NVIDIA_NVENET_4 0x0086
  71. #define PCI_DEVICE_ID_NVIDIA_NVENET_5 0x008c
  72. #define PCI_DEVICE_ID_NVIDIA_NVENET_3 0x00d6
  73. #define PCI_DEVICE_ID_NVIDIA_NVENET_7 0x00df
  74. #define PCI_DEVICE_ID_NVIDIA_NVENET_6 0x00e6
  75. #define PCI_DEVICE_ID_NVIDIA_NVENET_8 0x0056
  76. #define PCI_DEVICE_ID_NVIDIA_NVENET_9 0x0057
  77. #define PCI_DEVICE_ID_NVIDIA_NVENET_10 0x0037
  78. #define PCI_DEVICE_ID_NVIDIA_NVENET_11 0x0038
  79. #define PCI_DEVICE_ID_NVIDIA_NVENET_15 0x0373
  80. /*
  81. * Hardware access:
  82. */
  83. #define DEV_NEED_LASTPACKET1 0x0001 /* set LASTPACKET1 in tx flags */
  84. #define DEV_IRQMASK_1 0x0002 /* use NVREG_IRQMASK_WANTED_1 for irq mask */
  85. #define DEV_IRQMASK_2 0x0004 /* use NVREG_IRQMASK_WANTED_2 for irq mask */
  86. #define DEV_NEED_TIMERIRQ 0x0008 /* set the timer irq flag in the irq mask */
  87. #define DEV_NEED_LINKTIMER 0x0010 /* poll link settings. Relies on the timer irq */
  88. enum {
  89. NvRegIrqStatus = 0x000,
  90. #define NVREG_IRQSTAT_MIIEVENT 0040
  91. #define NVREG_IRQSTAT_MASK 0x1ff
  92. NvRegIrqMask = 0x004,
  93. #define NVREG_IRQ_RX_ERROR 0x0001
  94. #define NVREG_IRQ_RX 0x0002
  95. #define NVREG_IRQ_RX_NOBUF 0x0004
  96. #define NVREG_IRQ_TX_ERR 0x0008
  97. #define NVREG_IRQ_TX2 0x0010
  98. #define NVREG_IRQ_TIMER 0x0020
  99. #define NVREG_IRQ_LINK 0x0040
  100. #define NVREG_IRQ_TX1 0x0100
  101. #define NVREG_IRQMASK_WANTED_1 0x005f
  102. #define NVREG_IRQMASK_WANTED_2 0x0147
  103. #define NVREG_IRQ_UNKNOWN (~(NVREG_IRQ_RX_ERROR|NVREG_IRQ_RX|NVREG_IRQ_RX_NOBUF|NVREG_IRQ_TX_ERR|NVREG_IRQ_TX2|NVREG_IRQ_TIMER|NVREG_IRQ_LINK|NVREG_IRQ_TX1))
  104. NvRegUnknownSetupReg6 = 0x008,
  105. #define NVREG_UNKSETUP6_VAL 3
  106. /*
  107. * NVREG_POLL_DEFAULT is the interval length of the timer source on the nic
  108. * NVREG_POLL_DEFAULT=97 would result in an interval length of 1 ms
  109. */
  110. NvRegPollingInterval = 0x00c,
  111. #define NVREG_POLL_DEFAULT 970
  112. NvRegMisc1 = 0x080,
  113. #define NVREG_MISC1_HD 0x02
  114. #define NVREG_MISC1_FORCE 0x3b0f3c
  115. NvRegTransmitterControl = 0x084,
  116. #define NVREG_XMITCTL_START 0x01
  117. NvRegTransmitterStatus = 0x088,
  118. #define NVREG_XMITSTAT_BUSY 0x01
  119. NvRegPacketFilterFlags = 0x8c,
  120. #define NVREG_PFF_ALWAYS 0x7F0008
  121. #define NVREG_PFF_PROMISC 0x80
  122. #define NVREG_PFF_MYADDR 0x20
  123. NvRegOffloadConfig = 0x90,
  124. #define NVREG_OFFLOAD_HOMEPHY 0x601
  125. #define NVREG_OFFLOAD_NORMAL RX_NIC_BUFSIZE
  126. NvRegReceiverControl = 0x094,
  127. #define NVREG_RCVCTL_START 0x01
  128. NvRegReceiverStatus = 0x98,
  129. #define NVREG_RCVSTAT_BUSY 0x01
  130. NvRegRandomSeed = 0x9c,
  131. #define NVREG_RNDSEED_MASK 0x00ff
  132. #define NVREG_RNDSEED_FORCE 0x7f00
  133. #define NVREG_RNDSEED_FORCE2 0x2d00
  134. #define NVREG_RNDSEED_FORCE3 0x7400
  135. NvRegUnknownSetupReg1 = 0xA0,
  136. #define NVREG_UNKSETUP1_VAL 0x16070f
  137. NvRegUnknownSetupReg2 = 0xA4,
  138. #define NVREG_UNKSETUP2_VAL 0x16
  139. NvRegMacAddrA = 0xA8,
  140. NvRegMacAddrB = 0xAC,
  141. NvRegMulticastAddrA = 0xB0,
  142. #define NVREG_MCASTADDRA_FORCE 0x01
  143. NvRegMulticastAddrB = 0xB4,
  144. NvRegMulticastMaskA = 0xB8,
  145. NvRegMulticastMaskB = 0xBC,
  146. NvRegPhyInterface = 0xC0,
  147. #define PHY_RGMII 0x10000000
  148. NvRegTxRingPhysAddr = 0x100,
  149. NvRegRxRingPhysAddr = 0x104,
  150. NvRegRingSizes = 0x108,
  151. #define NVREG_RINGSZ_TXSHIFT 0
  152. #define NVREG_RINGSZ_RXSHIFT 16
  153. NvRegUnknownTransmitterReg = 0x10c,
  154. NvRegLinkSpeed = 0x110,
  155. #define NVREG_LINKSPEED_FORCE 0x10000
  156. #define NVREG_LINKSPEED_10 1000
  157. #define NVREG_LINKSPEED_100 100
  158. #define NVREG_LINKSPEED_1000 50
  159. NvRegUnknownSetupReg5 = 0x130,
  160. #define NVREG_UNKSETUP5_BIT31 (1<<31)
  161. NvRegUnknownSetupReg3 = 0x13c,
  162. #define NVREG_UNKSETUP3_VAL1 0x200010
  163. NvRegTxRxControl = 0x144,
  164. #define NVREG_TXRXCTL_KICK 0x0001
  165. #define NVREG_TXRXCTL_BIT1 0x0002
  166. #define NVREG_TXRXCTL_BIT2 0x0004
  167. #define NVREG_TXRXCTL_IDLE 0x0008
  168. #define NVREG_TXRXCTL_RESET 0x0010
  169. #define NVREG_TXRXCTL_RXCHECK 0x0400
  170. NvRegMIIStatus = 0x180,
  171. #define NVREG_MIISTAT_ERROR 0x0001
  172. #define NVREG_MIISTAT_LINKCHANGE 0x0008
  173. #define NVREG_MIISTAT_MASK 0x000f
  174. #define NVREG_MIISTAT_MASK2 0x000f
  175. NvRegUnknownSetupReg4 = 0x184,
  176. #define NVREG_UNKSETUP4_VAL 8
  177. NvRegAdapterControl = 0x188,
  178. #define NVREG_ADAPTCTL_START 0x02
  179. #define NVREG_ADAPTCTL_LINKUP 0x04
  180. #define NVREG_ADAPTCTL_PHYVALID 0x40000
  181. #define NVREG_ADAPTCTL_RUNNING 0x100000
  182. #define NVREG_ADAPTCTL_PHYSHIFT 24
  183. NvRegMIISpeed = 0x18c,
  184. #define NVREG_MIISPEED_BIT8 (1<<8)
  185. #define NVREG_MIIDELAY 5
  186. NvRegMIIControl = 0x190,
  187. #define NVREG_MIICTL_INUSE 0x08000
  188. #define NVREG_MIICTL_WRITE 0x00400
  189. #define NVREG_MIICTL_ADDRSHIFT 5
  190. NvRegMIIData = 0x194,
  191. NvRegWakeUpFlags = 0x200,
  192. #define NVREG_WAKEUPFLAGS_VAL 0x7770
  193. #define NVREG_WAKEUPFLAGS_BUSYSHIFT 24
  194. #define NVREG_WAKEUPFLAGS_ENABLESHIFT 16
  195. #define NVREG_WAKEUPFLAGS_D3SHIFT 12
  196. #define NVREG_WAKEUPFLAGS_D2SHIFT 8
  197. #define NVREG_WAKEUPFLAGS_D1SHIFT 4
  198. #define NVREG_WAKEUPFLAGS_D0SHIFT 0
  199. #define NVREG_WAKEUPFLAGS_ACCEPT_MAGPAT 0x01
  200. #define NVREG_WAKEUPFLAGS_ACCEPT_WAKEUPPAT 0x02
  201. #define NVREG_WAKEUPFLAGS_ACCEPT_LINKCHANGE 0x04
  202. #define NVREG_WAKEUPFLAGS_ENABLE 0x1111
  203. NvRegPatternCRC = 0x204,
  204. NvRegPatternMask = 0x208,
  205. NvRegPowerCap = 0x268,
  206. #define NVREG_POWERCAP_D3SUPP (1<<30)
  207. #define NVREG_POWERCAP_D2SUPP (1<<26)
  208. #define NVREG_POWERCAP_D1SUPP (1<<25)
  209. NvRegPowerState = 0x26c,
  210. #define NVREG_POWERSTATE_POWEREDUP 0x8000
  211. #define NVREG_POWERSTATE_VALID 0x0100
  212. #define NVREG_POWERSTATE_MASK 0x0003
  213. #define NVREG_POWERSTATE_D0 0x0000
  214. #define NVREG_POWERSTATE_D1 0x0001
  215. #define NVREG_POWERSTATE_D2 0x0002
  216. #define NVREG_POWERSTATE_D3 0x0003
  217. };
  218. #define FLAG_MASK_V1 0xffff0000
  219. #define FLAG_MASK_V2 0xffffc000
  220. #define LEN_MASK_V1 (0xffffffff ^ FLAG_MASK_V1)
  221. #define LEN_MASK_V2 (0xffffffff ^ FLAG_MASK_V2)
  222. #define NV_TX_LASTPACKET (1<<16)
  223. #define NV_TX_RETRYERROR (1<<19)
  224. #define NV_TX_LASTPACKET1 (1<<24)
  225. #define NV_TX_DEFERRED (1<<26)
  226. #define NV_TX_CARRIERLOST (1<<27)
  227. #define NV_TX_LATECOLLISION (1<<28)
  228. #define NV_TX_UNDERFLOW (1<<29)
  229. #define NV_TX_ERROR (1<<30)
  230. #define NV_TX_VALID (1<<31)
  231. #define NV_TX2_LASTPACKET (1<<29)
  232. #define NV_TX2_RETRYERROR (1<<18)
  233. #define NV_TX2_LASTPACKET1 (1<<23)
  234. #define NV_TX2_DEFERRED (1<<25)
  235. #define NV_TX2_CARRIERLOST (1<<26)
  236. #define NV_TX2_LATECOLLISION (1<<27)
  237. #define NV_TX2_UNDERFLOW (1<<28)
  238. /* error and valid are the same for both */
  239. #define NV_TX2_ERROR (1<<30)
  240. #define NV_TX2_VALID (1<<31)
  241. #define NV_RX_DESCRIPTORVALID (1<<16)
  242. #define NV_RX_MISSEDFRAME (1<<17)
  243. #define NV_RX_SUBSTRACT1 (1<<18)
  244. #define NV_RX_ERROR1 (1<<23)
  245. #define NV_RX_ERROR2 (1<<24)
  246. #define NV_RX_ERROR3 (1<<25)
  247. #define NV_RX_ERROR4 (1<<26)
  248. #define NV_RX_CRCERR (1<<27)
  249. #define NV_RX_OVERFLOW (1<<28)
  250. #define NV_RX_FRAMINGERR (1<<29)
  251. #define NV_RX_ERROR (1<<30)
  252. #define NV_RX_AVAIL (1<<31)
  253. #define NV_RX2_CHECKSUMMASK (0x1C000000)
  254. #define NV_RX2_CHECKSUMOK1 (0x10000000)
  255. #define NV_RX2_CHECKSUMOK2 (0x14000000)
  256. #define NV_RX2_CHECKSUMOK3 (0x18000000)
  257. #define NV_RX2_DESCRIPTORVALID (1<<29)
  258. #define NV_RX2_SUBSTRACT1 (1<<25)
  259. #define NV_RX2_ERROR1 (1<<18)
  260. #define NV_RX2_ERROR2 (1<<19)
  261. #define NV_RX2_ERROR3 (1<<20)
  262. #define NV_RX2_ERROR4 (1<<21)
  263. #define NV_RX2_CRCERR (1<<22)
  264. #define NV_RX2_OVERFLOW (1<<23)
  265. #define NV_RX2_FRAMINGERR (1<<24)
  266. /* error and avail are the same for both */
  267. #define NV_RX2_ERROR (1<<30)
  268. #define NV_RX2_AVAIL (1<<31)
  269. /* Miscelaneous hardware related defines: */
  270. #define NV_PCI_REGSZ 0x270
  271. /* various timeout delays: all in usec */
  272. #define NV_TXRX_RESET_DELAY 4
  273. #define NV_TXSTOP_DELAY1 10
  274. #define NV_TXSTOP_DELAY1MAX 500000
  275. #define NV_TXSTOP_DELAY2 100
  276. #define NV_RXSTOP_DELAY1 10
  277. #define NV_RXSTOP_DELAY1MAX 500000
  278. #define NV_RXSTOP_DELAY2 100
  279. #define NV_SETUP5_DELAY 5
  280. #define NV_SETUP5_DELAYMAX 50000
  281. #define NV_POWERUP_DELAY 5
  282. #define NV_POWERUP_DELAYMAX 5000
  283. #define NV_MIIBUSY_DELAY 50
  284. #define NV_MIIPHY_DELAY 10
  285. #define NV_MIIPHY_DELAYMAX 10000
  286. #define NV_WAKEUPPATTERNS 5
  287. #define NV_WAKEUPMASKENTRIES 4
  288. /* General driver defaults */
  289. #define NV_WATCHDOG_TIMEO (5*HZ)
  290. #define RX_RING 4
  291. #define TX_RING 2
  292. /*
  293. * If your nic mysteriously hangs then try to reduce the limits
  294. * to 1/0: It might be required to set NV_TX_LASTPACKET in the
  295. * last valid ring entry. But this would be impossible to
  296. * implement - probably a disassembly error.
  297. */
  298. #define TX_LIMIT_STOP 63
  299. #define TX_LIMIT_START 62
  300. /* rx/tx mac addr + type + vlan + align + slack*/
  301. #define RX_NIC_BUFSIZE (ETH_DATA_LEN + 64)
  302. /* even more slack */
  303. #define RX_ALLOC_BUFSIZE (ETH_DATA_LEN + 128)
  304. #define OOM_REFILL (1+HZ/20)
  305. #define POLL_WAIT (1+HZ/100)
  306. #define LINK_TIMEOUT (3*HZ)
  307. /*
  308. * desc_ver values:
  309. * This field has two purposes:
  310. * - Newer nics uses a different ring layout. The layout is selected by
  311. * comparing np->desc_ver with DESC_VER_xy.
  312. * - It contains bits that are forced on when writing to NvRegTxRxControl.
  313. */
  314. #define DESC_VER_1 0x0
  315. #define DESC_VER_2 (0x02100|NVREG_TXRXCTL_RXCHECK)
  316. /* PHY defines */
  317. #define PHY_OUI_MARVELL 0x5043
  318. #define PHY_OUI_CICADA 0x03f1
  319. #define PHYID1_OUI_MASK 0x03ff
  320. #define PHYID1_OUI_SHFT 6
  321. #define PHYID2_OUI_MASK 0xfc00
  322. #define PHYID2_OUI_SHFT 10
  323. #define PHY_INIT1 0x0f000
  324. #define PHY_INIT2 0x0e00
  325. #define PHY_INIT3 0x01000
  326. #define PHY_INIT4 0x0200
  327. #define PHY_INIT5 0x0004
  328. #define PHY_INIT6 0x02000
  329. #define PHY_GIGABIT 0x0100
  330. #define PHY_TIMEOUT 0x1
  331. #define PHY_ERROR 0x2
  332. #define PHY_100 0x1
  333. #define PHY_1000 0x2
  334. #define PHY_HALF 0x100
  335. /* FIXME: MII defines that should be added to <linux/mii.h> */
  336. #define MII_1000BT_CR 0x09
  337. #define MII_1000BT_SR 0x0a
  338. #define ADVERTISE_1000FULL 0x0200
  339. #define ADVERTISE_1000HALF 0x0100
  340. #define LPA_1000FULL 0x0800
  341. #define LPA_1000HALF 0x0400
  342. /* Bit to know if MAC addr is stored in correct order */
  343. #define MAC_ADDR_CORRECT 0x01
  344. /* Big endian: should work, but is untested */
  345. struct ring_desc {
  346. u32 PacketBuffer;
  347. u32 FlagLen;
  348. };
  349. /* Define the TX and RX Descriptor and Buffers */
  350. struct {
  351. struct ring_desc tx_ring[TX_RING];
  352. unsigned char txb[TX_RING * RX_NIC_BUFSIZE];
  353. struct ring_desc rx_ring[RX_RING];
  354. unsigned char rxb[RX_RING * RX_NIC_BUFSIZE];
  355. } forcedeth_bufs __shared;
  356. #define tx_ring forcedeth_bufs.tx_ring
  357. #define rx_ring forcedeth_bufs.rx_ring
  358. #define txb forcedeth_bufs.txb
  359. #define rxb forcedeth_bufs.rxb
  360. /* Private Storage for the NIC */
  361. static struct forcedeth_private {
  362. /* General data:
  363. * Locking: spin_lock(&np->lock); */
  364. int in_shutdown;
  365. u32 linkspeed;
  366. int duplex;
  367. int phyaddr;
  368. int wolenabled;
  369. unsigned int phy_oui;
  370. u16 gigabit;
  371. /* General data: RO fields */
  372. u8 *ring_addr;
  373. u32 orig_mac[2];
  374. u32 irqmask;
  375. u32 desc_ver;
  376. /* rx specific fields.
  377. * Locking: Within irq hander or disable_irq+spin_lock(&np->lock);
  378. */
  379. unsigned int cur_rx, refill_rx;
  380. /*
  381. * tx specific fields.
  382. */
  383. unsigned int next_tx, nic_tx;
  384. u32 tx_flags;
  385. } npx;
  386. static struct forcedeth_private *np;
  387. static inline void pci_push(u8 * base)
  388. {
  389. /* force out pending posted writes */
  390. readl(base);
  391. }
  392. static inline u32 nv_descr_getlength(struct ring_desc *prd, u32 v)
  393. {
  394. return le32_to_cpu(prd->FlagLen)
  395. & ((v == DESC_VER_1) ? LEN_MASK_V1 : LEN_MASK_V2);
  396. }
  397. static int reg_delay(int offset, u32 mask,
  398. u32 target, int delay, int delaymax, const char *msg)
  399. {
  400. u8 *base = (u8 *) BASE;
  401. pci_push(base);
  402. do {
  403. udelay(delay);
  404. delaymax -= delay;
  405. if (delaymax < 0) {
  406. if (msg)
  407. printf("%s", msg);
  408. return 1;
  409. }
  410. } while ((readl(base + offset) & mask) != target);
  411. return 0;
  412. }
  413. #define MII_READ (-1)
  414. #define MII_PHYSID1 0x02 /* PHYS ID 1 */
  415. #define MII_PHYSID2 0x03 /* PHYS ID 2 */
  416. #define MII_BMCR 0x00 /* Basic mode control register */
  417. #define MII_BMSR 0x01 /* Basic mode status register */
  418. #define MII_ADVERTISE 0x04 /* Advertisement control reg */
  419. #define MII_LPA 0x05 /* Link partner ability reg */
  420. #define BMSR_ANEGCOMPLETE 0x0020 /* Auto-negotiation complete */
  421. /* Link partner ability register. */
  422. #define LPA_SLCT 0x001f /* Same as advertise selector */
  423. #define LPA_10HALF 0x0020 /* Can do 10mbps half-duplex */
  424. #define LPA_10FULL 0x0040 /* Can do 10mbps full-duplex */
  425. #define LPA_100HALF 0x0080 /* Can do 100mbps half-duplex */
  426. #define LPA_100FULL 0x0100 /* Can do 100mbps full-duplex */
  427. #define LPA_100BASE4 0x0200 /* Can do 100mbps 4k packets */
  428. #define LPA_RESV 0x1c00 /* Unused... */
  429. #define LPA_RFAULT 0x2000 /* Link partner faulted */
  430. #define LPA_LPACK 0x4000 /* Link partner acked us */
  431. #define LPA_NPAGE 0x8000 /* Next page bit */
  432. /* mii_rw: read/write a register on the PHY.
  433. *
  434. * Caller must guarantee serialization
  435. */
  436. static int mii_rw(struct nic *nic __unused, int addr, int miireg,
  437. int value)
  438. {
  439. u8 *base = (u8 *) BASE;
  440. u32 reg;
  441. int retval;
  442. writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
  443. reg = readl(base + NvRegMIIControl);
  444. if (reg & NVREG_MIICTL_INUSE) {
  445. writel(NVREG_MIICTL_INUSE, base + NvRegMIIControl);
  446. udelay(NV_MIIBUSY_DELAY);
  447. }
  448. reg =
  449. (addr << NVREG_MIICTL_ADDRSHIFT) | miireg;
  450. if (value != MII_READ) {
  451. writel(value, base + NvRegMIIData);
  452. reg |= NVREG_MIICTL_WRITE;
  453. }
  454. writel(reg, base + NvRegMIIControl);
  455. if (reg_delay(NvRegMIIControl, NVREG_MIICTL_INUSE, 0,
  456. NV_MIIPHY_DELAY, NV_MIIPHY_DELAYMAX, NULL)) {
  457. dprintf(("mii_rw of reg %d at PHY %d timed out.\n",
  458. miireg, addr));
  459. retval = -1;
  460. } else if (value != MII_READ) {
  461. /* it was a write operation - fewer failures are detectable */
  462. dprintf(("mii_rw wrote 0x%x to reg %d at PHY %d\n",
  463. value, miireg, addr));
  464. retval = 0;
  465. } else if (readl(base + NvRegMIIStatus) & NVREG_MIISTAT_ERROR) {
  466. dprintf(("mii_rw of reg %d at PHY %d failed.\n",
  467. miireg, addr));
  468. retval = -1;
  469. } else {
  470. retval = readl(base + NvRegMIIData);
  471. dprintf(("mii_rw read from reg %d at PHY %d: 0x%x.\n",
  472. miireg, addr, retval));
  473. }
  474. return retval;
  475. }
  476. static int phy_reset(struct nic *nic)
  477. {
  478. u32 miicontrol;
  479. unsigned int tries = 0;
  480. miicontrol = mii_rw(nic, np->phyaddr, MII_BMCR, MII_READ);
  481. miicontrol |= BMCR_RESET;
  482. if (mii_rw(nic, np->phyaddr, MII_BMCR, miicontrol)) {
  483. return -1;
  484. }
  485. /* wait for 500ms */
  486. mdelay(500);
  487. /* must wait till reset is deasserted */
  488. while (miicontrol & BMCR_RESET) {
  489. mdelay(10);
  490. miicontrol = mii_rw(nic, np->phyaddr, MII_BMCR, MII_READ);
  491. /* FIXME: 100 tries seem excessive */
  492. if (tries++ > 100)
  493. return -1;
  494. }
  495. return 0;
  496. }
  497. static int phy_init(struct nic *nic)
  498. {
  499. u8 *base = (u8 *) BASE;
  500. u32 phyinterface, phy_reserved, mii_status, mii_control,
  501. mii_control_1000, reg;
  502. /* set advertise register */
  503. reg = mii_rw(nic, np->phyaddr, MII_ADVERTISE, MII_READ);
  504. reg |=
  505. (ADVERTISE_10HALF | ADVERTISE_10FULL | ADVERTISE_100HALF |
  506. ADVERTISE_100FULL | 0x800 | 0x400);
  507. if (mii_rw(nic, np->phyaddr, MII_ADVERTISE, reg)) {
  508. printf("phy write to advertise failed.\n");
  509. return PHY_ERROR;
  510. }
  511. /* get phy interface type */
  512. phyinterface = readl(base + NvRegPhyInterface);
  513. /* see if gigabit phy */
  514. mii_status = mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  515. if (mii_status & PHY_GIGABIT) {
  516. np->gigabit = PHY_GIGABIT;
  517. mii_control_1000 =
  518. mii_rw(nic, np->phyaddr, MII_1000BT_CR, MII_READ);
  519. mii_control_1000 &= ~ADVERTISE_1000HALF;
  520. if (phyinterface & PHY_RGMII)
  521. mii_control_1000 |= ADVERTISE_1000FULL;
  522. else
  523. mii_control_1000 &= ~ADVERTISE_1000FULL;
  524. if (mii_rw
  525. (nic, np->phyaddr, MII_1000BT_CR, mii_control_1000)) {
  526. printf("phy init failed.\n");
  527. return PHY_ERROR;
  528. }
  529. } else
  530. np->gigabit = 0;
  531. /* reset the phy */
  532. if (phy_reset(nic)) {
  533. printf("phy reset failed\n");
  534. return PHY_ERROR;
  535. }
  536. /* phy vendor specific configuration */
  537. if ((np->phy_oui == PHY_OUI_CICADA) && (phyinterface & PHY_RGMII)) {
  538. phy_reserved =
  539. mii_rw(nic, np->phyaddr, MII_RESV1, MII_READ);
  540. phy_reserved &= ~(PHY_INIT1 | PHY_INIT2);
  541. phy_reserved |= (PHY_INIT3 | PHY_INIT4);
  542. if (mii_rw(nic, np->phyaddr, MII_RESV1, phy_reserved)) {
  543. printf("phy init failed.\n");
  544. return PHY_ERROR;
  545. }
  546. phy_reserved =
  547. mii_rw(nic, np->phyaddr, MII_NCONFIG, MII_READ);
  548. phy_reserved |= PHY_INIT5;
  549. if (mii_rw(nic, np->phyaddr, MII_NCONFIG, phy_reserved)) {
  550. printf("phy init failed.\n");
  551. return PHY_ERROR;
  552. }
  553. }
  554. if (np->phy_oui == PHY_OUI_CICADA) {
  555. phy_reserved =
  556. mii_rw(nic, np->phyaddr, MII_SREVISION, MII_READ);
  557. phy_reserved |= PHY_INIT6;
  558. if (mii_rw(nic, np->phyaddr, MII_SREVISION, phy_reserved)) {
  559. printf("phy init failed.\n");
  560. return PHY_ERROR;
  561. }
  562. }
  563. /* restart auto negotiation */
  564. mii_control = mii_rw(nic, np->phyaddr, MII_BMCR, MII_READ);
  565. mii_control |= (BMCR_ANRESTART | BMCR_ANENABLE);
  566. if (mii_rw(nic, np->phyaddr, MII_BMCR, mii_control)) {
  567. return PHY_ERROR;
  568. }
  569. return 0;
  570. }
  571. static void start_rx(struct nic *nic __unused)
  572. {
  573. u8 *base = (u8 *) BASE;
  574. dprintf(("start_rx\n"));
  575. /* Already running? Stop it. */
  576. if (readl(base + NvRegReceiverControl) & NVREG_RCVCTL_START) {
  577. writel(0, base + NvRegReceiverControl);
  578. pci_push(base);
  579. }
  580. writel(np->linkspeed, base + NvRegLinkSpeed);
  581. pci_push(base);
  582. writel(NVREG_RCVCTL_START, base + NvRegReceiverControl);
  583. pci_push(base);
  584. }
  585. static void stop_rx(void)
  586. {
  587. u8 *base = (u8 *) BASE;
  588. dprintf(("stop_rx\n"));
  589. writel(0, base + NvRegReceiverControl);
  590. reg_delay(NvRegReceiverStatus, NVREG_RCVSTAT_BUSY, 0,
  591. NV_RXSTOP_DELAY1, NV_RXSTOP_DELAY1MAX,
  592. "stop_rx: ReceiverStatus remained busy");
  593. udelay(NV_RXSTOP_DELAY2);
  594. writel(0, base + NvRegLinkSpeed);
  595. }
  596. static void start_tx(struct nic *nic __unused)
  597. {
  598. u8 *base = (u8 *) BASE;
  599. dprintf(("start_tx\n"));
  600. writel(NVREG_XMITCTL_START, base + NvRegTransmitterControl);
  601. pci_push(base);
  602. }
  603. static void stop_tx(void)
  604. {
  605. u8 *base = (u8 *) BASE;
  606. dprintf(("stop_tx\n"));
  607. writel(0, base + NvRegTransmitterControl);
  608. reg_delay(NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0,
  609. NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX,
  610. "stop_tx: TransmitterStatus remained busy");
  611. udelay(NV_TXSTOP_DELAY2);
  612. writel(0, base + NvRegUnknownTransmitterReg);
  613. }
  614. static void txrx_reset(struct nic *nic __unused)
  615. {
  616. u8 *base = (u8 *) BASE;
  617. dprintf(("txrx_reset\n"));
  618. writel(NVREG_TXRXCTL_BIT2 | NVREG_TXRXCTL_RESET | np->desc_ver,
  619. base + NvRegTxRxControl);
  620. pci_push(base);
  621. udelay(NV_TXRX_RESET_DELAY);
  622. writel(NVREG_TXRXCTL_BIT2 | np->desc_ver, base + NvRegTxRxControl);
  623. pci_push(base);
  624. }
  625. /*
  626. * alloc_rx: fill rx ring entries.
  627. * Return 1 if the allocations for the skbs failed and the
  628. * rx engine is without Available descriptors
  629. */
  630. static int alloc_rx(struct nic *nic __unused)
  631. {
  632. unsigned int refill_rx = np->refill_rx;
  633. int i;
  634. //while (np->cur_rx != refill_rx) {
  635. for (i = 0; i < RX_RING; i++) {
  636. //int nr = refill_rx % RX_RING;
  637. rx_ring[i].PacketBuffer =
  638. virt_to_le32desc(&rxb[i * RX_NIC_BUFSIZE]);
  639. wmb();
  640. rx_ring[i].FlagLen =
  641. cpu_to_le32(RX_NIC_BUFSIZE | NV_RX_AVAIL);
  642. /* printf("alloc_rx: Packet %d marked as Available\n",
  643. refill_rx); */
  644. refill_rx++;
  645. }
  646. np->refill_rx = refill_rx;
  647. if (np->cur_rx - refill_rx == RX_RING)
  648. return 1;
  649. return 0;
  650. }
  651. static int update_linkspeed(struct nic *nic)
  652. {
  653. int adv, lpa;
  654. u32 newls;
  655. int newdup = np->duplex;
  656. u32 mii_status;
  657. int retval = 0;
  658. u32 control_1000, status_1000, phyreg;
  659. u8 *base = (u8 *) BASE;
  660. int i;
  661. /* BMSR_LSTATUS is latched, read it twice:
  662. * we want the current value.
  663. */
  664. mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  665. mii_status = mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  666. #if 1
  667. //yhlu
  668. for(i=0;i<30;i++) {
  669. mii_status = mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  670. if((mii_status & BMSR_LSTATUS) && (mii_status & BMSR_ANEGCOMPLETE)) break;
  671. mdelay(100);
  672. }
  673. #endif
  674. if (!(mii_status & BMSR_LSTATUS)) {
  675. printf
  676. ("no link detected by phy - falling back to 10HD.\n");
  677. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  678. newdup = 0;
  679. retval = 0;
  680. goto set_speed;
  681. }
  682. /* check auto negotiation is complete */
  683. if (!(mii_status & BMSR_ANEGCOMPLETE)) {
  684. /* still in autonegotiation - configure nic for 10 MBit HD and wait. */
  685. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  686. newdup = 0;
  687. retval = 0;
  688. printf("autoneg not completed - falling back to 10HD.\n");
  689. goto set_speed;
  690. }
  691. retval = 1;
  692. if (np->gigabit == PHY_GIGABIT) {
  693. control_1000 =
  694. mii_rw(nic, np->phyaddr, MII_1000BT_CR, MII_READ);
  695. status_1000 =
  696. mii_rw(nic, np->phyaddr, MII_1000BT_SR, MII_READ);
  697. if ((control_1000 & ADVERTISE_1000FULL) &&
  698. (status_1000 & LPA_1000FULL)) {
  699. printf
  700. ("update_linkspeed: GBit ethernet detected.\n");
  701. newls =
  702. NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_1000;
  703. newdup = 1;
  704. goto set_speed;
  705. }
  706. }
  707. adv = mii_rw(nic, np->phyaddr, MII_ADVERTISE, MII_READ);
  708. lpa = mii_rw(nic, np->phyaddr, MII_LPA, MII_READ);
  709. dprintf(("update_linkspeed: PHY advertises 0x%hX, lpa 0x%hX.\n",
  710. adv, lpa));
  711. /* FIXME: handle parallel detection properly, handle gigabit ethernet */
  712. lpa = lpa & adv;
  713. if (lpa & LPA_100FULL) {
  714. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_100;
  715. newdup = 1;
  716. } else if (lpa & LPA_100HALF) {
  717. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_100;
  718. newdup = 0;
  719. } else if (lpa & LPA_10FULL) {
  720. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  721. newdup = 1;
  722. } else if (lpa & LPA_10HALF) {
  723. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  724. newdup = 0;
  725. } else {
  726. printf("bad ability %hX - falling back to 10HD.\n", lpa);
  727. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  728. newdup = 0;
  729. }
  730. set_speed:
  731. if (np->duplex == newdup && np->linkspeed == newls)
  732. return retval;
  733. dprintf(("changing link setting from %d/%s to %d/%s.\n",
  734. np->linkspeed, np->duplex ? "Full-Duplex": "Half-Duplex", newls, newdup ? "Full-Duplex": "Half-Duplex"));
  735. np->duplex = newdup;
  736. np->linkspeed = newls;
  737. if (np->gigabit == PHY_GIGABIT) {
  738. phyreg = readl(base + NvRegRandomSeed);
  739. phyreg &= ~(0x3FF00);
  740. if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_10)
  741. phyreg |= NVREG_RNDSEED_FORCE3;
  742. else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100)
  743. phyreg |= NVREG_RNDSEED_FORCE2;
  744. else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
  745. phyreg |= NVREG_RNDSEED_FORCE;
  746. writel(phyreg, base + NvRegRandomSeed);
  747. }
  748. phyreg = readl(base + NvRegPhyInterface);
  749. phyreg &= ~(PHY_HALF | PHY_100 | PHY_1000);
  750. if (np->duplex == 0)
  751. phyreg |= PHY_HALF;
  752. if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100)
  753. phyreg |= PHY_100;
  754. else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
  755. phyreg |= PHY_1000;
  756. writel(phyreg, base + NvRegPhyInterface);
  757. writel(NVREG_MISC1_FORCE | (np->duplex ? 0 : NVREG_MISC1_HD),
  758. base + NvRegMisc1);
  759. pci_push(base);
  760. writel(np->linkspeed, base + NvRegLinkSpeed);
  761. pci_push(base);
  762. return retval;
  763. }
  764. #if 0 /* Not used */
  765. static void nv_linkchange(struct nic *nic)
  766. {
  767. if (update_linkspeed(nic)) {
  768. // if (netif_carrier_ok(nic)) {
  769. stop_rx();
  770. //= } else {
  771. // netif_carrier_on(dev);
  772. // printk(KERN_INFO "%s: link up.\n", dev->name);
  773. // }
  774. start_rx(nic);
  775. } else {
  776. // if (netif_carrier_ok(dev)) {
  777. // netif_carrier_off(dev);
  778. // printk(KERN_INFO "%s: link down.\n", dev->name);
  779. stop_rx();
  780. // }
  781. }
  782. }
  783. #endif
  784. static int init_ring(struct nic *nic)
  785. {
  786. int i;
  787. np->next_tx = np->nic_tx = 0;
  788. for (i = 0; i < TX_RING; i++)
  789. tx_ring[i].FlagLen = 0;
  790. np->cur_rx = 0;
  791. np->refill_rx = 0;
  792. for (i = 0; i < RX_RING; i++)
  793. rx_ring[i].FlagLen = 0;
  794. return alloc_rx(nic);
  795. }
  796. static void set_multicast(struct nic *nic)
  797. {
  798. u8 *base = (u8 *) BASE;
  799. u32 addr[2];
  800. u32 mask[2];
  801. u32 pff;
  802. u32 alwaysOff[2];
  803. u32 alwaysOn[2];
  804. memset(addr, 0, sizeof(addr));
  805. memset(mask, 0, sizeof(mask));
  806. pff = NVREG_PFF_MYADDR;
  807. alwaysOn[0] = alwaysOn[1] = alwaysOff[0] = alwaysOff[1] = 0;
  808. addr[0] = alwaysOn[0];
  809. addr[1] = alwaysOn[1];
  810. mask[0] = alwaysOn[0] | alwaysOff[0];
  811. mask[1] = alwaysOn[1] | alwaysOff[1];
  812. addr[0] |= NVREG_MCASTADDRA_FORCE;
  813. pff |= NVREG_PFF_ALWAYS;
  814. stop_rx();
  815. writel(addr[0], base + NvRegMulticastAddrA);
  816. writel(addr[1], base + NvRegMulticastAddrB);
  817. writel(mask[0], base + NvRegMulticastMaskA);
  818. writel(mask[1], base + NvRegMulticastMaskB);
  819. writel(pff, base + NvRegPacketFilterFlags);
  820. start_rx(nic);
  821. }
  822. /**************************************************************************
  823. RESET - Reset the NIC to prepare for use
  824. ***************************************************************************/
  825. static int forcedeth_reset(struct nic *nic)
  826. {
  827. u8 *base = (u8 *) BASE;
  828. int ret, oom, i;
  829. ret = 0;
  830. dprintf(("forcedeth: open\n"));
  831. /* 1) erase previous misconfiguration */
  832. /* 4.1-1: stop adapter: ignored, 4.3 seems to be overkill */
  833. writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
  834. writel(0, base + NvRegMulticastAddrB);
  835. writel(0, base + NvRegMulticastMaskA);
  836. writel(0, base + NvRegMulticastMaskB);
  837. writel(0, base + NvRegPacketFilterFlags);
  838. writel(0, base + NvRegTransmitterControl);
  839. writel(0, base + NvRegReceiverControl);
  840. writel(0, base + NvRegAdapterControl);
  841. /* 2) initialize descriptor rings */
  842. oom = init_ring(nic);
  843. writel(0, base + NvRegLinkSpeed);
  844. writel(0, base + NvRegUnknownTransmitterReg);
  845. txrx_reset(nic);
  846. writel(0, base + NvRegUnknownSetupReg6);
  847. np->in_shutdown = 0;
  848. /* 3) set mac address */
  849. {
  850. u32 mac[2];
  851. mac[0] =
  852. (nic->node_addr[0] << 0) + (nic->node_addr[1] << 8) +
  853. (nic->node_addr[2] << 16) + (nic->node_addr[3] << 24);
  854. mac[1] =
  855. (nic->node_addr[4] << 0) + (nic->node_addr[5] << 8);
  856. writel(mac[0], base + NvRegMacAddrA);
  857. writel(mac[1], base + NvRegMacAddrB);
  858. }
  859. /* 4) give hw rings */
  860. writel((u32) virt_to_le32desc(&rx_ring[0]),
  861. base + NvRegRxRingPhysAddr);
  862. writel((u32) virt_to_le32desc(&tx_ring[0]),
  863. base + NvRegTxRingPhysAddr);
  864. writel(((RX_RING - 1) << NVREG_RINGSZ_RXSHIFT) +
  865. ((TX_RING - 1) << NVREG_RINGSZ_TXSHIFT),
  866. base + NvRegRingSizes);
  867. /* 5) continue setup */
  868. np->linkspeed = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  869. np->duplex = 0;
  870. writel(np->linkspeed, base + NvRegLinkSpeed);
  871. writel(NVREG_UNKSETUP3_VAL1, base + NvRegUnknownSetupReg3);
  872. writel(np->desc_ver, base + NvRegTxRxControl);
  873. pci_push(base);
  874. writel(NVREG_TXRXCTL_BIT1 | np->desc_ver, base + NvRegTxRxControl);
  875. reg_delay(NvRegUnknownSetupReg5, NVREG_UNKSETUP5_BIT31,
  876. NVREG_UNKSETUP5_BIT31, NV_SETUP5_DELAY,
  877. NV_SETUP5_DELAYMAX,
  878. "open: SetupReg5, Bit 31 remained off\n");
  879. writel(0, base + NvRegUnknownSetupReg4);
  880. // writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
  881. writel(NVREG_MIISTAT_MASK2, base + NvRegMIIStatus);
  882. #if 0
  883. printf("%d-Mbs Link, %s-Duplex\n",
  884. np->linkspeed & NVREG_LINKSPEED_10 ? 10 : 100,
  885. np->duplex ? "Full" : "Half");
  886. #endif
  887. /* 6) continue setup */
  888. writel(NVREG_MISC1_FORCE | NVREG_MISC1_HD, base + NvRegMisc1);
  889. writel(readl(base + NvRegTransmitterStatus),
  890. base + NvRegTransmitterStatus);
  891. writel(NVREG_PFF_ALWAYS, base + NvRegPacketFilterFlags);
  892. writel(NVREG_OFFLOAD_NORMAL, base + NvRegOffloadConfig);
  893. writel(readl(base + NvRegReceiverStatus),
  894. base + NvRegReceiverStatus);
  895. /* Get a random number */
  896. i = random();
  897. writel(NVREG_RNDSEED_FORCE | (i & NVREG_RNDSEED_MASK),
  898. base + NvRegRandomSeed);
  899. writel(NVREG_UNKSETUP1_VAL, base + NvRegUnknownSetupReg1);
  900. writel(NVREG_UNKSETUP2_VAL, base + NvRegUnknownSetupReg2);
  901. writel(NVREG_POLL_DEFAULT, base + NvRegPollingInterval);
  902. writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
  903. writel((np->
  904. phyaddr << NVREG_ADAPTCTL_PHYSHIFT) |
  905. NVREG_ADAPTCTL_PHYVALID | NVREG_ADAPTCTL_RUNNING,
  906. base + NvRegAdapterControl);
  907. writel(NVREG_MIISPEED_BIT8 | NVREG_MIIDELAY, base + NvRegMIISpeed);
  908. writel(NVREG_UNKSETUP4_VAL, base + NvRegUnknownSetupReg4);
  909. writel(NVREG_WAKEUPFLAGS_VAL, base + NvRegWakeUpFlags);
  910. i = readl(base + NvRegPowerState);
  911. if ((i & NVREG_POWERSTATE_POWEREDUP) == 0)
  912. writel(NVREG_POWERSTATE_POWEREDUP | i,
  913. base + NvRegPowerState);
  914. pci_push(base);
  915. udelay(10);
  916. writel(readl(base + NvRegPowerState) | NVREG_POWERSTATE_VALID,
  917. base + NvRegPowerState);
  918. writel(0, base + NvRegIrqMask);
  919. pci_push(base);
  920. writel(NVREG_MIISTAT_MASK2, base + NvRegMIIStatus);
  921. writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
  922. pci_push(base);
  923. /*
  924. writel(np->irqmask, base + NvRegIrqMask);
  925. */
  926. writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
  927. writel(0, base + NvRegMulticastAddrB);
  928. writel(0, base + NvRegMulticastMaskA);
  929. writel(0, base + NvRegMulticastMaskB);
  930. writel(NVREG_PFF_ALWAYS | NVREG_PFF_MYADDR,
  931. base + NvRegPacketFilterFlags);
  932. set_multicast(nic);
  933. /* One manual link speed update: Interrupts are enabled, future link
  934. * speed changes cause interrupts and are handled by nv_link_irq().
  935. */
  936. {
  937. u32 miistat;
  938. miistat = readl(base + NvRegMIIStatus);
  939. writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
  940. dprintf(("startup: got 0x%hX.\n", miistat));
  941. }
  942. ret = update_linkspeed(nic);
  943. //start_rx(nic);
  944. start_tx(nic);
  945. if (ret) {
  946. //Start Connection netif_carrier_on(dev);
  947. } else {
  948. printf("no link during initialization.\n");
  949. }
  950. return ret;
  951. }
  952. /*
  953. * extern void hex_dump(const char *data, const unsigned int len);
  954. */
  955. /**************************************************************************
  956. POLL - Wait for a frame
  957. ***************************************************************************/
  958. static int forcedeth_poll(struct nic *nic, int retrieve)
  959. {
  960. /* return true if there's an ethernet packet ready to read */
  961. /* nic->packet should contain data on return */
  962. /* nic->packetlen should contain length of data */
  963. int len;
  964. int i;
  965. u32 Flags;
  966. i = np->cur_rx % RX_RING;
  967. Flags = le32_to_cpu(rx_ring[i].FlagLen);
  968. len = nv_descr_getlength(&rx_ring[i], np->desc_ver);
  969. if (Flags & NV_RX_AVAIL)
  970. return 0; /* still owned by hardware, */
  971. if (np->desc_ver == DESC_VER_1) {
  972. if (!(Flags & NV_RX_DESCRIPTORVALID))
  973. return 0;
  974. } else {
  975. if (!(Flags & NV_RX2_DESCRIPTORVALID))
  976. return 0;
  977. }
  978. if (!retrieve)
  979. return 1;
  980. /* got a valid packet - forward it to the network core */
  981. nic->packetlen = len;
  982. memcpy(nic->packet, rxb + (i * RX_NIC_BUFSIZE), nic->packetlen);
  983. /*
  984. * hex_dump(rxb + (i * RX_NIC_BUFSIZE), len);
  985. */
  986. wmb();
  987. np->cur_rx++;
  988. alloc_rx(nic);
  989. return 1;
  990. }
  991. /**************************************************************************
  992. TRANSMIT - Transmit a frame
  993. ***************************************************************************/
  994. static void forcedeth_transmit(struct nic *nic, const char *d, /* Destination */
  995. unsigned int t, /* Type */
  996. unsigned int s, /* size */
  997. const char *p)
  998. { /* Packet */
  999. /* send the packet to destination */
  1000. u8 *ptxb;
  1001. u16 nstype;
  1002. u8 *base = (u8 *) BASE;
  1003. int nr = np->next_tx % TX_RING;
  1004. /* point to the current txb incase multiple tx_rings are used */
  1005. ptxb = txb + (nr * RX_NIC_BUFSIZE);
  1006. //np->tx_skbuff[nr] = ptxb;
  1007. /* copy the packet to ring buffer */
  1008. memcpy(ptxb, d, ETH_ALEN); /* dst */
  1009. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN); /* src */
  1010. nstype = htons((u16) t); /* type */
  1011. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2); /* type */
  1012. memcpy(ptxb + ETH_HLEN, p, s);
  1013. s += ETH_HLEN;
  1014. while (s < ETH_ZLEN) /* pad to min length */
  1015. ptxb[s++] = '\0';
  1016. tx_ring[nr].PacketBuffer = (u32) virt_to_le32desc(ptxb);
  1017. wmb();
  1018. tx_ring[nr].FlagLen = cpu_to_le32((s - 1) | np->tx_flags);
  1019. writel(NVREG_TXRXCTL_KICK | np->desc_ver, base + NvRegTxRxControl);
  1020. pci_push(base);
  1021. np->next_tx++;
  1022. }
  1023. /**************************************************************************
  1024. DISABLE - Turn off ethernet interface
  1025. ***************************************************************************/
  1026. static void forcedeth_disable ( struct nic *nic __unused ) {
  1027. /* put the card in its initial state */
  1028. /* This function serves 3 purposes.
  1029. * This disables DMA and interrupts so we don't receive
  1030. * unexpected packets or interrupts from the card after
  1031. * etherboot has finished.
  1032. * This frees resources so etherboot may use
  1033. * this driver on another interface
  1034. * This allows etherboot to reinitialize the interface
  1035. * if something is something goes wrong.
  1036. */
  1037. u8 *base = (u8 *) BASE;
  1038. np->in_shutdown = 1;
  1039. stop_tx();
  1040. stop_rx();
  1041. /* disable interrupts on the nic or we will lock up */
  1042. writel(0, base + NvRegIrqMask);
  1043. pci_push(base);
  1044. dprintf(("Irqmask is zero again\n"));
  1045. /* specia op:o write back the misordered MAC address - otherwise
  1046. * the next probe_nic would see a wrong address.
  1047. */
  1048. writel(np->orig_mac[0], base + NvRegMacAddrA);
  1049. writel(np->orig_mac[1], base + NvRegMacAddrB);
  1050. }
  1051. /**************************************************************************
  1052. IRQ - Enable, Disable, or Force interrupts
  1053. ***************************************************************************/
  1054. static void forcedeth_irq(struct nic *nic __unused,
  1055. irq_action_t action __unused)
  1056. {
  1057. switch (action) {
  1058. case DISABLE:
  1059. break;
  1060. case ENABLE:
  1061. break;
  1062. case FORCE:
  1063. break;
  1064. }
  1065. }
  1066. static struct nic_operations forcedeth_operations = {
  1067. .connect = dummy_connect,
  1068. .poll = forcedeth_poll,
  1069. .transmit = forcedeth_transmit,
  1070. .irq = forcedeth_irq,
  1071. };
  1072. /**************************************************************************
  1073. PROBE - Look for an adapter, this routine's visible to the outside
  1074. ***************************************************************************/
  1075. #define IORESOURCE_MEM 0x00000200
  1076. #define board_found 1
  1077. #define valid_link 0
  1078. static int forcedeth_probe ( struct nic *nic, struct pci_device *pci ) {
  1079. unsigned long addr;
  1080. int sz;
  1081. u8 *base;
  1082. int i;
  1083. struct pci_device_id *ids = pci->driver->ids;
  1084. int id_count = pci->driver->id_count;
  1085. unsigned int flags = 0;
  1086. if (pci->ioaddr == 0)
  1087. return 0;
  1088. printf("forcedeth.c: Found %s, vendor=0x%hX, device=0x%hX\n",
  1089. pci->driver_name, pci->vendor, pci->device);
  1090. nic->ioaddr = pci->ioaddr;
  1091. nic->irqno = 0;
  1092. /* point to private storage */
  1093. np = &npx;
  1094. adjust_pci_device(pci);
  1095. addr = pci_bar_start(pci, PCI_BASE_ADDRESS_0);
  1096. sz = pci_bar_size(pci, PCI_BASE_ADDRESS_0);
  1097. /* BASE is used throughout to address the card */
  1098. BASE = (unsigned long) ioremap(addr, sz);
  1099. if (!BASE)
  1100. return 0;
  1101. /* handle different descriptor versions */
  1102. if (pci->device == PCI_DEVICE_ID_NVIDIA_NVENET_1 ||
  1103. pci->device == PCI_DEVICE_ID_NVIDIA_NVENET_2 ||
  1104. pci->device == PCI_DEVICE_ID_NVIDIA_NVENET_3)
  1105. np->desc_ver = DESC_VER_1;
  1106. else
  1107. np->desc_ver = DESC_VER_2;
  1108. //rx_ring[0] = rx_ring;
  1109. //tx_ring[0] = tx_ring;
  1110. /* read the mac address */
  1111. base = (u8 *) BASE;
  1112. np->orig_mac[0] = readl(base + NvRegMacAddrA);
  1113. np->orig_mac[1] = readl(base + NvRegMacAddrB);
  1114. /* lookup the flags from pci_device_id */
  1115. for(i = 0; i < id_count; i++) {
  1116. if(pci->vendor == ids[i].vendor &&
  1117. pci->device == ids[i].device) {
  1118. flags = ids[i].driver_data;
  1119. break;
  1120. }
  1121. }
  1122. /* read MAC address */
  1123. if(flags & MAC_ADDR_CORRECT) {
  1124. nic->node_addr[0] = (np->orig_mac[0] >> 0) & 0xff;
  1125. nic->node_addr[1] = (np->orig_mac[0] >> 8) & 0xff;
  1126. nic->node_addr[2] = (np->orig_mac[0] >> 16) & 0xff;
  1127. nic->node_addr[3] = (np->orig_mac[0] >> 24) & 0xff;
  1128. nic->node_addr[4] = (np->orig_mac[1] >> 0) & 0xff;
  1129. nic->node_addr[5] = (np->orig_mac[1] >> 8) & 0xff;
  1130. } else {
  1131. nic->node_addr[0] = (np->orig_mac[1] >> 8) & 0xff;
  1132. nic->node_addr[1] = (np->orig_mac[1] >> 0) & 0xff;
  1133. nic->node_addr[2] = (np->orig_mac[0] >> 24) & 0xff;
  1134. nic->node_addr[3] = (np->orig_mac[0] >> 16) & 0xff;
  1135. nic->node_addr[4] = (np->orig_mac[0] >> 8) & 0xff;
  1136. nic->node_addr[5] = (np->orig_mac[0] >> 0) & 0xff;
  1137. }
  1138. #ifdef LINUX
  1139. if (!is_valid_ether_addr(dev->dev_addr)) {
  1140. /*
  1141. * Bad mac address. At least one bios sets the mac address
  1142. * to 01:23:45:67:89:ab
  1143. */
  1144. printk(KERN_ERR
  1145. "%s: Invalid Mac address detected: %02x:%02x:%02x:%02x:%02x:%02x\n",
  1146. pci_name(pci_dev), dev->dev_addr[0],
  1147. dev->dev_addr[1], dev->dev_addr[2],
  1148. dev->dev_addr[3], dev->dev_addr[4],
  1149. dev->dev_addr[5]);
  1150. printk(KERN_ERR
  1151. "Please complain to your hardware vendor. Switching to a random MAC.\n");
  1152. dev->dev_addr[0] = 0x00;
  1153. dev->dev_addr[1] = 0x00;
  1154. dev->dev_addr[2] = 0x6c;
  1155. get_random_bytes(&dev->dev_addr[3], 3);
  1156. }
  1157. #endif
  1158. DBG ( "%s: MAC Address %s\n", pci->driver_name, eth_ntoa ( nic->node_addr ) );
  1159. /* disable WOL */
  1160. writel(0, base + NvRegWakeUpFlags);
  1161. np->wolenabled = 0;
  1162. if (np->desc_ver == DESC_VER_1) {
  1163. np->tx_flags = NV_TX_LASTPACKET | NV_TX_VALID;
  1164. } else {
  1165. np->tx_flags = NV_TX2_LASTPACKET | NV_TX2_VALID;
  1166. }
  1167. switch (pci->device) {
  1168. case 0x01C3: // nforce
  1169. // DEV_IRQMASK_1|DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
  1170. np->irqmask = NVREG_IRQMASK_WANTED_2 | NVREG_IRQ_TIMER;
  1171. // np->need_linktimer = 1;
  1172. // np->link_timeout = jiffies + LINK_TIMEOUT;
  1173. break;
  1174. case 0x0066:
  1175. /* Fall Through */
  1176. case 0x00D6:
  1177. // DEV_NEED_LASTPACKET1|DEV_IRQMASK_2|DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER
  1178. np->irqmask = NVREG_IRQMASK_WANTED_2;
  1179. np->irqmask |= NVREG_IRQ_TIMER;
  1180. // np->need_linktimer = 1;
  1181. // np->link_timeout = jiffies + LINK_TIMEOUT;
  1182. if (np->desc_ver == DESC_VER_1)
  1183. np->tx_flags |= NV_TX_LASTPACKET1;
  1184. else
  1185. np->tx_flags |= NV_TX2_LASTPACKET1;
  1186. break;
  1187. case 0x0373:
  1188. /* Fall Through */
  1189. case 0x0086:
  1190. /* Fall Through */
  1191. case 0x008c:
  1192. /* Fall Through */
  1193. case 0x00e6:
  1194. /* Fall Through */
  1195. case 0x00df:
  1196. /* Fall Through */
  1197. case 0x0056:
  1198. /* Fall Through */
  1199. case 0x0057:
  1200. /* Fall Through */
  1201. case 0x0037:
  1202. /* Fall Through */
  1203. case 0x0038:
  1204. //DEV_NEED_LASTPACKET1|DEV_IRQMASK_2|DEV_NEED_TIMERIRQ
  1205. np->irqmask = NVREG_IRQMASK_WANTED_2;
  1206. np->irqmask |= NVREG_IRQ_TIMER;
  1207. // np->need_linktimer = 1;
  1208. // np->link_timeout = jiffies + LINK_TIMEOUT;
  1209. if (np->desc_ver == DESC_VER_1)
  1210. np->tx_flags |= NV_TX_LASTPACKET1;
  1211. else
  1212. np->tx_flags |= NV_TX2_LASTPACKET1;
  1213. break;
  1214. default:
  1215. printf
  1216. ("Your card was undefined in this driver. Review driver_data in Linux driver and send a patch\n");
  1217. }
  1218. /* find a suitable phy */
  1219. for (i = 1; i < 32; i++) {
  1220. int id1, id2;
  1221. id1 = mii_rw(nic, i, MII_PHYSID1, MII_READ);
  1222. if (id1 < 0 || id1 == 0xffff)
  1223. continue;
  1224. id2 = mii_rw(nic, i, MII_PHYSID2, MII_READ);
  1225. if (id2 < 0 || id2 == 0xffff)
  1226. continue;
  1227. id1 = (id1 & PHYID1_OUI_MASK) << PHYID1_OUI_SHFT;
  1228. id2 = (id2 & PHYID2_OUI_MASK) >> PHYID2_OUI_SHFT;
  1229. dprintf
  1230. (("%s: open: Found PHY %hX:%hX at address %d.\n",
  1231. pci->driver_name, id1, id2, i));
  1232. np->phyaddr = i;
  1233. np->phy_oui = id1 | id2;
  1234. break;
  1235. }
  1236. if (i == 32) {
  1237. /* PHY in isolate mode? No phy attached and user wants to
  1238. * test loopback? Very odd, but can be correct.
  1239. */
  1240. printf
  1241. ("%s: open: Could not find a valid PHY.\n", pci->driver_name);
  1242. }
  1243. if (i != 32) {
  1244. /* reset it */
  1245. phy_init(nic);
  1246. }
  1247. dprintf(("%s: forcedeth.c: subsystem: %hX:%hX bound to %s\n",
  1248. pci->driver_name, pci->vendor, pci->dev_id, pci->driver_name));
  1249. if(!forcedeth_reset(nic)) return 0; // no valid link
  1250. /* point to NIC specific routines */
  1251. nic->nic_op = &forcedeth_operations;
  1252. return 1;
  1253. }
  1254. static struct pci_device_id forcedeth_nics[] = {
  1255. PCI_ROM(0x10de, 0x01C3, "nforce", "nForce NVENET_1 Ethernet Controller", 0),
  1256. PCI_ROM(0x10de, 0x0066, "nforce2", "nForce NVENET_2 Ethernet Controller", 0),
  1257. PCI_ROM(0x10de, 0x00D6, "nforce3", "nForce NVENET_3 Ethernet Controller", 0),
  1258. PCI_ROM(0x10de, 0x0086, "nforce4", "nForce NVENET_4 Ethernet Controller", 0),
  1259. PCI_ROM(0x10de, 0x008c, "nforce5", "nForce NVENET_5 Ethernet Controller", 0),
  1260. PCI_ROM(0x10de, 0x00e6, "nforce6", "nForce NVENET_6 Ethernet Controller", 0),
  1261. PCI_ROM(0x10de, 0x00df, "nforce7", "nForce NVENET_7 Ethernet Controller", 0),
  1262. PCI_ROM(0x10de, 0x0056, "nforce8", "nForce NVENET_8 Ethernet Controller", 0),
  1263. PCI_ROM(0x10de, 0x0057, "nforce9", "nForce NVENET_9 Ethernet Controller", 0),
  1264. PCI_ROM(0x10de, 0x0037, "nforce10", "nForce NVENET_10 Ethernet Controller", 0),
  1265. PCI_ROM(0x10de, 0x0038, "nforce11", "nForce NVENET_11 Ethernet Controller", 0),
  1266. PCI_ROM(0x10de, 0x0373, "nforce15", "nForce NVENET_15 Ethernet Controller", 0),
  1267. PCI_ROM(0x10de, 0x0269, "nforce16", "nForce NVENET_16 Ethernet Controller", 0),
  1268. PCI_ROM(0x10de, 0x0760, "nforce17", "nForce NVENET_17 Ethernet Controller", MAC_ADDR_CORRECT),
  1269. };
  1270. PCI_DRIVER ( forcedeth_driver, forcedeth_nics, PCI_NO_CLASS );
  1271. DRIVER ( "forcedeth", nic_driver, pci_driver, forcedeth_driver,
  1272. forcedeth_probe, forcedeth_disable );
  1273. /*
  1274. * Local variables:
  1275. * c-basic-offset: 8
  1276. * c-indent-level: 8
  1277. * tab-width: 8
  1278. * End:
  1279. */