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