amr_disk.c revision 111441
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 111441 2003-02-24 21:49:27Z 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
87111441Sphkstatic	disk_open_t	amrd_open;
88111441Sphkstatic	disk_strategy_t	amrd_strategy;
8951974Smsmith
9051974Smsmithstatic devclass_t	amrd_devclass;
9165245Smsmith#ifdef FREEBSD_4
9251974Smsmithstatic int		disks_registered = 0;
9365245Smsmith#endif
9451974Smsmith
9551974Smsmithstatic device_method_t amrd_methods[] = {
9651974Smsmith    DEVMETHOD(device_probe,	amrd_probe),
9751974Smsmith    DEVMETHOD(device_attach,	amrd_attach),
9851974Smsmith    DEVMETHOD(device_detach,	amrd_detach),
9951974Smsmith    { 0, 0 }
10051974Smsmith};
10151974Smsmith
10251974Smsmithstatic driver_t amrd_driver = {
10351974Smsmith    "amrd",
10451974Smsmith    amrd_methods,
10551974Smsmith    sizeof(struct amrd_softc)
10651974Smsmith};
10751974Smsmith
10851974SmsmithDRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
10951974Smsmith
11051974Smsmithstatic int
111111441Sphkamrd_open(struct disk *dp)
11251974Smsmith{
113111441Sphk    struct amrd_softc	*sc = (struct amrd_softc *)dp->d_drv1;
114105419Semoore#if __FreeBSD_version < 500000		/* old buf style */
115105419Semoore    struct disklabel    *label;
116105419Semoore#endif
11751974Smsmith
11865245Smsmith    debug_called(1);
119105419Semoore
12051974Smsmith    if (sc == NULL)
12151974Smsmith	return (ENXIO);
12251974Smsmith
12351974Smsmith    /* controller not active? */
12451974Smsmith    if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
12551974Smsmith	return(ENXIO);
12651974Smsmith
127105419Semoore#if __FreeBSD_version < 500000		/* old buf style */
128105419Semoore    label = &sc->amrd_disk.d_label;
129105419Semoore    bzero(label, sizeof(*label));
130105419Semoore    label->d_type       = DTYPE_SCSI;
131105419Semoore    label->d_secsize    = AMR_BLKSIZE;
132105419Semoore    label->d_nsectors   = sc->amrd_drive->al_sectors;
133105419Semoore    label->d_ntracks    = sc->amrd_drive->al_heads;
134105419Semoore    label->d_ncylinders = sc->amrd_drive->al_cylinders;
135105419Semoore    label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
136105419Semoore    label->d_secperunit = sc->amrd_drive->al_size;
137105419Semoore#else
138103714Sphk    sc->amrd_disk.d_sectorsize = AMR_BLKSIZE;
139103714Sphk    sc->amrd_disk.d_mediasize = (off_t)sc->amrd_drive->al_size * AMR_BLKSIZE;
140103714Sphk    sc->amrd_disk.d_fwsectors = sc->amrd_drive->al_sectors;
141103714Sphk    sc->amrd_disk.d_fwheads = sc->amrd_drive->al_heads;
142105419Semoore#endif
14351974Smsmith
14451974Smsmith    return (0);
14551974Smsmith}
14651974Smsmith
14751974Smsmith/*
14851974Smsmith * Read/write routine for a buffer.  Finds the proper unit, range checks
14951974Smsmith * arguments, and schedules the transfer.  Does not wait for the transfer
15051974Smsmith * to complete.  Multi-page transfers are supported.  All I/O requests must
15151974Smsmith * be a multiple of a sector in length.
15251974Smsmith */
15351974Smsmithstatic void
15465245Smsmithamrd_strategy(struct bio *bio)
15551974Smsmith{
156111441Sphk    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_disk->d_drv1;
15751974Smsmith
15851974Smsmith    /* bogus disk? */
15951974Smsmith    if (sc == NULL) {
16065245Smsmith	bio->bio_error = EINVAL;
16151974Smsmith	goto bad;
16251974Smsmith    }
16351974Smsmith
16451974Smsmith    devstat_start_transaction(&sc->amrd_stats);
16565245Smsmith    amr_submit_bio(sc->amrd_controller, bio);
16651974Smsmith    return;
16751974Smsmith
16851974Smsmith bad:
16965245Smsmith    bio->bio_flags |= BIO_ERROR;
17051974Smsmith
17151974Smsmith    /*
17251974Smsmith     * Correctly set the buf to indicate a completed transfer
17351974Smsmith     */
17465245Smsmith    bio->bio_resid = bio->bio_bcount;
17565245Smsmith    biodone(bio);
17651974Smsmith    return;
17751974Smsmith}
17851974Smsmith
17951974Smsmithvoid
18051974Smsmithamrd_intr(void *data)
18151974Smsmith{
18265245Smsmith    struct bio *bio = (struct bio *)data;
183111441Sphk    struct amrd_softc *sc = (struct amrd_softc *)bio->bio_disk->d_drv1;
18451974Smsmith
18565245Smsmith    debug_called(2);
18658883Smsmith
18765245Smsmith    if (bio->bio_flags & BIO_ERROR) {
18865245Smsmith	bio->bio_error = EIO;
18965245Smsmith	debug(1, "i/o error\n");
19058883Smsmith    } else {
19165245Smsmith	bio->bio_resid = 0;
19258883Smsmith    }
19351974Smsmith
194105419Semoore    AMR_BIO_FINISH(bio);
19551974Smsmith}
19651974Smsmith
19751974Smsmithstatic int
19851974Smsmithamrd_probe(device_t dev)
19951974Smsmith{
20051974Smsmith
20165245Smsmith    debug_called(1);
202105419Semoore
203105419Semoore    device_set_desc(dev, "LSILogic MegaRAID logical drive");
20451974Smsmith    return (0);
20551974Smsmith}
20651974Smsmith
20751974Smsmithstatic int
20851974Smsmithamrd_attach(device_t dev)
20951974Smsmith{
21051974Smsmith    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
21151974Smsmith    device_t		parent;
21251974Smsmith
21365245Smsmith    debug_called(1);
21451974Smsmith
21551974Smsmith    parent = device_get_parent(dev);
21651974Smsmith    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
21751974Smsmith    sc->amrd_unit = device_get_unit(dev);
21851974Smsmith    sc->amrd_drive = device_get_ivars(dev);
21952274Smsmith    sc->amrd_dev = dev;
22051974Smsmith
22152784Smsmith    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
22251974Smsmith		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
22365245Smsmith		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
22465245Smsmith		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
22551974Smsmith
22651974Smsmith    devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
22751974Smsmith		      DEVSTAT_NO_ORDERED_TAGS,
22854279Sken		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
22954279Sken		      DEVSTAT_PRIORITY_ARRAY);
23051974Smsmith
231111441Sphk    sc->amrd_disk.d_drv1 = sc;
232111441Sphk    sc->amrd_disk.d_maxsize = (AMR_NSEG - 1) * PAGE_SIZE;
233111441Sphk    sc->amrd_disk.d_open = amrd_open;
234111441Sphk    sc->amrd_disk.d_strategy = amrd_strategy;
235111441Sphk    sc->amrd_disk.d_name = "amrd";
236111441Sphk    disk_create(sc->amrd_unit, &sc->amrd_disk, 0, NULL, NULL);
23765245Smsmith#ifdef FREEBSD_4
23851974Smsmith    disks_registered++;
23965245Smsmith#endif
24051974Smsmith
24151974Smsmith    return (0);
24251974Smsmith}
24351974Smsmith
24451974Smsmithstatic int
24551974Smsmithamrd_detach(device_t dev)
24651974Smsmith{
24751974Smsmith    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
24851974Smsmith
24965245Smsmith    debug_called(1);
25051974Smsmith
251111250Sphk    if (sc->amrd_disk.d_flags & DISKFLAG_OPEN)
25265245Smsmith	return(EBUSY);
25365245Smsmith
25451974Smsmith    devstat_remove_entry(&sc->amrd_stats);
25565245Smsmith#ifdef FREEBSD_4
25665245Smsmith    if (--disks_registered == 0)
25765245Smsmith	cdevsw_remove(&amrddisk_cdevsw);
25865245Smsmith#else
259111216Sphk    disk_destroy(&sc->amrd_disk);
26065245Smsmith#endif
26151974Smsmith    return(0);
26251974Smsmith}
26351974Smsmith
264