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