| 1 | /* if_en.c 4.69 82/10/10 */ |
| 2 | |
| 3 | #include "en.h" |
| 4 | |
| 5 | /* |
| 6 | * Xerox prototype (3 Mb) Ethernet interface driver. |
| 7 | */ |
| 8 | |
| 9 | #include "../h/param.h" |
| 10 | #include "../h/systm.h" |
| 11 | #include "../h/mbuf.h" |
| 12 | #include "../h/pte.h" |
| 13 | #include "../h/buf.h" |
| 14 | #include "../h/protosw.h" |
| 15 | #include "../h/socket.h" |
| 16 | #include "../h/vmmac.h" |
| 17 | #include <errno.h> |
| 18 | |
| 19 | #include "../net/if.h" |
| 20 | #include "../net/netisr.h" |
| 21 | #include "../net/route.h" |
| 22 | #include "../netinet/in.h" |
| 23 | #include "../netinet/in_systm.h" |
| 24 | #include "../netinet/ip.h" |
| 25 | #include "../netinet/ip_var.h" |
| 26 | #include "../netpup/pup.h" |
| 27 | |
| 28 | #include "../vax/cpu.h" |
| 29 | #include "../vax/mtpr.h" |
| 30 | #include "../vaxif/if_en.h" |
| 31 | #include "../vaxif/if_enreg.h" |
| 32 | #include "../vaxif/if_uba.h" |
| 33 | #include "../vaxuba/ubareg.h" |
| 34 | #include "../vaxuba/ubavar.h" |
| 35 | |
| 36 | #define ENMTU (1024+512) |
| 37 | #define ENMRU (1024+512+16) /* 16 is enough to receive trailer */ |
| 38 | |
| 39 | int enprobe(), enattach(), enrint(), enxint(), encollide(); |
| 40 | struct uba_device *eninfo[NEN]; |
| 41 | u_short enstd[] = { 0 }; |
| 42 | struct uba_driver endriver = |
| 43 | { enprobe, 0, enattach, 0, enstd, "en", eninfo }; |
| 44 | #define ENUNIT(x) minor(x) |
| 45 | |
| 46 | int eninit(),enoutput(),enreset(); |
| 47 | |
| 48 | /* |
| 49 | * Ethernet software status per interface. |
| 50 | * |
| 51 | * Each interface is referenced by a network interface structure, |
| 52 | * es_if, which the routing code uses to locate the interface. |
| 53 | * This structure contains the output queue for the interface, its address, ... |
| 54 | * We also have, for each interface, a UBA interface structure, which |
| 55 | * contains information about the UNIBUS resources held by the interface: |
| 56 | * map registers, buffered data paths, etc. Information is cached in this |
| 57 | * structure for use by the if_uba.c routines in running the interface |
| 58 | * efficiently. |
| 59 | */ |
| 60 | struct en_softc { |
| 61 | struct ifnet es_if; /* network-visible interface */ |
| 62 | struct ifuba es_ifuba; /* UNIBUS resources */ |
| 63 | short es_delay; /* current output delay */ |
| 64 | short es_mask; /* mask for current output delay */ |
| 65 | short es_lastx; /* host last transmitted to */ |
| 66 | short es_oactive; /* is output active? */ |
| 67 | short es_olen; /* length of last output */ |
| 68 | } en_softc[NEN]; |
| 69 | |
| 70 | /* |
| 71 | * Do output DMA to determine interface presence and |
| 72 | * interrupt vector. DMA is too short to disturb other hosts. |
| 73 | */ |
| 74 | enprobe(reg) |
| 75 | caddr_t reg; |
| 76 | { |
| 77 | register int br, cvec; /* r11, r10 value-result */ |
| 78 | register struct endevice *addr = (struct endevice *)reg; |
| 79 | |
| 80 | #ifdef lint |
| 81 | br = 0; cvec = br; br = cvec; |
| 82 | enrint(0); enxint(0); encollide(0); |
| 83 | #endif |
| 84 | addr->en_istat = 0; |
| 85 | addr->en_owc = -1; |
| 86 | addr->en_oba = 0; |
| 87 | addr->en_ostat = EN_IEN|EN_GO; |
| 88 | DELAY(100000); |
| 89 | addr->en_ostat = 0; |
| 90 | #ifdef ECHACK |
| 91 | br = 0x16; |
| 92 | #endif |
| 93 | return (1); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Interface exists: make available by filling in network interface |
| 98 | * record. System will initialize the interface when it is ready |
| 99 | * to accept packets. |
| 100 | */ |
| 101 | enattach(ui) |
| 102 | struct uba_device *ui; |
| 103 | { |
| 104 | register struct en_softc *es = &en_softc[ui->ui_unit]; |
| 105 | register struct sockaddr_in *sin; |
| 106 | |
| 107 | es->es_if.if_unit = ui->ui_unit; |
| 108 | es->es_if.if_name = "en"; |
| 109 | es->es_if.if_mtu = ENMTU; |
| 110 | es->es_if.if_net = ui->ui_flags; |
| 111 | es->es_if.if_host[0] = |
| 112 | (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff; |
| 113 | sin = (struct sockaddr_in *)&es->es_if.if_addr; |
| 114 | sin->sin_family = AF_INET; |
| 115 | sin->sin_addr = if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]); |
| 116 | sin = (struct sockaddr_in *)&es->es_if.if_broadaddr; |
| 117 | sin->sin_family = AF_INET; |
| 118 | sin->sin_addr = if_makeaddr(es->es_if.if_net, 0); |
| 119 | es->es_if.if_flags = IFF_BROADCAST; |
| 120 | es->es_if.if_init = eninit; |
| 121 | es->es_if.if_output = enoutput; |
| 122 | es->es_if.if_ubareset = enreset; |
| 123 | es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT; |
| 124 | if_attach(&es->es_if); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Reset of interface after UNIBUS reset. |
| 129 | * If interface is on specified uba, reset its state. |
| 130 | */ |
| 131 | enreset(unit, uban) |
| 132 | int unit, uban; |
| 133 | { |
| 134 | register struct uba_device *ui; |
| 135 | |
| 136 | if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || |
| 137 | ui->ui_ubanum != uban) |
| 138 | return; |
| 139 | printf(" en%d", unit); |
| 140 | eninit(unit); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Initialization of interface; clear recorded pending |
| 145 | * operations, and reinitialize UNIBUS usage. |
| 146 | */ |
| 147 | eninit(unit) |
| 148 | int unit; |
| 149 | { |
| 150 | register struct en_softc *es = &en_softc[unit]; |
| 151 | register struct uba_device *ui = eninfo[unit]; |
| 152 | register struct endevice *addr; |
| 153 | int s; |
| 154 | |
| 155 | if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, |
| 156 | sizeof (struct en_header), (int)btoc(ENMRU)) == 0) { |
| 157 | printf("en%d: can't initialize\n", unit); |
| 158 | es->es_if.if_flags &= ~IFF_UP; |
| 159 | return; |
| 160 | } |
| 161 | addr = (struct endevice *)ui->ui_addr; |
| 162 | addr->en_istat = addr->en_ostat = 0; |
| 163 | |
| 164 | /* |
| 165 | * Hang a receive and start any |
| 166 | * pending writes by faking a transmit complete. |
| 167 | */ |
| 168 | s = splimp(); |
| 169 | addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; |
| 170 | addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; |
| 171 | addr->en_istat = EN_IEN|EN_GO; |
| 172 | es->es_oactive = 1; |
| 173 | es->es_if.if_flags |= IFF_UP; |
| 174 | enxint(unit); |
| 175 | splx(s); |
| 176 | if_rtinit(&es->es_if, RTF_UP); |
| 177 | } |
| 178 | |
| 179 | int enalldelay = 0; |
| 180 | int enlastdel = 50; |
| 181 | int enlastmask = (~0) << 5; |
| 182 | |
| 183 | /* |
| 184 | * Start or restart output on interface. |
| 185 | * If interface is already active, then this is a retransmit |
| 186 | * after a collision, and just restuff registers and delay. |
| 187 | * If interface is not already active, get another datagram |
| 188 | * to send off of the interface queue, and map it to the interface |
| 189 | * before starting the output. |
| 190 | */ |
| 191 | enstart(dev) |
| 192 | dev_t dev; |
| 193 | { |
| 194 | int unit = ENUNIT(dev); |
| 195 | struct uba_device *ui = eninfo[unit]; |
| 196 | register struct en_softc *es = &en_softc[unit]; |
| 197 | register struct endevice *addr; |
| 198 | struct mbuf *m; |
| 199 | int dest; |
| 200 | |
| 201 | if (es->es_oactive) |
| 202 | goto restart; |
| 203 | |
| 204 | /* |
| 205 | * Not already active: dequeue another request |
| 206 | * and map it to the UNIBUS. If no more requests, |
| 207 | * just return. |
| 208 | */ |
| 209 | IF_DEQUEUE(&es->es_if.if_snd, m); |
| 210 | if (m == 0) { |
| 211 | es->es_oactive = 0; |
| 212 | return; |
| 213 | } |
| 214 | dest = mtod(m, struct en_header *)->en_dhost; |
| 215 | es->es_olen = if_wubaput(&es->es_ifuba, m); |
| 216 | |
| 217 | /* |
| 218 | * Ethernet cannot take back-to-back packets (no |
| 219 | * buffering in interface. To help avoid overrunning |
| 220 | * receivers, enforce a small delay (about 1ms) in interface: |
| 221 | * * between all packets when enalldelay |
| 222 | * * whenever last packet was broadcast |
| 223 | * * whenever this packet is to same host as last packet |
| 224 | */ |
| 225 | if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) { |
| 226 | es->es_delay = enlastdel; |
| 227 | es->es_mask = enlastmask; |
| 228 | } |
| 229 | es->es_lastx = dest; |
| 230 | |
| 231 | restart: |
| 232 | /* |
| 233 | * Have request mapped to UNIBUS for transmission. |
| 234 | * Purge any stale data from this BDP, and start the otput. |
| 235 | */ |
| 236 | if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) |
| 237 | UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); |
| 238 | addr = (struct endevice *)ui->ui_addr; |
| 239 | addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; |
| 240 | addr->en_odelay = es->es_delay; |
| 241 | addr->en_owc = -((es->es_olen + 1) >> 1); |
| 242 | addr->en_ostat = EN_IEN|EN_GO; |
| 243 | es->es_oactive = 1; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Ethernet interface transmitter interrupt. |
| 248 | * Start another output if more data to send. |
| 249 | */ |
| 250 | enxint(unit) |
| 251 | int unit; |
| 252 | { |
| 253 | register struct uba_device *ui = eninfo[unit]; |
| 254 | register struct en_softc *es = &en_softc[unit]; |
| 255 | register struct endevice *addr = (struct endevice *)ui->ui_addr; |
| 256 | |
| 257 | if (es->es_oactive == 0) |
| 258 | return; |
| 259 | if (es->es_mask && (addr->en_ostat&EN_OERROR)) { |
| 260 | es->es_if.if_oerrors++; |
| 261 | endocoll(unit); |
| 262 | return; |
| 263 | } |
| 264 | es->es_if.if_opackets++; |
| 265 | es->es_oactive = 0; |
| 266 | es->es_delay = 0; |
| 267 | es->es_mask = ~0; |
| 268 | if (es->es_ifuba.ifu_xtofree) { |
| 269 | m_freem(es->es_ifuba.ifu_xtofree); |
| 270 | es->es_ifuba.ifu_xtofree = 0; |
| 271 | } |
| 272 | if (es->es_if.if_snd.ifq_head == 0) { |
| 273 | es->es_lastx = 256; /* putatively illegal */ |
| 274 | return; |
| 275 | } |
| 276 | enstart(unit); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Collision on ethernet interface. Do exponential |
| 281 | * backoff, and retransmit. If have backed off all |
| 282 | * the way print warning diagnostic, and drop packet. |
| 283 | */ |
| 284 | encollide(unit) |
| 285 | int unit; |
| 286 | { |
| 287 | struct en_softc *es = &en_softc[unit]; |
| 288 | |
| 289 | es->es_if.if_collisions++; |
| 290 | if (es->es_oactive == 0) |
| 291 | return; |
| 292 | endocoll(unit); |
| 293 | } |
| 294 | |
| 295 | endocoll(unit) |
| 296 | int unit; |
| 297 | { |
| 298 | register struct en_softc *es = &en_softc[unit]; |
| 299 | |
| 300 | /* |
| 301 | * Es_mask is a 16 bit number with n low zero bits, with |
| 302 | * n the number of backoffs. When es_mask is 0 we have |
| 303 | * backed off 16 times, and give up. |
| 304 | */ |
| 305 | if (es->es_mask == 0) { |
| 306 | printf("en%d: send error\n", unit); |
| 307 | enxint(unit); |
| 308 | return; |
| 309 | } |
| 310 | /* |
| 311 | * Another backoff. Restart with delay based on n low bits |
| 312 | * of the interval timer. |
| 313 | */ |
| 314 | es->es_mask <<= 1; |
| 315 | es->es_delay = mfpr(ICR) &~ es->es_mask; |
| 316 | enstart(unit); |
| 317 | } |
| 318 | |
| 319 | struct sockaddr_pup pupsrc = { AF_PUP }; |
| 320 | struct sockaddr_pup pupdst = { AF_PUP }; |
| 321 | struct sockproto pupproto = { PF_PUP }; |
| 322 | /* |
| 323 | * Ethernet interface receiver interrupt. |
| 324 | * If input error just drop packet. |
| 325 | * Otherwise purge input buffered data path and examine |
| 326 | * packet to determine type. If can't determine length |
| 327 | * from type, then have to drop packet. Othewise decapsulate |
| 328 | * packet based on type and pass to type specific higher-level |
| 329 | * input routine. |
| 330 | */ |
| 331 | enrint(unit) |
| 332 | int unit; |
| 333 | { |
| 334 | register struct en_softc *es = &en_softc[unit]; |
| 335 | struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; |
| 336 | register struct en_header *en; |
| 337 | struct mbuf *m; |
| 338 | int len, plen; short resid; |
| 339 | register struct ifqueue *inq; |
| 340 | int off; |
| 341 | |
| 342 | es->es_if.if_ipackets++; |
| 343 | |
| 344 | /* |
| 345 | * Purge BDP; drop if input error indicated. |
| 346 | */ |
| 347 | if (es->es_ifuba.ifu_flags & UBA_NEEDBDP) |
| 348 | UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); |
| 349 | if (addr->en_istat&EN_IERROR) { |
| 350 | es->es_if.if_ierrors++; |
| 351 | goto setup; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Calculate input data length. |
| 356 | * Get pointer to ethernet header (in input buffer). |
| 357 | * Deal with trailer protocol: if type is PUP trailer |
| 358 | * get true type from first 16-bit word past data. |
| 359 | * Remember that type was trailer by setting off. |
| 360 | */ |
| 361 | resid = addr->en_iwc; |
| 362 | if (resid) |
| 363 | resid |= 0176000; |
| 364 | len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1; |
| 365 | len -= sizeof (struct en_header); |
| 366 | if (len > ENMRU) |
| 367 | goto setup; /* sanity */ |
| 368 | en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); |
| 369 | #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) |
| 370 | if (en->en_type >= ENPUP_TRAIL && |
| 371 | en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { |
| 372 | off = (en->en_type - ENPUP_TRAIL) * 512; |
| 373 | if (off > ENMTU) |
| 374 | goto setup; /* sanity */ |
| 375 | en->en_type = *endataaddr(en, off, u_short *); |
| 376 | resid = *(endataaddr(en, off+2, u_short *)); |
| 377 | if (off + resid > len) |
| 378 | goto setup; /* sanity */ |
| 379 | len = off + resid; |
| 380 | } else |
| 381 | off = 0; |
| 382 | if (len == 0) |
| 383 | goto setup; |
| 384 | /* |
| 385 | * Pull packet off interface. Off is nonzero if packet |
| 386 | * has trailing header; if_rubaget will then force this header |
| 387 | * information to be at the front, but we still have to drop |
| 388 | * the type and length which are at the front of any trailer data. |
| 389 | */ |
| 390 | m = if_rubaget(&es->es_ifuba, len, off); |
| 391 | if (m == 0) |
| 392 | goto setup; |
| 393 | if (off) { |
| 394 | m->m_off += 2 * sizeof (u_short); |
| 395 | m->m_len -= 2 * sizeof (u_short); |
| 396 | } |
| 397 | switch (en->en_type) { |
| 398 | |
| 399 | #ifdef INET |
| 400 | case ENPUP_IPTYPE: |
| 401 | schednetisr(NETISR_IP); |
| 402 | inq = &ipintrq; |
| 403 | break; |
| 404 | #endif |
| 405 | #ifdef PUP |
| 406 | case ENPUP_PUPTYPE: { |
| 407 | struct pup_header *pup = mtod(m, struct pup_header *); |
| 408 | |
| 409 | pupproto.sp_protocol = pup->pup_type; |
| 410 | pupdst.spup_addr = pup->pup_dport; |
| 411 | pupsrc.spup_addr = pup->pup_sport; |
| 412 | raw_input(m, &pupproto, (struct sockaddr *)&pupsrc, |
| 413 | (struct sockaddr *)&pupdst); |
| 414 | goto setup; |
| 415 | } |
| 416 | #endif |
| 417 | default: |
| 418 | m_freem(m); |
| 419 | goto setup; |
| 420 | } |
| 421 | |
| 422 | if (IF_QFULL(inq)) { |
| 423 | IF_DROP(inq); |
| 424 | m_freem(m); |
| 425 | } else |
| 426 | IF_ENQUEUE(inq, m); |
| 427 | |
| 428 | setup: |
| 429 | /* |
| 430 | * Reset for next packet. |
| 431 | */ |
| 432 | addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; |
| 433 | addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1; |
| 434 | addr->en_istat = EN_IEN|EN_GO; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Ethernet output routine. |
| 439 | * Encapsulate a packet of type family for the local net. |
| 440 | * Use trailer local net encapsulation if enough data in first |
| 441 | * packet leaves a multiple of 512 bytes of data in remainder. |
| 442 | */ |
| 443 | enoutput(ifp, m0, dst) |
| 444 | struct ifnet *ifp; |
| 445 | struct mbuf *m0; |
| 446 | struct sockaddr *dst; |
| 447 | { |
| 448 | int type, dest, s, error; |
| 449 | register struct mbuf *m = m0; |
| 450 | register struct en_header *en; |
| 451 | register int off; |
| 452 | |
| 453 | switch (dst->sa_family) { |
| 454 | |
| 455 | #ifdef INET |
| 456 | case AF_INET: |
| 457 | dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; |
| 458 | if (dest & 0x00ffff00) { |
| 459 | error = EPERM; /* ??? */ |
| 460 | goto bad; |
| 461 | } |
| 462 | dest = (dest >> 24) & 0xff; |
| 463 | off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; |
| 464 | if (off > 0 && (off & 0x1ff) == 0 && |
| 465 | m->m_off >= MMINOFF + 2 * sizeof (u_short)) { |
| 466 | type = ENPUP_TRAIL + (off>>9); |
| 467 | m->m_off -= 2 * sizeof (u_short); |
| 468 | m->m_len += 2 * sizeof (u_short); |
| 469 | *mtod(m, u_short *) = ENPUP_IPTYPE; |
| 470 | *(mtod(m, u_short *) + 1) = m->m_len; |
| 471 | goto gottrailertype; |
| 472 | } |
| 473 | type = ENPUP_IPTYPE; |
| 474 | off = 0; |
| 475 | goto gottype; |
| 476 | #endif |
| 477 | #ifdef PUP |
| 478 | case AF_PUP: |
| 479 | dest = ((struct sockaddr_pup *)dst)->spup_addr.pp_host; |
| 480 | type = ENPUP_PUPTYPE; |
| 481 | off = 0; |
| 482 | goto gottype; |
| 483 | #endif |
| 484 | |
| 485 | default: |
| 486 | printf("en%d: can't handle af%d\n", ifp->if_unit, |
| 487 | dst->sa_family); |
| 488 | error = EAFNOSUPPORT; |
| 489 | goto bad; |
| 490 | } |
| 491 | |
| 492 | gottrailertype: |
| 493 | /* |
| 494 | * Packet to be sent as trailer: move first packet |
| 495 | * (control information) to end of chain. |
| 496 | */ |
| 497 | while (m->m_next) |
| 498 | m = m->m_next; |
| 499 | m->m_next = m0; |
| 500 | m = m0->m_next; |
| 501 | m0->m_next = 0; |
| 502 | m0 = m; |
| 503 | |
| 504 | gottype: |
| 505 | /* |
| 506 | * Add local net header. If no space in first mbuf, |
| 507 | * allocate another. |
| 508 | */ |
| 509 | if (m->m_off > MMAXOFF || |
| 510 | MMINOFF + sizeof (struct en_header) > m->m_off) { |
| 511 | m = m_get(M_DONTWAIT); |
| 512 | if (m == 0) { |
| 513 | error = ENOBUFS; |
| 514 | goto bad; |
| 515 | } |
| 516 | m->m_next = m0; |
| 517 | m->m_off = MMINOFF; |
| 518 | m->m_len = sizeof (struct en_header); |
| 519 | } else { |
| 520 | m->m_off -= sizeof (struct en_header); |
| 521 | m->m_len += sizeof (struct en_header); |
| 522 | } |
| 523 | en = mtod(m, struct en_header *); |
| 524 | en->en_shost = ifp->if_host[0]; |
| 525 | en->en_dhost = dest; |
| 526 | en->en_type = type; |
| 527 | |
| 528 | /* |
| 529 | * Queue message on interface, and start output if interface |
| 530 | * not yet active. |
| 531 | */ |
| 532 | s = splimp(); |
| 533 | if (IF_QFULL(&ifp->if_snd)) { |
| 534 | IF_DROP(&ifp->if_snd); |
| 535 | error = ENOBUFS; |
| 536 | goto qfull; |
| 537 | } |
| 538 | IF_ENQUEUE(&ifp->if_snd, m); |
| 539 | if (en_softc[ifp->if_unit].es_oactive == 0) |
| 540 | enstart(ifp->if_unit); |
| 541 | splx(s); |
| 542 | return (0); |
| 543 | qfull: |
| 544 | m0 = m; |
| 545 | splx(s); |
| 546 | bad: |
| 547 | m_freem(m0); |
| 548 | return (error); |
| 549 | } |