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