2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 IronPort Systems
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/selinfo.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
47 #include <geom/geom_disk.h>
52 #include <machine/md_var.h>
53 #include <machine/bus.h>
56 #include <dev/mfi/mfireg.h>
57 #include <dev/mfi/mfi_ioctl.h>
58 #include <dev/mfi/mfivar.h>
60 static int mfi_disk_probe(device_t dev);
61 static int mfi_disk_attach(device_t dev);
62 static int mfi_disk_detach(device_t dev);
64 static disk_open_t mfi_disk_open;
65 static disk_close_t mfi_disk_close;
66 static disk_strategy_t mfi_disk_strategy;
67 static dumper_t mfi_disk_dump;
69 static devclass_t mfi_disk_devclass;
71 static device_method_t mfi_disk_methods[] = {
72 DEVMETHOD(device_probe, mfi_disk_probe),
73 DEVMETHOD(device_attach, mfi_disk_attach),
74 DEVMETHOD(device_detach, mfi_disk_detach),
78 static driver_t mfi_disk_driver = {
81 sizeof(struct mfi_disk)
84 DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
87 mfi_disk_probe(device_t dev)
94 mfi_disk_attach(device_t dev)
97 struct mfi_ld_info *ld_info;
98 struct mfi_disk_pending *ld_pend;
103 sc = device_get_softc(dev);
104 ld_info = device_get_ivars(dev);
107 sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
108 sc->ld_unit = device_get_unit(dev);
109 sc->ld_info = ld_info;
110 sc->ld_controller = device_get_softc(device_get_parent(dev));
113 sectors = ld_info->size;
114 secsize = MFI_SECTOR_LEN;
115 mtx_lock(&sc->ld_controller->mfi_io_lock);
116 TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
117 TAILQ_FOREACH(ld_pend, &sc->ld_controller->mfi_ld_pend_tqh,
119 TAILQ_REMOVE(&sc->ld_controller->mfi_ld_pend_tqh,
121 free(ld_pend, M_MFIBUF);
124 mtx_unlock(&sc->ld_controller->mfi_io_lock);
126 switch (ld_info->ld_config.params.state) {
127 case MFI_LD_STATE_OFFLINE:
130 case MFI_LD_STATE_PARTIALLY_DEGRADED:
131 state = "partially degraded";
133 case MFI_LD_STATE_DEGRADED:
136 case MFI_LD_STATE_OPTIMAL:
144 if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
146 "%juMB (%ju sectors) RAID volume (no label) is %s\n",
147 sectors / (1024 * 1024 / secsize), sectors, state);
150 "%juMB (%ju sectors) RAID volume '%s' is %s\n",
151 sectors / (1024 * 1024 / secsize), sectors,
152 ld_info->ld_config.properties.name, state);
155 sc->ld_disk = disk_alloc();
156 sc->ld_disk->d_drv1 = sc;
157 sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
158 (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
159 sc->ld_disk->d_name = "mfid";
160 sc->ld_disk->d_open = mfi_disk_open;
161 sc->ld_disk->d_close = mfi_disk_close;
162 sc->ld_disk->d_strategy = mfi_disk_strategy;
163 sc->ld_disk->d_dump = mfi_disk_dump;
164 sc->ld_disk->d_unit = sc->ld_unit;
165 sc->ld_disk->d_sectorsize = secsize;
166 sc->ld_disk->d_mediasize = sectors * secsize;
167 if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
168 sc->ld_disk->d_fwheads = 255;
169 sc->ld_disk->d_fwsectors = 63;
171 sc->ld_disk->d_fwheads = 64;
172 sc->ld_disk->d_fwsectors = 32;
174 sc->ld_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
175 disk_create(sc->ld_disk, DISK_VERSION);
181 mfi_disk_detach(device_t dev)
185 sc = device_get_softc(dev);
187 mtx_lock(&sc->ld_controller->mfi_io_lock);
188 if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
189 (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
190 (sc->ld_controller->mfi_keep_deleted_volumes ||
191 sc->ld_controller->mfi_detaching)) {
192 mtx_unlock(&sc->ld_controller->mfi_io_lock);
195 mtx_unlock(&sc->ld_controller->mfi_io_lock);
197 disk_destroy(sc->ld_disk);
198 mtx_lock(&sc->ld_controller->mfi_io_lock);
199 TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
200 mtx_unlock(&sc->ld_controller->mfi_io_lock);
201 free(sc->ld_info, M_MFIBUF);
206 mfi_disk_open(struct disk *dp)
212 mtx_lock(&sc->ld_controller->mfi_io_lock);
213 if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
216 sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
219 mtx_unlock(&sc->ld_controller->mfi_io_lock);
225 mfi_disk_close(struct disk *dp)
230 mtx_lock(&sc->ld_controller->mfi_io_lock);
231 sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
232 mtx_unlock(&sc->ld_controller->mfi_io_lock);
238 mfi_disk_disable(struct mfi_disk *sc)
241 mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
242 if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
243 if (sc->ld_controller->mfi_delete_busy_volumes)
245 device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
248 sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
253 mfi_disk_enable(struct mfi_disk *sc)
256 mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
257 sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
261 mfi_disk_strategy(struct bio *bio)
264 struct mfi_softc *controller;
266 sc = bio->bio_disk->d_drv1;
269 bio->bio_error = EINVAL;
270 bio->bio_flags |= BIO_ERROR;
271 bio->bio_resid = bio->bio_bcount;
276 controller = sc->ld_controller;
277 if (controller->adpreset) {
278 bio->bio_error = EBUSY;
282 if (controller->hw_crit_error) {
283 bio->bio_error = EBUSY;
287 if (controller->issuepend_done == 0) {
288 bio->bio_error = EBUSY;
292 bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
293 /* Mark it as LD IO */
294 bio->bio_driver2 = (void *)MFI_LD_IO;
295 mtx_lock(&controller->mfi_io_lock);
296 mfi_enqueue_bio(controller, bio);
297 mfi_startio(controller);
298 mtx_unlock(&controller->mfi_io_lock);
303 mfi_disk_complete(struct bio *bio)
306 struct mfi_frame_header *hdr;
308 sc = bio->bio_disk->d_drv1;
309 hdr = bio->bio_driver1;
311 if (bio->bio_flags & BIO_ERROR) {
312 bio->bio_resid = bio->bio_bcount;
313 if (bio->bio_error == 0)
314 bio->bio_error = EIO;
315 disk_err(bio, "hard error", -1, 1);
323 mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
326 struct mfi_softc *parent_sc;
332 parent_sc = sc->ld_controller;
335 if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
336 MFI_SECTOR_LEN, virt, len)) != 0)
339 /* mfi_sync_cache(parent_sc, sc->ld_id); */