148156Sjlemon/*-
257828Sjlemon * Copyright (c) 1999,2000 Jonathan Lemon
348156Sjlemon * All rights reserved.
448156Sjlemon *
548156Sjlemon * Redistribution and use in source and binary forms, with or without
648156Sjlemon * modification, are permitted provided that the following conditions
748156Sjlemon * are met:
848156Sjlemon * 1. Redistributions of source code must retain the above copyright
948156Sjlemon *    notice, this list of conditions and the following disclaimer.
1048156Sjlemon * 2. Redistributions in binary form must reproduce the above copyright
1148156Sjlemon *    notice, this list of conditions and the following disclaimer in the
1248156Sjlemon *    documentation and/or other materials provided with the distribution.
1348156Sjlemon *
1448156Sjlemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1548156Sjlemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1648156Sjlemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1748156Sjlemon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1848156Sjlemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1948156Sjlemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2048156Sjlemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2148156Sjlemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2248156Sjlemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2348156Sjlemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2448156Sjlemon * SUCH DAMAGE.
2548156Sjlemon *
2650477Speter * $FreeBSD$
2748156Sjlemon */
2848156Sjlemon
2948156Sjlemon/*
3048156Sjlemon * Disk driver for Compaq SMART RAID adapters.
3148156Sjlemon */
3248156Sjlemon
3348156Sjlemon#include <sys/param.h>
3448156Sjlemon#include <sys/systm.h>
3548156Sjlemon#include <sys/kernel.h>
36129879Sphk#include <sys/module.h>
3748156Sjlemon
3860041Sphk#include <sys/bio.h>
3948156Sjlemon#include <sys/bus.h>
4048156Sjlemon#include <sys/conf.h>
4173113Sjlemon#include <sys/cons.h>
4248156Sjlemon
4348156Sjlemon#include <machine/bus.h>
4448156Sjlemon#include <sys/rman.h>
4548156Sjlemon
4673113Sjlemon#include <vm/vm.h>
4773113Sjlemon#include <vm/pmap.h>
4873113Sjlemon#include <machine/md_var.h>
4973113Sjlemon
50112946Sphk#include <geom/geom_disk.h>
51112946Sphk
5248156Sjlemon#include <dev/ida/idareg.h>
5348156Sjlemon#include <dev/ida/idavar.h>
5448156Sjlemon
5548156Sjlemon/* prototypes */
5659485Smdoddstatic int idad_probe(device_t dev);
5759485Smdoddstatic int idad_attach(device_t dev);
5859485Smdoddstatic int idad_detach(device_t dev);
5948156Sjlemon
6059485Smdoddstatic	d_strategy_t	idad_strategy;
61111220Sphkstatic	dumper_t	idad_dump;
6248156Sjlemon
6359485Smdoddstatic devclass_t	idad_devclass;
6448156Sjlemon
6559485Smdoddstatic device_method_t idad_methods[] = {
6659485Smdodd	DEVMETHOD(device_probe,		idad_probe),
6759485Smdodd	DEVMETHOD(device_attach,	idad_attach),
6859485Smdodd	DEVMETHOD(device_detach,	idad_detach),
6948156Sjlemon	{ 0, 0 }
7048156Sjlemon};
7148156Sjlemon
7259485Smdoddstatic driver_t idad_driver = {
7357828Sjlemon	"idad",
7459485Smdodd	idad_methods,
7559485Smdodd	sizeof(struct idad_softc)
7648156Sjlemon};
7748156Sjlemon
7859485SmdoddDRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
7959485Smdodd
8048156Sjlemon/*
8148156Sjlemon * Read/write routine for a buffer.  Finds the proper unit, range checks
8248156Sjlemon * arguments, and schedules the transfer.  Does not wait for the transfer
8348156Sjlemon * to complete.  Multi-page transfers are supported.  All I/O requests must
8448156Sjlemon * be a multiple of a sector in length.
8548156Sjlemon */
8648156Sjlemonstatic void
8759485Smdoddidad_strategy(struct bio *bp)
8848156Sjlemon{
8959485Smdodd	struct idad_softc *drv;
9048156Sjlemon
91111337Sphk	drv = bp->bio_disk->d_drv1;
9248156Sjlemon	if (drv == NULL) {
9359249Sphk    		bp->bio_error = EINVAL;
9448156Sjlemon		goto bad;
9548156Sjlemon	}
9648156Sjlemon
9748156Sjlemon	/*
9848156Sjlemon	 * software write protect check
9948156Sjlemon	 */
10059249Sphk	if (drv->flags & DRV_WRITEPROT && (bp->bio_cmd == BIO_WRITE)) {
10159249Sphk		bp->bio_error = EROFS;
10248156Sjlemon		goto bad;
10348156Sjlemon	}
10448156Sjlemon
105118678Smdodd	bp->bio_driver1 = drv;
10648156Sjlemon	ida_submit_buf(drv->controller, bp);
10748156Sjlemon	return;
10848156Sjlemon
10948156Sjlemonbad:
11059249Sphk	bp->bio_flags |= BIO_ERROR;
11148156Sjlemon
11248156Sjlemon	/*
11348156Sjlemon	 * Correctly set the buf to indicate a completed transfer
11448156Sjlemon	 */
11559249Sphk	bp->bio_resid = bp->bio_bcount;
11648156Sjlemon	biodone(bp);
11748156Sjlemon	return;
11848156Sjlemon}
11948156Sjlemon
12073113Sjlemonstatic int
121111220Sphkidad_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
12273113Sjlemon{
12393496Sphk
12473113Sjlemon	struct idad_softc *drv;
125110483Sps	int error = 0;
126111220Sphk	struct disk *dp;
12773113Sjlemon
128111220Sphk	dp = arg;
129111337Sphk	drv = dp->d_drv1;
13073113Sjlemon	if (drv == NULL)
13173113Sjlemon		return (ENXIO);
13273113Sjlemon
133110483Sps	drv->controller->flags &= ~IDA_INTERRUPTS;
13473113Sjlemon
135110483Sps	if (length > 0) {
136110483Sps		error = ida_command(drv->controller, CMD_WRITE, virtual,
137110483Sps		    length, drv->drive, offset / DEV_BSIZE, DMA_DATA_OUT);
13873113Sjlemon	}
139110483Sps	drv->controller->flags |= IDA_INTERRUPTS;
140110483Sps	return (error);
14173113Sjlemon}
14273113Sjlemon
14348156Sjlemonvoid
14459485Smdoddidad_intr(struct bio *bp)
14548156Sjlemon{
146111337Sphk	struct idad_softc *drv;
14748156Sjlemon
148111337Sphk	drv = bp->bio_disk->d_drv1;
149111337Sphk
15059249Sphk	if (bp->bio_flags & BIO_ERROR)
15159249Sphk		bp->bio_error = EIO;
15248156Sjlemon	else
15359249Sphk		bp->bio_resid = 0;
15448156Sjlemon
155111979Sphk	biodone(bp);
15648156Sjlemon}
15748156Sjlemon
15848156Sjlemonstatic int
15959485Smdoddidad_probe(device_t dev)
16048156Sjlemon{
16148156Sjlemon
16248156Sjlemon	device_set_desc(dev, "Compaq Logical Drive");
16348156Sjlemon	return (0);
16448156Sjlemon}
16548156Sjlemon
16648156Sjlemonstatic int
16759485Smdoddidad_attach(device_t dev)
16848156Sjlemon{
16948156Sjlemon	struct ida_drive_info dinfo;
17059485Smdodd	struct idad_softc *drv;
17148156Sjlemon	device_t parent;
17248156Sjlemon	int error;
17348156Sjlemon
17459485Smdodd	drv = (struct idad_softc *)device_get_softc(dev);
17548156Sjlemon	parent = device_get_parent(dev);
176124500Smdodd	drv->dev = dev;
17748156Sjlemon	drv->controller = (struct ida_softc *)device_get_softc(parent);
17848156Sjlemon	drv->unit = device_get_unit(dev);
179239740Sjhb	drv->drive = (intptr_t)device_get_ivars(dev);
18048156Sjlemon
181239740Sjhb	mtx_lock(&drv->controller->lock);
18248156Sjlemon	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
18373113Sjlemon	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
184239740Sjhb	mtx_unlock(&drv->controller->lock);
18548156Sjlemon	if (error) {
18648156Sjlemon		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
18748156Sjlemon		return (ENXIO);
18848156Sjlemon	}
18948156Sjlemon
190124539Smdodd	drv->cylinders = dinfo.dp.ncylinders;
191124539Smdodd	drv->heads = dinfo.dp.nheads;
192124539Smdodd	drv->sectors = dinfo.dp.nsectors;
19363934Sjlemon	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
19448156Sjlemon	drv->secperunit = dinfo.secperunit;
19548156Sjlemon
19648156Sjlemon	/* XXX
19748156Sjlemon	 * other initialization
19848156Sjlemon	 */
19948156Sjlemon	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
20048156Sjlemon	    drv->secperunit / ((1024 * 1024) / drv->secsize),
20148156Sjlemon	    drv->secperunit, drv->secsize);
20248156Sjlemon
203125975Sphk	drv->disk = disk_alloc();
204125975Sphk	drv->disk->d_strategy = idad_strategy;
205125975Sphk	drv->disk->d_name = "idad";
206125975Sphk	drv->disk->d_dump = idad_dump;
207125975Sphk	drv->disk->d_sectorsize = drv->secsize;
208125975Sphk	drv->disk->d_mediasize = (off_t)drv->secperunit * drv->secsize;
209125975Sphk	drv->disk->d_fwsectors = drv->sectors;
210125975Sphk	drv->disk->d_fwheads = drv->heads;
211125975Sphk	drv->disk->d_drv1 = drv;
212125975Sphk	drv->disk->d_maxsize = DFLTPHYS;		/* XXX guess? */
213125975Sphk	drv->disk->d_unit = drv->unit;
214125975Sphk	disk_create(drv->disk, DISK_VERSION);
21557828Sjlemon
21648156Sjlemon	return (0);
21748156Sjlemon}
21848156Sjlemon
21957828Sjlemonstatic int
22059485Smdoddidad_detach(device_t dev)
22157828Sjlemon{
22259485Smdodd	struct idad_softc *drv;
22357828Sjlemon
22459485Smdodd	drv = (struct idad_softc *)device_get_softc(dev);
225125975Sphk	disk_destroy(drv->disk);
22657828Sjlemon	return (0);
22757828Sjlemon}
228