xref: /dragonfly/sys/dev/raid/aac/aac_disk.c (revision 1b722dce)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001 Adaptec, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD: src/sys/dev/aac/aac_disk.c,v 1.3.2.8 2003/01/11 18:39:39 scottl Exp $
30  *	$DragonFly: src/sys/dev/raid/aac/aac_disk.c,v 1.20 2008/01/20 03:40:35 pavalos Exp $
31  */
32 
33 #include "opt_aac.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
40 
41 #include <sys/bus.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/disk.h>
45 #include <sys/dtype.h>
46 #include <sys/rman.h>
47 
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 
51 #include <machine/md_var.h>
52 
53 #include "aacreg.h"
54 #include "aac_ioctl.h"
55 #include "aacvar.h"
56 
57 /*
58  * Interface to parent.
59  */
60 static int aac_disk_probe(device_t dev);
61 static int aac_disk_attach(device_t dev);
62 static int aac_disk_detach(device_t dev);
63 
64 /*
65  * Interface to the device switch.
66  */
67 static	d_open_t	aac_disk_open;
68 static	d_close_t	aac_disk_close;
69 static	d_strategy_t	aac_disk_strategy;
70 static	d_dump_t	aac_disk_dump;
71 
72 #define AAC_DISK_CDEV_MAJOR	151
73 
74 static struct dev_ops aac_disk_ops = {
75 	{ "aacd", AAC_DISK_CDEV_MAJOR, D_DISK },
76 	.d_open =		aac_disk_open,
77 	.d_close =		aac_disk_close,
78 	.d_read =		physread,
79 	.d_write =		physwrite,
80 	.d_strategy =		aac_disk_strategy,
81 	.d_dump =		aac_disk_dump,
82 };
83 
84 static devclass_t	aac_disk_devclass;
85 
86 static device_method_t aac_disk_methods[] = {
87 	DEVMETHOD(device_probe,	aac_disk_probe),
88 	DEVMETHOD(device_attach,	aac_disk_attach),
89 	DEVMETHOD(device_detach,	aac_disk_detach),
90 	{ 0, 0 }
91 };
92 
93 static driver_t aac_disk_driver = {
94 	"aacd",
95 	aac_disk_methods,
96 	sizeof(struct aac_disk)
97 };
98 
99 #define AAC_MAXIO	65536
100 
101 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
102 
103 /* sysctl tunables */
104 static unsigned int aac_iosize_max = AAC_MAXIO;	/* due to limits of the card */
105 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max);
106 
107 SYSCTL_DECL(_hw_aac);
108 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0,
109 	    "Max I/O size per transfer to an array");
110 
111 /*
112  * Handle open from generic layer.
113  *
114  * This is called by the diskslice code on first open in order to get the
115  * basic device geometry paramters.
116  */
117 static int
118 aac_disk_open(struct dev_open_args *ap)
119 {
120 	cdev_t dev = ap->a_head.a_dev;
121 	struct aac_disk	*sc;
122 
123 	debug_called(0);
124 
125 	sc = (struct aac_disk *)dev->si_drv1;
126 
127 	if (sc == NULL) {
128 		kprintf("aac_disk_open: No Softc\n");
129 		return (ENXIO);
130 	}
131 
132 	/* check that the controller is up and running */
133 	if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
134 		kprintf("Controller Suspended controller state = 0x%x\n",
135 		       sc->ad_controller->aac_state);
136 		return(ENXIO);
137 	}
138 
139 	/* build synthetic label */
140 #if 0
141 	bzero(&info, sizeof(info));
142 	info.d_media_blksize= AAC_BLOCK_SIZE;		/* mandatory */
143 	info.d_media_blocks = sc->ad_size;
144 
145 	info.d_type = DTYPE_ESDI;			/* optional */
146 	info.d_secpertrack   = sc->ad_sectors;
147 	info.d_nheads	= sc->ad_heads;
148 	info.d_ncylinders = sc->ad_cylinders;
149 	info.d_secpercyl  = sc->ad_sectors * sc->ad_heads;
150 
151 	disk_setdiskinfo(&sc->ad_disk, &info);
152 #endif
153 	sc->ad_flags |= AAC_DISK_OPEN;
154 	return (0);
155 }
156 
157 /*
158  * Handle last close of the disk device.
159  */
160 static int
161 aac_disk_close(struct dev_close_args *ap)
162 {
163 	cdev_t dev = ap->a_head.a_dev;
164 	struct aac_disk	*sc;
165 
166 	debug_called(0);
167 
168 	sc = (struct aac_disk *)dev->si_drv1;
169 
170 	if (sc == NULL)
171 		return (ENXIO);
172 
173 	sc->ad_flags &= ~AAC_DISK_OPEN;
174 	return (0);
175 }
176 
177 /*
178  * Handle an I/O request.
179  */
180 static int
181 aac_disk_strategy(struct dev_strategy_args *ap)
182 {
183 	cdev_t dev = ap->a_head.a_dev;
184 	struct bio *bio = ap->a_bio;
185 	struct buf *bp = bio->bio_buf;
186 	struct aac_disk	*sc;
187 
188 	debug_called(4);
189 
190 	sc = (struct aac_disk *)dev->si_drv1;
191 
192 	/* bogus disk? */
193 	if (sc == NULL) {
194 		bp->b_flags |= B_ERROR;
195 		bp->b_error = EINVAL;
196 		biodone(bio);
197 		return(0);
198 	}
199 
200 	/* do-nothing operation? */
201 	if (bp->b_bcount == 0) {
202 		bp->b_resid = bp->b_bcount;
203 		biodone(bio);
204 		return(0);
205 	}
206 
207 	/* perform accounting */
208 
209 	/* pass the bio to the controller - it can work out who we are */
210 	AAC_LOCK_ACQUIRE(&sc->ad_controller->aac_io_lock);
211 	devstat_start_transaction(&sc->ad_stats);
212 	aac_submit_bio(sc, bio);
213 	AAC_LOCK_RELEASE(&sc->ad_controller->aac_io_lock);
214 
215 	return(0);
216 }
217 
218 /*
219  * Dump memory out to an array
220  *
221  * This queues blocks of memory of size AAC_MAXIO to the controller and waits
222  * for the controller to complete the requests.
223  */
224 static int
225 aac_disk_dump(struct dev_dump_args *ap)
226 {
227 	cdev_t dev = ap->a_head.a_dev;
228 	struct aac_disk *ad;
229 	struct aac_softc *sc;
230 	vm_offset_t addr;
231 	long blkcnt;
232 	int dumppages;
233 	int i, error;
234 
235 	ad = dev->si_drv1;
236 	addr = 0;
237 	dumppages = AAC_MAXIO / PAGE_SIZE;
238 
239 	if (ad == NULL)
240 		return (ENXIO);
241 
242 	sc= ad->ad_controller;
243 
244 	blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
245 
246 	while (ap->a_count > 0) {
247 		caddr_t va = NULL;
248 
249 		if ((ap->a_count / blkcnt) < dumppages)
250 			dumppages = ap->a_count / blkcnt;
251 
252 		for (i = 0; i < dumppages; ++i) {
253 			vm_offset_t a = addr + (i * PAGE_SIZE);
254 			if (is_physical_memory(a)) {
255 				va = pmap_kenter_temporary(trunc_page(a), i);
256 			} else {
257 				va = pmap_kenter_temporary(trunc_page(0), i);
258 			}
259 		}
260 
261 retry:
262 		/*
263 		 * Queue the block to the controller.  If the queue is full,
264 		 * EBUSY will be returned.
265 		 */
266 		error = aac_dump_enqueue(ad, ap->a_blkno, va, dumppages);
267 		if (error && (error != EBUSY))
268 			return (error);
269 
270 		if (!error) {
271 			if (dumpstatus(addr, (off_t)(ap->a_count * DEV_BSIZE)) < 0)
272 			return (EINTR);
273 
274 			ap->a_blkno += blkcnt * dumppages;
275 			ap->a_count -= blkcnt * dumppages;
276 			addr += PAGE_SIZE * dumppages;
277 			if (ap->a_count > 0)
278 			continue;
279 		}
280 
281 		/*
282 		 * Either the queue was full on the last attemp, or we have no
283 		 * more data to dump.  Let the queue drain out and retry the
284 		 * block if the queue was full.
285 		 */
286 		aac_dump_complete(sc);
287 
288 		if (error == EBUSY)
289 			goto retry;
290 	}
291 
292 	return (0);
293 }
294 
295 /*
296  * Handle completion of an I/O request.
297  */
298 void
299 aac_biodone(struct bio *bio, const char *code)
300 {
301 	struct buf *bp = bio->bio_buf;
302 	struct aac_disk	*sc;
303 
304 	debug_called(4);
305 
306 	sc = (struct aac_disk *)bio->bio_driver_info;
307 
308 	devstat_end_transaction_buf(&sc->ad_stats, bp);
309 	if (bp->b_flags & B_ERROR) {
310 		diskerr(bio, sc->ad_dev_t,
311 			code, 0, 0);
312 	}
313 	biodone(bio);
314 }
315 
316 /*
317  * Stub only.
318  */
319 static int
320 aac_disk_probe(device_t dev)
321 {
322 
323 	debug_called(2);
324 
325 	return (0);
326 }
327 
328 /*
329  * Attach a unit to the controller.
330  */
331 static int
332 aac_disk_attach(device_t dev)
333 {
334 	struct disk_info info;
335 	struct aac_disk	*sc;
336 
337 	debug_called(0);
338 
339 	sc = (struct aac_disk *)device_get_softc(dev);
340 
341 	/* initialise our softc */
342 	sc->ad_controller =
343 	    (struct aac_softc *)device_get_softc(device_get_parent(dev));
344 	sc->ad_container = device_get_ivars(dev);
345 	sc->ad_dev = dev;
346 
347 	/*
348 	 * require that extended translation be enabled - other drivers read the
349 	 * disk!
350 	 */
351 	sc->ad_size = sc->ad_container->co_mntobj.Capacity;
352 	if (sc->ad_size >= (2 * 1024 * 1024)) {		/* 2GB */
353 		sc->ad_heads = 255;
354 		sc->ad_sectors = 63;
355 	} else if (sc->ad_size >= (1 * 1024 * 1024)) {	/* 1GB */
356 		sc->ad_heads = 128;
357 		sc->ad_sectors = 32;
358 	} else {
359 		sc->ad_heads = 64;
360 		sc->ad_sectors = 32;
361 	}
362 	sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
363 
364 	device_printf(dev, "%uMB (%u sectors)\n",
365 		      sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
366 		      sc->ad_size);
367 
368 	devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev),
369 			  AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS,
370 			  DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER,
371 			  DEVSTAT_PRIORITY_ARRAY);
372 
373 	/* attach a generic disk device to ourselves */
374 	sc->ad_dev_t = disk_create(device_get_unit(dev), &sc->ad_disk,
375 				   &aac_disk_ops);
376 	sc->ad_dev_t->si_drv1 = sc;
377 
378 	sc->ad_dev_t->si_iosize_max = aac_iosize_max;
379 	sc->unit = device_get_unit(dev);
380 
381 	/*
382 	 * Set disk info, as it appears that all needed data is available already.
383 	 * Setting the disk info will also cause the probing to start.
384 	 */
385 	bzero(&info, sizeof(info));
386 	info.d_media_blksize= AAC_BLOCK_SIZE;		/* mandatory */
387 	info.d_media_blocks = sc->ad_size;
388 
389 	info.d_type = DTYPE_ESDI;			/* optional */
390 	info.d_secpertrack   = sc->ad_sectors;
391 	info.d_nheads	= sc->ad_heads;
392 	info.d_ncylinders = sc->ad_cylinders;
393 	info.d_secpercyl  = sc->ad_sectors * sc->ad_heads;
394 
395 	disk_setdiskinfo(&sc->ad_disk, &info);
396 
397 	return (0);
398 }
399 
400 /*
401  * Disconnect ourselves from the system.
402  */
403 static int
404 aac_disk_detach(device_t dev)
405 {
406 	struct aac_disk *sc;
407 
408 	debug_called(2);
409 
410 	sc = (struct aac_disk *)device_get_softc(dev);
411 
412 	if (sc->ad_flags & AAC_DISK_OPEN)
413 		return(EBUSY);
414 
415 	devstat_remove_entry(&sc->ad_stats);
416 	disk_destroy(&sc->ad_disk);
417 	return(0);
418 }
419