if_dmc.c revision 1.3 1 /* $NetBSD: if_dmc.c,v 1.3 2002/03/05 04:12:59 itojun Exp $ */
2 /*
3 * Copyright (c) 1982, 1986 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)if_dmc.c 7.10 (Berkeley) 12/16/90
35 */
36
37 /*
38 * DMC11 device driver, internet version
39 *
40 * Bill Nesheim
41 * Cornell University
42 *
43 * Lou Salkind
44 * New York University
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: if_dmc.c,v 1.3 2002/03/05 04:12:59 itojun Exp $");
49
50 #undef DMCDEBUG /* for base table dump on fatal error */
51
52 #include "opt_inet.h"
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/mbuf.h>
57 #include <sys/ioctl.h>
58 #include <sys/socket.h>
59 #include <sys/syslog.h>
60 #include <sys/device.h>
61
62 #include <net/if.h>
63 #include <net/netisr.h>
64
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #endif
69
70 #include <machine/bus.h>
71
72 #include <dev/qbus/ubareg.h>
73 #include <dev/qbus/ubavar.h>
74 #include <dev/qbus/if_uba.h>
75
76 #include <dev/qbus/if_dmcreg.h>
77
78
79 /*
80 * output timeout value, sec.; should depend on line speed.
81 */
82 static int dmc_timeout = 20;
83
84 #define NRCV 7
85 #define NXMT 3
86 #define NCMDS (NRCV+NXMT+4) /* size of command queue */
87
88 #define DMC_WBYTE(csr, val) \
89 bus_space_write_1(sc->sc_iot, sc->sc_ioh, csr, val)
90 #define DMC_WWORD(csr, val) \
91 bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
92 #define DMC_RBYTE(csr) \
93 bus_space_read_1(sc->sc_iot, sc->sc_ioh, csr)
94 #define DMC_RWORD(csr) \
95 bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
96
97
98 #ifdef DMCDEBUG
99 #define printd if(dmcdebug)printf
100 int dmcdebug = 0;
101 #endif
102
103 /* error reporting intervals */
104 #define DMC_RPNBFS 50
105 #define DMC_RPDSC 1
106 #define DMC_RPTMO 10
107 #define DMC_RPDCK 10
108
109 struct dmc_command {
110 char qp_cmd; /* command */
111 short qp_ubaddr; /* buffer address */
112 short qp_cc; /* character count || XMEM */
113 struct dmc_command *qp_next; /* next command on queue */
114 };
115
116 struct dmcbufs {
117 int ubinfo; /* from uballoc */
118 short cc; /* buffer size */
119 short flags; /* access control */
120 };
121 #define DBUF_OURS 0 /* buffer is available */
122 #define DBUF_DMCS 1 /* buffer claimed by somebody */
123 #define DBUF_XMIT 4 /* transmit buffer */
124 #define DBUF_RCV 8 /* receive buffer */
125
126
127 /*
128 * DMC software status per interface.
129 *
130 * Each interface is referenced by a network interface structure,
131 * sc_if, which the routing code uses to locate the interface.
132 * This structure contains the output queue for the interface, its address, ...
133 * We also have, for each interface, a set of 7 UBA interface structures
134 * for each, which
135 * contain information about the UNIBUS resources held by the interface:
136 * map registers, buffered data paths, etc. Information is cached in this
137 * structure for use by the if_uba.c routines in running the interface
138 * efficiently.
139 */
140 struct dmc_softc {
141 struct device sc_dev; /* Configuration common part */
142 struct ifnet sc_if; /* network-visible interface */
143 short sc_oused; /* output buffers currently in use */
144 short sc_iused; /* input buffers given to DMC */
145 short sc_flag; /* flags */
146 struct ubinfo sc_ui; /* UBA mapping info for base table */
147 int sc_errors[4]; /* non-fatal error counters */
148 bus_space_tag_t sc_iot;
149 bus_addr_t sc_ioh;
150 bus_dma_tag_t sc_dmat;
151 struct evcnt sc_rintrcnt; /* Interrupt counting */
152 struct evcnt sc_tintrcnt; /* Interrupt counting */
153 #define sc_datck sc_errors[0]
154 #define sc_timeo sc_errors[1]
155 #define sc_nobuf sc_errors[2]
156 #define sc_disc sc_errors[3]
157 struct dmcbufs sc_rbufs[NRCV]; /* receive buffer info */
158 struct dmcbufs sc_xbufs[NXMT]; /* transmit buffer info */
159 struct ifubinfo sc_ifuba; /* UNIBUS resources */
160 struct ifrw sc_ifr[NRCV]; /* UNIBUS receive buffer maps */
161 struct ifxmt sc_ifw[NXMT]; /* UNIBUS receive buffer maps */
162 /* command queue stuff */
163 struct dmc_command sc_cmdbuf[NCMDS];
164 struct dmc_command *sc_qhead; /* head of command queue */
165 struct dmc_command *sc_qtail; /* tail of command queue */
166 struct dmc_command *sc_qactive; /* command in progress */
167 struct dmc_command *sc_qfreeh; /* head of list of free cmd buffers */
168 struct dmc_command *sc_qfreet; /* tail of list of free cmd buffers */
169 /* end command queue stuff */
170 struct dmc_base {
171 short d_base[128]; /* DMC base table */
172 } dmc_base;
173 };
174
175 static int dmcmatch(struct device *, struct cfdata *, void *);
176 static void dmcattach(struct device *, struct device *, void *);
177 static int dmcinit(struct ifnet *);
178 static void dmcrint(void *);
179 static void dmcxint(void *);
180 static void dmcdown(struct dmc_softc *sc);
181 static void dmcrestart(struct dmc_softc *);
182 static void dmcload(struct dmc_softc *, int, u_short, u_short);
183 static void dmcstart(struct ifnet *);
184 static void dmctimeout(struct ifnet *);
185 static int dmcioctl(struct ifnet *, u_long, caddr_t);
186 static int dmcoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
187 struct rtentry *);
188 static void dmcreset(struct device *);
189
190 struct cfattach dmc_ca = {
191 sizeof(struct dmc_softc), dmcmatch, dmcattach
192 };
193
194 /* flags */
195 #define DMC_RUNNING 0x01 /* device initialized */
196 #define DMC_BMAPPED 0x02 /* base table mapped */
197 #define DMC_RESTART 0x04 /* software restart in progress */
198 #define DMC_ONLINE 0x08 /* device running (had a RDYO) */
199
200
201 /* queue manipulation macros */
202 #define QUEUE_AT_HEAD(qp, head, tail) \
203 (qp)->qp_next = (head); \
204 (head) = (qp); \
205 if ((tail) == (struct dmc_command *) 0) \
206 (tail) = (head)
207
208 #define QUEUE_AT_TAIL(qp, head, tail) \
209 if ((tail)) \
210 (tail)->qp_next = (qp); \
211 else \
212 (head) = (qp); \
213 (qp)->qp_next = (struct dmc_command *) 0; \
214 (tail) = (qp)
215
216 #define DEQUEUE(head, tail) \
217 (head) = (head)->qp_next;\
218 if ((head) == (struct dmc_command *) 0)\
219 (tail) = (head)
220
221 int
222 dmcmatch(struct device *parent, struct cfdata *cf, void *aux)
223 {
224 struct uba_attach_args *ua = aux;
225 struct dmc_softc ssc;
226 struct dmc_softc *sc = &ssc;
227 int i;
228
229 sc->sc_iot = ua->ua_iot;
230 sc->sc_ioh = ua->ua_ioh;
231
232 DMC_WBYTE(DMC_BSEL1, DMC_MCLR);
233 for (i = 100000; i && (DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0; i--)
234 ;
235 if ((DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0) {
236 printf("dmcprobe: can't start device\n" );
237 return (0);
238 }
239 DMC_WBYTE(DMC_BSEL0, DMC_RQI|DMC_IEI);
240 /* let's be paranoid */
241 DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) | DMC_RQI|DMC_IEI);
242 DELAY(1000000);
243 DMC_WBYTE(DMC_BSEL1, DMC_MCLR);
244 for (i = 100000; i && (DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0; i--)
245 ;
246 return (1);
247 }
248
249 /*
250 * Interface exists: make available by filling in network interface
251 * record. System will initialize the interface when it is ready
252 * to accept packets.
253 */
254 void
255 dmcattach(struct device *parent, struct device *self, void *aux)
256 {
257 struct uba_attach_args *ua = aux;
258 struct dmc_softc *sc = (struct dmc_softc *)self;
259
260 sc->sc_iot = ua->ua_iot;
261 sc->sc_ioh = ua->ua_ioh;
262 sc->sc_dmat = ua->ua_dmat;
263
264 strcpy(sc->sc_if.if_xname, sc->sc_dev.dv_xname);
265 sc->sc_if.if_mtu = DMCMTU;
266 sc->sc_if.if_init = dmcinit;
267 sc->sc_if.if_output = dmcoutput;
268 sc->sc_if.if_ioctl = dmcioctl;
269 sc->sc_if.if_watchdog = dmctimeout;
270 sc->sc_if.if_flags = IFF_POINTOPOINT;
271 sc->sc_if.if_softc = sc;
272 IFQ_SET_READY(&sc->sc_if.if_snd);
273
274 uba_intr_establish(ua->ua_icookie, ua->ua_cvec, dmcrint, sc,
275 &sc->sc_rintrcnt);
276 uba_intr_establish(ua->ua_icookie, ua->ua_cvec+4, dmcxint, sc,
277 &sc->sc_tintrcnt);
278 uba_reset_establish(dmcreset, &sc->sc_dev);
279 evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
280 sc->sc_dev.dv_xname, "intr");
281 evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
282 sc->sc_dev.dv_xname, "intr");
283
284 if_attach(&sc->sc_if);
285 }
286
287 /*
288 * Reset of interface after UNIBUS reset.
289 * If interface is on specified UBA, reset its state.
290 */
291 void
292 dmcreset(struct device *dev)
293 {
294 struct dmc_softc *sc = (struct dmc_softc *)dev;
295
296 sc->sc_flag = 0;
297 sc->sc_if.if_flags &= ~IFF_RUNNING;
298 dmcinit(&sc->sc_if);
299 }
300
301 /*
302 * Initialization of interface; reinitialize UNIBUS usage.
303 */
304 int
305 dmcinit(struct ifnet *ifp)
306 {
307 struct dmc_softc *sc = ifp->if_softc;
308 struct ifrw *ifrw;
309 struct ifxmt *ifxp;
310 struct dmcbufs *rp;
311 struct dmc_command *qp;
312 struct ifaddr *ifa;
313 struct cfdata *ui = sc->sc_dev.dv_cfdata;
314 int base;
315 int s;
316
317 /*
318 * Check to see that an address has been set
319 * (both local and destination for an address family).
320 */
321 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
322 if (ifa->ifa_addr->sa_family && ifa->ifa_dstaddr->sa_family)
323 break;
324 if (ifa == (struct ifaddr *) 0)
325 return 0;
326
327 if ((DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0) {
328 printf("dmcinit: DMC not running\n");
329 ifp->if_flags &= ~IFF_UP;
330 return 0;
331 }
332 /* map base table */
333 if ((sc->sc_flag & DMC_BMAPPED) == 0) {
334 sc->sc_ui.ui_size = sizeof(struct dmc_base);
335 sc->sc_ui.ui_vaddr = (caddr_t)&sc->dmc_base;
336 uballoc((void *)sc->sc_dev.dv_parent, &sc->sc_ui, 0);
337 sc->sc_flag |= DMC_BMAPPED;
338 }
339 /* initialize UNIBUS resources */
340 sc->sc_iused = sc->sc_oused = 0;
341 if ((ifp->if_flags & IFF_RUNNING) == 0) {
342 if (if_ubaminit(&sc->sc_ifuba, (void *)sc->sc_dev.dv_parent,
343 sizeof(struct dmc_header) + DMCMTU,
344 sc->sc_ifr, NRCV, sc->sc_ifw, NXMT) == 0) {
345 printf("%s: can't allocate uba resources\n",
346 sc->sc_dev.dv_xname);
347 ifp->if_flags &= ~IFF_UP;
348 return 0;
349 }
350 ifp->if_flags |= IFF_RUNNING;
351 }
352 sc->sc_flag &= ~DMC_ONLINE;
353 sc->sc_flag |= DMC_RUNNING;
354 /*
355 * Limit packets enqueued until we see if we're on the air.
356 */
357 ifp->if_snd.ifq_maxlen = 3;
358
359 /* initialize buffer pool */
360 /* receives */
361 ifrw = &sc->sc_ifr[0];
362 for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
363 rp->ubinfo = ifrw->ifrw_info;
364 rp->cc = DMCMTU + sizeof (struct dmc_header);
365 rp->flags = DBUF_OURS|DBUF_RCV;
366 ifrw++;
367 }
368 /* transmits */
369 ifxp = &sc->sc_ifw[0];
370 for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++) {
371 rp->ubinfo = ifxp->ifw_info;
372 rp->cc = 0;
373 rp->flags = DBUF_OURS|DBUF_XMIT;
374 ifxp++;
375 }
376
377 /* set up command queues */
378 sc->sc_qfreeh = sc->sc_qfreet
379 = sc->sc_qhead = sc->sc_qtail = sc->sc_qactive =
380 (struct dmc_command *)0;
381 /* set up free command buffer list */
382 for (qp = &sc->sc_cmdbuf[0]; qp < &sc->sc_cmdbuf[NCMDS]; qp++) {
383 QUEUE_AT_HEAD(qp, sc->sc_qfreeh, sc->sc_qfreet);
384 }
385
386 /* base in */
387 base = sc->sc_ui.ui_baddr;
388 dmcload(sc, DMC_BASEI, (u_short)base, (base>>2) & DMC_XMEM);
389 /* specify half duplex operation, flags tell if primary */
390 /* or secondary station */
391 if (ui->cf_flags == 0)
392 /* use DDCMP mode in full duplex */
393 dmcload(sc, DMC_CNTLI, 0, 0);
394 else if (ui->cf_flags == 1)
395 /* use MAINTENENCE mode */
396 dmcload(sc, DMC_CNTLI, 0, DMC_MAINT );
397 else if (ui->cf_flags == 2)
398 /* use DDCMP half duplex as primary station */
399 dmcload(sc, DMC_CNTLI, 0, DMC_HDPLX);
400 else if (ui->cf_flags == 3)
401 /* use DDCMP half duplex as secondary station */
402 dmcload(sc, DMC_CNTLI, 0, DMC_HDPLX | DMC_SEC);
403
404 /* enable operation done interrupts */
405 while ((DMC_RBYTE(DMC_BSEL2) & DMC_IEO) == 0)
406 DMC_WBYTE(DMC_BSEL2, DMC_RBYTE(DMC_BSEL2) | DMC_IEO);
407 s = splnet();
408 /* queue first NRCV buffers for DMC to fill */
409 for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
410 rp->flags |= DBUF_DMCS;
411 dmcload(sc, DMC_READ, rp->ubinfo,
412 (((rp->ubinfo>>2)&DMC_XMEM) | rp->cc));
413 sc->sc_iused++;
414 }
415 splx(s);
416 return 0;
417 }
418
419 /*
420 * Start output on interface. Get another datagram
421 * to send from the interface queue and map it to
422 * the interface before starting output.
423 *
424 * Must be called at spl 5
425 */
426 void
427 dmcstart(struct ifnet *ifp)
428 {
429 struct dmc_softc *sc = ifp->if_softc;
430 struct mbuf *m;
431 struct dmcbufs *rp;
432 int n;
433
434 /*
435 * Dequeue up to NXMT requests and map them to the UNIBUS.
436 * If no more requests, or no dmc buffers available, just return.
437 */
438 n = 0;
439 for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++ ) {
440 /* find an available buffer */
441 if ((rp->flags & DBUF_DMCS) == 0) {
442 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
443 if (m == 0)
444 return;
445 /* mark it dmcs */
446 rp->flags |= (DBUF_DMCS);
447 /*
448 * Have request mapped to UNIBUS for transmission
449 * and start the output.
450 */
451 rp->cc = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[n], m);
452 rp->cc &= DMC_CCOUNT;
453 if (++sc->sc_oused == 1)
454 sc->sc_if.if_timer = dmc_timeout;
455 dmcload(sc, DMC_WRITE, rp->ubinfo,
456 rp->cc | ((rp->ubinfo>>2)&DMC_XMEM));
457 }
458 n++;
459 }
460 }
461
462 /*
463 * Utility routine to load the DMC device registers.
464 */
465 void
466 dmcload(struct dmc_softc *sc, int type, u_short w0, u_short w1)
467 {
468 struct dmc_command *qp;
469 int sps;
470
471 sps = splnet();
472
473 /* grab a command buffer from the free list */
474 if ((qp = sc->sc_qfreeh) == (struct dmc_command *)0)
475 panic("dmc command queue overflow");
476 DEQUEUE(sc->sc_qfreeh, sc->sc_qfreet);
477
478 /* fill in requested info */
479 qp->qp_cmd = (type | DMC_RQI);
480 qp->qp_ubaddr = w0;
481 qp->qp_cc = w1;
482
483 if (sc->sc_qactive) { /* command in progress */
484 if (type == DMC_READ) {
485 QUEUE_AT_HEAD(qp, sc->sc_qhead, sc->sc_qtail);
486 } else {
487 QUEUE_AT_TAIL(qp, sc->sc_qhead, sc->sc_qtail);
488 }
489 } else { /* command port free */
490 sc->sc_qactive = qp;
491 DMC_WBYTE(DMC_BSEL0, qp->qp_cmd);
492 dmcrint(sc);
493 }
494 splx(sps);
495 }
496
497 /*
498 * DMC interface receiver interrupt.
499 * Ready to accept another command,
500 * pull one off the command queue.
501 */
502 void
503 dmcrint(void *arg)
504 {
505 struct dmc_softc *sc = arg;
506 struct dmc_command *qp;
507 int n;
508
509 if ((qp = sc->sc_qactive) == (struct dmc_command *) 0) {
510 printf("%s: dmcrint no command\n", sc->sc_dev.dv_xname);
511 return;
512 }
513 while (DMC_RBYTE(DMC_BSEL0) & DMC_RDYI) {
514 DMC_WWORD(DMC_SEL4, qp->qp_ubaddr);
515 DMC_WWORD(DMC_SEL6, qp->qp_cc);
516 DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) & ~(DMC_IEI|DMC_RQI));
517 /* free command buffer */
518 QUEUE_AT_HEAD(qp, sc->sc_qfreeh, sc->sc_qfreet);
519 while (DMC_RBYTE(DMC_BSEL0) & DMC_RDYI) {
520 /*
521 * Can't check for RDYO here 'cause
522 * this routine isn't reentrant!
523 */
524 DELAY(5);
525 }
526 /* move on to next command */
527 if ((sc->sc_qactive = sc->sc_qhead) == (struct dmc_command *)0)
528 break; /* all done */
529 /* more commands to do, start the next one */
530 qp = sc->sc_qactive;
531 DEQUEUE(sc->sc_qhead, sc->sc_qtail);
532 DMC_WBYTE(DMC_BSEL0, qp->qp_cmd);
533 n = RDYSCAN;
534 while (n-- > 0)
535 if ((DMC_RBYTE(DMC_BSEL0) & DMC_RDYI) ||
536 (DMC_RBYTE(DMC_BSEL2) & DMC_RDYO))
537 break;
538 }
539 if (sc->sc_qactive) {
540 DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) & (DMC_IEI|DMC_RQI));
541 /* VMS does it twice !*$%@# */
542 DMC_WBYTE(DMC_BSEL0, DMC_RBYTE(DMC_BSEL0) & (DMC_IEI|DMC_RQI));
543 }
544
545 }
546
547 /*
548 * DMC interface transmitter interrupt.
549 * A transfer may have completed, check for errors.
550 * If it was a read, notify appropriate protocol.
551 * If it was a write, pull the next one off the queue.
552 */
553 void
554 dmcxint(void *a)
555 {
556 struct dmc_softc *sc = a;
557
558 struct ifnet *ifp;
559 struct mbuf *m;
560 struct ifqueue *inq;
561 int arg, pkaddr, cmd, len, s;
562 struct ifrw *ifrw;
563 struct dmcbufs *rp;
564 struct ifxmt *ifxp;
565 struct dmc_header *dh;
566 char buf[64];
567
568 ifp = &sc->sc_if;
569
570 while (DMC_RBYTE(DMC_BSEL2) & DMC_RDYO) {
571
572 cmd = DMC_RBYTE(DMC_BSEL2) & 0xff;
573 arg = DMC_RWORD(DMC_SEL6) & 0xffff;
574 /* reconstruct UNIBUS address of buffer returned to us */
575 pkaddr = ((arg&DMC_XMEM)<<2) | (DMC_RWORD(DMC_SEL4) & 0xffff);
576 /* release port */
577 DMC_WBYTE(DMC_BSEL2, DMC_RBYTE(DMC_BSEL2) & ~DMC_RDYO);
578 switch (cmd & 07) {
579
580 case DMC_OUR:
581 /*
582 * A read has completed.
583 * Pass packet to type specific
584 * higher-level input routine.
585 */
586 ifp->if_ipackets++;
587 /* find location in dmcuba struct */
588 ifrw= &sc->sc_ifr[0];
589 for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
590 if(rp->ubinfo == pkaddr)
591 break;
592 ifrw++;
593 }
594 if (rp >= &sc->sc_rbufs[NRCV])
595 panic("dmc rcv");
596 if ((rp->flags & DBUF_DMCS) == 0)
597 printf("%s: done unalloc rbuf\n",
598 sc->sc_dev.dv_xname);
599
600 len = (arg & DMC_CCOUNT) - sizeof (struct dmc_header);
601 if (len < 0 || len > DMCMTU) {
602 ifp->if_ierrors++;
603 #ifdef DMCDEBUG
604 printd("%s: bad rcv pkt addr 0x%x len 0x%x\n",
605 sc->sc_dev.dv_xname, pkaddr, len);
606 #endif
607 goto setup;
608 }
609 /*
610 * Deal with trailer protocol: if type is trailer
611 * get true type from first 16-bit word past data.
612 * Remember that type was trailer by setting off.
613 */
614 dh = (struct dmc_header *)ifrw->ifrw_addr;
615 dh->dmc_type = ntohs((u_short)dh->dmc_type);
616 if (len == 0)
617 goto setup;
618
619 /*
620 * Pull packet off interface. Off is nonzero if
621 * packet has trailing header; dmc_get will then
622 * force this header information to be at the front,
623 * but we still have to drop the type and length
624 * which are at the front of any trailer data.
625 */
626 m = if_ubaget(&sc->sc_ifuba, ifrw, ifp, len);
627 if (m == 0)
628 goto setup;
629 /* Shave off dmc_header */
630 m_adj(m, sizeof(struct dmc_header));
631 switch (dh->dmc_type) {
632
633 #ifdef INET
634 case DMC_IPTYPE:
635 schednetisr(NETISR_IP);
636 inq = &ipintrq;
637 break;
638 #endif
639 default:
640 m_freem(m);
641 goto setup;
642 }
643
644 s = splnet();
645 if (IF_QFULL(inq)) {
646 IF_DROP(inq);
647 m_freem(m);
648 } else
649 IF_ENQUEUE(inq, m);
650 splx(s);
651
652 setup:
653 /* is this needed? */
654 rp->ubinfo = ifrw->ifrw_info;
655
656 dmcload(sc, DMC_READ, rp->ubinfo,
657 ((rp->ubinfo >> 2) & DMC_XMEM) | rp->cc);
658 break;
659
660 case DMC_OUX:
661 /*
662 * A write has completed, start another
663 * transfer if there is more data to send.
664 */
665 ifp->if_opackets++;
666 /* find associated dmcbuf structure */
667 ifxp = &sc->sc_ifw[0];
668 for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++) {
669 if(rp->ubinfo == pkaddr)
670 break;
671 ifxp++;
672 }
673 if (rp >= &sc->sc_xbufs[NXMT]) {
674 printf("%s: bad packet address 0x%x\n",
675 sc->sc_dev.dv_xname, pkaddr);
676 break;
677 }
678 if ((rp->flags & DBUF_DMCS) == 0)
679 printf("%s: unallocated packet 0x%x\n",
680 sc->sc_dev.dv_xname, pkaddr);
681 /* mark buffer free */
682 if_ubaend(&sc->sc_ifuba, ifxp);
683 rp->flags &= ~DBUF_DMCS;
684 if (--sc->sc_oused == 0)
685 sc->sc_if.if_timer = 0;
686 else
687 sc->sc_if.if_timer = dmc_timeout;
688 if ((sc->sc_flag & DMC_ONLINE) == 0) {
689 extern int ifqmaxlen;
690
691 /*
692 * We're on the air.
693 * Open the queue to the usual value.
694 */
695 sc->sc_flag |= DMC_ONLINE;
696 ifp->if_snd.ifq_maxlen = ifqmaxlen;
697 }
698 break;
699
700 case DMC_CNTLO:
701 arg &= DMC_CNTMASK;
702 if (arg & DMC_FATAL) {
703 if (arg != DMC_START) {
704 bitmask_snprintf(arg, CNTLO_BITS,
705 buf, sizeof(buf));
706 log(LOG_ERR,
707 "%s: fatal error, flags=%s\n",
708 sc->sc_dev.dv_xname, buf);
709 }
710 dmcrestart(sc);
711 break;
712 }
713 /* ACCUMULATE STATISTICS */
714 switch(arg) {
715 case DMC_NOBUFS:
716 ifp->if_ierrors++;
717 if ((sc->sc_nobuf++ % DMC_RPNBFS) == 0)
718 goto report;
719 break;
720 case DMC_DISCONN:
721 if ((sc->sc_disc++ % DMC_RPDSC) == 0)
722 goto report;
723 break;
724 case DMC_TIMEOUT:
725 if ((sc->sc_timeo++ % DMC_RPTMO) == 0)
726 goto report;
727 break;
728 case DMC_DATACK:
729 ifp->if_oerrors++;
730 if ((sc->sc_datck++ % DMC_RPDCK) == 0)
731 goto report;
732 break;
733 default:
734 goto report;
735 }
736 break;
737 report:
738 #ifdef DMCDEBUG
739 bitmask_snprintf(arg, CNTLO_BITS, buf, sizeof(buf));
740 printd("%s: soft error, flags=%s\n",
741 sc->sc_dev.dv_xname, buf);
742 #endif
743 if ((sc->sc_flag & DMC_RESTART) == 0) {
744 /*
745 * kill off the dmc to get things
746 * going again by generating a
747 * procedure error
748 */
749 sc->sc_flag |= DMC_RESTART;
750 arg = sc->sc_ui.ui_baddr;
751 dmcload(sc, DMC_BASEI, arg, (arg>>2)&DMC_XMEM);
752 }
753 break;
754
755 default:
756 printf("%s: bad control %o\n",
757 sc->sc_dev.dv_xname, cmd);
758 break;
759 }
760 }
761 dmcstart(ifp);
762 }
763
764 /*
765 * DMC output routine.
766 * Encapsulate a packet of type family for the dmc.
767 * Use trailer local net encapsulation if enough data in first
768 * packet leaves a multiple of 512 bytes of data in remainder.
769 */
770 int
771 dmcoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
772 struct rtentry *rt)
773 {
774 int type, error, s;
775 struct mbuf *m = m0;
776 struct dmc_header *dh;
777 ALTQ_DECL(struct altq_pktattr pktattr;)
778
779 if ((ifp->if_flags & IFF_UP) == 0) {
780 error = ENETDOWN;
781 goto bad;
782 }
783
784 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
785
786 switch (dst->sa_family) {
787 #ifdef INET
788 case AF_INET:
789 type = DMC_IPTYPE;
790 break;
791 #endif
792
793 case AF_UNSPEC:
794 dh = (struct dmc_header *)dst->sa_data;
795 type = dh->dmc_type;
796 break;
797
798 default:
799 printf("%s: can't handle af%d\n", ifp->if_xname,
800 dst->sa_family);
801 error = EAFNOSUPPORT;
802 goto bad;
803 }
804
805 /*
806 * Add local network header
807 * (there is space for a uba on a vax to step on)
808 */
809 M_PREPEND(m, sizeof(struct dmc_header), M_DONTWAIT);
810 if (m == 0) {
811 error = ENOBUFS;
812 goto bad;
813 }
814 dh = mtod(m, struct dmc_header *);
815 dh->dmc_type = htons((u_short)type);
816
817 /*
818 * Queue message on interface, and start output if interface
819 * not yet active.
820 */
821 s = splnet();
822 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
823 if (error) {
824 /* mbuf is already freed */
825 splx(s);
826 return (error);
827 }
828 dmcstart(ifp);
829 splx(s);
830 return (0);
831
832 bad:
833 m_freem(m0);
834 return (error);
835 }
836
837
838 /*
839 * Process an ioctl request.
840 */
841 /* ARGSUSED */
842 int
843 dmcioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
844 {
845 int s = splnet(), error = 0;
846 register struct dmc_softc *sc = ifp->if_softc;
847
848 switch (cmd) {
849
850 case SIOCSIFADDR:
851 ifp->if_flags |= IFF_UP;
852 if ((ifp->if_flags & IFF_RUNNING) == 0)
853 dmcinit(ifp);
854 break;
855
856 case SIOCSIFDSTADDR:
857 if ((ifp->if_flags & IFF_RUNNING) == 0)
858 dmcinit(ifp);
859 break;
860
861 case SIOCSIFFLAGS:
862 if ((ifp->if_flags & IFF_UP) == 0 &&
863 sc->sc_flag & DMC_RUNNING)
864 dmcdown(sc);
865 else if (ifp->if_flags & IFF_UP &&
866 (sc->sc_flag & DMC_RUNNING) == 0)
867 dmcrestart(sc);
868 break;
869
870 default:
871 error = EINVAL;
872 }
873 splx(s);
874 return (error);
875 }
876
877 /*
878 * Restart after a fatal error.
879 * Clear device and reinitialize.
880 */
881 void
882 dmcrestart(struct dmc_softc *sc)
883 {
884 int s, i;
885
886 #ifdef DMCDEBUG
887 /* dump base table */
888 printf("%s base table:\n", sc->sc_dev.dv_xname);
889 for (i = 0; i < sizeof (struct dmc_base); i++)
890 printf("%o\n" ,dmc_base[unit].d_base[i]);
891 #endif
892
893 dmcdown(sc);
894
895 /*
896 * Let the DMR finish the MCLR. At 1 Mbit, it should do so
897 * in about a max of 6.4 milliseconds with diagnostics enabled.
898 */
899 for (i = 100000; i && (DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0; i--)
900 ;
901 /* Did the timer expire or did the DMR finish? */
902 if ((DMC_RBYTE(DMC_BSEL1) & DMC_RUN) == 0) {
903 log(LOG_ERR, "%s: M820 Test Failed\n", sc->sc_dev.dv_xname);
904 return;
905 }
906
907 /* restart DMC */
908 dmcinit(&sc->sc_if);
909 sc->sc_flag &= ~DMC_RESTART;
910 s = splnet();
911 dmcstart(&sc->sc_if);
912 splx(s);
913 sc->sc_if.if_collisions++; /* why not? */
914 }
915
916 /*
917 * Reset a device and mark down.
918 * Flush output queue and drop queue limit.
919 */
920 void
921 dmcdown(struct dmc_softc *sc)
922 {
923 struct ifxmt *ifxp;
924
925 DMC_WBYTE(DMC_BSEL1, DMC_MCLR);
926 sc->sc_flag &= ~(DMC_RUNNING | DMC_ONLINE);
927
928 for (ifxp = sc->sc_ifw; ifxp < &sc->sc_ifw[NXMT]; ifxp++) {
929 #ifdef notyet
930 if (ifxp->ifw_xtofree) {
931 (void) m_freem(ifxp->ifw_xtofree);
932 ifxp->ifw_xtofree = 0;
933 }
934 #endif
935 }
936 IF_PURGE(&sc->sc_if.if_snd);
937 }
938
939 /*
940 * Watchdog timeout to see that transmitted packets don't
941 * lose interrupts. The device has to be online (the first
942 * transmission may block until the other side comes up).
943 */
944 void
945 dmctimeout(struct ifnet *ifp)
946 {
947 struct dmc_softc *sc = ifp->if_softc;
948 char buf1[64], buf2[64];
949
950 if (sc->sc_flag & DMC_ONLINE) {
951 bitmask_snprintf(DMC_RBYTE(DMC_BSEL0) & 0xff, DMC0BITS,
952 buf1, sizeof(buf1));
953 bitmask_snprintf(DMC_RBYTE(DMC_BSEL2) & 0xff, DMC2BITS,
954 buf2, sizeof(buf2));
955 log(LOG_ERR, "%s: output timeout, bsel0=%s bsel2=%s\n",
956 sc->sc_dev.dv_xname, buf1, buf2);
957 dmcrestart(sc);
958 }
959 }
960