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