ether2114x.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Digital Semiconductor DECchip 21140 PCI Fast Ethernet LAN Controller
  11. * as found on the Digital Fast EtherWORKS PCI 10/100 adapter (DE-500-X).
  12. * To do:
  13. * thresholds;
  14. * ring sizing;
  15. * handle more error conditions;
  16. * all the rest of it...
  17. */
  18. #include "u.h"
  19. #include "lib.h"
  20. #include "mem.h"
  21. #include "dat.h"
  22. #include "fns.h"
  23. #include "io.h"
  24. #include "etherif.h"
  25. #define DEBUG (0)
  26. #define debug if(DEBUG)print
  27. enum {
  28. Nrde = 32,
  29. Ntde = 4,
  30. };
  31. #define Rbsz ROUNDUP(sizeof(Etherpkt)+4, 4)
  32. enum { /* CRS0 - Bus Mode */
  33. Swr = 0x00000001, /* Software Reset */
  34. Bar = 0x00000002, /* Bus Arbitration */
  35. Dsl = 0x0000007C, /* Descriptor Skip Length (field) */
  36. Ble = 0x00000080, /* Big/Little Endian */
  37. Pbl = 0x00003F00, /* Programmable Burst Length (field) */
  38. Cal = 0x0000C000, /* Cache Alignment (field) */
  39. Cal8 = 0x00004000, /* 8 longword boundary alignment */
  40. Cal16 = 0x00008000, /* 16 longword boundary alignment */
  41. Cal32 = 0x0000C000, /* 32 longword boundary alignment */
  42. Tap = 0x000E0000, /* Transmit Automatic Polling (field) */
  43. Dbo = 0x00100000, /* Descriptor Byte Ordering Mode */
  44. Rml = 0x00200000, /* Read Multiple */
  45. };
  46. enum { /* CSR[57] - Status and Interrupt Enable */
  47. Ti = 0x00000001, /* Transmit Interrupt */
  48. Tps = 0x00000002, /* Transmit Process Stopped */
  49. Tu = 0x00000004, /* Transmit buffer Unavailable */
  50. Tjt = 0x00000008, /* Transmit Jabber Timeout */
  51. Unf = 0x00000020, /* transmit UNderFlow */
  52. Ri = 0x00000040, /* Receive Interrupt */
  53. Ru = 0x00000080, /* Receive buffer Unavailable */
  54. Rps = 0x00000100, /* Receive Process Stopped */
  55. Rwt = 0x00000200, /* Receive Watchdog Timeout */
  56. Eti = 0x00000400, /* Early Transmit Interrupt */
  57. Gte = 0x00000800, /* General purpose Timer Expired */
  58. Fbe = 0x00002000, /* Fatal Bit Error */
  59. Ais = 0x00008000, /* Abnormal Interrupt Summary */
  60. Nis = 0x00010000, /* Normal Interrupt Summary */
  61. Rs = 0x000E0000, /* Receive process State (field) */
  62. Ts = 0x00700000, /* Transmit process State (field) */
  63. Eb = 0x03800000, /* Error bits */
  64. };
  65. enum { /* CSR6 - Operating Mode */
  66. Hp = 0x00000001, /* Hash/Perfect receive filtering mode */
  67. Sr = 0x00000002, /* Start/stop Receive */
  68. Ho = 0x00000004, /* Hash-Only filtering mode */
  69. Pb = 0x00000008, /* Pass Bad frames */
  70. If = 0x00000010, /* Inverse Filtering */
  71. Sb = 0x00000020, /* Start/stop Backoff counter */
  72. Pr = 0x00000040, /* Promiscuous Mode */
  73. Pm = 0x00000080, /* Pass all Multicast */
  74. Fd = 0x00000200, /* Full Duplex mode */
  75. Om = 0x00000C00, /* Operating Mode (field) */
  76. Fc = 0x00001000, /* Force Collision */
  77. St = 0x00002000, /* Start/stop Transmission Command */
  78. Tr = 0x0000C000, /* ThReshold control bits (field) */
  79. Tr128 = 0x00000000,
  80. Tr256 = 0x00004000,
  81. Tr512 = 0x00008000,
  82. Tr1024 = 0x0000C000,
  83. Ca = 0x00020000, /* CApture effect enable */
  84. Ps = 0x00040000, /* Port Select */
  85. Hbd = 0x00080000, /* HeartBeat Disable */
  86. Imm = 0x00100000, /* IMMediate mode */
  87. Sf = 0x00200000, /* Store and Forward */
  88. Ttm = 0x00400000, /* Transmit Threshold Mode */
  89. Pcs = 0x00800000, /* PCS function */
  90. Scr = 0x01000000, /* SCRambler mode */
  91. Mbo = 0x02000000, /* Must Be One */
  92. Ra = 0x40000000, /* Receive All */
  93. Sc = 0x80000000, /* Special Capture effect enable */
  94. TrMODE = Tr512, /* default transmission threshold */
  95. };
  96. enum { /* CSR9 - ROM and MII Management */
  97. Scs = 0x00000001, /* serial ROM chip select */
  98. Sclk = 0x00000002, /* serial ROM clock */
  99. Sdi = 0x00000004, /* serial ROM data in */
  100. Sdo = 0x00000008, /* serial ROM data out */
  101. Ss = 0x00000800, /* serial ROM select */
  102. Wr = 0x00002000, /* write */
  103. Rd = 0x00004000, /* read */
  104. Mdc = 0x00010000, /* MII management clock */
  105. Mdo = 0x00020000, /* MII management write data */
  106. Mii = 0x00040000, /* MII management operation mode (W) */
  107. Mdi = 0x00080000, /* MII management data in */
  108. };
  109. enum { /* CSR12 - General-Purpose Port */
  110. Gpc = 0x00000100, /* General Purpose Control */
  111. };
  112. typedef struct Des {
  113. int status;
  114. int control;
  115. uint32_t addr;
  116. void* bp;
  117. } Des;
  118. enum { /* status */
  119. Of = 0x00000001, /* Rx: OverFlow */
  120. Ce = 0x00000002, /* Rx: CRC Error */
  121. Db = 0x00000004, /* Rx: Dribbling Bit */
  122. Re = 0x00000008, /* Rx: Report on MII Error */
  123. Rw = 0x00000010, /* Rx: Receive Watchdog */
  124. Ft = 0x00000020, /* Rx: Frame Type */
  125. Cs = 0x00000040, /* Rx: Collision Seen */
  126. Tl = 0x00000080, /* Rx: Frame too Long */
  127. Ls = 0x00000100, /* Rx: Last deScriptor */
  128. Fs = 0x00000200, /* Rx: First deScriptor */
  129. Mf = 0x00000400, /* Rx: Multicast Frame */
  130. Rf = 0x00000800, /* Rx: Runt Frame */
  131. Dt = 0x00003000, /* Rx: Data Type (field) */
  132. De = 0x00004000, /* Rx: Descriptor Error */
  133. Fl = 0x3FFF0000, /* Rx: Frame Length (field) */
  134. Ff = 0x40000000, /* Rx: Filtering Fail */
  135. Def = 0x00000001, /* Tx: DEFerred */
  136. Uf = 0x00000002, /* Tx: UnderFlow error */
  137. Lf = 0x00000004, /* Tx: Link Fail report */
  138. Cc = 0x00000078, /* Tx: Collision Count (field) */
  139. Hf = 0x00000080, /* Tx: Heartbeat Fail */
  140. Ec = 0x00000100, /* Tx: Excessive Collisions */
  141. Lc = 0x00000200, /* Tx: Late Collision */
  142. Nc = 0x00000400, /* Tx: No Carrier */
  143. Lo = 0x00000800, /* Tx: LOss of carrier */
  144. To = 0x00004000, /* Tx: Transmission jabber timeOut */
  145. Es = 0x00008000, /* [RT]x: Error Summary */
  146. Own = 0x80000000, /* [RT]x: OWN bit */
  147. };
  148. enum { /* control */
  149. Bs1 = 0x000007FF, /* [RT]x: Buffer 1 Size */
  150. Bs2 = 0x003FF800, /* [RT]x: Buffer 2 Size */
  151. Ch = 0x01000000, /* [RT]x: second address CHained */
  152. Er = 0x02000000, /* [RT]x: End of Ring */
  153. Ft0 = 0x00400000, /* Tx: Filtering Type 0 */
  154. Dpd = 0x00800000, /* Tx: Disabled PaDding */
  155. Ac = 0x04000000, /* Tx: Add CRC disable */
  156. Set = 0x08000000, /* Tx: SETup packet */
  157. Ft1 = 0x10000000, /* Tx: Filtering Type 1 */
  158. Fseg = 0x20000000, /* Tx: First SEGment */
  159. Lseg = 0x40000000, /* Tx: Last SEGment */
  160. Ic = 0x80000000, /* Tx: Interrupt on Completion */
  161. };
  162. enum { /* PHY registers */
  163. Bmcr = 0, /* Basic Mode Control */
  164. Bmsr = 1, /* Basic Mode Status */
  165. Phyidr1 = 2, /* PHY Identifier #1 */
  166. Phyidr2 = 3, /* PHY Identifier #2 */
  167. Anar = 4, /* Auto-Negotiation Advertisment */
  168. Anlpar = 5, /* Auto-Negotiation Link Partner Ability */
  169. Aner = 6, /* Auto-Negotiation Expansion */
  170. };
  171. enum { /* Variants */
  172. Tulip0 = (0x0009<<16)|0x1011,
  173. Tulip1 = (0x0014<<16)|0x1011,
  174. Tulip3 = (0x0019<<16)|0x1011,
  175. Pnic = (0x0002<<16)|0x11AD,
  176. Pnic2 = (0xC115<<16)|0x11AD,
  177. CentaurP = (0x0985<<16)|0x1317,
  178. CentaurPcb = (0x1985<<16)|0x1317,
  179. };
  180. typedef struct Ctlr Ctlr;
  181. typedef struct Ctlr {
  182. int port;
  183. Pcidev* pcidev;
  184. Ctlr* next;
  185. int active;
  186. int id; /* (pcidev->did<<16)|pcidev->vid */
  187. uint8_t *srom;
  188. int sromsz;
  189. uint8_t* sromea; /* MAC address */
  190. uint8_t* leaf;
  191. int sct; /* selected connection type */
  192. int k; /* info block count */
  193. uint8_t* infoblock[16];
  194. int sctk; /* sct block index */
  195. int curk; /* current block index */
  196. uint8_t* type5block;
  197. int phy[32]; /* logical to physical map */
  198. int phyreset; /* reset bitmap */
  199. int curphyad;
  200. int fdx;
  201. int ttm;
  202. uint8_t fd; /* option */
  203. int medium; /* option */
  204. int csr6; /* CSR6 - operating mode */
  205. int mask; /* CSR[57] - interrupt mask */
  206. int mbps;
  207. Des* rdr; /* receive descriptor ring */
  208. int nrdr; /* size of rdr */
  209. int rdrx; /* index into rdr */
  210. Des* tdr; /* transmit descriptor ring */
  211. int ntdr; /* size of tdr */
  212. int tdrh; /* host index into tdr */
  213. int tdri; /* interface index into tdr */
  214. int ntq; /* descriptors active */
  215. Block* setupbp;
  216. uint32_t of; /* receive statistics */
  217. uint32_t ce;
  218. uint32_t cs;
  219. uint32_t tl;
  220. uint32_t rf;
  221. uint32_t de;
  222. uint32_t uf; /* transmit statistics */
  223. uint32_t ec;
  224. uint32_t lc;
  225. uint32_t nc;
  226. uint32_t lo;
  227. uint32_t to;
  228. } Ctlr;
  229. static Ctlr* ctlrhead;
  230. static Ctlr* ctlrtail;
  231. #define csr32r(c, r) (inl((c)->port+((r)*8)))
  232. #define csr32w(c, r, l) (outl((c)->port+((r)*8), (uint32_t)(l)))
  233. static void
  234. attach(Ether* ether)
  235. {
  236. Ctlr *ctlr;
  237. ctlr = ether->ctlr;
  238. if(!(ctlr->csr6 & Sr)){
  239. ctlr->csr6 |= Sr;
  240. csr32w(ctlr, 6, ctlr->csr6);
  241. }
  242. }
  243. static void
  244. transmit(Ether* ether)
  245. {
  246. Ctlr *ctlr;
  247. Block *bp;
  248. Des *des;
  249. int control;
  250. RingBuf *tb;
  251. ctlr = ether->ctlr;
  252. while(ctlr->ntq < (ctlr->ntdr-1)){
  253. if(ctlr->setupbp){
  254. bp = ctlr->setupbp;
  255. ctlr->setupbp = 0;
  256. control = Ic|Set|BLEN(bp);
  257. }
  258. else{
  259. if(ether->ntb == 0)
  260. break;
  261. tb = &ether->tb[ether->ti];
  262. if(tb->owner != Interface)
  263. break;
  264. bp = allocb(tb->len);
  265. memmove(bp->wp, tb->pkt, tb->len);
  266. memmove(bp->wp+Eaddrlen, ether->ea, Eaddrlen);
  267. bp->wp += tb->len;
  268. tb->owner = Host;
  269. ether->ti = NEXT(ether->ti, ether->ntb);
  270. control = Ic|Lseg|Fseg|BLEN(bp);
  271. }
  272. ctlr->tdr[PREV(ctlr->tdrh, ctlr->ntdr)].control &= ~Ic;
  273. des = &ctlr->tdr[ctlr->tdrh];
  274. des->bp = bp;
  275. des->addr = PADDR(bp->rp);
  276. des->control |= control;
  277. ctlr->ntq++;
  278. //coherence();
  279. des->status = Own;
  280. csr32w(ctlr, 1, 0);
  281. ctlr->tdrh = NEXT(ctlr->tdrh, ctlr->ntdr);
  282. }
  283. }
  284. static void
  285. interrupt(Ureg*, void* arg)
  286. {
  287. Ctlr *ctlr;
  288. Ether *ether;
  289. int len, status;
  290. Des *des;
  291. RingBuf *ring;
  292. ether = arg;
  293. ctlr = ether->ctlr;
  294. while((status = csr32r(ctlr, 5)) & (Nis|Ais)){
  295. /*
  296. * Acknowledge the interrupts and mask-out
  297. * the ones that are implicitly handled.
  298. */
  299. csr32w(ctlr, 5, status);
  300. status &= (ctlr->mask & ~(Nis|Ais|Ti));
  301. /*
  302. * Received packets.
  303. */
  304. if(status & Ri){
  305. des = &ctlr->rdr[ctlr->rdrx];
  306. while((des->status & Own) == 0){
  307. len = ((des->status & Fl)>>16)-4;
  308. if(des->status & Es){
  309. if(des->status & Of)
  310. ctlr->of++;
  311. if(des->status & Ce)
  312. ctlr->ce++;
  313. if(des->status & Cs)
  314. ctlr->cs++;
  315. if(des->status & Tl)
  316. ctlr->tl++;
  317. if(des->status & Rf)
  318. ctlr->rf++;
  319. if(des->status & De)
  320. ctlr->de++;
  321. }
  322. else{
  323. ring = &ether->rb[ether->ri];
  324. if(ring->owner == Interface){
  325. ring->owner = Host;
  326. ring->len = len;
  327. memmove(ring->pkt, des->bp, len);
  328. ether->ri = NEXT(ether->ri, ether->nrb);
  329. }
  330. }
  331. des->control &= Er;
  332. des->control |= Rbsz;
  333. des->status = Own;
  334. ctlr->rdrx = NEXT(ctlr->rdrx, ctlr->nrdr);
  335. des = &ctlr->rdr[ctlr->rdrx];
  336. }
  337. status &= ~Ri;
  338. }
  339. /*
  340. * Check the transmit side:
  341. * check for Transmit Underflow and Adjust
  342. * the threshold upwards;
  343. * free any transmitted buffers and try to
  344. * top-up the ring.
  345. */
  346. if(status & Unf){
  347. csr32w(ctlr, 6, ctlr->csr6 & ~St);
  348. switch(ctlr->csr6 & Tr){
  349. case Tr128:
  350. len = Tr256;
  351. break;
  352. case Tr256:
  353. len = Tr512;
  354. break;
  355. case Tr512:
  356. len = Tr1024;
  357. break;
  358. default:
  359. case Tr1024:
  360. len = Sf;
  361. break;
  362. }
  363. ctlr->csr6 = (ctlr->csr6 & ~Tr)|len;
  364. csr32w(ctlr, 6, ctlr->csr6);
  365. csr32w(ctlr, 5, Tps);
  366. status &= ~(Unf|Tps);
  367. }
  368. while(ctlr->ntq){
  369. des = &ctlr->tdr[ctlr->tdri];
  370. if(des->status & Own)
  371. break;
  372. if(des->status & Es){
  373. if(des->status & Uf)
  374. ctlr->uf++;
  375. if(des->status & Ec)
  376. ctlr->ec++;
  377. if(des->status & Lc)
  378. ctlr->lc++;
  379. if(des->status & Nc)
  380. ctlr->nc++;
  381. if(des->status & Lo)
  382. ctlr->lo++;
  383. if(des->status & To)
  384. ctlr->to++;
  385. }
  386. freeb(des->bp);
  387. des->control &= Er;
  388. ctlr->ntq--;
  389. ctlr->tdri = NEXT(ctlr->tdri, ctlr->ntdr);
  390. }
  391. transmit(ether);
  392. /*
  393. * Anything left not catered for?
  394. */
  395. if(status)
  396. panic("#l%d: status %8.8uX\n", ether->ctlrno, status);
  397. }
  398. }
  399. static void
  400. ctlrinit(Ether* ether)
  401. {
  402. Ctlr *ctlr;
  403. Des *des;
  404. Block *bp;
  405. int i;
  406. uint8_t bi[Eaddrlen*2];
  407. ctlr = ether->ctlr;
  408. /*
  409. * Allocate and initialise the receive ring;
  410. * allocate and initialise the transmit ring;
  411. * unmask interrupts and start the transmit side;
  412. * create and post a setup packet to initialise
  413. * the physical ethernet address.
  414. */
  415. ctlr->rdr = malloc(ctlr->nrdr*sizeof(Des));
  416. for(des = ctlr->rdr; des < &ctlr->rdr[ctlr->nrdr]; des++){
  417. des->bp = malloc(Rbsz);
  418. des->status = Own;
  419. des->control = Rbsz;
  420. des->addr = PADDR(des->bp);
  421. }
  422. ctlr->rdr[ctlr->nrdr-1].control |= Er;
  423. ctlr->rdrx = 0;
  424. csr32w(ctlr, 3, PADDR(ctlr->rdr));
  425. ctlr->tdr = ialloc(ctlr->ntdr*sizeof(Des), 32);
  426. ctlr->tdr[ctlr->ntdr-1].control |= Er;
  427. ctlr->tdrh = 0;
  428. ctlr->tdri = 0;
  429. csr32w(ctlr, 4, PADDR(ctlr->tdr));
  430. /*
  431. * Clear any bits in the Status Register (CSR5) as
  432. * the PNIC has a different reset value from a true 2114x.
  433. */
  434. ctlr->mask = Nis|Ais|Fbe|Rwt|Rps|Ru|Ri|Unf|Tjt|Tps|Ti;
  435. csr32w(ctlr, 5, ctlr->mask);
  436. csr32w(ctlr, 7, ctlr->mask);
  437. ctlr->csr6 |= St;
  438. csr32w(ctlr, 6, ctlr->csr6);
  439. for(i = 0; i < Eaddrlen/2; i++){
  440. bi[i*4] = ether->ea[i*2];
  441. bi[i*4+1] = ether->ea[i*2+1];
  442. bi[i*4+2] = ether->ea[i*2+1];
  443. bi[i*4+3] = ether->ea[i*2];
  444. }
  445. bp = allocb(Eaddrlen*2*16);
  446. memset(bp->rp, 0xFF, sizeof(bi));
  447. for(i = sizeof(bi); i < sizeof(bi)*16; i += sizeof(bi))
  448. memmove(bp->rp+i, bi, sizeof(bi));
  449. bp->wp += sizeof(bi)*16;
  450. ctlr->setupbp = bp;
  451. transmit(ether);
  452. }
  453. static void
  454. csr9w(Ctlr* ctlr, int data)
  455. {
  456. csr32w(ctlr, 9, data);
  457. microdelay(1);
  458. }
  459. static int
  460. miimdi(Ctlr* ctlr, int n)
  461. {
  462. int data, i;
  463. /*
  464. * Read n bits from the MII Management Register.
  465. */
  466. data = 0;
  467. for(i = n-1; i >= 0; i--){
  468. if(csr32r(ctlr, 9) & Mdi)
  469. data |= (1<<i);
  470. csr9w(ctlr, Mii|Mdc);
  471. csr9w(ctlr, Mii);
  472. }
  473. csr9w(ctlr, 0);
  474. return data;
  475. }
  476. static void
  477. miimdo(Ctlr* ctlr, int bits, int n)
  478. {
  479. int i, mdo;
  480. /*
  481. * Write n bits to the MII Management Register.
  482. */
  483. for(i = n-1; i >= 0; i--){
  484. if(bits & (1<<i))
  485. mdo = Mdo;
  486. else
  487. mdo = 0;
  488. csr9w(ctlr, mdo);
  489. csr9w(ctlr, mdo|Mdc);
  490. csr9w(ctlr, mdo);
  491. }
  492. }
  493. static int
  494. miir(Ctlr* ctlr, int phyad, int regad)
  495. {
  496. int data, i;
  497. if(ctlr->id == Pnic){
  498. i = 1000;
  499. csr32w(ctlr, 20, 0x60020000|(phyad<<23)|(regad<<18));
  500. do{
  501. microdelay(1);
  502. data = csr32r(ctlr, 20);
  503. }while((data & 0x80000000) && --i);
  504. if(i == 0)
  505. return -1;
  506. return data & 0xFFFF;
  507. }
  508. /*
  509. * Preamble;
  510. * ST+OP+PHYAD+REGAD;
  511. * TA + 16 data bits.
  512. */
  513. miimdo(ctlr, 0xFFFFFFFF, 32);
  514. miimdo(ctlr, 0x1800|(phyad<<5)|regad, 14);
  515. data = miimdi(ctlr, 18);
  516. if(data & 0x10000)
  517. return -1;
  518. return data & 0xFFFF;
  519. }
  520. static void
  521. miiw(Ctlr* ctlr, int phyad, int regad, int data)
  522. {
  523. /*
  524. * Preamble;
  525. * ST+OP+PHYAD+REGAD+TA + 16 data bits;
  526. * Z.
  527. */
  528. miimdo(ctlr, 0xFFFFFFFF, 32);
  529. data &= 0xFFFF;
  530. data |= (0x05<<(5+5+2+16))|(phyad<<(5+2+16))|(regad<<(2+16))|(0x02<<16);
  531. miimdo(ctlr, data, 32);
  532. csr9w(ctlr, Mdc);
  533. csr9w(ctlr, 0);
  534. }
  535. static int
  536. sromr(Ctlr* ctlr, int r)
  537. {
  538. int i, op, data, size;
  539. if(ctlr->id == Pnic){
  540. i = 1000;
  541. csr32w(ctlr, 19, 0x600|r);
  542. do{
  543. microdelay(1);
  544. data = csr32r(ctlr, 19);
  545. }while((data & 0x80000000) && --i);
  546. if(ctlr->sromsz == 0)
  547. ctlr->sromsz = 6;
  548. return csr32r(ctlr, 9) & 0xFFFF;
  549. }
  550. /*
  551. * This sequence for reading a 16-bit register 'r'
  552. * in the EEPROM is taken (pretty much) straight from Section
  553. * 7.4 of the 21140 Hardware Reference Manual.
  554. */
  555. reread:
  556. csr9w(ctlr, Rd|Ss);
  557. csr9w(ctlr, Rd|Ss|Scs);
  558. csr9w(ctlr, Rd|Ss|Sclk|Scs);
  559. csr9w(ctlr, Rd|Ss);
  560. op = 0x06;
  561. for(i = 3-1; i >= 0; i--){
  562. data = Rd|Ss|(((op>>i) & 0x01)<<2)|Scs;
  563. csr9w(ctlr, data);
  564. csr9w(ctlr, data|Sclk);
  565. csr9w(ctlr, data);
  566. }
  567. /*
  568. * First time through must work out the EEPROM size.
  569. * This doesn't seem to work on the 21041 as implemented
  570. * in Virtual PC for the Mac, so wire any 21041 to 6,
  571. * it's the only 21041 this code will ever likely see.
  572. */
  573. if((size = ctlr->sromsz) == 0){
  574. if(ctlr->id == Tulip1)
  575. ctlr->sromsz = size = 6;
  576. else
  577. size = 8;
  578. }
  579. for(size = size-1; size >= 0; size--){
  580. data = Rd|Ss|(((r>>size) & 0x01)<<2)|Scs;
  581. csr9w(ctlr, data);
  582. csr9w(ctlr, data|Sclk);
  583. csr9w(ctlr, data);
  584. microdelay(1);
  585. if(ctlr->sromsz == 0 && !(csr32r(ctlr, 9) & Sdo))
  586. break;
  587. }
  588. data = 0;
  589. for(i = 16-1; i >= 0; i--){
  590. csr9w(ctlr, Rd|Ss|Sclk|Scs);
  591. if(csr32r(ctlr, 9) & Sdo)
  592. data |= (1<<i);
  593. csr9w(ctlr, Rd|Ss|Scs);
  594. }
  595. csr9w(ctlr, 0);
  596. if(ctlr->sromsz == 0){
  597. ctlr->sromsz = 8-size;
  598. goto reread;
  599. }
  600. return data & 0xFFFF;
  601. }
  602. static void
  603. softreset(Ctlr* ctlr)
  604. {
  605. /*
  606. * Soft-reset the controller and initialise bus mode.
  607. * Delay should be >= 50 PCI cycles (2×S @ 25MHz).
  608. */
  609. csr32w(ctlr, 0, Swr);
  610. microdelay(10);
  611. csr32w(ctlr, 0, Rml|Cal16);
  612. delay(1);
  613. }
  614. static int
  615. type5block(Ctlr* ctlr, uint8_t* block)
  616. {
  617. int csr15, i, len;
  618. /*
  619. * Reset or GPR sequence. Reset should be once only,
  620. * before the GPR sequence.
  621. * Note 'block' is not a pointer to the block head but
  622. * a pointer to the data in the block starting at the
  623. * reset length value so type5block can be used for the
  624. * sequences contained in type 1 and type 3 blocks.
  625. * The SROM docs state the 21140 type 5 block is the
  626. * same as that for the 21143, but the two controllers
  627. * use different registers and sequence-element lengths
  628. * so the 21140 code here is a guess for a real type 5
  629. * sequence.
  630. */
  631. len = *block++;
  632. if(ctlr->id != Tulip3){
  633. for(i = 0; i < len; i++){
  634. csr32w(ctlr, 12, *block);
  635. block++;
  636. }
  637. return len;
  638. }
  639. for(i = 0; i < len; i++){
  640. csr15 = *block++<<16;
  641. csr15 |= *block++<<24;
  642. csr32w(ctlr, 15, csr15);
  643. debug("%8.8uX ", csr15);
  644. }
  645. return 2*len;
  646. }
  647. static int
  648. typephylink(Ctlr* ctlr, uint8_t*)
  649. {
  650. int an, bmcr, bmsr, csr6, x;
  651. /*
  652. * Fail if
  653. * auto-negotiataion enabled but not complete;
  654. * no valid link established.
  655. */
  656. bmcr = miir(ctlr, ctlr->curphyad, Bmcr);
  657. miir(ctlr, ctlr->curphyad, Bmsr);
  658. bmsr = miir(ctlr, ctlr->curphyad, Bmsr);
  659. debug("bmcr 0x%2.2uX bmsr 0x%2.2uX\n", bmcr, bmsr);
  660. if(((bmcr & 0x1000) && !(bmsr & 0x0020)) || !(bmsr & 0x0004))
  661. return 0;
  662. if(bmcr & 0x1000){
  663. an = miir(ctlr, ctlr->curphyad, Anar);
  664. an &= miir(ctlr, ctlr->curphyad, Anlpar) & 0x3E0;
  665. debug("an 0x%2.uX 0x%2.2uX 0x%2.2uX\n",
  666. miir(ctlr, ctlr->curphyad, Anar),
  667. miir(ctlr, ctlr->curphyad, Anlpar),
  668. an);
  669. if(an & 0x0100)
  670. x = 0x4000;
  671. else if(an & 0x0080)
  672. x = 0x2000;
  673. else if(an & 0x0040)
  674. x = 0x1000;
  675. else if(an & 0x0020)
  676. x = 0x0800;
  677. else
  678. x = 0;
  679. }
  680. else if((bmcr & 0x2100) == 0x2100)
  681. x = 0x4000;
  682. else if(bmcr & 0x2000){
  683. /*
  684. * If FD capable, force it if necessary.
  685. */
  686. if((bmsr & 0x4000) && ctlr->fd){
  687. miiw(ctlr, ctlr->curphyad, Bmcr, 0x2100);
  688. x = 0x4000;
  689. }
  690. else
  691. x = 0x2000;
  692. }
  693. else if(bmcr & 0x0100)
  694. x = 0x1000;
  695. else
  696. x = 0x0800;
  697. csr6 = Sc|Mbo|Hbd|Ps|Ca|TrMODE|Sb;
  698. if(ctlr->fdx & x)
  699. csr6 |= Fd;
  700. if(ctlr->ttm & x)
  701. csr6 |= Ttm;
  702. debug("csr6 0x%8.8uX 0x%8.8uX 0x%8.8luX\n",
  703. csr6, ctlr->csr6, csr32r(ctlr, 6));
  704. if(csr6 != ctlr->csr6){
  705. ctlr->csr6 = csr6;
  706. csr32w(ctlr, 6, csr6);
  707. }
  708. return 1;
  709. }
  710. static int
  711. typephymode(Ctlr* ctlr, uint8_t* block, int wait)
  712. {
  713. uint8_t *p;
  714. int len, mc, nway, phyx, timeo;
  715. if(DEBUG){
  716. int i;
  717. len = (block[0] & ~0x80)+1;
  718. for(i = 0; i < len; i++)
  719. debug("%2.2uX ", block[i]);
  720. debug("\n");
  721. }
  722. if(block[1] == 1)
  723. len = 1;
  724. else if(block[1] == 3)
  725. len = 2;
  726. else
  727. return -1;
  728. /*
  729. * Snarf the media capabilities, nway advertisment,
  730. * FDX and TTM bitmaps.
  731. */
  732. p = &block[5+len*block[3]+len*block[4+len*block[3]]];
  733. mc = *p++;
  734. mc |= *p++<<8;
  735. nway = *p++;
  736. nway |= *p++<<8;
  737. ctlr->fdx = *p++;
  738. ctlr->fdx |= *p++<<8;
  739. ctlr->ttm = *p++;
  740. ctlr->ttm |= *p<<8;
  741. debug("mc %4.4uX nway %4.4uX fdx %4.4uX ttm %4.4uX\n",
  742. mc, nway, ctlr->fdx, ctlr->ttm);
  743. USED(mc);
  744. phyx = block[2];
  745. ctlr->curphyad = ctlr->phy[phyx];
  746. ctlr->csr6 = 0;//Sc|Mbo|Hbd|Ps|Ca|TrMODE|Sb;
  747. //csr32w(ctlr, 6, ctlr->csr6);
  748. if(typephylink(ctlr, block))
  749. return 0;
  750. if(!(ctlr->phyreset & (1<<phyx))){
  751. debug("reset seq: len %d: ", block[3]);
  752. if(ctlr->type5block)
  753. type5block(ctlr, &ctlr->type5block[2]);
  754. else
  755. type5block(ctlr, &block[4+len*block[3]]);
  756. debug("\n");
  757. ctlr->phyreset |= (1<<phyx);
  758. }
  759. /*
  760. * GPR sequence.
  761. */
  762. debug("gpr seq: len %d: ", block[3]);
  763. type5block(ctlr, &block[3]);
  764. debug("\n");
  765. ctlr->csr6 = 0;//Sc|Mbo|Hbd|Ps|Ca|TrMODE|Sb;
  766. //csr32w(ctlr, 6, ctlr->csr6);
  767. if(typephylink(ctlr, block))
  768. return 0;
  769. /*
  770. * Turn off auto-negotiation, set the auto-negotiation
  771. * advertisment register then start the auto-negotiation
  772. * process again.
  773. */
  774. miiw(ctlr, ctlr->curphyad, Bmcr, 0);
  775. miiw(ctlr, ctlr->curphyad, Anar, nway|1);
  776. miiw(ctlr, ctlr->curphyad, Bmcr, 0x1000);
  777. if(!wait)
  778. return 0;
  779. for(timeo = 0; timeo < 30; timeo++){
  780. if(typephylink(ctlr, block))
  781. return 0;
  782. delay(100);
  783. }
  784. return -1;
  785. }
  786. static int
  787. typesymmode(Ctlr *ctlr, uint8_t *block, int wait)
  788. {
  789. uint gpmode, gpdata, command;
  790. USED(wait);
  791. gpmode = block[3] | ((uint) block[4] << 8);
  792. gpdata = block[5] | ((uint) block[6] << 8);
  793. command = (block[7] | ((uint) block[8] << 8)) & 0x71;
  794. if (command & 0x8000) {
  795. print("ether2114x.c: FIXME: handle type 4 mode blocks where cmd.active_invalid != 0\n");
  796. return -1;
  797. }
  798. csr32w(ctlr, 15, gpmode);
  799. csr32w(ctlr, 15, gpdata);
  800. ctlr->csr6 = (command & 0x71) << 18;
  801. csr32w(ctlr, 6, ctlr->csr6);
  802. return 0;
  803. }
  804. static int
  805. type2mode(Ctlr* ctlr, uint8_t* block, int)
  806. {
  807. uint8_t *p;
  808. int csr6, csr13, csr14, csr15, gpc, gpd;
  809. csr6 = Sc|Mbo|Ca|TrMODE|Sb;
  810. debug("type2mode: medium 0x%2.2uX\n", block[2]);
  811. /*
  812. * Don't attempt full-duplex
  813. * unless explicitly requested.
  814. */
  815. if((block[2] & 0x3F) == 0x04){ /* 10BASE-TFD */
  816. if(!ctlr->fd)
  817. return -1;
  818. csr6 |= Fd;
  819. }
  820. /*
  821. * Operating mode programming values from the datasheet
  822. * unless media specific data is explicitly given.
  823. */
  824. p = &block[3];
  825. if(block[2] & 0x40){
  826. csr13 = (block[4]<<8)|block[3];
  827. csr14 = (block[6]<<8)|block[5];
  828. csr15 = (block[8]<<8)|block[7];
  829. p += 6;
  830. }
  831. else switch(block[2] & 0x3F){
  832. default:
  833. return -1;
  834. case 0x00: /* 10BASE-T */
  835. csr13 = 0x00000001;
  836. csr14 = 0x00007F3F;
  837. csr15 = 0x00000008;
  838. break;
  839. case 0x01: /* 10BASE-2 */
  840. csr13 = 0x00000009;
  841. csr14 = 0x00000705;
  842. csr15 = 0x00000006;
  843. break;
  844. case 0x02: /* 10BASE-5 (AUI) */
  845. csr13 = 0x00000009;
  846. csr14 = 0x00000705;
  847. csr15 = 0x0000000E;
  848. break;
  849. case 0x04: /* 10BASE-TFD */
  850. csr13 = 0x00000001;
  851. csr14 = 0x00007F3D;
  852. csr15 = 0x00000008;
  853. break;
  854. }
  855. gpc = *p++<<16;
  856. gpc |= *p++<<24;
  857. gpd = *p++<<16;
  858. gpd |= *p<<24;
  859. csr32w(ctlr, 13, 0);
  860. csr32w(ctlr, 14, csr14);
  861. csr32w(ctlr, 15, gpc|csr15);
  862. delay(10);
  863. csr32w(ctlr, 15, gpd|csr15);
  864. csr32w(ctlr, 13, csr13);
  865. ctlr->csr6 = csr6;
  866. csr32w(ctlr, 6, ctlr->csr6);
  867. debug("type2mode: csr13 %8.8uX csr14 %8.8uX csr15 %8.8uX\n",
  868. csr13, csr14, csr15);
  869. debug("type2mode: gpc %8.8uX gpd %8.8uX csr6 %8.8uX\n",
  870. gpc, gpd, csr6);
  871. return 0;
  872. }
  873. static int
  874. type0link(Ctlr* ctlr, uint8_t* block)
  875. {
  876. int m, polarity, sense;
  877. m = (block[3]<<8)|block[2];
  878. sense = 1<<((m & 0x000E)>>1);
  879. if(m & 0x0080)
  880. polarity = sense;
  881. else
  882. polarity = 0;
  883. return (csr32r(ctlr, 12) & sense)^polarity;
  884. }
  885. static int
  886. type0mode(Ctlr* ctlr, uint8_t* block, int wait)
  887. {
  888. int csr6, m, timeo;
  889. csr6 = Sc|Mbo|Hbd|Ca|TrMODE|Sb;
  890. debug("type0: medium 0x%uX, fd %d: 0x%2.2uX 0x%2.2uX 0x%2.2uX 0x%2.2uX\n",
  891. ctlr->medium, ctlr->fd, block[0], block[1], block[2], block[3]);
  892. switch(block[0]){
  893. default:
  894. break;
  895. case 0x04: /* 10BASE-TFD */
  896. case 0x05: /* 100BASE-TXFD */
  897. case 0x08: /* 100BASE-FXFD */
  898. /*
  899. * Don't attempt full-duplex
  900. * unless explicitly requested.
  901. */
  902. if(!ctlr->fd)
  903. return -1;
  904. csr6 |= Fd;
  905. break;
  906. }
  907. m = (block[3]<<8)|block[2];
  908. if(m & 0x0001)
  909. csr6 |= Ps;
  910. if(m & 0x0010)
  911. csr6 |= Ttm;
  912. if(m & 0x0020)
  913. csr6 |= Pcs;
  914. if(m & 0x0040)
  915. csr6 |= Scr;
  916. csr32w(ctlr, 12, block[1]);
  917. microdelay(10);
  918. csr32w(ctlr, 6, csr6);
  919. ctlr->csr6 = csr6;
  920. if(!wait)
  921. return 0;
  922. for(timeo = 0; timeo < 30; timeo++){
  923. if(type0link(ctlr, block))
  924. return 0;
  925. delay(100);
  926. }
  927. return -1;
  928. }
  929. static int
  930. media21041(Ether* ether, int wait)
  931. {
  932. Ctlr* ctlr;
  933. uint8_t *block;
  934. int csr6, csr13, csr14, csr15, medium, timeo;
  935. ctlr = ether->ctlr;
  936. block = ctlr->infoblock[ctlr->curk];
  937. debug("media21041: block[0] %2.2uX, medium %4.4uX sct %4.4uX\n",
  938. block[0], ctlr->medium, ctlr->sct);
  939. medium = block[0] & 0x3F;
  940. if(ctlr->medium >= 0 && medium != ctlr->medium)
  941. return 0;
  942. if(ctlr->sct != 0x0800 && (ctlr->sct & 0x3F) != medium)
  943. return 0;
  944. csr6 = Sc|Mbo|Ca|TrMODE|Sb;
  945. if(block[0] & 0x40){
  946. csr13 = (block[2]<<8)|block[1];
  947. csr14 = (block[4]<<8)|block[3];
  948. csr15 = (block[6]<<8)|block[5];
  949. }
  950. else switch(medium){
  951. default:
  952. return -1;
  953. case 0x00: /* 10BASE-T */
  954. csr13 = 0xEF01;
  955. csr14 = 0xFF3F;
  956. csr15 = 0x0008;
  957. break;
  958. case 0x01: /* 10BASE-2 */
  959. csr13 = 0xEF09;
  960. csr14 = 0xF73D;
  961. csr15 = 0x0006;
  962. break;
  963. case 0x02: /* 10BASE-5 */
  964. csr13 = 0xEF09;
  965. csr14 = 0xF73D;
  966. csr15 = 0x000E;
  967. break;
  968. case 0x04: /* 10BASE-TFD */
  969. csr13 = 0xEF01;
  970. csr14 = 0xFF3D;
  971. csr15 = 0x0008;
  972. break;
  973. }
  974. csr32w(ctlr, 13, 0);
  975. csr32w(ctlr, 14, csr14);
  976. csr32w(ctlr, 15, csr15);
  977. csr32w(ctlr, 13, csr13);
  978. delay(10);
  979. if(medium == 0x04)
  980. csr6 |= Fd;
  981. ctlr->csr6 = csr6;
  982. csr32w(ctlr, 6, ctlr->csr6);
  983. debug("media21041: csr6 %8.8uX csr13 %4.4uX csr14 %4.4uX csr15 %4.4uX\n",
  984. csr6, csr13, csr14, csr15);
  985. if(!wait)
  986. return 0;
  987. for(timeo = 0; timeo < 30; timeo++){
  988. if(!(csr32r(ctlr, 12) & 0x0002)){
  989. debug("media21041: ok: csr12 %4.4luX timeo %d\n",
  990. csr32r(ctlr, 12), timeo);
  991. return 10;
  992. }
  993. delay(100);
  994. }
  995. debug("media21041: !ok: csr12 %4.4luX\n", csr32r(ctlr, 12));
  996. return -1;
  997. }
  998. static int
  999. mediaxx(Ether* ether, int wait)
  1000. {
  1001. Ctlr* ctlr;
  1002. uint8_t *block;
  1003. ctlr = ether->ctlr;
  1004. block = ctlr->infoblock[ctlr->curk];
  1005. if(block[0] & 0x80){
  1006. switch(block[1]){
  1007. default:
  1008. return -1;
  1009. case 0:
  1010. if(ctlr->medium >= 0 && block[2] != ctlr->medium)
  1011. return 0;
  1012. /* need this test? */ if(ctlr->sct != 0x0800 && (ctlr->sct & 0x3F) != block[2])
  1013. return 0;
  1014. if(type0mode(ctlr, block+2, wait))
  1015. return 0;
  1016. break;
  1017. case 1:
  1018. if(typephymode(ctlr, block, wait))
  1019. return 0;
  1020. break;
  1021. case 2:
  1022. debug("type2: medium %d block[2] %d\n",
  1023. ctlr->medium, block[2]);
  1024. if(ctlr->medium >= 0 && ((block[2] & 0x3F) != ctlr->medium))
  1025. return 0;
  1026. if(type2mode(ctlr, block, wait))
  1027. return 0;
  1028. break;
  1029. case 3:
  1030. if(typephymode(ctlr, block, wait))
  1031. return 0;
  1032. break;
  1033. case 4:
  1034. debug("type4: medium %d block[2] %d\n",
  1035. ctlr->medium, block[2]);
  1036. if(ctlr->medium >= 0 && ((block[2] & 0x3F) != ctlr->medium))
  1037. return 0;
  1038. if(typesymmode(ctlr, block, wait))
  1039. return 0;
  1040. break;
  1041. }
  1042. }
  1043. else{
  1044. if(ctlr->medium >= 0 && block[0] != ctlr->medium)
  1045. return 0;
  1046. /* need this test? */if(ctlr->sct != 0x0800 && (ctlr->sct & 0x3F) != block[0])
  1047. return 0;
  1048. if(type0mode(ctlr, block, wait))
  1049. return 0;
  1050. }
  1051. if(ctlr->csr6){
  1052. if(!(ctlr->csr6 & Ps) || (ctlr->csr6 & Ttm))
  1053. return 10;
  1054. return 100;
  1055. }
  1056. return 0;
  1057. }
  1058. static int
  1059. media(Ether* ether, int wait)
  1060. {
  1061. Ctlr* ctlr;
  1062. int k, mbps;
  1063. ctlr = ether->ctlr;
  1064. for(k = 0; k < ctlr->k; k++){
  1065. switch(ctlr->id){
  1066. default:
  1067. mbps = mediaxx(ether, wait);
  1068. break;
  1069. case Tulip1: /* 21041 */
  1070. mbps = media21041(ether, wait);
  1071. break;
  1072. }
  1073. if(mbps > 0)
  1074. return mbps;
  1075. if(ctlr->curk == 0)
  1076. ctlr->curk = ctlr->k-1;
  1077. else
  1078. ctlr->curk--;
  1079. }
  1080. return 0;
  1081. }
  1082. static char* mediatable[9] = {
  1083. "10BASE-T", /* TP */
  1084. "10BASE-2", /* BNC */
  1085. "10BASE-5", /* AUI */
  1086. "100BASE-TX",
  1087. "10BASE-TFD",
  1088. "100BASE-TXFD",
  1089. "100BASE-T4",
  1090. "100BASE-FX",
  1091. "100BASE-FXFD",
  1092. };
  1093. static uint8_t en1207[] = { /* Accton EN1207-COMBO */
  1094. 0x00, 0x00, 0xE8, /* [0] vendor ethernet code */
  1095. 0x00, /* [3] spare */
  1096. 0x00, 0x08, /* [4] connection (LSB+MSB = 0x0800) */
  1097. 0x1F, /* [6] general purpose control */
  1098. 2, /* [7] block count */
  1099. 0x00, /* [8] media code (10BASE-TX) */
  1100. 0x0B, /* [9] general purpose port data */
  1101. 0x9E, 0x00, /* [10] command (LSB+MSB = 0x009E) */
  1102. 0x03, /* [8] media code (100BASE-TX) */
  1103. 0x1B, /* [9] general purpose port data */
  1104. 0x6D, 0x00, /* [10] command (LSB+MSB = 0x006D) */
  1105. /* There is 10BASE-2 as well, but... */
  1106. };
  1107. static uint8_t ana6910fx[] = { /* Adaptec (Cogent) ANA-6910FX */
  1108. 0x00, 0x00, 0x92, /* [0] vendor ethernet code */
  1109. 0x00, /* [3] spare */
  1110. 0x00, 0x08, /* [4] connection (LSB+MSB = 0x0800) */
  1111. 0x3F, /* [6] general purpose control */
  1112. 1, /* [7] block count */
  1113. 0x07, /* [8] media code (100BASE-FX) */
  1114. 0x03, /* [9] general purpose port data */
  1115. 0x2D, 0x00 /* [10] command (LSB+MSB = 0x000D) */
  1116. };
  1117. static uint8_t smc9332[] = { /* SMC 9332 */
  1118. 0x00, 0x00, 0xC0, /* [0] vendor ethernet code */
  1119. 0x00, /* [3] spare */
  1120. 0x00, 0x08, /* [4] connection (LSB+MSB = 0x0800) */
  1121. 0x1F, /* [6] general purpose control */
  1122. 2, /* [7] block count */
  1123. 0x00, /* [8] media code (10BASE-TX) */
  1124. 0x00, /* [9] general purpose port data */
  1125. 0x9E, 0x00, /* [10] command (LSB+MSB = 0x009E) */
  1126. 0x03, /* [8] media code (100BASE-TX) */
  1127. 0x09, /* [9] general purpose port data */
  1128. 0x6D, 0x00, /* [10] command (LSB+MSB = 0x006D) */
  1129. };
  1130. static uint8_t* leaf21140[] = {
  1131. en1207, /* Accton EN1207-COMBO */
  1132. ana6910fx, /* Adaptec (Cogent) ANA-6910FX */
  1133. smc9332, /* SMC 9332 */
  1134. 0,
  1135. };
  1136. /*
  1137. * Copied to ctlr->srom at offset 20.
  1138. */
  1139. static uint8_t leafpnic[] = {
  1140. 0x00, 0x00, 0x00, 0x00, /* MAC address */
  1141. 0x00, 0x00,
  1142. 0x00, /* controller 0 device number */
  1143. 0x1E, 0x00, /* controller 0 info leaf offset */
  1144. 0x00, /* reserved */
  1145. 0x00, 0x08, /* selected connection type */
  1146. 0x00, /* general purpose control */
  1147. 0x01, /* block count */
  1148. 0x8C, /* format indicator and count */
  1149. 0x01, /* block type */
  1150. 0x00, /* PHY number */
  1151. 0x00, /* GPR sequence length */
  1152. 0x00, /* reset sequence length */
  1153. 0x00, 0x78, /* media capabilities */
  1154. 0xE0, 0x01, /* Nway advertisment */
  1155. 0x00, 0x50, /* FDX bitmap */
  1156. 0x00, 0x18, /* TTM bitmap */
  1157. };
  1158. static int
  1159. srom(Ctlr* ctlr)
  1160. {
  1161. int i, k, oui, phy, x;
  1162. uint8_t *p;
  1163. /*
  1164. * This is a partial decoding of the SROM format described in
  1165. * 'Digital Semiconductor 21X4 Serial ROM Format, Version 4.05,
  1166. * 2-Mar-98'. Only the 2114[03] are handled, support for other
  1167. * controllers can be added as needed.
  1168. */
  1169. sromr(ctlr, 0);
  1170. if(ctlr->srom == nil)
  1171. ctlr->srom = malloc((1<<ctlr->sromsz)*sizeof(ushort));
  1172. for(i = 0; i < (1<<ctlr->sromsz); i++){
  1173. x = sromr(ctlr, i);
  1174. ctlr->srom[2*i] = x;
  1175. ctlr->srom[2*i+1] = x>>8;
  1176. }
  1177. if(DEBUG){
  1178. print("srom:");
  1179. for(i = 0; i < ((1<<ctlr->sromsz)*sizeof(ushort)); i++){
  1180. if(i && ((i & 0x0F) == 0))
  1181. print("\n ");
  1182. print(" %2.2uX", ctlr->srom[i]);
  1183. }
  1184. print("\n");
  1185. }
  1186. /*
  1187. * There are 2 SROM layouts:
  1188. * e.g. Digital EtherWORKS station address at offset 20;
  1189. * this complies with the 21140A SROM
  1190. * application note from Digital;
  1191. * e.g. SMC9332 station address at offset 0 followed by
  1192. * 2 additional bytes, repeated at offset
  1193. * 6; the 8 bytes are also repeated in
  1194. * reverse order at offset 8.
  1195. * To check which it is, read the SROM and check for the repeating
  1196. * patterns of the non-compliant cards; if that fails use the one at
  1197. * offset 20.
  1198. */
  1199. ctlr->sromea = ctlr->srom;
  1200. for(i = 0; i < 8; i++){
  1201. x = ctlr->srom[i];
  1202. if(x != ctlr->srom[15-i] || x != ctlr->srom[16+i]){
  1203. ctlr->sromea = &ctlr->srom[20];
  1204. break;
  1205. }
  1206. }
  1207. /*
  1208. * Fake up the SROM for the PNIC.
  1209. * It looks like a 21140 with a PHY.
  1210. * The MAC address is byte-swapped in the orginal SROM data.
  1211. */
  1212. if(ctlr->id == Pnic){
  1213. memmove(&ctlr->srom[20], leafpnic, sizeof(leafpnic));
  1214. for(i = 0; i < Eaddrlen; i += 2){
  1215. ctlr->srom[20+i] = ctlr->srom[i+1];
  1216. ctlr->srom[20+i+1] = ctlr->srom[i];
  1217. }
  1218. }
  1219. if(ctlr->id == CentaurP || ctlr->id == CentaurPcb){
  1220. memmove(&ctlr->srom[20], leafpnic, sizeof(leafpnic));
  1221. for(i = 0; i < Eaddrlen; i += 2){
  1222. ctlr->srom[20+i] = ctlr->srom[8+i];
  1223. ctlr->srom[20+i+1] = ctlr->srom[8+i+1];
  1224. }
  1225. }
  1226. /*
  1227. * Next, try to find the info leaf in the SROM for media detection.
  1228. * If it's a non-conforming card try to match the vendor ethernet code
  1229. * and point p at a fake info leaf with compact 21140 entries.
  1230. */
  1231. if(ctlr->sromea == ctlr->srom){
  1232. p = nil;
  1233. for(i = 0; leaf21140[i] != nil; i++){
  1234. if(memcmp(leaf21140[i], ctlr->sromea, 3) == 0){
  1235. p = &leaf21140[i][4];
  1236. break;
  1237. }
  1238. }
  1239. if(p == nil)
  1240. return -1;
  1241. }
  1242. else
  1243. p = &ctlr->srom[(ctlr->srom[28]<<8)|ctlr->srom[27]];
  1244. /*
  1245. * Set up the info needed for later media detection.
  1246. * For the 21140, set the general-purpose mask in CSR12.
  1247. * The info block entries are stored in order of increasing
  1248. * precedence, so detection will work backwards through the
  1249. * stored indexes into ctlr->srom.
  1250. * If an entry is found which matches the selected connection
  1251. * type, save the index. Otherwise, start at the last entry.
  1252. * If any MII entries are found (type 1 and 3 blocks), scan
  1253. * for PHYs.
  1254. */
  1255. ctlr->leaf = p;
  1256. ctlr->sct = *p++;
  1257. ctlr->sct |= *p++<<8;
  1258. if(ctlr->id != Tulip3 && ctlr->id != Tulip1){
  1259. csr32w(ctlr, 12, Gpc|*p++);
  1260. delay(200);
  1261. }
  1262. ctlr->k = *p++;
  1263. if(ctlr->k >= nelem(ctlr->infoblock))
  1264. ctlr->k = nelem(ctlr->infoblock)-1;
  1265. ctlr->sctk = ctlr->k-1;
  1266. phy = 0;
  1267. for(k = 0; k < ctlr->k; k++){
  1268. ctlr->infoblock[k] = p;
  1269. if(ctlr->id == Tulip1){
  1270. debug("type21041: 0x%2.2uX\n", p[0]);
  1271. if(ctlr->sct != 0x0800 && *p == (ctlr->sct & 0xFF))
  1272. ctlr->sctk = k;
  1273. if(*p & 0x40)
  1274. p += 7;
  1275. else
  1276. p += 1;
  1277. }
  1278. /*
  1279. * The RAMIX PMC665 has a badly-coded SROM,
  1280. * hence the test for 21143 and type 3.
  1281. */
  1282. else if((*p & 0x80) || (ctlr->id == Tulip3 && *(p+1) == 3)){
  1283. *p |= 0x80;
  1284. if(*(p+1) == 1 || *(p+1) == 3)
  1285. phy = 1;
  1286. if(*(p+1) == 5)
  1287. ctlr->type5block = p;
  1288. p += (*p & ~0x80)+1;
  1289. }
  1290. else{
  1291. debug("type0: 0x%2.2uX 0x%2.2uX 0x%2.2uX 0x%2.2uX\n",
  1292. p[0], p[1], p[2], p[3]);
  1293. if(ctlr->sct != 0x0800 && *p == (ctlr->sct & 0xFF))
  1294. ctlr->sctk = k;
  1295. p += 4;
  1296. }
  1297. }
  1298. ctlr->curk = ctlr->sctk;
  1299. debug("sct 0x%uX medium 0x%uX k %d curk %d phy %d\n",
  1300. ctlr->sct, ctlr->medium, ctlr->k, ctlr->curk, phy);
  1301. if(phy){
  1302. x = 0;
  1303. for(k = 0; k < nelem(ctlr->phy); k++){
  1304. if((ctlr->id == CentaurP || ctlr->id == CentaurPcb) && k != 1)
  1305. continue;
  1306. if((oui = miir(ctlr, k, 2)) == -1 || oui == 0)
  1307. continue;
  1308. if(DEBUG){
  1309. oui = (oui & 0x3FF)<<6;
  1310. oui |= miir(ctlr, k, 3)>>10;
  1311. miir(ctlr, k, 1);
  1312. debug("phy%d: index %d oui %uX reg1 %uX\n",
  1313. x, k, oui, miir(ctlr, k, 1));
  1314. USED(oui);
  1315. }
  1316. ctlr->phy[x] = k;
  1317. }
  1318. }
  1319. ctlr->fd = 0;
  1320. ctlr->medium = -1;
  1321. return 0;
  1322. }
  1323. static void
  1324. dec2114xpci(void)
  1325. {
  1326. Ctlr *ctlr;
  1327. Pcidev *p;
  1328. int x;
  1329. p = nil;
  1330. while(p = pcimatch(p, 0, 0)){
  1331. if(p->ccrb != 0x02 || p->ccru != 0)
  1332. continue;
  1333. switch((p->did<<16)|p->vid){
  1334. default:
  1335. continue;
  1336. case Tulip3: /* 21143 */
  1337. /*
  1338. * Exit sleep mode.
  1339. */
  1340. x = pcicfgr32(p, 0x40);
  1341. x &= ~0xC0000000;
  1342. pcicfgw32(p, 0x40, x);
  1343. /*FALLTHROUGH*/
  1344. case Tulip0: /* 21140 */
  1345. case Tulip1: /* 21041 */
  1346. case Pnic: /* PNIC */
  1347. case Pnic2: /* PNIC-II */
  1348. case CentaurP: /* ADMtek */
  1349. case CentaurPcb: /* ADMtek CardBus */
  1350. break;
  1351. }
  1352. /*
  1353. * bar[0] is the I/O port register address and
  1354. * bar[1] is the memory-mapped register address.
  1355. */
  1356. ctlr = malloc(sizeof(Ctlr));
  1357. ctlr->port = p->mem[0].bar & ~0x01;
  1358. ctlr->pcidev = p;
  1359. ctlr->id = (p->did<<16)|p->vid;
  1360. debug("2114x: type 0x%8.8uX rev 0x%4.4uX at port 0x%4.4uX\n",
  1361. ctlr->id, p->rid, ctlr->port);
  1362. /*
  1363. * Some cards (e.g. ANA-6910FX) seem to need the Ps bit
  1364. * set or they don't always work right after a hardware
  1365. * reset.
  1366. */
  1367. csr32w(ctlr, 6, Mbo|Ps);
  1368. softreset(ctlr);
  1369. if(srom(ctlr)){
  1370. free(ctlr);
  1371. break;
  1372. }
  1373. switch(ctlr->id){
  1374. default:
  1375. break;
  1376. case Pnic: /* PNIC */
  1377. /*
  1378. * Turn off the jabber timer.
  1379. */
  1380. csr32w(ctlr, 15, 0x00000001);
  1381. break;
  1382. case CentaurP:
  1383. case CentaurPcb:
  1384. /*
  1385. * Nice - the register offsets change from *8 to *4
  1386. * for CSR16 and up...
  1387. * CSR25/26 give the MAC address read from the SROM.
  1388. * Don't really need to use this other than as a check,
  1389. * the SROM will be read in anyway so the value there
  1390. * can be used directly.
  1391. */
  1392. debug("csr25 %8.8luX csr26 %8.8luX\n",
  1393. inl(ctlr->port+0xA4), inl(ctlr->port+0xA8));
  1394. debug("phyidr1 %4.4luX phyidr2 %4.4luX\n",
  1395. inl(ctlr->port+0xBC), inl(ctlr->port+0xC0));
  1396. break;
  1397. }
  1398. if(ctlrhead != nil)
  1399. ctlrtail->next = ctlr;
  1400. else
  1401. ctlrhead = ctlr;
  1402. ctlrtail = ctlr;
  1403. }
  1404. }
  1405. static void
  1406. detach(Ether* ether)
  1407. {
  1408. softreset(ether->ctlr);
  1409. }
  1410. int
  1411. ether2114xreset(Ether* ether)
  1412. {
  1413. Ctlr *ctlr;
  1414. int i, x;
  1415. uint8_t ea[Eaddrlen];
  1416. static int scandone;
  1417. if(scandone == 0){
  1418. dec2114xpci();
  1419. scandone = 1;
  1420. }
  1421. /*
  1422. * Any adapter matches if no ether->port is supplied,
  1423. * otherwise the ports must match.
  1424. */
  1425. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  1426. if(ctlr->active)
  1427. continue;
  1428. if(ether->port == 0 || ether->port == ctlr->port){
  1429. ctlr->active = 1;
  1430. break;
  1431. }
  1432. }
  1433. if(ctlr == nil)
  1434. return -1;
  1435. ether->ctlr = ctlr;
  1436. ether->port = ctlr->port;
  1437. ether->irq = ctlr->pcidev->intl;
  1438. ether->tbdf = ctlr->pcidev->tbdf;
  1439. /*
  1440. * Check if the adapter's station address is to be overridden.
  1441. * If not, read it from the EEPROM and set in ether->ea prior to
  1442. * loading the station address in the hardware.
  1443. */
  1444. memset(ea, 0, Eaddrlen);
  1445. if(memcmp(ea, ether->ea, Eaddrlen) == 0)
  1446. memmove(ether->ea, ctlr->sromea, Eaddrlen);
  1447. /*
  1448. * Look for a medium override in case there's no autonegotiation
  1449. * (no MII) or the autonegotiation fails.
  1450. */
  1451. for(i = 0; i < ether->nopt; i++){
  1452. if(cistrcmp(ether->opt[i], "FD") == 0){
  1453. ctlr->fd = 1;
  1454. continue;
  1455. }
  1456. for(x = 0; x < nelem(mediatable); x++){
  1457. debug("compare <%s> <%s>\n", mediatable[x],
  1458. ether->opt[i]);
  1459. if(cistrcmp(mediatable[x], ether->opt[i]))
  1460. continue;
  1461. ctlr->medium = x;
  1462. switch(ctlr->medium){
  1463. default:
  1464. ctlr->fd = 0;
  1465. break;
  1466. case 0x04: /* 10BASE-TFD */
  1467. case 0x05: /* 100BASE-TXFD */
  1468. case 0x08: /* 100BASE-FXFD */
  1469. ctlr->fd = 1;
  1470. break;
  1471. }
  1472. break;
  1473. }
  1474. }
  1475. /*
  1476. * Determine media.
  1477. */
  1478. ctlr->mbps = media(ether, 1);
  1479. /*
  1480. * Initialise descriptor rings, ethernet address.
  1481. */
  1482. ctlr->nrdr = Nrde;
  1483. ctlr->ntdr = Ntde;
  1484. pcisetbme(ctlr->pcidev);
  1485. ctlrinit(ether);
  1486. /*
  1487. * Linkage to the generic ethernet driver.
  1488. */
  1489. ether->attach = attach;
  1490. ether->transmit = transmit;
  1491. ether->interrupt = interrupt;
  1492. ether->detach = detach;
  1493. return 0;
  1494. }