kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / i4b / driver / i4b_bsdi_ibc.c
1 /*
2  *   Copyright (c) 1998, 1999 Bert Driehuis. All rights reserved.
3  *
4  *   Copyright (c) 1997, 1998 Hellmuth Michaelis. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  *   SUCH DAMAGE.
27  *
28  *---------------------------------------------------------------------------
29  *
30  *      i4b_bsdi_ibc.c - isdn4bsd kernel BSD/OS point to point driver
31  *      -------------------------------------------------------------
32  *
33  *      $Id: i4b_bsdi_ibc.c,v 1.3 2000/08/21 07:21:07 hm Exp $
34  *
35  * $FreeBSD: src/sys/i4b/driver/i4b_bsdi_ibc.c,v 1.3.2.1 2001/08/10 14:08:35 obrien Exp $
36  * $DragonFly: src/sys/net/i4b/driver/Attic/i4b_bsdi_ibc.c,v 1.4 2003/08/07 21:17:25 dillon Exp $
37  *
38  *      last edit-date: [Tue Dec 14 21:55:24 1999]
39  *
40  *---------------------------------------------------------------------------*/
41
42 #include "ibc.h"
43
44 #if NIBC > 0
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/ioccom.h>
52 #include <sys/ttycom.h>
53 #include <sys/sockio.h>
54 #include <sys/kernel.h>
55 #include <sys/protosw.h>
56
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <net/if_p2p.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67
68 #include <net/i4b/include/machine/i4b_ioctl.h>
69 #include <net/i4b/include/machine/i4b_cause.h>
70 #include "../../include/i4b_global.h"
71 #include "../../include/i4b_mbuf.h"
72 #include "../../include/i4b_l3l4.h"
73 #include "../../layer4/i4b_l4.h"
74
75 #define IFP2UNIT(ifp)   (ifp)->if_unit
76
77 #define IOCTL_CMD_T u_long
78
79 void ibcattach(void *);
80
81 #define IBCACCT         1       /* enable accounting messages */
82 #define IBCACCTINTVL    2       /* accounting msg interval in secs */
83
84 #define PPP_HDRLEN              4       /* 4 octets PPP header length   */
85
86 struct ibc_softc {
87         struct p2pcom sc_p2pcom;
88
89         int     sc_state;       /* state of the interface       */
90         call_desc_t *sc_cdp;    /* ptr to call descriptor       */
91
92 #ifdef IBCACCT
93         int sc_iinb;            /* isdn driver # of inbytes     */
94         int sc_ioutb;           /* isdn driver # of outbytes    */
95         int sc_inb;             /* # of bytes rx'd              */
96         int sc_outb;            /* # of bytes tx'd              */
97         int sc_linb;            /* last # of bytes rx'd         */
98         int sc_loutb;           /* last # of bytes tx'd         */
99         int sc_fn;              /* flag, first null acct        */
100 #endif
101 } ibc_softc[NIBC];
102
103 static void     ibc_init_linktab(int unit);
104
105 static int      ibc_start(struct ifnet *ifp);
106
107 static int      ibc_watchdog(int unit);
108 static int      ibc_mdmctl(struct p2pcom *pp, int flag);
109 static int      ibc_getmdm(struct p2pcom *pp, caddr_t arg);
110
111 /* initialized by L4 */
112
113 static drvr_link_t ibc_drvr_linktab[NIBC];
114 static isdn_link_t *isdn_ibc_lt[NIBC];
115
116 enum ibc_states {
117         ST_IDLE,                        /* initialized, ready, idle     */
118         ST_DIALING,                     /* dialling out to remote       */
119         ST_CONNECTED,                   /* connected to remote          */
120 };
121
122 int ibcdebug = 0;       /* Use bpatch to set this for debug printf's */
123 #define DBG(x)  if (ibcdebug) printf x
124
125 /*===========================================================================*
126  *                      DEVICE DRIVER ROUTINES
127  *===========================================================================*/
128
129 /*---------------------------------------------------------------------------*
130  *      interface attach routine at kernel boot time
131  *---------------------------------------------------------------------------*/
132 void
133 ibcattach(void *dummy)
134 {
135         struct ibc_softc *sc = ibc_softc;
136         struct ifnet *ifp;
137         int i;
138
139 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
140         printf("ibc: %d ISDN ibc device(s) attached\n",
141                NIBC);
142 #endif
143
144         for(i = 0; i < NIBC; sc++, i++) {
145                 ibc_init_linktab(i);
146
147                 sc->sc_p2pcom.p2p_mdmctl = ibc_mdmctl;
148                 sc->sc_p2pcom.p2p_getmdm = ibc_getmdm;
149                 sc->sc_state = ST_IDLE;
150                 ifp = &sc->sc_p2pcom.p2p_if;
151                 ifp->if_name = "ibc";
152                 ifp->if_next = NULL;
153                 ifp->if_unit = i;
154                 ifp->if_mtu = 1500 /*XXX*/;
155                 ifp->if_baudrate = 64000;
156                 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
157                 ifp->if_type = IFT_ISDNBASIC;
158                 ifp->if_start = ibc_start;
159                 ifp->if_output = 0;
160                 ifp->if_ioctl = p2p_ioctl;
161
162                 ifp->if_hdrlen = 0;
163                 ifp->if_addrlen = 0;
164                 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
165
166                 ifp->if_ipackets = 0;
167                 ifp->if_ierrors = 0;
168                 ifp->if_opackets = 0;
169                 ifp->if_oerrors = 0;
170                 ifp->if_collisions = 0;
171                 ifp->if_ibytes = 0;
172                 ifp->if_obytes = 0;
173                 ifp->if_imcasts = 0;
174                 ifp->if_omcasts = 0;
175                 ifp->if_iqdrops = 0;
176                 ifp->if_noproto = 0;
177 #if IBCACCT
178                 ifp->if_timer = 0;
179                 ifp->if_watchdog = ibc_watchdog;
180                 sc->sc_iinb = 0;
181                 sc->sc_ioutb = 0;
182                 sc->sc_inb = 0;
183                 sc->sc_outb = 0;
184                 sc->sc_linb = 0;
185                 sc->sc_loutb = 0;
186                 sc->sc_fn = 1;
187 #endif
188 #if defined(__FreeBSD_version) && ((__FreeBSD_version >= 500009) || (410000 <= __FreeBSD_version && __FreeBSD_version < 500000))
189                 ether_ifattach(ifp, 0);
190 #else
191                 if_attach(ifp);
192 #endif
193                 p2p_attach(&sc->sc_p2pcom);
194         }
195 }
196
197 static struct mbuf *
198 p2p_dequeue(struct p2pcom *pp)
199 {
200         struct mbuf *m;
201
202         IF_DEQUEUE(&pp->p2p_isnd, m);
203         if (m == 0)
204                 IF_DEQUEUE(&pp->p2p_if.if_snd, m);
205         return m;
206 }
207
208 /*---------------------------------------------------------------------------*
209  *      start output to ISDN B-channel
210  *---------------------------------------------------------------------------*/
211 static int
212 ibc_start(struct ifnet *ifp)
213 {
214         int unit = IFP2UNIT(ifp);
215         struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[unit];
216         struct p2pcom *pp = &sc->sc_p2pcom;
217         struct mbuf *m;
218         int s;
219
220         if(sc->sc_state != ST_CONNECTED) {
221                 DBG(("ibc%d: ibc_start called with sc_state=%d\n",
222                         unit, sc->sc_state));
223                 return 0;
224         }
225
226         s = SPLI4B();
227
228         IF_LOCK(isdn_ibc_lt[unit]->tx_queue);
229         if (_IF_QFULL(isdn_ibc_lt[unit]->tx_queue)) {
230                 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
231                 splx(s);
232                 return 0;
233         }
234
235         m = p2p_dequeue(pp);
236         if (m == NULL) {
237                 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
238                 splx(s);
239                 return 0;
240         }
241
242         do {
243                 microtime(&ifp->if_lastchange);
244
245                 ifp->if_obytes += m->m_pkthdr.len;
246                 sc->sc_outb += m->m_pkthdr.len;
247                 _IF_ENQUEUE(isdn_ibc_lt[unit]->tx_queue, m);
248
249                 ifp->if_opackets++;
250         } while (!_IF_QFULL(isdn_ibc_lt[unit]->tx_queue) &&
251                                         (m = p2p_dequeue(pp)) != NULL);
252         IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
253         isdn_ibc_lt[unit]->bch_tx_start(isdn_ibc_lt[unit]->unit,
254                                          isdn_ibc_lt[unit]->channel);
255         splx(s);
256         return 0;
257 }
258
259 #ifdef IBCACCT
260 /*---------------------------------------------------------------------------*
261  *      watchdog routine
262  *---------------------------------------------------------------------------*/
263 static int
264 ibc_watchdog(int unit)
265 {
266         struct ibc_softc *sc = &ibc_softc[unit];
267         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
268         bchan_statistics_t bs;
269
270         (*isdn_ibc_lt[unit]->bch_stat)
271                 (isdn_ibc_lt[unit]->unit, isdn_ibc_lt[unit]->channel, &bs);
272
273         sc->sc_ioutb += bs.outbytes;
274         sc->sc_iinb += bs.inbytes;
275
276         if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
277         {
278                 int ri = (sc->sc_iinb - sc->sc_linb)/IBCACCTINTVL;
279                 int ro = (sc->sc_ioutb - sc->sc_loutb)/IBCACCTINTVL;
280
281                 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
282                         sc->sc_fn = 0;
283                 else
284                         sc->sc_fn = 1;
285
286                 sc->sc_linb = sc->sc_iinb;
287                 sc->sc_loutb = sc->sc_ioutb;
288
289                 i4b_l4_accounting(BDRV_IBC, unit, ACCT_DURING,
290                          sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
291         }
292         ifp->if_timer = IBCACCTINTVL;
293         return 0;
294 }
295 #endif /* IBCACCT */
296
297 /*
298  *===========================================================================*
299  *                      P2P layer interface routines
300  *===========================================================================*
301  */
302
303 #if 0
304 /*---------------------------------------------------------------------------*
305  *      PPP interface phase change
306  *---------------------------------------------------------------------------*
307  */
308 static void
309 ibc_state_changed(struct sppp *sp, int new_state)
310 {
311         struct ibc_softc *sc = (struct ibc_softc *)sp;
312
313         i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
314 }
315
316 /*---------------------------------------------------------------------------*
317  *      PPP control protocol negotiation complete (run ip-up script now)
318  *---------------------------------------------------------------------------*
319  */
320 static void
321 ibc_negotiation_complete(struct sppp *sp)
322 {
323         struct ibc_softc *sc = (struct ibc_softc *)sp;
324
325         i4b_l4_negcomplete(sc->sc_cdp);
326 }
327 #endif
328
329 /*===========================================================================*
330  *                      ISDN INTERFACE ROUTINES
331  *===========================================================================*/
332
333 /*---------------------------------------------------------------------------*
334  *      this routine is called from L4 handler at connect time
335  *---------------------------------------------------------------------------*/
336 static void
337 ibc_connect(int unit, void *cdp)
338 {
339         struct ibc_softc *sc = &ibc_softc[unit];
340         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
341         int s;
342
343         DBG(("ibc%d: ibc_connect\n", unit));
344
345         s = splimp();
346
347         sc->sc_cdp = (call_desc_t *)cdp;
348         sc->sc_state = ST_CONNECTED;
349
350 #if IBCACCT
351         sc->sc_iinb = 0;
352         sc->sc_ioutb = 0;
353         sc->sc_inb = 0;
354         sc->sc_outb = 0;
355         sc->sc_linb = 0;
356         sc->sc_loutb = 0;
357         ifp->if_timer = IBCACCTINTVL;
358 #endif
359
360         splx(s);
361         if (sc->sc_p2pcom.p2p_modem)
362                 (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 1);
363
364                 /* This is a lie... PPP is just starting to negociate :-) */
365         i4b_l4_negcomplete(sc->sc_cdp);
366 }
367
368 /*---------------------------------------------------------------------------*
369  *      this routine is called from L4 handler at disconnect time
370  *---------------------------------------------------------------------------*/
371 static void
372 ibc_disconnect(int unit, void *cdp)
373 {
374         call_desc_t *cd = (call_desc_t *)cdp;
375         struct ibc_softc *sc = &ibc_softc[unit];
376         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
377         int s;
378
379         DBG(("ibc%d: ibc_disconnect\n", unit));
380
381         s = splimp();
382
383         /* new stuff to check that the active channel is being closed */
384         if (cd != sc->sc_cdp)
385         {
386                 DBG(("ibc_disconnect: ibc%d channel%d not active\n",
387                         cd->driver_unit, cd->channelid));
388                 splx(s);
389                 return;
390         }
391
392 #if IBCACCT
393         ifp->if_timer = 0;
394 #endif
395
396         i4b_l4_accounting(BDRV_IBC, unit, ACCT_FINAL,
397                  sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
398
399         if (sc->sc_state == ST_CONNECTED)
400         {
401                 sc->sc_cdp = (call_desc_t *)0;
402                 sc->sc_state = ST_IDLE;
403                 if (sc->sc_p2pcom.p2p_modem)
404                         (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 0);
405         }
406
407         splx(s);
408 }
409
410 /*---------------------------------------------------------------------------*
411  *      this routine is used to give a feedback from userland demon
412  *      in case of dial problems
413  *---------------------------------------------------------------------------*/
414 static void
415 ibc_dialresponse(int unit, int status)
416 {
417         DBG(("ibc%d: ibc_dialresponse %d\n", unit, status));
418 /*      struct ibc_softc *sc = &ibc_softc[unit];        */
419 }
420
421 /*---------------------------------------------------------------------------*
422  *      interface up/down
423  *---------------------------------------------------------------------------*/
424 static void
425 ibc_updown(int unit, int updown)
426 {
427         DBG(("ibc%d: ibc_updown %d\n", unit, updown));
428         /* could probably do something useful here */
429 }
430
431 /*---------------------------------------------------------------------------*
432  *      this routine is called from the HSCX interrupt handler
433  *      when a new frame (mbuf) has been received and was put on
434  *      the rx queue.
435  *---------------------------------------------------------------------------*/
436 static void
437 ibc_rx_data_rdy(int unit)
438 {
439         struct ibc_softc *sc = &ibc_softc[unit];
440         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
441         struct mbuf *m, *m0;
442         char *buf;
443         int s;
444
445         if((m = *isdn_ibc_lt[unit]->rx_mbuf) == NULL)
446                 return;
447
448         microtime(&ifp->if_lastchange);
449         ifp->if_ipackets++;
450
451                 /* Walk the mbuf chain */
452         s = splimp();
453         for (m0 = m; m != 0; m = m->m_next) {
454                 if (m->m_len == 0)
455                         continue;
456                 ifp->if_ibytes += m->m_len;
457 #if IBCACCT
458                 sc->sc_inb += m->m_len;
459 #endif
460                 buf = mtod(m, caddr_t);
461                 if ((*sc->sc_p2pcom.p2p_hdrinput)(
462                                         &sc->sc_p2pcom, buf, m->m_len) >= 0)
463                         (*sc->sc_p2pcom.p2p_input)(&sc->sc_p2pcom, 0);
464         }
465         splx(s);
466         m_freem(m0);
467 }
468
469 /*---------------------------------------------------------------------------*
470  *      this routine is called from the HSCX interrupt handler
471  *      when the last frame has been sent out and there is no
472  *      further frame (mbuf) in the tx queue.
473  *---------------------------------------------------------------------------*/
474 static void
475 ibc_tx_queue_empty(int unit)
476 {
477         ibc_start(&ibc_softc[unit].sc_p2pcom.p2p_if);
478 }
479
480 /*---------------------------------------------------------------------------*
481  *      this routine is called from the HSCX interrupt handler
482  *      each time a packet is received or transmitted. It should
483  *      be used to implement an activity timeout mechanism.
484  *---------------------------------------------------------------------------*/
485 static void
486 ibc_activity(int unit, int rxtx)
487 {
488         ibc_softc[unit].sc_cdp->last_active_time = SECOND;
489 }
490
491 /*---------------------------------------------------------------------------*
492  *      return this drivers linktab address
493  *---------------------------------------------------------------------------*/
494 drvr_link_t *
495 ibc_ret_linktab(int unit)
496 {
497         return(&ibc_drvr_linktab[unit]);
498 }
499
500 /*---------------------------------------------------------------------------*
501  *      setup the isdn_ibc_lt for this driver
502  *---------------------------------------------------------------------------*/
503 void
504 ibc_set_linktab(int unit, isdn_link_t *ilt)
505 {
506         isdn_ibc_lt[unit] = ilt;
507 }
508
509 /*---------------------------------------------------------------------------*
510  *      initialize this drivers linktab
511  *---------------------------------------------------------------------------*/
512 static void
513 ibc_init_linktab(int unit)
514 {
515         ibc_drvr_linktab[unit].unit = unit;
516         ibc_drvr_linktab[unit].bch_rx_data_ready = ibc_rx_data_rdy;
517         ibc_drvr_linktab[unit].bch_tx_queue_empty = ibc_tx_queue_empty;
518         ibc_drvr_linktab[unit].bch_activity = ibc_activity;
519         ibc_drvr_linktab[unit].line_connected = ibc_connect;
520         ibc_drvr_linktab[unit].line_disconnected = ibc_disconnect;
521         ibc_drvr_linktab[unit].dial_response = ibc_dialresponse;
522         ibc_drvr_linktab[unit].updown_ind = ibc_updown;
523 }
524
525 /*===========================================================================*/
526
527 static int
528 ibc_mdmctl(pp, flag)
529         struct p2pcom *pp;
530         int flag;
531 {
532         struct ifnet *ifp = &pp->p2p_if;
533         struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_unit];
534
535         DBG(("ibc%d: ibc_mdmctl called flags=%d\n", IFP2UNIT(ifp), flag));
536
537         if (flag == 1 && sc->sc_state == ST_IDLE) {
538                 sc->sc_state = ST_DIALING;
539                 i4b_l4_dialout(BDRV_IBC, IFP2UNIT(ifp));
540         } else if (flag == 0 && sc->sc_state != ST_IDLE) {
541                 sc->sc_state = ST_IDLE;
542                 i4b_l4_drvrdisc(BDRV_IBC, IFP2UNIT(ifp));
543         }
544         return 0;
545 }
546
547 static int
548 ibc_getmdm(pp, arg)
549         struct p2pcom *pp;
550         caddr_t arg;
551 {
552         struct ifnet *ifp = &pp->p2p_if;
553         struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_unit];
554
555         if (sc->sc_state == ST_CONNECTED)
556                 *(int *)arg = TIOCM_CAR;
557         else
558                 *(int *)arg = 0;
559         return 0;
560
561         DBG(("ibc%d: ibc_getmdm called ret=%d\n", IFP2UNIT(ifp), *(int *)arg));
562 }
563 #endif