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 #include <sys/bio.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/taskqueue.h> 36 #include <sys/selinfo.h> 37 38 /* 39 * Driver Parameter Definitions 40 */ 41 42 /* 43 * The firmware interface allows for a 16-bit s/g list length. We limit 44 * ourselves to a reasonable maximum and ensure alignment. 45 */ 46 #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */ 47 48 /* 49 * We allocate a small set of FIBs for the adapter to use to send us messages. 50 */ 51 #define AAC_ADAPTER_FIBS 8 52 53 /* 54 * FIBs are allocated in page-size chunks and can grow up to the 512 55 * limit imposed by the hardware. 56 */ 57 #define AAC_FIB_COUNT (PAGE_SIZE/sizeof(struct aac_fib)) 58 #define AAC_PREALLOCATE_FIBS 128 59 #define AAC_MAX_FIBS 512 60 61 /* 62 * The controller reports status events in AIFs. We hang on to a number of 63 * these in order to pass them out to user-space management tools. 64 */ 65 #define AAC_AIFQ_LENGTH 64 66 67 /* 68 * Firmware messages are passed in the printf buffer. 69 */ 70 #define AAC_PRINTF_BUFSIZE 256 71 72 /* 73 * We wait this many seconds for the adapter to come ready if it is still 74 * booting 75 */ 76 #define AAC_BOOT_TIMEOUT (3 * 60) 77 78 /* 79 * Timeout for immediate commands. 80 */ 81 #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */ 82 83 /* 84 * Timeout for normal commands 85 */ 86 #define AAC_CMD_TIMEOUT 30 /* seconds */ 87 88 /* 89 * Rate at which we periodically check for timed out commands and kick the 90 * controller. 91 */ 92 #define AAC_PERIODIC_INTERVAL 20 /* seconds */ 93 94 /* 95 * Per-container data structure 96 */ 97 struct aac_container 98 { 99 struct aac_mntobj co_mntobj; 100 device_t co_disk; 101 int co_found; 102 TAILQ_ENTRY(aac_container) co_link; 103 }; 104 105 /* 106 * Per-SIM data structure 107 */ 108 struct aac_sim 109 { 110 device_t sim_dev; 111 int TargetsPerBus; 112 int BusNumber; 113 int InitiatorBusId; 114 struct aac_softc *aac_sc; 115 TAILQ_ENTRY(aac_sim) sim_link; 116 }; 117 118 /* 119 * Per-disk structure 120 */ 121 struct aac_disk 122 { 123 device_t ad_dev; 124 struct aac_softc *ad_controller; 125 struct aac_container *ad_container; 126 struct disk ad_disk; 127 struct devstat ad_stats; 128 int ad_flags; 129 #define AAC_DISK_OPEN (1<<0) 130 int ad_cylinders; 131 int ad_heads; 132 int ad_sectors; 133 u_int32_t ad_size; 134 int unit; 135 }; 136 137 /* 138 * Per-command control structure. 139 */ 140 struct aac_command 141 { 142 TAILQ_ENTRY(aac_command) cm_link; /* list linkage */ 143 144 struct aac_softc *cm_sc; /* controller that owns us */ 145 146 struct aac_fib *cm_fib; /* FIB associated with this 147 * command */ 148 u_int32_t cm_fibphys; /* bus address of the FIB */ 149 struct bio *cm_data; /* pointer to data in kernel 150 * space */ 151 u_int32_t cm_datalen; /* data length */ 152 bus_dmamap_t cm_datamap; /* DMA map for bio data */ 153 struct aac_sg_table *cm_sgtable; /* pointer to s/g table in 154 * command */ 155 int cm_flags; 156 #define AAC_CMD_MAPPED (1<<0) /* command has had its data 157 * mapped */ 158 #define AAC_CMD_DATAIN (1<<1) /* command involves data moving 159 * from controller to host */ 160 #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving 161 * from host to controller */ 162 #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */ 163 #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */ 164 #define AAC_ON_AACQ_FREE (1<<5) 165 #define AAC_ON_AACQ_READY (1<<6) 166 #define AAC_ON_AACQ_BUSY (1<<7) 167 #define AAC_ON_AACQ_COMPLETE (1<<8) 168 #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)) 169 170 void (* cm_complete)(struct aac_command *cm); 171 void *cm_private; 172 time_t cm_timestamp; /* command creation time */ 173 int cm_queue; 174 int cm_index; 175 }; 176 177 struct aac_fibmap { 178 TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */ 179 struct aac_fib *aac_fibs; 180 bus_dmamap_t aac_fibmap; 181 struct aac_command *aac_commands; 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 typedef struct mtx aac_lock_t; 264 #define AAC_LOCK_INIT(l, s) mtx_init(l, s, NULL, MTX_DEF) 265 #define AAC_LOCK_ACQUIRE(l) mtx_lock(l) 266 #define AAC_LOCK_RELEASE(l) mtx_unlock(l) 267 268 269 /* 270 * Per-controller structure. 271 */ 272 struct aac_softc 273 { 274 /* bus connections */ 275 device_t aac_dev; 276 struct resource *aac_regs_resource; /* register interface 277 * window */ 278 int aac_regs_rid; /* resource ID */ 279 bus_space_handle_t aac_bhandle; /* bus space handle */ 280 bus_space_tag_t aac_btag; /* bus space tag */ 281 bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */ 282 bus_dma_tag_t aac_buffer_dmat; /* data buffer/command 283 * DMA tag */ 284 struct resource *aac_irq; /* interrupt */ 285 int aac_irq_rid; 286 void *aac_intr; /* interrupt handle */ 287 eventhandler_tag eh; 288 289 /* controller features, limits and status */ 290 int aac_state; 291 #define AAC_STATE_SUSPEND (1<<0) 292 #define AAC_STATE_OPEN (1<<1) 293 #define AAC_STATE_INTERRUPTS_ON (1<<2) 294 #define AAC_STATE_AIF_SLEEPER (1<<3) 295 struct FsaRevision aac_revision; 296 297 /* controller hardware interface */ 298 int aac_hwif; 299 #define AAC_HWIF_I960RX 0 300 #define AAC_HWIF_STRONGARM 1 301 #define AAC_HWIF_FALCON 2 302 #define AAC_HWIF_UNKNOWN -1 303 bus_dma_tag_t aac_common_dmat; /* common structure 304 * DMA tag */ 305 bus_dmamap_t aac_common_dmamap; /* common structure 306 * DMA map */ 307 struct aac_common *aac_common; 308 u_int32_t aac_common_busaddr; 309 struct aac_interface aac_if; 310 311 /* command/fib resources */ 312 bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */ 313 TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh; 314 uint total_fibs; 315 struct aac_command *aac_commands; 316 317 /* command management */ 318 TAILQ_HEAD(,aac_command) aac_free; /* command structures 319 * available for reuse */ 320 TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for 321 * controller resources */ 322 TAILQ_HEAD(,aac_command) aac_busy; 323 struct bio_queue_head aac_bioq; 324 struct aac_queue_table *aac_queues; 325 struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT]; 326 327 struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */ 328 329 /* connected containters */ 330 TAILQ_HEAD(,aac_container) aac_container_tqh; 331 aac_lock_t aac_container_lock; 332 333 /* Protect the sync fib */ 334 #define AAC_SYNC_LOCK_FORCE (1 << 0) 335 aac_lock_t aac_sync_lock; 336 337 aac_lock_t aac_io_lock; 338 339 /* delayed activity infrastructure */ 340 struct task aac_task_complete; /* deferred-completion 341 * task */ 342 struct intr_config_hook aac_ich; 343 344 /* management interface */ 345 dev_t aac_dev_t; 346 aac_lock_t aac_aifq_lock; 347 struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH]; 348 int aac_aifq_head; 349 int aac_aifq_tail; 350 struct selinfo rcv_select; 351 struct proc *aifthread; 352 int aifflags; 353 #define AAC_AIFFLAGS_RUNNING (1 << 0) 354 #define AAC_AIFFLAGS_AIF (1 << 1) 355 #define AAC_AIFFLAGS_EXIT (1 << 2) 356 #define AAC_AIFFLAGS_EXITED (1 << 3) 357 #define AAC_AIFFLAGS_PRINTF (1 << 4) 358 #define AAC_AIFFLAGS_ALLOCFIBS (1 << 5) 359 #define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \ 360 AAC_AIFFLAGS_ALLOCFIBS) 361 u_int32_t quirks; 362 #define AAC_QUIRK_PERC2QC (1 << 0) 363 #define AAC_QUIRK_NOCAM (1 << 1) /* No SCSI passthrough */ 364 #define AAC_QUIRK_CAM_NORESET (1 << 2) /* Fake SCSI resets */ 365 #define AAC_QUIRK_CAM_PASSONLY (1 << 3) /* Only create pass devices */ 366 367 u_int32_t scsi_method_id; 368 TAILQ_HEAD(,aac_sim) aac_sim_tqh; 369 }; 370 371 372 /* 373 * Public functions 374 */ 375 extern void aac_free(struct aac_softc *sc); 376 extern int aac_attach(struct aac_softc *sc); 377 extern int aac_detach(device_t dev); 378 extern int aac_shutdown(device_t dev); 379 extern int aac_suspend(device_t dev); 380 extern int aac_resume(device_t dev); 381 extern void aac_intr(void *arg); 382 extern void aac_submit_bio(struct bio *bp); 383 extern void aac_biodone(struct bio *bp); 384 extern void aac_startio(struct aac_softc *sc); 385 extern int aac_alloc_command(struct aac_softc *sc, 386 struct aac_command **cmp); 387 extern void aac_release_command(struct aac_command *cm); 388 extern int aac_alloc_sync_fib(struct aac_softc *sc, 389 struct aac_fib **fib, int flags); 390 extern void aac_release_sync_fib(struct aac_softc *sc); 391 extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command, 392 u_int32_t xferstate, struct aac_fib *fib, 393 u_int16_t datasize); 394 395 /* 396 * Debugging levels: 397 * 0 - quiet, only emit warnings 398 * 1 - noisy, emit major function points and things done 399 * 2 - extremely noisy, emit trace items in loops, etc. 400 */ 401 #ifdef AAC_DEBUG 402 # define debug(level, fmt, args...) \ 403 do { \ 404 if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \ 405 } while (0) 406 # define debug_called(level) \ 407 do { \ 408 if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \ 409 } while (0) 410 411 extern void aac_print_queues(struct aac_softc *sc); 412 extern void aac_panic(struct aac_softc *sc, char *reason); 413 extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib, 414 const char *caller); 415 extern void aac_print_aif(struct aac_softc *sc, 416 struct aac_aif_command *aif); 417 418 #define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__) 419 420 #else 421 # define debug(level, fmt, args...) 422 # define debug_called(level) 423 424 # define aac_print_queues(sc) 425 # define aac_panic(sc, reason) 426 427 # define AAC_PRINT_FIB(sc, fib) 428 # define aac_print_aif(sc, aac_aif_command) 429 #endif 430 431 struct aac_code_lookup { 432 char *string; 433 u_int32_t code; 434 }; 435 436 /* 437 * Queue primitives for driver queues. 438 */ 439 #define AACQ_ADD(sc, qname) \ 440 do { \ 441 struct aac_qstat *qs; \ 442 \ 443 qs = &(sc)->aac_qstat[qname]; \ 444 \ 445 qs->q_length++; \ 446 if (qs->q_length > qs->q_max) \ 447 qs->q_max = qs->q_length; \ 448 } while (0) 449 450 #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length-- 451 #define AACQ_INIT(sc, qname) \ 452 do { \ 453 sc->aac_qstat[qname].q_length = 0; \ 454 sc->aac_qstat[qname].q_max = 0; \ 455 } while (0) 456 457 458 #define AACQ_COMMAND_QUEUE(name, index) \ 459 static __inline void \ 460 aac_initq_ ## name (struct aac_softc *sc) \ 461 { \ 462 TAILQ_INIT(&sc->aac_ ## name); \ 463 AACQ_INIT(sc, index); \ 464 } \ 465 static __inline void \ 466 aac_enqueue_ ## name (struct aac_command *cm) \ 467 { \ 468 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 469 printf("command %p is on another queue, flags = %#x\n", \ 470 cm, cm->cm_flags); \ 471 panic("command is on another queue"); \ 472 } \ 473 TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 474 cm->cm_flags |= AAC_ON_ ## index; \ 475 AACQ_ADD(cm->cm_sc, index); \ 476 } \ 477 static __inline void \ 478 aac_requeue_ ## name (struct aac_command *cm) \ 479 { \ 480 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \ 481 printf("command %p is on another queue, flags = %#x\n", \ 482 cm, cm->cm_flags); \ 483 panic("command is on another queue"); \ 484 } \ 485 TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 486 cm->cm_flags |= AAC_ON_ ## index; \ 487 AACQ_ADD(cm->cm_sc, index); \ 488 } \ 489 static __inline struct aac_command * \ 490 aac_dequeue_ ## name (struct aac_softc *sc) \ 491 { \ 492 struct aac_command *cm; \ 493 \ 494 if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \ 495 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 496 printf("command %p not in queue, flags = %#x, " \ 497 "bit = %#x\n", cm, cm->cm_flags, \ 498 AAC_ON_ ## index); \ 499 panic("command not in queue"); \ 500 } \ 501 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \ 502 cm->cm_flags &= ~AAC_ON_ ## index; \ 503 AACQ_REMOVE(sc, index); \ 504 } \ 505 return(cm); \ 506 } \ 507 static __inline void \ 508 aac_remove_ ## name (struct aac_command *cm) \ 509 { \ 510 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \ 511 printf("command %p not in queue, flags = %#x, " \ 512 "bit = %#x\n", cm, cm->cm_flags, \ 513 AAC_ON_ ## index); \ 514 panic("command not in queue"); \ 515 } \ 516 TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \ 517 cm->cm_flags &= ~AAC_ON_ ## index; \ 518 AACQ_REMOVE(cm->cm_sc, index); \ 519 } \ 520 struct hack 521 522 AACQ_COMMAND_QUEUE(free, AACQ_FREE); 523 AACQ_COMMAND_QUEUE(ready, AACQ_READY); 524 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY); 525 526 /* 527 * outstanding bio queue 528 */ 529 static __inline void 530 aac_initq_bio(struct aac_softc *sc) 531 { 532 bioq_init(&sc->aac_bioq); 533 AACQ_INIT(sc, AACQ_BIO); 534 } 535 536 static __inline void 537 aac_enqueue_bio(struct aac_softc *sc, struct bio *bp) 538 { 539 bioq_insert_tail(&sc->aac_bioq, bp); 540 AACQ_ADD(sc, AACQ_BIO); 541 } 542 543 static __inline struct bio * 544 aac_dequeue_bio(struct aac_softc *sc) 545 { 546 struct bio *bp; 547 548 if ((bp = bioq_first(&sc->aac_bioq)) != NULL) { 549 bioq_remove(&sc->aac_bioq, bp); 550 AACQ_REMOVE(sc, AACQ_BIO); 551 } 552 return(bp); 553 } 554 555 static __inline void 556 aac_print_printf(struct aac_softc *sc) 557 { 558 /* 559 * XXX We have the ability to read the length of the printf string 560 * from out of the mailboxes. 561 */ 562 device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE, 563 sc->aac_common->ac_printf); 564 AAC_QNOTIFY(sc, AAC_DB_PRINTF); 565 } 566