aac_disk.c revision 111525
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 * $FreeBSD: head/sys/dev/aac/aac_disk.c 111525 2003-02-26 02:50:27Z scottl $ 30 */ 31 32#include "opt_aac.h" 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/sysctl.h> 38 39#include <dev/aac/aac_compat.h> 40#include <sys/bus.h> 41#include <sys/conf.h> 42#include <sys/devicestat.h> 43#include <sys/disk.h> 44 45#include <vm/vm.h> 46#include <vm/pmap.h> 47 48#include <machine/md_var.h> 49#include <machine/bus.h> 50#include <sys/rman.h> 51 52#include <dev/aac/aacreg.h> 53#include <dev/aac/aac_ioctl.h> 54#include <dev/aac/aacvar.h> 55 56/* 57 * Interface to parent. 58 */ 59static 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#ifdef FREEBSD_4 73static int disks_registered = 0; 74#endif 75 76static device_method_t aac_disk_methods[] = { 77 DEVMETHOD(device_probe, aac_disk_probe), 78 DEVMETHOD(device_attach, aac_disk_attach), 79 DEVMETHOD(device_detach, aac_disk_detach), 80 { 0, 0 } 81}; 82 83static driver_t aac_disk_driver = { 84 "aacd", 85 aac_disk_methods, 86 sizeof(struct aac_disk) 87}; 88 89#define AAC_MAXIO 65536 90 91DRIVER_MODULE(aacd, aac, aac_disk_driver, aac_disk_devclass, 0, 0); 92 93/* sysctl tunables */ 94static unsigned int aac_iosize_max = AAC_MAXIO; /* due to limits of the card */ 95TUNABLE_INT("hw.aac.iosize_max", &aac_iosize_max); 96 97SYSCTL_DECL(_hw_aac); 98SYSCTL_UINT(_hw_aac, OID_AUTO, iosize_max, CTLFLAG_RD, &aac_iosize_max, 0, 99 "Max I/O size per transfer to an array"); 100 101/* 102 * Handle open from generic layer. 103 * 104 * This is called by the diskslice code on first open in order to get the 105 * basic device geometry paramters. 106 */ 107static int 108aac_disk_open(struct disk *dp) 109{ 110 struct aac_disk *sc; 111 112 debug_called(4); 113 114 sc = (struct aac_disk *)dp->d_drv1; 115 116 if (sc == NULL) { 117 printf("aac_disk_open: No Softc\n"); 118 return (ENXIO); 119 } 120 121 /* check that the controller is up and running */ 122 if (sc->ad_controller->aac_state & AAC_STATE_SUSPEND) { 123 printf("Controller Suspended controller state = 0x%x\n", 124 sc->ad_controller->aac_state); 125 return(ENXIO); 126 } 127 128 sc->ad_flags |= AAC_DISK_OPEN; 129 return (0); 130} 131 132/* 133 * Handle last close of the disk device. 134 */ 135static int 136aac_disk_close(struct disk *dp) 137{ 138 struct aac_disk *sc; 139 140 debug_called(4); 141 142 sc = (struct aac_disk *)dp->d_drv1; 143 144 if (sc == NULL) 145 return (ENXIO); 146 147 sc->ad_flags &= ~AAC_DISK_OPEN; 148 return (0); 149} 150 151/* 152 * Handle an I/O request. 153 */ 154static void 155aac_disk_strategy(struct bio *bp) 156{ 157 struct aac_disk *sc; 158 159 debug_called(4); 160 161 sc = (struct aac_disk *)bp->bio_disk->d_drv1; 162 163 /* bogus disk? */ 164 if (sc == NULL) { 165 bp->bio_flags |= BIO_ERROR; 166 bp->bio_error = EINVAL; 167 biodone(bp); 168 return; 169 } 170 171 /* do-nothing operation? */ 172 if (bp->bio_bcount == 0) { 173 bp->bio_resid = bp->bio_bcount; 174 biodone(bp); 175 return; 176 } 177 178 /* perform accounting */ 179 devstat_start_transaction(&sc->ad_stats); 180 181 /* pass the bio to the controller - it can work out who we are */ 182 aac_submit_bio(bp); 183 return; 184} 185 186/* 187 * Map the S/G elements for doing a dump. 188 */ 189static void 190aac_dump_map_sg(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 191{ 192 struct aac_fib *fib; 193 struct aac_blockwrite *bw; 194 struct aac_sg_table *sg; 195 int i; 196 197 fib = (struct aac_fib *)arg; 198 bw = (struct aac_blockwrite *)&fib->data[0]; 199 sg = &bw->SgMap; 200 201 if (sg != NULL) { 202 sg->SgCount = nsegs; 203 for (i = 0; i < nsegs; i++) { 204 sg->SgEntry[i].SgAddress = segs[i].ds_addr; 205 sg->SgEntry[i].SgByteCount = segs[i].ds_len; 206 } 207 fib->Header.Size = nsegs * sizeof(struct aac_sg_entry); 208 } 209} 210 211/* 212 * Dump memory out to an array 213 * 214 * Send out one command at a time with up to AAC_MAXIO of data. 215 */ 216static int 217aac_disk_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length) 218{ 219 struct aac_disk *ad; 220 struct aac_softc *sc; 221 struct aac_fib *fib; 222 struct aac_blockwrite *bw; 223 size_t len; 224 int size; 225 static bus_dmamap_t dump_datamap; 226 static int first = 0; 227 struct disk *dp; 228 229 dp = arg; 230 ad = dp->d_drv1; 231 232 if (ad == NULL) 233 return (EINVAL); 234 235 sc= ad->ad_controller; 236 237 if (!first) { 238 first = 1; 239 if (bus_dmamap_create(sc->aac_buffer_dmat, 0, &dump_datamap)) { 240 printf("bus_dmamap_create failed\n"); 241 return (ENOMEM); 242 } 243 } 244 245 aac_alloc_sync_fib(sc, &fib, AAC_SYNC_LOCK_FORCE); 246 bw = (struct aac_blockwrite *)&fib->data[0]; 247 248 while (length > 0) { 249 len = (length > AAC_MAXIO) ? AAC_MAXIO : length; 250 bw->Command = VM_CtBlockWrite; 251 bw->ContainerId = ad->ad_container->co_mntobj.ObjectId; 252 bw->BlockNumber = offset / AAC_BLOCK_SIZE; 253 bw->ByteCount = len; 254 bw->Stable = CUNSTABLE; 255 bus_dmamap_load(sc->aac_buffer_dmat, dump_datamap, virtual, 256 len, aac_dump_map_sg, fib, 0); 257 bus_dmamap_sync(sc->aac_buffer_dmat, dump_datamap, 258 BUS_DMASYNC_PREWRITE); 259 260 /* fib->Header.Size is set in aac_dump_map_sg */ 261 size = fib->Header.Size + sizeof(struct aac_blockwrite); 262 263 if (aac_sync_fib(sc, ContainerCommand, 0, fib, size)) { 264 printf("Error dumping block 0x%x\n", physical); 265 return (EIO); 266 } 267 length -= len; 268 offset += len; 269 } 270 271 return (0); 272} 273 274/* 275 * Handle completion of an I/O request. 276 */ 277void 278aac_biodone(struct bio *bp) 279{ 280 struct aac_disk *sc; 281 282 debug_called(4); 283 284 sc = (struct aac_disk *)bp->bio_disk->d_drv1; 285 286 devstat_end_transaction_bio(&sc->ad_stats, bp); 287 if (bp->bio_flags & BIO_ERROR) { 288#if __FreeBSD_version > 500039 289 disk_err(bp, "hard error", -1, 1); 290#elif __FreeBSD_version > 500005 291 int blkno; 292 blkno = (sc->ad_label.d_nsectors) ? 0 : -1; 293 diskerr(bp, (char *)bp->bio_driver1, blkno, &sc->ad_label); 294#else 295 int blkno; 296 blkno = (sc->ad_label.d_nsectors) ? 0 : -1; 297 diskerr(bp, (char *)bp->bio_driver1, 0, blkno, &sc->ad_label); 298#endif 299 } 300 biodone(bp); 301} 302 303/* 304 * Stub only. 305 */ 306static int 307aac_disk_probe(device_t dev) 308{ 309 310 debug_called(2); 311 312 return (0); 313} 314 315/* 316 * Attach a unit to the controller. 317 */ 318static int 319aac_disk_attach(device_t dev) 320{ 321 struct aac_disk *sc; 322 323 debug_called(1); 324 325 sc = (struct aac_disk *)device_get_softc(dev); 326 327 /* initialise our softc */ 328 sc->ad_controller = 329 (struct aac_softc *)device_get_softc(device_get_parent(dev)); 330 sc->ad_container = device_get_ivars(dev); 331 sc->ad_dev = dev; 332 333 /* 334 * require that extended translation be enabled - other drivers read the 335 * disk! 336 */ 337 sc->ad_size = sc->ad_container->co_mntobj.Capacity; 338 if (sc->ad_size >= (2 * 1024 * 1024)) { /* 2GB */ 339 sc->ad_heads = 255; 340 sc->ad_sectors = 63; 341 } else if (sc->ad_size >= (1 * 1024 * 1024)) { /* 1GB */ 342 sc->ad_heads = 128; 343 sc->ad_sectors = 32; 344 } else { 345 sc->ad_heads = 64; 346 sc->ad_sectors = 32; 347 } 348 sc->ad_cylinders = (sc->ad_size / (sc->ad_heads * sc->ad_sectors)); 349 350 device_printf(dev, "%uMB (%u sectors)\n", 351 sc->ad_size / ((1024 * 1024) / AAC_BLOCK_SIZE), 352 sc->ad_size); 353 354 devstat_add_entry(&sc->ad_stats, "aacd", device_get_unit(dev), 355 AAC_BLOCK_SIZE, DEVSTAT_NO_ORDERED_TAGS, 356 DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 357 DEVSTAT_PRIORITY_ARRAY); 358 359 /* attach a generic disk device to ourselves */ 360 sc->unit = device_get_unit(dev); 361 sc->ad_disk.d_drv1 = sc; 362 sc->ad_disk.d_name = "aacd"; 363 sc->ad_disk.d_maxsize = aac_iosize_max; 364 sc->ad_disk.d_open = aac_disk_open; 365 sc->ad_disk.d_close = aac_disk_close; 366 sc->ad_disk.d_strategy = aac_disk_strategy; 367 sc->ad_disk.d_dump = aac_disk_dump; 368 sc->ad_disk.d_sectorsize = AAC_BLOCK_SIZE; 369 sc->ad_disk.d_mediasize = (off_t)sc->ad_size * AAC_BLOCK_SIZE; 370 sc->ad_disk.d_fwsectors = sc->ad_sectors; 371 sc->ad_disk.d_fwheads = sc->ad_heads; 372 disk_create(sc->unit, &sc->ad_disk, 0, NULL, NULL); 373 374 return (0); 375} 376 377/* 378 * Disconnect ourselves from the system. 379 */ 380static int 381aac_disk_detach(device_t dev) 382{ 383 struct aac_disk *sc; 384 385 debug_called(2); 386 387 sc = (struct aac_disk *)device_get_softc(dev); 388 389 if (sc->ad_flags & AAC_DISK_OPEN) 390 return(EBUSY); 391 392 devstat_remove_entry(&sc->ad_stats); 393 disk_destroy(&sc->ad_disk); 394#ifdef FREEBSD_4 395 if (--disks_registered == 0) 396 cdevsw_remove(&aac_disk_cdevsw); 397#endif 398 399 return(0); 400} 401