Commit | Line | Data |
---|---|---|
51595ca2 | 1 | /* if_ec.c 4.28 82/10/31 */ |
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(); | |
39d536e6 | 48 | struct mbuf *ecget(); |
75334b2a | 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 | 68 | short es_oactive; /* is output active? */ |
39d536e6 | 69 | u_char *es_buf[16]; /* virtual addresses of buffers */ |
75334b2a BF |
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 | */ | |
39d536e6 | 96 | (void) ubamem(numuba, ECMEM, 32*2, 0); |
75334b2a BF |
97 | /* |
98 | * Check for existence of buffers on Unibus. | |
75334b2a | 99 | */ |
39d536e6 | 100 | if (badaddr((caddr_t)ecbuf, 2)) { |
a26646de BF |
101 | bad1: |
102 | printf("ec: buffer mem not found\n"); | |
103 | bad2: | |
39d536e6 | 104 | (void) ubamem(numuba, 0, 0, 0); /* reenable map (780 only) */ |
a26646de | 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; | |
51595ca2 | 201 | ifp->if_reset = ecreset; |
75334b2a | 202 | for (i=0; i<16; i++) |
39d536e6 | 203 | es->es_buf[i] = (u_char *)&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); | |
39d536e6 | 220 | (void) ubamem(uban, ECMEM, 32*2, 0); /* mr 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 | { | |
39d536e6 | 264 | int unit = ECUNIT(dev); |
0ca845ca SL |
265 | struct ec_softc *es = &ec_softc[unit]; |
266 | struct ecdevice *addr; | |
75334b2a | 267 | struct mbuf *m; |
75334b2a BF |
268 | |
269 | if (es->es_oactive) | |
270 | goto restart; | |
271 | ||
75334b2a BF |
272 | IF_DEQUEUE(&es->es_if.if_snd, m); |
273 | if (m == 0) { | |
274 | es->es_oactive = 0; | |
275 | return; | |
276 | } | |
75334b2a BF |
277 | ecput(es->es_buf[ECTBF], m); |
278 | ||
75334b2a | 279 | restart: |
0ca845ca | 280 | addr = (struct ecdevice *)ecinfo[unit]->ui_addr; |
75334b2a BF |
281 | addr->ec_xcr = EC_WRITE|ECTBF; |
282 | es->es_oactive = 1; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Ethernet interface transmitter interrupt. | |
287 | * Start another output if more data to send. | |
288 | */ | |
289 | ecxint(unit) | |
290 | int unit; | |
291 | { | |
75334b2a | 292 | register struct ec_softc *es = &ec_softc[unit]; |
0ca845ca SL |
293 | register struct ecdevice *addr = |
294 | (struct ecdevice *)ecinfo[unit]->ui_addr; | |
75334b2a BF |
295 | |
296 | if (es->es_oactive == 0) | |
297 | return; | |
0ca845ca SL |
298 | if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) { |
299 | printf("ec%d: stray xmit interrupt, xcr=%b\n", unit, | |
300 | addr->ec_xcr, EC_XBITS); | |
301 | es->es_oactive = 0; | |
302 | addr->ec_xcr = EC_XCLR; | |
303 | return; | |
304 | } | |
75334b2a BF |
305 | es->es_if.if_opackets++; |
306 | es->es_oactive = 0; | |
75334b2a BF |
307 | es->es_mask = ~0; |
308 | addr->ec_xcr = EC_XCLR; | |
0ca845ca SL |
309 | if (es->es_if.if_snd.ifq_head) |
310 | ecstart(unit); | |
75334b2a BF |
311 | } |
312 | ||
313 | /* | |
314 | * Collision on ethernet interface. Do exponential | |
315 | * backoff, and retransmit. If have backed off all | |
316 | * the way print warning diagnostic, and drop packet. | |
317 | */ | |
318 | eccollide(unit) | |
319 | int unit; | |
320 | { | |
321 | struct ec_softc *es = &ec_softc[unit]; | |
75334b2a | 322 | |
75334b2a | 323 | es->es_if.if_collisions++; |
0ca845ca SL |
324 | if (es->es_oactive) |
325 | ecdocoll(unit); | |
75334b2a BF |
326 | } |
327 | ||
328 | ecdocoll(unit) | |
329 | int unit; | |
330 | { | |
331 | register struct ec_softc *es = &ec_softc[unit]; | |
be515f6e BF |
332 | register struct ecdevice *addr = |
333 | (struct ecdevice *)ecinfo[unit]->ui_addr; | |
334 | register i; | |
335 | int delay; | |
75334b2a BF |
336 | |
337 | /* | |
338 | * Es_mask is a 16 bit number with n low zero bits, with | |
339 | * n the number of backoffs. When es_mask is 0 we have | |
340 | * backed off 16 times, and give up. | |
341 | */ | |
342 | if (es->es_mask == 0) { | |
be515f6e | 343 | es->es_if.if_oerrors++; |
75334b2a BF |
344 | printf("ec%d: send error\n", unit); |
345 | /* | |
be515f6e BF |
346 | * Reset interface, then requeue rcv buffers. |
347 | * Some incoming packets may be lost, but that | |
348 | * can't be helped. | |
349 | */ | |
350 | addr->ec_xcr = EC_UECLR; | |
351 | for (i=ECRHBF; i>=ECRLBF; i--) | |
352 | addr->ec_rcr = EC_READ|i; | |
353 | /* | |
354 | * Reset and transmit next packet (if any). | |
75334b2a | 355 | */ |
be515f6e BF |
356 | es->es_oactive = 0; |
357 | es->es_mask = ~0; | |
358 | if (es->es_if.if_snd.ifq_head) | |
359 | ecstart(unit); | |
75334b2a BF |
360 | return; |
361 | } | |
362 | /* | |
be515f6e BF |
363 | * Do exponential backoff. Compute delay based on low bits |
364 | * of the interval timer. Then delay for that number of | |
365 | * slot times. A slot time is 51.2 microseconds (rounded to 51). | |
366 | * This does not take into account the time already used to | |
367 | * process the interrupt. | |
75334b2a BF |
368 | */ |
369 | es->es_mask <<= 1; | |
be515f6e BF |
370 | delay = mfpr(ICR) &~ es->es_mask; |
371 | DELAY(delay * 51); | |
75334b2a | 372 | /* |
be515f6e | 373 | * Clear the controller's collision flag, thus enabling retransmit. |
75334b2a | 374 | */ |
a26646de | 375 | addr->ec_xcr = EC_CLEAR; |
75334b2a BF |
376 | } |
377 | ||
75334b2a BF |
378 | /* |
379 | * Ethernet interface receiver interrupt. | |
380 | * If input error just drop packet. | |
381 | * Otherwise purge input buffered data path and examine | |
382 | * packet to determine type. If can't determine length | |
383 | * from type, then have to drop packet. Othewise decapsulate | |
384 | * packet based on type and pass to type specific higher-level | |
385 | * input routine. | |
386 | */ | |
387 | ecrint(unit) | |
388 | int unit; | |
389 | { | |
390 | struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
75334b2a | 391 | |
75334b2a BF |
392 | while (addr->ec_rcr & EC_RDONE) |
393 | ecread(unit); | |
394 | } | |
395 | ||
396 | ecread(unit) | |
397 | int unit; | |
398 | { | |
399 | register struct ec_softc *es = &ec_softc[unit]; | |
400 | struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
401 | register struct ec_header *ec; | |
402 | struct mbuf *m; | |
39d536e6 | 403 | int len, off, resid, ecoff, rbuf; |
75334b2a | 404 | register struct ifqueue *inq; |
39d536e6 | 405 | u_char *ecbuf; |
75334b2a BF |
406 | |
407 | es->es_if.if_ipackets++; | |
39d536e6 BJ |
408 | rbuf = addr->ec_rcr & EC_RBN; |
409 | if (rbuf < ECRLBF || rbuf > ECRHBF) | |
75334b2a | 410 | panic("ecrint"); |
39d536e6 | 411 | ecbuf = es->es_buf[rbuf]; |
75334b2a | 412 | ecoff = *(short *)ecbuf; |
be515f6e | 413 | if (ecoff <= ECRDOFF || ecoff > 2046) { |
75334b2a BF |
414 | es->es_if.if_ierrors++; |
415 | #ifdef notdef | |
416 | if (es->es_if.if_ierrors % 100 == 0) | |
417 | printf("ec%d: += 100 input errors\n", unit); | |
418 | #endif | |
75334b2a BF |
419 | goto setup; |
420 | } | |
421 | ||
422 | /* | |
423 | * Get input data length. | |
424 | * Get pointer to ethernet header (in input buffer). | |
425 | * Deal with trailer protocol: if type is PUP trailer | |
426 | * get true type from first 16-bit word past data. | |
427 | * Remember that type was trailer by setting off. | |
428 | */ | |
429 | len = ecoff - ECRDOFF - sizeof (struct ec_header); | |
430 | ec = (struct ec_header *)(ecbuf + ECRDOFF); | |
431 | #define ecdataaddr(ec, off, type) ((type)(((caddr_t)((ec)+1)+(off)))) | |
432 | if (ec->ec_type >= ECPUP_TRAIL && | |
433 | ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) { | |
434 | off = (ec->ec_type - ECPUP_TRAIL) * 512; | |
435 | if (off >= ECMTU) | |
436 | goto setup; /* sanity */ | |
437 | ec->ec_type = *ecdataaddr(ec, off, u_short *); | |
438 | resid = *(ecdataaddr(ec, off+2, u_short *)); | |
439 | if (off + resid > len) | |
440 | goto setup; /* sanity */ | |
441 | len = off + resid; | |
442 | } else | |
443 | off = 0; | |
444 | if (len == 0) | |
445 | goto setup; | |
446 | ||
447 | /* | |
448 | * Pull packet off interface. Off is nonzero if packet | |
449 | * has trailing header; ecget will then force this header | |
450 | * information to be at the front, but we still have to drop | |
451 | * the type and length which are at the front of any trailer data. | |
452 | */ | |
453 | m = ecget(ecbuf, len, off); | |
454 | if (m == 0) | |
455 | goto setup; | |
456 | if (off) { | |
457 | m->m_off += 2 * sizeof (u_short); | |
458 | m->m_len -= 2 * sizeof (u_short); | |
459 | } | |
460 | switch (ec->ec_type) { | |
461 | ||
462 | #ifdef INET | |
463 | case ECPUP_IPTYPE: | |
464 | schednetisr(NETISR_IP); | |
465 | inq = &ipintrq; | |
466 | break; | |
75334b2a BF |
467 | #endif |
468 | default: | |
469 | m_freem(m); | |
470 | goto setup; | |
471 | } | |
472 | ||
473 | if (IF_QFULL(inq)) { | |
474 | IF_DROP(inq); | |
475 | m_freem(m); | |
0ca845ca SL |
476 | goto setup; |
477 | } | |
478 | IF_ENQUEUE(inq, m); | |
75334b2a BF |
479 | |
480 | setup: | |
481 | /* | |
482 | * Reset for next packet. | |
483 | */ | |
39d536e6 | 484 | addr->ec_rcr = EC_READ|EC_RCLR|rbuf; |
75334b2a BF |
485 | } |
486 | ||
487 | /* | |
488 | * Ethernet output routine. | |
489 | * Encapsulate a packet of type family for the local net. | |
490 | * Use trailer local net encapsulation if enough data in first | |
491 | * packet leaves a multiple of 512 bytes of data in remainder. | |
be515f6e BF |
492 | * If destination is this address or broadcast, send packet to |
493 | * loop device to kludge around the fact that 3com interfaces can't | |
494 | * talk to themselves. | |
75334b2a BF |
495 | */ |
496 | ecoutput(ifp, m0, dst) | |
497 | struct ifnet *ifp; | |
498 | struct mbuf *m0; | |
499 | struct sockaddr *dst; | |
500 | { | |
501 | int type, dest, s, error; | |
502 | register struct ec_softc *es = &ec_softc[ifp->if_unit]; | |
503 | register struct mbuf *m = m0; | |
504 | register struct ec_header *ec; | |
0ca845ca | 505 | register int off, i; |
be515f6e | 506 | struct mbuf *mcopy = (struct mbuf *) 0; /* Null */ |
75334b2a | 507 | |
75334b2a BF |
508 | switch (dst->sa_family) { |
509 | ||
510 | #ifdef INET | |
511 | case AF_INET: | |
512 | dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr; | |
be515f6e | 513 | if ((dest &~ 0xff) == 0) |
92aca3da | 514 | mcopy = m_copy(m, 0, (int)M_COPYALL); |
be515f6e BF |
515 | else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)-> |
516 | sin_addr.s_addr) { | |
517 | mcopy = m; | |
518 | goto gotlocal; | |
519 | } | |
75334b2a BF |
520 | off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; |
521 | if (off > 0 && (off & 0x1ff) == 0 && | |
522 | m->m_off >= MMINOFF + 2 * sizeof (u_short)) { | |
523 | type = ECPUP_TRAIL + (off>>9); | |
524 | m->m_off -= 2 * sizeof (u_short); | |
525 | m->m_len += 2 * sizeof (u_short); | |
526 | *mtod(m, u_short *) = ECPUP_IPTYPE; | |
527 | *(mtod(m, u_short *) + 1) = m->m_len; | |
528 | goto gottrailertype; | |
529 | } | |
530 | type = ECPUP_IPTYPE; | |
531 | off = 0; | |
532 | goto gottype; | |
75334b2a BF |
533 | #endif |
534 | ||
535 | default: | |
536 | printf("ec%d: can't handle af%d\n", ifp->if_unit, | |
537 | dst->sa_family); | |
538 | error = EAFNOSUPPORT; | |
539 | goto bad; | |
540 | } | |
541 | ||
542 | gottrailertype: | |
543 | /* | |
544 | * Packet to be sent as trailer: move first packet | |
545 | * (control information) to end of chain. | |
546 | */ | |
547 | while (m->m_next) | |
548 | m = m->m_next; | |
549 | m->m_next = m0; | |
550 | m = m0->m_next; | |
551 | m0->m_next = 0; | |
552 | m0 = m; | |
553 | ||
554 | gottype: | |
555 | /* | |
556 | * Add local net header. If no space in first mbuf, | |
557 | * allocate another. | |
558 | */ | |
559 | if (m->m_off > MMAXOFF || | |
560 | MMINOFF + sizeof (struct ec_header) > m->m_off) { | |
561 | m = m_get(M_DONTWAIT); | |
562 | if (m == 0) { | |
563 | error = ENOBUFS; | |
564 | goto bad; | |
565 | } | |
566 | m->m_next = m0; | |
567 | m->m_off = MMINOFF; | |
568 | m->m_len = sizeof (struct ec_header); | |
569 | } else { | |
570 | m->m_off -= sizeof (struct ec_header); | |
571 | m->m_len += sizeof (struct ec_header); | |
572 | } | |
573 | ec = mtod(m, struct ec_header *); | |
574 | for (i=0; i<6; i++) | |
575 | ec->ec_shost[i] = es->es_enaddr[i]; | |
f53ac196 | 576 | if ((dest &~ 0xff) == 0) |
0ca845ca | 577 | /* broadcast address */ |
75334b2a BF |
578 | for (i=0; i<6; i++) |
579 | ec->ec_dhost[i] = 0xff; | |
580 | else { | |
cf1ea369 BF |
581 | if (dest & 0x8000) { |
582 | ec->ec_dhost[0] = ec_iltop[0]; | |
583 | ec->ec_dhost[1] = ec_iltop[1]; | |
584 | ec->ec_dhost[2] = ec_iltop[2]; | |
585 | } else { | |
586 | ec->ec_dhost[0] = es->es_enaddr[0]; | |
587 | ec->ec_dhost[1] = es->es_enaddr[1]; | |
588 | ec->ec_dhost[2] = es->es_enaddr[2]; | |
589 | } | |
590 | ec->ec_dhost[3] = (dest>>8) & 0x7f; | |
75334b2a BF |
591 | ec->ec_dhost[4] = (dest>>16) & 0xff; |
592 | ec->ec_dhost[5] = (dest>>24) & 0xff; | |
593 | } | |
594 | ec->ec_type = type; | |
595 | ||
596 | /* | |
597 | * Queue message on interface, and start output if interface | |
598 | * not yet active. | |
599 | */ | |
600 | s = splimp(); | |
601 | if (IF_QFULL(&ifp->if_snd)) { | |
602 | IF_DROP(&ifp->if_snd); | |
603 | error = ENOBUFS; | |
604 | goto qfull; | |
605 | } | |
606 | IF_ENQUEUE(&ifp->if_snd, m); | |
607 | if (es->es_oactive == 0) | |
608 | ecstart(ifp->if_unit); | |
609 | splx(s); | |
0ca845ca | 610 | |
be515f6e | 611 | gotlocal: |
0ca845ca SL |
612 | return(mcopy ? looutput(&loif, mcopy, dst) : 0); |
613 | ||
75334b2a BF |
614 | qfull: |
615 | m0 = m; | |
616 | splx(s); | |
617 | bad: | |
618 | m_freem(m0); | |
619 | return(error); | |
620 | } | |
621 | ||
622 | /* | |
0ca845ca SL |
623 | * Routine to copy from mbuf chain to transmitter |
624 | * buffer in UNIBUS memory. | |
75334b2a BF |
625 | */ |
626 | ecput(ecbuf, m) | |
0ca845ca | 627 | u_char *ecbuf; |
75334b2a BF |
628 | struct mbuf *m; |
629 | { | |
75334b2a | 630 | register struct mbuf *mp; |
0ca845ca | 631 | register int off; |
48db90de | 632 | u_char *bp; |
75334b2a | 633 | |
0ca845ca SL |
634 | for (off = 2048, mp = m; mp; mp = mp->m_next) |
635 | off -= mp->m_len; | |
636 | *(u_short *)ecbuf = off; | |
637 | bp = (u_char *)(ecbuf + off); | |
48db90de SL |
638 | for (mp = m; mp; mp = mp->m_next) { |
639 | register unsigned len = mp->m_len; | |
640 | u_char *mcp; | |
0ca845ca | 641 | |
0ca845ca SL |
642 | if (len == 0) |
643 | continue; | |
644 | mcp = mtod(mp, u_char *); | |
645 | if ((unsigned)bp & 01) { | |
4a404244 | 646 | *bp++ = *mcp++; |
0ca845ca | 647 | len--; |
4a404244 | 648 | } |
48db90de SL |
649 | if (off = (len >> 1)) { |
650 | register u_short *to, *from; | |
651 | ||
652 | to = (u_short *)bp; | |
653 | from = (u_short *)mcp; | |
654 | do | |
655 | *to++ = *from++; | |
656 | while (--off > 0); | |
657 | bp = (u_char *)to, | |
658 | mcp = (u_char *)from; | |
4a404244 | 659 | } |
48db90de | 660 | if (len & 01) |
75334b2a | 661 | *bp++ = *mcp++; |
75334b2a | 662 | } |
0ca845ca SL |
663 | #ifdef notdef |
664 | if (bp - ecbuf != 2048) | |
665 | printf("ec: bad ecput, diff=%d\n", bp-ecbuf); | |
666 | #endif | |
48db90de | 667 | m_freem(m); |
75334b2a BF |
668 | } |
669 | ||
670 | /* | |
671 | * Routine to copy from UNIBUS memory into mbufs. | |
672 | * Similar in spirit to if_rubaget. | |
4a404244 BJ |
673 | * |
674 | * Warning: This makes the fairly safe assumption that | |
675 | * mbufs have even lengths. | |
75334b2a BF |
676 | */ |
677 | struct mbuf * | |
678 | ecget(ecbuf, totlen, off0) | |
48db90de | 679 | u_char *ecbuf; |
75334b2a BF |
680 | int totlen, off0; |
681 | { | |
48db90de SL |
682 | register struct mbuf *m; |
683 | struct mbuf *top = 0, **mp = ⊤ | |
684 | register int off = off0, len; | |
685 | u_char *cp; | |
75334b2a | 686 | |
0ca845ca | 687 | cp = ecbuf + ECRDOFF + sizeof (struct ec_header); |
75334b2a | 688 | while (totlen > 0) { |
48db90de SL |
689 | register int words; |
690 | u_char *mcp; | |
691 | ||
75334b2a BF |
692 | MGET(m, 0); |
693 | if (m == 0) | |
694 | goto bad; | |
695 | if (off) { | |
696 | len = totlen - off; | |
697 | cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off; | |
698 | } else | |
699 | len = totlen; | |
700 | if (len >= CLBYTES) { | |
701 | struct mbuf *p; | |
702 | ||
703 | MCLGET(p, 1); | |
704 | if (p != 0) { | |
705 | m->m_len = len = CLBYTES; | |
706 | m->m_off = (int)p - (int)m; | |
707 | } else { | |
708 | m->m_len = len = MIN(MLEN, len); | |
709 | m->m_off = MMINOFF; | |
710 | } | |
711 | } else { | |
712 | m->m_len = len = MIN(MLEN, len); | |
713 | m->m_off = MMINOFF; | |
714 | } | |
48db90de SL |
715 | mcp = mtod(m, u_char *); |
716 | if (words = (len >> 1)) { | |
717 | register u_short *to, *from; | |
718 | ||
719 | to = (u_short *)mcp; | |
720 | from = (u_short *)cp; | |
721 | do | |
722 | *to++ = *from++; | |
723 | while (--words > 0); | |
724 | mcp = (u_char *)to; | |
725 | cp = (u_char *)from; | |
4a404244 | 726 | } |
0ca845ca | 727 | if (len & 01) |
75334b2a BF |
728 | *mcp++ = *cp++; |
729 | *mp = m; | |
730 | mp = &m->m_next; | |
48db90de | 731 | if (off == 0) { |
75334b2a | 732 | totlen -= len; |
48db90de SL |
733 | continue; |
734 | } | |
735 | off += len; | |
736 | if (off == totlen) { | |
737 | cp = ecbuf + ECRDOFF + sizeof (struct ec_header); | |
738 | off = 0; | |
739 | totlen = off0; | |
740 | } | |
75334b2a BF |
741 | } |
742 | return (top); | |
743 | bad: | |
744 | m_freem(top); | |
745 | return (0); | |
746 | } |