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