xref: /freebsd/sys/dev/ida/ida_disk.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
1 /*-
2  * Copyright (c) 1999,2000 Jonathan Lemon
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Disk driver for Compaq SMART RAID adapters.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/cons.h>
42 
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <machine/md_var.h>
49 
50 #include <geom/geom_disk.h>
51 
52 #include <dev/ida/idareg.h>
53 #include <dev/ida/idavar.h>
54 
55 /* prototypes */
56 static int idad_probe(device_t dev);
57 static int idad_attach(device_t dev);
58 static int idad_detach(device_t dev);
59 
60 static	d_strategy_t	idad_strategy;
61 static	dumper_t	idad_dump;
62 
63 static devclass_t	idad_devclass;
64 
65 static device_method_t idad_methods[] = {
66 	DEVMETHOD(device_probe,		idad_probe),
67 	DEVMETHOD(device_attach,	idad_attach),
68 	DEVMETHOD(device_detach,	idad_detach),
69 	{ 0, 0 }
70 };
71 
72 static driver_t idad_driver = {
73 	"idad",
74 	idad_methods,
75 	sizeof(struct idad_softc)
76 };
77 
78 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
79 
80 /*
81  * Read/write routine for a buffer.  Finds the proper unit, range checks
82  * arguments, and schedules the transfer.  Does not wait for the transfer
83  * to complete.  Multi-page transfers are supported.  All I/O requests must
84  * be a multiple of a sector in length.
85  */
86 static void
87 idad_strategy(struct bio *bp)
88 {
89 	struct idad_softc *drv;
90 
91 	drv = bp->bio_disk->d_drv1;
92 	if (drv == NULL) {
93     		bp->bio_error = EINVAL;
94 		goto bad;
95 	}
96 
97 	/*
98 	 * software write protect check
99 	 */
100 	if (drv->flags & DRV_WRITEPROT && (bp->bio_cmd == BIO_WRITE)) {
101 		bp->bio_error = EROFS;
102 		goto bad;
103 	}
104 
105 	bp->bio_driver1 = drv;
106 	ida_submit_buf(drv->controller, bp);
107 	return;
108 
109 bad:
110 	bp->bio_flags |= BIO_ERROR;
111 
112 	/*
113 	 * Correctly set the buf to indicate a completed transfer
114 	 */
115 	bp->bio_resid = bp->bio_bcount;
116 	biodone(bp);
117 	return;
118 }
119 
120 static int
121 idad_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
122 {
123 
124 	struct idad_softc *drv;
125 	int error = 0;
126 	struct disk *dp;
127 
128 	dp = arg;
129 	drv = dp->d_drv1;
130 	if (drv == NULL)
131 		return (ENXIO);
132 
133 	drv->controller->flags &= ~IDA_INTERRUPTS;
134 
135 	if (length > 0) {
136 		error = ida_command(drv->controller, CMD_WRITE, virtual,
137 		    length, drv->drive, offset / DEV_BSIZE, DMA_DATA_OUT);
138 	}
139 	drv->controller->flags |= IDA_INTERRUPTS;
140 	return (error);
141 }
142 
143 void
144 idad_intr(struct bio *bp)
145 {
146 	struct idad_softc *drv;
147 
148 	drv = bp->bio_disk->d_drv1;
149 
150 	if (bp->bio_flags & BIO_ERROR)
151 		bp->bio_error = EIO;
152 	else
153 		bp->bio_resid = 0;
154 
155 	biodone(bp);
156 }
157 
158 static int
159 idad_probe(device_t dev)
160 {
161 
162 	device_set_desc(dev, "Compaq Logical Drive");
163 	return (0);
164 }
165 
166 static int
167 idad_attach(device_t dev)
168 {
169 	struct ida_drive_info dinfo;
170 	struct idad_softc *drv;
171 	device_t parent;
172 	int error;
173 
174 	drv = (struct idad_softc *)device_get_softc(dev);
175 	parent = device_get_parent(dev);
176 	drv->dev = dev;
177 	drv->controller = (struct ida_softc *)device_get_softc(parent);
178 	drv->unit = device_get_unit(dev);
179 	drv->drive = (intptr_t)device_get_ivars(dev);
180 
181 	mtx_lock(&drv->controller->lock);
182 	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
183 	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
184 	mtx_unlock(&drv->controller->lock);
185 	if (error) {
186 		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
187 		return (ENXIO);
188 	}
189 
190 	drv->cylinders = dinfo.dp.ncylinders;
191 	drv->heads = dinfo.dp.nheads;
192 	drv->sectors = dinfo.dp.nsectors;
193 	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
194 	drv->secperunit = dinfo.secperunit;
195 
196 	/* XXX
197 	 * other initialization
198 	 */
199 	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
200 	    drv->secperunit / ((1024 * 1024) / drv->secsize),
201 	    drv->secperunit, drv->secsize);
202 
203 	drv->disk = disk_alloc();
204 	drv->disk->d_strategy = idad_strategy;
205 	drv->disk->d_name = "idad";
206 	drv->disk->d_dump = idad_dump;
207 	drv->disk->d_sectorsize = drv->secsize;
208 	drv->disk->d_mediasize = (off_t)drv->secperunit * drv->secsize;
209 	drv->disk->d_fwsectors = drv->sectors;
210 	drv->disk->d_fwheads = drv->heads;
211 	drv->disk->d_drv1 = drv;
212 	drv->disk->d_maxsize = DFLTPHYS;		/* XXX guess? */
213 	drv->disk->d_unit = drv->unit;
214 	disk_create(drv->disk, DISK_VERSION);
215 
216 	return (0);
217 }
218 
219 static int
220 idad_detach(device_t dev)
221 {
222 	struct idad_softc *drv;
223 
224 	drv = (struct idad_softc *)device_get_softc(dev);
225 	disk_destroy(drv->disk);
226 	return (0);
227 }
228