aic7xxx.c revision 39506
1/* 2 * Generic driver for the aic7xxx based adaptec SCSI controllers 3 * Product specific probe and attach routines can be found in: 4 * i386/eisa/ahc_eisa.c 27/284X and aic7770 motherboard controllers 5 * pci/ahc_pci.c 3985, 3980, 3940, 2940, aic7895, aic7890, 6 * aic7880, aic7870, aic7860, and aic7850 controllers 7 * 8 * Copyright (c) 1994, 1995, 1996, 1997, 1998 Justin T. Gibbs. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions, and the following disclaimer, 16 * without modification, immediately at the beginning of the file. 17 * 2. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * Where this Software is combined with software released under the terms of 21 * the GNU Public License ("GPL") and the terms of the GPL would require the 22 * combined work to also be released under the terms of the GPL, the terms 23 * and conditions of this License will apply in addition to those of the 24 * GPL with the exception of any terms or conditions of this License that 25 * conflict with, or are expressly prohibited by, the GPL. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 31 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * $Id: aic7xxx.c,v 1.3 1998/09/18 03:42:16 gibbs Exp $ 40 */ 41/* 42 * A few notes on features of the driver. 43 * 44 * SCB paging takes advantage of the fact that devices stay disconnected 45 * from the bus a relatively long time and that while they're disconnected, 46 * having the SCBs for these transactions down on the host adapter is of 47 * little use. Instead of leaving this idle SCB down on the card we copy 48 * it back up into kernel memory and reuse the SCB slot on the card to 49 * schedule another transaction. This can be a real payoff when doing random 50 * I/O to tagged queueing devices since there are more transactions active at 51 * once for the device to sort for optimal seek reduction. The algorithm goes 52 * like this... 53 * 54 * The sequencer maintains two lists of its hardware SCBs. The first is the 55 * singly linked free list which tracks all SCBs that are not currently in 56 * use. The second is the doubly linked disconnected list which holds the 57 * SCBs of transactions that are in the disconnected state sorted most 58 * recently disconnected first. When the kernel queues a transaction to 59 * the card, a hardware SCB to "house" this transaction is retrieved from 60 * either of these two lists. If the SCB came from the disconnected list, 61 * a check is made to see if any data transfer or SCB linking (more on linking 62 * in a bit) information has been changed since it was copied from the host 63 * and if so, DMAs the SCB back up before it can be used. Once a hardware 64 * SCB has been obtained, the SCB is DMAed from the host. Before any work 65 * can begin on this SCB, the sequencer must ensure that either the SCB is 66 * for a tagged transaction or the target is not already working on another 67 * non-tagged transaction. If a conflict arises in the non-tagged case, the 68 * sequencer finds the SCB for the active transactions and sets the SCB_LINKED 69 * field in that SCB to this next SCB to execute. To facilitate finding 70 * active non-tagged SCBs, the last four bytes of up to the first four hardware 71 * SCBs serve as a storage area for the currently active SCB ID for each 72 * target. 73 * 74 * When a device reconnects, a search is made of the hardware SCBs to find 75 * the SCB for this transaction. If the search fails, a hardware SCB is 76 * pulled from either the free or disconnected SCB list and the proper 77 * SCB is DMAed from the host. If the MK_MESSAGE control bit is set 78 * in the control byte of the SCB while it was disconnected, the sequencer 79 * will assert ATN and attempt to issue a message to the host. 80 * 81 * When a command completes, a check for non-zero status and residuals is 82 * made. If either of these conditions exists, the SCB is DMAed back up to 83 * the host so that it can interpret this information. Additionally, in the 84 * case of bad status, the sequencer generates a special interrupt and pauses 85 * itself. This allows the host to setup a request sense command if it 86 * chooses for this target synchronously with the error so that sense 87 * information isn't lost. 88 * 89 */ 90 91#include <opt_aic7xxx.h> 92 93#include <pci.h> 94#include <stddef.h> /* For offsetof */ 95 96#include <sys/param.h> 97#include <sys/systm.h> 98#include <sys/malloc.h> 99#include <sys/buf.h> 100#include <sys/proc.h> 101 102#include <cam/cam.h> 103#include <cam/cam_ccb.h> 104#include <cam/cam_sim.h> 105#include <cam/cam_xpt_sim.h> 106#include <cam/cam_debug.h> 107 108#include <cam/scsi/scsi_all.h> 109#include <cam/scsi/scsi_message.h> 110 111#if NPCI > 0 112#include <machine/bus_memio.h> 113#endif 114#include <machine/bus_pio.h> 115#include <machine/bus.h> 116#include <machine/clock.h> 117 118#include <vm/vm.h> 119#include <vm/vm_param.h> 120#include <vm/pmap.h> 121 122#include <dev/aic7xxx/aic7xxx.h> 123#include <dev/aic7xxx/sequencer.h> 124 125#include <aic7xxx_reg.h> 126#include <aic7xxx_seq.h> 127 128#include <sys/kernel.h> 129 130#ifndef AHC_TMODE_ENABLE 131#define AHC_TMODE_ENABLE 0 132#endif 133 134#define MAX(a,b) (((a) > (b)) ? (a) : (b)) 135#define MIN(a,b) (((a) < (b)) ? (a) : (b)) 136#define ALL_TARGETS (~0) 137#define ALL_LUNS (~0) 138#define ALL_CHANNELS '\0' 139 140#define SIM_IS_SCSIBUS_B(ahc, sim) \ 141 (sim == ahc->sim_b) 142#define SCB_IS_SCSIBUS_B(scb) \ 143 (((scb)->hscb->tcl & SELBUSB) != 0) 144#define SCB_TARGET(scb) \ 145 (((scb)->hscb->tcl & TID) >> 4) 146#define SCB_CHANNEL(scb) \ 147 (SCB_IS_SCSIBUS_B(scb) ? 'B' : 'A') 148#define SCB_LUN(scb) \ 149 ((scb)->hscb->tcl & LID) 150#define SCB_TARGET_OFFSET(scb) \ 151 (SCB_TARGET(scb) + (SCB_IS_SCSIBUS_B(scb) ? 8 : 0)) 152#define SCB_TARGET_MASK(scb) \ 153 (0x01 << (SCB_TARGET_OFFSET(scb))) 154 155#define ccb_scb_ptr spriv_ptr0 156#define ccb_ahc_ptr spriv_ptr1 157 158struct ahc_devinfo { 159 int target_offset; 160 u_int16_t target_mask; 161 u_int8_t target; 162 char channel; 163}; 164 165typedef enum { 166 SEARCH_COMPLETE, 167 SEARCH_COUNT, 168 SEARCH_REMOVE 169} ahc_search_action; 170 171u_long ahc_unit = 0; 172 173#ifdef AHC_DEBUG 174static int ahc_debug = AHC_DEBUG; 175#endif 176 177#if NPCI > 0 178void ahc_pci_intr(struct ahc_softc *ahc); 179#endif 180 181static void ahc_dump_targcmd(struct target_cmd *cmd); 182static void ahc_shutdown(int howto, void *arg); 183static void ahcminphys(struct buf *bp); 184static cam_status 185 ahc_find_tmode_devs(struct ahc_softc *ahc, 186 struct cam_sim *sim, union ccb *ccb, 187 struct tmode_tstate **tstate, 188 struct tmode_lstate **lstate, 189 int notfound_failure); 190static void ahc_action(struct cam_sim *sim, union ccb *ccb); 191static void ahc_async(void *callback_arg, u_int32_t code, 192 struct cam_path *path, void *arg); 193static void ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, 194 int nsegments, int error); 195static void ahc_poll(struct cam_sim *sim); 196static void ahc_setup_data(struct ahc_softc *ahc, 197 struct ccb_scsiio *csio, struct scb *scb); 198static void ahc_freeze_devq(struct ahc_softc *ahc, struct cam_path *path); 199static struct scb * 200 ahc_get_scb(struct ahc_softc *ahc); 201static void ahc_free_scb(struct ahc_softc *ahc, struct scb *scb); 202static struct scb * 203 ahc_alloc_scb(struct ahc_softc *ahc); 204static void ahc_fetch_devinfo(struct ahc_softc *ahc, 205 struct ahc_devinfo *devinfo); 206static void ahc_compile_devinfo(struct ahc_devinfo *devinfo, 207 u_int target, char channel); 208static u_int ahc_abort_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev); 209static void ahc_done(struct ahc_softc *ahc, struct scb *scbp); 210static void ahc_handle_target_cmd(struct ahc_softc *ahc); 211static void ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat); 212static void ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat); 213static void ahc_handle_reqinit(struct ahc_softc *ahc, 214 struct scb *scb); 215static int ahc_parse_msg(struct ahc_softc *ahc, struct scb *scb, 216 struct ahc_devinfo *devinfo); 217static void ahc_handle_devreset(struct ahc_softc *ahc, int target, 218 char channel, cam_status status, 219 ac_code acode, char *message, 220 int verbose_only); 221static void ahc_loadseq(struct ahc_softc *ahc); 222static int ahc_check_patch(struct ahc_softc *ahc, 223 struct patch **start_patch, 224 int start_instr, int *skip_addr); 225static void ahc_download_instr(struct ahc_softc *ahc, 226 int instrptr, u_int8_t *dconsts); 227static int ahc_match_scb(struct scb *scb, int target, char channel, 228 int lun, u_int tag); 229#ifdef AHC_DEBUG 230static void ahc_print_scb(struct scb *scb); 231#endif 232static u_int ahc_find_scb(struct ahc_softc *ahc, struct scb *scb); 233static int ahc_search_qinfifo(struct ahc_softc *ahc, int target, 234 char channel, int lun, u_int tag, 235 u_int32_t status, ahc_search_action action); 236static int ahc_reset_channel(struct ahc_softc *ahc, char channel, 237 int initiate_reset); 238static int ahc_abort_scbs(struct ahc_softc *ahc, int target, 239 char channel, int lun, u_int tag, 240 u_int32_t status); 241static int ahc_search_disc_list(struct ahc_softc *ahc, int target, 242 char channel, int lun, u_int tag); 243static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, 244 u_int prev, u_int scbptr); 245static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc); 246static void ahc_clear_intstat(struct ahc_softc *ahc); 247static void ahc_reset_current_bus(struct ahc_softc *ahc); 248static struct ahc_syncrate * 249 ahc_find_syncrate(struct ahc_softc *ahc, u_int *period, 250 u_int maxsync); 251static u_int ahc_find_period(struct ahc_softc *ahc, u_int scsirate, 252 u_int maxsync); 253static void ahc_validate_offset(struct ahc_softc *ahc, 254 struct ahc_syncrate *syncrate, 255 u_int *offset, int wide); 256static void ahc_set_syncrate(struct ahc_softc *ahc, 257 struct ahc_devinfo *devinfo, 258 struct cam_path *path, 259 struct ahc_syncrate *syncrate, 260 u_int period, u_int offset, u_int type); 261static void ahc_set_width(struct ahc_softc *ahc, 262 struct ahc_devinfo *devinfo, 263 struct cam_path *path, u_int width, u_int type); 264static void ahc_construct_sdtr(struct ahc_softc *ahc, 265 u_int period, u_int offset); 266 267static void ahc_construct_wdtr(struct ahc_softc *ahc, u_int bus_width); 268 269static void ahc_calc_residual(struct scb *scb); 270 271static void ahc_update_pending_syncrates(struct ahc_softc *ahc); 272 273static void ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb); 274 275static timeout_t 276 ahc_timeout; 277static __inline void pause_sequencer(struct ahc_softc *ahc); 278static __inline void unpause_sequencer(struct ahc_softc *ahc, 279 int unpause_always); 280static __inline void restart_sequencer(struct ahc_softc *ahc); 281static __inline u_int ahc_index_busy_tcl(struct ahc_softc *ahc, 282 u_int tcl, int unbusy); 283 284static __inline void ahc_busy_tcl(struct ahc_softc *ahc, struct scb *scb); 285 286static __inline void ahc_freeze_ccb(union ccb* ccb); 287static __inline cam_status ahc_ccb_status(union ccb* ccb); 288static __inline void ahc_set_ccb_status(union ccb* ccb, 289 cam_status status); 290 291static __inline u_int32_t 292ahc_hscb_busaddr(struct ahc_softc *ahc, u_int index) 293{ 294 return (ahc->hscb_busaddr + (sizeof(struct hardware_scb) * index)); 295} 296 297#define AHC_BUSRESET_DELAY 25 /* Reset delay in us */ 298 299static __inline void 300pause_sequencer(struct ahc_softc *ahc) 301{ 302 ahc_outb(ahc, HCNTRL, ahc->pause); 303 304 /* 305 * Since the sequencer can disable pausing in a critical section, we 306 * must loop until it actually stops. 307 */ 308 while ((ahc_inb(ahc, HCNTRL) & PAUSE) == 0) 309 ; 310} 311 312static __inline void 313unpause_sequencer(struct ahc_softc *ahc, int unpause_always) 314{ 315 if ((ahc->flags & AHC_HANDLING_REQINITS) == 0 316 && (unpause_always 317 || (ahc_inb(ahc, INTSTAT) & (SCSIINT | SEQINT | BRKADRINT)) == 0)) 318 ahc_outb(ahc, HCNTRL, ahc->unpause); 319} 320 321/* 322 * Restart the sequencer program from address zero 323 */ 324static __inline void 325restart_sequencer(struct ahc_softc *ahc) 326{ 327 pause_sequencer(ahc); 328 ahc_outb(ahc, SEQCTL, FASTMODE|SEQRESET); 329 unpause_sequencer(ahc, /*unpause_always*/TRUE); 330} 331 332static __inline u_int 333ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl, int unbusy) 334{ 335 u_int scbid; 336 337 scbid = ahc->untagged_scbs[tcl]; 338 if (unbusy) 339 ahc->untagged_scbs[tcl] = SCB_LIST_NULL; 340 341 return (scbid); 342} 343 344static __inline void 345ahc_busy_tcl(struct ahc_softc *ahc, struct scb *scb) 346{ 347 ahc->untagged_scbs[scb->hscb->tcl] = scb->hscb->tag; 348} 349 350static __inline void 351ahc_freeze_ccb(union ccb* ccb) 352{ 353 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) { 354 ccb->ccb_h.status |= CAM_DEV_QFRZN; 355 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1); 356 } 357} 358 359static __inline cam_status 360ahc_ccb_status(union ccb* ccb) 361{ 362 return (ccb->ccb_h.status & CAM_STATUS_MASK); 363} 364 365static __inline void 366ahc_set_ccb_status(union ccb* ccb, cam_status status) 367{ 368 ccb->ccb_h.status &= ~CAM_STATUS_MASK; 369 ccb->ccb_h.status |= status; 370} 371 372char * 373ahc_name(struct ahc_softc *ahc) 374{ 375 static char name[10]; 376 377 sprintf(name, "ahc%d", ahc->unit); 378 return (name); 379} 380 381#ifdef AHC_DEBUG 382static void 383ahc_print_scb(struct scb *scb) 384{ 385 struct hardware_scb *hscb = scb->hscb; 386 387 printf("scb:%p control:0x%x tcl:0x%x cmdlen:%d cmdpointer:0x%lx\n", 388 scb, 389 hscb->control, 390 hscb->tcl, 391 hscb->cmdlen, 392 hscb->cmdpointer ); 393 printf(" datlen:%d data:0x%lx segs:0x%x segp:0x%lx\n", 394 hscb->datalen, 395 hscb->data, 396 hscb->SG_count, 397 hscb->SG_pointer); 398 printf(" sg_addr:%lx sg_len:%ld\n", 399 scb->ahc_dma[0].addr, 400 scb->ahc_dma[0].len); 401 printf(" cdb:%x %x %x %x %x %x %x %x %x %x %x %x\n", 402 hscb->cmdstore[0], hscb->cmdstore[1], hscb->cmdstore[2], 403 hscb->cmdstore[3], hscb->cmdstore[4], hscb->cmdstore[5], 404 hscb->cmdstore[6], hscb->cmdstore[7], hscb->cmdstore[8], 405 hscb->cmdstore[9], hscb->cmdstore[10], hscb->cmdstore[11]); 406} 407#endif 408 409static struct { 410 u_int8_t errno; 411 char *errmesg; 412} hard_error[] = { 413 { ILLHADDR, "Illegal Host Access" }, 414 { ILLSADDR, "Illegal Sequencer Address referrenced" }, 415 { ILLOPCODE, "Illegal Opcode in sequencer program" }, 416 { SQPARERR, "Sequencer Parity Error" }, 417 { DPARERR, "Data-path Parity Error" }, 418 { MPARERR, "Scratch or SCB Memory Parity Error" }, 419 { PCIERRSTAT, "PCI Error detected" }, 420 { CIOPARERR, "CIOBUS Parity Error" }, 421}; 422 423 424/* 425 * Valid SCSIRATE values. (p. 3-17) 426 * Provides a mapping of tranfer periods in ns to the proper value to 427 * stick in the scsiscfr reg to use that transfer rate. 428 */ 429#define AHC_SYNCRATE_ULTRA2 0 430#define AHC_SYNCRATE_ULTRA 2 431#define AHC_SYNCRATE_FAST 5 432static struct ahc_syncrate ahc_syncrates[] = { 433 /* ultra2 fast/ultra period rate */ 434 { 0x13, 0x000, 10, "40.0" }, 435 { 0x14, 0x000, 11, "33.0" }, 436 { 0x15, 0x100, 12, "20.0" }, 437 { 0x16, 0x110, 15, "16.0" }, 438 { 0x17, 0x120, 18, "13.4" }, 439 { 0x18, 0x000, 25, "10.0" }, 440 { 0x19, 0x010, 31, "8.0" }, 441 { 0x1a, 0x020, 37, "6.67" }, 442 { 0x1b, 0x030, 43, "5.7" }, 443 { 0x10, 0x040, 50, "5.0" }, 444 { 0x00, 0x050, 56, "4.4" }, 445 { 0x00, 0x060, 62, "4.0" }, 446 { 0x00, 0x070, 68, "3.6" }, 447 { 0x00, 0x000, 0, NULL } 448}; 449 450/* 451 * Allocate a controller structure for a new device and initialize it. 452 */ 453struct ahc_softc * 454ahc_alloc(int unit, u_int32_t iobase, vm_offset_t maddr, ahc_chip chip, 455 ahc_feature features, ahc_flag flags, struct scb_data *scb_data) 456{ 457 /* 458 * find unit and check we have that many defined 459 */ 460 struct ahc_softc *ahc; 461 size_t alloc_size; 462 463 /* 464 * Allocate a storage area for us 465 */ 466 if (scb_data == NULL) 467 /* 468 * We are not sharing SCB space with another controller 469 * so allocate our own SCB data space. 470 */ 471 alloc_size = sizeof(struct full_ahc_softc); 472 else 473 alloc_size = sizeof(struct ahc_softc); 474 ahc = malloc(alloc_size, M_DEVBUF, M_NOWAIT); 475 if (!ahc) { 476 printf("ahc%d: cannot malloc!\n", unit); 477 return NULL; 478 } 479 bzero(ahc, alloc_size); 480 if (scb_data == NULL) { 481 struct full_ahc_softc* full_softc = (struct full_ahc_softc*)ahc; 482 ahc->scb_data = &full_softc->scb_data_storage; 483 STAILQ_INIT(&ahc->scb_data->free_scbs); 484 } else 485 ahc->scb_data = scb_data; 486 LIST_INIT(&ahc->pending_ccbs); 487 ahc->unit = unit; 488 489 /* 490 * XXX This should be done by the bus specific probe stubs with 491 * the bus layer providing the bsh and tag. Unfortunately, 492 * we need to clean up how we configure things before this 493 * can happen. 494 */ 495 if (maddr != NULL) { 496 ahc->tag = I386_BUS_SPACE_MEM; 497 ahc->bsh = (bus_space_handle_t)maddr; 498 } else { 499 ahc->tag = I386_BUS_SPACE_IO; 500 ahc->bsh = (bus_space_handle_t)iobase; 501 } 502 ahc->chip = chip; 503 ahc->features = features; 504 ahc->flags = flags; 505 ahc->unpause = (ahc_inb(ahc, HCNTRL) & IRQMS) | INTEN; 506 ahc->pause = ahc->unpause | PAUSE; 507 508 return (ahc); 509} 510 511void 512ahc_free(ahc) 513 struct ahc_softc *ahc; 514{ 515 free(ahc, M_DEVBUF); 516 return; 517} 518 519int 520ahc_reset(struct ahc_softc *ahc) 521{ 522 u_int hcntrl; 523 u_int sblkctl; 524 int wait; 525 526 ahc_outb(ahc, HCNTRL, CHIPRST | ahc->pause); 527 /* 528 * Ensure that the reset has finished 529 */ 530 wait = 1000; 531 while (--wait && !(ahc_inb(ahc, HCNTRL) & CHIPRSTACK)) 532 DELAY(1000); 533 if (wait == 0) { 534 printf("%s: WARNING - Failed chip reset! " 535 "Trying to initialize anyway.\n", ahc_name(ahc)); 536 } 537 ahc_outb(ahc, HCNTRL, ahc->pause); 538 539 /* Determine channel configuration */ 540 sblkctl = ahc_inb(ahc, SBLKCTL) & (SELBUSB|SELWIDE); 541 /* No Twin Channel PCI cards */ 542 if ((ahc->chip & AHC_PCI) != 0) 543 sblkctl &= ~SELBUSB; 544 switch (sblkctl) { 545 case 0: 546 /* Single Narrow Channel */ 547 break; 548 case 2: 549 /* Wide Channel */ 550 ahc->features |= AHC_WIDE; 551 break; 552 case 8: 553 /* Twin Channel */ 554 ahc->features |= AHC_TWIN; 555 break; 556 default: 557 printf(" Unsupported adapter type. Ignoring\n"); 558 return(-1); 559 } 560 return (0); 561} 562 563/* 564 * Look up the valid period to SCSIRATE conversion in our table. 565 * Return the period and offset that should be sent to the target 566 * if this was the beginning of an SDTR. 567 */ 568static struct ahc_syncrate * 569ahc_find_syncrate(struct ahc_softc *ahc, u_int *period, u_int maxsync) 570{ 571 struct ahc_syncrate *syncrate; 572 573 syncrate = &ahc_syncrates[maxsync]; 574 while ((syncrate->rate != NULL) 575 && ((ahc->features & AHC_ULTRA2) == 0 576 || (syncrate->sxfr_ultra2 != 0))) { 577 578 if (*period <= syncrate->period) { 579 /* 580 * When responding to a target that requests 581 * sync, the requested rate may fall between 582 * two rates that we can output, but still be 583 * a rate that we can receive. Because of this, 584 * we want to respond to the target with 585 * the same rate that it sent to us even 586 * if the period we use to send data to it 587 * is lower. Only lower the response period 588 * if we must. 589 */ 590 if (syncrate == &ahc_syncrates[maxsync]) { 591 *period = syncrate->period; 592 } 593 break; 594 } 595 syncrate++; 596 } 597 598 if ((*period == 0) 599 || (syncrate->rate == NULL) 600 || ((ahc->features & AHC_ULTRA2) != 0 601 && (syncrate->sxfr_ultra2 == 0))) { 602 /* Use asynchronous transfers. */ 603 *period = 0; 604 syncrate = NULL; 605 } 606 return (syncrate); 607} 608 609static u_int 610ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync) 611{ 612 struct ahc_syncrate *syncrate; 613 614 if ((ahc->features & AHC_ULTRA2) != 0) { 615 scsirate &= SXFR_ULTRA2; 616 } else { 617 scsirate &= SXFR; 618 } 619 620 syncrate = &ahc_syncrates[maxsync]; 621 while (syncrate->rate != NULL) { 622 623 if ((ahc->features & AHC_ULTRA2) != 0) { 624 if (syncrate->sxfr_ultra2 == 0) 625 break; 626 else if (scsirate == syncrate->sxfr_ultra2) 627 return (syncrate->period); 628 } else if (scsirate == (syncrate->sxfr & ~ULTRA_SXFR)) { 629 return (syncrate->period); 630 } 631 syncrate++; 632 } 633 return (0); /* async */ 634} 635 636static void 637ahc_validate_offset(struct ahc_softc *ahc, struct ahc_syncrate *syncrate, 638 u_int *offset, int wide) 639{ 640 u_int maxoffset; 641 642 /* Limit offset to what we can do */ 643 if (syncrate == NULL) { 644 maxoffset = 0; 645 } else if ((ahc->features & AHC_ULTRA2) != 0) { 646 maxoffset = MAX_OFFSET_ULTRA2; 647 } else { 648 if (wide) 649 maxoffset = MAX_OFFSET_16BIT; 650 else 651 maxoffset = MAX_OFFSET_8BIT; 652 } 653 *offset = MIN(*offset, maxoffset); 654} 655 656static void 657ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 658 struct cam_path *path, struct ahc_syncrate *syncrate, 659 u_int period, u_int offset, u_int type) 660{ 661 u_int old_period; 662 u_int old_offset; 663 664 if (syncrate == NULL) { 665 period = 0; 666 offset = 0; 667 } 668 669 old_period = ahc->transinfo[devinfo->target_offset].current.period; 670 old_offset = ahc->transinfo[devinfo->target_offset].current.offset; 671 672 if ((type & AHC_TRANS_CUR) != 0 673 && (old_period != period || old_offset != offset)) { 674 struct ccb_trans_settings neg; 675 u_int scsirate; 676 677 scsirate = ahc->transinfo[devinfo->target_offset].scsirate; 678 if ((ahc->features & AHC_ULTRA2) != 0) { 679 680 scsirate &= ~SXFR_ULTRA2; 681 682 if (syncrate != NULL) { 683 scsirate |= syncrate->sxfr_ultra2; 684 } 685 686 if ((type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE) { 687 ahc_outb(ahc, SCSIOFFSET, offset); 688 } 689 ahc_outb(ahc, TARG_OFFSET + devinfo->target_offset, 690 offset); 691 } else { 692 693 scsirate &= ~(SXFR|SOFS); 694 /* 695 * Ensure Ultra mode is set properly for 696 * this target. 697 */ 698 ahc->ultraenb &= ~devinfo->target_mask; 699 if (syncrate != NULL) { 700 if (syncrate->sxfr & ULTRA_SXFR) { 701 ahc->ultraenb |= devinfo->target_mask; 702 } 703 scsirate |= syncrate->sxfr & SXFR; 704 scsirate |= offset & SOFS; 705 } 706 if ((type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE) { 707 u_int sxfrctl0; 708 709 sxfrctl0 = ahc_inb(ahc, SXFRCTL0); 710 sxfrctl0 &= ~FAST20; 711 if (ahc->ultraenb & devinfo->target_mask) 712 sxfrctl0 |= FAST20; 713 ahc_outb(ahc, SXFRCTL0, sxfrctl0); 714 } 715 } 716 if ((type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE) 717 ahc_outb(ahc, SCSIRATE, scsirate); 718 719 ahc->transinfo[devinfo->target_offset].scsirate = scsirate; 720 ahc->transinfo[devinfo->target_offset].current.period = period; 721 ahc->transinfo[devinfo->target_offset].current.offset = offset; 722 723 /* Update the syncrates in any pending scbs */ 724 ahc_update_pending_syncrates(ahc); 725 726 /* 727 * Tell the SCSI layer about the 728 * new transfer parameters. 729 */ 730 neg.sync_period = period; 731 neg.sync_offset = offset; 732 neg.valid = CCB_TRANS_SYNC_RATE_VALID 733 | CCB_TRANS_SYNC_OFFSET_VALID; 734 xpt_setup_ccb(&neg.ccb_h, path, /*priority*/1); 735 xpt_async(AC_TRANSFER_NEG, path, &neg); 736 if (bootverbose) { 737 if (neg.sync_offset != 0) { 738 printf("%s: target %d synchronous at %sMHz, " 739 "offset = 0x%x\n", ahc_name(ahc), 740 devinfo->target, syncrate->rate, offset); 741 } else { 742 printf("%s: target %d using " 743 "asynchronous transfers\n", 744 ahc_name(ahc), devinfo->target); 745 } 746 } 747 } 748 749 if ((type & AHC_TRANS_GOAL) != 0) { 750 ahc->transinfo[devinfo->target_offset].goal.period = period; 751 ahc->transinfo[devinfo->target_offset].goal.offset = offset; 752 } 753 754 if ((type & AHC_TRANS_USER) != 0) { 755 ahc->transinfo[devinfo->target_offset].user.period = period; 756 ahc->transinfo[devinfo->target_offset].user.offset = offset; 757 } 758} 759 760static void 761ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, 762 struct cam_path *path, u_int width, u_int type) 763{ 764 u_int oldwidth; 765 766 oldwidth = ahc->transinfo[devinfo->target_offset].current.width; 767 768 if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) { 769 struct ccb_trans_settings neg; 770 u_int scsirate; 771 772 scsirate = ahc->transinfo[devinfo->target_offset].scsirate; 773 scsirate &= ~WIDEXFER; 774 if (width == MSG_EXT_WDTR_BUS_16_BIT) 775 scsirate |= WIDEXFER; 776 777 ahc->transinfo[devinfo->target_offset].scsirate = scsirate; 778 779 if ((type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE) 780 ahc_outb(ahc, SCSIRATE, scsirate); 781 782 ahc->transinfo[devinfo->target_offset].current.width = width; 783 784 /* Tell the SCSI layer about the new transfer params */ 785 neg.bus_width = width; 786 neg.valid = CCB_TRANS_BUS_WIDTH_VALID; 787 xpt_setup_ccb(&neg.ccb_h, path, /*priority*/1); 788 xpt_async(AC_TRANSFER_NEG, path, &neg); 789 if (bootverbose) { 790 printf("%s: target %d using %dbit transfers\n", 791 ahc_name(ahc), devinfo->target, 792 8 * (0x01 << neg.bus_width)); 793 } 794 } 795 if ((type & AHC_TRANS_GOAL) != 0) { 796 ahc->transinfo[devinfo->target_offset].goal.width = width; 797 } 798 if ((type & AHC_TRANS_USER) != 0) { 799 ahc->transinfo[devinfo->target_offset].user.width = width; 800 } 801} 802 803/* 804 * Attach all the sub-devices we can find 805 */ 806int 807ahc_attach(struct ahc_softc *ahc) 808{ 809 struct ccb_setasync csa; 810 struct cam_devq *devq; 811 int bus_id; 812 813 /* 814 * Create the device queue for our SIM. 815 */ 816 devq = cam_simq_alloc(ahc->scb_data->maxscbs); 817 if (devq == NULL) 818 return (0); 819 820 /* 821 * Construct our SIM entry 822 */ 823 ahc->sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc, ahc->unit, 824 1, ahc->scb_data->maxscbs, devq); 825 if (ahc->sim == NULL) { 826 cam_simq_free(devq); 827 return (0); 828 } 829 bus_id = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 1 : 0; 830 831 if (xpt_bus_register(ahc->sim, bus_id) != CAM_SUCCESS) { 832 cam_sim_free(ahc->sim, /*free_devq*/TRUE); 833 return (0); 834 } 835 836 if (xpt_create_path(&ahc->path, /*periph*/NULL, 837 cam_sim_path(ahc->sim), CAM_TARGET_WILDCARD, 838 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 839 xpt_bus_deregister(cam_sim_path(ahc->sim)); 840 cam_sim_free(ahc->sim, /*free_devq*/TRUE); 841 return (0); 842 } 843 844 xpt_setup_ccb(&csa.ccb_h, ahc->path, /*priority*/5); 845 csa.ccb_h.func_code = XPT_SASYNC_CB; 846 csa.event_enable = AC_LOST_DEVICE; 847 csa.callback = ahc_async; 848 csa.callback_arg = ahc->sim; 849 xpt_action((union ccb *)&csa); 850 851 if (ahc->features & AHC_TWIN) { 852 ahc->sim_b = cam_sim_alloc(ahc_action, ahc_poll, "ahc", 853 ahc, ahc->unit, 1, 854 ahc->scb_data->maxscbs, devq); 855 856 if (ahc->sim_b == NULL) { 857 printf("ahc_attach: Unable to attach second " 858 "bus due to resource shortage"); 859 /* 860 * Must return success or the first bus 861 * won't get attached either. 862 */ 863 return (1); 864 } 865 866 bus_id = (ahc->flags & AHC_CHANNEL_B_PRIMARY) ? 0 : 1; 867 if (xpt_bus_register(ahc->sim_b, bus_id) != CAM_SUCCESS) { 868 printf("ahc_attach: Unable to attach second " 869 "bus due to resource shortage"); 870 /* 871 * We do not want to destroy the device queue 872 * because the first bus is using it. 873 */ 874 cam_sim_free(ahc->sim_b, /*free_devq*/FALSE); 875 ahc->sim_b = NULL; 876 return (1); 877 } 878 879 if (xpt_create_path(&ahc->path_b, /*periph*/NULL, 880 cam_sim_path(ahc->sim_b), 881 CAM_TARGET_WILDCARD, 882 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 883 xpt_bus_deregister(cam_sim_path(ahc->sim_b)); 884 cam_sim_free(ahc->sim_b, /*free_devq*/FALSE); 885 ahc->sim_b = NULL; 886 return (1); 887 } 888 xpt_setup_ccb(&csa.ccb_h, ahc->path_b, /*priority*/5); 889 csa.ccb_h.func_code = XPT_SASYNC_CB; 890 csa.event_enable = AC_LOST_DEVICE; 891 csa.callback = ahc_async; 892 csa.callback_arg = ahc->sim_b; 893 xpt_action((union ccb *)&csa); 894 } 895 return (1); 896} 897 898static void 899ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) 900{ 901 u_int saved_tcl; 902 903 saved_tcl = ahc_inb(ahc, SAVED_TCL); 904 ahc_compile_devinfo(devinfo, (saved_tcl >> 4) & 0x0f, 905 (saved_tcl & SELBUSB) ? 'B': 'A'); 906} 907 908static void 909ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int target, char channel) 910{ 911 devinfo->target = target; 912 devinfo->target_offset = target; 913 devinfo->channel = channel; 914 if (channel == 'B') 915 devinfo->target_offset += 8; 916 devinfo->target_mask = (0x01 << devinfo->target_offset); 917} 918 919/* 920 * Catch an interrupt from the adapter 921 */ 922void 923ahc_intr(void *arg) 924{ 925 struct ahc_softc *ahc; 926 u_int intstat; 927 928 ahc = (struct ahc_softc *)arg; 929 930 intstat = ahc_inb(ahc, INTSTAT); 931 932 /* 933 * Any interrupts to process? 934 */ 935#if NPCI > 0 936 if ((intstat & INT_PEND) == 0) { 937 if ((ahc->chip & AHC_PCI) != 0 938 && (ahc->unsolicited_ints > 500)) { 939 if ((ahc_inb(ahc, ERROR) & PCIERRSTAT) != 0) 940 ahc_pci_intr(ahc); 941 ahc->unsolicited_ints = 0; 942 } else { 943 ahc->unsolicited_ints++; 944 } 945 return; 946 } else { 947 ahc->unsolicited_ints = 0; 948 } 949#else 950 if ((intstat & INT_PEND) == 0) 951 return; 952#endif 953 954 if (intstat & CMDCMPLT) { 955 struct scb *scb; 956 u_int scb_index; 957 958 ahc_outb(ahc, CLRINT, CLRCMDINT); 959 while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) { 960 scb_index = ahc->qoutfifo[ahc->qoutfifonext]; 961 ahc->qoutfifo[ahc->qoutfifonext++] = SCB_LIST_NULL; 962 963 if (scb_index == TARGET_CMD_CMPLT 964 && (ahc->flags & AHC_TARGETMODE) != 0) { 965 ahc_handle_target_cmd(ahc); 966 continue; 967 } 968 969 scb = ahc->scb_data->scbarray[scb_index]; 970 if (!scb || !(scb->flags & SCB_ACTIVE)) { 971 printf("%s: WARNING no command for scb %d " 972 "(cmdcmplt)\nQOUTPOS = %d\n", 973 ahc_name(ahc), scb_index, 974 ahc->qoutfifonext - 1); 975 continue; 976 } 977 978 /* 979 * Save off the residual 980 * if there is one. 981 */ 982 if (scb->hscb->residual_SG_count != 0) 983 ahc_calc_residual(scb); 984 ahc_done(ahc, scb); 985 } 986 } 987 if (intstat & BRKADRINT) { 988 /* 989 * We upset the sequencer :-( 990 * Lookup the error message 991 */ 992 int i, error, num_errors; 993 994 error = ahc_inb(ahc, ERROR); 995 num_errors = sizeof(hard_error)/sizeof(hard_error[0]); 996 for (i = 0; error != 1 && i < num_errors; i++) 997 error >>= 1; 998 panic("%s: brkadrint, %s at seqaddr = 0x%x\n", 999 ahc_name(ahc), hard_error[i].errmesg, 1000 ahc_inb(ahc, SEQADDR0) | 1001 (ahc_inb(ahc, SEQADDR1) << 8)); 1002 1003 /* Tell everyone that this HBA is no longer availible */ 1004 ahc_abort_scbs(ahc, ALL_TARGETS, ALL_CHANNELS, 1005 ALL_LUNS, SCB_LIST_NULL, CAM_NO_HBA); 1006 } 1007 if (intstat & SEQINT) 1008 ahc_handle_seqint(ahc, intstat); 1009 1010 if (intstat & SCSIINT) 1011 ahc_handle_scsiint(ahc, intstat); 1012} 1013 1014static void 1015ahc_handle_target_cmd(struct ahc_softc *ahc) 1016{ 1017 struct tmode_tstate *tstate; 1018 struct tmode_lstate *lstate; 1019 struct ccb_accept_tio *atio; 1020 struct target_cmd *cmd; 1021 u_int8_t *byte; 1022 int initiator; 1023 int target; 1024 int lun; 1025 1026 cmd = &ahc->targetcmds[ahc->next_targetcmd]; 1027 ahc->next_targetcmd++; 1028 if (ahc->next_targetcmd >= ahc->num_targetcmds) 1029 ahc->next_targetcmd = 0; 1030 1031 initiator = cmd->icl >> 4; 1032 target = cmd->targ_id; 1033 lun = (cmd->identify & MSG_IDENTIFY_LUNMASK); 1034 1035 xpt_print_path(ahc->path); 1036 printf("Received Target Command (%d:%d:%d)\n", 1037 initiator, target, lun); 1038 ahc_dump_targcmd(cmd); 1039 1040 byte = cmd->bytes; 1041 tstate = ahc->enabled_targets[target]; 1042 lstate = NULL; 1043 if (tstate != NULL && lun < 8) 1044 lstate = tstate->enabled_luns[lun]; 1045 1046 /* 1047 * XXX Need to have a default TMODE devce that attaches to luns 1048 * that wouldn't otherwise be enabled and returns the proper 1049 * inquiry information. After all, we don't want to duplicate 1050 * this code in each driver. For now, simply drop it on the 1051 * floor. 1052 */ 1053 if (lstate == NULL) { 1054 printf("Incoming Command on disabled lun\n"); 1055 return; 1056 } 1057 1058 atio = (struct ccb_accept_tio*)SLIST_FIRST(&lstate->accept_tios); 1059 /* XXX Should reconnect and return BUSY status */ 1060 if (atio == NULL) { 1061 printf("No ATIOs for incoming command\n"); 1062 return; 1063 } 1064 1065 /* 1066 * Package it up and send it off to 1067 * whomever has this lun enabled. 1068 */ 1069 atio->init_id = initiator; 1070 if (byte[0] != 0xFF) { 1071 /* Tag was included */ 1072 atio->tag_action = *byte++; 1073 atio->tag_id = *byte++; 1074 atio->ccb_h.flags = CAM_TAG_ACTION_VALID; 1075 } else { 1076 byte++; 1077 atio->ccb_h.flags = 0; 1078 } 1079 1080 /* Okay. Now determine the cdb size based on the command code */ 1081 switch (*byte >> CMD_GROUP_CODE_SHIFT) { 1082 case 0: 1083 atio->cdb_len = 6; 1084 break; 1085 case 1: 1086 case 2: 1087 atio->cdb_len = 10; 1088 break; 1089 case 4: 1090 atio->cdb_len = 16; 1091 break; 1092 case 5: 1093 atio->cdb_len = 12; 1094 break; 1095 case 3: 1096 default: 1097 /* Only copy the opcode. */ 1098 atio->cdb_len = 1; 1099 printf("Reserved or VU command code type encountered\n"); 1100 break; 1101 } 1102 bcopy(byte, atio->cdb_io.cdb_bytes, atio->cdb_len); 1103 1104 SLIST_REMOVE_HEAD(&lstate->accept_tios, sim_links.sle); 1105 atio->ccb_h.status |= CAM_CDB_RECVD; 1106 1107 if ((cmd->identify & MSG_IDENTIFY_DISCFLAG) == 0) { 1108 /* 1109 * We weren't allowed to disconnect. 1110 * We're hanging on the bus until a 1111 * continue target I/O comes in response 1112 * to this accept tio. 1113 */ 1114 xpt_print_path(atio->ccb_h.path); 1115 printf("Incoming Command did not disconnect %p\n", lstate); 1116 ahc->pending_device = lstate; 1117 } 1118 xpt_done((union ccb*)atio); 1119} 1120 1121static void 1122ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat) 1123{ 1124 struct scb *scb; 1125 struct ahc_devinfo devinfo; 1126 1127 ahc_fetch_devinfo(ahc, &devinfo); 1128 1129 /* 1130 * Clear the upper byte that holds SEQINT status 1131 * codes and clear the SEQINT bit. We will unpause 1132 * the sequencer, if appropriate, after servicing 1133 * the request. 1134 */ 1135 ahc_outb(ahc, CLRINT, CLRSEQINT); 1136 switch (intstat & SEQINT_MASK) { 1137 case NO_MATCH: 1138 { 1139 /* Ensure we don't leave the selection hardware on */ 1140 ahc_outb(ahc, SCSISEQ, 1141 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP)); 1142 1143 printf("%s:%c:%d: no active SCB for reconnecting " 1144 "target - issuing BUS DEVICE RESET\n", 1145 ahc_name(ahc), devinfo.channel, devinfo.target); 1146 printf("SAVED_TCL == 0x%x, ARG_1 == 0x%x, SEQ_FLAGS == 0x%x\n", 1147 ahc_inb(ahc, SAVED_TCL), ahc_inb(ahc, ARG_1), 1148 ahc_inb(ahc, SEQ_FLAGS)); 1149 break; 1150 } 1151 case SEND_REJECT: 1152 { 1153 u_int rejbyte = ahc_inb(ahc, ACCUM); 1154 printf("%s:%c:%d: Warning - unknown message received from " 1155 "target (0x%x). Rejecting\n", 1156 ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte); 1157 break; 1158 } 1159 case NO_IDENT: 1160 { 1161 /* 1162 * The reconnecting target either did not send an identify 1163 * message, or did, but we didn't find and SCB to match and 1164 * before it could respond to our ATN/abort, it hit a dataphase. 1165 * The only safe thing to do is to blow it away with a bus 1166 * reset. 1167 */ 1168 int found; 1169 1170 printf("%s:%c:%d: Target did not send an IDENTIFY message. " 1171 "LASTPHASE = 0x%x, SAVED_TCL == 0x%x\n", 1172 ahc_name(ahc), devinfo.channel, devinfo.target, 1173 ahc_inb(ahc, LASTPHASE), ahc_inb(ahc, SAVED_TCL)); 1174 found = ahc_reset_channel(ahc, devinfo.channel, 1175 /*initiate reset*/TRUE); 1176 printf("%s: Issued Channel %c Bus Reset. " 1177 "%d SCBs aborted\n", ahc_name(ahc), devinfo.channel, 1178 found); 1179 break; 1180 } 1181 case BAD_PHASE: 1182 if (ahc_inb(ahc, LASTPHASE) == P_BUSFREE) { 1183 printf("%s:%c:%d: Missed busfree.\n", ahc_name(ahc), 1184 devinfo.channel, devinfo.target); 1185 restart_sequencer(ahc); 1186 return; 1187 } else { 1188 printf("%s:%c:%d: unknown scsi bus phase. Attempting " 1189 "to continue\n", ahc_name(ahc), devinfo.channel, 1190 devinfo.target); 1191 } 1192 break; 1193 case EXTENDED_MSG: 1194 { 1195 ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN; 1196 ahc->msg_len = 0; 1197 ahc->msg_index = 0; 1198 1199 /* 1200 * To actually receive the message, simply turn on 1201 * REQINIT interrupts and let our interrupt handler 1202 * do the rest (REQINIT should already be true). 1203 */ 1204 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENREQINIT); 1205 ahc->flags |= AHC_HANDLING_REQINITS; 1206 return; 1207 } 1208 case REJECT_MSG: 1209 { 1210 /* 1211 * What we care about here is if we had an 1212 * outstanding SDTR or WDTR message for this 1213 * target. If we did, this is a signal that 1214 * the target is refusing negotiation. 1215 */ 1216 u_int scb_index; 1217 u_int last_msg; 1218 1219 scb_index = ahc_inb(ahc, SCB_TAG); 1220 scb = ahc->scb_data->scbarray[scb_index]; 1221 1222 last_msg = ahc_inb(ahc, LAST_MSG); 1223 1224 if ((last_msg == MSG_IDENTIFYFLAG) 1225 && (scb->hscb->control & MSG_SIMPLE_Q_TAG) != 0) { 1226 struct ccb_trans_settings neg; 1227 1228 printf("%s:%c:%d: refuses tagged commands. Performing " 1229 "non-tagged I/O\n", ahc_name(ahc), 1230 devinfo.channel, devinfo.target); 1231 1232 ahc->tagenable &= ~devinfo.target_mask; 1233 neg.flags = 0; 1234 neg.valid = CCB_TRANS_TQ_VALID; 1235 xpt_setup_ccb(&neg.ccb_h, scb->ccb->ccb_h.path, 1236 /*priority*/1); 1237 xpt_async(AC_TRANSFER_NEG, scb->ccb->ccb_h.path, &neg); 1238 /* 1239 * Resend the identify for this CCB as the target 1240 * may believe that the selection is invalid otherwise. 1241 */ 1242 ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) 1243 & ~MSG_SIMPLE_Q_TAG); 1244 scb->hscb->control &= ~MSG_SIMPLE_Q_TAG; 1245 scb->ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID; 1246 ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG); 1247 ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, SCSISIGO) | ATNO); 1248 1249 /* 1250 * Requeue all tagged commands for this target 1251 * currently in our posession so they can be 1252 * converted to untagged commands. 1253 */ 1254 ahc_search_qinfifo(ahc, SCB_TARGET(scb), 1255 SCB_CHANNEL(scb), 1256 SCB_LUN(scb), 1257 /*tag*/SCB_LIST_NULL, 1258 CAM_REQUEUE_REQ, 1259 SEARCH_COMPLETE); 1260 } else if ((last_msg == MSG_IDENTIFYFLAG 1261 || last_msg == HOST_MSG) 1262 && (scb->flags & SCB_MSGOUT_WDTR) != 0) { 1263 struct ahc_target_tinfo *tinfo; 1264 1265 /* note 8bit xfers and clear flag */ 1266 printf("%s:%c:%d: refuses WIDE negotiation. Using " 1267 "8bit transfers\n", ahc_name(ahc), 1268 devinfo.channel, devinfo.target); 1269 scb->flags &= ~SCB_MSGOUT_BITS; 1270 ahc->wdtrpending &= ~devinfo.target_mask; 1271 ahc_set_width(ahc, &devinfo, scb->ccb->ccb_h.path, 1272 MSG_EXT_WDTR_BUS_8_BIT, 1273 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL); 1274 ahc_set_syncrate(ahc, &devinfo, scb->ccb->ccb_h.path, 1275 /*syncrate*/NULL, /*period*/0, 1276 /*offset*/0, AHC_TRANS_ACTIVE); 1277 tinfo = &ahc->transinfo[devinfo.target_offset]; 1278 if (tinfo->goal.period) { 1279 /* Start the sync negotiation */ 1280 ahc->sdtrpending |= devinfo.target_mask; 1281 scb->flags |= SCB_MSGOUT_SDTR; 1282 ahc_outb(ahc, MSG_OUT, HOST_MSG); 1283 ahc_outb(ahc, SCSISIGO, 1284 ahc_inb(ahc, SCSISIGO) | ATNO); 1285 } 1286 } else if ((last_msg == MSG_IDENTIFYFLAG 1287 || last_msg == HOST_MSG) 1288 && (scb->flags & SCB_MSGOUT_SDTR) != 0) { 1289 1290 /* note asynch xfers and clear flag */ 1291 ahc_set_syncrate(ahc, &devinfo, scb->ccb->ccb_h.path, 1292 /*syncrate*/NULL, /*period*/0, 1293 /*offset*/0, 1294 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL); 1295 scb->flags &= ~SCB_MSGOUT_BITS; 1296 ahc->sdtrpending &= ~devinfo.target_mask; 1297 printf("%s:%c:%d: refuses synchronous negotiation. " 1298 "Using asynchronous transfers\n", 1299 ahc_name(ahc), 1300 devinfo.channel, devinfo.target); 1301 } else { 1302 /* 1303 * Otherwise, we ignore it. 1304 */ 1305#ifdef AHC_DEBUG 1306 if (ahc_debug & AHC_SHOWMISC) 1307 printf("%s:%c:%d: Message reject -- ignored\n", 1308 ahc_name(ahc), devinfo.channel, 1309 devinfo.target); 1310#endif 1311 break; 1312 } 1313 break; 1314 } 1315 case BAD_STATUS: 1316 { 1317 u_int scb_index; 1318 struct hardware_scb *hscb; 1319 struct ccb_scsiio *csio; 1320 /* 1321 * The sequencer will notify us when a command 1322 * has an error that would be of interest to 1323 * the kernel. This allows us to leave the sequencer 1324 * running in the common case of command completes 1325 * without error. The sequencer will already have 1326 * dma'd the SCB back up to us, so we can reference 1327 * the in kernel copy directly. 1328 */ 1329 scb_index = ahc_inb(ahc, SCB_TAG); 1330 scb = ahc->scb_data->scbarray[scb_index]; 1331 hscb = scb->hscb; 1332 1333 /* 1334 * Set the default return value to 0 (don't 1335 * send sense). The sense code will change 1336 * this if needed. 1337 */ 1338 ahc_outb(ahc, RETURN_1, 0); 1339 if (!(scb && (scb->flags & SCB_ACTIVE))) { 1340 printf("%s:%c:%d: ahc_intr - referenced scb " 1341 "not valid during seqint 0x%x scb(%d)\n", 1342 ahc_name(ahc), devinfo.channel, 1343 devinfo.target, intstat, scb_index); 1344 goto unpause; 1345 } 1346 1347 /* Don't want to clobber the original sense code */ 1348 if ((scb->flags & SCB_SENSE) != 0) { 1349 /* 1350 * Clear the SCB_SENSE Flag and have 1351 * the sequencer do a normal command 1352 * complete. 1353 */ 1354 scb->flags &= ~SCB_SENSE; 1355 ahc_set_ccb_status(scb->ccb, CAM_AUTOSENSE_FAIL); 1356 break; 1357 } 1358 ahc_set_ccb_status(scb->ccb, CAM_SCSI_STATUS_ERROR); 1359 csio = &scb->ccb->csio; 1360 csio->scsi_status = hscb->status; 1361 switch (hscb->status) { 1362 case SCSI_STATUS_OK: 1363 printf("%s: Interrupted for staus of 0???\n", 1364 ahc_name(ahc)); 1365 break; 1366 case SCSI_STATUS_CMD_TERMINATED: 1367 case SCSI_STATUS_CHECK_COND: 1368#ifdef AHC_DEBUG 1369 if (ahc_debug & AHC_SHOWSENSE) { 1370 xpt_print_path(csio->ccb_h.path); 1371 printf("SCB %d: requests Check Status\n", 1372 scb->hscb->tag); 1373 } 1374#endif 1375 1376 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) { 1377 struct ahc_dma_seg *sg = scb->ahc_dma; 1378 struct scsi_sense *sc = 1379 (struct scsi_sense *)(&hscb->cmdstore); 1380 struct ahc_target_tinfo *tinfo; 1381 1382 /* 1383 * Save off the residual if there is one. 1384 */ 1385 if (hscb->residual_SG_count != 0) 1386 ahc_calc_residual(scb); 1387 1388#ifdef AHC_DEBUG 1389 if (ahc_debug & AHC_SHOWSENSE) { 1390 xpt_print_path(csio->ccb_h.path); 1391 printf("Sending Sense\n"); 1392 } 1393#endif 1394 /* 1395 * bzero from the sense data before having 1396 * the drive fill it. The SCSI spec mandates 1397 * that any untransfered data should be 1398 * assumed to be zero. 1399 */ 1400 bzero(&csio->sense_data, 1401 sizeof(csio->sense_data)); 1402 sc->opcode = REQUEST_SENSE; 1403 sc->byte2 = SCB_LUN(scb) << 5; 1404 sc->unused[0] = 0; 1405 sc->unused[1] = 0; 1406 sc->length = csio->sense_len; 1407 sc->control = 0; 1408 1409 sg->addr = vtophys(&csio->sense_data); 1410 sg->len = csio->sense_len; 1411 1412 /* 1413 * Would be nice to preserve DISCENB here, 1414 * but due to the way we page SCBs, we can't. 1415 */ 1416 hscb->control = 0; 1417 /* 1418 * This request sense could be because the 1419 * the device lost power or in some other 1420 * way has lost our transfer negotiations. 1421 * Renegotiate if appropriate. 1422 */ 1423 ahc_set_width(ahc, &devinfo, 1424 scb->ccb->ccb_h.path, 1425 MSG_EXT_WDTR_BUS_8_BIT, 1426 AHC_TRANS_CUR); 1427 ahc_set_syncrate(ahc, &devinfo, 1428 scb->ccb->ccb_h.path, 1429 /*syncrate*/NULL, /*period*/0, 1430 /*offset*/0, AHC_TRANS_CUR); 1431 scb->flags &= ~SCB_MSGOUT_BITS; 1432 tinfo = &ahc->transinfo[devinfo.target_offset]; 1433 if (tinfo->goal.width) { 1434 ahc->wdtrpending |= devinfo.target_mask; 1435 hscb->control |= MK_MESSAGE; 1436 scb->flags |= SCB_MSGOUT_WDTR; 1437 } else if (tinfo->goal.period) { 1438 ahc->sdtrpending |= devinfo.target_mask; 1439 hscb->control |= MK_MESSAGE; 1440 scb->flags |= SCB_MSGOUT_SDTR; 1441 } 1442 hscb->status = 0; 1443 hscb->SG_count = 1; 1444 hscb->SG_pointer = scb->ahc_dmaphys; 1445 hscb->data = sg->addr; 1446 hscb->datalen = sg->len; 1447 hscb->cmdpointer = hscb->cmdstore_busaddr; 1448 hscb->cmdlen = sizeof(*sc); 1449 scb->sg_count = hscb->SG_count; 1450 scb->flags |= SCB_SENSE; 1451 /* 1452 * Ensure the target is busy since this 1453 * will be an untagged request. 1454 */ 1455 ahc_busy_tcl(ahc, scb); 1456 ahc_outb(ahc, RETURN_1, SEND_SENSE); 1457 1458 /* 1459 * Ensure we have enough time to actually 1460 * retrieve the sense. 1461 */ 1462 untimeout(ahc_timeout, (caddr_t)scb, 1463 scb->ccb->ccb_h.timeout_ch); 1464 scb->ccb->ccb_h.timeout_ch = 1465 timeout(ahc_timeout, (caddr_t)scb, 5 * hz); 1466 /* Freeze the queue while the sense occurs. */ 1467 ahc_freeze_devq(ahc, scb->ccb->ccb_h.path); 1468 ahc_freeze_ccb(scb->ccb); 1469 break; 1470 } 1471 break; 1472 case SCSI_STATUS_BUSY: 1473 case SCSI_STATUS_QUEUE_FULL: 1474 /* 1475 * Requeue any transactions that haven't been 1476 * sent yet. 1477 */ 1478 ahc_freeze_devq(ahc, scb->ccb->ccb_h.path); 1479 ahc_freeze_ccb(scb->ccb); 1480 break; 1481 } 1482 break; 1483 } 1484 case TARGET_SYNC_CMD: 1485 { 1486 /* 1487 * We've already processed the command. If the command 1488 * is still pending, don't unpause the sequencer until 1489 * it returns. 1490 */ 1491 xpt_print_path(ahc->path); 1492 printf("Saw a target sync cmd\n"); 1493 if (ahc->pending_device != NULL) { 1494 printf(" Pending device too.\n"); 1495 return; 1496 } 1497 break; 1498 } 1499 case TARGET_MSG_HELP: 1500 { 1501 /* 1502 * XXX Handle BDR, Abort, Abort Tag, and transfer negotiations. 1503 */ 1504 restart_sequencer(ahc); 1505 return; 1506 } 1507 case AWAITING_MSG: 1508 { 1509 int scb_index; 1510 1511 scb_index = ahc_inb(ahc, SCB_TAG); 1512 scb = ahc->scb_data->scbarray[scb_index]; 1513 1514 /* 1515 * To facilitate adding multiple messages together, 1516 * each routine should increment the index and len 1517 * variables instead of setting them explicitly. 1518 */ 1519 ahc->msg_index = 0; 1520 ahc->msg_len = 0; 1521 1522 /* 1523 * This SCB had MK_MESSAGE set in its control byte or 1524 * we have explicitly set HOST_MSG in MSG_OUT, 1525 * informing the sequencer that we want to send a 1526 * special message to this target. 1527 */ 1528 if ((scb->flags & SCB_DEVICE_RESET) == 0 1529 && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG 1530 && (scb->hscb->control & TAG_ENB) != 0) { 1531 ahc->msg_buf[ahc->msg_index++] = 1532 scb->ccb->csio.tag_action; 1533 ahc->msg_buf[ahc->msg_index++] = 1534 scb->hscb->tag; 1535 ahc->msg_len += 2; 1536 } 1537 1538 if (scb->flags & SCB_DEVICE_RESET) { 1539 ahc->msg_buf[ahc->msg_index++] = MSG_BUS_DEV_RESET; 1540 ahc->msg_len++; 1541 xpt_print_path(scb->ccb->ccb_h.path); 1542 printf("Bus Device Reset Message Sent\n"); 1543 } else if (scb->flags & SCB_ABORT) { 1544 if ((scb->hscb->control & TAG_ENB) != 0) 1545 ahc->msg_buf[ahc->msg_index++] = MSG_ABORT_TAG; 1546 else 1547 ahc->msg_buf[ahc->msg_index++] = MSG_ABORT; 1548 ahc->msg_len++; 1549 xpt_print_path(scb->ccb->ccb_h.path); 1550 printf("Abort Message Sent\n"); 1551 } else if (scb->flags & SCB_MSGOUT_WDTR) { 1552 struct ahc_target_tinfo *tinfo; 1553 1554 tinfo = &ahc->transinfo[devinfo.target_offset]; 1555 ahc_construct_wdtr(ahc, tinfo->goal.width); 1556 } else if (scb->flags & SCB_MSGOUT_SDTR) { 1557 struct ahc_target_tinfo *tinfo; 1558 u_int period; 1559 u_int maxsync; 1560 1561 /* 1562 * Now that the target is actually selected, we 1563 * can further refine our sync rate based on the 1564 * output transceiver mode. 1565 */ 1566 if ((ahc->features & AHC_ULTRA2) != 0) { 1567 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0 1568 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) { 1569 maxsync = AHC_SYNCRATE_ULTRA2; 1570 } else { 1571 maxsync = AHC_SYNCRATE_ULTRA; 1572 } 1573 } else if ((ahc->features & AHC_ULTRA) != 0) { 1574 maxsync = AHC_SYNCRATE_ULTRA; 1575 } else { 1576 maxsync = AHC_SYNCRATE_FAST; 1577 } 1578 tinfo = &ahc->transinfo[devinfo.target_offset]; 1579 period = tinfo->goal.period; 1580 ahc_find_syncrate(ahc, &period, maxsync); 1581 ahc_construct_sdtr(ahc, period, tinfo->goal.offset); 1582 } else { 1583 printf("ahc_intr: AWAITING_MSG for an SCB that " 1584 "does not have a waiting message"); 1585 panic("SCB = %d, SCB Control = %x, MSG_OUT = %x " 1586 "SCB flags = %x", scb_index, scb->hscb->control, 1587 ahc_inb(ahc, MSG_OUT), scb->flags); 1588 } 1589 1590 /* 1591 * Record the fact that we attempted to send a message. 1592 */ 1593 scb->flags |= SCB_MSGOUT_SENT; 1594 1595 /* 1596 * To actually send the message, simply turn on 1597 * REQINIT interrupts and let our interrupt handler 1598 * do the rest (REQINIT should already be true). 1599 */ 1600 ahc->msg_index = 0; 1601 ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT; 1602 ahc->flags |= AHC_HANDLING_REQINITS; 1603 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENREQINIT); 1604 1605 return; 1606 } 1607 case DATA_OVERRUN: 1608 { 1609 /* 1610 * When the sequencer detects an overrun, it 1611 * places the controller in "BITBUCKET" mode 1612 * and allows the target to complete its transfer. 1613 * Unfortunately, none of the counters get updated 1614 * when the controller is in this mode, so we have 1615 * no way of knowing how large the overrun was. 1616 */ 1617 u_int scbindex = ahc_inb(ahc, SCB_TAG); 1618 u_int lastphase = ahc_inb(ahc, LASTPHASE); 1619 int i; 1620 1621 scb = ahc->scb_data->scbarray[scbindex]; 1622 xpt_print_path(scb->ccb->ccb_h.path); 1623 printf("data overrun detected in %s phase." 1624 " Tag == 0x%x.\n", 1625 lastphase == P_DATAIN ? "Data-In" : "Data-Out", 1626 scb->hscb->tag); 1627 xpt_print_path(scb->ccb->ccb_h.path); 1628 printf("%s seen Data Phase. Length = %d. NumSGs = %d.\n", 1629 ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't", 1630 scb->ccb->csio.dxfer_len, scb->sg_count); 1631 for (i = 0; i < scb->sg_count - 1; i++) { 1632 printf("sg[%d] - Addr 0x%x : Length %d\n", 1633 i, 1634 scb->ahc_dma[i].addr, 1635 scb->ahc_dma[i].len); 1636 } 1637 /* 1638 * Set this and it will take affect when the 1639 * target does a command complete. 1640 */ 1641 ahc_freeze_devq(ahc, scb->ccb->ccb_h.path); 1642 ahc_set_ccb_status(scb->ccb, CAM_DATA_RUN_ERR); 1643 ahc_freeze_ccb(scb->ccb); 1644 break; 1645 } 1646 case TRACEPOINT: 1647 { 1648 printf("TRACEPOINT: RETURN_2 = %d\n", ahc_inb(ahc, RETURN_2)); 1649#if 0 1650 printf("SSTAT1 == 0x%x\n", ahc_inb(ahc, SSTAT1)); 1651 printf("SSTAT0 == 0x%x\n", ahc_inb(ahc, SSTAT0)); 1652 printf(", SCSISIGI == 0x%x\n", ahc_inb(ahc, SCSISIGI)); 1653 printf("TRACEPOINT: CCHCNT = %d, SG_COUNT = %d\n", 1654 ahc_inb(ahc, CCHCNT), ahc_inb(ahc, SG_COUNT)); 1655 printf("TRACEPOINT: SCB_TAG = %d\n", ahc_inb(ahc, SCB_TAG)); 1656 printf("TRACEPOINT1: CCHADDR = %d, CCHCNT = %d, SCBPTR = %d\n", 1657 ahc_inb(ahc, CCHADDR) 1658 | (ahc_inb(ahc, CCHADDR+1) << 8) 1659 | (ahc_inb(ahc, CCHADDR+2) << 16) 1660 | (ahc_inb(ahc, CCHADDR+3) << 24), 1661 ahc_inb(ahc, CCHCNT) 1662 | (ahc_inb(ahc, CCHCNT+1) << 8) 1663 | (ahc_inb(ahc, CCHCNT+2) << 16), 1664 ahc_inb(ahc, SCBPTR)); 1665 printf("TRACEPOINT: WAITING_SCBH = %d\n", ahc_inb(ahc, WAITING_SCBH)); 1666 printf("TRACEPOINT: SCB_TAG = %d\n", ahc_inb(ahc, SCB_TAG)); 1667#endif 1668 break; 1669 } 1670#if NOT_YET 1671 /* XXX Fill these in later */ 1672 case MESG_BUFFER_BUSY: 1673 break; 1674 case MSGIN_PHASEMIS: 1675 break; 1676#endif 1677 default: 1678 printf("ahc_intr: seqint, " 1679 "intstat == 0x%x, scsisigi = 0x%x\n", 1680 intstat, ahc_inb(ahc, SCSISIGI)); 1681 break; 1682 } 1683 1684unpause: 1685 /* 1686 * The sequencer is paused immediately on 1687 * a SEQINT, so we should restart it when 1688 * we're done. 1689 */ 1690 unpause_sequencer(ahc, /*unpause_always*/TRUE); 1691} 1692 1693static void 1694ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat) 1695{ 1696 u_int scb_index; 1697 u_int status; 1698 struct scb *scb; 1699 1700 scb_index = ahc_inb(ahc, SCB_TAG); 1701 status = ahc_inb(ahc, SSTAT1); 1702 1703 if (scb_index < ahc->scb_data->numscbs) { 1704 scb = ahc->scb_data->scbarray[scb_index]; 1705 if ((scb->flags & SCB_ACTIVE) == 0) 1706 scb = NULL; 1707 } else 1708 scb = NULL; 1709 1710 if ((status & SCSIRSTI) != 0) { 1711 char channel; 1712 channel = 'A'; 1713 if ((ahc->features & AHC_TWIN) != 0 1714 && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0)) 1715 channel = 'B'; 1716 printf("%s: Someone reset channel %c\n", 1717 ahc_name(ahc), channel); 1718 ahc_reset_channel(ahc, channel, /* Initiate Reset */FALSE); 1719 } else if ((status & BUSFREE) != 0 && (status & SELTO) == 0) { 1720 /* 1721 * First look at what phase we were last in. 1722 * If its message out, chances are pretty good 1723 * that the busfree was in response to one of 1724 * our abort requests. 1725 */ 1726 u_int lastphase = ahc_inb(ahc, LASTPHASE); 1727 u_int saved_tcl = ahc_inb(ahc, SAVED_TCL); 1728 u_int target = (saved_tcl >> 4) & 0x0f; 1729 char channel = saved_tcl & SELBUSB ? 'B': 'A'; 1730 int printerror = 1; 1731 1732 ahc_outb(ahc, SCSISEQ, 1733 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP)); 1734 if (lastphase == P_MESGOUT) { 1735 u_int message; 1736 u_int tag; 1737 1738 message = ahc_inb(ahc, SINDEX); 1739 1740 tag = SCB_LIST_NULL; 1741 switch (message) { 1742 case MSG_ABORT_TAG: 1743 tag = scb->hscb->tag; 1744 /* FALLTRHOUGH */ 1745 case MSG_ABORT: 1746 xpt_print_path(scb->ccb->ccb_h.path); 1747 printf("SCB %d - Abort %s Completed.\n", 1748 scb->hscb->tag, tag == SCB_LIST_NULL ? 1749 "" : "Tag"); 1750 if ((scb->flags & SCB_RECOVERY_SCB) != 0) { 1751 ahc_set_ccb_status(scb->ccb, 1752 CAM_REQ_ABORTED); 1753 ahc_done(ahc, scb); 1754 } 1755 printerror = 0; 1756 break; 1757 case MSG_BUS_DEV_RESET: 1758 ahc_handle_devreset(ahc, target, channel, 1759 CAM_BDR_SENT, AC_SENT_BDR, 1760 "Bus Device Reset", 1761 /*verbose_only*/FALSE); 1762 printerror = 0; 1763 break; 1764 default: 1765 break; 1766 } 1767 } 1768 if (printerror != 0) { 1769 if (scb != NULL) { 1770 u_int tag; 1771 1772 if ((scb->hscb->control & TAG_ENB) != 0) 1773 tag = scb->hscb->tag; 1774 else 1775 tag = SCB_LIST_NULL; 1776 ahc_abort_scbs(ahc, target, channel, 1777 SCB_LUN(scb), tag, 1778 CAM_UNEXP_BUSFREE); 1779 } else { 1780 ahc_abort_scbs(ahc, target, channel, 1781 ALL_LUNS, SCB_LIST_NULL, 1782 CAM_UNEXP_BUSFREE); 1783 printf("%s: ", ahc_name(ahc)); 1784 } 1785 printf("Unexpected busfree. LASTPHASE == 0x%x\n" 1786 "SEQADDR == 0x%x\n", 1787 lastphase, ahc_inb(ahc, SEQADDR0) 1788 | (ahc_inb(ahc, SEQADDR1) << 8)); 1789 } 1790 ahc_outb(ahc, MSG_OUT, MSG_NOOP); 1791 ahc_outb(ahc, SIMODE1, 1792 ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENREQINIT)); 1793 ahc->flags &= ~AHC_HANDLING_REQINITS; 1794 ahc_outb(ahc, CLRSINT1, CLRBUSFREE); 1795 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1796 restart_sequencer(ahc); 1797 } else if ((status & SELTO) != 0) { 1798 u_int scbptr; 1799 u_int nextscb; 1800 1801 scbptr = ahc_inb(ahc, WAITING_SCBH); 1802 ahc_outb(ahc, SCBPTR, scbptr); 1803 scb_index = ahc_inb(ahc, SCB_TAG); 1804 1805 if (scb_index < ahc->scb_data->numscbs) { 1806 scb = ahc->scb_data->scbarray[scb_index]; 1807 if ((scb->flags & SCB_ACTIVE) == 0) 1808 scb = NULL; 1809 } else 1810 scb = NULL; 1811 1812 if (scb == NULL) { 1813 printf("%s: ahc_intr - referenced scb not " 1814 "valid during SELTO scb(%d, %d)\n", 1815 ahc_name(ahc), scbptr, scb_index); 1816 } else { 1817 /* 1818 * Clear any pending messages for the timed out 1819 * target. 1820 */ 1821 ahc_outb(ahc, MSG_OUT, MSG_NOOP); 1822 ahc_handle_devreset(ahc, SCB_TARGET(scb), 1823 SCB_CHANNEL(scb), CAM_SEL_TIMEOUT, 1824 /*ac_code*/0, "Selection Timeout", 1825 /*verbose_only*/TRUE); 1826 } 1827 /* Stop the selection */ 1828 ahc_outb(ahc, SCSISEQ, 0); 1829 1830 ahc_outb(ahc, SIMODE1, 1831 ahc_inb(ahc, SIMODE1) & ~ENREQINIT); 1832 ahc->flags &= ~AHC_HANDLING_REQINITS; 1833 1834 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE); 1835 1836 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1837 1838 restart_sequencer(ahc); 1839 } else if (scb == NULL) { 1840 printf("%s: ahc_intr - referenced scb not " 1841 "valid during scsiint 0x%x scb(%d)\n" 1842 "SIMODE0 = 0x%x, SIMODE1 = 0x%x, SSTAT0 = 0x%x\n" 1843 "SEQADDR = 0x%x\n", ahc_name(ahc), 1844 status, scb_index, ahc_inb(ahc, SIMODE0), 1845 ahc_inb(ahc, SIMODE1), ahc_inb(ahc, SSTAT0), 1846 ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8)); 1847 ahc_outb(ahc, CLRSINT1, status); 1848 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1849 unpause_sequencer(ahc, /*unpause_always*/TRUE); 1850 scb = NULL; 1851 } else if ((status & SCSIPERR) != 0) { 1852 /* 1853 * Determine the bus phase and 1854 * queue an appropriate message 1855 */ 1856 char *phase; 1857 u_int mesg_out = MSG_NOOP; 1858 u_int lastphase = ahc_inb(ahc, LASTPHASE); 1859 1860 xpt_print_path(scb->ccb->ccb_h.path); 1861 1862 switch (lastphase) { 1863 case P_DATAOUT: 1864 phase = "Data-Out"; 1865 break; 1866 case P_DATAIN: 1867 phase = "Data-In"; 1868 mesg_out = MSG_INITIATOR_DET_ERR; 1869 break; 1870 case P_COMMAND: 1871 phase = "Command"; 1872 break; 1873 case P_MESGOUT: 1874 phase = "Message-Out"; 1875 break; 1876 case P_STATUS: 1877 phase = "Status"; 1878 mesg_out = MSG_INITIATOR_DET_ERR; 1879 break; 1880 case P_MESGIN: 1881 phase = "Message-In"; 1882 mesg_out = MSG_PARITY_ERROR; 1883 break; 1884 default: 1885 phase = "unknown"; 1886 break; 1887 } 1888 printf("parity error during %s phase.\n", phase); 1889 1890 printf("SEQADDR == 0x%x\n", ahc_inb(ahc, SEQADDR0) 1891 | (ahc_inb(ahc, SEQADDR1) << 8)); 1892 1893 printf("SCSIRATE == 0x%x\n", ahc_inb(ahc, SCSIRATE)); 1894 1895 /* 1896 * We've set the hardware to assert ATN if we 1897 * get a parity error on "in" phases, so all we 1898 * need to do is stuff the message buffer with 1899 * the appropriate message. "In" phases have set 1900 * mesg_out to something other than MSG_NOP. 1901 */ 1902 if (mesg_out != MSG_NOOP) { 1903 ahc_outb(ahc, MSG_OUT, mesg_out); 1904 } 1905 ahc_outb(ahc, CLRSINT1, CLRSCSIPERR); 1906 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1907 unpause_sequencer(ahc, /*unpause_always*/TRUE); 1908 } else if ((status & REQINIT) != 0 1909 && (ahc->flags & AHC_HANDLING_REQINITS) != 0) { 1910 ahc_handle_reqinit(ahc, scb); 1911 } else { 1912 xpt_print_path(scb->ccb->ccb_h.path); 1913 printf("Unknown SCSIINT. Status = 0x%x\n", status); 1914 ahc_outb(ahc, CLRSINT1, status); 1915 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1916 unpause_sequencer(ahc, /*unpause_always*/TRUE); 1917 } 1918} 1919 1920static void 1921ahc_handle_reqinit(struct ahc_softc *ahc, struct scb *scb) 1922{ 1923 struct ahc_devinfo devinfo; 1924 u_int simode1; 1925 1926 ahc_fetch_devinfo(ahc, &devinfo); 1927 1928 switch (ahc->msg_type) { 1929 case MSG_TYPE_INITIATOR_MSGOUT: 1930 { 1931 int lastbyte; 1932 int phasemis; 1933 u_int bus_phase; 1934 1935 if (ahc->msg_len == 0) 1936 panic("REQINIT interrupt with no active message"); 1937 1938 lastbyte = (ahc->msg_index == ahc->msg_len - 1); 1939 bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK; 1940 phasemis = bus_phase != P_MESGOUT; 1941 1942 if (lastbyte || phasemis) { 1943 /* Time to end our message session */ 1944 ahc->msg_len = 0; 1945 ahc->msg_type = MSG_TYPE_NONE; 1946 simode1 = ahc_inb(ahc, SIMODE1) & ~ENREQINIT; 1947 ahc_outb(ahc, SIMODE1, simode1); 1948 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1949 ahc->flags &= ~AHC_HANDLING_REQINITS; 1950 1951 if (phasemis == 0) { 1952 ahc_outb(ahc, SINDEX, 1953 ahc->msg_buf[ahc->msg_index]); 1954 ahc_outb(ahc, RETURN_1, 0); 1955 } else { 1956 ahc_outb(ahc, RETURN_1, MSGOUT_PHASEMIS); 1957 } 1958 1959 unpause_sequencer(ahc, /* unpause_always */TRUE); 1960 } else { 1961 /* 1962 * Clear our interrupt status and present the byte 1963 * on the bus, but don't unpause the sequencer. 1964 */ 1965 ahc_outb(ahc, CLRSINT1, CLRREQINIT); 1966 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1967 ahc_outb(ahc, SCSIDATL, ahc->msg_buf[ahc->msg_index++]); 1968 } 1969 break; 1970 } 1971 case MSG_TYPE_INITIATOR_MSGIN: 1972 { 1973 int phasemis; 1974 int done; 1975 1976 phasemis = (ahc_inb(ahc, SCSISIGI) & PHASE_MASK) != P_MESGIN; 1977 1978 if (phasemis == 0) { 1979 1980 ahc->msg_len++; 1981 /* Pull the byte in without acking it */ 1982 ahc->msg_buf[ahc->msg_index] = ahc_inb(ahc, SCSIBUSL); 1983 done = ahc_parse_msg(ahc, scb, &devinfo); 1984 /* Ack the byte */ 1985 ahc_outb(ahc, CLRSINT1, CLRREQINIT); 1986 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1987 ahc_inb(ahc, SCSIDATL); 1988 ahc->msg_index++; 1989 } 1990 if (phasemis || done) { 1991 /* Time to end our message session */ 1992 ahc->msg_len = 0; 1993 ahc->msg_type = MSG_TYPE_NONE; 1994 simode1 = ahc_inb(ahc, SIMODE1) & ~ENREQINIT; 1995 ahc->flags &= ~AHC_HANDLING_REQINITS; 1996 ahc_outb(ahc, SIMODE1, simode1); 1997 ahc_outb(ahc, CLRINT, CLRSCSIINT); 1998 unpause_sequencer(ahc, /* unpause_always */TRUE); 1999 } 2000 break; 2001 } 2002 default: 2003 panic("Unknown REQINIT message type"); 2004 } 2005} 2006 2007static int 2008ahc_parse_msg(struct ahc_softc *ahc, struct scb *scb, 2009 struct ahc_devinfo *devinfo) 2010{ 2011 int reject; 2012 int done; 2013 u_int targ_scsirate; 2014 2015 done = FALSE; 2016 reject = FALSE; 2017 targ_scsirate = ahc->transinfo[devinfo->target_offset].scsirate; 2018 /* 2019 * Parse as much of the message as is availible, 2020 * rejecting it if we don't support it. When 2021 * the entire message is availible and has been 2022 * handled, return TRUE indicating that we have 2023 * parsed an entire message. 2024 */ 2025 if (ahc->msg_buf[0] != MSG_EXTENDED) { 2026 reject = TRUE; 2027 } 2028 2029 /* 2030 * Just accept the length byte outright and perform 2031 * more checking once we know the message type. 2032 */ 2033 if (!reject && (ahc->msg_len > 2)) { 2034 switch (ahc->msg_buf[2]) { 2035 case MSG_EXT_SDTR: 2036 { 2037 struct ahc_syncrate *syncrate; 2038 u_int period; 2039 u_int offset; 2040 u_int saved_offset; 2041 u_int maxsync; 2042 2043 if (ahc->msg_buf[1] != MSG_EXT_SDTR_LEN) { 2044 reject = TRUE; 2045 break; 2046 } 2047 2048 /* 2049 * Wait until we have both args before validating 2050 * and acting on this message. 2051 */ 2052 if (ahc->msg_len < (MSG_EXT_SDTR_LEN + /*preamble*/2)) 2053 break; 2054 2055 period = ahc->msg_buf[3]; 2056 saved_offset = offset = ahc->msg_buf[4]; 2057 if ((ahc->features & AHC_ULTRA2) != 0) { 2058 if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0 2059 && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) { 2060 maxsync = AHC_SYNCRATE_ULTRA2; 2061 } else { 2062 maxsync = AHC_SYNCRATE_ULTRA; 2063 } 2064 } else if ((ahc->features & AHC_ULTRA) != 0) { 2065 maxsync = AHC_SYNCRATE_ULTRA; 2066 } else { 2067 maxsync = AHC_SYNCRATE_FAST; 2068 } 2069 syncrate = ahc_find_syncrate(ahc, &period, maxsync); 2070 ahc_validate_offset(ahc, syncrate, &offset, 2071 targ_scsirate & WIDEXFER); 2072 ahc_set_syncrate(ahc, devinfo, scb->ccb->ccb_h.path, 2073 syncrate, period, offset, 2074 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL); 2075 2076 /* 2077 * See if we initiated Sync Negotiation 2078 * and didn't have to fall down to async 2079 * transfers. 2080 */ 2081 if ((scb->flags & (SCB_MSGOUT_SDTR|SCB_MSGOUT_SENT)) 2082 == (SCB_MSGOUT_SDTR|SCB_MSGOUT_SENT)) { 2083 /* We started it */ 2084 if (saved_offset != offset) { 2085 /* Went too low - force async */ 2086 reject = TRUE; 2087 } 2088 scb->flags &= ~SCB_MSGOUT_BITS; 2089 ahc->sdtrpending &= ~devinfo->target_mask; 2090 } else { 2091 /* 2092 * Send our own SDTR in reply 2093 */ 2094 scb->flags &= ~SCB_MSGOUT_BITS; 2095 scb->flags |= SCB_MSGOUT_SDTR; 2096 ahc->sdtrpending |= devinfo->target_mask; 2097 xpt_print_path(scb->ccb->ccb_h.path); 2098 printf("Sending SDTR!!\n"); 2099 ahc_outb(ahc, MSG_OUT, HOST_MSG); 2100 ahc_outb(ahc, SCSISIGO, 2101 ahc_inb(ahc, SCSISIGO) | ATNO); 2102 } 2103 done = TRUE; 2104 break; 2105 } 2106 case MSG_EXT_WDTR: 2107 { 2108 struct ccb_trans_settings neg; 2109 u_int bus_width; 2110 2111 if (ahc->msg_buf[1] != MSG_EXT_WDTR_LEN) { 2112 reject = TRUE; 2113 break; 2114 } 2115 2116 /* 2117 * Wait until we have our arg before validating 2118 * and acting on this message. 2119 */ 2120 if (ahc->msg_len < (MSG_EXT_WDTR_LEN + /*preamble*/2)) 2121 break; 2122 2123 bus_width = ahc->msg_buf[3]; 2124 if ((scb->flags & (SCB_MSGOUT_WDTR|SCB_MSGOUT_SENT)) 2125 == (SCB_MSGOUT_WDTR|SCB_MSGOUT_SENT)) { 2126 /* 2127 * Don't send a WDTR back to the 2128 * target, since we asked first. 2129 */ 2130 switch (bus_width){ 2131 default: 2132 /* 2133 * How can we do anything greater 2134 * than 16bit transfers on a 16bit 2135 * bus? 2136 */ 2137 reject = TRUE; 2138 printf("%s: target %d requested %dBit " 2139 "transfers. Rejecting...\n", 2140 ahc_name(ahc), devinfo->target, 2141 8 * (0x01 << bus_width)); 2142 /* FALLTHROUGH */ 2143 case MSG_EXT_WDTR_BUS_8_BIT: 2144 bus_width = MSG_EXT_WDTR_BUS_8_BIT; 2145 break; 2146 case MSG_EXT_WDTR_BUS_16_BIT: 2147 break; 2148 } 2149 scb->flags &= ~SCB_MSGOUT_WDTR; 2150 ahc->wdtrpending &= ~devinfo->target_mask; 2151 } else { 2152 /* 2153 * Send our own WDTR in reply 2154 */ 2155 printf("Sending WDTR!\n"); 2156 scb->flags &= ~SCB_MSGOUT_BITS; 2157 scb->flags |= SCB_MSGOUT_WDTR; 2158 switch (bus_width) { 2159 default: 2160 if (ahc->features & AHC_WIDE) { 2161 /* Respond Wide */ 2162 bus_width = 2163 MSG_EXT_WDTR_BUS_16_BIT; 2164 break; 2165 } 2166 /* FALLTHROUGH */ 2167 case MSG_EXT_WDTR_BUS_8_BIT: 2168 bus_width = MSG_EXT_WDTR_BUS_8_BIT; 2169 break; 2170 } 2171 ahc_outb(ahc, MSG_OUT, HOST_MSG); 2172 ahc_outb(ahc, SCSISIGO, 2173 ahc_inb(ahc, SCSISIGO) | ATNO); 2174 ahc->wdtrpending |= devinfo->target_mask; 2175 } 2176 ahc_set_width(ahc, devinfo, scb->ccb->ccb_h.path, 2177 bus_width, 2178 AHC_TRANS_ACTIVE|AHC_TRANS_GOAL); 2179 2180 /* After a wide message, we are async */ 2181 ahc_set_syncrate(ahc, devinfo, scb->ccb->ccb_h.path, 2182 /*syncrate*/NULL, /*period*/0, 2183 /*offset*/0, AHC_TRANS_ACTIVE); 2184 if ((ahc->wdtrpending & devinfo->target_mask) == 0 2185 && (reject == 0)) { 2186 struct ahc_target_tinfo *tinfo; 2187 2188 scb->flags &= ~SCB_MSGOUT_WDTR; 2189 tinfo = &ahc->transinfo[devinfo->target_offset]; 2190 if (tinfo->goal.period) { 2191 /* Start the sync negotiation */ 2192 ahc->sdtrpending |= 2193 devinfo->target_mask; 2194 scb->flags |= SCB_MSGOUT_SDTR; 2195 ahc_outb(ahc, MSG_OUT, HOST_MSG); 2196 ahc_outb(ahc, SCSISIGO, 2197 ahc_inb(ahc, SCSISIGO) | ATNO); 2198 } 2199 } 2200 done = TRUE; 2201 break; 2202 } 2203 default: 2204 /* Unknown extended message. Reject it. */ 2205 reject = TRUE; 2206 break; 2207 } 2208 } 2209 2210 if (reject) { 2211 /* 2212 * Assert attention and setup to 2213 * reject the message. 2214 */ 2215 ahc_outb(ahc, MSG_OUT, MSG_MESSAGE_REJECT); 2216 ahc_outb(ahc, SCSISIGO, ahc_inb(ahc, SCSISIGO) | ATNO); 2217 done = TRUE; 2218 } 2219 return (done); 2220} 2221 2222static void 2223ahc_handle_devreset(struct ahc_softc *ahc, int target, char channel, 2224 cam_status status, ac_code acode, char *message, 2225 int verbose_only) 2226{ 2227 struct ahc_devinfo devinfo; 2228 struct cam_path *path; 2229 path_id_t path_id; 2230 u_int16_t targ_mask; 2231 u_int targ_scsirate; 2232 int scratch_offset = target; 2233 int found; 2234 int error; 2235 2236 ahc_compile_devinfo(&devinfo, target, channel); 2237 2238 if (channel == 'B') 2239 path_id = cam_sim_path(ahc->sim_b); 2240 else 2241 path_id = cam_sim_path(ahc->sim); 2242 2243 error = xpt_create_path(&path, /*periph*/NULL, path_id, target, 2244 CAM_LUN_WILDCARD); 2245 /* 2246 * Go back to async/narrow transfers and renegotiate. 2247 */ 2248 if (error == CAM_REQ_CMP) { 2249 ahc_set_width(ahc, &devinfo, path, MSG_EXT_WDTR_BUS_8_BIT, 2250 AHC_TRANS_CUR); 2251 ahc_set_syncrate(ahc, &devinfo, path, /*syncrate*/NULL, 2252 /*period*/0, /*offset*/0, AHC_TRANS_CUR); 2253 } 2254 found = ahc_abort_scbs(ahc, target, channel, ALL_LUNS, 2255 SCB_LIST_NULL, status); 2256 2257 if (error == CAM_REQ_CMP && acode != 0) 2258 xpt_async(AC_SENT_BDR, path, NULL); 2259 2260 if (error == CAM_REQ_CMP) 2261 xpt_free_path(path); 2262 2263 if (message != NULL 2264 && (verbose_only == 0 || bootverbose != 0)) 2265 printf("%s: %s on %c:%d. %d SCBs aborted\n", ahc_name(ahc), 2266 message, channel, target, found); 2267} 2268/* 2269 * We have an scb which has been processed by the 2270 * adaptor, now we look to see how the operation 2271 * went. 2272 */ 2273static void 2274ahc_done(struct ahc_softc *ahc, struct scb *scb) 2275{ 2276 union ccb *ccb; 2277 2278 CAM_DEBUG(scb->ccb->ccb_h.path, CAM_DEBUG_TRACE, 2279 ("ahc_done - scb %d\n", scb->hscb->tag)); 2280 2281 ccb = scb->ccb; 2282 LIST_REMOVE(&ccb->ccb_h, sim_links.le); 2283 2284 untimeout(ahc_timeout, (caddr_t)scb, ccb->ccb_h.timeout_ch); 2285 2286 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 2287 bus_dmasync_op_t op; 2288 2289 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 2290 op = BUS_DMASYNC_POSTREAD; 2291 else 2292 op = BUS_DMASYNC_POSTWRITE; 2293 bus_dmamap_sync(ahc->dmat, scb->dmamap, op); 2294 bus_dmamap_unload(ahc->dmat, scb->dmamap); 2295 } 2296 2297 /* 2298 * Unbusy this target/channel/lun. 2299 * XXX if we are holding two commands per lun, 2300 * send the next command. 2301 */ 2302 ahc_index_busy_tcl(ahc, scb->hscb->tcl, /*unbusy*/TRUE); 2303 2304 if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) { 2305 xpt_print_path(ccb->ccb_h.path); 2306 printf("CONT_TARGET_IO complete\n"); 2307 ccb->ccb_h.status = CAM_REQ_CMP; 2308 ahc_free_scb(ahc, scb); 2309 xpt_done(ccb); 2310 return; 2311 } 2312 2313 /* 2314 * If the recovery SCB completes, we have to be 2315 * out of our timeout. 2316 */ 2317 if ((scb->flags & SCB_RECOVERY_SCB) != 0) { 2318 2319 struct ccb_hdr *ccbh; 2320 2321 /* 2322 * We were able to complete the command successfully, 2323 * so reinstate the timeouts for all other pending 2324 * commands. 2325 */ 2326 ccbh = ahc->pending_ccbs.lh_first; 2327 while (ccbh != NULL) { 2328 struct scb *pending_scb; 2329 2330 pending_scb = (struct scb *)ccbh->ccb_scb_ptr; 2331 ccbh->timeout_ch = 2332 timeout(ahc_timeout, pending_scb, 2333 (ccbh->timeout * hz)/1000); 2334 ccbh = LIST_NEXT(ccbh, sim_links.le); 2335 } 2336 2337 /* 2338 * Ensure that we didn't put a second instance of this 2339 * SCB into the QINFIFO. 2340 */ 2341 ahc_search_qinfifo(ahc, SCB_TARGET(scb), SCB_CHANNEL(scb), 2342 SCB_LUN(scb), scb->hscb->tag, /*status*/0, 2343 SEARCH_REMOVE); 2344 if (ahc_ccb_status(ccb) == CAM_BDR_SENT) 2345 ahc_set_ccb_status(ccb, CAM_CMD_TIMEOUT); 2346 xpt_print_path(ccb->ccb_h.path); 2347 printf("no longer in timeout, status = %x\n", 2348 ccb->ccb_h.status); 2349 } 2350 2351 if ((scb->flags & (SCB_MSGOUT_WDTR|SCB_MSGOUT_SDTR)) != 0) { 2352 /* 2353 * Turn off the pending flags for any DTR messages 2354 * regardless of whether they completed successfully 2355 * or not. This ensures that we don't have lingering 2356 * state after we abort an SCB. 2357 */ 2358 u_int16_t mask; 2359 2360 mask = (0x01 << (SCB_TARGET(scb) 2361 | (SCB_IS_SCSIBUS_B(scb) ? SELBUSB : 0))); 2362 if (scb->flags & SCB_MSGOUT_WDTR) 2363 ahc->wdtrpending &= ~mask; 2364 if (scb->flags & SCB_MSGOUT_SDTR) 2365 ahc->sdtrpending &= ~mask; 2366 } 2367 /* Don't clobber any existing error state */ 2368 if (ahc_ccb_status(ccb) == CAM_REQ_INPROG) { 2369 ccb->ccb_h.status |= CAM_REQ_CMP; 2370 } else if ((scb->flags & SCB_SENSE) != 0) { 2371 /* We performed autosense retrieval */ 2372 scb->ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 2373 } 2374 ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 2375 ahc_free_scb(ahc, scb); 2376 xpt_done(ccb); 2377} 2378 2379/* 2380 * Determine the number of SCBs available on the controller 2381 */ 2382int 2383ahc_probe_scbs(struct ahc_softc *ahc) { 2384 int i; 2385 2386 for (i = 0; i < AHC_SCB_MAX; i++) { 2387 ahc_outb(ahc, SCBPTR, i); 2388 ahc_outb(ahc, SCB_CONTROL, i); 2389 if (ahc_inb(ahc, SCB_CONTROL) != i) 2390 break; 2391 ahc_outb(ahc, SCBPTR, 0); 2392 if (ahc_inb(ahc, SCB_CONTROL) != 0) 2393 break; 2394 } 2395 2396 return (i); 2397} 2398 2399/* 2400 * Start the board, ready for normal operation 2401 */ 2402int 2403ahc_init(struct ahc_softc *ahc) 2404{ 2405 int max_targ = 15; 2406 int i; 2407 int term; 2408 u_int scsi_conf, sxfrctl1; 2409 2410#ifdef AHC_PRINT_SRAM 2411 printf("Scratch Ram:"); 2412 for (i = 0x20; i < 0x5f; i++) { 2413 if (((i % 8) == 0) && (i != 0)) { 2414 printf ("\n "); 2415 } 2416 printf (" 0x%x", ahc_inb(ahc, i)); 2417 } 2418 if ((ahc->features & AHC_MORE_SRAM) != 0) { 2419 for (i = 0x70; i < 0x7f; i++) { 2420 if (((i % 8) == 0) && (i != 0)) { 2421 printf ("\n "); 2422 } 2423 printf (" 0x%x", ahc_inb(ahc, i)); 2424 } 2425 } 2426 printf ("\n"); 2427#endif 2428 2429 /* 2430 * Assume we have a board at this stage and it has been reset. 2431 */ 2432 if ((ahc->flags & AHC_USEDEFAULTS) != 0) { 2433 ahc->our_id = ahc->our_id_b = 7; 2434 } 2435 2436 /* 2437 * XXX Would be better to use a per device flag, but PCI and EISA 2438 * devices don't have them yet. 2439 */ 2440 if ((AHC_TMODE_ENABLE & (0x01 << ahc->unit)) != 0) 2441 ahc->flags |= AHC_TARGETMODE; 2442 2443 if ((ahc->features & AHC_TWIN) != 0) { 2444 printf("Twin Channel, A SCSI Id=%d, B SCSI Id=%d, ", 2445 ahc->our_id, ahc->our_id_b); 2446 } else { 2447 if ((ahc->features & AHC_WIDE) != 0) { 2448 printf("Wide "); 2449 } else { 2450 printf("Single "); 2451 } 2452 printf("Channel %c, SCSI Id=%d, ", ahc->channel, ahc->our_id); 2453 } 2454 2455 ahc_outb(ahc, SEQ_FLAGS, 0); 2456 2457 /* Determine the number of SCBs and initialize them */ 2458 2459 if (ahc->scb_data->maxhscbs == 0) { 2460 ahc->scb_data->maxhscbs = ahc_probe_scbs(ahc); 2461 /* SCB 0 heads the free list */ 2462 ahc_outb(ahc, FREE_SCBH, 0); 2463 for (i = 0; i < ahc->scb_data->maxhscbs; i++) { 2464 ahc_outb(ahc, SCBPTR, i); 2465 2466 /* Clear the control byte. */ 2467 ahc_outb(ahc, SCB_CONTROL, 0); 2468 2469 /* Set the next pointer */ 2470 ahc_outb(ahc, SCB_NEXT, i+1); 2471 2472 /* Make the tag number invalid */ 2473 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL); 2474 } 2475 2476 /* Make that the last SCB terminates the free list */ 2477 ahc_outb(ahc, SCBPTR, i-1); 2478 ahc_outb(ahc, SCB_NEXT, SCB_LIST_NULL); 2479 2480 /* Ensure we clear the 0 SCB's control byte. */ 2481 ahc_outb(ahc, SCBPTR, 0); 2482 ahc_outb(ahc, SCB_CONTROL, 0); 2483 2484 ahc->scb_data->maxhscbs = i; 2485 } 2486 2487 if (ahc->scb_data->maxhscbs == 0) 2488 panic("%s: No SCB space found", ahc_name(ahc)); 2489 2490 if (ahc->scb_data->maxhscbs < AHC_SCB_MAX) { 2491 ahc->flags |= AHC_PAGESCBS; 2492 ahc->scb_data->maxscbs = AHC_SCB_MAX; 2493 if ((ahc->flags & AHC_TARGETMODE) != 0) { 2494 /* Steal one slot for TMODE commands */ 2495 ahc->scb_data->maxscbs--; 2496 } 2497 printf("%d/%d SCBs\n", ahc->scb_data->maxhscbs, 2498 ahc->scb_data->maxscbs); 2499 } else { 2500 ahc->scb_data->maxscbs = ahc->scb_data->maxhscbs; 2501 if ((ahc->flags & AHC_TARGETMODE) != 0) { 2502 /* Steal one slot for TMODE commands */ 2503 ahc->scb_data->maxscbs--; 2504 } 2505 ahc->flags &= ~AHC_PAGESCBS; 2506 printf("%d SCBs\n", ahc->scb_data->maxhscbs); 2507 } 2508 2509#ifdef AHC_DEBUG 2510 if (ahc_debug & AHC_SHOWMISC) { 2511 printf("%s: hardware scb %d bytes; kernel scb %d bytes; " 2512 "ahc_dma %d bytes\n", 2513 ahc_name(ahc), 2514 sizeof(struct hardware_scb), 2515 sizeof(struct scb), 2516 sizeof(struct ahc_dma_seg)); 2517 } 2518#endif /* AHC_DEBUG */ 2519 2520 /* Set the SCSI Id, SXFRCTL0, SXFRCTL1, and SIMODE1, for both channels*/ 2521 if (ahc->features & AHC_TWIN) { 2522 2523 /* 2524 * The device is gated to channel B after a chip reset, 2525 * so set those values first 2526 */ 2527 term = (ahc->flags & AHC_TERM_ENB_B) != 0 ? STPWEN : 0; 2528 if ((ahc->features & AHC_ULTRA2) != 0) 2529 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id_b); 2530 else 2531 ahc_outb(ahc, SCSIID, ahc->our_id_b); 2532 scsi_conf = ahc_inb(ahc, SCSICONF + 1); 2533 sxfrctl1 = ahc_inb(ahc, SXFRCTL1); 2534 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL)) 2535 |term 2536 |ENSTIMER|ACTNEGEN); 2537 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR); 2538 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN); 2539 2540 if (scsi_conf & RESET_SCSI) { 2541 /* Reset the bus */ 2542 if (bootverbose) 2543 printf("%s: Resetting Channel B\n", 2544 ahc_name(ahc)); 2545 ahc_reset_current_bus(ahc); 2546 } 2547 2548 /* Select Channel A */ 2549 ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~SELBUSB); 2550 } 2551 term = (ahc->flags & AHC_TERM_ENB_A) != 0 ? STPWEN : 0; 2552 if ((ahc->features & AHC_ULTRA2) != 0) 2553 ahc_outb(ahc, SCSIID_ULTRA2, ahc->our_id); 2554 else 2555 ahc_outb(ahc, SCSIID, ahc->our_id); 2556 scsi_conf = ahc_inb(ahc, SCSICONF); 2557 sxfrctl1 = ahc_inb(ahc, SXFRCTL1); 2558 ahc_outb(ahc, SXFRCTL1, (scsi_conf & (ENSPCHK|STIMESEL)) 2559 |term 2560 |ENSTIMER|ACTNEGEN); 2561 ahc_outb(ahc, SIMODE1, ENSELTIMO|ENSCSIRST|ENSCSIPERR); 2562 ahc_outb(ahc, SXFRCTL0, DFON|SPIOEN); 2563 2564 if ((ahc->features & AHC_ULTRA2) != 0) { 2565 /* Wait for our transceiver status to settle */ 2566 i = 1000000; 2567 while (--i && ((ahc_inb(ahc, SBLKCTL) & (ENAB40|ENAB20)) == 0)) 2568 DELAY(100); 2569 2570 if (i == 0) 2571 panic("%s: Transceiver state never settled\n", 2572 ahc_name(ahc)); 2573 } 2574 2575 if (scsi_conf & RESET_SCSI) { 2576 /* Reset the bus */ 2577 if (bootverbose) 2578 printf("%s: Resetting Channel %c\n", ahc_name(ahc), 2579 ahc->channel); 2580 2581 ahc_reset_current_bus(ahc); 2582 } 2583 2584 /* 2585 * Look at the information that board initialization or 2586 * the board bios has left us. In the lower four bits of each 2587 * target's scratch space any value other than 0 indicates 2588 * that we should initiate synchronous transfers. If it's zero, 2589 * the user or the BIOS has decided to disable synchronous 2590 * negotiation to that target so we don't activate the needsdtr 2591 * flag. 2592 */ 2593 ahc->ultraenb = 0; 2594 ahc->tagenable = ALL_TARGETS; 2595 2596 /* Grab the disconnection disable table and invert it for our needs */ 2597 if (ahc->flags & AHC_USEDEFAULTS) { 2598 printf("%s: Host Adapter Bios disabled. Using default SCSI " 2599 "device parameters\n", ahc_name(ahc)); 2600 ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B; 2601 ahc->discenable = ALL_TARGETS; 2602 if ((ahc->features & AHC_ULTRA) != 0) 2603 ahc->ultraenb = 0xffff; 2604 } else { 2605 ahc->discenable = ~((ahc_inb(ahc, DISC_DSB + 1) << 8) 2606 | ahc_inb(ahc, DISC_DSB)); 2607 if ((ahc->features & (AHC_ULTRA|AHC_ULTRA2)) != 0) 2608 ahc->ultraenb = (ahc_inb(ahc, ULTRA_ENB + 1) << 8) 2609 | ahc_inb(ahc, ULTRA_ENB); 2610 } 2611 2612 if ((ahc->features & (AHC_WIDE|AHC_TWIN)) == 0) 2613 max_targ = 7; 2614 2615 for (i = 0; i <= max_targ; i++) { 2616 struct ahc_target_tinfo *transinfo; 2617 2618 transinfo = &ahc->transinfo[i]; 2619 /* Default to async narrow across the board */ 2620 bzero(transinfo, sizeof(*transinfo)); 2621 if (ahc->flags & AHC_USEDEFAULTS) { 2622 if ((ahc->features & AHC_WIDE) != 0) 2623 transinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT; 2624 2625 /* 2626 * These will be truncated when we determine the 2627 * connection type we have with the target. 2628 */ 2629 transinfo->user.period = ahc_syncrates->period; 2630 transinfo->user.offset = ~0; 2631 } else { 2632 u_int scsirate; 2633 u_int16_t mask; 2634 2635 /* Take the settings leftover in scratch RAM. */ 2636 scsirate = ahc_inb(ahc, TARG_SCSIRATE + i); 2637 mask = (0x01 << i); 2638 if ((ahc->features & AHC_ULTRA2) != 0) { 2639 u_int offset; 2640 2641 if ((scsirate & SOFS) == 0x0F) { 2642 /* 2643 * Haven't negotiated yet, 2644 * so the format is different. 2645 */ 2646 scsirate = (scsirate & SXFR) >> 4 2647 | (ahc->ultraenb & mask) 2648 ? 0x18 : 0x10 2649 | (scsirate & WIDEXFER); 2650 offset = MAX_OFFSET_ULTRA2; 2651 } else 2652 offset = ahc_inb(ahc, TARG_OFFSET + i); 2653 ahc_find_period(ahc, scsirate, 2654 AHC_SYNCRATE_ULTRA2); 2655 if (offset == 0) 2656 transinfo->user.period = 0; 2657 else 2658 transinfo->user.offset = ~0; 2659 } else if ((scsirate & SOFS) != 0) { 2660 transinfo->user.period = 2661 ahc_find_period(ahc, scsirate, 2662 (ahc->ultraenb & mask) 2663 ? AHC_SYNCRATE_ULTRA 2664 : AHC_SYNCRATE_FAST); 2665 if ((scsirate & SOFS) != 0 2666 && transinfo->user.period != 0) { 2667 transinfo->user.offset = ~0; 2668 } 2669 } 2670 if ((scsirate & WIDEXFER) != 0 2671 && (ahc->features & AHC_WIDE) != 0) { 2672 transinfo->user.width = MSG_EXT_WDTR_BUS_16_BIT; 2673 } 2674 2675 } 2676 } 2677 ahc->sdtrpending = 0; 2678 ahc->wdtrpending = 0; 2679 2680#ifdef AHC_DEBUG 2681 if (ahc_debug & AHC_SHOWMISC) 2682 printf("NEEDSDTR == 0x%x\nNEEDWDTR == 0x%x\n" 2683 "DISCENABLE == 0x%x\nULTRAENB == 0x%x\n", 2684 ahc->needsdtr_orig, ahc->needwdtr_orig, 2685 ahc->discenable, ahc->ultraenb); 2686#endif 2687 /* 2688 * Allocate enough "hardware scbs" to handle 2689 * the maximum number of concurrent transactions 2690 * we can have active. We have to use contigmalloc 2691 * if this array crosses a page boundary since the 2692 * sequencer depends on this array being physically 2693 * contiguous. 2694 */ 2695 if (ahc->scb_data->hscbs == NULL) { 2696 size_t array_size; 2697 2698 array_size = ahc->scb_data->maxscbs*sizeof(struct hardware_scb); 2699 if (array_size > PAGE_SIZE) { 2700 ahc->scb_data->hscbs = (struct hardware_scb *) 2701 contigmalloc(array_size, M_DEVBUF, 2702 M_NOWAIT, 0ul, 0xffffffff, 2703 PAGE_SIZE, 0x10000); 2704 } else { 2705 ahc->scb_data->hscbs = (struct hardware_scb *) 2706 malloc(array_size, M_DEVBUF, M_NOWAIT); 2707 } 2708 2709 if (ahc->scb_data->hscbs == NULL) { 2710 printf("%s: unable to allocate hardware SCB array. " 2711 "Failing attach\n", ahc_name(ahc)); 2712 return (-1); 2713 } 2714 /* At least the control byte of each hscb needs to be zeroed */ 2715 bzero(ahc->scb_data->hscbs, array_size); 2716 } 2717 2718 if ((ahc->flags & AHC_TARGETMODE) != 0) { 2719 size_t array_size; 2720 2721 ahc->num_targetcmds = 32; 2722 array_size = ahc->num_targetcmds * sizeof(struct target_cmd); 2723 ahc->targetcmds = malloc(array_size, M_DEVBUF, M_NOWAIT); 2724 2725 if (ahc->targetcmds == NULL) { 2726 printf("%s: unable to allocate targetcmd array. " 2727 "Failing attach\n", ahc_name(ahc)); 2728 return (-1); 2729 } 2730 2731 bzero(ahc->targetcmds, array_size); 2732 ahc_outb(ahc, TMODE_CMDADDR_NEXT, 0); 2733 } 2734 2735 /* 2736 * Tell the sequencer where it can find the our arrays in memory. 2737 */ 2738 { 2739 u_int32_t physaddr; 2740 2741 /* Tell the sequencer where it can find the hscb array. */ 2742 physaddr = vtophys(ahc->scb_data->hscbs); 2743 ahc_outb(ahc, HSCB_ADDR, physaddr & 0xFF); 2744 ahc_outb(ahc, HSCB_ADDR + 1, (physaddr >> 8) & 0xFF); 2745 ahc_outb(ahc, HSCB_ADDR + 2, (physaddr >> 16) & 0xFF); 2746 ahc_outb(ahc, HSCB_ADDR + 3, (physaddr >> 24) & 0xFF); 2747 ahc->hscb_busaddr = physaddr; 2748 2749 physaddr = vtophys(ahc->qoutfifo); 2750 ahc_outb(ahc, SCBID_ADDR, physaddr & 0xFF); 2751 ahc_outb(ahc, SCBID_ADDR + 1, (physaddr >> 8) & 0xFF); 2752 ahc_outb(ahc, SCBID_ADDR + 2, (physaddr >> 16) & 0xFF); 2753 ahc_outb(ahc, SCBID_ADDR + 3, (physaddr >> 24) & 0xFF); 2754 2755 if ((ahc->flags & AHC_TARGETMODE) != 0) { 2756 physaddr = vtophys(ahc->targetcmds); 2757 ahc_outb(ahc, TMODE_CMDADDR, physaddr & 0xFF); 2758 ahc_outb(ahc, TMODE_CMDADDR + 1, 2759 (physaddr >> 8) & 0xFF); 2760 ahc_outb(ahc, TMODE_CMDADDR + 2, 2761 (physaddr >> 16) & 0xFF); 2762 ahc_outb(ahc, TMODE_CMDADDR + 3, 2763 (physaddr >> 24) & 0xFF); 2764 2765 ahc_outb(ahc, CMDSIZE_TABLE, 5); 2766 ahc_outb(ahc, CMDSIZE_TABLE + 1, 9); 2767 ahc_outb(ahc, CMDSIZE_TABLE + 2, 9); 2768 ahc_outb(ahc, CMDSIZE_TABLE + 3, 0); 2769 ahc_outb(ahc, CMDSIZE_TABLE + 4, 15); 2770 ahc_outb(ahc, CMDSIZE_TABLE + 5, 11); 2771 ahc_outb(ahc, CMDSIZE_TABLE + 6, 0); 2772 ahc_outb(ahc, CMDSIZE_TABLE + 7, 0); 2773 } 2774 2775 /* There are no untagged SCBs active yet. */ 2776 for (i = 0; i < sizeof(ahc->untagged_scbs); i++) { 2777 ahc->untagged_scbs[i] = SCB_LIST_NULL; 2778 } 2779 for (i = 0; i < sizeof(ahc->qoutfifo); i++) { 2780 ahc->qoutfifo[i] = SCB_LIST_NULL; 2781 } 2782 } 2783 2784 /* Our Q FIFOs are empty. */ 2785 ahc_outb(ahc, KERNEL_QINPOS, 0); 2786 ahc_outb(ahc, QINPOS, 0); 2787 ahc_outb(ahc, QOUTPOS, 0); 2788 2789 /* 2790 * Use the built in queue management registers 2791 * if they are available. 2792 */ 2793 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 2794 ahc_outb(ahc, QOFF_CTLSTA, SCB_QSIZE_256); 2795 ahc_outb(ahc, SDSCB_QOFF, 0); 2796 ahc_outb(ahc, SNSCB_QOFF, 0); 2797 ahc_outb(ahc, HNSCB_QOFF, 0); 2798 } 2799 2800 2801 /* We don't have any waiting selections */ 2802 ahc_outb(ahc, WAITING_SCBH, SCB_LIST_NULL); 2803 2804 /* Our disconnection list is empty too */ 2805 ahc_outb(ahc, DISCONNECTED_SCBH, SCB_LIST_NULL); 2806 2807 /* Message out buffer starts empty */ 2808 ahc_outb(ahc, MSG_OUT, MSG_NOOP); 2809 2810 /* 2811 * Load the Sequencer program and Enable the adapter 2812 * in "fast" mode. 2813 */ 2814 if (bootverbose) 2815 printf("%s: Downloading Sequencer Program...", 2816 ahc_name(ahc)); 2817 2818 ahc_loadseq(ahc); 2819 2820 /* We have to wait until after any system dumps... */ 2821 at_shutdown(ahc_shutdown, ahc, SHUTDOWN_FINAL); 2822 2823 return (0); 2824} 2825 2826static void 2827ahcminphys(struct buf *bp) 2828{ 2829/* 2830 * Even though the card can transfer up to 16megs per command 2831 * we are limited by the number of segments in the dma segment 2832 * list that we can hold. The worst case is that all pages are 2833 * discontinuous physically, hense the "page per segment" limit 2834 * enforced here. 2835 */ 2836 if (bp->b_bcount > ((AHC_NSEG - 1) * PAGE_SIZE)) { 2837 bp->b_bcount = ((AHC_NSEG - 1) * PAGE_SIZE); 2838 } 2839} 2840 2841static cam_status 2842ahc_find_tmode_devs(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb, 2843 struct tmode_tstate **tstate, struct tmode_lstate **lstate, 2844 int notfound_failure) 2845{ 2846 int our_id; 2847 2848 /* 2849 * If we are not configured for target mode, someone 2850 * is really confused to be sending this to us. 2851 */ 2852 if ((ahc->flags & AHC_TARGETMODE) == 0) 2853 return (CAM_REQ_INVALID); 2854 2855 /* Range check target and lun */ 2856 if (cam_sim_bus(sim) == 0) 2857 our_id = ahc->our_id; 2858 else 2859 our_id = ahc->our_id_b; 2860 if (ccb->ccb_h.target_id > ((ahc->features & AHC_WIDE) ? 15 : 7) 2861 || ((ahc->features & AHC_MULTI_TID) == 0 2862 && (ccb->ccb_h.target_id != our_id))) 2863 return (CAM_TID_INVALID); 2864 2865 if (ccb->ccb_h.target_lun > 8) 2866 return (CAM_LUN_INVALID); 2867 2868 *tstate = ahc->enabled_targets[ccb->ccb_h.target_id]; 2869 *lstate = NULL; 2870 if (*tstate != NULL) 2871 *lstate = (*tstate)->enabled_luns[ccb->ccb_h.target_lun]; 2872 2873 if (notfound_failure != 0 && *lstate == NULL) 2874 return (CAM_PATH_INVALID); 2875 2876 return (CAM_REQ_CMP); 2877} 2878 2879static void 2880ahc_action(struct cam_sim *sim, union ccb *ccb) 2881{ 2882 struct ahc_softc *ahc; 2883 struct tmode_lstate *lstate; 2884 int target_id; 2885 int s; 2886 2887 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n")); 2888 2889 ahc = (struct ahc_softc *)cam_sim_softc(sim); 2890 2891 target_id = ccb->ccb_h.target_id; 2892 2893 switch (ccb->ccb_h.func_code) { 2894 /* Common cases first */ 2895 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */ 2896 case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/ 2897 { 2898 struct tmode_tstate *tstate; 2899 cam_status status; 2900 2901 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, 2902 &lstate, TRUE); 2903 2904 if (status != CAM_REQ_CMP) { 2905 ccb->ccb_h.status = status; 2906 xpt_done(ccb); 2907 break; 2908 } 2909 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) { 2910 SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h, 2911 sim_links.sle); 2912 ccb->ccb_h.status = CAM_REQ_INPROG; 2913 break; 2914 } 2915 2916 /* 2917 * The target_id represents the target we attempt to 2918 * select. In target mode, this is the initiator of 2919 * the original command. 2920 */ 2921 target_id = ccb->csio.init_id; 2922 xpt_print_path(ccb->ccb_h.path); 2923 printf("Sending a continue TIO\n"); 2924 /* FALLTHROUGH */ 2925 } 2926 case XPT_SCSI_IO: /* Execute the requested I/O operation */ 2927 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */ 2928 { 2929 struct scb *scb; 2930 struct hardware_scb *hscb; 2931 struct ahc_target_tinfo *tinfo; 2932 u_int16_t mask; 2933 2934 /* 2935 * get an scb to use. 2936 */ 2937 if ((scb = ahc_get_scb(ahc)) == NULL) { 2938 int s; 2939 2940 s = splcam(); 2941 ahc->flags |= AHC_RESOURCE_SHORTAGE; 2942 splx(s); 2943 xpt_freeze_simq(ahc->sim, /*count*/1); 2944 ahc_set_ccb_status(ccb, CAM_REQUEUE_REQ); 2945 xpt_done(ccb); 2946 return; 2947 } 2948 2949 hscb = scb->hscb; 2950 2951 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, 2952 ("start scb(%p)\n", scb)); 2953 scb->ccb = ccb; 2954 /* 2955 * So we can find the SCB when an abort is requested 2956 */ 2957 ccb->ccb_h.ccb_scb_ptr = scb; 2958 ccb->ccb_h.ccb_ahc_ptr = ahc; 2959 2960 /* 2961 * Put all the arguments for the xfer in the scb 2962 */ 2963 hscb->tcl = ((target_id << 4) & 0xF0) 2964 | (SIM_IS_SCSIBUS_B(ahc, sim) ? SELBUSB : 0) 2965 | (ccb->ccb_h.target_lun & 0x07); 2966 2967 mask = SCB_TARGET_MASK(scb); 2968 tinfo = &ahc->transinfo[SCB_TARGET_OFFSET(scb)]; 2969 2970 hscb->scsirate = tinfo->scsirate; 2971 hscb->scsioffset = tinfo->current.offset; 2972 if ((ahc->ultraenb & mask) != 0) 2973 hscb->control |= ULTRAENB; 2974 2975 if ((ahc->discenable & mask) != 0 2976 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0) 2977 hscb->control |= DISCENB; 2978 2979 if (ccb->ccb_h.func_code == XPT_RESET_DEV) { 2980 hscb->cmdpointer = NULL; 2981 scb->flags |= SCB_DEVICE_RESET; 2982 hscb->control |= MK_MESSAGE; 2983 ahc_execute_scb(scb, NULL, 0, 0); 2984 } else { 2985 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2986 if (tinfo->current.width != tinfo->goal.width) { 2987 if ((ahc->wdtrpending & mask) == 0) { 2988 ahc->wdtrpending |= mask; 2989 hscb->control |= MK_MESSAGE; 2990 scb->flags |= SCB_MSGOUT_WDTR; 2991 } 2992 } else if ((tinfo->current.period 2993 != tinfo->goal.period) 2994 && (ahc->sdtrpending & mask) == 0) { 2995 ahc->sdtrpending |= mask; 2996 hscb->control |= MK_MESSAGE; 2997 scb->flags |= SCB_MSGOUT_SDTR; 2998 } 2999 } else { 3000 if (ahc->pending_device == lstate) { 3001 scb->flags |= SCB_TARGET_IMMEDIATE; 3002 ahc->pending_device = NULL; 3003 } 3004 hscb->control |= TARGET_SCB; 3005 hscb->cmdpointer = IDENTIFY_SEEN; 3006 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) { 3007 hscb->cmdpointer |= SPHASE_PENDING; 3008 hscb->status = ccb->csio.scsi_status; 3009 } 3010 3011 /* Overloaded with tag ID */ 3012 hscb->cmdlen = ccb->csio.tag_id; 3013 /* 3014 * Overloaded with our target ID to 3015 * use for reselection. 3016 */ 3017 hscb->next = ccb->ccb_h.target_id; 3018 } 3019 if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) 3020 hscb->control |= ccb->csio.tag_action; 3021 3022 ahc_setup_data(ahc, &ccb->csio, scb); 3023 } 3024 break; 3025 } 3026 case XPT_NOTIFY_ACK: 3027 case XPT_IMMED_NOTIFY: 3028 { 3029 struct tmode_tstate *tstate; 3030 struct tmode_lstate *lstate; 3031 cam_status status; 3032 3033 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, 3034 &lstate, TRUE); 3035 3036 if (status != CAM_REQ_CMP) { 3037 ccb->ccb_h.status = status; 3038 xpt_done(ccb); 3039 break; 3040 } 3041 if (ccb->ccb_h.func_code == XPT_NOTIFY_ACK) { 3042 /* Clear notification state */ 3043 } 3044 SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h, 3045 sim_links.sle); 3046 ccb->ccb_h.status = CAM_REQ_INPROG; 3047 break; 3048 } 3049 case XPT_EN_LUN: /* Enable LUN as a target */ 3050 { 3051 struct tmode_tstate *tstate; 3052 struct tmode_lstate *lstate; 3053 struct ccb_en_lun *cel; 3054 cam_status status; 3055 int target; 3056 int lun; 3057 3058 status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate, &lstate, 3059 /* notfound_failure*/FALSE); 3060 3061 if (status != CAM_REQ_CMP) { 3062 ccb->ccb_h.status = status; 3063 xpt_done(ccb); 3064 break; 3065 } 3066 3067 cel = &ccb->cel; 3068 target = ccb->ccb_h.target_id; 3069 lun = ccb->ccb_h.target_lun; 3070 if (cel->enable != 0) { 3071 /* Are we already enabled?? */ 3072 if (lstate != NULL) { 3073 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA; 3074 xpt_done(ccb); 3075 break; 3076 } 3077 3078 if (cel->grp6_len != 0 3079 || cel->grp7_len != 0) { 3080 /* 3081 * Don't (yet?) support vendor 3082 * specific commands. 3083 */ 3084 ccb->ccb_h.status = CAM_REQ_INVALID; 3085 xpt_done(ccb); 3086 break; 3087 } 3088 3089 /* 3090 * Seems to be okay. 3091 * Setup our data structures. 3092 */ 3093 if (tstate == NULL) { 3094 tstate = malloc(sizeof(*tstate), 3095 M_DEVBUF, M_NOWAIT); 3096 if (tstate == NULL) { 3097 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 3098 xpt_done(ccb); 3099 break; 3100 } 3101 bzero(tstate, sizeof(*tstate)); 3102 ahc->enabled_targets[target] = tstate; 3103 } 3104 lstate = malloc(sizeof(*lstate), M_DEVBUF, M_NOWAIT); 3105 if (lstate == NULL) { 3106 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 3107 xpt_done(ccb); 3108 break; 3109 } 3110 bzero(lstate, sizeof(*lstate)); 3111 SLIST_INIT(&lstate->accept_tios); 3112 SLIST_INIT(&lstate->immed_notifies); 3113 tstate->enabled_luns[lun] = lstate; 3114 if ((ahc->features & AHC_MULTI_TID) != 0) { 3115 u_int16_t targid_mask; 3116 3117 pause_sequencer(ahc); 3118 targid_mask = ahc_inb(ahc, TARGID) 3119 | (ahc_inb(ahc, TARGID + 1) << 8); 3120 3121 targid_mask |= (0x01 << target); 3122 ahc_outb(ahc, TARGID, targid_mask); 3123 ahc_outb(ahc, TARGID+1, (targid_mask >> 8)); 3124 unpause_sequencer(ahc, /*always?*/FALSE); 3125 } 3126 ccb->ccb_h.status = CAM_REQ_CMP; 3127 xpt_print_path(ccb->ccb_h.path); 3128 printf("Lun now enabled for target mode\n"); 3129 xpt_done(ccb); 3130 break; 3131 } else { 3132 /* XXX Fully Implement Disable */ 3133 if (lstate == NULL) { 3134 ccb->ccb_h.status = CAM_LUN_INVALID; 3135 xpt_done(ccb); 3136 break; 3137 } 3138 ccb->ccb_h.status = CAM_REQ_CMP; 3139 xpt_done(ccb); 3140 break; 3141 } 3142 break; 3143 } 3144 case XPT_ABORT: /* Abort the specified CCB */ 3145 /* XXX Implement */ 3146 ccb->ccb_h.status = CAM_REQ_INVALID; 3147 xpt_done(ccb); 3148 break; 3149 case XPT_SET_TRAN_SETTINGS: 3150 { 3151 struct ahc_devinfo devinfo; 3152 struct ccb_trans_settings *cts; 3153 struct ahc_target_tinfo *tinfo; 3154 u_int update_type; 3155 int s; 3156 3157 cts = &ccb->cts; 3158 ahc_compile_devinfo(&devinfo, cts->ccb_h.target_id, 3159 SIM_IS_SCSIBUS_B(ahc, sim) ? 'B' : 'A'); 3160 tinfo = &ahc->transinfo[devinfo.target_offset]; 3161 update_type = 0; 3162 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) 3163 update_type |= AHC_TRANS_GOAL; 3164 if ((cts->flags & CCB_TRANS_USER_SETTINGS) != 0) 3165 update_type |= AHC_TRANS_USER; 3166 3167 s = splcam(); 3168 3169 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) { 3170 if ((cts->flags & CCB_TRANS_DISC_ENB) != 0) 3171 ahc->discenable |= devinfo.target_mask; 3172 else 3173 ahc->discenable &= ~devinfo.target_mask; 3174 } 3175 3176 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) { 3177 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) 3178 ahc->tagenable |= devinfo.target_mask; 3179 else 3180 ahc->tagenable &= ~devinfo.target_mask; 3181 } 3182 3183 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) { 3184 switch (cts->bus_width) { 3185 case MSG_EXT_WDTR_BUS_16_BIT: 3186 if ((ahc->features & AHC_WIDE) != 0) 3187 break; 3188 /* FALLTHROUGH to 8bit */ 3189 case MSG_EXT_WDTR_BUS_32_BIT: 3190 case MSG_EXT_WDTR_BUS_8_BIT: 3191 default: 3192 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT; 3193 break; 3194 } 3195 if ((update_type & AHC_TRANS_GOAL) != 0) 3196 tinfo->goal.width = cts->bus_width; 3197 if ((update_type & AHC_TRANS_USER) != 0) 3198 tinfo->user.width = cts->bus_width; 3199 } 3200 3201 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) != 0) { 3202 struct ahc_syncrate *syncrate; 3203 u_int maxsync; 3204 3205 if ((ahc->features & AHC_ULTRA2) != 0) 3206 maxsync = AHC_SYNCRATE_ULTRA2; 3207 else if ((ahc->features & AHC_ULTRA) != 0) 3208 maxsync = AHC_SYNCRATE_ULTRA; 3209 else 3210 maxsync = AHC_SYNCRATE_FAST; 3211 3212 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0) { 3213 if (cts->sync_offset != 0) 3214 cts->sync_offset = ~0; 3215 } else { 3216 cts->sync_offset = 0; 3217 } 3218 3219 syncrate = ahc_find_syncrate(ahc, &cts->sync_period, 3220 maxsync); 3221 ahc_validate_offset(ahc, syncrate, &cts->sync_offset, 3222 tinfo->goal.width); 3223 3224 /* We use a period of 0 to represent async */ 3225 if (cts->sync_offset == 0) 3226 cts->sync_period = 0; 3227 3228 if ((update_type & AHC_TRANS_GOAL) != 0) { 3229 tinfo->goal.period = cts->sync_period; 3230 tinfo->goal.offset = cts->sync_offset; 3231 } 3232 if ((update_type & AHC_TRANS_USER) != 0) { 3233 tinfo->user.period = cts->sync_period; 3234 tinfo->user.offset = cts->sync_offset; 3235 } 3236 } 3237 splx(s); 3238 ccb->ccb_h.status = CAM_REQ_CMP; 3239 xpt_done(ccb); 3240 break; 3241 } 3242 case XPT_GET_TRAN_SETTINGS: 3243 /* Get default/user set transfer settings for the target */ 3244 { 3245 struct ahc_devinfo devinfo; 3246 struct ccb_trans_settings *cts; 3247 struct ahc_target_tinfo *targ_info; 3248 struct ahc_transinfo *tinfo; 3249 int s; 3250 3251 cts = &ccb->cts; 3252 ahc_compile_devinfo(&devinfo, cts->ccb_h.target_id, 3253 SIM_IS_SCSIBUS_B(ahc, sim) ? 'B' : 'A'); 3254 targ_info = &ahc->transinfo[devinfo.target_offset]; 3255 3256 if ((cts->flags & CCB_TRANS_CURRENT_SETTINGS) != 0) 3257 tinfo = &targ_info->current; 3258 else 3259 tinfo = &targ_info->user; 3260 3261 s = splcam(); 3262 3263 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB); 3264 if ((ahc->discenable & devinfo.target_mask) != 0) 3265 cts->flags |= CCB_TRANS_DISC_ENB; 3266 3267 if ((ahc->tagenable & devinfo.target_mask) != 0) 3268 cts->flags |= CCB_TRANS_TAG_ENB; 3269 3270 cts->sync_period = tinfo->period; 3271 cts->sync_offset = tinfo->offset; 3272 cts->bus_width = tinfo->width; 3273 3274 splx(s); 3275 3276 cts->valid = CCB_TRANS_SYNC_RATE_VALID 3277 | CCB_TRANS_SYNC_OFFSET_VALID 3278 | CCB_TRANS_BUS_WIDTH_VALID 3279 | CCB_TRANS_DISC_VALID 3280 | CCB_TRANS_TQ_VALID; 3281 3282 ccb->ccb_h.status = CAM_REQ_CMP; 3283 xpt_done(ccb); 3284 break; 3285 } 3286 case XPT_CALC_GEOMETRY: 3287 { 3288 struct ccb_calc_geometry *ccg; 3289 u_int32_t size_mb; 3290 u_int32_t secs_per_cylinder; 3291 int extended; 3292 3293 ccg = &ccb->ccg; 3294 size_mb = ccg->volume_size 3295 / ((1024L * 1024L) / ccg->block_size); 3296 extended = SIM_IS_SCSIBUS_B(ahc, sim) 3297 ? ahc->flags & AHC_EXTENDED_TRANS_B 3298 : ahc->flags & AHC_EXTENDED_TRANS_A; 3299 3300 if (size_mb > 1024 && extended) { 3301 ccg->heads = 255; 3302 ccg->secs_per_track = 63; 3303 } else { 3304 ccg->heads = 64; 3305 ccg->secs_per_track = 32; 3306 } 3307 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 3308 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 3309 ccb->ccb_h.status = CAM_REQ_CMP; 3310 xpt_done(ccb); 3311 break; 3312 } 3313 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 3314 { 3315 struct cam_path *path; 3316 char channel; 3317 int found; 3318 3319 s = splcam(); 3320 if (SIM_IS_SCSIBUS_B(ahc, sim)) { 3321 channel = 'B'; 3322 path = ahc->path_b; 3323 } else { 3324 channel = 'A'; 3325 path = ahc->path; 3326 } 3327 found = ahc_reset_channel(ahc, channel, /*initiate reset*/TRUE); 3328 splx(s); 3329 if (bootverbose) { 3330 xpt_print_path(path); 3331 printf("SCSI bus reset delivered. " 3332 "%d SCBs aborted.\n", found); 3333 } 3334 ccb->ccb_h.status = CAM_REQ_CMP; 3335 xpt_done(ccb); 3336 break; 3337 } 3338 case XPT_TERM_IO: /* Terminate the I/O process */ 3339 /* XXX Implement */ 3340 ccb->ccb_h.status = CAM_REQ_INVALID; 3341 xpt_done(ccb); 3342 break; 3343 case XPT_PATH_INQ: /* Path routing inquiry */ 3344 { 3345 struct ccb_pathinq *cpi = &ccb->cpi; 3346 3347 cpi->version_num = 1; /* XXX??? */ 3348 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE; 3349 if ((ahc->features & AHC_WIDE) != 0) 3350 cpi->hba_inquiry |= PI_WIDE_16; 3351 if ((ahc->flags & AHC_TARGETMODE) != 0) { 3352 cpi->target_sprt = PIT_PROCESSOR 3353 | PIT_DISCONNECT 3354 | PIT_TERM_IO; 3355 } else { 3356 cpi->target_sprt = 0; 3357 } 3358 cpi->hba_misc = 0; 3359 cpi->hba_eng_cnt = 0; 3360 cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7; 3361 cpi->max_lun = 7; 3362 if (SIM_IS_SCSIBUS_B(ahc, sim)) 3363 cpi->initiator_id = ahc->our_id_b; 3364 else 3365 cpi->initiator_id = ahc->our_id; 3366 cpi->bus_id = cam_sim_bus(sim); 3367 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 3368 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN); 3369 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 3370 cpi->unit_number = cam_sim_unit(sim); 3371 cpi->ccb_h.status = CAM_REQ_CMP; 3372 xpt_done(ccb); 3373 break; 3374 } 3375 default: 3376 ccb->ccb_h.status = CAM_REQ_INVALID; 3377 xpt_done(ccb); 3378 break; 3379 } 3380} 3381 3382static void 3383ahc_async(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg) 3384{ 3385 struct ahc_softc *ahc; 3386 struct cam_sim *sim; 3387 3388 sim = (struct cam_sim *)callback_arg; 3389 ahc = (struct ahc_softc *)cam_sim_softc(sim); 3390 switch (code) { 3391 case AC_LOST_DEVICE: 3392 { 3393 struct ahc_devinfo devinfo; 3394 3395 ahc_compile_devinfo(&devinfo, xpt_path_target_id(path), 3396 SIM_IS_SCSIBUS_B(ahc, sim) ? 'B' : 'A'); 3397 3398 /* 3399 * Revert to async/narrow transfers 3400 * for the next device. 3401 */ 3402 pause_sequencer(ahc); 3403 ahc_set_width(ahc, &devinfo, path, MSG_EXT_WDTR_BUS_8_BIT, 3404 AHC_TRANS_GOAL|AHC_TRANS_CUR); 3405 ahc_set_syncrate(ahc, &devinfo, path, /*syncrate*/NULL, 3406 /*period*/0, /*offset*/0, 3407 AHC_TRANS_GOAL|AHC_TRANS_CUR); 3408 unpause_sequencer(ahc, /*unpause always*/FALSE); 3409 break; 3410 } 3411 default: 3412 break; 3413 } 3414} 3415 3416static void 3417ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments, 3418 int error) 3419{ 3420 struct scb *scb; 3421 union ccb *ccb; 3422 struct ahc_softc *ahc; 3423 int s; 3424 3425 scb = (struct scb *)arg; 3426 ccb = scb->ccb; 3427 ahc = (struct ahc_softc *)ccb->ccb_h.ccb_ahc_ptr; 3428 3429 if (nsegments != 0) { 3430 struct ahc_dma_seg *sg; 3431 bus_dma_segment_t *end_seg; 3432 bus_dmasync_op_t op; 3433 3434 end_seg = dm_segs + nsegments; 3435 3436 /* Copy the first SG into the data pointer area */ 3437 scb->hscb->SG_pointer = scb->ahc_dmaphys; 3438 scb->hscb->data = dm_segs->ds_addr; 3439 scb->hscb->datalen = dm_segs->ds_len; 3440 dm_segs++; 3441 3442 /* Copy the remaining segments into our SG list */ 3443 sg = scb->ahc_dma; 3444 while (dm_segs < end_seg) { 3445 sg->addr = dm_segs->ds_addr; 3446 sg->len = dm_segs->ds_len; 3447 sg++; 3448 dm_segs++; 3449 } 3450 3451 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) 3452 op = BUS_DMASYNC_PREREAD; 3453 else 3454 op = BUS_DMASYNC_PREWRITE; 3455 3456 bus_dmamap_sync(ahc->dmat, scb->dmamap, op); 3457 } else { 3458 scb->hscb->SG_pointer = 0; 3459 scb->hscb->data = 0; 3460 scb->hscb->datalen = 0; 3461 } 3462 3463 scb->sg_count = scb->hscb->SG_count = nsegments; 3464 3465 s = splcam(); 3466 3467 /* 3468 * Last time we need to check if this SCB needs to 3469 * be aborted. 3470 */ 3471 if (ahc_ccb_status(ccb) != CAM_REQ_INPROG) { 3472 if (nsegments != 0) 3473 bus_dmamap_unload(ahc->dmat, scb->dmamap); 3474 ahc_free_scb(ahc, scb); 3475 xpt_done(ccb); 3476 splx(s); 3477 return; 3478 } 3479 3480 /* Busy this tcl if we are untagged */ 3481 if ((scb->hscb->control & TAG_ENB) == 0) 3482 ahc_busy_tcl(ahc, scb); 3483 3484 LIST_INSERT_HEAD(&ahc->pending_ccbs, &ccb->ccb_h, 3485 sim_links.le); 3486 3487 scb->flags |= SCB_ACTIVE; 3488 ccb->ccb_h.status |= CAM_SIM_QUEUED; 3489 3490 ccb->ccb_h.timeout_ch = 3491 timeout(ahc_timeout, (caddr_t)scb, 3492 (ccb->ccb_h.timeout * hz) / 1000); 3493 3494 if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) { 3495 xpt_print_path(ccb->ccb_h.path); 3496 printf("Returning an immediate CTIO\n"); 3497 if ((ahc->flags & AHC_PAGESCBS) == 0) 3498 ahc_outb(ahc, SCBPTR, scb->hscb->tag); 3499 ahc_outb(ahc, SCB_TAG, scb->hscb->tag); 3500 unpause_sequencer(ahc, /*unpause_always*/TRUE); 3501 } else { 3502 3503 ahc->qinfifo[ahc->qinfifonext++] = scb->hscb->tag; 3504 3505 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 3506 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext); 3507 } else { 3508 pause_sequencer(ahc); 3509 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext); 3510 unpause_sequencer(ahc, /*unpause_always*/FALSE); 3511 } 3512 } 3513 3514 splx(s); 3515} 3516 3517static void 3518ahc_poll(struct cam_sim *sim) 3519{ 3520 ahc_intr(cam_sim_softc(sim)); 3521} 3522 3523static void 3524ahc_setup_data(struct ahc_softc *ahc, struct ccb_scsiio *csio, 3525 struct scb *scb) 3526{ 3527 struct hardware_scb *hscb; 3528 struct ccb_hdr *ccb_h; 3529 3530 hscb = scb->hscb; 3531 ccb_h = &csio->ccb_h; 3532 3533 if (ccb_h->func_code == XPT_SCSI_IO) { 3534 hscb->cmdlen = csio->cdb_len; 3535 if ((ccb_h->flags & CAM_CDB_POINTER) != 0) { 3536 if ((ccb_h->flags & CAM_CDB_PHYS) == 0) 3537 if (hscb->cmdlen <= 16) { 3538 memcpy(hscb->cmdstore, 3539 csio->cdb_io.cdb_ptr, 3540 hscb->cmdlen); 3541 hscb->cmdpointer = 3542 hscb->cmdstore_busaddr; 3543 } else 3544 hscb->cmdpointer = 3545 vtophys(csio->cdb_io.cdb_ptr); 3546 else 3547 hscb->cmdpointer = 3548 (u_int32_t)csio->cdb_io.cdb_ptr; 3549 } else { 3550 /* 3551 * CCB CDB Data Storage area is only 16 bytes 3552 * so no additional testing is required 3553 */ 3554 memcpy(hscb->cmdstore, csio->cdb_io.cdb_bytes, 3555 hscb->cmdlen); 3556 hscb->cmdpointer = hscb->cmdstore_busaddr; 3557 } 3558 } 3559 3560 /* Only use S/G if there is a transfer */ 3561 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 3562 if ((ccb_h->flags & CAM_SCATTER_VALID) == 0) { 3563 /* We've been given a pointer to a single buffer */ 3564 if ((ccb_h->flags & CAM_DATA_PHYS) == 0) { 3565 int s; 3566 int error; 3567 3568 s = splsoftvm(); 3569 error = bus_dmamap_load(ahc->dmat, 3570 scb->dmamap, 3571 csio->data_ptr, 3572 csio->dxfer_len, 3573 ahc_execute_scb, 3574 scb, /*flags*/0); 3575 if (error == EINPROGRESS) { 3576 /* 3577 * So as to maintain ordering, 3578 * freeze the controller queue 3579 * until our mapping is 3580 * returned. 3581 */ 3582 xpt_freeze_simq(ahc->sim, 3583 /*count*/1); 3584 scb->ccb->ccb_h.status |= 3585 CAM_RELEASE_SIMQ; 3586 } 3587 splx(s); 3588 } else { 3589 struct bus_dma_segment seg; 3590 3591 /* Pointer to physical buffer */ 3592 if (csio->dxfer_len > AHC_MAXTRANSFER_SIZE) 3593 panic("ahc_setup_data - Transfer size " 3594 "larger than can device max"); 3595 3596 seg.ds_addr = (bus_addr_t)csio->data_ptr; 3597 seg.ds_len = csio->dxfer_len; 3598 ahc_execute_scb(scb, &seg, 1, 0); 3599 } 3600 } else { 3601 struct bus_dma_segment *segs; 3602 3603 if ((ccb_h->flags & CAM_DATA_PHYS) != 0) 3604 panic("ahc_setup_data - Physical segment " 3605 "pointers unsupported"); 3606 3607 if ((ccb_h->flags & CAM_SG_LIST_PHYS) == 0) 3608 panic("ahc_setup_data - Virtual segment " 3609 "addresses unsupported"); 3610 3611 /* Just use the segments provided */ 3612 segs = (struct bus_dma_segment *)csio->data_ptr; 3613 ahc_execute_scb(scb, segs, csio->sglist_cnt, 0); 3614 } 3615 if (ccb_h->func_code == XPT_CONT_TARGET_IO) { 3616 hscb->cmdpointer |= DPHASE_PENDING; 3617 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) 3618 hscb->cmdpointer |= (TARGET_DATA_IN << 8); 3619 } 3620 } else { 3621 ahc_execute_scb(scb, NULL, 0, 0); 3622 } 3623} 3624 3625static void 3626ahc_freeze_devq(struct ahc_softc *ahc, struct cam_path *path) 3627{ 3628 int target; 3629 char channel; 3630 int lun; 3631 3632 target = xpt_path_target_id(path); 3633 lun = xpt_path_lun_id(path); 3634 channel = xpt_path_sim(path)->bus_id == 0 ? 'A' : 'B'; 3635 3636 ahc_search_qinfifo(ahc, target, channel, lun, 3637 /*tag*/SCB_LIST_NULL, CAM_REQUEUE_REQ, 3638 SEARCH_COMPLETE); 3639} 3640 3641/* 3642 * An scb (and hence an scb entry on the board) is put onto the 3643 * free list. 3644 */ 3645static void 3646ahc_free_scb(struct ahc_softc *ahc, struct scb *scb) 3647{ 3648 struct hardware_scb *hscb; 3649 int opri; 3650 3651 hscb = scb->hscb; 3652 3653 opri = splcam(); 3654 3655 if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0 3656 && (scb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 3657 scb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 3658 ahc->flags &= ~AHC_RESOURCE_SHORTAGE; 3659 } 3660 3661 /* Clean up for the next user */ 3662 scb->flags = SCB_FREE; 3663 hscb->control = 0; 3664 hscb->status = 0; 3665 3666 STAILQ_INSERT_HEAD(&ahc->scb_data->free_scbs, scb, links); 3667 splx(opri); 3668} 3669 3670/* 3671 * Get a free scb, either one already assigned to a hardware slot 3672 * on the adapter or one that will require an SCB to be paged out before 3673 * use. If there are none, see if we can allocate a new SCB. Otherwise 3674 * either return an error or sleep. 3675 */ 3676static struct scb * 3677ahc_get_scb(struct ahc_softc *ahc) 3678{ 3679 struct scb *scbp; 3680 int opri; 3681 3682 opri = splcam(); 3683 if ((scbp = STAILQ_FIRST(&ahc->scb_data->free_scbs))) { 3684 STAILQ_REMOVE_HEAD(&ahc->scb_data->free_scbs, links); 3685 } else if (ahc->scb_data->numscbs < ahc->scb_data->maxscbs) { 3686 scbp = ahc_alloc_scb(ahc); 3687 if (scbp == NULL) 3688 printf("%s: Can't malloc SCB\n", ahc_name(ahc)); 3689 } 3690 3691 splx(opri); 3692 3693 return (scbp); 3694} 3695 3696 3697static struct scb * 3698ahc_alloc_scb(struct ahc_softc *ahc) 3699{ 3700 static struct ahc_dma_seg *next_sg_array = NULL; 3701 static int sg_arrays_free = 0; 3702 struct scb *newscb; 3703 int error; 3704 3705 newscb = (struct scb *) malloc(sizeof(struct scb), M_DEVBUF, M_NOWAIT); 3706 if (newscb != NULL) { 3707 bzero(newscb, sizeof(struct scb)); 3708 error = bus_dmamap_create(ahc->dmat, /*flags*/0, 3709 &newscb->dmamap); 3710 if (error != 0) 3711 printf("%s: Unable to allocate SCB dmamap - error %d\n", 3712 ahc_name(ahc), error); 3713 3714 if (error == 0 && next_sg_array == NULL) { 3715 size_t alloc_size = sizeof(struct ahc_dma_seg) 3716 * AHC_NSEG; 3717 sg_arrays_free = PAGE_SIZE / alloc_size; 3718 alloc_size *= sg_arrays_free; 3719 if (alloc_size == 0) 3720 panic("%s: SG list doesn't fit in a page", 3721 ahc_name(ahc)); 3722 next_sg_array = (struct ahc_dma_seg *) 3723 malloc(alloc_size, M_DEVBUF, M_NOWAIT); 3724 } 3725 if (error == 0 && next_sg_array != NULL) { 3726 struct hardware_scb *hscb; 3727 3728 newscb->ahc_dma = next_sg_array; 3729 newscb->ahc_dmaphys = vtophys(next_sg_array); 3730 sg_arrays_free--; 3731 if (sg_arrays_free == 0) 3732 next_sg_array = NULL; 3733 else 3734 next_sg_array = &next_sg_array[AHC_NSEG]; 3735 hscb = &ahc->scb_data->hscbs[ahc->scb_data->numscbs]; 3736 newscb->hscb = hscb; 3737 hscb->control = 0; 3738 hscb->status = 0; 3739 hscb->tag = ahc->scb_data->numscbs; 3740 hscb->residual_data_count[2] = 0; 3741 hscb->residual_data_count[1] = 0; 3742 hscb->residual_data_count[0] = 0; 3743 hscb->residual_SG_count = 0; 3744 hscb->cmdstore_busaddr = 3745 ahc_hscb_busaddr(ahc, hscb->tag) 3746 + offsetof(struct hardware_scb, cmdstore); 3747 /* 3748 * Place in the scbarray 3749 * Never is removed. 3750 */ 3751 ahc->scb_data->scbarray[hscb->tag] = newscb; 3752 ahc->scb_data->numscbs++; 3753 } else { 3754 free(newscb, M_DEVBUF); 3755 newscb = NULL; 3756 } 3757 } 3758 return newscb; 3759} 3760 3761static void 3762ahc_loadseq(struct ahc_softc *ahc) 3763{ 3764 struct patch *cur_patch; 3765 int i; 3766 int downloaded; 3767 int skip_addr; 3768 u_int8_t download_consts[4]; 3769 3770 /* Setup downloadable constant table */ 3771 download_consts[TMODE_NUMCMDS] = ahc->num_targetcmds; 3772 3773 cur_patch = patches; 3774 downloaded = 0; 3775 skip_addr = 0; 3776 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE|LOADRAM); 3777 ahc_outb(ahc, SEQADDR0, 0); 3778 ahc_outb(ahc, SEQADDR1, 0); 3779 3780 for (i = 0; i < sizeof(seqprog)/4; i++) { 3781 if (ahc_check_patch(ahc, &cur_patch, i, &skip_addr) == 0) { 3782 /* 3783 * Don't download this instruction as it 3784 * is in a patch that was removed. 3785 */ 3786 continue; 3787 } 3788 ahc_download_instr(ahc, i, download_consts); 3789 downloaded++; 3790 } 3791 ahc_outb(ahc, SEQCTL, PERRORDIS|FAILDIS|FASTMODE); 3792 restart_sequencer(ahc); 3793 3794 if (bootverbose) 3795 printf(" %d instructions downloaded\n", downloaded); 3796} 3797 3798static int 3799ahc_check_patch(struct ahc_softc *ahc, struct patch **start_patch, 3800 int start_instr, int *skip_addr) 3801{ 3802 struct patch *cur_patch; 3803 struct patch *last_patch; 3804 int patch_index; 3805 int num_patches; 3806 3807 num_patches = sizeof(patches)/sizeof(struct patch); 3808 last_patch = &patches[num_patches]; 3809 cur_patch = *start_patch; 3810 3811 while (cur_patch < last_patch && start_instr == cur_patch->begin) { 3812 3813 if (cur_patch->patch_func(ahc) == 0) { 3814 int skip; 3815 3816 /* Start rejecting code */ 3817 *skip_addr = start_instr + cur_patch->skip_instr; 3818 cur_patch += cur_patch->skip_patch; 3819 } else { 3820 /* Accepted this patch. Advance to the next 3821 * one and wait for our intruction pointer to 3822 * hit this point. 3823 */ 3824 cur_patch++; 3825 } 3826 } 3827 3828 *start_patch = cur_patch; 3829 if (start_instr < *skip_addr) 3830 /* Still skipping */ 3831 return (0); 3832 3833 return (1); 3834} 3835 3836static void 3837ahc_download_instr(struct ahc_softc *ahc, int instrptr, u_int8_t *dconsts) 3838{ 3839 union ins_formats instr; 3840 struct ins_format1 *fmt1_ins; 3841 struct ins_format3 *fmt3_ins; 3842 int fmt3; 3843 u_int opcode; 3844 3845 /* Structure copy */ 3846 instr = *(union ins_formats*)&seqprog[instrptr * 4]; 3847 3848 fmt1_ins = &instr.format1; 3849 fmt3_ins = NULL; 3850 3851 /* Pull the opcode */ 3852 opcode = instr.format1.opcode; 3853 switch (opcode) { 3854 case AIC_OP_JMP: 3855 case AIC_OP_JC: 3856 case AIC_OP_JNC: 3857 case AIC_OP_CALL: 3858 case AIC_OP_JNE: 3859 case AIC_OP_JNZ: 3860 case AIC_OP_JE: 3861 case AIC_OP_JZ: 3862 { 3863 struct patch *cur_patch; 3864 int address_offset; 3865 u_int address; 3866 int skip_addr; 3867 int i; 3868 3869 fmt3_ins = &instr.format3; 3870 address_offset = 0; 3871 address = fmt3_ins->address; 3872 cur_patch = patches; 3873 skip_addr = 0; 3874 3875 for (i = 0; i < address;) { 3876 3877 ahc_check_patch(ahc, &cur_patch, i, &skip_addr); 3878 3879 if (skip_addr > i) { 3880 int end_addr; 3881 3882 end_addr = MIN(address, skip_addr); 3883 address_offset += end_addr - i; 3884 i = skip_addr; 3885 } else { 3886 i++; 3887 } 3888 } 3889 address -= address_offset; 3890 fmt3_ins->address = address; 3891 /* FALLTHROUGH */ 3892 } 3893 case AIC_OP_OR: 3894 case AIC_OP_AND: 3895 case AIC_OP_XOR: 3896 case AIC_OP_ADD: 3897 case AIC_OP_ADC: 3898 case AIC_OP_BMOV: 3899 if (fmt1_ins->parity != 0) { 3900 fmt1_ins->immediate = dconsts[fmt1_ins->immediate]; 3901 } 3902 fmt1_ins->parity = 0; 3903 /* FALLTHROUGH */ 3904 case AIC_OP_ROL: 3905 if ((ahc->features & AHC_ULTRA2) != 0) { 3906 int i, count; 3907 3908 /* Calculate odd parity for the instruction */ 3909 for (i = 0, count = 0; i < 31; i++) { 3910 u_int32_t mask; 3911 3912 mask = 0x01 << i; 3913 if ((instr.integer & mask) != 0) 3914 count++; 3915 } 3916 if ((count & 0x01) == 0) 3917 instr.format1.parity = 1; 3918 } else { 3919 /* Compress the instruction for older sequencers */ 3920 if (fmt3_ins != NULL) { 3921 instr.integer = 3922 fmt3_ins->immediate 3923 | (fmt3_ins->source << 8) 3924 | (fmt3_ins->address << 16) 3925 | (fmt3_ins->opcode << 25); 3926 } else { 3927 instr.integer = 3928 fmt1_ins->immediate 3929 | (fmt1_ins->source << 8) 3930 | (fmt1_ins->destination << 16) 3931 | (fmt1_ins->ret << 24) 3932 | (fmt1_ins->opcode << 25); 3933 } 3934 } 3935 ahc_outsb(ahc, SEQRAM, instr.bytes, 4); 3936 break; 3937 default: 3938 panic("Unknown opcode encountered in seq program"); 3939 break; 3940 } 3941} 3942 3943static void 3944ahc_set_recoveryscb(struct ahc_softc *ahc, struct scb *scb) { 3945 3946 if ((scb->flags & SCB_RECOVERY_SCB) == 0) { 3947 struct ccb_hdr *ccbh; 3948 3949 scb->flags |= SCB_RECOVERY_SCB; 3950 3951 /* 3952 * Take all queued, but not sent SCBs out of the equation. 3953 * Also ensure that no new CCBs are queued to us while we 3954 * try to fix this problem. 3955 */ 3956 if ((scb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 3957 xpt_freeze_simq(ahc->sim, /*count*/1); 3958 scb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 3959 } 3960 3961 /* 3962 * Go through all of our pending SCBs and remove 3963 * any scheduled timeouts for them. We will reschedule 3964 * them after we've successfully fixed this problem. 3965 */ 3966 ccbh = ahc->pending_ccbs.lh_first; 3967 while (ccbh != NULL) { 3968 struct scb *pending_scb; 3969 3970 pending_scb = (struct scb *)ccbh->ccb_scb_ptr; 3971 untimeout(ahc_timeout, pending_scb, ccbh->timeout_ch); 3972 ccbh = ccbh->sim_links.le.le_next; 3973 } 3974 } 3975} 3976 3977static void 3978ahc_timeout(void *arg) 3979{ 3980 struct scb *scb; 3981 struct ahc_softc *ahc; 3982 int s, found; 3983 u_int bus_state; 3984 int target; 3985 int lun; 3986 char channel; 3987 3988 scb = (struct scb *)arg; 3989 ahc = (struct ahc_softc *)scb->ccb->ccb_h.ccb_ahc_ptr; 3990 3991 s = splcam(); 3992 3993 /* 3994 * Ensure that the card doesn't do anything 3995 * behind our back. Also make sure that we 3996 * didn't "just" miss an interrupt that would 3997 * affect this timeout. 3998 */ 3999 do { 4000 ahc_intr(ahc); 4001 pause_sequencer(ahc); 4002 } while (ahc_inb(ahc, INTSTAT) & INT_PEND); 4003 4004 if ((scb->flags & SCB_ACTIVE) == 0) { 4005 /* Previous timeout took care of me already */ 4006 printf("Timedout SCB handled by another timeout\n"); 4007 unpause_sequencer(ahc, /*unpause_always*/TRUE); 4008 splx(s); 4009 return; 4010 } 4011 4012 target = SCB_TARGET(scb); 4013 channel = SCB_CHANNEL(scb); 4014 lun = SCB_LUN(scb); 4015 4016 xpt_print_path(scb->ccb->ccb_h.path); 4017 printf("SCB 0x%x - timed out ", scb->hscb->tag); 4018 /* 4019 * Take a snapshot of the bus state and print out 4020 * some information so we can track down driver bugs. 4021 */ 4022 bus_state = ahc_inb(ahc, LASTPHASE); 4023 4024 switch(bus_state) 4025 { 4026 case P_DATAOUT: 4027 printf("in dataout phase"); 4028 break; 4029 case P_DATAIN: 4030 printf("in datain phase"); 4031 break; 4032 case P_COMMAND: 4033 printf("in command phase"); 4034 break; 4035 case P_MESGOUT: 4036 printf("in message out phase"); 4037 break; 4038 case P_STATUS: 4039 printf("in status phase"); 4040 break; 4041 case P_MESGIN: 4042 printf("in message in phase"); 4043 break; 4044 case P_BUSFREE: 4045 printf("while idle, LASTPHASE == 0x%x", 4046 bus_state); 4047 break; 4048 default: 4049 /* 4050 * We aren't in a valid phase, so assume we're 4051 * idle. 4052 */ 4053 printf("invalid phase, LASTPHASE == 0x%x", 4054 bus_state); 4055 bus_state = P_BUSFREE; 4056 break; 4057 } 4058 4059 printf(", SCSISIGI == 0x%x\n", ahc_inb(ahc, SCSISIGI)); 4060 4061 printf("SEQADDR == 0x%x\n", ahc_inb(ahc, SEQADDR0) 4062 | (ahc_inb(ahc, SEQADDR1) << 8)); 4063 printf("SSTAT1 == 0x%x\n", ahc_inb(ahc, SSTAT1)); 4064#if 0 4065 printf("SCSIRATE == 0x%x\n", ahc_inb(ahc, SCSIRATE)); 4066 printf("CCSCBCTL == 0x%x\n", ahc_inb(ahc, CCSCBCTL)); 4067 printf("CCSCBCNT == 0x%x\n", ahc_inb(ahc, CCSCBCNT)); 4068 printf("DFCNTRL == 0x%x\n", ahc_inb(ahc, DFCNTRL)); 4069 printf("DFSTATUS == 0x%x\n", ahc_inb(ahc, DFSTATUS)); 4070 printf("CCHCNT == 0x%x\n", ahc_inb(ahc, CCHCNT)); 4071#endif 4072 /* Decide our course of action */ 4073 if (scb->flags & SCB_DEVICE_RESET) { 4074 /* 4075 * Been down this road before. 4076 * Do a full bus reset. 4077 */ 4078bus_reset: 4079 ahc_set_ccb_status(scb->ccb, CAM_CMD_TIMEOUT); 4080 found = ahc_reset_channel(ahc, channel, /*Initiate Reset*/TRUE); 4081 printf("%s: Issued Channel %c Bus Reset. " 4082 "%d SCBs aborted\n", ahc_name(ahc), channel, found); 4083 } else { 4084 /* 4085 * Send a Bus Device Reset message: 4086 * The target that is holding up the bus may not 4087 * be the same as the one that triggered this timeout 4088 * (different commands have different timeout lengths). 4089 * Our strategy here is to queue a BDR message 4090 * to the timed out target if the bus is idle. 4091 * Otherwise, if we have an active target we stuff the 4092 * message buffer with a BDR message and assert ATN 4093 * in the hopes that the target will let go of the bus 4094 * and go to the mesgout phase. If this fails, we'll 4095 * get another timeout 2 seconds later which will attempt 4096 * a bus reset. 4097 */ 4098 u_int active_scb_index; 4099 4100 active_scb_index = ahc_inb(ahc, SCB_TAG); 4101 4102 if (bus_state != P_BUSFREE 4103 && (active_scb_index < ahc->scb_data->numscbs)) { 4104 struct scb *active_scb; 4105 4106 /* 4107 * If the active SCB is not from our device, 4108 * assume that another device is hogging the bus 4109 * and wait for it's timeout to expire before 4110 * taking additional action. 4111 */ 4112 active_scb = ahc->scb_data->scbarray[active_scb_index]; 4113 if (active_scb->hscb->tcl != scb->hscb->tcl 4114 && (scb->flags & SCB_OTHERTCL_TIMEOUT) == 0) { 4115 struct ccb_hdr *ccbh; 4116 u_int newtimeout; 4117 4118 scb->flags |= SCB_OTHERTCL_TIMEOUT; 4119 newtimeout = MAX(active_scb->ccb->ccb_h.timeout, 4120 scb->ccb->ccb_h.timeout); 4121 ccbh = &scb->ccb->ccb_h; 4122 scb->ccb->ccb_h.timeout_ch = 4123 timeout(ahc_timeout, scb, 4124 (newtimeout * hz) / 1000); 4125 splx(s); 4126 return; 4127 } 4128 ahc_set_recoveryscb(ahc, active_scb); 4129 ahc_outb(ahc, MSG_OUT, MSG_BUS_DEV_RESET); 4130 ahc_outb(ahc, SCSISIGO, bus_state|ATNO); 4131 xpt_print_path(active_scb->ccb->ccb_h.path); 4132 printf("BDR message in message buffer\n"); 4133 active_scb->flags |= SCB_DEVICE_RESET; 4134 active_scb->ccb->ccb_h.timeout_ch = 4135 timeout(ahc_timeout, (caddr_t)active_scb, 2 * hz); 4136 unpause_sequencer(ahc, /*unpause_always*/TRUE); 4137 } else { 4138 int disconnected; 4139 4140 if (ahc_search_qinfifo(ahc, target, channel, lun, 4141 scb->hscb->tag, /*status*/0, 4142 SEARCH_COUNT) > 0) { 4143 disconnected = FALSE; 4144 } else { 4145 disconnected = TRUE; 4146 } 4147 4148 if (disconnected) { 4149 4150 ahc_set_recoveryscb(ahc, scb); 4151 /* 4152 * Simply set the MK_MESSAGE control bit. 4153 */ 4154 scb->hscb->control |= MK_MESSAGE; 4155 scb->flags |= SCB_QUEUED_MSG 4156 | SCB_DEVICE_RESET; 4157 4158 /* 4159 * Remove this SCB from the disconnected 4160 * list so that a reconnect at this point 4161 * causes a BDR. 4162 */ 4163 ahc_search_disc_list(ahc, target, channel, lun, 4164 scb->hscb->tag); 4165 ahc_index_busy_tcl(ahc, scb->hscb->tcl, 4166 /*unbusy*/TRUE); 4167 4168 /* 4169 * Actually re-queue this SCB in case we can 4170 * select the device before it reconnects. 4171 * Clear out any entries in the QINFIFO first 4172 * so we are the next SCB for this target 4173 * to run. 4174 */ 4175 ahc_search_qinfifo(ahc, SCB_TARGET(scb), 4176 channel, SCB_LUN(scb), 4177 SCB_LIST_NULL, 4178 CAM_REQUEUE_REQ, 4179 SEARCH_COMPLETE); 4180 xpt_print_path(scb->ccb->ccb_h.path); 4181 printf("Queuing a BDR SCB\n"); 4182 ahc->qinfifo[ahc->qinfifonext++] = 4183 scb->hscb->tag; 4184 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 4185 ahc_outb(ahc, HNSCB_QOFF, 4186 ahc->qinfifonext); 4187 } else { 4188 ahc_outb(ahc, KERNEL_QINPOS, 4189 ahc->qinfifonext); 4190 } 4191 scb->ccb->ccb_h.timeout_ch = 4192 timeout(ahc_timeout, (caddr_t)scb, 2 * hz); 4193 unpause_sequencer(ahc, /*unpause_always*/FALSE); 4194 } else { 4195 /* Go "immediatly" to the bus reset */ 4196 /* This shouldn't happen */ 4197 ahc_set_recoveryscb(ahc, scb); 4198 xpt_print_path(scb->ccb->ccb_h.path); 4199 printf("SCB %d: Immediate reset. " 4200 "Flags = 0x%x\n", scb->hscb->tag, 4201 scb->flags); 4202 goto bus_reset; 4203 } 4204 } 4205 } 4206 splx(s); 4207} 4208 4209/* 4210 * Look through the SCB array of the card and attempt to find the 4211 * hardware SCB that corresponds to the passed in SCB. Return 4212 * SCB_LIST_NULL if unsuccessful. This routine assumes that the 4213 * card is already paused. 4214 */ 4215static u_int 4216ahc_find_scb(struct ahc_softc *ahc, struct scb *scb) 4217{ 4218 u_int8_t saved_scbptr; 4219 u_int8_t curindex; 4220 4221 saved_scbptr = ahc_inb(ahc, SCBPTR); 4222 for (curindex = 0; curindex < ahc->scb_data->maxhscbs; curindex++) { 4223 ahc_outb(ahc, SCBPTR, curindex); 4224 if (ahc_inb(ahc, SCB_TAG) == scb->hscb->tag) 4225 break; 4226 } 4227 ahc_outb(ahc, SCBPTR, saved_scbptr); 4228 if (curindex >= ahc->scb_data->maxhscbs) 4229 curindex = SCB_LIST_NULL; 4230 4231 return curindex; 4232} 4233 4234static int 4235ahc_search_qinfifo(struct ahc_softc *ahc, int target, char channel, 4236 int lun, u_int tag, u_int32_t status, 4237 ahc_search_action action) 4238{ 4239 struct scb *scbp; 4240 u_int8_t qinpos; 4241 u_int8_t qintail; 4242 int found; 4243 4244 qinpos = ahc_inb(ahc, QINPOS); 4245 qintail = ahc->qinfifonext; 4246 found = 0; 4247 4248 /* 4249 * Start with an empty queue. Entries that are not chosen 4250 * for removal will be re-added to the queue as we go. 4251 */ 4252 ahc->qinfifonext = qinpos; 4253 4254 while (qinpos != qintail) { 4255 scbp = ahc->scb_data->scbarray[ahc->qinfifo[qinpos]]; 4256 if (ahc_match_scb(scbp, target, channel, lun, tag)) { 4257 /* 4258 * We found an scb that needs to be removed. 4259 */ 4260 switch (action) { 4261 case SEARCH_COMPLETE: 4262 if (ahc_ccb_status(scbp->ccb) == CAM_REQ_INPROG) 4263 ahc_set_ccb_status(scbp->ccb, status); 4264 ahc_freeze_ccb(scbp->ccb); 4265 ahc_done(ahc, scbp); 4266 break; 4267 case SEARCH_COUNT: 4268 ahc->qinfifo[ahc->qinfifonext++] = 4269 scbp->hscb->tag; 4270 break; 4271 case SEARCH_REMOVE: 4272 break; 4273 } 4274 found++; 4275 } else { 4276 ahc->qinfifo[ahc->qinfifonext++] = scbp->hscb->tag; 4277 } 4278 qinpos++; 4279 } 4280 4281 if ((ahc->features & AHC_QUEUE_REGS) != 0) { 4282 ahc_outb(ahc, HNSCB_QOFF, ahc->qinfifonext); 4283 } else { 4284 ahc_outb(ahc, KERNEL_QINPOS, ahc->qinfifonext); 4285 } 4286 4287 return (found); 4288} 4289 4290 4291/* 4292 * Abort all SCBs that match the given description (target/channel/lun/tag), 4293 * setting their status to the passed in status if the status has not already 4294 * been modified from CAM_REQ_INPROG. This routine assumes that the sequencer 4295 * is paused before it is called. 4296 */ 4297static int 4298ahc_abort_scbs(struct ahc_softc *ahc, int target, char channel, 4299 int lun, u_int tag, u_int32_t status) 4300{ 4301 struct scb *scbp; 4302 u_int active_scb; 4303 int i; 4304 int found; 4305 4306 /* restore this when we're done */ 4307 active_scb = ahc_inb(ahc, SCBPTR); 4308 4309 found = ahc_search_qinfifo(ahc, target, channel, lun, tag, 4310 CAM_REQUEUE_REQ, SEARCH_COMPLETE); 4311 4312 /* 4313 * Search waiting for selection list. 4314 */ 4315 { 4316 u_int8_t next, prev; 4317 4318 next = ahc_inb(ahc, WAITING_SCBH); /* Start at head of list. */ 4319 prev = SCB_LIST_NULL; 4320 4321 while (next != SCB_LIST_NULL) { 4322 u_int8_t scb_index; 4323 4324 ahc_outb(ahc, SCBPTR, next); 4325 scb_index = ahc_inb(ahc, SCB_TAG); 4326 if (scb_index >= ahc->scb_data->numscbs) { 4327 panic("Waiting List inconsistency. " 4328 "SCB index == %d, yet numscbs == %d.", 4329 scb_index, ahc->scb_data->numscbs); 4330 } 4331 scbp = ahc->scb_data->scbarray[scb_index]; 4332 if (ahc_match_scb(scbp, target, channel, lun, tag)) { 4333 4334 next = ahc_abort_wscb(ahc, next, prev); 4335 } else { 4336 4337 prev = next; 4338 next = ahc_inb(ahc, SCB_NEXT); 4339 } 4340 } 4341 } 4342 /* 4343 * Go through the disconnected list and remove any entries we 4344 * have queued for completion, 0'ing their control byte too. 4345 */ 4346 ahc_search_disc_list(ahc, target, channel, lun, tag); 4347 4348 /* 4349 * Go through the hardware SCB array looking for commands that 4350 * were active but not on any list. 4351 */ 4352 for(i = 0; i < ahc->scb_data->maxhscbs; i++) { 4353 u_int8_t scbid; 4354 4355 ahc_outb(ahc, SCBPTR, i); 4356 scbid = ahc_inb(ahc, SCB_TAG); 4357 if (scbid < ahc->scb_data->numscbs) { 4358 scbp = ahc->scb_data->scbarray[scbid]; 4359 if (ahc_match_scb(scbp, target, channel, lun, tag)) { 4360 ahc_add_curscb_to_free_list(ahc); 4361 } 4362 } 4363 } 4364 /* 4365 * Go through the pending CCB list and look for 4366 * commands for this target that are still active. 4367 * These are other tagged commands that were 4368 * disconnected when the reset occured. 4369 */ 4370 { 4371 struct ccb_hdr *ccb_h; 4372 4373 4374 ccb_h = ahc->pending_ccbs.lh_first; 4375 4376 while (ccb_h != NULL) { 4377 scbp = (struct scb *)ccb_h->ccb_scb_ptr; 4378 ccb_h = ccb_h->sim_links.le.le_next; 4379 if (ahc_match_scb(scbp, target, channel, lun, tag)) { 4380 if (ahc_ccb_status(scbp->ccb) == CAM_REQ_INPROG) 4381 ahc_set_ccb_status(scbp->ccb, status); 4382 ahc_freeze_ccb(scbp->ccb); 4383 ahc_done(ahc, scbp); 4384 found++; 4385 } 4386 } 4387 } 4388 ahc_outb(ahc, SCBPTR, active_scb); 4389 return found; 4390} 4391 4392static int 4393ahc_search_disc_list(struct ahc_softc *ahc, int target, char channel, 4394 int lun, u_int tag) 4395{ 4396 struct scb *scbp; 4397 u_int next; 4398 u_int prev; 4399 u_int count; 4400 u_int active_scb; 4401 4402 count = 0; 4403 next = ahc_inb(ahc, DISCONNECTED_SCBH); 4404 prev = SCB_LIST_NULL; 4405 4406 /* restore this when we're done */ 4407 active_scb = ahc_inb(ahc, SCBPTR); 4408 4409 while (next != SCB_LIST_NULL) { 4410 u_int scb_index; 4411 4412 ahc_outb(ahc, SCBPTR, next); 4413 scb_index = ahc_inb(ahc, SCB_TAG); 4414 if (scb_index >= ahc->scb_data->numscbs) { 4415 panic("Disconnected List inconsistency. " 4416 "SCB index == %d, yet numscbs == %d.", 4417 scb_index, ahc->scb_data->numscbs); 4418 } 4419 scbp = ahc->scb_data->scbarray[scb_index]; 4420 if (ahc_match_scb(scbp, target, channel, lun, tag)) { 4421 next = ahc_rem_scb_from_disc_list(ahc, prev, 4422 next); 4423 count++; 4424 } else { 4425 prev = next; 4426 next = ahc_inb(ahc, SCB_NEXT); 4427 } 4428 } 4429 ahc_outb(ahc, SCBPTR, active_scb); 4430 return (count); 4431} 4432 4433static u_int 4434ahc_rem_scb_from_disc_list(struct ahc_softc *ahc, u_int prev, u_int scbptr) 4435{ 4436 u_int next; 4437 4438 ahc_outb(ahc, SCBPTR, scbptr); 4439 next = ahc_inb(ahc, SCB_NEXT); 4440 4441 ahc_outb(ahc, SCB_CONTROL, 0); 4442 4443 ahc_add_curscb_to_free_list(ahc); 4444 4445 if (prev != SCB_LIST_NULL) { 4446 ahc_outb(ahc, SCBPTR, prev); 4447 ahc_outb(ahc, SCB_NEXT, next); 4448 } else 4449 ahc_outb(ahc, DISCONNECTED_SCBH, next); 4450 4451 return next; 4452} 4453 4454static void 4455ahc_add_curscb_to_free_list(struct ahc_softc *ahc) 4456{ 4457 /* Invalidate the tag so that ahc_find_scb doesn't think it's active */ 4458 ahc_outb(ahc, SCB_TAG, SCB_LIST_NULL); 4459 4460 ahc_outb(ahc, SCB_NEXT, ahc_inb(ahc, FREE_SCBH)); 4461 ahc_outb(ahc, FREE_SCBH, ahc_inb(ahc, SCBPTR)); 4462} 4463 4464/* 4465 * Manipulate the waiting for selection list and return the 4466 * scb that follows the one that we remove. 4467 */ 4468static u_int 4469ahc_abort_wscb(struct ahc_softc *ahc, u_int scbpos, u_int prev) 4470{ 4471 u_int curscb, next; 4472 4473 /* 4474 * Select the SCB we want to abort and 4475 * pull the next pointer out of it. 4476 */ 4477 curscb = ahc_inb(ahc, SCBPTR); 4478 ahc_outb(ahc, SCBPTR, scbpos); 4479 next = ahc_inb(ahc, SCB_NEXT); 4480 4481 /* Clear the necessary fields */ 4482 ahc_outb(ahc, SCB_CONTROL, 0); 4483 4484 ahc_add_curscb_to_free_list(ahc); 4485 4486 /* update the waiting list */ 4487 if (prev == SCB_LIST_NULL) { 4488 /* First in the list */ 4489 ahc_outb(ahc, WAITING_SCBH, next); 4490 4491 /* 4492 * Ensure we aren't attempting to perform 4493 * selection for this entry. 4494 */ 4495 ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO)); 4496 } else { 4497 /* 4498 * Select the scb that pointed to us 4499 * and update its next pointer. 4500 */ 4501 ahc_outb(ahc, SCBPTR, prev); 4502 ahc_outb(ahc, SCB_NEXT, next); 4503 } 4504 4505 /* 4506 * Point us back at the original scb position. 4507 */ 4508 ahc_outb(ahc, SCBPTR, curscb); 4509 return next; 4510} 4511 4512static void 4513ahc_clear_intstat(struct ahc_softc *ahc) 4514{ 4515 /* Clear any interrupt conditions this may have caused */ 4516 ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO); 4517 ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI 4518 |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG| 4519 CLRREQINIT); 4520 ahc_outb(ahc, CLRINT, CLRSCSIINT); 4521} 4522 4523static void 4524ahc_reset_current_bus(struct ahc_softc *ahc) 4525{ 4526 u_int8_t scsiseq; 4527 4528 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENSCSIRST); 4529 scsiseq = ahc_inb(ahc, SCSISEQ); 4530 ahc_outb(ahc, SCSISEQ, scsiseq | SCSIRSTO); 4531 DELAY(AHC_BUSRESET_DELAY); 4532 /* Turn off the bus reset */ 4533 ahc_outb(ahc, SCSISEQ, scsiseq & ~SCSIRSTO); 4534 4535 ahc_clear_intstat(ahc); 4536 4537 /* Re-enable reset interrupts */ 4538 ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) | ENSCSIRST); 4539} 4540 4541static int 4542ahc_reset_channel(struct ahc_softc *ahc, char channel, int initiate_reset) 4543{ 4544 u_int target, max_target; 4545 int found; 4546 u_int8_t sblkctl; 4547 char cur_channel; 4548 struct cam_path *path; 4549 4550 pause_sequencer(ahc); 4551 /* 4552 * Clean up all the state information for the 4553 * pending transactions on this bus. 4554 */ 4555 found = ahc_abort_scbs(ahc, ALL_TARGETS, channel, ALL_LUNS, 4556 SCB_LIST_NULL, CAM_SCSI_BUS_RESET); 4557 path = channel == 'B' ? ahc->path_b : ahc->path; 4558 4559 /* Notify the XPT that a bus reset occurred */ 4560 xpt_async(AC_BUS_RESET, path, NULL); 4561 4562 /* 4563 * Revert to async/narrow transfers until we renegotiate. 4564 */ 4565 max_target = (ahc->features & AHC_WIDE) ? 15 : 7; 4566 for (target = 0; target <= max_target; target++) { 4567 struct ahc_devinfo devinfo; 4568 4569 ahc_compile_devinfo(&devinfo, target, channel); 4570 ahc_set_width(ahc, &devinfo, path, MSG_EXT_WDTR_BUS_8_BIT, 4571 AHC_TRANS_CUR); 4572 ahc_set_syncrate(ahc, &devinfo, path, /*syncrate*/NULL, 4573 /*period*/0, /*offset*/0, AHC_TRANS_CUR); 4574 } 4575 4576 /* 4577 * Reset the bus if we are initiating this reset and 4578 * restart/unpause the sequencer 4579 */ 4580 sblkctl = ahc_inb(ahc, SBLKCTL); 4581 cur_channel = 'A'; 4582 if ((ahc->features & AHC_TWIN) != 0 4583 && ((sblkctl & SELBUSB) != 0)) 4584 cur_channel = 'B'; 4585 if (cur_channel != channel) { 4586 /* Case 1: Command for another bus is active 4587 * Stealthily reset the other bus without 4588 * upsetting the current bus. 4589 */ 4590 ahc_outb(ahc, SBLKCTL, sblkctl ^ SELBUSB); 4591 ahc_outb(ahc, SIMODE1, 4592 ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENREQINIT)); 4593 ahc_outb(ahc, SCSISEQ, 4594 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP)); 4595 if (initiate_reset) 4596 ahc_reset_current_bus(ahc); 4597 ahc_clear_intstat(ahc); 4598 ahc_outb(ahc, SBLKCTL, sblkctl); 4599 unpause_sequencer(ahc, /*unpause_always*/FALSE); 4600 } else { 4601 /* Case 2: A command from this bus is active or we're idle */ 4602 ahc_outb(ahc, SIMODE1, 4603 ahc_inb(ahc, SIMODE1) & ~(ENBUSFREE|ENREQINIT)); 4604 ahc->flags &= ~AHC_HANDLING_REQINITS; 4605 ahc->msg_type = MSG_TYPE_NONE; 4606 ahc_outb(ahc, SCSISEQ, 4607 ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP)); 4608 if (initiate_reset) 4609 ahc_reset_current_bus(ahc); 4610 ahc_clear_intstat(ahc); 4611 restart_sequencer(ahc); 4612 } 4613 return found; 4614} 4615 4616static int 4617ahc_match_scb (struct scb *scb, int target, char channel, int lun, u_int tag) 4618{ 4619 int targ = SCB_TARGET(scb); 4620 char chan = SCB_CHANNEL(scb); 4621 int slun = SCB_LUN(scb); 4622 int match; 4623 4624 match = ((chan == channel) || (channel == ALL_CHANNELS)); 4625 if (match != 0) 4626 match = ((targ == target) || (target == ALL_TARGETS)); 4627 if (match != 0) 4628 match = ((lun == slun) || (lun == ALL_LUNS)); 4629 if (match != 0) 4630 match = ((tag == scb->hscb->tag) || (tag == SCB_LIST_NULL)); 4631 4632 return match; 4633} 4634 4635static void 4636ahc_construct_sdtr(struct ahc_softc *ahc, u_int period, u_int offset) 4637{ 4638 ahc->msg_buf[ahc->msg_index++] = MSG_EXTENDED; 4639 ahc->msg_buf[ahc->msg_index++] = MSG_EXT_SDTR_LEN; 4640 ahc->msg_buf[ahc->msg_index++] = MSG_EXT_SDTR; 4641 ahc->msg_buf[ahc->msg_index++] = period; 4642 ahc->msg_buf[ahc->msg_index++] = offset; 4643 ahc->msg_len += 5; 4644} 4645 4646static void 4647ahc_construct_wdtr(struct ahc_softc *ahc, u_int bus_width) 4648{ 4649 ahc->msg_buf[ahc->msg_index++] = MSG_EXTENDED; 4650 ahc->msg_buf[ahc->msg_index++] = MSG_EXT_WDTR_LEN; 4651 ahc->msg_buf[ahc->msg_index++] = MSG_EXT_WDTR; 4652 ahc->msg_buf[ahc->msg_index++] = bus_width; 4653 ahc->msg_len += 4; 4654} 4655 4656static void 4657ahc_calc_residual(struct scb *scb) 4658{ 4659 struct hardware_scb *hscb; 4660 4661 hscb = scb->hscb; 4662 4663 /* 4664 * If the disconnected flag is still set, this is bogus 4665 * residual information left over from a sequencer 4666 * pagin/pageout, so ignore this case. 4667 */ 4668 if ((scb->hscb->control & DISCONNECTED) == 0) { 4669 u_int32_t resid; 4670 int resid_sgs; 4671 int sg; 4672 4673 /* 4674 * Remainder of the SG where the transfer 4675 * stopped. 4676 */ 4677 resid = (hscb->residual_data_count[2] << 16) 4678 | (hscb->residual_data_count[1] <<8) 4679 | (hscb->residual_data_count[0]); 4680 4681 /* 4682 * Add up the contents of all residual 4683 * SG segments that are after the SG where 4684 * the transfer stopped. 4685 */ 4686 resid_sgs = scb->hscb->residual_SG_count - 1/*current*/; 4687 sg = scb->sg_count - resid_sgs - 1/*first SG*/; 4688 while (resid_sgs > 0) { 4689 4690 resid += scb->ahc_dma[sg].len; 4691 sg++; 4692 resid_sgs--; 4693 } 4694 if ((scb->flags & SCB_SENSE) == 0) { 4695 4696 scb->ccb->csio.resid = resid; 4697 } else { 4698 4699 scb->ccb->csio.sense_resid = resid; 4700 } 4701 } 4702 4703 /* 4704 * Clean out the residual information in this SCB for its 4705 * next consumer. 4706 */ 4707 hscb->residual_data_count[0] = 0; 4708 hscb->residual_data_count[1] = 0; 4709 hscb->residual_data_count[2] = 0; 4710 hscb->residual_SG_count = 0; 4711 4712#ifdef AHC_DEBUG 4713 if (ahc_debug & AHC_SHOWMISC) { 4714 sc_print_addr(xs->sc_link); 4715 printf("Handled Residual of %ld bytes\n" ,xs->resid); 4716 } 4717#endif 4718} 4719 4720static void 4721ahc_update_pending_syncrates(struct ahc_softc *ahc) 4722{ 4723 /* 4724 * Traverse the pending SCB list and ensure that all of the 4725 * SCBs there have the proper settings. 4726 */ 4727 struct ccb_hdr *ccbh; 4728 int pending_ccb_count; 4729 int i; 4730 u_int saved_scbptr; 4731 4732 /* 4733 * We were able to complete the command successfully, 4734 * so reinstate the timeouts for all other pending 4735 * commands. 4736 */ 4737 ccbh = LIST_FIRST(&ahc->pending_ccbs); 4738 pending_ccb_count = 0; 4739 while (ccbh != NULL) { 4740 struct scb *pending_scb; 4741 struct hardware_scb *pending_hscb; 4742 struct ahc_target_tinfo *tinfo; 4743 struct ahc_devinfo devinfo; 4744 4745 pending_scb = (struct scb *)ccbh->ccb_scb_ptr; 4746 pending_hscb = pending_scb->hscb; 4747 ahc_compile_devinfo(&devinfo, SCB_TARGET(pending_scb), 4748 SCB_CHANNEL(pending_scb)); 4749 tinfo = &ahc->transinfo[devinfo.target_offset]; 4750 pending_hscb->control &= ~ULTRAENB; 4751 if ((ahc->ultraenb & devinfo.target_mask) != 0) 4752 pending_hscb->control |= ULTRAENB; 4753 pending_hscb->scsirate = tinfo->scsirate; 4754 pending_hscb->scsioffset = tinfo->current.offset; 4755 pending_ccb_count++; 4756 ccbh = LIST_NEXT(ccbh, sim_links.le); 4757 } 4758 4759 if (pending_ccb_count == 0) 4760 return; 4761 4762 saved_scbptr = ahc_inb(ahc, SCBPTR); 4763 /* Ensure that the hscbs down on the card match the new information */ 4764 for (i = 0; i < ahc->scb_data->maxhscbs; i++) { 4765 u_int scb_tag; 4766 4767 ahc_outb(ahc, SCBPTR, i); 4768 scb_tag = ahc_inb(ahc, SCB_TAG); 4769 if (scb_tag != SCB_LIST_NULL) { 4770 struct scb *pending_scb; 4771 struct hardware_scb *pending_hscb; 4772 struct ahc_target_tinfo *tinfo; 4773 struct ahc_devinfo devinfo; 4774 u_int control; 4775 4776 pending_scb = ahc->scb_data->scbarray[scb_tag]; 4777 pending_hscb = pending_scb->hscb; 4778 ahc_compile_devinfo(&devinfo, SCB_TARGET(pending_scb), 4779 SCB_CHANNEL(pending_scb)); 4780 tinfo = &ahc->transinfo[devinfo.target_offset]; 4781 control = ahc_inb(ahc, SCB_CONTROL); 4782 control &= ~ULTRAENB; 4783 if ((ahc->ultraenb & devinfo.target_mask) != 0) 4784 control |= ULTRAENB; 4785 ahc_outb(ahc, SCB_CONTROL, control); 4786 ahc_outb(ahc, SCB_SCSIRATE, tinfo->scsirate); 4787 ahc_outb(ahc, SCB_SCSIOFFSET, tinfo->current.offset); 4788 } 4789 } 4790 ahc_outb(ahc, SCBPTR, saved_scbptr); 4791} 4792 4793static void 4794ahc_dump_targcmd(struct target_cmd *cmd) 4795{ 4796 u_int8_t *byte; 4797 u_int8_t *last_byte; 4798 int initiator; 4799 int target; 4800 int lun; 4801 int i; 4802 4803 byte = &cmd->icl; 4804 /* Debugging info for received commands */ 4805 last_byte = &cmd[1].icl; 4806 4807 i = 0; 4808 while (byte < last_byte) { 4809 if (i == 0) 4810 printf("\t"); 4811 printf("%#x", *byte++); 4812 i++; 4813 if (i == 8) { 4814 printf("\n"); 4815 i = 0; 4816 } else { 4817 printf(", "); 4818 } 4819 } 4820} 4821 4822static void 4823ahc_shutdown(int howto, void *arg) 4824{ 4825 struct ahc_softc *ahc; 4826 int i; 4827 4828 ahc = (struct ahc_softc *)arg; 4829 4830 ahc_reset(ahc); 4831 ahc_outb(ahc, SCSISEQ, 0); 4832 ahc_outb(ahc, SXFRCTL0, 0); 4833 ahc_outb(ahc, DSPCISTATUS, 0); 4834 4835 for (i = TARG_SCSIRATE; i < HA_274_BIOSCTRL; i++) 4836 ahc_outb(ahc, i, 0); 4837} 4838