2 * Copyright (c) 2006 IronPort Systems
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
31 * Copyright 1994-2009 The FreeBSD Project.
32 * All rights reserved.
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
40 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
42 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47 * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
48 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 * The views and conclusions contained in the software and documentation
53 * are those of the authors and should not be interpreted as representing
54 * official policies,either expressed or implied, of the FreeBSD Project.
56 * $FreeBSD: src/sys/dev/mfi/mfi_disk.c,v 1.8 2008/11/17 23:30:19 jhb Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/module.h>
65 #include <sys/malloc.h>
76 #include <machine/md_var.h>
79 #include <dev/raid/mfi/mfireg.h>
80 #include <dev/raid/mfi/mfi_ioctl.h>
81 #include <dev/raid/mfi/mfivar.h>
83 static int mfi_disk_probe(device_t dev);
84 static int mfi_disk_attach(device_t dev);
85 static int mfi_disk_detach(device_t dev);
87 static d_open_t mfi_disk_open;
88 static d_close_t mfi_disk_close;
89 static d_strategy_t mfi_disk_strategy;
90 static d_dump_t mfi_disk_dump;
92 static struct dev_ops mfi_disk_ops = {
93 { "mfid", 0, D_DISK },
94 .d_open = mfi_disk_open,
95 .d_close = mfi_disk_close,
96 .d_strategy = mfi_disk_strategy,
97 .d_dump = mfi_disk_dump,
100 static devclass_t mfi_disk_devclass;
102 static device_method_t mfi_disk_methods[] = {
103 DEVMETHOD(device_probe, mfi_disk_probe),
104 DEVMETHOD(device_attach, mfi_disk_attach),
105 DEVMETHOD(device_detach, mfi_disk_detach),
109 static driver_t mfi_disk_driver = {
112 sizeof(struct mfi_disk)
115 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, NULL, NULL);
118 mfi_disk_probe(device_t dev)
125 mfi_disk_attach(device_t dev)
128 struct mfi_ld_info *ld_info;
129 struct disk_info info;
134 sc = device_get_softc(dev);
135 ld_info = device_get_ivars(dev);
138 sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
139 sc->ld_unit = device_get_unit(dev);
140 sc->ld_info = ld_info;
141 sc->ld_controller = device_get_softc(device_get_parent(dev));
144 sectors = ld_info->size;
145 secsize = MFI_SECTOR_LEN;
146 lockmgr(&sc->ld_controller->mfi_io_lock, LK_EXCLUSIVE);
147 TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
148 lockmgr(&sc->ld_controller->mfi_io_lock, LK_RELEASE);
150 switch (ld_info->ld_config.params.state) {
151 case MFI_LD_STATE_OFFLINE:
154 case MFI_LD_STATE_PARTIALLY_DEGRADED:
155 state = "partially degraded";
157 case MFI_LD_STATE_DEGRADED:
160 case MFI_LD_STATE_OPTIMAL:
167 device_printf(dev, "%juMB (%ju sectors) RAID volume '%s' is %s\n",
168 sectors / (1024 * 1024 / secsize), sectors,
169 ld_info->ld_config.properties.name,
172 devstat_add_entry(&sc->ld_devstat, "mfid", device_get_unit(dev),
173 MFI_SECTOR_LEN, DEVSTAT_NO_ORDERED_TAGS,
174 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
175 DEVSTAT_PRIORITY_ARRAY);
177 sc->ld_dev_t = disk_create(sc->ld_unit, &sc->ld_disk, &mfi_disk_ops);
178 sc->ld_dev_t->si_drv1 = sc;
179 sc->ld_dev_t->si_iosize_max =
180 min(sc->ld_controller->mfi_max_io * secsize,
181 (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
183 bzero(&info, sizeof(info));
184 info.d_media_blksize = secsize; /* mandatory */
185 info.d_media_blocks = sectors;
187 if (info.d_media_blocks >= (1 * 1024 * 1024)) {
189 info.d_secpertrack = 63;
192 info.d_secpertrack = 32;
195 disk_setdiskinfo(&sc->ld_disk, &info);
201 mfi_disk_detach(device_t dev)
205 sc = device_get_softc(dev);
207 lockmgr(&sc->ld_controller->mfi_io_lock, LK_EXCLUSIVE);
208 if ((sc->ld_flags & MFI_DISK_FLAGS_OPEN) &&
209 (sc->ld_controller->mfi_keep_deleted_volumes ||
210 sc->ld_controller->mfi_detaching)) {
211 lockmgr(&sc->ld_controller->mfi_io_lock, LK_RELEASE);
214 lockmgr(&sc->ld_controller->mfi_io_lock, LK_RELEASE);
216 disk_destroy(&sc->ld_disk);
217 devstat_remove_entry(&sc->ld_devstat);
218 lockmgr(&sc->ld_controller->mfi_io_lock, LK_EXCLUSIVE);
219 TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
220 lockmgr(&sc->ld_controller->mfi_io_lock, LK_RELEASE);
221 kfree(sc->ld_info, M_MFIBUF);
226 mfi_disk_open(struct dev_open_args *ap)
228 struct mfi_disk *sc = ap->a_head.a_dev->si_drv1;
231 lockmgr(&sc->ld_controller->mfi_io_lock, LK_EXCLUSIVE);
232 if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
235 sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
238 lockmgr(&sc->ld_controller->mfi_io_lock, LK_RELEASE);
244 mfi_disk_close(struct dev_close_args *ap)
246 struct mfi_disk *sc = ap->a_head.a_dev->si_drv1;
248 lockmgr(&sc->ld_controller->mfi_io_lock, LK_EXCLUSIVE);
249 sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
250 lockmgr(&sc->ld_controller->mfi_io_lock, LK_RELEASE);
256 mfi_disk_disable(struct mfi_disk *sc)
259 KKASSERT(lockstatus(&sc->ld_controller->mfi_io_lock, curthread) != 0);
260 if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
261 if (sc->ld_controller->mfi_delete_busy_volumes)
263 device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
266 sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
271 mfi_disk_enable(struct mfi_disk *sc)
274 KKASSERT(lockstatus(&sc->ld_controller->mfi_io_lock, curthread) != 0);
275 sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
279 mfi_disk_strategy(struct dev_strategy_args *ap)
281 struct bio *bio = ap->a_bio;
282 struct buf *bp = bio->bio_buf;
283 struct mfi_disk *sc = ap->a_head.a_dev->si_drv1;
284 struct mfi_softc *controller;
287 bp->b_error = EINVAL;
288 bp->b_flags |= B_ERROR;
289 bp->b_resid = bp->b_bcount;
297 * If it's a null transfer, do nothing. FreeBSD's original driver
298 * doesn't have this, but that caused hard error messages (even
299 * though everything else continued to work fine). Interestingly,
300 * only when HAMMER was used.
302 * Several others of our RAID drivers have this check, such as
303 * aac(4) and ida(4), so we insert it here, too.
305 * The cause of null transfers is yet unknown.
307 if (bp->b_bcount == 0) {
308 bp->b_resid = bp->b_bcount;
313 controller = sc->ld_controller;
314 bio->bio_driver_info = sc;
315 lockmgr(&controller->mfi_io_lock, LK_EXCLUSIVE);
316 mfi_enqueue_bio(controller, bio);
317 devstat_start_transaction(&sc->ld_devstat);
318 mfi_startio(controller);
319 lockmgr(&controller->mfi_io_lock, LK_RELEASE);
324 mfi_disk_complete(struct bio *bio)
326 struct mfi_disk *sc = bio->bio_driver_info;
327 struct buf *bp = bio->bio_buf;
329 devstat_end_transaction_buf(&sc->ld_devstat, bp);
330 if (bp->b_flags & B_ERROR) {
331 if (bp->b_error == 0)
333 diskerr(bio, sc->ld_disk.d_cdev, "hard error", -1, 1);
342 mfi_disk_dump(struct dev_dump_args *ap)
344 cdev_t dev = ap->a_head.a_dev;
345 off_t offset = ap->a_offset;
346 void *virt = ap->a_virtual;
347 size_t len = ap->a_length;
349 struct mfi_softc *parent_sc;
353 parent_sc = sc->ld_controller;
356 if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
357 MFI_SECTOR_LEN, virt, len)) != 0)
360 /* mfi_sync_cache(parent_sc, sc->ld_id); */