Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_l1.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b_l1.c - isdn4bsd layer 1 handler
28  *      -----------------------------------
29  *
30  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_l1.c,v 1.5.2.1 2001/08/10 14:08:38 obrien Exp $
31  *
32  *      last edit-date: [Wed Jan 24 09:12:03 2001]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include "isic.h"
37
38 #if NISIC > 0
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44
45 #include <net/if.h>
46
47 #include <machine/i4b_debug.h>
48 #include <machine/i4b_ioctl.h>
49 #include <machine/i4b_trace.h>
50
51 #include <i4b/layer1/isic/i4b_isic.h>
52 #include <i4b/layer1/isic/i4b_isac.h>
53
54 #include <i4b/layer1/i4b_l1.h>
55
56 #include <i4b/include/i4b_mbuf.h>
57 #include <i4b/include/i4b_global.h>
58
59 /*---------------------------------------------------------------------------*
60  *
61  *      L2 -> L1: PH-DATA-REQUEST
62  *      =========================
63  *
64  *      parms:
65  *              unit            physical interface unit number
66  *              m               mbuf containing L2 frame to be sent out
67  *              freeflag        MBUF_FREE: free mbuf here after having sent
68  *                                              it out
69  *                              MBUF_DONTFREE: mbuf is freed by Layer 2
70  *      returns:
71  *              ==0     fail, nothing sent out
72  *              !=0     ok, frame sent out
73  *
74  *---------------------------------------------------------------------------*/
75 int
76 isic_ph_data_req(int unit, struct mbuf *m, int freeflag)
77 {
78         u_char cmd;
79         int s;
80         struct l1_softc *sc = &l1_sc[unit];
81
82 #ifdef NOTDEF
83         NDBGL1(L1_PRIM, "unit %d, freeflag=%d", unit, freeflag);
84 #endif
85
86         if(m == NULL)                   /* failsafe */
87                 return (0);
88
89         s = SPLI4B();
90
91         if(sc->sc_I430state == ST_F3)   /* layer 1 not running ? */
92         {
93                 NDBGL1(L1_I_ERR, "still in state F3!");
94                 isic_ph_activate_req(unit);
95         }
96
97         if(sc->sc_state & ISAC_TX_ACTIVE)
98         {
99                 if(sc->sc_obuf2 == NULL)
100                 {
101                         sc->sc_obuf2 = m;               /* save mbuf ptr */
102
103                         if(freeflag)
104                                 sc->sc_freeflag2 = 1;   /* IRQ must mfree */
105                         else
106                                 sc->sc_freeflag2 = 0;   /* IRQ must not mfree */
107
108                         NDBGL1(L1_I_MSG, "using 2nd ISAC TX buffer, state = %s", isic_printstate(sc));
109
110                         if(sc->sc_trace & TRACE_D_TX)
111                         {
112                                 i4b_trace_hdr_t hdr;
113                                 hdr.unit = L0ISICUNIT(unit);
114                                 hdr.type = TRC_CH_D;
115                                 hdr.dir = FROM_TE;
116                                 hdr.count = ++sc->sc_trace_dcount;
117                                 MICROTIME(hdr.time);
118                                 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
119                         }
120                         splx(s);
121                         return(1);
122                 }
123
124                 NDBGL1(L1_I_ERR, "No Space in TX FIFO, state = %s", isic_printstate(sc));
125         
126                 if(freeflag == MBUF_FREE)
127                         i4b_Dfreembuf(m);                       
128         
129                 splx(s);
130                 return (0);
131         }
132
133         if(sc->sc_trace & TRACE_D_TX)
134         {
135                 i4b_trace_hdr_t hdr;
136                 hdr.unit = L0ISICUNIT(unit);
137                 hdr.type = TRC_CH_D;
138                 hdr.dir = FROM_TE;
139                 hdr.count = ++sc->sc_trace_dcount;
140                 MICROTIME(hdr.time);
141                 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
142         }
143         
144         sc->sc_state |= ISAC_TX_ACTIVE; /* set transmitter busy flag */
145
146         NDBGL1(L1_I_MSG, "ISAC_TX_ACTIVE set");
147
148         sc->sc_freeflag = 0;            /* IRQ must NOT mfree */
149         
150         ISAC_WRFIFO(m->m_data, min(m->m_len, ISAC_FIFO_LEN)); /* output to TX fifo */
151
152         if(m->m_len > ISAC_FIFO_LEN)    /* message > 32 bytes ? */
153         {
154                 sc->sc_obuf = m;        /* save mbuf ptr */
155                 sc->sc_op = m->m_data + ISAC_FIFO_LEN;  /* ptr for irq hdl */
156                 sc->sc_ol = m->m_len - ISAC_FIFO_LEN;   /* length for irq hdl */
157
158                 if(freeflag)
159                         sc->sc_freeflag = 1;    /* IRQ must mfree */
160                 
161                 cmd = ISAC_CMDR_XTF;
162         }
163         else
164         {
165                 sc->sc_obuf = NULL;
166                 sc->sc_op = NULL;
167                 sc->sc_ol = 0;
168
169                 if(freeflag)
170                         i4b_Dfreembuf(m);
171
172                 cmd = ISAC_CMDR_XTF | ISAC_CMDR_XME;
173         }
174
175         ISAC_WRITE(I_CMDR, cmd);
176         ISACCMDRWRDELAY();
177
178         splx(s);
179         
180         return(1);
181 }
182
183 /*---------------------------------------------------------------------------*
184  *
185  *      L2 -> L1: PH-ACTIVATE-REQUEST
186  *      =============================
187  *
188  *      parms:
189  *              unit    physical interface unit number
190  *
191  *      returns:
192  *              ==0     
193  *              !=0     
194  *
195  *---------------------------------------------------------------------------*/
196 int
197 isic_ph_activate_req(int unit)
198 {
199         struct l1_softc *sc = &l1_sc[unit];
200         NDBGL1(L1_PRIM, "unit %d", unit);
201         isic_next_state(sc, EV_PHAR);
202         return(0);
203 }
204
205 /*---------------------------------------------------------------------------*
206  *      command from the upper layers
207  *---------------------------------------------------------------------------*/
208 int
209 isic_mph_command_req(int unit, int command, void *parm)
210 {
211         struct l1_softc *sc = &l1_sc[unit];
212
213         switch(command)
214         {
215                 case CMR_DOPEN:         /* daemon running */
216                         NDBGL1(L1_PRIM, "unit %d, command = CMR_DOPEN", unit);
217                         sc->sc_enabled = 1;                     
218                         break;
219                         
220                 case CMR_DCLOSE:        /* daemon not running */
221                         NDBGL1(L1_PRIM, "unit %d, command = CMR_DCLOSE", unit);
222                         sc->sc_enabled = 0;
223                         break;
224
225                 case CMR_SETTRACE:
226                         NDBGL1(L1_PRIM, "unit %d, command = CMR_SETTRACE, parm = %d", unit, (unsigned int)parm);
227                         sc->sc_trace = (unsigned int)parm;
228                         break;
229                 
230                 case CMR_GCST:
231                         {
232                         struct chipstat *cst;
233                         NDBGL1(L1_PRIM, "unit %d, command = CMR_GCST, parm = %d", unit, (unsigned int)parm);
234                         cst = (struct chipstat *)parm;
235                         cst->driver_type = L1DRVR_ISIC;
236                         cst->stats.hscxstat.unit = sc->sc_unit;
237                         cst->stats.hscxstat.chan = cst->driver_bchannel;
238                         cst->stats.hscxstat.vfr = sc->sc_chan[cst->driver_bchannel].stat_VFR;
239                         cst->stats.hscxstat.rdo = sc->sc_chan[cst->driver_bchannel].stat_RDO;
240                         cst->stats.hscxstat.crc = sc->sc_chan[cst->driver_bchannel].stat_CRC;
241                         cst->stats.hscxstat.rab = sc->sc_chan[cst->driver_bchannel].stat_RAB;
242                         cst->stats.hscxstat.xdu = sc->sc_chan[cst->driver_bchannel].stat_XDU;
243                         cst->stats.hscxstat.rfo = sc->sc_chan[cst->driver_bchannel].stat_RFO;
244                         break;
245                         }
246                 default:
247                         NDBGL1(L1_ERROR, "ERROR, unknown command = %d, unit = %d, parm = %d", command, unit, (unsigned int)parm);
248                         break;
249         }
250
251         return(0);
252 }
253
254 #endif /* NISIC > 0 */