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