2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Jonathan Lemon
5 * Copyright (c) 1999 Michael Smith
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Disk driver for Mylex DAC960 RAID adapters.
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
49 #include <machine/bus.h>
52 #include <geom/geom_disk.h>
54 #include <dev/mlx/mlxio.h>
55 #include <dev/mlx/mlxvar.h>
56 #include <dev/mlx/mlxreg.h>
59 static int mlxd_probe(device_t dev);
60 static int mlxd_attach(device_t dev);
61 static int mlxd_detach(device_t dev);
63 devclass_t mlxd_devclass;
65 static device_method_t mlxd_methods[] = {
66 DEVMETHOD(device_probe, mlxd_probe),
67 DEVMETHOD(device_attach, mlxd_attach),
68 DEVMETHOD(device_detach, mlxd_detach),
72 static driver_t mlxd_driver = {
75 sizeof(struct mlxd_softc)
78 DRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
81 mlxd_open(struct disk *dp)
83 struct mlxd_softc *sc = (struct mlxd_softc *)dp->d_drv1;
90 /* controller not active? */
91 MLX_CONFIG_LOCK(sc->mlxd_controller);
92 MLX_IO_LOCK(sc->mlxd_controller);
93 if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN) {
94 MLX_IO_UNLOCK(sc->mlxd_controller);
95 MLX_CONFIG_UNLOCK(sc->mlxd_controller);
99 sc->mlxd_flags |= MLXD_OPEN;
100 MLX_IO_UNLOCK(sc->mlxd_controller);
101 MLX_CONFIG_UNLOCK(sc->mlxd_controller);
106 mlxd_close(struct disk *dp)
108 struct mlxd_softc *sc = (struct mlxd_softc *)dp->d_drv1;
114 MLX_CONFIG_LOCK(sc->mlxd_controller);
115 MLX_IO_LOCK(sc->mlxd_controller);
116 sc->mlxd_flags &= ~MLXD_OPEN;
117 MLX_IO_UNLOCK(sc->mlxd_controller);
118 MLX_CONFIG_UNLOCK(sc->mlxd_controller);
123 mlxd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
125 struct mlxd_softc *sc = (struct mlxd_softc *)dp->d_drv1;
133 if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
134 debug(0, "mlx_submit_ioctl returned %d\n", error);
141 * Read/write routine for a buffer. Finds the proper unit, range checks
142 * arguments, and schedules the transfer. Does not wait for the transfer
143 * to complete. Multi-page transfers are supported. All I/O requests must
144 * be a multiple of a sector in length.
147 mlxd_strategy(struct bio *bp)
149 struct mlxd_softc *sc = bp->bio_disk->d_drv1;
155 bp->bio_error = EINVAL;
156 bp->bio_flags |= BIO_ERROR;
160 if ((bp->bio_cmd != BIO_READ) && (bp->bio_cmd != BIO_WRITE)) {
161 bp->bio_error = EOPNOTSUPP;
165 /* XXX may only be temporarily offline - sleep? */
166 MLX_IO_LOCK(sc->mlxd_controller);
167 if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
168 MLX_IO_UNLOCK(sc->mlxd_controller);
169 bp->bio_error = ENXIO;
170 bp->bio_flags |= BIO_ERROR;
174 mlx_submit_buf(sc->mlxd_controller, bp);
175 MLX_IO_UNLOCK(sc->mlxd_controller);
180 * Correctly set the bio to indicate a failed transfer.
182 bp->bio_resid = bp->bio_bcount;
188 mlxd_intr(struct bio *bp)
193 if (bp->bio_flags & BIO_ERROR)
202 mlxd_probe(device_t dev)
207 device_set_desc(dev, "Mylex System Drive");
212 mlxd_attach(device_t dev)
214 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
221 parent = device_get_parent(dev);
222 sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
223 sc->mlxd_unit = device_get_unit(dev);
224 sc->mlxd_drive = device_get_ivars(dev);
227 switch(sc->mlxd_drive->ms_state) {
228 case MLX_SYSD_ONLINE:
231 case MLX_SYSD_CRITICAL:
234 case MLX_SYSD_OFFLINE:
238 state = "unknown state";
241 device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
242 sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
243 sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
245 sc->mlxd_disk = disk_alloc();
246 sc->mlxd_disk->d_open = mlxd_open;
247 sc->mlxd_disk->d_close = mlxd_close;
248 sc->mlxd_disk->d_ioctl = mlxd_ioctl;
249 sc->mlxd_disk->d_strategy = mlxd_strategy;
250 sc->mlxd_disk->d_name = "mlxd";
251 sc->mlxd_disk->d_unit = sc->mlxd_unit;
252 sc->mlxd_disk->d_drv1 = sc;
253 sc->mlxd_disk->d_sectorsize = MLX_BLKSIZE;
254 sc->mlxd_disk->d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
255 sc->mlxd_disk->d_fwsectors = sc->mlxd_drive->ms_sectors;
256 sc->mlxd_disk->d_fwheads = sc->mlxd_drive->ms_heads;
259 * Set maximum I/O size to the lesser of the recommended maximum and the practical
260 * maximum except on v2 cards where the maximum is set to 8 pages.
262 if (sc->mlxd_controller->mlx_iftype == MLX_IFTYPE_2)
263 sc->mlxd_disk->d_maxsize = 8 * MLX_PAGE_SIZE;
265 s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
266 s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * MLX_PAGE_SIZE;
267 sc->mlxd_disk->d_maxsize = imin(s1, s2);
270 disk_create(sc->mlxd_disk, DISK_VERSION);
276 mlxd_detach(device_t dev)
278 struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
282 disk_destroy(sc->mlxd_disk);