Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / i4b / layer1 / itjc / i4b_itjc_l1.c
1 /*
2  * Copyright (c) 1997, 2001 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_itjc_l1.c - NetJet-S layer 1 handler
28  *      ---------------------------------------------
29  *
30  * $FreeBSD: src/sys/i4b/layer1/itjc/i4b_itjc_l1.c,v 1.1.2.1 2001/08/10 14:08:39 obrien Exp $
31  *
32  *      last edit-date: [Wed Jan 10 17:16:19 2001]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include "itjc.h"
37 #include "pci.h"
38
39 #if (NITJC > 0) && (NPCI > 0)
40
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46
47 #include <machine/stdarg.h>
48 #include <machine/clock.h>
49
50 #include <net/if.h>
51
52 #include <machine/i4b_debug.h>
53 #include <machine/i4b_ioctl.h>
54 #include <machine/i4b_trace.h>
55
56 #include <i4b/layer1/isic/i4b_isic.h>
57 #include <i4b/layer1/isic/i4b_isac.h>
58
59 #include <i4b/layer1/itjc/i4b_itjc_ext.h>
60
61 #include <i4b/layer1/i4b_l1.h>
62
63 #include <i4b/include/i4b_mbuf.h>
64 #include <i4b/include/i4b_global.h>
65
66 /*---------------------------------------------------------------------------*
67  *
68  *      L2 -> L1: PH-DATA-REQUEST
69  *      =========================
70  *
71  *      parms:
72  *              unit            physical interface unit number
73  *              m               mbuf containing L2 frame to be sent out
74  *              freeflag        MBUF_FREE: free mbuf here after having sent
75  *                                              it out
76  *                              MBUF_DONTFREE: mbuf is freed by Layer 2
77  *      returns:
78  *              ==0     fail, nothing sent out
79  *              !=0     ok, frame sent out
80  *
81  *---------------------------------------------------------------------------*/
82 int
83 itjc_ph_data_req(int unit, struct mbuf *m, int freeflag)
84 {
85         u_char cmd;
86         int s;
87         struct l1_softc *sc = itjc_scp[unit];
88
89         NDBGL1(L1_PRIM, "PH-DATA-REQ, unit %d, freeflag=%d", unit, freeflag);
90
91         if(m == NULL)                   /* failsafe */
92                 return (0);
93
94         s = SPLI4B();
95
96         if(sc->sc_I430state == ST_F3)   /* layer 1 not running ? */
97         {
98                 NDBGL1(L1_I_ERR, "still in state F3!");
99                 itjc_ph_activate_req(unit);
100         }
101
102         if(sc->sc_state & ISAC_TX_ACTIVE)
103         {
104                 if(sc->sc_obuf2 == NULL)
105                 {
106                         sc->sc_obuf2 = m;               /* save mbuf ptr */
107
108                         if(freeflag)
109                                 sc->sc_freeflag2 = 1;   /* IRQ must mfree */
110                         else
111                                 sc->sc_freeflag2 = 0;   /* IRQ must not mfree */
112
113                         NDBGL1(L1_I_MSG, "using 2nd ISAC TX buffer, state = %s", itjc_printstate(sc));
114
115                         if(sc->sc_trace & TRACE_D_TX)
116                         {
117                                 i4b_trace_hdr_t hdr;
118                                 hdr.unit = L0ITJCUNIT(unit);
119                                 hdr.type = TRC_CH_D;
120                                 hdr.dir = FROM_TE;
121                                 hdr.count = ++sc->sc_trace_dcount;
122                                 MICROTIME(hdr.time);
123                                 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
124                         }
125                         splx(s);
126                         return(1);
127                 }
128
129                 NDBGL1(L1_I_ERR, "No Space in TX FIFO, state = %s", itjc_printstate(sc));
130         
131                 if(freeflag == MBUF_FREE)
132                         i4b_Dfreembuf(m);                       
133         
134                 splx(s);
135                 return (0);
136         }
137
138         if(sc->sc_trace & TRACE_D_TX)
139         {
140                 i4b_trace_hdr_t hdr;
141                 hdr.unit = L0ITJCUNIT(unit);
142                 hdr.type = TRC_CH_D;
143                 hdr.dir = FROM_TE;
144                 hdr.count = ++sc->sc_trace_dcount;
145                 MICROTIME(hdr.time);
146                 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
147         }
148         
149         sc->sc_state |= ISAC_TX_ACTIVE; /* set transmitter busy flag */
150
151         NDBGL1(L1_I_MSG, "ISAC_TX_ACTIVE set");
152
153         sc->sc_freeflag = 0;            /* IRQ must NOT mfree */
154         
155         ISAC_WRFIFO(m->m_data, min(m->m_len, ISAC_FIFO_LEN)); /* output to TX fifo */
156
157         if(m->m_len > ISAC_FIFO_LEN)    /* message > 32 bytes ? */
158         {
159                 sc->sc_obuf = m;        /* save mbuf ptr */
160                 sc->sc_op = m->m_data + ISAC_FIFO_LEN;  /* ptr for irq hdl */
161                 sc->sc_ol = m->m_len - ISAC_FIFO_LEN;   /* length for irq hdl */
162
163                 if(freeflag)
164                         sc->sc_freeflag = 1;    /* IRQ must mfree */
165                 
166                 cmd = ISAC_CMDR_XTF;
167         }
168         else
169         {
170                 sc->sc_obuf = NULL;
171                 sc->sc_op = NULL;
172                 sc->sc_ol = 0;
173
174                 if(freeflag)
175                         i4b_Dfreembuf(m);
176
177                 cmd = ISAC_CMDR_XTF | ISAC_CMDR_XME;
178         }
179
180         ISAC_WRITE(I_CMDR, cmd);
181         ISACCMDRWRDELAY();
182
183         splx(s);
184         
185         return(1);
186 }
187
188 /*---------------------------------------------------------------------------*
189  *
190  *      L2 -> L1: PH-ACTIVATE-REQUEST
191  *      =============================
192  *
193  *      parms:
194  *              unit    physical interface unit number
195  *
196  *      returns:
197  *              ==0     
198  *              !=0     
199  *
200  *---------------------------------------------------------------------------*/
201 int
202 itjc_ph_activate_req(int unit)
203 {
204         struct l1_softc *sc = itjc_scp[unit];
205         NDBGL1(L1_PRIM, "PH-ACTIVATE-REQ, unit %d", unit);
206         itjc_next_state(sc, EV_PHAR);
207         return(0);
208 }
209
210 /*---------------------------------------------------------------------------*
211  *      command from the upper layers
212  *---------------------------------------------------------------------------*/
213 int
214 itjc_mph_command_req(int unit, int command, void *parm)
215 {
216         struct l1_softc *sc = itjc_scp[unit];
217
218         switch(command)
219         {
220                 case CMR_DOPEN:         /* daemon running */
221                         NDBGL1(L1_PRIM, "unit %d, command = CMR_DOPEN", unit);
222                         sc->sc_enabled = 1;                     
223                         break;
224                         
225                 case CMR_DCLOSE:        /* daemon not running */
226                         NDBGL1(L1_PRIM, "unit %d, command = CMR_DCLOSE", unit);
227                         sc->sc_enabled = 0;
228                         break;
229
230                 case CMR_SETTRACE:
231                         NDBGL1(L1_PRIM, "unit %d, command = CMR_SETTRACE, parm = %d", unit, (unsigned int)parm);
232                         sc->sc_trace = (unsigned int)parm;
233                         break;
234                 
235                 default:
236                         NDBGL1(L1_ERROR, "ERROR, unknown command = %d, unit = %d, parm = %d", command, unit, (unsigned int)parm);
237                         break;
238         }
239
240         return(0);
241 }
242
243 #endif /* NITJC > 0 */