xref: /freebsd/sys/dev/ips/ips_disk.c (revision 489ba2223676ec251ab1bfe2906d2a62959c8ce3)
12aedd662SScott Long /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4aad970f1SDavid E. O'Brien  * Written by: David Jeffery
52aedd662SScott Long  * Copyright (c) 2002 Adaptec Inc.
62aedd662SScott Long  * All rights reserved.
72aedd662SScott Long  *
82aedd662SScott Long  * Redistribution and use in source and binary forms, with or without
92aedd662SScott Long  * modification, are permitted provided that the following conditions
102aedd662SScott Long  * are met:
112aedd662SScott Long  * 1. Redistributions of source code must retain the above copyright
122aedd662SScott Long  *    notice, this list of conditions and the following disclaimer.
132aedd662SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
142aedd662SScott Long  *    notice, this list of conditions and the following disclaimer in the
152aedd662SScott Long  *    documentation and/or other materials provided with the distribution.
162aedd662SScott Long  *
172aedd662SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
182aedd662SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192aedd662SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202aedd662SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
212aedd662SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222aedd662SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232aedd662SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242aedd662SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252aedd662SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262aedd662SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272aedd662SScott Long  * SUCH DAMAGE.
282aedd662SScott Long  */
292aedd662SScott Long 
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
322aedd662SScott Long 
33f94dfeb4SScott Long #include <dev/ips/ipsreg.h>
342aedd662SScott Long #include <dev/ips/ips.h>
352aedd662SScott Long #include <dev/ips/ips_disk.h>
362aedd662SScott Long #include <sys/stat.h>
372aedd662SScott Long 
382aedd662SScott Long static int ipsd_probe(device_t dev);
392aedd662SScott Long static int ipsd_attach(device_t dev);
402aedd662SScott Long static int ipsd_detach(device_t dev);
412aedd662SScott Long 
42*489ba222SMitchell Horne static int ipsd_dump(void *arg, void *virtual, off_t offset, size_t length);
437765040eSScott Long static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
447765040eSScott Long 			     int error);
457765040eSScott Long static void ipsd_dump_block_complete(ips_command_t *command);
467765040eSScott Long 
472aedd662SScott Long static disk_open_t ipsd_open;
482aedd662SScott Long static disk_close_t ipsd_close;
492aedd662SScott Long static disk_strategy_t ipsd_strategy;
502aedd662SScott Long 
512aedd662SScott Long static device_method_t ipsd_methods[] = {
522aedd662SScott Long 	DEVMETHOD(device_probe,		ipsd_probe),
532aedd662SScott Long 	DEVMETHOD(device_attach,	ipsd_attach),
542aedd662SScott Long 	DEVMETHOD(device_detach,	ipsd_detach),
552aedd662SScott Long 	{ 0, 0 }
562aedd662SScott Long };
572aedd662SScott Long 
582aedd662SScott Long static driver_t ipsd_driver = {
592aedd662SScott Long 	"ipsd",
602aedd662SScott Long 	ipsd_methods,
612aedd662SScott Long 	sizeof(ipsdisk_softc_t)
622aedd662SScott Long };
632aedd662SScott Long 
643144501aSJohn Baldwin DRIVER_MODULE(ipsd, ips, ipsd_driver, 0, 0);
652aedd662SScott Long 
662aedd662SScott Long /* handle opening of disk device.  It must set up all
672aedd662SScott Long    information about the geometry and size of the disk */
682aedd662SScott Long static int ipsd_open(struct disk *dp)
692aedd662SScott Long {
702aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
712aedd662SScott Long 
722aedd662SScott Long 	dsc->state |= IPS_DEV_OPEN;
732aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
742aedd662SScott Long        	return 0;
752aedd662SScott Long }
762aedd662SScott Long 
772aedd662SScott Long static int ipsd_close(struct disk *dp)
782aedd662SScott Long {
792aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
802aedd662SScott Long 	dsc->state &= ~IPS_DEV_OPEN;
812aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
822aedd662SScott Long         return 0;
832aedd662SScott Long }
842aedd662SScott Long 
852aedd662SScott Long /* ipsd_finish is called to clean up and return a completed IO request */
862aedd662SScott Long void ipsd_finish(struct bio *iobuf)
872aedd662SScott Long {
88b234a120SScott Long 	ipsdisk_softc_t *dsc;
89b234a120SScott Long 	dsc = iobuf->bio_disk->d_drv1;
90b234a120SScott Long 
912aedd662SScott Long 	if (iobuf->bio_flags & BIO_ERROR) {
922aedd662SScott Long 		ipsdisk_softc_t *dsc;
932aedd662SScott Long 		dsc = iobuf->bio_disk->d_drv1;
942aedd662SScott Long 		device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
952aedd662SScott Long 	} else
962aedd662SScott Long 		iobuf->bio_resid = 0;
972aedd662SScott Long 
982aedd662SScott Long 	biodone(iobuf);
99b234a120SScott Long 	ips_start_io_request(dsc->sc);
1002aedd662SScott Long }
1012aedd662SScott Long 
1022aedd662SScott Long 
1032aedd662SScott Long static void ipsd_strategy(struct bio *iobuf)
1042aedd662SScott Long {
1052aedd662SScott Long 	ipsdisk_softc_t *dsc;
1062aedd662SScott Long 
1072aedd662SScott Long 	dsc = iobuf->bio_disk->d_drv1;
1082aedd662SScott Long 	DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
109f472527cSPeter Wemm 	iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
110d176b803SScott Long 
111d176b803SScott Long 	if ((iobuf->bio_cmd != BIO_READ) &&
112d176b803SScott Long 	    (iobuf->bio_cmd != BIO_WRITE)) {
113d176b803SScott Long 		biofinish(iobuf, NULL, EOPNOTSUPP);
114d176b803SScott Long 		return;
115d176b803SScott Long 	}
116d176b803SScott Long 
117b234a120SScott Long 	mtx_lock(&dsc->sc->queue_mtx);
11803a908f2SScott Long 	bioq_insert_tail(&dsc->sc->queue, iobuf);
119b234a120SScott Long 	ips_start_io_request(dsc->sc);
12003a908f2SScott Long 	mtx_unlock(&dsc->sc->queue_mtx);
1212aedd662SScott Long }
1222aedd662SScott Long 
1232aedd662SScott Long static int ipsd_probe(device_t dev)
1242aedd662SScott Long {
1252aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in probe\n");
1262aedd662SScott Long 	device_set_desc(dev, "Logical Drive");
1272aedd662SScott Long 	return 0;
1282aedd662SScott Long }
1292aedd662SScott Long 
1302aedd662SScott Long static int ipsd_attach(device_t dev)
1312aedd662SScott Long {
1322aedd662SScott Long 	device_t adapter;
1332aedd662SScott Long 	ipsdisk_softc_t *dsc;
1342aedd662SScott Long 	u_int totalsectors;
1352aedd662SScott Long 
1362aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in attach\n");
1372aedd662SScott Long 
1382aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1392aedd662SScott Long 	bzero(dsc, sizeof(ipsdisk_softc_t));
1402aedd662SScott Long 	adapter = device_get_parent(dev);
1412aedd662SScott Long 	dsc->dev = dev;
1422aedd662SScott Long 	dsc->sc = device_get_softc(adapter);
1432aedd662SScott Long 	dsc->unit = device_get_unit(dev);
144f472527cSPeter Wemm 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
1450b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk = disk_alloc();
1460b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_drv1 = dsc;
1470b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_name = "ipsd";
1480b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
1490b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_open = ipsd_open;
1500b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_close = ipsd_close;
1510b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_strategy = ipsd_strategy;
1527765040eSScott Long 	dsc->ipsd_disk->d_dump = ipsd_dump;
1532aedd662SScott Long 
1542aedd662SScott Long 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
1552aedd662SScott Long    	if ((totalsectors > 0x400000) &&
1562aedd662SScott Long        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
1570b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
1580b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
1592aedd662SScott Long    	} else {
1600b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
1610b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
1622aedd662SScott Long    	}
1630b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
1640b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
1650b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_unit = dsc->unit;
16603a908f2SScott Long 	dsc->ipsd_disk->d_flags = 0;
1670b7ed341SPoul-Henning Kamp 	disk_create(dsc->ipsd_disk, DISK_VERSION);
1682aedd662SScott Long 
1692aedd662SScott Long 	device_printf(dev, "Logical Drive  (%dMB)\n",
1702aedd662SScott Long 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
1712aedd662SScott Long 	return 0;
1722aedd662SScott Long }
1732aedd662SScott Long 
1742aedd662SScott Long static int ipsd_detach(device_t dev)
1752aedd662SScott Long {
1762aedd662SScott Long 	ipsdisk_softc_t *dsc;
1772aedd662SScott Long 
1782aedd662SScott Long 	DEVICE_PRINTF(2, dev,"in detach\n");
1792aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1802aedd662SScott Long 	if(dsc->state & IPS_DEV_OPEN)
1812aedd662SScott Long 		return (EBUSY);
1820b7ed341SPoul-Henning Kamp 	disk_destroy(dsc->ipsd_disk);
1832aedd662SScott Long 	return 0;
1842aedd662SScott Long }
1857765040eSScott Long 
1867765040eSScott Long static int
187*489ba222SMitchell Horne ipsd_dump(void *arg, void *virtual, off_t offset, size_t length)
1887765040eSScott Long {
1897765040eSScott Long 	ipsdisk_softc_t *dsc;
1907765040eSScott Long 	ips_softc_t *sc;
1917765040eSScott Long 	ips_command_t *command;
1927765040eSScott Long 	ips_io_cmd *command_struct;
1937765040eSScott Long 	struct disk *dp;
1947765040eSScott Long 	void *va;
1957765040eSScott Long 	off_t off;
1967765040eSScott Long 	size_t len;
1977765040eSScott Long 	int error = 0;
1987765040eSScott Long 
1997765040eSScott Long 	dp = arg;
2007765040eSScott Long 	dsc = dp->d_drv1;
2017765040eSScott Long 
2027765040eSScott Long 	if (dsc == NULL)
2037765040eSScott Long 		return (EINVAL);
204022ad822SChristian Brueffer 	sc = dsc->sc;
2057765040eSScott Long 
2067765040eSScott Long 	if (ips_get_free_cmd(sc, &command, 0) != 0) {
2077765040eSScott Long 		printf("ipsd: failed to get cmd for dump\n");
2087765040eSScott Long 		return (ENOMEM);
2097765040eSScott Long 	}
2107765040eSScott Long 
2117765040eSScott Long 	command->data_dmatag = sc->sg_dmatag;
2127765040eSScott Long 	command->callback = ipsd_dump_block_complete;
2137765040eSScott Long 
2147765040eSScott Long 	command_struct = (ips_io_cmd *)command->command_buffer;
2157765040eSScott Long 	command_struct->id = command->id;
2167765040eSScott Long 	command_struct->drivenum= sc->drives[dsc->disk_number].drivenum;
2177765040eSScott Long 
2187765040eSScott Long 	off = offset;
2197765040eSScott Long 	va = virtual;
2207765040eSScott Long 
2217765040eSScott Long 	while (length > 0) {
2227765040eSScott Long 		len =
2237765040eSScott Long 		    (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length;
2247765040eSScott Long 
2257765040eSScott Long 		command_struct->lba = off / IPS_BLKSIZE;
2267765040eSScott Long 
2277765040eSScott Long 		if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
2287765040eSScott Long 		    va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) {
2297765040eSScott Long 			error = EIO;
2307765040eSScott Long 			break;
2317765040eSScott Long 		}
2322eea7051SScott Long 		if (COMMAND_ERROR(command)) {
2337765040eSScott Long 			error = EIO;
2347765040eSScott Long 			break;
2357765040eSScott Long 		}
2367765040eSScott Long 
2377765040eSScott Long 		length -= len;
2387765040eSScott Long 		off += len;
2397765040eSScott Long 		va = (uint8_t *)va + len;
2407765040eSScott Long 	}
2417765040eSScott Long 
2427765040eSScott Long 	ips_insert_free_cmd(command->sc, command);
2437765040eSScott Long 	return (error);
2447765040eSScott Long }
2457765040eSScott Long 
2467765040eSScott Long static void
2477765040eSScott Long ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2487765040eSScott Long {
2497765040eSScott Long 	ips_softc_t *sc;
2507765040eSScott Long 	ips_command_t *command;
2517765040eSScott Long 	ips_sg_element_t *sg_list;
2527765040eSScott Long 	ips_io_cmd *command_struct;
2537765040eSScott Long 	int i, length;
2547765040eSScott Long 
2557765040eSScott Long 	command = (ips_command_t *)arg;
2567765040eSScott Long 	sc = command->sc;
2577765040eSScott Long 	length = 0;
2587765040eSScott Long 
2597765040eSScott Long 	if (error) {
2607765040eSScott Long 		printf("ipsd_dump_map_sg: error %d\n", error);
2612eea7051SScott Long 		ips_set_error(command, error);
2627765040eSScott Long 		return;
2637765040eSScott Long 	}
2647765040eSScott Long 
2657765040eSScott Long 	command_struct = (ips_io_cmd *)command->command_buffer;
2667765040eSScott Long 
2677765040eSScott Long 	if (nsegs != 1) {
2687765040eSScott Long 		command_struct->segnum = nsegs;
2697765040eSScott Long 		sg_list = (ips_sg_element_t *)((uint8_t *)
2707765040eSScott Long 		    command->command_buffer + IPS_COMMAND_LEN);
2717765040eSScott Long 		for (i = 0; i < nsegs; i++) {
2727765040eSScott Long 			sg_list[i].addr = segs[i].ds_addr;
2737765040eSScott Long 			sg_list[i].len = segs[i].ds_len;
2747765040eSScott Long 			length += segs[i].ds_len;
2757765040eSScott Long 		}
2767765040eSScott Long 		command_struct->buffaddr =
2777765040eSScott Long 		    (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
2787765040eSScott Long 		command_struct->command = IPS_SG_WRITE_CMD;
2797765040eSScott Long 	} else {
2807765040eSScott Long 		command_struct->buffaddr = segs[0].ds_addr;
2817765040eSScott Long 		length = segs[0].ds_len;
2827765040eSScott Long 		command_struct->segnum = 0;
2837765040eSScott Long 		command_struct->command = IPS_WRITE_CMD;
2847765040eSScott Long 	}
2857765040eSScott Long 
2867765040eSScott Long 	length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
2877765040eSScott Long 	command_struct->length = length;
2887765040eSScott Long 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
2897765040eSScott Long 	    BUS_DMASYNC_PREWRITE);
2907765040eSScott Long 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
2917765040eSScott Long 	    BUS_DMASYNC_PREWRITE);
2927765040eSScott Long 
2937765040eSScott Long 	sc->ips_issue_cmd(command);
2947765040eSScott Long 	sc->ips_poll_cmd(command);
2957765040eSScott Long 	return;
2967765040eSScott Long }
2977765040eSScott Long 
2987765040eSScott Long static void
2997765040eSScott Long ipsd_dump_block_complete(ips_command_t *command)
3007765040eSScott Long {
3017765040eSScott Long 
3022eea7051SScott Long 	if (COMMAND_ERROR(command))
3037765040eSScott Long 		printf("ipsd_dump completion error= 0x%x\n",
3047765040eSScott Long 		    command->status.value);
3057765040eSScott Long 
3067765040eSScott Long 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
3077765040eSScott Long 	    BUS_DMASYNC_POSTWRITE);
3087765040eSScott Long 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
3097765040eSScott Long }
310