ether.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. { "GA620", ga620pnp, 0, },
  53. { "83820", dp83820pnp, 0, },
  54. { "dp83820", dp83820pnp, 0, },
  55. { 0, }
  56. };
  57. static void xetherdetach(void);
  58. int
  59. etherinit(void)
  60. {
  61. Ether *ctlr;
  62. int ctlrno, i, mask, n, x;
  63. fmtinstall('E', eipfmt);
  64. etherdetach = xetherdetach;
  65. mask = 0;
  66. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  67. ctlr = &ether[ctlrno];
  68. memset(ctlr, 0, sizeof(Ether));
  69. if(iniread && isaconfig("ether", ctlrno, ctlr) == 0)
  70. continue;
  71. for(n = 0; ethercards[n].type; n++){
  72. if(!iniread){
  73. if(ethercards[n].noprobe)
  74. continue;
  75. memset(ctlr, 0, sizeof(Ether));
  76. strcpy(ctlr->type, ethercards[n].type);
  77. }
  78. else if(cistrcmp(ethercards[n].type, ctlr->type))
  79. continue;
  80. ctlr->ctlrno = ctlrno;
  81. x = splhi();
  82. if((*ethercards[n].reset)(ctlr)){
  83. splx(x);
  84. if(iniread)
  85. break;
  86. else
  87. continue;
  88. }
  89. ctlr->state = 1; /* card found */
  90. mask |= 1<<ctlrno;
  91. if(ctlr->irq == 2)
  92. ctlr->irq = 9;
  93. setvec(VectorPIC + ctlr->irq, ctlr->interrupt, ctlr);
  94. print("ether#%d: %s: port 0x%luX irq %lud",
  95. ctlr->ctlrno, ctlr->type, ctlr->port, ctlr->irq);
  96. if(ctlr->mem)
  97. print(" addr 0x%luX", ctlr->mem & ~KZERO);
  98. if(ctlr->size)
  99. print(" size 0x%luX", ctlr->size);
  100. print(": %E\n", ctlr->ea);
  101. if(ctlr->nrb == 0)
  102. ctlr->nrb = Nrb;
  103. ctlr->rb = ialloc(sizeof(RingBuf)*ctlr->nrb, 0);
  104. if(ctlr->ntb == 0)
  105. ctlr->ntb = Ntb;
  106. ctlr->tb = ialloc(sizeof(RingBuf)*ctlr->ntb, 0);
  107. ctlr->rh = 0;
  108. ctlr->ri = 0;
  109. for(i = 0; i < ctlr->nrb; i++)
  110. ctlr->rb[i].owner = Interface;
  111. ctlr->th = 0;
  112. ctlr->ti = 0;
  113. for(i = 0; i < ctlr->ntb; i++)
  114. ctlr->tb[i].owner = Host;
  115. splx(x);
  116. break;
  117. }
  118. }
  119. return mask;
  120. }
  121. void
  122. etherinitdev(int i, char *s)
  123. {
  124. sprint(s, "ether%d", i);
  125. }
  126. void
  127. etherprintdevs(int i)
  128. {
  129. print(" ether%d", i);
  130. }
  131. static Ether*
  132. attach(int ctlrno)
  133. {
  134. Ether *ctlr;
  135. if(ctlrno >= MaxEther || ether[ctlrno].state == 0)
  136. return 0;
  137. ctlr = &ether[ctlrno];
  138. if(ctlr->state == 1){ /* card found? */
  139. ctlr->state = 2; /* attaching */
  140. (*ctlr->attach)(ctlr);
  141. }
  142. return ctlr;
  143. }
  144. static void
  145. xetherdetach(void)
  146. {
  147. Ether *ctlr;
  148. int ctlrno, x;
  149. x = splhi();
  150. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  151. ctlr = &ether[ctlrno];
  152. if(ctlr->detach && ctlr->state != 0) /* found | attaching? */
  153. ctlr->detach(ctlr);
  154. }
  155. splx(x);
  156. }
  157. uchar*
  158. etheraddr(int ctlrno)
  159. {
  160. Ether *ctlr;
  161. if((ctlr = attach(ctlrno)) == 0)
  162. return 0;
  163. return ctlr->ea;
  164. }
  165. static int
  166. wait(RingBuf* ring, uchar owner, int timo)
  167. {
  168. ulong start;
  169. start = m->ticks;
  170. while(TK2MS(m->ticks - start) < timo){
  171. if(ring->owner != owner)
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. int
  177. etherrxpkt(int ctlrno, Etherpkt* pkt, int timo)
  178. {
  179. int n;
  180. Ether *ctlr;
  181. RingBuf *ring;
  182. if((ctlr = attach(ctlrno)) == 0)
  183. return 0;
  184. ring = &ctlr->rb[ctlr->rh];
  185. if(wait(ring, Interface, timo) == 0){
  186. if(debug)
  187. print("ether%d: rx timeout\n", ctlrno);
  188. return 0;
  189. }
  190. n = ring->len;
  191. memmove(pkt, ring->pkt, n);
  192. ring->owner = Interface;
  193. ctlr->rh = NEXT(ctlr->rh, ctlr->nrb);
  194. return n;
  195. }
  196. int
  197. etherrxflush(int ctlrno)
  198. {
  199. int n;
  200. Ether *ctlr;
  201. RingBuf *ring;
  202. if((ctlr = attach(ctlrno)) == 0)
  203. return 0;
  204. n = 0;
  205. for(;;){
  206. ring = &ctlr->rb[ctlr->rh];
  207. if(wait(ring, Interface, 100) == 0)
  208. break;
  209. ring->owner = Interface;
  210. ctlr->rh = NEXT(ctlr->rh, ctlr->nrb);
  211. n++;
  212. }
  213. return n;
  214. }
  215. int
  216. ethertxpkt(int ctlrno, Etherpkt* pkt, int len, int)
  217. {
  218. Ether *ctlr;
  219. RingBuf *ring;
  220. int s;
  221. if((ctlr = attach(ctlrno)) == 0)
  222. return 0;
  223. ring = &ctlr->tb[ctlr->th];
  224. if(wait(ring, Interface, 1000) == 0){
  225. print("ether%d: tx buffer timeout\n", ctlrno);
  226. return 0;
  227. }
  228. memmove(pkt->s, ctlr->ea, Eaddrlen);
  229. if(debug)
  230. print("%E to %E...\n", pkt->s, pkt->d);
  231. memmove(ring->pkt, pkt, len);
  232. if(len < ETHERMINTU){
  233. memset(ring->pkt+len, 0, ETHERMINTU-len);
  234. len = ETHERMINTU;
  235. }
  236. ring->len = len;
  237. ring->owner = Interface;
  238. ctlr->th = NEXT(ctlr->th, ctlr->ntb);
  239. s = splhi();
  240. (*ctlr->transmit)(ctlr);
  241. splx(s);
  242. return 1;
  243. }