mlx_disk.c revision 59136
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 59136 2000-04-11 02:52:46Z 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/malloc.h>
3751973Smsmith#include <sys/kernel.h>
3851973Smsmith
3951973Smsmith#include <sys/buf.h>
4051973Smsmith#include <sys/bus.h>
4151973Smsmith#include <sys/conf.h>
4251973Smsmith#include <sys/devicestat.h>
4351973Smsmith#include <sys/disk.h>
4451973Smsmith
4551973Smsmith#include <machine/bus.h>
4651973Smsmith#include <machine/clock.h>
4751973Smsmith#include <sys/rman.h>
4851973Smsmith
4951973Smsmith#include <dev/mlx/mlxio.h>
5051973Smsmith#include <dev/mlx/mlxvar.h>
5152544Smsmith#include <dev/mlx/mlxreg.h>
5251973Smsmith
5351973Smsmith/* prototypes */
5451973Smsmithstatic int mlxd_probe(device_t dev);
5551973Smsmithstatic int mlxd_attach(device_t dev);
5651973Smsmithstatic int mlxd_detach(device_t dev);
5751973Smsmith
5851973Smsmithstatic	d_open_t	mlxd_open;
5951973Smsmithstatic	d_close_t	mlxd_close;
6051973Smsmithstatic	d_strategy_t	mlxd_strategy;
6151973Smsmithstatic	d_ioctl_t	mlxd_ioctl;
6251973Smsmith
6351973Smsmith#define MLXD_BDEV_MAJOR	27
6451973Smsmith#define MLXD_CDEV_MAJOR	131
6551973Smsmith
6651973Smsmithstatic struct cdevsw mlxd_cdevsw = {
6751973Smsmith		/* open */	mlxd_open,
6851973Smsmith		/* close */	mlxd_close,
6951973Smsmith		/* read */	physread,
7051973Smsmith		/* write */	physwrite,
7151973Smsmith		/* ioctl */	mlxd_ioctl,
7251973Smsmith		/* poll */	nopoll,
7351973Smsmith		/* mmap */	nommap,
7451973Smsmith		/* strategy */	mlxd_strategy,
7551973Smsmith		/* name */ 	"mlxd",
7651973Smsmith		/* maj */	MLXD_CDEV_MAJOR,
7751973Smsmith		/* dump */	nodump,
7851973Smsmith		/* psize */ 	nopsize,
7951973Smsmith		/* flags */	D_DISK,
8051973Smsmith		/* bmaj */	MLXD_BDEV_MAJOR
8151973Smsmith};
8251973Smsmith
8359136Smsmithdevclass_t		mlxd_devclass;
8451973Smsmithstatic struct cdevsw	mlxddisk_cdevsw;
8551973Smsmith
8651973Smsmithstatic device_method_t mlxd_methods[] = {
8751973Smsmith    DEVMETHOD(device_probe,	mlxd_probe),
8851973Smsmith    DEVMETHOD(device_attach,	mlxd_attach),
8951973Smsmith    DEVMETHOD(device_detach,	mlxd_detach),
9051973Smsmith    { 0, 0 }
9151973Smsmith};
9251973Smsmith
9351973Smsmithstatic driver_t mlxd_driver = {
9451973Smsmith    "mlxd",
9551973Smsmith    mlxd_methods,
9651973Smsmith    sizeof(struct mlxd_softc)
9751973Smsmith};
9851973Smsmith
9951973SmsmithDRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
10051973Smsmith
10151973Smsmithstatic int
10251973Smsmithmlxd_open(dev_t dev, int flags, int fmt, struct proc *p)
10351973Smsmith{
10452225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
10551973Smsmith    struct disklabel	*label;
10651973Smsmith
10758188Smsmith    debug_called(1);
10851973Smsmith
10951973Smsmith    if (sc == NULL)
11051973Smsmith	return (ENXIO);
11151973Smsmith
11251973Smsmith    /* controller not active? */
11351973Smsmith    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
11451973Smsmith	return(ENXIO);
11551973Smsmith
11651973Smsmith    label = &sc->mlxd_disk.d_label;
11751973Smsmith    bzero(label, sizeof(*label));
11851973Smsmith    label->d_type = DTYPE_SCSI;
11951973Smsmith    label->d_secsize    = MLX_BLKSIZE;
12051973Smsmith    label->d_nsectors   = sc->mlxd_drive->ms_sectors;
12151973Smsmith    label->d_ntracks    = sc->mlxd_drive->ms_heads;
12251973Smsmith    label->d_ncylinders = sc->mlxd_drive->ms_cylinders;
12351973Smsmith    label->d_secpercyl  = sc->mlxd_drive->ms_sectors * sc->mlxd_drive->ms_heads;
12451973Smsmith    label->d_secperunit = sc->mlxd_drive->ms_size;
12551973Smsmith
12651973Smsmith    sc->mlxd_flags |= MLXD_OPEN;
12751973Smsmith    return (0);
12851973Smsmith}
12951973Smsmith
13051973Smsmithstatic int
13151973Smsmithmlxd_close(dev_t dev, int flags, int fmt, struct proc *p)
13251973Smsmith{
13352225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
13451973Smsmith
13558188Smsmith    debug_called(1);
13651973Smsmith
13751973Smsmith    if (sc == NULL)
13851973Smsmith	return (ENXIO);
13951973Smsmith    sc->mlxd_flags &= ~MLXD_OPEN;
14051973Smsmith    return (0);
14151973Smsmith}
14251973Smsmith
14351973Smsmithstatic int
14451973Smsmithmlxd_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct proc *p)
14551973Smsmith{
14652225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)dev->si_drv1;
14751973Smsmith    int error;
14851973Smsmith
14958188Smsmith    debug_called(1);
15051973Smsmith
15151973Smsmith    if (sc == NULL)
15251973Smsmith	return (ENXIO);
15351973Smsmith
15451973Smsmith    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, p)) != ENOIOCTL) {
15558188Smsmith	debug(0, "mlx_submit_ioctl returned %d\n", error);
15651973Smsmith	return(error);
15751973Smsmith    }
15851973Smsmith    return (ENOTTY);
15951973Smsmith}
16051973Smsmith
16151973Smsmith/*
16251973Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
16351973Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
16451973Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
16551973Smsmith * be a multiple of a sector in length.
16651973Smsmith */
16751973Smsmithstatic void
16851973Smsmithmlxd_strategy(struct buf *bp)
16951973Smsmith{
17052225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->b_dev->si_drv1;
17151973Smsmith
17258188Smsmith    debug_called(1);
17351973Smsmith
17451973Smsmith    /* bogus disk? */
17551973Smsmith    if (sc == NULL) {
17651973Smsmith	bp->b_error = EINVAL;
17751973Smsmith	goto bad;
17851973Smsmith    }
17951973Smsmith
18051973Smsmith    /* XXX may only be temporarily offline - sleep? */
18151973Smsmith    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
18251973Smsmith	bp->b_error = ENXIO;
18351973Smsmith	goto bad;
18451973Smsmith    }
18551973Smsmith
18651973Smsmith    /* do-nothing operation */
18751973Smsmith    if (bp->b_bcount == 0)
18851973Smsmith	goto done;
18951973Smsmith
19051973Smsmith    devstat_start_transaction(&sc->mlxd_stats);
19151973Smsmith    mlx_submit_buf(sc->mlxd_controller, bp);
19251973Smsmith    return;
19351973Smsmith
19451973Smsmith bad:
19558934Sphk    bp->b_ioflags |= BIO_ERROR;
19651973Smsmith
19751973Smsmith done:
19851973Smsmith    /*
19951973Smsmith     * Correctly set the buf to indicate a completed transfer
20051973Smsmith     */
20151973Smsmith    bp->b_resid = bp->b_bcount;
20251973Smsmith    biodone(bp);
20351973Smsmith    return;
20451973Smsmith}
20551973Smsmith
20651973Smsmithvoid
20751973Smsmithmlxd_intr(void *data)
20851973Smsmith{
20951973Smsmith    struct buf *bp = (struct buf *)data;
21052225Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)bp->b_dev->si_drv1;
21151973Smsmith
21258188Smsmith    debug_called(1);
21351973Smsmith
21458934Sphk    if (bp->b_ioflags & BIO_ERROR)
21551973Smsmith	bp->b_error = EIO;
21651973Smsmith    else
21751973Smsmith	bp->b_resid = 0;
21851973Smsmith
21951973Smsmith    devstat_end_transaction_buf(&sc->mlxd_stats, bp);
22051973Smsmith    biodone(bp);
22151973Smsmith}
22251973Smsmith
22351973Smsmithstatic int
22451973Smsmithmlxd_probe(device_t dev)
22551973Smsmith{
22651973Smsmith
22758188Smsmith    debug_called(1);
22851973Smsmith
22951973Smsmith    device_set_desc(dev, "Mylex System Drive");
23051973Smsmith    return (0);
23151973Smsmith}
23251973Smsmith
23351973Smsmithstatic int
23451973Smsmithmlxd_attach(device_t dev)
23551973Smsmith{
23651973Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
23751973Smsmith    device_t		parent;
23851973Smsmith    char		*state;
23952225Smsmith    dev_t		dsk;
24051973Smsmith
24158188Smsmith    debug_called(1);
24251973Smsmith
24351973Smsmith    parent = device_get_parent(dev);
24451973Smsmith    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
24551973Smsmith    sc->mlxd_unit = device_get_unit(dev);
24651973Smsmith    sc->mlxd_drive = device_get_ivars(dev);
24752273Smsmith    sc->mlxd_dev = dev;
24851973Smsmith
24951973Smsmith    switch(sc->mlxd_drive->ms_state) {
25051973Smsmith    case MLX_SYSD_ONLINE:
25151973Smsmith	state = "online";
25251973Smsmith	break;
25351973Smsmith    case MLX_SYSD_CRITICAL:
25451973Smsmith	state = "critical";
25551973Smsmith	break;
25651973Smsmith    case MLX_SYSD_OFFLINE:
25751973Smsmith	state = "offline";
25851973Smsmith	break;
25951973Smsmith    default:
26051973Smsmith	state = "unknown state";
26151973Smsmith    }
26251973Smsmith
26352785Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
26451973Smsmith		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
26551973Smsmith		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
26651973Smsmith
26751973Smsmith    devstat_add_entry(&sc->mlxd_stats, "mlxd", sc->mlxd_unit, MLX_BLKSIZE,
26851973Smsmith		      DEVSTAT_NO_ORDERED_TAGS,
26954279Sken		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
27054279Sken		      DEVSTAT_PRIORITY_ARRAY);
27151973Smsmith
27252225Smsmith    dsk = disk_create(sc->mlxd_unit, &sc->mlxd_disk, 0, &mlxd_cdevsw, &mlxddisk_cdevsw);
27354419Smsmith    dsk->si_drv1 = sc;
27459136Smsmith    sc->mlxd_dev_t = dsk;
27551973Smsmith
27652225Smsmith    /* set maximum I/O size */
27754979Smsmith    dsk->si_iosize_max = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
27852225Smsmith
27951973Smsmith    return (0);
28051973Smsmith}
28151973Smsmith
28251973Smsmithstatic int
28351973Smsmithmlxd_detach(device_t dev)
28451973Smsmith{
28551973Smsmith    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
28651973Smsmith
28758188Smsmith    debug_called(1);
28851973Smsmith
28951973Smsmith    devstat_remove_entry(&sc->mlxd_stats);
29059136Smsmith    disk_destroy(sc->mlxd_dev_t);
29151973Smsmith
29251973Smsmith    return(0);
29351973Smsmith}
29451973Smsmith
295