aac_disk.c revision 129879
13090Sache/*-
23090Sache * Copyright (c) 2000 Michael Smith
33090Sache * Copyright (c) 2001 Scott Long
43090Sache * Copyright (c) 2000 BSDi
53090Sache * Copyright (c) 2001 Adaptec, Inc.
63090Sache * All rights reserved.
73090Sache *
83090Sache * Redistribution and use in source and binary forms, with or without
93090Sache * modification, are permitted provided that the following conditions
103090Sache * are met:
113090Sache * 1. Redistributions of source code must retain the above copyright
123090Sache *    notice, this list of conditions and the following disclaimer.
133090Sache * 2. Redistributions in binary form must reproduce the above copyright
143090Sache *    notice, this list of conditions and the following disclaimer in the
153090Sache *    documentation and/or other materials provided with the distribution.
163090Sache *
173090Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183090Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193090Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203090Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213090Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223090Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233090Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243090Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253090Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263090Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273090Sache * SUCH DAMAGE.
283090Sache */
293090Sache
303090Sache#include <sys/cdefs.h>
313090Sache__FBSDID("$FreeBSD: head/sys/dev/aac/aac_disk.c 129879 2004-05-30 20:08:47Z phk $");
323090Sache
333090Sache#include "opt_aac.h"
343090Sache
353090Sache#include <sys/param.h>
363090Sache#include <sys/systm.h>
373090Sache#include <sys/kernel.h>
383090Sache#include <sys/module.h>
393090Sache#include <sys/sysctl.h>
403090Sache
413090Sache#include <sys/bus.h>
423090Sache#include <sys/conf.h>
433090Sache#include <sys/disk.h>
443090Sache
453090Sache#include <vm/vm.h>
463090Sache#include <vm/pmap.h>
473090Sache
483090Sache#include <machine/md_var.h>
493090Sache#include <machine/bus.h>
503090Sache#include <sys/rman.h>
513090Sache
523090Sache#include <dev/aac/aacreg.h>
533090Sache#include <dev/aac/aac_ioctl.h>
543090Sache#include <dev/aac/aacvar.h>
553090Sache
563090Sache/*
573090Sache * Interface to parent.
583090Sache */
593090Sachestatic int aac_disk_probe(device_t dev);
603090Sachestatic int aac_disk_attach(device_t dev);
613090Sachestatic int aac_disk_detach(device_t dev);
623090Sache
633090Sache/*
643090Sache * Interface to the device switch.
653090Sache */
663090Sachestatic	disk_open_t	aac_disk_open;
673090Sachestatic	disk_close_t	aac_disk_close;
683090Sachestatic	disk_strategy_t	aac_disk_strategy;
693090Sachestatic	dumper_t	aac_disk_dump;
703090Sache
713090Sachestatic devclass_t	aac_disk_devclass;
723090Sache
733090Sachestatic device_method_t aac_disk_methods[] = {
743090Sache	DEVMETHOD(device_probe,	aac_disk_probe),
753090Sache	DEVMETHOD(device_attach,	aac_disk_attach),
763090Sache	DEVMETHOD(device_detach,	aac_disk_detach),
773090Sache	{ 0, 0 }
783090Sache};
793090Sache
803090Sachestatic driver_t aac_disk_driver = {
813090Sache	"aacd",
823090Sache	aac_disk_methods,
833090Sache	sizeof(struct aac_disk)
84};
85
86#define AAC_MAXIO	65536
87
88DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0);
89
90/* sysctl tunables */
91static unsigned int aac_iosize_max = AAC_MAXIO;	/* due to limits of the card */
92TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max);
93
94SYSCTL_DECL(_hw_aac);
95SYSCTL_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 */
104static int
105aac_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 */
132static int
133aac_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 */
151static void
152aac_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 */
191static void
192aac_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 */
220static int
221aac_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	aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE);
250	bw = (struct aac_blockwrite *)&fib->data[0];
251
252	while (length > 0) {
253		len = (length > AAC_MAXIO) ? AAC_MAXIO : length;
254		bw->Command = VM_CtBlockWrite;
255		bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
256		bw->BlockNumber = offset / AAC_BLOCK_SIZE;
257		bw->ByteCount = len;
258		bw->Stable = CUNSTABLE;
259
260		/*
261		 * There really isn't any way to recover from errors or
262		 * resource shortages here.  Oh well.  Because of that, don't
263		 * bother trying to send the command from the callback; there
264		 * is too much required context.
265		 */
266		if (bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual,
267		    len, aac_dump_map_sg, fib, 0) != 0)
268			return (EIO);
269
270		bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
271		    BUS_DMASYNC_PREWRITE);
272
273		/* fib->Header.Size is set in aac_dump_map_sg */
274		size = fib->Header.Size + sizeof(struct aac_blockwrite);
275
276		if (aac_sync_fib(sc, ContainerCommand, 0, fib, size)) {
277			printf("Error dumping block 0x%jx\n",
278			       (uintmax_t)physical);
279			return (EIO);
280		}
281
282		length -= len;
283		offset += len;
284		(vm_offset_t)virtual += len;
285	}
286
287	return (0);
288}
289
290/*
291 * Handle completion of an I/O request.
292 */
293void
294aac_biodone(struct bio *bp)
295{
296	struct aac_disk	*sc;
297
298	debug_called(4);
299
300	sc = (struct aac_disk *)bp->bio_disk->d_drv1;
301
302	if (bp->bio_flags & BIO_ERROR)
303		disk_err(bp, "hard error", -1, 1);
304
305	biodone(bp);
306}
307
308/*
309 * Stub only.
310 */
311static int
312aac_disk_probe(device_t dev)
313{
314
315	debug_called(2);
316
317	return (0);
318}
319
320/*
321 * Attach a unit to the controller.
322 */
323static int
324aac_disk_attach(device_t dev)
325{
326	struct aac_disk	*sc;
327
328	debug_called(1);
329
330	sc = (struct aac_disk *)device_get_softc(dev);
331
332	/* initialise our softc */
333	sc->ad_controller =
334	    (struct aac_softc *)device_get_softc(device_get_parent(dev));
335	sc->ad_container = device_get_ivars(dev);
336	sc->ad_dev = dev;
337
338	/*
339	 * require that extended translation be enabled - other drivers read the
340	 * disk!
341	 */
342	sc->ad_size = sc->ad_container->co_mntobj.Capacity;
343	if (sc->ad_size >= (2 * 1024 * 1024)) {		/* 2GB */
344		sc->ad_heads = 255;
345		sc->ad_sectors = 63;
346	} else if (sc->ad_size >= (1 * 1024 * 1024)) {	/* 1GB */
347		sc->ad_heads = 128;
348		sc->ad_sectors = 32;
349	} else {
350		sc->ad_heads = 64;
351		sc->ad_sectors = 32;
352	}
353	sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
354
355	device_printf(dev, "%uMB (%u sectors)\n",
356		      sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
357		      sc->ad_size);
358
359	/* attach a generic disk device to ourselves */
360	sc->unit = device_get_unit(dev);
361	sc->ad_disk = disk_alloc();
362	sc->ad_disk->d_drv1 = sc;
363	sc->ad_disk->d_name = "aacd";
364	sc->ad_disk->d_maxsize = aac_iosize_max;
365	sc->ad_disk->d_open = aac_disk_open;
366	sc->ad_disk->d_close = aac_disk_close;
367	sc->ad_disk->d_strategy = aac_disk_strategy;
368	sc->ad_disk->d_dump = aac_disk_dump;
369	sc->ad_disk->d_sectorsize = AAC_BLOCK_SIZE;
370	sc->ad_disk->d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE;
371	sc->ad_disk->d_fwsectors = sc->ad_sectors;
372	sc->ad_disk->d_fwheads = sc->ad_heads;
373	sc->ad_disk->d_unit = sc->unit;
374	disk_create(sc->ad_disk, DISK_VERSION);
375
376	return (0);
377}
378
379/*
380 * Disconnect ourselves from the system.
381 */
382static int
383aac_disk_detach(device_t dev)
384{
385	struct aac_disk *sc;
386
387	debug_called(2);
388
389	sc = (struct aac_disk *)device_get_softc(dev);
390
391	if (sc->ad_flags & AAC_DISK_OPEN)
392		return(EBUSY);
393
394	disk_destroy(sc->ad_disk);
395
396	return(0);
397}
398