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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/selinfo.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
44 #include <geom/geom_disk.h>
49 #include <machine/md_var.h>
50 #include <machine/bus.h>
53 #include <dev/mfi/mfireg.h>
54 #include <dev/mfi/mfi_ioctl.h>
55 #include <dev/mfi/mfivar.h>
57 static int mfi_disk_probe(device_t dev);
58 static int mfi_disk_attach(device_t dev);
59 static int mfi_disk_detach(device_t dev);
61 static disk_open_t mfi_disk_open;
62 static disk_close_t mfi_disk_close;
63 static disk_strategy_t mfi_disk_strategy;
64 static dumper_t mfi_disk_dump;
66 static devclass_t mfi_disk_devclass;
68 static device_method_t mfi_disk_methods[] = {
69 DEVMETHOD(device_probe, mfi_disk_probe),
70 DEVMETHOD(device_attach, mfi_disk_attach),
71 DEVMETHOD(device_detach, mfi_disk_detach),
75 static driver_t mfi_disk_driver = {
78 sizeof(struct mfi_disk)
81 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
84 mfi_disk_probe(device_t dev)
91 mfi_disk_attach(device_t dev)
94 struct mfi_ld_info *ld_info;
99 sc = device_get_softc(dev);
100 ld_info = device_get_ivars(dev);
103 sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
104 sc->ld_unit = device_get_unit(dev);
105 sc->ld_info = ld_info;
106 sc->ld_controller = device_get_softc(device_get_parent(dev));
109 sectors = ld_info->size;
110 secsize = MFI_SECTOR_LEN;
111 mtx_lock(&sc->ld_controller->mfi_io_lock);
112 TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
113 mtx_unlock(&sc->ld_controller->mfi_io_lock);
115 switch (ld_info->ld_config.params.state) {
116 case MFI_LD_STATE_OFFLINE:
119 case MFI_LD_STATE_PARTIALLY_DEGRADED:
120 state = "partially degraded";
122 case MFI_LD_STATE_DEGRADED:
125 case MFI_LD_STATE_OPTIMAL:
132 device_printf(dev, "%juMB (%ju sectors) RAID volume '%s' is %s\n",
133 sectors / (1024 * 1024 / secsize), sectors,
134 ld_info->ld_config.properties.name,
137 sc->ld_disk = disk_alloc();
138 sc->ld_disk->d_drv1 = sc;
139 sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
140 (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
141 sc->ld_disk->d_name = "mfid";
142 sc->ld_disk->d_open = mfi_disk_open;
143 sc->ld_disk->d_close = mfi_disk_close;
144 sc->ld_disk->d_strategy = mfi_disk_strategy;
145 sc->ld_disk->d_dump = mfi_disk_dump;
146 sc->ld_disk->d_unit = sc->ld_unit;
147 sc->ld_disk->d_sectorsize = secsize;
148 sc->ld_disk->d_mediasize = sectors * secsize;
149 if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
150 sc->ld_disk->d_fwheads = 255;
151 sc->ld_disk->d_fwsectors = 63;
153 sc->ld_disk->d_fwheads = 64;
154 sc->ld_disk->d_fwsectors = 32;
156 disk_create(sc->ld_disk, DISK_VERSION);
162 mfi_disk_detach(device_t dev)
166 sc = device_get_softc(dev);
168 mtx_lock(&sc->ld_controller->mfi_io_lock);
169 if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
170 (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
171 (sc->ld_controller->mfi_keep_deleted_volumes ||
172 sc->ld_controller->mfi_detaching)) {
173 mtx_unlock(&sc->ld_controller->mfi_io_lock);
176 mtx_unlock(&sc->ld_controller->mfi_io_lock);
178 disk_destroy(sc->ld_disk);
179 mtx_lock(&sc->ld_controller->mfi_io_lock);
180 TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
181 mtx_unlock(&sc->ld_controller->mfi_io_lock);
182 free(sc->ld_info, M_MFIBUF);
187 mfi_disk_open(struct disk *dp)
193 mtx_lock(&sc->ld_controller->mfi_io_lock);
194 if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
197 sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
200 mtx_unlock(&sc->ld_controller->mfi_io_lock);
206 mfi_disk_close(struct disk *dp)
211 mtx_lock(&sc->ld_controller->mfi_io_lock);
212 sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
213 mtx_unlock(&sc->ld_controller->mfi_io_lock);
219 mfi_disk_disable(struct mfi_disk *sc)
222 mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
223 if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
224 if (sc->ld_controller->mfi_delete_busy_volumes)
226 device_printf(sc->ld_dev, "Unable to delete busy device\n");
229 sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
234 mfi_disk_enable(struct mfi_disk *sc)
237 mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
238 sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
242 mfi_disk_strategy(struct bio *bio)
245 struct mfi_softc *controller;
247 sc = bio->bio_disk->d_drv1;
250 bio->bio_error = EINVAL;
251 bio->bio_flags |= BIO_ERROR;
252 bio->bio_resid = bio->bio_bcount;
257 controller = sc->ld_controller;
258 bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
259 mtx_lock(&controller->mfi_io_lock);
260 mfi_enqueue_bio(controller, bio);
261 mfi_startio(controller);
262 mtx_unlock(&controller->mfi_io_lock);
267 mfi_disk_complete(struct bio *bio)
270 struct mfi_frame_header *hdr;
272 sc = bio->bio_disk->d_drv1;
273 hdr = bio->bio_driver1;
275 if (bio->bio_flags & BIO_ERROR) {
276 if (bio->bio_error == 0)
277 bio->bio_error = EIO;
278 disk_err(bio, "hard error", -1, 1);
286 mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
289 struct mfi_softc *parent_sc;
295 parent_sc = sc->ld_controller;
298 if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
299 MFI_SECTOR_LEN, virt, len)) != 0)
302 /* mfi_sync_cache(parent_sc, sc->ld_id); */