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