2 * Copyright (c) 1999 Jonathan Lemon
3 * Copyright (c) 1999 Michael Smith
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/mlx/mlx_disk.c,v 1.8.2.4 2001/06/25 04:37:51 msmith Exp $
31 * Disk driver for Mylex DAC960 RAID adapters.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
39 #include <sys/devicestat.h>
41 #include <sys/dtype.h>
44 #include "mlx_compat.h"
50 static int mlxd_probe(device_t dev);
51 static int mlxd_attach(device_t dev);
52 static int mlxd_detach(device_t dev);
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;
59 static struct dev_ops mlxd_ops = {
60 { "mlxd", 0, D_DISK },
62 .d_close = mlxd_close,
65 .d_ioctl = mlxd_ioctl,
66 .d_strategy = mlxd_strategy,
69 devclass_t mlxd_devclass;
71 static device_method_t mlxd_methods[] = {
72 DEVMETHOD(device_probe, mlxd_probe),
73 DEVMETHOD(device_attach, mlxd_attach),
74 DEVMETHOD(device_detach, mlxd_detach),
78 static driver_t mlxd_driver = {
81 sizeof(struct mlxd_softc)
84 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, NULL, NULL);
87 mlxd_open(struct dev_open_args *ap)
89 cdev_t dev = ap->a_head.a_dev;
90 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
97 /* controller not active? */
98 if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
101 bzero(&info, sizeof(info));
102 info.d_media_blksize= MLX_BLKSIZE; /* mandatory */
103 info.d_media_blocks = sc->mlxd_drive->ms_size;
105 info.d_type = DTYPE_SCSI; /* optional */
106 info.d_secpertrack = sc->mlxd_drive->ms_sectors;
107 info.d_nheads = sc->mlxd_drive->ms_heads;
108 info.d_ncylinders = sc->mlxd_drive->ms_cylinders;
109 info.d_secpercyl = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
111 disk_setdiskinfo(&sc->mlxd_disk, &info);
113 sc->mlxd_flags |= MLXD_OPEN;
118 mlxd_close(struct dev_close_args *ap)
120 cdev_t dev = ap->a_head.a_dev;
121 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
127 sc->mlxd_flags &= ~MLXD_OPEN;
132 mlxd_ioctl(struct dev_ioctl_args *ap)
134 cdev_t dev = ap->a_head.a_dev;
135 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
143 if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, ap->a_cmd, ap->a_data, ap->a_fflag)) != ENOIOCTL) {
144 debug(0, "mlx_submit_ioctl returned %d\n", error);
151 * Read/write routine for a buffer. Finds the proper unit, range checks
152 * arguments, and schedules the transfer. Does not wait for the transfer
153 * to complete. Multi-page transfers are supported. All I/O requests must
154 * be a multiple of a sector in length.
157 mlxd_strategy(struct dev_strategy_args *ap)
159 struct bio *bio = ap->a_bio;
160 struct buf *bp = bio->bio_buf;
161 struct mlxd_softc *sc = (struct mlxd_softc *)bio->bio_driver_info;
167 bp->b_error = EINVAL;
168 bp->b_flags |= B_ERROR;
172 /* XXX may only be temporarily offline - sleep? */
173 if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
175 bp->b_flags |= B_ERROR;
179 devstat_start_transaction(&sc->mlxd_stats);
180 mlx_submit_bio(sc->mlxd_controller, bio);
185 * Correctly set the bio to indicate a failed tranfer.
187 bp->b_resid = bp->b_bcount;
193 mlxd_intr(struct bio *bio)
195 struct buf *bp = bio->bio_buf;
196 struct mlxd_softc *sc = (struct mlxd_softc *)bio->bio_driver_info;
200 if (bp->b_flags & B_ERROR)
204 devstat_end_transaction_buf(&sc->mlxd_stats, bp);
209 mlxd_probe(device_t dev)
214 device_set_desc(dev, "Mylex System Drive");
219 mlxd_attach(device_t dev)
221 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
222 struct disk_info info;
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);
236 switch(sc->mlxd_drive->ms_state) {
237 case MLX_SYSD_ONLINE:
240 case MLX_SYSD_CRITICAL:
243 case MLX_SYSD_OFFLINE:
247 state = "unknown state";
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);
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);
259 dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, &mlxd_ops);
261 sc->mlxd_dev_t = dsk;
264 * Set maximum I/O size to the lesser of the recommended maximum and the practical
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);
272 * Set disk info, as it appears that all needed data is available already.
273 * Setting the disk info will also cause the probing to start.
275 bzero(&info, sizeof(info));
276 info.d_media_blksize= MLX_BLKSIZE; /* mandatory */
277 info.d_media_blocks = sc->mlxd_drive->ms_size;
279 info.d_type = DTYPE_SCSI; /* optional */
280 info.d_secpertrack = sc->mlxd_drive->ms_sectors;
281 info.d_nheads = sc->mlxd_drive->ms_heads;
282 info.d_ncylinders = sc->mlxd_drive->ms_cylinders;
283 info.d_secpercyl = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
285 disk_setdiskinfo(&sc->mlxd_disk, &info);
291 mlxd_detach(device_t dev)
293 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
297 devstat_remove_entry(&sc->mlxd_stats);
298 disk_destroy(&sc->mlxd_disk);