if_xname support Part 2/2: Convert remaining netif devices and implement full
[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.5 2004/01/06 03: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_dunit
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                 if_initname(ifp, "ibc", i);
152                 ifp->if_next = NULL;
153                 ifp->if_mtu = 1500 /*XXX*/;
154                 ifp->if_baudrate = 64000;
155                 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
156                 ifp->if_type = IFT_ISDNBASIC;
157                 ifp->if_start = ibc_start;
158                 ifp->if_output = 0;
159                 ifp->if_ioctl = p2p_ioctl;
160
161                 ifp->if_hdrlen = 0;
162                 ifp->if_addrlen = 0;
163                 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
164
165                 ifp->if_ipackets = 0;
166                 ifp->if_ierrors = 0;
167                 ifp->if_opackets = 0;
168                 ifp->if_oerrors = 0;
169                 ifp->if_collisions = 0;
170                 ifp->if_ibytes = 0;
171                 ifp->if_obytes = 0;
172                 ifp->if_imcasts = 0;
173                 ifp->if_omcasts = 0;
174                 ifp->if_iqdrops = 0;
175                 ifp->if_noproto = 0;
176 #if IBCACCT
177                 ifp->if_timer = 0;
178                 ifp->if_watchdog = ibc_watchdog;
179                 sc->sc_iinb = 0;
180                 sc->sc_ioutb = 0;
181                 sc->sc_inb = 0;
182                 sc->sc_outb = 0;
183                 sc->sc_linb = 0;
184                 sc->sc_loutb = 0;
185                 sc->sc_fn = 1;
186 #endif
187 #if defined(__FreeBSD_version) && ((__FreeBSD_version >= 500009) || (410000 <= __FreeBSD_version && __FreeBSD_version < 500000))
188                 ether_ifattach(ifp, 0);
189 #else
190                 if_attach(ifp);
191 #endif
192                 p2p_attach(&sc->sc_p2pcom);
193         }
194 }
195
196 static struct mbuf *
197 p2p_dequeue(struct p2pcom *pp)
198 {
199         struct mbuf *m;
200
201         IF_DEQUEUE(&pp->p2p_isnd, m);
202         if (m == 0)
203                 IF_DEQUEUE(&pp->p2p_if.if_snd, m);
204         return m;
205 }
206
207 /*---------------------------------------------------------------------------*
208  *      start output to ISDN B-channel
209  *---------------------------------------------------------------------------*/
210 static int
211 ibc_start(struct ifnet *ifp)
212 {
213         int unit = IFP2UNIT(ifp);
214         struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[unit];
215         struct p2pcom *pp = &sc->sc_p2pcom;
216         struct mbuf *m;
217         int s;
218
219         if(sc->sc_state != ST_CONNECTED) {
220                 DBG(("ibc%d: ibc_start called with sc_state=%d\n",
221                         unit, sc->sc_state));
222                 return 0;
223         }
224
225         s = SPLI4B();
226
227         IF_LOCK(isdn_ibc_lt[unit]->tx_queue);
228         if (_IF_QFULL(isdn_ibc_lt[unit]->tx_queue)) {
229                 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
230                 splx(s);
231                 return 0;
232         }
233
234         m = p2p_dequeue(pp);
235         if (m == NULL) {
236                 IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
237                 splx(s);
238                 return 0;
239         }
240
241         do {
242                 microtime(&ifp->if_lastchange);
243
244                 ifp->if_obytes += m->m_pkthdr.len;
245                 sc->sc_outb += m->m_pkthdr.len;
246                 _IF_ENQUEUE(isdn_ibc_lt[unit]->tx_queue, m);
247
248                 ifp->if_opackets++;
249         } while (!_IF_QFULL(isdn_ibc_lt[unit]->tx_queue) &&
250                                         (m = p2p_dequeue(pp)) != NULL);
251         IF_UNLOCK(isdn_ibc_lt[unit]->tx_queue);
252         isdn_ibc_lt[unit]->bch_tx_start(isdn_ibc_lt[unit]->unit,
253                                          isdn_ibc_lt[unit]->channel);
254         splx(s);
255         return 0;
256 }
257
258 #ifdef IBCACCT
259 /*---------------------------------------------------------------------------*
260  *      watchdog routine
261  *---------------------------------------------------------------------------*/
262 static int
263 ibc_watchdog(int unit)
264 {
265         struct ibc_softc *sc = &ibc_softc[unit];
266         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
267         bchan_statistics_t bs;
268
269         (*isdn_ibc_lt[unit]->bch_stat)
270                 (isdn_ibc_lt[unit]->unit, isdn_ibc_lt[unit]->channel, &bs);
271
272         sc->sc_ioutb += bs.outbytes;
273         sc->sc_iinb += bs.inbytes;
274
275         if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
276         {
277                 int ri = (sc->sc_iinb - sc->sc_linb)/IBCACCTINTVL;
278                 int ro = (sc->sc_ioutb - sc->sc_loutb)/IBCACCTINTVL;
279
280                 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
281                         sc->sc_fn = 0;
282                 else
283                         sc->sc_fn = 1;
284
285                 sc->sc_linb = sc->sc_iinb;
286                 sc->sc_loutb = sc->sc_ioutb;
287
288                 i4b_l4_accounting(BDRV_IBC, unit, ACCT_DURING,
289                          sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
290         }
291         ifp->if_timer = IBCACCTINTVL;
292         return 0;
293 }
294 #endif /* IBCACCT */
295
296 /*
297  *===========================================================================*
298  *                      P2P layer interface routines
299  *===========================================================================*
300  */
301
302 #if 0
303 /*---------------------------------------------------------------------------*
304  *      PPP interface phase change
305  *---------------------------------------------------------------------------*
306  */
307 static void
308 ibc_state_changed(struct sppp *sp, int new_state)
309 {
310         struct ibc_softc *sc = (struct ibc_softc *)sp;
311
312         i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
313 }
314
315 /*---------------------------------------------------------------------------*
316  *      PPP control protocol negotiation complete (run ip-up script now)
317  *---------------------------------------------------------------------------*
318  */
319 static void
320 ibc_negotiation_complete(struct sppp *sp)
321 {
322         struct ibc_softc *sc = (struct ibc_softc *)sp;
323
324         i4b_l4_negcomplete(sc->sc_cdp);
325 }
326 #endif
327
328 /*===========================================================================*
329  *                      ISDN INTERFACE ROUTINES
330  *===========================================================================*/
331
332 /*---------------------------------------------------------------------------*
333  *      this routine is called from L4 handler at connect time
334  *---------------------------------------------------------------------------*/
335 static void
336 ibc_connect(int unit, void *cdp)
337 {
338         struct ibc_softc *sc = &ibc_softc[unit];
339         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
340         int s;
341
342         DBG(("ibc%d: ibc_connect\n", unit));
343
344         s = splimp();
345
346         sc->sc_cdp = (call_desc_t *)cdp;
347         sc->sc_state = ST_CONNECTED;
348
349 #if IBCACCT
350         sc->sc_iinb = 0;
351         sc->sc_ioutb = 0;
352         sc->sc_inb = 0;
353         sc->sc_outb = 0;
354         sc->sc_linb = 0;
355         sc->sc_loutb = 0;
356         ifp->if_timer = IBCACCTINTVL;
357 #endif
358
359         splx(s);
360         if (sc->sc_p2pcom.p2p_modem)
361                 (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 1);
362
363                 /* This is a lie... PPP is just starting to negociate :-) */
364         i4b_l4_negcomplete(sc->sc_cdp);
365 }
366
367 /*---------------------------------------------------------------------------*
368  *      this routine is called from L4 handler at disconnect time
369  *---------------------------------------------------------------------------*/
370 static void
371 ibc_disconnect(int unit, void *cdp)
372 {
373         call_desc_t *cd = (call_desc_t *)cdp;
374         struct ibc_softc *sc = &ibc_softc[unit];
375         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
376         int s;
377
378         DBG(("ibc%d: ibc_disconnect\n", unit));
379
380         s = splimp();
381
382         /* new stuff to check that the active channel is being closed */
383         if (cd != sc->sc_cdp)
384         {
385                 DBG(("ibc_disconnect: ibc%d channel%d not active\n",
386                         cd->driver_unit, cd->channelid));
387                 splx(s);
388                 return;
389         }
390
391 #if IBCACCT
392         ifp->if_timer = 0;
393 #endif
394
395         i4b_l4_accounting(BDRV_IBC, unit, ACCT_FINAL,
396                  sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
397
398         if (sc->sc_state == ST_CONNECTED)
399         {
400                 sc->sc_cdp = (call_desc_t *)0;
401                 sc->sc_state = ST_IDLE;
402                 if (sc->sc_p2pcom.p2p_modem)
403                         (*sc->sc_p2pcom.p2p_modem)(&sc->sc_p2pcom, 0);
404         }
405
406         splx(s);
407 }
408
409 /*---------------------------------------------------------------------------*
410  *      this routine is used to give a feedback from userland demon
411  *      in case of dial problems
412  *---------------------------------------------------------------------------*/
413 static void
414 ibc_dialresponse(int unit, int status)
415 {
416         DBG(("ibc%d: ibc_dialresponse %d\n", unit, status));
417 /*      struct ibc_softc *sc = &ibc_softc[unit];        */
418 }
419
420 /*---------------------------------------------------------------------------*
421  *      interface up/down
422  *---------------------------------------------------------------------------*/
423 static void
424 ibc_updown(int unit, int updown)
425 {
426         DBG(("ibc%d: ibc_updown %d\n", unit, updown));
427         /* could probably do something useful here */
428 }
429
430 /*---------------------------------------------------------------------------*
431  *      this routine is called from the HSCX interrupt handler
432  *      when a new frame (mbuf) has been received and was put on
433  *      the rx queue.
434  *---------------------------------------------------------------------------*/
435 static void
436 ibc_rx_data_rdy(int unit)
437 {
438         struct ibc_softc *sc = &ibc_softc[unit];
439         struct ifnet *ifp = &sc->sc_p2pcom.p2p_if;
440         struct mbuf *m, *m0;
441         char *buf;
442         int s;
443
444         if((m = *isdn_ibc_lt[unit]->rx_mbuf) == NULL)
445                 return;
446
447         microtime(&ifp->if_lastchange);
448         ifp->if_ipackets++;
449
450                 /* Walk the mbuf chain */
451         s = splimp();
452         for (m0 = m; m != 0; m = m->m_next) {
453                 if (m->m_len == 0)
454                         continue;
455                 ifp->if_ibytes += m->m_len;
456 #if IBCACCT
457                 sc->sc_inb += m->m_len;
458 #endif
459                 buf = mtod(m, caddr_t);
460                 if ((*sc->sc_p2pcom.p2p_hdrinput)(
461                                         &sc->sc_p2pcom, buf, m->m_len) >= 0)
462                         (*sc->sc_p2pcom.p2p_input)(&sc->sc_p2pcom, 0);
463         }
464         splx(s);
465         m_freem(m0);
466 }
467
468 /*---------------------------------------------------------------------------*
469  *      this routine is called from the HSCX interrupt handler
470  *      when the last frame has been sent out and there is no
471  *      further frame (mbuf) in the tx queue.
472  *---------------------------------------------------------------------------*/
473 static void
474 ibc_tx_queue_empty(int unit)
475 {
476         ibc_start(&ibc_softc[unit].sc_p2pcom.p2p_if);
477 }
478
479 /*---------------------------------------------------------------------------*
480  *      this routine is called from the HSCX interrupt handler
481  *      each time a packet is received or transmitted. It should
482  *      be used to implement an activity timeout mechanism.
483  *---------------------------------------------------------------------------*/
484 static void
485 ibc_activity(int unit, int rxtx)
486 {
487         ibc_softc[unit].sc_cdp->last_active_time = SECOND;
488 }
489
490 /*---------------------------------------------------------------------------*
491  *      return this drivers linktab address
492  *---------------------------------------------------------------------------*/
493 drvr_link_t *
494 ibc_ret_linktab(int unit)
495 {
496         return(&ibc_drvr_linktab[unit]);
497 }
498
499 /*---------------------------------------------------------------------------*
500  *      setup the isdn_ibc_lt for this driver
501  *---------------------------------------------------------------------------*/
502 void
503 ibc_set_linktab(int unit, isdn_link_t *ilt)
504 {
505         isdn_ibc_lt[unit] = ilt;
506 }
507
508 /*---------------------------------------------------------------------------*
509  *      initialize this drivers linktab
510  *---------------------------------------------------------------------------*/
511 static void
512 ibc_init_linktab(int unit)
513 {
514         ibc_drvr_linktab[unit].unit = unit;
515         ibc_drvr_linktab[unit].bch_rx_data_ready = ibc_rx_data_rdy;
516         ibc_drvr_linktab[unit].bch_tx_queue_empty = ibc_tx_queue_empty;
517         ibc_drvr_linktab[unit].bch_activity = ibc_activity;
518         ibc_drvr_linktab[unit].line_connected = ibc_connect;
519         ibc_drvr_linktab[unit].line_disconnected = ibc_disconnect;
520         ibc_drvr_linktab[unit].dial_response = ibc_dialresponse;
521         ibc_drvr_linktab[unit].updown_ind = ibc_updown;
522 }
523
524 /*===========================================================================*/
525
526 static int
527 ibc_mdmctl(pp, flag)
528         struct p2pcom *pp;
529         int flag;
530 {
531         struct ifnet *ifp = &pp->p2p_if;
532         struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_dunit];
533
534         DBG(("ibc%d: ibc_mdmctl called flags=%d\n", IFP2UNIT(ifp), flag));
535
536         if (flag == 1 && sc->sc_state == ST_IDLE) {
537                 sc->sc_state = ST_DIALING;
538                 i4b_l4_dialout(BDRV_IBC, IFP2UNIT(ifp));
539         } else if (flag == 0 && sc->sc_state != ST_IDLE) {
540                 sc->sc_state = ST_IDLE;
541                 i4b_l4_drvrdisc(BDRV_IBC, IFP2UNIT(ifp));
542         }
543         return 0;
544 }
545
546 static int
547 ibc_getmdm(pp, arg)
548         struct p2pcom *pp;
549         caddr_t arg;
550 {
551         struct ifnet *ifp = &pp->p2p_if;
552         struct ibc_softc *sc = (struct ibc_softc *)&ibc_softc[ifp->if_dunit];
553
554         if (sc->sc_state == ST_CONNECTED)
555                 *(int *)arg = TIOCM_CAR;
556         else
557                 *(int *)arg = 0;
558         return 0;
559
560         DBG(("ibc%d: ibc_getmdm called ret=%d\n", IFP2UNIT(ifp), *(int *)arg));
561 }
562 #endif