2 * Copyright (c) 2000 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 /*---------------------------------------------------------------------------
28 * i4b_ihfc_l1.c - hfc layer 1 handler
29 * -----------------------------------
30 * The idea of this file is to separate hfcs/sp/pci data/signal
31 * handling and the I4B data/signal handling.
33 * Everything which has got anything to do with I4B has been put here!
35 * last edit-date: [Wed Jul 19 09:41:03 2000]
36 * $Id: i4b_ihfc_l1if.c,v 1.10 2000/09/19 13:50:36 hm Exp $
38 *---------------------------------------------------------------------------*/
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
47 #include <sys/socket.h>
51 #include <i4b/include/i4b_debug.h>
52 #include <i4b/include/i4b_ioctl.h>
53 #include <i4b/include/i4b_trace.h>
55 #include <i4b/include/i4b_mbuf.h>
56 #include <i4b/include/i4b_global.h>
58 #include <i4b/layer1/i4b_l1.h>
59 #include <i4b/layer1/ihfc/i4b_ihfc.h>
60 #include <i4b/layer1/ihfc/i4b_ihfc_ext.h>
62 /*---------------------------------------------------------------------------*
65 * NOTE: The prototypes for get/putmbuf and B_linkinit
66 * have been put in i4b_hfc_ext.h for global hfc use.
68 * NOTE: channel != chan
69 *---------------------------------------------------------------------------*/
71 isdn_link_t * ihfc_B_ret_linktab (int unit, int channel);
72 static void ihfc_B_set_linktab (int unit, int channel, drvr_link_t *B_linktab);
74 static void ihfc_B_start (int unit, int chan);
75 static void ihfc_B_stat (int unit, int chan, bchan_statistics_t *bsp);
76 void ihfc_B_setup (int unit, int chan, int bprot, int activate);
78 static int ihfc_mph_command_req (int unit, int command, void *parm);
80 static int ihfc_ph_activate_req (int unit);
81 static int ihfc_ph_data_req (int unit, struct mbuf *m, int freeflag);
83 static void ihfc_T3_expired (ihfc_sc_t *sc);
85 /*---------------------------------------------------------------------------*
86 * Our I4B L1 mulitplexer link
87 *---------------------------------------------------------------------------*/
88 struct i4b_l1mux_func ihfc_l1mux_func = {
96 /*---------------------------------------------------------------------------*
97 * L2 -> L1: PH-DATA-REQUEST (D-Channel)
99 * NOTE: We may get called here from ihfc_hdlc_Dread or isac_hdlc_Dread
100 * via the upper layers.
101 *---------------------------------------------------------------------------*/
103 ihfc_ph_data_req(int unit, struct mbuf *m, int freeflag)
105 ihfc_sc_t *sc = &ihfc_softc[unit];
115 NDBGL1(L1_PRIM, "L1 was not running: "
116 "ihfc_ph_activate_req(unit = %d)!", unit);
118 ihfc_ph_activate_req(unit);
121 /* "Allow" I-frames (-hp) */
123 if (freeflag == MBUF_DONTFREE) m = m_copypacket(m, M_DONTWAIT);
125 if (!_IF_QFULL(&S_IFQUEUE) && m)
127 IF_ENQUEUE(&S_IFQUEUE, m);
129 ihfc_B_start(unit, chan); /* (recycling) */
133 NDBGL1(L1_ERROR, "No frame out (unit = %d)", unit);
134 if (m) i4b_Dfreembuf(m);
140 if (S_INTR_ACTIVE) S_INT_S1 |= 0x04;
147 /*---------------------------------------------------------------------------*
148 * L2 -> L1: PH-ACTIVATE-REQUEST (B-channel and D-channel)
149 *---------------------------------------------------------------------------*/
151 ihfc_ph_activate_req(int unit)
153 ihfc_sc_t *sc = &ihfc_softc[unit];
158 if ((!S_STM_T3) && (S_PHSTATE != 3))
163 S_STM_T3CALLOUT = timeout((TIMEOUT_FUNC_T)
164 ihfc_T3_expired, (ihfc_sc_t *)sc,
165 IHFC_ACTIVATION_TIMEOUT);
171 /*---------------------------------------------------------------------------*
172 * T3 timeout - persistant deactivation
173 *---------------------------------------------------------------------------*/
175 ihfc_T3_expired(ihfc_sc_t *sc)
184 if (S_PHSTATE != 3) /* line was not activated */
186 i4b_Dcleanifq(&S_IFQUEUE);
187 i4b_l1_ph_deactivate_ind(S_I4BUNIT);
189 i4b_l1_mph_status_ind(S_I4BUNIT, STI_PDEACT, 0, 0);
191 HFC_FSM(sc, 2); /* L1 deactivate */
197 /*---------------------------------------------------------------------------*
198 * Command from the upper layers (B-channel and D-channel)
199 *---------------------------------------------------------------------------*/
201 ihfc_mph_command_req(int unit, int command, void *parm)
203 ihfc_sc_t *sc = &ihfc_softc[unit];
207 case CMR_DOPEN: /* daemon running */
209 "unit %d, command = CMR_DOPEN", unit);
213 case CMR_DCLOSE: /* daemon not running */
215 "unit %d, command = CMR_DCLOSE", unit);
219 case CMR_SETTRACE: /* set new trace mask */
221 "unit %d, command = CMR_SETTRACE, parm = %d",
222 unit, (unsigned int)parm);
223 S_TRACE = (unsigned int)parm;
226 case CMR_GCST: /* get chip statistic */
228 "unit %d, command = CMR_GCST, parm = %d",
229 unit, (unsigned int)parm);
231 #define CST ((struct chipstat *)parm)
233 CST->driver_type = L1DRVR_IHFC;
235 /* XXX CST->xxxx_stat = xxx; */
242 "ERROR, unknown command = %d, unit = %d, parm = %d",
243 command, unit, (unsigned int)parm);
250 /*---------------------------------------------------------------------------*
251 * Data source switch for Read channels - 1, 3 and 5 (B and D-Channel)
252 *---------------------------------------------------------------------------*/
254 ihfc_putmbuf (ihfc_sc_t *sc, u_char chan, struct mbuf *m)
260 if(S_TRACE & TRACE_D_RX)
262 hdr.count = ++S_DTRACECOUNT;
265 hdr.unit = S_I4BUNIT;
269 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
272 if (!S_ENABLED) { i4b_Dfreembuf(m); return; }
274 m->m_pkthdr.len = m->m_len;
276 i4b_l1_ph_data_ind(S_I4BUNIT, m);
280 if(S_TRACE & TRACE_B_RX)
282 hdr.count = ++S_BTRACECOUNT;
284 hdr.type = (chan < 4) ? TRC_CH_B1 : TRC_CH_B2;
285 hdr.unit = S_I4BUNIT;
289 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
292 if (!S_ENABLED) { i4b_Bfreembuf(m); return; }
294 if (S_PROT == BPROT_NONE)
296 if(!i4b_l1_bchan_tel_silence(m->m_data, m->m_len))
298 S_BDRVLINK->bch_activity(S_BDRVLINK->unit, ACT_RX);
301 if (!_IF_QFULL(&S_IFQUEUE))
304 IF_ENQUEUE(&S_IFQUEUE, m);
305 S_BDRVLINK->bch_rx_data_ready(S_BDRVLINK->unit);
311 if (S_PROT == BPROT_RHDLC)
314 S_BYTES += m->m_pkthdr.len = m->m_len;
315 S_BDRVLINK->bch_rx_data_ready(S_BDRVLINK->unit);
321 NDBGL1(L1_ERROR, "Unknown protocol: %d", S_PROT);
325 /*---------------------------------------------------------------------------*
326 * Data destinator switch for write channels - 0, 2 and 4
327 *---------------------------------------------------------------------------*/
329 ihfc_getmbuf (ihfc_sc_t *sc, u_char chan)
331 register struct mbuf *m;
336 IF_DEQUEUE(&S_IFQUEUE, m);
338 if((S_TRACE & TRACE_D_TX) && m)
340 hdr.count = ++S_DTRACECOUNT;
343 hdr.unit = S_I4BUNIT;
347 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
352 IF_DEQUEUE(&S_IFQUEUE, m);
356 S_BDRVLINK->bch_tx_queue_empty(S_BDRVLINK->unit);
358 IF_DEQUEUE(&S_IFQUEUE, m);
362 if(!i4b_l1_bchan_tel_silence(m->m_data, m->m_len))
364 S_BDRVLINK->bch_activity(S_BDRVLINK->unit, ACT_TX);
369 if(S_TRACE & TRACE_B_TX)
371 hdr.count = ++S_BTRACECOUNT;
373 hdr.type = (chan < 4) ? TRC_CH_B1 : TRC_CH_B2;
374 hdr.unit = S_I4BUNIT;
378 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
386 /*---------------------------------------------------------------------------*
387 * Initialize rx/tx data structures (B-channel)
388 *---------------------------------------------------------------------------*/
390 ihfc_B_setup(int unit, int chan, int bprot, int activate)
392 ihfc_sc_t *sc = &ihfc_softc[unit];
395 if (((u_int)chan > 5) || ((u_int)chan < 2)) return;
399 HFC_INIT(sc, chan, bprot, activate);
404 /*---------------------------------------------------------------------------*
405 * Start transmission (B-channel or D-channel tx)
406 * NOTE: if "chan" variable is corrupted, it will not cause any harm,
407 * but data may be lost and there may be software sync. errors.
408 *---------------------------------------------------------------------------*/
410 ihfc_B_start(int unit, int chan)
412 ihfc_sc_t *sc = &ihfc_softc[unit];
415 if ((u_int)chan > 5) return;
419 if (S_FILTER && !S_MBUF && !S_INTR_ACTIVE)
421 S_INTR_ACTIVE |= 2; /* never know what *
425 S_FILTER(sc, chan); /* quick tx */
433 /*---------------------------------------------------------------------------*
434 * Fill statistics struct (B-channel)
435 *---------------------------------------------------------------------------*/
437 ihfc_B_stat(int unit, int chan, bchan_statistics_t *bsp)
439 ihfc_sc_t *sc = &ihfc_softc[unit];
442 if ((u_int)chan > 5) return;
448 bsp->inbytes = S_BYTES; S_BYTES = 0;
452 bsp->outbytes = S_BYTES; S_BYTES = 0;
457 /*---------------------------------------------------------------------------*
458 * Return the address of IHFC linktab to I4B (B-channel)
459 *---------------------------------------------------------------------------*/
461 ihfc_B_ret_linktab(int unit, int channel)
463 ihfc_sc_t *sc = &ihfc_softc[unit];
466 return(&sc->sc_blinktab[channel]);
471 /*---------------------------------------------------------------------------*
472 * Set the I4B driver linktab for IHFC use (B-channel)
473 *---------------------------------------------------------------------------*/
475 ihfc_B_set_linktab(int unit, int channel, drvr_link_t *B_linktab)
477 ihfc_sc_t *sc = &ihfc_softc[unit];
480 sc->sc_bdrvlinktab[channel] = B_linktab;
483 /*---------------------------------------------------------------------------*
484 * Initialize linktab for I4B use (B-channel)
485 *---------------------------------------------------------------------------*/
487 ihfc_B_linkinit(ihfc_sc_t *sc)
491 /* make sure the hardware driver is known to layer 4 */
492 ctrl_types[CTRL_PASSIVE].set_linktab = i4b_l1_set_linktab;
493 ctrl_types[CTRL_PASSIVE].get_linktab = i4b_l1_ret_linktab;
495 for (chan = 2; chan < 6; chan++)
497 S_BLINK.unit = S_UNIT;
498 S_BLINK.channel = chan; /* point to tx-chan */
499 S_BLINK.bch_config = ihfc_B_setup;
500 S_BLINK.bch_tx_start = ihfc_B_start;
501 S_BLINK.bch_stat = ihfc_B_stat;
503 /* This is a transmit channel (even) */
504 S_BLINK.tx_queue = &S_IFQUEUE;
506 /* This is a receive channel (odd) */
507 S_BLINK.rx_queue = &S_IFQUEUE;
508 S_BLINK.rx_mbuf = &S_MBUFDUMMY;