Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / i4b / layer3 / i4b_l4if.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_l4if.c - Layer 3 interface to Layer 4
28  *      -------------------------------------------
29  *
30  *      $Id: i4b_l4if.c,v 1.27 2000/08/24 11:48:58 hm Exp $ 
31  *
32  * $FreeBSD: src/sys/i4b/layer3/i4b_l4if.c,v 1.6.2.1 2001/08/10 14:08:42 obrien Exp $
33  *
34  *      last edit-date: [Fri Jun  2 14:32:19 2000]
35  *
36  *---------------------------------------------------------------------------*/
37
38 #ifdef __FreeBSD__
39 #include "i4bq931.h"
40 #else
41 #define NI4BQ931        1
42 #endif
43 #if NI4BQ931 > 0
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48
49 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
50 #include <sys/callout.h>
51 #endif
52
53 #ifdef __FreeBSD__
54 #include <machine/i4b_debug.h>
55 #include <machine/i4b_ioctl.h>
56 #include <machine/i4b_cause.h>
57 #else
58 #include <i4b/i4b_debug.h>
59 #include <i4b/i4b_ioctl.h>
60 #include <i4b/i4b_cause.h>
61 #endif
62
63 #include <i4b/include/i4b_l2l3.h>
64 #include <i4b/include/i4b_l3l4.h>
65 #include <i4b/include/i4b_global.h>
66
67 #include <i4b/layer3/i4b_l3.h>
68 #include <i4b/layer3/i4b_l3fsm.h>
69
70 #include <i4b/layer4/i4b_l4.h>
71
72 extern void isic_settrace(int unit, int val);           /*XXX*/
73 extern int isic_gettrace(int unit);                     /*XXX*/
74
75 static void n_connect_request(u_int cdid);
76 static void n_connect_response(u_int cdid, int response, int cause);
77 static void n_disconnect_request(u_int cdid, int cause);
78 static void n_alert_request(u_int cdid);
79 static void n_mgmt_command(int unit, int cmd, void *parm);
80
81 /*---------------------------------------------------------------------------*
82  *      i4b_mdl_status_ind - status indication from lower layers
83  *---------------------------------------------------------------------------*/
84 int
85 i4b_mdl_status_ind(int unit, int status, int parm)
86 {
87         int sendup;
88         int i;
89         
90         NDBGL3(L3_MSG, "unit = %d, status = %d, parm = %d", unit, status, parm);
91
92         switch(status)
93         {
94                 case STI_ATTACH:
95                         NDBGL3(L3_MSG, "STI_ATTACH: attaching unit %d to controller %d", unit, nctrl);
96                 
97                         /* init function pointers */
98                         
99                         ctrl_desc[nctrl].N_CONNECT_REQUEST = n_connect_request;
100                         ctrl_desc[nctrl].N_CONNECT_RESPONSE = n_connect_response;
101                         ctrl_desc[nctrl].N_DISCONNECT_REQUEST = n_disconnect_request;
102                         ctrl_desc[nctrl].N_ALERT_REQUEST = n_alert_request;     
103                         ctrl_desc[nctrl].N_DOWNLOAD = NULL;     /* only used by active cards */
104                         ctrl_desc[nctrl].N_DIAGNOSTICS = NULL;  /* only used by active cards */
105                         ctrl_desc[nctrl].N_MGMT_COMMAND = n_mgmt_command;
106                 
107                         /* init type and unit */
108                         
109                         ctrl_desc[nctrl].unit = unit;
110                         ctrl_desc[nctrl].ctrl_type = CTRL_PASSIVE;
111                         ctrl_desc[nctrl].card_type = parm;
112                 
113                         /* state fields */
114                 
115                         ctrl_desc[nctrl].dl_est = DL_DOWN;
116                         ctrl_desc[nctrl].nbch = 2; /* XXX extra param? */
117                         for (i = 0; i < ctrl_desc[nctrl].nbch; i++)
118                             ctrl_desc[nctrl].bch_state[i] = BCH_ST_FREE;
119
120                         ctrl_desc[nctrl].tei = -1;
121                         
122                         /* init unit to controller table */
123                         
124                         utoc_tab[unit] = nctrl;
125                         
126                         /* increment no. of controllers */
127                         
128                         nctrl++;
129
130                         break;
131                         
132                 case STI_L1STAT:
133                         i4b_l4_l12stat(unit, 1, parm);
134                         NDBGL3(L3_MSG, "STI_L1STAT: unit %d layer 1 = %s", unit, status ? "up" : "down");
135                         break;
136                         
137                 case STI_L2STAT:
138                         i4b_l4_l12stat(unit, 2, parm);
139                         NDBGL3(L3_MSG, "STI_L2STAT: unit %d layer 2 = %s", unit, status ? "up" : "down");
140                         break;
141
142                 case STI_TEIASG:
143                         ctrl_desc[unit].tei = parm;
144                         i4b_l4_teiasg(unit, parm);
145                         NDBGL3(L3_MSG, "STI_TEIASG: unit %d TEI = %d = 0x%02x", unit, parm, parm);
146                         break;
147
148                 case STI_PDEACT:        /* L1 T4 timeout */
149                         NDBGL3(L3_ERR, "STI_PDEACT: unit %d TEI = %d = 0x%02x", unit, parm, parm);
150
151                         sendup = 0;
152
153                         for(i=0; i < N_CALL_DESC; i++)
154                         {
155                                 if( (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
156                                     (ctrl_desc[call_desc[i].controller].unit == unit))
157                                 {
158                                         i4b_l3_stop_all_timers(&(call_desc[i]));
159                                         if(call_desc[i].cdid != CDID_UNUSED)
160                                                 sendup++;
161                                 }
162                         }
163
164                         ctrl_desc[utoc_tab[unit]].dl_est = DL_DOWN;
165                         for (i = 0; i < ctrl_desc[utoc_tab[unit]].nbch; i++)
166                             ctrl_desc[utoc_tab[unit]].bch_state[i] = BCH_ST_FREE;
167                         ctrl_desc[utoc_tab[unit]].tei = -1;
168
169                         if(sendup)
170                         {
171                                 i4b_l4_pdeact(unit, sendup);
172                                 call_desc[i].cdid = CDID_UNUSED;
173                         }
174                         break;
175
176                 case STI_NOL1ACC:       /* no outgoing access to S0 */
177                         NDBGL3(L3_ERR, "STI_NOL1ACC: unit %d no outgoing access to S0", unit);
178
179                         for(i=0; i < N_CALL_DESC; i++)
180                         {
181                                 if( (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
182                                     (ctrl_desc[call_desc[i].controller].unit == unit))
183                                 {
184                                         if(call_desc[i].cdid != CDID_UNUSED)
185                                         {
186                                                 SET_CAUSE_TYPE(call_desc[i].cause_in, CAUSET_I4B);
187                                                 SET_CAUSE_VAL(call_desc[i].cause_in, CAUSE_I4B_L1ERROR);
188                                                 i4b_l4_disconnect_ind(&(call_desc[i]));
189                                         }
190                                 }
191                         }
192
193                         ctrl_desc[utoc_tab[unit]].dl_est = DL_DOWN;
194                         for (i = 0; i < ctrl_desc[utoc_tab[unit]].nbch; i++)
195                             ctrl_desc[utoc_tab[unit]].bch_state[i] = BCH_ST_FREE;
196                         ctrl_desc[utoc_tab[unit]].tei = -1;
197                         break;
198
199                 default:
200                         NDBGL3(L3_ERR, "ERROR, unit %d, unknown status value %d!", unit, status);
201                         break;
202         }               
203         return(0);
204 }
205
206 /*---------------------------------------------------------------------------*
207  *      send command to the lower layers
208  *---------------------------------------------------------------------------*/
209 static void
210 n_mgmt_command(int unit, int cmd, void *parm)
211 {
212         int i;
213
214         switch(cmd)
215         {
216                 case CMR_DOPEN:
217                         NDBGL3(L3_MSG, "CMR_DOPEN for unit %d", unit);
218                         
219                         for(i=0; i < N_CALL_DESC; i++)
220                         {
221                                 if( (ctrl_desc[call_desc[i].controller].ctrl_type == CTRL_PASSIVE) &&
222                                     (ctrl_desc[call_desc[i].controller].unit == unit))
223                                 {
224                                         call_desc[i].cdid = CDID_UNUSED;
225                                 }
226                         }
227
228                         ctrl_desc[utoc_tab[unit]].dl_est = DL_DOWN;
229                         for (i = 0; i < ctrl_desc[utoc_tab[unit]].nbch; i++)
230                             ctrl_desc[utoc_tab[unit]].bch_state[i] = BCH_ST_FREE;
231                         ctrl_desc[utoc_tab[unit]].tei = -1;
232                         break;
233
234                 case CMR_DCLOSE:
235                         NDBGL3(L3_MSG, "CMR_DCLOSE for unit %d", unit);
236                         break;
237                         
238                 case CMR_SETTRACE:
239                         NDBGL3(L3_MSG, "CMR_SETTRACE for unit %d", unit);
240                         break;
241                         
242                 default:
243                         NDBGL3(L3_MSG, "unknown cmd %d for unit %d", cmd, unit);
244                         break;
245         }
246
247         MDL_Command_Req(unit, cmd, parm);
248         
249 }
250
251 /*---------------------------------------------------------------------------*
252  *      handle connect request message from userland
253  *---------------------------------------------------------------------------*/
254 static void
255 n_connect_request(u_int cdid)
256 {
257         call_desc_t *cd;
258
259         cd = cd_by_cdid(cdid);
260
261         next_l3state(cd, EV_SETUPRQ);   
262 }
263
264 /*---------------------------------------------------------------------------*
265  *      handle setup response message from userland
266  *---------------------------------------------------------------------------*/
267 static void
268 n_connect_response(u_int cdid, int response, int cause)
269 {
270         call_desc_t *cd;
271         int chstate;
272
273         cd = cd_by_cdid(cdid);
274
275         T400_stop(cd);
276         
277         cd->response = response;
278         cd->cause_out = cause;
279
280         switch(response)
281         {
282                 case SETUP_RESP_ACCEPT:
283                         next_l3state(cd, EV_SETACRS);
284                         chstate = BCH_ST_USED;
285                         break;
286                 
287                 case SETUP_RESP_REJECT:
288                         next_l3state(cd, EV_SETRJRS);
289                         chstate = BCH_ST_FREE;
290                         break;
291                         
292                 case SETUP_RESP_DNTCRE:
293                         next_l3state(cd, EV_SETDCRS);
294                         chstate = BCH_ST_FREE;
295                         break;
296
297                 default:        /* failsafe */
298                         next_l3state(cd, EV_SETDCRS);
299                         chstate = BCH_ST_FREE;
300                         NDBGL3(L3_ERR, "unknown response, doing SETUP_RESP_DNTCRE");
301                         break;
302         }
303
304         if((cd->channelid >= 0) && (cd->channelid < ctrl_desc[cd->controller].nbch))
305         {
306                 ctrl_desc[cd->controller].bch_state[cd->channelid] = chstate;
307         }
308         else
309         {
310                 NDBGL3(L3_MSG, "Warning, invalid channelid %d, response = %d\n", cd->channelid, response);
311         }
312 }
313
314 /*---------------------------------------------------------------------------*
315  *      handle disconnect request message from userland
316  *---------------------------------------------------------------------------*/
317 static void
318 n_disconnect_request(u_int cdid, int cause)
319 {
320         call_desc_t *cd;
321
322         cd = cd_by_cdid(cdid);
323
324         cd->cause_out = cause;
325
326         next_l3state(cd, EV_DISCRQ);
327 }
328
329 /*---------------------------------------------------------------------------*
330  *      handle alert request message from userland
331  *---------------------------------------------------------------------------*/
332 static void
333 n_alert_request(u_int cdid)
334 {
335         call_desc_t *cd;
336
337         cd = cd_by_cdid(cdid);
338
339         next_l3state(cd, EV_ALERTRQ);
340 }
341
342 #endif /* NI4BQ931 > 0 */