Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / net / i4b / layer2 / i4b_l2.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_l2.c - ISDN layer 2 (Q.921)
28  *      -------------------------------
29  *
30  * $FreeBSD: src/sys/i4b/layer2/i4b_l2.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
31  * $DragonFly: src/sys/net/i4b/layer2/i4b_l2.c,v 1.10 2006/01/14 11:05:18 swildner Exp $
32  *
33  *      last edit-date: [Fri Jan 12 16:43:31 2001]
34  *
35  *---------------------------------------------------------------------------*/
36
37 #include "use_i4bq921.h"
38 #if NI4BQ921 > 0
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/thread2.h>
45 #include <net/if.h>
46
47 #include <net/i4b/include/machine/i4b_debug.h>
48
49 #include "../include/i4b_l1l2.h"
50 #include "../include/i4b_l2l3.h"
51 #include "../include/i4b_mbuf.h"
52 #include "../include/i4b_global.h"
53
54 #include "i4b_l2.h"
55 #include "i4b_l2fsm.h"
56
57 int i4b_dl_establish_ind(int);
58 int i4b_dl_establish_cnf(int);
59 int i4b_dl_release_ind(int);
60 int i4b_dl_release_cnf(int);
61 int i4b_dl_data_ind(int, struct mbuf *);
62 int i4b_dl_unit_data_ind(int, struct mbuf *);
63
64 static int i4b_mdl_command_req(int, int, void *);
65
66 /* from layer 2 */
67
68 extern int i4b_mdl_status_ind(int, int, int);
69
70 /* this layers debug level */
71
72 unsigned int i4b_l2_debug = L2_DEBUG_DEFAULT;
73
74 struct i4b_l2l3_func i4b_l2l3_func = {
75
76         /* Layer 2 --> Layer 3 */
77         
78         (int (*)(int))                          i4b_dl_establish_ind,
79         (int (*)(int))                          i4b_dl_establish_cnf,
80         (int (*)(int))                          i4b_dl_release_ind,
81         (int (*)(int))                          i4b_dl_release_cnf,
82         (int (*)(int, struct mbuf *))           i4b_dl_data_ind,
83         (int (*)(int, struct mbuf *))           i4b_dl_unit_data_ind,
84
85         /* Layer 3 --> Layer 2 */
86
87         (int (*)(int))                          i4b_dl_establish_req,
88         (int (*)(int))                          i4b_dl_release_req,
89         (int (*)(int, struct mbuf *))           i4b_dl_data_req,
90         (int (*)(int, struct mbuf *))           i4b_dl_unit_data_req,
91
92         /* Layer 2 --> Layer 3 management */
93         
94         (int (*)(int, int, int))                i4b_mdl_status_ind,
95
96         /* Layer 3  --> Layer 2 management */
97         
98         (int (*)(int, int, void *))             i4b_mdl_command_req     
99 };
100
101 /*---------------------------------------------------------------------------*
102  *      DL_ESTABLISH_REQ from layer 3
103  *---------------------------------------------------------------------------*/
104 int
105 i4b_dl_establish_req(int unit)
106 {
107         l2_softc_t *l2sc = &l2_softc[unit];
108         
109         NDBGL2(L2_PRIM, "unit %d",unit);
110         i4b_l1_activate(l2sc);
111         i4b_next_l2state(l2sc, EV_DLESTRQ);
112         return(0);
113 }
114
115 /*---------------------------------------------------------------------------*
116  *      DL_RELEASE_REQ from layer 3
117  *---------------------------------------------------------------------------*/
118 int
119 i4b_dl_release_req(int unit)
120 {
121         l2_softc_t *l2sc = &l2_softc[unit];
122
123         NDBGL2(L2_PRIM, "unit %d",unit);        
124         i4b_next_l2state(l2sc, EV_DLRELRQ);
125         return(0);      
126 }
127
128 /*---------------------------------------------------------------------------*
129  *      DL UNIT DATA REQUEST from Layer 3
130  *---------------------------------------------------------------------------*/
131 int
132 i4b_dl_unit_data_req(int unit, struct mbuf *m)
133 {
134 #ifdef NOTDEF
135         NDBGL2(L2_PRIM, "unit %d",unit);
136 #endif
137         return(0);
138 }
139
140 /*---------------------------------------------------------------------------*
141  *      DL DATA REQUEST from Layer 3
142  *---------------------------------------------------------------------------*/
143 int
144 i4b_dl_data_req(int unit, struct mbuf *m)
145 {
146         l2_softc_t *l2sc = &l2_softc[unit];
147
148 #ifdef NOTDEF
149         NDBGL2(L2_PRIM, "unit %d",unit);
150 #endif
151         switch(l2sc->Q921_state)
152         {
153                 case ST_AW_EST:
154                 case ST_MULTIFR:
155                 case ST_TIMREC:
156                 
157                         if(IF_QFULL(&l2sc->i_queue))
158                         {
159                                 NDBGL2(L2_ERROR, "i_queue full!!");
160                                 i4b_Dfreembuf(m);
161                         }
162                         else
163                         {
164                                 CRIT_VAR;
165
166                                 CRIT_BEG;
167                                 IF_ENQUEUE(&l2sc->i_queue, m);
168                                 CRIT_END;
169
170                                 i4b_i_frame_queued_up(l2sc);
171                         }
172                         break;
173                         
174                 default:
175                         NDBGL2(L2_ERROR, "unit %d ERROR in state [%s], freeing mbuf", unit, i4b_print_l2state(l2sc));
176                         i4b_Dfreembuf(m);
177                         break;
178         }               
179         return(0);
180 }
181
182 /*---------------------------------------------------------------------------*
183  *      i4b_ph_activate_ind - link activation indication from layer 1
184  *---------------------------------------------------------------------------*/
185 int
186 i4b_ph_activate_ind(int unit)
187 {
188         l2_softc_t *l2sc = &l2_softc[unit];
189
190         NDBGL1(L1_PRIM, "unit %d",unit);
191         l2sc->ph_active = PH_ACTIVE;
192         return(0);
193 }
194
195 /*---------------------------------------------------------------------------*
196  *      i4b_ph_deactivate_ind - link deactivation indication from layer 1
197  *---------------------------------------------------------------------------*/
198 int
199 i4b_ph_deactivate_ind(int unit)
200 {
201         l2_softc_t *l2sc = &l2_softc[unit];
202
203         NDBGL1(L1_PRIM, "unit %d",unit);
204         l2sc->ph_active = PH_INACTIVE;
205         return(0);
206 }
207
208
209 /*---------------------------------------------------------------------------*
210  *      i4b_l2_unit_init - place layer 2 unit into known state
211  *---------------------------------------------------------------------------*/
212 static void
213 i4b_l2_unit_init(int unit)
214 {
215         l2_softc_t *l2sc = &l2_softc[unit];
216         CRIT_VAR;
217
218         CRIT_BEG;
219         l2sc->Q921_state = ST_TEI_UNAS;
220         l2sc->tei_valid = TEI_INVALID;
221         l2sc->vr = 0;
222         l2sc->vs = 0;
223         l2sc->va = 0;
224         l2sc->ack_pend = 0;
225         l2sc->rej_excpt = 0;
226         l2sc->peer_busy = 0;
227         l2sc->own_busy = 0;
228         l2sc->l3initiated = 0;
229
230         l2sc->rxd_CR = 0;
231         l2sc->rxd_PF = 0;
232         l2sc->rxd_NR = 0;
233         l2sc->RC = 0;
234         l2sc->iframe_sent = 0;
235                 
236         l2sc->postfsmfunc = NULL;
237
238         if(l2sc->ua_num != UA_EMPTY)
239         {
240                 i4b_Dfreembuf(l2sc->ua_frame);
241                 l2sc->ua_num = UA_EMPTY;
242                 l2sc->ua_frame = NULL;
243         }
244
245         i4b_T200_stop(l2sc);
246         i4b_T202_stop(l2sc);
247         i4b_T203_stop(l2sc);
248
249         CRIT_END;       
250 }
251
252 /*---------------------------------------------------------------------------*
253  *      i4b_mph_status_ind - status indication upward
254  *---------------------------------------------------------------------------*/
255 int
256 i4b_mph_status_ind(int unit, int status, int parm)
257 {
258         l2_softc_t *l2sc = &l2_softc[unit];
259         int sendup = 1;
260         CRIT_VAR;
261         
262         CRIT_BEG;
263
264         NDBGL1(L1_PRIM, "unit %d, status=%d, parm=%d", unit, status, parm);
265
266         switch(status)
267         {
268                 case STI_ATTACH:
269                         l2sc->unit = unit;
270                         l2sc->i_queue.ifq_maxlen = IQUEUE_MAXLEN;
271
272                         l2sc->ua_frame = NULL;
273                         bzero(&l2sc->stat, sizeof(lapdstat_t));                 
274                         
275                         /* initialize the callout handles for timeout routines */
276                         callout_init(&l2sc->T200_timeout);
277                         callout_init(&l2sc->T202_timeout);
278                         callout_init(&l2sc->T203_timeout);
279                         callout_init(&l2sc->IFQU_timeout);
280
281                         i4b_l2_unit_init(unit);
282                         break;
283
284                 case STI_L1STAT:        /* state of layer 1 */
285                         break;
286                 
287                 case STI_PDEACT:        /* Timer 4 expired */
288 /*XXX*/                 if((l2sc->Q921_state >= ST_AW_EST) &&
289                            (l2sc->Q921_state <= ST_TIMREC))
290                         {
291                                 NDBGL2(L2_ERROR, "unit %d, persistent deactivation!", unit);
292                                 i4b_l2_unit_init(unit);
293                         }
294                         else
295                         {
296                                 sendup = 0;
297                         }
298                         break;
299
300                 case STI_NOL1ACC:
301                         i4b_l2_unit_init(unit);
302                         NDBGL2(L2_ERROR, "unit %d, cannot access S0 bus!", unit);
303                         break;
304                         
305                 default:
306                         NDBGL2(L2_ERROR, "ERROR, unit %d, unknown status message!", unit);
307                         break;
308         }
309         
310         if(sendup)
311                 MDL_Status_Ind(unit, status, parm);  /* send up to layer 3 */
312
313         CRIT_END;
314         
315         return(0);
316 }
317
318 /*---------------------------------------------------------------------------*
319  *      MDL_COMMAND_REQ from layer 3
320  *---------------------------------------------------------------------------*/
321 int
322 i4b_mdl_command_req(int unit, int command, void * parm)
323 {
324         NDBGL2(L2_PRIM, "unit %d, command=%d, parm=%d", unit, command, (unsigned int)parm);
325
326         switch(command)
327         {
328                 case CMR_DOPEN:
329                         i4b_l2_unit_init(unit);
330                         break;
331         }               
332
333         MPH_Command_Req(unit, command, parm);
334         
335         return(0);
336 }
337
338 /*---------------------------------------------------------------------------*
339  *      i4b_ph_data_ind - process a rx'd frame got from layer 1
340  *---------------------------------------------------------------------------*/
341 int
342 i4b_ph_data_ind(int unit, struct mbuf *m)
343 {
344         l2_softc_t *l2sc = &l2_softc[unit];
345 #ifdef NOTDEF
346         NDBGL1(L1_PRIM, "unit %d", unit);
347 #endif
348         u_char *ptr = m->m_data;
349
350         if ( (*(ptr + OFF_CNTL) & 0x01) == 0 )
351         {
352                 if(m->m_len < 4)        /* 6 oct - 2 chksum oct */
353                 {
354                         l2sc->stat.err_rx_len++;
355                         NDBGL2(L2_ERROR, "ERROR, I-frame < 6 octetts!");
356                         i4b_Dfreembuf(m);
357                         return(0);
358                 }
359                 i4b_rxd_i_frame(unit, m);
360         }
361         else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x01 )
362         {
363                 if(m->m_len < 4)        /* 6 oct - 2 chksum oct */
364                 {
365                         l2sc->stat.err_rx_len++;
366                         NDBGL2(L2_ERROR, "ERROR, S-frame < 6 octetts!");
367                         i4b_Dfreembuf(m);
368                         return(0);
369                 }
370                 i4b_rxd_s_frame(unit, m);
371         }
372         else if ( (*(ptr + OFF_CNTL) & 0x03) == 0x03 )
373         {
374                 if(m->m_len < 3)        /* 5 oct - 2 chksum oct */
375                 {
376                         l2sc->stat.err_rx_len++;
377                         NDBGL2(L2_ERROR, "ERROR, U-frame < 5 octetts!");
378                         i4b_Dfreembuf(m);
379                         return(0);
380                 }
381                 i4b_rxd_u_frame(unit, m);
382         }
383         else
384         {
385                 l2sc->stat.err_rx_badf++;
386                 NDBGL2(L2_ERROR, "ERROR, bad frame rx'd - ");
387                 i4b_print_frame(m->m_len, m->m_data);
388                 i4b_Dfreembuf(m);
389         }
390         return(0);
391 }
392
393 #endif /* NI4BQ921 > 0 */