xref: /freebsd/sys/dev/ips/ips_disk.c (revision 3144501a3f777d3bf89df9728d437ae177df47eb)
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 
427765040eSScott Long static int ipsd_dump(void *arg, void *virtual, vm_offset_t physical,
437765040eSScott Long 		     off_t offset, size_t length);
447765040eSScott Long static void ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs,
457765040eSScott Long 			     int error);
467765040eSScott Long static void ipsd_dump_block_complete(ips_command_t *command);
477765040eSScott Long 
482aedd662SScott Long static disk_open_t ipsd_open;
492aedd662SScott Long static disk_close_t ipsd_close;
502aedd662SScott Long static disk_strategy_t ipsd_strategy;
512aedd662SScott Long 
522aedd662SScott Long static device_method_t ipsd_methods[] = {
532aedd662SScott Long 	DEVMETHOD(device_probe,		ipsd_probe),
542aedd662SScott Long 	DEVMETHOD(device_attach,	ipsd_attach),
552aedd662SScott Long 	DEVMETHOD(device_detach,	ipsd_detach),
562aedd662SScott Long 	{ 0, 0 }
572aedd662SScott Long };
582aedd662SScott Long 
592aedd662SScott Long static driver_t ipsd_driver = {
602aedd662SScott Long 	"ipsd",
612aedd662SScott Long 	ipsd_methods,
622aedd662SScott Long 	sizeof(ipsdisk_softc_t)
632aedd662SScott Long };
642aedd662SScott Long 
65*3144501aSJohn Baldwin DRIVER_MODULE(ipsd, ips, ipsd_driver, 0, 0);
662aedd662SScott Long 
672aedd662SScott Long /* handle opening of disk device.  It must set up all
682aedd662SScott Long    information about the geometry and size of the disk */
692aedd662SScott Long static int ipsd_open(struct disk *dp)
702aedd662SScott Long {
712aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
722aedd662SScott Long 
732aedd662SScott Long 	dsc->state |= IPS_DEV_OPEN;
742aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm open\n");
752aedd662SScott Long        	return 0;
762aedd662SScott Long }
772aedd662SScott Long 
782aedd662SScott Long static int ipsd_close(struct disk *dp)
792aedd662SScott Long {
802aedd662SScott Long 	ipsdisk_softc_t *dsc = dp->d_drv1;
812aedd662SScott Long 	dsc->state &= ~IPS_DEV_OPEN;
822aedd662SScott Long 	DEVICE_PRINTF(2, dsc->dev, "I'm closed for the day\n");
832aedd662SScott Long         return 0;
842aedd662SScott Long }
852aedd662SScott Long 
862aedd662SScott Long /* ipsd_finish is called to clean up and return a completed IO request */
872aedd662SScott Long void ipsd_finish(struct bio *iobuf)
882aedd662SScott Long {
89b234a120SScott Long 	ipsdisk_softc_t *dsc;
90b234a120SScott Long 	dsc = iobuf->bio_disk->d_drv1;
91b234a120SScott Long 
922aedd662SScott Long 	if (iobuf->bio_flags & BIO_ERROR) {
932aedd662SScott Long 		ipsdisk_softc_t *dsc;
942aedd662SScott Long 		dsc = iobuf->bio_disk->d_drv1;
952aedd662SScott Long 		device_printf(dsc->dev, "iobuf error %d\n", iobuf->bio_error);
962aedd662SScott Long 	} else
972aedd662SScott Long 		iobuf->bio_resid = 0;
982aedd662SScott Long 
992aedd662SScott Long 	biodone(iobuf);
100b234a120SScott Long 	ips_start_io_request(dsc->sc);
1012aedd662SScott Long }
1022aedd662SScott Long 
1032aedd662SScott Long 
1042aedd662SScott Long static void ipsd_strategy(struct bio *iobuf)
1052aedd662SScott Long {
1062aedd662SScott Long 	ipsdisk_softc_t *dsc;
1072aedd662SScott Long 
1082aedd662SScott Long 	dsc = iobuf->bio_disk->d_drv1;
1092aedd662SScott Long 	DEVICE_PRINTF(8,dsc->dev,"in strategy\n");
110f472527cSPeter Wemm 	iobuf->bio_driver1 = (void *)(uintptr_t)dsc->sc->drives[dsc->disk_number].drivenum;
111d176b803SScott Long 
112d176b803SScott Long 	if ((iobuf->bio_cmd != BIO_READ) &&
113d176b803SScott Long 	    (iobuf->bio_cmd != BIO_WRITE)) {
114d176b803SScott Long 		biofinish(iobuf, NULL, EOPNOTSUPP);
115d176b803SScott Long 		return;
116d176b803SScott Long 	}
117d176b803SScott Long 
118b234a120SScott Long 	mtx_lock(&dsc->sc->queue_mtx);
11903a908f2SScott Long 	bioq_insert_tail(&dsc->sc->queue, iobuf);
120b234a120SScott Long 	ips_start_io_request(dsc->sc);
12103a908f2SScott Long 	mtx_unlock(&dsc->sc->queue_mtx);
1222aedd662SScott Long }
1232aedd662SScott Long 
1242aedd662SScott Long static int ipsd_probe(device_t dev)
1252aedd662SScott Long {
1262aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in probe\n");
1272aedd662SScott Long 	device_set_desc(dev, "Logical Drive");
1282aedd662SScott Long 	return 0;
1292aedd662SScott Long }
1302aedd662SScott Long 
1312aedd662SScott Long static int ipsd_attach(device_t dev)
1322aedd662SScott Long {
1332aedd662SScott Long 	device_t adapter;
1342aedd662SScott Long 	ipsdisk_softc_t *dsc;
1352aedd662SScott Long 	u_int totalsectors;
1362aedd662SScott Long 
1372aedd662SScott Long 	DEVICE_PRINTF(2,dev, "in attach\n");
1382aedd662SScott Long 
1392aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1402aedd662SScott Long 	bzero(dsc, sizeof(ipsdisk_softc_t));
1412aedd662SScott Long 	adapter = device_get_parent(dev);
1422aedd662SScott Long 	dsc->dev = dev;
1432aedd662SScott Long 	dsc->sc = device_get_softc(adapter);
1442aedd662SScott Long 	dsc->unit = device_get_unit(dev);
145f472527cSPeter Wemm 	dsc->disk_number = (uintptr_t) device_get_ivars(dev);
1460b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk = disk_alloc();
1470b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_drv1 = dsc;
1480b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_name = "ipsd";
1490b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_maxsize = IPS_MAX_IO_SIZE;
1500b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_open = ipsd_open;
1510b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_close = ipsd_close;
1520b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_strategy = ipsd_strategy;
1537765040eSScott Long 	dsc->ipsd_disk->d_dump = ipsd_dump;
1542aedd662SScott Long 
1552aedd662SScott Long 	totalsectors = dsc->sc->drives[dsc->disk_number].sector_count;
1562aedd662SScott Long    	if ((totalsectors > 0x400000) &&
1572aedd662SScott Long        			((dsc->sc->adapter_info.miscflags & 0x8) == 0)) {
1580b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwheads = IPS_NORM_HEADS;
1590b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwsectors = IPS_NORM_SECTORS;
1602aedd662SScott Long    	} else {
1610b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwheads = IPS_COMP_HEADS;
1620b7ed341SPoul-Henning Kamp       		dsc->ipsd_disk->d_fwsectors = IPS_COMP_SECTORS;
1632aedd662SScott Long    	}
1640b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_sectorsize = IPS_BLKSIZE;
1650b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_mediasize = (off_t)totalsectors * IPS_BLKSIZE;
1660b7ed341SPoul-Henning Kamp 	dsc->ipsd_disk->d_unit = dsc->unit;
16703a908f2SScott Long 	dsc->ipsd_disk->d_flags = 0;
1680b7ed341SPoul-Henning Kamp 	disk_create(dsc->ipsd_disk, DISK_VERSION);
1692aedd662SScott Long 
1702aedd662SScott Long 	device_printf(dev, "Logical Drive  (%dMB)\n",
1712aedd662SScott Long 		      dsc->sc->drives[dsc->disk_number].sector_count >> 11);
1722aedd662SScott Long 	return 0;
1732aedd662SScott Long }
1742aedd662SScott Long 
1752aedd662SScott Long static int ipsd_detach(device_t dev)
1762aedd662SScott Long {
1772aedd662SScott Long 	ipsdisk_softc_t *dsc;
1782aedd662SScott Long 
1792aedd662SScott Long 	DEVICE_PRINTF(2, dev,"in detach\n");
1802aedd662SScott Long 	dsc = (ipsdisk_softc_t *)device_get_softc(dev);
1812aedd662SScott Long 	if(dsc->state & IPS_DEV_OPEN)
1822aedd662SScott Long 		return (EBUSY);
1830b7ed341SPoul-Henning Kamp 	disk_destroy(dsc->ipsd_disk);
1842aedd662SScott Long 	return 0;
1852aedd662SScott Long }
1867765040eSScott Long 
1877765040eSScott Long static int
1887765040eSScott Long ipsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
1897765040eSScott Long 	  size_t length)
1907765040eSScott Long {
1917765040eSScott Long 	ipsdisk_softc_t *dsc;
1927765040eSScott Long 	ips_softc_t *sc;
1937765040eSScott Long 	ips_command_t *command;
1947765040eSScott Long 	ips_io_cmd *command_struct;
1957765040eSScott Long 	struct disk *dp;
1967765040eSScott Long 	void *va;
1977765040eSScott Long 	off_t off;
1987765040eSScott Long 	size_t len;
1997765040eSScott Long 	int error = 0;
2007765040eSScott Long 
2017765040eSScott Long 	dp = arg;
2027765040eSScott Long 	dsc = dp->d_drv1;
2037765040eSScott Long 
2047765040eSScott Long 	if (dsc == NULL)
2057765040eSScott Long 		return (EINVAL);
206022ad822SChristian Brueffer 	sc = dsc->sc;
2077765040eSScott Long 
2087765040eSScott Long 	if (ips_get_free_cmd(sc, &command, 0) != 0) {
2097765040eSScott Long 		printf("ipsd: failed to get cmd for dump\n");
2107765040eSScott Long 		return (ENOMEM);
2117765040eSScott Long 	}
2127765040eSScott Long 
2137765040eSScott Long 	command->data_dmatag = sc->sg_dmatag;
2147765040eSScott Long 	command->callback = ipsd_dump_block_complete;
2157765040eSScott Long 
2167765040eSScott Long 	command_struct = (ips_io_cmd *)command->command_buffer;
2177765040eSScott Long 	command_struct->id = command->id;
2187765040eSScott Long 	command_struct->drivenum= sc->drives[dsc->disk_number].drivenum;
2197765040eSScott Long 
2207765040eSScott Long 	off = offset;
2217765040eSScott Long 	va = virtual;
2227765040eSScott Long 
2237765040eSScott Long 	while (length > 0) {
2247765040eSScott Long 		len =
2257765040eSScott Long 		    (length > IPS_MAX_IO_SIZE) ? IPS_MAX_IO_SIZE : length;
2267765040eSScott Long 
2277765040eSScott Long 		command_struct->lba = off / IPS_BLKSIZE;
2287765040eSScott Long 
2297765040eSScott Long 		if (bus_dmamap_load(command->data_dmatag, command->data_dmamap,
2307765040eSScott Long 		    va, len, ipsd_dump_map_sg, command, BUS_DMA_NOWAIT) != 0) {
2317765040eSScott Long 			error = EIO;
2327765040eSScott Long 			break;
2337765040eSScott Long 		}
2342eea7051SScott Long 		if (COMMAND_ERROR(command)) {
2357765040eSScott Long 			error = EIO;
2367765040eSScott Long 			break;
2377765040eSScott Long 		}
2387765040eSScott Long 
2397765040eSScott Long 		length -= len;
2407765040eSScott Long 		off += len;
2417765040eSScott Long 		va = (uint8_t *)va + len;
2427765040eSScott Long 	}
2437765040eSScott Long 
2447765040eSScott Long 	ips_insert_free_cmd(command->sc, command);
2457765040eSScott Long 	return (error);
2467765040eSScott Long }
2477765040eSScott Long 
2487765040eSScott Long static void
2497765040eSScott Long ipsd_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2507765040eSScott Long {
2517765040eSScott Long 	ips_softc_t *sc;
2527765040eSScott Long 	ips_command_t *command;
2537765040eSScott Long 	ips_sg_element_t *sg_list;
2547765040eSScott Long 	ips_io_cmd *command_struct;
2557765040eSScott Long 	int i, length;
2567765040eSScott Long 
2577765040eSScott Long 	command = (ips_command_t *)arg;
2587765040eSScott Long 	sc = command->sc;
2597765040eSScott Long 	length = 0;
2607765040eSScott Long 
2617765040eSScott Long 	if (error) {
2627765040eSScott Long 		printf("ipsd_dump_map_sg: error %d\n", error);
2632eea7051SScott Long 		ips_set_error(command, error);
2647765040eSScott Long 		return;
2657765040eSScott Long 	}
2667765040eSScott Long 
2677765040eSScott Long 	command_struct = (ips_io_cmd *)command->command_buffer;
2687765040eSScott Long 
2697765040eSScott Long 	if (nsegs != 1) {
2707765040eSScott Long 		command_struct->segnum = nsegs;
2717765040eSScott Long 		sg_list = (ips_sg_element_t *)((uint8_t *)
2727765040eSScott Long 		    command->command_buffer + IPS_COMMAND_LEN);
2737765040eSScott Long 		for (i = 0; i < nsegs; i++) {
2747765040eSScott Long 			sg_list[i].addr = segs[i].ds_addr;
2757765040eSScott Long 			sg_list[i].len = segs[i].ds_len;
2767765040eSScott Long 			length += segs[i].ds_len;
2777765040eSScott Long 		}
2787765040eSScott Long 		command_struct->buffaddr =
2797765040eSScott Long 		    (uint32_t)command->command_phys_addr + IPS_COMMAND_LEN;
2807765040eSScott Long 		command_struct->command = IPS_SG_WRITE_CMD;
2817765040eSScott Long 	} else {
2827765040eSScott Long 		command_struct->buffaddr = segs[0].ds_addr;
2837765040eSScott Long 		length = segs[0].ds_len;
2847765040eSScott Long 		command_struct->segnum = 0;
2857765040eSScott Long 		command_struct->command = IPS_WRITE_CMD;
2867765040eSScott Long 	}
2877765040eSScott Long 
2887765040eSScott Long 	length = (length + IPS_BLKSIZE - 1) / IPS_BLKSIZE;
2897765040eSScott Long 	command_struct->length = length;
2907765040eSScott Long 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
2917765040eSScott Long 	    BUS_DMASYNC_PREWRITE);
2927765040eSScott Long 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
2937765040eSScott Long 	    BUS_DMASYNC_PREWRITE);
2947765040eSScott Long 
2957765040eSScott Long 	sc->ips_issue_cmd(command);
2967765040eSScott Long 	sc->ips_poll_cmd(command);
2977765040eSScott Long 	return;
2987765040eSScott Long }
2997765040eSScott Long 
3007765040eSScott Long static void
3017765040eSScott Long ipsd_dump_block_complete(ips_command_t *command)
3027765040eSScott Long {
3037765040eSScott Long 
3042eea7051SScott Long 	if (COMMAND_ERROR(command))
3057765040eSScott Long 		printf("ipsd_dump completion error= 0x%x\n",
3067765040eSScott Long 		    command->status.value);
3077765040eSScott Long 
3087765040eSScott Long 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
3097765040eSScott Long 	    BUS_DMASYNC_POSTWRITE);
3107765040eSScott Long 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
3117765040eSScott Long }
312