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	int s;
9148156Sjlemon
92111337Sphk	drv = bp->bio_disk->d_drv1;
9348156Sjlemon	if (drv == NULL) {
9459249Sphk    		bp->bio_error = EINVAL;
9548156Sjlemon		goto bad;
9648156Sjlemon	}
9748156Sjlemon
9848156Sjlemon	/*
9948156Sjlemon	 * software write protect check
10048156Sjlemon	 */
10159249Sphk	if (drv->flags & DRV_WRITEPROT && (bp->bio_cmd == BIO_WRITE)) {
10259249Sphk		bp->bio_error = EROFS;
10348156Sjlemon		goto bad;
10448156Sjlemon	}
10548156Sjlemon
106118678Smdodd	bp->bio_driver1 = drv;
10748156Sjlemon	s = splbio();
10848156Sjlemon	ida_submit_buf(drv->controller, bp);
10948156Sjlemon	splx(s);
11048156Sjlemon	return;
11148156Sjlemon
11248156Sjlemonbad:
11359249Sphk	bp->bio_flags |= BIO_ERROR;
11448156Sjlemon
11548156Sjlemon	/*
11648156Sjlemon	 * Correctly set the buf to indicate a completed transfer
11748156Sjlemon	 */
11859249Sphk	bp->bio_resid = bp->bio_bcount;
11948156Sjlemon	biodone(bp);
12048156Sjlemon	return;
12148156Sjlemon}
12248156Sjlemon
12373113Sjlemonstatic int
124111220Sphkidad_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
12573113Sjlemon{
12693496Sphk
12773113Sjlemon	struct idad_softc *drv;
128110483Sps	int error = 0;
129111220Sphk	struct disk *dp;
13073113Sjlemon
131111220Sphk	dp = arg;
132111337Sphk	drv = dp->d_drv1;
13373113Sjlemon	if (drv == NULL)
13473113Sjlemon		return (ENXIO);
13573113Sjlemon
136110483Sps	drv->controller->flags &= ~IDA_INTERRUPTS;
13773113Sjlemon
138110483Sps	if (length > 0) {
139110483Sps		error = ida_command(drv->controller, CMD_WRITE, virtual,
140110483Sps		    length, drv->drive, offset / DEV_BSIZE, DMA_DATA_OUT);
14173113Sjlemon	}
142110483Sps	drv->controller->flags |= IDA_INTERRUPTS;
143110483Sps	return (error);
14473113Sjlemon}
14573113Sjlemon
14648156Sjlemonvoid
14759485Smdoddidad_intr(struct bio *bp)
14848156Sjlemon{
149111337Sphk	struct idad_softc *drv;
15048156Sjlemon
151111337Sphk	drv = bp->bio_disk->d_drv1;
152111337Sphk
15359249Sphk	if (bp->bio_flags & BIO_ERROR)
15459249Sphk		bp->bio_error = EIO;
15548156Sjlemon	else
15659249Sphk		bp->bio_resid = 0;
15748156Sjlemon
158111979Sphk	biodone(bp);
15948156Sjlemon}
16048156Sjlemon
16148156Sjlemonstatic int
16259485Smdoddidad_probe(device_t dev)
16348156Sjlemon{
16448156Sjlemon
16548156Sjlemon	device_set_desc(dev, "Compaq Logical Drive");
16648156Sjlemon	return (0);
16748156Sjlemon}
16848156Sjlemon
16948156Sjlemonstatic int
17059485Smdoddidad_attach(device_t dev)
17148156Sjlemon{
17248156Sjlemon	struct ida_drive_info dinfo;
17359485Smdodd	struct idad_softc *drv;
17448156Sjlemon	device_t parent;
17548156Sjlemon	int error;
17648156Sjlemon
17759485Smdodd	drv = (struct idad_softc *)device_get_softc(dev);
17848156Sjlemon	parent = device_get_parent(dev);
179124500Smdodd	drv->dev = dev;
18048156Sjlemon	drv->controller = (struct ida_softc *)device_get_softc(parent);
18148156Sjlemon	drv->unit = device_get_unit(dev);
18263934Sjlemon	drv->drive = drv->controller->num_drives;
18363934Sjlemon	drv->controller->num_drives++;
18448156Sjlemon
18548156Sjlemon	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
18673113Sjlemon	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
18748156Sjlemon	if (error) {
18848156Sjlemon		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
18948156Sjlemon		return (ENXIO);
19048156Sjlemon	}
19148156Sjlemon
192124539Smdodd	drv->cylinders = dinfo.dp.ncylinders;
193124539Smdodd	drv->heads = dinfo.dp.nheads;
194124539Smdodd	drv->sectors = dinfo.dp.nsectors;
19563934Sjlemon	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
19648156Sjlemon	drv->secperunit = dinfo.secperunit;
19748156Sjlemon
19848156Sjlemon	/* XXX
19948156Sjlemon	 * other initialization
20048156Sjlemon	 */
20148156Sjlemon	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
20248156Sjlemon	    drv->secperunit / ((1024 * 1024) / drv->secsize),
20348156Sjlemon	    drv->secperunit, drv->secsize);
20448156Sjlemon
205125975Sphk	drv->disk = disk_alloc();
206125975Sphk	drv->disk->d_strategy = idad_strategy;
207125975Sphk	drv->disk->d_name = "idad";
208125975Sphk	drv->disk->d_dump = idad_dump;
209125975Sphk	drv->disk->d_sectorsize = drv->secsize;
210125975Sphk	drv->disk->d_mediasize = (off_t)drv->secperunit * drv->secsize;
211125975Sphk	drv->disk->d_fwsectors = drv->sectors;
212125975Sphk	drv->disk->d_fwheads = drv->heads;
213125975Sphk	drv->disk->d_drv1 = drv;
214125975Sphk	drv->disk->d_maxsize = DFLTPHYS;		/* XXX guess? */
215125975Sphk	drv->disk->d_unit = drv->unit;
216125975Sphk	drv->disk->d_flags = DISKFLAG_NEEDSGIANT;
217125975Sphk	disk_create(drv->disk, DISK_VERSION);
21857828Sjlemon
21948156Sjlemon	return (0);
22048156Sjlemon}
22148156Sjlemon
22257828Sjlemonstatic int
22359485Smdoddidad_detach(device_t dev)
22457828Sjlemon{
22559485Smdodd	struct idad_softc *drv;
22657828Sjlemon
22759485Smdodd	drv = (struct idad_softc *)device_get_softc(dev);
228125975Sphk	disk_destroy(drv->disk);
22957828Sjlemon	return (0);
23057828Sjlemon}
231