xref: /dragonfly/sys/dev/raid/ida/ida_disk.c (revision 4a65f651)
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: src/sys/dev/ida/ida_disk.c,v 1.12.2.6 2001/11/27 20:21:02 ps Exp $
27  * $DragonFly: src/sys/dev/raid/ida/ida_disk.c,v 1.16 2007/06/17 23:50:16 dillon Exp $
28  */
29 
30 /*
31  * Disk driver for Compaq SMART RAID adapters.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 
39 #include <sys/buf.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/cons.h>
43 #include <sys/devicestat.h>
44 #include <sys/disk.h>
45 #include <sys/dtype.h>
46 #include <sys/rman.h>
47 #include <sys/thread2.h>
48 
49 #include <machine/clock.h>
50 #include <machine/md_var.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 
55 #include "idareg.h"
56 #include "idavar.h"
57 
58 /* prototypes */
59 static int idad_probe(device_t dev);
60 static int idad_attach(device_t dev);
61 static int idad_detach(device_t dev);
62 
63 static	d_open_t	idad_open;
64 static	d_close_t	idad_close;
65 static	d_strategy_t	idad_strategy;
66 static	d_dump_t	idad_dump;
67 
68 #define IDAD_BDEV_MAJOR	29
69 #define IDAD_CDEV_MAJOR	109
70 
71 static struct dev_ops id_ops = {
72 	{ "idad", IDAD_CDEV_MAJOR, D_DISK },
73 	.d_open =	idad_open,
74 	.d_close =	idad_close,
75 	.d_read =	physread,
76 	.d_write =	physwrite,
77 	.d_strategy =	idad_strategy,
78 	.d_dump =	idad_dump,
79 };
80 
81 static devclass_t	idad_devclass;
82 
83 static device_method_t idad_methods[] = {
84 	DEVMETHOD(device_probe,		idad_probe),
85 	DEVMETHOD(device_attach,	idad_attach),
86 	DEVMETHOD(device_detach,	idad_detach),
87 	{ 0, 0 }
88 };
89 
90 static driver_t idad_driver = {
91 	"idad",
92 	idad_methods,
93 	sizeof(struct idad_softc)
94 };
95 
96 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
97 
98 static __inline struct idad_softc *
99 idad_getsoftc(cdev_t dev)
100 {
101 
102 	return ((struct idad_softc *)dev->si_drv1);
103 }
104 
105 static int
106 idad_open(struct dev_open_args *ap)
107 {
108 	cdev_t dev = ap->a_head.a_dev;
109 	struct idad_softc *drv;
110 
111 	drv = idad_getsoftc(dev);
112 	if (drv == NULL)
113 		return (ENXIO);
114 #if 0
115 	bzero(&info, sizeof(info));
116 	info.d_media_blksize = drv->secsize;		/* mandatory */
117 	info.d_media_blocks = drv->secperunit;
118 
119 	info.d_secpertrack = drv->sectors;		/* optional */
120 	info.d_type = DTYPE_SCSI;
121 	info.d_nheads = drv->heads;
122 	info.d_ncylinders = drv->cylinders;
123 	info.d_secpercyl = drv->sectors * drv->heads;
124 
125 	disk_setdiskinfo(&drv->disk, &info);
126 #endif
127 	return (0);
128 }
129 
130 static int
131 idad_close(struct dev_close_args *ap)
132 {
133 	cdev_t dev = ap->a_head.a_dev;
134 	struct idad_softc *drv;
135 
136 	drv = idad_getsoftc(dev);
137 	if (drv == NULL)
138 		return (ENXIO);
139 	return (0);
140 }
141 
142 /*
143  * Read/write routine for a buffer.  Finds the proper unit, range checks
144  * arguments, and schedules the transfer.  Does not wait for the transfer
145  * to complete.  Multi-page transfers are supported.  All I/O requests must
146  * be a multiple of a sector in length.
147  */
148 static int
149 idad_strategy(struct dev_strategy_args *ap)
150 {
151 	cdev_t dev = ap->a_head.a_dev;
152 	struct bio *bio = ap->a_bio;
153 	struct buf *bp = bio->bio_buf;
154 	struct idad_softc *drv;
155 
156 	drv = idad_getsoftc(dev);
157 	if (drv == NULL) {
158     		bp->b_error = EINVAL;
159 		goto bad;
160 	}
161 
162 	/*
163 	 * software write protect check
164 	 */
165 	if ((drv->flags & DRV_WRITEPROT) && bp->b_cmd != BUF_CMD_READ) {
166 		bp->b_error = EROFS;
167 		goto bad;
168 	}
169 
170 	/*
171 	 * If it's a null transfer, return immediately
172 	 */
173 	if (bp->b_bcount == 0)
174 		goto done;
175 
176 	bio->bio_driver_info = drv;
177 	crit_enter();
178 	devstat_start_transaction(&drv->stats);
179 	ida_submit_buf(drv->controller, bio);
180 	crit_exit();
181 	return(0);
182 
183 bad:
184 	bp->b_flags |= B_ERROR;
185 
186 done:
187 	/*
188 	 * Correctly set the buf to indicate a completed transfer
189 	 */
190 	bp->b_resid = bp->b_bcount;
191 	biodone(bio);
192 	return(0);
193 }
194 
195 static int
196 idad_dump(struct dev_dump_args *ap)
197 {
198 	cdev_t dev = ap->a_head.a_dev;
199 	struct idad_softc *drv;
200 	long blkcnt;
201 	int i, error, dumppages;
202         caddr_t va;
203 	vm_offset_t addr, a;
204 
205 	drv = idad_getsoftc(dev);
206 	if (drv == NULL)
207 		return (ENXIO);
208 
209 	addr = 0;
210 	blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
211 
212 	while (ap->a_count > 0) {
213 		va = NULL;
214 
215 		dumppages = imin(ap->a_count / blkcnt, MAXDUMPPGS);
216 
217 		for (i = 0; i < dumppages; i++) {
218 			a = addr + (i * PAGE_SIZE);
219 			if (is_physical_memory(a))
220 				va = pmap_kenter_temporary(trunc_page(a), i);
221 			else
222 				va = pmap_kenter_temporary(trunc_page(0), i);
223 		}
224 
225 		error = ida_command(drv->controller, CMD_WRITE, va,
226 		    PAGE_SIZE * dumppages, drv->drive, ap->a_blkno, DMA_DATA_OUT);
227 		if (error)
228 			return (error);
229 
230 		if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
231 			return (EINTR);
232 
233 		ap->a_blkno += blkcnt * dumppages;
234 		ap->a_count -= blkcnt * dumppages;
235 		addr += PAGE_SIZE * dumppages;
236 	}
237 	return (0);
238 }
239 
240 void
241 idad_intr(struct bio *bio)
242 {
243 	struct idad_softc *drv = (struct idad_softc *)bio->bio_driver_info;
244 	struct buf *bp = bio->bio_buf;
245 
246 	if (bp->b_flags & B_ERROR)
247 		bp->b_error = EIO;
248 	else
249 		bp->b_resid = 0;
250 
251 	devstat_end_transaction_buf(&drv->stats, bp);
252 	biodone(bio);
253 }
254 
255 static int
256 idad_probe(device_t dev)
257 {
258 
259 	device_set_desc(dev, "Compaq Logical Drive");
260 	return (0);
261 }
262 
263 static int
264 idad_attach(device_t dev)
265 {
266 	struct ida_drive_info dinfo;
267 	struct disk_info info;
268 	struct idad_softc *drv;
269 	device_t parent;
270 	cdev_t dsk;
271 	int error;
272 
273 	drv = (struct idad_softc *)device_get_softc(dev);
274 	parent = device_get_parent(dev);
275 	drv->controller = (struct ida_softc *)device_get_softc(parent);
276 	drv->unit = device_get_unit(dev);
277 	drv->drive = drv->controller->num_drives;
278 	drv->controller->num_drives++;
279 
280 	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
281 	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
282 	if (error) {
283 		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
284 		return (ENXIO);
285 	}
286 
287 	drv->cylinders = dinfo.ncylinders;
288 	drv->heads = dinfo.nheads;
289 	drv->sectors = dinfo.nsectors;
290 	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
291 	drv->secperunit = dinfo.secperunit;
292 
293 	/* XXX
294 	 * other initialization
295 	 */
296 	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
297 	    drv->secperunit / ((1024 * 1024) / drv->secsize),
298 	    drv->secperunit, drv->secsize);
299 
300 	devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize,
301 	    DEVSTAT_NO_ORDERED_TAGS,
302 	    DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER,
303 	    DEVSTAT_PRIORITY_ARRAY);
304 
305 	dsk = disk_create(drv->unit, &drv->disk, &id_ops);
306 
307 	dsk->si_drv1 = drv;
308 	dsk->si_iosize_max = DFLTPHYS;		/* XXX guess? */
309 
310 	/*
311 	 * Set disk info, as it appears that all needed data is available already.
312 	 * Setting the disk info will also cause the probing to start.
313 	 */
314 	bzero(&info, sizeof(info));
315 	info.d_media_blksize = drv->secsize;		/* mandatory */
316 	info.d_media_blocks = drv->secperunit;
317 
318 	info.d_secpertrack = drv->sectors;		/* optional */
319 	info.d_type = DTYPE_SCSI;
320 	info.d_nheads = drv->heads;
321 	info.d_ncylinders = drv->cylinders;
322 	info.d_secpercyl = drv->sectors * drv->heads;
323 
324 	disk_setdiskinfo(&drv->disk, &info);
325 
326 	return (0);
327 }
328 
329 static int
330 idad_detach(device_t dev)
331 {
332 	struct idad_softc *drv;
333 
334 	drv = (struct idad_softc *)device_get_softc(dev);
335 	devstat_remove_entry(&drv->stats);
336 	disk_destroy(&drv->disk);
337 	return (0);
338 }
339