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: releng/10.2/sys/dev/mlx/mlx_disk.c 240963 2012-09-26 14:17:14Z jhb $");
31119418Sobrien
3251973Smsmith/*
3351973Smsmith * Disk driver for Mylex DAC960 RAID adapters.
3451973Smsmith */
3551973Smsmith
3651973Smsmith#include <sys/param.h>
3751973Smsmith#include <sys/systm.h>
38240963Sjhb#include <sys/bio.h>
3951973Smsmith#include <sys/kernel.h>
40240608Sjhb#include <sys/lock.h>
41129879Sphk#include <sys/module.h>
42240608Sjhb#include <sys/sx.h>
4351973Smsmith
4451973Smsmith#include <sys/bus.h>
4551973Smsmith#include <sys/conf.h>
4651973Smsmith
4751973Smsmith#include <machine/bus.h>
4851973Smsmith#include <sys/rman.h>
4951973Smsmith
50112946Sphk#include <geom/geom_disk.h>
51112946Sphk
5251973Smsmith#include <dev/mlx/mlxio.h>
5351973Smsmith#include <dev/mlx/mlxvar.h>
5452544Smsmith#include <dev/mlx/mlxreg.h>
5551973Smsmith
5651973Smsmith/* prototypes */
5751973Smsmithstatic int mlxd_probe(device_t dev);
5851973Smsmithstatic int mlxd_attach(device_t dev);
5951973Smsmithstatic int mlxd_detach(device_t dev);
6051973Smsmith
6159136Smsmithdevclass_t		mlxd_devclass;
6251973Smsmith
6351973Smsmithstatic device_method_t mlxd_methods[] = {
6451973Smsmith    DEVMETHOD(device_probe,	mlxd_probe),
6551973Smsmith    DEVMETHOD(device_attach,	mlxd_attach),
6651973Smsmith    DEVMETHOD(device_detach,	mlxd_detach),
6751973Smsmith    { 0, 0 }
6851973Smsmith};
6951973Smsmith
7051973Smsmithstatic driver_t mlxd_driver = {
7151973Smsmith    "mlxd",
7251973Smsmith    mlxd_methods,
7351973Smsmith    sizeof(struct mlxd_softc)
7451973Smsmith};
7551973Smsmith
7651973SmsmithDRIVER_MODULE(mlxd, mlx, mlxd_driver, mlxd_devclass, 0, 0);
7751973Smsmith
7851973Smsmithstatic int
79111469Sphkmlxd_open(struct disk *dp)
8051973Smsmith{
81111469Sphk    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
8251973Smsmith
8358188Smsmith    debug_called(1);
8451973Smsmith
8551973Smsmith    if (sc == NULL)
8651973Smsmith	return (ENXIO);
8751973Smsmith
8851973Smsmith    /* controller not active? */
89240608Sjhb    MLX_CONFIG_LOCK(sc->mlxd_controller);
90240608Sjhb    MLX_IO_LOCK(sc->mlxd_controller);
91240608Sjhb    if (sc->mlxd_controller->mlx_state & MLX_STATE_SHUTDOWN) {
92240608Sjhb	MLX_IO_UNLOCK(sc->mlxd_controller);
93240608Sjhb	MLX_CONFIG_UNLOCK(sc->mlxd_controller);
9451973Smsmith	return(ENXIO);
95240608Sjhb    }
9651973Smsmith
9751973Smsmith    sc->mlxd_flags |= MLXD_OPEN;
98240608Sjhb    MLX_IO_UNLOCK(sc->mlxd_controller);
99240608Sjhb    MLX_CONFIG_UNLOCK(sc->mlxd_controller);
10051973Smsmith    return (0);
10151973Smsmith}
10251973Smsmith
10351973Smsmithstatic int
104111469Sphkmlxd_close(struct disk *dp)
10551973Smsmith{
106111469Sphk    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
10751973Smsmith
10858188Smsmith    debug_called(1);
109240608Sjhb
11051973Smsmith    if (sc == NULL)
11151973Smsmith	return (ENXIO);
112240608Sjhb    MLX_CONFIG_LOCK(sc->mlxd_controller);
113240608Sjhb    MLX_IO_LOCK(sc->mlxd_controller);
11451973Smsmith    sc->mlxd_flags &= ~MLXD_OPEN;
115240608Sjhb    MLX_IO_UNLOCK(sc->mlxd_controller);
116240608Sjhb    MLX_CONFIG_UNLOCK(sc->mlxd_controller);
11751973Smsmith    return (0);
11851973Smsmith}
11951973Smsmith
12051973Smsmithstatic int
121111469Sphkmlxd_ioctl(struct disk *dp, u_long cmd, void *addr, int flag, struct thread *td)
12251973Smsmith{
123111469Sphk    struct mlxd_softc	*sc = (struct mlxd_softc *)dp->d_drv1;
12451973Smsmith    int error;
12551973Smsmith
12658188Smsmith    debug_called(1);
12751973Smsmith
12851973Smsmith    if (sc == NULL)
12951973Smsmith	return (ENXIO);
13051973Smsmith
13183366Sjulian    if ((error = mlx_submit_ioctl(sc->mlxd_controller, sc->mlxd_drive, cmd, addr, flag, td)) != ENOIOCTL) {
13258188Smsmith	debug(0, "mlx_submit_ioctl returned %d\n", error);
13351973Smsmith	return(error);
13451973Smsmith    }
13551973Smsmith    return (ENOTTY);
13651973Smsmith}
13751973Smsmith
13851973Smsmith/*
13951973Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
14051973Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
14151973Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
14251973Smsmith * be a multiple of a sector in length.
14351973Smsmith */
14451973Smsmithstatic void
145240963Sjhbmlxd_strategy(struct bio *bp)
14651973Smsmith{
147240963Sjhb    struct mlxd_softc	*sc = bp->bio_disk->d_drv1;
14851973Smsmith
14958188Smsmith    debug_called(1);
15051973Smsmith
15151973Smsmith    /* bogus disk? */
15251973Smsmith    if (sc == NULL) {
153240963Sjhb	bp->bio_error = EINVAL;
154240963Sjhb	bp->bio_flags |= BIO_ERROR;
15551973Smsmith	goto bad;
15651973Smsmith    }
15751973Smsmith
15851973Smsmith    /* XXX may only be temporarily offline - sleep? */
159240608Sjhb    MLX_IO_LOCK(sc->mlxd_controller);
16051973Smsmith    if (sc->mlxd_drive->ms_state == MLX_SYSD_OFFLINE) {
161240608Sjhb	MLX_IO_UNLOCK(sc->mlxd_controller);
162240963Sjhb	bp->bio_error = ENXIO;
163240963Sjhb	bp->bio_flags |= BIO_ERROR;
16451973Smsmith	goto bad;
16551973Smsmith    }
16651973Smsmith
16751973Smsmith    mlx_submit_buf(sc->mlxd_controller, bp);
168240608Sjhb    MLX_IO_UNLOCK(sc->mlxd_controller);
16951973Smsmith    return;
17051973Smsmith
17151973Smsmith bad:
17251973Smsmith    /*
17378752Smsmith     * Correctly set the bio to indicate a failed tranfer.
17451973Smsmith     */
175240963Sjhb    bp->bio_resid = bp->bio_bcount;
176240963Sjhb    biodone(bp);
17751973Smsmith    return;
17851973Smsmith}
17951973Smsmith
18051973Smsmithvoid
181240963Sjhbmlxd_intr(struct bio *bp)
18251973Smsmith{
18351973Smsmith
18458188Smsmith    debug_called(1);
18551973Smsmith
186240963Sjhb    if (bp->bio_flags & BIO_ERROR)
187240963Sjhb	bp->bio_error = EIO;
18851973Smsmith    else
189240963Sjhb	bp->bio_resid = 0;
19051973Smsmith
191240963Sjhb    biodone(bp);
19251973Smsmith}
19351973Smsmith
19451973Smsmithstatic int
19551973Smsmithmlxd_probe(device_t dev)
19651973Smsmith{
19751973Smsmith
19858188Smsmith    debug_called(1);
19951973Smsmith
20051973Smsmith    device_set_desc(dev, "Mylex System Drive");
20151973Smsmith    return (0);
20251973Smsmith}
20351973Smsmith
20451973Smsmithstatic int
20551973Smsmithmlxd_attach(device_t dev)
20651973Smsmith{
20751973Smsmith    struct mlxd_softc	*sc = (struct mlxd_softc *)device_get_softc(dev);
20851973Smsmith    device_t		parent;
20951973Smsmith    char		*state;
21060074Smsmith    int			s1, s2;
21151973Smsmith
21258188Smsmith    debug_called(1);
21351973Smsmith
21451973Smsmith    parent = device_get_parent(dev);
21551973Smsmith    sc->mlxd_controller = (struct mlx_softc *)device_get_softc(parent);
21651973Smsmith    sc->mlxd_unit = device_get_unit(dev);
21751973Smsmith    sc->mlxd_drive = device_get_ivars(dev);
21852273Smsmith    sc->mlxd_dev = dev;
21951973Smsmith
22051973Smsmith    switch(sc->mlxd_drive->ms_state) {
22151973Smsmith    case MLX_SYSD_ONLINE:
22251973Smsmith	state = "online";
22351973Smsmith	break;
22451973Smsmith    case MLX_SYSD_CRITICAL:
22551973Smsmith	state = "critical";
22651973Smsmith	break;
22751973Smsmith    case MLX_SYSD_OFFLINE:
22851973Smsmith	state = "offline";
22951973Smsmith	break;
23051973Smsmith    default:
23151973Smsmith	state = "unknown state";
23251973Smsmith    }
23351973Smsmith
23452785Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
23551973Smsmith		  sc->mlxd_drive->ms_size / ((1024 * 1024) / MLX_BLKSIZE),
23651973Smsmith		  sc->mlxd_drive->ms_size, sc->mlxd_drive->ms_raidlevel, state);
23751973Smsmith
238125975Sphk    sc->mlxd_disk = disk_alloc();
239125975Sphk    sc->mlxd_disk->d_open = mlxd_open;
240125975Sphk    sc->mlxd_disk->d_close = mlxd_close;
241125975Sphk    sc->mlxd_disk->d_ioctl = mlxd_ioctl;
242125975Sphk    sc->mlxd_disk->d_strategy = mlxd_strategy;
243125975Sphk    sc->mlxd_disk->d_name = "mlxd";
244125975Sphk    sc->mlxd_disk->d_unit = sc->mlxd_unit;
245125975Sphk    sc->mlxd_disk->d_drv1 = sc;
246125975Sphk    sc->mlxd_disk->d_sectorsize = MLX_BLKSIZE;
247125975Sphk    sc->mlxd_disk->d_mediasize = MLX_BLKSIZE * (off_t)sc->mlxd_drive->ms_size;
248125975Sphk    sc->mlxd_disk->d_fwsectors = sc->mlxd_drive->ms_sectors;
249125975Sphk    sc->mlxd_disk->d_fwheads = sc->mlxd_drive->ms_heads;
25051973Smsmith
25160074Smsmith    /*
25260074Smsmith     * Set maximum I/O size to the lesser of the recommended maximum and the practical
253111499Sjhb     * maximum except on v2 cards where the maximum is set to 8 pages.
25460074Smsmith     */
255111499Sjhb    if (sc->mlxd_controller->mlx_iftype == MLX_IFTYPE_2)
256138090Sscottl	sc->mlxd_disk->d_maxsize = 8 * MLX_PAGE_SIZE;
257111499Sjhb    else {
258111499Sjhb	s1 = sc->mlxd_controller->mlx_enq2->me_maxblk * MLX_BLKSIZE;
259138090Sscottl	s2 = (sc->mlxd_controller->mlx_enq2->me_max_sg - 1) * MLX_PAGE_SIZE;
260125975Sphk	sc->mlxd_disk->d_maxsize = imin(s1, s2);
261111499Sjhb    }
26252225Smsmith
263125975Sphk    disk_create(sc->mlxd_disk, DISK_VERSION);
264111469Sphk
26551973Smsmith    return (0);
26651973Smsmith}
26751973Smsmith
26851973Smsmithstatic int
26951973Smsmithmlxd_detach(device_t dev)
27051973Smsmith{
27151973Smsmith    struct mlxd_softc *sc = (struct mlxd_softc *)device_get_softc(dev);
27251973Smsmith
27358188Smsmith    debug_called(1);
27451973Smsmith
275125975Sphk    disk_destroy(sc->mlxd_disk);
27651973Smsmith
27751973Smsmith    return(0);
27851973Smsmith}
27951973Smsmith
280