add uba mem configuration entry, get mem addr from flags
[unix-history] / usr / src / sys / vax / if / if_ec.c
CommitLineData
b6220e6c 1/* if_ec.c 6.6 85/02/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 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"
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"
a6e960e7
JB
32#include "if_ecreg.h"
33#include "if_uba.h"
a9687d27
BJ
34#include "../vaxuba/ubareg.h"
35#include "../vaxuba/ubavar.h"
75334b2a 36
b6220e6c
MK
37#if CLSIZE == 2
38#define ECBUFSIZE 32 /* on-board memory, clusters */
3f506470 39#endif
3f506470 40
b6220e6c 41int ecubamem(), ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
75334b2a
BF
42struct uba_device *ecinfo[NEC];
43u_short ecstd[] = { 0 };
44struct uba_driver ecdriver =
b6220e6c 45 { ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo, 0, 0, 0, ecubamem };
75334b2a 46
1cd789fb 47int ecinit(),ecioctl(),ecoutput(),ecreset();
39d536e6 48struct mbuf *ecget();
75334b2a 49
be515f6e
BF
50extern struct ifnet loif;
51
75334b2a
BF
52/*
53 * Ethernet software status per interface.
54 *
55 * Each interface is referenced by a network interface structure,
56 * es_if, which the routing code uses to locate the interface.
57 * This structure contains the output queue for the interface, its address, ...
58 * We also have, for each interface, a UBA interface structure, which
59 * contains information about the UNIBUS resources held by the interface:
60 * map registers, buffered data paths, etc. Information is cached in this
61 * structure for use by the if_uba.c routines in running the interface
62 * efficiently.
63 */
64struct ec_softc {
8ae4cebd
SL
65 struct arpcom es_ac; /* common Ethernet structures */
66#define es_if es_ac.ac_if /* network-visible interface */
67#define es_addr es_ac.ac_enaddr /* hardware Ethernet address */
75334b2a 68 struct ifuba es_ifuba; /* UNIBUS resources */
75334b2a 69 short es_mask; /* mask for current output delay */
75334b2a 70 short es_oactive; /* is output active? */
39d536e6 71 u_char *es_buf[16]; /* virtual addresses of buffers */
75334b2a
BF
72} ec_softc[NEC];
73
74/*
b6220e6c
MK
75 * Configure on-board memory for an interface.
76 * Called from autoconfig and after a uba reset.
77 * The address of the memory on the uba is supplied in the device flags.
75334b2a 78 */
b6220e6c
MK
79ecubamem(ui, uban)
80 register struct uba_device *ui;
75334b2a 81{
b6220e6c
MK
82 register caddr_t ecbuf = (caddr_t) &umem[uban][ui->ui_flags];
83 register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
75334b2a 84
b6220e6c
MK
85 /*
86 * Make sure csr is there (we run before ecprobe).
87 */
88 if (badaddr((caddr_t)addr, 2))
89 return (-1);
90#if VAX780
91 if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
92 uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
93 return (-1);
94 }
75334b2a 95#endif
f5a616ae
BF
96 /*
97 * Make sure memory is turned on
98 */
99 addr->ec_rcr = EC_AROM;
a26646de 100 /*
b6220e6c
MK
101 * Tell the system that the board has memory here, so it won't
102 * attempt to allocate the addresses later.
a26646de 103 */
b6220e6c
MK
104 if (ubamem(uban, ui->ui_flags, ECBUFSIZE*CLSIZE, 1) == 0) {
105 printf("ec%d: cannot reserve uba addresses\n", ui->ui_unit);
106 addr->ec_rcr = EC_MDISAB; /* disable memory */
107 return (-1);
108 }
75334b2a
BF
109 /*
110 * Check for existence of buffers on Unibus.
75334b2a 111 */
39d536e6 112 if (badaddr((caddr_t)ecbuf, 2)) {
b6220e6c
MK
113bad:
114 printf("ec%d: buffer mem not found\n", ui->ui_unit);
115 (void) ubamem(uban, ui->ui_flags, ECBUFSIZE*2, 0);
a26646de 116 addr->ec_rcr = EC_MDISAB; /* disable memory */
b6220e6c 117 return (-1);
75334b2a 118 }
a26646de 119#if VAX780
b6220e6c
MK
120 if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
121 uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
122 goto bad;
a26646de
BF
123 }
124#endif
b6220e6c
MK
125 if (ui->ui_alive == 0) /* Only printf from autoconfig */
126 printf("ec%d: mem %x-%x\n", ui->ui_unit,
127 ui->ui_flags, ui->ui_flags + ECBUFSIZE*CLBYTES - 1);
128 ui->ui_type = 1; /* Memory on, allocated */
129 return (0);
130}
131
132/*
133 * Do output DMA to determine interface presence and
134 * interrupt vector. DMA is too short to disturb other hosts.
135 */
136ecprobe(reg, ui)
137 caddr_t reg;
138 struct uba_device *ui;
139{
140 register int br, cvec; /* r11, r10 value-result */
141 register struct ecdevice *addr = (struct ecdevice *)reg;
142 register caddr_t ecbuf = (caddr_t) &umem[ui->ui_ubanum][ui->ui_flags];
143
144#ifdef lint
145 br = 0; cvec = br; br = cvec;
146 ecrint(0); ecxint(0); eccollide(0);
147#endif
75334b2a
BF
148
149 /*
b6220e6c 150 * Check that buffer memory was found and enabled.
75334b2a 151 */
b6220e6c
MK
152 if (ui->ui_type == 0)
153 return(0);
75334b2a
BF
154 /*
155 * Make a one byte packet in what should be buffer #0.
b6220e6c 156 * Submit it for sending. This should cause an xmit interrupt.
75334b2a
BF
157 * The xmit interrupt vector is 8 bytes after the receive vector,
158 * so adjust for this before returning.
159 */
160 *(u_short *)ecbuf = (u_short) 03777;
161 ecbuf[03777] = '\0';
162 addr->ec_xcr = EC_XINTEN|EC_XWBN;
163 DELAY(100000);
164 addr->ec_xcr = EC_XCLR;
0ca845ca 165 if (cvec > 0 && cvec != 0x200) {
a26646de
BF
166 if (cvec & 04) { /* collision interrupt */
167 cvec -= 04;
168 br += 1; /* rcv is collision + 1 */
169 } else { /* xmit interrupt */
170 cvec -= 010;
171 br += 2; /* rcv is xmit + 2 */
172 }
0ca845ca 173 }
75334b2a
BF
174 return (1);
175}
176
177/*
178 * Interface exists: make available by filling in network interface
179 * record. System will initialize the interface when it is ready
180 * to accept packets.
181 */
182ecattach(ui)
183 struct uba_device *ui;
184{
0ca845ca
SL
185 struct ec_softc *es = &ec_softc[ui->ui_unit];
186 register struct ifnet *ifp = &es->es_if;
75334b2a 187 register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
0ca845ca
SL
188 struct sockaddr_in *sin;
189 int i, j;
190 u_char *cp;
75334b2a 191
0ca845ca
SL
192 ifp->if_unit = ui->ui_unit;
193 ifp->if_name = "ec";
3fa8d9bb 194 ifp->if_mtu = ETHERMTU;
75334b2a
BF
195
196 /*
0ca845ca 197 * Read the ethernet address off the board, one nibble at a time.
75334b2a
BF
198 */
199 addr->ec_xcr = EC_UECLR;
200 addr->ec_rcr = EC_AROM;
0217ce84 201 cp = (u_char *) &es->es_addr;
0ca845ca 202#define NEXTBIT addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
0217ce84 203 for (i=0; i < sizeof (es->es_addr); i++) {
75334b2a
BF
204 *cp = 0;
205 for (j=0; j<=4; j+=4) {
206 *cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
0ca845ca 207 NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
75334b2a
BF
208 }
209 cp++;
210 }
75334b2a
BF
211 sin = (struct sockaddr_in *)&es->es_if.if_addr;
212 sin->sin_family = AF_INET;
0ca845ca 213 ifp->if_init = ecinit;
1cd789fb 214 ifp->if_ioctl = ecioctl;
0ca845ca 215 ifp->if_output = ecoutput;
51595ca2 216 ifp->if_reset = ecreset;
75334b2a 217 for (i=0; i<16; i++)
3f506470 218 es->es_buf[i]
b6220e6c 219 = (u_char *)&umem[ui->ui_ubanum][ui->ui_flags + 2048*i];
0ca845ca 220 if_attach(ifp);
75334b2a
BF
221}
222
223/*
224 * Reset of interface after UNIBUS reset.
225 * If interface is on specified uba, reset its state.
226 */
227ecreset(unit, uban)
228 int unit, uban;
229{
230 register struct uba_device *ui;
75334b2a
BF
231
232 if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
233 ui->ui_ubanum != uban)
234 return;
235 printf(" ec%d", unit);
ce3f80e9 236 ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
75334b2a
BF
237 ecinit(unit);
238}
239
240/*
241 * Initialization of interface; clear recorded pending
242 * operations, and reinitialize UNIBUS usage.
243 */
244ecinit(unit)
245 int unit;
246{
0ca845ca
SL
247 struct ec_softc *es = &ec_softc[unit];
248 struct ecdevice *addr;
8ae4cebd 249 register struct ifnet *ifp = &es->es_if;
44eb2da3 250 register struct sockaddr_in *sin;
1cd789fb 251 int i, s;
8ae4cebd
SL
252
253 sin = (struct sockaddr_in *)&ifp->if_addr;
1cd789fb 254 if (sin->sin_addr.s_addr == 0) /* address still unknown */
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 */
1cd789fb
SL
262 if ((es->es_if.if_flags & IFF_RUNNING) == 0) {
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;
269 es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
270 if (es->es_if.if_snd.ifq_head)
271 ecstart(unit);
272 splx(s);
273 }
a13c006d 274 if_rtinit(&es->es_if, RTF_UP);
8ae4cebd 275 arpwhohas(&es->es_ac, &sin->sin_addr);
75334b2a
BF
276}
277
75334b2a
BF
278/*
279 * Start or restart output on interface.
280 * If interface is already active, then this is a retransmit
be515f6e 281 * after a collision, and just restuff registers.
75334b2a
BF
282 * If interface is not already active, get another datagram
283 * to send off of the interface queue, and map it to the interface
284 * before starting the output.
285 */
b6220e6c 286ecstart(unit)
75334b2a 287{
0ca845ca
SL
288 struct ec_softc *es = &ec_softc[unit];
289 struct ecdevice *addr;
75334b2a 290 struct mbuf *m;
75334b2a
BF
291
292 if (es->es_oactive)
293 goto restart;
294
75334b2a
BF
295 IF_DEQUEUE(&es->es_if.if_snd, m);
296 if (m == 0) {
297 es->es_oactive = 0;
298 return;
299 }
75334b2a
BF
300 ecput(es->es_buf[ECTBF], m);
301
75334b2a 302restart:
0ca845ca 303 addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
75334b2a
BF
304 addr->ec_xcr = EC_WRITE|ECTBF;
305 es->es_oactive = 1;
306}
307
308/*
309 * Ethernet interface transmitter interrupt.
310 * Start another output if more data to send.
311 */
312ecxint(unit)
313 int unit;
314{
75334b2a 315 register struct ec_softc *es = &ec_softc[unit];
0ca845ca
SL
316 register struct ecdevice *addr =
317 (struct ecdevice *)ecinfo[unit]->ui_addr;
75334b2a
BF
318
319 if (es->es_oactive == 0)
320 return;
0ca845ca
SL
321 if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
322 printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
323 addr->ec_xcr, EC_XBITS);
324 es->es_oactive = 0;
325 addr->ec_xcr = EC_XCLR;
326 return;
327 }
75334b2a
BF
328 es->es_if.if_opackets++;
329 es->es_oactive = 0;
75334b2a
BF
330 es->es_mask = ~0;
331 addr->ec_xcr = EC_XCLR;
0ca845ca
SL
332 if (es->es_if.if_snd.ifq_head)
333 ecstart(unit);
75334b2a
BF
334}
335
336/*
337 * Collision on ethernet interface. Do exponential
338 * backoff, and retransmit. If have backed off all
339 * the way print warning diagnostic, and drop packet.
340 */
341eccollide(unit)
342 int unit;
343{
344 struct ec_softc *es = &ec_softc[unit];
75334b2a 345
75334b2a 346 es->es_if.if_collisions++;
0ca845ca
SL
347 if (es->es_oactive)
348 ecdocoll(unit);
75334b2a
BF
349}
350
351ecdocoll(unit)
352 int unit;
353{
354 register struct ec_softc *es = &ec_softc[unit];
be515f6e
BF
355 register struct ecdevice *addr =
356 (struct ecdevice *)ecinfo[unit]->ui_addr;
357 register i;
358 int delay;
75334b2a
BF
359
360 /*
361 * Es_mask is a 16 bit number with n low zero bits, with
362 * n the number of backoffs. When es_mask is 0 we have
363 * backed off 16 times, and give up.
364 */
365 if (es->es_mask == 0) {
be515f6e 366 es->es_if.if_oerrors++;
75334b2a
BF
367 printf("ec%d: send error\n", unit);
368 /*
be515f6e
BF
369 * Reset interface, then requeue rcv buffers.
370 * Some incoming packets may be lost, but that
371 * can't be helped.
372 */
373 addr->ec_xcr = EC_UECLR;
374 for (i=ECRHBF; i>=ECRLBF; i--)
375 addr->ec_rcr = EC_READ|i;
376 /*
377 * Reset and transmit next packet (if any).
75334b2a 378 */
be515f6e
BF
379 es->es_oactive = 0;
380 es->es_mask = ~0;
381 if (es->es_if.if_snd.ifq_head)
382 ecstart(unit);
75334b2a
BF
383 return;
384 }
385 /*
be515f6e
BF
386 * Do exponential backoff. Compute delay based on low bits
387 * of the interval timer. Then delay for that number of
388 * slot times. A slot time is 51.2 microseconds (rounded to 51).
389 * This does not take into account the time already used to
390 * process the interrupt.
75334b2a
BF
391 */
392 es->es_mask <<= 1;
be515f6e
BF
393 delay = mfpr(ICR) &~ es->es_mask;
394 DELAY(delay * 51);
75334b2a 395 /*
be515f6e 396 * Clear the controller's collision flag, thus enabling retransmit.
75334b2a 397 */
a26646de 398 addr->ec_xcr = EC_CLEAR;
75334b2a
BF
399}
400
75334b2a
BF
401/*
402 * Ethernet interface receiver interrupt.
403 * If input error just drop packet.
404 * Otherwise purge input buffered data path and examine
405 * packet to determine type. If can't determine length
406 * from type, then have to drop packet. Othewise decapsulate
407 * packet based on type and pass to type specific higher-level
408 * input routine.
409 */
410ecrint(unit)
411 int unit;
412{
413 struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
75334b2a 414
75334b2a
BF
415 while (addr->ec_rcr & EC_RDONE)
416 ecread(unit);
417}
418
419ecread(unit)
420 int unit;
421{
422 register struct ec_softc *es = &ec_softc[unit];
423 struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3fa8d9bb 424 register struct ether_header *ec;
75334b2a 425 struct mbuf *m;
39d536e6 426 int len, off, resid, ecoff, rbuf;
75334b2a 427 register struct ifqueue *inq;
39d536e6 428 u_char *ecbuf;
75334b2a
BF
429
430 es->es_if.if_ipackets++;
39d536e6
BJ
431 rbuf = addr->ec_rcr & EC_RBN;
432 if (rbuf < ECRLBF || rbuf > ECRHBF)
75334b2a 433 panic("ecrint");
39d536e6 434 ecbuf = es->es_buf[rbuf];
75334b2a 435 ecoff = *(short *)ecbuf;
be515f6e 436 if (ecoff <= ECRDOFF || ecoff > 2046) {
75334b2a
BF
437 es->es_if.if_ierrors++;
438#ifdef notdef
439 if (es->es_if.if_ierrors % 100 == 0)
440 printf("ec%d: += 100 input errors\n", unit);
441#endif
75334b2a
BF
442 goto setup;
443 }
444
445 /*
446 * Get input data length.
447 * Get pointer to ethernet header (in input buffer).
448 * Deal with trailer protocol: if type is PUP trailer
449 * get true type from first 16-bit word past data.
450 * Remember that type was trailer by setting off.
451 */
3fa8d9bb
SL
452 len = ecoff - ECRDOFF - sizeof (struct ether_header);
453 ec = (struct ether_header *)(ecbuf + ECRDOFF);
454 ec->ether_type = ntohs((u_short)ec->ether_type);
75334b2a 455#define ecdataaddr(ec, off, type) ((type)(((caddr_t)((ec)+1)+(off))))
3fa8d9bb
SL
456 if (ec->ether_type >= ETHERPUP_TRAIL &&
457 ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
458 off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
459 if (off >= ETHERMTU)
75334b2a 460 goto setup; /* sanity */
3fa8d9bb
SL
461 ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
462 resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
75334b2a
BF
463 if (off + resid > len)
464 goto setup; /* sanity */
465 len = off + resid;
466 } else
467 off = 0;
468 if (len == 0)
469 goto setup;
470
471 /*
472 * Pull packet off interface. Off is nonzero if packet
473 * has trailing header; ecget will then force this header
474 * information to be at the front, but we still have to drop
475 * the type and length which are at the front of any trailer data.
476 */
477 m = ecget(ecbuf, len, off);
478 if (m == 0)
479 goto setup;
480 if (off) {
481 m->m_off += 2 * sizeof (u_short);
482 m->m_len -= 2 * sizeof (u_short);
483 }
3fa8d9bb 484 switch (ec->ether_type) {
75334b2a
BF
485
486#ifdef INET
3fa8d9bb 487 case ETHERPUP_IPTYPE:
75334b2a
BF
488 schednetisr(NETISR_IP);
489 inq = &ipintrq;
490 break;
8ae4cebd
SL
491
492 case ETHERPUP_ARPTYPE:
493 arpinput(&es->es_ac, m);
27bcf34c 494 goto setup;
75334b2a
BF
495#endif
496 default:
497 m_freem(m);
498 goto setup;
499 }
500
501 if (IF_QFULL(inq)) {
502 IF_DROP(inq);
503 m_freem(m);
0ca845ca
SL
504 goto setup;
505 }
506 IF_ENQUEUE(inq, m);
75334b2a
BF
507
508setup:
509 /*
510 * Reset for next packet.
511 */
39d536e6 512 addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
75334b2a
BF
513}
514
515/*
516 * Ethernet output routine.
517 * Encapsulate a packet of type family for the local net.
518 * Use trailer local net encapsulation if enough data in first
519 * packet leaves a multiple of 512 bytes of data in remainder.
be515f6e
BF
520 * If destination is this address or broadcast, send packet to
521 * loop device to kludge around the fact that 3com interfaces can't
522 * talk to themselves.
75334b2a
BF
523 */
524ecoutput(ifp, m0, dst)
525 struct ifnet *ifp;
526 struct mbuf *m0;
527 struct sockaddr *dst;
528{
8ae4cebd 529 int type, s, error;
0217ce84 530 struct ether_addr edst;
8ae4cebd 531 struct in_addr idst;
75334b2a
BF
532 register struct ec_softc *es = &ec_softc[ifp->if_unit];
533 register struct mbuf *m = m0;
3fa8d9bb 534 register struct ether_header *ec;
66923854 535 register int off;
8ae4cebd 536 struct mbuf *mcopy = (struct mbuf *)0;
75334b2a 537
75334b2a
BF
538 switch (dst->sa_family) {
539
540#ifdef INET
541 case AF_INET:
8ae4cebd 542 idst = ((struct sockaddr_in *)dst)->sin_addr;
0217ce84 543 if (!arpresolve(&es->es_ac, m, &idst, &edst))
8ae4cebd
SL
544 return (0); /* if not yet resolved */
545 if (in_lnaof(idst) == INADDR_ANY)
92aca3da 546 mcopy = m_copy(m, 0, (int)M_COPYALL);
75334b2a 547 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
1cd789fb
SL
548 /* need per host negotiation */
549 if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
75334b2a
BF
550 if (off > 0 && (off & 0x1ff) == 0 &&
551 m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
3fa8d9bb 552 type = ETHERPUP_TRAIL + (off>>9);
75334b2a
BF
553 m->m_off -= 2 * sizeof (u_short);
554 m->m_len += 2 * sizeof (u_short);
3fa8d9bb
SL
555 *mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
556 *(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
75334b2a
BF
557 goto gottrailertype;
558 }
3fa8d9bb 559 type = ETHERPUP_IPTYPE;
75334b2a
BF
560 off = 0;
561 goto gottype;
75334b2a
BF
562#endif
563
8ae4cebd
SL
564 case AF_UNSPEC:
565 ec = (struct ether_header *)dst->sa_data;
0217ce84 566 edst = ec->ether_dhost;
8ae4cebd
SL
567 type = ec->ether_type;
568 goto gottype;
569
75334b2a
BF
570 default:
571 printf("ec%d: can't handle af%d\n", ifp->if_unit,
572 dst->sa_family);
573 error = EAFNOSUPPORT;
574 goto bad;
575 }
576
577gottrailertype:
578 /*
579 * Packet to be sent as trailer: move first packet
580 * (control information) to end of chain.
581 */
582 while (m->m_next)
583 m = m->m_next;
584 m->m_next = m0;
585 m = m0->m_next;
586 m0->m_next = 0;
587 m0 = m;
588
589gottype:
590 /*
591 * Add local net header. If no space in first mbuf,
592 * allocate another.
593 */
594 if (m->m_off > MMAXOFF ||
3fa8d9bb 595 MMINOFF + sizeof (struct ether_header) > m->m_off) {
cce93e4b 596 m = m_get(M_DONTWAIT, MT_HEADER);
75334b2a
BF
597 if (m == 0) {
598 error = ENOBUFS;
599 goto bad;
600 }
601 m->m_next = m0;
602 m->m_off = MMINOFF;
3fa8d9bb 603 m->m_len = sizeof (struct ether_header);
75334b2a 604 } else {
3fa8d9bb
SL
605 m->m_off -= sizeof (struct ether_header);
606 m->m_len += sizeof (struct ether_header);
75334b2a 607 }
3fa8d9bb 608 ec = mtod(m, struct ether_header *);
0217ce84
MK
609 ec->ether_dhost = edst;
610 ec->ether_shost = es->es_addr;
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{
775 register struct ifreq *ifr = (struct ifreq *)data;
1cd789fb
SL
776 int s = splimp(), error = 0;
777
778 switch (cmd) {
779
780 case SIOCSIFADDR:
781 if (ifp->if_flags & IFF_RUNNING)
782 if_rtinit(ifp, -1); /* delete previous route */
17718098 783 ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
1cd789fb
SL
784 ecinit(ifp->if_unit);
785 break;
786
787 default:
788 error = EINVAL;
789 }
790 splx(s);
791 return (error);
792}
17718098
SL
793
794ecsetaddr(ifp, sin)
795 register struct ifnet *ifp;
796 register struct sockaddr_in *sin;
797{
798
799 ifp->if_addr = *(struct sockaddr *)sin;
800 ifp->if_net = in_netof(sin->sin_addr);
801 ifp->if_host[0] = in_lnaof(sin->sin_addr);
802 sin = (struct sockaddr_in *)&ifp->if_broadaddr;
803 sin->sin_family = AF_INET;
804 sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
805 ifp->if_flags |= IFF_BROADCAST;
806}