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$ 30 */ 31 32 /* 33 * Driver Parameter Definitions 34 */ 35 36 /* 37 * The firmware interface allows for a 16-bit s/g list length. We limit 38 * ourselves to a reasonable maximum and ensure alignment. 39 */ 40 #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */ 41 42 /* 43 * We allocate a small set of FIBs for the adapter to use to send us messages. 44 */ 45 #define AAC_ADAPTER_FIBS 8 46 47 /* 48 * FIBs are allocated up-front, and the pool isn't grown. We should allocate 49 * enough here to let us keep the adapter busy without wasting large amounts 50 * of kernel memory. The current interface implementation limits us to 512 51 * FIBs queued for the adapter at any one time. 52 */ 53 #define AAC_FIB_COUNT 128 54 55 /* 56 * The controller reports status events in AIFs. We hang on to a number of 57 * these in order to pass them out to user-space management tools. 58 */ 59 #define AAC_AIFQ_LENGTH 64 60 61 /* 62 * Firmware messages are passed in the printf buffer. 63 */ 64 #define AAC_PRINTF_BUFSIZE 256 65 66 /* 67 * We wait this many seconds for the adapter to come ready if it is still 68 * booting 69 */ 70 #define AAC_BOOT_TIMEOUT (3 * 60) 71 72 /* 73 * Timeout for immediate commands. 74 */ 75 #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */ 76 77 /* 78 * Timeout for normal commands 79 */ 80 #define AAC_CMD_TIMEOUT 30 /* seconds */ 81 82 /* 83 * Rate at which we periodically check for timed out commands and kick the 84 * controller. 85 */ 86 #define AAC_PERIODIC_INTERVAL 20 /* seconds */ 87 88 /* 89 * Character device major numbers. 90 */ 91 #define AAC_DISK_MAJOR 200 92 93 /* 94 * Driver Variable Definitions 95 */ 96 97 #if __FreeBSD_version >= 500005 98 # include <sys/taskqueue.h> 99 #endif 100 101 /* 102 * Per-container data structure 103 */ 104 struct aac_container 105 { 106 struct aac_mntobj co_mntobj; 107 device_t co_disk; 108 int co_found; 109 TAILQ_ENTRY(aac_container) co_link; 110 }; 111 112 /* 113 * Per-SIM data structure 114 */ 115 struct aac_sim 116 { 117 device_t sim_dev; 118 int TargetsPerBus; 119 int BusNumber; 120 int InitiatorBusId; 121 struct aac_softc *aac_sc; 122 TAILQ_ENTRY(aac_sim) sim_link; 123 }; 124 125 /* 126 * Per-disk structure 127 */ 128 struct aac_disk 129 { 130 device_t ad_dev; 131 dev_t ad_dev_t; 132 struct aac_softc *ad_controller; 133 struct aac_container *ad_container; 134 struct disk ad_disk; 135 struct devstat ad_stats; 136 int ad_flags; 137 #define AAC_DISK_OPEN (1<<0) 138 int ad_cylinders; 139 int ad_heads; 140 int ad_sectors; 141 u_int32_t ad_size; 142 int unit; 143 }; 144 145 /* 146 * Per-command control structure. 147 */ 148 struct aac_command 149 { 150 TAILQ_ENTRY(aac_command) cm_link; /* list linkage */ 151 152 struct aac_softc *cm_sc; /* controller that owns us */ 153 154 struct aac_fib *cm_fib; /* FIB associated with this 155 * command */ 156 u_int32_t cm_fibphys; /* bus address of the FIB */ 157 struct bio *cm_data; /* pointer to data in kernel 158 * space */ 159 u_int32_t cm_datalen; /* data length */ 160 bus_dmamap_t cm_datamap; /* DMA map for bio data */ 161 struct aac_sg_table *cm_sgtable; /* pointer to s/g table in 162 * command */ 163 int cm_flags; 164 #define AAC_CMD_MAPPED (1<<0) /* command has had its data 165 * mapped */ 166 #define AAC_CMD_DATAIN (1<<1) /* command involves data moving 167 * from controller to host */ 168 #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving 169 * from host to controller */ 170 #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */ 171 #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */ 172 #define AAC_ON_AACQ_FREE (1<<5) 173 #define AAC_ON_AACQ_READY (1<<6) 174 #define AAC_ON_AACQ_BUSY (1<<7) 175 #define AAC_ON_AACQ_COMPLETE (1<<8) 176 #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)) 177 178 void (* cm_complete)(struct aac_command *cm); 179 void *cm_private; 180 time_t cm_timestamp; /* command creation time */ 181 int cm_queue; 182 }; 183 184 /* 185 * We gather a number of adapter-visible items into a single structure. 186 * 187 * The ordering of this strucure may be important; we copy the Linux driver: 188 * 189 * Adapter FIBs 190 * Init struct 191 * Queue headers (Comm Area) 192 * Printf buffer 193 * 194 * In addition, we add: 195 * Sync Fib 196 */ 197 struct aac_common { 198 /* fibs for the controller to send us messages */ 199 struct aac_fib ac_fibs[AAC_ADAPTER_FIBS]; 200 201 /* the init structure */ 202 struct aac_adapter_init ac_init; 203 204 /* arena within which the queue structures are kept */ 205 u_int8_t ac_qbuf[sizeof(struct aac_queue_table) + 206 AAC_QUEUE_ALIGN]; 207 208 /* buffer for text messages from the controller */ 209 char ac_printf[AAC_PRINTF_BUFSIZE]; 210 211 /* fib for synchronous commands */ 212 struct aac_fib ac_sync_fib; 213 }; 214 215 /* 216 * Interface operations 217 */ 218 struct aac_interface 219 { 220 int (*aif_get_fwstatus)(struct aac_softc *sc); 221 void (*aif_qnotify)(struct aac_softc *sc, int qbit); 222 int (*aif_get_istatus)(struct aac_softc *sc); 223 void (*aif_clr_istatus)(struct aac_softc *sc, int mask); 224 void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command, 225 u_int32_t arg0, u_int32_t arg1, 226 u_int32_t arg2, u_int32_t arg3); 227 int (*aif_get_mailboxstatus)(struct aac_softc *sc); 228 void (*aif_set_interrupts)(struct aac_softc *sc, int enable); 229 }; 230 extern struct aac_interface aac_rx_interface; 231 extern struct aac_interface aac_sa_interface; 232 extern struct aac_interface aac_fa_interface; 233 234 #define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc))) 235 #define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit))) 236 #define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc))) 237 #define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \ 238 (mask))) 239 #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \ 240 ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \ 241 (arg3))) 242 #define AAC_GET_MAILBOXSTATUS(sc) ((sc)->aac_if.aif_get_mailboxstatus( \ 243 (sc))) 244 #define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 245 0)) 246 #define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \ 247 1)) 248 249 #define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \ 250 sc->aac_bhandle, reg, val) 251 #define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \ 252 sc->aac_bhandle, reg) 253 #define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \ 254 sc->aac_bhandle, reg, val) 255 #define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \ 256 sc->aac_bhandle, reg) 257 #define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \ 258 sc->aac_bhandle, reg, val) 259 #define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \ 260 sc->aac_bhandle, reg) 261 262 /* Define the OS version specific locks */ 263 #if __FreeBSD_version >= 500005 264 #include <sys/lock.h> 265 #include <sys/mutex.h> 266 typedef struct mtx aac_lock_t; 267 #define AAC_LOCK_INIT(l, s) mtx_init(l, s, NULL, MTX_DEF) 268 #define AAC_LOCK_ACQUIRE(l) mtx_lock(l) 269 #define AAC_LOCK_RELEASE(l) mtx_unlock(l) 270 #else 271 typedef struct simplelock aac_lock_t; 272 #define AAC_LOCK_INIT(l, s) simple_lock_init(l) 273 #define AAC_LOCK_ACQUIRE(l) simple_lock(l) 274 #define AAC_LOCK_RELEASE(l) simple_unlock(l) 275 #endif 276 277 #if __FreeBSD_version >= 500005 278 #include <sys/selinfo.h> 279 #else 280 #include <sys/select.h> 281 #endif 282 283 /* 284 * Per-controller structure. 285 */ 286 struct aac_softc 287 { 288 /* bus connections */ 289 device_t aac_dev; 290 struct resource *aac_regs_resource; /* register interface 291 * window */ 292 int aac_regs_rid; /* resource ID */ 293 bus_space_handle_t aac_bhandle; /* bus space handle */ 294 bus_space_tag_t aac_btag; /* bus space tag */ 295 bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */ 296 bus_dma_tag_t aac_buffer_dmat; /* data buffer/command 297 * DMA tag */ 298 struct resource *aac_irq; /* interrupt */ 299 int aac_irq_rid; 300 void *aac_intr; /* interrupt handle */ 301 eventhandler_tag eh; 302 303 /* controller features, limits and status */ 304 int aac_state; 305 #define AAC_STATE_SUSPEND (1<<0) 306 #define AAC_STATE_OPEN (1<<1) 307 #define AAC_STATE_INTERRUPTS_ON (1<<2) 308 #define AAC_STATE_AIF_SLEEPER (1<<3) 309 struct FsaRevision aac_revision; 310 311 /* controller hardware interface */ 312 int aac_hwif; 313 #define AAC_HWIF_I960RX 0 314 #define AAC_HWIF_STRONGARM 1 315 #define AAC_HWIF_FALCON 2 316 #define AAC_HWIF_UNKNOWN -1 317 bus_dma_tag_t aac_common_dmat; /* common structure 318 * DMA tag */ 319 bus_dmamap_t aac_common_dmamap; /* common structure 320 * DMA map */ 321 struct aac_common *aac_common; 322 u_int32_t aac_common_busaddr; 323 struct aac_interface aac_if; 324 325 /* command/fib resources */ 326 bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */ 327 struct aac_fib *aac_fibs; 328 bus_dmamap_t aac_fibmap; 329 u_int32_t aac_fibphys; 330 struct aac_command aac_command[AAC_FIB_COUNT]; 331 332 /* command management */ 333 TAILQ_HEAD(,aac_command) aac_free; /* command structures 334 * available for reuse */ 335 TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for 336 * controller resources */ 337 TAILQ_HEAD(,aac_command) aac_busy; 338 TAILQ_HEAD(,aac_command) aac_complete; /* commands which have been 339 * returned by the controller */ 340 struct bio_queue_head aac_bioq; 341 struct aac_queue_table *aac_queues; 342 struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT]; 343 344 struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */ 345 346 /* connected containters */ 347 TAILQ_HEAD(,aac_container) aac_container_tqh; 348 aac_lock_t aac_container_lock; 349 350 /* Protect the sync fib */ 351 #define AAC_SYNC_LOCK_FORCE (1 << 0) 352 aac_lock_t aac_sync_lock; 353 354 /* delayed activity infrastructure */ 355 #if __FreeBSD_version >= 500005 356 struct task aac_task_complete; /* deferred-completion 357 * task */ 358 #endif 359 struct intr_config_hook aac_ich; 360 361 /* management interface */ 362 dev_t aac_dev_t; 363 aac_lock_t aac_aifq_lock; 364 struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH]; 365 int aac_aifq_head; 366 int aac_aifq_tail; 367 struct selinfo rcv_select; 368 struct proc *aifthread; 369 int aifflags; 370 #define AAC_AIFFLAGS_RUNNING (1 << 0) 371 #define AAC_AIFFLAGS_AIF (1 << 1) 372 #define AAC_AIFFLAGS_EXIT (1 << 2) 373 #define AAC_AIFFLAGS_EXITED (1 << 3) 374 #define AAC_AIFFLAGS_PRINTF (1 << 4) 375 #define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF) 376 u_int32_t quirks; 377 #define AAC_QUIRK_PERC2QC (1 << 0) 378 #define AAC_QUIRK_NOCAM (1 << 1) /* No SCSI passthrough */ 379 #define AAC_QUIRK_CAM_NORESET (1 << 2) /* Fake SCSI resets */ 380 #define AAC_QUIRK_CAM_PASSONLY (1 << 3) /* Only create pass devices */ 381 382 u_int32_t scsi_method_id; 383 TAILQ_HEAD(,aac_sim) aac_sim_tqh; 384 }; 385 386 387 /* 388 * Public functions 389 */ 390 extern void aac_free(struct aac_softc *sc); 391 extern int aac_attach(struct aac_softc *sc); 392 extern int aac_detach(device_t dev); 393 extern int aac_shutdown(device_t dev); 394 extern int aac_suspend(device_t dev); 395 extern int aac_resume(device_t dev); 396 extern void aac_intr(void *arg); 397 extern void aac_submit_bio(struct bio *bp); 398 extern void aac_biodone(struct bio *bp); 399 extern void aac_startio(struct aac_softc *sc); 400 extern int aac_alloc_command(struct aac_softc *sc, 401 struct aac_command **cmp); 402 extern void aac_release_command(struct aac_command *cm); 403 extern int aac_alloc_sync_fib(struct aac_softc *sc, 404 struct aac_fib **fib, int flags); 405 extern void aac_release_sync_fib(struct aac_softc *sc); 406 extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command, 407 u_int32_t xferstate, struct aac_fib *fib, 408 u_int16_t datasize); 409 410 /* 411 * Debugging levels: 412 * 0 - quiet, only emit warnings 413 * 1 - noisy, emit major function points and things done 414 * 2 - extremely noisy, emit trace items in loops, etc. 415 */ 416 #ifdef AAC_DEBUG 417 # define debug(level, fmt, args...) \ 418 do { \ 419 if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \ 420 } while (0) 421 # define debug_called(level) \ 422 do { \ 423 if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \ 424 } while (0) 425 426 extern void aac_print_queues(struct aac_softc *sc); 427 extern void aac_panic(struct aac_softc *sc, char *reason); 428 extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, 429 const char *caller); 430 extern void aac_print_aif(struct aac_softc *sc, 431 struct aac_aif_command *aif); 432 433 #define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__) 434 435 #else 436 # define debug(level, fmt, args...) 437 # define debug_called(level) 438 439 # define aac_print_queues(sc) 440 # define aac_panic(sc, reason) 441 442 # define AAC_PRINT_FIB(sc, fib) 443 # define aac_print_aif(sc, aac_aif_command) 444 #endif 445 446 struct aac_code_lookup { 447 char *string; 448 u_int32_t code; 449 }; 450 451 /* 452 * Queue primitives for driver queues. 453 */ 454 #define AACQ_ADD(sc, qname) \ 455 do { \ 456 struct aac_qstat *qs; \ 457 \ 458 qs = &(sc)->aac_qstat[qname]; \ 459 \ 460 qs->q_length++; \ 461 if (qs->q_length > qs->q_max) \ 462 qs->q_max = qs->q_length; \ 463 } while (0) 464 465 #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length-- 466 #define AACQ_INIT(sc, qname) \ 467 do { \ 468 sc->aac_qstat[qname].q_length = 0; \ 469 sc->aac_qstat[qname].q_max = 0; \ 470 } while (0) 471 472 473 #define AACQ_COMMAND_QUEUE(name, index) \ 474 static __inline void \ 475 aac_initq_ ## name (struct aac_softc *sc) \ 476 { \ 477 TAILQ_INIT(&sc->aac_ ## name); \ 478 AACQ_INIT(sc, index); \ 479 } \ 480 static __inline void \ 481 aac_enqueue_ ## name (struct aac_command *cm) \ 482 { \ 483 int s; \ 484 \ 485 s = splbio(); \ 486 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 487 printf("command %p is on another queue, flags = %#x\n", \ 488 cm, cm->cm_flags); \ 489 panic("command is on another queue"); \ 490 } \ 491 TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 492 cm->cm_flags |= AAC_ON_ ## index; \ 493 AACQ_ADD(cm->cm_sc, index); \ 494 splx(s); \ 495 } \ 496 static __inline void \ 497 aac_requeue_ ## name (struct aac_command *cm) \ 498 { \ 499 int s; \ 500 \ 501 s = splbio(); \ 502 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 503 printf("command %p is on another queue, flags = %#x\n", \ 504 cm, cm->cm_flags); \ 505 panic("command is on another queue"); \ 506 } \ 507 TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 508 cm->cm_flags |= AAC_ON_ ## index; \ 509 AACQ_ADD(cm->cm_sc, index); \ 510 splx(s); \ 511 } \ 512 static __inline struct aac_command * \ 513 aac_dequeue_ ## name (struct aac_softc *sc) \ 514 { \ 515 struct aac_command *cm; \ 516 int s; \ 517 \ 518 s = splbio(); \ 519 if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \ 520 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 521 printf("command %p not in queue, flags = %#x, " \ 522 "bit = %#x\n", cm, cm->cm_flags, \ 523 AAC_ON_ ## index); \ 524 panic("command not in queue"); \ 525 } \ 526 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \ 527 cm->cm_flags &= ~AAC_ON_ ## index; \ 528 AACQ_REMOVE(sc, index); \ 529 } \ 530 splx(s); \ 531 return(cm); \ 532 } \ 533 static __inline void \ 534 aac_remove_ ## name (struct aac_command *cm) \ 535 { \ 536 int s; \ 537 \ 538 s = splbio(); \ 539 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 540 printf("command %p not in queue, flags = %#x, " \ 541 "bit = %#x\n", cm, cm->cm_flags, \ 542 AAC_ON_ ## index); \ 543 panic("command not in queue"); \ 544 } \ 545 TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 546 cm->cm_flags &= ~AAC_ON_ ## index; \ 547 AACQ_REMOVE(cm->cm_sc, index); \ 548 splx(s); \ 549 } \ 550 struct hack 551 552 AACQ_COMMAND_QUEUE(free, AACQ_FREE); 553 AACQ_COMMAND_QUEUE(ready, AACQ_READY); 554 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY); 555 AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE); 556 557 /* 558 * outstanding bio queue 559 */ 560 static __inline void 561 aac_initq_bio(struct aac_softc *sc) 562 { 563 bioq_init(&sc->aac_bioq); 564 AACQ_INIT(sc, AACQ_BIO); 565 } 566 567 static __inline void 568 aac_enqueue_bio(struct aac_softc *sc, struct bio *bp) 569 { 570 int s; 571 572 s = splbio(); 573 bioq_insert_tail(&sc->aac_bioq, bp); 574 AACQ_ADD(sc, AACQ_BIO); 575 splx(s); 576 } 577 578 static __inline struct bio * 579 aac_dequeue_bio(struct aac_softc *sc) 580 { 581 int s; 582 struct bio *bp; 583 584 s = splbio(); 585 if ((bp = bioq_first(&sc->aac_bioq)) != NULL) { 586 bioq_remove(&sc->aac_bioq, bp); 587 AACQ_REMOVE(sc, AACQ_BIO); 588 } 589 splx(s); 590 return(bp); 591 } 592 593 static __inline void 594 aac_print_printf(struct aac_softc *sc) 595 { 596 /* 597 * XXX We have the ability to read the length of the printf string 598 * from out of the mailboxes. 599 */ 600 device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE, 601 sc->aac_common->ac_printf); 602 AAC_QNOTIFY(sc, AAC_DB_PRINTF); 603 } 604