xref: /freebsd/sys/dev/mfi/mfi_disk.c (revision 843b298e623b10f8ff965a68534bf64311ef97de)
12e21a3efSScott Long /*-
22e21a3efSScott Long  * Copyright (c) 2006 IronPort Systems
32e21a3efSScott Long  * All rights reserved.
42e21a3efSScott Long  *
52e21a3efSScott Long  * Redistribution and use in source and binary forms, with or without
62e21a3efSScott Long  * modification, are permitted provided that the following conditions
72e21a3efSScott Long  * are met:
82e21a3efSScott Long  * 1. Redistributions of source code must retain the above copyright
92e21a3efSScott Long  *    notice, this list of conditions and the following disclaimer.
102e21a3efSScott Long  * 2. Redistributions in binary form must reproduce the above copyright
112e21a3efSScott Long  *    notice, this list of conditions and the following disclaimer in the
122e21a3efSScott Long  *    documentation and/or other materials provided with the distribution.
132e21a3efSScott Long  *
142e21a3efSScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152e21a3efSScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162e21a3efSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172e21a3efSScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182e21a3efSScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192e21a3efSScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202e21a3efSScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212e21a3efSScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222e21a3efSScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232e21a3efSScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242e21a3efSScott Long  * SUCH DAMAGE.
252e21a3efSScott Long  */
262e21a3efSScott Long 
272e21a3efSScott Long #include <sys/cdefs.h>
282e21a3efSScott Long __FBSDID("$FreeBSD$");
292e21a3efSScott Long 
302e21a3efSScott Long #include "opt_mfi.h"
312e21a3efSScott Long 
322e21a3efSScott Long #include <sys/param.h>
332e21a3efSScott Long #include <sys/systm.h>
342e21a3efSScott Long #include <sys/kernel.h>
35741367d5SDoug Ambrisko #include <sys/selinfo.h>
362e21a3efSScott Long #include <sys/module.h>
372e21a3efSScott Long #include <sys/malloc.h>
38d1c5fc76SJohn Baldwin #include <sys/sysctl.h>
39741367d5SDoug Ambrisko #include <sys/uio.h>
402e21a3efSScott Long 
412e21a3efSScott Long #include <sys/bio.h>
422e21a3efSScott Long #include <sys/bus.h>
432e21a3efSScott Long #include <sys/conf.h>
442e21a3efSScott Long #include <sys/disk.h>
452e21a3efSScott Long #include <geom/geom_disk.h>
462e21a3efSScott Long 
472e21a3efSScott Long #include <vm/vm.h>
482e21a3efSScott Long #include <vm/pmap.h>
492e21a3efSScott Long 
502e21a3efSScott Long #include <machine/md_var.h>
512e21a3efSScott Long #include <machine/bus.h>
522e21a3efSScott Long #include <sys/rman.h>
532e21a3efSScott Long 
542e21a3efSScott Long #include <dev/mfi/mfireg.h>
552e21a3efSScott Long #include <dev/mfi/mfi_ioctl.h>
562e21a3efSScott Long #include <dev/mfi/mfivar.h>
572e21a3efSScott Long 
582e21a3efSScott Long static int	mfi_disk_probe(device_t dev);
592e21a3efSScott Long static int	mfi_disk_attach(device_t dev);
602e21a3efSScott Long static int	mfi_disk_detach(device_t dev);
612e21a3efSScott Long 
622e21a3efSScott Long static disk_open_t	mfi_disk_open;
632e21a3efSScott Long static disk_close_t	mfi_disk_close;
642e21a3efSScott Long static disk_strategy_t	mfi_disk_strategy;
652e21a3efSScott Long static dumper_t		mfi_disk_dump;
662e21a3efSScott Long 
672e21a3efSScott Long static devclass_t	mfi_disk_devclass;
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 
822e21a3efSScott Long DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
832e21a3efSScott Long 
842e21a3efSScott Long static int
852e21a3efSScott Long mfi_disk_probe(device_t dev)
862e21a3efSScott Long {
872e21a3efSScott Long 
882e21a3efSScott Long 	return (0);
892e21a3efSScott Long }
902e21a3efSScott Long 
912e21a3efSScott Long static int
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 	}
1722e21a3efSScott Long 	disk_create(sc->ld_disk, DISK_VERSION);
1732e21a3efSScott Long 
1742e21a3efSScott Long 	return (0);
1752e21a3efSScott Long }
1762e21a3efSScott Long 
1772e21a3efSScott Long static int
1782e21a3efSScott Long mfi_disk_detach(device_t dev)
1792e21a3efSScott Long {
1802e21a3efSScott Long 	struct mfi_disk *sc;
1812e21a3efSScott Long 
1822e21a3efSScott Long 	sc = device_get_softc(dev);
1832e21a3efSScott Long 
1848ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
1858ec5c98bSJohn Baldwin 	if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
1868ec5c98bSJohn Baldwin 	    (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
1878ec5c98bSJohn Baldwin 	    (sc->ld_controller->mfi_keep_deleted_volumes ||
1888ec5c98bSJohn Baldwin 	    sc->ld_controller->mfi_detaching)) {
1898ec5c98bSJohn Baldwin 		mtx_unlock(&sc->ld_controller->mfi_io_lock);
1902e21a3efSScott Long 		return (EBUSY);
1918ec5c98bSJohn Baldwin 	}
1928ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1932e21a3efSScott Long 
1942e21a3efSScott Long 	disk_destroy(sc->ld_disk);
1958ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
1968ec5c98bSJohn Baldwin 	TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
1978ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1988ec5c98bSJohn Baldwin 	free(sc->ld_info, M_MFIBUF);
1992e21a3efSScott Long 	return (0);
2002e21a3efSScott Long }
2012e21a3efSScott Long 
2022e21a3efSScott Long static int
2032e21a3efSScott Long mfi_disk_open(struct disk *dp)
2042e21a3efSScott Long {
205ddfae47bSScott Long 	struct mfi_disk *sc;
2068ec5c98bSJohn Baldwin 	int error;
207ddfae47bSScott Long 
208ddfae47bSScott Long 	sc = dp->d_drv1;
209ddfae47bSScott Long 	mtx_lock(&sc->ld_controller->mfi_io_lock);
2108ec5c98bSJohn Baldwin 	if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
2118ec5c98bSJohn Baldwin 		error = ENXIO;
2128ec5c98bSJohn Baldwin 	else {
213ddfae47bSScott Long 		sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
2148ec5c98bSJohn Baldwin 		error = 0;
2158ec5c98bSJohn Baldwin 	}
216ddfae47bSScott Long 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2172e21a3efSScott Long 
2188ec5c98bSJohn Baldwin 	return (error);
2192e21a3efSScott Long }
2202e21a3efSScott Long 
2212e21a3efSScott Long static int
2222e21a3efSScott Long mfi_disk_close(struct disk *dp)
2232e21a3efSScott Long {
224ddfae47bSScott Long 	struct mfi_disk *sc;
225ddfae47bSScott Long 
226ddfae47bSScott Long 	sc = dp->d_drv1;
227ddfae47bSScott Long 	mtx_lock(&sc->ld_controller->mfi_io_lock);
228ddfae47bSScott Long 	sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
229ddfae47bSScott Long 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2302e21a3efSScott Long 
2312e21a3efSScott Long 	return (0);
2322e21a3efSScott Long }
2332e21a3efSScott Long 
2348ec5c98bSJohn Baldwin int
2358ec5c98bSJohn Baldwin mfi_disk_disable(struct mfi_disk *sc)
2368ec5c98bSJohn Baldwin {
2378ec5c98bSJohn Baldwin 
2388ec5c98bSJohn Baldwin 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2398ec5c98bSJohn Baldwin 	if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
2408ec5c98bSJohn Baldwin 		if (sc->ld_controller->mfi_delete_busy_volumes)
2418ec5c98bSJohn Baldwin 			return (0);
2420d9a4ef3SDoug Ambrisko 		device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
2438ec5c98bSJohn Baldwin 		return (EBUSY);
2448ec5c98bSJohn Baldwin 	}
2458ec5c98bSJohn Baldwin 	sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
2468ec5c98bSJohn Baldwin 	return (0);
2478ec5c98bSJohn Baldwin }
2488ec5c98bSJohn Baldwin 
2498ec5c98bSJohn Baldwin void
2508ec5c98bSJohn Baldwin mfi_disk_enable(struct mfi_disk *sc)
2518ec5c98bSJohn Baldwin {
2528ec5c98bSJohn Baldwin 
2538ec5c98bSJohn Baldwin 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2548ec5c98bSJohn Baldwin 	sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
2558ec5c98bSJohn Baldwin }
2568ec5c98bSJohn Baldwin 
2572e21a3efSScott Long static void
2582e21a3efSScott Long mfi_disk_strategy(struct bio *bio)
2592e21a3efSScott Long {
2602e21a3efSScott Long 	struct mfi_disk *sc;
2612e21a3efSScott Long 	struct mfi_softc *controller;
2622e21a3efSScott Long 
2632e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
2642e21a3efSScott Long 
2652e21a3efSScott Long 	if (sc == NULL) {
2662e21a3efSScott Long 		bio->bio_error = EINVAL;
2672e21a3efSScott Long 		bio->bio_flags |= BIO_ERROR;
2682e21a3efSScott Long 		bio->bio_resid = bio->bio_bcount;
2692e21a3efSScott Long 		biodone(bio);
2702e21a3efSScott Long 		return;
2712e21a3efSScott Long 	}
2722e21a3efSScott Long 
273*843b298eSXin LI 	controller = sc->ld_controller;
2740d9a4ef3SDoug Ambrisko 	if (controller->adpreset) {
2750d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2760d9a4ef3SDoug Ambrisko 		return;
2770d9a4ef3SDoug Ambrisko 	}
2780d9a4ef3SDoug Ambrisko 
2790d9a4ef3SDoug Ambrisko 	if (controller->hw_crit_error) {
2800d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2810d9a4ef3SDoug Ambrisko 		return;
2820d9a4ef3SDoug Ambrisko 	}
2830d9a4ef3SDoug Ambrisko 
2840d9a4ef3SDoug Ambrisko 	if (controller->issuepend_done == 0) {
2850d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2860d9a4ef3SDoug Ambrisko 		return;
2870d9a4ef3SDoug Ambrisko 	}
2880d9a4ef3SDoug Ambrisko 
2892e21a3efSScott Long 	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
2900d9a4ef3SDoug Ambrisko 	/* Mark it as LD IO */
2910d9a4ef3SDoug Ambrisko 	bio->bio_driver2 = (void *)MFI_LD_IO;
2922e21a3efSScott Long 	mtx_lock(&controller->mfi_io_lock);
2932e21a3efSScott Long 	mfi_enqueue_bio(controller, bio);
2942e21a3efSScott Long 	mfi_startio(controller);
2952e21a3efSScott Long 	mtx_unlock(&controller->mfi_io_lock);
2962e21a3efSScott Long 	return;
2972e21a3efSScott Long }
2982e21a3efSScott Long 
2992e21a3efSScott Long void
3002e21a3efSScott Long mfi_disk_complete(struct bio *bio)
3012e21a3efSScott Long {
3022e21a3efSScott Long 	struct mfi_disk *sc;
3032e21a3efSScott Long 	struct mfi_frame_header *hdr;
3042e21a3efSScott Long 
3052e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
3062e21a3efSScott Long 	hdr = bio->bio_driver1;
3072e21a3efSScott Long 
3082e21a3efSScott Long 	if (bio->bio_flags & BIO_ERROR) {
309dfcbfdbbSSean Bruno 		bio->bio_resid = bio->bio_bcount;
3102e21a3efSScott Long 		if (bio->bio_error == 0)
3112e21a3efSScott Long 			bio->bio_error = EIO;
3122e21a3efSScott Long 		disk_err(bio, "hard error", -1, 1);
3132e21a3efSScott Long 	} else {
3142e21a3efSScott Long 		bio->bio_resid = 0;
3152e21a3efSScott Long 	}
3162e21a3efSScott Long 	biodone(bio);
3172e21a3efSScott Long }
3182e21a3efSScott Long 
3192e21a3efSScott Long static int
3202e21a3efSScott Long mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
3212e21a3efSScott Long {
3222e21a3efSScott Long 	struct mfi_disk *sc;
3232e21a3efSScott Long 	struct mfi_softc *parent_sc;
3242e21a3efSScott Long 	struct disk *dp;
3252e21a3efSScott Long 	int error;
3262e21a3efSScott Long 
3272e21a3efSScott Long 	dp = arg;
3282e21a3efSScott Long 	sc = dp->d_drv1;
3292e21a3efSScott Long 	parent_sc = sc->ld_controller;
3302e21a3efSScott Long 
3312e21a3efSScott Long 	if (len > 0) {
3322e21a3efSScott Long 		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
333c0b332d1SPaul Saab 		    MFI_SECTOR_LEN, virt, len)) != 0)
3342e21a3efSScott Long 			return (error);
3352e21a3efSScott Long 	} else {
3362e21a3efSScott Long 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
3372e21a3efSScott Long 	}
3382e21a3efSScott Long 
3392e21a3efSScott Long 	return (0);
3402e21a3efSScott Long }
341