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