ether82557.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * Intel 82557 Fast Ethernet PCI Bus LAN Controller
  3. * as found on the Intel EtherExpress PRO/100B. This chip is full
  4. * of smarts, unfortunately they're not all in the right place.
  5. * To do:
  6. * the PCI scanning code could be made common to other adapters;
  7. * auto-negotiation, full-duplex;
  8. * optionally use memory-mapped registers;
  9. * detach for PCI reset problems (also towards loadable drivers).
  10. */
  11. #include "all.h"
  12. #include "io.h"
  13. #include "mem.h"
  14. #include "../ip/ip.h"
  15. #include "etherif.h"
  16. enum {
  17. Nrfd = 64, /* receive frame area */
  18. Ncb = 64, /* maximum control blocks queued */
  19. NullPointer = 0xFFFFFFFF, /* 82557 NULL pointer */
  20. };
  21. enum { /* CSR */
  22. Status = 0x00, /* byte or word (word includes Ack) */
  23. Ack = 0x01, /* byte */
  24. CommandR = 0x02, /* byte or word (word includes Interrupt) */
  25. Interrupt = 0x03, /* byte */
  26. General = 0x04, /* dword */
  27. Port = 0x08, /* dword */
  28. Fcr = 0x0C, /* Flash control register */
  29. Ecr = 0x0E, /* EEPROM control register */
  30. Mcr = 0x10, /* MDI control register */
  31. };
  32. enum { /* Status */
  33. RUidle = 0x0000,
  34. RUsuspended = 0x0004,
  35. RUnoresources = 0x0008,
  36. RUready = 0x0010,
  37. RUrbd = 0x0020, /* bit */
  38. RUstatus = 0x003F, /* mask */
  39. CUidle = 0x0000,
  40. CUsuspended = 0x0040,
  41. CUactive = 0x0080,
  42. CUstatus = 0x00C0, /* mask */
  43. StatSWI = 0x0400, /* SoftWare generated Interrupt */
  44. StatMDI = 0x0800, /* MDI r/w done */
  45. StatRNR = 0x1000, /* Receive unit Not Ready */
  46. StatCNA = 0x2000, /* Command unit Not Active (Active->Idle) */
  47. StatFR = 0x4000, /* Finished Receiving */
  48. StatCX = 0x8000, /* Command eXecuted */
  49. StatTNO = 0x8000, /* Transmit NOT OK */
  50. };
  51. enum { /* Command (byte) */
  52. CUnop = 0x00,
  53. CUstart = 0x10,
  54. CUresume = 0x20,
  55. LoadDCA = 0x40, /* Load Dump Counters Address */
  56. DumpSC = 0x50, /* Dump Statistical Counters */
  57. LoadCUB = 0x60, /* Load CU Base */
  58. ResetSA = 0x70, /* Dump and Reset Statistical Counters */
  59. RUstart = 0x01,
  60. RUresume = 0x02,
  61. RUabort = 0x04,
  62. LoadHDS = 0x05, /* Load Header Data Size */
  63. LoadRUB = 0x06, /* Load RU Base */
  64. RBDresume = 0x07, /* Resume frame reception */
  65. };
  66. enum { /* Interrupt (byte) */
  67. InterruptM = 0x01, /* interrupt Mask */
  68. InterruptSI = 0x02, /* Software generated Interrupt */
  69. };
  70. enum { /* Ecr */
  71. EEsk = 0x01, /* serial clock */
  72. EEcs = 0x02, /* chip select */
  73. EEdi = 0x04, /* serial data in */
  74. EEdo = 0x08, /* serial data out */
  75. EEstart = 0x04, /* start bit */
  76. EEread = 0x02, /* read opcode */
  77. EEaddrsz = 6, /* bits of address */
  78. };
  79. enum { /* Mcr */
  80. MDIread = 0x08000000, /* read opcode */
  81. MDIwrite = 0x04000000, /* write opcode */
  82. MDIready = 0x10000000, /* ready bit */
  83. MDIie = 0x20000000, /* interrupt enable */
  84. };
  85. typedef struct Rfd {
  86. int field;
  87. ulong link;
  88. ulong rbd;
  89. ushort count;
  90. ushort size;
  91. uchar data[1700];
  92. } Rfd;
  93. enum { /* field */
  94. RfdCollision = 0x00000001,
  95. RfdIA = 0x00000002, /* IA match */
  96. RfdRxerr = 0x00000010, /* PHY character error */
  97. RfdType = 0x00000020, /* Type frame */
  98. RfdRunt = 0x00000080,
  99. RfdOverrun = 0x00000100,
  100. RfdBuffer = 0x00000200,
  101. RfdAlignment = 0x00000400,
  102. RfdCRC = 0x00000800,
  103. RfdOK = 0x00002000, /* frame received OK */
  104. RfdC = 0x00008000, /* reception Complete */
  105. RfdSF = 0x00080000, /* Simplified or Flexible (1) Rfd */
  106. RfdH = 0x00100000, /* Header RFD */
  107. RfdI = 0x20000000, /* Interrupt after completion */
  108. RfdS = 0x40000000, /* Suspend after completion */
  109. RfdEL = 0x80000000, /* End of List */
  110. };
  111. enum { /* count */
  112. RfdF = 0x4000,
  113. RfdEOF = 0x8000,
  114. };
  115. typedef struct Cb Cb;
  116. typedef struct Cb {
  117. ushort status;
  118. ushort command;
  119. ulong link;
  120. union {
  121. uchar data[24]; /* CbIAS + CbConfigure */
  122. struct {
  123. ulong tbd;
  124. ushort count;
  125. uchar threshold;
  126. uchar number;
  127. ulong tba;
  128. ushort tbasz;
  129. ushort pad;
  130. };
  131. };
  132. Msgbuf* mb;
  133. Cb* next;
  134. } Cb;
  135. enum { /* action command */
  136. CbU = 0x1000, /* transmit underrun */
  137. CbOK = 0x2000, /* DMA completed OK */
  138. CbC = 0x8000, /* execution Complete */
  139. CbNOP = 0x0000,
  140. CbIAS = 0x0001, /* Individual Address Setup */
  141. CbConfigure = 0x0002,
  142. CbMAS = 0x0003, /* Multicast Address Setup */
  143. CbTransmit = 0x0004,
  144. CbDump = 0x0006,
  145. CbDiagnose = 0x0007,
  146. CbCommand = 0x0007, /* mask */
  147. CbSF = 0x0008, /* Flexible-mode CbTransmit */
  148. CbI = 0x2000, /* Interrupt after completion */
  149. CbS = 0x4000, /* Suspend after completion */
  150. CbEL = 0x8000, /* End of List */
  151. };
  152. enum { /* CbTransmit count */
  153. CbEOF = 0x8000,
  154. };
  155. typedef struct Ctlr {
  156. Lock slock; /* attach */
  157. int state;
  158. int port;
  159. ushort eeprom[0x40];
  160. Lock miilock;
  161. Rendez timer; /* watchdog timer for receive lockup errata */
  162. int tick;
  163. char wname[NAMELEN];
  164. Lock rlock; /* registers */
  165. int command; /* last command issued */
  166. Msgbuf* rfdhead; /* receive side */
  167. Msgbuf* rfdtail;
  168. int nrfd;
  169. Lock cblock; /* transmit side */
  170. int action;
  171. uchar configdata[24];
  172. int threshold;
  173. int ncb;
  174. Cb* cbr;
  175. Cb* cbhead;
  176. Cb* cbtail;
  177. int cbq;
  178. int cbqmax;
  179. int cbqmaxhw;
  180. Lock dlock; /* dump statistical counters */
  181. ulong dump[17];
  182. } Ctlr;
  183. static uchar configdata[24] = {
  184. 0x16, /* byte count */
  185. 0x08, /* Rx/Tx FIFO limit */
  186. 0x00, /* adaptive IFS */
  187. 0x00,
  188. 0x00, /* Rx DMA maximum byte count */
  189. // 0x80, /* Tx DMA maximum byte count */
  190. 0x00, /* Tx DMA maximum byte count */
  191. 0x32, /* !late SCB, CNA interrupts */
  192. 0x03, /* discard short Rx frames */
  193. 0x00, /* 503/MII */
  194. 0x00,
  195. 0x2E, /* normal operation, NSAI */
  196. 0x00, /* linear priority */
  197. 0x60, /* inter-frame spacing */
  198. 0x00,
  199. 0xF2,
  200. 0xC8, /* 503, promiscuous mode off */
  201. 0x00,
  202. 0x40,
  203. 0xF3, /* transmit padding enable */
  204. 0x80, /* full duplex pin enable */
  205. 0x3F, /* no Multi IA */
  206. 0x05, /* no Multi Cast ALL */
  207. };
  208. #define csr8r(c, r) (inb((c)->port+(r)))
  209. #define csr16r(c, r) (ins((c)->port+(r)))
  210. #define csr32r(c, r) (inl((c)->port+(r)))
  211. #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
  212. #define csr16w(c, r, w) (outs((c)->port+(r), (ushort)(w)))
  213. #define csr32w(c, r, l) (outl((c)->port+(r), (ulong)(l)))
  214. static void
  215. command(Ctlr* ctlr, int c, int v)
  216. {
  217. ilock(&ctlr->rlock);
  218. /*
  219. * Only back-to-back CUresume can be done
  220. * without waiting for any previous command to complete.
  221. * This should be the common case.
  222. * Unfortunately there's a chip errata where back-to-back
  223. * CUresumes can be lost, the fix is to always wait.
  224. if(c == CUresume && ctlr->command == CUresume){
  225. csr8w(ctlr, CommandR, c);
  226. iunlock(&ctlr->rlock);
  227. return;
  228. }
  229. */
  230. while(csr8r(ctlr, CommandR))
  231. ;
  232. switch(c){
  233. case CUstart:
  234. case LoadDCA:
  235. case LoadCUB:
  236. case RUstart:
  237. case LoadHDS:
  238. case LoadRUB:
  239. csr32w(ctlr, General, v);
  240. break;
  241. /*
  242. case CUnop:
  243. case CUresume:
  244. case DumpSC:
  245. case ResetSA:
  246. case RUresume:
  247. case RUabort:
  248. */
  249. default:
  250. break;
  251. }
  252. csr8w(ctlr, CommandR, c);
  253. ctlr->command = c;
  254. iunlock(&ctlr->rlock);
  255. }
  256. static Msgbuf*
  257. rfdalloc(ulong link)
  258. {
  259. Msgbuf *mb;
  260. Rfd *rfd;
  261. if(mb = mballoc(sizeof(Rfd), 0, Maeth2)){
  262. rfd = (Rfd*)mb->data;
  263. rfd->field = 0;
  264. rfd->link = link;
  265. rfd->rbd = NullPointer;
  266. rfd->count = 0;
  267. rfd->size = sizeof(Enpkt);
  268. }
  269. return mb;
  270. }
  271. static int
  272. return0(void*)
  273. {
  274. return 0;
  275. }
  276. static void
  277. watchdog(void)
  278. {
  279. Ether *ether;
  280. Ctlr *ctlr;
  281. static void txstart(Ether*);
  282. ether = getarg();
  283. for(;;){
  284. ctlr = ether->ctlr;
  285. tsleep(&ctlr->timer, return0, 0, 4000);
  286. /*
  287. * Hmmm. This doesn't seem right. Currently
  288. * the device can't be disabled but it may be in
  289. * the future.
  290. */
  291. ctlr = ether->ctlr;
  292. if(ctlr == nil || ctlr->state == 0)
  293. continue;
  294. ilock(&ctlr->cblock);
  295. if(ctlr->tick++){
  296. ctlr->action = CbMAS;
  297. txstart(ether);
  298. }
  299. iunlock(&ctlr->cblock);
  300. }
  301. }
  302. static void
  303. attach(Ether* ether)
  304. {
  305. Ctlr *ctlr;
  306. ctlr = ether->ctlr;
  307. lock(&ctlr->slock);
  308. if(ctlr->state == 0){
  309. ilock(&ctlr->rlock);
  310. csr8w(ctlr, Interrupt, 0);
  311. iunlock(&ctlr->rlock);
  312. command(ctlr, RUstart, PADDR(ctlr->rfdhead->data));
  313. ctlr->state = 1;
  314. /*
  315. * Start the watchdog timer for the receive lockup errata
  316. * unless the EEPROM compatibility word indicates it may be
  317. * omitted.
  318. */
  319. if((ctlr->eeprom[0x03] & 0x0003) != 0x0003){
  320. sprint(ctlr->wname, "ether%dwatchdog", ether->ctlrno);
  321. userinit(watchdog, ether, ctlr->wname);
  322. }
  323. }
  324. unlock(&ctlr->slock);
  325. }
  326. static void
  327. txstart(Ether* ether)
  328. {
  329. Ctlr *ctlr;
  330. Msgbuf *mb;
  331. Cb *cb;
  332. ctlr = ether->ctlr;
  333. while(ctlr->cbq < (ctlr->ncb-1)){
  334. cb = ctlr->cbhead->next;
  335. if(ctlr->action == 0){
  336. mb = etheroq(ether);
  337. if(mb == nil)
  338. break;
  339. cb->command = CbS|CbSF|CbTransmit;
  340. cb->tbd = PADDR(&cb->tba);
  341. cb->count = 0;
  342. cb->threshold = ctlr->threshold;
  343. cb->number = 1;
  344. cb->tba = PADDR(mb->data);
  345. cb->mb = mb;
  346. cb->tbasz = mb->count;
  347. }
  348. else if(ctlr->action == CbConfigure){
  349. cb->command = CbS|CbConfigure;
  350. memmove(cb->data, ctlr->configdata, sizeof(ctlr->configdata));
  351. ctlr->action = 0;
  352. }
  353. else if(ctlr->action == CbIAS){
  354. cb->command = CbS|CbIAS;
  355. memmove(cb->data, ether->ea, Easize);
  356. ctlr->action = 0;
  357. }
  358. else if(ctlr->action == CbMAS){
  359. cb->command = CbS|CbMAS;
  360. memset(cb->data, 0, sizeof(cb->data));
  361. ctlr->action = 0;
  362. }
  363. else{
  364. print("#l%d: action 0x%ux\n", ether->ctlrno, ctlr->action);
  365. ctlr->action = 0;
  366. break;
  367. }
  368. cb->status = 0;
  369. //coherence();
  370. ctlr->cbhead->command &= ~CbS;
  371. ctlr->cbhead = cb;
  372. ctlr->cbq++;
  373. }
  374. command(ctlr, CUresume, 0);
  375. if(ctlr->cbq > ctlr->cbqmax)
  376. ctlr->cbqmax = ctlr->cbq;
  377. }
  378. static void
  379. configure(Ether* ether, int promiscuous)
  380. {
  381. Ctlr *ctlr;
  382. ctlr = ether->ctlr;
  383. ilock(&ctlr->cblock);
  384. if(promiscuous){
  385. ctlr->configdata[6] |= 0x80; /* Save Bad Frames */
  386. //ctlr->configdata[6] &= ~0x40; /* !Discard Overrun Rx Frames */
  387. ctlr->configdata[7] &= ~0x01; /* !Discard Short Rx Frames */
  388. ctlr->configdata[15] |= 0x01; /* Promiscuous mode */
  389. ctlr->configdata[18] &= ~0x01; /* (!Padding enable?), !stripping enable */
  390. ctlr->configdata[21] |= 0x08; /* Multi Cast ALL */
  391. }
  392. else{
  393. ctlr->configdata[6] &= ~0x80;
  394. //ctlr->configdata[6] |= 0x40;
  395. ctlr->configdata[7] |= 0x01;
  396. ctlr->configdata[15] &= ~0x01;
  397. ctlr->configdata[18] |= 0x01; /* 0x03? */
  398. ctlr->configdata[21] &= ~0x08;
  399. }
  400. ctlr->action = CbConfigure;
  401. txstart(ether);
  402. iunlock(&ctlr->cblock);
  403. }
  404. static void
  405. transmit(Ether* ether)
  406. {
  407. Ctlr *ctlr;
  408. ctlr = ether->ctlr;
  409. ilock(&ctlr->cblock);
  410. txstart(ether);
  411. iunlock(&ctlr->cblock);
  412. }
  413. static void
  414. receive(Ether* ether)
  415. {
  416. Rfd *rfd;
  417. Ctlr *ctlr;
  418. int count;
  419. Msgbuf *mb, *pmb, *xmb;
  420. ctlr = ether->ctlr;
  421. mb = ctlr->rfdhead;
  422. for(rfd = (Rfd*)mb->data; rfd->field & RfdC; rfd = (Rfd*)mb->data){
  423. /*
  424. * If it's an OK receive frame
  425. * 1) save the count
  426. * 2) if it's small, try to allocate a block and copy
  427. * the data, then adjust the necessary fields for reuse;
  428. * 3) if it's big, try to allocate a new Rfd and if
  429. * successful
  430. * adjust the received buffer pointers for the
  431. * actual data received;
  432. * initialise the replacement buffer to point to
  433. * the next in the ring;
  434. * initialise mb to point to the replacement;
  435. * 4) if there's a good packet, pass it on for disposal.
  436. */
  437. if(rfd->field & RfdOK){
  438. pmb = nil;
  439. count = rfd->count & 0x3FFF;
  440. if((count < ETHERMAXTU/4) && (pmb = mballoc(count, 0, Maeth3))){
  441. memmove(pmb->data, mb->data+sizeof(Rfd)-sizeof(rfd->data), count);
  442. pmb->count = count;
  443. rfd->count = 0;
  444. rfd->field = 0;
  445. }
  446. else if(xmb = rfdalloc(rfd->link)){
  447. mb->data += sizeof(Rfd)-sizeof(rfd->data);
  448. mb->count = count;
  449. xmb->next = mb->next;
  450. mb->next = 0;
  451. pmb = mb;
  452. mb = xmb;
  453. }
  454. if(pmb != nil)
  455. etheriq(ether, pmb);
  456. }
  457. else{
  458. rfd->count = 0;
  459. rfd->field = 0;
  460. }
  461. /*
  462. * The ring tail pointer follows the head with with one
  463. * unused buffer in between to defeat hardware prefetch;
  464. * once the tail pointer has been bumped on to the next
  465. * and the new tail has the Suspend bit set, it can be
  466. * removed from the old tail buffer.
  467. * As a replacement for the current head buffer may have
  468. * been allocated above, ensure that the new tail points
  469. * to it (next and link).
  470. */
  471. rfd = (Rfd*)ctlr->rfdtail->data;
  472. ctlr->rfdtail = ctlr->rfdtail->next;
  473. ctlr->rfdtail->next = mb;
  474. ((Rfd*)ctlr->rfdtail->data)->link = PADDR(mb->data);
  475. ((Rfd*)ctlr->rfdtail->data)->field |= RfdS;
  476. //coherence();
  477. rfd->field &= ~RfdS;
  478. /*
  479. * Finally done with the current (possibly replaced)
  480. * head, move on to the next and maintain the sentinel
  481. * between tail and head.
  482. */
  483. ctlr->rfdhead = mb->next;
  484. mb = ctlr->rfdhead;
  485. }
  486. }
  487. static void
  488. interrupt(Ureg*, void* arg)
  489. {
  490. Cb* cb;
  491. Ctlr *ctlr;
  492. Ether *ether;
  493. int status;
  494. ether = arg;
  495. ctlr = ether->ctlr;
  496. for(;;){
  497. ilock(&ctlr->rlock);
  498. status = csr16r(ctlr, Status);
  499. csr8w(ctlr, Ack, (status>>8) & 0xFF);
  500. iunlock(&ctlr->rlock);
  501. if(!(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI)))
  502. break;
  503. /*
  504. * If the watchdog timer for the receiver lockup errata is running,
  505. * let it know the receiver is active.
  506. */
  507. if(status & (StatFR|StatRNR)){
  508. ilock(&ctlr->cblock);
  509. ctlr->tick = 0;
  510. iunlock(&ctlr->cblock);
  511. }
  512. if(status & StatFR){
  513. receive(ether);
  514. status &= ~StatFR;
  515. }
  516. if(status & StatRNR){
  517. command(ctlr, RUresume, 0);
  518. status &= ~StatRNR;
  519. }
  520. if(status & StatCNA){
  521. ilock(&ctlr->cblock);
  522. cb = ctlr->cbtail;
  523. while(ctlr->cbq){
  524. if(!(cb->status & CbC))
  525. break;
  526. if(cb->mb){
  527. mbfree(cb->mb);
  528. cb->mb = nil;
  529. }
  530. if((cb->status & CbU) && ctlr->threshold < 0xE0)
  531. ctlr->threshold++;
  532. ctlr->cbq--;
  533. cb = cb->next;
  534. }
  535. ctlr->cbtail = cb;
  536. txstart(ether);
  537. iunlock(&ctlr->cblock);
  538. status &= ~StatCNA;
  539. }
  540. if(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI))
  541. panic("#l%d: status %ux\n", ether->ctlrno, status);
  542. }
  543. }
  544. static void
  545. ctlrinit(Ctlr* ctlr)
  546. {
  547. int i;
  548. Msgbuf *mb;
  549. Rfd *rfd;
  550. ulong link;
  551. /*
  552. * Create the Receive Frame Area (RFA) as a ring of allocated
  553. * buffers.
  554. * A sentinel buffer is maintained between the last buffer in
  555. * the ring (marked with RfdS) and the head buffer to defeat the
  556. * hardware prefetch of the next RFD and allow dynamic buffer
  557. * allocation.
  558. */
  559. link = NullPointer;
  560. for(i = 0; i < Nrfd; i++){
  561. mb = rfdalloc(link);
  562. if(ctlr->rfdhead == nil)
  563. ctlr->rfdtail = mb;
  564. mb->next = ctlr->rfdhead;
  565. ctlr->rfdhead = mb;
  566. link = PADDR(mb->data);
  567. }
  568. ctlr->rfdtail->next = ctlr->rfdhead;
  569. rfd = (Rfd*)ctlr->rfdtail->data;
  570. rfd->link = PADDR(ctlr->rfdhead->data);
  571. rfd->field |= RfdS;
  572. ctlr->rfdhead = ctlr->rfdhead->next;
  573. /*
  574. * Create a ring of control blocks for the
  575. * transmit side.
  576. */
  577. ilock(&ctlr->cblock);
  578. ctlr->cbr = ialloc(ctlr->ncb*sizeof(Cb), 0);
  579. for(i = 0; i < ctlr->ncb; i++){
  580. ctlr->cbr[i].status = CbC|CbOK;
  581. ctlr->cbr[i].command = CbS|CbNOP;
  582. ctlr->cbr[i].link = PADDR(&ctlr->cbr[NEXT(i, ctlr->ncb)].status);
  583. ctlr->cbr[i].next = &ctlr->cbr[NEXT(i, ctlr->ncb)];
  584. }
  585. ctlr->cbhead = ctlr->cbr;
  586. ctlr->cbtail = ctlr->cbr;
  587. ctlr->cbq = 0;
  588. memmove(ctlr->configdata, configdata, sizeof(configdata));
  589. ctlr->threshold = 80;
  590. ctlr->tick = 0;
  591. iunlock(&ctlr->cblock);
  592. }
  593. static int
  594. miir(Ctlr* ctlr, int phyadd, int regadd)
  595. {
  596. int mcr, timo;
  597. lock(&ctlr->miilock);
  598. csr32w(ctlr, Mcr, MDIread|(phyadd<<21)|(regadd<<16));
  599. mcr = 0;
  600. for(timo = 64; timo; timo--){
  601. mcr = csr32r(ctlr, Mcr);
  602. if(mcr & MDIready)
  603. break;
  604. microdelay(1);
  605. }
  606. unlock(&ctlr->miilock);
  607. if(mcr & MDIready)
  608. return mcr & 0xFFFF;
  609. return -1;
  610. }
  611. static int
  612. miiw(Ctlr* ctlr, int phyadd, int regadd, int data)
  613. {
  614. int mcr, timo;
  615. lock(&ctlr->miilock);
  616. csr32w(ctlr, Mcr, MDIwrite|(phyadd<<21)|(regadd<<16)|(data & 0xFFFF));
  617. mcr = 0;
  618. for(timo = 64; timo; timo--){
  619. mcr = csr32r(ctlr, Mcr);
  620. if(mcr & MDIready)
  621. break;
  622. microdelay(1);
  623. }
  624. unlock(&ctlr->miilock);
  625. if(mcr & MDIready)
  626. return 0;
  627. return -1;
  628. }
  629. static int
  630. hy93c46r(Ctlr* ctlr, int r)
  631. {
  632. int i, op, data;
  633. /*
  634. * Hyundai HY93C46 or equivalent serial EEPROM.
  635. * This sequence for reading a 16-bit register 'r'
  636. * in the EEPROM is taken straight from Section
  637. * 3.3.4.2 of the Intel 82557 User's Guide.
  638. */
  639. csr16w(ctlr, Ecr, EEcs);
  640. op = EEstart|EEread;
  641. for(i = 2; i >= 0; i--){
  642. data = (((op>>i) & 0x01)<<2)|EEcs;
  643. csr16w(ctlr, Ecr, data);
  644. csr16w(ctlr, Ecr, data|EEsk);
  645. microdelay(1);
  646. csr16w(ctlr, Ecr, data);
  647. microdelay(1);
  648. }
  649. for(i = EEaddrsz-1; i >= 0; i--){
  650. data = (((r>>i) & 0x01)<<2)|EEcs;
  651. csr16w(ctlr, Ecr, data);
  652. csr16w(ctlr, Ecr, data|EEsk);
  653. delay(1);
  654. csr16w(ctlr, Ecr, data);
  655. microdelay(1);
  656. if(!(csr16r(ctlr, Ecr) & EEdo))
  657. break;
  658. }
  659. data = 0;
  660. for(i = 15; i >= 0; i--){
  661. csr16w(ctlr, Ecr, EEcs|EEsk);
  662. microdelay(1);
  663. if(csr16r(ctlr, Ecr) & EEdo)
  664. data |= (1<<i);
  665. csr16w(ctlr, Ecr, EEcs);
  666. microdelay(1);
  667. }
  668. csr16w(ctlr, Ecr, 0);
  669. return data;
  670. }
  671. typedef struct Adapter {
  672. int port;
  673. int irq;
  674. int tbdf;
  675. } Adapter;
  676. static Msgbuf* adapter;
  677. static void
  678. i82557adapter(Msgbuf** mbb, int port, int irq, int tbdf)
  679. {
  680. Msgbuf *mb;
  681. Adapter *ap;
  682. mb = mballoc(sizeof(Adapter), 0, Maeth1);
  683. ap = (Adapter*)mb->data;
  684. ap->port = port;
  685. ap->irq = irq;
  686. ap->tbdf = tbdf;
  687. mb->next = *mbb;
  688. *mbb = mb;
  689. }
  690. static void
  691. i82557pci(void)
  692. {
  693. Pcidev *p;
  694. p = nil;
  695. while(p = pcimatch(p, 0x8086, 0)){
  696. switch(p->did){
  697. default:
  698. continue;
  699. case 0x1209: /* Intel 82559ER */
  700. case 0x1229: /* Intel 8255[789] */
  701. case 0x1031: /* Intel 82562EM */
  702. case 0x2449: /* Intel 82562ET */
  703. break;
  704. }
  705. /*
  706. * bar[0] is the memory-mapped register address (4KB),
  707. * bar[1] is the I/O port register address (32 bytes) and
  708. * bar[2] is for the flash ROM (1MB).
  709. */
  710. i82557adapter(&adapter, p->mem[1].bar & ~0x01, p->intl, p->tbdf);
  711. pcisetbme(p);
  712. }
  713. }
  714. static char* mediatable[9] = {
  715. "10BASE-T", /* TP */
  716. "10BASE-2", /* BNC */
  717. "10BASE-5", /* AUI */
  718. "100BASE-TX",
  719. "10BASE-TFD",
  720. "100BASE-TXFD",
  721. "100BASE-T4",
  722. "100BASE-FX",
  723. "100BASE-FXFD",
  724. };
  725. int
  726. etheri82557reset(Ether* ether)
  727. {
  728. int anar, anlpar, bmcr, bmsr, i, k, medium, phyaddr, port, x;
  729. unsigned short sum;
  730. Msgbuf *mb, **mbb;
  731. Adapter *ap;
  732. uchar ea[Easize];
  733. Ctlr *ctlr;
  734. static int scandone;
  735. if(scandone == 0){
  736. i82557pci();
  737. scandone = 1;
  738. }
  739. /*
  740. * Any adapter matches if no port is supplied,
  741. * otherwise the ports must match.
  742. */
  743. port = 0;
  744. mbb = &adapter;
  745. for(mb = *mbb; mb; mb = mb->next){
  746. ap = (Adapter*)mb->data;
  747. if(ether->port == 0 || ether->port == ap->port){
  748. port = ap->port;
  749. ether->irq = ap->irq;
  750. ether->tbdf = ap->tbdf;
  751. *mbb = mb->next;
  752. mbfree(mb);
  753. break;
  754. }
  755. mbb = &mb->next;
  756. }
  757. if(port == 0)
  758. return -1;
  759. /*
  760. * Allocate a controller structure and start to initialise it.
  761. * Perform a software reset after which should ensure busmastering
  762. * is still enabled. The EtherExpress PRO/100B appears to leave
  763. * the PCI configuration alone (see the 'To do' list above) so punt
  764. * for now.
  765. * Load the RUB and CUB registers for linear addressing (0).
  766. */
  767. ether->ctlr = ialloc(sizeof(Ctlr), 0);
  768. ctlr = ether->ctlr;
  769. ctlr->port = port;
  770. ilock(&ctlr->rlock);
  771. csr32w(ctlr, Port, 0);
  772. delay(1);
  773. csr8w(ctlr, Interrupt, InterruptM);
  774. iunlock(&ctlr->rlock);
  775. command(ctlr, LoadRUB, 0);
  776. command(ctlr, LoadCUB, 0);
  777. command(ctlr, LoadDCA, PADDR(ctlr->dump));
  778. /*
  779. * Initialise the receive frame, transmit ring and configuration areas.
  780. */
  781. ctlr->ncb = Ncb;
  782. ctlrinit(ctlr);
  783. /*
  784. * Read the EEPROM.
  785. */
  786. sum = 0;
  787. for(i = 0; i < 0x40; i++){
  788. x = hy93c46r(ctlr, i);
  789. ctlr->eeprom[i] = x;
  790. sum += x;
  791. }
  792. if(sum != 0xBABA)
  793. print("#l%d: EEPROM checksum - 0x%4.4ux\n", ether->ctlrno, sum);
  794. /*
  795. * Eeprom[6] indicates whether there is a PHY and whether
  796. * it's not 10Mb-only, in which case use the given PHY address
  797. * to set any PHY specific options and determine the speed.
  798. * If no PHY, assume 82503 (serial) operation.
  799. */
  800. if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000)){
  801. phyaddr = ctlr->eeprom[6] & 0x00FF;
  802. /*
  803. * Resolve the highest common ability of the two
  804. * link partners. In descending order:
  805. * 0x0100 100BASE-TX Full Duplex
  806. * 0x0200 100BASE-T4
  807. * 0x0080 100BASE-TX
  808. * 0x0040 10BASE-T Full Duplex
  809. * 0x0020 10BASE-T
  810. */
  811. anar = miir(ctlr, phyaddr, 0x04);
  812. anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
  813. anar &= anlpar;
  814. bmcr = 0;
  815. if(anar & 0x380)
  816. bmcr = 0x2000;
  817. if(anar & 0x0140)
  818. bmcr |= 0x0100;
  819. switch((ctlr->eeprom[6]>>8) & 0x001F){
  820. case 0x04: /* DP83840 */
  821. case 0x0A: /* DP83840A */
  822. /*
  823. * The DP83840[A] requires some tweaking for
  824. * reliable operation.
  825. * The manual says bit 10 should be unconditionally
  826. * set although it supposedly only affects full-duplex
  827. * operation (an & 0x0140).
  828. */
  829. x = miir(ctlr, phyaddr, 0x17) & ~0x0520;
  830. x |= 0x0420;
  831. for(i = 0; i < ether->nopt; i++){
  832. if(cistrcmp(ether->opt[i], "congestioncontrol"))
  833. continue;
  834. x |= 0x0100;
  835. break;
  836. }
  837. miiw(ctlr, phyaddr, 0x17, x);
  838. /*
  839. * If the link partner can't autonegotiate, determine
  840. * the speed from elsewhere.
  841. */
  842. if(anlpar == 0){
  843. miir(ctlr, phyaddr, 0x01);
  844. bmsr = miir(ctlr, phyaddr, 0x01);
  845. x = miir(ctlr, phyaddr, 0x19);
  846. if((bmsr & 0x0004) && !(x & 0x0040))
  847. bmcr = 0x2000;
  848. }
  849. break;
  850. case 0x07: /* Intel 82555 */
  851. /*
  852. * Auto-negotiation may fail if the other end is
  853. * a DP83840A and the cable is short.
  854. */
  855. miir(ctlr, phyaddr, 0x01);
  856. bmsr = miir(ctlr, phyaddr, 0x01);
  857. if((miir(ctlr, phyaddr, 0) & 0x1000) && !(bmsr & 0x0020)){
  858. miiw(ctlr, phyaddr, 0x1A, 0x2010);
  859. x = miir(ctlr, phyaddr, 0);
  860. miiw(ctlr, phyaddr, 0, 0x0200|x);
  861. for(i = 0; i < 3000; i++){
  862. delay(1);
  863. if(miir(ctlr, phyaddr, 0x01) & 0x0020)
  864. break;
  865. }
  866. miiw(ctlr, phyaddr, 0x1A, 0x2000);
  867. anar = miir(ctlr, phyaddr, 0x04);
  868. anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
  869. anar &= anlpar;
  870. bmcr = 0;
  871. if(anar & 0x380)
  872. bmcr = 0x2000;
  873. if(anar & 0x0140)
  874. bmcr |= 0x0100;
  875. }
  876. break;
  877. }
  878. /*
  879. * Force speed and duplex if no auto-negotiation.
  880. */
  881. if(anlpar == 0){
  882. medium = -1;
  883. for(i = 0; i < ether->nopt; i++){
  884. for(k = 0; k < nelem(mediatable); k++){
  885. if(cistrcmp(mediatable[k], ether->opt[i]))
  886. continue;
  887. medium = k;
  888. break;
  889. }
  890. switch(medium){
  891. default:
  892. break;
  893. case 0x00: /* 10BASE-T */
  894. case 0x01: /* 10BASE-2 */
  895. case 0x02: /* 10BASE-5 */
  896. bmcr &= ~(0x2000|0x0100);
  897. ctlr->configdata[19] &= ~0x40;
  898. break;
  899. case 0x03: /* 100BASE-TX */
  900. case 0x06: /* 100BASE-T4 */
  901. case 0x07: /* 100BASE-FX */
  902. ctlr->configdata[19] &= ~0x40;
  903. bmcr |= 0x2000;
  904. break;
  905. case 0x04: /* 10BASE-TFD */
  906. bmcr = (bmcr & ~0x2000)|0x0100;
  907. ctlr->configdata[19] |= 0x40;
  908. break;
  909. case 0x05: /* 100BASE-TXFD */
  910. case 0x08: /* 100BASE-FXFD */
  911. bmcr |= 0x2000|0x0100;
  912. ctlr->configdata[19] |= 0x40;
  913. break;
  914. }
  915. }
  916. if(medium != -1)
  917. miiw(ctlr, phyaddr, 0x00, bmcr);
  918. }
  919. if(bmcr & 0x2000)
  920. ether->mbps = 100;
  921. else
  922. ether->mbps = 10;
  923. ctlr->configdata[8] = 1;
  924. ctlr->configdata[15] &= ~0x80;
  925. }
  926. else{
  927. ctlr->configdata[8] = 0;
  928. ctlr->configdata[15] |= 0x80;
  929. }
  930. /*
  931. * Load the chip configuration and start it off.
  932. */
  933. configure(ether, 0);
  934. command(ctlr, CUstart, PADDR(&ctlr->cbr->status));
  935. /*
  936. * Check if the adapter's station address is to be overridden.
  937. * If not, read it from the EEPROM and set in ether->ea prior to loading
  938. * the station address with the Individual Address Setup command.
  939. */
  940. memset(ea, 0, Easize);
  941. if(memcmp(ea, ether->ea, Easize) == 0){
  942. for(i = 0; i < Easize/2; i++){
  943. x = ctlr->eeprom[i];
  944. ether->ea[2*i] = x;
  945. ether->ea[2*i+1] = x>>8;
  946. }
  947. }
  948. ilock(&ctlr->cblock);
  949. ctlr->action = CbIAS;
  950. txstart(ether);
  951. iunlock(&ctlr->cblock);
  952. /*
  953. * Linkage to the generic ethernet driver.
  954. */
  955. ether->port = port;
  956. ether->attach = attach;
  957. ether->transmit = transmit;
  958. ether->interrupt = interrupt;
  959. return 0;
  960. }