kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / net / i4b / layer1 / ifpi2 / i4b_ifpi2_l1.c
1 /*
2  * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
3  * Copyright (c) 2001 Gary Jennejohn. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *---------------------------------------------------------------------------
27  *
28  *      i4b_ifpi2_l1.c - AVM Fritz PCI Version 2 layer 1 handler
29  *      ---------------------------------------------
30  *
31  *      $Id$ 
32  *
33  * $FreeBSD: src/sys/i4b/layer1/ifpi2/i4b_ifpi2_l1.c,v 1.2.2.1 2002/04/25 20:26:50 gj Exp $
34  * $DragonFly: src/sys/net/i4b/layer1/ifpi2/i4b_ifpi2_l1.c,v 1.3 2003/08/07 21:17:25 dillon Exp $
35  *
36  *      last edit-date: [Fri Jun  2 14:54:30 2000]
37  *
38  *---------------------------------------------------------------------------*/
39
40 #include "use_ifpi2.h"
41 #include "use_pci.h"
42
43 #if (NIFPI2 > 0) && (NPCI > 0)
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49
50
51 #include <net/if.h>
52
53 #include <net/i4b/include/machine/i4b_debug.h>
54 #include <net/i4b/include/machine/i4b_ioctl.h>
55 #include <net/i4b/include/machine/i4b_trace.h>
56
57 #include "../isic/i4b_isic.h"
58 #include "../isic/i4b_isac.h"
59
60 #include "i4b_ifpi2_ext.h"
61
62 #include "../i4b_l1.h"
63
64 #include "../../include/i4b_mbuf.h"
65 #include "../../include/i4b_global.h"
66
67 /*---------------------------------------------------------------------------*
68  *
69  *      L2 -> L1: PH-DATA-REQUEST
70  *      =========================
71  *
72  *      parms:
73  *              unit            physical interface unit number
74  *              m               mbuf containing L2 frame to be sent out
75  *              freeflag        MBUF_FREE: free mbuf here after having sent
76  *                                              it out
77  *                              MBUF_DONTFREE: mbuf is freed by Layer 2
78  *      returns:
79  *              ==0     fail, nothing sent out
80  *              !=0     ok, frame sent out
81  *
82  *---------------------------------------------------------------------------*/
83 int
84 ifpi2_ph_data_req(int unit, struct mbuf *m, int freeflag)
85 {
86         u_char cmd;
87         int s;
88         struct l1_softc *sc = ifpi2_scp[unit];
89
90 #ifdef NOTDEF
91         NDBGL1(L1_PRIM, "PH-DATA-REQ, unit %d, freeflag=%d", unit, freeflag);
92 #endif
93
94         if(m == NULL)                   /* failsafe */
95                 return (0);
96
97         s = SPLI4B();
98
99         if(sc->sc_I430state == ST_F3)   /* layer 1 not running ? */
100         {
101                 NDBGL1(L1_I_ERR, "still in state F3!");
102                 ifpi2_ph_activate_req(unit);
103         }
104
105         if(sc->sc_state & ISAC_TX_ACTIVE)
106         {
107                 if(sc->sc_obuf2 == NULL)
108                 {
109                         sc->sc_obuf2 = m;               /* save mbuf ptr */
110
111                         if(freeflag)
112                                 sc->sc_freeflag2 = 1;   /* IRQ must mfree */
113                         else
114                                 sc->sc_freeflag2 = 0;   /* IRQ must not mfree */
115
116                         NDBGL1(L1_I_MSG, "using 2nd ISAC TX buffer, state = %s", ifpi2_printstate(sc));
117
118                         if(sc->sc_trace & TRACE_D_TX)
119                         {
120                                 i4b_trace_hdr_t hdr;
121                                 hdr.unit = L0IFPI2UNIT(unit);
122                                 hdr.type = TRC_CH_D;
123                                 hdr.dir = FROM_TE;
124                                 hdr.count = ++sc->sc_trace_dcount;
125                                 MICROTIME(hdr.time);
126                                 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
127                         }
128                         splx(s);
129                         return(1);
130                 }
131
132                 NDBGL1(L1_I_ERR, "No Space in TX FIFO, state = %s", ifpi2_printstate(sc));
133         
134                 if(freeflag == MBUF_FREE)
135                         i4b_Dfreembuf(m);                       
136         
137                 splx(s);
138                 return (0);
139         }
140
141         if(sc->sc_trace & TRACE_D_TX)
142         {
143                 i4b_trace_hdr_t hdr;
144                 hdr.unit = L0IFPI2UNIT(unit);
145                 hdr.type = TRC_CH_D;
146                 hdr.dir = FROM_TE;
147                 hdr.count = ++sc->sc_trace_dcount;
148                 MICROTIME(hdr.time);
149                 i4b_l1_trace_ind(&hdr, m->m_len, m->m_data);
150         }
151         
152         sc->sc_state |= ISAC_TX_ACTIVE; /* set transmitter busy flag */
153
154         NDBGL1(L1_I_MSG, "ISAC_TX_ACTIVE set");
155
156         sc->sc_freeflag = 0;            /* IRQ must NOT mfree */
157         
158         ISAC_WRFIFO(m->m_data, min(m->m_len, ISAC_FIFO_LEN)); /* output to TX fifo */
159
160         if(m->m_len > ISAC_FIFO_LEN)    /* message > 32 bytes ? */
161         {
162                 sc->sc_obuf = m;        /* save mbuf ptr */
163                 sc->sc_op = m->m_data + ISAC_FIFO_LEN;  /* ptr for irq hdl */
164                 sc->sc_ol = m->m_len - ISAC_FIFO_LEN;   /* length for irq hdl */
165
166                 if(freeflag)
167                         sc->sc_freeflag = 1;    /* IRQ must mfree */
168                 
169                 cmd = ISAC_CMDR_XTF;
170         }
171         else
172         {
173                 sc->sc_obuf = NULL;
174                 sc->sc_op = NULL;
175                 sc->sc_ol = 0;
176
177                 if(freeflag)
178                         i4b_Dfreembuf(m);
179
180                 cmd = ISAC_CMDR_XTF | ISAC_CMDR_XME;
181         }
182
183         ISAC_WRITE(I_CMDR, cmd);
184         ISACCMDRWRDELAY();
185
186         splx(s);
187         
188         return(1);
189 }
190
191 /*---------------------------------------------------------------------------*
192  *
193  *      L2 -> L1: PH-ACTIVATE-REQUEST
194  *      =============================
195  *
196  *      parms:
197  *              unit    physical interface unit number
198  *
199  *      returns:
200  *              ==0     
201  *              !=0     
202  *
203  *---------------------------------------------------------------------------*/
204 int
205 ifpi2_ph_activate_req(int unit)
206 {
207         struct l1_softc *sc = ifpi2_scp[unit];
208         NDBGL1(L1_PRIM, "PH-ACTIVATE-REQ, unit %d", unit);
209         ifpi2_next_state(sc, EV_PHAR);
210         return(0);
211 }
212
213 /*---------------------------------------------------------------------------*
214  *      command from the upper layers
215  *---------------------------------------------------------------------------*/
216 int
217 ifpi2_mph_command_req(int unit, int command, void *parm)
218 {
219         struct l1_softc *sc = ifpi2_scp[unit];
220
221         switch(command)
222         {
223                 case CMR_DOPEN:         /* daemon running */
224                         NDBGL1(L1_PRIM, "unit %d, command = CMR_DOPEN", unit);
225                         sc->sc_enabled = 1;                     
226                         break;
227                         
228                 case CMR_DCLOSE:        /* daemon not running */
229                         NDBGL1(L1_PRIM, "unit %d, command = CMR_DCLOSE", unit);
230                         sc->sc_enabled = 0;
231                         break;
232
233                 case CMR_SETTRACE:
234                         NDBGL1(L1_PRIM, "unit %d, command = CMR_SETTRACE, parm = %d", unit, (unsigned int)parm);
235                         sc->sc_trace = (unsigned int)parm;
236                         break;
237                 
238                 default:
239                         NDBGL1(L1_ERROR, "ERROR, unknown command = %d, unit = %d, parm = %d", command, unit, (unsigned int)parm);
240                         break;
241         }
242
243         return(0);
244 }
245
246 #endif /* NIFPI2 > 0 */