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