Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

forcedeth.c 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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 "timer.h"
  54. #include "mii.h"
  55. #define drv_version "v1.2"
  56. #define drv_date "05-14-2005"
  57. //#define TFTM_DEBUG
  58. #ifdef TFTM_DEBUG
  59. #define dprintf(x) printf x
  60. #else
  61. #define dprintf(x)
  62. #endif
  63. #define ETH_DATA_LEN 1500
  64. /* Condensed operations for readability. */
  65. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  66. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  67. static unsigned long BASE;
  68. /* NIC specific static variables go here */
  69. #define PCI_DEVICE_ID_NVIDIA_NVENET_1 0x01c3
  70. #define PCI_DEVICE_ID_NVIDIA_NVENET_2 0x0066
  71. #define PCI_DEVICE_ID_NVIDIA_NVENET_4 0x0086
  72. #define PCI_DEVICE_ID_NVIDIA_NVENET_5 0x008c
  73. #define PCI_DEVICE_ID_NVIDIA_NVENET_3 0x00d6
  74. #define PCI_DEVICE_ID_NVIDIA_NVENET_7 0x00df
  75. #define PCI_DEVICE_ID_NVIDIA_NVENET_6 0x00e6
  76. #define PCI_DEVICE_ID_NVIDIA_NVENET_8 0x0056
  77. #define PCI_DEVICE_ID_NVIDIA_NVENET_9 0x0057
  78. #define PCI_DEVICE_ID_NVIDIA_NVENET_10 0x0037
  79. #define PCI_DEVICE_ID_NVIDIA_NVENET_11 0x0038
  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. /* Big endian: should work, but is untested */
  343. struct ring_desc {
  344. u32 PacketBuffer;
  345. u32 FlagLen;
  346. };
  347. /* Define the TX and RX Descriptor and Buffers */
  348. struct {
  349. struct ring_desc tx_ring[TX_RING];
  350. unsigned char txb[TX_RING * RX_NIC_BUFSIZE];
  351. struct ring_desc rx_ring[RX_RING];
  352. unsigned char rxb[RX_RING * RX_NIC_BUFSIZE];
  353. } forcedeth_bufs __shared;
  354. #define tx_ring forcedeth_bufs.tx_ring
  355. #define rx_ring forcedeth_bufs.rx_ring
  356. #define txb forcedeth_bufs.txb
  357. #define rxb forcedeth_bufs.rxb
  358. /* Private Storage for the NIC */
  359. static struct forcedeth_private {
  360. /* General data:
  361. * Locking: spin_lock(&np->lock); */
  362. int in_shutdown;
  363. u32 linkspeed;
  364. int duplex;
  365. int phyaddr;
  366. int wolenabled;
  367. unsigned int phy_oui;
  368. u16 gigabit;
  369. /* General data: RO fields */
  370. u8 *ring_addr;
  371. u32 orig_mac[2];
  372. u32 irqmask;
  373. u32 desc_ver;
  374. /* rx specific fields.
  375. * Locking: Within irq hander or disable_irq+spin_lock(&np->lock);
  376. */
  377. unsigned int cur_rx, refill_rx;
  378. /*
  379. * tx specific fields.
  380. */
  381. unsigned int next_tx, nic_tx;
  382. u32 tx_flags;
  383. } npx;
  384. static struct forcedeth_private *np;
  385. static inline void pci_push(u8 * base)
  386. {
  387. /* force out pending posted writes */
  388. readl(base);
  389. }
  390. static inline u32 nv_descr_getlength(struct ring_desc *prd, u32 v)
  391. {
  392. return le32_to_cpu(prd->FlagLen)
  393. & ((v == DESC_VER_1) ? LEN_MASK_V1 : LEN_MASK_V2);
  394. }
  395. static int reg_delay(int offset, u32 mask,
  396. u32 target, int delay, int delaymax, const char *msg)
  397. {
  398. u8 *base = (u8 *) BASE;
  399. pci_push(base);
  400. do {
  401. udelay(delay);
  402. delaymax -= delay;
  403. if (delaymax < 0) {
  404. if (msg)
  405. printf(msg);
  406. return 1;
  407. }
  408. } while ((readl(base + offset) & mask) != target);
  409. return 0;
  410. }
  411. #define MII_READ (-1)
  412. #define MII_PHYSID1 0x02 /* PHYS ID 1 */
  413. #define MII_PHYSID2 0x03 /* PHYS ID 2 */
  414. #define MII_BMCR 0x00 /* Basic mode control register */
  415. #define MII_BMSR 0x01 /* Basic mode status register */
  416. #define MII_ADVERTISE 0x04 /* Advertisement control reg */
  417. #define MII_LPA 0x05 /* Link partner ability reg */
  418. #define BMSR_ANEGCOMPLETE 0x0020 /* Auto-negotiation complete */
  419. /* Link partner ability register. */
  420. #define LPA_SLCT 0x001f /* Same as advertise selector */
  421. #define LPA_10HALF 0x0020 /* Can do 10mbps half-duplex */
  422. #define LPA_10FULL 0x0040 /* Can do 10mbps full-duplex */
  423. #define LPA_100HALF 0x0080 /* Can do 100mbps half-duplex */
  424. #define LPA_100FULL 0x0100 /* Can do 100mbps full-duplex */
  425. #define LPA_100BASE4 0x0200 /* Can do 100mbps 4k packets */
  426. #define LPA_RESV 0x1c00 /* Unused... */
  427. #define LPA_RFAULT 0x2000 /* Link partner faulted */
  428. #define LPA_LPACK 0x4000 /* Link partner acked us */
  429. #define LPA_NPAGE 0x8000 /* Next page bit */
  430. /* mii_rw: read/write a register on the PHY.
  431. *
  432. * Caller must guarantee serialization
  433. */
  434. static int mii_rw(struct nic *nic __unused, int addr, int miireg,
  435. int value)
  436. {
  437. u8 *base = (u8 *) BASE;
  438. u32 reg;
  439. int retval;
  440. writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
  441. reg = readl(base + NvRegMIIControl);
  442. if (reg & NVREG_MIICTL_INUSE) {
  443. writel(NVREG_MIICTL_INUSE, base + NvRegMIIControl);
  444. udelay(NV_MIIBUSY_DELAY);
  445. }
  446. reg =
  447. (addr << NVREG_MIICTL_ADDRSHIFT) | miireg;
  448. if (value != MII_READ) {
  449. writel(value, base + NvRegMIIData);
  450. reg |= NVREG_MIICTL_WRITE;
  451. }
  452. writel(reg, base + NvRegMIIControl);
  453. if (reg_delay(NvRegMIIControl, NVREG_MIICTL_INUSE, 0,
  454. NV_MIIPHY_DELAY, NV_MIIPHY_DELAYMAX, NULL)) {
  455. dprintf(("mii_rw of reg %d at PHY %d timed out.\n",
  456. miireg, addr));
  457. retval = -1;
  458. } else if (value != MII_READ) {
  459. /* it was a write operation - fewer failures are detectable */
  460. dprintf(("mii_rw wrote 0x%x to reg %d at PHY %d\n",
  461. value, miireg, addr));
  462. retval = 0;
  463. } else if (readl(base + NvRegMIIStatus) & NVREG_MIISTAT_ERROR) {
  464. dprintf(("mii_rw of reg %d at PHY %d failed.\n",
  465. miireg, addr));
  466. retval = -1;
  467. } else {
  468. retval = readl(base + NvRegMIIData);
  469. dprintf(("mii_rw read from reg %d at PHY %d: 0x%x.\n",
  470. miireg, addr, retval));
  471. }
  472. return retval;
  473. }
  474. static int phy_reset(struct nic *nic)
  475. {
  476. u32 miicontrol;
  477. unsigned int tries = 0;
  478. miicontrol = mii_rw(nic, np->phyaddr, MII_BMCR, MII_READ);
  479. miicontrol |= BMCR_RESET;
  480. if (mii_rw(nic, np->phyaddr, MII_BMCR, miicontrol)) {
  481. return -1;
  482. }
  483. /* wait for 500ms */
  484. mdelay(500);
  485. /* must wait till reset is deasserted */
  486. while (miicontrol & BMCR_RESET) {
  487. mdelay(10);
  488. miicontrol = mii_rw(nic, np->phyaddr, MII_BMCR, MII_READ);
  489. /* FIXME: 100 tries seem excessive */
  490. if (tries++ > 100)
  491. return -1;
  492. }
  493. return 0;
  494. }
  495. static int phy_init(struct nic *nic)
  496. {
  497. u8 *base = (u8 *) BASE;
  498. u32 phyinterface, phy_reserved, mii_status, mii_control,
  499. mii_control_1000, reg;
  500. /* set advertise register */
  501. reg = mii_rw(nic, np->phyaddr, MII_ADVERTISE, MII_READ);
  502. reg |=
  503. (ADVERTISE_10HALF | ADVERTISE_10FULL | ADVERTISE_100HALF |
  504. ADVERTISE_100FULL | 0x800 | 0x400);
  505. if (mii_rw(nic, np->phyaddr, MII_ADVERTISE, reg)) {
  506. printf("phy write to advertise failed.\n");
  507. return PHY_ERROR;
  508. }
  509. /* get phy interface type */
  510. phyinterface = readl(base + NvRegPhyInterface);
  511. /* see if gigabit phy */
  512. mii_status = mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  513. if (mii_status & PHY_GIGABIT) {
  514. np->gigabit = PHY_GIGABIT;
  515. mii_control_1000 =
  516. mii_rw(nic, np->phyaddr, MII_1000BT_CR, MII_READ);
  517. mii_control_1000 &= ~ADVERTISE_1000HALF;
  518. if (phyinterface & PHY_RGMII)
  519. mii_control_1000 |= ADVERTISE_1000FULL;
  520. else
  521. mii_control_1000 &= ~ADVERTISE_1000FULL;
  522. if (mii_rw
  523. (nic, np->phyaddr, MII_1000BT_CR, mii_control_1000)) {
  524. printf("phy init failed.\n");
  525. return PHY_ERROR;
  526. }
  527. } else
  528. np->gigabit = 0;
  529. /* reset the phy */
  530. if (phy_reset(nic)) {
  531. printf("phy reset failed\n");
  532. return PHY_ERROR;
  533. }
  534. /* phy vendor specific configuration */
  535. if ((np->phy_oui == PHY_OUI_CICADA) && (phyinterface & PHY_RGMII)) {
  536. phy_reserved =
  537. mii_rw(nic, np->phyaddr, MII_RESV1, MII_READ);
  538. phy_reserved &= ~(PHY_INIT1 | PHY_INIT2);
  539. phy_reserved |= (PHY_INIT3 | PHY_INIT4);
  540. if (mii_rw(nic, np->phyaddr, MII_RESV1, phy_reserved)) {
  541. printf("phy init failed.\n");
  542. return PHY_ERROR;
  543. }
  544. phy_reserved =
  545. mii_rw(nic, np->phyaddr, MII_NCONFIG, MII_READ);
  546. phy_reserved |= PHY_INIT5;
  547. if (mii_rw(nic, np->phyaddr, MII_NCONFIG, phy_reserved)) {
  548. printf("phy init failed.\n");
  549. return PHY_ERROR;
  550. }
  551. }
  552. if (np->phy_oui == PHY_OUI_CICADA) {
  553. phy_reserved =
  554. mii_rw(nic, np->phyaddr, MII_SREVISION, MII_READ);
  555. phy_reserved |= PHY_INIT6;
  556. if (mii_rw(nic, np->phyaddr, MII_SREVISION, phy_reserved)) {
  557. printf("phy init failed.\n");
  558. return PHY_ERROR;
  559. }
  560. }
  561. /* restart auto negotiation */
  562. mii_control = mii_rw(nic, np->phyaddr, MII_BMCR, MII_READ);
  563. mii_control |= (BMCR_ANRESTART | BMCR_ANENABLE);
  564. if (mii_rw(nic, np->phyaddr, MII_BMCR, mii_control)) {
  565. return PHY_ERROR;
  566. }
  567. return 0;
  568. }
  569. static void start_rx(struct nic *nic __unused)
  570. {
  571. u8 *base = (u8 *) BASE;
  572. dprintf(("start_rx\n"));
  573. /* Already running? Stop it. */
  574. if (readl(base + NvRegReceiverControl) & NVREG_RCVCTL_START) {
  575. writel(0, base + NvRegReceiverControl);
  576. pci_push(base);
  577. }
  578. writel(np->linkspeed, base + NvRegLinkSpeed);
  579. pci_push(base);
  580. writel(NVREG_RCVCTL_START, base + NvRegReceiverControl);
  581. pci_push(base);
  582. }
  583. static void stop_rx(void)
  584. {
  585. u8 *base = (u8 *) BASE;
  586. dprintf(("stop_rx\n"));
  587. writel(0, base + NvRegReceiverControl);
  588. reg_delay(NvRegReceiverStatus, NVREG_RCVSTAT_BUSY, 0,
  589. NV_RXSTOP_DELAY1, NV_RXSTOP_DELAY1MAX,
  590. "stop_rx: ReceiverStatus remained busy");
  591. udelay(NV_RXSTOP_DELAY2);
  592. writel(0, base + NvRegLinkSpeed);
  593. }
  594. static void start_tx(struct nic *nic __unused)
  595. {
  596. u8 *base = (u8 *) BASE;
  597. dprintf(("start_tx\n"));
  598. writel(NVREG_XMITCTL_START, base + NvRegTransmitterControl);
  599. pci_push(base);
  600. }
  601. static void stop_tx(void)
  602. {
  603. u8 *base = (u8 *) BASE;
  604. dprintf(("stop_tx\n"));
  605. writel(0, base + NvRegTransmitterControl);
  606. reg_delay(NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0,
  607. NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX,
  608. "stop_tx: TransmitterStatus remained busy");
  609. udelay(NV_TXSTOP_DELAY2);
  610. writel(0, base + NvRegUnknownTransmitterReg);
  611. }
  612. static void txrx_reset(struct nic *nic __unused)
  613. {
  614. u8 *base = (u8 *) BASE;
  615. dprintf(("txrx_reset\n"));
  616. writel(NVREG_TXRXCTL_BIT2 | NVREG_TXRXCTL_RESET | np->desc_ver,
  617. base + NvRegTxRxControl);
  618. pci_push(base);
  619. udelay(NV_TXRX_RESET_DELAY);
  620. writel(NVREG_TXRXCTL_BIT2 | np->desc_ver, base + NvRegTxRxControl);
  621. pci_push(base);
  622. }
  623. /*
  624. * alloc_rx: fill rx ring entries.
  625. * Return 1 if the allocations for the skbs failed and the
  626. * rx engine is without Available descriptors
  627. */
  628. static int alloc_rx(struct nic *nic __unused)
  629. {
  630. unsigned int refill_rx = np->refill_rx;
  631. int i;
  632. //while (np->cur_rx != refill_rx) {
  633. for (i = 0; i < RX_RING; i++) {
  634. //int nr = refill_rx % RX_RING;
  635. rx_ring[i].PacketBuffer =
  636. virt_to_le32desc(&rxb[i * RX_NIC_BUFSIZE]);
  637. wmb();
  638. rx_ring[i].FlagLen =
  639. cpu_to_le32(RX_NIC_BUFSIZE | NV_RX_AVAIL);
  640. /* printf("alloc_rx: Packet %d marked as Available\n",
  641. refill_rx); */
  642. refill_rx++;
  643. }
  644. np->refill_rx = refill_rx;
  645. if (np->cur_rx - refill_rx == RX_RING)
  646. return 1;
  647. return 0;
  648. }
  649. static int update_linkspeed(struct nic *nic)
  650. {
  651. int adv, lpa;
  652. u32 newls;
  653. int newdup = np->duplex;
  654. u32 mii_status;
  655. int retval = 0;
  656. u32 control_1000, status_1000, phyreg;
  657. u8 *base = (u8 *) BASE;
  658. int i;
  659. /* BMSR_LSTATUS is latched, read it twice:
  660. * we want the current value.
  661. */
  662. mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  663. mii_status = mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  664. #if 1
  665. //yhlu
  666. for(i=0;i<30;i++) {
  667. mii_status = mii_rw(nic, np->phyaddr, MII_BMSR, MII_READ);
  668. if((mii_status & BMSR_LSTATUS) && (mii_status & BMSR_ANEGCOMPLETE)) break;
  669. mdelay(100);
  670. }
  671. #endif
  672. if (!(mii_status & BMSR_LSTATUS)) {
  673. printf
  674. ("no link detected by phy - falling back to 10HD.\n");
  675. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  676. newdup = 0;
  677. retval = 0;
  678. goto set_speed;
  679. }
  680. /* check auto negotiation is complete */
  681. if (!(mii_status & BMSR_ANEGCOMPLETE)) {
  682. /* still in autonegotiation - configure nic for 10 MBit HD and wait. */
  683. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  684. newdup = 0;
  685. retval = 0;
  686. printf("autoneg not completed - falling back to 10HD.\n");
  687. goto set_speed;
  688. }
  689. retval = 1;
  690. if (np->gigabit == PHY_GIGABIT) {
  691. control_1000 =
  692. mii_rw(nic, np->phyaddr, MII_1000BT_CR, MII_READ);
  693. status_1000 =
  694. mii_rw(nic, np->phyaddr, MII_1000BT_SR, MII_READ);
  695. if ((control_1000 & ADVERTISE_1000FULL) &&
  696. (status_1000 & LPA_1000FULL)) {
  697. printf
  698. ("update_linkspeed: GBit ethernet detected.\n");
  699. newls =
  700. NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_1000;
  701. newdup = 1;
  702. goto set_speed;
  703. }
  704. }
  705. adv = mii_rw(nic, np->phyaddr, MII_ADVERTISE, MII_READ);
  706. lpa = mii_rw(nic, np->phyaddr, MII_LPA, MII_READ);
  707. dprintf(("update_linkspeed: PHY advertises 0x%hX, lpa 0x%hX.\n",
  708. adv, lpa));
  709. /* FIXME: handle parallel detection properly, handle gigabit ethernet */
  710. lpa = lpa & adv;
  711. if (lpa & LPA_100FULL) {
  712. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_100;
  713. newdup = 1;
  714. } else if (lpa & LPA_100HALF) {
  715. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_100;
  716. newdup = 0;
  717. } else if (lpa & LPA_10FULL) {
  718. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  719. newdup = 1;
  720. } else if (lpa & LPA_10HALF) {
  721. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  722. newdup = 0;
  723. } else {
  724. printf("bad ability %hX - falling back to 10HD.\n", lpa);
  725. newls = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  726. newdup = 0;
  727. }
  728. set_speed:
  729. if (np->duplex == newdup && np->linkspeed == newls)
  730. return retval;
  731. dprintf(("changing link setting from %d/%s to %d/%s.\n",
  732. np->linkspeed, np->duplex ? "Full-Duplex": "Half-Duplex", newls, newdup ? "Full-Duplex": "Half-Duplex"));
  733. np->duplex = newdup;
  734. np->linkspeed = newls;
  735. if (np->gigabit == PHY_GIGABIT) {
  736. phyreg = readl(base + NvRegRandomSeed);
  737. phyreg &= ~(0x3FF00);
  738. if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_10)
  739. phyreg |= NVREG_RNDSEED_FORCE3;
  740. else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100)
  741. phyreg |= NVREG_RNDSEED_FORCE2;
  742. else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
  743. phyreg |= NVREG_RNDSEED_FORCE;
  744. writel(phyreg, base + NvRegRandomSeed);
  745. }
  746. phyreg = readl(base + NvRegPhyInterface);
  747. phyreg &= ~(PHY_HALF | PHY_100 | PHY_1000);
  748. if (np->duplex == 0)
  749. phyreg |= PHY_HALF;
  750. if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_100)
  751. phyreg |= PHY_100;
  752. else if ((np->linkspeed & 0xFFF) == NVREG_LINKSPEED_1000)
  753. phyreg |= PHY_1000;
  754. writel(phyreg, base + NvRegPhyInterface);
  755. writel(NVREG_MISC1_FORCE | (np->duplex ? 0 : NVREG_MISC1_HD),
  756. base + NvRegMisc1);
  757. pci_push(base);
  758. writel(np->linkspeed, base + NvRegLinkSpeed);
  759. pci_push(base);
  760. return retval;
  761. }
  762. #if 0 /* Not used */
  763. static void nv_linkchange(struct nic *nic)
  764. {
  765. if (update_linkspeed(nic)) {
  766. // if (netif_carrier_ok(nic)) {
  767. stop_rx();
  768. //= } else {
  769. // netif_carrier_on(dev);
  770. // printk(KERN_INFO "%s: link up.\n", dev->name);
  771. // }
  772. start_rx(nic);
  773. } else {
  774. // if (netif_carrier_ok(dev)) {
  775. // netif_carrier_off(dev);
  776. // printk(KERN_INFO "%s: link down.\n", dev->name);
  777. stop_rx();
  778. // }
  779. }
  780. }
  781. #endif
  782. static int init_ring(struct nic *nic)
  783. {
  784. int i;
  785. np->next_tx = np->nic_tx = 0;
  786. for (i = 0; i < TX_RING; i++)
  787. tx_ring[i].FlagLen = 0;
  788. np->cur_rx = 0;
  789. np->refill_rx = 0;
  790. for (i = 0; i < RX_RING; i++)
  791. rx_ring[i].FlagLen = 0;
  792. return alloc_rx(nic);
  793. }
  794. static void set_multicast(struct nic *nic)
  795. {
  796. u8 *base = (u8 *) BASE;
  797. u32 addr[2];
  798. u32 mask[2];
  799. u32 pff;
  800. u32 alwaysOff[2];
  801. u32 alwaysOn[2];
  802. memset(addr, 0, sizeof(addr));
  803. memset(mask, 0, sizeof(mask));
  804. pff = NVREG_PFF_MYADDR;
  805. alwaysOn[0] = alwaysOn[1] = alwaysOff[0] = alwaysOff[1] = 0;
  806. addr[0] = alwaysOn[0];
  807. addr[1] = alwaysOn[1];
  808. mask[0] = alwaysOn[0] | alwaysOff[0];
  809. mask[1] = alwaysOn[1] | alwaysOff[1];
  810. addr[0] |= NVREG_MCASTADDRA_FORCE;
  811. pff |= NVREG_PFF_ALWAYS;
  812. stop_rx();
  813. writel(addr[0], base + NvRegMulticastAddrA);
  814. writel(addr[1], base + NvRegMulticastAddrB);
  815. writel(mask[0], base + NvRegMulticastMaskA);
  816. writel(mask[1], base + NvRegMulticastMaskB);
  817. writel(pff, base + NvRegPacketFilterFlags);
  818. start_rx(nic);
  819. }
  820. /**************************************************************************
  821. RESET - Reset the NIC to prepare for use
  822. ***************************************************************************/
  823. static int forcedeth_reset(struct nic *nic)
  824. {
  825. u8 *base = (u8 *) BASE;
  826. int ret, oom, i;
  827. ret = 0;
  828. dprintf(("forcedeth: open\n"));
  829. /* 1) erase previous misconfiguration */
  830. /* 4.1-1: stop adapter: ignored, 4.3 seems to be overkill */
  831. writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
  832. writel(0, base + NvRegMulticastAddrB);
  833. writel(0, base + NvRegMulticastMaskA);
  834. writel(0, base + NvRegMulticastMaskB);
  835. writel(0, base + NvRegPacketFilterFlags);
  836. writel(0, base + NvRegTransmitterControl);
  837. writel(0, base + NvRegReceiverControl);
  838. writel(0, base + NvRegAdapterControl);
  839. /* 2) initialize descriptor rings */
  840. oom = init_ring(nic);
  841. writel(0, base + NvRegLinkSpeed);
  842. writel(0, base + NvRegUnknownTransmitterReg);
  843. txrx_reset(nic);
  844. writel(0, base + NvRegUnknownSetupReg6);
  845. np->in_shutdown = 0;
  846. /* 3) set mac address */
  847. {
  848. u32 mac[2];
  849. mac[0] =
  850. (nic->node_addr[0] << 0) + (nic->node_addr[1] << 8) +
  851. (nic->node_addr[2] << 16) + (nic->node_addr[3] << 24);
  852. mac[1] =
  853. (nic->node_addr[4] << 0) + (nic->node_addr[5] << 8);
  854. writel(mac[0], base + NvRegMacAddrA);
  855. writel(mac[1], base + NvRegMacAddrB);
  856. }
  857. /* 4) give hw rings */
  858. writel((u32) virt_to_le32desc(&rx_ring[0]),
  859. base + NvRegRxRingPhysAddr);
  860. writel((u32) virt_to_le32desc(&tx_ring[0]),
  861. base + NvRegTxRingPhysAddr);
  862. writel(((RX_RING - 1) << NVREG_RINGSZ_RXSHIFT) +
  863. ((TX_RING - 1) << NVREG_RINGSZ_TXSHIFT),
  864. base + NvRegRingSizes);
  865. /* 5) continue setup */
  866. np->linkspeed = NVREG_LINKSPEED_FORCE | NVREG_LINKSPEED_10;
  867. np->duplex = 0;
  868. writel(np->linkspeed, base + NvRegLinkSpeed);
  869. writel(NVREG_UNKSETUP3_VAL1, base + NvRegUnknownSetupReg3);
  870. writel(np->desc_ver, base + NvRegTxRxControl);
  871. pci_push(base);
  872. writel(NVREG_TXRXCTL_BIT1 | np->desc_ver, base + NvRegTxRxControl);
  873. reg_delay(NvRegUnknownSetupReg5, NVREG_UNKSETUP5_BIT31,
  874. NVREG_UNKSETUP5_BIT31, NV_SETUP5_DELAY,
  875. NV_SETUP5_DELAYMAX,
  876. "open: SetupReg5, Bit 31 remained off\n");
  877. writel(0, base + NvRegUnknownSetupReg4);
  878. // writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
  879. writel(NVREG_MIISTAT_MASK2, base + NvRegMIIStatus);
  880. #if 0
  881. printf("%d-Mbs Link, %s-Duplex\n",
  882. np->linkspeed & NVREG_LINKSPEED_10 ? 10 : 100,
  883. np->duplex ? "Full" : "Half");
  884. #endif
  885. /* 6) continue setup */
  886. writel(NVREG_MISC1_FORCE | NVREG_MISC1_HD, base + NvRegMisc1);
  887. writel(readl(base + NvRegTransmitterStatus),
  888. base + NvRegTransmitterStatus);
  889. writel(NVREG_PFF_ALWAYS, base + NvRegPacketFilterFlags);
  890. writel(NVREG_OFFLOAD_NORMAL, base + NvRegOffloadConfig);
  891. writel(readl(base + NvRegReceiverStatus),
  892. base + NvRegReceiverStatus);
  893. /* Get a random number */
  894. i = random();
  895. writel(NVREG_RNDSEED_FORCE | (i & NVREG_RNDSEED_MASK),
  896. base + NvRegRandomSeed);
  897. writel(NVREG_UNKSETUP1_VAL, base + NvRegUnknownSetupReg1);
  898. writel(NVREG_UNKSETUP2_VAL, base + NvRegUnknownSetupReg2);
  899. writel(NVREG_POLL_DEFAULT, base + NvRegPollingInterval);
  900. writel(NVREG_UNKSETUP6_VAL, base + NvRegUnknownSetupReg6);
  901. writel((np->
  902. phyaddr << NVREG_ADAPTCTL_PHYSHIFT) |
  903. NVREG_ADAPTCTL_PHYVALID | NVREG_ADAPTCTL_RUNNING,
  904. base + NvRegAdapterControl);
  905. writel(NVREG_MIISPEED_BIT8 | NVREG_MIIDELAY, base + NvRegMIISpeed);
  906. writel(NVREG_UNKSETUP4_VAL, base + NvRegUnknownSetupReg4);
  907. writel(NVREG_WAKEUPFLAGS_VAL, base + NvRegWakeUpFlags);
  908. i = readl(base + NvRegPowerState);
  909. if ((i & NVREG_POWERSTATE_POWEREDUP) == 0)
  910. writel(NVREG_POWERSTATE_POWEREDUP | i,
  911. base + NvRegPowerState);
  912. pci_push(base);
  913. udelay(10);
  914. writel(readl(base + NvRegPowerState) | NVREG_POWERSTATE_VALID,
  915. base + NvRegPowerState);
  916. writel(0, base + NvRegIrqMask);
  917. pci_push(base);
  918. writel(NVREG_MIISTAT_MASK2, base + NvRegMIIStatus);
  919. writel(NVREG_IRQSTAT_MASK, base + NvRegIrqStatus);
  920. pci_push(base);
  921. /*
  922. writel(np->irqmask, base + NvRegIrqMask);
  923. */
  924. writel(NVREG_MCASTADDRA_FORCE, base + NvRegMulticastAddrA);
  925. writel(0, base + NvRegMulticastAddrB);
  926. writel(0, base + NvRegMulticastMaskA);
  927. writel(0, base + NvRegMulticastMaskB);
  928. writel(NVREG_PFF_ALWAYS | NVREG_PFF_MYADDR,
  929. base + NvRegPacketFilterFlags);
  930. set_multicast(nic);
  931. /* One manual link speed update: Interrupts are enabled, future link
  932. * speed changes cause interrupts and are handled by nv_link_irq().
  933. */
  934. {
  935. u32 miistat;
  936. miistat = readl(base + NvRegMIIStatus);
  937. writel(NVREG_MIISTAT_MASK, base + NvRegMIIStatus);
  938. dprintf(("startup: got 0x%hX.\n", miistat));
  939. }
  940. ret = update_linkspeed(nic);
  941. //start_rx(nic);
  942. start_tx(nic);
  943. if (ret) {
  944. //Start Connection netif_carrier_on(dev);
  945. } else {
  946. printf("no link during initialization.\n");
  947. }
  948. return ret;
  949. }
  950. /*
  951. * extern void hex_dump(const char *data, const unsigned int len);
  952. */
  953. /**************************************************************************
  954. POLL - Wait for a frame
  955. ***************************************************************************/
  956. static int forcedeth_poll(struct nic *nic, int retrieve)
  957. {
  958. /* return true if there's an ethernet packet ready to read */
  959. /* nic->packet should contain data on return */
  960. /* nic->packetlen should contain length of data */
  961. int len;
  962. int i;
  963. u32 Flags;
  964. i = np->cur_rx % RX_RING;
  965. Flags = le32_to_cpu(rx_ring[i].FlagLen);
  966. len = nv_descr_getlength(&rx_ring[i], np->desc_ver);
  967. if (Flags & NV_RX_AVAIL)
  968. return 0; /* still owned by hardware, */
  969. if (np->desc_ver == DESC_VER_1) {
  970. if (!(Flags & NV_RX_DESCRIPTORVALID))
  971. return 0;
  972. } else {
  973. if (!(Flags & NV_RX2_DESCRIPTORVALID))
  974. return 0;
  975. }
  976. if (!retrieve)
  977. return 1;
  978. /* got a valid packet - forward it to the network core */
  979. nic->packetlen = len;
  980. memcpy(nic->packet, rxb + (i * RX_NIC_BUFSIZE), nic->packetlen);
  981. /*
  982. * hex_dump(rxb + (i * RX_NIC_BUFSIZE), len);
  983. */
  984. wmb();
  985. np->cur_rx++;
  986. alloc_rx(nic);
  987. return 1;
  988. }
  989. /**************************************************************************
  990. TRANSMIT - Transmit a frame
  991. ***************************************************************************/
  992. static void forcedeth_transmit(struct nic *nic, const char *d, /* Destination */
  993. unsigned int t, /* Type */
  994. unsigned int s, /* size */
  995. const char *p)
  996. { /* Packet */
  997. /* send the packet to destination */
  998. u8 *ptxb;
  999. u16 nstype;
  1000. u8 *base = (u8 *) BASE;
  1001. int nr = np->next_tx % TX_RING;
  1002. /* point to the current txb incase multiple tx_rings are used */
  1003. ptxb = txb + (nr * RX_NIC_BUFSIZE);
  1004. //np->tx_skbuff[nr] = ptxb;
  1005. /* copy the packet to ring buffer */
  1006. memcpy(ptxb, d, ETH_ALEN); /* dst */
  1007. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN); /* src */
  1008. nstype = htons((u16) t); /* type */
  1009. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2); /* type */
  1010. memcpy(ptxb + ETH_HLEN, p, s);
  1011. s += ETH_HLEN;
  1012. while (s < ETH_ZLEN) /* pad to min length */
  1013. ptxb[s++] = '\0';
  1014. tx_ring[nr].PacketBuffer = (u32) virt_to_le32desc(ptxb);
  1015. wmb();
  1016. tx_ring[nr].FlagLen = cpu_to_le32((s - 1) | np->tx_flags);
  1017. writel(NVREG_TXRXCTL_KICK | np->desc_ver, base + NvRegTxRxControl);
  1018. pci_push(base);
  1019. np->next_tx++;
  1020. }
  1021. /**************************************************************************
  1022. DISABLE - Turn off ethernet interface
  1023. ***************************************************************************/
  1024. static void forcedeth_disable ( struct nic *nic __unused ) {
  1025. /* put the card in its initial state */
  1026. /* This function serves 3 purposes.
  1027. * This disables DMA and interrupts so we don't receive
  1028. * unexpected packets or interrupts from the card after
  1029. * etherboot has finished.
  1030. * This frees resources so etherboot may use
  1031. * this driver on another interface
  1032. * This allows etherboot to reinitialize the interface
  1033. * if something is something goes wrong.
  1034. */
  1035. u8 *base = (u8 *) BASE;
  1036. np->in_shutdown = 1;
  1037. stop_tx();
  1038. stop_rx();
  1039. /* disable interrupts on the nic or we will lock up */
  1040. writel(0, base + NvRegIrqMask);
  1041. pci_push(base);
  1042. dprintf(("Irqmask is zero again\n"));
  1043. /* specia op:o write back the misordered MAC address - otherwise
  1044. * the next probe_nic would see a wrong address.
  1045. */
  1046. writel(np->orig_mac[0], base + NvRegMacAddrA);
  1047. writel(np->orig_mac[1], base + NvRegMacAddrB);
  1048. }
  1049. /**************************************************************************
  1050. IRQ - Enable, Disable, or Force interrupts
  1051. ***************************************************************************/
  1052. static void forcedeth_irq(struct nic *nic __unused,
  1053. irq_action_t action __unused)
  1054. {
  1055. switch (action) {
  1056. case DISABLE:
  1057. break;
  1058. case ENABLE:
  1059. break;
  1060. case FORCE:
  1061. break;
  1062. }
  1063. }
  1064. static struct nic_operations forcedeth_operations = {
  1065. .connect = dummy_connect,
  1066. .poll = forcedeth_poll,
  1067. .transmit = forcedeth_transmit,
  1068. .irq = forcedeth_irq,
  1069. };
  1070. /**************************************************************************
  1071. PROBE - Look for an adapter, this routine's visible to the outside
  1072. ***************************************************************************/
  1073. #define IORESOURCE_MEM 0x00000200
  1074. #define board_found 1
  1075. #define valid_link 0
  1076. static int forcedeth_probe ( struct nic *nic, struct pci_device *pci ) {
  1077. unsigned long addr;
  1078. int sz;
  1079. u8 *base;
  1080. int i;
  1081. if (pci->ioaddr == 0)
  1082. return 0;
  1083. printf("forcedeth.c: Found %s, vendor=0x%hX, device=0x%hX\n",
  1084. pci->name, pci->vendor, pci->device);
  1085. pci_fill_nic ( nic, pci );
  1086. /* point to private storage */
  1087. np = &npx;
  1088. adjust_pci_device(pci);
  1089. addr = pci_bar_start(pci, PCI_BASE_ADDRESS_0);
  1090. sz = pci_bar_size(pci, PCI_BASE_ADDRESS_0);
  1091. /* BASE is used throughout to address the card */
  1092. BASE = (unsigned long) ioremap(addr, sz);
  1093. if (!BASE)
  1094. return 0;
  1095. /* handle different descriptor versions */
  1096. if (pci->device == PCI_DEVICE_ID_NVIDIA_NVENET_1 ||
  1097. pci->device == PCI_DEVICE_ID_NVIDIA_NVENET_2 ||
  1098. pci->device == PCI_DEVICE_ID_NVIDIA_NVENET_3)
  1099. np->desc_ver = DESC_VER_1;
  1100. else
  1101. np->desc_ver = DESC_VER_2;
  1102. //rx_ring[0] = rx_ring;
  1103. //tx_ring[0] = tx_ring;
  1104. /* read the mac address */
  1105. base = (u8 *) BASE;
  1106. np->orig_mac[0] = readl(base + NvRegMacAddrA);
  1107. np->orig_mac[1] = readl(base + NvRegMacAddrB);
  1108. nic->node_addr[0] = (np->orig_mac[1] >> 8) & 0xff;
  1109. nic->node_addr[1] = (np->orig_mac[1] >> 0) & 0xff;
  1110. nic->node_addr[2] = (np->orig_mac[0] >> 24) & 0xff;
  1111. nic->node_addr[3] = (np->orig_mac[0] >> 16) & 0xff;
  1112. nic->node_addr[4] = (np->orig_mac[0] >> 8) & 0xff;
  1113. nic->node_addr[5] = (np->orig_mac[0] >> 0) & 0xff;
  1114. #ifdef LINUX
  1115. if (!is_valid_ether_addr(dev->dev_addr)) {
  1116. /*
  1117. * Bad mac address. At least one bios sets the mac address
  1118. * to 01:23:45:67:89:ab
  1119. */
  1120. printk(KERN_ERR
  1121. "%s: Invalid Mac address detected: %02x:%02x:%02x:%02x:%02x:%02x\n",
  1122. pci_name(pci_dev), dev->dev_addr[0],
  1123. dev->dev_addr[1], dev->dev_addr[2],
  1124. dev->dev_addr[3], dev->dev_addr[4],
  1125. dev->dev_addr[5]);
  1126. printk(KERN_ERR
  1127. "Please complain to your hardware vendor. Switching to a random MAC.\n");
  1128. dev->dev_addr[0] = 0x00;
  1129. dev->dev_addr[1] = 0x00;
  1130. dev->dev_addr[2] = 0x6c;
  1131. get_random_bytes(&dev->dev_addr[3], 3);
  1132. }
  1133. #endif
  1134. DBG ( "%s: MAC Address %s\n", pci->name, eth_ntoa ( nic->node_addr ) );
  1135. /* disable WOL */
  1136. writel(0, base + NvRegWakeUpFlags);
  1137. np->wolenabled = 0;
  1138. if (np->desc_ver == DESC_VER_1) {
  1139. np->tx_flags = NV_TX_LASTPACKET | NV_TX_VALID;
  1140. } else {
  1141. np->tx_flags = NV_TX2_LASTPACKET | NV_TX2_VALID;
  1142. }
  1143. switch (pci->device) {
  1144. case 0x01C3: // nforce
  1145. // DEV_IRQMASK_1|DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER,
  1146. np->irqmask = NVREG_IRQMASK_WANTED_2 | NVREG_IRQ_TIMER;
  1147. // np->need_linktimer = 1;
  1148. // np->link_timeout = jiffies + LINK_TIMEOUT;
  1149. break;
  1150. case 0x0066:
  1151. /* Fall Through */
  1152. case 0x00D6:
  1153. // DEV_NEED_LASTPACKET1|DEV_IRQMASK_2|DEV_NEED_TIMERIRQ|DEV_NEED_LINKTIMER
  1154. np->irqmask = NVREG_IRQMASK_WANTED_2;
  1155. np->irqmask |= NVREG_IRQ_TIMER;
  1156. // np->need_linktimer = 1;
  1157. // np->link_timeout = jiffies + LINK_TIMEOUT;
  1158. if (np->desc_ver == DESC_VER_1)
  1159. np->tx_flags |= NV_TX_LASTPACKET1;
  1160. else
  1161. np->tx_flags |= NV_TX2_LASTPACKET1;
  1162. break;
  1163. case 0x0086:
  1164. /* Fall Through */
  1165. case 0x008c:
  1166. /* Fall Through */
  1167. case 0x00e6:
  1168. /* Fall Through */
  1169. case 0x00df:
  1170. /* Fall Through */
  1171. case 0x0056:
  1172. /* Fall Through */
  1173. case 0x0057:
  1174. /* Fall Through */
  1175. case 0x0037:
  1176. /* Fall Through */
  1177. case 0x0038:
  1178. //DEV_NEED_LASTPACKET1|DEV_IRQMASK_2|DEV_NEED_TIMERIRQ
  1179. np->irqmask = NVREG_IRQMASK_WANTED_2;
  1180. np->irqmask |= NVREG_IRQ_TIMER;
  1181. // np->need_linktimer = 1;
  1182. // np->link_timeout = jiffies + LINK_TIMEOUT;
  1183. if (np->desc_ver == DESC_VER_1)
  1184. np->tx_flags |= NV_TX_LASTPACKET1;
  1185. else
  1186. np->tx_flags |= NV_TX2_LASTPACKET1;
  1187. break;
  1188. default:
  1189. printf
  1190. ("Your card was undefined in this driver. Review driver_data in Linux driver and send a patch\n");
  1191. }
  1192. /* find a suitable phy */
  1193. for (i = 1; i < 32; i++) {
  1194. int id1, id2;
  1195. id1 = mii_rw(nic, i, MII_PHYSID1, MII_READ);
  1196. if (id1 < 0 || id1 == 0xffff)
  1197. continue;
  1198. id2 = mii_rw(nic, i, MII_PHYSID2, MII_READ);
  1199. if (id2 < 0 || id2 == 0xffff)
  1200. continue;
  1201. id1 = (id1 & PHYID1_OUI_MASK) << PHYID1_OUI_SHFT;
  1202. id2 = (id2 & PHYID2_OUI_MASK) >> PHYID2_OUI_SHFT;
  1203. dprintf
  1204. (("%s: open: Found PHY %hX:%hX at address %d.\n",
  1205. pci->name, id1, id2, i));
  1206. np->phyaddr = i;
  1207. np->phy_oui = id1 | id2;
  1208. break;
  1209. }
  1210. if (i == 32) {
  1211. /* PHY in isolate mode? No phy attached and user wants to
  1212. * test loopback? Very odd, but can be correct.
  1213. */
  1214. printf
  1215. ("%s: open: Could not find a valid PHY.\n", pci->name);
  1216. }
  1217. if (i != 32) {
  1218. /* reset it */
  1219. phy_init(nic);
  1220. }
  1221. dprintf(("%s: forcedeth.c: subsystem: %hX:%hX bound to %s\n",
  1222. pci->name, pci->vendor, pci->dev_id, pci->name));
  1223. if(!forcedeth_reset(nic)) return 0; // no valid link
  1224. /* point to NIC specific routines */
  1225. nic->nic_op = &forcedeth_operations;
  1226. return 1;
  1227. }
  1228. static struct pci_device_id forcedeth_nics[] = {
  1229. PCI_ROM(0x10de, 0x01C3, "nforce", "nForce NVENET_1 Ethernet Controller"),
  1230. PCI_ROM(0x10de, 0x0066, "nforce2", "nForce NVENET_2 Ethernet Controller"),
  1231. PCI_ROM(0x10de, 0x00D6, "nforce3", "nForce NVENET_3 Ethernet Controller"),
  1232. PCI_ROM(0x10de, 0x0086, "nforce4", "nForce NVENET_4 Ethernet Controller"),
  1233. PCI_ROM(0x10de, 0x008c, "nforce5", "nForce NVENET_5 Ethernet Controller"),
  1234. PCI_ROM(0x10de, 0x00e6, "nforce6", "nForce NVENET_6 Ethernet Controller"),
  1235. PCI_ROM(0x10de, 0x00df, "nforce7", "nForce NVENET_7 Ethernet Controller"),
  1236. PCI_ROM(0x10de, 0x0056, "nforce8", "nForce NVENET_8 Ethernet Controller"),
  1237. PCI_ROM(0x10de, 0x0057, "nforce9", "nForce NVENET_9 Ethernet Controller"),
  1238. PCI_ROM(0x10de, 0x0037, "nforce10", "nForce NVENET_10 Ethernet Controller"),
  1239. PCI_ROM(0x10de, 0x0038, "nforce11", "nForce NVENET_11 Ethernet Controller"),
  1240. };
  1241. PCI_DRIVER ( forcedeth_driver, forcedeth_nics, PCI_NO_CLASS );
  1242. DRIVER ( "forcedeth", nic_driver, pci_driver, forcedeth_driver,
  1243. forcedeth_probe, forcedeth_disable );