amr_disk.c revision 111250
151974Smsmith/*-
251974Smsmith * Copyright (c) 1999 Jonathan Lemon
365245Smsmith * Copyright (c) 1999, 2000 Michael Smith
465245Smsmith * Copyright (c) 2000 BSDi
551974Smsmith * All rights reserved.
651974Smsmith *
751974Smsmith * Redistribution and use in source and binary forms, with or without
851974Smsmith * modification, are permitted provided that the following conditions
951974Smsmith * are met:
1051974Smsmith * 1. Redistributions of source code must retain the above copyright
1151974Smsmith *    notice, this list of conditions and the following disclaimer.
1251974Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1351974Smsmith *    notice, this list of conditions and the following disclaimer in the
1451974Smsmith *    documentation and/or other materials provided with the distribution.
1551974Smsmith *
1651974Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1751974Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1851974Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1951974Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2051974Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2151974Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2251974Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2351974Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2451974Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2551974Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2651974Smsmith * SUCH DAMAGE.
2751974Smsmith *
28106225Semoore * Copyright (c) 2002 Eric Moore
29106225Semoore * Copyright (c) 2002 LSI Logic Corporation
30106225Semoore * All rights reserved.
31106225Semoore *
32106225Semoore * Redistribution and use in source and binary forms, with or without
33106225Semoore * modification, are permitted provided that the following conditions
34106225Semoore * are met:
35106225Semoore * 1. Redistributions of source code must retain the above copyright
36106225Semoore *    notice, this list of conditions and the following disclaimer.
37106225Semoore * 2. Redistributions in binary form must reproduce the above copyright
38106225Semoore *    notice, this list of conditions and the following disclaimer in the
39106225Semoore *    documentation and/or other materials provided with the distribution.
40105419Semoore * 3. The party using or redistributing the source code and binary forms
41106225Semoore *    agrees to the disclaimer below and the terms and conditions set forth
42105419Semoore *    herein.
43105419Semoore *
44106225Semoore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45106225Semoore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46106225Semoore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47106225Semoore * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48106225Semoore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49106225Semoore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50106225Semoore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51106225Semoore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52106225Semoore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53106225Semoore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54106225Semoore * SUCH DAMAGE.
55105419Semoore *
56106225Semoore *
5751974Smsmith * $FreeBSD: head/sys/dev/amr/amr_disk.c 111250 2003-02-22 10:10:12Z phk $
5851974Smsmith */
5951974Smsmith
6051974Smsmith/*
6151974Smsmith * Disk driver for AMI MegaRaid controllers
6251974Smsmith */
6351974Smsmith
6451974Smsmith#include <sys/param.h>
6551974Smsmith#include <sys/systm.h>
6651974Smsmith#include <sys/kernel.h>
6751974Smsmith
6865245Smsmith#include <dev/amr/amr_compat.h>
6951974Smsmith#include <sys/bus.h>
7051974Smsmith#include <sys/conf.h>
7151974Smsmith#include <sys/devicestat.h>
7251974Smsmith#include <sys/disk.h>
7351974Smsmith
7451974Smsmith#include <machine/bus.h>
7551974Smsmith#include <sys/rman.h>
7651974Smsmith
7751974Smsmith#include <dev/amr/amrio.h>
7851974Smsmith#include <dev/amr/amrreg.h>
7951974Smsmith#include <dev/amr/amrvar.h>
8065245Smsmith#include <dev/amr/amr_tables.h>
8151974Smsmith
8251974Smsmith/* prototypes */
8351974Smsmithstatic int amrd_probe(device_t dev);
8451974Smsmithstatic int amrd_attach(device_t dev);
8551974Smsmithstatic int amrd_detach(device_t dev);
8651974Smsmith
8751974Smsmithstatic	d_open_t	amrd_open;
8851974Smsmithstatic	d_strategy_t	amrd_strategy;
8951974Smsmith
9051974Smsmith#define AMRD_CDEV_MAJOR	133
9151974Smsmith
9251974Smsmithstatic struct cdevsw amrd_cdevsw = {
9351974Smsmith		/* open */	amrd_open,
94111250Sphk		/* close */	noclose,
9551974Smsmith		/* read */	physread,
9651974Smsmith		/* write */	physwrite,
97111249Sphk		/* ioctl */	noioctl,
9851974Smsmith		/* poll */	nopoll,
9951974Smsmith		/* mmap */	nommap,
10051974Smsmith		/* strategy */	amrd_strategy,
10151974Smsmith		/* name */ 	"amrd",
10251974Smsmith		/* maj */	AMRD_CDEV_MAJOR,
10351974Smsmith		/* dump */	nodump,
10451974Smsmith		/* psize */ 	nopsize,
10551974Smsmith		/* flags */	D_DISK,
106107756Semoore#if __FreeBSD_version < 500000
107107756Semoore		/* bmaj */	-1
108107756Semoore#endif
10951974Smsmith};
11051974Smsmith
11151974Smsmithstatic devclass_t	amrd_devclass;
11251974Smsmithstatic struct cdevsw	amrddisk_cdevsw;
11365245Smsmith#ifdef FREEBSD_4
11451974Smsmithstatic int		disks_registered = 0;
11565245Smsmith#endif
11651974Smsmith
11751974Smsmithstatic device_method_t amrd_methods[] = {
11851974Smsmith    DEVMETHOD(device_probe,	amrd_probe),
11951974Smsmith    DEVMETHOD(device_attach,	amrd_attach),
12051974Smsmith    DEVMETHOD(device_detach,	amrd_detach),
12151974Smsmith    { 0, 0 }
12251974Smsmith};
12351974Smsmith
12451974Smsmithstatic driver_t amrd_driver = {
12551974Smsmith    "amrd",
12651974Smsmith    amrd_methods,
12751974Smsmith    sizeof(struct amrd_softc)
12851974Smsmith};
12951974Smsmith
13051974SmsmithDRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
13151974Smsmith
13251974Smsmithstatic int
133105419Semooreamrd_open(dev_t dev, int flags, int fmt, d_thread_t *td)
13451974Smsmith{
13558883Smsmith    struct amrd_softc	*sc = (struct amrd_softc *)dev->si_drv1;
136105419Semoore#if __FreeBSD_version < 500000		/* old buf style */
137105419Semoore    struct disklabel    *label;
138105419Semoore#endif
13951974Smsmith
14065245Smsmith    debug_called(1);
141105419Semoore
14251974Smsmith    if (sc == NULL)
14351974Smsmith	return (ENXIO);
14451974Smsmith
14551974Smsmith    /* controller not active? */
14651974Smsmith    if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
14751974Smsmith	return(ENXIO);
14851974Smsmith
149105419Semoore#if __FreeBSD_version < 500000		/* old buf style */
150105419Semoore    label = &sc->amrd_disk.d_label;
151105419Semoore    bzero(label, sizeof(*label));
152105419Semoore    label->d_type       = DTYPE_SCSI;
153105419Semoore    label->d_secsize    = AMR_BLKSIZE;
154105419Semoore    label->d_nsectors   = sc->amrd_drive->al_sectors;
155105419Semoore    label->d_ntracks    = sc->amrd_drive->al_heads;
156105419Semoore    label->d_ncylinders = sc->amrd_drive->al_cylinders;
157105419Semoore    label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
158105419Semoore    label->d_secperunit = sc->amrd_drive->al_size;
159105419Semoore#else
160103714Sphk    sc->amrd_disk.d_sectorsize = AMR_BLKSIZE;
161103714Sphk    sc->amrd_disk.d_mediasize = (off_t)sc->amrd_drive->al_size * AMR_BLKSIZE;
162103714Sphk    sc->amrd_disk.d_fwsectors = sc->amrd_drive->al_sectors;
163103714Sphk    sc->amrd_disk.d_fwheads = sc->amrd_drive->al_heads;
164105419Semoore#endif
16551974Smsmith
16651974Smsmith    return (0);
16751974Smsmith}
16851974Smsmith
16951974Smsmith/*
17051974Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
17151974Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
17251974Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
17351974Smsmith * be a multiple of a sector in length.
17451974Smsmith */
17551974Smsmithstatic void
17665245Smsmithamrd_strategy(struct bio *bio)
17751974Smsmith{
17865245Smsmith    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_dev->si_drv1;
17951974Smsmith
18051974Smsmith    /* bogus disk? */
18151974Smsmith    if (sc == NULL) {
18265245Smsmith	bio->bio_error = EINVAL;
18351974Smsmith	goto bad;
18451974Smsmith    }
18551974Smsmith
18651974Smsmith    devstat_start_transaction(&sc->amrd_stats);
18765245Smsmith    amr_submit_bio(sc->amrd_controller, bio);
18851974Smsmith    return;
18951974Smsmith
19051974Smsmith bad:
19165245Smsmith    bio->bio_flags |= BIO_ERROR;
19251974Smsmith
19351974Smsmith    /*
19451974Smsmith     * Correctly set the buf to indicate a completed transfer
19551974Smsmith     */
19665245Smsmith    bio->bio_resid = bio->bio_bcount;
19765245Smsmith    biodone(bio);
19851974Smsmith    return;
19951974Smsmith}
20051974Smsmith
20151974Smsmithvoid
20251974Smsmithamrd_intr(void *data)
20351974Smsmith{
20465245Smsmith    struct bio *bio = (struct bio *)data;
20565245Smsmith    struct amrd_softc *sc = (struct amrd_softc *)bio->bio_dev->si_drv1;
20651974Smsmith
20765245Smsmith    debug_called(2);
20858883Smsmith
20965245Smsmith    if (bio->bio_flags & BIO_ERROR) {
21065245Smsmith	bio->bio_error = EIO;
21165245Smsmith	debug(1, "i/o error\n");
21258883Smsmith    } else {
21365245Smsmith	bio->bio_resid = 0;
21458883Smsmith    }
21551974Smsmith
216105419Semoore    AMR_BIO_FINISH(bio);
21751974Smsmith}
21851974Smsmith
21951974Smsmithstatic int
22051974Smsmithamrd_probe(device_t dev)
22151974Smsmith{
22251974Smsmith
22365245Smsmith    debug_called(1);
224105419Semoore
225105419Semoore    device_set_desc(dev, "LSILogic MegaRAID logical drive");
22651974Smsmith    return (0);
22751974Smsmith}
22851974Smsmith
22951974Smsmithstatic int
23051974Smsmithamrd_attach(device_t dev)
23151974Smsmith{
23251974Smsmith    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
23351974Smsmith    device_t		parent;
23451974Smsmith
23565245Smsmith    debug_called(1);
23651974Smsmith
23751974Smsmith    parent = device_get_parent(dev);
23851974Smsmith    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
23951974Smsmith    sc->amrd_unit = device_get_unit(dev);
24051974Smsmith    sc->amrd_drive = device_get_ivars(dev);
24152274Smsmith    sc->amrd_dev = dev;
24251974Smsmith
24352784Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
24451974Smsmith		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
24565245Smsmith		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
24665245Smsmith		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
24751974Smsmith
24851974Smsmith    devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
24951974Smsmith		      DEVSTAT_NO_ORDERED_TAGS,
25054279Sken		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
25154279Sken		      DEVSTAT_PRIORITY_ARRAY);
25251974Smsmith
25358883Smsmith    sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, 0, &amrd_cdevsw, &amrddisk_cdevsw);
25458883Smsmith    sc->amrd_dev_t->si_drv1 = sc;
25565245Smsmith#ifdef FREEBSD_4
25651974Smsmith    disks_registered++;
25765245Smsmith#endif
25851974Smsmith
25965245Smsmith    /* set maximum I/O size to match the maximum s/g size */
26065245Smsmith    sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
26158883Smsmith
26251974Smsmith    return (0);
26351974Smsmith}
26451974Smsmith
26551974Smsmithstatic int
26651974Smsmithamrd_detach(device_t dev)
26751974Smsmith{
26851974Smsmith    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
26951974Smsmith
27065245Smsmith    debug_called(1);
27151974Smsmith
272111250Sphk    if (sc->amrd_disk.d_flags & DISKFLAG_OPEN)
27365245Smsmith	return(EBUSY);
27465245Smsmith
27551974Smsmith    devstat_remove_entry(&sc->amrd_stats);
27665245Smsmith#ifdef FREEBSD_4
27765245Smsmith    if (--disks_registered == 0)
27865245Smsmith	cdevsw_remove(&amrddisk_cdevsw);
27965245Smsmith#else
280111216Sphk    disk_destroy(&sc->amrd_disk);
28165245Smsmith#endif
28251974Smsmith    return(0);
28351974Smsmith}
28451974Smsmith
285