12e21a3efSScott Long /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
42e21a3efSScott Long * Copyright (c) 2006 IronPort Systems
52e21a3efSScott Long * All rights reserved.
62e21a3efSScott Long *
72e21a3efSScott Long * Redistribution and use in source and binary forms, with or without
82e21a3efSScott Long * modification, are permitted provided that the following conditions
92e21a3efSScott Long * are met:
102e21a3efSScott Long * 1. Redistributions of source code must retain the above copyright
112e21a3efSScott Long * notice, this list of conditions and the following disclaimer.
122e21a3efSScott Long * 2. Redistributions in binary form must reproduce the above copyright
132e21a3efSScott Long * notice, this list of conditions and the following disclaimer in the
142e21a3efSScott Long * documentation and/or other materials provided with the distribution.
152e21a3efSScott Long *
162e21a3efSScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172e21a3efSScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182e21a3efSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192e21a3efSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202e21a3efSScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212e21a3efSScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222e21a3efSScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232e21a3efSScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242e21a3efSScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252e21a3efSScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262e21a3efSScott Long * SUCH DAMAGE.
272e21a3efSScott Long */
282e21a3efSScott Long
292e21a3efSScott Long #include <sys/cdefs.h>
302e21a3efSScott Long #include "opt_mfi.h"
312e21a3efSScott Long
322e21a3efSScott Long #include <sys/param.h>
332e21a3efSScott Long #include <sys/bio.h>
342e21a3efSScott Long #include <sys/bus.h>
352e21a3efSScott Long #include <sys/conf.h>
362e21a3efSScott Long #include <sys/disk.h>
37e2e050c8SConrad Meyer #include <sys/kernel.h>
38e2e050c8SConrad Meyer #include <sys/lock.h>
39e2e050c8SConrad Meyer #include <sys/malloc.h>
40e2e050c8SConrad Meyer #include <sys/module.h>
41e2e050c8SConrad Meyer #include <sys/mutex.h>
42e2e050c8SConrad Meyer #include <sys/selinfo.h>
43e2e050c8SConrad Meyer #include <sys/sysctl.h>
44e2e050c8SConrad Meyer #include <sys/systm.h>
45e2e050c8SConrad Meyer #include <sys/uio.h>
46e2e050c8SConrad Meyer
472e21a3efSScott Long #include <geom/geom_disk.h>
482e21a3efSScott Long
492e21a3efSScott Long #include <vm/vm.h>
502e21a3efSScott Long #include <vm/pmap.h>
512e21a3efSScott Long
522e21a3efSScott Long #include <machine/md_var.h>
532e21a3efSScott Long #include <machine/bus.h>
542e21a3efSScott Long #include <sys/rman.h>
552e21a3efSScott Long
562e21a3efSScott Long #include <dev/mfi/mfireg.h>
572e21a3efSScott Long #include <dev/mfi/mfi_ioctl.h>
582e21a3efSScott Long #include <dev/mfi/mfivar.h>
592e21a3efSScott Long
602e21a3efSScott Long static int mfi_disk_probe(device_t dev);
612e21a3efSScott Long static int mfi_disk_attach(device_t dev);
622e21a3efSScott Long static int mfi_disk_detach(device_t dev);
632e21a3efSScott Long
642e21a3efSScott Long static disk_open_t mfi_disk_open;
652e21a3efSScott Long static disk_close_t mfi_disk_close;
662e21a3efSScott Long static disk_strategy_t mfi_disk_strategy;
672e21a3efSScott Long static dumper_t mfi_disk_dump;
682e21a3efSScott Long
692e21a3efSScott Long static device_method_t mfi_disk_methods[] = {
702e21a3efSScott Long DEVMETHOD(device_probe, mfi_disk_probe),
712e21a3efSScott Long DEVMETHOD(device_attach, mfi_disk_attach),
722e21a3efSScott Long DEVMETHOD(device_detach, mfi_disk_detach),
732e21a3efSScott Long { 0, 0 }
742e21a3efSScott Long };
752e21a3efSScott Long
762e21a3efSScott Long static driver_t mfi_disk_driver = {
772e21a3efSScott Long "mfid",
782e21a3efSScott Long mfi_disk_methods,
792e21a3efSScott Long sizeof(struct mfi_disk)
802e21a3efSScott Long };
812e21a3efSScott Long
82354f6c1cSJohn Baldwin DRIVER_MODULE(mfid, mfi, mfi_disk_driver, 0, 0);
832e21a3efSScott Long
842e21a3efSScott Long static int
mfi_disk_probe(device_t dev)852e21a3efSScott Long mfi_disk_probe(device_t dev)
862e21a3efSScott Long {
872e21a3efSScott Long
882e21a3efSScott Long return (0);
892e21a3efSScott Long }
902e21a3efSScott Long
912e21a3efSScott Long static int
mfi_disk_attach(device_t dev)922e21a3efSScott Long mfi_disk_attach(device_t dev)
932e21a3efSScott Long {
942e21a3efSScott Long struct mfi_disk *sc;
95ddfae47bSScott Long struct mfi_ld_info *ld_info;
9658ef3154SDoug Ambrisko struct mfi_disk_pending *ld_pend;
972e21a3efSScott Long uint64_t sectors;
982e21a3efSScott Long uint32_t secsize;
99441f6d5dSScott Long char *state;
1002e21a3efSScott Long
1012e21a3efSScott Long sc = device_get_softc(dev);
102ddfae47bSScott Long ld_info = device_get_ivars(dev);
1032e21a3efSScott Long
1042e21a3efSScott Long sc->ld_dev = dev;
105ddfae47bSScott Long sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
1062e21a3efSScott Long sc->ld_unit = device_get_unit(dev);
107ddfae47bSScott Long sc->ld_info = ld_info;
1082e21a3efSScott Long sc->ld_controller = device_get_softc(device_get_parent(dev));
109ddfae47bSScott Long sc->ld_flags = 0;
1102e21a3efSScott Long
111ddfae47bSScott Long sectors = ld_info->size;
112c0b332d1SPaul Saab secsize = MFI_SECTOR_LEN;
1138ec5c98bSJohn Baldwin mtx_lock(&sc->ld_controller->mfi_io_lock);
114ddfae47bSScott Long TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
11558ef3154SDoug Ambrisko TAILQ_FOREACH(ld_pend, &sc->ld_controller->mfi_ld_pend_tqh,
11658ef3154SDoug Ambrisko ld_link) {
11758ef3154SDoug Ambrisko TAILQ_REMOVE(&sc->ld_controller->mfi_ld_pend_tqh,
11858ef3154SDoug Ambrisko ld_pend, ld_link);
11958ef3154SDoug Ambrisko free(ld_pend, M_MFIBUF);
12058ef3154SDoug Ambrisko break;
12158ef3154SDoug Ambrisko }
1228ec5c98bSJohn Baldwin mtx_unlock(&sc->ld_controller->mfi_io_lock);
1232e21a3efSScott Long
124ddfae47bSScott Long switch (ld_info->ld_config.params.state) {
125441f6d5dSScott Long case MFI_LD_STATE_OFFLINE:
126441f6d5dSScott Long state = "offline";
127441f6d5dSScott Long break;
128441f6d5dSScott Long case MFI_LD_STATE_PARTIALLY_DEGRADED:
129441f6d5dSScott Long state = "partially degraded";
130441f6d5dSScott Long break;
131441f6d5dSScott Long case MFI_LD_STATE_DEGRADED:
132441f6d5dSScott Long state = "degraded";
133441f6d5dSScott Long break;
134441f6d5dSScott Long case MFI_LD_STATE_OPTIMAL:
135441f6d5dSScott Long state = "optimal";
136441f6d5dSScott Long break;
137441f6d5dSScott Long default:
138441f6d5dSScott Long state = "unknown";
139441f6d5dSScott Long break;
140441f6d5dSScott Long }
14187e72716SSean Bruno
14287e72716SSean Bruno if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
14387e72716SSean Bruno device_printf(dev,
14487e72716SSean Bruno "%juMB (%ju sectors) RAID volume (no label) is %s\n",
14587e72716SSean Bruno sectors / (1024 * 1024 / secsize), sectors, state);
14687e72716SSean Bruno } else {
14787e72716SSean Bruno device_printf(dev,
14887e72716SSean Bruno "%juMB (%ju sectors) RAID volume '%s' is %s\n",
149441f6d5dSScott Long sectors / (1024 * 1024 / secsize), sectors,
15087e72716SSean Bruno ld_info->ld_config.properties.name, state);
15187e72716SSean Bruno }
1522e21a3efSScott Long
1532e21a3efSScott Long sc->ld_disk = disk_alloc();
1542e21a3efSScott Long sc->ld_disk->d_drv1 = sc;
155d0e14c50SJohn Baldwin sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
156d0e14c50SJohn Baldwin (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
1572e21a3efSScott Long sc->ld_disk->d_name = "mfid";
1582e21a3efSScott Long sc->ld_disk->d_open = mfi_disk_open;
1592e21a3efSScott Long sc->ld_disk->d_close = mfi_disk_close;
1602e21a3efSScott Long sc->ld_disk->d_strategy = mfi_disk_strategy;
1612e21a3efSScott Long sc->ld_disk->d_dump = mfi_disk_dump;
1622e21a3efSScott Long sc->ld_disk->d_unit = sc->ld_unit;
1632e21a3efSScott Long sc->ld_disk->d_sectorsize = secsize;
1642e21a3efSScott Long sc->ld_disk->d_mediasize = sectors * secsize;
1652e21a3efSScott Long if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
1662e21a3efSScott Long sc->ld_disk->d_fwheads = 255;
1672e21a3efSScott Long sc->ld_disk->d_fwsectors = 63;
1682e21a3efSScott Long } else {
1692e21a3efSScott Long sc->ld_disk->d_fwheads = 64;
1702e21a3efSScott Long sc->ld_disk->d_fwsectors = 32;
1712e21a3efSScott Long }
172c33ce503SKonstantin Belousov sc->ld_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
1732e21a3efSScott Long disk_create(sc->ld_disk, DISK_VERSION);
1742e21a3efSScott Long
1752e21a3efSScott Long return (0);
1762e21a3efSScott Long }
1772e21a3efSScott Long
1782e21a3efSScott Long static int
mfi_disk_detach(device_t dev)1792e21a3efSScott Long mfi_disk_detach(device_t dev)
1802e21a3efSScott Long {
1812e21a3efSScott Long struct mfi_disk *sc;
1822e21a3efSScott Long
1832e21a3efSScott Long sc = device_get_softc(dev);
1842e21a3efSScott Long
1858ec5c98bSJohn Baldwin mtx_lock(&sc->ld_controller->mfi_io_lock);
1868ec5c98bSJohn Baldwin if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
1878ec5c98bSJohn Baldwin (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
1888ec5c98bSJohn Baldwin (sc->ld_controller->mfi_keep_deleted_volumes ||
1898ec5c98bSJohn Baldwin sc->ld_controller->mfi_detaching)) {
1908ec5c98bSJohn Baldwin mtx_unlock(&sc->ld_controller->mfi_io_lock);
1912e21a3efSScott Long return (EBUSY);
1928ec5c98bSJohn Baldwin }
1938ec5c98bSJohn Baldwin mtx_unlock(&sc->ld_controller->mfi_io_lock);
1942e21a3efSScott Long
1952e21a3efSScott Long disk_destroy(sc->ld_disk);
1968ec5c98bSJohn Baldwin mtx_lock(&sc->ld_controller->mfi_io_lock);
1978ec5c98bSJohn Baldwin TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
1988ec5c98bSJohn Baldwin mtx_unlock(&sc->ld_controller->mfi_io_lock);
1998ec5c98bSJohn Baldwin free(sc->ld_info, M_MFIBUF);
2002e21a3efSScott Long return (0);
2012e21a3efSScott Long }
2022e21a3efSScott Long
2032e21a3efSScott Long static int
mfi_disk_open(struct disk * dp)2042e21a3efSScott Long mfi_disk_open(struct disk *dp)
2052e21a3efSScott Long {
206ddfae47bSScott Long struct mfi_disk *sc;
2078ec5c98bSJohn Baldwin int error;
208ddfae47bSScott Long
209ddfae47bSScott Long sc = dp->d_drv1;
210ddfae47bSScott Long mtx_lock(&sc->ld_controller->mfi_io_lock);
2118ec5c98bSJohn Baldwin if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
2128ec5c98bSJohn Baldwin error = ENXIO;
2138ec5c98bSJohn Baldwin else {
214ddfae47bSScott Long sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
2158ec5c98bSJohn Baldwin error = 0;
2168ec5c98bSJohn Baldwin }
217ddfae47bSScott Long mtx_unlock(&sc->ld_controller->mfi_io_lock);
2182e21a3efSScott Long
2198ec5c98bSJohn Baldwin return (error);
2202e21a3efSScott Long }
2212e21a3efSScott Long
2222e21a3efSScott Long static int
mfi_disk_close(struct disk * dp)2232e21a3efSScott Long mfi_disk_close(struct disk *dp)
2242e21a3efSScott Long {
225ddfae47bSScott Long struct mfi_disk *sc;
226ddfae47bSScott Long
227ddfae47bSScott Long sc = dp->d_drv1;
228ddfae47bSScott Long mtx_lock(&sc->ld_controller->mfi_io_lock);
229ddfae47bSScott Long sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
230ddfae47bSScott Long mtx_unlock(&sc->ld_controller->mfi_io_lock);
2312e21a3efSScott Long
2322e21a3efSScott Long return (0);
2332e21a3efSScott Long }
2342e21a3efSScott Long
2358ec5c98bSJohn Baldwin int
mfi_disk_disable(struct mfi_disk * sc)2368ec5c98bSJohn Baldwin mfi_disk_disable(struct mfi_disk *sc)
2378ec5c98bSJohn Baldwin {
2388ec5c98bSJohn Baldwin
2398ec5c98bSJohn Baldwin mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2408ec5c98bSJohn Baldwin if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
2418ec5c98bSJohn Baldwin if (sc->ld_controller->mfi_delete_busy_volumes)
2428ec5c98bSJohn Baldwin return (0);
2430d9a4ef3SDoug Ambrisko device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
2448ec5c98bSJohn Baldwin return (EBUSY);
2458ec5c98bSJohn Baldwin }
2468ec5c98bSJohn Baldwin sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
2478ec5c98bSJohn Baldwin return (0);
2488ec5c98bSJohn Baldwin }
2498ec5c98bSJohn Baldwin
2508ec5c98bSJohn Baldwin void
mfi_disk_enable(struct mfi_disk * sc)2518ec5c98bSJohn Baldwin mfi_disk_enable(struct mfi_disk *sc)
2528ec5c98bSJohn Baldwin {
2538ec5c98bSJohn Baldwin
2548ec5c98bSJohn Baldwin mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2558ec5c98bSJohn Baldwin sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
2568ec5c98bSJohn Baldwin }
2578ec5c98bSJohn Baldwin
2582e21a3efSScott Long static void
mfi_disk_strategy(struct bio * bio)2592e21a3efSScott Long mfi_disk_strategy(struct bio *bio)
2602e21a3efSScott Long {
2612e21a3efSScott Long struct mfi_disk *sc;
2622e21a3efSScott Long struct mfi_softc *controller;
2632e21a3efSScott Long
2642e21a3efSScott Long sc = bio->bio_disk->d_drv1;
2652e21a3efSScott Long
2662e21a3efSScott Long if (sc == NULL) {
2672e21a3efSScott Long bio->bio_error = EINVAL;
2682e21a3efSScott Long bio->bio_flags |= BIO_ERROR;
2692e21a3efSScott Long bio->bio_resid = bio->bio_bcount;
2702e21a3efSScott Long biodone(bio);
2712e21a3efSScott Long return;
2722e21a3efSScott Long }
2732e21a3efSScott Long
274843b298eSXin LI controller = sc->ld_controller;
2750d9a4ef3SDoug Ambrisko if (controller->adpreset) {
2760d9a4ef3SDoug Ambrisko bio->bio_error = EBUSY;
2770d9a4ef3SDoug Ambrisko return;
2780d9a4ef3SDoug Ambrisko }
2790d9a4ef3SDoug Ambrisko
2800d9a4ef3SDoug Ambrisko if (controller->hw_crit_error) {
2810d9a4ef3SDoug Ambrisko bio->bio_error = EBUSY;
2820d9a4ef3SDoug Ambrisko return;
2830d9a4ef3SDoug Ambrisko }
2840d9a4ef3SDoug Ambrisko
2850d9a4ef3SDoug Ambrisko if (controller->issuepend_done == 0) {
2860d9a4ef3SDoug Ambrisko bio->bio_error = EBUSY;
2870d9a4ef3SDoug Ambrisko return;
2880d9a4ef3SDoug Ambrisko }
2890d9a4ef3SDoug Ambrisko
2902e21a3efSScott Long bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
2910d9a4ef3SDoug Ambrisko /* Mark it as LD IO */
2920d9a4ef3SDoug Ambrisko bio->bio_driver2 = (void *)MFI_LD_IO;
2932e21a3efSScott Long mtx_lock(&controller->mfi_io_lock);
2942e21a3efSScott Long mfi_enqueue_bio(controller, bio);
2952e21a3efSScott Long mfi_startio(controller);
2962e21a3efSScott Long mtx_unlock(&controller->mfi_io_lock);
2972e21a3efSScott Long return;
2982e21a3efSScott Long }
2992e21a3efSScott Long
3002e21a3efSScott Long void
mfi_disk_complete(struct bio * bio)3012e21a3efSScott Long mfi_disk_complete(struct bio *bio)
3022e21a3efSScott Long {
3032e21a3efSScott Long
3042e21a3efSScott Long if (bio->bio_flags & BIO_ERROR) {
305dfcbfdbbSSean Bruno bio->bio_resid = bio->bio_bcount;
3062e21a3efSScott Long if (bio->bio_error == 0)
3072e21a3efSScott Long bio->bio_error = EIO;
3082e21a3efSScott Long disk_err(bio, "hard error", -1, 1);
3092e21a3efSScott Long } else {
3102e21a3efSScott Long bio->bio_resid = 0;
3112e21a3efSScott Long }
3122e21a3efSScott Long biodone(bio);
3132e21a3efSScott Long }
3142e21a3efSScott Long
3152e21a3efSScott Long static int
mfi_disk_dump(void * arg,void * virt,off_t offset,size_t len)316489ba222SMitchell Horne mfi_disk_dump(void *arg, void *virt, off_t offset, size_t len)
3172e21a3efSScott Long {
3182e21a3efSScott Long struct mfi_disk *sc;
3192e21a3efSScott Long struct mfi_softc *parent_sc;
3202e21a3efSScott Long struct disk *dp;
3212e21a3efSScott Long int error;
3222e21a3efSScott Long
3232e21a3efSScott Long dp = arg;
3242e21a3efSScott Long sc = dp->d_drv1;
3252e21a3efSScott Long parent_sc = sc->ld_controller;
3262e21a3efSScott Long
3272e21a3efSScott Long if (len > 0) {
3282e21a3efSScott Long if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
329c0b332d1SPaul Saab MFI_SECTOR_LEN, virt, len)) != 0)
3302e21a3efSScott Long return (error);
3312e21a3efSScott Long } else {
3322e21a3efSScott Long /* mfi_sync_cache(parent_sc, sc->ld_id); */
3332e21a3efSScott Long }
3342e21a3efSScott Long
3352e21a3efSScott Long return (0);
3362e21a3efSScott Long }
337