]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sr/if_sr_isa.c
Initialize isa_devtab entries for interrupt handlers in individual
[FreeBSD/FreeBSD.git] / sys / dev / sr / if_sr_isa.c
1 /*
2  * Copyright (c) 1996 John Hay.
3  * Copyright (c) 1996 SDL Communications, Inc.
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. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: if_sr.c,v 1.15 1998/06/21 14:53:12 bde Exp $
31  */
32
33 /*
34  * Programming assumptions and other issues.
35  *
36  * Only a 16K window will be used.
37  *
38  * The descriptors of a DMA channel will fit in a 16K memory window.
39  *
40  * The buffers of a transmit DMA channel will fit in a 16K memory window.
41  *
42  * When interface is going up, handshaking is set and it is only cleared
43  * when the interface is down'ed.
44  *
45  * There should be a way to set/reset Raw HDLC/PPP, Loopback, DCE/DTE,
46  * internal/external clock, etc.....
47  *
48  */
49
50 #include "sr.h"
51 #ifdef notyet
52 #include "fr.h"
53 #else
54 #define NFR     0
55 #endif
56 #include "bpfilter.h"
57
58 #include "sppp.h"
59 #if NSPPP <= 0
60 #error Device 'sr' requires sppp.
61 #endif
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/sockio.h>
69 #include <sys/socket.h>
70
71 #include <net/if.h>
72 #include <net/if_sppp.h>
73
74 #if NBPFILTER > 0
75 #include <net/bpf.h>
76 #endif
77
78 #include <machine/md_var.h>
79
80 #include <i386/isa/if_srregs.h>
81 #include <i386/isa/ic/hd64570.h>
82 #include <i386/isa/isa_device.h>
83
84 /* #define USE_MODEMCK */
85
86 #ifndef BUGGY
87 #define BUGGY           0
88 #endif
89
90 #define PPP_HEADER_LEN  4
91
92 /*
93  * These macros are used to hide the difference between the way the
94  * ISA N2 cards and the PCI N2 cards access the Hitachi 64570 SCA.
95  */
96 #define SRC_GET8(base,off)      (*hc->src_get8)(base,(u_int)&off)
97 #define SRC_GET16(base,off)     (*hc->src_get16)(base,(u_int)&off)
98 #define SRC_PUT8(base,off,d)    (*hc->src_put8)(base,(u_int)&off,d)
99 #define SRC_PUT16(base,off,d)   (*hc->src_put16)(base,(u_int)&off,d)
100
101 /*
102  * These macros enable/disable the DPRAM and select the correct
103  * DPRAM page.
104  */
105 #define SRC_GET_WIN(addr)       ((addr >> SRC_WIN_SHFT) & SR_PG_MSK)
106
107 #define SRC_SET_ON(iobase)      outb(iobase+SR_PCR,                          \
108                                         SR_PCR_MEM_WIN | inb(iobase+SR_PCR))
109 #define SRC_SET_MEM(iobase,win) outb(iobase+SR_PSR, SRC_GET_WIN(win) |       \
110                                         (inb(iobase+SR_PSR) & ~SR_PG_MSK))
111 #define SRC_SET_OFF(iobase)     outb(iobase+SR_PCR,                          \
112                                         ~SR_PCR_MEM_WIN & inb(iobase+SR_PCR))
113
114 /*
115  * Define the hardware (card information) structure needed to keep
116  * track of the device itself... There is only one per card.
117  */
118 struct sr_hardc {
119         struct  sr_hardc *next;         /* PCI card linkage */
120         struct  sr_softc *sc;           /* software channels */
121         int     cunit;                  /* card w/in system */
122
123         u_short iobase;                 /* I/O Base Address */
124         int     cardtype;
125         int     numports;               /* # of ports on cd */
126         int     mempages;
127         u_int   memsize;                /* DPRAM size: bytes */
128         u_int   winmsk;
129         vm_offset_t     sca_base;
130         vm_offset_t     mem_pstart;     /* start of buffer */
131         caddr_t mem_start;              /* start of DP RAM */
132         caddr_t mem_end;                /* end of DP RAM */
133         caddr_t plx_base;
134
135         sca_regs        *sca;           /* register array */
136
137         /*
138          * We vectorize the following functions to allow re-use between the
139          * ISA card's needs and those of the PCI card.
140          */
141         void    (*src_put8)(u_int base, u_int off, u_int val);
142         void    (*src_put16)(u_int base, u_int off, u_int val);
143         u_int   (*src_get8)(u_int base, u_int off);
144         u_int   (*src_get16)(u_int base, u_int off);
145 };
146
147 static int      next_sc_unit = 0;
148 static int      sr_watcher = 0;
149 static struct   sr_hardc sr_hardc[NSR];
150 static struct   sr_hardc *sr_hardc_pci;
151
152 /*
153  * Define the software interface for the card... There is one for
154  * every channel (port).
155  */
156 struct sr_softc {
157         struct  sppp ifsppp;    /* PPP service w/in system */
158         struct  sr_hardc *hc;   /* card-level information */
159
160         int     unit;           /* With regard to all sr devices */
161         int     subunit;        /* With regard to this card */
162
163         int     attached;       /* attached to FR or PPP */
164         int     protocol;       /* FR or PPP */
165 #define N2_USE_FRP      2       /* Frame Relay Protocol */
166 #define N2_USE_PPP      1       /* Point-to-Point Protocol */
167
168         struct  buf_block {
169                 u_int   txdesc; /* DPRAM offset */
170                 u_int   txstart;/* DPRAM offset */
171                 u_int   txend;  /* DPRAM offset */
172                 u_int   txtail; /* # of 1st free gran */
173                 u_int   txmax;  /* # of free grans */
174                 u_int   txeda;  /* err descr addr */
175         } block[SR_TX_BLOCKS];
176
177         char    xmit_busy;      /* Transmitter is busy */
178         char    txb_inuse;      /* # of tx grans in use */
179         u_int   txb_new;        /* ndx to new buffer */
180         u_int   txb_next_tx;    /* ndx to next gran rdy tx */
181
182         u_int   rxdesc;         /* DPRAM offset */
183         u_int   rxstart;        /* DPRAM offset */
184         u_int   rxend;          /* DPRAM offset */
185         u_int   rxhind;         /* ndx to the hd of rx bufrs */
186         u_int   rxmax;          /* # of avail grans */
187
188         u_int   clk_cfg;        /* Clock configuration */
189
190         int     scachan;        /* channel # on card */
191 };
192
193 /*
194  * List of valid interrupt numbers for the N2 ISA card.
195  */
196 static int sr_irqtable[16] = {
197         0,      /*  0 */
198         0,      /*  1 */
199         0,      /*  2 */
200         1,      /*  3 */
201         1,      /*  4 */
202         1,      /*  5 */
203         0,      /*  6 */
204         1,      /*  7 */
205         0,      /*  8 */
206         0,      /*  9 */
207         1,      /* 10 */
208         1,      /* 11 */
209         1,      /* 12 */
210         0,      /* 13 */
211         0,      /* 14 */
212         1       /* 15 */
213 };
214
215 static int      srprobe(struct isa_device *id);
216 static int      srattach_isa(struct isa_device *id);
217
218 struct  isa_driver srdriver = {srprobe, srattach_isa, "src"};
219
220 /*
221  * Baud Rate table for Sync Mode.
222  * Each entry consists of 3 elements:
223  * Baud Rate (x100) , TMC, BR
224  *
225  * Baud Rate = FCLK / TMC / 2^BR
226  * Baud table for Crystal freq. of 9.8304 Mhz
227  */
228 #ifdef N2_TEST_SPEED
229 struct rate_line {
230         int     target;         /* target rate/100 */
231         int     tmc_reg;        /* TMC register value */
232         int     br_reg;         /* BR (BaudRateClk) selector */
233 } n2_rates[] = {
234         /* Baudx100     TMC             BR */
235         { 3,            128,            8 },
236         { 6,            128,            7 },
237         { 12,           128,            6 },
238         { 24,           128,            5 },
239         { 48,           128,            4 },
240         { 96,           128,            3 },
241         { 192,          128,            2 },
242         { 384,          128,            1 },
243         { 560,          88,             1 },
244         { 640,          77,             1 },
245         { 1280,         38,             1 },
246         { 2560,         19,             1 },
247         { 5120,         10,             1 },
248         { 10000,        5,              1 },
249         { 15000,        3,              1 },
250         { 25000,        2,              1 },
251         { 50000,        1,              1 },
252         { 0,            0,              0 }
253 };
254
255 int     sr_test_speed[] = {
256         N2_TEST_SPEED,
257         N2_TEST_SPEED
258 };
259
260 int     etc0vals[] = {
261         SR_MCR_ETC0,            /* ISA channel 0 */
262         SR_MCR_ETC1,            /* ISA channel 1 */
263         SR_FECR_ETC0,           /* PCI channel 0 */
264         SR_FECR_ETC1            /* PCI channel 1 */
265 };
266 #endif
267
268 struct  sr_hardc *srattach_pci(int unit, vm_offset_t plx_vaddr,
269                                 vm_offset_t sca_vaddr);
270 void    srintr_hc(struct sr_hardc *hc);
271
272 static int      srattach(struct sr_hardc *hc);
273 static ointhand2_t      srintr;
274 static void     sr_xmit(struct sr_softc *sc);
275 static void     srstart(struct ifnet *ifp);
276 static int      srioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
277 static void     srwatchdog(struct ifnet *ifp);
278 static int      sr_packet_avail(struct sr_softc *sc, int *len, u_char *rxstat);
279 static void     sr_copy_rxbuf(struct mbuf *m, struct sr_softc *sc, int len);
280 static void     sr_eat_packet(struct sr_softc *sc, int single);
281 static void     sr_get_packets(struct sr_softc *sc);
282
283 static void     sr_up(struct sr_softc *sc);
284 static void     sr_down(struct sr_softc *sc);
285 static void     src_init(struct sr_hardc *hc);
286 static void     sr_init_sca(struct sr_hardc *hc);
287 static void     sr_init_msci(struct sr_softc *sc);
288 static void     sr_init_rx_dmac(struct sr_softc *sc);
289 static void     sr_init_tx_dmac(struct sr_softc *sc);
290 static void     sr_dmac_intr(struct sr_hardc *hc, u_char isr);
291 static void     sr_msci_intr(struct sr_hardc *hc, u_char isr);
292 static void     sr_timer_intr(struct sr_hardc *hc, u_char isr);
293 static void     sr_modemck(void *x);
294
295 static u_int    src_get8_io(u_int base, u_int off);
296 static u_int    src_get16_io(u_int base, u_int off);
297 static void     src_put8_io(u_int base, u_int off, u_int val);
298 static void     src_put16_io(u_int base, u_int off, u_int val);
299 static u_int    src_get8_mem(u_int base, u_int off);
300 static u_int    src_get16_mem(u_int base, u_int off);
301 static void     src_put8_mem(u_int base, u_int off, u_int val);
302 static void     src_put16_mem(u_int base, u_int off, u_int val);
303
304 #if NFR > 0
305 extern void     fr_detach(struct ifnet *);
306 extern int      fr_attach(struct ifnet *);
307 extern int      fr_ioctl(struct ifnet *, int, caddr_t);
308 extern void     fr_flush(struct ifnet *);
309 extern int      fr_input(struct ifnet *, struct mbuf *);
310 extern struct   mbuf *fr_dequeue(struct ifnet *);
311 #endif
312
313 /*
314  * I/O for ISA N2 card(s)
315  */
316 #define SRC_REG(iobase,y)       ((((y) & 0xf) + (((y) & 0xf0) << 6) +       \
317                                 (iobase)) | 0x8000)
318
319 static u_int
320 src_get8_io(u_int base, u_int off)
321 {
322         return inb(SRC_REG(base, off));
323 }
324
325 static u_int
326 src_get16_io(u_int base, u_int off)
327 {
328         return inw(SRC_REG(base, off));
329 }
330
331 static void
332 src_put8_io(u_int base, u_int off, u_int val)
333 {
334         outb(SRC_REG(base, off), val);
335 }
336
337 static void
338 src_put16_io(u_int base, u_int off, u_int val)
339 {
340         outw(SRC_REG(base, off), val);
341 }
342
343 /*
344  * I/O for PCI N2 card(s)
345  */
346 #define SRC_PCI_SCA_REG(y)      ((y & 2) ? ((y & 0xfd) + 0x100) : y)
347
348 static u_int
349 src_get8_mem(u_int base, u_int off)
350 {
351         return *((u_char *)(base + SRC_PCI_SCA_REG(off)));
352 }
353
354 static u_int
355 src_get16_mem(u_int base, u_int off)
356 {
357         return *((u_short *)(base + SRC_PCI_SCA_REG(off)));
358 }
359
360 static void
361 src_put8_mem(u_int base, u_int off, u_int val)
362 {
363         *((u_char *)(base + SRC_PCI_SCA_REG(off))) = (u_char)val;
364 }
365
366 static void
367 src_put16_mem(u_int base, u_int off, u_int val)
368 {
369         *((u_short *)(base + SRC_PCI_SCA_REG(off))) = (u_short)val;
370 }
371
372 /*
373  * Probe for an ISA card. If it is there, size its memory. Then get the
374  * rest of its information and fill it in.
375  */
376 static int
377 srprobe(struct isa_device *id)
378 {
379         struct sr_hardc *hc = &sr_hardc[id->id_unit];
380         u_int pgs, i, tmp;
381         u_short port;
382         u_short *smem;
383         u_char mar;
384         sca_regs *sca = 0;
385
386         /*
387          * Now see if the card is realy there.
388          */
389         hc->cardtype = SR_CRD_N2;
390
391         /*
392          * We have to fill these in early because the SRC_PUT* and SRC_GET*
393          * macros use them.
394          */
395         hc->src_get8 = src_get8_io;
396         hc->src_get16 = src_get16_io;
397         hc->src_put8 = src_put8_io;
398         hc->src_put16 = src_put16_io;
399
400         hc->sca = 0;
401         port = id->id_iobase;
402         hc->numports = NCHAN;   /* assumed # of channels on the card */
403
404         if (id->id_flags & SR_FLAGS_NCHAN_MSK)
405                 hc->numports = id->id_flags & SR_FLAGS_NCHAN_MSK;
406
407         outb(port + SR_PCR, 0); /* turn off the card */
408
409         /*
410          * Next, we'll test the Base Address Register to retension of
411          * data... ... seeing if we're *really* talking to an N2.
412          */
413         for (i = 0; i < 0x100; i++) {
414                 outb(port + SR_BAR, i);
415                 inb(port + SR_PCR);
416                 tmp = inb(port + SR_BAR);
417                 if (tmp != i) {
418                         printf("sr%d: probe failed BAR %x, %x.\n",
419                                id->id_unit, i, tmp);
420                         return 0;
421                 }
422         }
423
424         /*
425          * Now see if we can see the SCA.
426          */
427         outb(port + SR_PCR, SR_PCR_SCARUN | inb(port + SR_PCR));
428         SRC_PUT8(port, sca->wcrl, 0);
429         SRC_PUT8(port, sca->wcrm, 0);
430         SRC_PUT8(port, sca->wcrh, 0);
431         SRC_PUT8(port, sca->pcr, 0);
432         SRC_PUT8(port, sca->msci[0].tmc, 0);
433         inb(port);
434
435         tmp = SRC_GET8(port, sca->msci[0].tmc);
436         if (tmp != 0) {
437                 printf("sr%d: Error reading SCA 0, %x\n", id->id_unit, tmp);
438                 return 0;
439         }
440         SRC_PUT8(port, sca->msci[0].tmc, 0x5A);
441         inb(port);
442
443         tmp = SRC_GET8(port, sca->msci[0].tmc);
444         if (tmp != 0x5A) {
445                 printf("sr%d: Error reading SCA 0x5A, %x\n", id->id_unit, tmp);
446                 return 0;
447         }
448         SRC_PUT16(port, sca->dmac[0].cda, 0);
449         inb(port);
450
451         tmp = SRC_GET16(port, sca->dmac[0].cda);
452         if (tmp != 0) {
453                 printf("sr%d: Error reading SCA 0, %x\n", id->id_unit, tmp);
454                 return 0;
455         }
456         SRC_PUT16(port, sca->dmac[0].cda, 0x55AA);
457         inb(port);
458
459         tmp = SRC_GET16(port, sca->dmac[0].cda);
460         if (tmp != 0x55AA) {
461                 printf("sr%d: Error reading SCA 0x55AA, %x\n",
462                        id->id_unit, tmp);
463                 return 0;
464         }
465         /*
466          * OK, the board's interface registers seem to work. Now we'll see
467          * if the Dual-Ported RAM is fully accessible...
468          */
469         outb(port + SR_PCR, SR_PCR_EN_VPM | SR_PCR_ISA16);
470         outb(port + SR_PSR, SR_PSR_WIN_16K);
471
472         /*
473          * Take the kernel "virtual" address supplied to us and convert
474          * it to a "real" address. Then program the card to use that.
475          */
476         mar = (kvtop(id->id_maddr) >> 16) & SR_PCR_16M_SEL;
477         outb(port + SR_PCR, mar | inb(port + SR_PCR));
478         mar = kvtop(id->id_maddr) >> 12;
479         outb(port + SR_BAR, mar);
480         outb(port + SR_PCR, inb(port + SR_PCR) | SR_PCR_MEM_WIN);
481         smem = (u_short *)id->id_maddr; /* DP RAM Address */
482
483         /*
484          * Here we will perform the memory scan to size the device.
485          *
486          * This is done by marking each potential page with a magic number.
487          * We then loop through the pages looking for that magic number. As
488          * soon as we no longer see that magic number, we'll quit the scan,
489          * knowing that no more memory is present. This provides the number
490          * of pages present on the card.
491          *
492          * Note: We're sizing 16K memory granules.
493          */
494         for (i = 0; i <= SR_PSR_PG_SEL; i++) {
495                 outb(port + SR_PSR,
496                      (inb(port + SR_PSR) & ~SR_PSR_PG_SEL) | i);
497
498                 *smem = 0xAA55;
499         }
500
501         for (i = 0; i <= SR_PSR_PG_SEL; i++) {
502                 outb(port + SR_PSR,
503                      (inb(port + SR_PSR) & ~SR_PSR_PG_SEL) | i);
504
505                 if (*smem != 0xAA55) {
506                         /*
507                          * If we have less than 64k of memory, give up. That
508                          * is 4 x 16k pages.
509                          */
510                         if (i < 4) {
511                                 printf("sr%d: Bad mem page %d, mem %x, %x.\n",
512                                        id->id_unit, i, 0xAA55, *smem);
513                                 return 0;
514                         }
515                         break;
516                 }
517                 *smem = i;
518         }
519
520         hc->mempages = i;
521         hc->memsize = i * SRC_WIN_SIZ;
522         hc->winmsk = SRC_WIN_MSK;
523         pgs = i;                /* final count of 16K pages */
524
525         /*
526          * This next loop erases the contents of that page in DPRAM
527          */
528         for (i = 0; i <= pgs; i++) {
529                 outb(port + SR_PSR,
530                      (inb(port + SR_PSR) & ~SR_PSR_PG_SEL) | i);
531                 bzero(smem, SRC_WIN_SIZ);
532         }
533
534         SRC_SET_OFF(port);
535
536         /*
537          * We have a card here, fill in what we can.
538          */
539         id->id_msize = SRC_WIN_SIZ;
540         hc->iobase = id->id_iobase;
541         hc->sca_base = id->id_iobase;
542         hc->mem_start = id->id_maddr;
543         hc->mem_end = (id->id_maddr + id->id_msize) - 1;
544         hc->mem_pstart = 0;
545         hc->cunit = id->id_unit;
546
547         /*
548          * Do a little sanity check.
549          */
550         if (sr_irqtable[ffs(id->id_irq) - 1] == 0)
551                 printf("sr%d: Warning: illegal interrupt %d chosen.\n",
552                        id->id_unit, ffs(id->id_irq) - 1);
553
554         /*
555          * Bogus card configuration
556          */
557         if ((hc->numports > NCHAN)      /* only 2 ports/card */
558             ||(hc->memsize > (512 * 1024)))     /* no more than 256K */
559                 return 0;
560
561         return SRC_IO_SIZ;      /* return the amount of IO addresses used. */
562 }
563
564 /*
565  * srattach_isa and srattach_pci allocate memory for hardc, softc and
566  * data buffers. It also does any initialization that is bus specific.
567  * At the end they call the common srattach() function.
568  */
569 static int
570 srattach_isa(struct isa_device *id)
571 {
572         u_char mar;
573         struct sr_hardc *hc = &sr_hardc[id->id_unit];
574
575         outb(hc->iobase + SR_PCR, inb(hc->iobase + SR_PCR) | SR_PCR_SCARUN);
576         outb(hc->iobase + SR_PSR, inb(hc->iobase + SR_PSR) | SR_PSR_EN_SCA_DMA);
577         outb(hc->iobase + SR_MCR,
578              SR_MCR_DTR0 | SR_MCR_DTR1 | SR_MCR_TE0 | SR_MCR_TE1);
579
580         SRC_SET_ON(hc->iobase);
581
582         /*
583          * Configure the card. Mem address, irq,
584          */
585         mar = (kvtop(id->id_maddr) >> 16) & SR_PCR_16M_SEL;
586         outb(hc->iobase + SR_PCR,
587              mar | (inb(hc->iobase + SR_PCR) & ~SR_PCR_16M_SEL));
588         mar = kvtop(id->id_maddr) >> 12;
589         outb(hc->iobase + SR_BAR, mar);
590
591         /*
592          * Allocate the software interface table(s)
593          */
594         hc->sc = malloc(hc->numports * sizeof(struct sr_softc),
595                         M_DEVBUF, M_WAITOK);
596         bzero(hc->sc, hc->numports * sizeof(struct sr_softc));
597
598         /*
599          * Get the TX clock direction and configuration. The default is a
600          * single external clock which is used by RX and TX.
601          */
602 #ifdef N2_TEST_SPEED
603         if (sr_test_speed[0] > 0)
604                 hc->sc[0].clk_cfg = SR_FLAGS_INT_CLK;
605         else if (id->id_flags & SR_FLAGS_0_CLK_MSK)
606                 hc->sc[0].clk_cfg =
607                     (id->id_flags & SR_FLAGS_0_CLK_MSK)
608                     >> SR_FLAGS_CLK_SHFT;
609 #else
610         if (id->id_flags & SR_FLAGS_0_CLK_MSK)
611                 hc->sc[0].clk_cfg =
612                     (id->id_flags & SR_FLAGS_0_CLK_MSK)
613                     >> SR_FLAGS_CLK_SHFT;
614 #endif
615
616         if (hc->numports == 2)
617 #ifdef N2_TEST_SPEED
618                 if (sr_test_speed[1] > 0)
619                         hc->sc[0].clk_cfg = SR_FLAGS_INT_CLK;
620                 else
621 #endif
622                 if (id->id_flags & SR_FLAGS_1_CLK_MSK)
623                         hc->sc[1].clk_cfg = (id->id_flags & SR_FLAGS_1_CLK_MSK)
624                             >> (SR_FLAGS_CLK_SHFT + SR_FLAGS_CLK_CHAN_SHFT);
625
626         return srattach(hc);
627 }
628
629 struct sr_hardc *
630 srattach_pci(int unit, vm_offset_t plx_vaddr, vm_offset_t sca_vaddr)
631 {
632         int numports, pndx;
633         u_int fecr, *fecrp = (u_int *)(sca_vaddr + SR_FECR);
634         struct sr_hardc *hc, **hcp;
635
636         /*
637          * Configure the PLX. This is magic. I'm doing it just like I'm told
638          * to. :-)
639          * 
640          * offset
641          *  0x00 - Map Range    - Mem-mapped to locate anywhere
642          *  0x04 - Re-Map       - PCI address decode enable
643          *  0x18 - Bus Region   - 32-bit bus, ready enable
644          *  0x1c - Master Range - include all 16 MB
645          *  0x20 - Master RAM   - Map SCA Base at 0
646          *  0x28 - Master Remap - direct master memory enable
647          *  0x68 - Interrupt    - Enable interrupt (0 to disable)
648          * 
649          * Note: This is "cargo cult" stuff.  - jrc
650          */
651         *((u_int *)(plx_vaddr + 0x00)) = 0xfffff000;
652         *((u_int *)(plx_vaddr + 0x04)) = 1;
653         *((u_int *)(plx_vaddr + 0x18)) = 0x40030043;
654         *((u_int *)(plx_vaddr + 0x1c)) = 0xff000000;
655         *((u_int *)(plx_vaddr + 0x20)) = 0;
656         *((u_int *)(plx_vaddr + 0x28)) = 0xe9;
657         *((u_int *)(plx_vaddr + 0x68)) = 0x10900;
658
659         /*
660          * Get info from card.
661          *
662          * Only look for the second port if the first exists. Too many things
663          * will break if we have only a second port.
664          */
665         fecr = *fecrp;
666         numports = 0;
667
668         if (((fecr & SR_FECR_ID0) >> SR_FE_ID0_SHFT) != SR_FE_ID_NONE) {
669                 numports++;
670                 if (((fecr & SR_FECR_ID1) >> SR_FE_ID1_SHFT) != SR_FE_ID_NONE)
671                         numports++;
672         }
673         if (numports == 0)
674                 return NULL;
675
676         hc = sr_hardc_pci;
677         hcp = &sr_hardc_pci;
678
679         while (hc) {
680                 hcp = &hc->next;
681                 hc = hc->next;
682         }
683
684         hc = malloc(sizeof(struct sr_hardc), M_DEVBUF, M_WAITOK);
685         *hcp = hc;
686         bzero(hc, sizeof(struct sr_hardc));
687
688         hc->sc = malloc(numports * sizeof(struct sr_softc),
689                         M_DEVBUF, M_WAITOK);
690         bzero(hc->sc, numports * sizeof(struct sr_softc));
691
692         hc->numports = numports;
693         hc->cunit = unit;
694         hc->cardtype = SR_CRD_N2PCI;
695         hc->plx_base = (caddr_t)plx_vaddr;
696         hc->sca_base = sca_vaddr;
697
698         hc->src_put8 = src_put8_mem;
699         hc->src_put16 = src_put16_mem;
700         hc->src_get8 = src_get8_mem;
701         hc->src_get16 = src_get16_mem;
702
703         /*
704          * Malloc area for tx and rx buffers. For now allocate SRC_WIN_SIZ
705          * (16k) for each buffer.
706          *
707          * Allocate the block below 16M because the N2pci card can only access
708          * 16M memory at a time.
709          *
710          * (We could actually allocate a contiguous block above the 16MB limit,
711          * but this would complicate card programming more than we want to
712          * right now -jrc)
713          */
714         hc->memsize = 2 * hc->numports * SRC_WIN_SIZ;
715         hc->mem_start = contigmalloc(hc->memsize,
716                                      M_DEVBUF,
717                                      M_NOWAIT,
718                                      0ul,
719                                      0xfffffful,
720                                      0x10000,
721                                      0x1000000);
722
723         if (hc->mem_start == NULL) {
724                 printf("src%d: pci: failed to allocate buffer space.\n", unit);
725                 return NULL;
726         }
727         hc->winmsk = 0xffffffff;
728         hc->mem_end = (caddr_t)((u_int)hc->mem_start + hc->memsize);
729         hc->mem_pstart = kvtop(hc->mem_start);
730         bzero(hc->mem_start, hc->memsize);
731
732         for (pndx = 0; pndx < numports; pndx++) {
733                 int intf_sw;
734                 struct sr_softc *sc;
735
736                 sc = &hc->sc[pndx];
737
738                 switch (pndx) {
739                 case 1:
740                         intf_sw = fecr & SR_FECR_ID1 >> SR_FE_ID1_SHFT;
741                         break;
742                 case 0:
743                 default:
744                         intf_sw = fecr & SR_FECR_ID0 >> SR_FE_ID0_SHFT;
745                 }
746
747 #ifdef N2_TEST_SPEED
748                 if (sr_test_speed[pndx] > 0)
749                         sc->clk_cfg = SR_FLAGS_INT_CLK;
750                 else
751 #endif
752                         switch (intf_sw) {
753                         default:
754                         case SR_FE_ID_RS232:
755                         case SR_FE_ID_HSSI:
756                         case SR_FE_ID_RS422:
757                         case SR_FE_ID_TEST:
758                                 break;
759
760                         case SR_FE_ID_V35:
761                                 sc->clk_cfg = SR_FLAGS_EXT_SEP_CLK;
762                                 break;
763
764                         case SR_FE_ID_X21:
765                                 sc->clk_cfg = SR_FLAGS_EXT_CLK;
766                                 break;
767                         }
768         }
769
770         *fecrp = SR_FECR_DTR0
771             | SR_FECR_DTR1
772             | SR_FECR_TE0
773             | SR_FECR_TE1;
774
775         srattach(hc);
776
777         return hc;
778 }
779
780 /*
781  * Register the ports on the adapter.
782  * Fill in the info for each port.
783  * Attach each port to sppp and bpf.
784  */
785 static int
786 srattach(struct sr_hardc *hc)
787 {
788         struct sr_softc *sc = hc->sc;
789         struct ifnet *ifp;
790         int unit;               /* index: channel w/in card */
791
792         /*
793          * Report Card configuration information before we start configuring
794          * each channel on the card...
795          */
796         printf("src%d: %uK RAM (%d mempages) @ %08x-%08x, %u ports.\n",
797                hc->cunit, hc->memsize / 1024, hc->mempages,
798                (u_int)hc->mem_start, (u_int)hc->mem_end, hc->numports);
799
800         src_init(hc);
801         sr_init_sca(hc);
802
803         /*
804          * Now configure each port on the card.
805          */
806         for (unit = 0; unit < hc->numports; sc++, unit++) {
807                 sc->hc = hc;
808                 sc->subunit = unit;
809                 sc->unit = next_sc_unit;
810                 next_sc_unit++;
811                 sc->scachan = unit % NCHAN;
812
813                 sr_init_rx_dmac(sc);
814                 sr_init_tx_dmac(sc);
815                 sr_init_msci(sc);
816
817                 ifp = &sc->ifsppp.pp_if;
818                 ifp->if_softc = sc;
819                 ifp->if_unit = sc->unit;
820                 ifp->if_name = "sr";
821                 ifp->if_mtu = PP_MTU;
822                 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
823                 ifp->if_ioctl = srioctl;
824                 ifp->if_start = srstart;
825                 ifp->if_watchdog = srwatchdog;
826
827                 printf("sr%d: Adapter %d, port %d.\n",
828                        sc->unit, hc->cunit, sc->subunit);
829
830                 /*
831                  * Despite the fact that we want to allow both PPP *and*
832                  * Frame Relay access to a channel, due to the architecture
833                  * of the system, we'll have to do the attach here.
834                  *
835                  * At some point I'll defer the attach to the "up" call and
836                  * have the attach/detach performed when the interface is
837                  * up/downed...
838                  */
839                 sc->attached = 0;
840                 sc->protocol = N2_USE_PPP;      /* default protocol */
841
842 #if     0
843                 sc->ifsppp.pp_flags = PP_KEEPALIVE;
844                 sppp_attach((struct ifnet *)&sc->ifsppp);
845 #endif
846
847                 if_attach(ifp);
848
849 #if NBPFILTER > 0
850                 bpfattach(ifp, DLT_PPP, PPP_HEADER_LEN);
851 #endif
852         }
853
854         if (hc->mempages)
855                 SRC_SET_OFF(hc->iobase);
856
857         return 1;
858 }
859
860 /*
861  * N2 Interrupt Service Routine
862  *
863  * First figure out which SCA gave the interrupt.
864  * Process it.
865  * See if there is other interrupts pending.
866  * Repeat until there no interrupts remain.
867  */
868 static void
869 srintr(int unit)
870 {
871         struct sr_hardc *hc;
872
873         hc = &sr_hardc[unit];
874         srintr_hc(hc);
875
876         return;
877 }
878
879 void
880 srintr_hc(struct sr_hardc *hc)
881 {
882         sca_regs *sca = hc->sca;        /* MSCI register tree */
883         u_char  isr0, isr1, isr2;       /* interrupt statii captured */
884
885 #if BUGGY > 1
886         printf("sr: srintr_hc(hc=%08x)\n", hc);
887 #endif
888
889         /*
890          * Since multiple interfaces may share this interrupt, we must loop
891          * until no interrupts are still pending service.
892          */
893         while (1) {
894                 /*
895                  * Read all three interrupt status registers from the N2
896                  * card...
897                  */
898                 isr0 = SRC_GET8(hc->sca_base, sca->isr0);
899                 isr1 = SRC_GET8(hc->sca_base, sca->isr1);
900                 isr2 = SRC_GET8(hc->sca_base, sca->isr2);
901
902                 /*
903                  * If all three registers returned 0, we've finished
904                  * processing interrupts from this device, so we can quit
905                  * this loop...
906                  */
907                 if ((isr0 | isr1 | isr2) == 0)
908                         break;
909
910 #if BUGGY > 2
911                 printf("src%d: srintr_hc isr0 %x, isr1 %x, isr2 %x\n",
912                         unit, isr0, isr1, isr2);
913 #endif
914
915                 /*
916                  * Now we can dispatch the interrupts. Since we don't expect
917                  * either MSCI or timer interrupts, we'll test for DMA
918                  * interrupts first...
919                  */
920                 if (isr1)       /* DMA-initiated interrupt */
921                         sr_dmac_intr(hc, isr1);
922
923                 if (isr0)       /* serial part IRQ? */
924                         sr_msci_intr(hc, isr0);
925
926                 if (isr2)       /* timer-initiated interrupt */
927                         sr_timer_intr(hc, isr2);
928         }
929 }
930
931 /*
932  * This will only start the transmitter. It is assumed that the data
933  * is already there.
934  * It is normally called from srstart() or sr_dmac_intr().
935  */
936 static void
937 sr_xmit(struct sr_softc *sc)
938 {
939         u_short cda_value;      /* starting descriptor */
940         u_short eda_value;      /* ending descriptor */
941         struct sr_hardc *hc;
942         struct ifnet *ifp;      /* O/S Network Services */
943         dmac_channel *dmac;     /* DMA channel registers */
944
945 #if BUGGY > 0
946         printf("sr: sr_xmit( sc=%08x)\n", sc);
947 #endif
948
949         hc = sc->hc;
950         ifp = &sc->ifsppp.pp_if;
951         dmac = &hc->sca->dmac[DMAC_TXCH(sc->scachan)];
952
953         /*
954          * Get the starting and ending addresses of the chain to be
955          * transmitted and pass these on to the DMA engine on-chip.
956          */
957         cda_value = sc->block[sc->txb_next_tx].txdesc + hc->mem_pstart;
958         cda_value &= 0x00ffff;
959         eda_value = sc->block[sc->txb_next_tx].txeda + hc->mem_pstart;
960         eda_value &= 0x00ffff;
961
962         SRC_PUT16(hc->sca_base, dmac->cda, cda_value);
963         SRC_PUT16(hc->sca_base, dmac->eda, eda_value);
964
965         /*
966          * Now we'll let the DMA status register know about this change
967          */
968         SRC_PUT8(hc->sca_base, dmac->dsr, SCA_DSR_DE);
969
970         sc->xmit_busy = 1;      /* mark transmitter busy */
971
972 #if BUGGY > 2
973         printf("sr%d: XMIT  cda=%04x, eda=%4x, rcda=%08lx\n",
974                sc->unit, cda_value, eda_value,
975                sc->block[sc->txb_next_tx].txdesc + hc->mem_pstart);
976 #endif
977
978         sc->txb_next_tx++;      /* update next transmit seq# */
979
980         if (sc->txb_next_tx == SR_TX_BLOCKS)    /* handle wrap... */
981                 sc->txb_next_tx = 0;
982
983         /*
984          * Finally, we'll set a timout (which will start srwatchdog())
985          * within the O/S network services layer...
986          */
987         ifp->if_timer = 2;      /* Value in seconds. */
988 }
989
990 /*
991  * This function will be called from the upper level when a user add a
992  * packet to be send, and from the interrupt handler after a finished
993  * transmit.
994  *
995  * NOTE: it should run at spl_imp().
996  *
997  * This function only place the data in the oncard buffers. It does not
998  * start the transmition. sr_xmit() does that.
999  *
1000  * Transmitter idle state is indicated by the IFF_OACTIVE flag.
1001  * The function that clears that should ensure that the transmitter
1002  * and its DMA is in a "good" idle state.
1003  */
1004 static void
1005 srstart(struct ifnet *ifp)
1006 {
1007         struct sr_softc *sc;    /* channel control structure */
1008         struct sr_hardc *hc;    /* card control/config block */
1009         int len;                /* total length of a packet */
1010         int pkts;               /* packets placed in DPRAM */
1011         int tlen;               /* working length of pkt */
1012         u_int i;
1013         struct mbuf *mtx;       /* message buffer from O/S */
1014         u_char *txdata;         /* buffer address in DPRAM */
1015         sca_descriptor *txdesc; /* working descriptor pointr */
1016         struct buf_block *blkp;
1017
1018 #if BUGGY > 0
1019         printf("sr: srstart( ifp=%08x)\n", ifp);
1020 #endif
1021
1022         sc = ifp->if_softc;
1023         hc = sc->hc;
1024
1025         if ((ifp->if_flags & IFF_RUNNING) == 0)
1026                 return;
1027
1028         /*
1029          * It is OK to set the memory window outside the loop because all tx
1030          * buffers and descriptors are assumed to be in the same 16K window.
1031          */
1032         if (hc->mempages) {
1033                 SRC_SET_ON(hc->iobase);
1034                 SRC_SET_MEM(hc->iobase, sc->block[0].txdesc);
1035         }
1036
1037         /*
1038          * Loop to place packets into DPRAM.
1039          *
1040          * We stay in this loop until there is nothing in
1041          * the TX queue left or the tx buffers are full.
1042          */
1043 top_srstart:
1044
1045         /*
1046          * See if we have space for more packets.
1047          */
1048         if (sc->txb_inuse == SR_TX_BLOCKS) {    /* out of space? */
1049                 ifp->if_flags |= IFF_OACTIVE;   /* yes, mark active */
1050
1051                 if (hc->mempages)
1052                         SRC_SET_OFF(hc->iobase);
1053
1054 #if BUGGY > 9
1055                 printf("sr%d.srstart: sc->txb_inuse=%d; DPRAM full...\n",
1056                        sc->unit, sc->txb_inuse);
1057 #endif
1058                 return;
1059         }
1060         /*
1061          * OK, the card can take more traffic.  Let's see if there's any
1062          * pending from the system...
1063          *
1064          * NOTE:
1065          * The architecture of the networking interface doesn't
1066          * actually call us like 'write()', providing an address.  We get
1067          * started, a lot like a disk strategy routine, and we actually call
1068          * back out to the system to get traffic to send...
1069          *
1070          * NOTE:
1071          * If we were gonna run through another layer, we would use a
1072          * dispatch table to select the service we're getting a packet
1073          * from...
1074          */
1075         switch (sc->protocol) {
1076 #if NFR > 0
1077         case N2_USE_FRP:
1078                 mtx = fr_dequeue(ifp);
1079                 break;
1080 #endif
1081         case N2_USE_PPP:
1082         default:
1083                 mtx = sppp_dequeue(ifp);
1084         }
1085
1086         if (!mtx) {
1087                 if (hc->mempages)
1088                         SRC_SET_OFF(hc->iobase);
1089                 return;
1090         }
1091         /*
1092          * OK, we got a packet from the network services of the O/S. Now we
1093          * can move it into the DPRAM (under control of the descriptors) and
1094          * fire it off...
1095          */
1096         pkts = 0;
1097         i = 0;                  /* counts # of granules used */
1098
1099         blkp = &sc->block[sc->txb_new]; /* address of free granule */
1100         txdesc = (sca_descriptor *)
1101             (hc->mem_start + (blkp->txdesc & hc->winmsk));
1102
1103         txdata = (u_char *)(hc->mem_start
1104                             + (blkp->txstart & hc->winmsk));
1105
1106         /*
1107          * Now we'll try to install as many packets as possible into the
1108          * card's DP RAM buffers.
1109          */
1110         for (;;) {              /* perform actual copy of packet */
1111                 len = mtx->m_pkthdr.len;        /* length of message */
1112
1113 #if BUGGY > 1
1114                 printf("sr%d.srstart: mbuf @ %08lx, %d bytes\n",
1115                            sc->unit, mtx, len);
1116 #endif
1117
1118 #if NBPFILTER > 0
1119                 if (ifp->if_bpf)
1120                         bpf_mtap(ifp, mtx);
1121 #endif
1122
1123                 /*
1124                  * We can perform a straight copy because the tranmit
1125                  * buffers won't wrap.
1126                  */
1127                 m_copydata(mtx, 0, len, txdata);
1128
1129                 /*
1130                  * Now we know how big the message is gonna be.  We must now
1131                  * construct the descriptors to drive this message out...
1132                  */
1133                 tlen = len;
1134                 while (tlen > SR_BUF_SIZ) {     /* loop for full granules */
1135                         txdesc->stat = 0;       /* reset bits */
1136                         txdesc->len = SR_BUF_SIZ;       /* size of granule */
1137                         tlen -= SR_BUF_SIZ;
1138
1139                         txdesc++;       /* move to next dscr */
1140                         txdata += SR_BUF_SIZ;   /* adjust data addr */
1141                         i++;
1142                 }
1143
1144                 /*
1145                  * This section handles the setting of the final piece of a
1146                  * message.
1147                  */
1148                 txdesc->stat = SCA_DESC_EOM;
1149                 txdesc->len = tlen;
1150                 pkts++;
1151
1152                 /*
1153                  * prepare for subsequent packets (if any)
1154                  */
1155                 txdesc++;
1156                 txdata += SR_BUF_SIZ;   /* next mem granule */
1157                 i++;            /* count of granules */
1158
1159                 /*
1160                  * OK, we've now placed the message into the DPRAM where it
1161                  * can be transmitted.  We'll now release the message memory
1162                  * and update the statistics...
1163                  */
1164                 m_freem(mtx);
1165                 ++sc->ifsppp.pp_if.if_opackets;
1166
1167                 /*
1168                  * Check if we have space for another packet. XXX This is
1169                  * hardcoded.  A packet can't be larger than 3 buffers (3 x
1170                  * 512).
1171                  */
1172                 if ((i + 3) >= blkp->txmax) {   /* enough remains? */
1173 #if BUGGY > 9
1174                         printf("sr%d.srstart: i=%d (%d pkts); card full.\n",
1175                                sc->unit, i, pkts);
1176 #endif
1177                         break;
1178                 }
1179                 /*
1180                  * We'll pull the next message to be sent (if any)
1181                  */
1182                 switch (sc->protocol) {
1183 #if NFR > 0
1184                 case N2_USE_FRP:
1185                         mtx = fr_dequeue(ifp);
1186                         break;
1187 #endif
1188                 case N2_USE_PPP:
1189                 default:
1190                         mtx = sppp_dequeue(ifp);
1191                 }
1192
1193                 if (!mtx) {     /* no message?  We're done! */
1194 #if BUGGY > 9
1195                         printf("sr%d.srstart: pending=0, pkts=%d\n",
1196                                sc->unit, pkts);
1197 #endif
1198                         break;
1199                 }
1200         }
1201
1202         blkp->txtail = i;       /* record next free granule */
1203
1204         /*
1205          * Mark the last descriptor, so that the SCA know where to stop.
1206          */
1207         txdesc--;               /* back up to last descriptor in list */
1208         txdesc->stat |= SCA_DESC_EOT;   /* mark as end of list */
1209
1210         /*
1211          * Now we'll reset the transmit granule's descriptor address so we
1212          * can record this in the structure and fire it off w/ the DMA
1213          * processor of the serial chip...
1214          */
1215         txdesc = (sca_descriptor *)blkp->txdesc;
1216         blkp->txeda = (u_short)((u_int)&txdesc[i]);
1217
1218         sc->txb_inuse++;        /* update inuse status */
1219         sc->txb_new++;          /* new traffic wuz added */
1220
1221         if (sc->txb_new == SR_TX_BLOCKS)
1222                 sc->txb_new = 0;
1223
1224         /*
1225          * If the tranmitter wasn't marked as "busy" we will force it to be
1226          * started...
1227          */
1228         if (sc->xmit_busy == 0) {
1229                 sr_xmit(sc);
1230 #if BUGGY > 9
1231                 printf("sr%d.srstart: called sr_xmit()\n", sc->unit);
1232 #endif
1233         }
1234         goto top_srstart;
1235 }
1236
1237 /*
1238  * Handle ioctl's at the device level, though we *will* call up
1239  * a layer...
1240  */
1241 #if BUGGY > 2
1242 static int bug_splats[] = {0, 0, 0, 0, 0, 0, 0, 0};
1243 #endif
1244
1245 static int
1246 srioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1247 {
1248         int s, error, was_up, should_be_up;
1249         struct sppp *sp = (struct sppp *)ifp;
1250         struct sr_softc *sc = ifp->if_softc;
1251
1252 #if BUGGY > 0
1253         printf("sr%d: srioctl(ifp=%08x, cmd=%08x, data=%08x)\n",
1254                ifp->if_unit, ifp, cmd, data);
1255 #endif
1256
1257         was_up = ifp->if_flags & IFF_RUNNING;
1258
1259         if (cmd == SIOCSIFFLAGS) {
1260                 /*
1261                  * First, handle an apparent protocol switch
1262                  */
1263 #if NFR > 0
1264                 if (was_up == 0)/* can only happen if DOWN */
1265                         if (ifp->if_flags & IFF_LINK1)
1266                                 sc->protocol = N2_USE_FRP;
1267                         else
1268                                 sc->protocol = N2_USE_PPP;
1269 #else
1270                 sc->protocol = N2_USE_PPP;
1271                 ifp->if_flags &= ~IFF_LINK1;
1272 #endif
1273
1274                 /*
1275                  * Next we can handle minor protocol point(s)
1276                  */
1277                 if (ifp->if_flags & IFF_LINK2)
1278                         sp->pp_flags |= PP_CISCO;
1279                 else
1280                         sp->pp_flags &= ~PP_CISCO;
1281         }
1282         /*
1283          * Next, we'll allow the network service layer we've called process
1284          * the ioctl...
1285          */
1286         if ((sc->attached != 0)
1287             && (sc->attached != sc->protocol)) {
1288                 switch (sc->attached) {
1289 #if NFR > 0
1290                 case N2_USE_FRP:
1291                         fr_detach(ifp);
1292                         break;
1293 #endif
1294                 case N2_USE_PPP:
1295                 default:
1296                         sppp_detach(ifp);
1297                         sc->ifsppp.pp_flags &= ~PP_KEEPALIVE;
1298                 }
1299
1300                 sc->attached = 0;
1301         }
1302         if (sc->attached == 0) {
1303                 switch (sc->protocol) {
1304 #if NFR > 0
1305                 case N2_USE_FRP:
1306                         fr_attach(&sc->ifsppp.pp_if);
1307                         break;
1308 #endif
1309                 case N2_USE_PPP:
1310                 default:
1311                         sc->ifsppp.pp_flags |= PP_KEEPALIVE;
1312                         sppp_attach(&sc->ifsppp.pp_if);
1313
1314                         /*
1315                          * Shortcut the sppp tls/tlf actions to
1316                          * up/down events since our lower layer is
1317                          * always ready.
1318                          */
1319                         sc->ifsppp.pp_tls = sc->ifsppp.pp_up;
1320                         sc->ifsppp.pp_tlf = sc->ifsppp.pp_down;
1321                 }
1322
1323                 sc->attached = sc->protocol;
1324         }
1325         switch (sc->protocol) {
1326 #if NFR > 0
1327         case N2_USE_FRP:
1328                 error = fr_ioctl(ifp, cmd, data);
1329                 break;
1330 #endif
1331         case N2_USE_PPP:
1332         default:
1333                 error = sppp_ioctl(ifp, cmd, data);
1334         }
1335
1336 #if BUGGY > 1
1337         printf("sr%d: ioctl: ifsppp.pp_flags = %08x, if_flags %08x.\n",
1338               ifp->if_unit, ((struct sppp *)ifp)->pp_flags, ifp->if_flags);
1339 #endif
1340
1341         if (error)
1342                 return error;
1343
1344         if ((cmd != SIOCSIFFLAGS) && (cmd != SIOCSIFADDR)) {
1345 #if BUGGY > 2
1346                 if (bug_splats[sc->unit]++ < 2) {
1347                         printf("sr(%d).if_addrlist = %08x\n",
1348                                sc->unit, ifp->if_addrlist);
1349                         printf("sr(%d).if_bpf = %08x\n",
1350                                sc->unit, ifp->if_bpf);
1351                         printf("sr(%d).if_init = %08x\n",
1352                                sc->unit, ifp->if_init);
1353                         printf("sr(%d).if_output = %08x\n",
1354                                sc->unit, ifp->if_output);
1355                         printf("sr(%d).if_start = %08x\n",
1356                                sc->unit, ifp->if_start);
1357                         printf("sr(%d).if_done = %08x\n",
1358                                sc->unit, ifp->if_done);
1359                         printf("sr(%d).if_ioctl = %08x\n",
1360                                sc->unit, ifp->if_ioctl);
1361                         printf("sr(%d).if_reset = %08x\n",
1362                                sc->unit, ifp->if_reset);
1363                         printf("sr(%d).if_watchdog = %08x\n",
1364                                sc->unit, ifp->if_watchdog);
1365                 }
1366 #endif
1367                 return 0;
1368         }
1369
1370         s = splimp();
1371         should_be_up = ifp->if_flags & IFF_RUNNING;
1372
1373         if (!was_up && should_be_up) {
1374                 /*
1375                  * Interface should be up -- start it.
1376                  */
1377                 sr_up(sc);
1378                 srstart(ifp);
1379
1380                 /*
1381                  * XXX Clear the IFF_UP flag so that the link will only go
1382                  * up after sppp lcp and ipcp negotiation.
1383                  */
1384                 ifp->if_flags &= ~IFF_UP;
1385         } else if (was_up && !should_be_up) {
1386                 /*
1387                  * Interface should be down -- stop it.
1388                  */
1389                 sr_down(sc);
1390                 switch (sc->protocol) {
1391 #if NFR > 0
1392                 case N2_USE_FRP:
1393                         fr_flush(ifp);
1394                         break;
1395 #endif
1396                 case N2_USE_PPP:
1397                 default:
1398                         sppp_flush(ifp);
1399                 }
1400         }
1401         splx(s);
1402
1403 #if BUGGY > 2
1404         if (bug_splats[sc->unit]++ < 2) {
1405                 printf("sr(%d).if_addrlist = %08x\n",
1406                        sc->unit, ifp->if_addrlist);
1407                 printf("sr(%d).if_bpf = %08x\n",
1408                        sc->unit, ifp->if_bpf);
1409                 printf("sr(%d).if_init = %08x\n",
1410                        sc->unit, ifp->if_init);
1411                 printf("sr(%d).if_output = %08x\n",
1412                        sc->unit, ifp->if_output);
1413                 printf("sr(%d).if_start = %08x\n",
1414                        sc->unit, ifp->if_start);
1415                 printf("sr(%d).if_done = %08x\n",
1416                        sc->unit, ifp->if_done);
1417                 printf("sr(%d).if_ioctl = %08x\n",
1418                        sc->unit, ifp->if_ioctl);
1419                 printf("sr(%d).if_reset = %08x\n",
1420                        sc->unit, ifp->if_reset);
1421                 printf("sr(%d).if_watchdog = %08x\n",
1422                        sc->unit, ifp->if_watchdog);
1423         }
1424 #endif
1425
1426         return 0;
1427 }
1428
1429 /*
1430  * This is to catch lost tx interrupts.
1431  */
1432 static void
1433 srwatchdog(struct ifnet *ifp)
1434 {
1435         int     got_st0, got_st1, got_st3, got_dsr;
1436         struct sr_softc *sc = ifp->if_softc;
1437         struct sr_hardc *hc = sc->hc;
1438         msci_channel *msci = &hc->sca->msci[sc->scachan];
1439         dmac_channel *dmac = &sc->hc->sca->dmac[sc->scachan];
1440
1441 #if BUGGY > 0
1442         printf("srwatchdog(unit=%d)\n", unit);
1443 #endif
1444
1445         if (!(ifp->if_flags & IFF_RUNNING))
1446                 return;
1447
1448         ifp->if_oerrors++;      /* update output error count */
1449
1450         got_st0 = SRC_GET8(hc->sca_base, msci->st0);
1451         got_st1 = SRC_GET8(hc->sca_base, msci->st1);
1452         got_st3 = SRC_GET8(hc->sca_base, msci->st3);
1453         got_dsr = SRC_GET8(hc->sca_base, dmac->dsr);
1454
1455 #if     0
1456         if (ifp->if_flags & IFF_DEBUG)
1457 #endif
1458                 printf("sr%d: transmit failed, "
1459                        "ST0 %02x, ST1 %02x, ST3 %02x, DSR %02x.\n",
1460                        sc->unit,
1461                        got_st0, got_st1, got_st3, got_dsr);
1462
1463         if (SRC_GET8(hc->sca_base, msci->st1) & SCA_ST1_UDRN) {
1464                 SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_TXABORT);
1465                 SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_TXENABLE);
1466                 SRC_PUT8(hc->sca_base, msci->st1, SCA_ST1_UDRN);
1467         }
1468         sc->xmit_busy = 0;
1469         ifp->if_flags &= ~IFF_OACTIVE;
1470
1471         if (sc->txb_inuse && --sc->txb_inuse)
1472                 sr_xmit(sc);
1473
1474         srstart(ifp);   /* restart transmitter */
1475 }
1476
1477 static void
1478 sr_up(struct sr_softc *sc)
1479 {
1480         u_int *fecrp;
1481         struct sr_hardc *hc = sc->hc;
1482         sca_regs *sca = hc->sca;
1483         msci_channel *msci = &sca->msci[sc->scachan];
1484
1485 #if BUGGY > 0
1486         printf("sr_up(sc=%08x)\n", sc);
1487 #endif
1488
1489         /*
1490          * This section should really do the attach to the appropriate
1491          * system service, be it frame relay or PPP...
1492          */
1493         if (sc->attached == 0) {
1494                 switch (sc->protocol) {
1495 #if NFR > 0
1496                 case N2_USE_FRP:
1497                         fr_attach(&sc->ifsppp.pp_if);
1498                         break;
1499 #endif
1500                 case N2_USE_PPP:
1501                 default:
1502                         sc->ifsppp.pp_flags |= PP_KEEPALIVE;
1503                         sppp_attach(&sc->ifsppp.pp_if);
1504         
1505                         /*
1506                          * Shortcut the sppp tls/tlf actions to
1507                          * up/down events since our lower layer is
1508                          * always ready.
1509                          */
1510                         sc->ifsppp.pp_tls = sc->ifsppp.pp_up;
1511                         sc->ifsppp.pp_tlf = sc->ifsppp.pp_down;
1512         }
1513
1514                 sc->attached = sc->protocol;
1515         }
1516
1517         /*
1518          * Enable transmitter and receiver. Raise DTR and RTS. Enable
1519          * interrupts.
1520          *
1521          * XXX What about using AUTO mode in msci->md0 ???
1522          */
1523         SRC_PUT8(hc->sca_base, msci->ctl,
1524                  SRC_GET8(hc->sca_base, msci->ctl) & ~SCA_CTL_RTS);
1525
1526         if (sc->scachan == 0)
1527                 switch (hc->cardtype) {
1528                 case SR_CRD_N2:
1529                         outb(hc->iobase + SR_MCR,
1530                              (inb(hc->iobase + SR_MCR) & ~SR_MCR_DTR0));
1531                         break;
1532                 case SR_CRD_N2PCI:
1533                         fecrp = (u_int *)(hc->sca_base + SR_FECR);
1534                         *fecrp &= ~SR_FECR_DTR0;
1535                         break;
1536                 }
1537         else
1538                 switch (hc->cardtype) {
1539                 case SR_CRD_N2:
1540                         outb(hc->iobase + SR_MCR,
1541                              (inb(hc->iobase + SR_MCR) & ~SR_MCR_DTR1));
1542                         break;
1543                 case SR_CRD_N2PCI:
1544                         fecrp = (u_int *)(hc->sca_base + SR_FECR);
1545                         *fecrp &= ~SR_FECR_DTR1;
1546                         break;
1547                 }
1548
1549         if (sc->scachan == 0) {
1550                 SRC_PUT8(hc->sca_base, sca->ier0,
1551                          SRC_GET8(hc->sca_base, sca->ier0) | 0x000F);
1552                 SRC_PUT8(hc->sca_base, sca->ier1,
1553                          SRC_GET8(hc->sca_base, sca->ier1) | 0x000F);
1554         } else {
1555                 SRC_PUT8(hc->sca_base, sca->ier0,
1556                          SRC_GET8(hc->sca_base, sca->ier0) | 0x00F0);
1557                 SRC_PUT8(hc->sca_base, sca->ier1,
1558                          SRC_GET8(hc->sca_base, sca->ier1) | 0x00F0);
1559         }
1560
1561         SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_RXENABLE);
1562         inb(hc->iobase);        /* XXX slow it down a bit. */
1563         SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_TXENABLE);
1564
1565 #ifdef USE_MODEMCK
1566         if (sr_watcher == 0)
1567                 sr_modemck(NULL);
1568 #endif
1569 }
1570
1571 static void
1572 sr_down(struct sr_softc *sc)
1573 {
1574         u_int *fecrp;
1575         struct sr_hardc *hc = sc->hc;
1576         sca_regs *sca = hc->sca;
1577         msci_channel *msci = &sca->msci[sc->scachan];
1578
1579 #if BUGGY > 0
1580         printf("sr_down(sc=%08x)\n", sc);
1581 #endif
1582
1583         /*
1584          * Disable transmitter and receiver. Lower DTR and RTS. Disable
1585          * interrupts.
1586          */
1587         SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_RXDISABLE);
1588         inb(hc->iobase);        /* XXX slow it down a bit. */
1589         SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_TXDISABLE);
1590
1591         SRC_PUT8(hc->sca_base, msci->ctl,
1592                  SRC_GET8(hc->sca_base, msci->ctl) | SCA_CTL_RTS);
1593
1594         if (sc->scachan == 0)
1595                 switch (hc->cardtype) {
1596                 case SR_CRD_N2:
1597                         outb(hc->iobase + SR_MCR,
1598                              (inb(hc->iobase + SR_MCR) | SR_MCR_DTR0));
1599                         break;
1600                 case SR_CRD_N2PCI:
1601                         fecrp = (u_int *)(hc->sca_base + SR_FECR);
1602                         *fecrp |= SR_FECR_DTR0;
1603                         break;
1604                 }
1605         else
1606                 switch (hc->cardtype) {
1607                 case SR_CRD_N2:
1608                         outb(hc->iobase + SR_MCR,
1609                              (inb(hc->iobase + SR_MCR) | SR_MCR_DTR1));
1610                         break;
1611                 case SR_CRD_N2PCI:
1612                         fecrp = (u_int *)(hc->sca_base + SR_FECR);
1613                         *fecrp |= SR_FECR_DTR1;
1614                         break;
1615                 }
1616
1617         if (sc->scachan == 0) {
1618                 SRC_PUT8(hc->sca_base, sca->ier0,
1619                          SRC_GET8(hc->sca_base, sca->ier0) & ~0x0F);
1620                 SRC_PUT8(hc->sca_base, sca->ier1,
1621                          SRC_GET8(hc->sca_base, sca->ier1) & ~0x0F);
1622         } else {
1623                 SRC_PUT8(hc->sca_base, sca->ier0,
1624                          SRC_GET8(hc->sca_base, sca->ier0) & ~0xF0);
1625                 SRC_PUT8(hc->sca_base, sca->ier1,
1626                          SRC_GET8(hc->sca_base, sca->ier1) & ~0xF0);
1627         }
1628
1629         /*
1630          * This section does the detach from the currently configured net
1631          * service, be it frame relay or PPP...
1632          */
1633         switch (sc->protocol) {
1634 #if NFR > 0
1635         case N2_USE_FRP:
1636                 fr_detach(&sc->ifsppp.pp_if);
1637                 break;
1638 #endif
1639         case N2_USE_PPP:
1640         default:
1641                 sppp_detach(&sc->ifsppp.pp_if);
1642         }
1643
1644         sc->attached = 0;
1645 }
1646
1647 /*
1648  * Initialize the card, allocate memory for the sr_softc structures
1649  * and fill in the pointers.
1650  */
1651 static void
1652 src_init(struct sr_hardc *hc)
1653 {
1654         struct sr_softc *sc = hc->sc;
1655         int x;
1656         u_int chanmem;
1657         u_int bufmem;
1658         u_int next;
1659         u_int descneeded;
1660
1661 #if BUGGY > 0
1662         printf("src_init(hc=%08x)\n", hc);
1663 #endif
1664
1665         chanmem = hc->memsize / hc->numports;
1666         next = 0;
1667
1668         for (x = 0; x < hc->numports; x++, sc++) {
1669                 int blk;
1670
1671                 for (blk = 0; blk < SR_TX_BLOCKS; blk++) {
1672                         sc->block[blk].txdesc = next;
1673                         bufmem = (16 * 1024) / SR_TX_BLOCKS;
1674                         descneeded = bufmem / SR_BUF_SIZ;
1675
1676                         sc->block[blk].txstart = sc->block[blk].txdesc
1677                             + ((((descneeded * sizeof(sca_descriptor))
1678                                  / SR_BUF_SIZ) + 1)
1679                                * SR_BUF_SIZ);
1680
1681                         sc->block[blk].txend = next + bufmem;
1682                         sc->block[blk].txmax =
1683                             (sc->block[blk].txend - sc->block[blk].txstart)
1684                             / SR_BUF_SIZ;
1685                         next += bufmem;
1686
1687 #if BUGGY > 2
1688                         printf("sr%d: blk %d: txdesc %08x, txstart %08x\n",
1689                                sc->unit, blk,
1690                                sc->block[blk].txdesc, sc->block[blk].txstart);
1691 #endif
1692                 }
1693
1694                 sc->rxdesc = next;
1695                 bufmem = chanmem - (bufmem * SR_TX_BLOCKS);
1696                 descneeded = bufmem / SR_BUF_SIZ;
1697                 sc->rxstart = sc->rxdesc +
1698                     ((((descneeded * sizeof(sca_descriptor)) /
1699                        SR_BUF_SIZ) + 1) * SR_BUF_SIZ);
1700                 sc->rxend = next + bufmem;
1701                 sc->rxmax = (sc->rxend - sc->rxstart) / SR_BUF_SIZ;
1702                 next += bufmem;
1703         }
1704 }
1705
1706 /*
1707  * The things done here are channel independent.
1708  *
1709  * Configure the sca waitstates.
1710  * Configure the global interrupt registers.
1711  * Enable master dma enable.
1712  */
1713 static void
1714 sr_init_sca(struct sr_hardc *hc)
1715 {
1716         sca_regs *sca = hc->sca;
1717
1718 #if BUGGY > 0
1719         printf("sr_init_sca(hc=%08x)\n", hc);
1720 #endif
1721
1722         /*
1723          * Do the wait registers. Set everything to 0 wait states.
1724          */
1725         SRC_PUT8(hc->sca_base, sca->pabr0, 0);
1726         SRC_PUT8(hc->sca_base, sca->pabr1, 0);
1727         SRC_PUT8(hc->sca_base, sca->wcrl, 0);
1728         SRC_PUT8(hc->sca_base, sca->wcrm, 0);
1729         SRC_PUT8(hc->sca_base, sca->wcrh, 0);
1730
1731         /*
1732          * Configure the interrupt registers. Most are cleared until the
1733          * interface is configured.
1734          */
1735         SRC_PUT8(hc->sca_base, sca->ier0, 0x00);        /* MSCI interrupts. */
1736         SRC_PUT8(hc->sca_base, sca->ier1, 0x00);        /* DMAC interrupts */
1737         SRC_PUT8(hc->sca_base, sca->ier2, 0x00);        /* TIMER interrupts. */
1738         SRC_PUT8(hc->sca_base, sca->itcr, 0x00);        /* Use ivr and no intr
1739                                                          * ack */
1740         SRC_PUT8(hc->sca_base, sca->ivr, 0x40); /* Interrupt vector. */
1741         SRC_PUT8(hc->sca_base, sca->imvr, 0x40);
1742
1743         /*
1744          * Configure the timers. XXX Later
1745          */
1746
1747         /*
1748          * Set the DMA channel priority to rotate between all four channels.
1749          *
1750          * Enable all dma channels.
1751          */
1752         SRC_PUT8(hc->sca_base, sca->pcr, SCA_PCR_PR2);
1753         SRC_PUT8(hc->sca_base, sca->dmer, SCA_DMER_EN);
1754 }
1755
1756 /*
1757  * Configure the msci
1758  *
1759  * NOTE: The serial port configuration is hardcoded at the moment.
1760  */
1761 static void
1762 sr_init_msci(struct sr_softc *sc)
1763 {
1764         int portndx;            /* on-board port number */
1765         u_int mcr_v;            /* contents of modem control */
1766         u_int *fecrp;           /* pointer for PCI's MCR i/o */
1767         struct sr_hardc *hc = sc->hc;
1768         msci_channel *msci = &hc->sca->msci[sc->scachan];
1769 #ifdef N2_TEST_SPEED
1770         int br_v;               /* contents for BR divisor */
1771         int etcndx;             /* index into ETC table */
1772         int fifo_v, gotspeed;   /* final tabled speed found */
1773         int tmc_v;              /* timer control register */
1774         int wanted;             /* speed (bitrate) wanted... */
1775         struct rate_line *rtp;
1776 #endif
1777
1778         portndx = sc->scachan;
1779
1780 #if BUGGY > 0
1781         printf("sr: sr_init_msci( sc=%08x)\n", sc);
1782 #endif
1783
1784         SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_RESET);
1785         SRC_PUT8(hc->sca_base, msci->md0, SCA_MD0_CRC_1 |
1786                  SCA_MD0_CRC_CCITT |
1787                  SCA_MD0_CRC_ENABLE |
1788                  SCA_MD0_MODE_HDLC);
1789         SRC_PUT8(hc->sca_base, msci->md1, SCA_MD1_NOADDRCHK);
1790         SRC_PUT8(hc->sca_base, msci->md2, SCA_MD2_DUPLEX | SCA_MD2_NRZ);
1791
1792         /*
1793          * According to the manual I should give a reset after changing the
1794          * mode registers.
1795          */
1796         SRC_PUT8(hc->sca_base, msci->cmd, SCA_CMD_RXRESET);
1797         SRC_PUT8(hc->sca_base, msci->ctl, SCA_CTL_IDLPAT |
1798                  SCA_CTL_UDRNC |
1799                  SCA_CTL_RTS);
1800
1801         /*
1802          * XXX Later we will have to support different clock settings.
1803          */
1804         switch (sc->clk_cfg) {
1805         default:
1806 #if BUGGY > 0
1807                 printf("sr%: clk_cfg=%08x, selected default clock.\n",
1808                        portndx, sc->clk_cfg);
1809 #endif
1810                 /* FALLTHROUGH */
1811         case SR_FLAGS_EXT_CLK:
1812                 /*
1813                  * For now all interfaces are programmed to use the RX clock
1814                  * for the TX clock.
1815                  */
1816
1817 #if BUGGY > 0
1818                 printf("sr%d: External Clock Selected.\n", portndx);
1819 #endif
1820
1821                 SRC_PUT8(hc->sca_base, msci->rxs, 0);
1822                 SRC_PUT8(hc->sca_base, msci->txs, 0);
1823                 break;
1824
1825         case SR_FLAGS_EXT_SEP_CLK:
1826 #if BUGGY > 0
1827                 printf("sr%d: Split Clocking Selected.\n", portndx);
1828 #endif
1829
1830 #if     1
1831                 SRC_PUT8(hc->sca_base, msci->rxs, 0);
1832                 SRC_PUT8(hc->sca_base, msci->txs, 0);
1833 #else
1834                 SRC_PUT8(hc->sca_base, msci->rxs,
1835                          SCA_RXS_CLK_RXC0 | SCA_RXS_DIV1);
1836
1837                 /*
1838                  * We need to configure the internal bit clock for the
1839                  * transmitter's channel...
1840                  */
1841                 SRC_PUT8(hc->sca_base, msci->txs,
1842                          SCA_TXS_CLK_RX | SCA_TXS_DIV1);
1843 #endif
1844                 break;
1845
1846         case SR_FLAGS_INT_CLK:
1847 #if BUGGY > 0
1848                 printf("sr%d: Internal Clocking selected.\n", portndx);
1849 #endif
1850
1851                 /*
1852                  * XXX I do need some code to set the baud rate here!
1853                  */
1854 #ifdef N2_TEST_SPEED
1855                 switch (hc->cardtype) {
1856                 case SR_CRD_N2PCI:
1857                         fecrp = (u_int *)(hc->sca_base + SR_FECR);
1858                         mcr_v = *fecrp;
1859                         etcndx = 2;
1860                         break;
1861                 case SR_CRD_N2:
1862                 default:
1863                         mcr_v = inb(hc->iobase + SR_MCR);
1864                         etcndx = 0;
1865                 }
1866
1867                 fifo_v = 0x10;  /* stolen from Linux version */
1868
1869                 /*
1870                  * search for appropriate speed in table, don't calc it:
1871                  */
1872                 wanted = sr_test_speed[portndx];
1873                 rtp = &n2_rates[0];     /* point to first table item */
1874
1875                 while ((rtp->target > 0)        /* search table for speed */
1876                        &&(rtp->target != wanted))
1877                         rtp++;
1878
1879                 /*
1880                  * We've searched the table for a matching speed.  If we've
1881                  * found the correct rate line, we'll get the pre-calc'd
1882                  * values for the TMC and baud rate divisor for subsequent
1883                  * use...
1884                  */
1885                 if (rtp->target > 0) {  /* use table-provided values */
1886                         gotspeed = wanted;
1887                         tmc_v = rtp->tmc_reg;
1888                         br_v = rtp->br_reg;
1889                 } else {        /* otherwise assume 1MBit comm rate */
1890                         gotspeed = 10000;
1891                         tmc_v = 5;
1892                         br_v = 1;
1893                 }
1894
1895                 /*
1896                  * Now we mask in the enable clock output for the MCR:
1897                  */
1898                 mcr_v |= etc0vals[etcndx + portndx];
1899
1900                 /*
1901                  * Now we'll program the registers with these speed- related
1902                  * contents...
1903                  */
1904                 SRC_PUT8(hc->sca_base, msci->tmc, tmc_v);
1905                 SRC_PUT8(hc->sca_base, msci->trc0, fifo_v);
1906                 SRC_PUT8(hc->sca_base, msci->rxs, SCA_RXS_CLK_INT + br_v);
1907                 SRC_PUT8(hc->sca_base, msci->txs, SCA_TXS_CLK_INT + br_v);
1908
1909                 switch (hc->cardtype) {
1910                 case SR_CRD_N2PCI:
1911                         *fecrp = mcr_v;
1912                         break;
1913                 case SR_CRD_N2:
1914                 default:
1915                         outb(hc->iobase + SR_MCR, mcr_v);
1916                 }
1917
1918 #if BUGGY > 0
1919                 if (wanted != gotspeed)
1920                         printf("sr%d: Speed wanted=%d, found=%d\n",
1921                                wanted, gotspeed);
1922
1923                 printf("sr%d: Internal Clock %dx100 BPS, tmc=%d, div=%d\n",
1924                        portndx, gotspeed, tmc_v, br_v);
1925 #endif
1926 #else
1927                 SRC_PUT8(hc->sca_base, msci->rxs,
1928                          SCA_RXS_CLK_INT | SCA_RXS_DIV1);
1929                 SRC_PUT8(hc->sca_base, msci->txs,
1930                          SCA_TXS_CLK_INT | SCA_TXS_DIV1);
1931
1932                 SRC_PUT8(hc->sca_base, msci->tmc, 5);
1933
1934                 if (portndx == 0)
1935                         switch (hc->cardtype) {
1936                         case SR_CRD_N2PCI:
1937                                 fecrp = (u_int *)(hc->sca_base + SR_FECR);
1938                                 *fecrp |= SR_FECR_ETC0;
1939                                 break;
1940                         case SR_CRD_N2:
1941                         default:
1942                                 mcr_v = inb(hc->iobase + SR_MCR);
1943                                 mcr_v |= SR_MCR_ETC0;
1944                                 outb(hc->iobase + SR_MCR, mcr_v);
1945                         }
1946                 else
1947                         switch (hc->cardtype) {
1948                         case SR_CRD_N2:
1949                                 mcr_v = inb(hc->iobase + SR_MCR);
1950                                 mcr_v |= SR_MCR_ETC1;
1951                                 outb(hc->iobase + SR_MCR, mcr_v);
1952                                 break;
1953                         case SR_CRD_N2PCI:
1954                                 fecrp = (u_int *)(hc->sca_base + SR_FECR);
1955                                 *fecrp |= SR_FECR_ETC1;
1956                                 break;
1957                         }
1958 #endif
1959         }
1960
1961         /*
1962          * XXX Disable all interrupts for now. I think if you are using the
1963          * dmac you don't use these interrupts.
1964          */
1965         SRC_PUT8(hc->sca_base, msci->ie0, 0);
1966         SRC_PUT8(hc->sca_base, msci->ie1, 0x0C);
1967         SRC_PUT8(hc->sca_base, msci->ie2, 0);
1968         SRC_PUT8(hc->sca_base, msci->fie, 0);
1969
1970         SRC_PUT8(hc->sca_base, msci->sa0, 0);
1971         SRC_PUT8(hc->sca_base, msci->sa1, 0);
1972
1973         SRC_PUT8(hc->sca_base, msci->idl, 0x7E);        /* set flags value */
1974
1975         SRC_PUT8(hc->sca_base, msci->rrc, 0x0E);
1976         SRC_PUT8(hc->sca_base, msci->trc0, 0x10);
1977         SRC_PUT8(hc->sca_base, msci->trc1, 0x1F);
1978 }
1979
1980 /*
1981  * Configure the rx dma controller.
1982  */
1983 static void
1984 sr_init_rx_dmac(struct sr_softc *sc)
1985 {
1986         struct sr_hardc *hc;
1987         dmac_channel *dmac;
1988         sca_descriptor *rxd;
1989         u_int cda_v, sarb_v, rxbuf, rxda, rxda_d;
1990
1991 #if BUGGY > 0
1992         printf("sr_init_rx_dmac(sc=%08x)\n", sc);
1993 #endif
1994
1995         hc = sc->hc;
1996         dmac = &hc->sca->dmac[DMAC_RXCH(sc->scachan)];
1997
1998         if (hc->mempages)
1999                 SRC_SET_MEM(hc->iobase, sc->rxdesc);
2000
2001         /*
2002          * This phase initializes the contents of the descriptor table
2003          * needed to construct a circular buffer...
2004          */
2005         rxd = (sca_descriptor *)(hc->mem_start + (sc->rxdesc & hc->winmsk));
2006         rxda_d = (u_int) hc->mem_start - (sc->rxdesc & ~hc->winmsk);
2007
2008         for (rxbuf = sc->rxstart;
2009              rxbuf < sc->rxend;
2010              rxbuf += SR_BUF_SIZ, rxd++) {
2011                 /*
2012                  * construct the circular chain...
2013                  */
2014                 rxda = (u_int) & rxd[1] - rxda_d + hc->mem_pstart;
2015                 rxd->cp = (u_short)(rxda & 0xffff);
2016
2017                 /*
2018                  * set the on-card buffer address...
2019                  */
2020                 rxd->bp = (u_short)((rxbuf + hc->mem_pstart) & 0xffff);
2021                 rxd->bpb = (u_char)(((rxbuf + hc->mem_pstart) >> 16) & 0xff);
2022
2023                 rxd->len = 0;   /* bytes resident w/in granule */
2024                 rxd->stat = 0xff;       /* The sca write here when finished */
2025         }
2026
2027         /*
2028          * heal the chain so that the last entry points to the first...
2029          */
2030         rxd--;
2031         rxd->cp = (u_short)((sc->rxdesc + hc->mem_pstart) & 0xffff);
2032
2033         /*
2034          * reset the reception handler's index...
2035          */
2036         sc->rxhind = 0;
2037
2038         /*
2039          * We'll now configure the receiver's DMA logic...
2040          */
2041         SRC_PUT8(hc->sca_base, dmac->dsr, 0);   /* Disable DMA transfer */
2042         SRC_PUT8(hc->sca_base, dmac->dcr, SCA_DCR_ABRT);
2043
2044         /* XXX maybe also SCA_DMR_CNTE */
2045         SRC_PUT8(hc->sca_base, dmac->dmr, SCA_DMR_TMOD | SCA_DMR_NF);
2046         SRC_PUT16(hc->sca_base, dmac->bfl, SR_BUF_SIZ);
2047
2048         cda_v = (u_short)((sc->rxdesc + hc->mem_pstart) & 0xffff);
2049         sarb_v = (u_char)(((sc->rxdesc + hc->mem_pstart) >> 16) & 0xff);
2050
2051         SRC_PUT16(hc->sca_base, dmac->cda, cda_v);
2052         SRC_PUT8(hc->sca_base, dmac->sarb, sarb_v);
2053
2054         rxd = (sca_descriptor *)sc->rxstart;
2055
2056         SRC_PUT16(hc->sca_base, dmac->eda,
2057                   (u_short)((u_int) & rxd[sc->rxmax - 1] & 0xffff));
2058
2059         SRC_PUT8(hc->sca_base, dmac->dir, 0xF0);
2060
2061
2062         SRC_PUT8(hc->sca_base, dmac->dsr, SCA_DSR_DE);  /* Enable DMA */
2063 }
2064
2065 /*
2066  * Configure the TX DMA descriptors.
2067  * Initialize the needed values and chain the descriptors.
2068  */
2069 static void
2070 sr_init_tx_dmac(struct sr_softc *sc)
2071 {
2072         int blk;
2073         u_int txbuf, txda, txda_d;
2074         struct sr_hardc *hc;
2075         sca_descriptor *txd;
2076         dmac_channel *dmac;
2077         struct buf_block *blkp;
2078         u_int x;
2079         u_int sarb_v;
2080
2081 #if BUGGY > 0
2082         printf("sr_init_tx_dmac(sc=%08x)\n", sc);
2083 #endif
2084
2085         hc = sc->hc;
2086         dmac = &hc->sca->dmac[DMAC_TXCH(sc->scachan)];
2087
2088         if (hc->mempages)
2089                 SRC_SET_MEM(hc->iobase, sc->block[0].txdesc);
2090
2091         /*
2092          * Initialize the array of descriptors for transmission
2093          */
2094         for (blk = 0; blk < SR_TX_BLOCKS; blk++) {
2095                 blkp = &sc->block[blk];
2096                 txd = (sca_descriptor *)(hc->mem_start
2097                                          + (blkp->txdesc & hc->winmsk));
2098                 txda_d = (u_int) hc->mem_start
2099                     - (blkp->txdesc & ~hc->winmsk);
2100
2101                 x = 0;
2102                 txbuf = blkp->txstart;
2103                 for (; txbuf < blkp->txend; txbuf += SR_BUF_SIZ, txd++) {
2104                         txda = (u_int) & txd[1] - txda_d + hc->mem_pstart;
2105                         txd->cp = (u_short)(txda & 0xffff);
2106
2107                         txd->bp = (u_short)((txbuf + hc->mem_pstart)
2108                                             & 0xffff);
2109                         txd->bpb = (u_char)(((txbuf + hc->mem_pstart) >> 16)
2110                                             & 0xff);
2111                         txd->len = 0;
2112                         txd->stat = 0;
2113                         x++;
2114                 }
2115
2116                 txd--;
2117                 txd->cp = (u_short)((blkp->txdesc + hc->mem_pstart)
2118                                     & 0xffff);
2119
2120                 blkp->txtail = (u_int)txd - (u_int)hc->mem_start;
2121         }
2122
2123         SRC_PUT8(hc->sca_base, dmac->dsr, 0);   /* Disable DMA */
2124         SRC_PUT8(hc->sca_base, dmac->dcr, SCA_DCR_ABRT);
2125         SRC_PUT8(hc->sca_base, dmac->dmr, SCA_DMR_TMOD | SCA_DMR_NF);
2126         SRC_PUT8(hc->sca_base, dmac->dir,
2127                  SCA_DIR_EOT | SCA_DIR_BOF | SCA_DIR_COF);
2128
2129         sarb_v = (sc->block[0].txdesc + hc->mem_pstart) >> 16;
2130         sarb_v &= 0x00ff;
2131
2132         SRC_PUT8(hc->sca_base, dmac->sarb, (u_char) sarb_v);
2133 }
2134
2135 /*
2136  * Look through the descriptors to see if there is a complete packet
2137  * available. Stop if we get to where the sca is busy.
2138  *
2139  * Return the length and status of the packet.
2140  * Return nonzero if there is a packet available.
2141  *
2142  * NOTE:
2143  * It seems that we get the interrupt a bit early. The updateing of
2144  * descriptor values is not always completed when this is called.
2145  */
2146 static int
2147 sr_packet_avail(struct sr_softc *sc, int *len, u_char *rxstat)
2148 {
2149         int granules;   /* count of granules in pkt */
2150         int wki, wko;
2151         struct sr_hardc *hc;
2152         sca_descriptor *rxdesc; /* current descriptor */
2153         sca_descriptor *endp;   /* ending descriptor */
2154         sca_descriptor *cda;    /* starting descriptor */
2155
2156         hc = sc->hc;            /* get card's information */
2157
2158         /*
2159          * set up starting descriptor by pulling that info from the DMA half
2160          * of the HD chip...
2161          */
2162         wki = DMAC_RXCH(sc->scachan);
2163         wko = SRC_GET16(hc->sca_base, hc->sca->dmac[wki].cda);
2164
2165         cda = (sca_descriptor *)(hc->mem_start + (wko & hc->winmsk));
2166
2167 #if BUGGY > 1
2168         printf("sr_packet_avail(): wki=%d, wko=%04x, cda=%08x\n",
2169                wki, wko, cda);
2170 #endif
2171
2172         /*
2173          * open the appropriate memory window and set our expectations...
2174          */
2175         if (hc->mempages) {
2176                 SRC_SET_MEM(hc->iobase, sc->rxdesc);
2177                 SRC_SET_ON(hc->iobase);
2178         }
2179         rxdesc = (sca_descriptor *)
2180             (hc->mem_start + (sc->rxdesc & hc->winmsk));
2181         endp = rxdesc;
2182         rxdesc = &rxdesc[sc->rxhind];
2183         endp = &endp[sc->rxmax];
2184
2185         *len = 0;               /* reset result total length */
2186         granules = 0;           /* reset count of granules */
2187
2188         /*
2189          * This loop will scan descriptors, but it *will* puke up if we wrap
2190          * around to our starting point...
2191          */
2192         while (rxdesc != cda) {
2193                 *len += rxdesc->len;    /* increment result length */
2194                 granules++;
2195
2196                 /*
2197                  * If we hit a valid packet's completion we'll know we've
2198                  * got a live one, and that we can deliver the packet.
2199                  * Since we're only allowed to report a packet available,
2200                  * somebody else does that...
2201                  */
2202                 if (rxdesc->stat & SCA_DESC_EOM) {      /* End Of Message */
2203                         *rxstat = rxdesc->stat; /* return closing */
2204 #if BUGGY > 0
2205                         printf("sr%d: PKT AVAIL len %d, %x, bufs %u.\n",
2206                                sc->unit, *len, *rxstat, granules);
2207 #endif
2208                         return 1;       /* indicate success */
2209                 }
2210                 /*
2211                  * OK, this packet take up multiple granules.  Move on to
2212                  * the next descriptor so we can consider it...
2213                  */
2214                 rxdesc++;
2215
2216                 if (rxdesc == endp)     /* recognize & act on wrap point */
2217                         rxdesc = (sca_descriptor *)
2218                             (hc->mem_start + (sc->rxdesc & hc->winmsk));
2219         }
2220
2221         /*
2222          * Nothing found in the DPRAM.  Let the caller know...
2223          */
2224         *len = 0;
2225         *rxstat = 0;
2226
2227         return 0;
2228 }
2229
2230 /*
2231  * Copy a packet from the on card memory into a provided mbuf.
2232  * Take into account that buffers wrap and that a packet may
2233  * be larger than a buffer.
2234  */
2235 static void
2236 sr_copy_rxbuf(struct mbuf *m, struct sr_softc *sc, int len)
2237 {
2238         struct sr_hardc *hc;
2239         sca_descriptor *rxdesc;
2240         u_int rxdata;
2241         u_int rxmax;
2242         u_int off = 0;
2243         u_int tlen;
2244
2245 #if BUGGY > 0
2246         printf("sr_copy_rxbuf(m=%08x,sc=%08x,len=%d)\n",
2247                m, sc, len);
2248 #endif
2249
2250         hc = sc->hc;
2251
2252         rxdata = sc->rxstart + (sc->rxhind * SR_BUF_SIZ);
2253         rxmax = sc->rxstart + (sc->rxmax * SR_BUF_SIZ);
2254
2255         rxdesc = (sca_descriptor *)
2256             (hc->mem_start + (sc->rxdesc & hc->winmsk));
2257         rxdesc = &rxdesc[sc->rxhind];
2258
2259         /*
2260          * Using the count of bytes in the received packet, we decrement it
2261          * for each granule (controller by an SCA descriptor) to control the
2262          * looping...
2263          */
2264         while (len) {
2265                 /*
2266                  * tlen gets the length of *this* granule... ...which is
2267                  * then copied to the target buffer.
2268                  */
2269                 tlen = (len < SR_BUF_SIZ) ? len : SR_BUF_SIZ;
2270
2271                 if (hc->mempages)
2272                         SRC_SET_MEM(hc->iobase, rxdata);
2273
2274                 bcopy(hc->mem_start + (rxdata & hc->winmsk),
2275                       mtod(m, caddr_t) +off,
2276                       tlen);
2277
2278                 off += tlen;
2279                 len -= tlen;
2280
2281                 /*
2282                  * now, return to the descriptor's window in DPRAM and reset
2283                  * the descriptor we've just suctioned...
2284                  */
2285                 if (hc->mempages)
2286                         SRC_SET_MEM(hc->iobase, sc->rxdesc);
2287
2288                 rxdesc->len = 0;
2289                 rxdesc->stat = 0xff;
2290
2291                 /*
2292                  * Move on to the next granule.  If we've any remaining
2293                  * bytes to process we'll just continue in our loop...
2294                  */
2295                 rxdata += SR_BUF_SIZ;
2296                 rxdesc++;
2297
2298                 if (rxdata == rxmax) {  /* handle the wrap point */
2299                         rxdata = sc->rxstart;
2300                         rxdesc = (sca_descriptor *)
2301                             (hc->mem_start + (sc->rxdesc & hc->winmsk));
2302                 }
2303         }
2304 }
2305
2306 /*
2307  * If single is set, just eat a packet. Otherwise eat everything up to
2308  * where cda points. Update pointers to point to the next packet.
2309  *
2310  * This handles "flushing" of a packet as received...
2311  *
2312  * If the "single" parameter is zero, all pending reeceive traffic will
2313  * be flushed out of existence.  A non-zero value will only drop the
2314  * *next* (currently) pending packet...
2315  */
2316 static void
2317 sr_eat_packet(struct sr_softc *sc, int single)
2318 {
2319         struct sr_hardc *hc;
2320         sca_descriptor *rxdesc; /* current descriptor being eval'd */
2321         sca_descriptor *endp;   /* last descriptor in chain */
2322         sca_descriptor *cda;    /* current start point */
2323         u_int loopcnt = 0;      /* count of packets flushed ??? */
2324         u_char stat;            /* captured status byte from descr */
2325
2326         hc = sc->hc;
2327         cda = (sca_descriptor *)(hc->mem_start +
2328                                  (SRC_GET16(hc->sca_base,
2329                                   hc->sca->dmac[DMAC_RXCH(sc->scachan)].cda) &
2330                                   hc->winmsk));
2331
2332         /*
2333          * loop until desc->stat == (0xff || EOM) Clear the status and
2334          * length in the descriptor. Increment the descriptor.
2335          */
2336         if (hc->mempages)
2337                 SRC_SET_MEM(hc->iobase, sc->rxdesc);
2338
2339         rxdesc = (sca_descriptor *)
2340             (hc->mem_start + (sc->rxdesc & hc->winmsk));
2341         endp = rxdesc;
2342         rxdesc = &rxdesc[sc->rxhind];
2343         endp = &endp[sc->rxmax];
2344
2345         /*
2346          * allow loop, but abort it if we wrap completely...
2347          */
2348         while (rxdesc != cda) {
2349                 loopcnt++;
2350
2351                 if (loopcnt > sc->rxmax) {
2352                         printf("sr%d: eat pkt %d loop, cda %x, "
2353                                "rxdesc %x, stat %x.\n",
2354                                sc->unit, loopcnt, (u_int) cda, (u_int) rxdesc,
2355                                rxdesc->stat);
2356                         break;
2357                 }
2358                 stat = rxdesc->stat;
2359
2360                 rxdesc->len = 0;
2361                 rxdesc->stat = 0xff;
2362
2363                 rxdesc++;
2364                 sc->rxhind++;
2365
2366                 if (rxdesc == endp) {
2367                         rxdesc = (sca_descriptor *)
2368                             (hc->mem_start + (sc->rxdesc & hc->winmsk));
2369                         sc->rxhind = 0;
2370                 }
2371                 if (single && (stat == SCA_DESC_EOM))
2372                         break;
2373         }
2374
2375         /*
2376          * Update the eda to the previous descriptor.
2377          */
2378         rxdesc = (sca_descriptor *)sc->rxdesc;
2379         rxdesc = &rxdesc[(sc->rxhind + sc->rxmax - 2) % sc->rxmax];
2380
2381         SRC_PUT16(hc->sca_base,
2382                   hc->sca->dmac[DMAC_RXCH(sc->scachan)].eda,
2383                   (u_short)((u_int)(rxdesc + hc->mem_pstart) & 0xffff));
2384 }
2385
2386 /*
2387  * While there is packets available in the rx buffer, read them out
2388  * into mbufs and ship them off.
2389  */
2390 static void
2391 sr_get_packets(struct sr_softc *sc)
2392 {
2393         u_char rxstat;          /* acquired status byte */
2394         int i;
2395         int pkts;               /* count of packets found */
2396         int rxndx;              /* rcv buffer index */
2397         int tries;              /* settling time counter */
2398         u_int len;              /* length of pending packet */
2399         struct sr_hardc *hc;    /* card-level information */
2400         sca_descriptor *rxdesc; /* descriptor in memory */
2401         struct ifnet *ifp;      /* network intf ctl table */
2402         struct mbuf *m = NULL;  /* message buffer */
2403
2404 #if BUGGY > 0
2405         printf("sr_get_packets(sc=%08x)\n", sc);
2406 #endif
2407
2408         hc = sc->hc;
2409         ifp = &sc->ifsppp.pp_if;
2410
2411         if (hc->mempages) {
2412                 SRC_SET_MEM(hc->iobase, sc->rxdesc);
2413                 SRC_SET_ON(hc->iobase); /* enable shared memory */
2414         }
2415         pkts = 0;               /* reset count of found packets */
2416
2417         /*
2418          * for each complete packet in the receiving pool, process each
2419          * packet...
2420          */
2421         while (sr_packet_avail(sc, &len, &rxstat)) {    /* packet pending? */
2422                 /*
2423                  * I have seen situations where we got the interrupt but the
2424                  * status value wasn't deposited.  This code should allow
2425                  * the status byte's value to settle...
2426                  */
2427
2428                 tries = 5;
2429
2430                 while ((rxstat == 0x00ff)
2431                        && --tries)
2432                         sr_packet_avail(sc, &len, &rxstat);
2433
2434 #if BUGGY > 1
2435                 printf("sr_packet_avail() returned len=%d, rxstat=%02ux\n",
2436                        len, rxstat);
2437 #endif
2438
2439                 pkts++;
2440
2441                 /*
2442                  * OK, we've settled the incoming message status. We can now
2443                  * process it...
2444                  */
2445                 if (((rxstat & SCA_DESC_ERRORS) == 0) && (len < MCLBYTES)) {
2446 #if BUGGY > 1
2447                         printf("sr%d: sr_get_packet() rxstat=%02x, len=%d\n",
2448                                sc->unit, rxstat, len);
2449 #endif
2450
2451                         MGETHDR(m, M_DONTWAIT, MT_DATA);
2452                         if (m == NULL) {
2453                                 /*
2454                                  * eat (flush) packet if get mbuf fail!!
2455                                  */
2456                                 sr_eat_packet(sc, 1);
2457                                 continue;
2458                         }
2459                         /*
2460                          * construct control information for pass-off
2461                          */
2462                         m->m_pkthdr.rcvif = ifp;
2463                         m->m_pkthdr.len = m->m_len = len;
2464                         if (len > MHLEN) {
2465                                 MCLGET(m, M_DONTWAIT);
2466                                 if ((m->m_flags & M_EXT) == 0) {
2467                                         /*
2468                                          * We couldn't get a big enough
2469                                          * message packet, so we'll send the
2470                                          * packet to /dev/null...
2471                                          */
2472                                         m_freem(m);
2473                                         sr_eat_packet(sc, 1);
2474                                         continue;
2475                                 }
2476                         }
2477                         /*
2478                          * OK, we've got a good message buffer.  Now we can
2479                          * copy the received message into it
2480                          */
2481                         sr_copy_rxbuf(m, sc, len);      /* copy from DPRAM */
2482
2483 #if NBPFILTER > 0
2484                         if (ifp->if_bpf)
2485                                 bpf_mtap(ifp, m);
2486 #endif
2487
2488 #if BUGGY > 3
2489                         {
2490                                 u_char *bp;
2491
2492                                 bp = (u_char *)m;
2493                                 printf("sr%d: rcvd=%02x%02x%02x%02x%02x%02x\n",
2494                                        sc->unit,
2495                                        bp[0], bp[1], bp[2],
2496                                        bp[4], bp[5], bp[6]);
2497                         }
2498 #endif
2499
2500                         /*
2501                          * Pass off the message to PPP, connecting it it to
2502                          * the system...
2503                          */
2504                         switch (sc->protocol) {
2505 #if NFR > 0
2506                         case N2_USE_FRP:
2507                                 fr_input(ifp, m);
2508                                 break;
2509 #endif
2510                         case N2_USE_PPP:
2511                         default:
2512                                 sppp_input(ifp, m);
2513                         }
2514
2515                         ifp->if_ipackets++;
2516
2517                         /*
2518                          * Update the eda to the previous descriptor.
2519                          */
2520                         i = (len + SR_BUF_SIZ - 1) / SR_BUF_SIZ;
2521                         sc->rxhind = (sc->rxhind + i) % sc->rxmax;
2522
2523                         rxdesc = (sca_descriptor *)sc->rxdesc;
2524                         rxndx = (sc->rxhind + sc->rxmax - 2) % sc->rxmax;
2525                         rxdesc = &rxdesc[rxndx];
2526
2527                         SRC_PUT16(hc->sca_base,
2528                                   hc->sca->dmac[DMAC_RXCH(sc->scachan)].eda,
2529                                   (u_short)((u_int)(rxdesc + hc->mem_pstart)
2530                                              & 0xffff));
2531
2532                 } else {
2533                         int got_st3, got_cda, got_eda;
2534                         int tries = 5;
2535
2536                         while((rxstat == 0xff) && --tries)
2537                                 sr_packet_avail(sc, &len, &rxstat);
2538
2539                         /*
2540                          * It look like we get an interrupt early
2541                          * sometimes and then the status is not
2542                          * filled in yet.
2543                          */
2544                         if(tries && (tries != 5))
2545                                 continue;
2546
2547                         /*
2548                          * This chunk of code handles the error packets.
2549                          * We'll log them for posterity...
2550                          */
2551                         sr_eat_packet(sc, 1);
2552
2553                         ifp->if_ierrors++;
2554
2555                         got_st3 = SRC_GET8(hc->sca_base,
2556                                   hc->sca->msci[sc->scachan].st3);
2557                         got_cda = SRC_GET16(hc->sca_base,
2558                                   hc->sca->dmac[DMAC_RXCH(sc->scachan)].cda);
2559                         got_eda = SRC_GET16(hc->sca_base,
2560                                   hc->sca->dmac[DMAC_RXCH(sc->scachan)].eda);
2561
2562 #if BUGGY > 0
2563                         printf("sr%d: Receive error chan %d, "
2564                                "stat %02x, msci st3 %02x,"
2565                                "rxhind %d, cda %04x, eda %04x.\n",
2566                                sc->unit, sc->scachan, rxstat,
2567                                got_st3, sc->rxhind, got_cda, got_eda);
2568 #endif
2569                 }
2570         }
2571
2572 #if BUGGY > 0
2573         printf("sr%d: sr_get_packets() found %d packet(s)\n",
2574                sc->unit, pkts);
2575 #endif
2576
2577         if (hc->mempages)
2578                 SRC_SET_OFF(hc->iobase);
2579 }
2580
2581 /*
2582  * All DMA interrupts come here.
2583  *
2584  * Each channel has two interrupts.
2585  * Interrupt A for errors and Interrupt B for normal stuff like end
2586  * of transmit or receive dmas.
2587  */
2588 static void
2589 sr_dmac_intr(struct sr_hardc *hc, u_char isr1)
2590 {
2591         u_char dsr;             /* contents of DMA Stat Reg */
2592         u_char dotxstart;       /* enables for tranmit part */
2593         int mch;                /* channel being processed */
2594         struct sr_softc *sc;    /* channel's softc structure */
2595         sca_regs *sca = hc->sca;
2596         dmac_channel *dmac;     /* dma structure of chip */
2597
2598 #if BUGGY > 0
2599         printf("sr_dmac_intr(hc=%08x,isr1=%04x)\n", hc, isr1);
2600 #endif
2601
2602         mch = 0;                /* assume chan0 on card */
2603         dotxstart = isr1;       /* copy for xmitter starts */
2604
2605         /*
2606          * Shortcut if there is no interrupts for dma channel 0 or 1.
2607          * Skip processing for channel 0 if no incoming hit
2608          */
2609         if ((isr1 & 0x0F) == 0) {
2610                 mch = 1;
2611                 isr1 >>= 4;
2612         }
2613         do {
2614                 sc = &hc->sc[mch];
2615
2616                 /*
2617                  * Transmit channel - DMA Status Register Evaluation
2618                  */
2619                 if (isr1 & 0x0C) {
2620                         dmac = &sca->dmac[DMAC_TXCH(mch)];
2621
2622                         /*
2623                          * get the DMA Status Register contents and write
2624                          * back to reset interrupt...
2625                          */
2626                         dsr = SRC_GET8(hc->sca_base, dmac->dsr);
2627                         SRC_PUT8(hc->sca_base, dmac->dsr, dsr);
2628
2629                         /*
2630                          * Check for (& process) a Counter overflow
2631                          */
2632                         if (dsr & SCA_DSR_COF) {
2633                                 printf("sr%d: TX DMA Counter overflow, "
2634                                        "txpacket no %lu.\n",
2635                                        sc->unit, sc->ifsppp.pp_if.if_opackets);
2636                                 sc->ifsppp.pp_if.if_oerrors++;
2637                         }
2638                         /*
2639                          * Check for (& process) a Buffer overflow
2640                          */
2641                         if (dsr & SCA_DSR_BOF) {
2642                                 printf("sr%d: TX DMA Buffer overflow, "
2643                                        "txpacket no %lu, dsr %02x, "
2644                                        "cda %04x, eda %04x.\n",
2645                                        sc->unit, sc->ifsppp.pp_if.if_opackets,
2646                                        dsr,
2647                                        SRC_GET16(hc->sca_base, dmac->cda),
2648                                        SRC_GET16(hc->sca_base, dmac->eda));
2649                                 sc->ifsppp.pp_if.if_oerrors++;
2650                         }
2651                         /*
2652                          * Check for (& process) an End of Transfer (OK)
2653                          */
2654                         if (dsr & SCA_DSR_EOT) {
2655                                 /*
2656                                  * This should be the most common case.
2657                                  *
2658                                  * Clear the IFF_OACTIVE flag.
2659                                  *
2660                                  * Call srstart to start a new transmit if
2661                                  * there is data to transmit.
2662                                  */
2663 #if BUGGY > 0
2664                                 printf("sr%d: TX Completed OK\n", sc->unit);
2665 #endif
2666                                 sc->xmit_busy = 0;
2667                                 sc->ifsppp.pp_if.if_flags &= ~IFF_OACTIVE;
2668                                 sc->ifsppp.pp_if.if_timer = 0;
2669
2670                                 if (sc->txb_inuse && --sc->txb_inuse)
2671                                         sr_xmit(sc);
2672                         }
2673                 }
2674                 /*
2675                  * Receive channel processing of DMA Status Register
2676                  */
2677                 if (isr1 & 0x03) {
2678                         dmac = &sca->dmac[DMAC_RXCH(mch)];
2679
2680                         dsr = SRC_GET8(hc->sca_base, dmac->dsr);
2681                         SRC_PUT8(hc->sca_base, dmac->dsr, dsr);
2682
2683                         /*
2684                          * End of frame processing (MSG OK?)
2685                          */
2686                         if (dsr & SCA_DSR_EOM) {
2687 #if BUGGY > 0
2688                                 int tt, ind;
2689
2690                                 tt = sc->ifsppp.pp_if.if_ipackets;
2691                                 ind = sc->rxhind;
2692 #endif
2693
2694                                 sr_get_packets(sc);
2695
2696 #if BUGGY > 0
2697                                 if (tt == sc->ifsppp.pp_if.if_ipackets) {
2698                                         sca_descriptor *rxdesc;
2699                                         int i;
2700
2701                                         printf("SR: RXINTR isr1 %x, dsr %x, "
2702                                                "no data %d pkts, orxind %d.\n",
2703                                                dotxstart, dsr, tt, ind);
2704                                         printf("SR: rxdesc %x, rxstart %x, "
2705                                                "rxend %x, rxhind %d, "
2706                                                "rxmax %d.\n",
2707                                                sc->rxdesc, sc->rxstart,
2708                                                sc->rxend, sc->rxhind,
2709                                                sc->rxmax);
2710                                         printf("SR: cda %x, eda %x.\n",
2711                                             SRC_GET16(hc->sca_base, dmac->cda),
2712                                             SRC_GET16(hc->sca_base, dmac->eda));
2713
2714                                         if (hc->mempages) {
2715                                                 SRC_SET_ON(hc->iobase);
2716                                                 SRC_SET_MEM(hc->iobase, sc->rxdesc);
2717                                         }
2718                                         rxdesc = (sca_descriptor *)
2719                                                  (hc->mem_start +
2720                                                   (sc->rxdesc & hc->winmsk));
2721                                         rxdesc = &rxdesc[sc->rxhind];
2722
2723                                         for (i = 0; i < 3; i++, rxdesc++)
2724                                                 printf("SR: rxdesc->stat %x, "
2725                                                        "len %d.\n",
2726                                                        rxdesc->stat,
2727                                                        rxdesc->len);
2728
2729                                         if (hc->mempages)
2730                                                 SRC_SET_OFF(hc->iobase);
2731                                 }
2732 #endif
2733                         }
2734                         /*
2735                          * Check for Counter overflow
2736                          */
2737                         if (dsr & SCA_DSR_COF) {
2738                                 printf("sr%d: RX DMA Counter overflow, "
2739                                        "rxpkts %lu.\n",
2740                                        sc->unit, sc->ifsppp.pp_if.if_ipackets);
2741                                 sc->ifsppp.pp_if.if_ierrors++;
2742                         }
2743                         /*
2744                          * Check for Buffer overflow
2745                          */
2746                         if (dsr & SCA_DSR_BOF) {
2747                                 printf("sr%d: RX DMA Buffer overflow, "
2748                                        "rxpkts %lu, rxind %d, "
2749                                        "cda %x, eda %x, dsr %x.\n",
2750                                        sc->unit, sc->ifsppp.pp_if.if_ipackets,
2751                                        sc->rxhind,
2752                                        SRC_GET16(hc->sca_base, dmac->cda),
2753                                        SRC_GET16(hc->sca_base, dmac->eda),
2754                                        dsr);
2755
2756                                 /*
2757                                  * Make sure we eat as many as possible.
2758                                  * Then get the system running again.
2759                                  */
2760                                 if (hc->mempages)
2761                                         SRC_SET_ON(hc->iobase);
2762
2763                                 sr_eat_packet(sc, 0);
2764                                 sc->ifsppp.pp_if.if_ierrors++;
2765
2766                                 SRC_PUT8(hc->sca_base,
2767                                          sca->msci[mch].cmd,
2768                                          SCA_CMD_RXMSGREJ);
2769
2770                                 SRC_PUT8(hc->sca_base, dmac->dsr, SCA_DSR_DE);
2771
2772 #if BUGGY > 0
2773                                 printf("sr%d: RX DMA Buffer overflow, "
2774                                        "rxpkts %lu, rxind %d, "
2775                                        "cda %x, eda %x, dsr %x. After\n",
2776                                        sc->unit,
2777                                        sc->ifsppp.pp_if.if_ipackets,
2778                                        sc->rxhind,
2779                                        SRC_GET16(hc->sca_base, dmac->cda),
2780                                        SRC_GET16(hc->sca_base, dmac->eda),
2781                                        SRC_GET8(hc->sca_base, dmac->dsr));
2782 #endif
2783
2784                                 if (hc->mempages)
2785                                         SRC_SET_OFF(hc->iobase);
2786                         }
2787                         /*
2788                          * End of Transfer
2789                          */
2790                         if (dsr & SCA_DSR_EOT) {
2791                                 /*
2792                                  * If this happen, it means that we are
2793                                  * receiving faster than what the processor
2794                                  * can handle.
2795                                  * 
2796                                  * XXX We should enable the dma again.
2797                                  */
2798                                 printf("sr%d: RX End of xfer, rxpkts %lu.\n",
2799                                        sc->unit,
2800                                        sc->ifsppp.pp_if.if_ipackets);
2801                                 sc->ifsppp.pp_if.if_ierrors++;
2802                         }
2803                 }
2804                 isr1 >>= 4;     /* process next half of ISR */
2805                 mch++;          /* and move to next channel */
2806         } while ((mch < NCHAN) && isr1);        /* loop for each chn */
2807
2808         /*
2809          * Now that we have done all the urgent things, see if we can fill
2810          * the transmit buffers.
2811          */
2812         for (mch = 0; mch < NCHAN; mch++) {
2813                 if (dotxstart & 0x0C) { /* TX initiation enabled? */
2814                         sc = &hc->sc[mch];
2815                         srstart(&sc->ifsppp.pp_if);
2816                 }
2817                 dotxstart >>= 4;/* shift for next channel */
2818         }
2819 }
2820
2821 /*
2822  * Perform timeout on an FR channel 
2823  *
2824  * Establish a periodic check of open N2 ports;  If
2825  * a port is open/active, its DCD state is checked
2826  * and a loss of DCD is recognized (and eventually
2827  * processed).
2828  */
2829 static void
2830 sr_modemck(void *arg)
2831 {
2832         u_int s;
2833         int card;               /* card index in table */
2834         int cards;              /* card list index */
2835         int mch;                /* channel on card */
2836         u_char dcd_v;           /* Data Carrier Detect */
2837         u_char got_st0;         /* contents of ST0 */
2838         u_char got_st1;         /* contents of ST1 */
2839         u_char got_st2;         /* contents of ST2 */
2840         u_char got_st3;         /* contents of ST3 */
2841         struct sr_hardc *hc;    /* card's configuration */
2842         struct sr_hardc *Card[16];/* up to 16 cards in system */
2843         struct sr_softc *sc;    /* channel's softc structure */
2844         struct ifnet *ifp;      /* interface control table */
2845         msci_channel *msci;     /* regs specific to channel */
2846
2847         s = splimp();
2848
2849 #if     0
2850         if (sr_opens == 0) {    /* count of "up" channels */
2851                 sr_watcher = 0; /* indicate no watcher */
2852                 splx(s);
2853                 return;
2854         }
2855 #endif
2856
2857         sr_watcher = 1;         /* mark that we're online */
2858
2859         /*
2860          * Now we'll need a list of cards to process.  Since we can handle
2861          * both ISA and PCI cards (and I didn't think of making this logic
2862          * global YET) we'll generate a single table of card table
2863          * addresses.
2864          */
2865         cards = 0;
2866
2867         for (card = 0; card < NSR; card++) {
2868                 hc = &sr_hardc[card];
2869
2870                 if (hc->sc == (void *)0)
2871                         continue;
2872
2873                 Card[cards++] = hc;
2874         }
2875
2876         hc = sr_hardc_pci;
2877
2878         while (hc) {
2879                 Card[cards++] = hc;
2880                 hc = hc->next;
2881         }
2882
2883         /*
2884          * OK, we've got work we can do.  Let's do it... (Please note that
2885          * this code _only_ deals w/ ISA cards)
2886          */
2887         for (card = 0; card < cards; card++) {
2888                 hc = Card[card];/* get card table */
2889
2890                 for (mch = 0; mch < hc->numports; mch++) {
2891                         sc = &hc->sc[mch];
2892
2893                         if (sc->attached == 0)
2894                                 continue;
2895
2896                         ifp = &sc->ifsppp.pp_if;
2897
2898                         /*
2899                          * if this channel isn't "up", skip it
2900                          */
2901                         if ((ifp->if_flags & IFF_UP) == 0)
2902                                 continue;
2903
2904                         /*
2905                          * OK, now we can go looking at this channel's
2906                          * actual register contents...
2907                          */
2908                         msci = &hc->sca->msci[sc->scachan];
2909
2910                         /*
2911                          * OK, now we'll look into the actual status of this
2912                          * channel...
2913                          * 
2914                          * I suck in more registers than strictly needed
2915                          */
2916                         got_st0 = SRC_GET8(hc->sca_base, msci->st0);
2917                         got_st1 = SRC_GET8(hc->sca_base, msci->st1);
2918                         got_st2 = SRC_GET8(hc->sca_base, msci->st2);
2919                         got_st3 = SRC_GET8(hc->sca_base, msci->st3);
2920
2921                         /*
2922                          * We want to see if the DCD signal is up (DCD is
2923                          * true if zero)
2924                          */
2925                         dcd_v = (got_st3 & SCA_ST3_DCD) == 0;
2926
2927                         if (dcd_v == 0)
2928                                 printf("sr%d: DCD lost\n", sc->unit);
2929                 }
2930         }
2931
2932         /*
2933          * OK, now set up for the next modem signal checking pass...
2934          */
2935         timeout(sr_modemck, NULL, hz);
2936
2937         splx(s);
2938 }
2939
2940 static void
2941 sr_msci_intr(struct sr_hardc *hc, u_char isr0)
2942 {
2943         printf("src%d: SRINTR: MSCI\n", hc->cunit);
2944 }
2945
2946 static void
2947 sr_timer_intr(struct sr_hardc *hc, u_char isr2)
2948 {
2949         printf("src%d: SRINTR: TIMER\n", hc->cunit);
2950 }
2951
2952 /*
2953  ********************************* END ************************************
2954  */