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