xref: /freebsd/sys/dev/ips/ips_disk.c (revision 2eea70515c189e5c7c5457efd3e875ed395d736b)
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 
397765040eSScott Long static int ipsd_dump(void *arg, void *virtual, vm_offset_t physical,
407765040eSScott Long 		     off_t offset, size_t length);
417765040eSScott Long static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
427765040eSScott Long 			     int error);
437765040eSScott Long static void ipsd_dump_block_complete(ips_command_t *command);
447765040eSScott Long 
452aedd662SScott Long static disk_open_t ipsd_open;
462aedd662SScott Long static disk_close_t ipsd_close;
472aedd662SScott Long static disk_strategy_t ipsd_strategy;
482aedd662SScott Long 
492aedd662SScott Long static device_method_t ipsd_methods[] = {
502aedd662SScott Long 	DEVMETHOD(device_probe,		ipsd_probe),
512aedd662SScott Long 	DEVMETHOD(device_attach,	ipsd_attach),
522aedd662SScott Long 	DEVMETHOD(device_detach,	ipsd_detach),
532aedd662SScott Long 	{ 0, 0 }
542aedd662SScott Long };
552aedd662SScott Long 
562aedd662SScott Long static driver_t ipsd_driver = {
572aedd662SScott Long 	"ipsd",
582aedd662SScott Long 	ipsd_methods,
592aedd662SScott Long 	sizeof(ipsdisk_softc_t)
602aedd662SScott Long };
612aedd662SScott Long 
622aedd662SScott Long static devclass_t ipsd_devclass;
632aedd662SScott Long DRIVER_MODULE(ipsd, ips, ipsd_driver, ipsd_devclass, 0, 0);
642aedd662SScott Long 
652aedd662SScott Long /* handle opening of disk device.  It must set up all
662aedd662SScott Long    information about the geometry and size of the disk */
672aedd662SScott Long static int ipsd_open(struct disk *dp)
682aedd662SScott Long {
692aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
702aedd662SScott Long 
712aedd662SScott Long 	dsc->state |= IPS_DEV_OPEN;
722aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
732aedd662SScott Long        	return 0;
742aedd662SScott Long }
752aedd662SScott Long 
762aedd662SScott Long static int ipsd_close(struct disk *dp)
772aedd662SScott Long {
782aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
792aedd662SScott Long 	dsc->state &= ~IPS_DEV_OPEN;
802aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
812aedd662SScott Long         return 0;
822aedd662SScott Long }
832aedd662SScott Long 
842aedd662SScott Long /* ipsd_finish is called to clean up and return a completed IO request */
852aedd662SScott Long void ipsd_finish(struct bio *iobuf)
862aedd662SScott Long {
87b234a120SScott Long 	ipsdisk_softc_t *dsc;
88b234a120SScott Long 	dsc = iobuf->bio_disk->d_drv1;
89b234a120SScott Long 
902aedd662SScott Long 	if (iobuf->bio_flags & BIO_ERROR) {
912aedd662SScott Long 		ipsdisk_softc_t *dsc;
922aedd662SScott Long 		dsc = iobuf->bio_disk->d_drv1;
932aedd662SScott Long 		device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
942aedd662SScott Long 	} else
952aedd662SScott Long 		iobuf->bio_resid = 0;
962aedd662SScott Long 
972aedd662SScott Long 	biodone(iobuf);
98b234a120SScott Long 	ips_start_io_request(dsc->sc);
992aedd662SScott Long }
1002aedd662SScott Long 
1012aedd662SScott Long 
1022aedd662SScott Long static void ipsd_strategy(struct bio *iobuf)
1032aedd662SScott Long {
1042aedd662SScott Long 	ipsdisk_softc_t *dsc;
1052aedd662SScott Long 
1062aedd662SScott Long 	dsc = iobuf->bio_disk->d_drv1;
1072aedd662SScott Long 	DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
108f472527cSPeter Wemm 	iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
109b234a120SScott Long 	mtx_lock(&dsc->sc->queue_mtx);
11003a908f2SScott Long 	bioq_insert_tail(&dsc->sc->queue, iobuf);
111b234a120SScott Long 	ips_start_io_request(dsc->sc);
11203a908f2SScott Long 	mtx_unlock(&dsc->sc->queue_mtx);
1132aedd662SScott Long }
1142aedd662SScott Long 
1152aedd662SScott Long static int ipsd_probe(device_t dev)
1162aedd662SScott Long {
1172aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in probe\n");
1182aedd662SScott Long 	device_set_desc(dev, "Logical Drive");
1192aedd662SScott Long 	return 0;
1202aedd662SScott Long }
1212aedd662SScott Long 
1222aedd662SScott Long static int ipsd_attach(device_t dev)
1232aedd662SScott Long {
1242aedd662SScott Long 	device_t adapter;
1252aedd662SScott Long 	ipsdisk_softc_t *dsc;
1262aedd662SScott Long 	u_int totalsectors;
1272aedd662SScott Long 
1282aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in attach\n");
1292aedd662SScott Long 
1302aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1312aedd662SScott Long 	bzero(dsc, sizeof(ipsdisk_softc_t));
1322aedd662SScott Long 	adapter = device_get_parent(dev);
1332aedd662SScott Long 	dsc->dev = dev;
1342aedd662SScott Long 	dsc->sc = device_get_softc(adapter);
1352aedd662SScott Long 	dsc->unit = device_get_unit(dev);
136f472527cSPeter Wemm 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
1370b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk = disk_alloc();
1380b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_drv1 = dsc;
1390b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_name = "ipsd";
1400b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
1410b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_open = ipsd_open;
1420b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_close = ipsd_close;
1430b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_strategy = ipsd_strategy;
1447765040eSScott Long 	dsc->ipsd_disk->d_dump = ipsd_dump;
1452aedd662SScott Long 
1462aedd662SScott Long 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
1472aedd662SScott Long    	if ((totalsectors > 0x400000) &&
1482aedd662SScott Long        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
1490b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
1500b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
1512aedd662SScott Long    	} else {
1520b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
1530b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
1542aedd662SScott Long    	}
1550b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
1560b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
1570b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_unit = dsc->unit;
15803a908f2SScott Long 	dsc->ipsd_disk->d_flags = 0;
1590b7ed341SPoul-Henning Kamp 	disk_create(dsc->ipsd_disk, DISK_VERSION);
1602aedd662SScott Long 
1612aedd662SScott Long 	device_printf(dev, "Logical Drive  (%dMB)\n",
1622aedd662SScott Long 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
1632aedd662SScott Long 	return 0;
1642aedd662SScott Long }
1652aedd662SScott Long 
1662aedd662SScott Long static int ipsd_detach(device_t dev)
1672aedd662SScott Long {
1682aedd662SScott Long 	ipsdisk_softc_t *dsc;
1692aedd662SScott Long 
1702aedd662SScott Long 	DEVICE_PRINTF(2, dev,"in detach\n");
1712aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1722aedd662SScott Long 	if(dsc->state & IPS_DEV_OPEN)
1732aedd662SScott Long 		return (EBUSY);
1740b7ed341SPoul-Henning Kamp 	disk_destroy(dsc->ipsd_disk);
1752aedd662SScott Long 	return 0;
1762aedd662SScott Long }
1777765040eSScott Long 
1787765040eSScott Long static int
1797765040eSScott Long ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
1807765040eSScott Long 	  size_t length)
1817765040eSScott Long {
1827765040eSScott Long 	ipsdisk_softc_t *dsc;
1837765040eSScott Long 	ips_softc_t *sc;
1847765040eSScott Long 	ips_command_t *command;
1857765040eSScott Long 	ips_io_cmd *command_struct;
1867765040eSScott Long 	struct disk *dp;
1877765040eSScott Long 	void *va;
1887765040eSScott Long 	off_t off;
1897765040eSScott Long 	size_t len;
1907765040eSScott Long 	int error = 0;
1917765040eSScott Long 
1927765040eSScott Long 	dp = arg;
1937765040eSScott Long 	dsc = dp->d_drv1;
1947765040eSScott Long 	sc = dsc->sc;
1957765040eSScott Long 
1967765040eSScott Long 	if (dsc == NULL)
1977765040eSScott Long 		return (EINVAL);
1987765040eSScott Long 
1997765040eSScott Long 	if (ips_get_free_cmd(sc, &command, 0) != 0) {
2007765040eSScott Long 		printf("ipsd: failed to get cmd for dump\n");
2017765040eSScott Long 		return (ENOMEM);
2027765040eSScott Long 	}
2037765040eSScott Long 
2047765040eSScott Long 	command->data_dmatag = sc->sg_dmatag;
2057765040eSScott Long 	command->callback = ipsd_dump_block_complete;
2067765040eSScott Long 
2077765040eSScott Long 	command_struct = (ips_io_cmd *)command->command_buffer;
2087765040eSScott Long 	command_struct->id = command->id;
2097765040eSScott Long 	command_struct->drivenum= sc->drives[dsc->disk_number].drivenum;
2107765040eSScott Long 
2117765040eSScott Long 	off = offset;
2127765040eSScott Long 	va = virtual;
2137765040eSScott Long 
2147765040eSScott Long 	while (length > 0) {
2157765040eSScott Long 		len =
2167765040eSScott Long 		    (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length;
2177765040eSScott Long 
2187765040eSScott Long 		command_struct->lba = off / IPS_BLKSIZE;
2197765040eSScott Long 
2207765040eSScott Long 		if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
2217765040eSScott Long 		    va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) {
2227765040eSScott Long 			error = EIO;
2237765040eSScott Long 			break;
2247765040eSScott Long 		}
2252eea7051SScott Long 		if (COMMAND_ERROR(command)) {
2267765040eSScott Long 			error = EIO;
2277765040eSScott Long 			break;
2287765040eSScott Long 		}
2297765040eSScott Long 
2307765040eSScott Long 		length -= len;
2317765040eSScott Long 		off += len;
2327765040eSScott Long 		va = (uint8_t *)va + len;
2337765040eSScott Long 	}
2347765040eSScott Long 
2357765040eSScott Long 	ips_insert_free_cmd(command->sc, command);
2367765040eSScott Long 	return (error);
2377765040eSScott Long }
2387765040eSScott Long 
2397765040eSScott Long static void
2407765040eSScott Long ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2417765040eSScott Long {
2427765040eSScott Long 	ips_softc_t *sc;
2437765040eSScott Long 	ips_command_t *command;
2447765040eSScott Long 	ips_sg_element_t *sg_list;
2457765040eSScott Long 	ips_io_cmd *command_struct;
2467765040eSScott Long 	int i, length;
2477765040eSScott Long 
2487765040eSScott Long 	command = (ips_command_t *)arg;
2497765040eSScott Long 	sc = command->sc;
2507765040eSScott Long 	length = 0;
2517765040eSScott Long 
2527765040eSScott Long 	if (error) {
2537765040eSScott Long 		printf("ipsd_dump_map_sg: error %d\n", error);
2542eea7051SScott Long 		ips_set_error(command, error);
2557765040eSScott Long 		return;
2567765040eSScott Long 	}
2577765040eSScott Long 
2587765040eSScott Long 	command_struct = (ips_io_cmd *)command->command_buffer;
2597765040eSScott Long 
2607765040eSScott Long 	if (nsegs != 1) {
2617765040eSScott Long 		command_struct->segnum = nsegs;
2627765040eSScott Long 		sg_list = (ips_sg_element_t *)((uint8_t *)
2637765040eSScott Long 		    command->command_buffer + IPS_COMMAND_LEN);
2647765040eSScott Long 		for (i = 0; i < nsegs; i++) {
2657765040eSScott Long 			sg_list[i].addr = segs[i].ds_addr;
2667765040eSScott Long 			sg_list[i].len = segs[i].ds_len;
2677765040eSScott Long 			length += segs[i].ds_len;
2687765040eSScott Long 		}
2697765040eSScott Long 		command_struct->buffaddr =
2707765040eSScott Long 		    (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
2717765040eSScott Long 		command_struct->command = IPS_SG_WRITE_CMD;
2727765040eSScott Long 	} else {
2737765040eSScott Long 		command_struct->buffaddr = segs[0].ds_addr;
2747765040eSScott Long 		length = segs[0].ds_len;
2757765040eSScott Long 		command_struct->segnum = 0;
2767765040eSScott Long 		command_struct->command = IPS_WRITE_CMD;
2777765040eSScott Long 	}
2787765040eSScott Long 
2797765040eSScott Long 	length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
2807765040eSScott Long 	command_struct->length = length;
2817765040eSScott Long 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
2827765040eSScott Long 	    BUS_DMASYNC_PREWRITE);
2837765040eSScott Long 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
2847765040eSScott Long 	    BUS_DMASYNC_PREWRITE);
2857765040eSScott Long 
2867765040eSScott Long 	sc->ips_issue_cmd(command);
2877765040eSScott Long 	sc->ips_poll_cmd(command);
2887765040eSScott Long 	return;
2897765040eSScott Long }
2907765040eSScott Long 
2917765040eSScott Long static void
2927765040eSScott Long ipsd_dump_block_complete(ips_command_t *command)
2937765040eSScott Long {
2947765040eSScott Long 
2952eea7051SScott Long 	if (COMMAND_ERROR(command))
2967765040eSScott Long 		printf("ipsd_dump completion error= 0x%x\n",
2977765040eSScott Long 		    command->status.value);
2987765040eSScott Long 
2997765040eSScott Long 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
3007765040eSScott Long 	    BUS_DMASYNC_POSTWRITE);
3017765040eSScott Long 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
3027765040eSScott Long }
303