xref: /dragonfly/sys/dev/raid/ida/ida_disk.c (revision 49781055)
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.10 2006/02/17 19:18:05 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 
46 #if NPCI > 0
47 #include <machine/bus_memio.h>
48 #endif
49 #include <machine/bus_pio.h>
50 #include <machine/bus.h>
51 #include <machine/clock.h>
52 #include <sys/rman.h>
53 #include <sys/thread2.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <machine/md_var.h>
58 
59 #include "idareg.h"
60 #include "idavar.h"
61 
62 /* prototypes */
63 static int idad_probe(device_t dev);
64 static int idad_attach(device_t dev);
65 static int idad_detach(device_t dev);
66 
67 static	d_open_t	idad_open;
68 static	d_close_t	idad_close;
69 static	d_strategy_t	idad_strategy;
70 static	d_dump_t	idad_dump;
71 
72 #define IDAD_BDEV_MAJOR	29
73 #define IDAD_CDEV_MAJOR	109
74 
75 static struct cdevsw id_cdevsw = {
76 	/* name */ 	"idad",
77 	/* maj */	IDAD_CDEV_MAJOR,
78 	/* flags */	D_DISK,
79 	/* port */	NULL,
80 	/* clone */	NULL,
81 
82 	/* open */	idad_open,
83 	/* close */	idad_close,
84 	/* read */	physread,
85 	/* write */	physwrite,
86 	/* ioctl */	noioctl,
87 	/* poll */	nopoll,
88 	/* mmap */	nommap,
89 	/* strategy */	idad_strategy,
90 	/* dump */	idad_dump,
91 	/* psize */ 	nopsize
92 };
93 
94 static devclass_t	idad_devclass;
95 
96 static device_method_t idad_methods[] = {
97 	DEVMETHOD(device_probe,		idad_probe),
98 	DEVMETHOD(device_attach,	idad_attach),
99 	DEVMETHOD(device_detach,	idad_detach),
100 	{ 0, 0 }
101 };
102 
103 static driver_t idad_driver = {
104 	"idad",
105 	idad_methods,
106 	sizeof(struct idad_softc)
107 };
108 
109 DRIVER_MODULE(idad, ida, idad_driver, idad_devclass, 0, 0);
110 
111 static __inline struct idad_softc *
112 idad_getsoftc(dev_t dev)
113 {
114 
115 	return ((struct idad_softc *)dev->si_drv1);
116 }
117 
118 static int
119 idad_open(dev_t dev, int flags, int fmt, d_thread_t *td)
120 {
121 	struct idad_softc *drv;
122 	struct disklabel *label;
123 
124 	drv = idad_getsoftc(dev);
125 	if (drv == NULL)
126 		return (ENXIO);
127 
128 	label = &drv->disk.d_label;
129 	bzero(label, sizeof(*label));
130 	label->d_type = DTYPE_SCSI;
131 	label->d_secsize = drv->secsize;
132 	label->d_nsectors = drv->sectors;
133 	label->d_ntracks = drv->heads;
134 	label->d_ncylinders = drv->cylinders;
135 	label->d_secpercyl = drv->sectors * drv->heads;
136 	label->d_secperunit = drv->secperunit;
137 
138 	return (0);
139 }
140 
141 static int
142 idad_close(dev_t dev, int flags, int fmt, d_thread_t *td)
143 {
144 	struct idad_softc *drv;
145 
146 	drv = idad_getsoftc(dev);
147 	if (drv == NULL)
148 		return (ENXIO);
149 	return (0);
150 }
151 
152 /*
153  * Read/write routine for a buffer.  Finds the proper unit, range checks
154  * arguments, and schedules the transfer.  Does not wait for the transfer
155  * to complete.  Multi-page transfers are supported.  All I/O requests must
156  * be a multiple of a sector in length.
157  */
158 static void
159 idad_strategy(dev_t dev, struct bio *bio)
160 {
161 	struct buf *bp = bio->bio_buf;
162 	struct idad_softc *drv;
163 
164 	drv = idad_getsoftc(dev);
165 	if (drv == NULL) {
166     		bp->b_error = EINVAL;
167 		goto bad;
168 	}
169 
170 	/*
171 	 * software write protect check
172 	 */
173 	if (drv->flags & DRV_WRITEPROT && (bp->b_flags & B_READ) == 0) {
174 		bp->b_error = EROFS;
175 		goto bad;
176 	}
177 
178 	/*
179 	 * If it's a null transfer, return immediately
180 	 */
181 	if (bp->b_bcount == 0)
182 		goto done;
183 
184 	bio->bio_driver_info = drv;
185 	crit_enter();
186 	devstat_start_transaction(&drv->stats);
187 	ida_submit_buf(drv->controller, bio);
188 	crit_exit();
189 	return;
190 
191 bad:
192 	bp->b_flags |= B_ERROR;
193 
194 done:
195 	/*
196 	 * Correctly set the buf to indicate a completed transfer
197 	 */
198 	bp->b_resid = bp->b_bcount;
199 	biodone(bio);
200 }
201 
202 static int
203 idad_dump(dev_t dev, u_int count, u_int blkno, u_int secsize)
204 {
205 	struct idad_softc *drv;
206 	long blkcnt;
207 	int i, error, dumppages;
208         caddr_t va;
209 	vm_offset_t addr, a;
210 
211 	drv = idad_getsoftc(dev);
212 	if (drv == NULL)
213 		return (ENXIO);
214 
215 	addr = 0;
216 	blkcnt = howmany(PAGE_SIZE, secsize);
217 
218 	while (count > 0) {
219 		va = NULL;
220 
221 		dumppages = imin(count / blkcnt, MAXDUMPPGS);
222 
223 		for (i = 0; i < dumppages; i++) {
224 			a = addr + (i * PAGE_SIZE);
225 			if (is_physical_memory(a))
226 				va = pmap_kenter_temporary(trunc_page(a), i);
227 			else
228 				va = pmap_kenter_temporary(trunc_page(0), i);
229 		}
230 
231 		error = ida_command(drv->controller, CMD_WRITE, va,
232 		    PAGE_SIZE * dumppages, drv->drive, blkno, DMA_DATA_OUT);
233 		if (error)
234 			return (error);
235 
236 		if (dumpstatus(addr, (off_t)count * DEV_BSIZE) < 0)
237 			return (EINTR);
238 
239 		blkno += blkcnt * dumppages;
240 		count -= blkcnt * dumppages;
241 		addr += PAGE_SIZE * dumppages;
242 	}
243 	return (0);
244 }
245 
246 void
247 idad_intr(struct bio *bio)
248 {
249 	struct idad_softc *drv = (struct idad_softc *)bio->bio_driver_info;
250 	struct buf *bp = bio->bio_buf;
251 
252 	if (bp->b_flags & B_ERROR)
253 		bp->b_error = EIO;
254 	else
255 		bp->b_resid = 0;
256 
257 	devstat_end_transaction_buf(&drv->stats, bp);
258 	biodone(bio);
259 }
260 
261 static int
262 idad_probe(device_t dev)
263 {
264 
265 	device_set_desc(dev, "Compaq Logical Drive");
266 	return (0);
267 }
268 
269 static int
270 idad_attach(device_t dev)
271 {
272 	struct ida_drive_info dinfo;
273 	struct idad_softc *drv;
274 	device_t parent;
275 	dev_t dsk;
276 	int error;
277 
278 	drv = (struct idad_softc *)device_get_softc(dev);
279 	parent = device_get_parent(dev);
280 	drv->controller = (struct ida_softc *)device_get_softc(parent);
281 	drv->unit = device_get_unit(dev);
282 	drv->drive = drv->controller->num_drives;
283 	drv->controller->num_drives++;
284 
285 	error = ida_command(drv->controller, CMD_GET_LOG_DRV_INFO,
286 	    &dinfo, sizeof(dinfo), drv->drive, 0, DMA_DATA_IN);
287 	if (error) {
288 		device_printf(dev, "CMD_GET_LOG_DRV_INFO failed\n");
289 		return (ENXIO);
290 	}
291 
292 	drv->cylinders = dinfo.ncylinders;
293 	drv->heads = dinfo.nheads;
294 	drv->sectors = dinfo.nsectors;
295 	drv->secsize = dinfo.secsize == 0 ? 512 : dinfo.secsize;
296 	drv->secperunit = dinfo.secperunit;
297 
298 	/* XXX
299 	 * other initialization
300 	 */
301 	device_printf(dev, "%uMB (%u sectors), blocksize=%d\n",
302 	    drv->secperunit / ((1024 * 1024) / drv->secsize),
303 	    drv->secperunit, drv->secsize);
304 
305 	devstat_add_entry(&drv->stats, "idad", drv->unit, drv->secsize,
306 	    DEVSTAT_NO_ORDERED_TAGS,
307 	    DEVSTAT_TYPE_STORARRAY| DEVSTAT_TYPE_IF_OTHER,
308 	    DEVSTAT_PRIORITY_ARRAY);
309 
310 	dsk = disk_create(drv->unit, &drv->disk, 0, &id_cdevsw);
311 
312 	dsk->si_drv1 = drv;
313 	dsk->si_iosize_max = DFLTPHYS;		/* XXX guess? */
314 
315 	return (0);
316 }
317 
318 static int
319 idad_detach(device_t dev)
320 {
321 	struct idad_softc *drv;
322 
323 	drv = (struct idad_softc *)device_get_softc(dev);
324 	devstat_remove_entry(&drv->stats);
325 	disk_destroy(&drv->disk);
326 	return (0);
327 }
328