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