ether.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ip.h"
  8. #include "etherif.h"
  9. static Ether ether[MaxEther];
  10. extern int ether2114xreset(Ether*);
  11. extern int elnk3reset(Ether*);
  12. extern int i82557reset(Ether*);
  13. extern int igbepnp(Ether *);
  14. extern int i82563pnp(Ether *);
  15. extern int elnk3reset(Ether*);
  16. extern int ether589reset(Ether*);
  17. extern int ne2000reset(Ether*);
  18. extern int wd8003reset(Ether*);
  19. extern int ec2treset(Ether*);
  20. extern int amd79c970reset(Ether*);
  21. extern int rtl8139pnp(Ether*);
  22. extern int rtl8169pnp(Ether*);
  23. extern int ether83815reset(Ether*);
  24. extern int rhinepnp(Ether*);
  25. extern int ga620pnp(Ether*);
  26. extern int dp83820pnp(Ether*);
  27. struct {
  28. char *type;
  29. int (*reset)(Ether*);
  30. int noprobe;
  31. } ethercards[] = {
  32. { "21140", ether2114xreset, 0, },
  33. { "2114x", ether2114xreset, 0, },
  34. { "i82557", i82557reset, 0, },
  35. { "igbe", igbepnp, 0, },
  36. { "i82563",i82563pnp, 0, },
  37. { "igbepcie",i82563pnp, 0, },
  38. { "elnk3", elnk3reset, 0, },
  39. { "3C509", elnk3reset, 0, },
  40. { "3C575", elnk3reset, 0, },
  41. { "3C589", ether589reset, 1, },
  42. { "3C562", ether589reset, 1, },
  43. { "589E", ether589reset, 1, },
  44. { "NE2000", ne2000reset, 0, },
  45. { "WD8003", wd8003reset, 1, },
  46. { "EC2T", ec2treset, 0, },
  47. { "AMD79C970", amd79c970reset, 0, },
  48. { "RTL8139", rtl8139pnp, 0, },
  49. { "RTL8169", rtl8169pnp, 0, },
  50. { "83815", ether83815reset, 0, },
  51. { "rhine", rhinepnp, 0, },
  52. { "vt6102", rhinepnp, 0, },
  53. { "GA620", ga620pnp, 0, },
  54. { "83820", dp83820pnp, 0, },
  55. { "dp83820", dp83820pnp, 0, },
  56. { 0, }
  57. };
  58. static void xetherdetach(void);
  59. int
  60. etherinit(void)
  61. {
  62. Ether *ctlr;
  63. int ctlrno, i, mask, n, x;
  64. fmtinstall('E', eipfmt);
  65. etherdetach = xetherdetach;
  66. mask = 0;
  67. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  68. ctlr = &ether[ctlrno];
  69. memset(ctlr, 0, sizeof(Ether));
  70. if(iniread && isaconfig("ether", ctlrno, ctlr) == 0)
  71. continue;
  72. for(n = 0; ethercards[n].type; n++){
  73. if(!iniread){
  74. if(ethercards[n].noprobe)
  75. continue;
  76. memset(ctlr, 0, sizeof(Ether));
  77. strcpy(ctlr->type, ethercards[n].type);
  78. }
  79. else if(cistrcmp(ethercards[n].type, ctlr->type))
  80. continue;
  81. ctlr->ctlrno = ctlrno;
  82. x = splhi();
  83. if((*ethercards[n].reset)(ctlr)){
  84. splx(x);
  85. if(iniread)
  86. break;
  87. else
  88. continue;
  89. }
  90. ctlr->state = 1; /* card found */
  91. mask |= 1<<ctlrno;
  92. if(ctlr->irq == 2)
  93. ctlr->irq = 9;
  94. setvec(VectorPIC + ctlr->irq, ctlr->interrupt, ctlr);
  95. print("ether#%d: %s: port 0x%luX irq %lud",
  96. ctlr->ctlrno, ctlr->type, ctlr->port, ctlr->irq);
  97. if(ctlr->mem)
  98. print(" addr 0x%luX", ctlr->mem & ~KZERO);
  99. if(ctlr->size)
  100. print(" size 0x%luX", ctlr->size);
  101. print(": %E\n", ctlr->ea);
  102. if(ctlr->nrb == 0)
  103. ctlr->nrb = Nrb;
  104. ctlr->rb = ialloc(sizeof(RingBuf)*ctlr->nrb, 0);
  105. if(ctlr->ntb == 0)
  106. ctlr->ntb = Ntb;
  107. ctlr->tb = ialloc(sizeof(RingBuf)*ctlr->ntb, 0);
  108. ctlr->rh = 0;
  109. ctlr->ri = 0;
  110. for(i = 0; i < ctlr->nrb; i++)
  111. ctlr->rb[i].owner = Interface;
  112. ctlr->th = 0;
  113. ctlr->ti = 0;
  114. for(i = 0; i < ctlr->ntb; i++)
  115. ctlr->tb[i].owner = Host;
  116. splx(x);
  117. break;
  118. }
  119. }
  120. return mask;
  121. }
  122. void
  123. etherinitdev(int i, char *s)
  124. {
  125. sprint(s, "ether%d", i);
  126. }
  127. void
  128. etherprintdevs(int i)
  129. {
  130. print(" ether%d", i);
  131. }
  132. static Ether*
  133. attach(int ctlrno)
  134. {
  135. Ether *ctlr;
  136. if(ctlrno >= MaxEther || ether[ctlrno].state == 0)
  137. return 0;
  138. ctlr = &ether[ctlrno];
  139. if(ctlr->state == 1){ /* card found? */
  140. ctlr->state = 2; /* attaching */
  141. (*ctlr->attach)(ctlr);
  142. }
  143. return ctlr;
  144. }
  145. static void
  146. xetherdetach(void)
  147. {
  148. Ether *ctlr;
  149. int ctlrno, x;
  150. x = splhi();
  151. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  152. ctlr = &ether[ctlrno];
  153. if(ctlr->detach && ctlr->state != 0) /* found | attaching? */
  154. ctlr->detach(ctlr);
  155. }
  156. splx(x);
  157. }
  158. uchar*
  159. etheraddr(int ctlrno)
  160. {
  161. Ether *ctlr;
  162. if((ctlr = attach(ctlrno)) == 0)
  163. return 0;
  164. return ctlr->ea;
  165. }
  166. static int
  167. wait(RingBuf* ring, uchar owner, int timo)
  168. {
  169. ulong start;
  170. start = m->ticks;
  171. while(TK2MS(m->ticks - start) < timo){
  172. if(ring->owner != owner)
  173. return 1;
  174. }
  175. return 0;
  176. }
  177. int
  178. etherrxpkt(int ctlrno, Etherpkt* pkt, int timo)
  179. {
  180. int n;
  181. Ether *ctlr;
  182. RingBuf *ring;
  183. if((ctlr = attach(ctlrno)) == 0)
  184. return 0;
  185. ring = &ctlr->rb[ctlr->rh];
  186. if(wait(ring, Interface, timo) == 0){
  187. if(debug)
  188. print("ether%d: rx timeout\n", ctlrno);
  189. return 0;
  190. }
  191. n = ring->len;
  192. memmove(pkt, ring->pkt, n);
  193. ring->owner = Interface;
  194. ctlr->rh = NEXT(ctlr->rh, ctlr->nrb);
  195. return n;
  196. }
  197. int
  198. etherrxflush(int ctlrno)
  199. {
  200. int n;
  201. Ether *ctlr;
  202. RingBuf *ring;
  203. if((ctlr = attach(ctlrno)) == 0)
  204. return 0;
  205. n = 0;
  206. for(;;){
  207. ring = &ctlr->rb[ctlr->rh];
  208. if(wait(ring, Interface, 100) == 0)
  209. break;
  210. ring->owner = Interface;
  211. ctlr->rh = NEXT(ctlr->rh, ctlr->nrb);
  212. n++;
  213. }
  214. return n;
  215. }
  216. int
  217. ethertxpkt(int ctlrno, Etherpkt* pkt, int len, int)
  218. {
  219. Ether *ctlr;
  220. RingBuf *ring;
  221. int s;
  222. if((ctlr = attach(ctlrno)) == 0)
  223. return 0;
  224. ring = &ctlr->tb[ctlr->th];
  225. if(wait(ring, Interface, 1000) == 0){
  226. print("ether%d: tx buffer timeout\n", ctlrno);
  227. return 0;
  228. }
  229. memmove(pkt->s, ctlr->ea, Eaddrlen);
  230. if(debug)
  231. print("%E to %E...\n", pkt->s, pkt->d);
  232. memmove(ring->pkt, pkt, len);
  233. if(len < ETHERMINTU){
  234. memset(ring->pkt+len, 0, ETHERMINTU-len);
  235. len = ETHERMINTU;
  236. }
  237. ring->len = len;
  238. ring->owner = Interface;
  239. ctlr->th = NEXT(ctlr->th, ctlr->ntb);
  240. s = splhi();
  241. (*ctlr->transmit)(ctlr);
  242. splx(s);
  243. return 1;
  244. }