1/* $NetBSD: isic_l1.c,v 1.17 2007/10/19 11:59:54 ad Exp $ */
2
3/*
4 * Copyright (c) 1997, 2000 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 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: isic_l1.c,v 1.17 2007/10/19 11:59:54 ad Exp $");
31
32#include <sys/param.h>
33#include <sys/ioctl.h>
34#include <sys/kernel.h>
35#include <sys/systm.h>
36#include <sys/mbuf.h>
37
38#include <sys/bus.h>
39#include <sys/device.h>
40
41#include <sys/socket.h>
42#include <net/if.h>
43
44#include <sys/callout.h>
45
46#include <netisdn/i4b_debug.h>
47#include <netisdn/i4b_ioctl.h>
48#include <netisdn/i4b_trace.h>
49
50#include <netisdn/i4b_l2.h>
51#include <netisdn/i4b_l1l2.h>
52#include <netisdn/i4b_mbuf.h>
53#include <netisdn/i4b_global.h>
54
55#include <dev/ic/isic_l1.h>
56#include <dev/ic/isac.h>
57#include <dev/ic/ipac.h>
58#include <dev/ic/hscx.h>
59
60#include "nisac.h"
61#include "nisacsx.h"
62
63unsigned int i4b_l1_debug = L1_DEBUG_DEFAULT;
64
65static int isic_std_ph_data_req(isdn_layer1token, struct mbuf *, int);
66static int isic_std_ph_activate_req(isdn_layer1token);
67static int isic_std_mph_command_req(isdn_layer1token, int, void*);
68static void isic_enable_intr(struct isic_softc *sc, int enable);
69
70const struct isdn_layer1_isdnif_driver isic_std_driver = {
71	isic_std_ph_data_req,
72	isic_std_ph_activate_req,
73	isic_std_mph_command_req
74};
75
76/* from i4btrc driver i4b_trace.c */
77extern int get_trace_data_from_l1(int unit, int what, int len, char *buf);
78
79/*---------------------------------------------------------------------------*
80 *
81 *	L2 -> L1: PH-DATA-REQUEST
82 *	=========================
83 *
84 *	parms:
85 *		token		softc of physical driver
86 *		m		mbuf containing L2 frame to be sent out
87 *		freeflag	MBUF_FREE: free mbuf here after having sent
88 *						it out
89 *				MBUF_DONTFREE: mbuf is freed by Layer 2
90 *	returns:
91 *		==0	fail, nothing sent out
92 *		!=0	ok, frame sent out
93 *
94 *---------------------------------------------------------------------------*/
95static int
96isic_std_ph_data_req(isdn_layer1token token, struct mbuf *m, int freeflag)
97{
98	struct isic_softc *sc = (struct isic_softc*)token;
99	u_char cmd;
100	int s;
101
102	if (m == NULL)			/* failsafe */
103		return (0);
104
105	s = splnet();
106
107	if(sc->sc_I430state == ST_F3)	/* layer 1 not running ? */
108	{
109		NDBGL1(L1_I_ERR, "still in state F3!");
110		isic_std_ph_activate_req(token);
111	}
112
113	if(sc->sc_state & ISAC_TX_ACTIVE)
114	{
115		if(sc->sc_obuf2 == NULL)
116		{
117			sc->sc_obuf2 = m;		/* save mbuf ptr */
118
119			if(freeflag)
120				sc->sc_freeflag2 = 1;	/* IRQ must mfree */
121			else
122				sc->sc_freeflag2 = 0;	/* IRQ must not mfree */
123
124			NDBGL1(L1_I_MSG, "using 2nd ISAC TX buffer, state = %s", isic_printstate(sc));
125
126			if(sc->sc_trace & TRACE_D_TX)
127			{
128				i4b_trace_hdr hdr;
129				hdr.type = TRC_CH_D;
130				hdr.dir = FROM_TE;
131				hdr.count = ++sc->sc_trace_dcount;
132				isdn_layer2_trace_ind(&sc->sc_l2, sc->sc_l3token, &hdr, m->m_len, m->m_data);
133			}
134			splx(s);
135			return(1);
136		}
137
138		NDBGL1(L1_I_ERR, "No Space in TX FIFO, state = %s", isic_printstate(sc));
139
140		if(freeflag == MBUF_FREE)
141			i4b_Dfreembuf(m);
142
143		splx(s);
144		return (0);
145	}
146
147	if(sc->sc_trace & TRACE_D_TX)
148	{
149		i4b_trace_hdr hdr;
150		hdr.type = TRC_CH_D;
151		hdr.dir = FROM_TE;
152		hdr.count = ++sc->sc_trace_dcount;
153		isdn_layer2_trace_ind(&sc->sc_l2, sc->sc_l3token, &hdr, m->m_len, m->m_data);
154	}
155
156	sc->sc_state |= ISAC_TX_ACTIVE;	/* set transmitter busy flag */
157
158	NDBGL1(L1_I_MSG, "ISAC_TX_ACTIVE set");
159
160	sc->sc_freeflag = 0;		/* IRQ must NOT mfree */
161
162	ISAC_WRFIFO(m->m_data, min(m->m_len, ISAC_FIFO_LEN)); /* output to TX fifo */
163
164	if(m->m_len > ISAC_FIFO_LEN)	/* message > 32 bytes ? */
165	{
166		sc->sc_obuf = m;	/* save mbuf ptr */
167		sc->sc_op = m->m_data + ISAC_FIFO_LEN; 	/* ptr for irq hdl */
168		sc->sc_ol = m->m_len - ISAC_FIFO_LEN;	/* length for irq hdl */
169
170		if(freeflag)
171			sc->sc_freeflag = 1;	/* IRQ must mfree */
172
173		cmd = ISAC_CMDR_XTF;
174	}
175	else
176	{
177		sc->sc_obuf = NULL;
178		sc->sc_op = NULL;
179		sc->sc_ol = 0;
180
181		if(freeflag)
182			i4b_Dfreembuf(m);
183
184		cmd = ISAC_CMDR_XTF | ISAC_CMDR_XME;
185  	}
186
187	ISAC_WRITE(I_CMDR, cmd);
188	ISACCMDRWRDELAY();
189
190	splx(s);
191
192	return(1);
193}
194
195/*---------------------------------------------------------------------------*
196 *
197 *	L2 -> L1: PH-ACTIVATE-REQUEST
198 *	=============================
199 *
200 *	parms:
201 *		token		softc of physical interface
202 *
203 *	returns:
204 *		==0
205 *		!=0
206 *
207 *---------------------------------------------------------------------------*/
208static int
209isic_std_ph_activate_req(isdn_layer1token token)
210{
211	struct isic_softc *sc = (struct isic_softc*)token;
212
213	NDBGL1(L1_PRIM, " %s ", device_xname(&sc->sc_dev));
214	isic_next_state(sc, EV_PHAR);
215	return(0);
216}
217
218/*---------------------------------------------------------------------------*
219 *	command from the upper layers
220 *---------------------------------------------------------------------------*/
221static int
222isic_std_mph_command_req(isdn_layer1token token, int command, void *parm)
223{
224	struct isic_softc *sc = (struct isic_softc*)token;
225	int s, pass_down = 0;
226
227	s = splnet();
228	switch(command)
229	{
230		case CMR_DOPEN:		/* daemon running */
231			NDBGL1(L1_PRIM, "%s, command = CMR_DOPEN", device_xname(&sc->sc_dev));
232			sc->sc_intr_valid = ISIC_INTR_VALID;
233			pass_down = 1;
234			break;
235
236		case CMR_DCLOSE:	/* daemon not running */
237			NDBGL1(L1_PRIM, "%s, command = CMR_DCLOSE", device_xname(&sc->sc_dev));
238			sc->sc_intr_valid = ISIC_INTR_DISABLED;
239			isic_enable_intr(sc, 0);
240			pass_down = 1;
241			break;
242
243		case CMR_SETLEDS:
244			pass_down = 1;
245			break;
246
247		case CMR_SETTRACE:
248			NDBGL1(L1_PRIM, "%s, command = CMR_SETTRACE, parm = %p", device_xname(&sc->sc_dev), parm);
249			sc->sc_trace = (int)(unsigned long)parm;
250			break;
251
252		default:
253			NDBGL1(L1_ERROR, "ERROR, unknown command = %d, %s, parm = %p", command, device_xname(&sc->sc_dev), parm);
254			break;
255	}
256
257	if (pass_down && sc->drv_command != NULL)
258		sc->drv_command(sc, command, parm);
259
260	if (command == CMR_DOPEN)
261		isic_enable_intr(sc, 1);
262
263	splx(s);
264
265	return(0);
266}
267
268static void
269isic_enable_intr(struct isic_softc *sc, int enable)
270{
271#if NNISACSX > 0
272	if (sc->sc_cardtyp == CARD_TYPEP_AVMA1PCIV2) {
273		if (enable)
274			isic_isacsx_init(sc);
275		else isic_isacsx_disable_intr(sc);
276		return;
277	}
278#endif /* NNISACSX > 0 */
279
280#if NNISAC > 0
281	if (enable) {
282			isic_isac_init(sc);
283	} else {
284		/* disable receiver */
285		ISAC_WRITE(I_MODE, ISAC_MODE_MDS2|ISAC_MODE_MDS1|ISAC_MODE_DIM0);
286		/* mask interrupts */
287		if (sc->sc_ipac) {
288			IPAC_WRITE(IPAC_MASK, 0xff);
289		} else {
290			ISAC_WRITE(I_MASK, 0xff);
291		}
292	}
293#endif /* NNISAC > 0 */
294}
295