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 $
28 * $DragonFly: src/sys/dev/raid/mlx/mlx_disk.c,v 1.9 2006/07/28 02:17:37 dillon Exp $
32 * Disk driver for Mylex DAC960 RAID adapters.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
41 #include <sys/devicestat.h>
44 #include <machine/bus.h>
47 #include "mlx_compat.h"
53 static int mlxd_probe(device_t dev);
54 static int mlxd_attach(device_t dev);
55 static int mlxd_detach(device_t dev);
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;
62 #define MLXD_CDEV_MAJOR 131
64 static struct dev_ops mlxd_ops = {
65 { "mlxd", MLXD_CDEV_MAJOR, D_DISK },
67 .d_close = mlxd_close,
70 .d_ioctl = mlxd_ioctl,
71 .d_strategy = mlxd_strategy,
74 devclass_t mlxd_devclass;
76 static device_method_t mlxd_methods[] = {
77 DEVMETHOD(device_probe, mlxd_probe),
78 DEVMETHOD(device_attach, mlxd_attach),
79 DEVMETHOD(device_detach, mlxd_detach),
83 static driver_t mlxd_driver = {
86 sizeof(struct mlxd_softc)
89 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
92 mlxd_open(struct dev_open_args *ap)
94 dev_t dev = ap->a_head.a_dev;
95 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
96 struct disklabel *label;
103 /* controller not active? */
104 if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
107 label = &sc->mlxd_disk.d_label;
108 bzero(label, sizeof(*label));
109 label->d_type = DTYPE_SCSI;
110 label->d_secsize = MLX_BLKSIZE;
111 label->d_nsectors = sc->mlxd_drive->ms_sectors;
112 label->d_ntracks = sc->mlxd_drive->ms_heads;
113 label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
114 label->d_secpercyl = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
115 label->d_secperunit = sc->mlxd_drive->ms_size;
117 sc->mlxd_flags |= MLXD_OPEN;
122 mlxd_close(struct dev_close_args *ap)
124 dev_t dev = ap->a_head.a_dev;
125 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
131 sc->mlxd_flags &= ~MLXD_OPEN;
136 mlxd_ioctl(struct dev_ioctl_args *ap)
138 dev_t dev = ap->a_head.a_dev;
139 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
147 if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, ap->a_cmd, ap->a_data, ap->a_fflag)) != ENOIOCTL) {
148 debug(0, "mlx_submit_ioctl returned %d\n", error);
155 * Read/write routine for a buffer. Finds the proper unit, range checks
156 * arguments, and schedules the transfer. Does not wait for the transfer
157 * to complete. Multi-page transfers are supported. All I/O requests must
158 * be a multiple of a sector in length.
161 mlxd_strategy(struct dev_strategy_args *ap)
163 struct bio *bio = ap->a_bio;
164 struct buf *bp = bio->bio_buf;
165 struct mlxd_softc *sc = (struct mlxd_softc *)bio->bio_driver_info;
171 bp->b_error = EINVAL;
172 bp->b_flags |= B_ERROR;
176 /* XXX may only be temporarily offline - sleep? */
177 if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
179 bp->b_flags |= B_ERROR;
183 devstat_start_transaction(&sc->mlxd_stats);
184 mlx_submit_bio(sc->mlxd_controller, bio);
189 * Correctly set the bio to indicate a failed tranfer.
191 bp->b_resid = bp->b_bcount;
197 mlxd_intr(struct bio *bio)
199 struct buf *bp = bio->bio_buf;
200 struct mlxd_softc *sc = (struct mlxd_softc *)bio->bio_driver_info;
204 if (bp->b_flags & B_ERROR)
208 devstat_end_transaction_buf(&sc->mlxd_stats, bp);
213 mlxd_probe(device_t dev)
218 device_set_desc(dev, "Mylex System Drive");
223 mlxd_attach(device_t dev)
225 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
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);
239 switch(sc->mlxd_drive->ms_state) {
240 case MLX_SYSD_ONLINE:
243 case MLX_SYSD_CRITICAL:
246 case MLX_SYSD_OFFLINE:
250 state = "unknown state";
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);
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);
262 dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_ops);
264 sc->mlxd_dev_t = dsk;
267 * Set maximum I/O size to the lesser of the recommended maximum and the practical
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);
278 mlxd_detach(device_t dev)
280 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
284 devstat_remove_entry(&sc->mlxd_stats);
285 disk_destroy(&sc->mlxd_disk);