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