xref: /dragonfly/sys/dev/raid/mlx/mlx_disk.c (revision 23265324)
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.11 2006/10/25 20:56:01 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 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/devicestat.h>
41 #include <sys/disk.h>
42 #include <sys/rman.h>
43 
44 #include "mlx_compat.h"
45 #include "mlxio.h"
46 #include "mlxvar.h"
47 #include "mlxreg.h"
48 
49 /* prototypes */
50 static int mlxd_probe(device_t dev);
51 static int mlxd_attach(device_t dev);
52 static int mlxd_detach(device_t dev);
53 
54 static	d_open_t	mlxd_open;
55 static	d_close_t	mlxd_close;
56 static	d_strategy_t	mlxd_strategy;
57 static	d_ioctl_t	mlxd_ioctl;
58 
59 #define MLXD_CDEV_MAJOR	131
60 
61 static struct dev_ops mlxd_ops = {
62 		{ "mlxd", MLXD_CDEV_MAJOR, D_DISK },
63 		.d_open =	mlxd_open,
64 		.d_close =	mlxd_close,
65 		.d_read =	physread,
66 		.d_write =	physwrite,
67 		.d_ioctl =	mlxd_ioctl,
68 		.d_strategy =	mlxd_strategy,
69 };
70 
71 devclass_t		mlxd_devclass;
72 
73 static device_method_t mlxd_methods[] = {
74     DEVMETHOD(device_probe,	mlxd_probe),
75     DEVMETHOD(device_attach,	mlxd_attach),
76     DEVMETHOD(device_detach,	mlxd_detach),
77     { 0, 0 }
78 };
79 
80 static driver_t mlxd_driver = {
81     "mlxd",
82     mlxd_methods,
83     sizeof(struct mlxd_softc)
84 };
85 
86 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
87 
88 static int
89 mlxd_open(struct dev_open_args *ap)
90 {
91     cdev_t dev = ap->a_head.a_dev;
92     struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
93     struct disklabel	*label;
94 
95     debug_called(1);
96 
97     if (sc == NULL)
98 	return (ENXIO);
99 
100     /* controller not active? */
101     if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
102 	return(ENXIO);
103 
104     label = &sc->mlxd_disk.d_label;
105     bzero(label, sizeof(*label));
106     label->d_type = DTYPE_SCSI;
107     label->d_secsize    = MLX_BLKSIZE;
108     label->d_nsectors   = sc->mlxd_drive->ms_sectors;
109     label->d_ntracks    = sc->mlxd_drive->ms_heads;
110     label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
111     label->d_secpercyl  = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
112     label->d_secperunit = sc->mlxd_drive->ms_size;
113 
114     sc->mlxd_flags |= MLXD_OPEN;
115     return (0);
116 }
117 
118 static int
119 mlxd_close(struct dev_close_args *ap)
120 {
121     cdev_t dev = ap->a_head.a_dev;
122     struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
123 
124     debug_called(1);
125 
126     if (sc == NULL)
127 	return (ENXIO);
128     sc->mlxd_flags &= ~MLXD_OPEN;
129     return (0);
130 }
131 
132 static int
133 mlxd_ioctl(struct dev_ioctl_args *ap)
134 {
135     cdev_t dev = ap->a_head.a_dev;
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, ap->a_cmd, ap->a_data, ap->a_fflag)) != 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 int
158 mlxd_strategy(struct dev_strategy_args *ap)
159 {
160     struct bio *bio = ap->a_bio;
161     struct buf *bp = bio->bio_buf;
162     struct mlxd_softc	*sc = (struct mlxd_softc *)bio->bio_driver_info;
163 
164     debug_called(1);
165 
166     /* bogus disk? */
167     if (sc == NULL) {
168 	bp->b_error = EINVAL;
169 	bp->b_flags |= B_ERROR;
170 	goto bad;
171     }
172 
173     /* XXX may only be temporarily offline - sleep? */
174     if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
175 	bp->b_error = ENXIO;
176 	bp->b_flags |= B_ERROR;
177 	goto bad;
178     }
179 
180     devstat_start_transaction(&sc->mlxd_stats);
181     mlx_submit_bio(sc->mlxd_controller, bio);
182     return(0);
183 
184  bad:
185     /*
186      * Correctly set the bio to indicate a failed tranfer.
187      */
188     bp->b_resid = bp->b_bcount;
189     biodone(bio);
190     return(0);
191 }
192 
193 void
194 mlxd_intr(struct bio *bio)
195 {
196     struct buf *bp = bio->bio_buf;
197     struct mlxd_softc	*sc = (struct mlxd_softc *)bio->bio_driver_info;
198 
199     debug_called(1);
200 
201     if (bp->b_flags & B_ERROR)
202 	bp->b_error = EIO;
203     else
204 	bp->b_resid = 0;
205     devstat_end_transaction_buf(&sc->mlxd_stats, bp);
206     biodone(bio);
207 }
208 
209 static int
210 mlxd_probe(device_t dev)
211 {
212 
213     debug_called(1);
214 
215     device_set_desc(dev, "Mylex System Drive");
216     return (0);
217 }
218 
219 static int
220 mlxd_attach(device_t dev)
221 {
222     struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
223     device_t		parent;
224     char		*state;
225     cdev_t		dsk;
226     int			s1, s2;
227 
228     debug_called(1);
229 
230     parent = device_get_parent(dev);
231     sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
232     sc->mlxd_unit = device_get_unit(dev);
233     sc->mlxd_drive = device_get_ivars(dev);
234     sc->mlxd_dev = dev;
235 
236     switch(sc->mlxd_drive->ms_state) {
237     case MLX_SYSD_ONLINE:
238 	state = "online";
239 	break;
240     case MLX_SYSD_CRITICAL:
241 	state = "critical";
242 	break;
243     case MLX_SYSD_OFFLINE:
244 	state = "offline";
245 	break;
246     default:
247 	state = "unknown state";
248     }
249 
250     device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
251 		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
252 		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
253 
254     devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
255 		      DEVSTAT_NO_ORDERED_TAGS,
256 		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
257 		      DEVSTAT_PRIORITY_ARRAY);
258 
259     dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_ops);
260     dsk->si_drv1 = sc;
261     sc->mlxd_dev_t = dsk;
262 
263     /*
264      * Set maximum I/O size to the lesser of the recommended maximum and the practical
265      * maximum.
266      */
267     s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
268     s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
269     dsk->si_iosize_max = imin(s1, s2);
270 
271     return (0);
272 }
273 
274 static int
275 mlxd_detach(device_t dev)
276 {
277     struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
278 
279     debug_called(1);
280 
281     devstat_remove_entry(&sc->mlxd_stats);
282     disk_destroy(&sc->mlxd_disk);
283 
284     return(0);
285 }
286 
287