xref: /freebsd/sys/dev/aac/aac_disk.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
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 <sys/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 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
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 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
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 	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
157 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
158 
159 	/* bogus disk? */
160 	if (sc == NULL) {
161 		bp->bio_flags |= BIO_ERROR;
162 		bp->bio_error = EINVAL;
163 		biodone(bp);
164 		return;
165 	}
166 
167 	/* do-nothing operation? */
168 	if (bp->bio_bcount == 0) {
169 		bp->bio_resid = bp->bio_bcount;
170 		biodone(bp);
171 		return;
172 	}
173 
174 	/* perform accounting */
175 
176 	/* pass the bio to the controller - it can work out who we are */
177 	mtx_lock(&sc->ad_controller->aac_io_lock);
178 	aac_submit_bio(bp);
179 	mtx_unlock(&sc->ad_controller->aac_io_lock);
180 
181 	return;
182 }
183 
184 /*
185  * Map the S/G elements for doing a dump.
186  */
187 static void
188 aac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
189 {
190 	struct aac_fib *fib;
191 	struct aac_blockwrite *bw;
192 	struct aac_sg_table *sg;
193 	int i;
194 
195 	fib = (struct aac_fib *)arg;
196 	bw = (struct aac_blockwrite *)&fib->data[0];
197 	sg = &bw->SgMap;
198 
199 	if (sg != NULL) {
200 		sg->SgCount = nsegs;
201 		for (i = 0; i < nsegs; i++) {
202 			if (segs[i].ds_addr >= BUS_SPACE_MAXADDR_32BIT)
203 				return;
204 			sg->SgEntry[i].SgAddress = segs[i].ds_addr;
205 			sg->SgEntry[i].SgByteCount = segs[i].ds_len;
206 		}
207 		fib->Header.Size = nsegs * sizeof(struct aac_sg_entry);
208 	}
209 }
210 
211 /*
212  * Map the S/G elements for doing a dump on 64-bit capable devices.
213  */
214 static void
215 aac_dump_map_sg64(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
216 {
217 	struct aac_fib *fib;
218 	struct aac_blockwrite64 *bw;
219 	struct aac_sg_table64 *sg;
220 	int i;
221 
222 	fib = (struct aac_fib *)arg;
223 	bw = (struct aac_blockwrite64 *)&fib->data[0];
224 	sg = &bw->SgMap64;
225 
226 	if (sg != NULL) {
227 		sg->SgCount = nsegs;
228 		for (i = 0; i < nsegs; i++) {
229 			sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
230 			sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
231 		}
232 		fib->Header.Size = nsegs * sizeof(struct aac_sg_entry64);
233 	}
234 }
235 
236 /*
237  * Dump memory out to an array
238  *
239  * Send out one command at a time with up to AAC_MAXIO of data.
240  */
241 static int
242 aac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
243 {
244 	struct aac_disk *ad;
245 	struct aac_softc *sc;
246 	struct aac_fib *fib;
247 	size_t len;
248 	int size;
249 	static bus_dmamap_t dump_datamap;
250 	static int first = 0;
251 	struct disk *dp;
252 	bus_dmamap_callback_t *callback;
253 	u_int32_t command;
254 
255 	dp = arg;
256 	ad = dp->d_drv1;
257 
258 	if (ad == NULL)
259 		return (EINVAL);
260 
261 	sc= ad->ad_controller;
262 
263 	if (!first) {
264 		first = 1;
265 		if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) {
266 			printf("bus_dmamap_create failed\n");
267 			return (ENOMEM);
268 		}
269 	}
270 
271 	/* Skip aac_alloc_sync_fib().  We don't want to mess with sleep locks */
272 	fib = &sc->aac_common->ac_sync_fib;
273 
274 	while (length > 0) {
275 		len = (length > AAC_MAXIO) ? AAC_MAXIO : length;
276 		if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
277 			struct aac_blockwrite *bw;
278 			bw = (struct aac_blockwrite *)&fib->data[0];
279 			bw->Command = VM_CtBlockWrite;
280 			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
281 			bw->BlockNumber = offset / AAC_BLOCK_SIZE;
282 			bw->ByteCount = len;
283 			bw->Stable = CUNSTABLE;
284 			command = ContainerCommand;
285 			callback = aac_dump_map_sg;
286 			size = sizeof(struct aac_blockwrite);
287 		} else {
288 			struct aac_blockwrite64 *bw;
289 			bw = (struct aac_blockwrite64 *)&fib->data[0];
290 			bw->Command = VM_CtHostWrite64;
291 			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
292 			bw->BlockNumber = offset / AAC_BLOCK_SIZE;
293 			bw->SectorCount = len / AAC_BLOCK_SIZE;
294 			bw->Pad = 0;
295 			bw->Flags = 0;
296 			command = ContainerCommand64;
297 			callback = aac_dump_map_sg64;
298 			size = sizeof(struct aac_blockwrite64);
299 		}
300 
301 		/*
302 		 * There really isn't any way to recover from errors or
303 		 * resource shortages here.  Oh well.  Because of that, don't
304 		 * bother trying to send the command from the callback; there
305 		 * is too much required context.
306 		 */
307 		if (bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual,
308 		    len, callback, fib, BUS_DMA_NOWAIT) != 0)
309 			return (ENOMEM);
310 
311 		bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
312 		    BUS_DMASYNC_PREWRITE);
313 
314 		/* fib->Header.Size is set in aac_dump_map_sg */
315 		size += fib->Header.Size;
316 
317 		if (aac_sync_fib(sc, command, 0, fib, size)) {
318 			printf("Error dumping block 0x%jx\n",
319 			       (uintmax_t)physical);
320 			return (EIO);
321 		}
322 
323 		bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
324 		    BUS_DMASYNC_POSTWRITE);
325 
326 		bus_dmamap_unload(sc->aac_buffer_dmat, dump_datamap);
327 
328 		length -= len;
329 		offset += len;
330 		virtual = (uint8_t *)virtual + len;
331 	}
332 
333 	return (0);
334 }
335 
336 /*
337  * Handle completion of an I/O request.
338  */
339 void
340 aac_biodone(struct bio *bp)
341 {
342 	struct aac_disk	*sc;
343 
344 	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
345 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
346 
347 	if (bp->bio_flags & BIO_ERROR)
348 		disk_err(bp, "hard error", -1, 1);
349 
350 	biodone(bp);
351 }
352 
353 /*
354  * Stub only.
355  */
356 static int
357 aac_disk_probe(device_t dev)
358 {
359 
360 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
361 
362 	return (0);
363 }
364 
365 /*
366  * Attach a unit to the controller.
367  */
368 static int
369 aac_disk_attach(device_t dev)
370 {
371 	struct aac_disk	*sc;
372 
373 	sc = (struct aac_disk *)device_get_softc(dev);
374 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
375 
376 	/* initialise our softc */
377 	sc->ad_controller =
378 	    (struct aac_softc *)device_get_softc(device_get_parent(dev));
379 	sc->ad_container = device_get_ivars(dev);
380 	sc->ad_dev = dev;
381 
382 	/*
383 	 * require that extended translation be enabled - other drivers read the
384 	 * disk!
385 	 */
386 	sc->ad_size = sc->ad_container->co_mntobj.Capacity;
387 	if (sc->ad_controller->flags & AAC_FLAGS_LBA_64BIT)
388 		sc->ad_size += (u_int64_t)
389 			sc->ad_container->co_mntobj.CapacityHigh << 32;
390 	if (sc->ad_size >= (2 * 1024 * 1024)) {		/* 2GB */
391 		sc->ad_heads = 255;
392 		sc->ad_sectors = 63;
393 	} else if (sc->ad_size >= (1 * 1024 * 1024)) {	/* 1GB */
394 		sc->ad_heads = 128;
395 		sc->ad_sectors = 32;
396 	} else {
397 		sc->ad_heads = 64;
398 		sc->ad_sectors = 32;
399 	}
400 	sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
401 
402 	device_printf(dev, "%juMB (%ju sectors)\n",
403 		      (intmax_t)sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
404 		      (intmax_t)sc->ad_size);
405 
406 	/* attach a generic disk device to ourselves */
407 	sc->unit = device_get_unit(dev);
408 	sc->ad_disk = disk_alloc();
409 	sc->ad_disk->d_drv1 = sc;
410 	sc->ad_disk->d_name = "aacd";
411 	sc->ad_disk->d_maxsize = aac_iosize_max;
412 	sc->ad_disk->d_open = aac_disk_open;
413 	sc->ad_disk->d_close = aac_disk_close;
414 	sc->ad_disk->d_strategy = aac_disk_strategy;
415 	sc->ad_disk->d_dump = aac_disk_dump;
416 	sc->ad_disk->d_sectorsize = AAC_BLOCK_SIZE;
417 	sc->ad_disk->d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE;
418 	sc->ad_disk->d_fwsectors = sc->ad_sectors;
419 	sc->ad_disk->d_fwheads = sc->ad_heads;
420 	sc->ad_disk->d_unit = sc->unit;
421 	disk_create(sc->ad_disk, DISK_VERSION);
422 
423 	return (0);
424 }
425 
426 /*
427  * Disconnect ourselves from the system.
428  */
429 static int
430 aac_disk_detach(device_t dev)
431 {
432 	struct aac_disk *sc;
433 
434 	sc = (struct aac_disk *)device_get_softc(dev);
435 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
436 
437 	if (sc->ad_flags & AAC_DISK_OPEN)
438 		return(EBUSY);
439 
440 	disk_destroy(sc->ad_disk);
441 
442 	return(0);
443 }
444