mlx_disk.c revision 111216
151973Smsmith/*-
251973Smsmith * Copyright (c) 1999 Jonathan Lemon
351973Smsmith * Copyright (c) 1999 Michael Smith
451973Smsmith * All rights reserved.
551973Smsmith *
651973Smsmith * Redistribution and use in source and binary forms, with or without
751973Smsmith * modification, are permitted provided that the following conditions
851973Smsmith * are met:
951973Smsmith * 1. Redistributions of source code must retain the above copyright
1051973Smsmith *    notice, this list of conditions and the following disclaimer.
1151973Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1251973Smsmith *    notice, this list of conditions and the following disclaimer in the
1351973Smsmith *    documentation and/or other materials provided with the distribution.
1451973Smsmith *
1551973Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1651973Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1751973Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1851973Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1951973Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2051973Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2151973Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2251973Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2351973Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2451973Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2551973Smsmith * SUCH DAMAGE.
2651973Smsmith *
2751973Smsmith * $FreeBSD: head/sys/dev/mlx/mlx_disk.c 111216 2003-02-21 15:13:26Z phk $
2851973Smsmith */
2951973Smsmith
3051973Smsmith/*
3151973Smsmith * Disk driver for Mylex DAC960 RAID adapters.
3251973Smsmith */
3351973Smsmith
3451973Smsmith#include <sys/param.h>
3551973Smsmith#include <sys/systm.h>
3651973Smsmith#include <sys/kernel.h>
3751973Smsmith
3851973Smsmith#include <sys/bus.h>
3951973Smsmith#include <sys/conf.h>
4051973Smsmith#include <sys/devicestat.h>
4151973Smsmith#include <sys/disk.h>
4251973Smsmith
4351973Smsmith#include <machine/bus.h>
4451973Smsmith#include <sys/rman.h>
4551973Smsmith
4678752Smsmith#include <dev/mlx/mlx_compat.h>
4751973Smsmith#include <dev/mlx/mlxio.h>
4851973Smsmith#include <dev/mlx/mlxvar.h>
4952544Smsmith#include <dev/mlx/mlxreg.h>
5051973Smsmith
5151973Smsmith/* prototypes */
5251973Smsmithstatic int mlxd_probe(device_t dev);
5351973Smsmithstatic int mlxd_attach(device_t dev);
5451973Smsmithstatic int mlxd_detach(device_t dev);
5551973Smsmith
5651973Smsmithstatic	d_open_t	mlxd_open;
5751973Smsmithstatic	d_close_t	mlxd_close;
5851973Smsmithstatic	d_strategy_t	mlxd_strategy;
5951973Smsmithstatic	d_ioctl_t	mlxd_ioctl;
6051973Smsmith
6151973Smsmith#define MLXD_CDEV_MAJOR	131
6251973Smsmith
6351973Smsmithstatic struct cdevsw mlxd_cdevsw = {
6451973Smsmith		/* open */	mlxd_open,
6551973Smsmith		/* close */	mlxd_close,
6651973Smsmith		/* read */	physread,
6751973Smsmith		/* write */	physwrite,
6851973Smsmith		/* ioctl */	mlxd_ioctl,
6951973Smsmith		/* poll */	nopoll,
7051973Smsmith		/* mmap */	nommap,
7151973Smsmith		/* strategy */	mlxd_strategy,
7251973Smsmith		/* name */ 	"mlxd",
7351973Smsmith		/* maj */	MLXD_CDEV_MAJOR,
7451973Smsmith		/* dump */	nodump,
7551973Smsmith		/* psize */ 	nopsize,
7651973Smsmith		/* flags */	D_DISK,
7751973Smsmith};
7851973Smsmith
7959136Smsmithdevclass_t		mlxd_devclass;
8051973Smsmithstatic struct cdevsw	mlxddisk_cdevsw;
8151973Smsmith
8251973Smsmithstatic device_method_t mlxd_methods[] = {
8351973Smsmith    DEVMETHOD(device_probe,	mlxd_probe),
8451973Smsmith    DEVMETHOD(device_attach,	mlxd_attach),
8551973Smsmith    DEVMETHOD(device_detach,	mlxd_detach),
8651973Smsmith    { 0, 0 }
8751973Smsmith};
8851973Smsmith
8951973Smsmithstatic driver_t mlxd_driver = {
9051973Smsmith    "mlxd",
9151973Smsmith    mlxd_methods,
9251973Smsmith    sizeof(struct mlxd_softc)
9351973Smsmith};
9451973Smsmith
9551973SmsmithDRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
9651973Smsmith
9751973Smsmithstatic int
9883366Sjulianmlxd_open(dev_t dev, int flags, int fmt, struct thread *td)
9951973Smsmith{
10052225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
10151973Smsmith
10258188Smsmith    debug_called(1);
10351973Smsmith
10451973Smsmith    if (sc == NULL)
10551973Smsmith	return (ENXIO);
10651973Smsmith
10751973Smsmith    /* controller not active? */
10851973Smsmith    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
10951973Smsmith	return(ENXIO);
11051973Smsmith
111103714Sphk    sc->mlxd_disk.d_sectorsize = MLX_BLKSIZE;
112103714Sphk    sc->mlxd_disk.d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
113103714Sphk    sc->mlxd_disk.d_fwsectors = sc->mlxd_drive->ms_sectors;
114103714Sphk    sc->mlxd_disk.d_fwheads = sc->mlxd_drive->ms_heads;
11551973Smsmith
11651973Smsmith    sc->mlxd_flags |= MLXD_OPEN;
11751973Smsmith    return (0);
11851973Smsmith}
11951973Smsmith
12051973Smsmithstatic int
12183366Sjulianmlxd_close(dev_t dev, int flags, int fmt, struct thread *td)
12251973Smsmith{
12352225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
12451973Smsmith
12558188Smsmith    debug_called(1);
12651973Smsmith
12751973Smsmith    if (sc == NULL)
12851973Smsmith	return (ENXIO);
12951973Smsmith    sc->mlxd_flags &= ~MLXD_OPEN;
13051973Smsmith    return (0);
13151973Smsmith}
13251973Smsmith
13351973Smsmithstatic int
13483366Sjulianmlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
13551973Smsmith{
13652225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
13751973Smsmith    int error;
13851973Smsmith
13958188Smsmith    debug_called(1);
14051973Smsmith
14151973Smsmith    if (sc == NULL)
14251973Smsmith	return (ENXIO);
14351973Smsmith
14483366Sjulian    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
14558188Smsmith	debug(0, "mlx_submit_ioctl returned %d\n", error);
14651973Smsmith	return(error);
14751973Smsmith    }
14851973Smsmith    return (ENOTTY);
14951973Smsmith}
15051973Smsmith
15151973Smsmith/*
15251973Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
15351973Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
15451973Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
15551973Smsmith * be a multiple of a sector in length.
15651973Smsmith */
15751973Smsmithstatic void
15878752Smsmithmlxd_strategy(mlx_bio *bp)
15951973Smsmith{
16078752Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
16151973Smsmith
16258188Smsmith    debug_called(1);
16351973Smsmith
16451973Smsmith    /* bogus disk? */
16551973Smsmith    if (sc == NULL) {
16678752Smsmith	MLX_BIO_SET_ERROR(bp, EINVAL);
16751973Smsmith	goto bad;
16851973Smsmith    }
16951973Smsmith
17051973Smsmith    /* XXX may only be temporarily offline - sleep? */
17151973Smsmith    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
17278752Smsmith	MLX_BIO_SET_ERROR(bp, ENXIO);
17351973Smsmith	goto bad;
17451973Smsmith    }
17551973Smsmith
17678752Smsmith    MLX_BIO_STATS_START(bp);
17751973Smsmith    mlx_submit_buf(sc->mlxd_controller, bp);
17851973Smsmith    return;
17951973Smsmith
18051973Smsmith bad:
18151973Smsmith    /*
18278752Smsmith     * Correctly set the bio to indicate a failed tranfer.
18351973Smsmith     */
18478752Smsmith    MLX_BIO_RESID(bp) = MLX_BIO_LENGTH(bp);
18578752Smsmith    MLX_BIO_DONE(bp);
18651973Smsmith    return;
18751973Smsmith}
18851973Smsmith
18951973Smsmithvoid
19051973Smsmithmlxd_intr(void *data)
19151973Smsmith{
19278752Smsmith    mlx_bio 		*bp = (mlx_bio *)data;
19351973Smsmith
19458188Smsmith    debug_called(1);
19551973Smsmith
19678752Smsmith    if (MLX_BIO_HAS_ERROR(bp))
19778752Smsmith	MLX_BIO_SET_ERROR(bp, EIO);
19851973Smsmith    else
19978752Smsmith	MLX_BIO_RESID(bp) = 0;
20051973Smsmith
20178752Smsmith    MLX_BIO_STATS_END(bp);
20278752Smsmith    MLX_BIO_DONE(bp);
20351973Smsmith}
20451973Smsmith
20551973Smsmithstatic int
20651973Smsmithmlxd_probe(device_t dev)
20751973Smsmith{
20851973Smsmith
20958188Smsmith    debug_called(1);
21051973Smsmith
21151973Smsmith    device_set_desc(dev, "Mylex System Drive");
21251973Smsmith    return (0);
21351973Smsmith}
21451973Smsmith
21551973Smsmithstatic int
21651973Smsmithmlxd_attach(device_t dev)
21751973Smsmith{
21851973Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
21951973Smsmith    device_t		parent;
22051973Smsmith    char		*state;
22152225Smsmith    dev_t		dsk;
22260074Smsmith    int			s1, s2;
22351973Smsmith
22458188Smsmith    debug_called(1);
22551973Smsmith
22651973Smsmith    parent = device_get_parent(dev);
22751973Smsmith    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
22851973Smsmith    sc->mlxd_unit = device_get_unit(dev);
22951973Smsmith    sc->mlxd_drive = device_get_ivars(dev);
23052273Smsmith    sc->mlxd_dev = dev;
23151973Smsmith
23251973Smsmith    switch(sc->mlxd_drive->ms_state) {
23351973Smsmith    case MLX_SYSD_ONLINE:
23451973Smsmith	state = "online";
23551973Smsmith	break;
23651973Smsmith    case MLX_SYSD_CRITICAL:
23751973Smsmith	state = "critical";
23851973Smsmith	break;
23951973Smsmith    case MLX_SYSD_OFFLINE:
24051973Smsmith	state = "offline";
24151973Smsmith	break;
24251973Smsmith    default:
24351973Smsmith	state = "unknown state";
24451973Smsmith    }
24551973Smsmith
24652785Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
24751973Smsmith		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
24851973Smsmith		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
24951973Smsmith
25051973Smsmith    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
25151973Smsmith		      DEVSTAT_NO_ORDERED_TAGS,
25254279Sken		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
25354279Sken		      DEVSTAT_PRIORITY_ARRAY);
25451973Smsmith
25552225Smsmith    dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw);
25654419Smsmith    dsk->si_drv1 = sc;
25759136Smsmith    sc->mlxd_dev_t = dsk;
25851973Smsmith
25960074Smsmith    /*
26060074Smsmith     * Set maximum I/O size to the lesser of the recommended maximum and the practical
26160074Smsmith     * maximum.
26260074Smsmith     */
26360074Smsmith    s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
26460074Smsmith    s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
26560074Smsmith    dsk->si_iosize_max = imin(s1, s2);
26652225Smsmith
26751973Smsmith    return (0);
26851973Smsmith}
26951973Smsmith
27051973Smsmithstatic int
27151973Smsmithmlxd_detach(device_t dev)
27251973Smsmith{
27351973Smsmith    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
27451973Smsmith
27558188Smsmith    debug_called(1);
27651973Smsmith
27751973Smsmith    devstat_remove_entry(&sc->mlxd_stats);
278111216Sphk    disk_destroy(&sc->mlxd_disk);
27951973Smsmith
28051973Smsmith    return(0);
28151973Smsmith}
28251973Smsmith
283