devether.c 10 KB

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