Commit | Line | Data |
---|---|---|
17718098 | 1 | /* if_ec.c 4.36 83/06/13 */ |
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" |
1cd789fb SL |
17 | #include "../h/ioctl.h" |
18 | #include "../h/errno.h" | |
a9687d27 BJ |
19 | |
20 | #include "../net/if.h" | |
21 | #include "../net/netisr.h" | |
22 | #include "../net/route.h" | |
d2cc167c BJ |
23 | #include "../netinet/in.h" |
24 | #include "../netinet/in_systm.h" | |
d2cc167c BJ |
25 | #include "../netinet/ip.h" |
26 | #include "../netinet/ip_var.h" | |
8ae4cebd | 27 | #include "../netinet/if_ether.h" |
d2cc167c | 28 | #include "../netpup/pup.h" |
a9687d27 BJ |
29 | |
30 | #include "../vax/cpu.h" | |
31 | #include "../vax/mtpr.h" | |
a9687d27 BJ |
32 | #include "../vaxif/if_ecreg.h" |
33 | #include "../vaxif/if_uba.h" | |
34 | #include "../vaxuba/ubareg.h" | |
35 | #include "../vaxuba/ubavar.h" | |
75334b2a | 36 | |
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 }; | |
44 | #define ECUNIT(x) minor(x) | |
45 | ||
1cd789fb | 46 | int ecinit(),ecioctl(),ecoutput(),ecreset(); |
39d536e6 | 47 | struct mbuf *ecget(); |
75334b2a | 48 | |
be515f6e BF |
49 | extern struct ifnet loif; |
50 | ||
75334b2a BF |
51 | /* |
52 | * Ethernet software status per interface. | |
53 | * | |
54 | * Each interface is referenced by a network interface structure, | |
55 | * es_if, which the routing code uses to locate the interface. | |
56 | * This structure contains the output queue for the interface, its address, ... | |
57 | * We also have, for each interface, a UBA interface structure, which | |
58 | * contains information about the UNIBUS resources held by the interface: | |
59 | * map registers, buffered data paths, etc. Information is cached in this | |
60 | * structure for use by the if_uba.c routines in running the interface | |
61 | * efficiently. | |
62 | */ | |
63 | struct ec_softc { | |
8ae4cebd SL |
64 | struct arpcom es_ac; /* common Ethernet structures */ |
65 | #define es_if es_ac.ac_if /* network-visible interface */ | |
66 | #define es_addr es_ac.ac_enaddr /* hardware Ethernet address */ | |
75334b2a | 67 | struct ifuba es_ifuba; /* UNIBUS resources */ |
75334b2a | 68 | short es_mask; /* mask for current output delay */ |
75334b2a | 69 | short es_oactive; /* is output active? */ |
39d536e6 | 70 | u_char *es_buf[16]; /* virtual addresses of buffers */ |
75334b2a BF |
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"; | |
3fa8d9bb | 164 | ifp->if_mtu = ETHERMTU; |
75334b2a BF |
165 | |
166 | /* | |
0ca845ca | 167 | * Read the ethernet address off the board, one nibble at a time. |
75334b2a BF |
168 | */ |
169 | addr->ec_xcr = EC_UECLR; | |
170 | addr->ec_rcr = EC_AROM; | |
8ae4cebd | 171 | cp = es->es_addr; |
0ca845ca | 172 | #define NEXTBIT addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM |
75334b2a BF |
173 | for (i=0; i<6; i++) { |
174 | *cp = 0; | |
175 | for (j=0; j<=4; j+=4) { | |
176 | *cp |= ((addr->ec_rcr >> 8) & 0xf) << j; | |
0ca845ca | 177 | NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT; |
75334b2a BF |
178 | } |
179 | cp++; | |
180 | } | |
75334b2a BF |
181 | sin = (struct sockaddr_in *)&es->es_if.if_addr; |
182 | sin->sin_family = AF_INET; | |
66923854 | 183 | sin->sin_addr = arpmyaddr((struct arpcom *)0); |
0ca845ca | 184 | ifp->if_init = ecinit; |
1cd789fb | 185 | ifp->if_ioctl = ecioctl; |
0ca845ca | 186 | ifp->if_output = ecoutput; |
51595ca2 | 187 | ifp->if_reset = ecreset; |
75334b2a | 188 | for (i=0; i<16; i++) |
39d536e6 | 189 | es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i]; |
0ca845ca | 190 | if_attach(ifp); |
75334b2a BF |
191 | } |
192 | ||
193 | /* | |
194 | * Reset of interface after UNIBUS reset. | |
195 | * If interface is on specified uba, reset its state. | |
196 | */ | |
197 | ecreset(unit, uban) | |
198 | int unit, uban; | |
199 | { | |
200 | register struct uba_device *ui; | |
75334b2a BF |
201 | |
202 | if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 || | |
203 | ui->ui_ubanum != uban) | |
204 | return; | |
205 | printf(" ec%d", unit); | |
39d536e6 | 206 | (void) ubamem(uban, ECMEM, 32*2, 0); /* mr disable (no alloc) */ |
75334b2a BF |
207 | ecinit(unit); |
208 | } | |
209 | ||
210 | /* | |
211 | * Initialization of interface; clear recorded pending | |
212 | * operations, and reinitialize UNIBUS usage. | |
213 | */ | |
214 | ecinit(unit) | |
215 | int unit; | |
216 | { | |
0ca845ca SL |
217 | struct ec_softc *es = &ec_softc[unit]; |
218 | struct ecdevice *addr; | |
8ae4cebd SL |
219 | register struct ifnet *ifp = &es->es_if; |
220 | register struct sockaddr_in *sin, *sinb; | |
1cd789fb | 221 | int i, s; |
8ae4cebd SL |
222 | |
223 | sin = (struct sockaddr_in *)&ifp->if_addr; | |
1cd789fb | 224 | if (sin->sin_addr.s_addr == 0) /* address still unknown */ |
8ae4cebd | 225 | return; |
75334b2a BF |
226 | |
227 | /* | |
a0d46072 | 228 | * Hang receive buffers and start any pending writes. |
f5a616ae BF |
229 | * Writing into the rcr also makes sure the memory |
230 | * is turned on. | |
75334b2a | 231 | */ |
1cd789fb SL |
232 | if ((es->es_if.if_flags & IFF_RUNNING) == 0) { |
233 | addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
234 | s = splimp(); | |
235 | for (i = ECRHBF; i >= ECRLBF; i--) | |
236 | addr->ec_rcr = EC_READ | i; | |
237 | es->es_oactive = 0; | |
238 | es->es_mask = ~0; | |
239 | es->es_if.if_flags |= IFF_UP|IFF_RUNNING; | |
240 | if (es->es_if.if_snd.ifq_head) | |
241 | ecstart(unit); | |
242 | splx(s); | |
243 | } | |
a13c006d | 244 | if_rtinit(&es->es_if, RTF_UP); |
8ae4cebd SL |
245 | arpattach(&es->es_ac); |
246 | arpwhohas(&es->es_ac, &sin->sin_addr); | |
75334b2a BF |
247 | } |
248 | ||
75334b2a BF |
249 | /* |
250 | * Start or restart output on interface. | |
251 | * If interface is already active, then this is a retransmit | |
be515f6e | 252 | * after a collision, and just restuff registers. |
75334b2a BF |
253 | * If interface is not already active, get another datagram |
254 | * to send off of the interface queue, and map it to the interface | |
255 | * before starting the output. | |
256 | */ | |
257 | ecstart(dev) | |
258 | dev_t dev; | |
259 | { | |
39d536e6 | 260 | int unit = ECUNIT(dev); |
0ca845ca SL |
261 | struct ec_softc *es = &ec_softc[unit]; |
262 | struct ecdevice *addr; | |
75334b2a | 263 | struct mbuf *m; |
75334b2a BF |
264 | |
265 | if (es->es_oactive) | |
266 | goto restart; | |
267 | ||
75334b2a BF |
268 | IF_DEQUEUE(&es->es_if.if_snd, m); |
269 | if (m == 0) { | |
270 | es->es_oactive = 0; | |
271 | return; | |
272 | } | |
75334b2a BF |
273 | ecput(es->es_buf[ECTBF], m); |
274 | ||
75334b2a | 275 | restart: |
0ca845ca | 276 | addr = (struct ecdevice *)ecinfo[unit]->ui_addr; |
75334b2a BF |
277 | addr->ec_xcr = EC_WRITE|ECTBF; |
278 | es->es_oactive = 1; | |
279 | } | |
280 | ||
281 | /* | |
282 | * Ethernet interface transmitter interrupt. | |
283 | * Start another output if more data to send. | |
284 | */ | |
285 | ecxint(unit) | |
286 | int unit; | |
287 | { | |
75334b2a | 288 | register struct ec_softc *es = &ec_softc[unit]; |
0ca845ca SL |
289 | register struct ecdevice *addr = |
290 | (struct ecdevice *)ecinfo[unit]->ui_addr; | |
75334b2a BF |
291 | |
292 | if (es->es_oactive == 0) | |
293 | return; | |
0ca845ca SL |
294 | if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) { |
295 | printf("ec%d: stray xmit interrupt, xcr=%b\n", unit, | |
296 | addr->ec_xcr, EC_XBITS); | |
297 | es->es_oactive = 0; | |
298 | addr->ec_xcr = EC_XCLR; | |
299 | return; | |
300 | } | |
75334b2a BF |
301 | es->es_if.if_opackets++; |
302 | es->es_oactive = 0; | |
75334b2a BF |
303 | es->es_mask = ~0; |
304 | addr->ec_xcr = EC_XCLR; | |
0ca845ca SL |
305 | if (es->es_if.if_snd.ifq_head) |
306 | ecstart(unit); | |
75334b2a BF |
307 | } |
308 | ||
309 | /* | |
310 | * Collision on ethernet interface. Do exponential | |
311 | * backoff, and retransmit. If have backed off all | |
312 | * the way print warning diagnostic, and drop packet. | |
313 | */ | |
314 | eccollide(unit) | |
315 | int unit; | |
316 | { | |
317 | struct ec_softc *es = &ec_softc[unit]; | |
75334b2a | 318 | |
75334b2a | 319 | es->es_if.if_collisions++; |
0ca845ca SL |
320 | if (es->es_oactive) |
321 | ecdocoll(unit); | |
75334b2a BF |
322 | } |
323 | ||
324 | ecdocoll(unit) | |
325 | int unit; | |
326 | { | |
327 | register struct ec_softc *es = &ec_softc[unit]; | |
be515f6e BF |
328 | register struct ecdevice *addr = |
329 | (struct ecdevice *)ecinfo[unit]->ui_addr; | |
330 | register i; | |
331 | int delay; | |
75334b2a BF |
332 | |
333 | /* | |
334 | * Es_mask is a 16 bit number with n low zero bits, with | |
335 | * n the number of backoffs. When es_mask is 0 we have | |
336 | * backed off 16 times, and give up. | |
337 | */ | |
338 | if (es->es_mask == 0) { | |
be515f6e | 339 | es->es_if.if_oerrors++; |
75334b2a BF |
340 | printf("ec%d: send error\n", unit); |
341 | /* | |
be515f6e BF |
342 | * Reset interface, then requeue rcv buffers. |
343 | * Some incoming packets may be lost, but that | |
344 | * can't be helped. | |
345 | */ | |
346 | addr->ec_xcr = EC_UECLR; | |
347 | for (i=ECRHBF; i>=ECRLBF; i--) | |
348 | addr->ec_rcr = EC_READ|i; | |
349 | /* | |
350 | * Reset and transmit next packet (if any). | |
75334b2a | 351 | */ |
be515f6e BF |
352 | es->es_oactive = 0; |
353 | es->es_mask = ~0; | |
354 | if (es->es_if.if_snd.ifq_head) | |
355 | ecstart(unit); | |
75334b2a BF |
356 | return; |
357 | } | |
358 | /* | |
be515f6e BF |
359 | * Do exponential backoff. Compute delay based on low bits |
360 | * of the interval timer. Then delay for that number of | |
361 | * slot times. A slot time is 51.2 microseconds (rounded to 51). | |
362 | * This does not take into account the time already used to | |
363 | * process the interrupt. | |
75334b2a BF |
364 | */ |
365 | es->es_mask <<= 1; | |
be515f6e BF |
366 | delay = mfpr(ICR) &~ es->es_mask; |
367 | DELAY(delay * 51); | |
75334b2a | 368 | /* |
be515f6e | 369 | * Clear the controller's collision flag, thus enabling retransmit. |
75334b2a | 370 | */ |
a26646de | 371 | addr->ec_xcr = EC_CLEAR; |
75334b2a BF |
372 | } |
373 | ||
75334b2a BF |
374 | /* |
375 | * Ethernet interface receiver interrupt. | |
376 | * If input error just drop packet. | |
377 | * Otherwise purge input buffered data path and examine | |
378 | * packet to determine type. If can't determine length | |
379 | * from type, then have to drop packet. Othewise decapsulate | |
380 | * packet based on type and pass to type specific higher-level | |
381 | * input routine. | |
382 | */ | |
383 | ecrint(unit) | |
384 | int unit; | |
385 | { | |
386 | struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
75334b2a | 387 | |
75334b2a BF |
388 | while (addr->ec_rcr & EC_RDONE) |
389 | ecread(unit); | |
390 | } | |
391 | ||
392 | ecread(unit) | |
393 | int unit; | |
394 | { | |
395 | register struct ec_softc *es = &ec_softc[unit]; | |
396 | struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr; | |
3fa8d9bb | 397 | register struct ether_header *ec; |
75334b2a | 398 | struct mbuf *m; |
39d536e6 | 399 | int len, off, resid, ecoff, rbuf; |
75334b2a | 400 | register struct ifqueue *inq; |
39d536e6 | 401 | u_char *ecbuf; |
75334b2a BF |
402 | |
403 | es->es_if.if_ipackets++; | |
39d536e6 BJ |
404 | rbuf = addr->ec_rcr & EC_RBN; |
405 | if (rbuf < ECRLBF || rbuf > ECRHBF) | |
75334b2a | 406 | panic("ecrint"); |
39d536e6 | 407 | ecbuf = es->es_buf[rbuf]; |
75334b2a | 408 | ecoff = *(short *)ecbuf; |
be515f6e | 409 | if (ecoff <= ECRDOFF || ecoff > 2046) { |
75334b2a BF |
410 | es->es_if.if_ierrors++; |
411 | #ifdef notdef | |
412 | if (es->es_if.if_ierrors % 100 == 0) | |
413 | printf("ec%d: += 100 input errors\n", unit); | |
414 | #endif | |
75334b2a BF |
415 | goto setup; |
416 | } | |
417 | ||
418 | /* | |
419 | * Get input data length. | |
420 | * Get pointer to ethernet header (in input buffer). | |
421 | * Deal with trailer protocol: if type is PUP trailer | |
422 | * get true type from first 16-bit word past data. | |
423 | * Remember that type was trailer by setting off. | |
424 | */ | |
3fa8d9bb SL |
425 | len = ecoff - ECRDOFF - sizeof (struct ether_header); |
426 | ec = (struct ether_header *)(ecbuf + ECRDOFF); | |
427 | ec->ether_type = ntohs((u_short)ec->ether_type); | |
75334b2a | 428 | #define ecdataaddr(ec, off, type) ((type)(((caddr_t)((ec)+1)+(off)))) |
3fa8d9bb SL |
429 | if (ec->ether_type >= ETHERPUP_TRAIL && |
430 | ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) { | |
431 | off = (ec->ether_type - ETHERPUP_TRAIL) * 512; | |
432 | if (off >= ETHERMTU) | |
75334b2a | 433 | goto setup; /* sanity */ |
3fa8d9bb SL |
434 | ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *)); |
435 | resid = ntohs(*(ecdataaddr(ec, off+2, u_short *))); | |
75334b2a BF |
436 | if (off + resid > len) |
437 | goto setup; /* sanity */ | |
438 | len = off + resid; | |
439 | } else | |
440 | off = 0; | |
441 | if (len == 0) | |
442 | goto setup; | |
443 | ||
444 | /* | |
445 | * Pull packet off interface. Off is nonzero if packet | |
446 | * has trailing header; ecget will then force this header | |
447 | * information to be at the front, but we still have to drop | |
448 | * the type and length which are at the front of any trailer data. | |
449 | */ | |
450 | m = ecget(ecbuf, len, off); | |
451 | if (m == 0) | |
452 | goto setup; | |
453 | if (off) { | |
454 | m->m_off += 2 * sizeof (u_short); | |
455 | m->m_len -= 2 * sizeof (u_short); | |
456 | } | |
3fa8d9bb | 457 | switch (ec->ether_type) { |
75334b2a BF |
458 | |
459 | #ifdef INET | |
3fa8d9bb | 460 | case ETHERPUP_IPTYPE: |
75334b2a BF |
461 | schednetisr(NETISR_IP); |
462 | inq = &ipintrq; | |
463 | break; | |
8ae4cebd SL |
464 | |
465 | case ETHERPUP_ARPTYPE: | |
466 | arpinput(&es->es_ac, m); | |
467 | return; | |
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 | */ | |
39d536e6 | 485 | addr->ec_rcr = EC_READ|EC_RCLR|rbuf; |
75334b2a BF |
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 | { | |
8ae4cebd SL |
502 | int type, s, error; |
503 | u_char edst[6]; | |
504 | struct in_addr idst; | |
75334b2a BF |
505 | register struct ec_softc *es = &ec_softc[ifp->if_unit]; |
506 | register struct mbuf *m = m0; | |
3fa8d9bb | 507 | register struct ether_header *ec; |
66923854 | 508 | register int off; |
8ae4cebd | 509 | struct mbuf *mcopy = (struct mbuf *)0; |
75334b2a | 510 | |
75334b2a BF |
511 | switch (dst->sa_family) { |
512 | ||
513 | #ifdef INET | |
514 | case AF_INET: | |
8ae4cebd SL |
515 | idst = ((struct sockaddr_in *)dst)->sin_addr; |
516 | if (!arpresolve(&es->es_ac, m, &idst, edst)) | |
517 | return (0); /* if not yet resolved */ | |
518 | if (in_lnaof(idst) == INADDR_ANY) | |
92aca3da | 519 | mcopy = m_copy(m, 0, (int)M_COPYALL); |
75334b2a | 520 | off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len; |
1cd789fb SL |
521 | /* need per host negotiation */ |
522 | if ((ifp->if_flags & IFF_NOTRAILERS) == 0) | |
75334b2a BF |
523 | if (off > 0 && (off & 0x1ff) == 0 && |
524 | m->m_off >= MMINOFF + 2 * sizeof (u_short)) { | |
3fa8d9bb | 525 | type = ETHERPUP_TRAIL + (off>>9); |
75334b2a BF |
526 | m->m_off -= 2 * sizeof (u_short); |
527 | m->m_len += 2 * sizeof (u_short); | |
3fa8d9bb SL |
528 | *mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE); |
529 | *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len); | |
75334b2a BF |
530 | goto gottrailertype; |
531 | } | |
3fa8d9bb | 532 | type = ETHERPUP_IPTYPE; |
75334b2a BF |
533 | off = 0; |
534 | goto gottype; | |
75334b2a BF |
535 | #endif |
536 | ||
8ae4cebd SL |
537 | case AF_UNSPEC: |
538 | ec = (struct ether_header *)dst->sa_data; | |
66923854 | 539 | bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst)); |
8ae4cebd SL |
540 | type = ec->ether_type; |
541 | goto gottype; | |
542 | ||
75334b2a BF |
543 | default: |
544 | printf("ec%d: can't handle af%d\n", ifp->if_unit, | |
545 | dst->sa_family); | |
546 | error = EAFNOSUPPORT; | |
547 | goto bad; | |
548 | } | |
549 | ||
550 | gottrailertype: | |
551 | /* | |
552 | * Packet to be sent as trailer: move first packet | |
553 | * (control information) to end of chain. | |
554 | */ | |
555 | while (m->m_next) | |
556 | m = m->m_next; | |
557 | m->m_next = m0; | |
558 | m = m0->m_next; | |
559 | m0->m_next = 0; | |
560 | m0 = m; | |
561 | ||
562 | gottype: | |
563 | /* | |
564 | * Add local net header. If no space in first mbuf, | |
565 | * allocate another. | |
566 | */ | |
567 | if (m->m_off > MMAXOFF || | |
3fa8d9bb | 568 | MMINOFF + sizeof (struct ether_header) > m->m_off) { |
cce93e4b | 569 | m = m_get(M_DONTWAIT, MT_HEADER); |
75334b2a BF |
570 | if (m == 0) { |
571 | error = ENOBUFS; | |
572 | goto bad; | |
573 | } | |
574 | m->m_next = m0; | |
575 | m->m_off = MMINOFF; | |
3fa8d9bb | 576 | m->m_len = sizeof (struct ether_header); |
75334b2a | 577 | } else { |
3fa8d9bb SL |
578 | m->m_off -= sizeof (struct ether_header); |
579 | m->m_len += sizeof (struct ether_header); | |
75334b2a | 580 | } |
3fa8d9bb | 581 | ec = mtod(m, struct ether_header *); |
66923854 | 582 | bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst)); |
3fa8d9bb | 583 | ec->ether_type = htons((u_short)type); |
8ae4cebd | 584 | bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost, 6); |
75334b2a BF |
585 | |
586 | /* | |
587 | * Queue message on interface, and start output if interface | |
588 | * not yet active. | |
589 | */ | |
590 | s = splimp(); | |
591 | if (IF_QFULL(&ifp->if_snd)) { | |
592 | IF_DROP(&ifp->if_snd); | |
593 | error = ENOBUFS; | |
594 | goto qfull; | |
595 | } | |
596 | IF_ENQUEUE(&ifp->if_snd, m); | |
597 | if (es->es_oactive == 0) | |
598 | ecstart(ifp->if_unit); | |
599 | splx(s); | |
66923854 | 600 | return (mcopy ? looutput(&loif, mcopy, dst) : 0); |
0ca845ca | 601 | |
75334b2a BF |
602 | qfull: |
603 | m0 = m; | |
604 | splx(s); | |
605 | bad: | |
606 | m_freem(m0); | |
66923854 | 607 | return (error); |
75334b2a BF |
608 | } |
609 | ||
610 | /* | |
4fce3bf9 | 611 | * Routine to copy from mbuf chain to transmit |
0ca845ca | 612 | * buffer in UNIBUS memory. |
4fce3bf9 SL |
613 | * If packet size is less than the minimum legal size, |
614 | * the buffer is expanded. We probably should zero out the extra | |
615 | * bytes for security, but that would slow things down. | |
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 | 622 | register int off; |
48db90de | 623 | u_char *bp; |
75334b2a | 624 | |
0ca845ca SL |
625 | for (off = 2048, mp = m; mp; mp = mp->m_next) |
626 | off -= mp->m_len; | |
3fa8d9bb SL |
627 | if (2048 - off < ETHERMIN + sizeof (struct ether_header)) |
628 | off = 2048 - ETHERMIN - sizeof (struct ether_header); | |
0ca845ca SL |
629 | *(u_short *)ecbuf = off; |
630 | bp = (u_char *)(ecbuf + off); | |
48db90de SL |
631 | for (mp = m; mp; mp = mp->m_next) { |
632 | register unsigned len = mp->m_len; | |
633 | u_char *mcp; | |
0ca845ca | 634 | |
0ca845ca SL |
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 | } |
48db90de SL |
642 | if (off = (len >> 1)) { |
643 | register u_short *to, *from; | |
644 | ||
645 | to = (u_short *)bp; | |
646 | from = (u_short *)mcp; | |
647 | do | |
648 | *to++ = *from++; | |
649 | while (--off > 0); | |
650 | bp = (u_char *)to, | |
651 | mcp = (u_char *)from; | |
4a404244 | 652 | } |
48db90de | 653 | if (len & 01) |
75334b2a | 654 | *bp++ = *mcp++; |
75334b2a | 655 | } |
48db90de | 656 | m_freem(m); |
75334b2a BF |
657 | } |
658 | ||
659 | /* | |
660 | * Routine to copy from UNIBUS memory into mbufs. | |
661 | * Similar in spirit to if_rubaget. | |
4a404244 BJ |
662 | * |
663 | * Warning: This makes the fairly safe assumption that | |
664 | * mbufs have even lengths. | |
75334b2a BF |
665 | */ |
666 | struct mbuf * | |
667 | ecget(ecbuf, totlen, off0) | |
48db90de | 668 | u_char *ecbuf; |
75334b2a BF |
669 | int totlen, off0; |
670 | { | |
48db90de SL |
671 | register struct mbuf *m; |
672 | struct mbuf *top = 0, **mp = ⊤ | |
673 | register int off = off0, len; | |
674 | u_char *cp; | |
75334b2a | 675 | |
3fa8d9bb | 676 | cp = ecbuf + ECRDOFF + sizeof (struct ether_header); |
75334b2a | 677 | while (totlen > 0) { |
48db90de SL |
678 | register int words; |
679 | u_char *mcp; | |
680 | ||
cce93e4b | 681 | MGET(m, M_DONTWAIT, MT_DATA); |
75334b2a BF |
682 | if (m == 0) |
683 | goto bad; | |
684 | if (off) { | |
685 | len = totlen - off; | |
3fa8d9bb SL |
686 | cp = ecbuf + ECRDOFF + |
687 | sizeof (struct ether_header) + off; | |
75334b2a BF |
688 | } else |
689 | len = totlen; | |
690 | if (len >= CLBYTES) { | |
691 | struct mbuf *p; | |
692 | ||
693 | MCLGET(p, 1); | |
694 | if (p != 0) { | |
695 | m->m_len = len = CLBYTES; | |
696 | m->m_off = (int)p - (int)m; | |
697 | } else { | |
698 | m->m_len = len = MIN(MLEN, len); | |
699 | m->m_off = MMINOFF; | |
700 | } | |
701 | } else { | |
702 | m->m_len = len = MIN(MLEN, len); | |
703 | m->m_off = MMINOFF; | |
704 | } | |
48db90de SL |
705 | mcp = mtod(m, u_char *); |
706 | if (words = (len >> 1)) { | |
707 | register u_short *to, *from; | |
708 | ||
709 | to = (u_short *)mcp; | |
710 | from = (u_short *)cp; | |
711 | do | |
712 | *to++ = *from++; | |
713 | while (--words > 0); | |
714 | mcp = (u_char *)to; | |
715 | cp = (u_char *)from; | |
4a404244 | 716 | } |
0ca845ca | 717 | if (len & 01) |
75334b2a BF |
718 | *mcp++ = *cp++; |
719 | *mp = m; | |
720 | mp = &m->m_next; | |
48db90de | 721 | if (off == 0) { |
75334b2a | 722 | totlen -= len; |
48db90de SL |
723 | continue; |
724 | } | |
725 | off += len; | |
726 | if (off == totlen) { | |
3fa8d9bb | 727 | cp = ecbuf + ECRDOFF + sizeof (struct ether_header); |
48db90de SL |
728 | off = 0; |
729 | totlen = off0; | |
730 | } | |
75334b2a BF |
731 | } |
732 | return (top); | |
733 | bad: | |
734 | m_freem(top); | |
735 | return (0); | |
736 | } | |
1cd789fb SL |
737 | |
738 | /* | |
739 | * Process an ioctl request. | |
740 | */ | |
741 | ecioctl(ifp, cmd, data) | |
742 | register struct ifnet *ifp; | |
743 | int cmd; | |
744 | caddr_t data; | |
745 | { | |
746 | register struct ifreq *ifr = (struct ifreq *)data; | |
1cd789fb SL |
747 | int s = splimp(), error = 0; |
748 | ||
749 | switch (cmd) { | |
750 | ||
751 | case SIOCSIFADDR: | |
752 | if (ifp->if_flags & IFF_RUNNING) | |
753 | if_rtinit(ifp, -1); /* delete previous route */ | |
17718098 | 754 | ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr); |
1cd789fb SL |
755 | ecinit(ifp->if_unit); |
756 | break; | |
757 | ||
758 | default: | |
759 | error = EINVAL; | |
760 | } | |
761 | splx(s); | |
762 | return (error); | |
763 | } | |
17718098 SL |
764 | |
765 | ecsetaddr(ifp, sin) | |
766 | register struct ifnet *ifp; | |
767 | register struct sockaddr_in *sin; | |
768 | { | |
769 | ||
770 | ifp->if_addr = *(struct sockaddr *)sin; | |
771 | ifp->if_net = in_netof(sin->sin_addr); | |
772 | ifp->if_host[0] = in_lnaof(sin->sin_addr); | |
773 | sin = (struct sockaddr_in *)&ifp->if_broadaddr; | |
774 | sin->sin_family = AF_INET; | |
775 | sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY); | |
776 | ifp->if_flags |= IFF_BROADCAST; | |
777 | } |