ether82563.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*
  2. * Intel Gigabit Ethernet PCI-Express Controllers.
  3. * 8256[36], 8257[1-79], 21[078]
  4. * Pretty basic, does not use many of the chip smarts.
  5. * The interrupt mitigation tuning for each chip variant
  6. * is probably different. The reset/initialisation
  7. * sequence needs straightened out. Doubt the PHY code
  8. * for the 82575eb is right.
  9. *
  10. * on the assumption that allowing jumbo packets makes the controller
  11. * much slower (as is true of the 82579), never allow jumbos.
  12. */
  13. #include "u.h"
  14. #include "../port/lib.h"
  15. #include "mem.h"
  16. #include "dat.h"
  17. #include "fns.h"
  18. #include "io.h"
  19. #include "../port/error.h"
  20. #include "../port/netif.h"
  21. #include "etherif.h"
  22. #define now() TK2MS(MACHP(0)->ticks)
  23. /*
  24. * these are in the order they appear in the manual, not numeric order.
  25. * It was too hard to find them in the book. Ref 21489, rev 2.6
  26. */
  27. enum {
  28. /* General */
  29. Ctrl = 0x0000, /* Device Control */
  30. Status = 0x0008, /* Device Status */
  31. Eec = 0x0010, /* EEPROM/Flash Control/Data */
  32. Fextnvm6 = 0x0010, /* Future Extended NVM 6 */
  33. Eerd = 0x0014, /* EEPROM Read */
  34. Ctrlext = 0x0018, /* Extended Device Control */
  35. Fla = 0x001c, /* Flash Access */
  36. Mdic = 0x0020, /* MDI Control */
  37. Seresctl = 0x0024, /* Serdes ana */
  38. Fcal = 0x0028, /* Flow Control Address Low */
  39. Fcah = 0x002C, /* Flow Control Address High */
  40. Fct = 0x0030, /* Flow Control Type */
  41. Kumctrlsta = 0x0034, /* MAC-PHY Interface */
  42. Vet = 0x0038, /* VLAN EtherType */
  43. Fcttv = 0x0170, /* Flow Control Transmit Timer Value */
  44. Txcw = 0x0178, /* Transmit Configuration Word */
  45. Rxcw = 0x0180, /* Receive Configuration Word */
  46. Ledctl = 0x0E00, /* LED control */
  47. Pba = 0x1000, /* Packet Buffer Allocation */
  48. Pbs = 0x1008, /* Packet Buffer Size */
  49. /* Interrupt */
  50. Icr = 0x00C0, /* Interrupt Cause Read */
  51. Itr = 0x00c4, /* Interrupt Throttling Rate */
  52. Ics = 0x00C8, /* Interrupt Cause Set */
  53. Ims = 0x00D0, /* Interrupt Mask Set/Read */
  54. Imc = 0x00D8, /* Interrupt mask Clear */
  55. Iam = 0x00E0, /* Interrupt acknowledge Auto Mask */
  56. /* Receive */
  57. Rctl = 0x0100, /* Control */
  58. Ert = 0x2008, /* Early Receive Threshold (573[EVL], 579 only) */
  59. Fcrtl = 0x2160, /* Flow Control RX Threshold Low */
  60. Fcrth = 0x2168, /* Flow Control Rx Threshold High */
  61. Psrctl = 0x2170, /* Packet Split Receive Control */
  62. Rdbal = 0x2800, /* Rdesc Base Address Low Queue 0 */
  63. Rdbah = 0x2804, /* Rdesc Base Address High Queue 0 */
  64. Rdlen = 0x2808, /* Descriptor Length Queue 0 */
  65. Rdh = 0x2810, /* Descriptor Head Queue 0 */
  66. Rdt = 0x2818, /* Descriptor Tail Queue 0 */
  67. Rdtr = 0x2820, /* Descriptor Timer Ring */
  68. Rxdctl = 0x2828, /* Descriptor Control */
  69. Radv = 0x282C, /* Interrupt Absolute Delay Timer */
  70. Rdbal1 = 0x2900, /* Rdesc Base Address Low Queue 1 */
  71. Rdbah1 = 0x2804, /* Rdesc Base Address High Queue 1 */
  72. Rdlen1 = 0x2908, /* Descriptor Length Queue 1 */
  73. Rdh1 = 0x2910, /* Descriptor Head Queue 1 */
  74. Rdt1 = 0x2918, /* Descriptor Tail Queue 1 */
  75. Rxdctl1 = 0x2928, /* Descriptor Control Queue 1 */
  76. Rsrpd = 0x2c00, /* Small Packet Detect */
  77. Raid = 0x2c08, /* ACK interrupt delay */
  78. Cpuvec = 0x2c10, /* CPU Vector */
  79. Rxcsum = 0x5000, /* Checksum Control */
  80. Rfctl = 0x5008, /* Filter Control */
  81. Mta = 0x5200, /* Multicast Table Array */
  82. Ral = 0x5400, /* Receive Address Low */
  83. Rah = 0x5404, /* Receive Address High */
  84. Vfta = 0x5600, /* VLAN Filter Table Array */
  85. Mrqc = 0x5818, /* Multiple Receive Queues Command */
  86. Rssim = 0x5864, /* RSS Interrupt Mask */
  87. Rssir = 0x5868, /* RSS Interrupt Request */
  88. Reta = 0x5c00, /* Redirection Table */
  89. Rssrk = 0x5c80, /* RSS Random Key */
  90. /* Transmit */
  91. Tctl = 0x0400, /* Transmit Control */
  92. Tipg = 0x0410, /* Transmit IPG */
  93. Tkabgtxd = 0x3004, /* glci afe band gap transmit ref data, or something */
  94. Tdbal = 0x3800, /* Tdesc Base Address Low */
  95. Tdbah = 0x3804, /* Tdesc Base Address High */
  96. Tdlen = 0x3808, /* Descriptor Length */
  97. Tdh = 0x3810, /* Descriptor Head */
  98. Tdt = 0x3818, /* Descriptor Tail */
  99. Tidv = 0x3820, /* Interrupt Delay Value */
  100. Txdctl = 0x3828, /* Descriptor Control */
  101. Tadv = 0x382C, /* Interrupt Absolute Delay Timer */
  102. Tarc0 = 0x3840, /* Arbitration Counter Queue 0 */
  103. Tdbal1 = 0x3900, /* Descriptor Base Low Queue 1 */
  104. Tdbah1 = 0x3904, /* Descriptor Base High Queue 1 */
  105. Tdlen1 = 0x3908, /* Descriptor Length Queue 1 */
  106. Tdh1 = 0x3910, /* Descriptor Head Queue 1 */
  107. Tdt1 = 0x3918, /* Descriptor Tail Queue 1 */
  108. Txdctl1 = 0x3928, /* Descriptor Control 1 */
  109. Tarc1 = 0x3940, /* Arbitration Counter Queue 1 */
  110. /* Statistics */
  111. Statistics = 0x4000, /* Start of Statistics Area */
  112. Gorcl = 0x88/4, /* Good Octets Received Count */
  113. Gotcl = 0x90/4, /* Good Octets Transmitted Count */
  114. Torl = 0xC0/4, /* Total Octets Received */
  115. Totl = 0xC8/4, /* Total Octets Transmitted */
  116. Nstatistics = 0x124/4,
  117. };
  118. enum { /* Ctrl */
  119. GIOmd = 1<<2, /* BIO master disable */
  120. Lrst = 1<<3, /* link reset */
  121. Slu = 1<<6, /* Set Link Up */
  122. SspeedMASK = 3<<8, /* Speed Selection */
  123. SspeedSHIFT = 8,
  124. Sspeed10 = 0x00000000, /* 10Mb/s */
  125. Sspeed100 = 0x00000100, /* 100Mb/s */
  126. Sspeed1000 = 0x00000200, /* 1000Mb/s */
  127. Frcspd = 1<<11, /* Force Speed */
  128. Frcdplx = 1<<12, /* Force Duplex */
  129. SwdpinsloMASK = 0x003C0000, /* Software Defined Pins - lo nibble */
  130. SwdpinsloSHIFT = 18,
  131. SwdpioloMASK = 0x03C00000, /* Software Defined Pins - I or O */
  132. SwdpioloSHIFT = 22,
  133. Devrst = 1<<26, /* Device Reset */
  134. Rfce = 1<<27, /* Receive Flow Control Enable */
  135. Tfce = 1<<28, /* Transmit Flow Control Enable */
  136. Vme = 1<<30, /* VLAN Mode Enable */
  137. Phyrst = 1<<31, /* Phy Reset */
  138. };
  139. enum { /* Status */
  140. Lu = 1<<1, /* Link Up */
  141. Lanid = 3<<2, /* mask for Lan ID. */
  142. Txoff = 1<<4, /* Transmission Paused */
  143. Tbimode = 1<<5, /* TBI Mode Indication */
  144. Phyra = 1<<10, /* PHY Reset Asserted */
  145. GIOme = 1<<19, /* GIO Master Enable Status */
  146. };
  147. enum { /* Eerd */
  148. EEstart = 1<<0, /* Start Read */
  149. EEdone = 1<<1, /* Read done */
  150. };
  151. enum { /* Ctrlext */
  152. Asdchk = 1<<12, /* ASD Check */
  153. Eerst = 1<<13, /* EEPROM Reset */
  154. Spdbyps = 1<<15, /* Speed Select Bypass */
  155. };
  156. enum { /* EEPROM content offsets */
  157. Ea = 0x00, /* Ethernet Address */
  158. Cf = 0x03, /* Compatibility Field */
  159. Icw1 = 0x0A, /* Initialization Control Word 1 */
  160. Sid = 0x0B, /* Subsystem ID */
  161. Svid = 0x0C, /* Subsystem Vendor ID */
  162. Did = 0x0D, /* Device ID */
  163. Vid = 0x0E, /* Vendor ID */
  164. Icw2 = 0x0F, /* Initialization Control Word 2 */
  165. };
  166. enum { /* Mdic */
  167. MDIdMASK = 0x0000FFFF, /* Data */
  168. MDIdSHIFT = 0,
  169. MDIrMASK = 0x001F0000, /* PHY Register Address */
  170. MDIrSHIFT = 16,
  171. MDIpMASK = 0x03E00000, /* PHY Address */
  172. MDIpSHIFT = 21,
  173. MDIwop = 0x04000000, /* Write Operation */
  174. MDIrop = 0x08000000, /* Read Operation */
  175. MDIready = 0x10000000, /* End of Transaction */
  176. MDIie = 0x20000000, /* Interrupt Enable */
  177. MDIe = 0x40000000, /* Error */
  178. };
  179. enum { /* phy interface registers */
  180. Phyctl = 0, /* phy ctl */
  181. Physsr = 17, /* phy secondary status */
  182. Phyier = 18, /* 82573 phy interrupt enable */
  183. Phyisr = 19, /* 82563 phy interrupt status */
  184. Phylhr = 19, /* 8257[12] link health */
  185. Phyier218 = 24, /* 218 (phy79?) phy interrupt enable */
  186. Phyisr218 = 25, /* 218 (phy79?) phy interrupt status */
  187. Phystat = 26, /* 82580 (phy79?) phy status */
  188. Phypage = 31, /* page number */
  189. Rtlink = 1<<10, /* realtime link status */
  190. Phyan = 1<<11, /* phy has auto-negotiated */
  191. /* Phyctl bits */
  192. Ran = 1<<9, /* restart auto-negotiation */
  193. Ean = 1<<12, /* enable auto-negotiation */
  194. /* 82573 Phyier interrupt enable bits */
  195. Lscie = 1<<10, /* link status changed */
  196. Ancie = 1<<11, /* auto-negotiation complete */
  197. Spdie = 1<<14, /* speed changed */
  198. Panie = 1<<15, /* phy auto-negotiation error */
  199. /* Phylhr/Phyisr bits */
  200. Anf = 1<<6, /* lhr: auto-negotiation fault */
  201. Ane = 1<<15, /* isr: auto-negotiation error */
  202. /* 82580 Phystat bits */
  203. Ans = 3<<14, /* 82580 autoneg. status */
  204. Link = 1<<6, /* 82580 link */
  205. /* 218 Phystat bits */
  206. Anfs = 3<<13, /* fault status */
  207. Ans218 = 1<<12, /* autoneg complete */
  208. /* 218 Phyier218 interrupt enable bits */
  209. Spdie218 = 1<<1, /* speed changed */
  210. Lscie218 = 1<<2, /* link status changed */
  211. Ancie218 = 1<<8, /* auto-negotiation changed */
  212. };
  213. enum { /* Icr, Ics, Ims, Imc */
  214. Txdw = 0x00000001, /* Transmit Descriptor Written Back */
  215. Txqe = 0x00000002, /* Transmit Queue Empty */
  216. Lsc = 0x00000004, /* Link Status Change */
  217. Rxseq = 0x00000008, /* Receive Sequence Error */
  218. Rxdmt0 = 0x00000010, /* Rdesc Minimum Threshold Reached */
  219. Rxo = 0x00000040, /* Receiver Overrun */
  220. Rxt0 = 0x00000080, /* Receiver Timer Interrupt */
  221. Mdac = 0x00000200, /* MDIO Access Completed */
  222. Rxcfg = 0x00000400, /* Receiving /C/ ordered sets */
  223. Gpi0 = 0x00000800, /* General Purpose Interrupts */
  224. Gpi1 = 0x00001000,
  225. Gpi2 = 0x00002000,
  226. Gpi3 = 0x00004000,
  227. Ack = 0x00020000, /* Receive ACK frame */
  228. };
  229. enum { /* Txcw */
  230. TxcwFd = 0x00000020, /* Full Duplex */
  231. TxcwHd = 0x00000040, /* Half Duplex */
  232. TxcwPauseMASK = 0x00000180, /* Pause */
  233. TxcwPauseSHIFT = 7,
  234. TxcwPs = 1<<TxcwPauseSHIFT, /* Pause Supported */
  235. TxcwAs = 2<<TxcwPauseSHIFT, /* Asymmetric FC desired */
  236. TxcwRfiMASK = 0x00003000, /* Remote Fault Indication */
  237. TxcwRfiSHIFT = 12,
  238. TxcwNpr = 0x00008000, /* Next Page Request */
  239. TxcwConfig = 0x40000000, /* Transmit Config Control */
  240. TxcwAne = 0x80000000, /* Auto-Negotiation Enable */
  241. };
  242. enum { /* Rctl */
  243. Rrst = 0x00000001, /* Receiver Software Reset */
  244. Ren = 0x00000002, /* Receiver Enable */
  245. Sbp = 0x00000004, /* Store Bad Packets */
  246. Upe = 0x00000008, /* Unicast Promiscuous Enable */
  247. Mpe = 0x00000010, /* Multicast Promiscuous Enable */
  248. Lpe = 0x00000020, /* Long Packet Reception Enable */
  249. LbmMASK = 0x000000C0, /* Loopback Mode */
  250. LbmOFF = 0x00000000, /* No Loopback */
  251. LbmTBI = 0x00000040, /* TBI Loopback */
  252. LbmMII = 0x00000080, /* GMII/MII Loopback */
  253. LbmXCVR = 0x000000C0, /* Transceiver Loopback */
  254. RdtmsMASK = 0x00000300, /* Rdesc Minimum Threshold Size */
  255. RdtmsHALF = 0x00000000, /* Threshold is 1/2 Rdlen */
  256. RdtmsQUARTER = 0x00000100, /* Threshold is 1/4 Rdlen */
  257. RdtmsEIGHTH = 0x00000200, /* Threshold is 1/8 Rdlen */
  258. MoMASK = 0x00003000, /* Multicast Offset */
  259. Bam = 0x00008000, /* Broadcast Accept Mode */
  260. BsizeMASK = 0x00030000, /* Receive Buffer Size */
  261. Bsize16384 = 0x00010000, /* Bsex = 1 */
  262. Bsize8192 = 0x00020000, /* Bsex = 1 */
  263. Bsize2048 = 0x00000000,
  264. Bsize1024 = 0x00010000,
  265. Bsize512 = 0x00020000,
  266. Bsize256 = 0x00030000,
  267. BsizeFlex = 0x08000000, /* Flexible Bsize in 1KB increments */
  268. Vfe = 0x00040000, /* VLAN Filter Enable */
  269. Cfien = 0x00080000, /* Canonical Form Indicator Enable */
  270. Cfi = 0x00100000, /* Canonical Form Indicator value */
  271. Dpf = 0x00400000, /* Discard Pause Frames */
  272. Pmcf = 0x00800000, /* Pass MAC Control Frames */
  273. Bsex = 0x02000000, /* Buffer Size Extension */
  274. Secrc = 0x04000000, /* Strip CRC from incoming packet */
  275. };
  276. enum { /* Tctl */
  277. Trst = 0x00000001, /* Transmitter Software Reset */
  278. Ten = 0x00000002, /* Transmit Enable */
  279. Psp = 0x00000008, /* Pad Short Packets */
  280. Mulr = 0x10000000, /* Allow multiple concurrent requests */
  281. Ctmask = 0x00000FF0, /* Collision Threshold */
  282. Ctshift = 4,
  283. ColdMASK = 0x003FF000, /* Collision Distance */
  284. ColdSHIFT = 12,
  285. Swxoff = 0x00400000, /* Sofware XOFF Transmission */
  286. Pbe = 0x00800000, /* Packet Burst Enable */
  287. Rtlc = 0x01000000, /* Re-transmit on Late Collision */
  288. Nrtu = 0x02000000, /* No Re-transmit on Underrrun */
  289. };
  290. enum { /* [RT]xdctl */
  291. PthreshMASK = 0x0000003F, /* Prefetch Threshold */
  292. PthreshSHIFT = 0,
  293. HthreshMASK = 0x00003F00, /* Host Threshold */
  294. HthreshSHIFT = 8,
  295. WthreshMASK = 0x003F0000, /* Writeback Threshold */
  296. WthreshSHIFT = 16,
  297. Gran = 0x01000000, /* Granularity (descriptors, not cls) */
  298. Qenable = 0x02000000, /* Queue Enable (82575) */
  299. };
  300. enum { /* Rxcsum */
  301. PcssMASK = 0x00FF, /* Packet Checksum Start */
  302. PcssSHIFT = 0,
  303. Ipofl = 0x0100, /* IP Checksum Off-load Enable */
  304. Tuofl = 0x0200, /* TCP/UDP Checksum Off-load Enable */
  305. };
  306. enum { /* Receive Delay Timer Ring */
  307. DelayMASK = 0xFFFF, /* delay timer in 1.024nS increments */
  308. DelaySHIFT = 0,
  309. Fpd = 0x80000000, /* Flush partial Descriptor Block */
  310. };
  311. typedef struct Ctlr Ctlr;
  312. typedef struct Rd Rd;
  313. typedef struct Td Td;
  314. struct Rd { /* Receive Descriptor */
  315. uint32_t addr[2];
  316. uint16_t length;
  317. uint16_t checksum;
  318. uint8_t status;
  319. uint8_t errors;
  320. uint16_t special;
  321. };
  322. enum { /* Rd status */
  323. Rdd = 0x01, /* Descriptor Done */
  324. Reop = 0x02, /* End of Packet */
  325. Ixsm = 0x04, /* Ignore Checksum Indication */
  326. Vp = 0x08, /* Packet is 802.1Q (matched VET) */
  327. Tcpcs = 0x20, /* TCP Checksum Calculated on Packet */
  328. Ipcs = 0x40, /* IP Checksum Calculated on Packet */
  329. Pif = 0x80, /* Passed in-exact filter */
  330. };
  331. enum { /* Rd errors */
  332. Ce = 0x01, /* CRC Error or Alignment Error */
  333. Se = 0x02, /* Symbol Error */
  334. Seq = 0x04, /* Sequence Error */
  335. Cxe = 0x10, /* Carrier Extension Error */
  336. Tcpe = 0x20, /* TCP/UDP Checksum Error */
  337. Ipe = 0x40, /* IP Checksum Error */
  338. Rxe = 0x80, /* RX Data Error */
  339. };
  340. struct Td { /* Transmit Descriptor */
  341. uint32_t addr[2]; /* Data */
  342. uint32_t control;
  343. uint32_t status;
  344. };
  345. enum { /* Tdesc control */
  346. LenMASK = 0x000FFFFF, /* Data/Packet Length Field */
  347. LenSHIFT = 0,
  348. DtypeCD = 0x00000000, /* Data Type 'Context Descriptor' */
  349. DtypeDD = 0x00100000, /* Data Type 'Data Descriptor' */
  350. PtypeTCP = 0x01000000, /* TCP/UDP Packet Type (CD) */
  351. Teop = 0x01000000, /* End of Packet (DD) */
  352. PtypeIP = 0x02000000, /* IP Packet Type (CD) */
  353. Ifcs = 0x02000000, /* Insert FCS (DD) */
  354. Tse = 0x04000000, /* TCP Segmentation Enable */
  355. Rs = 0x08000000, /* Report Status */
  356. Rps = 0x10000000, /* Report Status Sent */
  357. Dext = 0x20000000, /* Descriptor Extension */
  358. Vle = 0x40000000, /* VLAN Packet Enable */
  359. Ide = 0x80000000, /* Interrupt Delay Enable */
  360. };
  361. enum { /* Tdesc status */
  362. Tdd = 0x0001, /* Descriptor Done */
  363. Ec = 0x0002, /* Excess Collisions */
  364. Lc = 0x0004, /* Late Collision */
  365. Tu = 0x0008, /* Transmit Underrun */
  366. CssMASK = 0xFF00, /* Checksum Start Field */
  367. CssSHIFT = 8,
  368. };
  369. typedef struct {
  370. uint16_t *reg;
  371. uint32_t *reg32;
  372. uint16_t base;
  373. uint16_t lim;
  374. } Flash;
  375. enum {
  376. /* 16 and 32-bit flash registers for ich flash parts */
  377. Bfpr = 0x00/4, /* flash base 0:12; lim 16:28 */
  378. Fsts = 0x04/2, /* flash status; Hsfsts */
  379. Fctl = 0x06/2, /* flash control; Hsfctl */
  380. Faddr = 0x08/4, /* flash address to r/w */
  381. Fdata = 0x10/4, /* data @ address */
  382. /* status register */
  383. Fdone = 1<<0, /* flash cycle done */
  384. Fcerr = 1<<1, /* cycle error; write 1 to clear */
  385. Ael = 1<<2, /* direct access error log; 1 to clear */
  386. Scip = 1<<5, /* spi cycle in progress */
  387. Fvalid = 1<<14, /* flash descriptor valid */
  388. /* control register */
  389. Fgo = 1<<0, /* start cycle */
  390. Flcycle = 1<<1, /* two bits: r=0; w=2 */
  391. Fdbc = 1<<8, /* bytes to read; 5 bits */
  392. };
  393. /*
  394. * the kumeran interface is mac-to-phy for external gigabit ethernet on
  395. * intel's esb2 ich8 (io controller hub), it carries mii bits. can be used
  396. * to reset the phy. intel proprietary, see "kumeran specification".
  397. */
  398. enum {
  399. I217inbandctlpage = 770, /* phy page */
  400. I217inbandctlreg = 18, /* phy register */
  401. I217inbandctllnkststxtmoutmask = 0x3F00,
  402. I217inbandctllnkststxtmoutshift = 8,
  403. Fextnvm6reqpllclk = 0x100,
  404. Fextnvm6enak1entrycond = 0x200, /* extend K1 entry latency */
  405. Nvmk1cfg = 0x1B, /* NVM K1 Config Word */
  406. Nvmk1enable = 0x1, /* NVM Enable K1 bit */
  407. Kumctrlstaoff = 0x1F0000,
  408. Kumctrlstaoffshift = 16,
  409. Kumctrlstaren = 0x200000,
  410. Kumctrlstak1cfg = 0x7,
  411. Kumctrlstak1enable = 0x2,
  412. };
  413. enum {
  414. /*
  415. * these were 512, 1024 & 64, but 52, 253 & 9 are usually ample;
  416. * however cpu servers and terminals can need more receive buffers
  417. * due to bursts of traffic.
  418. *
  419. * Tdlen and Rdlen have to be multiples of 128. Rd and Td are both
  420. * 16 bytes long, so Nrd and Ntd must be multiples of 8.
  421. */
  422. Ntd = 32, /* power of two >= 8 */
  423. Nrd = 128, /* power of two >= 8 */
  424. Nrb = 1024, /* private receive buffers per Ctlr */
  425. Slop = 32, /* for vlan headers, crcs, etc. */
  426. };
  427. enum {
  428. Iany,
  429. i82563,
  430. i82566,
  431. i82567,
  432. i82571,
  433. i82572,
  434. i82573,
  435. i82574,
  436. i82575,
  437. i82576,
  438. i82577,
  439. i82579,
  440. i210,
  441. i217,
  442. i218,
  443. };
  444. static char *tname[] = {
  445. [Iany] = "any",
  446. [i82563] = "i82563",
  447. [i82566] = "i82566",
  448. [i82567] = "i82567",
  449. [i82571] = "i82571",
  450. [i82572] = "i82572",
  451. [i82573] = "i82573",
  452. [i82574] = "i82574",
  453. [i82575] = "i82575",
  454. [i82576] = "i82576",
  455. [i82577] = "i82577",
  456. [i82579] = "i82579",
  457. [i210] = "i210",
  458. [i217] = "i217",
  459. [i218] = "i218",
  460. };
  461. struct Ctlr {
  462. int port;
  463. Pcidev *pcidev;
  464. Ctlr *next;
  465. Ether *edev;
  466. int active;
  467. int type;
  468. uint16_t eeprom[0x40];
  469. QLock alock; /* attach */
  470. int attached;
  471. int *nic;
  472. Lock imlock;
  473. int im; /* interrupt mask */
  474. Rendez lrendez;
  475. int lim;
  476. int phynum;
  477. int didk1fix;
  478. Watermark wmrb;
  479. Watermark wmrd;
  480. Watermark wmtd;
  481. QLock slock;
  482. uint statistics[Nstatistics];
  483. uint lsleep;
  484. uint lintr;
  485. uint rsleep;
  486. uint rintr;
  487. uint txdw;
  488. uint tintr;
  489. uint ixsm;
  490. uint ipcs;
  491. uint tcpcs;
  492. uint speeds[4];
  493. uint8_t ra[Eaddrlen]; /* receive address */
  494. uint32_t mta[128]; /* multicast table array */
  495. Rendez rrendez;
  496. int rim;
  497. int rdfree; /* rx descriptors awaiting packets */
  498. Rd *rdba; /* receive descriptor base address */
  499. Block **rb; /* receive buffers */
  500. int rdh; /* receive descriptor head */
  501. int rdt; /* receive descriptor tail */
  502. Rendez trendez;
  503. QLock tlock;
  504. Td *tdba; /* transmit descriptor base address */
  505. Block **tb; /* transmit buffers */
  506. int tdh; /* transmit descriptor head */
  507. int tdt; /* transmit descriptor tail */
  508. int fcrtl;
  509. int fcrth;
  510. uint pbs; /* packet buffer size */
  511. uint pba; /* packet buffer allocation */
  512. };
  513. #define csr32r(c, r) (*((c)->nic+((r)/4)))
  514. #define csr32w(c, r, v) (*((c)->nic+((r)/4)) = (v))
  515. static Ctlr* i82563ctlrhead;
  516. static Ctlr* i82563ctlrtail;
  517. static Lock i82563rblock; /* free receive Blocks */
  518. static Block* i82563rbpool;
  519. static int nrbfull; /* # of rcv Blocks with data awaiting processing */
  520. static int speedtab[] = {
  521. 10, 100, 1000, 0
  522. };
  523. static char* statistics[] = {
  524. "CRC Error",
  525. "Alignment Error",
  526. "Symbol Error",
  527. "RX Error",
  528. "Missed Packets",
  529. "Single Collision",
  530. "Excessive Collisions",
  531. "Multiple Collision",
  532. "Late Collisions",
  533. nil,
  534. "Collision",
  535. "Transmit Underrun",
  536. "Defer",
  537. "Transmit - No CRS",
  538. "Sequence Error",
  539. "Carrier Extension Error",
  540. "Receive Error Length",
  541. nil,
  542. "XON Received",
  543. "XON Transmitted",
  544. "XOFF Received",
  545. "XOFF Transmitted",
  546. "FC Received Unsupported",
  547. "Packets Received (64 Bytes)",
  548. "Packets Received (65-127 Bytes)",
  549. "Packets Received (128-255 Bytes)",
  550. "Packets Received (256-511 Bytes)",
  551. "Packets Received (512-1023 Bytes)",
  552. "Packets Received (1024-mtu Bytes)",
  553. "Good Packets Received",
  554. "Broadcast Packets Received",
  555. "Multicast Packets Received",
  556. "Good Packets Transmitted",
  557. nil,
  558. "Good Octets Received",
  559. nil,
  560. "Good Octets Transmitted",
  561. nil,
  562. nil,
  563. nil,
  564. "Receive No Buffers",
  565. "Receive Undersize",
  566. "Receive Fragment",
  567. "Receive Oversize",
  568. "Receive Jabber",
  569. "Management Packets Rx",
  570. "Management Packets Drop",
  571. "Management Packets Tx",
  572. "Total Octets Received",
  573. nil,
  574. "Total Octets Transmitted",
  575. nil,
  576. "Total Packets Received",
  577. "Total Packets Transmitted",
  578. "Packets Transmitted (64 Bytes)",
  579. "Packets Transmitted (65-127 Bytes)",
  580. "Packets Transmitted (128-255 Bytes)",
  581. "Packets Transmitted (256-511 Bytes)",
  582. "Packets Transmitted (512-1023 Bytes)",
  583. "Packets Transmitted (1024-mtu Bytes)",
  584. "Multicast Packets Transmitted",
  585. "Broadcast Packets Transmitted",
  586. "TCP Segmentation Context Transmitted",
  587. "TCP Segmentation Context Fail",
  588. "Interrupt Assertion",
  589. "Interrupt Rx Pkt Timer",
  590. "Interrupt Rx Abs Timer",
  591. "Interrupt Tx Pkt Timer",
  592. "Interrupt Tx Abs Timer",
  593. "Interrupt Tx Queue Empty",
  594. "Interrupt Tx Desc Low",
  595. "Interrupt Rx Min",
  596. "Interrupt Rx Overrun",
  597. };
  598. static int i82563reset(Ctlr *);
  599. static int32_t
  600. i82563ifstat(Ether* edev, void* a, int32_t n, uint32_t offset)
  601. {
  602. Ctlr *ctlr;
  603. char *s, *p, *e, *stat;
  604. int i, r;
  605. uint64_t tuvl, ruvl;
  606. ctlr = edev->ctlr;
  607. qlock(&ctlr->slock);
  608. p = s = malloc(READSTR);
  609. if(p == nil) {
  610. qunlock(&ctlr->slock);
  611. error(Enomem);
  612. }
  613. e = p + READSTR;
  614. for(i = 0; i < Nstatistics; i++){
  615. r = csr32r(ctlr, Statistics + i*4);
  616. if((stat = statistics[i]) == nil)
  617. continue;
  618. switch(i){
  619. case Gorcl:
  620. case Gotcl:
  621. case Torl:
  622. case Totl:
  623. ruvl = r;
  624. ruvl += (uint64_t)csr32r(ctlr, Statistics+(i+1)*4) << 32;
  625. tuvl = ruvl;
  626. tuvl += ctlr->statistics[i];
  627. tuvl += (uint64_t)ctlr->statistics[i+1] << 32;
  628. if(tuvl == 0)
  629. continue;
  630. ctlr->statistics[i] = tuvl;
  631. ctlr->statistics[i+1] = tuvl >> 32;
  632. p = seprint(p, e, "%s: %llu %llu\n", stat, tuvl, ruvl);
  633. i++;
  634. break;
  635. default:
  636. ctlr->statistics[i] += r;
  637. if(ctlr->statistics[i] == 0)
  638. continue;
  639. p = seprint(p, e, "%s: %u %u\n", stat,
  640. ctlr->statistics[i], r);
  641. break;
  642. }
  643. }
  644. p = seprint(p, e, "lintr: %u %u\n", ctlr->lintr, ctlr->lsleep);
  645. p = seprint(p, e, "rintr: %u %u\n", ctlr->rintr, ctlr->rsleep);
  646. p = seprint(p, e, "tintr: %u %u\n", ctlr->tintr, ctlr->txdw);
  647. p = seprint(p, e, "ixcs: %u %u %u\n", ctlr->ixsm, ctlr->ipcs, ctlr->tcpcs);
  648. p = seprint(p, e, "ctrl: %.8x\n", csr32r(ctlr, Ctrl));
  649. p = seprint(p, e, "ctrlext: %.8x\n", csr32r(ctlr, Ctrlext));
  650. p = seprint(p, e, "status: %.8x\n", csr32r(ctlr, Status));
  651. p = seprint(p, e, "txcw: %.8x\n", csr32r(ctlr, Txcw));
  652. p = seprint(p, e, "txdctl: %.8x\n", csr32r(ctlr, Txdctl));
  653. p = seprint(p, e, "pbs: %dKB\n", ctlr->pbs);
  654. p = seprint(p, e, "pba: %#.8ux\n", ctlr->pba);
  655. p = seprint(p, e, "speeds: 10:%u 100:%u 1000:%u ?:%u\n",
  656. ctlr->speeds[0], ctlr->speeds[1], ctlr->speeds[2], ctlr->speeds[3]);
  657. p = seprint(p, e, "type: %s\n", tname[ctlr->type]);
  658. p = seprint(p, e, "nrbfull (rcv blocks outstanding): %d\n", nrbfull);
  659. // p = seprint(p, e, "eeprom:");
  660. // for(i = 0; i < 0x40; i++){
  661. // if(i && ((i & 7) == 0))
  662. // p = seprint(p, e, "\n ");
  663. // p = seprint(p, e, " %4.4x", ctlr->eeprom[i]);
  664. // }
  665. // p = seprint(p, e, "\n");
  666. p = seprintmark(p, e, &ctlr->wmrb);
  667. p = seprintmark(p, e, &ctlr->wmrd);
  668. p = seprintmark(p, e, &ctlr->wmtd);
  669. USED(p);
  670. n = readstr(offset, a, n, s);
  671. free(s);
  672. qunlock(&ctlr->slock);
  673. return n;
  674. }
  675. static int32_t
  676. i82563ctl(Ether*_1, void*_2, int32_t _3)
  677. {
  678. error(Enonexist);
  679. return 0;
  680. }
  681. static void
  682. i82563promiscuous(void* arg, int on)
  683. {
  684. int rctl;
  685. Ctlr *ctlr;
  686. Ether *edev;
  687. edev = arg;
  688. ctlr = edev->ctlr;
  689. rctl = csr32r(ctlr, Rctl) & ~MoMASK;
  690. if(on)
  691. rctl |= Upe|Mpe;
  692. else
  693. rctl &= ~(Upe|Mpe);
  694. csr32w(ctlr, Rctl, rctl);
  695. }
  696. /*
  697. * Returns number of longs of ctlr->mta in use (a power of 2).
  698. * This must be right for multicast (thus ipv6) to work reliably.
  699. */
  700. static int
  701. mcasttblsize(Ctlr *ctlr)
  702. {
  703. switch (ctlr->type) {
  704. case i210:
  705. return 16;
  706. /*
  707. * openbsd says all `ich8' versions (ich8, ich9, ich10, pch, pch2 and
  708. * pch_lpt) have 32 longs. the 218 seems to be an exception.
  709. */
  710. case i82566:
  711. case i82567:
  712. case i217:
  713. return 32;
  714. case i218:
  715. return 64;
  716. default:
  717. return 128;
  718. }
  719. }
  720. static void
  721. i82563multicast(void* arg, uint8_t* addr, int on)
  722. {
  723. int bit, x;
  724. Ctlr *ctlr;
  725. Ether *edev;
  726. edev = arg;
  727. ctlr = edev->ctlr;
  728. x = (addr[5]>>1) & (mcasttblsize(ctlr) - 1);
  729. bit = (addr[5] & 1)<<4 | addr[4]>>4;
  730. /*
  731. * multiple ether addresses can hash to the same filter bit,
  732. * so it's never safe to clear a filter bit.
  733. * if we want to clear filter bits, we need to keep track of
  734. * all the multicast addresses in use, clear all the filter bits,
  735. * then set the ones corresponding to in-use addresses.
  736. */
  737. if(on)
  738. ctlr->mta[x] |= 1<<bit;
  739. // else
  740. // ctlr->mta[x] &= ~(1<<bit);
  741. csr32w(ctlr, Mta+x*4, ctlr->mta[x]);
  742. }
  743. static Block*
  744. i82563rballoc(void)
  745. {
  746. Block *bp;
  747. ilock(&i82563rblock);
  748. if((bp = i82563rbpool) != nil){
  749. i82563rbpool = bp->next;
  750. bp->next = nil;
  751. ainc(&bp->ref); /* prevent bp from being freed */
  752. }
  753. iunlock(&i82563rblock);
  754. return bp;
  755. }
  756. static void
  757. i82563rbfree(Block* b)
  758. {
  759. b->rp = b->wp = (uint8_t*)PGROUND((uintptr)b->base);
  760. b->flag &= ~(Bipck | Budpck | Btcpck | Bpktck);
  761. ilock(&i82563rblock);
  762. b->next = i82563rbpool;
  763. i82563rbpool = b;
  764. nrbfull--;
  765. iunlock(&i82563rblock);
  766. }
  767. static void
  768. i82563im(Ctlr* ctlr, int im)
  769. {
  770. ilock(&ctlr->imlock);
  771. ctlr->im |= im;
  772. csr32w(ctlr, Ims, ctlr->im);
  773. iunlock(&ctlr->imlock);
  774. }
  775. static void
  776. i82563txinit(Ctlr* ctlr)
  777. {
  778. int i, r, tctl;
  779. Block *bp;
  780. tctl = 0x0F<<Ctshift | Psp;
  781. switch (ctlr->type) {
  782. case i210:
  783. break;
  784. default:
  785. tctl |= Mulr;
  786. /* fall through */
  787. case i217:
  788. case i218:
  789. tctl |= 66<<ColdSHIFT;
  790. break;
  791. }
  792. csr32w(ctlr, Tctl, tctl);
  793. csr32w(ctlr, Tipg, 6<<20 | 8<<10 | 8); /* yb sez: 0x702008 */
  794. for(i = 0; i < Ntd; i++)
  795. if((bp = ctlr->tb[i]) != nil) {
  796. ctlr->tb[i] = nil;
  797. freeb(bp);
  798. }
  799. memset(ctlr->tdba, 0, Ntd * sizeof(Td));
  800. coherence();
  801. csr32w(ctlr, Tdbal, PCIWADDR(ctlr->tdba));
  802. csr32w(ctlr, Tdbah, 0); /* 32-bit system */
  803. csr32w(ctlr, Tdlen, Ntd * sizeof(Td));
  804. ctlr->tdh = PREV(0, Ntd);
  805. csr32w(ctlr, Tdh, 0);
  806. ctlr->tdt = 0;
  807. csr32w(ctlr, Tdt, 0);
  808. csr32w(ctlr, Tidv, 0); /* don't coalesce interrupts */
  809. csr32w(ctlr, Tadv, 0);
  810. r = csr32r(ctlr, Txdctl) & ~(WthreshMASK|PthreshMASK);
  811. r |= 4<<WthreshSHIFT | 4<<PthreshSHIFT;
  812. if(ctlr->type == i82575 || ctlr->type == i82576 || ctlr->type == i210)
  813. r |= Qenable;
  814. csr32w(ctlr, Txdctl, r);
  815. coherence();
  816. csr32w(ctlr, Tctl, csr32r(ctlr, Tctl) | Ten);
  817. }
  818. static int
  819. i82563cleanup(Ctlr *ctlr)
  820. {
  821. Block *bp;
  822. int tdh, n;
  823. tdh = ctlr->tdh;
  824. while(ctlr->tdba[n = NEXT(tdh, Ntd)].status & Tdd){
  825. tdh = n;
  826. if((bp = ctlr->tb[tdh]) != nil){
  827. ctlr->tb[tdh] = nil;
  828. freeb(bp);
  829. }else
  830. iprint("82563 tx underrun!\n");
  831. ctlr->tdba[tdh].status = 0;
  832. }
  833. return ctlr->tdh = tdh;
  834. }
  835. static void
  836. i82563transmit(Ether* edev)
  837. {
  838. Td *td;
  839. Block *bp;
  840. Ctlr *ctlr;
  841. int tdh, tdt;
  842. ctlr = edev->ctlr;
  843. qlock(&ctlr->tlock);
  844. /*
  845. * Free any completed packets
  846. */
  847. tdh = i82563cleanup(ctlr);
  848. /* if link down on 218, don't try since we need k1fix to run first */
  849. if (!edev->Netif.link && ctlr->type == i218 && !ctlr->didk1fix) {
  850. qunlock(&ctlr->tlock);
  851. return;
  852. }
  853. /*
  854. * Try to fill the ring back up.
  855. */
  856. tdt = ctlr->tdt;
  857. for(;;){
  858. if(NEXT(tdt, Ntd) == tdh){ /* ring full? */
  859. ctlr->txdw++;
  860. i82563im(ctlr, Txdw);
  861. break;
  862. }
  863. if((bp = qget(edev->oq)) == nil)
  864. break;
  865. td = &ctlr->tdba[tdt];
  866. td->addr[0] = PCIWADDR(bp->rp);
  867. td->control = Ide|Rs|Ifcs|Teop|BLEN(bp);
  868. ctlr->tb[tdt] = bp;
  869. /* note size of queue of tds awaiting transmission */
  870. notemark(&ctlr->wmtd, (tdt + Ntd - tdh) % Ntd);
  871. tdt = NEXT(tdt, Ntd);
  872. }
  873. if(ctlr->tdt != tdt){
  874. ctlr->tdt = tdt;
  875. coherence();
  876. csr32w(ctlr, Tdt, tdt);
  877. }
  878. /* else may not be any new ones, but could be some still in flight */
  879. qunlock(&ctlr->tlock);
  880. }
  881. static void
  882. i82563replenish(Ctlr* ctlr)
  883. {
  884. Rd *rd;
  885. int rdt;
  886. Block *bp;
  887. rdt = ctlr->rdt;
  888. while(NEXT(rdt, Nrd) != ctlr->rdh){
  889. rd = &ctlr->rdba[rdt];
  890. if(ctlr->rb[rdt] != nil){
  891. print("#l%d: 82563: rx overrun\n", ctlr->edev->ctlrno);
  892. break;
  893. }
  894. bp = i82563rballoc();
  895. if(bp == nil)
  896. /*
  897. * this almost never gets better. likely there's a bug
  898. * elsewhere in the kernel that is failing to free a
  899. * receive Block.
  900. */
  901. panic("#l%d: 82563: all %d rx buffers in use, nrbfull %d",
  902. ctlr->edev->ctlrno, Nrb, nrbfull);
  903. ctlr->rb[rdt] = bp;
  904. rd->addr[0] = PCIWADDR(bp->rp);
  905. // rd->addr[1] = 0;
  906. rd->status = 0;
  907. ctlr->rdfree++;
  908. rdt = NEXT(rdt, Nrd);
  909. }
  910. ctlr->rdt = rdt;
  911. coherence();
  912. csr32w(ctlr, Rdt, rdt);
  913. }
  914. static void
  915. i82563rxinit(Ctlr* ctlr)
  916. {
  917. Block *bp;
  918. int i, r, rctl, type;
  919. rctl = Dpf|Bsize2048|Bam|RdtmsHALF;
  920. type = ctlr->type;
  921. if(type == i82575 || type == i82576 || type == i210){
  922. /*
  923. * Setting Qenable in Rxdctl does not
  924. * appear to stick unless Ren is on.
  925. */
  926. csr32w(ctlr, Rctl, Ren|rctl);
  927. csr32w(ctlr, Rxdctl, csr32r(ctlr, Rxdctl) | Qenable);
  928. }
  929. csr32w(ctlr, Rctl, rctl);
  930. switch (type) {
  931. case i82573:
  932. case i82577:
  933. // case i82577: /* not yet implemented */
  934. case i82579:
  935. case i210:
  936. case i217:
  937. case i218:
  938. csr32w(ctlr, Ert, 1024/8); /* early rx threshold */
  939. break;
  940. }
  941. csr32w(ctlr, Rdbal, PCIWADDR(ctlr->rdba));
  942. csr32w(ctlr, Rdbah, 0); /* 32-bit system */
  943. csr32w(ctlr, Rdlen, Nrd * sizeof(Rd));
  944. ctlr->rdh = ctlr->rdt = 0;
  945. csr32w(ctlr, Rdh, 0);
  946. csr32w(ctlr, Rdt, 0);
  947. /* to hell with interrupt moderation, we want low latency */
  948. csr32w(ctlr, Rdtr, 0);
  949. csr32w(ctlr, Radv, 0);
  950. for(i = 0; i < Nrd; i++)
  951. if((bp = ctlr->rb[i]) != nil){
  952. ctlr->rb[i] = nil;
  953. freeb(bp);
  954. }
  955. i82563replenish(ctlr);
  956. if(type == i82575 || type == i82576 || type == i210){
  957. /*
  958. * See comment above for Qenable.
  959. * Could shuffle the code?
  960. */
  961. r = csr32r(ctlr, Rxdctl) & ~(WthreshMASK|PthreshMASK);
  962. csr32w(ctlr, Rxdctl, r | 2<<WthreshSHIFT | 2<<PthreshSHIFT);
  963. }
  964. /*
  965. * Don't enable checksum offload. In practice, it interferes with
  966. * tftp booting on at least the 82575.
  967. */
  968. csr32w(ctlr, Rxcsum, 0);
  969. }
  970. static int
  971. i82563rim(void* ctlr)
  972. {
  973. return ((Ctlr*)ctlr)->rim != 0;
  974. }
  975. /*
  976. * With no errors and the Ixsm bit set,
  977. * the descriptor status Tpcs and Ipcs bits give
  978. * an indication of whether the checksums were
  979. * calculated and valid.
  980. *
  981. * Must be called with rd->errors == 0.
  982. */
  983. static void
  984. ckcksums(Ctlr *ctlr, Rd *rd, Block *bp)
  985. {
  986. if (0) {
  987. if(rd->status & Ixsm)
  988. return;
  989. ctlr->ixsm++;
  990. if(rd->status & Ipcs){
  991. /*
  992. * IP checksum calculated (and valid as errors == 0).
  993. */
  994. ctlr->ipcs++;
  995. bp->flag |= Bipck;
  996. }
  997. if(rd->status & Tcpcs){
  998. /*
  999. * TCP/UDP checksum calculated (and valid as errors == 0).
  1000. */
  1001. ctlr->tcpcs++;
  1002. bp->flag |= Btcpck|Budpck;
  1003. }
  1004. bp->checksum = rd->checksum;
  1005. bp->flag |= Bpktck;
  1006. }
  1007. }
  1008. static void
  1009. i82563rproc(void* arg)
  1010. {
  1011. Rd *rd;
  1012. Block *bp;
  1013. Ctlr *ctlr;
  1014. int rdh, rim, passed;
  1015. Ether *edev;
  1016. edev = arg;
  1017. ctlr = edev->ctlr;
  1018. i82563rxinit(ctlr);
  1019. coherence();
  1020. csr32w(ctlr, Rctl, csr32r(ctlr, Rctl) | Ren);
  1021. if(ctlr->type == i210)
  1022. csr32w(ctlr, Rxdctl, csr32r(ctlr, Rxdctl) | Qenable);
  1023. for(;;){
  1024. i82563replenish(ctlr);
  1025. i82563im(ctlr, Rxt0|Rxo|Rxdmt0|Rxseq|Ack);
  1026. ctlr->rsleep++;
  1027. sleep(&ctlr->rrendez, i82563rim, ctlr);
  1028. rdh = ctlr->rdh;
  1029. passed = 0;
  1030. for(;;){
  1031. rim = ctlr->rim;
  1032. ctlr->rim = 0;
  1033. rd = &ctlr->rdba[rdh];
  1034. if(!(rd->status & Rdd))
  1035. break;
  1036. /*
  1037. * Accept eop packets with no errors.
  1038. */
  1039. bp = ctlr->rb[rdh];
  1040. if((rd->status & Reop) && rd->errors == 0){
  1041. bp->wp += rd->length;
  1042. bp->lim = bp->wp; /* lie like a dog. */
  1043. if(0)
  1044. ckcksums(ctlr, rd, bp);
  1045. ilock(&i82563rblock);
  1046. nrbfull++;
  1047. iunlock(&i82563rblock);
  1048. notemark(&ctlr->wmrb, nrbfull);
  1049. etheriq(edev, bp, 1); /* pass pkt upstream */
  1050. passed++;
  1051. } else {
  1052. if (rd->status & Reop && rd->errors)
  1053. print("%s: input packet error %#x\n",
  1054. tname[ctlr->type], rd->errors);
  1055. freeb(bp);
  1056. }
  1057. ctlr->rb[rdh] = nil;
  1058. /* rd needs to be replenished to accept another pkt */
  1059. rd->status = 0;
  1060. ctlr->rdfree--;
  1061. ctlr->rdh = rdh = NEXT(rdh, Nrd);
  1062. /*
  1063. * if number of rds ready for packets is too low,
  1064. * set up the unready ones.
  1065. */
  1066. if(ctlr->rdfree <= Nrd - 32 || (rim & Rxdmt0))
  1067. i82563replenish(ctlr);
  1068. }
  1069. /* note how many rds had full buffers */
  1070. notemark(&ctlr->wmrd, passed);
  1071. }
  1072. }
  1073. static int
  1074. i82563lim(void* ctlr)
  1075. {
  1076. return ((Ctlr*)ctlr)->lim != 0;
  1077. }
  1078. static int
  1079. phynum(Ctlr *ctlr)
  1080. {
  1081. if (ctlr->phynum < 0)
  1082. switch (ctlr->type) {
  1083. case i82577:
  1084. // case i82578: /* not yet implemented */
  1085. case i82579:
  1086. case i217:
  1087. case i218:
  1088. ctlr->phynum = 2; /* pcie phy */
  1089. break;
  1090. default:
  1091. ctlr->phynum = 1; /* gbe phy */
  1092. break;
  1093. }
  1094. return ctlr->phynum;
  1095. }
  1096. static uint
  1097. phyread(Ctlr *ctlr, int reg)
  1098. {
  1099. uint phy, i;
  1100. if (reg >= 32)
  1101. iprint("phyread: reg %d >= 32\n", reg);
  1102. csr32w(ctlr, Mdic, MDIrop | phynum(ctlr)<<MDIpSHIFT | reg<<MDIrSHIFT);
  1103. phy = 0;
  1104. for(i = 0; i < 64; i++){
  1105. phy = csr32r(ctlr, Mdic);
  1106. if(phy & (MDIe|MDIready))
  1107. break;
  1108. microdelay(1);
  1109. }
  1110. if((phy & (MDIe|MDIready)) != MDIready)
  1111. return ~0;
  1112. return phy & 0xffff;
  1113. }
  1114. static uint
  1115. phywrite(Ctlr *ctlr, int reg, uint16_t val)
  1116. {
  1117. uint phy, i;
  1118. if (reg >= 32)
  1119. iprint("phyread: reg %d >= 32\n", reg);
  1120. csr32w(ctlr, Mdic, MDIwop | phynum(ctlr)<<MDIpSHIFT | reg<<MDIrSHIFT |
  1121. val);
  1122. phy = 0;
  1123. for(i = 0; i < 64; i++){
  1124. phy = csr32r(ctlr, Mdic);
  1125. if(phy & (MDIe|MDIready))
  1126. break;
  1127. microdelay(1);
  1128. }
  1129. if((phy & (MDIe|MDIready)) != MDIready)
  1130. return ~0;
  1131. return 0;
  1132. }
  1133. static uint32_t
  1134. kmrnread(Ctlr *ctlr, uint32_t reg_addr)
  1135. {
  1136. csr32w(ctlr, Kumctrlsta, ((reg_addr << Kumctrlstaoffshift) &
  1137. Kumctrlstaoff) | Kumctrlstaren); /* write register address */
  1138. microdelay(2);
  1139. return csr32r(ctlr, Kumctrlsta); /* read data */
  1140. }
  1141. static void
  1142. kmrnwrite(Ctlr *ctlr, uint32_t reg_addr, uint16_t data)
  1143. {
  1144. csr32w(ctlr, Kumctrlsta, ((reg_addr << Kumctrlstaoffshift) &
  1145. Kumctrlstaoff) | data);
  1146. microdelay(2);
  1147. }
  1148. /*
  1149. * this is essentially black magic. we blindly follow the incantations
  1150. * prescribed by the god Intel:
  1151. *
  1152. * On ESB2, the MAC-to-PHY (Kumeran) interface must be configured after
  1153. * link is up before any traffic is sent.
  1154. *
  1155. * workaround DMA unit hang on I218
  1156. *
  1157. * At 1Gbps link speed, one of the MAC's internal clocks can be stopped
  1158. * for up to 4us when entering K1 (a power mode of the MAC-PHY
  1159. * interconnect). If the MAC is waiting for completion indications for 2
  1160. * DMA write requests into Host memory (e.g. descriptor writeback or Rx
  1161. * packet writing) and the indications occur while the clock is stopped,
  1162. * both indications will be missed by the MAC, causing the MAC to wait
  1163. * for the completion indications and be unable to generate further DMA
  1164. * write requests. This results in an apparent hardware hang.
  1165. *
  1166. * Work-around the bug by disabling the de-assertion of the clock request
  1167. * when 1Gbps link is acquired (K1 must be disabled while doing this).
  1168. * Also, set appropriate Tx re-transmission timeouts for 10 and 100-half
  1169. * link speeds to avoid Tx hangs.
  1170. */
  1171. static void
  1172. k1fix(Ctlr *ctlr)
  1173. {
  1174. int txtmout; /* units of 10µs */
  1175. uint32_t fextnvm6, status;
  1176. uint16_t reg;
  1177. Ether *edev;
  1178. edev = ctlr->edev;
  1179. fextnvm6 = csr32r(ctlr, Fextnvm6);
  1180. status = csr32r(ctlr, Status);
  1181. /* status speed bits are different on 217/8 than earlier ctlrs */
  1182. if (edev->Netif.link && status & (Sspeed1000>>2)) {
  1183. reg = kmrnread(ctlr, Kumctrlstak1cfg);
  1184. kmrnwrite(ctlr, Kumctrlstak1cfg, reg & ~Kumctrlstak1enable);
  1185. microdelay(10);
  1186. csr32w(ctlr, Fextnvm6, fextnvm6 | Fextnvm6reqpllclk);
  1187. kmrnwrite(ctlr, Kumctrlstak1cfg, reg);
  1188. ctlr->didk1fix = 1;
  1189. return;
  1190. }
  1191. /* else uncommon cases */
  1192. fextnvm6 &= ~Fextnvm6reqpllclk;
  1193. /*
  1194. * 217 manual claims not to have Frcdplx bit in status;
  1195. * 218 manual just omits the non-phy registers.
  1196. */
  1197. if (!edev->Netif.link ||
  1198. (status & (Sspeed100>>2|Frcdplx)) == (Sspeed100>>2|Frcdplx)) {
  1199. csr32w(ctlr, Fextnvm6, fextnvm6);
  1200. ctlr->didk1fix = 1;
  1201. return;
  1202. }
  1203. /* access other page via phy addr 1 reg 31, then access reg 16-30 */
  1204. phywrite(ctlr, Phypage, I217inbandctlpage<<5);
  1205. reg = phyread(ctlr, I217inbandctlreg) & ~I217inbandctllnkststxtmoutmask;
  1206. if (status & (Sspeed100>>2)) { /* 100Mb/s half-duplex? */
  1207. txtmout = 5;
  1208. fextnvm6 &= ~Fextnvm6enak1entrycond;
  1209. } else { /* 10Mb/s */
  1210. txtmout = 50;
  1211. fextnvm6 |= Fextnvm6enak1entrycond;
  1212. }
  1213. phywrite(ctlr, I217inbandctlreg, reg |
  1214. txtmout << I217inbandctllnkststxtmoutshift);
  1215. csr32w(ctlr, Fextnvm6, fextnvm6);
  1216. phywrite(ctlr, Phypage, 0<<5); /* reset page to usual 0 */
  1217. ctlr->didk1fix = 1;
  1218. }
  1219. /*
  1220. * watch for changes of link state
  1221. */
  1222. static void
  1223. i82563lproc(void *v)
  1224. {
  1225. uint phy, sp, a, phy79, prevlink;
  1226. Ctlr *ctlr;
  1227. Ether *edev;
  1228. edev = v;
  1229. ctlr = edev->ctlr;
  1230. phy79 = 0;
  1231. switch (ctlr->type) {
  1232. case i82579:
  1233. // case i82580:
  1234. case i217:
  1235. case i218:
  1236. // case i219:
  1237. // case i350:
  1238. // case i354:
  1239. phy79 = 1;
  1240. break;
  1241. }
  1242. if(ctlr->type == i82573 && (phy = phyread(ctlr, Phyier)) != ~0)
  1243. phywrite(ctlr, Phyier, phy | Lscie | Ancie | Spdie | Panie);
  1244. else if(phy79 && (phy = phyread(ctlr, Phyier218)) != ~0)
  1245. phywrite(ctlr, Phyier218, phy | Lscie218 | Ancie218 | Spdie218);
  1246. prevlink = 0;
  1247. for(;;){
  1248. a = 0;
  1249. phy = phyread(ctlr, phy79? Phystat: Physsr);
  1250. if(phy == ~0)
  1251. goto next;
  1252. if (phy79) {
  1253. sp = (phy>>8) & 3;
  1254. // a = phy & (ctlr->type == i218? Anfs: Ans);
  1255. a = phy & Anfs;
  1256. } else {
  1257. sp = (phy>>14) & 3;
  1258. switch(ctlr->type){
  1259. case i82563:
  1260. case i210:
  1261. a = phyread(ctlr, Phyisr) & Ane; /* a-n error */
  1262. break;
  1263. case i82571:
  1264. case i82572:
  1265. case i82575:
  1266. case i82576:
  1267. a = phyread(ctlr, Phylhr) & Anf; /* a-n fault */
  1268. sp = (sp-1) & 3;
  1269. break;
  1270. }
  1271. }
  1272. if(a)
  1273. phywrite(ctlr, Phyctl, phyread(ctlr, Phyctl) |
  1274. Ran | Ean); /* enable & restart autoneg */
  1275. edev->Netif.link = (phy & (phy79? Link: Rtlink)) != 0;
  1276. if(edev->Netif.link){
  1277. ctlr->speeds[sp]++;
  1278. if (speedtab[sp])
  1279. edev->Netif.mbps = speedtab[sp];
  1280. if (prevlink == 0 && ctlr->type == i218)
  1281. k1fix(ctlr); /* link newly up: kludge away */
  1282. } else
  1283. ctlr->didk1fix = 0; /* force fix at next link up */
  1284. prevlink = edev->Netif.link;
  1285. next:
  1286. ctlr->lim = 0;
  1287. i82563im(ctlr, Lsc);
  1288. ctlr->lsleep++;
  1289. sleep(&ctlr->lrendez, i82563lim, ctlr);
  1290. }
  1291. }
  1292. static void
  1293. i82563tproc(void *v)
  1294. {
  1295. Ether *edev;
  1296. Ctlr *ctlr;
  1297. edev = v;
  1298. ctlr = edev->ctlr;
  1299. for(;;){
  1300. sleep(&ctlr->trendez, return0, 0);
  1301. i82563transmit(edev);
  1302. }
  1303. }
  1304. static void
  1305. freerbs(Ctlr *_)
  1306. {
  1307. int i;
  1308. Block *bp;
  1309. for(i = Nrb; i > 0; i--){
  1310. bp = i82563rballoc();
  1311. bp->free = nil;
  1312. freeb(bp);
  1313. }
  1314. }
  1315. static void
  1316. freemem(Ctlr *ctlr)
  1317. {
  1318. freerbs(ctlr);
  1319. free(ctlr->tb);
  1320. ctlr->tb = nil;
  1321. free(ctlr->rb);
  1322. ctlr->rb = nil;
  1323. free(ctlr->tdba);
  1324. ctlr->tdba = nil;
  1325. free(ctlr->rdba);
  1326. ctlr->rdba = nil;
  1327. }
  1328. static void
  1329. i82563attach(Ether* edev)
  1330. {
  1331. Proc *up = externup();
  1332. int i;
  1333. Block *bp;
  1334. Ctlr *ctlr;
  1335. char name[KNAMELEN];
  1336. ctlr = edev->ctlr;
  1337. qlock(&ctlr->alock);
  1338. if(ctlr->attached){
  1339. qunlock(&ctlr->alock);
  1340. return;
  1341. }
  1342. if(waserror()){
  1343. freemem(ctlr);
  1344. qunlock(&ctlr->alock);
  1345. nexterror();
  1346. }
  1347. ctlr->rdba = mallocalign(Nrd * sizeof(Rd), 128, 0, 0);
  1348. ctlr->tdba = mallocalign(Ntd * sizeof(Td), 128, 0, 0);
  1349. if(ctlr->rdba == nil || ctlr->tdba == nil ||
  1350. (ctlr->rb = malloc(Nrd*sizeof(Block*))) == nil ||
  1351. (ctlr->tb = malloc(Ntd*sizeof(Block*))) == nil)
  1352. error(Enomem);
  1353. for(i = 0; i < Nrb; i++){
  1354. if((bp = allocb(ETHERMAXTU + Slop + PGSZ)) == nil)
  1355. error(Enomem);
  1356. bp->free = i82563rbfree;
  1357. freeb(bp);
  1358. }
  1359. nrbfull = 0;
  1360. ctlr->edev = edev; /* point back to Ether* */
  1361. ctlr->attached = 1;
  1362. initmark(&ctlr->wmrb, Nrb, "rcv bufs unprocessed");
  1363. initmark(&ctlr->wmrd, Nrd-1, "rcv descrs processed at once");
  1364. initmark(&ctlr->wmtd, Ntd-1, "xmit descr queue len");
  1365. snprint(name, sizeof name, "#l%dl", edev->ctlrno);
  1366. kproc(name, i82563lproc, edev);
  1367. snprint(name, sizeof name, "#l%dr", edev->ctlrno);
  1368. kproc(name, i82563rproc, edev);
  1369. snprint(name, sizeof name, "#l%dt", edev->ctlrno);
  1370. kproc(name, i82563tproc, edev);
  1371. i82563txinit(ctlr);
  1372. qunlock(&ctlr->alock);
  1373. poperror();
  1374. }
  1375. static void
  1376. i82563interrupt(Ureg*_, void* arg)
  1377. {
  1378. Ctlr *ctlr;
  1379. Ether *edev;
  1380. int icr, im, i, loops;
  1381. edev = arg;
  1382. ctlr = edev->ctlr;
  1383. ilock(&ctlr->imlock);
  1384. csr32w(ctlr, Imc, ~0);
  1385. im = ctlr->im;
  1386. loops = 0;
  1387. i = Nrd; /* don't livelock */
  1388. for(icr = csr32r(ctlr, Icr); icr & ctlr->im && i-- > 0;
  1389. icr = csr32r(ctlr, Icr)){
  1390. loops++;
  1391. if(icr & Lsc){
  1392. im &= ~Lsc;
  1393. ctlr->lim = icr & Lsc;
  1394. wakeup(&ctlr->lrendez);
  1395. ctlr->lintr++;
  1396. }
  1397. if(icr & (Rxt0|Rxo|Rxdmt0|Rxseq|Ack)){
  1398. ctlr->rim = icr & (Rxt0|Rxo|Rxdmt0|Rxseq|Ack);
  1399. im &= ~(Rxt0|Rxo|Rxdmt0|Rxseq|Ack);
  1400. wakeup(&ctlr->rrendez);
  1401. ctlr->rintr++;
  1402. }
  1403. if(icr & Txdw){
  1404. im &= ~Txdw;
  1405. ctlr->tintr++;
  1406. wakeup(&ctlr->trendez);
  1407. }
  1408. }
  1409. ctlr->im = im;
  1410. csr32w(ctlr, Ims, im);
  1411. iunlock(&ctlr->imlock);
  1412. }
  1413. /* assume misrouted interrupts and check all controllers */
  1414. static void
  1415. i82575interrupt(Ureg*_1, void *_2)
  1416. {
  1417. Ctlr *ctlr;
  1418. for (ctlr = i82563ctlrhead; ctlr != nil && ctlr->edev != nil;
  1419. ctlr = ctlr->next)
  1420. i82563interrupt(nil, ctlr->edev);
  1421. }
  1422. static int
  1423. i82563detach0(Ctlr* ctlr)
  1424. {
  1425. int r, timeo;
  1426. /*
  1427. * Perform a device reset to get the chip back to the
  1428. * power-on state, followed by an EEPROM reset to read
  1429. * the defaults for some internal registers.
  1430. */
  1431. csr32w(ctlr, Imc, ~0);
  1432. csr32w(ctlr, Rctl, 0);
  1433. csr32w(ctlr, Tctl, 0);
  1434. delay(10);
  1435. /*
  1436. * Balance Rx/Tx packet buffer.
  1437. * No need to set PBA register unless using jumbo, defaults to 32KB
  1438. * for receive. If it is changed, then have to do a MAC reset,
  1439. * and need to do that at the the right time as it will wipe stuff.
  1440. */
  1441. ctlr->pba = csr32r(ctlr, Pba);
  1442. /* set packet buffer size if present. no effect until soft reset. */
  1443. switch (ctlr->type) {
  1444. case i82566:
  1445. case i82567:
  1446. case i217:
  1447. ctlr->pbs = 16; /* in KB */
  1448. csr32w(ctlr, Pbs, ctlr->pbs);
  1449. break;
  1450. case i218:
  1451. // after pxe or 9fat boot, pba is always 0xe0012 on i218 => 32K
  1452. ctlr->pbs = (ctlr->pba >> 16) + (uint16_t)ctlr->pba;
  1453. csr32w(ctlr, Pbs, ctlr->pbs);
  1454. break;
  1455. }
  1456. r = csr32r(ctlr, Ctrl);
  1457. if(ctlr->type == i82566 || ctlr->type == i82567 || ctlr->type == i82579)
  1458. r |= Phyrst;
  1459. csr32w(ctlr, Ctrl, Devrst | r);
  1460. delay(1);
  1461. for(timeo = 0; timeo < 1000; timeo++){
  1462. if(!(csr32r(ctlr, Ctrl) & Devrst))
  1463. break;
  1464. delay(1);
  1465. }
  1466. if(csr32r(ctlr, Ctrl) & Devrst)
  1467. return -1;
  1468. r = csr32r(ctlr, Ctrlext);
  1469. csr32w(ctlr, Ctrlext, r|Eerst);
  1470. delay(1);
  1471. for(timeo = 0; timeo < 1000; timeo++){
  1472. if(!(csr32r(ctlr, Ctrlext) & Eerst))
  1473. break;
  1474. delay(1);
  1475. }
  1476. if(csr32r(ctlr, Ctrlext) & Eerst)
  1477. return -1;
  1478. csr32w(ctlr, Imc, ~0);
  1479. delay(1);
  1480. for(timeo = 0; timeo < 1000; timeo++){
  1481. if(!csr32r(ctlr, Icr))
  1482. break;
  1483. delay(1);
  1484. }
  1485. if(csr32r(ctlr, Icr))
  1486. return -1;
  1487. csr32w(ctlr, Ctrl, Slu | csr32r(ctlr, Ctrl));
  1488. return 0;
  1489. }
  1490. static int
  1491. i82563detach(Ctlr* ctlr)
  1492. {
  1493. int r;
  1494. static Lock detlck;
  1495. ilock(&detlck);
  1496. r = i82563detach0(ctlr);
  1497. iunlock(&detlck);
  1498. return r;
  1499. }
  1500. static void
  1501. i82563shutdown(Ether* ether)
  1502. {
  1503. i82563detach(ether->ctlr);
  1504. }
  1505. static uint16_t
  1506. eeread(Ctlr *ctlr, int adr)
  1507. {
  1508. uint32_t n;
  1509. csr32w(ctlr, Eerd, EEstart | adr << 2);
  1510. for (n = 1000000; (csr32r(ctlr, Eerd) & EEdone) == 0 && n-- > 0; )
  1511. ;
  1512. if (n == 0)
  1513. panic("i82563: eeread stuck");
  1514. return csr32r(ctlr, Eerd) >> 16;
  1515. }
  1516. /* load eeprom into ctlr */
  1517. static int
  1518. eeload(Ctlr *ctlr)
  1519. {
  1520. uint16_t sum;
  1521. int data, adr;
  1522. sum = 0;
  1523. for (adr = 0; adr < 0x40; adr++) {
  1524. data = eeread(ctlr, adr);
  1525. ctlr->eeprom[adr] = data;
  1526. sum += data;
  1527. }
  1528. return sum;
  1529. }
  1530. static int
  1531. fcycle(Ctlr *_, Flash *f)
  1532. {
  1533. uint16_t s, i;
  1534. s = f->reg[Fsts];
  1535. if((s&Fvalid) == 0)
  1536. return -1;
  1537. f->reg[Fsts] |= Fcerr | Ael;
  1538. for(i = 0; i < 10; i++){
  1539. if((s&Scip) == 0) /* spi cycle done? */
  1540. return 0;
  1541. delay(1);
  1542. s = f->reg[Fsts];
  1543. }
  1544. return -1;
  1545. }
  1546. static int
  1547. fread(Ctlr *ctlr, Flash *f, int ladr)
  1548. {
  1549. uint16_t s;
  1550. uint32_t n;
  1551. delay(1);
  1552. if(fcycle(ctlr, f) == -1)
  1553. return -1;
  1554. f->reg[Fsts] |= Fdone;
  1555. f->reg32[Faddr] = ladr;
  1556. /* setup flash control register */
  1557. s = f->reg[Fctl] & ~(0x1f << 8);
  1558. s |= (2-1) << 8; /* 2 bytes */
  1559. s &= ~(2*Flcycle); /* read */
  1560. f->reg[Fctl] = s | Fgo;
  1561. for (n = 1000000; (f->reg[Fsts] & Fdone) == 0 && n-- > 0; )
  1562. ;
  1563. if (n == 0)
  1564. panic("i82563: fread stuck");
  1565. if(f->reg[Fsts] & (Fcerr|Ael))
  1566. return -1;
  1567. return f->reg32[Fdata] & 0xffff;
  1568. }
  1569. /* load flash into ctlr */
  1570. static int
  1571. fload(Ctlr *ctlr)
  1572. {
  1573. uint32_t data, io, r, adr;
  1574. uint16_t sum;
  1575. Flash f;
  1576. io = ctlr->pcidev->mem[1].bar & ~0x0f;
  1577. f.reg = vmap(io, ctlr->pcidev->mem[1].size);
  1578. if(f.reg == nil)
  1579. return -1;
  1580. f.reg32 = (void*)f.reg;
  1581. // FMASK is supposed to be gone by now. What to do?
  1582. #define FMASK(o, w) (((1<<(w))-1)<<(o))
  1583. f.base = f.reg32[Bfpr] & FMASK(0, 13);
  1584. f.lim = (f.reg32[Bfpr]>>16) & FMASK(0, 13);
  1585. if(csr32r(ctlr, Eec) & (1<<22))
  1586. f.base += (f.lim + 1 - f.base) >> 1;
  1587. r = f.base << 12;
  1588. sum = 0;
  1589. for (adr = 0; adr < 0x40; adr++) {
  1590. data = fread(ctlr, &f, r + adr*2);
  1591. if(data == -1)
  1592. break;
  1593. ctlr->eeprom[adr] = data;
  1594. sum += data;
  1595. }
  1596. vunmap(f.reg, ctlr->pcidev->mem[1].size);
  1597. return sum;
  1598. }
  1599. static int
  1600. i82563reset(Ctlr *ctlr)
  1601. {
  1602. int i, r, type;
  1603. if(i82563detach(ctlr)) {
  1604. iprint("82563 reset: detach failed\n");
  1605. return -1;
  1606. }
  1607. type = ctlr->type;
  1608. if (ctlr->ra[Eaddrlen - 1] != 0)
  1609. goto macset;
  1610. switch (type) {
  1611. case i82566:
  1612. case i82567:
  1613. case i82577:
  1614. // case i82578: /* not yet implemented */
  1615. case i82579:
  1616. case i217:
  1617. case i218:
  1618. r = fload(ctlr);
  1619. break;
  1620. default:
  1621. r = eeload(ctlr);
  1622. break;
  1623. }
  1624. if (r != 0 && r != 0xBABA){
  1625. print("%s: bad EEPROM checksum - %#.4ux\n",
  1626. tname[type], r);
  1627. return -1;
  1628. }
  1629. /* set mac addr */
  1630. for(i = 0; i < Eaddrlen/2; i++){
  1631. ctlr->ra[2*i] = ctlr->eeprom[Ea+i];
  1632. ctlr->ra[2*i+1] = ctlr->eeprom[Ea+i] >> 8;
  1633. }
  1634. /* ea ctlr[1] = ea ctlr[0]+1 */
  1635. ctlr->ra[5] += (csr32r(ctlr, Status) & Lanid) >> 2;
  1636. /*
  1637. * zero other mac addresses.`
  1638. * AV bits should be zeroed by master reset & there may only be 11
  1639. * other registers on e.g., the i217.
  1640. */
  1641. for(i = 1; i < 12; i++){ /* `12' used to be `16' here */
  1642. csr32w(ctlr, Ral+i*8, 0);
  1643. csr32w(ctlr, Rah+i*8, 0);
  1644. }
  1645. memset(ctlr->mta, 0, sizeof(ctlr->mta));
  1646. macset:
  1647. csr32w(ctlr, Ral, ctlr->ra[3]<<24 | ctlr->ra[2]<<16 | ctlr->ra[1]<<8 |
  1648. ctlr->ra[0]); /* low mac addr */
  1649. /* address valid | high mac addr */
  1650. csr32w(ctlr, Rah, 0x80000000 | ctlr->ra[5]<<8 | ctlr->ra[4]);
  1651. /* populate multicast table */
  1652. for(i = 0; i < mcasttblsize(ctlr); i++)
  1653. csr32w(ctlr, Mta + i*4, ctlr->mta[i]);
  1654. /*
  1655. * Does autonegotiation affect this manual setting?
  1656. * The correct values here should depend on the PBA value
  1657. * and maximum frame length, no?
  1658. */
  1659. /* fixed flow control ethernet address 0x0180c2000001 */
  1660. csr32w(ctlr, Fcal, 0x00C28001);
  1661. csr32w(ctlr, Fcah, 0x0100);
  1662. if (type != i82579 && type != i210 && type != i217 && type != i218)
  1663. /* flow control type, dictated by Intel */
  1664. csr32w(ctlr, Fct, 0x8808);
  1665. csr32w(ctlr, Fcttv, 0x0100); /* for XOFF frame */
  1666. // ctlr->fcrtl = 0x00002000; /* rcv low water mark: 8KB */
  1667. /* rcv high water mark: 16KB, < rcv buffer in PBA & RXA */
  1668. // ctlr->fcrth = 0x00004000;
  1669. ctlr->fcrtl = ctlr->fcrth = 0;
  1670. csr32w(ctlr, Fcrtl, ctlr->fcrtl);
  1671. csr32w(ctlr, Fcrth, ctlr->fcrth);
  1672. return 0;
  1673. }
  1674. static void
  1675. i82563pci(void)
  1676. {
  1677. int type;
  1678. uint32_t io;
  1679. void *mem;
  1680. Pcidev *p;
  1681. Ctlr *ctlr;
  1682. p = nil;
  1683. while(p = pcimatch(p, 0x8086, 0)){
  1684. switch(p->did){
  1685. default:
  1686. continue;
  1687. case 0x1096:
  1688. case 0x10ba:
  1689. type = i82563;
  1690. break;
  1691. case 0x1049: /* mm */
  1692. case 0x104a: /* dm */
  1693. case 0x104b: /* dc */
  1694. case 0x104d: /* mc */
  1695. case 0x10bd: /* dm */
  1696. case 0x294c: /* dc-2 */
  1697. type = i82566;
  1698. break;
  1699. case 0x10cd: /* lf */
  1700. case 0x10ce: /* v-2 */
  1701. case 0x10de: /* lm-3 */
  1702. case 0x10f5: /* lm-2 */
  1703. type = i82567;
  1704. break;
  1705. case 0x10a4:
  1706. case 0x105e:
  1707. type = i82571;
  1708. break;
  1709. case 0x107d: /* eb copper */
  1710. case 0x107e: /* ei fiber */
  1711. case 0x107f: /* ei */
  1712. case 0x10b9: /* sic, 82572gi */
  1713. type = i82572;
  1714. break;
  1715. case 0x108b: /* v */
  1716. case 0x108c: /* e (iamt) */
  1717. case 0x109a: /* l */
  1718. type = i82573;
  1719. break;
  1720. case 0x10d3: /* l */
  1721. type = i82574;
  1722. break;
  1723. case 0x10a7: /* 82575eb: one of a pair of controllers */
  1724. type = i82575;
  1725. break;
  1726. case 0x10c9: /* 82576 copper */
  1727. case 0x10e6: /* 82576 fiber */
  1728. case 0x10e7: /* 82576 serdes */
  1729. type = i82576;
  1730. break;
  1731. case 0x10ea: /* 82577lm */
  1732. type = i82577;
  1733. break;
  1734. case 0x1502: /* 82579lm */
  1735. case 0x1503: /* 82579v */
  1736. type = i82579;
  1737. break;
  1738. case 0x1533: /* i210-t1 */
  1739. case 0x1534: /* i210 */
  1740. case 0x1536: /* i210-fiber */
  1741. case 0x1537: /* i210-backplane */
  1742. case 0x1538:
  1743. case 0x1539: /* i211 */
  1744. case 0x157b: /* i210 */
  1745. case 0x157c: /* i210 */
  1746. type = i210;
  1747. break;
  1748. case 0x153a: /* i217-lm */
  1749. case 0x153b: /* i217-v */
  1750. type = i217;
  1751. break;
  1752. case 0x15a3: /* i218 */
  1753. type = i218;
  1754. break;
  1755. }
  1756. io = p->mem[0].bar & ~0x0F;
  1757. mem = vmap(io, p->mem[0].size);
  1758. if(mem == nil){
  1759. print("%s: can't map %.8lux\n", tname[type], io);
  1760. continue;
  1761. }
  1762. ctlr = malloc(sizeof(Ctlr));
  1763. if(ctlr == nil) {
  1764. vunmap(mem, p->mem[0].size);
  1765. error(Enomem);
  1766. }
  1767. ctlr->port = io;
  1768. ctlr->pcidev = p;
  1769. ctlr->type = type;
  1770. ctlr->nic = mem;
  1771. ctlr->phynum = -1; /* not yet known */
  1772. if(i82563reset(ctlr)){
  1773. vunmap(mem, p->mem[0].size);
  1774. free(ctlr);
  1775. continue;
  1776. }
  1777. pcisetbme(p);
  1778. if(i82563ctlrhead != nil)
  1779. i82563ctlrtail->next = ctlr;
  1780. else
  1781. i82563ctlrhead = ctlr;
  1782. i82563ctlrtail = ctlr;
  1783. }
  1784. }
  1785. static int
  1786. pnp(Ether* edev, int type)
  1787. {
  1788. Ctlr *ctlr;
  1789. static int done;
  1790. if(!done) {
  1791. i82563pci();
  1792. done = 1;
  1793. }
  1794. /*
  1795. * Any adapter matches if no edev->port is supplied,
  1796. * otherwise the ports must match.
  1797. */
  1798. for(ctlr = i82563ctlrhead; ctlr != nil; ctlr = ctlr->next){
  1799. if(ctlr->active)
  1800. continue;
  1801. if(type != Iany && ctlr->type != type)
  1802. continue;
  1803. if(edev->ISAConf.port == 0 || edev->ISAConf.port == ctlr->port){
  1804. ctlr->active = 1;
  1805. break;
  1806. }
  1807. }
  1808. if(ctlr == nil)
  1809. return -1;
  1810. edev->ctlr = ctlr;
  1811. ctlr->edev = edev; /* point back to Ether* */
  1812. edev->ISAConf.port = ctlr->port;
  1813. edev->ISAConf.irq = ctlr->pcidev->intl;
  1814. edev->tbdf = ctlr->pcidev->tbdf;
  1815. edev->Netif.mbps = 1000;
  1816. edev->Netif.maxmtu = ETHERMAXTU;
  1817. memmove(edev->ea, ctlr->ra, Eaddrlen);
  1818. /*
  1819. * Linkage to the generic ethernet driver.
  1820. */
  1821. edev->attach = i82563attach;
  1822. edev->transmit = i82563transmit;
  1823. edev->interrupt = (ctlr->type == i82575?
  1824. i82575interrupt: i82563interrupt);
  1825. edev->ifstat = i82563ifstat;
  1826. edev->ctl = i82563ctl;
  1827. edev->Netif.arg = edev;
  1828. edev->Netif.promiscuous = i82563promiscuous;
  1829. edev->shutdown = i82563shutdown;
  1830. edev->Netif.multicast = i82563multicast;
  1831. return 0;
  1832. }
  1833. static int
  1834. anypnp(Ether *e)
  1835. {
  1836. return pnp(e, Iany);
  1837. }
  1838. static int
  1839. i82563pnp(Ether *e)
  1840. {
  1841. return pnp(e, i82563);
  1842. }
  1843. static int
  1844. i82566pnp(Ether *e)
  1845. {
  1846. return pnp(e, i82566);
  1847. }
  1848. static int
  1849. i82571pnp(Ether *e)
  1850. {
  1851. return pnp(e, i82571);
  1852. }
  1853. static int
  1854. i82572pnp(Ether *e)
  1855. {
  1856. return pnp(e, i82572);
  1857. }
  1858. static int
  1859. i82573pnp(Ether *e)
  1860. {
  1861. return pnp(e, i82573);
  1862. }
  1863. static int
  1864. i82575pnp(Ether *e)
  1865. {
  1866. return pnp(e, i82575);
  1867. }
  1868. static int
  1869. i82579pnp(Ether *e)
  1870. {
  1871. return pnp(e, i82579);
  1872. }
  1873. static int
  1874. i210pnp(Ether *e)
  1875. {
  1876. return pnp(e, i210);
  1877. }
  1878. static int
  1879. i217pnp(Ether *e)
  1880. {
  1881. return pnp(e, i217);
  1882. }
  1883. static int
  1884. i218pnp(Ether *e)
  1885. {
  1886. return pnp(e, i218);
  1887. }
  1888. void
  1889. ether82563link(void)
  1890. {
  1891. /* recognise lots of model numbers for debugging assistance */
  1892. addethercard("i82563", i82563pnp);
  1893. addethercard("i82566", i82566pnp);
  1894. addethercard("i82571", i82571pnp);
  1895. addethercard("i82572", i82572pnp);
  1896. addethercard("i82573", i82573pnp);
  1897. addethercard("i82575", i82575pnp);
  1898. addethercard("i82579", i82579pnp);
  1899. addethercard("i210", i210pnp);
  1900. addethercard("i217", i217pnp);
  1901. addethercard("i218", i218pnp);
  1902. addethercard("igbepcie", anypnp);
  1903. }