Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / net / i4b / layer1 / ifpi / i4b_ifpi_pci.c
1 /*
2  *   Copyright (c) 1999, 2000 Gary Jennejohn. All rights reserved.
3  *
4  *   Redistribution and use in source and binary forms, with or without
5  *   modification, are permitted provided that the following conditions
6  *   are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *   2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  *   3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *   4. Altered versions must be plainly marked as such, and must not be
17  *      misrepresented as being the original software and/or documentation.
18  *   
19  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  *   SUCH DAMAGE.
30  *
31  *---------------------------------------------------------------------------
32  *
33  *      i4b_ifpi_pci.c: AVM Fritz!Card PCI hardware driver
34  *      --------------------------------------------------
35  *
36  *      $Id: i4b_ifpi_pci.c,v 1.4 2000/06/02 11:58:56 hm Exp $
37  *
38  * $FreeBSD: src/sys/i4b/layer1/ifpi/i4b_ifpi_pci.c,v 1.6.2.1 2001/08/10 14:08:37 obrien Exp $
39  * $DragonFly: src/sys/net/i4b/layer1/ifpi/i4b_ifpi_pci.c,v 1.2 2003/06/17 04:28:39 dillon Exp $
40  *
41  *      last edit-date: [Fri Jan 12 17:01:26 2001]
42  *
43  *---------------------------------------------------------------------------*/
44
45 #include "ifpi.h"
46 #include "opt_i4b.h"
47 #include "pci.h"
48
49 #if (NIFPI > 0) && (NPCI > 0)
50
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/mbuf.h>
55
56 #include <machine/bus.h>
57 #include <sys/bus.h>
58 #include <sys/rman.h>
59
60 #include <pci/pcireg.h>
61 #include <pci/pcivar.h>
62
63 #include <sys/socket.h>
64 #include <net/if.h>
65
66 #include <machine/i4b_debug.h>
67 #include <machine/i4b_ioctl.h>
68 #include <machine/i4b_trace.h>
69
70 #include <i4b/include/i4b_global.h>
71 #include <i4b/include/i4b_mbuf.h>
72
73 #include <i4b/layer1/i4b_l1.h>
74 #include <i4b/layer1/isic/i4b_isic.h>
75 #include <i4b/layer1/isic/i4b_isac.h>
76 #include <i4b/layer1/isic/i4b_hscx.h>
77
78 #include <i4b/layer1/ifpi/i4b_ifpi_ext.h>
79
80 #define PCI_AVMA1_VID 0x1244
81 #define PCI_AVMA1_DID 0x0a00
82
83 /* prototypes */
84 static void avma1pp_disable(device_t);
85
86 static void avma1pp_intr(void *);
87 static void hscx_write_reg(int, u_int, u_int, struct l1_softc *);
88 static u_char hscx_read_reg(int, u_int, struct l1_softc *);
89 static u_int hscx_read_reg_int(int, u_int, struct l1_softc *);
90 static void hscx_read_fifo(int, void *, size_t, struct l1_softc *);
91 static void hscx_write_fifo(int, void *, size_t, struct l1_softc *);
92 static void avma1pp_hscx_int_handler(struct l1_softc *);
93 static void avma1pp_hscx_intr(int, u_int, struct l1_softc *);
94 static void avma1pp_init_linktab(struct l1_softc *);
95 static void avma1pp_bchannel_setup(int, int, int, int);
96 static void avma1pp_bchannel_start(int, int);
97 static void avma1pp_hscx_init(struct l1_softc *, int, int);
98 static void avma1pp_bchannel_stat(int, int, bchan_statistics_t *);
99 static void avma1pp_set_linktab(int, int, drvr_link_t *);
100 static isdn_link_t * avma1pp_ret_linktab(int, int);
101 static int avma1pp_pci_probe(device_t);
102 static int avma1pp_hscx_fifo(l1_bchan_state_t *, struct l1_softc *);
103 int avma1pp_attach_avma1pp(device_t);
104 static void ifpi_isac_intr(struct l1_softc *sc);
105
106 static device_method_t avma1pp_pci_methods[] = {
107         /* Device interface */
108         DEVMETHOD(device_probe,         avma1pp_pci_probe),
109         DEVMETHOD(device_attach,        avma1pp_attach_avma1pp),
110         DEVMETHOD(device_shutdown,      avma1pp_disable),
111
112         /* bus interface */
113         DEVMETHOD(bus_print_child,      bus_generic_print_child),
114         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
115
116         { 0, 0 }
117 };
118
119 #if 0 /* use what's in l1_softc */
120 /* a minimal softc for the Fritz!Card PCI */
121 struct avma1pp_softc 
122 {
123         bus_space_handle_t      avma1pp_bhandle;
124         bus_space_tag_t         avma1pp_btag;
125         void                    *avma1pp_intrhand;
126         struct resource         *avma1pp_irq;
127         struct resource         *avma1pp_res;
128         /* pointer to ifpi_sc */
129         struct l1_softc *avma1pp_isc;
130 };
131 #endif
132
133 static driver_t avma1pp_pci_driver = {
134         "ifpi",
135         avma1pp_pci_methods,
136         sizeof(struct l1_softc)
137 };
138
139 static devclass_t avma1pp_pci_devclass;
140
141 DRIVER_MODULE(avma1pp, pci, avma1pp_pci_driver, avma1pp_pci_devclass, 0, 0);
142
143 /* jump table for multiplex routines */
144
145 struct i4b_l1mux_func avma1pp_l1mux_func = {
146         avma1pp_ret_linktab,
147         avma1pp_set_linktab,
148         ifpi_mph_command_req,
149         ifpi_ph_data_req,
150         ifpi_ph_activate_req,
151 };
152
153 struct l1_softc *ifpi_scp[IFPI_MAXUNIT];
154
155 /*---------------------------------------------------------------------------*
156  *      AVM PCI Fritz!Card special registers
157  *---------------------------------------------------------------------------*/
158
159 /*
160  *      register offsets from i/o base
161  */
162 #define STAT0_OFFSET            0x02
163 #define STAT1_OFFSET            0x03
164 #define ADDR_REG_OFFSET         0x04
165 /*#define MODREG_OFFSET         0x06
166 #define VERREG_OFFSET           0x07*/
167
168 /* these 2 are used to select an ISAC register set */
169 #define ISAC_LO_REG_OFFSET      0x04
170 #define ISAC_HI_REG_OFFSET      0x06
171
172 /* offset higher than this goes to the HI register set */
173 #define MAX_LO_REG_OFFSET       0x2f
174
175 /* mask for the offset */
176 #define ISAC_REGSET_MASK        0x0f
177
178 /* the offset from the base to the ISAC registers */
179 #define ISAC_REG_OFFSET         0x10
180
181 /* the offset from the base to the ISAC FIFO */
182 #define ISAC_FIFO               0x02
183
184 /* not really the HSCX, but sort of */
185 #define HSCX_FIFO               0x00
186 #define HSCX_STAT               0x04
187
188 /*
189  *      AVM PCI Status Latch 0 read only bits
190  */
191 #define ASL_IRQ_ISAC            0x01    /* ISAC  interrupt, active low */
192 #define ASL_IRQ_HSCX            0x02    /* HSX   interrupt, active low */
193 #define ASL_IRQ_TIMER           0x04    /* Timer interrupt, active low */
194 #define ASL_IRQ_BCHAN           ASL_IRQ_HSCX
195 /* actually active LOW */
196 #define ASL_IRQ_Pending         (ASL_IRQ_ISAC | ASL_IRQ_HSCX | ASL_IRQ_TIMER)
197
198 /*
199  *      AVM Status Latch 0 write only bits
200  */
201 #define ASL_RESET_ALL           0x01  /* reset siemens IC's, active 1 */
202 #define ASL_TIMERDISABLE        0x02  /* active high */
203 #define ASL_TIMERRESET          0x04  /* active high */
204 #define ASL_ENABLE_INT          0x08  /* active high */
205 #define ASL_TESTBIT             0x10  /* active high */
206
207 /*
208  *      AVM Status Latch 1 write only bits
209  */
210 #define ASL1_INTSEL              0x0f  /* active high */
211 #define ASL1_ENABLE_IOM          0x80  /* active high */
212
213 /*
214  * "HSCX" mode bits
215  */
216 #define  HSCX_MODE_ITF_FLG      0x01
217 #define  HSCX_MODE_TRANS        0x02
218 #define  HSCX_MODE_CCR_7        0x04
219 #define  HSCX_MODE_CCR_16       0x08
220 #define  HSCX_MODE_TESTLOOP     0x80
221
222 /*
223  * "HSCX" status bits
224  */
225 #define  HSCX_STAT_RME          0x01
226 #define  HSCX_STAT_RDO          0x10
227 #define  HSCX_STAT_CRCVFRRAB    0x0E
228 #define  HSCX_STAT_CRCVFR       0x06
229 #define  HSCX_STAT_RML_MASK     0x3f00
230
231 /*
232  * "HSCX" interrupt bits
233  */
234 #define  HSCX_INT_XPR           0x80
235 #define  HSCX_INT_XDU           0x40
236 #define  HSCX_INT_RPR           0x20
237 #define  HSCX_INT_MASK          0xE0
238
239 /*
240  * "HSCX" command bits
241  */
242 #define  HSCX_CMD_XRS           0x80
243 #define  HSCX_CMD_XME           0x01
244 #define  HSCX_CMD_RRS           0x20
245 #define  HSCX_CMD_XML_MASK      0x3f00
246
247 /*
248  * Commands and parameters are sent to the "HSCX" as a long, but the
249  * fields are handled as bytes.
250  *
251  * The long contains:
252  *      (prot << 16)|(txl << 8)|cmd
253  *
254  * where:
255  *      prot = protocol to use
256  *      txl = transmit length
257  *      cmd = the command to be executed
258  *
259  * The fields are defined as u_char in struct l1_softc.
260  *
261  * Macro to coalesce the byte fields into a u_int
262  */
263 #define AVMA1PPSETCMDLONG(f) (f) = ((sc->avma1pp_cmd) | (sc->avma1pp_txl << 8) \
264                                         | (sc->avma1pp_prot << 16))
265
266 /*
267  * to prevent deactivating the "HSCX" when both channels are active we
268  * define an HSCX_ACTIVE flag which is or'd into the channel's state
269  * flag in avma1pp_bchannel_setup upon active and cleared upon deactivation.
270  * It is set high to allow room for new flags.
271  */
272 #define HSCX_AVMA1PP_ACTIVE     0x1000 
273
274 /*---------------------------------------------------------------------------*
275  *      AVM read fifo routines
276  *---------------------------------------------------------------------------*/
277
278 static void
279 avma1pp_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
280 {
281         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
282         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
283
284         switch (what) {
285                 case ISIC_WHAT_ISAC:
286                         bus_space_write_1(btag, bhandle,  ADDR_REG_OFFSET, ISAC_FIFO);
287                         bus_space_read_multi_1(btag, bhandle,  ISAC_REG_OFFSET, buf, size);
288                         break;
289                 case ISIC_WHAT_HSCXA:
290                         hscx_read_fifo(0, buf, size, sc);
291                         break;
292                 case ISIC_WHAT_HSCXB:
293                         hscx_read_fifo(1, buf, size, sc);
294                         break;
295         }
296 }
297
298 static void
299 hscx_read_fifo(int chan, void *buf, size_t len, struct l1_softc *sc)
300 {
301         u_int32_t *ip;
302         size_t cnt;
303         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
304         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
305
306         bus_space_write_4(btag, bhandle, ADDR_REG_OFFSET, chan);
307         ip = (u_int32_t *)buf;
308         cnt = 0;
309         /* what if len isn't a multiple of sizeof(int) and buf is */
310         /* too small ???? */
311         while (cnt < len)
312         {
313                 *ip++ = bus_space_read_4(btag, bhandle, ISAC_REG_OFFSET);
314                 cnt += 4;
315         }
316 }
317
318 /*---------------------------------------------------------------------------*
319  *      AVM write fifo routines
320  *---------------------------------------------------------------------------*/
321 static void
322 avma1pp_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
323 {
324         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
325         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
326
327         switch (what) {
328                 case ISIC_WHAT_ISAC:
329                         bus_space_write_1(btag, bhandle,  ADDR_REG_OFFSET, ISAC_FIFO);
330                         bus_space_write_multi_1(btag, bhandle,  ISAC_REG_OFFSET, (u_int8_t*)buf, size);
331                         break;
332                 case ISIC_WHAT_HSCXA:
333                         hscx_write_fifo(0, buf, size, sc);
334                         break;
335                 case ISIC_WHAT_HSCXB:
336                         hscx_write_fifo(1, buf, size, sc);
337                         break;
338         }
339 }
340
341 static void
342 hscx_write_fifo(int chan, void *buf, size_t len, struct l1_softc *sc)
343 {
344         u_int32_t *ip;
345         size_t cnt;
346         l1_bchan_state_t *Bchan = &sc->sc_chan[chan];
347         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
348         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
349
350
351         sc->avma1pp_cmd &= ~HSCX_CMD_XME;
352         sc->avma1pp_txl = 0;
353         if (Bchan->out_mbuf_cur == NULL)
354         {
355           if (Bchan->bprot != BPROT_NONE)
356                  sc->avma1pp_cmd |= HSCX_CMD_XME;
357         }
358         if (len != sc->sc_bfifolen)
359                 sc->avma1pp_txl = len;
360         
361         cnt = 0; /* borrow cnt */
362         AVMA1PPSETCMDLONG(cnt);
363         hscx_write_reg(chan, HSCX_STAT, cnt, sc);
364
365         ip = (u_int32_t *)buf;
366         cnt = 0;
367         while (cnt < len)
368         {
369                 bus_space_write_4(btag, bhandle, ISAC_REG_OFFSET, *ip);
370                 ip++;
371                 cnt += 4;
372         }
373 }
374
375 /*---------------------------------------------------------------------------*
376  *      AVM write register routines
377  *---------------------------------------------------------------------------*/
378
379 static void
380 avma1pp_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
381 {
382         u_char reg_bank;
383         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
384         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
385
386         switch (what) {
387                 case ISIC_WHAT_ISAC:
388                         reg_bank = (offs > MAX_LO_REG_OFFSET) ? ISAC_HI_REG_OFFSET:ISAC_LO_REG_OFFSET;
389 #ifdef AVMA1PCI_DEBUG
390                         printf("write_reg bank %d  off %ld.. ", (int)reg_bank, (long)offs);
391 #endif
392                         /* set the register bank */
393                         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, reg_bank);
394                         bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + (offs & ISAC_REGSET_MASK), data);
395                         break;
396                 case ISIC_WHAT_HSCXA:
397                         hscx_write_reg(0, offs, data, sc);
398                         break;
399                 case ISIC_WHAT_HSCXB:
400                         hscx_write_reg(1, offs, data, sc);
401                         break;
402         }
403 }
404
405 static void
406 hscx_write_reg(int chan, u_int off, u_int val, struct l1_softc *sc)
407 {
408         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
409         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
410
411         /* point at the correct channel */
412         bus_space_write_4(btag, bhandle, ADDR_REG_OFFSET, chan);
413         bus_space_write_4(btag, bhandle, ISAC_REG_OFFSET + off, val);
414 }
415
416 /*---------------------------------------------------------------------------*
417  *      AVM read register routines
418  *---------------------------------------------------------------------------*/
419 static u_int8_t
420 avma1pp_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
421 {
422         u_char reg_bank;
423         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
424         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
425
426         switch (what) {
427                 case ISIC_WHAT_ISAC:
428                         reg_bank = (offs > MAX_LO_REG_OFFSET) ? ISAC_HI_REG_OFFSET:ISAC_LO_REG_OFFSET;
429 #ifdef AVMA1PCI_DEBUG
430                         printf("read_reg bank %d  off %ld.. ", (int)reg_bank, (long)offs);
431 #endif
432                         /* set the register bank */
433                         bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, reg_bank);
434                         return(bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET +
435                                 (offs & ISAC_REGSET_MASK)));
436                 case ISIC_WHAT_HSCXA:
437                         return hscx_read_reg(0, offs, sc);
438                 case ISIC_WHAT_HSCXB:
439                         return hscx_read_reg(1, offs, sc);
440         }
441         return 0;
442 }
443
444 static u_char
445 hscx_read_reg(int chan, u_int off, struct l1_softc *sc)
446 {
447         return(hscx_read_reg_int(chan, off, sc) & 0xff);
448 }
449
450 /*
451  * need to be able to return an int because the RBCH is in the 2nd
452  * byte.
453  */
454 static u_int
455 hscx_read_reg_int(int chan, u_int off, struct l1_softc *sc)
456 {
457         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
458         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
459
460         /* point at the correct channel */
461         bus_space_write_4(btag, bhandle, ADDR_REG_OFFSET, chan);
462         return(bus_space_read_4(btag, bhandle, ISAC_REG_OFFSET + off));
463 }
464
465 /*---------------------------------------------------------------------------*
466  *      avma1pp_probe - probe for a card
467  *---------------------------------------------------------------------------*/
468 static int
469 avma1pp_pci_probe(dev)
470         device_t                dev;
471 {
472         u_int16_t               did, vid;
473
474         vid = pci_get_vendor(dev);
475         did = pci_get_device(dev);
476
477         if ((vid == PCI_AVMA1_VID) && (did == PCI_AVMA1_DID)) {
478                 device_set_desc(dev, "AVM Fritz!Card PCI");
479                 return(0);
480         }
481
482         return(ENXIO);
483 }
484
485 /*---------------------------------------------------------------------------*
486  *      avma1pp_attach_avma1pp - attach Fritz!Card PCI
487  *---------------------------------------------------------------------------*/
488 int
489 avma1pp_attach_avma1pp(device_t dev)
490 {
491         struct l1_softc *sc;
492         u_int v;
493         int unit, error = 0;
494         int s;
495         u_int16_t did, vid;
496         void *ih = 0;
497         bus_space_handle_t bhandle;
498         bus_space_tag_t btag; 
499         l1_bchan_state_t *chan;
500
501         s = splimp();
502
503         vid = pci_get_vendor(dev);
504         did = pci_get_device(dev);
505         sc = device_get_softc(dev);
506         unit = device_get_unit(dev);
507         bzero(sc, sizeof(struct l1_softc));
508
509         /* probably not really required */
510         if(unit > IFPI_MAXUNIT) {
511                 printf("avma1pp%d: Error, unit > IFPI_MAXUNIT!\n", unit);
512                 splx(s);
513                 return(ENXIO);
514         }
515
516         if ((vid != PCI_AVMA1_VID) && (did != PCI_AVMA1_DID)) {
517                 printf("avma1pp%d: unknown device!?\n", unit);
518                 goto fail;
519         }
520
521         ifpi_scp[unit] = sc;
522
523         sc->sc_resources.io_rid[0] = PCIR_MAPS+4;
524         sc->sc_resources.io_base[0] = bus_alloc_resource(dev, SYS_RES_IOPORT,
525                 &sc->sc_resources.io_rid[0],
526                 0, ~0, 1, RF_ACTIVE);
527
528         if (sc->sc_resources.io_base[0] == NULL) {
529                 printf("avma1pp%d: couldn't map IO port\n", unit);
530                 error = ENXIO;
531                 goto fail;
532         }
533
534         bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
535         btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
536
537         /* Allocate interrupt */
538         sc->sc_resources.irq_rid = 0;
539         sc->sc_resources.irq = bus_alloc_resource(dev, SYS_RES_IRQ,
540                 &sc->sc_resources.irq_rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
541
542         if (sc->sc_resources.irq == NULL) {
543                 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS+4, sc->sc_resources.io_base[0]);
544                 printf("avma1pp%d: couldn't map interrupt\n", unit);
545                 error = ENXIO;
546                 goto fail;
547         }
548
549         error = bus_setup_intr(dev, sc->sc_resources.irq, INTR_TYPE_NET, avma1pp_intr, sc, &ih);
550
551         if (error) {
552                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_resources.irq);
553                 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_MAPS+4, sc->sc_resources.io_base[0]);
554                 printf("avma1pp%d: couldn't set up irq\n", unit);
555                 goto fail;
556         }
557
558         sc->sc_unit = unit;
559
560         /* end of new-bus stuff */
561
562         ISAC_BASE = (caddr_t)ISIC_WHAT_ISAC;
563
564         HSCX_A_BASE = (caddr_t)ISIC_WHAT_HSCXA;
565         HSCX_B_BASE = (caddr_t)ISIC_WHAT_HSCXB;
566
567         /* setup access routines */
568
569         sc->clearirq = NULL;
570         sc->readreg = avma1pp_read_reg;
571         sc->writereg = avma1pp_write_reg;
572
573         sc->readfifo = avma1pp_read_fifo;
574         sc->writefifo = avma1pp_write_fifo;
575
576         /* setup card type */
577         
578         sc->sc_cardtyp = CARD_TYPEP_AVMA1PCI;
579
580         /* setup IOM bus type */
581         
582         sc->sc_bustyp = BUS_TYPE_IOM2;
583
584         /* set up some other miscellaneous things */
585         sc->sc_ipac = 0;
586         sc->sc_bfifolen = HSCX_FIFO_LEN;
587
588         /* reset the card */
589         /* the Linux driver does this to clear any pending ISAC interrupts */
590         v = 0;
591         v = ISAC_READ(I_STAR);
592 #ifdef AVMA1PCI_DEBUG
593         printf("avma1pp_attach: I_STAR %x...", v);
594 #endif
595         v = ISAC_READ(I_MODE);
596 #ifdef AVMA1PCI_DEBUG
597         printf("avma1pp_attach: I_MODE %x...", v);
598 #endif
599         v = ISAC_READ(I_ADF2);
600 #ifdef AVMA1PCI_DEBUG
601         printf("avma1pp_attach: I_ADF2 %x...", v);
602 #endif
603         v = ISAC_READ(I_ISTA);
604 #ifdef AVMA1PCI_DEBUG
605         printf("avma1pp_attach: I_ISTA %x...", v);
606 #endif
607         if (v & ISAC_ISTA_EXI)
608         {
609                  v = ISAC_READ(I_EXIR);
610 #ifdef AVMA1PCI_DEBUG
611                  printf("avma1pp_attach: I_EXIR %x...", v);
612 #endif
613         }
614         v = ISAC_READ(I_CIRR);
615 #ifdef AVMA1PCI_DEBUG
616         printf("avma1pp_attach: I_CIRR %x...", v);
617 #endif
618         ISAC_WRITE(I_MASK, 0xff);
619         /* the Linux driver does this to clear any pending HSCX interrupts */
620         v = hscx_read_reg_int(0, HSCX_STAT, sc);
621 #ifdef AVMA1PCI_DEBUG
622         printf("avma1pp_attach: 0 HSCX_STAT %x...", v);
623 #endif
624         v = hscx_read_reg_int(1, HSCX_STAT, sc);
625 #ifdef AVMA1PCI_DEBUG
626         printf("avma1pp_attach: 1 HSCX_STAT %x\n", v);
627 #endif
628
629         bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_RESET_ALL|ASL_TIMERDISABLE);
630         DELAY(SEC_DELAY/100); /* 10 ms */
631         bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_TIMERRESET|ASL_ENABLE_INT|ASL_TIMERDISABLE);
632         DELAY(SEC_DELAY/100); /* 10 ms */
633 #ifdef AVMA1PCI_DEBUG
634         bus_space_write_1(btag, bhandle, STAT1_OFFSET, ASL1_ENABLE_IOM|sc->sc_irq);
635         DELAY(SEC_DELAY/100); /* 10 ms */
636         v = bus_space_read_1(btag, bhandle, STAT1_OFFSET);
637         printf("after reset: S1 %#x\n", v);
638
639         v = bus_space_read_4(btag, bhandle, 0);
640         printf("avma1pp_attach_avma1pp: v %#x\n", v);
641 #endif
642
643    /* from here to the end would normally be done in isic_pciattach */
644
645          printf("ifpi%d: ISAC %s (IOM-%c)\n", unit,
646                 "2085 Version A1/A2 or 2086/2186 Version 1.1",
647                  sc->sc_bustyp == BUS_TYPE_IOM1 ? '1' : '2');
648
649         /* init the ISAC */
650         ifpi_isac_init(sc);
651
652 #if defined (__FreeBSD__) && __FreeBSD__ > 4
653         /* Init the channel mutexes */
654         chan = &sc->sc_chan[HSCX_CH_A];
655         mtx_init(&chan->rx_queue.ifq_mtx, "i4b_avma1pp_rx", MTX_DEF);
656         mtx_init(&chan->tx_queue.ifq_mtx, "i4b_avma1pp_tx", MTX_DEF);
657         chan = &sc->sc_chan[HSCX_CH_B];
658         mtx_init(&chan->rx_queue.ifq_mtx, "i4b_avma1pp_rx", MTX_DEF);
659         mtx_init(&chan->tx_queue.ifq_mtx, "i4b_avma1pp_tx", MTX_DEF);
660 #endif
661
662         /* init the "HSCX" */
663         avma1pp_bchannel_setup(sc->sc_unit, HSCX_CH_A, BPROT_NONE, 0);
664         
665         avma1pp_bchannel_setup(sc->sc_unit, HSCX_CH_B, BPROT_NONE, 0);
666
667         /* can't use the normal B-Channel stuff */
668         avma1pp_init_linktab(sc);
669
670         /* set trace level */
671
672         sc->sc_trace = TRACE_OFF;
673
674         sc->sc_state = ISAC_IDLE;
675
676         sc->sc_ibuf = NULL;
677         sc->sc_ib = NULL;
678         sc->sc_ilen = 0;
679
680         sc->sc_obuf = NULL;
681         sc->sc_op = NULL;
682         sc->sc_ol = 0;
683         sc->sc_freeflag = 0;
684
685         sc->sc_obuf2 = NULL;
686         sc->sc_freeflag2 = 0;
687
688 #if defined(__FreeBSD__) && __FreeBSD__ >=3
689         callout_handle_init(&sc->sc_T3_callout);
690         callout_handle_init(&sc->sc_T4_callout);        
691 #endif
692         
693         /* init higher protocol layers */
694         
695         i4b_l1_mph_status_ind(L0IFPIUNIT(sc->sc_unit), STI_ATTACH, sc->sc_cardtyp, &avma1pp_l1mux_func);
696
697   fail:
698         splx(s);
699         return(error);
700 }
701
702 /*
703  * this is the real interrupt routine
704  */
705 static void
706 avma1pp_hscx_intr(int h_chan, u_int stat, struct l1_softc *sc)
707 {
708         register l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
709         int activity = -1;
710         u_int param = 0;
711         
712         NDBGL1(L1_H_IRQ, "%#x", stat);
713
714         if((stat & HSCX_INT_XDU) && (chan->bprot != BPROT_NONE))/* xmit data underrun */
715         {
716                 chan->stat_XDU++;                       
717                 NDBGL1(L1_H_XFRERR, "xmit data underrun");
718                 /* abort the transmission */
719                 sc->avma1pp_txl = 0;
720                 sc->avma1pp_cmd |= HSCX_CMD_XRS;
721                 AVMA1PPSETCMDLONG(param);
722                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
723                 sc->avma1pp_cmd &= ~HSCX_CMD_XRS;
724                 AVMA1PPSETCMDLONG(param);
725                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
726
727                 if (chan->out_mbuf_head != NULL)  /* don't continue to transmit this buffer */
728                 {
729                         i4b_Bfreembuf(chan->out_mbuf_head);
730                         chan->out_mbuf_cur = chan->out_mbuf_head = NULL;
731                 }
732         }
733
734         /*
735          * The following is based on examination of the Linux driver.
736          *
737          * The logic here is different than with a "real" HSCX; all kinds
738          * of information (interrupt/status bits) are in stat.
739          *              HSCX_INT_RPR indicates a receive interrupt
740          *                      HSCX_STAT_RDO indicates an overrun condition, abort -
741          *                      otherwise read the bytes ((stat & HSCX_STZT_RML_MASK) >> 8)
742          *                      HSCX_STAT_RME indicates end-of-frame and apparently any
743          *                      CRC/framing errors are only reported in this state.
744          *                              if ((stat & HSCX_STAT_CRCVFRRAB) != HSCX_STAT_CRCVFR)
745          *                                      CRC/framing error
746          */
747         
748         if(stat & HSCX_INT_RPR)
749         {
750                 register int fifo_data_len;
751                 int error = 0;
752                 /* always have to read the FIFO, so use a scratch buffer */
753                 u_char scrbuf[HSCX_FIFO_LEN];
754
755                 if(stat & HSCX_STAT_RDO)
756                 {
757                         chan->stat_RDO++;
758                         NDBGL1(L1_H_XFRERR, "receive data overflow");
759                         error++;                                
760                 }
761
762                 /*
763                  * check whether we're receiving data for an inactive B-channel
764                  * and discard it. This appears to happen for telephony when
765                  * both B-channels are active and one is deactivated. Since
766                  * it is not really possible to deactivate the channel in that
767                  * case (the ASIC seems to deactivate _both_ channels), the
768                  * "deactivated" channel keeps receiving data which can lead
769                  * to exhaustion of mbufs and a kernel panic.
770                  *
771                  * This is a hack, but it's the only solution I can think of
772                  * without having the documentation for the ASIC.
773                  * GJ - 28 Nov 1999
774                  */
775                  if (chan->state == HSCX_IDLE)
776                  {
777                         NDBGL1(L1_H_XFRERR, "toss data from %d", h_chan);
778                         error++;
779                  }
780
781                 fifo_data_len = ((stat & HSCX_STAT_RML_MASK) >> 8);
782                 
783                 if(fifo_data_len == 0)
784                         fifo_data_len = sc->sc_bfifolen;
785
786                 /* ALWAYS read data from HSCX fifo */
787         
788                 HSCX_RDFIFO(h_chan, scrbuf, fifo_data_len);
789                 chan->rxcount += fifo_data_len;
790
791                 /* all error conditions checked, now decide and take action */
792                 
793                 if(error == 0)
794                 {
795                         if(chan->in_mbuf == NULL)
796                         {
797                                 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
798                                         panic("L1 avma1pp_hscx_intr: RME, cannot allocate mbuf!\n");
799                                 chan->in_cbptr = chan->in_mbuf->m_data;
800                                 chan->in_len = 0;
801                         }
802
803                         if((chan->in_len + fifo_data_len) <= BCH_MAX_DATALEN)
804                         {
805                                 /* OK to copy the data */
806                                 bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
807                                 chan->in_cbptr += fifo_data_len;
808                                 chan->in_len += fifo_data_len;
809
810                                 /* setup mbuf data length */
811                                         
812                                 chan->in_mbuf->m_len = chan->in_len;
813                                 chan->in_mbuf->m_pkthdr.len = chan->in_len;
814
815                                 if(sc->sc_trace & TRACE_B_RX)
816                                 {
817                                         i4b_trace_hdr_t hdr;
818                                         hdr.unit = L0IFPIUNIT(sc->sc_unit);
819                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
820                                         hdr.dir = FROM_NT;
821                                         hdr.count = ++sc->sc_trace_bcount;
822                                         MICROTIME(hdr.time);
823                                         i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
824                                 }
825
826                                 if (stat & HSCX_STAT_RME)
827                                 {
828                                   if((stat & HSCX_STAT_CRCVFRRAB) == HSCX_STAT_CRCVFR)
829                                   {
830                                          (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
831                                          activity = ACT_RX;
832                                 
833                                          /* mark buffer ptr as unused */
834                                         
835                                          chan->in_mbuf = NULL;
836                                          chan->in_cbptr = NULL;
837                                          chan->in_len = 0;
838                                   }
839                                   else
840                                   {
841                                                 chan->stat_CRC++;
842                                                 NDBGL1(L1_H_XFRERR, "CRC/RAB");
843                                           if (chan->in_mbuf != NULL)
844                                           {
845                                                   i4b_Bfreembuf(chan->in_mbuf);
846                                                   chan->in_mbuf = NULL;
847                                                   chan->in_cbptr = NULL;
848                                                   chan->in_len = 0;
849                                           }
850                                   }
851                                 }
852                         } /* END enough space in mbuf */
853                         else
854                         {
855                                  if(chan->bprot == BPROT_NONE)
856                                  {
857                                           /* setup mbuf data length */
858                                 
859                                           chan->in_mbuf->m_len = chan->in_len;
860                                           chan->in_mbuf->m_pkthdr.len = chan->in_len;
861
862                                           if(sc->sc_trace & TRACE_B_RX)
863                                           {
864                                                         i4b_trace_hdr_t hdr;
865                                                         hdr.unit = L0IFPIUNIT(sc->sc_unit);
866                                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
867                                                         hdr.dir = FROM_NT;
868                                                         hdr.count = ++sc->sc_trace_bcount;
869                                                         MICROTIME(hdr.time);
870                                                         i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
871                                                 }
872
873                                           if(!(i4b_l1_bchan_tel_silence(chan->in_mbuf->m_data, chan->in_mbuf->m_len)))
874                                                  activity = ACT_RX;
875                                 
876                                           /* move rx'd data to rx queue */
877
878 #if defined (__FreeBSD__) && __FreeBSD__ > 4
879                                           (void) IF_HANDOFF(&chan->rx_queue, chan->in_mbuf, NULL);
880 #else
881                                           if(!(IF_QFULL(&chan->rx_queue)))
882                                           {
883                                                 IF_ENQUEUE(&chan->rx_queue, chan->in_mbuf);
884                                           }
885                                           else
886                                           {
887                                                 i4b_Bfreembuf(chan->in_mbuf);
888                                           }
889 #endif                                  
890                                           /* signal upper layer that data are available */
891                                           (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
892
893                                           /* alloc new buffer */
894                                 
895                                           if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
896                                                  panic("L1 avma1pp_hscx_intr: RPF, cannot allocate new mbuf!\n");
897         
898                                           /* setup new data ptr */
899                                 
900                                           chan->in_cbptr = chan->in_mbuf->m_data;
901         
902                                           /* OK to copy the data */
903                                           bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
904
905                                           chan->in_cbptr += fifo_data_len;
906                                           chan->in_len = fifo_data_len;
907
908                                           chan->rxcount += fifo_data_len;
909                                         }
910                                  else
911                                         {
912                                           NDBGL1(L1_H_XFRERR, "RAWHDLC rx buffer overflow in RPF, in_len=%d", chan->in_len);
913                                           chan->in_cbptr = chan->in_mbuf->m_data;
914                                           chan->in_len = 0;
915                                         }
916                           }
917                 } /* if(error == 0) */
918                 else
919                 {
920                         /* land here for RDO */
921                         if (chan->in_mbuf != NULL)
922                         {
923                                 i4b_Bfreembuf(chan->in_mbuf);
924                                 chan->in_mbuf = NULL;
925                                 chan->in_cbptr = NULL;
926                                 chan->in_len = 0;
927                         }
928                         sc->avma1pp_txl = 0;
929                         sc->avma1pp_cmd |= HSCX_CMD_RRS;
930                         AVMA1PPSETCMDLONG(param);
931                         hscx_write_reg(h_chan, HSCX_STAT, param, sc);
932                         sc->avma1pp_cmd &= ~HSCX_CMD_RRS;
933                         AVMA1PPSETCMDLONG(param);
934                         hscx_write_reg(h_chan, HSCX_STAT, param, sc);
935                 }
936         }
937
938
939         /* transmit fifo empty, new data can be written to fifo */
940         
941         if(stat & HSCX_INT_XPR)
942         {
943                 /*
944                  * for a description what is going on here, please have
945                  * a look at isic_bchannel_start() in i4b_bchan.c !
946                  */
947
948                 NDBGL1(L1_H_IRQ, "unit %d, chan %d - XPR, Tx Fifo Empty!", sc->sc_unit, h_chan);
949
950                 if(chan->out_mbuf_cur == NULL)  /* last frame is transmitted */
951                 {
952                         IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
953
954                         if(chan->out_mbuf_head == NULL)
955                         {
956                                 chan->state &= ~HSCX_TX_ACTIVE;
957                                 (*chan->isic_drvr_linktab->bch_tx_queue_empty)(chan->isic_drvr_linktab->unit);
958                         }
959                         else
960                         {
961                                 chan->state |= HSCX_TX_ACTIVE;
962                                 chan->out_mbuf_cur = chan->out_mbuf_head;
963                                 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
964                                 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
965
966                                 if(sc->sc_trace & TRACE_B_TX)
967                                 {
968                                         i4b_trace_hdr_t hdr;
969                                         hdr.unit = L0IFPIUNIT(sc->sc_unit);
970                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
971                                         hdr.dir = FROM_TE;
972                                         hdr.count = ++sc->sc_trace_bcount;
973                                         MICROTIME(hdr.time);
974                                         i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
975                                 }
976                                 
977                                 if(chan->bprot == BPROT_NONE)
978                                 {
979                                         if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
980                                                 activity = ACT_TX;
981                                 }
982                                 else
983                                 {
984                                         activity = ACT_TX;
985                                 }
986                         }
987                 }
988                         
989                 avma1pp_hscx_fifo(chan, sc);
990         }
991
992         /* call timeout handling routine */
993         
994         if(activity == ACT_RX || activity == ACT_TX)
995                 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
996 }
997
998 /*
999  * this is the main routine which checks each channel and then calls
1000  * the real interrupt routine as appropriate
1001  */
1002 static void
1003 avma1pp_hscx_int_handler(struct l1_softc *sc)
1004 {
1005         u_int stat;
1006
1007         /* has to be a u_int because the byte count is in the 2nd byte */
1008         stat = hscx_read_reg_int(0, HSCX_STAT, sc);
1009         if (stat & HSCX_INT_MASK)
1010           avma1pp_hscx_intr(0, stat, sc);
1011         stat = hscx_read_reg_int(1, HSCX_STAT, sc);
1012         if (stat & HSCX_INT_MASK)
1013           avma1pp_hscx_intr(1, stat, sc);
1014 }
1015
1016 static void
1017 avma1pp_disable(device_t dev)
1018 {
1019         struct l1_softc *sc = device_get_softc(dev);
1020         bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
1021         bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
1022
1023         bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_RESET_ALL|ASL_TIMERDISABLE);
1024 }
1025
1026 static void
1027 avma1pp_intr(void *xsc)
1028 {
1029         u_char stat;
1030         struct l1_softc *sc;
1031         bus_space_handle_t bhandle;
1032         bus_space_tag_t btag; 
1033
1034         sc = xsc;
1035         bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
1036         btag = rman_get_bustag(sc->sc_resources.io_base[0]); 
1037
1038         stat = bus_space_read_1(btag, bhandle, STAT0_OFFSET);
1039         NDBGL1(L1_H_IRQ, "stat %x", stat);
1040         /* was there an interrupt from this card ? */
1041         if ((stat & ASL_IRQ_Pending) == ASL_IRQ_Pending)
1042                 return; /* no */
1043         /* interrupts are low active */
1044         if (!(stat & ASL_IRQ_TIMER))
1045           NDBGL1(L1_H_IRQ, "timer interrupt ???");
1046         if (!(stat & ASL_IRQ_HSCX))
1047         {
1048           NDBGL1(L1_H_IRQ, "HSCX");
1049                 avma1pp_hscx_int_handler(sc);
1050         }
1051         if (!(stat & ASL_IRQ_ISAC))
1052         {
1053           NDBGL1(L1_H_IRQ, "ISAC");
1054                 ifpi_isac_intr(sc);
1055         }
1056 }
1057
1058 static void
1059 avma1pp_hscx_init(struct l1_softc *sc, int h_chan, int activate)
1060 {
1061         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1062         u_int param = 0;
1063
1064         NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1065                 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
1066
1067         if (activate == 0)
1068         {
1069                 /* only deactivate if both channels are idle */
1070                 if (sc->sc_chan[HSCX_CH_A].state != HSCX_IDLE ||
1071                         sc->sc_chan[HSCX_CH_B].state != HSCX_IDLE)
1072                 {
1073                         return;
1074                 }
1075                 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1076                 sc->avma1pp_prot = HSCX_MODE_TRANS;
1077                 AVMA1PPSETCMDLONG(param);
1078                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
1079                 return;
1080         }
1081         if(chan->bprot == BPROT_RHDLC)
1082         {
1083                   NDBGL1(L1_BCHAN, "BPROT_RHDLC");
1084
1085                 /* HDLC Frames, transparent mode 0 */
1086                 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1087                 sc->avma1pp_prot = HSCX_MODE_ITF_FLG;
1088                 AVMA1PPSETCMDLONG(param);
1089                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
1090                 sc->avma1pp_cmd = HSCX_CMD_XRS;
1091                 AVMA1PPSETCMDLONG(param);
1092                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
1093                 sc->avma1pp_cmd = 0;
1094         }
1095         else
1096         {
1097                   NDBGL1(L1_BCHAN, "BPROT_NONE??");
1098
1099                 /* Raw Telephony, extended transparent mode 1 */
1100                 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1101                 sc->avma1pp_prot = HSCX_MODE_TRANS;
1102                 AVMA1PPSETCMDLONG(param);
1103                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
1104                 sc->avma1pp_cmd = HSCX_CMD_XRS;
1105                 AVMA1PPSETCMDLONG(param);
1106                 hscx_write_reg(h_chan, HSCX_STAT, param, sc);
1107                 sc->avma1pp_cmd = 0;
1108         }
1109 }
1110
1111 static void
1112 avma1pp_bchannel_setup(int unit, int h_chan, int bprot, int activate)
1113 {
1114 #ifdef __FreeBSD__
1115         struct l1_softc *sc = ifpi_scp[unit];
1116 #else
1117         struct l1_softc *sc = isic_find_sc(unit);
1118 #endif
1119         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1120
1121         int s = SPLI4B();
1122         
1123         if(activate == 0)
1124         {
1125                 /* deactivation */
1126                 chan->state = HSCX_IDLE;
1127                 avma1pp_hscx_init(sc, h_chan, activate);
1128         }
1129                 
1130         NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1131                 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
1132
1133         /* general part */
1134
1135         chan->unit = sc->sc_unit;       /* unit number */
1136         chan->channel = h_chan;         /* B channel */
1137         chan->bprot = bprot;            /* B channel protocol */
1138         chan->state = HSCX_IDLE;        /* B channel state */
1139
1140         /* receiver part */
1141
1142         chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
1143
1144         i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
1145
1146         chan->rxcount = 0;              /* reset rx counter */
1147         
1148         i4b_Bfreembuf(chan->in_mbuf);   /* clean rx mbuf */
1149
1150         chan->in_mbuf = NULL;           /* reset mbuf ptr */
1151         chan->in_cbptr = NULL;          /* reset mbuf curr ptr */
1152         chan->in_len = 0;               /* reset mbuf data len */
1153         
1154         /* transmitter part */
1155
1156         chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
1157         
1158         i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
1159
1160         chan->txcount = 0;              /* reset tx counter */
1161         
1162         i4b_Bfreembuf(chan->out_mbuf_head);     /* clean tx mbuf */
1163
1164         chan->out_mbuf_head = NULL;     /* reset head mbuf ptr */
1165         chan->out_mbuf_cur = NULL;      /* reset current mbuf ptr */    
1166         chan->out_mbuf_cur_ptr = NULL;  /* reset current mbuf data ptr */
1167         chan->out_mbuf_cur_len = 0;     /* reset current mbuf data cnt */
1168         
1169         if(activate != 0)
1170         {
1171                 /* activation */
1172                 avma1pp_hscx_init(sc, h_chan, activate);
1173                 chan->state |= HSCX_AVMA1PP_ACTIVE;
1174         }
1175
1176         splx(s);
1177 }
1178
1179 static void
1180 avma1pp_bchannel_start(int unit, int h_chan)
1181 {
1182 #ifdef __FreeBSD__
1183         struct l1_softc *sc = ifpi_scp[unit];
1184 #else
1185         struct l1_softc *sc = isic_find_sc(unit);
1186 #endif
1187         register l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1188         int s;
1189         int activity = -1;
1190
1191         s = SPLI4B();                           /* enter critical section */
1192         if(chan->state & HSCX_TX_ACTIVE)        /* already running ? */
1193         {
1194                 splx(s);
1195                 return;                         /* yes, leave */
1196         }
1197
1198         /* get next mbuf from queue */
1199         
1200         IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
1201         
1202         if(chan->out_mbuf_head == NULL)         /* queue empty ? */
1203         {
1204                 splx(s);                        /* leave critical section */
1205                 return;                         /* yes, exit */
1206         }
1207
1208         /* init current mbuf values */
1209         
1210         chan->out_mbuf_cur = chan->out_mbuf_head;
1211         chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1212         chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;    
1213         
1214         /* activity indicator for timeout handling */
1215
1216         if(chan->bprot == BPROT_NONE)
1217         {
1218                 if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
1219                         activity = ACT_TX;
1220         }
1221         else
1222         {
1223                 activity = ACT_TX;
1224         }
1225
1226         chan->state |= HSCX_TX_ACTIVE;          /* we start transmitting */
1227         
1228         if(sc->sc_trace & TRACE_B_TX)   /* if trace, send mbuf to trace dev */
1229         {
1230                 i4b_trace_hdr_t hdr;
1231                 hdr.unit = L0IFPIUNIT(sc->sc_unit);
1232                 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1233                 hdr.dir = FROM_TE;
1234                 hdr.count = ++sc->sc_trace_bcount;
1235                 MICROTIME(hdr.time);
1236                 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1237         }                       
1238
1239         avma1pp_hscx_fifo(chan, sc);
1240
1241         /* call timeout handling routine */
1242         
1243         if(activity == ACT_RX || activity == ACT_TX)
1244                 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
1245
1246         splx(s);        
1247 }
1248
1249 /*---------------------------------------------------------------------------*
1250  *      return the address of isic drivers linktab      
1251  *---------------------------------------------------------------------------*/
1252 static isdn_link_t *
1253 avma1pp_ret_linktab(int unit, int channel)
1254 {
1255 #ifdef __FreeBSD__
1256         struct l1_softc *sc = ifpi_scp[unit];
1257 #else
1258         struct l1_softc *sc = isic_find_sc(unit);
1259 #endif
1260         l1_bchan_state_t *chan = &sc->sc_chan[channel];
1261
1262         return(&chan->isic_isdn_linktab);
1263 }
1264  
1265 /*---------------------------------------------------------------------------*
1266  *      set the driver linktab in the b channel softc
1267  *---------------------------------------------------------------------------*/
1268 static void
1269 avma1pp_set_linktab(int unit, int channel, drvr_link_t *dlt)
1270 {
1271 #ifdef __FreeBSD__
1272         struct l1_softc *sc = ifpi_scp[unit];
1273 #else
1274         struct l1_softc *sc = isic_find_sc(unit);
1275 #endif
1276         l1_bchan_state_t *chan = &sc->sc_chan[channel];
1277
1278         chan->isic_drvr_linktab = dlt;
1279 }
1280
1281
1282 /*---------------------------------------------------------------------------*
1283  *      initialize our local linktab
1284  *---------------------------------------------------------------------------*/
1285 static void
1286 avma1pp_init_linktab(struct l1_softc *sc)
1287 {
1288         l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
1289         isdn_link_t *lt = &chan->isic_isdn_linktab;
1290
1291         /* make sure the hardware driver is known to layer 4 */
1292         /* avoid overwriting if already set */
1293         if (ctrl_types[CTRL_PASSIVE].set_linktab == NULL)
1294         {
1295                 ctrl_types[CTRL_PASSIVE].set_linktab = avma1pp_set_linktab;
1296                 ctrl_types[CTRL_PASSIVE].get_linktab = avma1pp_ret_linktab;
1297         }
1298
1299         /* local setup */
1300         lt->unit = sc->sc_unit;
1301         lt->channel = HSCX_CH_A;
1302         lt->bch_config = avma1pp_bchannel_setup;
1303         lt->bch_tx_start = avma1pp_bchannel_start;
1304         lt->bch_stat = avma1pp_bchannel_stat;
1305         lt->tx_queue = &chan->tx_queue;
1306
1307         /* used by non-HDLC data transfers, i.e. telephony drivers */
1308         lt->rx_queue = &chan->rx_queue;
1309
1310         /* used by HDLC data transfers, i.e. ipr and isp drivers */     
1311         lt->rx_mbuf = &chan->in_mbuf;   
1312                                                 
1313         chan = &sc->sc_chan[HSCX_CH_B];
1314         lt = &chan->isic_isdn_linktab;
1315
1316         lt->unit = sc->sc_unit;
1317         lt->channel = HSCX_CH_B;
1318         lt->bch_config = avma1pp_bchannel_setup;
1319         lt->bch_tx_start = avma1pp_bchannel_start;
1320         lt->bch_stat = avma1pp_bchannel_stat;
1321         lt->tx_queue = &chan->tx_queue;
1322
1323         /* used by non-HDLC data transfers, i.e. telephony drivers */
1324         lt->rx_queue = &chan->rx_queue;
1325
1326         /* used by HDLC data transfers, i.e. ipr and isp drivers */     
1327         lt->rx_mbuf = &chan->in_mbuf;   
1328 }
1329
1330 /*
1331  * use this instead of isic_bchannel_stat in i4b_bchan.c because it's static
1332  */
1333 static void
1334 avma1pp_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
1335 {
1336 #ifdef __FreeBSD__
1337         struct l1_softc *sc = ifpi_scp[unit];
1338 #else
1339         struct l1_softc *sc = isic_find_sc(unit);
1340 #endif
1341         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1342         int s;
1343
1344         s = SPLI4B();
1345         
1346         bsp->outbytes = chan->txcount;
1347         bsp->inbytes = chan->rxcount;
1348
1349         chan->txcount = 0;
1350         chan->rxcount = 0;
1351
1352         splx(s);
1353 }
1354
1355 /*---------------------------------------------------------------------------*
1356  *      fill HSCX fifo with data from the current mbuf
1357  *      Put this here until it can go into i4b_hscx.c
1358  *---------------------------------------------------------------------------*/
1359 static int
1360 avma1pp_hscx_fifo(l1_bchan_state_t *chan, struct l1_softc *sc)
1361 {
1362         int len;
1363         int nextlen;
1364         int i;
1365         int cmd = 0;
1366         /* using a scratch buffer simplifies writing to the FIFO */
1367         u_char scrbuf[HSCX_FIFO_LEN];
1368
1369         len = 0;
1370
1371         /*
1372          * fill the HSCX tx fifo with data from the current mbuf. if
1373          * current mbuf holds less data than HSCX fifo length, try to
1374          * get the next mbuf from (a possible) mbuf chain. if there is
1375          * not enough data in a single mbuf or in a chain, then this
1376          * is the last mbuf and we tell the HSCX that it has to send
1377          * CRC and closing flag
1378          */
1379          
1380         while(chan->out_mbuf_cur && len != sc->sc_bfifolen)
1381         {
1382                 nextlen = min(chan->out_mbuf_cur_len, sc->sc_bfifolen - len);
1383
1384 #ifdef NOTDEF
1385                 printf("i:mh=%p, mc=%p, mcp=%p, mcl=%d l=%d nl=%d # ",
1386                         chan->out_mbuf_head,
1387                         chan->out_mbuf_cur,                     
1388                         chan->out_mbuf_cur_ptr,
1389                         chan->out_mbuf_cur_len,
1390                         len,
1391                         nextlen);
1392 #endif
1393
1394                 cmd |= HSCX_CMDR_XTF;
1395                 /* collect the data in the scratch buffer */
1396                 for (i = 0; i < nextlen; i++)
1397                         scrbuf[i + len] = chan->out_mbuf_cur_ptr[i];
1398
1399                 len += nextlen;
1400                 chan->txcount += nextlen;
1401         
1402                 chan->out_mbuf_cur_ptr += nextlen;
1403                 chan->out_mbuf_cur_len -= nextlen;
1404                         
1405                 if(chan->out_mbuf_cur_len == 0) 
1406                 {
1407                         if((chan->out_mbuf_cur = chan->out_mbuf_cur->m_next) != NULL)
1408                         {
1409                                 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
1410                                 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1411         
1412                                 if(sc->sc_trace & TRACE_B_TX)
1413                                 {
1414                                         i4b_trace_hdr_t hdr;
1415                                         hdr.unit = L0IFPIUNIT(sc->sc_unit);
1416                                         hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1417                                         hdr.dir = FROM_TE;
1418                                         hdr.count = ++sc->sc_trace_bcount;
1419                                         MICROTIME(hdr.time);
1420                                         i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1421                                 }
1422                         }
1423                         else
1424                         {
1425                                 if (chan->bprot != BPROT_NONE)
1426                                         cmd |= HSCX_CMDR_XME;
1427                                 i4b_Bfreembuf(chan->out_mbuf_head);
1428                                 chan->out_mbuf_head = NULL;
1429                         }
1430                 }
1431         }
1432         /* write what we have from the scratch buf to the HSCX fifo */
1433         if (len != 0)
1434                 HSCX_WRFIFO(chan->channel, scrbuf, len);
1435         return(cmd);
1436 }
1437
1438 /*---------------------------------------------------------------------------*
1439  *      ifpi - ISAC interrupt routine
1440  *---------------------------------------------------------------------------*/
1441 static void
1442 ifpi_isac_intr(struct l1_softc *sc)
1443 {
1444         register u_char isac_irq_stat;
1445
1446         for(;;)
1447         {
1448                 /* get isac irq status */
1449                 isac_irq_stat = ISAC_READ(I_ISTA);
1450
1451                 if(isac_irq_stat)
1452                         ifpi_isac_irq(sc, isac_irq_stat); /* isac handler */
1453                 else
1454                         break;
1455         }
1456
1457         ISAC_WRITE(I_MASK, 0xff);
1458
1459         DELAY(100);
1460
1461         ISAC_WRITE(I_MASK, ISAC_IMASK);
1462 }
1463
1464 /*---------------------------------------------------------------------------*
1465  *      ifpi_recover - try to recover from irq lockup
1466  *---------------------------------------------------------------------------*/
1467 void
1468 ifpi_recover(struct l1_softc *sc)
1469 {
1470         u_char byte;
1471         
1472         /* get isac irq status */
1473
1474         byte = ISAC_READ(I_ISTA);
1475
1476         NDBGL1(L1_ERROR, "  ISAC: ISTA = 0x%x", byte);
1477         
1478         if(byte & ISAC_ISTA_EXI)
1479                 NDBGL1(L1_ERROR, "  ISAC: EXIR = 0x%x", (u_char)ISAC_READ(I_EXIR));
1480
1481         if(byte & ISAC_ISTA_CISQ)
1482         {
1483                 byte = ISAC_READ(I_CIRR);
1484         
1485                 NDBGL1(L1_ERROR, "  ISAC: CISQ = 0x%x", byte);
1486                 
1487                 if(byte & ISAC_CIRR_SQC)
1488                         NDBGL1(L1_ERROR, "  ISAC: SQRR = 0x%x", (u_char)ISAC_READ(I_SQRR));
1489         }
1490
1491         NDBGL1(L1_ERROR, "  ISAC: IMASK = 0x%x", ISAC_IMASK);
1492
1493         ISAC_WRITE(I_MASK, 0xff);       
1494         DELAY(100);
1495         ISAC_WRITE(I_MASK, ISAC_IMASK);
1496 }
1497
1498
1499 #endif /* NIFPI > 0 */