mlx_disk.c revision 78752
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 78752 2001-06-25 04:32:31Z msmith $
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
9851973Smsmithmlxd_open(dev_t dev, int flags, int fmt, struct proc *p)
9951973Smsmith{
10052225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
10151973Smsmith    struct disklabel	*label;
10251973Smsmith
10358188Smsmith    debug_called(1);
10451973Smsmith
10551973Smsmith    if (sc == NULL)
10651973Smsmith	return (ENXIO);
10751973Smsmith
10851973Smsmith    /* controller not active? */
10951973Smsmith    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
11051973Smsmith	return(ENXIO);
11151973Smsmith
11251973Smsmith    label = &sc->mlxd_disk.d_label;
11351973Smsmith    bzero(label, sizeof(*label));
11451973Smsmith    label->d_type = DTYPE_SCSI;
11551973Smsmith    label->d_secsize    = MLX_BLKSIZE;
11651973Smsmith    label->d_nsectors   = sc->mlxd_drive->ms_sectors;
11751973Smsmith    label->d_ntracks    = sc->mlxd_drive->ms_heads;
11851973Smsmith    label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
11951973Smsmith    label->d_secpercyl  = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
12051973Smsmith    label->d_secperunit = sc->mlxd_drive->ms_size;
12151973Smsmith
12251973Smsmith    sc->mlxd_flags |= MLXD_OPEN;
12351973Smsmith    return (0);
12451973Smsmith}
12551973Smsmith
12651973Smsmithstatic int
12751973Smsmithmlxd_close(dev_t dev, int flags, int fmt, struct proc *p)
12851973Smsmith{
12952225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
13051973Smsmith
13158188Smsmith    debug_called(1);
13251973Smsmith
13351973Smsmith    if (sc == NULL)
13451973Smsmith	return (ENXIO);
13551973Smsmith    sc->mlxd_flags &= ~MLXD_OPEN;
13651973Smsmith    return (0);
13751973Smsmith}
13851973Smsmith
13951973Smsmithstatic int
14051973Smsmithmlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
14151973Smsmith{
14252225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
14351973Smsmith    int error;
14451973Smsmith
14558188Smsmith    debug_called(1);
14651973Smsmith
14751973Smsmith    if (sc == NULL)
14851973Smsmith	return (ENXIO);
14951973Smsmith
15051973Smsmith    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, p)) != ENOIOCTL) {
15158188Smsmith	debug(0, "mlx_submit_ioctl returned %d\n", error);
15251973Smsmith	return(error);
15351973Smsmith    }
15451973Smsmith    return (ENOTTY);
15551973Smsmith}
15651973Smsmith
15751973Smsmith/*
15851973Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
15951973Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
16051973Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
16151973Smsmith * be a multiple of a sector in length.
16251973Smsmith */
16351973Smsmithstatic void
16478752Smsmithmlxd_strategy(mlx_bio *bp)
16551973Smsmith{
16678752Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
16751973Smsmith
16858188Smsmith    debug_called(1);
16951973Smsmith
17051973Smsmith    /* bogus disk? */
17151973Smsmith    if (sc == NULL) {
17278752Smsmith	MLX_BIO_SET_ERROR(bp, EINVAL);
17351973Smsmith	goto bad;
17451973Smsmith    }
17551973Smsmith
17651973Smsmith    /* XXX may only be temporarily offline - sleep? */
17751973Smsmith    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
17878752Smsmith	MLX_BIO_SET_ERROR(bp, ENXIO);
17951973Smsmith	goto bad;
18051973Smsmith    }
18151973Smsmith
18278752Smsmith    MLX_BIO_STATS_START(bp);
18351973Smsmith    mlx_submit_buf(sc->mlxd_controller, bp);
18451973Smsmith    return;
18551973Smsmith
18651973Smsmith bad:
18751973Smsmith    /*
18878752Smsmith     * Correctly set the bio to indicate a failed tranfer.
18951973Smsmith     */
19078752Smsmith    MLX_BIO_RESID(bp) = MLX_BIO_LENGTH(bp);
19178752Smsmith    MLX_BIO_DONE(bp);
19251973Smsmith    return;
19351973Smsmith}
19451973Smsmith
19551973Smsmithvoid
19651973Smsmithmlxd_intr(void *data)
19751973Smsmith{
19878752Smsmith    mlx_bio 		*bp = (mlx_bio *)data;
19951973Smsmith
20058188Smsmith    debug_called(1);
20151973Smsmith
20278752Smsmith    if (MLX_BIO_HAS_ERROR(bp))
20378752Smsmith	MLX_BIO_SET_ERROR(bp, EIO);
20451973Smsmith    else
20578752Smsmith	MLX_BIO_RESID(bp) = 0;
20651973Smsmith
20778752Smsmith    MLX_BIO_STATS_END(bp);
20878752Smsmith    MLX_BIO_DONE(bp);
20951973Smsmith}
21051973Smsmith
21151973Smsmithstatic int
21251973Smsmithmlxd_probe(device_t dev)
21351973Smsmith{
21451973Smsmith
21558188Smsmith    debug_called(1);
21651973Smsmith
21751973Smsmith    device_set_desc(dev, "Mylex System Drive");
21851973Smsmith    return (0);
21951973Smsmith}
22051973Smsmith
22151973Smsmithstatic int
22251973Smsmithmlxd_attach(device_t dev)
22351973Smsmith{
22451973Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
22551973Smsmith    device_t		parent;
22651973Smsmith    char		*state;
22752225Smsmith    dev_t		dsk;
22860074Smsmith    int			s1, s2;
22951973Smsmith
23058188Smsmith    debug_called(1);
23151973Smsmith
23251973Smsmith    parent = device_get_parent(dev);
23351973Smsmith    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
23451973Smsmith    sc->mlxd_unit = device_get_unit(dev);
23551973Smsmith    sc->mlxd_drive = device_get_ivars(dev);
23652273Smsmith    sc->mlxd_dev = dev;
23751973Smsmith
23851973Smsmith    switch(sc->mlxd_drive->ms_state) {
23951973Smsmith    case MLX_SYSD_ONLINE:
24051973Smsmith	state = "online";
24151973Smsmith	break;
24251973Smsmith    case MLX_SYSD_CRITICAL:
24351973Smsmith	state = "critical";
24451973Smsmith	break;
24551973Smsmith    case MLX_SYSD_OFFLINE:
24651973Smsmith	state = "offline";
24751973Smsmith	break;
24851973Smsmith    default:
24951973Smsmith	state = "unknown state";
25051973Smsmith    }
25151973Smsmith
25252785Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
25351973Smsmith		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
25451973Smsmith		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
25551973Smsmith
25651973Smsmith    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
25751973Smsmith		      DEVSTAT_NO_ORDERED_TAGS,
25854279Sken		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
25954279Sken		      DEVSTAT_PRIORITY_ARRAY);
26051973Smsmith
26152225Smsmith    dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw);
26254419Smsmith    dsk->si_drv1 = sc;
26359136Smsmith    sc->mlxd_dev_t = dsk;
26451973Smsmith
26560074Smsmith    /*
26660074Smsmith     * Set maximum I/O size to the lesser of the recommended maximum and the practical
26760074Smsmith     * maximum.
26860074Smsmith     */
26960074Smsmith    s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
27060074Smsmith    s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * PAGE_SIZE;
27160074Smsmith    dsk->si_iosize_max = imin(s1, s2);
27252225Smsmith
27351973Smsmith    return (0);
27451973Smsmith}
27551973Smsmith
27651973Smsmithstatic int
27751973Smsmithmlxd_detach(device_t dev)
27851973Smsmith{
27951973Smsmith    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
28051973Smsmith
28158188Smsmith    debug_called(1);
28251973Smsmith
28351973Smsmith    devstat_remove_entry(&sc->mlxd_stats);
28459136Smsmith    disk_destroy(sc->mlxd_dev_t);
28551973Smsmith
28651973Smsmith    return(0);
28751973Smsmith}
28851973Smsmith
289