Initial import from FreeBSD RELENG_4:
[games.git] / sys / net / i4b / layer2 / i4b_iframe.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_iframe.c - i frame handling routines
28  *      ------------------------------------------
29  *
30  *      $Id: i4b_iframe.c,v 1.25 2000/08/24 11:48:57 hm Exp $ 
31  *
32  * $FreeBSD: src/sys/i4b/layer2/i4b_iframe.c,v 1.6.2.1 2001/08/10 14:08:41 obrien Exp $
33  *
34  *      last edit-date: [Thu Aug 24 12:49:18 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/kernel.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 <machine/i4b_debug.h>
58 #include <machine/i4b_trace.h>
59 #else
60 #include <i4b/i4b_debug.h>
61 #include <i4b/i4b_ioctl.h>
62 #include <i4b/i4b_trace.h>
63 #endif
64
65 #include <i4b/include/i4b_global.h>
66 #include <i4b/include/i4b_l1l2.h>
67 #include <i4b/include/i4b_l2l3.h>
68 #include <i4b/include/i4b_mbuf.h>
69
70 #include <i4b/layer2/i4b_l2.h>
71 #include <i4b/layer2/i4b_l2fsm.h>
72
73 /*---------------------------------------------------------------------------*
74  *      process i frame
75  *      implements the routine "I COMMAND" Q.921 03/93 pp 68 and pp 77
76  *---------------------------------------------------------------------------*/
77 void
78 i4b_rxd_i_frame(int unit, struct mbuf *m)
79 {
80         l2_softc_t *l2sc = &l2_softc[unit];
81         u_char *ptr = m->m_data;
82         int nr;
83         int ns;
84         int p;
85         CRIT_VAR;
86         
87         if(!((l2sc->tei_valid == TEI_VALID) &&
88              (l2sc->tei == GETTEI(*(ptr+OFF_TEI)))))
89         {
90                 i4b_Dfreembuf(m);
91                 return;
92         }
93
94         if((l2sc->Q921_state != ST_MULTIFR) && (l2sc->Q921_state != ST_TIMREC))
95         {
96                 i4b_Dfreembuf(m);
97                 NDBGL2(L2_I_ERR, "ERROR, state != (MF || TR)!");
98                 return;
99         }
100
101         CRIT_BEG;
102
103         l2sc->stat.rx_i++;              /* update frame count */
104         
105         nr = GETINR(*(ptr + OFF_INR));
106         ns = GETINS(*(ptr + OFF_INS));
107         p = GETIP(*(ptr + OFF_INR));
108         
109         i4b_rxd_ack(l2sc, nr);          /* last packet ack */
110
111         if(l2sc->own_busy)              /* own receiver busy ? */
112         {       
113                 i4b_Dfreembuf(m);       /* yes, discard information */
114
115                 if(p == 1)              /* P bit == 1 ? */
116                 {
117                         i4b_tx_rnr_response(l2sc, p); /* yes, tx RNR */
118                         l2sc->ack_pend = 0;     /* clear ACK pending */
119                 }
120         }
121         else    /* own receiver ready */
122         {
123                 if(ns == l2sc->vr)      /* expected sequence number ? */
124                 {
125                         M128INC(l2sc->vr);      /* yes, update */
126                         
127                         l2sc->rej_excpt = 0;    /* clr reject exception */
128
129                         m_adj(m, I_HDR_LEN);    /* strip i frame header */
130
131                         l2sc->iframe_sent = 0;  /* reset i acked already */
132
133                         DL_Data_Ind(l2sc->unit, m);     /* pass data up */
134
135                         if(!l2sc->iframe_sent)
136                         {
137                                 i4b_tx_rr_response(l2sc, p); /* yes, tx RR */
138                                 l2sc->ack_pend = 0;     /* clr ACK pending */
139                         }
140                 }
141                 else    /* ERROR, sequence number NOT expected */
142                 {
143                         i4b_Dfreembuf(m);       /* discard information */
144
145                         if(l2sc->rej_excpt == 1)  /* already exception ? */
146                         {
147                                 if(p == 1)      /* immediate response ? */
148                                 {
149                                         i4b_tx_rr_response(l2sc, p); /* yes, tx RR */
150                                         l2sc->ack_pend = 0; /* clr ack pend */
151                                 }
152                         }
153                         else    /* not in exception cond */
154                         {
155                                 l2sc->rej_excpt = 1;    /* set exception */
156                                 i4b_tx_rej_response(l2sc, p);   /* tx REJ */
157                                 l2sc->ack_pend = 0;     /* clr ack pending */
158                         }
159                 }
160         }
161
162         /* sequence number ranges as expected ? */
163         
164         if(i4b_l2_nr_ok(nr, l2sc->va, l2sc->vs))
165         {
166                 if(l2sc->Q921_state == ST_TIMREC)
167                 {
168                         l2sc->va = nr;
169
170                         CRIT_END;
171
172                         return;
173                 }
174
175                 if(l2sc->peer_busy)     /* yes, other side busy ? */
176                 {
177                         l2sc->va = nr;  /* yes, update ack count */
178                 }
179                 else    /* other side ready */
180                 {
181                         if(nr == l2sc->vs)      /* count expected ? */
182                         {
183                                 l2sc->va = nr;  /* update ack */
184                                 i4b_T200_stop(l2sc);
185                                 i4b_T203_restart(l2sc);
186                         }
187                         else
188                         {
189                                 if(nr != l2sc->va)
190                                 {
191                                         l2sc->va = nr;
192                                         i4b_T200_restart(l2sc);
193                                 }
194                         }
195                 }
196         }
197         else
198         {
199                 i4b_nr_error_recovery(l2sc);    /* sequence error */
200                 l2sc->Q921_state = ST_AW_EST; 
201         }
202
203         CRIT_END;
204 }
205
206 /*---------------------------------------------------------------------------*
207  *      internal I FRAME QUEUED UP routine (Q.921 03/93 p 61)
208  *---------------------------------------------------------------------------*/
209 void
210 i4b_i_frame_queued_up(l2_softc_t *l2sc)
211 {
212         struct mbuf *m;
213         u_char *ptr;
214         CRIT_VAR;
215
216         CRIT_BEG;
217         
218         if((l2sc->peer_busy) || (l2sc->vs == ((l2sc->va + MAX_K_VALUE) & 127)))
219         {
220                 if(l2sc->peer_busy)
221                 {
222                         NDBGL2(L2_I_MSG, "regen IFQUP, cause: peer busy!");
223                 }
224
225                 if(l2sc->vs == ((l2sc->va + MAX_K_VALUE) & 127))
226                 {
227                         NDBGL2(L2_I_MSG, "regen IFQUP, cause: vs=va+k!");
228                 }       
229
230                 /*
231                  * XXX see: Q.921, page 36, 5.6.1 ".. may retransmit an I
232                  * frame ...", shall we retransmit the last i frame ?
233                  */
234
235                 if(!(IF_QEMPTY(&l2sc->i_queue)))
236                 {
237                         NDBGL2(L2_I_MSG, "re-scheduling IFQU call!");
238                         START_TIMER(l2sc->IFQU_callout, i4b_i_frame_queued_up, l2sc, IFQU_DLY);
239                 }
240                 CRIT_END;
241                 return;
242         }
243
244         IF_DEQUEUE(&l2sc->i_queue, m);    /* fetch next frame to tx */
245
246         if(!m)
247         {
248                 NDBGL2(L2_I_ERR, "ERROR, mbuf NULL after IF_DEQUEUE");
249                 CRIT_END;
250                 return;
251         }
252
253         ptr = m->m_data;
254         
255         PUTSAPI(SAPI_CCP, CR_CMD_TO_NT, *(ptr + OFF_SAPI));
256         PUTTEI(l2sc->tei, *(ptr + OFF_TEI));
257
258         *(ptr + OFF_INS) = (l2sc->vs << 1) & 0xfe; /* bit 0 = 0 */
259         *(ptr + OFF_INR) = (l2sc->vr << 1) & 0xfe; /* P bit = 0 */
260
261         l2sc->stat.tx_i++;      /* update frame counter */
262         
263         PH_Data_Req(l2sc->unit, m, MBUF_DONTFREE); /* free'd when ack'd ! */
264
265         l2sc->iframe_sent = 1;          /* in case we ack an I frame with another I frame */
266         
267         if(l2sc->ua_num != UA_EMPTY)    /* failsafe */
268         {
269                 NDBGL2(L2_I_ERR, "ERROR, l2sc->ua_num: %d != UA_EMPTY", l2sc->ua_num);
270                 i4b_print_l2var(l2sc);
271                 i4b_Dfreembuf(l2sc->ua_frame);
272         }
273
274         l2sc->ua_frame = m;             /* save unacked frame */
275         l2sc->ua_num = l2sc->vs;        /* save frame number */
276
277         M128INC(l2sc->vs);
278         
279         l2sc->ack_pend = 0;
280
281         CRIT_END;
282
283         if(l2sc->T200 == TIMER_IDLE)
284         {
285                 i4b_T203_stop(l2sc);
286                 i4b_T200_start(l2sc);
287         }
288 }
289  
290 #endif /* NI4BQ921 > 0 */