xref: /freebsd/sys/dev/mlx/mlx_disk.c (revision 23cea7199b0f0d7e28803dda5b747d3cde7170df)
1 /*-
2  * Copyright (c) 1999 Jonathan Lemon
3  * Copyright (c) 1999 Michael Smith
4  * 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 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Disk driver for Mylex DAC960 RAID adapters.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/sx.h>
42 
43 #include <sys/bus.h>
44 #include <sys/conf.h>
45 
46 #include <machine/bus.h>
47 #include <sys/rman.h>
48 
49 #include <geom/geom_disk.h>
50 
51 #include <dev/mlx/mlx_compat.h>
52 #include <dev/mlx/mlxio.h>
53 #include <dev/mlx/mlxvar.h>
54 #include <dev/mlx/mlxreg.h>
55 
56 /* prototypes */
57 static int mlxd_probe(device_t dev);
58 static int mlxd_attach(device_t dev);
59 static int mlxd_detach(device_t dev);
60 
61 devclass_t		mlxd_devclass;
62 
63 static device_method_t mlxd_methods[] = {
64     DEVMETHOD(device_probe,	mlxd_probe),
65     DEVMETHOD(device_attach,	mlxd_attach),
66     DEVMETHOD(device_detach,	mlxd_detach),
67     { 0, 0 }
68 };
69 
70 static driver_t mlxd_driver = {
71     "mlxd",
72     mlxd_methods,
73     sizeof(struct mlxd_softc)
74 };
75 
76 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
77 
78 static int
79 mlxd_open(struct disk *dp)
80 {
81     struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
82 
83     debug_called(1);
84 
85     if (sc == NULL)
86 	return (ENXIO);
87 
88     /* controller not active? */
89     MLX_CONFIG_LOCK(sc->mlxd_controller);
90     MLX_IO_LOCK(sc->mlxd_controller);
91     if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN) {
92 	MLX_IO_UNLOCK(sc->mlxd_controller);
93 	MLX_CONFIG_UNLOCK(sc->mlxd_controller);
94 	return(ENXIO);
95     }
96 
97     sc->mlxd_flags |= MLXD_OPEN;
98     MLX_IO_UNLOCK(sc->mlxd_controller);
99     MLX_CONFIG_UNLOCK(sc->mlxd_controller);
100     return (0);
101 }
102 
103 static int
104 mlxd_close(struct disk *dp)
105 {
106     struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
107 
108     debug_called(1);
109 
110     if (sc == NULL)
111 	return (ENXIO);
112     MLX_CONFIG_LOCK(sc->mlxd_controller);
113     MLX_IO_LOCK(sc->mlxd_controller);
114     sc->mlxd_flags &= ~MLXD_OPEN;
115     MLX_IO_UNLOCK(sc->mlxd_controller);
116     MLX_CONFIG_UNLOCK(sc->mlxd_controller);
117     return (0);
118 }
119 
120 static int
121 mlxd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
122 {
123     struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
124     int error;
125 
126     debug_called(1);
127 
128     if (sc == NULL)
129 	return (ENXIO);
130 
131     if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
132 	debug(0, "mlx_submit_ioctl returned %d\n", error);
133 	return(error);
134     }
135     return (ENOTTY);
136 }
137 
138 /*
139  * Read/write routine for a buffer.  Finds the proper unit, range checks
140  * arguments, and schedules the transfer.  Does not wait for the transfer
141  * to complete.  Multi-page transfers are supported.  All I/O requests must
142  * be a multiple of a sector in length.
143  */
144 static void
145 mlxd_strategy(mlx_bio *bp)
146 {
147     struct mlxd_softc	*sc = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
148 
149     debug_called(1);
150 
151     /* bogus disk? */
152     if (sc == NULL) {
153 	MLX_BIO_SET_ERROR(bp, EINVAL);
154 	goto bad;
155     }
156 
157     /* XXX may only be temporarily offline - sleep? */
158     MLX_IO_LOCK(sc->mlxd_controller);
159     if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
160 	MLX_IO_UNLOCK(sc->mlxd_controller);
161 	MLX_BIO_SET_ERROR(bp, ENXIO);
162 	goto bad;
163     }
164 
165     MLX_BIO_STATS_START(bp);
166     mlx_submit_buf(sc->mlxd_controller, bp);
167     MLX_IO_UNLOCK(sc->mlxd_controller);
168     return;
169 
170  bad:
171     /*
172      * Correctly set the bio to indicate a failed tranfer.
173      */
174     MLX_BIO_RESID(bp) = MLX_BIO_LENGTH(bp);
175     MLX_BIO_DONE(bp);
176     return;
177 }
178 
179 void
180 mlxd_intr(void *data)
181 {
182     mlx_bio 		*bp = (mlx_bio *)data;
183 
184     debug_called(1);
185 
186     if (MLX_BIO_HAS_ERROR(bp))
187 	MLX_BIO_SET_ERROR(bp, EIO);
188     else
189 	MLX_BIO_RESID(bp) = 0;
190 
191     MLX_BIO_STATS_END(bp);
192     MLX_BIO_DONE(bp);
193 }
194 
195 static int
196 mlxd_probe(device_t dev)
197 {
198 
199     debug_called(1);
200 
201     device_set_desc(dev, "Mylex System Drive");
202     return (0);
203 }
204 
205 static int
206 mlxd_attach(device_t dev)
207 {
208     struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
209     device_t		parent;
210     char		*state;
211     int			s1, s2;
212 
213     debug_called(1);
214 
215     parent = device_get_parent(dev);
216     sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
217     sc->mlxd_unit = device_get_unit(dev);
218     sc->mlxd_drive = device_get_ivars(dev);
219     sc->mlxd_dev = dev;
220 
221     switch(sc->mlxd_drive->ms_state) {
222     case MLX_SYSD_ONLINE:
223 	state = "online";
224 	break;
225     case MLX_SYSD_CRITICAL:
226 	state = "critical";
227 	break;
228     case MLX_SYSD_OFFLINE:
229 	state = "offline";
230 	break;
231     default:
232 	state = "unknown state";
233     }
234 
235     device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
236 		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
237 		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
238 
239     sc->mlxd_disk = disk_alloc();
240     sc->mlxd_disk->d_open = mlxd_open;
241     sc->mlxd_disk->d_close = mlxd_close;
242     sc->mlxd_disk->d_ioctl = mlxd_ioctl;
243     sc->mlxd_disk->d_strategy = mlxd_strategy;
244     sc->mlxd_disk->d_name = "mlxd";
245     sc->mlxd_disk->d_unit = sc->mlxd_unit;
246     sc->mlxd_disk->d_drv1 = sc;
247     sc->mlxd_disk->d_sectorsize = MLX_BLKSIZE;
248     sc->mlxd_disk->d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
249     sc->mlxd_disk->d_fwsectors = sc->mlxd_drive->ms_sectors;
250     sc->mlxd_disk->d_fwheads = sc->mlxd_drive->ms_heads;
251 
252     /*
253      * Set maximum I/O size to the lesser of the recommended maximum and the practical
254      * maximum except on v2 cards where the maximum is set to 8 pages.
255      */
256     if (sc->mlxd_controller->mlx_iftype == MLX_IFTYPE_2)
257 	sc->mlxd_disk->d_maxsize = 8 * MLX_PAGE_SIZE;
258     else {
259 	s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
260 	s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * MLX_PAGE_SIZE;
261 	sc->mlxd_disk->d_maxsize = imin(s1, s2);
262     }
263 
264     disk_create(sc->mlxd_disk, DISK_VERSION);
265 
266     return (0);
267 }
268 
269 static int
270 mlxd_detach(device_t dev)
271 {
272     struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
273 
274     debug_called(1);
275 
276     disk_destroy(sc->mlxd_disk);
277 
278     return(0);
279 }
280 
281