mlx_disk.c revision 138090
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 */
2851973Smsmith
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/mlx/mlx_disk.c 138090 2004-11-25 12:15:49Z scottl $");
31119418Sobrien
3251973Smsmith/*
3351973Smsmith * Disk driver for Mylex DAC960 RAID adapters.
3451973Smsmith */
3551973Smsmith
3651973Smsmith#include <sys/param.h>
3751973Smsmith#include <sys/systm.h>
3851973Smsmith#include <sys/kernel.h>
39129879Sphk#include <sys/module.h>
4051973Smsmith
4151973Smsmith#include <sys/bus.h>
4251973Smsmith#include <sys/conf.h>
4351973Smsmith
4451973Smsmith#include <machine/bus.h>
4551973Smsmith#include <sys/rman.h>
4651973Smsmith
47112946Sphk#include <geom/geom_disk.h>
48112946Sphk
4978752Smsmith#include <dev/mlx/mlx_compat.h>
5051973Smsmith#include <dev/mlx/mlxio.h>
5151973Smsmith#include <dev/mlx/mlxvar.h>
5252544Smsmith#include <dev/mlx/mlxreg.h>
5351973Smsmith
5451973Smsmith/* prototypes */
5551973Smsmithstatic int mlxd_probe(device_t dev);
5651973Smsmithstatic int mlxd_attach(device_t dev);
5751973Smsmithstatic int mlxd_detach(device_t dev);
5851973Smsmith
5959136Smsmithdevclass_t		mlxd_devclass;
6051973Smsmith
6151973Smsmithstatic device_method_t mlxd_methods[] = {
6251973Smsmith    DEVMETHOD(device_probe,	mlxd_probe),
6351973Smsmith    DEVMETHOD(device_attach,	mlxd_attach),
6451973Smsmith    DEVMETHOD(device_detach,	mlxd_detach),
6551973Smsmith    { 0, 0 }
6651973Smsmith};
6751973Smsmith
6851973Smsmithstatic driver_t mlxd_driver = {
6951973Smsmith    "mlxd",
7051973Smsmith    mlxd_methods,
7151973Smsmith    sizeof(struct mlxd_softc)
7251973Smsmith};
7351973Smsmith
7451973SmsmithDRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
7551973Smsmith
7651973Smsmithstatic int
77111469Sphkmlxd_open(struct disk *dp)
7851973Smsmith{
79111469Sphk    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
8051973Smsmith
8158188Smsmith    debug_called(1);
8251973Smsmith
8351973Smsmith    if (sc == NULL)
8451973Smsmith	return (ENXIO);
8551973Smsmith
8651973Smsmith    /* controller not active? */
8751973Smsmith    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN)
8851973Smsmith	return(ENXIO);
8951973Smsmith
9051973Smsmith    sc->mlxd_flags |= MLXD_OPEN;
9151973Smsmith    return (0);
9251973Smsmith}
9351973Smsmith
9451973Smsmithstatic int
95111469Sphkmlxd_close(struct disk *dp)
9651973Smsmith{
97111469Sphk    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
9851973Smsmith
9958188Smsmith    debug_called(1);
10051973Smsmith
10151973Smsmith    if (sc == NULL)
10251973Smsmith	return (ENXIO);
10351973Smsmith    sc->mlxd_flags &= ~MLXD_OPEN;
10451973Smsmith    return (0);
10551973Smsmith}
10651973Smsmith
10751973Smsmithstatic int
108111469Sphkmlxd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
10951973Smsmith{
110111469Sphk    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
11151973Smsmith    int error;
11251973Smsmith
11358188Smsmith    debug_called(1);
11451973Smsmith
11551973Smsmith    if (sc == NULL)
11651973Smsmith	return (ENXIO);
11751973Smsmith
11883366Sjulian    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
11958188Smsmith	debug(0, "mlx_submit_ioctl returned %d\n", error);
12051973Smsmith	return(error);
12151973Smsmith    }
12251973Smsmith    return (ENOTTY);
12351973Smsmith}
12451973Smsmith
12551973Smsmith/*
12651973Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
12751973Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
12851973Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
12951973Smsmith * be a multiple of a sector in length.
13051973Smsmith */
13151973Smsmithstatic void
13278752Smsmithmlxd_strategy(mlx_bio *bp)
13351973Smsmith{
13478752Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)MLX_BIO_SOFTC(bp);
13551973Smsmith
13658188Smsmith    debug_called(1);
13751973Smsmith
13851973Smsmith    /* bogus disk? */
13951973Smsmith    if (sc == NULL) {
14078752Smsmith	MLX_BIO_SET_ERROR(bp, EINVAL);
14151973Smsmith	goto bad;
14251973Smsmith    }
14351973Smsmith
14451973Smsmith    /* XXX may only be temporarily offline - sleep? */
14551973Smsmith    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
14678752Smsmith	MLX_BIO_SET_ERROR(bp, ENXIO);
14751973Smsmith	goto bad;
14851973Smsmith    }
14951973Smsmith
15078752Smsmith    MLX_BIO_STATS_START(bp);
15151973Smsmith    mlx_submit_buf(sc->mlxd_controller, bp);
15251973Smsmith    return;
15351973Smsmith
15451973Smsmith bad:
15551973Smsmith    /*
15678752Smsmith     * Correctly set the bio to indicate a failed tranfer.
15751973Smsmith     */
15878752Smsmith    MLX_BIO_RESID(bp) = MLX_BIO_LENGTH(bp);
15978752Smsmith    MLX_BIO_DONE(bp);
16051973Smsmith    return;
16151973Smsmith}
16251973Smsmith
16351973Smsmithvoid
16451973Smsmithmlxd_intr(void *data)
16551973Smsmith{
16678752Smsmith    mlx_bio 		*bp = (mlx_bio *)data;
16751973Smsmith
16858188Smsmith    debug_called(1);
16951973Smsmith
17078752Smsmith    if (MLX_BIO_HAS_ERROR(bp))
17178752Smsmith	MLX_BIO_SET_ERROR(bp, EIO);
17251973Smsmith    else
17378752Smsmith	MLX_BIO_RESID(bp) = 0;
17451973Smsmith
17578752Smsmith    MLX_BIO_STATS_END(bp);
17678752Smsmith    MLX_BIO_DONE(bp);
17751973Smsmith}
17851973Smsmith
17951973Smsmithstatic int
18051973Smsmithmlxd_probe(device_t dev)
18151973Smsmith{
18251973Smsmith
18358188Smsmith    debug_called(1);
18451973Smsmith
18551973Smsmith    device_set_desc(dev, "Mylex System Drive");
18651973Smsmith    return (0);
18751973Smsmith}
18851973Smsmith
18951973Smsmithstatic int
19051973Smsmithmlxd_attach(device_t dev)
19151973Smsmith{
19251973Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
19351973Smsmith    device_t		parent;
19451973Smsmith    char		*state;
19560074Smsmith    int			s1, s2;
19651973Smsmith
19758188Smsmith    debug_called(1);
19851973Smsmith
19951973Smsmith    parent = device_get_parent(dev);
20051973Smsmith    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
20151973Smsmith    sc->mlxd_unit = device_get_unit(dev);
20251973Smsmith    sc->mlxd_drive = device_get_ivars(dev);
20352273Smsmith    sc->mlxd_dev = dev;
20451973Smsmith
20551973Smsmith    switch(sc->mlxd_drive->ms_state) {
20651973Smsmith    case MLX_SYSD_ONLINE:
20751973Smsmith	state = "online";
20851973Smsmith	break;
20951973Smsmith    case MLX_SYSD_CRITICAL:
21051973Smsmith	state = "critical";
21151973Smsmith	break;
21251973Smsmith    case MLX_SYSD_OFFLINE:
21351973Smsmith	state = "offline";
21451973Smsmith	break;
21551973Smsmith    default:
21651973Smsmith	state = "unknown state";
21751973Smsmith    }
21851973Smsmith
21952785Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
22051973Smsmith		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
22151973Smsmith		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
22251973Smsmith
223125975Sphk    sc->mlxd_disk = disk_alloc();
224125975Sphk    sc->mlxd_disk->d_open = mlxd_open;
225125975Sphk    sc->mlxd_disk->d_close = mlxd_close;
226125975Sphk    sc->mlxd_disk->d_ioctl = mlxd_ioctl;
227125975Sphk    sc->mlxd_disk->d_strategy = mlxd_strategy;
228125975Sphk    sc->mlxd_disk->d_name = "mlxd";
229125975Sphk    sc->mlxd_disk->d_unit = sc->mlxd_unit;
230125975Sphk    sc->mlxd_disk->d_drv1 = sc;
231125975Sphk    sc->mlxd_disk->d_sectorsize = MLX_BLKSIZE;
232125975Sphk    sc->mlxd_disk->d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
233125975Sphk    sc->mlxd_disk->d_fwsectors = sc->mlxd_drive->ms_sectors;
234125975Sphk    sc->mlxd_disk->d_fwheads = sc->mlxd_drive->ms_heads;
235125975Sphk    sc->mlxd_disk->d_flags = DISKFLAG_NEEDSGIANT;
23651973Smsmith
23760074Smsmith    /*
23860074Smsmith     * Set maximum I/O size to the lesser of the recommended maximum and the practical
239111499Sjhb     * maximum except on v2 cards where the maximum is set to 8 pages.
24060074Smsmith     */
241111499Sjhb    if (sc->mlxd_controller->mlx_iftype == MLX_IFTYPE_2)
242138090Sscottl	sc->mlxd_disk->d_maxsize = 8 * MLX_PAGE_SIZE;
243111499Sjhb    else {
244111499Sjhb	s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
245138090Sscottl	s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * MLX_PAGE_SIZE;
246125975Sphk	sc->mlxd_disk->d_maxsize = imin(s1, s2);
247111499Sjhb    }
24852225Smsmith
249125975Sphk    disk_create(sc->mlxd_disk, DISK_VERSION);
250111469Sphk
25151973Smsmith    return (0);
25251973Smsmith}
25351973Smsmith
25451973Smsmithstatic int
25551973Smsmithmlxd_detach(device_t dev)
25651973Smsmith{
25751973Smsmith    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
25851973Smsmith
25958188Smsmith    debug_called(1);
26051973Smsmith
261125975Sphk    disk_destroy(sc->mlxd_disk);
26251973Smsmith
26351973Smsmith    return(0);
26451973Smsmith}
26551973Smsmith
266