]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/stg/tmc18c30_subr.c
MFV r323110: 8558 lwp_create() returns EAGAIN on system with more than 80K ZFS filesy...
[FreeBSD/FreeBSD.git] / sys / dev / stg / tmc18c30_subr.c
1 /*-
2  * [Ported for FreeBSD]
3  *  Copyright (c) 2000
4  *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
5  *      All rights reserved.
6  * [NetBSD for NEC PC-98 series]
7  *  Copyright (c) 1996, 1997, 1998
8  *      NetBSD/pc98 porting staff. All rights reserved.
9  *  Copyright (c) 1996, 1997, 1998
10  *      Naofumi HONDA. All rights reserved.
11  *  Copyright (c) 1996, 1997, 1998
12  *      Kouichi Matsuda. All rights reserved.
13  * 
14  *  Redistribution and use in source and binary forms, with or without
15  *  modification, are permitted provided that the following conditions
16  *  are met:
17  *  1. Redistributions of source code must retain the above copyright
18  *     notice, this list of conditions and the following disclaimer.
19  *  2. Redistributions in binary form must reproduce the above copyright
20  *     notice, this list of conditions and the following disclaimer in the
21  *     documentation and/or other materials provided with the distribution.
22  *  3. The name of the author may not be used to endorse or promote products
23  *     derived from this software without specific prior written permission.
24  * 
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46
47 #include <sys/module.h>
48 #include <sys/bus.h>
49
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/rman.h> 
53
54 #include <cam/scsi/scsi_low.h>
55
56 #include <dev/stg/tmc18c30reg.h>
57 #include <dev/stg/tmc18c30var.h>
58 #include <dev/stg/tmc18c30.h>
59
60 #define STG_HOSTID      7
61
62 devclass_t stg_devclass;
63
64 int
65 stg_alloc_resource(device_t dev)
66 {
67         struct stg_softc *      sc = device_get_softc(dev);
68         rman_res_t              maddr, msize;
69         int                     error;
70
71         mtx_init(&sc->sc_sclow.sl_lock, "stg", NULL, MTX_DEF);
72         sc->port_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 
73                                               &sc->port_rid, RF_ACTIVE);
74         if (sc->port_res == NULL) {
75                 stg_release_resource(dev);
76                 return(ENOMEM);
77         }
78
79         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
80                                              RF_ACTIVE);
81         if (sc->irq_res == NULL) {
82                 stg_release_resource(dev);
83                 return(ENOMEM);
84         }
85         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
86         if (error) {
87                 return(0);      /* XXX */
88         }
89
90         /* no need to allocate memory if not configured */
91         if (maddr == 0 || msize == 0) {
92                 return(0);
93         }
94
95         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
96                                              RF_ACTIVE);
97         if (sc->mem_res == NULL) {
98                 stg_release_resource(dev);
99                 return(ENOMEM);
100         }
101
102         return(0);
103 }
104
105 void
106 stg_release_resource(device_t dev)
107 {
108         struct stg_softc        *sc = device_get_softc(dev);
109
110         if (sc->stg_intrhand)
111                 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
112         if (sc->port_res)
113                 bus_release_resource(dev, SYS_RES_IOPORT,
114                                      sc->port_rid, sc->port_res);
115         if (sc->irq_res)
116                 bus_release_resource(dev, SYS_RES_IRQ,
117                                      sc->irq_rid, sc->irq_res);
118         if (sc->mem_res)
119                 bus_release_resource(dev, SYS_RES_MEMORY,
120                                      sc->mem_rid, sc->mem_res);
121         mtx_destroy(&sc->sc_sclow.sl_lock);
122 }
123
124 int
125 stg_probe(device_t dev)
126 {
127         int rv;
128         struct stg_softc *sc = device_get_softc(dev);
129
130         rv = stgprobesubr(sc->port_res,
131                           device_get_flags(dev));
132
133         return rv;
134 }
135
136 int
137 stg_attach(device_t dev)
138 {
139         struct stg_softc *sc;
140         struct scsi_low_softc *slp;
141         u_int32_t flags = device_get_flags(dev);
142
143         sc = device_get_softc(dev);
144
145         slp = &sc->sc_sclow;
146         slp->sl_dev = dev;
147
148         slp->sl_hostid = STG_HOSTID;
149         slp->sl_cfgflags = flags;
150
151         stgattachsubr(sc);
152
153         return(STGIOSZ);
154 }
155
156 int
157 stg_detach(device_t dev)
158 {
159         struct stg_softc *sc = device_get_softc(dev);
160
161         scsi_low_deactivate(&sc->sc_sclow);
162         scsi_low_detach(&sc->sc_sclow);
163         stg_release_resource(dev);
164         return (0);
165 }
166
167 void
168 stg_intr(void *arg)
169 {
170         struct stg_softc *sc;
171
172         sc = arg;
173         SCSI_LOW_LOCK(&sc->sc_sclow);
174         stgintr(sc);
175         SCSI_LOW_UNLOCK(&sc->sc_sclow);
176 }