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