aac_disk.c revision 129879
123353Sdfr/*-
223353Sdfr * Copyright (c) 2000 Michael Smith
323353Sdfr * Copyright (c) 2001 Scott Long
423353Sdfr * Copyright (c) 2000 BSDi
523353Sdfr * Copyright (c) 2001 Adaptec, Inc.
623353Sdfr * All rights reserved.
723353Sdfr *
823353Sdfr * Redistribution and use in source and binary forms, with or without
923353Sdfr * modification, are permitted provided that the following conditions
1023353Sdfr * are met:
1123353Sdfr * 1. Redistributions of source code must retain the above copyright
1223353Sdfr *    notice, this list of conditions and the following disclaimer.
1323353Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1423353Sdfr *    notice, this list of conditions and the following disclaimer in the
1523353Sdfr *    documentation and/or other materials provided with the distribution.
1623353Sdfr *
1723353Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1823353Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1923353Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2023353Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2123353Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2223353Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2323353Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2423353Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2523353Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2623353Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2723353Sdfr * SUCH DAMAGE.
2823353Sdfr */
2950476Speter
3023353Sdfr#include <sys/cdefs.h>
3123353Sdfr__FBSDID("$FreeBSD: head/sys/dev/aac/aac_disk.c 129879 2004-05-30 20:08:47Z phk $");
32206622Suqs
3323353Sdfr#include "opt_aac.h"
3423353Sdfr
3523353Sdfr#include <sys/param.h>
3623353Sdfr#include <sys/systm.h>
3723353Sdfr#include <sys/kernel.h>
3884306Sru#include <sys/module.h>
3984306Sru#include <sys/sysctl.h>
4023353Sdfr
4123353Sdfr#include <sys/bus.h>
4223353Sdfr#include <sys/conf.h>
4323353Sdfr#include <sys/disk.h>
4423353Sdfr
45115440Shmp#include <vm/vm.h>
46140931Sru#include <vm/pmap.h>
47115440Shmp
48140931Sru#include <machine/md_var.h>
4923353Sdfr#include <machine/bus.h>
5023353Sdfr#include <sys/rman.h>
5123353Sdfr
5223353Sdfr#include <dev/aac/aacreg.h>
5323353Sdfr#include <dev/aac/aac_ioctl.h>
5423353Sdfr#include <dev/aac/aacvar.h>
5529966Swosch
5629966Swosch/*
5723353Sdfr * Interface to parent.
58147647Shmp */
5934504Scharnierstatic int aac_disk_probe(device_t dev);
60static int aac_disk_attach(device_t dev);
61static int aac_disk_detach(device_t dev);
62
63/*
64 * Interface to the device switch.
65 */
66static	disk_open_t	aac_disk_open;
67static	disk_close_t	aac_disk_close;
68static	disk_strategy_t	aac_disk_strategy;
69static	dumper_t	aac_disk_dump;
70
71static devclass_t	aac_disk_devclass;
72
73static 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
80static driver_t aac_disk_driver = {
81	"aacd",
82	aac_disk_methods,
83	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