ether.c 5.0 KB

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