Commit | Line | Data |
---|---|---|
a9687d27 | 1 | /* if_ec.c 4.24 82/10/10 */ |
75334b2a BF |
2 | |
3 | #include "ec.h" | |
75334b2a BF |
4 | |
5 | /* | |
6 | * 3Com Ethernet Controller interface | |
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" | |
75334b2a | 16 | #include "../h/vmmac.h" |
a9687d27 BJ |
17 | #include <errno.h> |
18 | ||
19 | #include "../net/if.h" | |
20 | #include "../net/netisr.h" | |
21 | #include "../net/route.h" | |
d2cc167c BJ |
22 | #include "../netinet/in.h" |
23 | #include "../netinet/in_systm.h" | |
d2cc167c BJ |
24 | #include "../netinet/ip.h" |
25 | #include "../netinet/ip_var.h" | |
26 | #include "../netpup/pup.h" | |
a9687d27 BJ |
27 | |
28 | #include "../vax/cpu.h" | |
29 | #include "../vax/mtpr.h" | |
30 | #include "../vaxif/if_ec.h" | |
31 | #include "../vaxif/if_ecreg.h" | |
32 | #include "../vaxif/if_uba.h" | |
33 | #include "../vaxuba/ubareg.h" | |
34 | #include "../vaxuba/ubavar.h" | |
75334b2a BF |
35 | |
36 | #define ECMTU 1500 | |
a26646de | 37 | #define ECMEM 0000000 |
75334b2a BF |
38 | |
39 | int ecprobe(), ecattach(), ecrint(), ecxint(), eccollide(); | |
40 | struct uba_device *ecinfo[NEC]; | |
41 | u_short ecstd[] = { 0 }; | |
42 | struct uba_driver ecdriver = | |
43 | { ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo }; | |
cf1ea369 | 44 | u_char ec_iltop[3] = { 0x02, 0x07, 0x01 }; |
75334b2a BF |
45 | #define ECUNIT(x) minor(x) |
46 | ||
47 | int ecinit(),ecoutput(),ecreset(); | |
48 | struct mbuf *ecget(); | |
49 | ||
be515f6e BF |
50 | extern struct ifnet loif; |
51 | ||
75334b2a BF |
52 | /* |
53 | * Ethernet software status per interface. | |
54 | * | |
55 | * Each interface is referenced by a network interface structure, | |
56 | * es_if, which the routing code uses to locate the interface. | |
57 | * This structure contains the output queue for the interface, its address, ... | |
58 | * We also have, for each interface, a UBA interface structure, which | |
59 | * contains information about the UNIBUS resources held by the interface: | |
60 | * map registers, buffered data paths, etc. Information is cached in this | |
61 | * structure for use by the if_uba.c routines in running the interface | |
62 | * efficiently. | |
63 | */ | |
64 | struct ec_softc { | |
65 | struct ifnet es_if; /* network-visible interface */ | |
66 | struct ifuba es_ifuba; /* UNIBUS resources */ | |
75334b2a | 67 | short es_mask; /* mask for current output delay */ |
75334b2a BF |
68 | short es_oactive; /* is output active? */ |
69 | caddr_t es_buf[16]; /* virtual addresses of buffers */ | |
70 | u_char es_enaddr[6]; /* board's ethernet address */ | |
71 | } ec_softc[NEC]; | |
72 | ||
73 | /* | |
74 | * Do output DMA to determine interface presence and | |
75 | * interrupt vector. DMA is too short to disturb other hosts. | |
76 | */ | |
77 | ecprobe(reg) | |
78 | caddr_t reg; | |
79 | { | |
80 | register int br, cvec; /* r11, r10 value-result */ | |
81 | register struct ecdevice *addr = (struct ecdevice *)reg; | |
a26646de | 82 | register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM]; |
75334b2a | 83 | |
75334b2a BF |
84 | #ifdef lint |
85 | br = 0; cvec = br; br = cvec; | |
86 | ecrint(0); ecxint(0); eccollide(0); | |
87 | #endif | |
f5a616ae BF |
88 | /* |
89 | * Make sure memory is turned on | |
90 | */ | |
91 | addr->ec_rcr = EC_AROM; | |
a26646de BF |
92 | /* |
93 | * Disable map registers for ec unibus space, | |
94 | * but don't allocate yet. | |
95 | */ | |
96 | ubamem(numuba, ECMEM, 32*2, 0); | |
75334b2a BF |
97 | /* |
98 | * Check for existence of buffers on Unibus. | |
75334b2a BF |
99 | */ |
100 | if (badaddr((caddr_t) ecbuf, 2)) { | |
a26646de BF |
101 | bad1: |
102 | printf("ec: buffer mem not found\n"); | |
103 | bad2: | |
104 | ubamem(numuba, 0, 0, 0); /* reenable map (780 only) */ | |
105 | addr->ec_rcr = EC_MDISAB; /* disable memory */ | |
75334b2a BF |
106 | return (0); |
107 | } | |
a26646de BF |
108 | #if VAX780 |
109 | if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) { | |
110 | uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr; | |
111 | goto bad1; | |
112 | } | |
113 | #endif | |
75334b2a BF |
114 | |
115 | /* | |
116 | * Tell the system that the board has memory here, so it won't | |
117 | * attempt to allocate the addresses later. | |
118 | */ | |
a26646de BF |
119 | if (ubamem(numuba, ECMEM, 32*2, 1) == 0) { |
120 | printf("ecprobe: cannot reserve uba addresses\n"); | |
121 | goto bad2; | |
122 | } | |
75334b2a BF |
123 | |
124 | /* | |
125 | * Make a one byte packet in what should be buffer #0. | |
126 | * Submit it for sending. This whould cause an xmit interrupt. | |
127 | * The xmit interrupt vector is 8 bytes after the receive vector, | |
128 | * so adjust for this before returning. | |
129 | */ | |
130 | *(u_short *)ecbuf = (u_short) 03777; | |
131 | ecbuf[03777] = '\0'; | |
132 | addr->ec_xcr = EC_XINTEN|EC_XWBN; | |
133 | DELAY(100000); | |
134 | addr->ec_xcr = EC_XCLR; | |
0ca845ca | 135 | if (cvec > 0 && cvec != 0x200) { |
a26646de BF |
136 | if (cvec & 04) { /* collision interrupt */ |
137 | cvec -= 04; | |
138 | br += 1; /* rcv is collision + 1 */ | |
139 | } else { /* xmit interrupt */ | |
140 | cvec -= 010; | |
141 | br += 2; /* rcv is xmit + 2 */ | |
142 | } | |
0ca845ca | 143 | } |
75334b2a BF |
144 | return (1); |
145 | } | |
146 | ||
147 | /* | |
148 | * Interface exists: make available by filling in network interface | |
149 | * record. System will initialize the interface when it is ready | |
150 | * to accept packets. | |
151 | */ | |
152 | ecattach(ui) | |
153 | struct uba_device *ui; | |
154 | { | |
0ca845ca SL |
155 | struct ec_softc *es = &ec_softc[ui->ui_unit]; |
156 | register struct ifnet *ifp = &es->es_if; | |
75334b2a | 157 | register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr; |
0ca845ca SL |
158 | struct sockaddr_in *sin; |
159 | int i, j; | |
160 | u_char *cp; | |
75334b2a | 161 | |
0ca845ca SL |
162 | ifp->if_unit = ui->ui_unit; |
163 | ifp->if_name = "ec"; | |
164 | ifp->if_mtu = ECMTU; | |
165 | ifp->if_net = ui->ui_flags; | |
75334b2a BF |
166 | |
167 | /* | |
0ca845ca | 168 | * Read the ethernet address off the board, one nibble at a time. |
75334b2a BF |
169 | */ |
170 | addr->ec_xcr = EC_UECLR; | |
171 | addr->ec_rcr = EC_AROM; | |
172 | cp = es->es_enaddr; | |
0ca845ca | 173 | #define NEXTBIT addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM |
75334b2a BF |
174 | for (i=0; i<6; i++) { |
175 | *cp = 0; | |
176 | for (j=0; j<=4; j+=4) { | |
177 | *cp |= ((addr->ec_rcr >> 8) & 0xf) << j; | |
0ca845ca | 178 | NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT; |
75334b2a BF |
179 | } |
180 | cp++; | |
181 | } | |
0ca845ca | 182 | #ifdef notdef |
75334b2a BF |
183 | printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit, |
184 | es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff, | |
185 | es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff, | |
186 | es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff); | |
0ca845ca SL |
187 | #endif |
188 | ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) | | |
75334b2a BF |
189 | ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff); |
190 | sin = (struct sockaddr_in *)&es->es_if.if_addr; | |
191 | sin->sin_family = AF_INET; | |
0ca845ca | 192 | sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]); |
75334b2a | 193 | |
0ca845ca | 194 | sin = (struct sockaddr_in *)&ifp->if_broadaddr; |
75334b2a | 195 | sin->sin_family = AF_INET; |
0ca845ca SL |
196 | sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); |
197 | ifp->if_flags = IFF_BROADCAST; | |
75334b2a | 198 | |
0ca845ca SL |
199 | ifp->if_init = ecinit; |
200 | ifp->if_output = ecoutput; | |
201 | ifp->if_ubareset = ecreset; | |
75334b2a | 202 | for (i=0; i<16; i++) |
a26646de | 203 | es->es_buf[i] = &umem[ui->ui_ubanum][ECMEM+2048*i]; |
0ca845ca | 204 | if_attach(ifp); |
75334b2a BF |
205 | } |
206 | ||
207 | /* | |
208 | * Reset of interface after UNIBUS reset. | |
209 | * If interface is on specified uba, reset its state. | |
210 | */ | |
211 | ecreset(unit, uban) | |
212 | int unit, uban; | |
213 | { | |
214 | register struct uba_device *ui; | |
75334b2a BF |
215 | |
216 | if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 || | |
217 | ui->ui_ubanum != uban) | |
218 | return; | |
219 | printf(" ec%d", unit); | |
a26646de | 220 | ubamem(uban, ECMEM, 32*2, 0); /* map register disable (no alloc) */ |
75334b2a BF |
221 | ecinit(unit); |
222 | } | |
223 | ||
224 | /* | |
225 | * Initialization of interface; clear recorded pending | |
226 | * operations, and reinitialize UNIBUS usage. | |
227 | */ | |
228 | ecinit(unit) | |
229 | int unit; | |
230 | { | |
0ca845ca SL |
231 | struct ec_softc *es = &ec_softc[unit]; |
232 | struct ecdevice *addr; | |
233 | int i, s; | |
75334b2a BF |
234 | |
235 | /* | |
a0d46072 | 236 | * Hang receive buffers and start any pending writes. |
f5a616ae BF |
237 | * Writing into the rcr also makes sure the memory |
238 | * is turned on. | |
75334b2a | 239 | */ |
0ca845ca | 240 | addr = (struct ecdevice *)ecinfo[unit]->ui_addr; |
75334b2a BF |
241 | s = splimp(); |
242 | for (i=ECRHBF; i>=ECRLBF; i--) | |
243 | addr->ec_rcr = EC_READ|i; | |
a0d46072 BF |
244 | es->es_oactive = 0; |
245 | es->es_mask = ~0; | |
75334b2a | 246 | es->es_if.if_flags |= IFF_UP; |
a0d46072 BF |
247 | if (es->es_if.if_snd.ifq_head) |
248 | ecstart(unit); | |
75334b2a | 249 | splx(s); |
a13c006d | 250 | if_rtinit(&es->es_if, RTF_UP); |
75334b2a BF |
251 | } |
252 | ||
75334b2a BF |
253 | /* |
254 | * Start or restart output on interface. | |
255 | * If interface is already active, then this is a retransmit | |
be515f6e | 256 | * after a collision, and just restuff registers. |
75334b2a BF |
257 | * If interface is not already active, get another datagram |
258 | * to send off of the interface queue, and map it to the interface | |
259 | * before starting the output. | |
260 | */ | |
261 | ecstart(dev) | |
262 | dev_t dev; | |
263 | { | |
0ca845ca SL |
264 | int unit = ECUNIT(dev), dest; |
265 | struct ec_softc *es = &ec_softc[unit]; | |
266 | struct ecdevice *addr; | |
75334b2a BF |
267 | struct mbuf *m; |
268 | caddr_t ecbuf; | |
75334b2a BF |
269 | |
270 | if (es->es_oactive) | |
271 | goto restart; | |
272 | ||
75334b2a BF |
273 | IF_DEQUEUE(&es->es_if.if_snd, m); |
274 | if (m == 0) { | |
275 | es->es_oactive = 0; | |
276 | return; | |
277 | } | |
75334b2a BF |
278 | ecput(es->es_buf[ECTBF], m); |
279 | ||
75334b2a | 280 | restart: |
0ca845ca | 281 | addr = (struct ecdevice *)ecinfo[unit]->ui_addr; |
75334b2a BF |
282 | addr->ec_xcr = EC_WRITE|ECTBF; |
283 | es->es_oactive = 1; | |
284 | } | |
285 | ||
286 | /* | |
287 | * Ethernet interface transmitter interrupt. | |
288 | * Start another output if more data to send. | |
289 | */ | |
290 | ecxint(unit) | |
291 | int unit; | |
292 | { | |
75334b2a | 293 | register struct ec_softc *es = &ec_softc[unit]; |
0ca845ca SL |
294 | register struct ecdevice *addr = |
295 | (struct ecdevice *)ecinfo[unit]->ui_addr; | |
75334b2a BF |
296 | |
297 | if (es->es_oactive == 0) | |
298 | return; | |
0ca845ca SL |
299 | if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) { |
300 | printf("ec%d: stray xmit interrupt, xcr=%b\n", unit, | |
301 | addr->ec_xcr, EC_XBITS); | |
302 | es->es_oactive = 0; | |
303 | addr->ec_xcr = EC_XCLR; | |
304 | return; | |
305 | } | |
75334b2a BF |
306 | es->es_if.if_opackets++; |
307 | es->es_oactive = 0; | |
75334b2a BF |
308 | es->es_mask = ~0; |
309 | addr->ec_xcr = EC_XCLR; | |
0ca845ca SL |
310 | if (es->es_if.if_snd.ifq_head) |
311 | ecstart(unit); | |
75334b2a BF |
312 | } |
313 | ||
314 | /* | |
315 | * Collision on ethernet interface. Do exponential | |
316 | * backoff, and retransmit. If have backed off all | |
317 | * the way print warning diagnostic, and drop packet. | |
318 | */ | |
319 | eccollide(unit) | |
320 | int unit; | |
321 | { | |
322 | struct ec_softc *es = &ec_softc[unit]; | |
75334b2a | 323 | |
75334b2a | 324 | es->es_if.if_collisions++; |
0ca845ca SL |
325 | if (es->es_oactive) |
326 | ecdocoll(unit); | |
75334b2a BF |
327 | } |
328 | ||
329 | ecdocoll(unit) | |
330 | int unit; | |
331 | { | |
332 | register struct ec_softc *es = &ec_softc[unit]; | |
be515f6e BF |
333 | register struct ecdevice *addr = |
334 | (struct ecdevice *)ecinfo[unit]->ui_addr; | |
335 | register i; | |
336 | int delay; | |
75334b2a BF |
337 | |
338 | /* | |
339 | * Es_mask is a 16 bit number with n low zero bits, with | |
340 | * n the number of backoffs. When es_mask is 0 we have | |
341 | * backed off 16 times, and give up. | |
342 | */ | |
343 | if (es->es_mask == 0) { | |
be515f6e | 344 | es->es_if.if_oerrors++; |
75334b2a BF |
345 | printf("ec%d: send error\n", unit); |
346 | /* | |
be515f6e BF |
347 | * Reset interface, then requeue rcv buffers. |
348 | * Some incoming packets may be lost, but that | |
349 | * can't be helped. | |
350 | */ | |
351 | addr->ec_xcr = EC_UECLR; | |
352 | for (i=ECRHBF; i>=ECRLBF; i--) | |
353 | addr->ec_rcr = EC_READ|i; | |
354 | /* | |
355 | * Reset and transmit next packet (if any). | |
75334b2a | 356 | */ |
be515f6e BF |
357 | es->es_oactive = 0; |
358 | es->es_mask = ~0; | |
359 | if (es->es_if.if_snd.ifq_head) | |
360 | ecstart(unit); | |
75334b2a BF |
361 | return; |
362 | } | |
363 | /* | |
be515f6e BF |
364 | * Do exponential backoff. Compute delay based on low bits |
365 | * of the interval timer. Then delay for that number of | |
366 | * slot times. A slot time is 51.2 microseconds (rounded to 51). | |
367 | * This does not take into account the time already used to | |
368 | * process the interrupt. | |
75334b2a BF |
369 | */ |
370 | es->es_mask <<= 1; | |
be515f6e BF |
371 | delay = mfpr(ICR) &~ es->es_mask; |
372 | DELAY(delay * 51); | |
75334b2a | 373 | /* |
be515f6e | 374 | * Clear the controller's collision flag, thus enabling retransmit. |
75334b2a | 375 | */ |
a26646de | 376 | addr->ec_xcr = EC_CLEAR; |
75334b2a BF |
377 | } |
378 | ||
75334b2a BF |
379 | /* |
380 | * Ethernet interface receiver interrupt. | |
381 | * If input error just drop packet. | |
382 | * Otherwise purge input buffered data path and examine | |
383 | * packet to determine type. If can't determine length | |
384 | * from type, then have to drop packet. Othewise decapsulate | |
385 | * packet based on type and pass to type specific higher-level | |
386 | * input routine. | |
387 | */ | |
388 | ecrint(unit) | |
389 | int unit; | |
390 | { | |
391 | struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
75334b2a | 392 | |
75334b2a BF |
393 | while (addr->ec_rcr & EC_RDONE) |
394 | ecread(unit); | |
395 | } | |
396 | ||
397 | ecread(unit) | |
398 | int unit; | |
399 | { | |
400 | register struct ec_softc *es = &ec_softc[unit]; | |
401 | struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
402 | register struct ec_header *ec; | |
403 | struct mbuf *m; | |
0ca845ca | 404 | int len, off, resid, ecoff, buf; |
75334b2a BF |
405 | register struct ifqueue *inq; |
406 | caddr_t ecbuf; | |
75334b2a BF |
407 | |
408 | es->es_if.if_ipackets++; | |
409 | buf = addr->ec_rcr & EC_RBN; | |
410 | if (buf < ECRLBF || buf > ECRHBF) | |
411 | panic("ecrint"); | |
412 | ecbuf = es->es_buf[buf]; | |
413 | ecoff = *(short *)ecbuf; | |
be515f6e | 414 | if (ecoff <= ECRDOFF || ecoff > 2046) { |
75334b2a BF |
415 | es->es_if.if_ierrors++; |
416 | #ifdef notdef | |
417 | if (es->es_if.if_ierrors % 100 == 0) | |
418 | printf("ec%d: += 100 input errors\n", unit); | |
419 | #endif | |
75334b2a BF |
420 | goto setup; |
421 | } | |
422 | ||
423 | /* | |
424 | * Get input data length. | |
425 | * Get pointer to ethernet header (in input buffer). | |
426 | * Deal with trailer protocol: if type is PUP trailer | |
427 | * get true type from first 16-bit word past data. | |
428 | * Remember that type was trailer by setting off. | |
429 | */ | |
430 | len = ecoff - ECRDOFF - sizeof (struct ec_header); | |
431 | ec = (struct ec_header *)(ecbuf + ECRDOFF); | |
432 | #define ecdataaddr(ec, off, type) ((type)(((caddr_t)((ec)+1)+(off)))) | |
433 | if (ec->ec_type >= ECPUP_TRAIL && | |
434 | ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) { | |
435 | off = (ec->ec_type - ECPUP_TRAIL) * 512; | |
436 | if (off >= ECMTU) | |
437 | goto setup; /* sanity */ | |
438 | ec->ec_type = *ecdataaddr(ec, off, u_short *); | |
439 | resid = *(ecdataaddr(ec, off+2, u_short *)); | |
440 | if (off + resid > len) | |
441 | goto setup; /* sanity */ | |
442 | len = off + resid; | |
443 | } else | |
444 | off = 0; | |
445 | if (len == 0) | |
446 | goto setup; | |
447 | ||
448 | /* | |
449 | * Pull packet off interface. Off is nonzero if packet | |
450 | * has trailing header; ecget will then force this header | |
451 | * information to be at the front, but we still have to drop | |
452 | * the type and length which are at the front of any trailer data. | |
453 | */ | |
454 | m = ecget(ecbuf, len, off); | |
455 | if (m == 0) | |
456 | goto setup; | |
457 | if (off) { | |
458 | m->m_off += 2 * sizeof (u_short); | |
459 | m->m_len -= 2 * sizeof (u_short); | |
460 | } | |
461 | switch (ec->ec_type) { | |
462 | ||
463 | #ifdef INET | |
464 | case ECPUP_IPTYPE: | |
465 | schednetisr(NETISR_IP); | |
466 | inq = &ipintrq; | |
467 | break; | |
75334b2a BF |
468 | #endif |
469 | default: | |
470 | m_freem(m); | |
471 | goto setup; | |
472 | } | |
473 | ||
474 | if (IF_QFULL(inq)) { | |
475 | IF_DROP(inq); | |
476 | m_freem(m); | |
0ca845ca SL |
477 | goto setup; |
478 | } | |
479 | IF_ENQUEUE(inq, m); | |
75334b2a BF |
480 | |
481 | setup: | |
482 | /* | |
483 | * Reset for next packet. | |
484 | */ | |
485 | addr->ec_rcr = EC_READ|EC_RCLR|buf; | |
486 | } | |
487 | ||
488 | /* | |
489 | * Ethernet output routine. | |
490 | * Encapsulate a packet of type family for the local net. | |
491 | * Use trailer local net encapsulation if enough data in first | |
492 | * packet leaves a multiple of 512 bytes of data in remainder. | |
be515f6e BF |
493 | * If destination is this address or broadcast, send packet to |
494 | * loop device to kludge around the fact that 3com interfaces can't | |
495 | * talk to themselves. | |
75334b2a BF |
496 | */ |
497 | ecoutput(ifp, m0, dst) | |
498 | struct ifnet *ifp; | |
499 | struct mbuf *m0; | |
500 | struct sockaddr *dst; | |
501 | { | |
502 | int type, dest, s, error; | |
503 | register struct ec_softc *es = &ec_softc[ifp->if_unit]; | |
504 | register struct mbuf *m = m0; | |
505 | register struct ec_header *ec; | |
0ca845ca | 506 | register int off, i; |
be515f6e | 507 | struct mbuf *mcopy = (struct mbuf *) 0; /* Null */ |
75334b2a | 508 | |
75334b2a BF |
509 | switch (dst->sa_family) { |
510 | ||
511 | #ifdef INET | |
512 | case AF_INET: | |
513 | dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; | |
be515f6e BF |
514 | if ((dest &~ 0xff) == 0) |
515 | mcopy = m_copy(m, 0, M_COPYALL); | |
516 | else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)-> | |
517 | sin_addr.s_addr) { | |
518 | mcopy = m; | |
519 | goto gotlocal; | |
520 | } | |
75334b2a BF |
521 | off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; |
522 | if (off > 0 && (off & 0x1ff) == 0 && | |
523 | m->m_off >= MMINOFF + 2 * sizeof (u_short)) { | |
524 | type = ECPUP_TRAIL + (off>>9); | |
525 | m->m_off -= 2 * sizeof (u_short); | |
526 | m->m_len += 2 * sizeof (u_short); | |
527 | *mtod(m, u_short *) = ECPUP_IPTYPE; | |
528 | *(mtod(m, u_short *) + 1) = m->m_len; | |
529 | goto gottrailertype; | |
530 | } | |
531 | type = ECPUP_IPTYPE; | |
532 | off = 0; | |
533 | goto gottype; | |
75334b2a BF |
534 | #endif |
535 | ||
536 | default: | |
537 | printf("ec%d: can't handle af%d\n", ifp->if_unit, | |
538 | dst->sa_family); | |
539 | error = EAFNOSUPPORT; | |
540 | goto bad; | |
541 | } | |
542 | ||
543 | gottrailertype: | |
544 | /* | |
545 | * Packet to be sent as trailer: move first packet | |
546 | * (control information) to end of chain. | |
547 | */ | |
548 | while (m->m_next) | |
549 | m = m->m_next; | |
550 | m->m_next = m0; | |
551 | m = m0->m_next; | |
552 | m0->m_next = 0; | |
553 | m0 = m; | |
554 | ||
555 | gottype: | |
556 | /* | |
557 | * Add local net header. If no space in first mbuf, | |
558 | * allocate another. | |
559 | */ | |
560 | if (m->m_off > MMAXOFF || | |
561 | MMINOFF + sizeof (struct ec_header) > m->m_off) { | |
562 | m = m_get(M_DONTWAIT); | |
563 | if (m == 0) { | |
564 | error = ENOBUFS; | |
565 | goto bad; | |
566 | } | |
567 | m->m_next = m0; | |
568 | m->m_off = MMINOFF; | |
569 | m->m_len = sizeof (struct ec_header); | |
570 | } else { | |
571 | m->m_off -= sizeof (struct ec_header); | |
572 | m->m_len += sizeof (struct ec_header); | |
573 | } | |
574 | ec = mtod(m, struct ec_header *); | |
575 | for (i=0; i<6; i++) | |
576 | ec->ec_shost[i] = es->es_enaddr[i]; | |
f53ac196 | 577 | if ((dest &~ 0xff) == 0) |
0ca845ca | 578 | /* broadcast address */ |
75334b2a BF |
579 | for (i=0; i<6; i++) |
580 | ec->ec_dhost[i] = 0xff; | |
581 | else { | |
cf1ea369 BF |
582 | if (dest & 0x8000) { |
583 | ec->ec_dhost[0] = ec_iltop[0]; | |
584 | ec->ec_dhost[1] = ec_iltop[1]; | |
585 | ec->ec_dhost[2] = ec_iltop[2]; | |
586 | } else { | |
587 | ec->ec_dhost[0] = es->es_enaddr[0]; | |
588 | ec->ec_dhost[1] = es->es_enaddr[1]; | |
589 | ec->ec_dhost[2] = es->es_enaddr[2]; | |
590 | } | |
591 | ec->ec_dhost[3] = (dest>>8) & 0x7f; | |
75334b2a BF |
592 | ec->ec_dhost[4] = (dest>>16) & 0xff; |
593 | ec->ec_dhost[5] = (dest>>24) & 0xff; | |
594 | } | |
595 | ec->ec_type = type; | |
596 | ||
597 | /* | |
598 | * Queue message on interface, and start output if interface | |
599 | * not yet active. | |
600 | */ | |
601 | s = splimp(); | |
602 | if (IF_QFULL(&ifp->if_snd)) { | |
603 | IF_DROP(&ifp->if_snd); | |
604 | error = ENOBUFS; | |
605 | goto qfull; | |
606 | } | |
607 | IF_ENQUEUE(&ifp->if_snd, m); | |
608 | if (es->es_oactive == 0) | |
609 | ecstart(ifp->if_unit); | |
610 | splx(s); | |
0ca845ca | 611 | |
be515f6e | 612 | gotlocal: |
0ca845ca SL |
613 | return(mcopy ? looutput(&loif, mcopy, dst) : 0); |
614 | ||
75334b2a BF |
615 | qfull: |
616 | m0 = m; | |
617 | splx(s); | |
618 | bad: | |
619 | m_freem(m0); | |
620 | return(error); | |
621 | } | |
622 | ||
623 | /* | |
0ca845ca SL |
624 | * Routine to copy from mbuf chain to transmitter |
625 | * buffer in UNIBUS memory. | |
75334b2a BF |
626 | */ |
627 | ecput(ecbuf, m) | |
0ca845ca | 628 | u_char *ecbuf; |
75334b2a BF |
629 | struct mbuf *m; |
630 | { | |
75334b2a | 631 | register struct mbuf *mp; |
0ca845ca | 632 | register int off; |
48db90de | 633 | u_char *bp; |
75334b2a | 634 | |
0ca845ca SL |
635 | for (off = 2048, mp = m; mp; mp = mp->m_next) |
636 | off -= mp->m_len; | |
637 | *(u_short *)ecbuf = off; | |
638 | bp = (u_char *)(ecbuf + off); | |
48db90de SL |
639 | for (mp = m; mp; mp = mp->m_next) { |
640 | register unsigned len = mp->m_len; | |
641 | u_char *mcp; | |
0ca845ca | 642 | |
0ca845ca SL |
643 | if (len == 0) |
644 | continue; | |
645 | mcp = mtod(mp, u_char *); | |
646 | if ((unsigned)bp & 01) { | |
4a404244 | 647 | *bp++ = *mcp++; |
0ca845ca | 648 | len--; |
4a404244 | 649 | } |
48db90de SL |
650 | if (off = (len >> 1)) { |
651 | register u_short *to, *from; | |
652 | ||
653 | to = (u_short *)bp; | |
654 | from = (u_short *)mcp; | |
655 | do | |
656 | *to++ = *from++; | |
657 | while (--off > 0); | |
658 | bp = (u_char *)to, | |
659 | mcp = (u_char *)from; | |
4a404244 | 660 | } |
48db90de | 661 | if (len & 01) |
75334b2a | 662 | *bp++ = *mcp++; |
75334b2a | 663 | } |
0ca845ca SL |
664 | #ifdef notdef |
665 | if (bp - ecbuf != 2048) | |
666 | printf("ec: bad ecput, diff=%d\n", bp-ecbuf); | |
667 | #endif | |
48db90de | 668 | m_freem(m); |
75334b2a BF |
669 | } |
670 | ||
671 | /* | |
672 | * Routine to copy from UNIBUS memory into mbufs. | |
673 | * Similar in spirit to if_rubaget. | |
4a404244 BJ |
674 | * |
675 | * Warning: This makes the fairly safe assumption that | |
676 | * mbufs have even lengths. | |
75334b2a BF |
677 | */ |
678 | struct mbuf * | |
679 | ecget(ecbuf, totlen, off0) | |
48db90de | 680 | u_char *ecbuf; |
75334b2a BF |
681 | int totlen, off0; |
682 | { | |
48db90de SL |
683 | register struct mbuf *m; |
684 | struct mbuf *top = 0, **mp = ⊤ | |
685 | register int off = off0, len; | |
686 | u_char *cp; | |
75334b2a | 687 | |
0ca845ca | 688 | cp = ecbuf + ECRDOFF + sizeof (struct ec_header); |
75334b2a | 689 | while (totlen > 0) { |
48db90de SL |
690 | register int words; |
691 | u_char *mcp; | |
692 | ||
75334b2a BF |
693 | MGET(m, 0); |
694 | if (m == 0) | |
695 | goto bad; | |
696 | if (off) { | |
697 | len = totlen - off; | |
698 | cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off; | |
699 | } else | |
700 | len = totlen; | |
701 | if (len >= CLBYTES) { | |
702 | struct mbuf *p; | |
703 | ||
704 | MCLGET(p, 1); | |
705 | if (p != 0) { | |
706 | m->m_len = len = CLBYTES; | |
707 | m->m_off = (int)p - (int)m; | |
708 | } else { | |
709 | m->m_len = len = MIN(MLEN, len); | |
710 | m->m_off = MMINOFF; | |
711 | } | |
712 | } else { | |
713 | m->m_len = len = MIN(MLEN, len); | |
714 | m->m_off = MMINOFF; | |
715 | } | |
48db90de SL |
716 | mcp = mtod(m, u_char *); |
717 | if (words = (len >> 1)) { | |
718 | register u_short *to, *from; | |
719 | ||
720 | to = (u_short *)mcp; | |
721 | from = (u_short *)cp; | |
722 | do | |
723 | *to++ = *from++; | |
724 | while (--words > 0); | |
725 | mcp = (u_char *)to; | |
726 | cp = (u_char *)from; | |
4a404244 | 727 | } |
0ca845ca | 728 | if (len & 01) |
75334b2a BF |
729 | *mcp++ = *cp++; |
730 | *mp = m; | |
731 | mp = &m->m_next; | |
48db90de | 732 | if (off == 0) { |
75334b2a | 733 | totlen -= len; |
48db90de SL |
734 | continue; |
735 | } | |
736 | off += len; | |
737 | if (off == totlen) { | |
738 | cp = ecbuf + ECRDOFF + sizeof (struct ec_header); | |
739 | off = 0; | |
740 | totlen = off0; | |
741 | } | |
75334b2a BF |
742 | } |
743 | return (top); | |
744 | bad: | |
745 | m_freem(top); | |
746 | return (0); | |
747 | } |