kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / i4b / layer2 / i4b_sframe.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_sframe.c - s frame handling routines
28  *      ----------------------------------------
29  *
30  *      $Id: i4b_sframe.c,v 1.15 2000/08/24 11:48:58 hm Exp $ 
31  *
32  * $FreeBSD: src/sys/i4b/layer2/i4b_sframe.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
33  * $DragonFly: src/sys/net/i4b/layer2/i4b_sframe.c,v 1.3 2003/08/07 21:17:29 dillon Exp $
34  *
35  *      last edit-date: [Mon May 29 16:55:23 2000]
36  *
37  *---------------------------------------------------------------------------*/
38
39 #ifdef __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 #ifdef __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_mbuf.h"
65
66 #include "i4b_l2.h"
67 #include "i4b_l2fsm.h"
68
69 /*---------------------------------------------------------------------------*
70  *      process s frame
71  *---------------------------------------------------------------------------*/
72 void
73 i4b_rxd_s_frame(int unit, struct mbuf *m)
74 {
75         l2_softc_t *l2sc = &l2_softc[unit];
76         u_char *ptr = m->m_data;
77         
78         if(!((l2sc->tei_valid == TEI_VALID) &&
79              (l2sc->tei == GETTEI(*(ptr+OFF_TEI)))))
80         {
81                 i4b_Dfreembuf(m);
82                 return;
83         }
84
85         l2sc->rxd_CR = GETCR(*(ptr + OFF_SAPI));
86         l2sc->rxd_PF = GETSPF(*(ptr + OFF_SNR));
87         l2sc->rxd_NR = GETSNR(*(ptr + OFF_SNR));
88
89         i4b_rxd_ack(l2sc, l2sc->rxd_NR);
90         
91         switch(*(ptr + OFF_SRCR))
92         {
93                 case RR:
94                         l2sc->stat.rx_rr++; /* update statistics */
95                         NDBGL2(L2_S_MSG, "rx'd RR, N(R) = %d", l2sc->rxd_NR);
96                         i4b_next_l2state(l2sc, EV_RXRR);
97                         break;
98
99                 case RNR:
100                         l2sc->stat.rx_rnr++; /* update statistics */
101                         NDBGL2(L2_S_MSG, "rx'd RNR, N(R) = %d", l2sc->rxd_NR);
102                         i4b_next_l2state(l2sc, EV_RXRNR);
103                         break;
104
105                 case REJ:
106                         l2sc->stat.rx_rej++; /* update statistics */
107                         NDBGL2(L2_S_MSG, "rx'd REJ, N(R) = %d", l2sc->rxd_NR);
108                         i4b_next_l2state(l2sc, EV_RXREJ);
109                         break;
110
111                 default:
112                         l2sc->stat.err_rx_bads++; /* update statistics */
113                         NDBGL2(L2_S_ERR, "ERROR, unknown code, frame = ");
114                         i4b_print_frame(m->m_len, m->m_data);
115                         break;
116         }
117         i4b_Dfreembuf(m);       
118 }
119
120 /*---------------------------------------------------------------------------*
121  *      transmit RR command
122  *---------------------------------------------------------------------------*/
123 void
124 i4b_tx_rr_command(l2_softc_t *l2sc, pbit_t pbit)
125 {
126         struct mbuf *m;
127
128         NDBGL2(L2_S_MSG, "tx RR, unit = %d", l2sc->unit);
129
130         m = i4b_build_s_frame(l2sc, CR_CMD_TO_NT, pbit, RR);
131
132         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
133
134         l2sc->stat.tx_rr++; /* update statistics */
135 }
136
137 /*---------------------------------------------------------------------------*
138  *      transmit RR response
139  *---------------------------------------------------------------------------*/
140 void
141 i4b_tx_rr_response(l2_softc_t *l2sc, fbit_t fbit)
142 {
143         struct mbuf *m;
144
145         NDBGL2(L2_S_MSG, "tx RR, unit = %d", l2sc->unit);
146
147         m = i4b_build_s_frame(l2sc, CR_RSP_TO_NT, fbit, RR);    
148
149         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
150
151         l2sc->stat.tx_rr++; /* update statistics */
152 }
153
154 /*---------------------------------------------------------------------------*
155  *      transmit RNR command
156  *---------------------------------------------------------------------------*/
157 void
158 i4b_tx_rnr_command(l2_softc_t *l2sc, pbit_t pbit)
159 {
160         struct mbuf *m;
161
162         NDBGL2(L2_S_MSG, "tx RNR, unit = %d", l2sc->unit);
163
164         m = i4b_build_s_frame(l2sc, CR_CMD_TO_NT, pbit, RNR);   
165
166         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
167
168         l2sc->stat.tx_rnr++; /* update statistics */
169 }
170
171 /*---------------------------------------------------------------------------*
172  *      transmit RNR response
173  *---------------------------------------------------------------------------*/
174 void
175 i4b_tx_rnr_response(l2_softc_t *l2sc, fbit_t fbit)
176 {
177         struct mbuf *m;
178
179         NDBGL2(L2_S_MSG, "tx RNR, unit = %d", l2sc->unit);
180
181         m = i4b_build_s_frame(l2sc, CR_RSP_TO_NT, fbit, RNR);
182
183         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
184
185         l2sc->stat.tx_rnr++; /* update statistics */
186 }
187
188 /*---------------------------------------------------------------------------*
189  *      transmit REJ response
190  *---------------------------------------------------------------------------*/
191 void
192 i4b_tx_rej_response(l2_softc_t *l2sc, fbit_t fbit)
193 {
194         struct mbuf *m;
195
196         NDBGL2(L2_S_MSG, "tx REJ, unit = %d", l2sc->unit);
197
198         m = i4b_build_s_frame(l2sc, CR_RSP_TO_NT, fbit, REJ);
199
200         PH_Data_Req(l2sc->unit, m, MBUF_FREE);
201
202         l2sc->stat.tx_rej++; /* update statistics */    
203 }
204
205 /*---------------------------------------------------------------------------*
206  *      build S-frame for sending
207  *---------------------------------------------------------------------------*/
208 struct mbuf *
209 i4b_build_s_frame(l2_softc_t *l2sc, crbit_to_nt_t crbit, pbit_t pbit, u_char type)
210 {
211         struct mbuf *m;
212         
213         if((m = i4b_Dgetmbuf(S_FRAME_LEN)) == NULL)
214                 return(NULL);
215
216         PUTSAPI(SAPI_CCP, crbit, m->m_data[OFF_SAPI]);
217                 
218         PUTTEI(l2sc->tei, m->m_data[OFF_TEI]);
219
220         m->m_data[OFF_SRCR] = type;
221
222         m->m_data[OFF_SNR] = (l2sc->vr << 1) | (pbit & 0x01);
223
224         return(m);
225 }
226
227 #endif /* NI4BQ921 > 0 */