aac_disk.c revision 212773
165793Smsmith/*-
265793Smsmith * Copyright (c) 2000 Michael Smith
381082Sscottl * Copyright (c) 2001 Scott Long
465793Smsmith * Copyright (c) 2000 BSDi
581082Sscottl * Copyright (c) 2001 Adaptec, Inc.
665793Smsmith * All rights reserved.
765793Smsmith *
865793Smsmith * Redistribution and use in source and binary forms, with or without
965793Smsmith * modification, are permitted provided that the following conditions
1065793Smsmith * are met:
1165793Smsmith * 1. Redistributions of source code must retain the above copyright
1265793Smsmith *    notice, this list of conditions and the following disclaimer.
1365793Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1465793Smsmith *    notice, this list of conditions and the following disclaimer in the
1565793Smsmith *    documentation and/or other materials provided with the distribution.
1665793Smsmith *
1765793Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1865793Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1965793Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2065793Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2165793Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2265793Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2365793Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2465793Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2565793Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2665793Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2765793Smsmith * SUCH DAMAGE.
2865793Smsmith */
2965793Smsmith
30119418Sobrien#include <sys/cdefs.h>
31119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/aac/aac_disk.c 212773 2010-09-16 23:33:24Z emaste $");
32119418Sobrien
3381151Sscottl#include "opt_aac.h"
3481151Sscottl
3565793Smsmith#include <sys/param.h>
3665793Smsmith#include <sys/systm.h>
3765793Smsmith#include <sys/kernel.h>
38129879Sphk#include <sys/module.h>
3965793Smsmith
4065793Smsmith#include <sys/bus.h>
4165793Smsmith#include <sys/conf.h>
4265793Smsmith#include <sys/disk.h>
4365793Smsmith
4482527Sscottl#include <vm/vm.h>
4582527Sscottl#include <vm/pmap.h>
4682527Sscottl
4782527Sscottl#include <machine/md_var.h>
4865793Smsmith#include <machine/bus.h>
4965793Smsmith#include <sys/rman.h>
5065793Smsmith
5165793Smsmith#include <dev/aac/aacreg.h>
52138635Sscottl#include <sys/aac_ioctl.h>
5365793Smsmith#include <dev/aac/aacvar.h>
5465793Smsmith
5565793Smsmith/*
5665793Smsmith * Interface to parent.
5765793Smsmith */
5865793Smsmithstatic int aac_disk_probe(device_t dev);
5965793Smsmithstatic int aac_disk_attach(device_t dev);
6065793Smsmithstatic int aac_disk_detach(device_t dev);
6165793Smsmith
6265793Smsmith/*
6365793Smsmith * Interface to the device switch.
6465793Smsmith */
65111525Sscottlstatic	disk_open_t	aac_disk_open;
66111525Sscottlstatic	disk_close_t	aac_disk_close;
67111525Sscottlstatic	disk_strategy_t	aac_disk_strategy;
68111220Sphkstatic	dumper_t	aac_disk_dump;
6965793Smsmith
7089112Smsmithstatic devclass_t	aac_disk_devclass;
7165793Smsmith
7265793Smsmithstatic device_method_t aac_disk_methods[] = {
7383114Sscottl	DEVMETHOD(device_probe,	aac_disk_probe),
7483114Sscottl	DEVMETHOD(device_attach,	aac_disk_attach),
7583114Sscottl	DEVMETHOD(device_detach,	aac_disk_detach),
7683114Sscottl	{ 0, 0 }
7765793Smsmith};
7865793Smsmith
7965793Smsmithstatic driver_t aac_disk_driver = {
8083114Sscottl	"aacd",
8183114Sscottl	aac_disk_methods,
8283114Sscottl	sizeof(struct aac_disk)
8365793Smsmith};
8465793Smsmith
8565793SmsmithDRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
8665793Smsmith
8783114Sscottl/*
8865793Smsmith * Handle open from generic layer.
8965793Smsmith *
90206534Semaste * This is called by the diskslice code on first open in order to get the
9165793Smsmith * basic device geometry paramters.
9265793Smsmith */
9365793Smsmithstatic int
94111525Sscottlaac_disk_open(struct disk *dp)
9565793Smsmith{
9683114Sscottl	struct aac_disk	*sc;
9765793Smsmith
98177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
9983114Sscottl
100111525Sscottl	sc = (struct aac_disk *)dp->d_drv1;
10165793Smsmith
102109088Sscottl	if (sc == NULL) {
103109088Sscottl		printf("aac_disk_open: No Softc\n");
10483114Sscottl		return (ENXIO);
105109088Sscottl	}
10665793Smsmith
10783114Sscottl	/* check that the controller is up and running */
108109088Sscottl	if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
109212773Semaste		device_printf(sc->ad_controller->aac_dev,
110212773Semaste		    "Controller Suspended controller state = 0x%x\n",
111212773Semaste		    sc->ad_controller->aac_state);
11283114Sscottl		return(ENXIO);
113109088Sscottl	}
11465793Smsmith
11583114Sscottl	sc->ad_flags |= AAC_DISK_OPEN;
11683114Sscottl	return (0);
11765793Smsmith}
11865793Smsmith
11983114Sscottl/*
12065793Smsmith * Handle last close of the disk device.
12165793Smsmith */
12265793Smsmithstatic int
123111525Sscottlaac_disk_close(struct disk *dp)
12465793Smsmith{
12583114Sscottl	struct aac_disk	*sc;
12665793Smsmith
127177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
12883114Sscottl
129111525Sscottl	sc = (struct aac_disk *)dp->d_drv1;
13065793Smsmith
13183114Sscottl	if (sc == NULL)
13283114Sscottl		return (ENXIO);
13365793Smsmith
13483114Sscottl	sc->ad_flags &= ~AAC_DISK_OPEN;
13583114Sscottl	return (0);
13665793Smsmith}
13765793Smsmith
13883114Sscottl/*
13965793Smsmith * Handle an I/O request.
14065793Smsmith */
14165793Smsmithstatic void
14265793Smsmithaac_disk_strategy(struct bio *bp)
14365793Smsmith{
14483114Sscottl	struct aac_disk	*sc;
14565793Smsmith
146111525Sscottl	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
147177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
14865793Smsmith
14983114Sscottl	/* bogus disk? */
15083114Sscottl	if (sc == NULL) {
15183114Sscottl		bp->bio_flags |= BIO_ERROR;
15283114Sscottl		bp->bio_error = EINVAL;
15383114Sscottl		biodone(bp);
15483114Sscottl		return;
15583114Sscottl	}
15682527Sscottl
15783114Sscottl	/* do-nothing operation? */
15883114Sscottl	if (bp->bio_bcount == 0) {
15983114Sscottl		bp->bio_resid = bp->bio_bcount;
16083114Sscottl		biodone(bp);
16183114Sscottl		return;
16283114Sscottl	}
16365793Smsmith
16483114Sscottl	/* perform accounting */
16583114Sscottl
16683114Sscottl	/* pass the bio to the controller - it can work out who we are */
167133540Sscottl	mtx_lock(&sc->ad_controller->aac_io_lock);
16883114Sscottl	aac_submit_bio(bp);
169133540Sscottl	mtx_unlock(&sc->ad_controller->aac_io_lock);
170111532Sscottl
17183114Sscottl	return;
17265793Smsmith}
17365793Smsmith
17483114Sscottl/*
17595350Sscottl * Map the S/G elements for doing a dump.
17695350Sscottl */
17795350Sscottlstatic void
17895350Sscottlaac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
17995350Sscottl{
18095350Sscottl	struct aac_fib *fib;
18195350Sscottl	struct aac_blockwrite *bw;
18295350Sscottl	struct aac_sg_table *sg;
18395350Sscottl	int i;
18495350Sscottl
18595350Sscottl	fib = (struct aac_fib *)arg;
18695350Sscottl	bw = (struct aac_blockwrite *)&fib->data[0];
18795350Sscottl	sg = &bw->SgMap;
18895350Sscottl
18995350Sscottl	if (sg != NULL) {
19095350Sscottl		sg->SgCount = nsegs;
19195350Sscottl		for (i = 0; i < nsegs; i++) {
192116553Sscottl			if (segs[i].ds_addr >= BUS_SPACE_MAXADDR_32BIT)
193116553Sscottl				return;
19495350Sscottl			sg->SgEntry[i].SgAddress = segs[i].ds_addr;
19595350Sscottl			sg->SgEntry[i].SgByteCount = segs[i].ds_len;
19695350Sscottl		}
19795350Sscottl		fib->Header.Size = nsegs * sizeof(struct aac_sg_entry);
19895350Sscottl	}
19995350Sscottl}
20095350Sscottl
20195350Sscottl/*
202177899Semaste * Map the S/G elements for doing a dump on 64-bit capable devices.
203177899Semaste */
204177899Semastestatic void
205177899Semasteaac_dump_map_sg64(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
206177899Semaste{
207177899Semaste	struct aac_fib *fib;
208177899Semaste	struct aac_blockwrite64 *bw;
209177899Semaste	struct aac_sg_table64 *sg;
210177899Semaste	int i;
211177899Semaste
212177899Semaste	fib = (struct aac_fib *)arg;
213177899Semaste	bw = (struct aac_blockwrite64 *)&fib->data[0];
214177899Semaste	sg = &bw->SgMap64;
215177899Semaste
216177899Semaste	if (sg != NULL) {
217177899Semaste		sg->SgCount = nsegs;
218177899Semaste		for (i = 0; i < nsegs; i++) {
219177899Semaste			sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
220177899Semaste			sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
221177899Semaste		}
222177899Semaste		fib->Header.Size = nsegs * sizeof(struct aac_sg_entry64);
223177899Semaste	}
224177899Semaste}
225177899Semaste
226177899Semaste/*
22782527Sscottl * Dump memory out to an array
22882527Sscottl *
229195614Sjkim * Send out one command at a time with up to maxio of data.
23082527Sscottl */
23182527Sscottlstatic int
232111220Sphkaac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
23382527Sscottl{
23483114Sscottl	struct aac_disk *ad;
23583114Sscottl	struct aac_softc *sc;
23695350Sscottl	struct aac_fib *fib;
237195614Sjkim	size_t len, maxio;
23895350Sscottl	int size;
23995350Sscottl	static bus_dmamap_t dump_datamap;
24095350Sscottl	static int first = 0;
241111220Sphk	struct disk *dp;
242177899Semaste	bus_dmamap_callback_t *callback;
243177899Semaste	u_int32_t command;
24482527Sscottl
245111220Sphk	dp = arg;
246111525Sscottl	ad = dp->d_drv1;
24782527Sscottl
24883114Sscottl	if (ad == NULL)
24995350Sscottl		return (EINVAL);
25082527Sscottl
25183114Sscottl	sc= ad->ad_controller;
25282527Sscottl
25395350Sscottl	if (!first) {
25495350Sscottl		first = 1;
25595350Sscottl		if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) {
256212773Semaste			device_printf(sc->aac_dev,
257212773Semaste			    "bus_dmamap_create failed\n");
25895350Sscottl			return (ENOMEM);
25995350Sscottl		}
26095350Sscottl	}
26182527Sscottl
262130006Sscottl	/* Skip aac_alloc_sync_fib().  We don't want to mess with sleep locks */
263130006Sscottl	fib = &sc->aac_common->ac_sync_fib;
26482527Sscottl
26595350Sscottl	while (length > 0) {
266195614Sjkim		maxio = sc->aac_max_sectors << 9;
267195614Sjkim		len = (length > maxio) ? maxio : length;
268177899Semaste		if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
269177899Semaste			struct aac_blockwrite *bw;
270177899Semaste			bw = (struct aac_blockwrite *)&fib->data[0];
271177899Semaste			bw->Command = VM_CtBlockWrite;
272177899Semaste			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
273177899Semaste			bw->BlockNumber = offset / AAC_BLOCK_SIZE;
274177899Semaste			bw->ByteCount = len;
275177899Semaste			bw->Stable = CUNSTABLE;
276177899Semaste			command = ContainerCommand;
277177899Semaste			callback = aac_dump_map_sg;
278177899Semaste			size = sizeof(struct aac_blockwrite);
279177899Semaste		} else {
280177899Semaste			struct aac_blockwrite64 *bw;
281177899Semaste			bw = (struct aac_blockwrite64 *)&fib->data[0];
282177899Semaste			bw->Command = VM_CtHostWrite64;
283177899Semaste			bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
284177899Semaste			bw->BlockNumber = offset / AAC_BLOCK_SIZE;
285177899Semaste			bw->SectorCount = len / AAC_BLOCK_SIZE;
286177899Semaste			bw->Pad = 0;
287177899Semaste			bw->Flags = 0;
288177899Semaste			command = ContainerCommand64;
289177899Semaste			callback = aac_dump_map_sg64;
290177899Semaste			size = sizeof(struct aac_blockwrite64);
291177899Semaste		}
292116553Sscottl
293116553Sscottl		/*
294116553Sscottl		 * There really isn't any way to recover from errors or
295116553Sscottl		 * resource shortages here.  Oh well.  Because of that, don't
296116553Sscottl		 * bother trying to send the command from the callback; there
297116553Sscottl		 * is too much required context.
298116553Sscottl		 */
299116553Sscottl		if (bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual,
300177899Semaste		    len, callback, fib, BUS_DMA_NOWAIT) != 0)
301145811Sscottl			return (ENOMEM);
302116553Sscottl
30395350Sscottl		bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
30495350Sscottl		    BUS_DMASYNC_PREWRITE);
30582527Sscottl
30695350Sscottl		/* fib->Header.Size is set in aac_dump_map_sg */
307177899Semaste		size += fib->Header.Size;
30883114Sscottl
309177899Semaste		if (aac_sync_fib(sc, command, 0, fib, size)) {
310212773Semaste			device_printf(sc->aac_dev,
311212773Semaste			     "Error dumping block 0x%jx\n",
312212773Semaste			     (uintmax_t)physical);
31395350Sscottl			return (EIO);
31483114Sscottl		}
315116553Sscottl
316145811Sscottl		bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
317145811Sscottl		    BUS_DMASYNC_POSTWRITE);
318145811Sscottl
319145811Sscottl		bus_dmamap_unload(sc->aac_buffer_dmat, dump_datamap);
320145811Sscottl
32195350Sscottl		length -= len;
32295350Sscottl		offset += len;
323132771Skan		virtual = (uint8_t *)virtual + len;
32483114Sscottl	}
32582527Sscottl
32683114Sscottl	return (0);
32782527Sscottl}
32882527Sscottl
32983114Sscottl/*
33065793Smsmith * Handle completion of an I/O request.
33165793Smsmith */
33265793Smsmithvoid
33370393Smsmithaac_biodone(struct bio *bp)
33465793Smsmith{
33583114Sscottl	struct aac_disk	*sc;
33665793Smsmith
337111525Sscottl	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
338177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
33983114Sscottl
340111691Sscottl	if (bp->bio_flags & BIO_ERROR)
341103675Sphk		disk_err(bp, "hard error", -1, 1);
342111691Sscottl
34383114Sscottl	biodone(bp);
34465793Smsmith}
34565793Smsmith
34683114Sscottl/*
34765793Smsmith * Stub only.
34865793Smsmith */
34965793Smsmithstatic int
35065793Smsmithaac_disk_probe(device_t dev)
35165793Smsmith{
35265793Smsmith
353177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
35465793Smsmith
35583114Sscottl	return (0);
35665793Smsmith}
35765793Smsmith
35883114Sscottl/*
35965793Smsmith * Attach a unit to the controller.
36065793Smsmith */
36165793Smsmithstatic int
36265793Smsmithaac_disk_attach(device_t dev)
36365793Smsmith{
36483114Sscottl	struct aac_disk	*sc;
36583114Sscottl
36683114Sscottl	sc = (struct aac_disk *)device_get_softc(dev);
367177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
36865793Smsmith
36983114Sscottl	/* initialise our softc */
37083114Sscottl	sc->ad_controller =
37183114Sscottl	    (struct aac_softc *)device_get_softc(device_get_parent(dev));
37283114Sscottl	sc->ad_container = device_get_ivars(dev);
37383114Sscottl	sc->ad_dev = dev;
37465793Smsmith
37583114Sscottl	/*
37683114Sscottl	 * require that extended translation be enabled - other drivers read the
37783114Sscottl	 * disk!
37883114Sscottl	 */
37983114Sscottl	sc->ad_size = sc->ad_container->co_mntobj.Capacity;
380177619Semaste	if (sc->ad_controller->flags & AAC_FLAGS_LBA_64BIT)
381177619Semaste		sc->ad_size += (u_int64_t)
382177619Semaste			sc->ad_container->co_mntobj.CapacityHigh << 32;
38383114Sscottl	if (sc->ad_size >= (2 * 1024 * 1024)) {		/* 2GB */
38483114Sscottl		sc->ad_heads = 255;
38583114Sscottl		sc->ad_sectors = 63;
38683114Sscottl	} else if (sc->ad_size >= (1 * 1024 * 1024)) {	/* 1GB */
38783114Sscottl		sc->ad_heads = 128;
38883114Sscottl		sc->ad_sectors = 32;
38983114Sscottl	} else {
39083114Sscottl		sc->ad_heads = 64;
39183114Sscottl		sc->ad_sectors = 32;
39283114Sscottl	}
39383114Sscottl	sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
39465793Smsmith
395177619Semaste	device_printf(dev, "%juMB (%ju sectors)\n",
396177619Semaste		      (intmax_t)sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
397177619Semaste		      (intmax_t)sc->ad_size);
39865793Smsmith
39983114Sscottl	/* attach a generic disk device to ourselves */
40083114Sscottl	sc->unit = device_get_unit(dev);
401125975Sphk	sc->ad_disk = disk_alloc();
402125975Sphk	sc->ad_disk->d_drv1 = sc;
403125975Sphk	sc->ad_disk->d_name = "aacd";
404195614Sjkim	sc->ad_disk->d_maxsize = sc->ad_controller->aac_max_sectors << 9;
405125975Sphk	sc->ad_disk->d_open = aac_disk_open;
406125975Sphk	sc->ad_disk->d_close = aac_disk_close;
407125975Sphk	sc->ad_disk->d_strategy = aac_disk_strategy;
408125975Sphk	sc->ad_disk->d_dump = aac_disk_dump;
409125975Sphk	sc->ad_disk->d_sectorsize = AAC_BLOCK_SIZE;
410125975Sphk	sc->ad_disk->d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE;
411125975Sphk	sc->ad_disk->d_fwsectors = sc->ad_sectors;
412125975Sphk	sc->ad_disk->d_fwheads = sc->ad_heads;
413125975Sphk	sc->ad_disk->d_unit = sc->unit;
414125975Sphk	disk_create(sc->ad_disk, DISK_VERSION);
41581082Sscottl
41683114Sscottl	return (0);
41765793Smsmith}
41865793Smsmith
41983114Sscottl/*
42065793Smsmith * Disconnect ourselves from the system.
42165793Smsmith */
42265793Smsmithstatic int
42365793Smsmithaac_disk_detach(device_t dev)
42465793Smsmith{
42583114Sscottl	struct aac_disk *sc;
42665793Smsmith
42783114Sscottl	sc = (struct aac_disk *)device_get_softc(dev);
428177567Semaste	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
42965793Smsmith
43083114Sscottl	if (sc->ad_flags & AAC_DISK_OPEN)
43183114Sscottl		return(EBUSY);
43283114Sscottl
433125975Sphk	disk_destroy(sc->ad_disk);
43465793Smsmith
43583114Sscottl	return(0);
43665793Smsmith}
437