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