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