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