xref: /freebsd/sys/dev/aac/aac_disk.c (revision c98323078dede7579020518ec84cdcb478e5c142)
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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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/disk.h>
44 
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 
48 #include <machine/md_var.h>
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 
52 #include <dev/aac/aacreg.h>
53 #include <dev/aac/aac_ioctl.h>
54 #include <dev/aac/aacvar.h>
55 
56 /*
57  * Interface to parent.
58  */
59 static int aac_disk_probe(device_t dev);
60 static int aac_disk_attach(device_t dev);
61 static int aac_disk_detach(device_t dev);
62 
63 /*
64  * Interface to the device switch.
65  */
66 static	disk_open_t	aac_disk_open;
67 static	disk_close_t	aac_disk_close;
68 static	disk_strategy_t	aac_disk_strategy;
69 static	dumper_t	aac_disk_dump;
70 
71 static devclass_t	aac_disk_devclass;
72 
73 static device_method_t aac_disk_methods[] = {
74 	DEVMETHOD(device_probe,	aac_disk_probe),
75 	DEVMETHOD(device_attach,	aac_disk_attach),
76 	DEVMETHOD(device_detach,	aac_disk_detach),
77 	{ 0, 0 }
78 };
79 
80 static driver_t aac_disk_driver = {
81 	"aacd",
82 	aac_disk_methods,
83 	sizeof(struct aac_disk)
84 };
85 
86 #define AAC_MAXIO	65536
87 
88 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
89 
90 /* sysctl tunables */
91 static unsigned int aac_iosize_max = AAC_MAXIO;	/* due to limits of the card */
92 TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max);
93 
94 SYSCTL_DECL(_hw_aac);
95 SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RDTUN, &aac_iosize_max, 0,
96 	    "Max I/O size per transfer to an array");
97 
98 /*
99  * Handle open from generic layer.
100  *
101  * This is called by the diskslice code on first open in order to get the
102  * basic device geometry paramters.
103  */
104 static int
105 aac_disk_open(struct disk *dp)
106 {
107 	struct aac_disk	*sc;
108 
109 	debug_called(4);
110 
111 	sc = (struct aac_disk *)dp->d_drv1;
112 
113 	if (sc == NULL) {
114 		printf("aac_disk_open: No Softc\n");
115 		return (ENXIO);
116 	}
117 
118 	/* check that the controller is up and running */
119 	if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
120 		printf("Controller Suspended controller state = 0x%x\n",
121 		       sc->ad_controller->aac_state);
122 		return(ENXIO);
123 	}
124 
125 	sc->ad_flags |= AAC_DISK_OPEN;
126 	return (0);
127 }
128 
129 /*
130  * Handle last close of the disk device.
131  */
132 static int
133 aac_disk_close(struct disk *dp)
134 {
135 	struct aac_disk	*sc;
136 
137 	debug_called(4);
138 
139 	sc = (struct aac_disk *)dp->d_drv1;
140 
141 	if (sc == NULL)
142 		return (ENXIO);
143 
144 	sc->ad_flags &= ~AAC_DISK_OPEN;
145 	return (0);
146 }
147 
148 /*
149  * Handle an I/O request.
150  */
151 static void
152 aac_disk_strategy(struct bio *bp)
153 {
154 	struct aac_disk	*sc;
155 
156 	debug_called(4);
157 
158 	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
159 
160 	/* bogus disk? */
161 	if (sc == NULL) {
162 		bp->bio_flags |= BIO_ERROR;
163 		bp->bio_error = EINVAL;
164 		biodone(bp);
165 		return;
166 	}
167 
168 	/* do-nothing operation? */
169 	if (bp->bio_bcount == 0) {
170 		bp->bio_resid = bp->bio_bcount;
171 		biodone(bp);
172 		return;
173 	}
174 
175 	/* perform accounting */
176 
177 	/* pass the bio to the controller - it can work out who we are */
178 	AAC_LOCK_ACQUIRE(&sc->ad_controller->aac_io_lock);
179 	aac_submit_bio(bp);
180 	AAC_LOCK_RELEASE(&sc->ad_controller->aac_io_lock);
181 
182 	return;
183 }
184 
185 /*
186  * Map the S/G elements for doing a dump.
187  *
188  * XXX This does not handle >4GB of RAM.  Fixing it is possible except on
189  *     adapters that cannot do 64bit s/g lists.
190  */
191 static void
192 aac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
193 {
194 	struct aac_fib *fib;
195 	struct aac_blockwrite *bw;
196 	struct aac_sg_table *sg;
197 	int i;
198 
199 	fib = (struct aac_fib *)arg;
200 	bw = (struct aac_blockwrite *)&fib->data[0];
201 	sg = &bw->SgMap;
202 
203 	if (sg != NULL) {
204 		sg->SgCount = nsegs;
205 		for (i = 0; i < nsegs; i++) {
206 			if (segs[i].ds_addr >= BUS_SPACE_MAXADDR_32BIT)
207 				return;
208 			sg->SgEntry[i].SgAddress = segs[i].ds_addr;
209 			sg->SgEntry[i].SgByteCount = segs[i].ds_len;
210 		}
211 		fib->Header.Size = nsegs * sizeof(struct aac_sg_entry);
212 	}
213 }
214 
215 /*
216  * Dump memory out to an array
217  *
218  * Send out one command at a time with up to AAC_MAXIO of data.
219  */
220 static int
221 aac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
222 {
223 	struct aac_disk *ad;
224 	struct aac_softc *sc;
225 	struct aac_fib *fib;
226 	struct aac_blockwrite *bw;
227 	size_t len;
228 	int size;
229 	static bus_dmamap_t dump_datamap;
230 	static int first = 0;
231 	struct disk *dp;
232 
233 	dp = arg;
234 	ad = dp->d_drv1;
235 
236 	if (ad == NULL)
237 		return (EINVAL);
238 
239 	sc= ad->ad_controller;
240 
241 	if (!first) {
242 		first = 1;
243 		if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) {
244 			printf("bus_dmamap_create failed\n");
245 			return (ENOMEM);
246 		}
247 	}
248 
249 	/* Skip aac_alloc_sync_fib().  We don't want to mess with sleep locks */
250 	fib = &sc->aac_common->ac_sync_fib;
251 	bw = (struct aac_blockwrite *)&fib->data[0];
252 
253 	while (length > 0) {
254 		len = (length > AAC_MAXIO) ? AAC_MAXIO : length;
255 		bw->Command = VM_CtBlockWrite;
256 		bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
257 		bw->BlockNumber = offset / AAC_BLOCK_SIZE;
258 		bw->ByteCount = len;
259 		bw->Stable = CUNSTABLE;
260 
261 		/*
262 		 * There really isn't any way to recover from errors or
263 		 * resource shortages here.  Oh well.  Because of that, don't
264 		 * bother trying to send the command from the callback; there
265 		 * is too much required context.
266 		 */
267 		if (bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual,
268 		    len, aac_dump_map_sg, fib, 0) != 0)
269 			return (EIO);
270 
271 		bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
272 		    BUS_DMASYNC_PREWRITE);
273 
274 		/* fib->Header.Size is set in aac_dump_map_sg */
275 		size = fib->Header.Size + sizeof(struct aac_blockwrite);
276 
277 		if (aac_sync_fib(sc, ContainerCommand, 0, fib, size)) {
278 			printf("Error dumping block 0x%jx\n",
279 			       (uintmax_t)physical);
280 			return (EIO);
281 		}
282 
283 		length -= len;
284 		offset += len;
285 		(vm_offset_t)virtual += len;
286 	}
287 
288 	return (0);
289 }
290 
291 /*
292  * Handle completion of an I/O request.
293  */
294 void
295 aac_biodone(struct bio *bp)
296 {
297 	struct aac_disk	*sc;
298 
299 	debug_called(4);
300 
301 	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
302 
303 	if (bp->bio_flags & BIO_ERROR)
304 		disk_err(bp, "hard error", -1, 1);
305 
306 	biodone(bp);
307 }
308 
309 /*
310  * Stub only.
311  */
312 static int
313 aac_disk_probe(device_t dev)
314 {
315 
316 	debug_called(2);
317 
318 	return (0);
319 }
320 
321 /*
322  * Attach a unit to the controller.
323  */
324 static int
325 aac_disk_attach(device_t dev)
326 {
327 	struct aac_disk	*sc;
328 
329 	debug_called(1);
330 
331 	sc = (struct aac_disk *)device_get_softc(dev);
332 
333 	/* initialise our softc */
334 	sc->ad_controller =
335 	    (struct aac_softc *)device_get_softc(device_get_parent(dev));
336 	sc->ad_container = device_get_ivars(dev);
337 	sc->ad_dev = dev;
338 
339 	/*
340 	 * require that extended translation be enabled - other drivers read the
341 	 * disk!
342 	 */
343 	sc->ad_size = sc->ad_container->co_mntobj.Capacity;
344 	if (sc->ad_size >= (2 * 1024 * 1024)) {		/* 2GB */
345 		sc->ad_heads = 255;
346 		sc->ad_sectors = 63;
347 	} else if (sc->ad_size >= (1 * 1024 * 1024)) {	/* 1GB */
348 		sc->ad_heads = 128;
349 		sc->ad_sectors = 32;
350 	} else {
351 		sc->ad_heads = 64;
352 		sc->ad_sectors = 32;
353 	}
354 	sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
355 
356 	device_printf(dev, "%uMB (%u sectors)\n",
357 		      sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
358 		      sc->ad_size);
359 
360 	/* attach a generic disk device to ourselves */
361 	sc->unit = device_get_unit(dev);
362 	sc->ad_disk = disk_alloc();
363 	sc->ad_disk->d_drv1 = sc;
364 	sc->ad_disk->d_name = "aacd";
365 	sc->ad_disk->d_maxsize = aac_iosize_max;
366 	sc->ad_disk->d_open = aac_disk_open;
367 	sc->ad_disk->d_close = aac_disk_close;
368 	sc->ad_disk->d_strategy = aac_disk_strategy;
369 	sc->ad_disk->d_dump = aac_disk_dump;
370 	sc->ad_disk->d_sectorsize = AAC_BLOCK_SIZE;
371 	sc->ad_disk->d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE;
372 	sc->ad_disk->d_fwsectors = sc->ad_sectors;
373 	sc->ad_disk->d_fwheads = sc->ad_heads;
374 	sc->ad_disk->d_unit = sc->unit;
375 	disk_create(sc->ad_disk, DISK_VERSION);
376 
377 	return (0);
378 }
379 
380 /*
381  * Disconnect ourselves from the system.
382  */
383 static int
384 aac_disk_detach(device_t dev)
385 {
386 	struct aac_disk *sc;
387 
388 	debug_called(2);
389 
390 	sc = (struct aac_disk *)device_get_softc(dev);
391 
392 	if (sc->ad_flags & AAC_DISK_OPEN)
393 		return(EBUSY);
394 
395 	disk_destroy(sc->ad_disk);
396 
397 	return(0);
398 }
399