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.7 2004/05/13 23:49:19 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 cdevsw mlxd_cdevsw = {
66 /* maj */ MLXD_CDEV_MAJOR,
72 /* close */ mlxd_close,
74 /* write */ physwrite,
75 /* ioctl */ mlxd_ioctl,
78 /* strategy */ mlxd_strategy,
83 devclass_t mlxd_devclass;
85 static device_method_t mlxd_methods[] = {
86 DEVMETHOD(device_probe, mlxd_probe),
87 DEVMETHOD(device_attach, mlxd_attach),
88 DEVMETHOD(device_detach, mlxd_detach),
92 static driver_t mlxd_driver = {
95 sizeof(struct mlxd_softc)
98 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
101 mlxd_open(dev_t dev, int flags, int fmt, d_thread_t *td)
103 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
104 struct disklabel *label;
111 /* controller not active? */
112 if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
115 label = &sc->mlxd_disk.d_label;
116 bzero(label, sizeof(*label));
117 label->d_type = DTYPE_SCSI;
118 label->d_secsize = MLX_BLKSIZE;
119 label->d_nsectors = sc->mlxd_drive->ms_sectors;
120 label->d_ntracks = sc->mlxd_drive->ms_heads;
121 label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
122 label->d_secpercyl = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
123 label->d_secperunit = sc->mlxd_drive->ms_size;
125 sc->mlxd_flags |= MLXD_OPEN;
130 mlxd_close(dev_t dev, int flags, int fmt, d_thread_t *td)
132 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
138 sc->mlxd_flags &= ~MLXD_OPEN;
143 mlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
145 struct mlxd_softc *sc = (struct mlxd_softc *)dev->si_drv1;
153 if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
154 debug(0, "mlx_submit_ioctl returned %d\n", error);
161 * Read/write routine for a buffer. Finds the proper unit, range checks
162 * arguments, and schedules the transfer. Does not wait for the transfer
163 * to complete. Multi-page transfers are supported. All I/O requests must
164 * be a multiple of a sector in length.
167 mlxd_strategy(mlx_bio *bp)
169 struct mlxd_softc *sc = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
175 MLX_BIO_SET_ERROR(bp, EINVAL);
179 /* XXX may only be temporarily offline - sleep? */
180 if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
181 MLX_BIO_SET_ERROR(bp, ENXIO);
185 MLX_BIO_STATS_START(bp);
186 mlx_submit_buf(sc->mlxd_controller, bp);
191 * Correctly set the bio to indicate a failed tranfer.
193 MLX_BIO_RESID(bp) = MLX_BIO_LENGTH(bp);
199 mlxd_intr(void *data)
201 mlx_bio *bp = (mlx_bio *)data;
205 if (MLX_BIO_HAS_ERROR(bp))
206 MLX_BIO_SET_ERROR(bp, EIO);
208 MLX_BIO_RESID(bp) = 0;
210 MLX_BIO_STATS_END(bp);
215 mlxd_probe(device_t dev)
220 device_set_desc(dev, "Mylex System Drive");
225 mlxd_attach(device_t dev)
227 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
235 parent = device_get_parent(dev);
236 sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
237 sc->mlxd_unit = device_get_unit(dev);
238 sc->mlxd_drive = device_get_ivars(dev);
241 switch(sc->mlxd_drive->ms_state) {
242 case MLX_SYSD_ONLINE:
245 case MLX_SYSD_CRITICAL:
248 case MLX_SYSD_OFFLINE:
252 state = "unknown state";
255 device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
256 sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
257 sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
259 devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
260 DEVSTAT_NO_ORDERED_TAGS,
261 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
262 DEVSTAT_PRIORITY_ARRAY);
264 dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw);
266 sc->mlxd_dev_t = dsk;
269 * Set maximum I/O size to the lesser of the recommended maximum and the practical
272 s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
273 s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
274 dsk->si_iosize_max = imin(s1, s2);
280 mlxd_detach(device_t dev)
282 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
286 devstat_remove_entry(&sc->mlxd_stats);
287 disk_destroy(&sc->mlxd_disk);