aic7xxx_inline.h revision 76634
1/* 2 * Inline routines shareable across OS platforms. 3 * 4 * Copyright (c) 1994-2001 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * Alternatively, this software may be distributed under the terms of the 17 * GNU Public License ("GPL"). 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $Id: //depot/src/aic7xxx/aic7xxx_inline.h#21 $ 32 * 33 * $FreeBSD: head/sys/dev/aic7xxx/aic7xxx_inline.h 76634 2001-05-15 19:41:12Z gibbs $ 34 */ 35 36#ifndef _AIC7XXX_INLINE_H_ 37#define _AIC7XXX_INLINE_H_ 38 39/************************* Sequencer Execution Control ************************/ 40static __inline int ahc_is_paused(struct ahc_softc *ahc); 41static __inline void ahc_pause_bug_fix(struct ahc_softc *ahc); 42static __inline void ahc_pause(struct ahc_softc *ahc); 43static __inline void ahc_unpause(struct ahc_softc *ahc); 44 45/* 46 * Work around any chip bugs related to halting sequencer execution. 47 * On Ultra2 controllers, we must clear the CIOBUS stretch signal by 48 * reading a register that will set this signal and deassert it. 49 * Without this workaround, if the chip is paused, by an interrupt or 50 * manual pause while accessing scb ram, accesses to certain registers 51 * will hang the system (infinite pci retries). 52 */ 53static __inline void 54ahc_pause_bug_fix(struct ahc_softc *ahc) 55{ 56 if ((ahc->features & AHC_ULTRA2) != 0) 57 (void)ahc_inb(ahc, CCSCBCTL); 58} 59 60/* 61 * Determine whether the sequencer has halted code execution. 62 * Returns non-zero status if the sequencer is stopped. 63 */ 64static __inline int 65ahc_is_paused(struct ahc_softc *ahc) 66{ 67 return ((ahc_inb(ahc, HCNTRL) & PAUSE) != 0); 68} 69 70/* 71 * Request that the sequencer stop and wait, indefinitely, for it 72 * to stop. The sequencer will only acknowledge that it is paused 73 * once it has reached an instruction boundary and PAUSEDIS is 74 * cleared in the SEQCTL register. The sequencer may use PAUSEDIS 75 * for critical sections. 76 */ 77static __inline void 78ahc_pause(struct ahc_softc *ahc) 79{ 80 ahc_outb(ahc, HCNTRL, ahc->pause); 81 82 /* 83 * Since the sequencer can disable pausing in a critical section, we 84 * must loop until it actually stops. 85 */ 86 while (ahc_is_paused(ahc) == 0) 87 ; 88 89 ahc_pause_bug_fix(ahc); 90} 91 92/* 93 * Allow the sequencer to continue program execution. 94 * We check here to ensure that no additional interrupt 95 * sources that would cause the sequencer to halt have been 96 * asserted. If, for example, a SCSI bus reset is detected 97 * while we are fielding a different, pausing, interrupt type, 98 * we don't want to release the sequencer before going back 99 * into our interrupt handler and dealing with this new 100 * condition. 101 */ 102static __inline void 103ahc_unpause(struct ahc_softc *ahc) 104{ 105 if ((ahc_inb(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0) 106 ahc_outb(ahc, HCNTRL, ahc->unpause); 107} 108 109/*********************** Untagged Transaction Routines ************************/ 110static __inline void ahc_freeze_untagged_queues(struct ahc_softc *ahc); 111static __inline void ahc_release_untagged_queues(struct ahc_softc *ahc); 112 113/* 114 * Block our completion routine from starting the next untagged 115 * transaction for this target or target lun. 116 */ 117static __inline void 118ahc_freeze_untagged_queues(struct ahc_softc *ahc) 119{ 120 if ((ahc->flags & AHC_SCB_BTT) == 0) 121 ahc->untagged_queue_lock++; 122} 123 124/* 125 * Allow the next untagged transaction for this target or target lun 126 * to be executed. We use a counting semaphore to allow the lock 127 * to be acquired recursively. Once the count drops to zero, the 128 * transaction queues will be run. 129 */ 130static __inline void 131ahc_release_untagged_queues(struct ahc_softc *ahc) 132{ 133 if ((ahc->flags & AHC_SCB_BTT) == 0) { 134 ahc->untagged_queue_lock--; 135 if (ahc->untagged_queue_lock == 0) 136 ahc_run_untagged_queues(ahc); 137 } 138} 139 140/************************** Memory mapping routines ***************************/ 141static __inline struct ahc_dma_seg * 142 ahc_sg_bus_to_virt(struct scb *scb, 143 uint32_t sg_busaddr); 144static __inline uint32_t 145 ahc_sg_virt_to_bus(struct scb *scb, 146 struct ahc_dma_seg *sg); 147static __inline uint32_t 148 ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index); 149 150static __inline struct ahc_dma_seg * 151ahc_sg_bus_to_virt(struct scb *scb, uint32_t sg_busaddr) 152{ 153 int sg_index; 154 155 sg_index = (sg_busaddr - scb->sg_list_phys)/sizeof(struct ahc_dma_seg); 156 /* sg_list_phys points to entry 1, not 0 */ 157 sg_index++; 158 159 return (&scb->sg_list[sg_index]); 160} 161 162static __inline uint32_t 163ahc_sg_virt_to_bus(struct scb *scb, struct ahc_dma_seg *sg) 164{ 165 int sg_index; 166 167 /* sg_list_phys points to entry 1, not 0 */ 168 sg_index = sg - &scb->sg_list[1]; 169 170 return (scb->sg_list_phys + (sg_index * sizeof(*scb->sg_list))); 171} 172 173static __inline uint32_t 174ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index) 175{ 176 return (ahc->scb_data->hscb_busaddr 177 + (sizeof(struct hardware_scb) * index)); 178} 179 180/******************************** Debugging ***********************************/ 181static __inline char *ahc_name(struct ahc_softc *ahc); 182 183static __inline char * 184ahc_name(struct ahc_softc *ahc) 185{ 186 return (ahc->name); 187} 188 189/*********************** Miscelaneous Support Functions ***********************/ 190 191static __inline void ahc_update_residual(struct scb *scb); 192static __inline struct ahc_initiator_tinfo * 193 ahc_fetch_transinfo(struct ahc_softc *ahc, 194 char channel, u_int our_id, 195 u_int remote_id, 196 struct ahc_tmode_tstate **tstate); 197static __inline struct scb* 198 ahc_get_scb(struct ahc_softc *ahc); 199static __inline void ahc_free_scb(struct ahc_softc *ahc, struct scb *scb); 200static __inline void ahc_swap_with_next_hscb(struct ahc_softc *ahc, 201 struct scb *scb); 202static __inline void ahc_queue_scb(struct ahc_softc *ahc, struct scb *scb); 203static __inline struct scsi_sense_data * 204 ahc_get_sense_buf(struct ahc_softc *ahc, 205 struct scb *scb); 206static __inline uint32_t 207 ahc_get_sense_bufaddr(struct ahc_softc *ahc, 208 struct scb *scb); 209 210/* 211 * Determine whether the sequencer reported a residual 212 * for this SCB/transaction. 213 */ 214static __inline void 215ahc_update_residual(struct scb *scb) 216{ 217 uint32_t sgptr; 218 219 sgptr = ahc_le32toh(scb->hscb->sgptr); 220 if ((sgptr & SG_RESID_VALID) != 0) 221 ahc_calc_residual(scb); 222 else 223 ahc_set_residual(scb, 0); 224} 225 226/* 227 * Return pointers to the transfer negotiation information 228 * for the specified our_id/remote_id pair. 229 */ 230static __inline struct ahc_initiator_tinfo * 231ahc_fetch_transinfo(struct ahc_softc *ahc, char channel, u_int our_id, 232 u_int remote_id, struct ahc_tmode_tstate **tstate) 233{ 234 /* 235 * Transfer data structures are stored from the perspective 236 * of the target role. Since the parameters for a connection 237 * in the initiator role to a given target are the same as 238 * when the roles are reversed, we pretend we are the target. 239 */ 240 if (channel == 'B') 241 our_id += 8; 242 *tstate = ahc->enabled_targets[our_id]; 243 return (&(*tstate)->transinfo[remote_id]); 244} 245 246/* 247 * Get a free scb. If there are none, see if we can allocate a new SCB. 248 */ 249static __inline struct scb * 250ahc_get_scb(struct ahc_softc *ahc) 251{ 252 struct scb *scb; 253 254 if ((scb = SLIST_FIRST(&ahc->scb_data->free_scbs)) == NULL) { 255 ahc_alloc_scbs(ahc); 256 scb = SLIST_FIRST(&ahc->scb_data->free_scbs); 257 if (scb == NULL) 258 return (NULL); 259 } 260 SLIST_REMOVE_HEAD(&ahc->scb_data->free_scbs, links.sle); 261 return (scb); 262} 263 264/* 265 * Return an SCB resource to the free list. 266 */ 267static __inline void 268ahc_free_scb(struct ahc_softc *ahc, struct scb *scb) 269{ 270 struct hardware_scb *hscb; 271 272 hscb = scb->hscb; 273 /* Clean up for the next user */ 274 ahc->scb_data->scbindex[hscb->tag] = NULL; 275 scb->flags = SCB_FREE; 276 hscb->control = 0; 277 278 SLIST_INSERT_HEAD(&ahc->scb_data->free_scbs, scb, links.sle); 279 280 /* Notify the OSM that a resource is now available. */ 281 ahc_platform_scb_free(ahc, scb); 282} 283 284static __inline struct scb * 285ahc_lookup_scb(struct ahc_softc *ahc, u_int tag) 286{ 287 return (ahc->scb_data->scbindex[tag]); 288 289} 290 291static __inline void 292ahc_swap_with_next_hscb(struct ahc_softc *ahc, struct scb *scb) 293{ 294 struct hardware_scb *q_hscb; 295 u_int saved_tag; 296 297 /* 298 * Our queuing method is a bit tricky. The card 299 * knows in advance which HSCB to download, and we 300 * can't disappoint it. To achieve this, the next 301 * SCB to download is saved off in ahc->next_queued_scb. 302 * When we are called to queue "an arbitrary scb", 303 * we copy the contents of the incoming HSCB to the one 304 * the sequencer knows about, swap HSCB pointers and 305 * finally assign the SCB to the tag indexed location 306 * in the scb_array. This makes sure that we can still 307 * locate the correct SCB by SCB_TAG. 308 */ 309 q_hscb = ahc->next_queued_scb->hscb; 310 saved_tag = q_hscb->tag; 311 memcpy(q_hscb, scb->hscb, sizeof(*scb->hscb)); 312 if ((scb->flags & SCB_CDB32_PTR) != 0) { 313 q_hscb->shared_data.cdb_ptr = 314 ahc_hscb_busaddr(ahc, q_hscb->tag) 315 + offsetof(struct hardware_scb, cdb32); 316 } 317 q_hscb->tag = saved_tag; 318 q_hscb->next = scb->hscb->tag; 319 320 /* Now swap HSCB pointers. */ 321 ahc->next_queued_scb->hscb = scb->hscb; 322 scb->hscb = q_hscb; 323 324 /* Now define the mapping from tag to SCB in the scbindex */ 325 ahc->scb_data->scbindex[scb->hscb->tag] = scb; 326} 327 328/* 329 * Tell the sequencer about a new transaction to execute. 330 */ 331static __inline void 332ahc_queue_scb(struct ahc_softc *ahc, struct scb *scb) 333{ 334 ahc_swap_with_next_hscb(ahc, scb); 335 336 if (scb->hscb->tag == SCB_LIST_NULL 337 || scb->hscb->next == SCB_LIST_NULL) 338 panic("Attempt to queue invalid SCB tag %x:%x\n", 339 scb->hscb->tag, scb->hscb->next); 340 341 /* 342 * Keep a history of SCBs we've downloaded in the qinfifo. 343 */ 344 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag; 345 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 346 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext); 347 } else { 348 if ((ahc->features & AHC_AUTOPAUSE) == 0) 349 ahc_pause(ahc); 350 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext); 351 if ((ahc->features & AHC_AUTOPAUSE) == 0) 352 ahc_unpause(ahc); 353 } 354} 355 356static __inline struct scsi_sense_data * 357ahc_get_sense_buf(struct ahc_softc *ahc, struct scb *scb) 358{ 359 int offset; 360 361 offset = scb - ahc->scb_data->scbarray; 362 return (&ahc->scb_data->sense[offset]); 363} 364 365static __inline uint32_t 366ahc_get_sense_bufaddr(struct ahc_softc *ahc, struct scb *scb) 367{ 368 int offset; 369 370 offset = scb - ahc->scb_data->scbarray; 371 return (ahc->scb_data->sense_busaddr 372 + (offset * sizeof(struct scsi_sense_data))); 373} 374 375/************************** Interrupt Processing ******************************/ 376static __inline u_int ahc_check_cmdcmpltqueues(struct ahc_softc *ahc); 377static __inline void ahc_intr(struct ahc_softc *ahc); 378 379/* 380 * See if the firmware has posted any completed commands 381 * into our in-core command complete fifos. 382 */ 383#define AHC_RUN_QOUTFIFO 0x1 384#define AHC_RUN_TQINFIFO 0x2 385static __inline u_int 386ahc_check_cmdcmpltqueues(struct ahc_softc *ahc) 387{ 388 u_int retval; 389 390 retval = 0; 391 if (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) 392 retval |= AHC_RUN_QOUTFIFO; 393#ifdef AHC_TARGET_MODE 394 if ((ahc->flags & AHC_TARGETROLE) != 0 395 && ahc->targetcmds[ahc->tqinfifonext].cmd_valid != 0) 396 retval |= AHC_RUN_TQINFIFO; 397#endif 398 return (retval); 399} 400 401/* 402 * Catch an interrupt from the adapter 403 */ 404static __inline void 405ahc_intr(struct ahc_softc *ahc) 406{ 407 u_int intstat; 408 u_int queuestat; 409 410 /* 411 * Instead of directly reading the interrupt status register, 412 * infer the cause of the interrupt by checking our in-core 413 * completion queues. This avoids a costly PCI bus read in 414 * most cases. 415 */ 416 if ((ahc->flags & (AHC_ALL_INTERRUPTS|AHC_EDGE_INTERRUPT)) == 0 417 && (queuestat = ahc_check_cmdcmpltqueues(ahc)) != 0) 418 intstat = CMDCMPLT; 419 else { 420 intstat = ahc_inb(ahc, INTSTAT); 421 /* 422 * We can't generate queuestat once above 423 * or we are exposed to a race when our 424 * interrupt is shared with another device. 425 * if instat showed a command complete interrupt, 426 * but our first generation of queue stat 427 * "just missed" the delivery of this transaction, 428 * we would clear the command complete interrupt 429 * below without ever servicing the completed 430 * command. 431 */ 432 queuestat = ahc_check_cmdcmpltqueues(ahc); 433#if AHC_PCI_CONFIG > 0 434 if (ahc->unsolicited_ints > 500 435 && (ahc->chip & AHC_PCI) != 0 436 && (ahc_inb(ahc, ERROR) & PCIERRSTAT) != 0) 437 ahc->bus_intr(ahc); 438#endif 439 } 440 441 if (intstat == 0xFF && (ahc->features & AHC_REMOVABLE) != 0) 442 /* Hot eject */ 443 return; 444 445 if ((intstat & INT_PEND) == 0) { 446 ahc->unsolicited_ints++; 447 return; 448 } 449 ahc->unsolicited_ints = 0; 450 451 if (intstat & CMDCMPLT) { 452 ahc_outb(ahc, CLRINT, CLRCMDINT); 453 454 /* 455 * Ensure that the chip sees that we've cleared 456 * this interrupt before we walk the output fifo. 457 * Otherwise, we may, due to posted bus writes, 458 * clear the interrupt after we finish the scan, 459 * and after the sequencer has added new entries 460 * and asserted the interrupt again. 461 */ 462 ahc_flush_device_writes(ahc); 463#ifdef AHC_TARGET_MODE 464 if ((queuestat & AHC_RUN_QOUTFIFO) != 0) 465#endif 466 ahc_run_qoutfifo(ahc); 467#ifdef AHC_TARGET_MODE 468 if ((queuestat & AHC_RUN_TQINFIFO) != 0) 469 ahc_run_tqinfifo(ahc, /*paused*/FALSE); 470#endif 471 } 472 if (intstat & BRKADRINT) { 473 ahc_handle_brkadrint(ahc); 474 /* Fatal error, no more interrupts to handle. */ 475 return; 476 } 477 478 if ((intstat & (SEQINT|SCSIINT)) != 0) 479 ahc_pause_bug_fix(ahc); 480 481 if ((intstat & SEQINT) != 0) 482 ahc_handle_seqint(ahc, intstat); 483 484 if ((intstat & SCSIINT) != 0) 485 ahc_handle_scsiint(ahc, intstat); 486} 487 488#endif /* _AIC7XXX_INLINE_H_ */ 489