amr_disk.c revision 139749
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/*-
29 * Copyright (c) 2002 Eric Moore
30 * Copyright (c) 2002 LSI Logic Corporation
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 *    notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 * 3. The party using or redistributing the source code and binary forms
42 *    agrees to the disclaimer below and the terms and conditions set forth
43 *    herein.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58#include <sys/cdefs.h>
59__FBSDID("$FreeBSD: head/sys/dev/amr/amr_disk.c 139749 2005-01-06 01:43:34Z imp $");
60
61/*
62 * Disk driver for AMI MegaRaid controllers
63 */
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/module.h>
69
70#include <dev/amr/amr_compat.h>
71#include <sys/bus.h>
72#include <sys/conf.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 * System crashdump support
148 */
149
150static int
151amrd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
152{
153
154    struct amrd_softc	*amrd_sc;
155    struct amr_softc	*amr_sc;
156    int			error;
157    struct disk		*dp;
158
159    dp = arg;
160    amrd_sc = (struct amrd_softc *)dp->d_drv1;
161    if (amrd_sc == NULL)
162	return(ENXIO);
163    amr_sc  = (struct amr_softc *)amrd_sc->amrd_controller;
164
165    if (length > 0) {
166	int	driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
167	if ((error = amr_dump_blocks(amr_sc,driveno,offset / AMR_BLKSIZE ,(void *)virtual,(int) length / AMR_BLKSIZE  )) != 0)
168	    	return(error);
169
170    }
171    return(0);
172}
173
174/*
175 * Read/write routine for a buffer.  Finds the proper unit, range checks
176 * arguments, and schedules the transfer.  Does not wait for the transfer
177 * to complete.  Multi-page transfers are supported.  All I/O requests must
178 * be a multiple of a sector in length.
179 */
180static void
181amrd_strategy(struct bio *bio)
182{
183    struct amrd_softc	*sc = (struct amrd_softc *)bio->bio_disk->d_drv1;
184
185    /* bogus disk? */
186    if (sc == NULL) {
187	bio->bio_error = EINVAL;
188	goto bad;
189    }
190
191    amr_submit_bio(sc->amrd_controller, bio);
192    return;
193
194 bad:
195    bio->bio_flags |= BIO_ERROR;
196
197    /*
198     * Correctly set the buf to indicate a completed transfer
199     */
200    bio->bio_resid = bio->bio_bcount;
201    biodone(bio);
202    return;
203}
204
205void
206amrd_intr(void *data)
207{
208    struct bio *bio = (struct bio *)data;
209
210    debug_called(2);
211
212    if (bio->bio_flags & BIO_ERROR) {
213	bio->bio_error = EIO;
214	debug(1, "i/o error\n");
215    } else {
216	bio->bio_resid = 0;
217    }
218
219    AMR_BIO_FINISH(bio);
220}
221
222static int
223amrd_probe(device_t dev)
224{
225
226    debug_called(1);
227
228    device_set_desc(dev, "LSILogic MegaRAID logical drive");
229    return (0);
230}
231
232static int
233amrd_attach(device_t dev)
234{
235    struct amrd_softc	*sc = (struct amrd_softc *)device_get_softc(dev);
236    device_t		parent;
237
238    debug_called(1);
239
240    parent = device_get_parent(dev);
241    sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
242    sc->amrd_unit = device_get_unit(dev);
243    sc->amrd_drive = device_get_ivars(dev);
244    sc->amrd_dev = dev;
245
246    device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
247		  sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
248		  sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
249		  amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
250
251    sc->amrd_disk = disk_alloc();
252    sc->amrd_disk->d_drv1 = sc;
253    sc->amrd_disk->d_maxsize = (AMR_NSEG - 1) * PAGE_SIZE;
254    sc->amrd_disk->d_open = amrd_open;
255    sc->amrd_disk->d_strategy = amrd_strategy;
256    sc->amrd_disk->d_name = "amrd";
257    sc->amrd_disk->d_dump = (dumper_t *)amrd_dump;
258    sc->amrd_disk->d_unit = sc->amrd_unit;
259    sc->amrd_disk->d_flags = DISKFLAG_NEEDSGIANT;
260    disk_create(sc->amrd_disk, DISK_VERSION);
261#ifdef FREEBSD_4
262    disks_registered++;
263#endif
264
265    return (0);
266}
267
268static int
269amrd_detach(device_t dev)
270{
271    struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
272
273    debug_called(1);
274
275    if (sc->amrd_disk->d_flags & DISKFLAG_OPEN)
276	return(EBUSY);
277
278#ifdef FREEBSD_4
279    if (--disks_registered == 0)
280	cdevsw_remove(&amrddisk_cdevsw);
281#else
282    disk_destroy(sc->amrd_disk);
283#endif
284    return(0);
285}
286
287