xref: /freebsd/sys/dev/ips/ips_disk.c (revision aad970f1fee9a2a3e5a0f880be9b87c6193b3bd1)
12aedd662SScott Long /*-
2aad970f1SDavid E. O'Brien  * Written by: David Jeffery
32aedd662SScott Long  * Copyright (c) 2002 Adaptec Inc.
42aedd662SScott Long  * All rights reserved.
52aedd662SScott Long  *
62aedd662SScott Long  * Redistribution and use in source and binary forms, with or without
72aedd662SScott Long  * modification, are permitted provided that the following conditions
82aedd662SScott Long  * are met:
92aedd662SScott Long  * 1. Redistributions of source code must retain the above copyright
102aedd662SScott Long  *    notice, this list of conditions and the following disclaimer.
112aedd662SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
122aedd662SScott Long  *    notice, this list of conditions and the following disclaimer in the
132aedd662SScott Long  *    documentation and/or other materials provided with the distribution.
142aedd662SScott Long  *
152aedd662SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162aedd662SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172aedd662SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182aedd662SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192aedd662SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202aedd662SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212aedd662SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222aedd662SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232aedd662SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242aedd662SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252aedd662SScott Long  * SUCH DAMAGE.
262aedd662SScott Long  */
272aedd662SScott Long 
28aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
29aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
302aedd662SScott Long 
312aedd662SScott Long #include <dev/ips/ips.h>
322aedd662SScott Long #include <dev/ips/ips_disk.h>
332aedd662SScott Long #include <sys/stat.h>
342aedd662SScott Long 
352aedd662SScott Long static int ipsd_probe(device_t dev);
362aedd662SScott Long static int ipsd_attach(device_t dev);
372aedd662SScott Long static int ipsd_detach(device_t dev);
382aedd662SScott Long 
392aedd662SScott Long static disk_open_t ipsd_open;
402aedd662SScott Long static disk_close_t ipsd_close;
412aedd662SScott Long static disk_strategy_t ipsd_strategy;
422aedd662SScott Long 
432aedd662SScott Long static device_method_t ipsd_methods[] = {
442aedd662SScott Long 	DEVMETHOD(device_probe,		ipsd_probe),
452aedd662SScott Long 	DEVMETHOD(device_attach,	ipsd_attach),
462aedd662SScott Long 	DEVMETHOD(device_detach,	ipsd_detach),
472aedd662SScott Long 	{ 0, 0 }
482aedd662SScott Long };
492aedd662SScott Long 
502aedd662SScott Long 
512aedd662SScott Long static driver_t ipsd_driver = {
522aedd662SScott Long 	"ipsd",
532aedd662SScott Long 	ipsd_methods,
542aedd662SScott Long 	sizeof(ipsdisk_softc_t)
552aedd662SScott Long };
562aedd662SScott Long 
572aedd662SScott Long static devclass_t ipsd_devclass;
582aedd662SScott Long DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
592aedd662SScott Long 
602aedd662SScott Long /* handle opening of disk device.  It must set up all
612aedd662SScott Long    information about the geometry and size of the disk */
622aedd662SScott Long static int ipsd_open(struct disk *dp)
632aedd662SScott Long {
642aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
652aedd662SScott Long 
662aedd662SScott Long 	dsc->state |= IPS_DEV_OPEN;
672aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
682aedd662SScott Long        	return 0;
692aedd662SScott Long }
702aedd662SScott Long 
712aedd662SScott Long static int ipsd_close(struct disk *dp)
722aedd662SScott Long {
732aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
742aedd662SScott Long 	dsc->state &= ~IPS_DEV_OPEN;
752aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
762aedd662SScott Long         return 0;
772aedd662SScott Long }
782aedd662SScott Long 
792aedd662SScott Long /* ipsd_finish is called to clean up and return a completed IO request */
802aedd662SScott Long void ipsd_finish(struct bio *iobuf)
812aedd662SScott Long {
822aedd662SScott Long 	if (iobuf->bio_flags & BIO_ERROR) {
832aedd662SScott Long 		ipsdisk_softc_t *dsc;
842aedd662SScott Long 		dsc = iobuf->bio_disk->d_drv1;
852aedd662SScott Long 		device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
862aedd662SScott Long 	} else
872aedd662SScott Long 		iobuf->bio_resid = 0;
882aedd662SScott Long 
892aedd662SScott Long 	biodone(iobuf);
902aedd662SScott Long }
912aedd662SScott Long 
922aedd662SScott Long 
932aedd662SScott Long static void ipsd_strategy(struct bio *iobuf)
942aedd662SScott Long {
952aedd662SScott Long 	ipsdisk_softc_t *dsc;
962aedd662SScott Long 
972aedd662SScott Long 	dsc = iobuf->bio_disk->d_drv1;
982aedd662SScott Long 	DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
99f472527cSPeter Wemm 	iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
1002aedd662SScott Long 	ips_start_io_request(dsc->sc, iobuf);
1012aedd662SScott Long }
1022aedd662SScott Long 
1032aedd662SScott Long static int ipsd_probe(device_t dev)
1042aedd662SScott Long {
1052aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in probe\n");
1062aedd662SScott Long 	device_set_desc(dev, "Logical Drive");
1072aedd662SScott Long 	return 0;
1082aedd662SScott Long }
1092aedd662SScott Long 
1102aedd662SScott Long static int ipsd_attach(device_t dev)
1112aedd662SScott Long {
1122aedd662SScott Long 	device_t adapter;
1132aedd662SScott Long 	ipsdisk_softc_t *dsc;
1142aedd662SScott Long 	u_int totalsectors;
1152aedd662SScott Long 
1162aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in attach\n");
1172aedd662SScott Long 
1182aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1192aedd662SScott Long 	bzero(dsc, sizeof(ipsdisk_softc_t));
1202aedd662SScott Long 	adapter = device_get_parent(dev);
1212aedd662SScott Long 	dsc->dev = dev;
1222aedd662SScott Long 	dsc->sc = device_get_softc(adapter);
1232aedd662SScott Long 	dsc->unit = device_get_unit(dev);
124f472527cSPeter Wemm 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
1252aedd662SScott Long 	dsc->ipsd_disk.d_drv1 = dsc;
1262aedd662SScott Long 	dsc->ipsd_disk.d_name = "ipsd";
1272aedd662SScott Long 	dsc->ipsd_disk.d_maxsize = IPS_MAX_IO_SIZE;
1282aedd662SScott Long 	dsc->ipsd_disk.d_open = ipsd_open;
1292aedd662SScott Long 	dsc->ipsd_disk.d_close = ipsd_close;
1302aedd662SScott Long 	dsc->ipsd_disk.d_strategy = ipsd_strategy;
1312aedd662SScott Long 
1322aedd662SScott Long 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
1332aedd662SScott Long    	if ((totalsectors > 0x400000) &&
1342aedd662SScott Long        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
1352aedd662SScott Long       		dsc->ipsd_disk.d_fwheads = IPS_NORM_HEADS;
1362aedd662SScott Long       		dsc->ipsd_disk.d_fwsectors = IPS_NORM_SECTORS;
1372aedd662SScott Long    	} else {
1382aedd662SScott Long       		dsc->ipsd_disk.d_fwheads = IPS_COMP_HEADS;
1392aedd662SScott Long       		dsc->ipsd_disk.d_fwsectors = IPS_COMP_SECTORS;
1402aedd662SScott Long    	}
1412aedd662SScott Long 	dsc->ipsd_disk.d_sectorsize = IPS_BLKSIZE;
1422aedd662SScott Long 	dsc->ipsd_disk.d_mediasize = totalsectors * IPS_BLKSIZE;
1432aedd662SScott Long 	disk_create(dsc->unit, &dsc->ipsd_disk, 0, NULL, NULL);
1442aedd662SScott Long 
1452aedd662SScott Long 	device_printf(dev, "Logical Drive  (%dMB)\n",
1462aedd662SScott Long 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
1472aedd662SScott Long 	return 0;
1482aedd662SScott Long }
1492aedd662SScott Long 
1502aedd662SScott Long static int ipsd_detach(device_t dev)
1512aedd662SScott Long {
1522aedd662SScott Long 	ipsdisk_softc_t *dsc;
1532aedd662SScott Long 
1542aedd662SScott Long 	DEVICE_PRINTF(2, dev,"in detach\n");
1552aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1562aedd662SScott Long 	if(dsc->state & IPS_DEV_OPEN)
1572aedd662SScott Long 		return (EBUSY);
1582aedd662SScott Long 	disk_destroy(&dsc->ipsd_disk);
1592aedd662SScott Long 	return 0;
1602aedd662SScott Long }
1612aedd662SScott Long 
162