xref: /freebsd/sys/dev/mfi/mfi_disk.c (revision 441f6d5dcac296a4e74d72fa43233f6f80cd5eea)
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>
38741367d5SDoug Ambrisko #include <sys/uio.h>
392e21a3efSScott Long 
402e21a3efSScott Long #include <sys/bio.h>
412e21a3efSScott Long #include <sys/bus.h>
422e21a3efSScott Long #include <sys/conf.h>
432e21a3efSScott Long #include <sys/disk.h>
442e21a3efSScott Long #include <geom/geom_disk.h>
452e21a3efSScott Long 
462e21a3efSScott Long #include <vm/vm.h>
472e21a3efSScott Long #include <vm/pmap.h>
482e21a3efSScott Long 
492e21a3efSScott Long #include <machine/md_var.h>
502e21a3efSScott Long #include <machine/bus.h>
512e21a3efSScott Long #include <sys/rman.h>
522e21a3efSScott Long 
532e21a3efSScott Long #include <dev/mfi/mfireg.h>
542e21a3efSScott Long #include <dev/mfi/mfi_ioctl.h>
552e21a3efSScott Long #include <dev/mfi/mfivar.h>
562e21a3efSScott Long 
572e21a3efSScott Long static int	mfi_disk_probe(device_t dev);
582e21a3efSScott Long static int	mfi_disk_attach(device_t dev);
592e21a3efSScott Long static int	mfi_disk_detach(device_t dev);
602e21a3efSScott Long 
612e21a3efSScott Long static disk_open_t	mfi_disk_open;
622e21a3efSScott Long static disk_close_t	mfi_disk_close;
632e21a3efSScott Long static disk_strategy_t	mfi_disk_strategy;
642e21a3efSScott Long static dumper_t		mfi_disk_dump;
652e21a3efSScott Long 
662e21a3efSScott Long static devclass_t	mfi_disk_devclass;
672e21a3efSScott Long 
682e21a3efSScott Long struct mfi_disk {
692e21a3efSScott Long 	device_t	ld_dev;
702e21a3efSScott Long 	int		ld_id;
712e21a3efSScott Long 	int		ld_unit;
722e21a3efSScott Long 	struct mfi_softc *ld_controller;
732e21a3efSScott Long 	struct mfi_ld	*ld_ld;
742e21a3efSScott Long 	struct disk	*ld_disk;
752e21a3efSScott Long };
762e21a3efSScott Long 
772e21a3efSScott Long static device_method_t mfi_disk_methods[] = {
782e21a3efSScott Long 	DEVMETHOD(device_probe,		mfi_disk_probe),
792e21a3efSScott Long 	DEVMETHOD(device_attach,	mfi_disk_attach),
802e21a3efSScott Long 	DEVMETHOD(device_detach,	mfi_disk_detach),
812e21a3efSScott Long 	{ 0, 0 }
822e21a3efSScott Long };
832e21a3efSScott Long 
842e21a3efSScott Long static driver_t mfi_disk_driver = {
852e21a3efSScott Long 	"mfid",
862e21a3efSScott Long 	mfi_disk_methods,
872e21a3efSScott Long 	sizeof(struct mfi_disk)
882e21a3efSScott Long };
892e21a3efSScott Long 
902e21a3efSScott Long DRIVER_MODULE(mfid, mfi, mfi_disk_driver, mfi_disk_devclass, 0, 0);
912e21a3efSScott Long 
922e21a3efSScott Long static int
932e21a3efSScott Long mfi_disk_probe(device_t dev)
942e21a3efSScott Long {
952e21a3efSScott Long 
962e21a3efSScott Long 	return (0);
972e21a3efSScott Long }
982e21a3efSScott Long 
992e21a3efSScott Long static int
1002e21a3efSScott Long mfi_disk_attach(device_t dev)
1012e21a3efSScott Long {
1022e21a3efSScott Long 	struct mfi_disk *sc;
1032e21a3efSScott Long 	struct mfi_ld *ld;
1042e21a3efSScott Long 	uint64_t sectors;
1052e21a3efSScott Long 	uint32_t secsize;
106441f6d5dSScott Long 	char *state;
1072e21a3efSScott Long 
1082e21a3efSScott Long 	sc = device_get_softc(dev);
1092e21a3efSScott Long 	ld = device_get_ivars(dev);
1102e21a3efSScott Long 
1112e21a3efSScott Long 	sc->ld_dev = dev;
1122e21a3efSScott Long 	sc->ld_id = ld->ld_id;
1132e21a3efSScott Long 	sc->ld_unit = device_get_unit(dev);
1142e21a3efSScott Long 	sc->ld_ld = device_get_ivars(dev);
1152e21a3efSScott Long 	sc->ld_controller = device_get_softc(device_get_parent(dev));
1162e21a3efSScott Long 
117c0b332d1SPaul Saab 	sectors = ld->ld_info->size;
118c0b332d1SPaul Saab 	secsize = MFI_SECTOR_LEN;
11920df0de4SScott Long 	TAILQ_INSERT_TAIL(&sc->ld_controller->mfi_ld_tqh, ld, ld_link);
1202e21a3efSScott Long 
121441f6d5dSScott Long 	switch (ld->ld_info->ld_config.params.state) {
122441f6d5dSScott Long 	case MFI_LD_STATE_OFFLINE:
123441f6d5dSScott Long 		state = "offline";
124441f6d5dSScott Long 		break;
125441f6d5dSScott Long 	case MFI_LD_STATE_PARTIALLY_DEGRADED:
126441f6d5dSScott Long 		state = "partially degraded";
127441f6d5dSScott Long 		break;
128441f6d5dSScott Long 	case MFI_LD_STATE_DEGRADED:
129441f6d5dSScott Long 		state = "degraded";
130441f6d5dSScott Long 		break;
131441f6d5dSScott Long 	case MFI_LD_STATE_OPTIMAL:
132441f6d5dSScott Long 		state = "optimal";
133441f6d5dSScott Long 		break;
134441f6d5dSScott Long 	default:
135441f6d5dSScott Long 		state = "unknown";
136441f6d5dSScott Long 		break;
137441f6d5dSScott Long 	}
138441f6d5dSScott Long 	device_printf(dev, "%juMB (%ju sectors) RAID volume '%s' is %s\n",
139441f6d5dSScott Long 		      sectors / (1024 * 1024 / secsize), sectors,
140441f6d5dSScott Long 		      ld->ld_info->ld_config.properties.name,
141441f6d5dSScott Long 		      state);
1422e21a3efSScott Long 
1432e21a3efSScott Long 	sc->ld_disk = disk_alloc();
1442e21a3efSScott Long 	sc->ld_disk->d_drv1 = sc;
1452e21a3efSScott Long 	sc->ld_disk->d_maxsize = sc->ld_controller->mfi_max_io * secsize;
1462e21a3efSScott Long 	sc->ld_disk->d_name = "mfid";
1472e21a3efSScott Long 	sc->ld_disk->d_open = mfi_disk_open;
1482e21a3efSScott Long 	sc->ld_disk->d_close = mfi_disk_close;
1492e21a3efSScott Long 	sc->ld_disk->d_strategy = mfi_disk_strategy;
1502e21a3efSScott Long 	sc->ld_disk->d_dump = mfi_disk_dump;
1512e21a3efSScott Long 	sc->ld_disk->d_unit = sc->ld_unit;
1522e21a3efSScott Long 	sc->ld_disk->d_sectorsize = secsize;
1532e21a3efSScott Long 	sc->ld_disk->d_mediasize = sectors * secsize;
1542e21a3efSScott Long 	if (sc->ld_disk->d_mediasize >= (1 * 1024 * 1024)) {
1552e21a3efSScott Long 		sc->ld_disk->d_fwheads = 255;
1562e21a3efSScott Long 		sc->ld_disk->d_fwsectors = 63;
1572e21a3efSScott Long 	} else {
1582e21a3efSScott Long 		sc->ld_disk->d_fwheads = 64;
1592e21a3efSScott Long 		sc->ld_disk->d_fwsectors = 32;
1602e21a3efSScott Long 	}
1612e21a3efSScott Long 	disk_create(sc->ld_disk, DISK_VERSION);
1622e21a3efSScott Long 
1632e21a3efSScott Long 	return (0);
1642e21a3efSScott Long }
1652e21a3efSScott Long 
1662e21a3efSScott Long static int
1672e21a3efSScott Long mfi_disk_detach(device_t dev)
1682e21a3efSScott Long {
1692e21a3efSScott Long 	struct mfi_disk *sc;
1702e21a3efSScott Long 
1712e21a3efSScott Long 	sc = device_get_softc(dev);
1722e21a3efSScott Long 
1732e21a3efSScott Long 	if (sc->ld_disk->d_flags & DISKFLAG_OPEN)
1742e21a3efSScott Long 		return (EBUSY);
1752e21a3efSScott Long 
1762e21a3efSScott Long 	disk_destroy(sc->ld_disk);
1772e21a3efSScott Long 	return (0);
1782e21a3efSScott Long }
1792e21a3efSScott Long 
1802e21a3efSScott Long static int
1812e21a3efSScott Long mfi_disk_open(struct disk *dp)
1822e21a3efSScott Long {
1832e21a3efSScott Long 
1842e21a3efSScott Long 	return (0);
1852e21a3efSScott Long }
1862e21a3efSScott Long 
1872e21a3efSScott Long static int
1882e21a3efSScott Long mfi_disk_close(struct disk *dp)
1892e21a3efSScott Long {
1902e21a3efSScott Long 
1912e21a3efSScott Long 	return (0);
1922e21a3efSScott Long }
1932e21a3efSScott Long 
1942e21a3efSScott Long static void
1952e21a3efSScott Long mfi_disk_strategy(struct bio *bio)
1962e21a3efSScott Long {
1972e21a3efSScott Long 	struct mfi_disk *sc;
1982e21a3efSScott Long 	struct mfi_softc *controller;
1992e21a3efSScott Long 
2002e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
2012e21a3efSScott Long 
2022e21a3efSScott Long 	if (sc == NULL) {
2032e21a3efSScott Long 		bio->bio_error = EINVAL;
2042e21a3efSScott Long 		bio->bio_flags |= BIO_ERROR;
2052e21a3efSScott Long 		bio->bio_resid = bio->bio_bcount;
2062e21a3efSScott Long 		biodone(bio);
2072e21a3efSScott Long 		return;
2082e21a3efSScott Long 	}
2092e21a3efSScott Long 
2102e21a3efSScott Long 	controller = sc->ld_controller;
2112e21a3efSScott Long 	bio->bio_driver1 = (void *)(uintptr_t)sc->ld_id;
2122e21a3efSScott Long 	mtx_lock(&controller->mfi_io_lock);
2132e21a3efSScott Long 	mfi_enqueue_bio(controller, bio);
2142e21a3efSScott Long 	mfi_startio(controller);
2152e21a3efSScott Long 	mtx_unlock(&controller->mfi_io_lock);
2162e21a3efSScott Long 	return;
2172e21a3efSScott Long }
2182e21a3efSScott Long 
2192e21a3efSScott Long void
2202e21a3efSScott Long mfi_disk_complete(struct bio *bio)
2212e21a3efSScott Long {
2222e21a3efSScott Long 	struct mfi_disk *sc;
2232e21a3efSScott Long 	struct mfi_frame_header *hdr;
2242e21a3efSScott Long 
2252e21a3efSScott Long 	sc = bio->bio_disk->d_drv1;
2262e21a3efSScott Long 	hdr = bio->bio_driver1;
2272e21a3efSScott Long 
2282e21a3efSScott Long 	if (bio->bio_flags & BIO_ERROR) {
2292e21a3efSScott Long 		if (bio->bio_error == 0)
2302e21a3efSScott Long 			bio->bio_error = EIO;
2312e21a3efSScott Long 		disk_err(bio, "hard error", -1, 1);
2322e21a3efSScott Long 	} else {
2332e21a3efSScott Long 		bio->bio_resid = 0;
2342e21a3efSScott Long 	}
2352e21a3efSScott Long 	biodone(bio);
2362e21a3efSScott Long }
2372e21a3efSScott Long 
2382e21a3efSScott Long static int
2392e21a3efSScott Long mfi_disk_dump(void *arg, void *virt, vm_offset_t phys, off_t offset, size_t len)
2402e21a3efSScott Long {
2412e21a3efSScott Long 	struct mfi_disk *sc;
2422e21a3efSScott Long 	struct mfi_softc *parent_sc;
2432e21a3efSScott Long 	struct disk *dp;
2442e21a3efSScott Long 	int error;
2452e21a3efSScott Long 
2462e21a3efSScott Long 	dp = arg;
2472e21a3efSScott Long 	sc = dp->d_drv1;
2482e21a3efSScott Long 	parent_sc = sc->ld_controller;
2492e21a3efSScott Long 
2502e21a3efSScott Long 	if (len > 0) {
2512e21a3efSScott Long 		if ((error = mfi_dump_blocks(parent_sc, sc->ld_id, offset /
252c0b332d1SPaul Saab 		    MFI_SECTOR_LEN, virt, len)) != 0)
2532e21a3efSScott Long 			return (error);
2542e21a3efSScott Long 	} else {
2552e21a3efSScott Long 		/* mfi_sync_cache(parent_sc, sc->ld_id); */
2562e21a3efSScott Long 	}
2572e21a3efSScott Long 
2582e21a3efSScott Long 	return (0);
2592e21a3efSScott Long }
260