6b41a5582284e5cc5a331cbec4c9b87d4c7e8c3f
[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.13 2007/06/17 23:50:16 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/dtype.h>
43 #include <sys/rman.h>
44
45 #include "mlx_compat.h"
46 #include "mlxio.h"
47 #include "mlxvar.h"
48 #include "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 static  d_open_t        mlxd_open;
56 static  d_close_t       mlxd_close;
57 static  d_strategy_t    mlxd_strategy;
58 static  d_ioctl_t       mlxd_ioctl;
59
60 #define MLXD_CDEV_MAJOR 131
61
62 static struct dev_ops mlxd_ops = {
63                 { "mlxd", MLXD_CDEV_MAJOR, D_DISK },
64                 .d_open =       mlxd_open,
65                 .d_close =      mlxd_close,
66                 .d_read =       physread,
67                 .d_write =      physwrite,
68                 .d_ioctl =      mlxd_ioctl,
69                 .d_strategy =   mlxd_strategy,
70 };
71
72 devclass_t              mlxd_devclass;
73
74 static device_method_t mlxd_methods[] = {
75     DEVMETHOD(device_probe,     mlxd_probe),
76     DEVMETHOD(device_attach,    mlxd_attach),
77     DEVMETHOD(device_detach,    mlxd_detach),
78     { 0, 0 }
79 };
80
81 static driver_t mlxd_driver = {
82     "mlxd",
83     mlxd_methods,
84     sizeof(struct mlxd_softc)
85 };
86
87 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
88
89 static int
90 mlxd_open(struct dev_open_args *ap)
91 {
92     cdev_t dev = ap->a_head.a_dev;
93     struct mlxd_softc   *sc = (struct mlxd_softc *)dev->si_drv1;
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 #if 0
104     bzero(&info, sizeof(info));
105     info.d_media_blksize= MLX_BLKSIZE;          /* mandatory */
106     info.d_media_blocks = sc->mlxd_drive->ms_size;
107
108     info.d_type         = DTYPE_SCSI;           /* optional */
109     info.d_secpertrack  = sc->mlxd_drive->ms_sectors;
110     info.d_nheads       = sc->mlxd_drive->ms_heads;
111     info.d_ncylinders   = sc->mlxd_drive->ms_cylinders;
112     info.d_secpercyl    = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
113
114     disk_setdiskinfo(&sc->mlxd_disk, &info);
115 #endif
116     sc->mlxd_flags |= MLXD_OPEN;
117     return (0);
118 }
119
120 static int
121 mlxd_close(struct dev_close_args *ap)
122 {
123     cdev_t dev = ap->a_head.a_dev;
124     struct mlxd_softc   *sc = (struct mlxd_softc *)dev->si_drv1;
125
126     debug_called(1);
127         
128     if (sc == NULL)
129         return (ENXIO);
130     sc->mlxd_flags &= ~MLXD_OPEN;
131     return (0);
132 }
133
134 static int
135 mlxd_ioctl(struct dev_ioctl_args *ap)
136 {
137     cdev_t dev = ap->a_head.a_dev;
138     struct mlxd_softc   *sc = (struct mlxd_softc *)dev->si_drv1;
139     int error;
140
141     debug_called(1);
142         
143     if (sc == NULL)
144         return (ENXIO);
145
146     if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, ap->a_cmd, ap->a_data, ap->a_fflag)) != ENOIOCTL) {
147         debug(0, "mlx_submit_ioctl returned %d\n", error);
148         return(error);
149     }
150     return (ENOTTY);
151 }
152
153 /*
154  * Read/write routine for a buffer.  Finds the proper unit, range checks
155  * arguments, and schedules the transfer.  Does not wait for the transfer
156  * to complete.  Multi-page transfers are supported.  All I/O requests must
157  * be a multiple of a sector in length.
158  */
159 static int
160 mlxd_strategy(struct dev_strategy_args *ap)
161 {
162     struct bio *bio = ap->a_bio;
163     struct buf *bp = bio->bio_buf;
164     struct mlxd_softc   *sc = (struct mlxd_softc *)bio->bio_driver_info;
165
166     debug_called(1);
167
168     /* bogus disk? */
169     if (sc == NULL) {
170         bp->b_error = EINVAL;
171         bp->b_flags |= B_ERROR;
172         goto bad;
173     }
174
175     /* XXX may only be temporarily offline - sleep? */
176     if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
177         bp->b_error = ENXIO;
178         bp->b_flags |= B_ERROR;
179         goto bad;
180     }
181
182     devstat_start_transaction(&sc->mlxd_stats);
183     mlx_submit_bio(sc->mlxd_controller, bio);
184     return(0);
185
186  bad:
187     /*
188      * Correctly set the bio to indicate a failed tranfer.
189      */
190     bp->b_resid = bp->b_bcount;
191     biodone(bio);
192     return(0);
193 }
194
195 void
196 mlxd_intr(struct bio *bio)
197 {
198     struct buf *bp = bio->bio_buf;
199     struct mlxd_softc   *sc = (struct mlxd_softc *)bio->bio_driver_info;
200
201     debug_called(1);
202
203     if (bp->b_flags & B_ERROR)
204         bp->b_error = EIO;
205     else
206         bp->b_resid = 0;
207     devstat_end_transaction_buf(&sc->mlxd_stats, bp);
208     biodone(bio);
209 }
210
211 static int
212 mlxd_probe(device_t dev)
213 {
214
215     debug_called(1);
216         
217     device_set_desc(dev, "Mylex System Drive");
218     return (0);
219 }
220
221 static int
222 mlxd_attach(device_t dev)
223 {
224     struct mlxd_softc   *sc = (struct mlxd_softc *)device_get_softc(dev);
225         struct disk_info info;
226     device_t            parent;
227     char                *state;
228     cdev_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, &mlxd_ops);
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         /*
275          * Set disk info, as it appears that all needed data is available already.
276          * Setting the disk info will also cause the probing to start.
277          */
278         bzero(&info, sizeof(info));
279     info.d_media_blksize= MLX_BLKSIZE;          /* mandatory */
280     info.d_media_blocks = sc->mlxd_drive->ms_size;
281
282     info.d_type         = DTYPE_SCSI;           /* optional */
283     info.d_secpertrack  = sc->mlxd_drive->ms_sectors;
284     info.d_nheads       = sc->mlxd_drive->ms_heads;
285     info.d_ncylinders   = sc->mlxd_drive->ms_cylinders;
286     info.d_secpercyl    = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
287
288     disk_setdiskinfo(&sc->mlxd_disk, &info);
289
290     return (0);
291 }
292
293 static int
294 mlxd_detach(device_t dev)
295 {
296     struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
297
298     debug_called(1);
299
300     devstat_remove_entry(&sc->mlxd_stats);
301     disk_destroy(&sc->mlxd_disk);
302
303     return(0);
304 }
305