2 * Copyright (c) 1999 Jonathan Lemon
3 * Copyright (c) 1999, 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * Copyright (c) 2002 Eric Moore
29 * Copyright (c) 2002 LSI Logic Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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
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
56 * $FreeBSD: src/sys/dev/amr/amr_disk.c,v 1.5.2.5 2002/12/20 15:12:04 emoore Exp $
57 * $DragonFly: src/sys/dev/raid/amr/amr_disk.c,v 1.15 2007/06/17 23:50:16 dillon Exp $
61 * Disk driver for AMI MegaRaid controllers
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
68 #include "amr_compat.h"
71 #include <sys/devicestat.h>
73 #include <sys/dtype.h>
79 #include <machine/md_var.h>
84 #include "amr_tables.h"
87 static int amrd_probe(device_t dev);
88 static int amrd_attach(device_t dev);
89 static int amrd_detach(device_t dev);
91 static d_open_t amrd_open;
92 static d_close_t amrd_close;
93 static d_strategy_t amrd_strategy;
94 static d_ioctl_t amrd_ioctl;
95 static d_dump_t amrd_dump;
97 #define AMRD_CDEV_MAJOR 133
99 static struct dev_ops amrd_ops = {
100 { "amrd", AMRD_CDEV_MAJOR, D_DISK },
102 .d_close = amrd_close,
104 .d_write = physwrite,
105 .d_ioctl = amrd_ioctl,
106 .d_strategy = amrd_strategy,
110 static devclass_t amrd_devclass;
112 static device_method_t amrd_methods[] = {
113 DEVMETHOD(device_probe, amrd_probe),
114 DEVMETHOD(device_attach, amrd_attach),
115 DEVMETHOD(device_detach, amrd_detach),
119 static driver_t amrd_driver = {
122 sizeof(struct amrd_softc)
125 DRIVER_MODULE(amrd, amr, amrd_driver, amrd_devclass, 0, 0);
128 amrd_open(struct dev_open_args *ap)
130 cdev_t dev = ap->a_head.a_dev;
131 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
138 /* controller not active? */
139 if (sc->amrd_controller->amr_state & AMR_STATE_SHUTDOWN)
142 bzero(&info, sizeof(info));
143 info.d_media_blksize = AMR_BLKSIZE; /* optional */
144 info.d_media_blocks = sc->amrd_drive->al_size;
146 info.d_type = DTYPE_SCSI; /* mandatory */
147 info.d_secpertrack = sc->amrd_drive->al_sectors;
148 info.d_nheads = sc->amrd_drive->al_heads;
149 info.d_ncylinders = sc->amrd_drive->al_cylinders;
150 info.d_secpercyl = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
152 disk_setdiskinfo(&sc->amrd_disk, &info);
154 sc->amrd_flags |= AMRD_OPEN;
159 amrd_close(struct dev_close_args *ap)
161 cdev_t dev = ap->a_head.a_dev;
162 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
168 sc->amrd_flags &= ~AMRD_OPEN;
173 amrd_ioctl(struct dev_ioctl_args *ap)
179 /********************************************************************************
180 * System crashdump support
183 amrd_dump(struct dev_dump_args *ap)
185 cdev_t dev = ap->a_head.a_dev;
186 struct amrd_softc *amrd_sc = (struct amrd_softc *)dev->si_drv1;
187 struct amr_softc *amr_sc;
193 amr_sc = (struct amr_softc *)amrd_sc->amrd_controller;
195 if (!amrd_sc || !amr_sc)
198 driveno = amrd_sc->amrd_drive - amr_sc->amr_drive;
200 if (ap->a_length > 0) {
201 if ((error = amr_dump_blocks(amr_sc,driveno,ap->a_offset / AMR_BLKSIZE,
202 (void *)ap->a_virtual,(int) ap->a_length / AMR_BLKSIZE )) != 0)
209 * Read/write routine for a buffer. Finds the proper unit, range checks
210 * arguments, and schedules the transfer. Does not wait for the transfer
211 * to complete. Multi-page transfers are supported. All I/O requests must
212 * be a multiple of a sector in length.
215 amrd_strategy(struct dev_strategy_args *ap)
217 cdev_t dev = ap->a_head.a_dev;
218 struct bio *bio = ap->a_bio;
219 struct buf *bp = bio->bio_buf;
220 struct amrd_softc *sc = (struct amrd_softc *)dev->si_drv1;
224 bp->b_error = EINVAL;
227 bio->bio_driver_info = sc;
229 devstat_start_transaction(&sc->amrd_stats);
230 amr_submit_bio(sc->amrd_controller, bio);
234 bp->b_flags |= B_ERROR;
237 * Correctly set the buf to indicate a completed transfer
239 bp->b_resid = bp->b_bcount;
245 amrd_intr(struct bio *bio)
247 struct buf *bp = bio->bio_buf;
251 if (bp->b_flags & B_ERROR) {
253 debug(1, "i/o error\n");
261 amrd_probe(device_t dev)
266 device_set_desc(dev, "LSILogic MegaRAID logical drive");
271 amrd_attach(device_t dev)
273 struct disk_info info;
274 struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
279 parent = device_get_parent(dev);
280 sc->amrd_controller = (struct amr_softc *)device_get_softc(parent);
281 sc->amrd_unit = device_get_unit(dev);
282 sc->amrd_drive = device_get_ivars(dev);
285 device_printf(dev, "%uMB (%u sectors) RAID %d (%s)\n",
286 sc->amrd_drive->al_size / ((1024 * 1024) / AMR_BLKSIZE),
287 sc->amrd_drive->al_size, sc->amrd_drive->al_properties & AMR_DRV_RAID_MASK,
288 amr_describe_code(amr_table_drvstate, AMR_DRV_CURSTATE(sc->amrd_drive->al_state)));
290 devstat_add_entry(&sc->amrd_stats, "amrd", sc->amrd_unit, AMR_BLKSIZE,
291 DEVSTAT_NO_ORDERED_TAGS,
292 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
293 DEVSTAT_PRIORITY_ARRAY);
295 sc->amrd_dev_t = disk_create(sc->amrd_unit, &sc->amrd_disk, &amrd_ops);
296 sc->amrd_dev_t->si_drv1 = sc;
298 /* set maximum I/O size to match the maximum s/g size */
299 sc->amrd_dev_t->si_iosize_max = (AMR_NSEG - 1) * PAGE_SIZE;
302 * Set disk info, as it appears that all needed data is available already.
303 * Setting the disk info will also cause the probing to start.
305 bzero(&info, sizeof(info));
306 info.d_media_blksize = AMR_BLKSIZE; /* optional */
307 info.d_media_blocks = sc->amrd_drive->al_size;
309 info.d_type = DTYPE_SCSI; /* mandatory */
310 info.d_secpertrack = sc->amrd_drive->al_sectors;
311 info.d_nheads = sc->amrd_drive->al_heads;
312 info.d_ncylinders = sc->amrd_drive->al_cylinders;
313 info.d_secpercyl = sc->amrd_drive->al_sectors * sc->amrd_drive->al_heads;
315 disk_setdiskinfo(&sc->amrd_disk, &info);
321 amrd_detach(device_t dev)
323 struct amrd_softc *sc = (struct amrd_softc *)device_get_softc(dev);
327 if (sc->amrd_flags & AMRD_OPEN)
330 devstat_remove_entry(&sc->amrd_stats);
331 disk_destroy(&sc->amrd_disk);