c4d72edd390779aa7ddd9814e1161b62619b55bd
[dragonfly.git] / sys / net / i4b / layer2 / i4b_uframe.c
1 /*
2  * Copyright (c) 1997, 2000 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_uframe.c - routines for handling U-frames
28  *      -----------------------------------------------
29  *
30  *      $Id: i4b_uframe.c,v 1.13 2000/08/24 11:48:58 hm Exp $ 
31  *
32  * $FreeBSD: src/sys/i4b/layer2/i4b_uframe.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
33  * $DragonFly: src/sys/net/i4b/layer2/i4b_uframe.c,v 1.4 2004/02/13 17:45:50 joerg Exp $
34  *
35  *      last edit-date: [Mon May 29 16:55:30 2000]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #if defined(__DragonFly__) || defined(__FreeBSD__)
40 #include "use_i4bq921.h"
41 #else
42 #define NI4BQ921        1
43 #endif
44 #if NI4BQ921 > 0
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <net/if.h>
51
52 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
53 #include <sys/callout.h>
54 #endif
55
56 #if defined(__DragonFly__) || defined(__FreeBSD__)
57 #include <net/i4b/include/machine/i4b_debug.h>
58 #else
59 #include <i4b/i4b_debug.h>
60 #include <i4b/i4b_ioctl.h>
61 #endif
62
63 #include "../include/i4b_l1l2.h"
64 #include "../include/i4b_l2l3.h"
65 #include "../include/i4b_mbuf.h"
66
67 #include "i4b_l2.h"
68 #include "i4b_l2fsm.h"
69
70 /*---------------------------------------------------------------------------*
71  *      process a received U-frame
72  *---------------------------------------------------------------------------*/
73 void
74 i4b_rxd_u_frame(int unit, struct mbuf *m)
75 {
76         l2_softc_t *l2sc = &l2_softc[unit];
77         u_char *ptr = m->m_data;
78
79         int sapi = GETSAPI(*(ptr + OFF_SAPI));
80         int tei = GETTEI(*(ptr + OFF_TEI));     
81         int pfbit = GETUPF(*(ptr + OFF_CNTL));
82         
83         switch(*(ptr + OFF_CNTL) & ~UPFBIT)
84         {
85                 /* commands */
86
87                 case SABME:
88                         if((l2sc->tei_valid == TEI_VALID) &&
89                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
90                         {
91                                 l2sc->stat.rx_sabme++;
92                                 NDBGL2(L2_U_MSG, "SABME, sapi = %d, tei = %d", sapi, tei);
93                                 l2sc->rxd_PF = pfbit;
94                                 i4b_next_l2state(l2sc, EV_RXSABME);
95                         }
96                         i4b_Dfreembuf(m);
97                         break;
98
99                 case UI:
100                         if(sapi == SAPI_L2M &&
101                            tei == GROUP_TEI &&
102                            *(ptr + OFF_MEI) == MEI)
103                         {
104                                 /* layer 2 management (SAPI = 63) */
105                                 l2sc->stat.rx_tei++;
106                                 i4b_tei_rxframe(unit, m);
107                         }
108                         else if(sapi == SAPI_CCP && tei == GROUP_TEI)
109                         {
110                                 /* call control (SAPI = 0) */
111                                 l2sc->stat.rx_ui++;
112                                 /* strip ui header */
113                                 m_adj(m, UI_HDR_LEN);
114                                 /* to upper layer */
115                                 DL_Unit_Data_Ind(unit, m);
116                         }
117                         else
118                         {
119                                 l2sc->stat.err_rx_badui++;
120                                 NDBGL2(L2_U_ERR, "unknown UI frame!");
121                                 i4b_Dfreembuf(m);                               
122                         }
123                         break;
124                         
125                 case DISC:
126                         if((l2sc->tei_valid == TEI_VALID) &&
127                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
128                         {               
129                                 l2sc->stat.rx_disc++;
130                                 NDBGL2(L2_U_MSG, "DISC, sapi = %d, tei = %d", sapi, tei);
131                                 l2sc->rxd_PF = pfbit;
132                                 i4b_next_l2state(l2sc, EV_RXDISC);
133                         }
134                         i4b_Dfreembuf(m);
135                         break;
136
137                 case XID:
138                         if((l2sc->tei_valid == TEI_VALID) &&
139                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
140                         {               
141                                 l2sc->stat.rx_xid++;
142                                 NDBGL2(L2_U_MSG, "XID, sapi = %d, tei = %d", sapi, tei);
143                         }
144                         i4b_Dfreembuf(m);                       
145                         break;
146                         
147                 /* responses */
148
149                 case DM:
150                         if((l2sc->tei_valid == TEI_VALID) &&
151                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
152                         {               
153                                 l2sc->stat.rx_dm++;
154                                 NDBGL2(L2_U_MSG, "DM, sapi = %d, tei = %d", sapi, tei);
155                                 i4b_print_frame(m->m_len, m->m_data);
156                                 l2sc->rxd_PF = pfbit;
157                                 i4b_next_l2state(l2sc, EV_RXDM);
158                         }
159                         i4b_Dfreembuf(m);
160                         break;
161                         
162                 case UA:
163                         if((l2sc->tei_valid == TEI_VALID) &&
164                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
165                         {               
166                                 l2sc->stat.rx_ua++;
167                                 NDBGL2(L2_U_MSG, "UA, sapi = %d, tei = %d", sapi, tei);
168                                 l2sc->rxd_PF = pfbit;
169                                 i4b_next_l2state(l2sc, EV_RXUA);
170                         }
171                         i4b_Dfreembuf(m);                       
172                         break;                  
173
174                 case FRMR:
175                         if((l2sc->tei_valid == TEI_VALID) &&
176                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
177                         {
178                                 l2sc->stat.rx_frmr++;
179                                 NDBGL2(L2_U_MSG, "FRMR, sapi = %d, tei = %d", sapi, tei);
180                                 l2sc->rxd_PF = pfbit;
181                                 i4b_next_l2state(l2sc, EV_RXFRMR);
182                         }
183                         i4b_Dfreembuf(m);                       
184                         break;
185
186                 default:
187                         if((l2sc->tei_valid == TEI_VALID) &&
188                            (l2sc->tei == GETTEI(*(ptr+OFF_TEI))))
189                         {               
190                                 NDBGL2(L2_U_ERR, "UNKNOWN TYPE ERROR, sapi = %d, tei = %d, frame = ", sapi, tei);
191                                 i4b_print_frame(m->m_len, m->m_data);
192                         }
193                         else
194                         {               
195                                 NDBGL2(L2_U_ERR, "not mine -  UNKNOWN TYPE ERROR, sapi = %d, tei = %d, frame = ", sapi, tei);
196                                 i4b_print_frame(m->m_len, m->m_data);
197                         }
198                         l2sc->stat.err_rx_badui++;                      
199                         i4b_Dfreembuf(m);                       
200                         break;
201         }
202 }
203
204 /*---------------------------------------------------------------------------*
205  *      build U-frame for sending
206  *---------------------------------------------------------------------------*/
207 struct mbuf *
208 i4b_build_u_frame(l2_softc_t *l2sc, crbit_to_nt_t crbit, pbit_t pbit, u_char type)
209 {
210         struct mbuf *m;
211         
212         if((m = i4b_Dgetmbuf(U_FRAME_LEN)) == NULL)
213                 return(NULL);
214
215         PUTSAPI(SAPI_CCP, crbit, m->m_data[OFF_SAPI]);
216                 
217         PUTTEI(l2sc->tei, m->m_data[OFF_TEI]);
218
219         if(pbit)
220                 m->m_data[OFF_CNTL] = type | UPBITSET;
221         else
222                 m->m_data[OFF_CNTL] = type & ~UPBITSET;
223
224         return(m);
225 }
226
227 /*---------------------------------------------------------------------------*
228  *      transmit SABME command
229  *---------------------------------------------------------------------------*/
230 void
231 i4b_tx_sabme(l2_softc_t *l2sc, pbit_t pbit)
232 {
233         struct mbuf *m;
234
235         l2sc->stat.tx_sabme++;
236         NDBGL2(L2_U_MSG, "tx SABME, tei = %d", l2sc->tei);
237         m = i4b_build_u_frame(l2sc, CR_CMD_TO_NT, pbit, SABME);
238         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
239 }
240
241 /*---------------------------------------------------------------------------*
242  *      transmit DM response
243  *---------------------------------------------------------------------------*/
244 void
245 i4b_tx_dm(l2_softc_t *l2sc, fbit_t fbit)
246 {
247         struct mbuf *m;
248
249         l2sc->stat.tx_dm++;     
250         NDBGL2(L2_U_MSG, "tx DM, tei = %d", l2sc->tei);
251         m = i4b_build_u_frame(l2sc, CR_RSP_TO_NT, fbit, DM);
252         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
253 }
254
255 /*---------------------------------------------------------------------------*
256  *      transmit DISC command
257  *---------------------------------------------------------------------------*/
258 void
259 i4b_tx_disc(l2_softc_t *l2sc, pbit_t pbit)
260 {
261         struct mbuf *m;
262         
263         l2sc->stat.tx_disc++;
264         NDBGL2(L2_U_MSG, "tx DISC, tei = %d", l2sc->tei);
265         m = i4b_build_u_frame(l2sc, CR_CMD_TO_NT, pbit, DISC);
266         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
267 }
268
269 /*---------------------------------------------------------------------------*
270  *      transmit UA response
271  *---------------------------------------------------------------------------*/
272 void
273 i4b_tx_ua(l2_softc_t *l2sc, fbit_t fbit)
274 {
275         struct mbuf *m;
276         
277         l2sc->stat.tx_ua++;
278         NDBGL2(L2_U_MSG, "tx UA, tei = %d", l2sc->tei);
279         m = i4b_build_u_frame(l2sc, CR_RSP_TO_NT, fbit, UA);
280         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
281 }
282
283 /*---------------------------------------------------------------------------*
284  *      transmit FRMR response
285  *---------------------------------------------------------------------------*/
286 void
287 i4b_tx_frmr(l2_softc_t *l2sc, fbit_t fbit)
288 {
289         struct mbuf *m;
290         
291         l2sc->stat.tx_frmr++;
292         NDBGL2(L2_U_MSG, "tx FRMR, tei = %d", l2sc->tei);
293         m = i4b_build_u_frame(l2sc, CR_RSP_TO_NT, fbit, FRMR);
294         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
295 }
296
297 #endif /* NI4BQ921 > 0 */