xref: /freebsd/sys/dev/mfi/mfi_disk.c (revision dfcbfdbb909b07ee3b9211509987285cdc29a70c)
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;
962e21a3efSScott Long 	uint64_t sectors;
972e21a3efSScott Long 	uint32_t secsize;
98441f6d5dSScott Long 	char *state;
992e21a3efSScott Long 
1002e21a3efSScott Long 	sc = device_get_softc(dev);
101ddfae47bSScott Long 	ld_info = device_get_ivars(dev);
1022e21a3efSScott Long 
1032e21a3efSScott Long 	sc->ld_dev = dev;
104ddfae47bSScott Long 	sc->ld_id = ld_info->ld_config.properties.ld.v.target_id;
1052e21a3efSScott Long 	sc->ld_unit = device_get_unit(dev);
106ddfae47bSScott Long 	sc->ld_info = ld_info;
1072e21a3efSScott Long 	sc->ld_controller = device_get_softc(device_get_parent(dev));
108ddfae47bSScott Long 	sc->ld_flags = 0;
1092e21a3efSScott Long 
110ddfae47bSScott Long 	sectors = ld_info->size;
111c0b332d1SPaul Saab 	secsize = MFI_SECTOR_LEN;
1128ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
113ddfae47bSScott Long 	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
1148ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1152e21a3efSScott Long 
116ddfae47bSScott Long 	switch (ld_info->ld_config.params.state) {
117441f6d5dSScott Long 	case MFI_LD_STATE_OFFLINE:
118441f6d5dSScott Long 		state = "offline";
119441f6d5dSScott Long 		break;
120441f6d5dSScott Long 	case MFI_LD_STATE_PARTIALLY_DEGRADED:
121441f6d5dSScott Long 		state = "partially degraded";
122441f6d5dSScott Long 		break;
123441f6d5dSScott Long 	case MFI_LD_STATE_DEGRADED:
124441f6d5dSScott Long 		state = "degraded";
125441f6d5dSScott Long 		break;
126441f6d5dSScott Long 	case MFI_LD_STATE_OPTIMAL:
127441f6d5dSScott Long 		state = "optimal";
128441f6d5dSScott Long 		break;
129441f6d5dSScott Long 	default:
130441f6d5dSScott Long 		state = "unknown";
131441f6d5dSScott Long 		break;
132441f6d5dSScott Long 	}
13387e72716SSean Bruno 
13487e72716SSean Bruno         if ( strlen(ld_info->ld_config.properties.name) == 0 ) {
13587e72716SSean Bruno                 device_printf(dev,
13687e72716SSean Bruno                       "%juMB (%ju sectors) RAID volume (no label) is %s\n",
13787e72716SSean Bruno                       sectors / (1024 * 1024 / secsize), sectors, state);
13887e72716SSean Bruno         } else {
13987e72716SSean Bruno                 device_printf(dev,
14087e72716SSean Bruno                       "%juMB (%ju sectors) RAID volume '%s' is %s\n",
141441f6d5dSScott Long                       sectors / (1024 * 1024 / secsize), sectors,
14287e72716SSean Bruno                       ld_info->ld_config.properties.name, state);
14387e72716SSean Bruno         }
1442e21a3efSScott Long 
1452e21a3efSScott Long 	sc->ld_disk = disk_alloc();
1462e21a3efSScott Long 	sc->ld_disk->d_drv1 = sc;
147d0e14c50SJohn Baldwin 	sc->ld_disk->d_maxsize = min(sc->ld_controller->mfi_max_io * secsize,
148d0e14c50SJohn Baldwin 	    (sc->ld_controller->mfi_max_sge - 1) * PAGE_SIZE);
1492e21a3efSScott Long 	sc->ld_disk->d_name = "mfid";
1502e21a3efSScott Long 	sc->ld_disk->d_open = mfi_disk_open;
1512e21a3efSScott Long 	sc->ld_disk->d_close = mfi_disk_close;
1522e21a3efSScott Long 	sc->ld_disk->d_strategy = mfi_disk_strategy;
1532e21a3efSScott Long 	sc->ld_disk->d_dump = mfi_disk_dump;
1542e21a3efSScott Long 	sc->ld_disk->d_unit = sc->ld_unit;
1552e21a3efSScott Long 	sc->ld_disk->d_sectorsize = secsize;
1562e21a3efSScott Long 	sc->ld_disk->d_mediasize = sectors * secsize;
1572e21a3efSScott Long 	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
1582e21a3efSScott Long 		sc->ld_disk->d_fwheads = 255;
1592e21a3efSScott Long 		sc->ld_disk->d_fwsectors = 63;
1602e21a3efSScott Long 	} else {
1612e21a3efSScott Long 		sc->ld_disk->d_fwheads = 64;
1622e21a3efSScott Long 		sc->ld_disk->d_fwsectors = 32;
1632e21a3efSScott Long 	}
1642e21a3efSScott Long 	disk_create(sc->ld_disk, DISK_VERSION);
1652e21a3efSScott Long 
1662e21a3efSScott Long 	return (0);
1672e21a3efSScott Long }
1682e21a3efSScott Long 
1692e21a3efSScott Long static int
1702e21a3efSScott Long mfi_disk_detach(device_t dev)
1712e21a3efSScott Long {
1722e21a3efSScott Long 	struct mfi_disk *sc;
1732e21a3efSScott Long 
1742e21a3efSScott Long 	sc = device_get_softc(dev);
1752e21a3efSScott Long 
1768ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
1778ec5c98bSJohn Baldwin 	if (((sc->ld_disk->d_flags & DISKFLAG_OPEN) ||
1788ec5c98bSJohn Baldwin 	    (sc->ld_flags & MFI_DISK_FLAGS_OPEN)) &&
1798ec5c98bSJohn Baldwin 	    (sc->ld_controller->mfi_keep_deleted_volumes ||
1808ec5c98bSJohn Baldwin 	    sc->ld_controller->mfi_detaching)) {
1818ec5c98bSJohn Baldwin 		mtx_unlock(&sc->ld_controller->mfi_io_lock);
1822e21a3efSScott Long 		return (EBUSY);
1838ec5c98bSJohn Baldwin 	}
1848ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1852e21a3efSScott Long 
1862e21a3efSScott Long 	disk_destroy(sc->ld_disk);
1878ec5c98bSJohn Baldwin 	mtx_lock(&sc->ld_controller->mfi_io_lock);
1888ec5c98bSJohn Baldwin 	TAILQ_REMOVE(&sc->ld_controller->mfi_ld_tqh, sc, ld_link);
1898ec5c98bSJohn Baldwin 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
1908ec5c98bSJohn Baldwin 	free(sc->ld_info, M_MFIBUF);
1912e21a3efSScott Long 	return (0);
1922e21a3efSScott Long }
1932e21a3efSScott Long 
1942e21a3efSScott Long static int
1952e21a3efSScott Long mfi_disk_open(struct disk *dp)
1962e21a3efSScott Long {
197ddfae47bSScott Long 	struct mfi_disk *sc;
1988ec5c98bSJohn Baldwin 	int error;
199ddfae47bSScott Long 
200ddfae47bSScott Long 	sc = dp->d_drv1;
201ddfae47bSScott Long 	mtx_lock(&sc->ld_controller->mfi_io_lock);
2028ec5c98bSJohn Baldwin 	if (sc->ld_flags & MFI_DISK_FLAGS_DISABLED)
2038ec5c98bSJohn Baldwin 		error = ENXIO;
2048ec5c98bSJohn Baldwin 	else {
205ddfae47bSScott Long 		sc->ld_flags |= MFI_DISK_FLAGS_OPEN;
2068ec5c98bSJohn Baldwin 		error = 0;
2078ec5c98bSJohn Baldwin 	}
208ddfae47bSScott Long 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2092e21a3efSScott Long 
2108ec5c98bSJohn Baldwin 	return (error);
2112e21a3efSScott Long }
2122e21a3efSScott Long 
2132e21a3efSScott Long static int
2142e21a3efSScott Long mfi_disk_close(struct disk *dp)
2152e21a3efSScott Long {
216ddfae47bSScott Long 	struct mfi_disk *sc;
217ddfae47bSScott Long 
218ddfae47bSScott Long 	sc = dp->d_drv1;
219ddfae47bSScott Long 	mtx_lock(&sc->ld_controller->mfi_io_lock);
220ddfae47bSScott Long 	sc->ld_flags &= ~MFI_DISK_FLAGS_OPEN;
221ddfae47bSScott Long 	mtx_unlock(&sc->ld_controller->mfi_io_lock);
2222e21a3efSScott Long 
2232e21a3efSScott Long 	return (0);
2242e21a3efSScott Long }
2252e21a3efSScott Long 
2268ec5c98bSJohn Baldwin int
2278ec5c98bSJohn Baldwin mfi_disk_disable(struct mfi_disk *sc)
2288ec5c98bSJohn Baldwin {
2298ec5c98bSJohn Baldwin 
2308ec5c98bSJohn Baldwin 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2318ec5c98bSJohn Baldwin 	if (sc->ld_flags & MFI_DISK_FLAGS_OPEN) {
2328ec5c98bSJohn Baldwin 		if (sc->ld_controller->mfi_delete_busy_volumes)
2338ec5c98bSJohn Baldwin 			return (0);
2340d9a4ef3SDoug Ambrisko 		device_printf(sc->ld_dev, "Unable to delete busy ld device\n");
2358ec5c98bSJohn Baldwin 		return (EBUSY);
2368ec5c98bSJohn Baldwin 	}
2378ec5c98bSJohn Baldwin 	sc->ld_flags |= MFI_DISK_FLAGS_DISABLED;
2388ec5c98bSJohn Baldwin 	return (0);
2398ec5c98bSJohn Baldwin }
2408ec5c98bSJohn Baldwin 
2418ec5c98bSJohn Baldwin void
2428ec5c98bSJohn Baldwin mfi_disk_enable(struct mfi_disk *sc)
2438ec5c98bSJohn Baldwin {
2448ec5c98bSJohn Baldwin 
2458ec5c98bSJohn Baldwin 	mtx_assert(&sc->ld_controller->mfi_io_lock, MA_OWNED);
2468ec5c98bSJohn Baldwin 	sc->ld_flags &= ~MFI_DISK_FLAGS_DISABLED;
2478ec5c98bSJohn Baldwin }
2488ec5c98bSJohn Baldwin 
2492e21a3efSScott Long static void
2502e21a3efSScott Long mfi_disk_strategy(struct bio *bio)
2512e21a3efSScott Long {
2522e21a3efSScott Long 	struct mfi_disk *sc;
2532e21a3efSScott Long 	struct mfi_softc *controller;
2542e21a3efSScott Long 
2552e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
2560d9a4ef3SDoug Ambrisko 	controller = sc->ld_controller;
2572e21a3efSScott Long 
2582e21a3efSScott Long 	if (sc == NULL) {
2592e21a3efSScott Long 		bio->bio_error = EINVAL;
2602e21a3efSScott Long 		bio->bio_flags |= BIO_ERROR;
2612e21a3efSScott Long 		bio->bio_resid = bio->bio_bcount;
2622e21a3efSScott Long 		biodone(bio);
2632e21a3efSScott Long 		return;
2642e21a3efSScott Long 	}
2652e21a3efSScott Long 
2660d9a4ef3SDoug Ambrisko 	if (controller->adpreset) {
2670d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2680d9a4ef3SDoug Ambrisko 		return;
2690d9a4ef3SDoug Ambrisko 	}
2700d9a4ef3SDoug Ambrisko 
2710d9a4ef3SDoug Ambrisko 	if (controller->hw_crit_error) {
2720d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2730d9a4ef3SDoug Ambrisko 		return;
2740d9a4ef3SDoug Ambrisko 	}
2750d9a4ef3SDoug Ambrisko 
2760d9a4ef3SDoug Ambrisko 	if (controller->issuepend_done == 0) {
2770d9a4ef3SDoug Ambrisko 		bio->bio_error = EBUSY;
2780d9a4ef3SDoug Ambrisko 		return;
2790d9a4ef3SDoug Ambrisko 	}
2800d9a4ef3SDoug Ambrisko 
2812e21a3efSScott Long 	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
2820d9a4ef3SDoug Ambrisko 	/* Mark it as LD IO */
2830d9a4ef3SDoug Ambrisko 	bio->bio_driver2 = (void *)MFI_LD_IO;
2842e21a3efSScott Long 	mtx_lock(&controller->mfi_io_lock);
2852e21a3efSScott Long 	mfi_enqueue_bio(controller, bio);
2862e21a3efSScott Long 	mfi_startio(controller);
2872e21a3efSScott Long 	mtx_unlock(&controller->mfi_io_lock);
2882e21a3efSScott Long 	return;
2892e21a3efSScott Long }
2902e21a3efSScott Long 
2912e21a3efSScott Long void
2922e21a3efSScott Long mfi_disk_complete(struct bio *bio)
2932e21a3efSScott Long {
2942e21a3efSScott Long 	struct mfi_disk *sc;
2952e21a3efSScott Long 	struct mfi_frame_header *hdr;
2962e21a3efSScott Long 
2972e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
2982e21a3efSScott Long 	hdr = bio->bio_driver1;
2992e21a3efSScott Long 
3002e21a3efSScott Long 	if (bio->bio_flags & BIO_ERROR) {
301*dfcbfdbbSSean Bruno 		bio->bio_resid = bio->bio_bcount;
3022e21a3efSScott Long 		if (bio->bio_error == 0)
3032e21a3efSScott Long 			bio->bio_error = EIO;
3042e21a3efSScott Long 		disk_err(bio, "hard error", -1, 1);
3052e21a3efSScott Long 	} else {
3062e21a3efSScott Long 		bio->bio_resid = 0;
3072e21a3efSScott Long 	}
3082e21a3efSScott Long 	biodone(bio);
3092e21a3efSScott Long }
3102e21a3efSScott Long 
3112e21a3efSScott Long static int
3122e21a3efSScott Long mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
3132e21a3efSScott Long {
3142e21a3efSScott Long 	struct mfi_disk *sc;
3152e21a3efSScott Long 	struct mfi_softc *parent_sc;
3162e21a3efSScott Long 	struct disk *dp;
3172e21a3efSScott Long 	int error;
3182e21a3efSScott Long 
3192e21a3efSScott Long 	dp = arg;
3202e21a3efSScott Long 	sc = dp->d_drv1;
3212e21a3efSScott Long 	parent_sc = sc->ld_controller;
3222e21a3efSScott Long 
3232e21a3efSScott Long 	if (len > 0) {
3242e21a3efSScott Long 		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
325c0b332d1SPaul Saab 		    MFI_SECTOR_LEN, virt, len)) != 0)
3262e21a3efSScott Long 			return (error);
3272e21a3efSScott Long 	} else {
3282e21a3efSScott Long 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
3292e21a3efSScott Long 	}
3302e21a3efSScott Long 
3312e21a3efSScott Long 	return (0);
3322e21a3efSScott Long }
333