xref: /freebsd/sys/dev/mlx/mlx_disk.c (revision 716fd348)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Jonathan Lemon
5  * Copyright (c) 1999 Michael Smith
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * Disk driver for Mylex DAC960 RAID adapters.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bio.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 #include <sys/sx.h>
45 
46 #include <sys/bus.h>
47 #include <sys/conf.h>
48 
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 
52 #include <geom/geom_disk.h>
53 
54 #include <dev/mlx/mlxio.h>
55 #include <dev/mlx/mlxvar.h>
56 #include <dev/mlx/mlxreg.h>
57 
58 /* prototypes */
59 static int mlxd_probe(device_t dev);
60 static int mlxd_attach(device_t dev);
61 static int mlxd_detach(device_t dev);
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, 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(struct bio *bp)
146 {
147     struct mlxd_softc	*sc = bp->bio_disk->d_drv1;
148 
149     debug_called(1);
150 
151     /* bogus disk? */
152     if (sc == NULL) {
153 	bp->bio_error = EINVAL;
154 	bp->bio_flags |= BIO_ERROR;
155 	goto bad;
156     }
157 
158     if ((bp->bio_cmd != BIO_READ) && (bp->bio_cmd != BIO_WRITE)) {
159 	bp->bio_error = EOPNOTSUPP;
160 	goto bad;
161     }
162 
163     /* XXX may only be temporarily offline - sleep? */
164     MLX_IO_LOCK(sc->mlxd_controller);
165     if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
166 	MLX_IO_UNLOCK(sc->mlxd_controller);
167 	bp->bio_error = ENXIO;
168 	bp->bio_flags |= BIO_ERROR;
169 	goto bad;
170     }
171 
172     mlx_submit_buf(sc->mlxd_controller, bp);
173     MLX_IO_UNLOCK(sc->mlxd_controller);
174     return;
175 
176  bad:
177     /*
178      * Correctly set the bio to indicate a failed transfer.
179      */
180     bp->bio_resid = bp->bio_bcount;
181     biodone(bp);
182     return;
183 }
184 
185 void
186 mlxd_intr(struct bio *bp)
187 {
188 
189     debug_called(1);
190 
191     if (bp->bio_flags & BIO_ERROR)
192 	bp->bio_error = EIO;
193     else
194 	bp->bio_resid = 0;
195 
196     biodone(bp);
197 }
198 
199 static int
200 mlxd_probe(device_t dev)
201 {
202 
203     debug_called(1);
204 
205     device_set_desc(dev, "Mylex System Drive");
206     return (0);
207 }
208 
209 static int
210 mlxd_attach(device_t dev)
211 {
212     struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
213     device_t		parent;
214     char		*state;
215     int			s1, s2;
216 
217     debug_called(1);
218 
219     parent = device_get_parent(dev);
220     sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
221     sc->mlxd_unit = device_get_unit(dev);
222     sc->mlxd_drive = device_get_ivars(dev);
223     sc->mlxd_dev = dev;
224 
225     switch(sc->mlxd_drive->ms_state) {
226     case MLX_SYSD_ONLINE:
227 	state = "online";
228 	break;
229     case MLX_SYSD_CRITICAL:
230 	state = "critical";
231 	break;
232     case MLX_SYSD_OFFLINE:
233 	state = "offline";
234 	break;
235     default:
236 	state = "unknown state";
237     }
238 
239     device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
240 		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
241 		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
242 
243     sc->mlxd_disk = disk_alloc();
244     sc->mlxd_disk->d_open = mlxd_open;
245     sc->mlxd_disk->d_close = mlxd_close;
246     sc->mlxd_disk->d_ioctl = mlxd_ioctl;
247     sc->mlxd_disk->d_strategy = mlxd_strategy;
248     sc->mlxd_disk->d_name = "mlxd";
249     sc->mlxd_disk->d_unit = sc->mlxd_unit;
250     sc->mlxd_disk->d_drv1 = sc;
251     sc->mlxd_disk->d_sectorsize = MLX_BLKSIZE;
252     sc->mlxd_disk->d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
253     sc->mlxd_disk->d_fwsectors = sc->mlxd_drive->ms_sectors;
254     sc->mlxd_disk->d_fwheads = sc->mlxd_drive->ms_heads;
255 
256     /*
257      * Set maximum I/O size to the lesser of the recommended maximum and the practical
258      * maximum except on v2 cards where the maximum is set to 8 pages.
259      */
260     if (sc->mlxd_controller->mlx_iftype == MLX_IFTYPE_2)
261 	sc->mlxd_disk->d_maxsize = 8 * MLX_PAGE_SIZE;
262     else {
263 	s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
264 	s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * MLX_PAGE_SIZE;
265 	sc->mlxd_disk->d_maxsize = imin(s1, s2);
266     }
267 
268     disk_create(sc->mlxd_disk, DISK_VERSION);
269 
270     return (0);
271 }
272 
273 static int
274 mlxd_detach(device_t dev)
275 {
276     struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
277 
278     debug_called(1);
279 
280     disk_destroy(sc->mlxd_disk);
281 
282     return(0);
283 }
284 
285