2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
47 #include <machine/md_var.h>
48 #include <machine/bus.h>
51 #include <dev/aac/aacreg.h>
52 #include <sys/aac_ioctl.h>
53 #include <dev/aac/aacvar.h>
56 * Interface to parent.
58 static int aac_disk_probe(device_t dev);
59 static int aac_disk_attach(device_t dev);
60 static int aac_disk_detach(device_t dev);
63 * Interface to the device switch.
65 static disk_open_t aac_disk_open;
66 static disk_close_t aac_disk_close;
67 static disk_strategy_t aac_disk_strategy;
68 static dumper_t aac_disk_dump;
70 static devclass_t aac_disk_devclass;
72 static 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),
79 static driver_t aac_disk_driver = {
82 sizeof(struct aac_disk)
85 DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, NULL, NULL);
88 * Handle open from generic layer.
90 * This is called by the diskslice code on first open in order to get the
91 * basic device geometry paramters.
94 aac_disk_open(struct disk *dp)
98 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
100 sc = (struct aac_disk *)dp->d_drv1;
103 printf("aac_disk_open: No Softc\n");
107 /* check that the controller is up and running */
108 if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) {
109 device_printf(sc->ad_controller->aac_dev,
110 "Controller Suspended controller state = 0x%x\n",
111 sc->ad_controller->aac_state);
115 sc->ad_flags |= AAC_DISK_OPEN;
120 * Handle last close of the disk device.
123 aac_disk_close(struct disk *dp)
127 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
129 sc = (struct aac_disk *)dp->d_drv1;
134 sc->ad_flags &= ~AAC_DISK_OPEN;
139 * Handle an I/O request.
142 aac_disk_strategy(struct bio *bp)
146 sc = (struct aac_disk *)bp->bio_disk->d_drv1;
147 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
151 bp->bio_flags |= BIO_ERROR;
152 bp->bio_error = EINVAL;
157 /* do-nothing operation? */
158 if (bp->bio_bcount == 0) {
159 bp->bio_resid = bp->bio_bcount;
164 /* perform accounting */
166 /* pass the bio to the controller - it can work out who we are */
167 mtx_lock(&sc->ad_controller->aac_io_lock);
169 mtx_unlock(&sc->ad_controller->aac_io_lock);
175 * Map the S/G elements for doing a dump.
178 aac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
181 struct aac_blockwrite *bw;
182 struct aac_sg_table *sg;
185 fib = (struct aac_fib *)arg;
186 bw = (struct aac_blockwrite *)&fib->data[0];
191 for (i = 0; i < nsegs; i++) {
192 if (segs[i].ds_addr >= BUS_SPACE_MAXADDR_32BIT)
194 sg->SgEntry[i].SgAddress = segs[i].ds_addr;
195 sg->SgEntry[i].SgByteCount = segs[i].ds_len;
197 fib->Header.Size = nsegs * sizeof(struct aac_sg_entry);
202 * Map the S/G elements for doing a dump on 64-bit capable devices.
205 aac_dump_map_sg64(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
208 struct aac_blockwrite64 *bw;
209 struct aac_sg_table64 *sg;
212 fib = (struct aac_fib *)arg;
213 bw = (struct aac_blockwrite64 *)&fib->data[0];
218 for (i = 0; i < nsegs; i++) {
219 sg->SgEntry64[i].SgAddress = segs[i].ds_addr;
220 sg->SgEntry64[i].SgByteCount = segs[i].ds_len;
222 fib->Header.Size = nsegs * sizeof(struct aac_sg_entry64);
227 * Dump memory out to an array
229 * Send out one command at a time with up to maxio of data.
232 aac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
235 struct aac_softc *sc;
239 static bus_dmamap_t dump_datamap;
240 static int first = 0;
242 bus_dmamap_callback_t *callback;
251 sc= ad->ad_controller;
255 if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) {
256 device_printf(sc->aac_dev,
257 "bus_dmamap_create failed\n");
262 /* Skip aac_alloc_sync_fib(). We don't want to mess with sleep locks */
263 fib = &sc->aac_common->ac_sync_fib;
266 maxio = sc->aac_max_sectors << 9;
267 len = (length > maxio) ? maxio : length;
268 if ((sc->flags & AAC_FLAGS_SG_64BIT) == 0) {
269 struct aac_blockwrite *bw;
270 bw = (struct aac_blockwrite *)&fib->data[0];
271 bw->Command = VM_CtBlockWrite;
272 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
273 bw->BlockNumber = offset / AAC_BLOCK_SIZE;
275 bw->Stable = CUNSTABLE;
276 command = ContainerCommand;
277 callback = aac_dump_map_sg;
278 size = sizeof(struct aac_blockwrite);
280 struct aac_blockwrite64 *bw;
281 bw = (struct aac_blockwrite64 *)&fib->data[0];
282 bw->Command = VM_CtHostWrite64;
283 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId;
284 bw->BlockNumber = offset / AAC_BLOCK_SIZE;
285 bw->SectorCount = len / AAC_BLOCK_SIZE;
288 command = ContainerCommand64;
289 callback = aac_dump_map_sg64;
290 size = sizeof(struct aac_blockwrite64);
294 * There really isn't any way to recover from errors or
295 * resource shortages here. Oh well. Because of that, don't
296 * bother trying to send the command from the callback; there
297 * is too much required context.
299 if (bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual,
300 len, callback, fib, BUS_DMA_NOWAIT) != 0)
303 bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
304 BUS_DMASYNC_PREWRITE);
306 /* fib->Header.Size is set in aac_dump_map_sg */
307 size += fib->Header.Size;
309 if (aac_sync_fib(sc, command, 0, fib, size)) {
310 device_printf(sc->aac_dev,
311 "Error dumping block 0x%jx\n",
312 (uintmax_t)physical);
316 bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap,
317 BUS_DMASYNC_POSTWRITE);
319 bus_dmamap_unload(sc->aac_buffer_dmat, dump_datamap);
323 virtual = (uint8_t *)virtual + len;
330 * Handle completion of an I/O request.
333 aac_biodone(struct bio *bp)
335 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
337 if (bp->bio_flags & BIO_ERROR) {
338 bp->bio_resid = bp->bio_bcount;
339 disk_err(bp, "hard error", -1, 1);
349 aac_disk_probe(device_t dev)
352 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
358 * Attach a unit to the controller.
361 aac_disk_attach(device_t dev)
365 sc = (struct aac_disk *)device_get_softc(dev);
366 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
368 /* initialise our softc */
370 (struct aac_softc *)device_get_softc(device_get_parent(dev));
371 sc->ad_container = device_get_ivars(dev);
375 * require that extended translation be enabled - other drivers read the
378 sc->ad_size = sc->ad_container->co_mntobj.Capacity;
379 if (sc->ad_controller->flags & AAC_FLAGS_LBA_64BIT)
380 sc->ad_size += (u_int64_t)
381 sc->ad_container->co_mntobj.CapacityHigh << 32;
382 if (sc->ad_size >= (2 * 1024 * 1024)) { /* 2GB */
385 } else if (sc->ad_size >= (1 * 1024 * 1024)) { /* 1GB */
392 sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors));
394 device_printf(dev, "%juMB (%ju sectors)\n",
395 (intmax_t)sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE),
396 (intmax_t)sc->ad_size);
398 /* attach a generic disk device to ourselves */
399 sc->unit = device_get_unit(dev);
400 sc->ad_disk = disk_alloc();
401 sc->ad_disk->d_drv1 = sc;
402 sc->ad_disk->d_name = "aacd";
403 sc->ad_disk->d_maxsize = sc->ad_controller->aac_max_sectors << 9;
404 sc->ad_disk->d_open = aac_disk_open;
405 sc->ad_disk->d_close = aac_disk_close;
406 sc->ad_disk->d_strategy = aac_disk_strategy;
407 sc->ad_disk->d_dump = aac_disk_dump;
408 sc->ad_disk->d_sectorsize = AAC_BLOCK_SIZE;
409 sc->ad_disk->d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE;
410 sc->ad_disk->d_fwsectors = sc->ad_sectors;
411 sc->ad_disk->d_fwheads = sc->ad_heads;
412 sc->ad_disk->d_unit = sc->unit;
413 disk_create(sc->ad_disk, DISK_VERSION);
419 * Disconnect ourselves from the system.
422 aac_disk_detach(device_t dev)
426 sc = (struct aac_disk *)device_get_softc(dev);
427 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
429 if (sc->ad_flags & AAC_DISK_OPEN)
432 disk_destroy(sc->ad_disk);