xref: /freebsd/sys/dev/mfi/mfi_disk.c (revision 354f6c1cc1ee0bf109a9134f918105e63b59c8d4)
12e21a3efSScott Long /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
312e21a3efSScott Long 
322e21a3efSScott Long #include "opt_mfi.h"
332e21a3efSScott Long 
342e21a3efSScott Long #include <sys/param.h>
352e21a3efSScott Long #include <sys/bio.h>
362e21a3efSScott Long #include <sys/bus.h>
372e21a3efSScott Long #include <sys/conf.h>
382e21a3efSScott Long #include <sys/disk.h>
39e2e050c8SConrad Meyer #include <sys/kernel.h>
40e2e050c8SConrad Meyer #include <sys/lock.h>
41e2e050c8SConrad Meyer #include <sys/malloc.h>
42e2e050c8SConrad Meyer #include <sys/module.h>
43e2e050c8SConrad Meyer #include <sys/mutex.h>
44e2e050c8SConrad Meyer #include <sys/selinfo.h>
45e2e050c8SConrad Meyer #include <sys/sysctl.h>
46e2e050c8SConrad Meyer #include <sys/systm.h>
47e2e050c8SConrad Meyer #include <sys/uio.h>
48e2e050c8SConrad Meyer 
492e21a3efSScott Long #include <geom/geom_disk.h>
502e21a3efSScott Long 
512e21a3efSScott Long #include <vm/vm.h>
522e21a3efSScott Long #include <vm/pmap.h>
532e21a3efSScott Long 
542e21a3efSScott Long #include <machine/md_var.h>
552e21a3efSScott Long #include <machine/bus.h>
562e21a3efSScott Long #include <sys/rman.h>
572e21a3efSScott Long 
582e21a3efSScott Long #include <dev/mfi/mfireg.h>
592e21a3efSScott Long #include <dev/mfi/mfi_ioctl.h>
602e21a3efSScott Long #include <dev/mfi/mfivar.h>
612e21a3efSScott Long 
622e21a3efSScott Long static int	mfi_disk_probe(device_t dev);
632e21a3efSScott Long static int	mfi_disk_attach(device_t dev);
642e21a3efSScott Long static int	mfi_disk_detach(device_t dev);
652e21a3efSScott Long 
662e21a3efSScott Long static disk_open_t	mfi_disk_open;
672e21a3efSScott Long static disk_close_t	mfi_disk_close;
682e21a3efSScott Long static disk_strategy_t	mfi_disk_strategy;
692e21a3efSScott Long static dumper_t		mfi_disk_dump;
702e21a3efSScott Long 
712e21a3efSScott Long static device_method_t mfi_disk_methods[] = {
722e21a3efSScott Long 	DEVMETHOD(device_probe,		mfi_disk_probe),
732e21a3efSScott Long 	DEVMETHOD(device_attach,	mfi_disk_attach),
742e21a3efSScott Long 	DEVMETHOD(device_detach,	mfi_disk_detach),
752e21a3efSScott Long 	{ 0, 0 }
762e21a3efSScott Long };
772e21a3efSScott Long 
782e21a3efSScott Long static driver_t mfi_disk_driver = {
792e21a3efSScott Long 	"mfid",
802e21a3efSScott Long 	mfi_disk_methods,
812e21a3efSScott Long 	sizeof(struct mfi_disk)
822e21a3efSScott Long };
832e21a3efSScott Long 
84*354f6c1cSJohn Baldwin DRIVER_MODULE(mfid, mfi, mfi_disk_driver, 0, 0);
852e21a3efSScott Long 
862e21a3efSScott Long static int
872e21a3efSScott Long mfi_disk_probe(device_t dev)
882e21a3efSScott Long {
892e21a3efSScott Long 
902e21a3efSScott Long 	return (0);
912e21a3efSScott Long }
922e21a3efSScott Long 
932e21a3efSScott Long static int
942e21a3efSScott Long mfi_disk_attach(device_t dev)
952e21a3efSScott Long {
962e21a3efSScott Long 	struct mfi_disk *sc;
97ddfae47bSScott Long 	struct mfi_ld_info *ld_info;
9858ef3154SDoug Ambrisko 	struct mfi_disk_pending *ld_pend;
992e21a3efSScott Long 	uint64_t sectors;
1002e21a3efSScott Long 	uint32_t secsize;
101441f6d5dSScott Long 	char *state;
1022e21a3efSScott Long 
1032e21a3efSScott Long 	sc = device_get_softc(dev);
104ddfae47bSScott Long 	ld_info = device_get_ivars(dev);
1052e21a3efSScott Long 
1062e21a3efSScott Long 	sc->ld_dev = dev;
107ddfae47bSScott Long 	sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
1082e21a3efSScott Long 	sc->ld_unit = device_get_unit(dev);
109ddfae47bSScott Long 	sc->ld_info = ld_info;
1102e21a3efSScott Long 	sc->ld_controller = device_get_softc(device_get_parent(dev));
111ddfae47bSScott Long 	sc->ld_flags = 0;
1122e21a3efSScott Long 
113ddfae47bSScott Long 	sectors = ld_info->size;
114c0b332d1SPaul Saab 	secsize = MFI_SECTOR_LEN;
1158ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
116ddfae47bSScott Long 	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
11758ef3154SDoug Ambrisko 	TAILQ_FOREACH(ld_pend, &sc->ld_controller->mfi_ld_pend_tqh,
11858ef3154SDoug Ambrisko 	    ld_link) {
11958ef3154SDoug Ambrisko 		TAILQ_REMOVE(&sc->ld_controller->mfi_ld_pend_tqh,
12058ef3154SDoug Ambrisko 		    ld_pend, ld_link);
12158ef3154SDoug Ambrisko 		free(ld_pend, M_MFIBUF);
12258ef3154SDoug Ambrisko 		break;
12358ef3154SDoug Ambrisko 	}
1248ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1252e21a3efSScott Long 
126ddfae47bSScott Long 	switch (ld_info->ld_config.params.state) {
127441f6d5dSScott Long 	case MFI_LD_STATE_OFFLINE:
128441f6d5dSScott Long 		state = "offline";
129441f6d5dSScott Long 		break;
130441f6d5dSScott Long 	case MFI_LD_STATE_PARTIALLY_DEGRADED:
131441f6d5dSScott Long 		state = "partially degraded";
132441f6d5dSScott Long 		break;
133441f6d5dSScott Long 	case MFI_LD_STATE_DEGRADED:
134441f6d5dSScott Long 		state = "degraded";
135441f6d5dSScott Long 		break;
136441f6d5dSScott Long 	case MFI_LD_STATE_OPTIMAL:
137441f6d5dSScott Long 		state = "optimal";
138441f6d5dSScott Long 		break;
139441f6d5dSScott Long 	default:
140441f6d5dSScott Long 		state = "unknown";
141441f6d5dSScott Long 		break;
142441f6d5dSScott Long 	}
14387e72716SSean Bruno 
14487e72716SSean Bruno 	if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
14587e72716SSean Bruno 		device_printf(dev,
14687e72716SSean Bruno 		      "%juMB (%ju sectors) RAID volume (no label) is %s\n",
14787e72716SSean Bruno 		       sectors / (1024 * 1024 / secsize), sectors, state);
14887e72716SSean Bruno 	} else {
14987e72716SSean Bruno 		device_printf(dev,
15087e72716SSean Bruno 		      "%juMB (%ju sectors) RAID volume '%s' is %s\n",
151441f6d5dSScott Long 		      sectors / (1024 * 1024 / secsize), sectors,
15287e72716SSean Bruno 		      ld_info->ld_config.properties.name, state);
15387e72716SSean Bruno 	}
1542e21a3efSScott Long 
1552e21a3efSScott Long 	sc->ld_disk = disk_alloc();
1562e21a3efSScott Long 	sc->ld_disk->d_drv1 = sc;
157d0e14c50SJohn Baldwin 	sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
158d0e14c50SJohn Baldwin 	    (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
1592e21a3efSScott Long 	sc->ld_disk->d_name = "mfid";
1602e21a3efSScott Long 	sc->ld_disk->d_open = mfi_disk_open;
1612e21a3efSScott Long 	sc->ld_disk->d_close = mfi_disk_close;
1622e21a3efSScott Long 	sc->ld_disk->d_strategy = mfi_disk_strategy;
1632e21a3efSScott Long 	sc->ld_disk->d_dump = mfi_disk_dump;
1642e21a3efSScott Long 	sc->ld_disk->d_unit = sc->ld_unit;
1652e21a3efSScott Long 	sc->ld_disk->d_sectorsize = secsize;
1662e21a3efSScott Long 	sc->ld_disk->d_mediasize = sectors * secsize;
1672e21a3efSScott Long 	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
1682e21a3efSScott Long 		sc->ld_disk->d_fwheads = 255;
1692e21a3efSScott Long 		sc->ld_disk->d_fwsectors = 63;
1702e21a3efSScott Long 	} else {
1712e21a3efSScott Long 		sc->ld_disk->d_fwheads = 64;
1722e21a3efSScott Long 		sc->ld_disk->d_fwsectors = 32;
1732e21a3efSScott Long 	}
174c33ce503SKonstantin Belousov 	sc->ld_disk->d_flags = DISKFLAG_UNMAPPED_BIO;
1752e21a3efSScott Long 	disk_create(sc->ld_disk, DISK_VERSION);
1762e21a3efSScott Long 
1772e21a3efSScott Long 	return (0);
1782e21a3efSScott Long }
1792e21a3efSScott Long 
1802e21a3efSScott Long static int
1812e21a3efSScott Long mfi_disk_detach(device_t dev)
1822e21a3efSScott Long {
1832e21a3efSScott Long 	struct mfi_disk *sc;
1842e21a3efSScott Long 
1852e21a3efSScott Long 	sc = device_get_softc(dev);
1862e21a3efSScott Long 
1878ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
1888ec5c98bSJohn Baldwin 	if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
1898ec5c98bSJohn Baldwin 	    (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
1908ec5c98bSJohn Baldwin 	    (sc->ld_controller->mfi_keep_deleted_volumes ||
1918ec5c98bSJohn Baldwin 	    sc->ld_controller->mfi_detaching)) {
1928ec5c98bSJohn Baldwin 		mtx_unlock(&sc->ld_controller->mfi_io_lock);
1932e21a3efSScott Long 		return (EBUSY);
1948ec5c98bSJohn Baldwin 	}
1958ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1962e21a3efSScott Long 
1972e21a3efSScott Long 	disk_destroy(sc->ld_disk);
1988ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
1998ec5c98bSJohn Baldwin 	TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
2008ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2018ec5c98bSJohn Baldwin 	free(sc->ld_info, M_MFIBUF);
2022e21a3efSScott Long 	return (0);
2032e21a3efSScott Long }
2042e21a3efSScott Long 
2052e21a3efSScott Long static int
2062e21a3efSScott Long mfi_disk_open(struct disk *dp)
2072e21a3efSScott Long {
208ddfae47bSScott Long 	struct mfi_disk *sc;
2098ec5c98bSJohn Baldwin 	int error;
210ddfae47bSScott Long 
211ddfae47bSScott Long 	sc = dp->d_drv1;
212ddfae47bSScott Long 	mtx_lock(&sc->ld_controller->mfi_io_lock);
2138ec5c98bSJohn Baldwin 	if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
2148ec5c98bSJohn Baldwin 		error = ENXIO;
2158ec5c98bSJohn Baldwin 	else {
216ddfae47bSScott Long 		sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
2178ec5c98bSJohn Baldwin 		error = 0;
2188ec5c98bSJohn Baldwin 	}
219ddfae47bSScott Long 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2202e21a3efSScott Long 
2218ec5c98bSJohn Baldwin 	return (error);
2222e21a3efSScott Long }
2232e21a3efSScott Long 
2242e21a3efSScott Long static int
2252e21a3efSScott Long mfi_disk_close(struct disk *dp)
2262e21a3efSScott Long {
227ddfae47bSScott Long 	struct mfi_disk *sc;
228ddfae47bSScott Long 
229ddfae47bSScott Long 	sc = dp->d_drv1;
230ddfae47bSScott Long 	mtx_lock(&sc->ld_controller->mfi_io_lock);
231ddfae47bSScott Long 	sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
232ddfae47bSScott Long 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2332e21a3efSScott Long 
2342e21a3efSScott Long 	return (0);
2352e21a3efSScott Long }
2362e21a3efSScott Long 
2378ec5c98bSJohn Baldwin int
2388ec5c98bSJohn Baldwin mfi_disk_disable(struct mfi_disk *sc)
2398ec5c98bSJohn Baldwin {
2408ec5c98bSJohn Baldwin 
2418ec5c98bSJohn Baldwin 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2428ec5c98bSJohn Baldwin 	if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
2438ec5c98bSJohn Baldwin 		if (sc->ld_controller->mfi_delete_busy_volumes)
2448ec5c98bSJohn Baldwin 			return (0);
2450d9a4ef3SDoug Ambrisko 		device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
2468ec5c98bSJohn Baldwin 		return (EBUSY);
2478ec5c98bSJohn Baldwin 	}
2488ec5c98bSJohn Baldwin 	sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
2498ec5c98bSJohn Baldwin 	return (0);
2508ec5c98bSJohn Baldwin }
2518ec5c98bSJohn Baldwin 
2528ec5c98bSJohn Baldwin void
2538ec5c98bSJohn Baldwin mfi_disk_enable(struct mfi_disk *sc)
2548ec5c98bSJohn Baldwin {
2558ec5c98bSJohn Baldwin 
2568ec5c98bSJohn Baldwin 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2578ec5c98bSJohn Baldwin 	sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
2588ec5c98bSJohn Baldwin }
2598ec5c98bSJohn Baldwin 
2602e21a3efSScott Long static void
2612e21a3efSScott Long mfi_disk_strategy(struct bio *bio)
2622e21a3efSScott Long {
2632e21a3efSScott Long 	struct mfi_disk *sc;
2642e21a3efSScott Long 	struct mfi_softc *controller;
2652e21a3efSScott Long 
2662e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
2672e21a3efSScott Long 
2682e21a3efSScott Long 	if (sc == NULL) {
2692e21a3efSScott Long 		bio->bio_error = EINVAL;
2702e21a3efSScott Long 		bio->bio_flags |= BIO_ERROR;
2712e21a3efSScott Long 		bio->bio_resid = bio->bio_bcount;
2722e21a3efSScott Long 		biodone(bio);
2732e21a3efSScott Long 		return;
2742e21a3efSScott Long 	}
2752e21a3efSScott Long 
276843b298eSXin LI 	controller = sc->ld_controller;
2770d9a4ef3SDoug Ambrisko 	if (controller->adpreset) {
2780d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2790d9a4ef3SDoug Ambrisko 		return;
2800d9a4ef3SDoug Ambrisko 	}
2810d9a4ef3SDoug Ambrisko 
2820d9a4ef3SDoug Ambrisko 	if (controller->hw_crit_error) {
2830d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2840d9a4ef3SDoug Ambrisko 		return;
2850d9a4ef3SDoug Ambrisko 	}
2860d9a4ef3SDoug Ambrisko 
2870d9a4ef3SDoug Ambrisko 	if (controller->issuepend_done == 0) {
2880d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2890d9a4ef3SDoug Ambrisko 		return;
2900d9a4ef3SDoug Ambrisko 	}
2910d9a4ef3SDoug Ambrisko 
2922e21a3efSScott Long 	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
2930d9a4ef3SDoug Ambrisko 	/* Mark it as LD IO */
2940d9a4ef3SDoug Ambrisko 	bio->bio_driver2 = (void *)MFI_LD_IO;
2952e21a3efSScott Long 	mtx_lock(&controller->mfi_io_lock);
2962e21a3efSScott Long 	mfi_enqueue_bio(controller, bio);
2972e21a3efSScott Long 	mfi_startio(controller);
2982e21a3efSScott Long 	mtx_unlock(&controller->mfi_io_lock);
2992e21a3efSScott Long 	return;
3002e21a3efSScott Long }
3012e21a3efSScott Long 
3022e21a3efSScott Long void
3032e21a3efSScott Long mfi_disk_complete(struct bio *bio)
3042e21a3efSScott Long {
3052e21a3efSScott Long 
3062e21a3efSScott Long 	if (bio->bio_flags & BIO_ERROR) {
307dfcbfdbbSSean Bruno 		bio->bio_resid = bio->bio_bcount;
3082e21a3efSScott Long 		if (bio->bio_error == 0)
3092e21a3efSScott Long 			bio->bio_error = EIO;
3102e21a3efSScott Long 		disk_err(bio, "hard error", -1, 1);
3112e21a3efSScott Long 	} else {
3122e21a3efSScott Long 		bio->bio_resid = 0;
3132e21a3efSScott Long 	}
3142e21a3efSScott Long 	biodone(bio);
3152e21a3efSScott Long }
3162e21a3efSScott Long 
3172e21a3efSScott Long static int
3182e21a3efSScott Long mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
3192e21a3efSScott Long {
3202e21a3efSScott Long 	struct mfi_disk *sc;
3212e21a3efSScott Long 	struct mfi_softc *parent_sc;
3222e21a3efSScott Long 	struct disk *dp;
3232e21a3efSScott Long 	int error;
3242e21a3efSScott Long 
3252e21a3efSScott Long 	dp = arg;
3262e21a3efSScott Long 	sc = dp->d_drv1;
3272e21a3efSScott Long 	parent_sc = sc->ld_controller;
3282e21a3efSScott Long 
3292e21a3efSScott Long 	if (len > 0) {
3302e21a3efSScott Long 		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
331c0b332d1SPaul Saab 		    MFI_SECTOR_LEN, virt, len)) != 0)
3322e21a3efSScott Long 			return (error);
3332e21a3efSScott Long 	} else {
3342e21a3efSScott Long 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
3352e21a3efSScott Long 	}
3362e21a3efSScott Long 
3372e21a3efSScott Long 	return (0);
3382e21a3efSScott Long }
339