devether.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "ureg.h"
  8. #include "../port/error.h"
  9. #include "../port/netif.h"
  10. #include "etherif.h"
  11. static Ether *etherxx[MaxEther];
  12. Chan*
  13. etherattach(char* spec)
  14. {
  15. ulong ctlrno;
  16. char *p;
  17. Chan *chan;
  18. ctlrno = 0;
  19. if(spec && *spec){
  20. ctlrno = strtoul(spec, &p, 0);
  21. if((ctlrno == 0 && p == spec) || *p || (ctlrno >= MaxEther))
  22. error(Ebadarg);
  23. }
  24. if(etherxx[ctlrno] == 0)
  25. error(Enodev);
  26. chan = devattach('l', spec);
  27. chan->dev = ctlrno;
  28. if(etherxx[ctlrno]->attach)
  29. etherxx[ctlrno]->attach(etherxx[ctlrno]);
  30. return chan;
  31. }
  32. static Walkqid*
  33. etherwalk(Chan* chan, Chan* nchan, char** name, int nname)
  34. {
  35. return netifwalk(etherxx[chan->dev], chan, nchan, name, nname);
  36. }
  37. static int
  38. etherstat(Chan* chan, uchar* dp, int n)
  39. {
  40. return netifstat(etherxx[chan->dev], chan, dp, n);
  41. }
  42. static Chan*
  43. etheropen(Chan* chan, int omode)
  44. {
  45. return netifopen(etherxx[chan->dev], chan, omode);
  46. }
  47. static void
  48. ethercreate(Chan*, char*, int, ulong)
  49. {
  50. }
  51. static void
  52. etherclose(Chan* chan)
  53. {
  54. netifclose(etherxx[chan->dev], chan);
  55. }
  56. static long
  57. etherread(Chan* chan, void* buf, long n, vlong off)
  58. {
  59. Ether *ether;
  60. ulong offset = off;
  61. ether = etherxx[chan->dev];
  62. if((chan->qid.type & QTDIR) == 0 && ether->ifstat){
  63. /*
  64. * With some controllers it is necessary to reach
  65. * into the chip to extract statistics.
  66. */
  67. if(NETTYPE(chan->qid.path) == Nifstatqid)
  68. return ether->ifstat(ether, buf, n, offset);
  69. else if(NETTYPE(chan->qid.path) == Nstatqid)
  70. ether->ifstat(ether, buf, 0, offset);
  71. }
  72. return netifread(ether, chan, buf, n, offset);
  73. }
  74. static Block*
  75. etherbread(Chan* chan, long n, ulong offset)
  76. {
  77. return netifbread(etherxx[chan->dev], chan, n, offset);
  78. }
  79. static int
  80. etherwstat(Chan* chan, uchar* dp, int n)
  81. {
  82. return netifwstat(etherxx[chan->dev], chan, dp, n);
  83. }
  84. static void
  85. etherrtrace(Netfile* f, Etherpkt* pkt, int len)
  86. {
  87. int i, n;
  88. Block *bp;
  89. if(qwindow(f->in) <= 0)
  90. return;
  91. if(len > 58)
  92. n = 58;
  93. else
  94. n = len;
  95. bp = iallocb(64);
  96. if(bp == nil)
  97. return;
  98. memmove(bp->wp, pkt->d, n);
  99. i = TK2MS(MACHP(0)->ticks);
  100. bp->wp[58] = len>>8;
  101. bp->wp[59] = len;
  102. bp->wp[60] = i>>24;
  103. bp->wp[61] = i>>16;
  104. bp->wp[62] = i>>8;
  105. bp->wp[63] = i;
  106. bp->wp += 64;
  107. qpass(f->in, bp);
  108. }
  109. Block*
  110. etheriq(Ether* ether, Block* bp, int fromwire)
  111. {
  112. Etherpkt *pkt;
  113. ushort type;
  114. int len, multi, tome, fromme;
  115. Netfile **ep, *f, **fp, *fx;
  116. Block *xbp;
  117. ether->inpackets++;
  118. pkt = (Etherpkt*)bp->rp;
  119. len = BLEN(bp);
  120. type = (pkt->type[0]<<8)|pkt->type[1];
  121. fx = 0;
  122. ep = &ether->f[Ntypes];
  123. multi = pkt->d[0] & 1;
  124. /* check for valid multcast addresses */
  125. if(multi && memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) != 0 && ether->prom == 0){
  126. if(!activemulti(ether, pkt->d, sizeof(pkt->d))){
  127. if(fromwire){
  128. freeb(bp);
  129. bp = 0;
  130. }
  131. return bp;
  132. }
  133. }
  134. /* is it for me? */
  135. tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  136. fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
  137. /*
  138. * Multiplex the packet to all the connections which want it.
  139. * If the packet is not to be used subsequently (fromwire != 0),
  140. * attempt to simply pass it into one of the connections, thereby
  141. * saving a copy of the data (usual case hopefully).
  142. */
  143. for(fp = ether->f; fp < ep; fp++){
  144. if(f = *fp)
  145. if(f->type == type || f->type < 0)
  146. if(tome || multi || f->prom){
  147. /* Don't want to hear bridged packets */
  148. if(f->bridge && !fromwire && !fromme)
  149. continue;
  150. if(!f->headersonly){
  151. if(fromwire && fx == 0)
  152. fx = f;
  153. else if(xbp = iallocb(len)){
  154. memmove(xbp->wp, pkt, len);
  155. xbp->wp += len;
  156. if(qpass(f->in, xbp) < 0)
  157. ether->soverflows++;
  158. }
  159. else
  160. ether->soverflows++;
  161. }
  162. else
  163. etherrtrace(f, pkt, len);
  164. }
  165. }
  166. if(fx){
  167. if(qpass(fx->in, bp) < 0)
  168. ether->soverflows++;
  169. return 0;
  170. }
  171. if(fromwire){
  172. freeb(bp);
  173. return 0;
  174. }
  175. return bp;
  176. }
  177. static int
  178. etheroq(Ether* ether, Block* bp)
  179. {
  180. int len, loopback, s;
  181. Etherpkt *pkt;
  182. ether->outpackets++;
  183. /*
  184. * Check if the packet has to be placed back onto the input queue,
  185. * i.e. if it's a loopback or broadcast packet or the interface is
  186. * in promiscuous mode.
  187. * If it's a loopback packet indicate to etheriq that the data isn't
  188. * needed and return, etheriq will pass-on or free the block.
  189. * To enable bridging to work, only packets that were originated
  190. * by this interface are fed back.
  191. */
  192. pkt = (Etherpkt*)bp->rp;
  193. len = BLEN(bp);
  194. loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  195. if(loopback || memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) == 0 || ether->prom){
  196. s = splhi();
  197. etheriq(ether, bp, 0);
  198. splx(s);
  199. }
  200. if(!loopback){
  201. qbwrite(ether->oq, bp);
  202. if(ether->transmit != nil)
  203. ether->transmit(ether);
  204. } else
  205. freeb(bp);
  206. return len;
  207. }
  208. static long
  209. etherwrite(Chan* chan, void* buf, long n, vlong)
  210. {
  211. Ether *ether;
  212. Block *bp;
  213. int nn, onoff;
  214. Cmdbuf *cb;
  215. ether = etherxx[chan->dev];
  216. if(NETTYPE(chan->qid.path) != Ndataqid) {
  217. nn = netifwrite(ether, chan, buf, n);
  218. if(nn >= 0)
  219. return nn;
  220. cb = parsecmd(buf, n);
  221. if(strcmp(cb->f[0], "nonblocking") == 0){
  222. if(cb->nf <= 1)
  223. onoff = 1;
  224. else
  225. onoff = atoi(cb->f[1]);
  226. qnoblock(ether->oq, onoff);
  227. free(cb);
  228. return n;
  229. }
  230. free(cb);
  231. if(ether->ctl!=nil)
  232. return ether->ctl(ether,buf,n);
  233. error(Ebadctl);
  234. }
  235. if(n > ether->maxmtu)
  236. error(Etoobig);
  237. if(n < ether->minmtu)
  238. error(Etoosmall);
  239. bp = allocb(n);
  240. if(waserror()){
  241. freeb(bp);
  242. nexterror();
  243. }
  244. memmove(bp->rp, buf, n);
  245. memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
  246. poperror();
  247. bp->wp += n;
  248. return etheroq(ether, bp);
  249. }
  250. static long
  251. etherbwrite(Chan* chan, Block* bp, ulong)
  252. {
  253. Ether *ether;
  254. long n;
  255. n = BLEN(bp);
  256. if(NETTYPE(chan->qid.path) != Ndataqid){
  257. if(waserror()) {
  258. freeb(bp);
  259. nexterror();
  260. }
  261. n = etherwrite(chan, bp->rp, n, 0);
  262. poperror();
  263. freeb(bp);
  264. return n;
  265. }
  266. ether = etherxx[chan->dev];
  267. if(n > ether->maxmtu){
  268. freeb(bp);
  269. error(Etoobig);
  270. }
  271. if(n < ether->minmtu){
  272. freeb(bp);
  273. error(Etoosmall);
  274. }
  275. return etheroq(ether, bp);
  276. }
  277. static struct {
  278. char* type;
  279. int (*reset)(Ether*);
  280. } cards[MaxEther+1];
  281. void
  282. addethercard(char* t, int (*r)(Ether*))
  283. {
  284. static int ncard;
  285. if(ncard == MaxEther)
  286. panic("too many ether cards");
  287. cards[ncard].type = t;
  288. cards[ncard].reset = r;
  289. ncard++;
  290. }
  291. int
  292. parseether(uchar *to, char *from)
  293. {
  294. char nip[4];
  295. char *p;
  296. int i;
  297. p = from;
  298. for(i = 0; i < Eaddrlen; i++){
  299. if(*p == 0)
  300. return -1;
  301. nip[0] = *p++;
  302. if(*p == 0)
  303. return -1;
  304. nip[1] = *p++;
  305. nip[2] = 0;
  306. to[i] = strtoul(nip, 0, 16);
  307. if(*p == ':')
  308. p++;
  309. }
  310. return 0;
  311. }
  312. static Ether*
  313. etherprobe(int cardno, int ctlrno)
  314. {
  315. int i;
  316. Ether *ether;
  317. char buf[128], name[32];
  318. ether = malloc(sizeof(Ether));
  319. memset(ether, 0, sizeof(Ether));
  320. ether->ctlrno = ctlrno;
  321. ether->tbdf = BUSUNKNOWN;
  322. ether->mbps = 10;
  323. ether->minmtu = ETHERMINTU;
  324. ether->maxmtu = ETHERMAXTU;
  325. if(cardno < 0){
  326. if(isaconfig("ether", ctlrno, ether) == 0){
  327. free(ether);
  328. return nil;
  329. }
  330. for(cardno = 0; cards[cardno].type; cardno++){
  331. if(cistrcmp(cards[cardno].type, ether->type))
  332. continue;
  333. for(i = 0; i < ether->nopt; i++){
  334. if(strncmp(ether->opt[i], "ea=", 3))
  335. continue;
  336. if(parseether(ether->ea, &ether->opt[i][3]))
  337. memset(ether->ea, 0, Eaddrlen);
  338. }
  339. break;
  340. }
  341. }
  342. if(cardno >= MaxEther || cards[cardno].type == nil){
  343. free(ether);
  344. return nil;
  345. }
  346. if(cards[cardno].reset(ether) < 0){
  347. free(ether);
  348. return nil;
  349. }
  350. /*
  351. * IRQ2 doesn't really exist, it's used to gang the interrupt
  352. * controllers together. A device set to IRQ2 will appear on
  353. * the second interrupt controller as IRQ9.
  354. */
  355. if(ether->irq == 2)
  356. ether->irq = 9;
  357. snprint(name, sizeof(name), "ether%d", ctlrno);
  358. /*
  359. * If ether->irq is <0, it is a hack to indicate no interrupt
  360. * used by ethersink.
  361. */
  362. if(ether->irq >= 0)
  363. intrenable(ether->irq, ether->interrupt, ether, ether->tbdf, name);
  364. i = sprint(buf, "#l%d: %s: %dMbps port 0x%luX irq %d",
  365. ctlrno, cards[cardno].type, ether->mbps, ether->port, ether->irq);
  366. if(ether->mem)
  367. i += sprint(buf+i, " addr 0x%luX", ether->mem);
  368. if(ether->size)
  369. i += sprint(buf+i, " size 0x%luX", ether->size);
  370. i += sprint(buf+i, ": %2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  371. ether->ea[0], ether->ea[1], ether->ea[2],
  372. ether->ea[3], ether->ea[4], ether->ea[5]);
  373. sprint(buf+i, "\n");
  374. print(buf);
  375. if(ether->mbps >= 1000){
  376. netifinit(ether, name, Ntypes, 512*1024);
  377. if(ether->oq == 0)
  378. ether->oq = qopen(512*1024, Qmsg, 0, 0);
  379. }else if(ether->mbps >= 100){
  380. netifinit(ether, name, Ntypes, 256*1024);
  381. if(ether->oq == 0)
  382. ether->oq = qopen(256*1024, Qmsg, 0, 0);
  383. }else{
  384. netifinit(ether, name, Ntypes, 128*1024);
  385. if(ether->oq == 0)
  386. ether->oq = qopen(128*1024, Qmsg, 0, 0);
  387. }
  388. if(ether->oq == 0)
  389. panic("etherreset %s", name);
  390. ether->alen = Eaddrlen;
  391. memmove(ether->addr, ether->ea, Eaddrlen);
  392. memset(ether->bcast, 0xFF, Eaddrlen);
  393. return ether;
  394. }
  395. static void
  396. etherreset(void)
  397. {
  398. Ether *ether;
  399. int cardno, ctlrno;
  400. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  401. if((ether = etherprobe(-1, ctlrno)) == nil)
  402. continue;
  403. etherxx[ctlrno] = ether;
  404. }
  405. if(getconf("*noetherprobe"))
  406. return;
  407. cardno = ctlrno = 0;
  408. while(cards[cardno].type != nil && ctlrno < MaxEther){
  409. if(etherxx[ctlrno] != nil){
  410. ctlrno++;
  411. continue;
  412. }
  413. if((ether = etherprobe(cardno, ctlrno)) == nil){
  414. cardno++;
  415. continue;
  416. }
  417. etherxx[ctlrno] = ether;
  418. ctlrno++;
  419. }
  420. }
  421. static void
  422. ethershutdown(void)
  423. {
  424. Ether *ether;
  425. int i;
  426. for(i = 0; i < MaxEther; i++){
  427. ether = etherxx[i];
  428. if(ether == nil)
  429. continue;
  430. if(ether->shutdown == nil) {
  431. print("#l%d: no shutdown fuction\n", i);
  432. continue;
  433. }
  434. (*ether->shutdown)(ether);
  435. }
  436. }
  437. #define POLY 0xedb88320
  438. /* really slow 32 bit crc for ethers */
  439. ulong
  440. ethercrc(uchar *p, int len)
  441. {
  442. int i, j;
  443. ulong crc, b;
  444. crc = 0xffffffff;
  445. for(i = 0; i < len; i++){
  446. b = *p++;
  447. for(j = 0; j < 8; j++){
  448. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  449. b >>= 1;
  450. }
  451. }
  452. return crc;
  453. }
  454. Dev etherdevtab = {
  455. 'l',
  456. "ether",
  457. etherreset,
  458. devinit,
  459. ethershutdown,
  460. etherattach,
  461. etherwalk,
  462. etherstat,
  463. etheropen,
  464. ethercreate,
  465. etherclose,
  466. etherread,
  467. etherbread,
  468. etherwrite,
  469. etherbwrite,
  470. devremove,
  471. etherwstat,
  472. };