Remove spl*() calls from net/i4b, replacing them with critical sections.
[dragonfly.git] / sys / net / i4b / layer2 / i4b_util.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_util.c - layer 2 utility routines
28  *      -------------------------------------
29  *
30  *      $Id: i4b_util.c,v 1.26 2000/08/24 11:48:58 hm Exp $ 
31  *
32  * $FreeBSD: src/sys/i4b/layer2/i4b_util.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
33  * $DragonFly: src/sys/net/i4b/layer2/i4b_util.c,v 1.6 2005/06/03 16:50:10 dillon Exp $
34  *
35  *      last edit-date: [Mon May 29 16:55:35 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 <sys/thread2.h>
51 #include <net/if.h>
52
53 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
54 #include <sys/callout.h>
55 #endif
56
57 #if defined(__DragonFly__) || defined(__FreeBSD__)
58 #include <net/i4b/include/machine/i4b_debug.h>
59 #else
60 #include <i4b/i4b_debug.h>
61 #include <i4b/i4b_ioctl.h>
62 #endif
63
64 #include "../include/i4b_global.h"
65 #include "../include/i4b_l1l2.h"
66 #include "../include/i4b_mbuf.h"
67
68 #include "i4b_l2.h"
69
70 /*---------------------------------------------------------------------------*
71  *      routine ESTABLISH DATA LINK (Q.921 03/93 page 83)
72  *---------------------------------------------------------------------------*/
73 void
74 i4b_establish_data_link(l2_softc_t *l2sc)
75 {
76         i4b_l1_activate(l2sc);  
77
78         i4b_clear_exception_conditions(l2sc);
79
80         l2sc->RC = 0;
81
82         i4b_tx_sabme(l2sc, P1);
83
84         i4b_T200_restart(l2sc);
85
86         i4b_T203_stop(l2sc);    
87 }
88
89 /*---------------------------------------------------------------------------*
90  *      routine CLEAR EXCEPTION CONDITIONS (Q.921 03/93 page 83)
91  *---------------------------------------------------------------------------*/
92 void
93 i4b_clear_exception_conditions(l2_softc_t *l2sc)
94 {
95         CRIT_VAR;
96
97         CRIT_BEG;
98         
99 /*XXX -------------------------------------------------------------- */
100 /*XXX is this really appropriate here or should it moved elsewhere ? */
101
102         i4b_Dcleanifq(&l2sc->i_queue);
103         
104         if(l2sc->ua_num != UA_EMPTY)
105         {
106                 i4b_Dfreembuf(l2sc->ua_frame);
107                 l2sc->ua_num = UA_EMPTY;
108         }
109 /*XXX -------------------------------------------------------------- */
110
111         l2sc->peer_busy = 0;
112
113         l2sc->rej_excpt = 0;
114
115         l2sc->own_busy = 0;
116
117         l2sc->ack_pend = 0;     
118
119         CRIT_END;       
120 }
121
122 /*---------------------------------------------------------------------------*
123  *      routine TRANSMIT ENQUIRE (Q.921 03/93 page 83)
124  *---------------------------------------------------------------------------*/
125 void
126 i4b_transmit_enquire(l2_softc_t *l2sc)
127 {
128         if(l2sc->own_busy)
129                 i4b_tx_rnr_command(l2sc, P1);
130         else
131                 i4b_tx_rr_command(l2sc, P1);
132
133         l2sc->ack_pend = 0;
134
135         i4b_T200_start(l2sc);
136 }
137
138 /*---------------------------------------------------------------------------*
139  *      routine NR ERROR RECOVERY (Q.921 03/93 page 83)
140  *---------------------------------------------------------------------------*/
141 void
142 i4b_nr_error_recovery(l2_softc_t *l2sc)
143 {
144         i4b_mdl_error_ind(l2sc, "i4b_nr_error_recovery", MDL_ERR_J);
145
146         i4b_establish_data_link(l2sc);
147         
148         l2sc->l3initiated = 0;
149 }
150
151 /*---------------------------------------------------------------------------*
152  *      routine ENQUIRY RESPONSE (Q.921 03/93 page 84)
153  *---------------------------------------------------------------------------*/
154 void
155 i4b_enquiry_response(l2_softc_t *l2sc)
156 {
157         if(l2sc->own_busy)
158                 i4b_tx_rnr_response(l2sc, F1);
159         else
160                 i4b_tx_rr_response(l2sc, F1);
161
162         l2sc->ack_pend = 0;
163 }
164
165 /*---------------------------------------------------------------------------*
166  *      routine INVOKE RETRANSMISSION (Q.921 03/93 page 84)
167  *---------------------------------------------------------------------------*/
168 void
169 i4b_invoke_retransmission(l2_softc_t *l2sc, int nr)
170 {
171         CRIT_VAR;
172
173         CRIT_BEG;
174
175         NDBGL2(L2_ERROR, "nr = %d", nr );
176         
177         while(l2sc->vs != nr)
178         {
179                 NDBGL2(L2_ERROR, "nr(%d) != vs(%d)", nr, l2sc->vs);
180
181                 M128DEC(l2sc->vs);
182
183 /* XXXXXXXXXXXXXXXXX */
184
185                 if((l2sc->ua_num != UA_EMPTY) && (l2sc->vs == l2sc->ua_num))
186                 {
187                         if(IF_QFULL(&l2sc->i_queue))
188                         {
189                                 NDBGL2(L2_ERROR, "ERROR, I-queue full!");
190                         }
191                         else
192                         {
193                                 IF_ENQUEUE(&l2sc->i_queue, l2sc->ua_frame);
194                                 l2sc->ua_num = UA_EMPTY;
195                         }
196                 }
197                 else
198                 {
199                         NDBGL2(L2_ERROR, "ERROR, l2sc->vs = %d, l2sc->ua_num = %d ",l2sc->vs, l2sc->ua_num);
200                 }
201
202 /* XXXXXXXXXXXXXXXXX */
203                         
204                 i4b_i_frame_queued_up(l2sc);
205         }
206
207         CRIT_END;
208 }
209
210 /*---------------------------------------------------------------------------*
211  *      routine ACKNOWLEDGE PENDING (Q.921 03/93 p 70)
212  *---------------------------------------------------------------------------*/
213 void
214 i4b_acknowledge_pending(l2_softc_t *l2sc)
215 {
216         if(l2sc->ack_pend)
217         {
218                 l2sc->ack_pend = 0;
219                 i4b_tx_rr_response(l2sc, F0);
220         }
221 }
222
223 /*---------------------------------------------------------------------------*
224  *      i4b_print_frame - just print the hex contents of a frame
225  *---------------------------------------------------------------------------*/
226 void
227 i4b_print_frame(int len, u_char *buf)
228 {
229 #if DO_I4B_DEBUG
230         int i;
231
232         if (!(i4b_l2_debug & L2_ERROR))         /* XXXXXXXXXXXXXXXXXXXXX */
233                 return;
234
235         for(i = 0; i < len; i++)
236                 printf(" 0x%x", buf[i]);
237         printf("\n");
238 #endif
239 }
240
241 /*---------------------------------------------------------------------------*
242  *      i4b_print_l2var - print some l2softc vars
243  *---------------------------------------------------------------------------*/
244 void
245 i4b_print_l2var(l2_softc_t *l2sc)
246 {
247         NDBGL2(L2_ERROR, "unit%d V(R)=%d, V(S)=%d, V(A)=%d,ACKP=%d,PBSY=%d,OBSY=%d",
248                 l2sc->unit,
249                 l2sc->vr,
250                 l2sc->vs,
251                 l2sc->va,
252                 l2sc->ack_pend,
253                 l2sc->peer_busy,
254                 l2sc->own_busy);
255 }
256
257 /*---------------------------------------------------------------------------*
258  *      got s or i frame, check if valid ack for last sent frame
259  *---------------------------------------------------------------------------*/
260 void
261 i4b_rxd_ack(l2_softc_t *l2sc, int nr)
262 {
263
264 #ifdef NOTDEF
265         NDBGL2(L2_ERROR, "N(R)=%d, UA=%d, V(R)=%d, V(S)=%d, V(A)=%d",
266                 nr,
267                 l2sc->ua_num,
268                 l2sc->vr,
269                 l2sc->vs,
270                 l2sc->va);
271 #endif
272
273         if(l2sc->ua_num != UA_EMPTY)
274         {
275                 CRIT_VAR;
276
277                 CRIT_BEG;
278                 
279                 M128DEC(nr);
280
281                 if(l2sc->ua_num != nr)
282                         NDBGL2(L2_ERROR, "((N(R)-1)=%d) != (UA=%d) !!!", nr, l2sc->ua_num);
283                         
284                 i4b_Dfreembuf(l2sc->ua_frame);
285                 l2sc->ua_num = UA_EMPTY;
286                 
287                 CRIT_END;
288         }
289 }
290
291 /*---------------------------------------------------------------------------*
292  *      if not already active, activate layer 1
293  *---------------------------------------------------------------------------*/
294 void
295 i4b_l1_activate(l2_softc_t *l2sc)
296 {
297         if(l2sc->ph_active == PH_INACTIVE)
298         {
299                 l2sc->ph_active = PH_ACTIVEPEND;
300                 PH_Act_Req(l2sc->unit);
301         }
302 };
303
304 /*---------------------------------------------------------------------------*
305  *      check for v(a) <= n(r) <= v(s)
306  *      nr = receive sequence frame counter, va = acknowledge sequence frame
307  *      counter and vs = transmit sequence frame counter
308  *---------------------------------------------------------------------------*/
309 int
310 i4b_l2_nr_ok(int nr, int va, int vs)
311 {
312         if((va > nr) && ((nr != 0) || (va != 127)))
313         {
314                 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [1]", va, nr, vs);
315                 return 0;       /* fail */
316         }
317
318         if((nr > vs) && ((vs != 0) || (nr != 127)))
319         {
320                 NDBGL2(L2_ERROR, "ERROR, va = %d, nr = %d, vs = %d [2]", va, nr, vs);
321                 return 0;       /* fail */
322         }
323         return 1;               /* good */
324 }
325         
326 #endif /* NI4BQ921 > 0 */
327