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