1db57feb7SJonathan Lemon /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4ee7eb00eSJonathan Lemon * Copyright (c) 1999,2000 Jonathan Lemon
5db57feb7SJonathan Lemon * All rights reserved.
6db57feb7SJonathan Lemon *
7db57feb7SJonathan Lemon * Redistribution and use in source and binary forms, with or without
8db57feb7SJonathan Lemon * modification, are permitted provided that the following conditions
9db57feb7SJonathan Lemon * are met:
10db57feb7SJonathan Lemon * 1. Redistributions of source code must retain the above copyright
11db57feb7SJonathan Lemon * notice, this list of conditions and the following disclaimer.
12db57feb7SJonathan Lemon * 2. Redistributions in binary form must reproduce the above copyright
13db57feb7SJonathan Lemon * notice, this list of conditions and the following disclaimer in the
14db57feb7SJonathan Lemon * documentation and/or other materials provided with the distribution.
15db57feb7SJonathan Lemon *
16db57feb7SJonathan Lemon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17db57feb7SJonathan Lemon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18db57feb7SJonathan Lemon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19db57feb7SJonathan Lemon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20db57feb7SJonathan Lemon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21db57feb7SJonathan Lemon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22db57feb7SJonathan Lemon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23db57feb7SJonathan Lemon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24db57feb7SJonathan Lemon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25db57feb7SJonathan Lemon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26db57feb7SJonathan Lemon * SUCH DAMAGE.
27db57feb7SJonathan Lemon */
28db57feb7SJonathan Lemon
29db57feb7SJonathan Lemon /*
30db57feb7SJonathan Lemon * Disk driver for Compaq SMART RAID adapters.
31db57feb7SJonathan Lemon */
32db57feb7SJonathan Lemon
33db57feb7SJonathan Lemon #include <sys/param.h>
34db57feb7SJonathan Lemon #include <sys/systm.h>
35db57feb7SJonathan Lemon #include <sys/kernel.h>
36fe12f24bSPoul-Henning Kamp #include <sys/module.h>
37db57feb7SJonathan Lemon
389626b608SPoul-Henning Kamp #include <sys/bio.h>
39db57feb7SJonathan Lemon #include <sys/bus.h>
40db57feb7SJonathan Lemon #include <sys/conf.h>
4155d782fcSJonathan Lemon #include <sys/cons.h>
42e2e050c8SConrad Meyer #include <sys/lock.h>
43e2e050c8SConrad Meyer #include <sys/mutex.h>
44db57feb7SJonathan Lemon
45db57feb7SJonathan Lemon #include <machine/bus.h>
46db57feb7SJonathan Lemon #include <sys/rman.h>
47db57feb7SJonathan Lemon
4855d782fcSJonathan Lemon #include <vm/vm.h>
4955d782fcSJonathan Lemon #include <vm/pmap.h>
5055d782fcSJonathan Lemon #include <machine/md_var.h>
5155d782fcSJonathan Lemon
52891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
53891619a6SPoul-Henning Kamp
54db57feb7SJonathan Lemon #include <dev/ida/idareg.h>
55db57feb7SJonathan Lemon #include <dev/ida/idavar.h>
56db57feb7SJonathan Lemon
57db57feb7SJonathan Lemon /* prototypes */
5881530866SMatthew N. Dodd static int idad_probe(device_t dev);
5981530866SMatthew N. Dodd static int idad_attach(device_t dev);
6081530866SMatthew N. Dodd static int idad_detach(device_t dev);
61db57feb7SJonathan Lemon
6281530866SMatthew N. Dodd static d_strategy_t idad_strategy;
632c6b49f6SPoul-Henning Kamp static dumper_t idad_dump;
64db57feb7SJonathan Lemon
6581530866SMatthew N. Dodd static device_method_t idad_methods[] = {
6681530866SMatthew N. Dodd DEVMETHOD(device_probe, idad_probe),
6781530866SMatthew N. Dodd DEVMETHOD(device_attach, idad_attach),
6881530866SMatthew N. Dodd DEVMETHOD(device_detach, idad_detach),
69db57feb7SJonathan Lemon { 0, 0 }
70db57feb7SJonathan Lemon };
71db57feb7SJonathan Lemon
7281530866SMatthew N. Dodd static driver_t idad_driver = {
73ee7eb00eSJonathan Lemon "idad",
7481530866SMatthew N. Dodd idad_methods,
7581530866SMatthew N. Dodd sizeof(struct idad_softc)
76db57feb7SJonathan Lemon };
77db57feb7SJonathan Lemon
78fb9f63e8SJohn Baldwin DRIVER_MODULE(idad, ida, idad_driver, 0, 0);
7981530866SMatthew N. Dodd
80db57feb7SJonathan Lemon /*
81db57feb7SJonathan Lemon * Read/write routine for a buffer. Finds the proper unit, range checks
82db57feb7SJonathan Lemon * arguments, and schedules the transfer. Does not wait for the transfer
83db57feb7SJonathan Lemon * to complete. Multi-page transfers are supported. All I/O requests must
84db57feb7SJonathan Lemon * be a multiple of a sector in length.
85db57feb7SJonathan Lemon */
86db57feb7SJonathan Lemon static void
idad_strategy(struct bio * bp)8781530866SMatthew N. Dodd idad_strategy(struct bio *bp)
88db57feb7SJonathan Lemon {
8981530866SMatthew N. Dodd struct idad_softc *drv;
90db57feb7SJonathan Lemon
91e5dc8339SPoul-Henning Kamp drv = bp->bio_disk->d_drv1;
92db57feb7SJonathan Lemon if (drv == NULL) {
938177437dSPoul-Henning Kamp bp->bio_error = EINVAL;
94db57feb7SJonathan Lemon goto bad;
95db57feb7SJonathan Lemon }
96db57feb7SJonathan Lemon
97db57feb7SJonathan Lemon /*
98db57feb7SJonathan Lemon * software write protect check
99db57feb7SJonathan Lemon */
1008177437dSPoul-Henning Kamp if (drv->flags & DRV_WRITEPROT && (bp->bio_cmd == BIO_WRITE)) {
1018177437dSPoul-Henning Kamp bp->bio_error = EROFS;
102db57feb7SJonathan Lemon goto bad;
103db57feb7SJonathan Lemon }
104db57feb7SJonathan Lemon
105d176b803SScott Long if ((bp->bio_cmd != BIO_READ) && (bp->bio_cmd != BIO_WRITE)) {
106d176b803SScott Long bp->bio_error = EOPNOTSUPP;
107d176b803SScott Long goto bad;
108d176b803SScott Long }
109d176b803SScott Long
110654d0694SMatthew N. Dodd bp->bio_driver1 = drv;
111db57feb7SJonathan Lemon ida_submit_buf(drv->controller, bp);
112db57feb7SJonathan Lemon return;
113db57feb7SJonathan Lemon
114db57feb7SJonathan Lemon bad:
1158177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR;
116db57feb7SJonathan Lemon
117db57feb7SJonathan Lemon /*
118db57feb7SJonathan Lemon * Correctly set the buf to indicate a completed transfer
119db57feb7SJonathan Lemon */
1208177437dSPoul-Henning Kamp bp->bio_resid = bp->bio_bcount;
121db57feb7SJonathan Lemon biodone(bp);
122db57feb7SJonathan Lemon return;
123db57feb7SJonathan Lemon }
124db57feb7SJonathan Lemon
12555d782fcSJonathan Lemon static int
idad_dump(void * arg,void * virtual,off_t offset,size_t length)126489ba222SMitchell Horne idad_dump(void *arg, void *virtual, off_t offset, size_t length)
12755d782fcSJonathan Lemon {
12881661c94SPoul-Henning Kamp
12955d782fcSJonathan Lemon struct idad_softc *drv;
130ff2387cbSPaul Saab int error = 0;
1312c6b49f6SPoul-Henning Kamp struct disk *dp;
13255d782fcSJonathan Lemon
1332c6b49f6SPoul-Henning Kamp dp = arg;
134e5dc8339SPoul-Henning Kamp drv = dp->d_drv1;
13555d782fcSJonathan Lemon if (drv == NULL)
13655d782fcSJonathan Lemon return (ENXIO);
13755d782fcSJonathan Lemon
138ff2387cbSPaul Saab drv->controller->flags &= ~IDA_INTERRUPTS;
13955d782fcSJonathan Lemon
140ff2387cbSPaul Saab if (length > 0) {
141ff2387cbSPaul Saab error = ida_command(drv->controller, CMD_WRITE, virtual,
142ff2387cbSPaul Saab length, drv->drive, offset / DEV_BSIZE, DMA_DATA_OUT);
14355d782fcSJonathan Lemon }
144ff2387cbSPaul Saab drv->controller->flags |= IDA_INTERRUPTS;
145ff2387cbSPaul Saab return (error);
14655d782fcSJonathan Lemon }
14755d782fcSJonathan Lemon
148db57feb7SJonathan Lemon void
idad_intr(struct bio * bp)14981530866SMatthew N. Dodd idad_intr(struct bio *bp)
150db57feb7SJonathan Lemon {
151db57feb7SJonathan Lemon
1528177437dSPoul-Henning Kamp if (bp->bio_flags & BIO_ERROR)
1538177437dSPoul-Henning Kamp bp->bio_error = EIO;
154db57feb7SJonathan Lemon else
1558177437dSPoul-Henning Kamp bp->bio_resid = 0;
156db57feb7SJonathan Lemon
15760794e04SPoul-Henning Kamp biodone(bp);
158db57feb7SJonathan Lemon }
159db57feb7SJonathan Lemon
160db57feb7SJonathan Lemon static int
idad_probe(device_t dev)16181530866SMatthew N. Dodd idad_probe(device_t dev)
162db57feb7SJonathan Lemon {
163db57feb7SJonathan Lemon
164db57feb7SJonathan Lemon device_set_desc(dev, "Compaq Logical Drive");
165db57feb7SJonathan Lemon return (0);
166db57feb7SJonathan Lemon }
167db57feb7SJonathan Lemon
168db57feb7SJonathan Lemon static int
idad_attach(device_t dev)16981530866SMatthew N. Dodd idad_attach(device_t dev)
170db57feb7SJonathan Lemon {
171db57feb7SJonathan Lemon struct ida_drive_info dinfo;
17281530866SMatthew N. Dodd struct idad_softc *drv;
173db57feb7SJonathan Lemon device_t parent;
174db57feb7SJonathan Lemon int error;
175db57feb7SJonathan Lemon
17681530866SMatthew N. Dodd drv = (struct idad_softc *)device_get_softc(dev);
177db57feb7SJonathan Lemon parent = device_get_parent(dev);
1780718ed92SMatthew N. Dodd drv->dev = dev;
179db57feb7SJonathan Lemon drv->controller = (struct ida_softc *)device_get_softc(parent);
180db57feb7SJonathan Lemon drv->unit = device_get_unit(dev);
1816b5b57aeSJohn Baldwin drv->drive = (intptr_t)device_get_ivars(dev);
182db57feb7SJonathan Lemon
1836b5b57aeSJohn Baldwin mtx_lock(&drv->controller->lock);
184db57feb7SJonathan Lemon error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
18555d782fcSJonathan Lemon &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
1866b5b57aeSJohn Baldwin mtx_unlock(&drv->controller->lock);
187db57feb7SJonathan Lemon if (error) {
188db57feb7SJonathan Lemon device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
189db57feb7SJonathan Lemon return (ENXIO);
190db57feb7SJonathan Lemon }
191db57feb7SJonathan Lemon
192b7279e13SMatthew N. Dodd drv->cylinders = dinfo.dp.ncylinders;
193b7279e13SMatthew N. Dodd drv->heads = dinfo.dp.nheads;
194b7279e13SMatthew N. Dodd drv->sectors = dinfo.dp.nsectors;
1951277c3baSJonathan Lemon drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
196db57feb7SJonathan Lemon drv->secperunit = dinfo.secperunit;
197db57feb7SJonathan Lemon
198db57feb7SJonathan Lemon /* XXX
199db57feb7SJonathan Lemon * other initialization
200db57feb7SJonathan Lemon */
201db57feb7SJonathan Lemon device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
202db57feb7SJonathan Lemon drv->secperunit / ((1024 * 1024) / drv->secsize),
203db57feb7SJonathan Lemon drv->secperunit, drv->secsize);
204db57feb7SJonathan Lemon
2050b7ed341SPoul-Henning Kamp drv->disk = disk_alloc();
2060b7ed341SPoul-Henning Kamp drv->disk->d_strategy = idad_strategy;
2070b7ed341SPoul-Henning Kamp drv->disk->d_name = "idad";
2080b7ed341SPoul-Henning Kamp drv->disk->d_dump = idad_dump;
2090b7ed341SPoul-Henning Kamp drv->disk->d_sectorsize = drv->secsize;
2100b7ed341SPoul-Henning Kamp drv->disk->d_mediasize = (off_t)drv->secperunit * drv->secsize;
2110b7ed341SPoul-Henning Kamp drv->disk->d_fwsectors = drv->sectors;
2120b7ed341SPoul-Henning Kamp drv->disk->d_fwheads = drv->heads;
2130b7ed341SPoul-Henning Kamp drv->disk->d_drv1 = drv;
2140b7ed341SPoul-Henning Kamp drv->disk->d_maxsize = DFLTPHYS; /* XXX guess? */
2150b7ed341SPoul-Henning Kamp drv->disk->d_unit = drv->unit;
2160b7ed341SPoul-Henning Kamp disk_create(drv->disk, DISK_VERSION);
217ee7eb00eSJonathan Lemon
218db57feb7SJonathan Lemon return (0);
219db57feb7SJonathan Lemon }
220db57feb7SJonathan Lemon
221ee7eb00eSJonathan Lemon static int
idad_detach(device_t dev)22281530866SMatthew N. Dodd idad_detach(device_t dev)
223ee7eb00eSJonathan Lemon {
22481530866SMatthew N. Dodd struct idad_softc *drv;
225ee7eb00eSJonathan Lemon
22681530866SMatthew N. Dodd drv = (struct idad_softc *)device_get_softc(dev);
2270b7ed341SPoul-Henning Kamp disk_destroy(drv->disk);
228ee7eb00eSJonathan Lemon return (0);
229ee7eb00eSJonathan Lemon }
230