ethervgbe.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. /*
  2. * VIA Velocity gigabit ethernet.
  3. * Register info has been stolen from FreeBSD driver.
  4. *
  5. * Has been tested on:
  6. * - VIA8237 (ABIT AV8): 100Mpbs Full duplex only.
  7. * It works enough to run replica/pull, vncv, ...
  8. *
  9. * To do:
  10. * - 64/48 bits
  11. * - autonegotiation
  12. * - thresholds
  13. * - dynamic ring sizing ??
  14. * - link status change
  15. * - shutdown
  16. * - promiscuous
  17. * - report error
  18. * - Rx/Tx Csum
  19. * - Jumbo frames
  20. *
  21. * Philippe Anel, xigh@free.fr
  22. */
  23. #include "u.h"
  24. #include "../port/lib.h"
  25. #include "mem.h"
  26. #include "dat.h"
  27. #include "fns.h"
  28. #include "io.h"
  29. #include "../port/error.h"
  30. #include "../port/netif.h"
  31. #include "etherif.h"
  32. #include "ethermii.h"
  33. #define DEBUG
  34. enum
  35. {
  36. DumpIntr = (1<<0),
  37. DumpRx = (1<<1),
  38. DumpTx = (1<<2),
  39. };
  40. #define htole16(x) (x)
  41. #define htole32(x) (x)
  42. #define le32toh(x) (x)
  43. enum
  44. {
  45. Timeout = 50000,
  46. RxCount = 256,
  47. TxCount = 256,
  48. RxSize = 2048,
  49. EthAddr = 0x00,
  50. /* Command registers. */
  51. Cr0S = 0x08, /* Global command 0 (Set) */
  52. Cr0C = 0x0c, /* Global command 0 (Clear) */
  53. Cr0_Start = 0x01, /* - start MAC */
  54. Cr0_Stop = 0x02, /* - stop MAC */
  55. Cr0_EnableRx = 0x04, /* - turn on Rx engine */
  56. Cr0_EnableTx = 0x08, /* - turn on Tx engine */
  57. Cr1S = 0x09, /* Global command 1 (Set) */
  58. Cr1C = 0x0d, /* Global command 1 (Clear) */
  59. Cr1_NoPool = 0x08, /* - disable Rx/Tx desc pool */
  60. Cr1_reset = 0x80, /* - software reset */
  61. Cr2S = 0x0a, /* Global command 2 (Set) */
  62. Cr2_XonEnable = 0x80, /* - 802.3x XON/XOFF flow control */
  63. Cr3S = 0x0b, /* Global command 3 (Set) */
  64. Cr3C = 0x0f, /* Global command 3 (Set) */
  65. Cr3_IntMask = 0x02, /* - Mask all interrupts */
  66. /* Eeprom registers. */
  67. Eecsr = 0x93, /* EEPROM control/status */
  68. Eecsr_Autold = 0x20, /* - trigger reload from EEPROM */
  69. /* Mii registers. */
  70. MiiStatus = 0x6D, /* MII port status */
  71. MiiStatus_idle = 0x80, /* - idle */
  72. MiiCmd = 0x70, /* MII command */
  73. MiiCmd_write = 0x20, /* - write */
  74. MiiCmd_read = 0x40, /* - read */
  75. MiiCmd_auto = 0x80, /* - enable autopolling */
  76. MiiAddr = 0x71, /* MII address */
  77. MiiData = 0x72, /* MII data */
  78. /* 64 bits related registers. */
  79. TxDescHi = 0x18,
  80. DataBufHi = 0x1d,
  81. /* Rx engine registers. */
  82. RxDescLo = 0x38, /* Rx descriptor base address (lo 32 bits) */
  83. RxCsrS = 0x32, /* Rx descriptor queue control/status (Set) */
  84. RxCsrC = 0x36, /* Rx descriptor queue control/status (Clear) */
  85. RxCsr_RunQueue = 0x01, /* - enable queue */
  86. RxCsr_Active = 0x02, /* - queue active indicator */
  87. RxCsr_Wakeup = 0x04, /* - wake up queue */
  88. RxCsr_Dead = 0x08, /* - queue dead indicator */
  89. RxNum = 0x50, /* Size of Rx desc ring */
  90. RxDscIdx = 0x3c, /* Current Rx descriptor index */
  91. RxResCnt = 0x5e, /* Rx descriptor residue count */
  92. RxHostErr = 0x23, /* Rx host error status */
  93. RxTimer = 0x3e, /* Rx queue timer pend */
  94. RxControl = 0x06, /* MAC Rx control */
  95. RxControl_BadFrame = 0x01, /* - accept CRC error frames */
  96. RxControl_Runt = 0x02, /* - accept runts */
  97. RxControl_MultiCast = 0x04, /* - accept multicasts */
  98. RxControl_BroadCast = 0x08, /* - accept broadcasts */
  99. RxControl_Promisc = 0x10, /* - promisc mode */
  100. RxControl_Giant = 0x20, /* - accept VLAN tagged frames */
  101. RxControl_UniCast = 0x40, /* - use perfect filtering */
  102. RxControl_SymbolErr = 0x80, /* - accept symbol err packet */
  103. RxConfig = 0x7e, /* MAC Rx config */
  104. RxConfig_VlanFilter = 0x01, /* - filter VLAN ID mismatches */
  105. RxConfig_VlanOpt0 = (0<<1), /* - TX: no tag insert, RX: all, no extr */
  106. RxConfig_VlanOpt1 = (1<<1), /* - TX: no tag insert, RX: tagged pkts, no extr */
  107. RxConfig_VlanOpt2 = (2<<1), /* - TX: tag insert, RX: all, extract tags */
  108. RxConfig_VlanOpt3 = (3<<1), /* - TX: tag insert, RX: tagged pkts, with extr */
  109. RxConfig_FifoLowWat = 0x08, /* - RX FIFO low watermark (7QW/15QW) */
  110. RxConfig_FifoTh128 = (0<<4), /* - RX FIFO threshold 128 bytes */
  111. RxConfig_FifoTh512 = (1<<4), /* - RX FIFO threshold 512 bytes */
  112. RxConfig_FifoTh1024 = (2<<4), /* - RX FIFO threshold 1024 bytes */
  113. RxConfig_FifoThFwd = (3<<4), /* - RX FIFO threshold ??? */
  114. RxConfig_ArbPrio = 0x80, /* - arbitration priority */
  115. /* Tx engine registers. */
  116. TxDescLo = 0x40, /* Tx descriptor base address (lo 32 bits) */
  117. TxCsrS = 0x30, /* Tx descriptor queue control/status (Set) */
  118. TxCsrC = 0x38, /* Tx descriptor queue control/status (Clear) */
  119. TxCsr_RunQueue = 0x01, /* - enable queue */
  120. TxCsr_Active = 0x02, /* - queue active indicator */
  121. TxCsr_Wakeup = 0x04, /* - wake up queue */
  122. TxCsr_Dead = 0x08, /* - queue dead indicator */
  123. TxNum = 0x52, /* Size of Tx desc ring */
  124. TxDscIdx = 0x54, /* Current Tx descriptor index */
  125. TxHostErr = 0x22, /* Tx host error status */
  126. TxTimer = 0x3f, /* Tx queue timer pend */
  127. TxControl = 0x07, /* MAC Rx control */
  128. TxControl_LC_Off = (0<<0), /* - loopback control off */
  129. TxControl_LC_Mac = (1<<0), /* - loopback control MAC internal */
  130. TxControl_LC_Ext = (2<<0), /* - loopback control external */
  131. TxControl_Coll16 = (0<<2), /* - one set of 16 retries */
  132. TxControl_Coll32 = (1<<2), /* - two sets of 16 retries */
  133. TxControl_Coll48 = (2<<2), /* - three sets of 16 retries */
  134. TxControl_CollInf = (3<<2), /* - retry forever */
  135. TxConfig = 0x7f, /* MAC Tx config */
  136. TxConfig_SnapOpt = 0x01, /* - 1 == insert VLAN tag at 13th byte, */
  137. /* 0 == insert VLAN tag after SNAP header (21st byte) */
  138. TxConfig_NonBlk = 0x02, /* - priority TX/non-blocking mode */
  139. TxConfig_Blk64 = (0<<3), /* - non-blocking threshold 64 packets */
  140. TxConfig_Blk32 = (1<<3), /* - non-blocking threshold 32 packets */
  141. TxConfig_Blk128 = (2<<3), /* - non-blocking threshold 128 packets */
  142. TxConfig_Blk8 = (3<<3), /* - non-blocking threshold 8 packets */
  143. TxConfig_ArbPrio = 0x80, /* - arbitration priority */
  144. /* Timer registers. */
  145. Timer0 = 0x74, /* single-shot timer */
  146. Timer1 = 0x76, /* periodic timer */
  147. /* Chip config registers. */
  148. ChipCfgA = 0x78, /* chip config A */
  149. ChipCfgB = 0x79, /* chip config B */
  150. ChipCfgC = 0x7a, /* chip config C */
  151. ChipCfgD = 0x7b, /* chip config D */
  152. /* DMA config registers. */
  153. DmaCfg0 = 0x7C, /* DMA config 0 */
  154. DmaCfg1 = 0x7D, /* DMA config 1 */
  155. /* Interrupt registers. */
  156. IntCtl = 0x20, /* Interrupt control */
  157. Imr = 0x28, /* Interrupt mask */
  158. Isr = 0x24, /* Interrupt status */
  159. Isr_RxHiPrio = (1<<0), /* - hi prio Rx int */
  160. Isr_TxHiPrio = (1<<1), /* - hi prio Tx int */
  161. Isr_RxComplete = (1<<2), /* - Rx queue completed */
  162. Isr_TxComplete = (1<<3), /* - One of Tx queues completed */
  163. Isr_TxComplete0 = (1<<4), /* - Tx queue 0 completed */
  164. Isr_TxComplete1 = (1<<5), /* - Tx queue 1 completed */
  165. Isr_TxComplete2 = (1<<6), /* - Tx queue 2 completed */
  166. Isr_TxComplete3 = (1<<7), /* - Tx queue 3 completed */
  167. Isr_Reserved8 = (1<<8), /* - reserved */
  168. Isr_Reserver9 = (1<<9), /* - reserved */
  169. Isr_RxCountOvflow = (1<<10), /* - Rx packet count overflow */
  170. Isr_RxPause = (1<<11), /* - pause frame Rx */
  171. Isr_RxFifoOvflow = (1<<12), /* - RX FIFO overflow */
  172. Isr_RxNoDesc = (1<<13), /* - ran out of Rx descriptors */
  173. Isr_RxNoDescWar = (1<<14), /* - running out of Rx descriptors */
  174. Isr_LinkStatus = (1<<15), /* - link status change */
  175. Isr_Timer0 = (1<<16), /* - one shot timer expired */
  176. Isr_Timer1 = (1<<17), /* - periodic timer expired */
  177. Isr_Power = (1<<18), /* - wake up power event */
  178. Isr_PhyIntr = (1<<19), /* - PHY interrupt */
  179. Isr_Stopped = (1<<20), /* - software shutdown complete */
  180. Isr_MibOvflow = (1<<21), /* - MIB counter overflow warning */
  181. Isr_SoftIntr = (1<<22), /* - software interrupt */
  182. Isr_HoldOffReload = (1<<23), /* - reload hold timer */
  183. Isr_RxDmaStall = (1<<24), /* - Rx DMA stall */
  184. Isr_TxDmaStall = (1<<25), /* - Tx DMA stall */
  185. Isr_Reserved26 = (1<<26), /* - reserved */
  186. Isr_Reserved27 = (1<<27), /* - reserved */
  187. Isr_Source0 = (1<<28), /* - interrupt source indication */
  188. Isr_Source1 = (1<<29), /* - interrupt source indication */
  189. Isr_Source2 = (1<<30), /* - interrupt source indication */
  190. Isr_Source3 = (1<<31), /* - interrupt source indication */
  191. Isr_Mask = Isr_TxComplete0|Isr_RxComplete|Isr_Stopped|
  192. Isr_RxFifoOvflow|Isr_PhyIntr|Isr_LinkStatus|
  193. Isr_RxNoDesc|Isr_RxDmaStall|Isr_TxDmaStall
  194. };
  195. typedef struct Frag Frag;
  196. struct Frag
  197. {
  198. ulong addr_lo;
  199. ushort addr_hi;
  200. ushort length;
  201. };
  202. typedef struct RxDesc RxDesc;
  203. struct RxDesc
  204. {
  205. ulong status;
  206. ulong control;
  207. Frag;
  208. };
  209. typedef struct TxDesc TxDesc;
  210. struct TxDesc
  211. {
  212. ulong status;
  213. ulong control;
  214. Frag frags[7];
  215. };
  216. enum
  217. {
  218. RxDesc_Status_VidMiss = (1<<0), /* VLAN tag filter miss */
  219. RxDesc_Status_CrcErr = (1<<1), /* bad CRC error */
  220. RxDesc_Status_FrAlErr = (1<<3), /* frame alignment error */
  221. RxDesc_Status_CsumErr = (1<<3), /* bad TCP/IP checksum */
  222. RxDesc_Status_RxLenErr = (1<<4), /* Rx length error */
  223. RxDesc_Status_SymErr = (1<<5), /* PCS symbol error */
  224. RxDesc_Status_SnTag = (1<<6), /* RX'ed tagged SNAP pkt */
  225. RxDesc_Status_DeTag = (1<<7), /* VLAN tag extracted */
  226. RxDesc_Status_OneFrag = (0<<8), /* only one fragment */
  227. RxDesc_Status_FirstFrag = (1<<8), /* first frag in frame */
  228. RxDesc_Status_LastFrag = (2<<8), /* last frag in frame */
  229. RxDesc_Status_MidFrag = (3<<8), /* intermediate frag */
  230. RxDesc_Status_Vtag = (1<<10), /* VLAN tag indicator */
  231. RxDesc_Status_UniCast = (1<<11), /* unicast frame */
  232. RxDesc_Status_BroadCast = (1<<12), /* broadcast frame */
  233. RxDesc_Status_MultiCast = (1<<13), /* multicast frame */
  234. RxDesc_Status_Perfect = (1<<14), /* perfect filter hit */
  235. RxDesc_Status_Goodframe = (1<<15), /* frame is good. */
  236. RxDesc_Status_SizShift = 16, /* received frame len shift */
  237. RxDesc_Status_SizMask = 0x3FFF, /* received frame len mask */
  238. RxDesc_Status_Shutdown = (1<<30), /* shutdown during RX */
  239. RxDesc_Status_Own = (1<<31), /* own bit */
  240. /* ... */
  241. TxDesc_Status_Own = (1<<31), /* own bit */
  242. /* ... */
  243. TxDesc_Control_Intr = (1<<23), /* Tx intr request */
  244. TxDesc_Control_Normal = (3<<24), /* normal frame */
  245. };
  246. typedef struct Stats Stats;
  247. struct Stats
  248. {
  249. ulong rx;
  250. ulong tx;
  251. ulong txe;
  252. ulong intr;
  253. };
  254. typedef struct Ctlr Ctlr;
  255. struct Ctlr
  256. {
  257. Ctlr* link;
  258. Pcidev* pdev;
  259. int port;
  260. int inited;
  261. Lock init_lock;
  262. ulong debugflags;
  263. ulong debugcount;
  264. Mii* mii;
  265. int active;
  266. uchar ea[6];
  267. RxDesc* rx_ring;
  268. Block* rx_blocks[RxCount];
  269. Lock tx_lock;
  270. TxDesc* tx_ring;
  271. Block* tx_blocks[TxCount];
  272. ulong tx_count;
  273. Stats stats;
  274. };
  275. static Ctlr* vgbehead;
  276. static Ctlr* vgbetail;
  277. #define riob(c, r) inb(c->port + r)
  278. #define riow(c, r) ins(c->port + r)
  279. #define riol(c, r) inl(c->port + r)
  280. #define wiob(c, r, d) outb(c->port + r, d)
  281. #define wiow(c, r, d) outs(c->port + r, d)
  282. #define wiol(c, r, d) outl(c->port + r, d)
  283. #define siob(c, r, b) wiob(c, r, riob(c, r) | b)
  284. #define siow(c, r, b) wiow(c, r, riob(c, r) | b)
  285. #define siol(c, r, b) wiol(c, r, riob(c, r) | b)
  286. #define ciob(c, r, b) wiob(c, r, riob(c, r) & ~b)
  287. #define ciow(c, r, b) wiow(c, r, riob(c, r) & ~b)
  288. #define ciol(c, r, b) wiol(c, r, riob(c, r) & ~b)
  289. static int
  290. vgbemiiw(Mii* mii, int phy, int addr, int data)
  291. {
  292. Ctlr* ctlr;
  293. int i;
  294. if(phy != 1)
  295. return -1;
  296. ctlr = mii->ctlr;
  297. wiob(ctlr, MiiAddr, addr);
  298. wiow(ctlr, MiiData, (ushort) data);
  299. wiob(ctlr, MiiCmd, MiiCmd_write);
  300. for(i = 0; i < Timeout; i++)
  301. if((riob(ctlr, MiiCmd) & MiiCmd_write) == 0)
  302. break;
  303. if(i >= Timeout){
  304. print("vgbe: miiw timeout\n");
  305. return -1;
  306. }
  307. return 0;
  308. }
  309. static int
  310. vgbemiir(Mii* mii, int phy, int addr)
  311. {
  312. Ctlr* ctlr;
  313. int i;
  314. if(phy != 1)
  315. return -1;
  316. ctlr = mii->ctlr;
  317. wiob(ctlr, MiiAddr, addr);
  318. wiob(ctlr, MiiCmd, MiiCmd_read);
  319. for(i = 0; i < Timeout; i++)
  320. if((riob(ctlr, MiiCmd) & MiiCmd_read) == 0)
  321. break;
  322. if(i >= Timeout){
  323. print("vgbe: miir timeout\n");
  324. return -1;
  325. }
  326. return riow(ctlr, MiiData);
  327. }
  328. static long
  329. vgbeifstat(Ether* edev, void* a, long n, ulong offset)
  330. {
  331. char* p;
  332. Ctlr* ctlr;
  333. int l;
  334. ctlr = edev->ctlr;
  335. p = malloc(READSTR);
  336. if(p == nil)
  337. error(Enomem);
  338. l = 0;
  339. l += snprint(p+l, READSTR-l, "tx: %uld\n", ctlr->stats.tx);
  340. l += snprint(p+l, READSTR-l, "tx [errs]: %uld\n", ctlr->stats.txe);
  341. l += snprint(p+l, READSTR-l, "rx: %uld\n", ctlr->stats.rx);
  342. l += snprint(p+l, READSTR-l, "intr: %uld\n", ctlr->stats.intr);
  343. snprint(p+l, READSTR-l, "\n");
  344. n = readstr(offset, a, n, p);
  345. free(p);
  346. return n;
  347. }
  348. static char* vgbeisr_info[] = {
  349. "hi prio Rx int",
  350. "hi prio Tx int",
  351. "Rx queue completed",
  352. "One of Tx queues completed",
  353. "Tx queue 0 completed",
  354. "Tx queue 1 completed",
  355. "Tx queue 2 completed",
  356. "Tx queue 3 completed",
  357. "reserved",
  358. "reserved",
  359. "Rx packet count overflow",
  360. "pause frame Rx'ed",
  361. "RX FIFO overflow",
  362. "ran out of Rx descriptors",
  363. "running out of Rx descriptors",
  364. "link status change",
  365. "one shot timer expired",
  366. "periodic timer expired",
  367. "wake up power event",
  368. "PHY interrupt",
  369. "software shutdown complete",
  370. "MIB counter overflow warning",
  371. "software interrupt",
  372. "reload hold timer",
  373. "Rx DMA stall",
  374. "Tx DMA stall",
  375. "reserved",
  376. "reserved",
  377. "interrupt source indication 0",
  378. "interrupt source indication 1",
  379. "interrupt source indication 2",
  380. "interrupt source indication 3",
  381. };
  382. static void
  383. vgbedumpisr(ulong isr)
  384. {
  385. int i;
  386. for(i = 0; i < 32; i++){
  387. ulong mask;
  388. mask = 1<<i;
  389. if(isr & mask)
  390. print("vgbe: irq: - %02d : %c %s\n", i,
  391. Isr_Mask & mask ? '*' : '-', vgbeisr_info[i]);
  392. }
  393. }
  394. static void
  395. noop(Block *)
  396. {
  397. }
  398. static int
  399. vgbenewrx(Ctlr* ctlr, int i)
  400. {
  401. Block* block;
  402. RxDesc* desc;
  403. /*
  404. * allocate a receive Block. we're maintaining
  405. * a private pool of Blocks, so we don't want freeb
  406. * to actually free them, thus we set block->free.
  407. */
  408. block = allocb(RxSize);
  409. block->free = noop;
  410. /* Remember that block. */
  411. ctlr->rx_blocks[i] = block;
  412. /* Initialize Rx descriptor. (TODO: 48/64 bits support ?) */
  413. desc = &ctlr->rx_ring[i];
  414. desc->status = htole32(RxDesc_Status_Own);
  415. desc->control = htole32(0);
  416. desc->addr_lo = htole32((ulong)PCIWADDR(block->rp));
  417. desc->addr_hi = htole16(0);
  418. desc->length = htole16(RxSize | 0x8000);
  419. return 0;
  420. }
  421. static void
  422. vgberxeof(Ether* edev)
  423. {
  424. Ctlr* ctlr;
  425. int i;
  426. Block* block;
  427. ulong length, status;
  428. RxDesc* desc;
  429. ctlr = edev->ctlr;
  430. if(ctlr->debugflags & DumpRx)
  431. print("vgbe: rx_eof\n");
  432. for(i = 0; i < RxCount; i++){
  433. /* Remember that block. */
  434. desc = &ctlr->rx_ring[i];
  435. status = le32toh(desc->status);
  436. if(status & RxDesc_Status_Own)
  437. continue;
  438. if(status & RxDesc_Status_Goodframe){
  439. length = status >> RxDesc_Status_SizShift;
  440. length &= RxDesc_Status_SizMask;
  441. if(ctlr->debugflags & DumpRx)
  442. print("vgbe: Rx-desc[%03d] status=%#08ulx ctl=%#08ulx len=%uld bytes\n",
  443. i, status, desc->control, length);
  444. block = ctlr->rx_blocks[i];
  445. block->wp = block->rp + length;
  446. ctlr->stats.rx++;
  447. etheriq(edev, block, 1);
  448. }
  449. else
  450. print("vgbe: Rx-desc[%#02x] *BAD FRAME* status=%#08ulx ctl=%#08ulx\n",
  451. i, status, desc->control);
  452. /* reset packet ... */
  453. desc->status = htole32(RxDesc_Status_Own);
  454. desc->control = htole32(0);
  455. }
  456. if(ctlr->debugflags & DumpRx)
  457. print("vgbe: rx_eof: done\n");
  458. wiow(ctlr, RxResCnt, RxCount);
  459. wiob(ctlr, RxCsrS, RxCsr_Wakeup);
  460. }
  461. static void
  462. vgbetxeof(Ether* edev)
  463. {
  464. Ctlr* ctlr;
  465. int i, count;
  466. Block* block;
  467. ulong status;
  468. ctlr = edev->ctlr;
  469. ilock(&ctlr->tx_lock);
  470. if(ctlr->debugflags & DumpTx)
  471. print("vgbe: tx_eof\n");
  472. for(count = 0, i = 0; i < TxCount; i++){
  473. block = ctlr->tx_blocks[i];
  474. if(block == nil)
  475. continue;
  476. status = le32toh(ctlr->tx_ring[i].status);
  477. if(status & TxDesc_Status_Own)
  478. continue;
  479. /* Todo add info if it failed */
  480. ctlr->stats.tx++;
  481. if(ctlr->debugflags & DumpTx)
  482. print("vgbe: Block[%03d]:%#p has been sent\n", i, block);
  483. count++;
  484. ctlr->tx_blocks[i] = nil;
  485. freeb(block);
  486. if(ctlr->debugflags & DumpTx)
  487. print("vgbe: Block[%03d]:%#p has been freed\n", i, block);
  488. }
  489. ctlr->tx_count -= count;
  490. if(ctlr->debugflags & DumpTx)
  491. print("vgbe: tx_eof: done [count=%d]\n", count);
  492. iunlock(&ctlr->tx_lock);
  493. if(ctlr->tx_count)
  494. wiob(ctlr, TxCsrS, TxCsr_Wakeup);
  495. }
  496. static void
  497. vgbeinterrupt(Ureg *, void* arg)
  498. {
  499. Ether* edev;
  500. Ctlr* ctlr;
  501. ulong status;
  502. edev = (Ether *) arg;
  503. if(edev == nil)
  504. return;
  505. ctlr = edev->ctlr;
  506. if(ctlr == nil)
  507. return;
  508. /* Mask interrupts. */
  509. wiol(ctlr, Imr, 0);
  510. status = riol(ctlr, Isr);
  511. if(status == 0xffff)
  512. goto end;
  513. /* acknowledge */
  514. if(status)
  515. wiol(ctlr, Isr, status);
  516. if((status & Isr_Mask) == 0)
  517. goto end;
  518. ctlr->stats.intr++;
  519. if(ctlr->debugflags & DumpIntr)
  520. if(ctlr->debugcount){
  521. print("vgbe: irq: status = %#08ulx\n", status);
  522. vgbedumpisr(status);
  523. ctlr->debugcount--;
  524. }
  525. if(status & Isr_RxComplete)
  526. vgberxeof(edev);
  527. if(status & Isr_TxComplete0)
  528. vgbetxeof(edev);
  529. if(status & Isr_Stopped)
  530. print("vgbe: irq: software shutdown complete\n");
  531. if(status & Isr_RxFifoOvflow)
  532. print("vgbe: irq: RX FIFO overflow\n");
  533. if(status & Isr_PhyIntr)
  534. print("vgbe: irq: PHY interrupt\n");
  535. if(status & Isr_LinkStatus)
  536. print("vgbe: irq: link status change\n");
  537. if(status & Isr_RxNoDesc)
  538. print("vgbe: irq: ran out of Rx descriptors\n");
  539. if(status & Isr_RxDmaStall){
  540. print("vgbe: irq: Rx DMA stall\n");
  541. wiol(ctlr, Cr3C, Cr3_IntMask);
  542. return;
  543. }
  544. if(status & Isr_TxDmaStall){
  545. print("vgbe: irq: Tx DMA stall\n");
  546. wiol(ctlr, Cr3C, Cr3_IntMask);
  547. return;
  548. }
  549. end:
  550. /* Unmask interrupts. */
  551. wiol(ctlr, Imr, ~0);
  552. }
  553. static void
  554. vgbetransmit(Ether* edev)
  555. {
  556. Block* block;
  557. Ctlr* ctlr;
  558. int i, index, start, count;
  559. TxDesc* desc;
  560. ulong status, length;
  561. ctlr = edev->ctlr;
  562. ilock(&ctlr->tx_lock);
  563. start = riow(ctlr, TxDscIdx);
  564. if(ctlr->debugflags & DumpTx)
  565. print("vgbe: transmit (start=%d)\n", start);
  566. /* find empty slot */
  567. for(count = 0, i = 0; i < TxCount; i++){
  568. index = (i + start) % TxCount;
  569. if(ctlr->tx_blocks[index])
  570. continue;
  571. desc = &ctlr->tx_ring[index];
  572. status = le32toh(desc->status);
  573. if(status & TxDesc_Status_Own)
  574. continue;
  575. block = qget(edev->oq);
  576. if(block == nil)
  577. break;
  578. count++;
  579. length = BLEN(block);
  580. if(ctlr->debugflags & DumpTx)
  581. print("vgbe: Tx-Desc[%03d] Block:%#p, addr=%#08ulx, len:%ld\n", index, block,
  582. PCIWADDR(block->rp), length);
  583. ctlr->tx_blocks[index] = block;
  584. /* Initialize Tx descriptor. */
  585. desc->status = htole32((length<<16)|TxDesc_Status_Own);
  586. desc->control = htole32(TxDesc_Control_Intr|TxDesc_Control_Normal|((1+1)<<28));
  587. desc->frags[0].addr_lo = htole32((ulong) PCIWADDR(block->rp));
  588. desc->frags[0].addr_hi = htole16(0);
  589. desc->frags[0].length = htole16(length);
  590. }
  591. ctlr->tx_count += count;
  592. if(ctlr->debugflags & DumpTx)
  593. print("vgbe: transmit: done [count=%d]\n", count);
  594. iunlock(&ctlr->tx_lock);
  595. if(ctlr->tx_count)
  596. wiob(ctlr, TxCsrS, TxCsr_Wakeup);
  597. if(count == 0)
  598. print("vgbe: transmit: no Tx entry available\n");
  599. }
  600. static void
  601. vgbeattach(Ether* edev)
  602. {
  603. Ctlr* ctlr;
  604. RxDesc* rxdesc;
  605. TxDesc* txdesc;
  606. int i;
  607. ctlr = edev->ctlr;
  608. lock(&ctlr->init_lock);
  609. if(ctlr->inited){
  610. unlock(&ctlr->init_lock);
  611. return;
  612. }
  613. // print("vgbe: attach\n");
  614. /* Allocate Rx ring. (TODO: Alignment ?) */
  615. rxdesc = mallocalign(RxCount* sizeof(RxDesc), 256, 0, 0);
  616. if(rxdesc == nil){
  617. print("vgbe: unable to alloc Rx ring\n");
  618. unlock(&ctlr->init_lock);
  619. return;
  620. }
  621. ctlr->rx_ring = rxdesc;
  622. /* Allocate Rx blocks, initialize Rx ring. */
  623. for(i = 0; i < RxCount; i++)
  624. vgbenewrx(ctlr, i);
  625. /* Init Rx MAC. */
  626. wiob(ctlr, RxControl,
  627. RxControl_MultiCast|RxControl_BroadCast|RxControl_UniCast);
  628. wiob(ctlr, RxConfig, RxConfig_VlanOpt0);
  629. /* Load Rx ring. */
  630. wiol(ctlr, RxDescLo, (ulong) PCIWADDR(rxdesc));
  631. wiow(ctlr, RxNum, RxCount - 1);
  632. wiow(ctlr, RxDscIdx, 0);
  633. wiow(ctlr, RxResCnt, RxCount);
  634. /* Allocate Tx ring. */
  635. txdesc = mallocalign(TxCount* sizeof(TxDesc), 256, 0, 0);
  636. if(txdesc == nil){
  637. print("vgbe: unable to alloc Tx ring\n");
  638. unlock(&ctlr->init_lock);
  639. return;
  640. }
  641. ctlr->tx_ring = txdesc;
  642. /* Init DMAs */
  643. wiob(ctlr, DmaCfg0, 4);
  644. /* Init Tx MAC. */
  645. wiob(ctlr, TxControl, 0);
  646. wiob(ctlr, TxConfig, TxConfig_NonBlk|TxConfig_ArbPrio);
  647. /* Load Tx ring. */
  648. wiol(ctlr, TxDescLo, (ulong) PCIWADDR(txdesc));
  649. wiow(ctlr, TxNum, TxCount - 1);
  650. wiow(ctlr, TxDscIdx, 0);
  651. /* Enable Xon/Xoff */
  652. wiob(ctlr, Cr2S, 0xb|Cr2_XonEnable);
  653. /* Enable Rx queue */
  654. wiob(ctlr, RxCsrS, RxCsr_RunQueue);
  655. /* Enable Tx queue */
  656. wiob(ctlr, TxCsrS, TxCsr_RunQueue);
  657. /* Done */
  658. ctlr->inited = 1;
  659. unlock(&ctlr->init_lock);
  660. /* Enable interrupts */
  661. wiol(ctlr, Isr, 0xffffffff);
  662. wiob(ctlr, Cr3S, Cr3_IntMask);
  663. /* Wake up Rx queue */
  664. wiob(ctlr, RxCsrS, RxCsr_Wakeup);
  665. }
  666. static void
  667. vgbereset(Ctlr* ctlr)
  668. {
  669. // MiiPhy* phy;
  670. int timeo, i;
  671. // print("vgbe: reset\n");
  672. /* Soft reset the controller. */
  673. wiob(ctlr, Cr1S, Cr1_reset);
  674. for(timeo = 0; timeo < Timeout; timeo++)
  675. if((riob(ctlr, Cr1S) & Cr1_reset) == 0)
  676. break;
  677. if(timeo >= Timeout){
  678. print("vgbe: softreset timeout\n");
  679. return;
  680. }
  681. /* Reload eeprom. */
  682. siob(ctlr, Eecsr, Eecsr_Autold);
  683. for(timeo = 0; timeo < Timeout; timeo++)
  684. if((riob(ctlr, Eecsr) & Eecsr_Autold) == 0)
  685. break;
  686. if(timeo >= Timeout){
  687. print("vgbe: eeprom reload timeout\n");
  688. return;
  689. }
  690. /* Load the MAC address. */
  691. for(i = 0; i < Eaddrlen; i++)
  692. ctlr->ea[i] = riob(ctlr, EthAddr+i);
  693. /* Initialize interrupts. */
  694. wiol(ctlr, Isr, 0xffffffff);
  695. wiol(ctlr, Imr, 0xffffffff);
  696. /* Disable interrupts. */
  697. wiol(ctlr, Cr3C, Cr3_IntMask);
  698. /* 32 bits addresses only. (TODO: 64 bits ?) */
  699. wiol(ctlr, TxDescHi, 0);
  700. wiow(ctlr, DataBufHi, 0);
  701. /* Enable MAC (turning off Rx/Tx engines for the moment). */
  702. wiob(ctlr, Cr0C, Cr0_Stop|Cr0_EnableRx|Cr0_EnableTx);
  703. wiob(ctlr, Cr0S, Cr0_Start);
  704. /* Initialize Rx engine. */
  705. wiow(ctlr, RxCsrC, RxCsr_RunQueue);
  706. /* Initialize Tx engine. */
  707. wiow(ctlr, TxCsrC, TxCsr_RunQueue);
  708. /* Enable Rx/Tx engines. */
  709. wiob(ctlr, Cr0S, Cr0_EnableRx|Cr0_EnableTx);
  710. /* Initialize link management. */
  711. ctlr->mii = malloc(sizeof(Mii));
  712. if(ctlr->mii == nil){
  713. print("vgbe: unable to alloc Mii\n");
  714. return;
  715. }
  716. ctlr->mii->mir = vgbemiir;
  717. ctlr->mii->miw = vgbemiiw;
  718. ctlr->mii->ctlr = ctlr;
  719. if(mii(ctlr->mii, 1<<1) == 0){
  720. print("vgbe: no phy found\n");
  721. return;
  722. }
  723. // phy = ctlr->mii->curphy;
  724. // print("vgbe: phy:oui %#x\n", phy->oui);
  725. }
  726. static void
  727. vgbepci(void)
  728. {
  729. Pcidev* pdev;
  730. // print("vgbe: pci\n");
  731. pdev = nil;
  732. while(pdev = pcimatch(pdev, 0, 0)){
  733. Ctlr* ctlr;
  734. int port, size;
  735. if(pdev->ccrb != 0x02 || pdev->ccru != 0)
  736. continue;
  737. switch((pdev->did<<16) | pdev->vid){
  738. default:
  739. continue;
  740. case (0x3119<<16)|0x1106: /* VIA Velocity (VT6122) */
  741. break;
  742. }
  743. if((pdev->pcr & 1) == 0){
  744. print("vgbe: io not enabled [pcr=%#lux]\n", (ulong)pdev->pcr);
  745. continue;
  746. }
  747. pcisetbme(pdev);
  748. pcisetpms(pdev, 0);
  749. port = pdev->mem[0].bar;
  750. size = pdev->mem[0].size;
  751. if((port & 1) == 0){
  752. print("vgbe: bar[0]=%#x is not io\n", port);
  753. continue;
  754. }
  755. if(port > 0xff00){
  756. print("vgbe: invalid port %#ux\n", port);
  757. continue;
  758. }
  759. port &= 0xfffe;
  760. if(size != 256){
  761. print("vgbe: invalid io size: %d\n", size);
  762. continue;
  763. }
  764. if(ioalloc(port, size, 0, "vge") < 0){
  765. print("vgbe: port %#ux already in use\n", port);
  766. continue;
  767. }
  768. ctlr = malloc(sizeof(Ctlr));
  769. if(ctlr == nil){
  770. print("vgbe: unable to alloc Ctlr\n");
  771. iofree(port);
  772. continue;
  773. }
  774. ctlr->pdev = pdev;
  775. ctlr->port = port;
  776. ctlr->inited = 0;
  777. if(vgbehead != nil)
  778. vgbetail->link = ctlr;
  779. else
  780. vgbehead = ctlr;
  781. vgbetail = ctlr;
  782. }
  783. }
  784. static long
  785. vgbectl(Ether* edev, void* buf, long n)
  786. {
  787. Cmdbuf* cb;
  788. Ctlr* ctlr;
  789. ulong index;
  790. char* rptr;
  791. RxDesc* rd;
  792. TxDesc* td;
  793. uchar* p;
  794. ctlr = edev->ctlr;
  795. cb = parsecmd(buf, n);
  796. if(waserror()){
  797. free(cb);
  798. nexterror();
  799. }
  800. if(cistrcmp(cb->f[0], "reset") == 0){
  801. vgbereset(ctlr);
  802. wiob(ctlr, Cr3S, Cr3_IntMask);
  803. wiob(ctlr, RxCsrS, RxCsr_RunQueue);
  804. wiob(ctlr, RxCsrS, RxCsr_Wakeup);
  805. }
  806. else if(cistrcmp(cb->f[0], "dumpintr") == 0){
  807. if(cb->nf < 2)
  808. error(Ecmdargs);
  809. if(cistrcmp(cb->f[1], "on") == 0){
  810. ctlr->debugflags |= DumpIntr;
  811. ctlr->debugcount = ~0;
  812. }
  813. else if(cistrcmp(cb->f[1], "off") == 0)
  814. ctlr->debugflags &= ~DumpIntr;
  815. else{
  816. ulong count;
  817. char* rptr;
  818. count = strtoul(cb->f[1], &rptr, 0);
  819. if(rptr == cb->f[1])
  820. error("invalid control request");
  821. ctlr->debugflags |= DumpIntr;
  822. ctlr->debugcount = count;
  823. print("vgbe: debugcount set to %uld\n", count);
  824. }
  825. }
  826. else if(cistrcmp(cb->f[0], "dumprx") == 0){
  827. if(cb->nf < 2)
  828. error(Ecmdargs);
  829. if(cistrcmp(cb->f[1], "on") == 0)
  830. ctlr->debugflags |= DumpRx;
  831. else if(cistrcmp(cb->f[1], "off") == 0)
  832. ctlr->debugflags &= ~DumpRx;
  833. else{
  834. index = strtoul(cb->f[1], &rptr, 0);
  835. if((rptr == cb->f[1]) || (index >= RxCount))
  836. error("invalid control request");
  837. rd = &ctlr->rx_ring[index];
  838. print("vgbe: DumpRx[%03uld] status=%#08ulx ctl=%#08ulx len=%#04ux bytes\n",
  839. index, rd->status, rd->control, rd->length);
  840. }
  841. }
  842. else if(cistrcmp(cb->f[0], "dumptx") == 0){
  843. if(cb->nf < 2)
  844. error(Ecmdargs);
  845. if(cistrcmp(cb->f[1], "on") == 0)
  846. ctlr->debugflags |= DumpTx;
  847. else if(cistrcmp(cb->f[1], "off") == 0)
  848. ctlr->debugflags &= ~DumpTx;
  849. else{
  850. index = strtoul(cb->f[1], &rptr, 0);
  851. if((rptr == cb->f[1]) || (index >= TxCount))
  852. error("invalid control request");
  853. td = &ctlr->tx_ring[index];
  854. print("vgbe: DumpTx[%03uld] status=%#08ulx ctl=%#08ulx len=%#04ux bytes",
  855. index, td->status, td->control, td->frags[0].length);
  856. p = (uchar*)td;
  857. for(index = 0; index < sizeof(TxDesc); index++){
  858. if((index % 16) == 0)
  859. print("\nvgbe: ");
  860. else
  861. print(" ");
  862. print("%#02x", p[index]);
  863. }
  864. }
  865. }
  866. else if(cistrcmp(cb->f[0], "dumpall") == 0){
  867. if(cb->nf < 2)
  868. error(Ecmdargs);
  869. if(cistrcmp(cb->f[1], "on") == 0){
  870. ctlr->debugflags = ~0;
  871. ctlr->debugcount = ~0;
  872. }
  873. else if(cistrcmp(cb->f[1], "off") == 0)
  874. ctlr->debugflags = 0;
  875. else error("invalid control request");
  876. }
  877. else
  878. error(Ebadctl);
  879. free(cb);
  880. poperror();
  881. return n;
  882. }
  883. static void
  884. vgbepromiscuous(void* arg, int on)
  885. {
  886. USED(arg, on);
  887. }
  888. /* multicast already on, don't need to do anything */
  889. static void
  890. vgbemulticast(void*, uchar*, int)
  891. {
  892. }
  893. static void
  894. vgbeshutdown(Ether* ether)
  895. {
  896. vgbereset(ether->ctlr);
  897. }
  898. static int
  899. vgbepnp(Ether* edev)
  900. {
  901. Ctlr* ctlr;
  902. // print("vgbe: pnp\n");
  903. if(vgbehead == nil)
  904. vgbepci();
  905. for(ctlr = vgbehead; ctlr != nil; ctlr = ctlr->link){
  906. if(ctlr->active)
  907. continue;
  908. if(edev->port == 0 || edev->port == ctlr->port){
  909. ctlr->active = 1;
  910. break;
  911. }
  912. }
  913. if(ctlr == nil)
  914. return -1;
  915. vgbereset(ctlr);
  916. edev->ctlr = ctlr;
  917. edev->port = ctlr->port;
  918. edev->irq = ctlr->pdev->intl;
  919. edev->tbdf = ctlr->pdev->tbdf;
  920. edev->mbps = 1000;
  921. memmove(edev->ea, ctlr->ea, Eaddrlen);
  922. edev->attach = vgbeattach;
  923. edev->transmit = vgbetransmit;
  924. edev->interrupt = vgbeinterrupt;
  925. edev->ifstat = vgbeifstat;
  926. // edev->promiscuous = vgbepromiscuous;
  927. edev->multicast = vgbemulticast;
  928. edev->shutdown = vgbeshutdown;
  929. edev->ctl = vgbectl;
  930. edev->arg = edev;
  931. return 0;
  932. }
  933. void
  934. ethervgbelink(void)
  935. {
  936. addethercard("vgbe", vgbepnp);
  937. }