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