amr_disk.c revision 111441
1/*-
2 * Copyright (c) 1999 Jonathan Lemon
3 * Copyright (c) 1999, 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * Copyright (c) 2002 Eric Moore
29 * Copyright (c) 2002 LSI Logic Corporation
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 *    notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 * 3. The party using or redistributing the source code and binary forms
41 *    agrees to the disclaimer below and the terms and conditions set forth
42 *    herein.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 *
57 * $FreeBSD: head/sys/dev/amr/amr_disk.c 111441 2003-02-24 21:49:27Z phk $
58 */
59
60/*
61 * Disk driver for AMI MegaRaid controllers
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/kernel.h>
67
68#include <dev/amr/amr_compat.h>
69#include <sys/bus.h>
70#include <sys/conf.h>
71#include <sys/devicestat.h>
72#include <sys/disk.h>
73
74#include <machine/bus.h>
75#include <sys/rman.h>
76
77#include <dev/amr/amrio.h>
78#include <dev/amr/amrreg.h>
79#include <dev/amr/amrvar.h>
80#include <dev/amr/amr_tables.h>
81
82/* prototypes */
83static int amrd_probe(device_t dev);
84static int amrd_attach(device_t dev);
85static int amrd_detach(device_t dev);
86
87static	disk_open_t	amrd_open;
88static	disk_strategy_t	amrd_strategy;
89
90static devclass_t	amrd_devclass;
91#ifdef FREEBSD_4
92static int		disks_registered = 0;
93#endif
94
95static device_method_t amrd_methods[] = {
96    DEVMETHOD(device_probe,	amrd_probe),
97    DEVMETHOD(device_attach,	amrd_attach),
98    DEVMETHOD(device_detach,	amrd_detach),
99    { 0, 0 }
100};
101
102static driver_t amrd_driver = {
103    "amrd",
104    amrd_methods,
105    sizeof(struct amrd_softc)
106};
107
108DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
109
110static int
111amrd_open(struct disk *dp)
112{
113    struct amrd_softc	*sc = (struct amrd_softc *)dp->d_drv1;
114#if __FreeBSD_version < 500000		/* old buf style */
115    struct disklabel    *label;
116#endif
117
118    debug_called(1);
119
120    if (sc == NULL)
121	return (ENXIO);
122
123    /* controller not active? */
124    if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
125	return(ENXIO);
126
127#if __FreeBSD_version < 500000		/* old buf style */
128    label = &sc->amrd_disk.d_label;
129    bzero(label, sizeof(*label));
130    label->d_type       = DTYPE_SCSI;
131    label->d_secsize    = AMR_BLKSIZE;
132    label->d_nsectors   = sc->amrd_drive->al_sectors;
133    label->d_ntracks    = sc->amrd_drive->al_heads;
134    label->d_ncylinders = sc->amrd_drive->al_cylinders;
135    label->d_secpercyl  = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
136    label->d_secperunit = sc->amrd_drive->al_size;
137#else
138    sc->amrd_disk.d_sectorsize = AMR_BLKSIZE;
139    sc->amrd_disk.d_mediasize = (off_t)sc->amrd_drive->al_size * AMR_BLKSIZE;
140    sc->amrd_disk.d_fwsectors = sc->amrd_drive->al_sectors;
141    sc->amrd_disk.d_fwheads = sc->amrd_drive->al_heads;
142#endif
143
144    return (0);
145}
146
147/*
148 * Read/write routine for a buffer.  Finds the proper unit, range checks
149 * arguments, and schedules the transfer.  Does not wait for the transfer
150 * to complete.  Multi-page transfers are supported.  All I/O requests must
151 * be a multiple of a sector in length.
152 */
153static void
154amrd_strategy(struct bio *bio)
155{
156    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_disk->d_drv1;
157
158    /* bogus disk? */
159    if (sc == NULL) {
160	bio->bio_error = EINVAL;
161	goto bad;
162    }
163
164    devstat_start_transaction(&sc->amrd_stats);
165    amr_submit_bio(sc->amrd_controller, bio);
166    return;
167
168 bad:
169    bio->bio_flags |= BIO_ERROR;
170
171    /*
172     * Correctly set the buf to indicate a completed transfer
173     */
174    bio->bio_resid = bio->bio_bcount;
175    biodone(bio);
176    return;
177}
178
179void
180amrd_intr(void *data)
181{
182    struct bio *bio = (struct bio *)data;
183    struct amrd_softc *sc = (struct amrd_softc *)bio->bio_disk->d_drv1;
184
185    debug_called(2);
186
187    if (bio->bio_flags & BIO_ERROR) {
188	bio->bio_error = EIO;
189	debug(1, "i/o error\n");
190    } else {
191	bio->bio_resid = 0;
192    }
193
194    AMR_BIO_FINISH(bio);
195}
196
197static int
198amrd_probe(device_t dev)
199{
200
201    debug_called(1);
202
203    device_set_desc(dev, "LSILogic MegaRAID logical drive");
204    return (0);
205}
206
207static int
208amrd_attach(device_t dev)
209{
210    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
211    device_t		parent;
212
213    debug_called(1);
214
215    parent = device_get_parent(dev);
216    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
217    sc->amrd_unit = device_get_unit(dev);
218    sc->amrd_drive = device_get_ivars(dev);
219    sc->amrd_dev = dev;
220
221    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
222		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
223		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
224		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
225
226    devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
227		      DEVSTAT_NO_ORDERED_TAGS,
228		      DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
229		      DEVSTAT_PRIORITY_ARRAY);
230
231    sc->amrd_disk.d_drv1 = sc;
232    sc->amrd_disk.d_maxsize = (AMR_NSEG - 1) * PAGE_SIZE;
233    sc->amrd_disk.d_open = amrd_open;
234    sc->amrd_disk.d_strategy = amrd_strategy;
235    sc->amrd_disk.d_name = "amrd";
236    disk_create(sc->amrd_unit, &sc->amrd_disk, 0, NULL, NULL);
237#ifdef FREEBSD_4
238    disks_registered++;
239#endif
240
241    return (0);
242}
243
244static int
245amrd_detach(device_t dev)
246{
247    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
248
249    debug_called(1);
250
251    if (sc->amrd_disk.d_flags & DISKFLAG_OPEN)
252	return(EBUSY);
253
254    devstat_remove_entry(&sc->amrd_stats);
255#ifdef FREEBSD_4
256    if (--disks_registered == 0)
257	cdevsw_remove(&amrddisk_cdevsw);
258#else
259    disk_destroy(&sc->amrd_disk);
260#endif
261    return(0);
262}
263
264