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