133static int auto_login = 1; 134static int max_speed = 2; 135 136SYSCTL_DECL(_hw_firewire); 137SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem"); 138SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0, 139 "SBP debug flag"); 140SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0, 141 "SBP perform login automatically"); 142SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0, 143 "SBP transfer max speed"); 144 145#define SBP_DEBUG(x) if (debug > x) { 146#define END_DEBUG } 147 148#define NEED_RESPONSE 0 149 150struct ind_ptr { 151 u_int32_t hi,lo; 152}; 153#define SBP_IND_MAX 0x20 154struct sbp_ocb { 155 STAILQ_ENTRY(sbp_ocb) ocb; 156 union ccb *ccb; 157 volatile u_int32_t orb[8]; 158 volatile struct ind_ptr ind_ptr[SBP_IND_MAX]; 159 struct sbp_dev *sdev; 160 int flags; 161 bus_dmamap_t dmamap; 162}; 163#define OCB_ACT_MGM 0 164#define OCB_ACT_CMD 1 165#define OCB_ACT_MASK 3 166#define OCB_RESERVED 0x10 167#define OCB_DONE 0x20 168 169#define SBP_RESOURCE_SHORTAGE 0x10 170 171struct sbp_login_res{ 172#if FW_ENDIANSWAP == 0 && BYTE_ORDER == LITTLE_ENDIAN 173 u_int16_t len; 174 u_int16_t id; 175 u_int16_t res0; 176 u_int16_t cmd_hi; 177 u_int32_t cmd_lo; 178 u_int16_t res1; 179 u_int16_t recon_hold; 180#else 181 u_int16_t id; 182 u_int16_t len; 183 u_int16_t cmd_hi; 184 u_int16_t res0; 185 u_int32_t cmd_lo; 186 u_int16_t recon_hold; 187 u_int16_t res1; 188#endif 189}; 190struct sbp_status{ 191#if FW_ENDIANSWAP == 0 && BYTE_ORDER == LITTLE_ENDIAN 192 u_int8_t len:3, 193 dead:1, 194 resp:2, 195 src:2; 196 u_int8_t status:8; 197 u_int16_t orb_hi; 198 u_int32_t orb_lo; 199 u_int32_t data[6]; 200#else 201 u_int16_t orb_hi; 202 u_int8_t status:8; 203 u_int8_t len:3, 204 dead:1, 205 resp:2, 206 src:2; 207 u_int32_t orb_lo; 208 u_int32_t data[6]; 209#endif 210}; 211struct sbp_cmd_status{ 212#define SBP_SFMT_CURR 0 213#define SBP_SFMT_DEFER 1 214#if FW_ENDIANSWAP == 0 && BYTE_ORDER == LITTLE_ENDIAN 215 u_int8_t status:6, 216 sfmt:2; 217 u_int8_t s_key:4, 218 ill_len:1, 219 eom:1, 220 mark:1, 221 valid:1; 222 u_int8_t s_code; 223 u_int8_t s_qlfr; 224 u_int32_t info; 225 u_int32_t cdb; 226 u_int32_t fru:8, 227 s_keydep:24; 228 u_int32_t vend[2]; 229#else 230 u_int8_t s_qlfr; 231 u_int8_t s_code; 232 u_int8_t s_key:4, 233 ill_len:1, 234 eom:1, 235 mark:1, 236 valid:1; 237 u_int8_t status:6, 238 sfmt:2; 239 u_int32_t info; 240 u_int32_t cdb; 241 u_int32_t s_keydep:24, 242 fru:8; 243 u_int32_t vend[2]; 244#endif 245}; 246 247struct sbp_dev{ 248#define SBP_DEV_RESET 0 /* accept login */ 249#define SBP_DEV_LOGIN 1 /* to login */ 250#define SBP_DEV_RECONN 2 /* to reconnect */ 251#define SBP_DEV_TOATTACH 3 /* to attach */ 252#define SBP_DEV_PROBE 4 /* scan lun */ 253#define SBP_DEV_ATTACHED 5 /* in operation */ 254#define SBP_DEV_DEAD 6 /* unavailable unit */ 255#define SBP_DEV_RETRY 7 /* unavailable unit */ 256 int status; 257 int lun_id; 258 struct cam_path *path; 259 struct sbp_target *target; 260 struct sbp_login_res login; 261 STAILQ_HEAD(, sbp_ocb) ocbs; 262 char vendor[32]; 263 char product[32]; 264 char revision[10]; 265}; 266 267struct sbp_target { 268 int target_id; 269 int num_lun; 270 struct sbp_dev *luns; 271 struct sbp_softc *sbp; 272 struct fw_device *fwdev; 273 u_int32_t mgm_hi, mgm_lo; 274}; 275 276struct sbp_softc { 277 struct firewire_dev_comm fd; 278 unsigned char flags; 279 struct cam_sim *sim; 280 struct sbp_target targets[SBP_NUM_TARGETS]; 281 struct fw_bind fwb; 282 STAILQ_HEAD(, sbp_ocb) free_ocbs; 283 struct sbp_ocb *ocb; 284 bus_dma_tag_t dmat; 285}; 286static void sbp_post_explore __P((void *)); 287static void sbp_recv __P((struct fw_xfer *)); 288static void sbp_login_callback __P((struct fw_xfer *)); 289static void sbp_cmd_callback __P((struct fw_xfer *)); 290static void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *)); 291static void sbp_execute_ocb __P((void *, bus_dma_segment_t *, int, int)); 292static void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *)); 293static void sbp_abort_ocb __P((struct sbp_ocb *, int)); 294static void sbp_abort_all_ocbs __P((struct sbp_dev *, int)); 295static struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int)); 296static struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *)); 297static struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *)); 298static struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, u_int32_t)); 299static void sbp_detach_target __P((struct sbp_target *)); 300static void sbp_timeout __P((void *arg)); 301static void sbp_mgm_orb __P((struct sbp_dev *, int)); 302 303MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/Firewire"); 304 305/* cam related functions */ 306static void sbp_action(struct cam_sim *sim, union ccb *ccb); 307static void sbp_poll(struct cam_sim *sim); 308static void sbp_cam_callback(struct cam_periph *periph, 309 union ccb *ccb); 310static void sbp_cam_scan_lun(struct sbp_dev *sdev); 311 312static char *orb_status0[] = { 313 /* 0 */ "No additional information to report", 314 /* 1 */ "Request type not supported", 315 /* 2 */ "Speed not supported", 316 /* 3 */ "Page size not supported", 317 /* 4 */ "Access denied", 318 /* 5 */ "Logical unit not supported", 319 /* 6 */ "Maximum payload too small", 320 /* 7 */ "Reserved for future standardization", 321 /* 8 */ "Resources unavailable", 322 /* 9 */ "Function rejected", 323 /* A */ "Login ID not recognized", 324 /* B */ "Dummy ORB completed", 325 /* C */ "Request aborted", 326 /* FF */ "Unspecified error" 327#define MAX_ORB_STATUS0 0xd 328}; 329 330static char *orb_status1_object[] = { 331 /* 0 */ "Operation request block (ORB)", 332 /* 1 */ "Data buffer", 333 /* 2 */ "Page table", 334 /* 3 */ "Unable to specify" 335}; 336 337static char *orb_status1_serial_bus_error[] = { 338 /* 0 */ "Missing acknowledge", 339 /* 1 */ "Reserved; not to be used", 340 /* 2 */ "Time-out error", 341 /* 3 */ "Reserved; not to be used", 342 /* 4 */ "Busy retry limit exceeded(X)", 343 /* 5 */ "Busy retry limit exceeded(A)", 344 /* 6 */ "Busy retry limit exceeded(B)", 345 /* 7 */ "Reserved for future standardization", 346 /* 8 */ "Reserved for future standardization", 347 /* 9 */ "Reserved for future standardization", 348 /* A */ "Reserved for future standardization", 349 /* B */ "Tardy retry limit exceeded", 350 /* C */ "Conflict error", 351 /* D */ "Data error", 352 /* E */ "Type error", 353 /* F */ "Address error" 354}; 355 356static void 357sbp_identify(driver_t *driver, device_t parent) 358{ 359 device_t child; 360SBP_DEBUG(0) 361 printf("sbp_identify\n"); 362END_DEBUG 363 364 child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 365} 366 367/* 368 * sbp_probe() 369 */ 370static int 371sbp_probe(device_t dev) 372{ 373 device_t pa; 374 375SBP_DEBUG(0) 376 printf("sbp_probe\n"); 377END_DEBUG 378 379 pa = device_get_parent(dev); 380 if(device_get_unit(dev) != device_get_unit(pa)){ 381 return(ENXIO); 382 } 383 384 device_set_desc(dev, "SBP2/SCSI over firewire"); 385 return (0); 386} 387 388static void 389sbp_show_sdev_info(struct sbp_dev *sdev, int new) 390{ 391 int lun; 392 struct fw_device *fwdev; 393 394 printf("%s:%d:%d ", 395 device_get_nameunit(sdev->target->sbp->fd.dev), 396 sdev->target->target_id, 397 sdev->lun_id 398 ); 399 if (new == 2) { 400 return; 401 } 402 fwdev = sdev->target->fwdev; 403 lun = getcsrdata(fwdev, 0x14); 404 printf("ordered:%d type:%d EUI:%08x%08x node:%d " 405 "speed:%d maxrec:%d", 406 (lun & 0x00400000) >> 22, 407 (lun & 0x001f0000) >> 16, 408 fwdev->eui.hi, 409 fwdev->eui.lo, 410 fwdev->dst, 411 fwdev->speed, 412 fwdev->maxrec 413 ); 414 if (new) 415 printf(" new!\n"); 416 else 417 printf("\n"); 418 sbp_show_sdev_info(sdev, 2); 419 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 420} 421 422static struct sbp_target * 423sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 424{ 425 int i, lun; 426 struct sbp_target *target; 427 struct sbp_dev *sdev; 428 429SBP_DEBUG(1) 430 printf("sbp_alloc_target\n"); 431END_DEBUG 432 for (i = 0; i < SBP_NUM_TARGETS; i++) 433 if(sbp->targets[i].fwdev == NULL) break; 434 if (i == SBP_NUM_TARGETS) { 435 printf("increase SBP_NUM_TARGETS!\n"); 436 return NULL; 437 } 438 /* new target */ 439 target = &sbp->targets[i]; 440 target->sbp = sbp; 441 target->fwdev = fwdev; 442 target->target_id = i; 443 if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){ 444 /* bad target */ 445 printf("NULL management address\n"); 446 target->fwdev = NULL; 447 return NULL; 448 } 449 target->mgm_hi = 0xffff; 450 target->mgm_lo = 0xf0000000 | target->mgm_lo << 2; 451 /* XXX should probe all luns */ 452 /* XXX num_lun may be changed. realloc luns? */ 453 lun = getcsrdata(target->fwdev, 0x14) & 0xff; 454 target->num_lun = lun + 1; 455 target->luns = (struct sbp_dev *) malloc( 456 sizeof(struct sbp_dev) * target->num_lun, 457 M_SBP, M_NOWAIT | M_ZERO); 458 for (i = 0; i < target->num_lun; i++) { 459 sdev = &target->luns[i]; 460 sdev->lun_id = i; 461 sdev->target = target; 462 STAILQ_INIT(&sdev->ocbs); 463 if (i == lun) 464 sdev->status = SBP_DEV_RESET; 465 else 466 sdev->status = SBP_DEV_DEAD; 467 } 468 return target; 469} 470 471static void 472sbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len) 473{ 474 static char *nullstr = "(null)"; 475 int i, clen, found=0; 476 struct csrhdr *chdr; 477 struct csrreg *creg; 478 u_int32_t *src, *dst; 479 480 chdr = (struct csrhdr *)&fwdev->csrrom[0]; 481 creg = (struct csrreg *)chdr; 482 creg += chdr->info_len; 483 for( i = chdr->info_len + 4; i <= fwdev->rommax; i+=4){ 484 if((creg++)->key == key){ 485 found = 1; 486 break; 487 } 488 } 489 if (!found) { 490 strncpy(buf, nullstr, len); 491 return; 492 } 493 src = (u_int32_t *) creg + creg->val; 494 clen = ((*src >> 16) - 2) * 4; 495 src += 3; 496 dst = (u_int32_t *) buf; 497 if (len < clen) 498 clen = len; 499 for (i = 0; i < clen/4; i++) 500 *dst++ = htonl(*src++); 501 buf[clen] = 0; 502} 503 504static void 505sbp_probe_lun(struct sbp_dev *sdev) 506{ 507 struct fw_device *fwdev; 508 int rev; 509 510 fwdev = sdev->target->fwdev; 511 bzero(sdev->vendor, sizeof(sdev->vendor)); 512 bzero(sdev->product, sizeof(sdev->product)); 513 sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor)); 514 sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product)); 515 rev = getcsrdata(sdev->target->fwdev, 0x3c); 516 snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev); 517} 518static void 519sbp_probe_target(struct sbp_target *target, int alive) 520{ 521 struct sbp_softc *sbp; 522 struct sbp_dev *sdev; 523 struct firewire_comm *fc; 524 int i; 525 526SBP_DEBUG(1) 527 printf("sbp_probe_target %d\n", target->target_id); 528 if (!alive) 529 printf("not alive\n"); 530END_DEBUG 531 532 sbp = target->sbp; 533 fc = target->sbp->fd.fc; 534 for (i=0; i < target->num_lun; i++) { 535 sdev = &target->luns[i]; 536 if (alive && (sdev->status != SBP_DEV_DEAD)) { 537 if (sdev->path != NULL) { 538 xpt_freeze_devq(sdev->path, 1); 539 } 540 sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 541 switch (sdev->status) { 542 case SBP_DEV_ATTACHED: 543 sbp_mgm_orb(sdev, ORB_FUN_RCN); 544 break; 545 case SBP_DEV_RETRY: 546 sbp_probe_lun(sdev); 547 sbp_mgm_orb(sdev, ORB_FUN_LGI); 548 break; 549 default: 550 /* new or revived target */ 551 sbp_probe_lun(sdev); 552 if (auto_login) { 553 sdev->status = SBP_DEV_TOATTACH; 554 sbp_mgm_orb(sdev, ORB_FUN_LGI); 555 } 556 break; 557 }
|
562 } else { 563 switch (sdev->status) { 564 case SBP_DEV_ATTACHED: 565SBP_DEBUG(0) 566 /* the device has gone */ 567 sbp_show_sdev_info(sdev, 2); 568 printf("lost target\n"); 569END_DEBUG 570 if (sdev->path) 571 xpt_freeze_devq(sdev->path, 1); 572 sdev->status = SBP_DEV_RETRY; 573 sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 574 break; 575 case SBP_DEV_PROBE: 576 case SBP_DEV_TOATTACH: 577 sdev->status = SBP_DEV_RESET; 578 break; 579 case SBP_DEV_RETRY: 580 case SBP_DEV_RESET: 581 case SBP_DEV_DEAD: 582 break; 583 } 584 } 585 } 586} 587 588#if 0 589static void 590sbp_release_queue(void *arg) 591{ 592 struct sbp_softc *sbp; 593 594SBP_DEBUG(0) 595 printf("sbp_release_queue\n"); 596END_DEBUG 597 sbp = (struct sbp_softc *)arg; 598 xpt_release_simq(sbp->sim, 1); 599} 600 601static void 602sbp_release_devq(void *arg) 603{ 604 struct sbp_dev *sdev; 605 int s; 606 607 sdev = (struct sbp_dev *)arg; 608SBP_DEBUG(0) 609 sbp_show_sdev_info(sdev, 2); 610 printf("sbp_release_devq\n"); 611END_DEBUG 612 s = splcam(); 613 xpt_release_devq(sdev->path, 1, TRUE); 614 splx(s); 615} 616#endif 617 618static void 619sbp_post_explore(void *arg) 620{ 621 struct sbp_softc *sbp = (struct sbp_softc *)arg; 622 struct sbp_target *target; 623 struct fw_device *fwdev; 624 int i, alive; 625 626SBP_DEBUG(1) 627 printf("sbp_post_explore\n"); 628END_DEBUG 629#if 0 630 xpt_freeze_simq(sbp->sim, /*count*/ 1); 631#endif 632 /* Gabage Collection */ 633 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 634 target = &sbp->targets[i]; 635 for( fwdev = TAILQ_FIRST(&sbp->fd.fc->devices); 636 fwdev != NULL; fwdev = TAILQ_NEXT(fwdev, link)){ 637 if(target->fwdev == NULL) break; 638 if(target->fwdev == fwdev) break; 639 } 640 if(fwdev == NULL){ 641 /* device has removed in lower driver */ 642 sbp_detach_target(target); 643 } 644 } 645 /* traverse device list */ 646 for( fwdev = TAILQ_FIRST(&sbp->fd.fc->devices); 647 fwdev != NULL; fwdev = TAILQ_NEXT(fwdev, link)){ 648SBP_DEBUG(0) 649 printf("sbp_post_explore: EUI:%08x%08x ", 650 fwdev->eui.hi, fwdev->eui.lo); 651 if (fwdev->status == FWDEVATTACHED) { 652 printf("spec=%d key=%d.\n", 653 getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10, 654 getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 655 } else { 656 printf("not attached, state=%d.\n", fwdev->status); 657 } 658END_DEBUG 659 alive = (fwdev->status == FWDEVATTACHED) 660 && (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10) 661 && (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 662 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 663 target = &sbp->targets[i]; 664 if(target->fwdev == fwdev ) { 665 /* known target */ 666 break; 667 } 668 } 669 if(i == SBP_NUM_TARGETS){ 670 if (alive) { 671 /* new target */ 672 target = sbp_alloc_target(sbp, fwdev); 673 if (target == NULL) 674 continue; 675 } else { 676 continue; 677 } 678 } 679 sbp_probe_target(target, alive); 680 } 681#if 0 682 timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000); 683#endif 684} 685 686#if NEED_RESPONSE 687static void 688sbp_loginres_callback(struct fw_xfer *xfer){ 689SBP_DEBUG(1) 690 struct sbp_dev *sdev; 691 sdev = (struct sbp_dev *)xfer->sc; 692 sbp_show_sdev_info(sdev, 2); 693 printf("sbp_loginres_callback\n"); 694END_DEBUG 695 fw_xfer_free(xfer); 696 return; 697} 698#endif 699 700static void 701sbp_login_callback(struct fw_xfer *xfer) 702{ 703SBP_DEBUG(1) 704 struct sbp_dev *sdev; 705 sdev = (struct sbp_dev *)xfer->sc; 706 sbp_show_sdev_info(sdev, 2); 707 printf("sbp_login_callback\n"); 708END_DEBUG 709 fw_xfer_free(xfer); 710 return; 711} 712 713static void 714sbp_cmd_callback(struct fw_xfer *xfer) 715{ 716SBP_DEBUG(2) 717 struct sbp_dev *sdev; 718 sdev = (struct sbp_dev *)xfer->sc; 719 sbp_show_sdev_info(sdev, 2); 720 printf("sbp_cmd_callback\n"); 721END_DEBUG 722 fw_xfer_free(xfer); 723 return; 724} 725 726static void 727sbp_cam_callback(struct cam_periph *periph, union ccb *ccb) 728{ 729 struct sbp_dev *sdev; 730 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 731SBP_DEBUG(1) 732 sbp_show_sdev_info(sdev, 2); 733 printf("sbp_cam_callback\n"); 734END_DEBUG 735 sdev->status = SBP_DEV_ATTACHED; 736 free(ccb, M_SBP); 737} 738 739static void 740sbp_cam_scan_lun(struct sbp_dev *sdev) 741{ 742 union ccb *ccb = malloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO); 743 744SBP_DEBUG(0) 745 sbp_show_sdev_info(sdev, 2); 746 printf("sbp_cam_scan_lun\n"); 747END_DEBUG 748 xpt_setup_ccb(&ccb->ccb_h, sdev->path, 5/*priority (low)*/); 749 ccb->ccb_h.func_code = XPT_SCAN_LUN; 750 ccb->ccb_h.cbfcnp = sbp_cam_callback; 751 ccb->crcn.flags = CAM_FLAG_NONE; 752 ccb->ccb_h.ccb_sdev_ptr = sdev; 753 xpt_action(ccb); 754 755 /* The scan is in progress now. */ 756} 757 758 759static void 760sbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb) 761{ 762 struct sbp_dev *sdev; 763 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 764SBP_DEBUG(1) 765 sbp_show_sdev_info(sdev, 2); 766 printf("sbp_ping_unit_callback\n"); 767END_DEBUG 768 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 769 if (--ccb->ccb_h.retry_count == 0) { 770 sbp_show_sdev_info(sdev, 2); 771 printf("sbp_tur_callback: retry count exceeded\n"); 772 sdev->status = SBP_DEV_RETRY; 773 free(ccb, M_SBP); 774 } else { 775 /* requeue */ 776 xpt_action(ccb); 777 xpt_release_devq(sdev->path, 1, TRUE); 778 } 779 } else { 780 free(ccb->csio.data_ptr, M_SBP); 781 free(ccb, M_SBP); 782 sdev->status = SBP_DEV_ATTACHED; 783 xpt_release_devq(sdev->path, 1, TRUE); 784 } 785} 786 787/* 788 * XXX Some devices need to execute inquiry or read_capacity 789 * after bus_rest during busy transfer. 790 * Otherwise they return incorrect result for READ(and WRITE?) 791 * command without any SBP-II/SCSI error. 792 * 793 * e.g. Maxtor 3000XT, Yano A-dish. 794 */ 795static void 796sbp_ping_unit(struct sbp_dev *sdev) 797{ 798 union ccb *ccb; 799 struct scsi_inquiry_data *inq_buf; 800 801 ccb = malloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO); 802 inq_buf = (struct scsi_inquiry_data *) 803 malloc(sizeof(*inq_buf), M_SBP, M_WAITOK); 804 805SBP_DEBUG(1) 806 sbp_show_sdev_info(sdev, 2); 807 printf("sbp_ping_unit\n"); 808END_DEBUG 809 810 /* 811 * We need to execute this command before any other queued command. 812 * Make priority 0 and freeze queue after execution for retry. 813 * cam's scan_lun command doesn't provide this feature. 814 */ 815 xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/); 816 scsi_inquiry( 817 &ccb->csio, 818 /*retries*/ 5, 819 sbp_ping_unit_callback, 820 MSG_SIMPLE_Q_TAG, 821 (u_int8_t *)inq_buf, 822 SHORT_INQUIRY_LENGTH, 823 /*evpd*/FALSE, 824 /*page_code*/0, 825 SSD_MIN_SIZE, 826 /*timeout*/60000 827 ); 828 ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 829 xpt_action(ccb); 830} 831 832static void 833sbp_do_attach(struct fw_xfer *xfer) 834{ 835 struct sbp_dev *sdev; 836 837 sdev = (struct sbp_dev *)xfer->sc; 838SBP_DEBUG(0) 839 sbp_show_sdev_info(sdev, 2); 840 printf("sbp_do_attach\n"); 841END_DEBUG 842 fw_xfer_free(xfer); 843 if (sdev->path == NULL) 844 xpt_create_path(&sdev->path, xpt_periph, 845 cam_sim_path(sdev->target->sbp->sim), 846 sdev->target->target_id, sdev->lun_id); 847 848 if (sdev->status == SBP_DEV_RETRY) { 849 sdev->status = SBP_DEV_PROBE; 850 sbp_ping_unit(sdev); 851 /* freezed twice */ 852 xpt_release_devq(sdev->path, 1, TRUE); 853 } else { 854 sdev->status = SBP_DEV_PROBE; 855 sbp_cam_scan_lun(sdev); 856 } 857 xpt_release_devq(sdev->path, 1, TRUE); 858 return; 859} 860 861static void 862sbp_agent_reset_callback(struct fw_xfer *xfer) 863{ 864 struct sbp_dev *sdev; 865 866 sdev = (struct sbp_dev *)xfer->sc; 867SBP_DEBUG(1) 868 sbp_show_sdev_info(sdev, 2); 869 printf("sbp_cmd_callback\n"); 870END_DEBUG 871 fw_xfer_free(xfer); 872 sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 873 if (sdev->path) 874 xpt_release_devq(sdev->path, 1, TRUE); 875} 876 877static void 878sbp_agent_reset(struct sbp_dev *sdev, int attach) 879{ 880 struct fw_xfer *xfer; 881 struct fw_pkt *fp; 882 883SBP_DEBUG(0) 884 sbp_show_sdev_info(sdev, 2); 885 printf("sbp_agent_reset\n"); 886END_DEBUG 887 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 888 if (xfer == NULL) 889 return; 890 if (attach) 891 xfer->act.hand = sbp_do_attach; 892 else 893 xfer->act.hand = sbp_agent_reset_callback; 894 fp = (struct fw_pkt *)xfer->send.buf; 895 fp->mode.wreqq.data = htonl(0xf); 896 fw_asyreq(xfer->fc, -1, xfer); 897} 898 899static void 900sbp_busy_timeout_callback(struct fw_xfer *xfer) 901{ 902 struct sbp_dev *sdev; 903 904 sdev = (struct sbp_dev *)xfer->sc; 905SBP_DEBUG(1) 906 sbp_show_sdev_info(sdev, 2); 907 printf("sbp_but_timeout_callback\n"); 908END_DEBUG 909 fw_xfer_free(xfer); 910 sbp_agent_reset(sdev, 1); 911} 912 913static void 914sbp_busy_timeout(struct sbp_dev *sdev) 915{ 916 struct fw_pkt *fp; 917 struct fw_xfer *xfer; 918SBP_DEBUG(0) 919 sbp_show_sdev_info(sdev, 2); 920 printf("sbp_busy_timeout\n"); 921END_DEBUG 922 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 923 924 xfer->act.hand = sbp_busy_timeout_callback; 925 fp = (struct fw_pkt *)xfer->send.buf; 926 fp->mode.wreqq.dest_hi = htons(0xffff); 927 fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUS_TIME); 928 fp->mode.wreqq.data = htonl(0xf); 929 fw_asyreq(xfer->fc, -1, xfer); 930} 931 932#if 0 933static void 934sbp_reset_start(struct sbp_dev *sdev) 935{ 936 struct fw_xfer *xfer; 937 struct fw_pkt *fp; 938 939SBP_DEBUG(0) 940 sbp_show_sdev_info(sdev, 2); 941 printf("sbp_reset_start\n"); 942END_DEBUG 943 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 944 945 xfer->act.hand = sbp_busy_timeout; 946 fp = (struct fw_pkt *)xfer->send.buf; 947 fp->mode.wreqq.dest_hi = htons(0xffff); 948 fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START); 949 fp->mode.wreqq.data = htonl(0xf); 950 fw_asyreq(xfer->fc, -1, xfer); 951} 952#endif 953 954static void 955sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 956{ 957 struct fw_xfer *xfer; 958 struct fw_pkt *fp; 959SBP_DEBUG(2) 960 sbp_show_sdev_info(sdev, 2); 961 printf("sbp_orb_pointer\n"); 962END_DEBUG 963 964 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 965 if (xfer == NULL) 966 return; 967 xfer->act.hand = sbp_cmd_callback; 968 969 fp = (struct fw_pkt *)xfer->send.buf; 970 fp->mode.wreqb.len = htons(8); 971 fp->mode.wreqb.extcode = 0; 972 fp->mode.wreqb.payload[0] = 973 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 974 fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 975 976 if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 977 fw_xfer_free(xfer); 978 ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 979 xpt_done(ocb->ccb); 980 } 981} 982 983static void 984sbp_doorbell(struct sbp_dev *sdev) 985{ 986 struct fw_xfer *xfer; 987 struct fw_pkt *fp; 988SBP_DEBUG(1) 989 sbp_show_sdev_info(sdev, 2); 990 printf("sbp_doorbell\n"); 991END_DEBUG 992 993 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 994 if (xfer == NULL) 995 return; 996 xfer->act.hand = sbp_cmd_callback; 997 fp = (struct fw_pkt *)xfer->send.buf; 998 fp->mode.wreqq.data = htonl(0xf); 999 fw_asyreq(xfer->fc, -1, xfer); 1000} 1001 1002static struct fw_xfer * 1003sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1004{ 1005 struct fw_xfer *xfer; 1006 struct fw_pkt *fp; 1007 1008 xfer = fw_xfer_alloc(); 1009 if(xfer == NULL){ 1010 return NULL; 1011 } 1012 if (tcode == FWTCODE_WREQQ) 1013 xfer->send.len = 16; 1014 else 1015 xfer->send.len = 24; 1016 1017 xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT); 1018 if(xfer->send.buf == NULL){ 1019 fw_xfer_free( xfer); 1020 return NULL; 1021 } 1022 1023 xfer->send.off = 0; 1024 xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1025 xfer->sc = (caddr_t)sdev; 1026 xfer->fc = sdev->target->sbp->fd.fc; 1027 xfer->retry_req = fw_asybusy; 1028 1029 fp = (struct fw_pkt *)xfer->send.buf; 1030 fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi); 1031 fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset); 1032 fp->mode.wreqq.tlrt = 0; 1033 fp->mode.wreqq.tcode = tcode; 1034 fp->mode.wreqq.pri = 0; 1035 xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst; 1036 fp->mode.wreqq.dst = htons(xfer->dst); 1037 1038 return xfer; 1039 1040} 1041 1042static void 1043sbp_mgm_orb(struct sbp_dev *sdev, int func) 1044{ 1045 struct fw_xfer *xfer; 1046 struct fw_pkt *fp; 1047 struct sbp_ocb *ocb; 1048 int s, nid; 1049 1050 if ((ocb = sbp_get_ocb(sdev->target->sbp)) == NULL) { 1051 s = splfw(); 1052 sdev->target->sbp->flags |= SBP_RESOURCE_SHORTAGE; 1053 splx(s); 1054 return; 1055 } 1056 ocb->flags = OCB_ACT_MGM; 1057 ocb->sdev = sdev; 1058 ocb->ccb = NULL; 1059 1060 nid = sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS; 1061 bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb)); 1062 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1063 ocb->orb[7] = htonl(SBP_DEV2ADDR( 1064 device_get_unit(sdev->target->sbp->fd.dev), 1065 sdev->target->target_id, 1066 sdev->lun_id)); 1067
|
1072 switch (func) { 1073 case ORB_FUN_LGI: 1074 ocb->orb[2] = htonl(nid << 16); 1075 ocb->orb[3] = htonl(vtophys(&sdev->login)); 1076 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id); 1077 ocb->orb[5] = htonl(sizeof(struct sbp_login_res)); 1078 break; 1079 case ORB_FUN_RCN: 1080 case ORB_FUN_LGO: 1081 case ORB_FUN_LUR: 1082 case ORB_FUN_RST: 1083 case ORB_FUN_ATA: 1084 case ORB_FUN_ATS: 1085 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id); 1086 break; 1087 } 1088 1089 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1090 if(xfer == NULL){ 1091 return; 1092 } 1093 xfer->act.hand = sbp_login_callback; 1094 1095 fp = (struct fw_pkt *)xfer->send.buf; 1096 fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi); 1097 fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo); 1098 fp->mode.wreqb.len = htons(8); 1099 fp->mode.wreqb.extcode = 0; 1100 fp->mode.wreqb.payload[0] = htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1101 fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1102 sbp_enqueue_ocb(sdev, ocb); 1103 1104 fw_asyreq(xfer->fc, -1, xfer); 1105} 1106 1107static void 1108sbp_print_scsi_cmd(struct sbp_ocb *ocb) 1109{ 1110 struct ccb_scsiio *csio; 1111 1112 csio = &ocb->ccb->csio; 1113 printf("%s:%d:%d XPT_SCSI_IO: " 1114 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1115 ", flags: 0x%02x, " 1116 "%db cmd/%db data/%db sense\n", 1117 device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1118 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1119 csio->cdb_io.cdb_bytes[0], 1120 csio->cdb_io.cdb_bytes[1], 1121 csio->cdb_io.cdb_bytes[2], 1122 csio->cdb_io.cdb_bytes[3], 1123 csio->cdb_io.cdb_bytes[4], 1124 csio->cdb_io.cdb_bytes[5], 1125 csio->cdb_io.cdb_bytes[6], 1126 csio->cdb_io.cdb_bytes[7], 1127 csio->cdb_io.cdb_bytes[8], 1128 csio->cdb_io.cdb_bytes[9], 1129 ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1130 csio->cdb_len, csio->dxfer_len, 1131 csio->sense_len); 1132} 1133 1134static void 1135sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1136{ 1137 struct sbp_cmd_status *sbp_cmd_status; 1138 struct scsi_sense_data *sense; 1139 1140 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1141 sense = &ocb->ccb->csio.sense_data; 1142 1143SBP_DEBUG(0) 1144 sbp_print_scsi_cmd(ocb); 1145 /* XXX need decode status */ 1146 sbp_show_sdev_info(ocb->sdev, 2); 1147 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d", 1148 sbp_cmd_status->status, 1149 sbp_cmd_status->sfmt, 1150 sbp_cmd_status->valid, 1151 sbp_cmd_status->s_key, 1152 sbp_cmd_status->s_code, 1153 sbp_cmd_status->s_qlfr, 1154 sbp_status->len 1155 ); 1156#if 0 /* XXX */ 1157 if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) { 1158 printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]); 1159 scsi_sense_desc( 1160 sbp_cmd_status->s_code, 1161 sbp_cmd_status->s_qlfr, 1162 ocb->ccb->ccb_h.path->device->inq_data 1163 ) 1164 } else { 1165 printf("\n"); 1166 } 1167#else 1168 printf("\n"); 1169#endif 1170END_DEBUG 1171 1172 1173 if(sbp_cmd_status->status == SCSI_STATUS_CHECK_COND || 1174 sbp_cmd_status->status == SCSI_STATUS_CMD_TERMINATED){ 1175 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1176 sense->error_code = SSD_CURRENT_ERROR; 1177 }else{ 1178 sense->error_code = SSD_DEFERRED_ERROR; 1179 } 1180 if(sbp_cmd_status->valid) 1181 sense->error_code |= SSD_ERRCODE_VALID; 1182 sense->flags = sbp_cmd_status->s_key; 1183 if(sbp_cmd_status->mark) 1184 sense->flags |= SSD_FILEMARK; 1185 if(sbp_cmd_status->eom) 1186 sense->flags |= SSD_EOM; 1187 if(sbp_cmd_status->ill_len) 1188 sense->flags |= SSD_ILI; 1189 sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff; 1190 sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff; 1191 sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff; 1192 sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff; 1193 if (sbp_status->len <= 1) 1194 /* XXX not scsi status. shouldn't be happened */ 1195 sense->extra_len = 0; 1196 else if (sbp_status->len <= 4) 1197 /* add_sense_code(_qual), info, cmd_spec_info */ 1198 sense->extra_len = 6; 1199 else 1200 /* fru, sense_key_spec */ 1201 sense->extra_len = 10; 1202 sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff; 1203 sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff; 1204 sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff; 1205 sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff; 1206 sense->add_sense_code = sbp_cmd_status->s_code; 1207 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1208 sense->fru = sbp_cmd_status->fru; 1209 sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff; 1210 sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff; 1211 sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff; 1212 1213 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1214 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1215 | CAM_AUTOSNS_VALID; 1216/* 1217{ 1218 u_int8_t j, *tmp; 1219 tmp = sense; 1220 for( j = 0 ; j < 32 ; j+=8){ 1221 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1222 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1223 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1224 } 1225 1226} 1227*/ 1228 } else { 1229 printf("sbp_scsi_status: unknown scsi status\n"); 1230 } 1231} 1232 1233static void 1234sbp_fix_inq_data(struct sbp_ocb *ocb) 1235{ 1236 union ccb *ccb; 1237 struct sbp_dev *sdev; 1238 struct scsi_inquiry_data *inq; 1239 1240 ccb = ocb->ccb; 1241 sdev = ocb->sdev; 1242 1243 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1244 return; 1245SBP_DEBUG(1) 1246 sbp_show_sdev_info(sdev, 2); 1247 printf("sbp_fix_inq_data\n"); 1248END_DEBUG 1249 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1250 switch (SID_TYPE(inq)) { 1251 case T_DIRECT: 1252 /* 1253 * XXX Convert Direct Access device to RBC. 1254 * I've never seen Firewire DA devices which support READ_6. 1255 */ 1256#if 1 1257 if (SID_TYPE(inq) == T_DIRECT) 1258 inq->device |= T_RBC; /* T_DIRECT == 0 */ 1259#endif 1260 /* fall through */ 1261 case T_RBC: 1262 /* disable tag queuing */ 1263 inq->flags &= ~SID_CmdQue; 1264 /* 1265 * Override vendor/product/revision information. 1266 * Some devices sometimes return strange strings. 1267 */ 1268 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1269 bcopy(sdev->product, inq->product, sizeof(inq->product)); 1270 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1271 break; 1272 } 1273} 1274 1275static void 1276sbp_recv1(struct fw_xfer *xfer){ 1277 struct fw_pkt *rfp; 1278#if NEED_RESPONSE 1279 struct fw_pkt *sfp; 1280#endif 1281 struct sbp_softc *sbp; 1282 struct sbp_dev *sdev; 1283 struct sbp_ocb *ocb; 1284 struct sbp_login_res *login_res = NULL; 1285 struct sbp_status *sbp_status; 1286 struct sbp_target *target; 1287 int orb_fun, status_valid; 1288 u_int32_t addr; 1289/* 1290 u_int32_t *ld; 1291 ld = xfer->recv.buf; 1292printf("sbp %x %d %d %08x %08x %08x %08x\n", 1293 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1294printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1295printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1296*/ 1297 if(xfer->resp != 0){ 1298 printf("sbp_recv: xfer->resp != 0\n"); 1299 fw_xfer_free( xfer); 1300 return; 1301 } 1302 if(xfer->recv.buf == NULL){ 1303 printf("sbp_recv: xfer->recv.buf == NULL\n"); 1304 fw_xfer_free( xfer); 1305 return; 1306 } 1307 sbp = (struct sbp_softc *)xfer->sc; 1308 rfp = (struct fw_pkt *)xfer->recv.buf; 1309 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1310 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1311 fw_xfer_free( xfer); 1312 return; 1313 } 1314 sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload; 1315 addr = ntohl(rfp->mode.wreqb.dest_lo); 1316SBP_DEBUG(2) 1317 printf("received address 0x%x\n", addr); 1318END_DEBUG 1319 target = &sbp->targets[SBP_ADDR2TRG(addr)]; 1320 sdev = &target->luns[SBP_ADDR2LUN(addr)]; 1321 1322 status_valid = (sbp_status->resp == ORB_RES_CMPL 1323 && sbp_status->dead == 0 1324 && sbp_status->status == 0); 1325 1326SBP_DEBUG(0) 1327 if (!status_valid || debug > 1){ 1328 int status; 1329 1330 sbp_show_sdev_info(sdev, 2); 1331 printf("ORB status src:%x resp:%x dead:%x" 1332 " len:%x stat:%x orb:%x%08x\n", 1333 sbp_status->src, sbp_status->resp, sbp_status->dead, 1334 sbp_status->len, sbp_status->status, 1335 ntohl(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1336 sbp_show_sdev_info(sdev, 2); 1337 status = sbp_status->status; 1338 switch(sbp_status->resp) { 1339 case 0: 1340 if (status > MAX_ORB_STATUS0) 1341 printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1342 else 1343 printf("%s\n", orb_status0[status]); 1344 break; 1345 case 1: 1346 printf("Object: %s, Serial Bus Error: %s\n", 1347 orb_status1_object[(status>>6) & 3], 1348 orb_status1_serial_bus_error[status & 0xf]); 1349 break; 1350 default: 1351 printf("unknown respose code\n"); 1352 } 1353 } 1354END_DEBUG 1355 ocb = sbp_dequeue_ocb(sdev, ntohl(sbp_status->orb_lo)); 1356 1357 /* we have to reset the fetch agent if it's dead */ 1358 if (sbp_status->dead) { 1359 if (sdev->path) 1360 xpt_freeze_devq(sdev->path, 1); 1361 sbp_agent_reset(sdev, 0); 1362 } 1363 1364 1365 if (ocb == NULL) { 1366 printf("No ocb on the queue for target %d.\n", sdev->target->target_id); 1367 fw_xfer_free( xfer); 1368 return; 1369 } 1370 1371 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1372 case ORB_FMT_NOP: 1373 break; 1374 case ORB_FMT_VED: 1375 break; 1376 case ORB_FMT_STD: 1377 switch(ocb->flags & OCB_ACT_MASK){ 1378 case OCB_ACT_MGM: 1379 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1380 switch(orb_fun) { 1381 case ORB_FUN_LGI: 1382 login_res = &sdev->login; 1383 login_res->len = ntohs(login_res->len); 1384 login_res->id = ntohs(login_res->id); 1385 login_res->cmd_hi = ntohs(login_res->cmd_hi); 1386 login_res->cmd_lo = ntohl(login_res->cmd_lo); 1387 if (status_valid) { 1388SBP_DEBUG(0) 1389sbp_show_sdev_info(sdev, 2); 1390printf("login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo, ntohs(login_res->recon_hold)); 1391END_DEBUG 1392#if 1 1393 sbp_busy_timeout(sdev); 1394#else 1395 sbp_mgm_orb(sdev, ORB_FUN_ATS); 1396#endif 1397 } else { 1398 /* forgot logout ? */ 1399 printf("login failed\n"); 1400 sdev->status = SBP_DEV_RESET; 1401 } 1402 break; 1403 case ORB_FUN_RCN: 1404 login_res = &sdev->login; 1405 if (status_valid) { 1406 sdev->status = SBP_DEV_ATTACHED; 1407SBP_DEBUG(0) 1408sbp_show_sdev_info(sdev, 2); 1409printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1410END_DEBUG 1411#if 1 1412 sbp_ping_unit(sdev); 1413 xpt_release_devq(sdev->path, 1, TRUE); 1414#else 1415 sbp_mgm_orb(sdev, ORB_FUN_ATS); 1416#endif 1417 } else { 1418 /* reconnection hold time exceed? */ 1419 printf("reconnect failed\n"); 1420 sbp_mgm_orb(sdev, ORB_FUN_LGI); 1421 } 1422 break; 1423 case ORB_FUN_LGO: 1424 sdev->status = SBP_DEV_RESET; 1425 break; 1426 case ORB_FUN_LUR: 1427 case ORB_FUN_RST: 1428 case ORB_FUN_ATA: 1429 case ORB_FUN_ATS: 1430 if (sdev->status == SBP_DEV_ATTACHED) { 1431 xpt_release_devq(sdev->path, 1, TRUE); 1432 } else { 1433 sbp_busy_timeout(sdev); 1434 } 1435 break; 1436 default: 1437 break; 1438 } 1439 break; 1440 case OCB_ACT_CMD: 1441 if(ocb->ccb != NULL){ 1442 union ccb *ccb; 1443/* 1444 u_int32_t *ld; 1445 ld = ocb->ccb->csio.data_ptr; 1446 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1447 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1448 else 1449 printf("ptr NULL\n"); 1450printf("len %d\n", sbp_status->len); 1451*/ 1452 ccb = ocb->ccb; 1453 if(sbp_status->len > 1){ 1454 sbp_scsi_status(sbp_status, ocb); 1455 }else{ 1456 if(sbp_status->resp != ORB_RES_CMPL){ 1457 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1458 }else{ 1459 ccb->ccb_h.status = CAM_REQ_CMP; 1460 } 1461 } 1462 /* fix up inq data */ 1463 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1464 sbp_fix_inq_data(ocb); 1465 xpt_done(ccb); 1466 } 1467 break; 1468 default: 1469 break; 1470 } 1471 } 1472 1473 if (!(ocb->flags & OCB_RESERVED)) 1474 sbp_free_ocb(sbp, ocb); 1475 1476/* The received packet is usually small enough to be stored within 1477 * the buffer. In that case, the controller return ack_complete and 1478 * no respose is necessary. 1479 * 1480 * XXX fwohci.c and firewire.c should inform event_code such as 1481 * ack_complete or ack_pending to upper driver. 1482 */ 1483#if NEED_RESPONSE 1484 xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO); 1485 xfer->send.len = 12; 1486 xfer->send.off = 0; 1487 sfp = (struct fw_pkt *)xfer->send.buf; 1488 sfp->mode.wres.dst = rfp->mode.wreqb.src; 1489 xfer->dst = ntohs(sfp->mode.wres.dst); 1490 xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1491 xfer->act.hand = sbp_loginres_callback; 1492 xfer->retry_req = fw_asybusy; 1493 1494 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1495 sfp->mode.wres.tcode = FWTCODE_WRES; 1496 sfp->mode.wres.rtcode = 0; 1497 sfp->mode.wres.pri = 0; 1498 1499 fw_asyreq(xfer->fc, -1, xfer); 1500#else 1501 fw_xfer_free(xfer); 1502#endif 1503 1504 return; 1505 1506} 1507 1508static void 1509sbp_recv(struct fw_xfer *xfer) 1510{ 1511 int s; 1512 1513 s = splcam(); 1514 sbp_recv1(xfer); 1515 splx(s); 1516} 1517/* 1518 * sbp_attach() 1519 */ 1520static int 1521sbp_attach(device_t dev) 1522{ 1523 struct sbp_softc *sbp; 1524 struct cam_devq *devq; 1525 struct fw_xfer *xfer; 1526 int i, s, error; 1527 1528SBP_DEBUG(0) 1529 printf("sbp_attach\n"); 1530END_DEBUG 1531 1532 sbp = ((struct sbp_softc *)device_get_softc(dev)); 1533 bzero(sbp, sizeof(struct sbp_softc)); 1534 sbp->fd.dev = dev; 1535 sbp->fd.fc = device_get_ivars(dev); 1536 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 1537 /*boundary*/0, 1538 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1539 /*highaddr*/BUS_SPACE_MAXADDR, 1540 /*filter*/NULL, /*filterarg*/NULL, 1541 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1542 /*maxsegsz*/0x8000, 1543 /*flags*/BUS_DMA_ALLOCNOW, 1544 &sbp->dmat); 1545 if (error != 0) { 1546 printf("sbp_attach: Could not allocate DMA tag " 1547 "- error %d\n", error); 1548 return (ENOMEM); 1549 } 1550 1551 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1552 if (devq == NULL) 1553 return (ENXIO); 1554 1555 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1556 sbp->targets[i].fwdev = NULL; 1557 sbp->targets[i].luns = NULL; 1558 } 1559 1560 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1561 device_get_unit(dev), 1562 /*untagged*/ SBP_QUEUE_LEN, 1563 /*tagged*/0, devq); 1564 1565 if (sbp->sim == NULL) { 1566 cam_simq_free(devq); 1567 return (ENXIO); 1568 } 1569 1570 sbp->ocb = (struct sbp_ocb *) contigmalloc( 1571 sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1572 M_SBP, M_DONTWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 1573 bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB); 1574 1575 if (sbp->ocb == NULL) { 1576 printf("sbp0: ocb alloction failure\n"); 1577 return (ENOMEM); 1578 } 1579 1580 STAILQ_INIT(&sbp->free_ocbs); 1581 for (i = 0; i < SBP_NUM_OCB; i++) { 1582 sbp_free_ocb(sbp, &sbp->ocb[i]); 1583 } 1584 1585 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) { 1586 cam_sim_free(sbp->sim, /*free_devq*/TRUE); 1587 contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1588 M_SBP); 1589 return (ENXIO); 1590 } 1591 1592 xfer = fw_xfer_alloc(); 1593 xfer->act.hand = sbp_recv; 1594 xfer->act_type = FWACT_XFER; 1595#if NEED_RESPONSE 1596 xfer->fc = sbp->fd.fc; 1597#endif 1598 xfer->sc = (caddr_t)sbp; 1599 1600 sbp->fwb.start_hi = SBP_BIND_HI; 1601 sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0); 1602 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1603 sbp->fwb.addrlen = 0xffff; 1604 sbp->fwb.xfer = xfer; 1605 fw_bindadd(sbp->fd.fc, &sbp->fwb); 1606 1607 sbp->fd.post_explore = sbp_post_explore; 1608 s = splfw(); 1609 sbp_post_explore((void *)sbp); 1610 splx(s); 1611 1612 return (0); 1613} 1614 1615static int 1616sbp_detach(device_t dev) 1617{ 1618 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1619 struct firewire_comm *fc = sbp->fd.fc; 1620 int i; 1621 1622SBP_DEBUG(0) 1623 printf("sbp_detach\n"); 1624END_DEBUG 1625 1626 /* bus reset for logout */ 1627 sbp->fd.post_explore = NULL; 1628 fc->ibr(fc); 1629 1630 contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1631 fw_bindremove(fc, &sbp->fwb); 1632 for (i = 0; i < SBP_NUM_TARGETS; i ++) 1633 sbp_detach_target(&sbp->targets[i]); 1634 xpt_bus_deregister(cam_sim_path(sbp->sim)); 1635 bus_dma_tag_destroy(sbp->dmat); 1636 return (0); 1637} 1638 1639static void 1640sbp_detach_target(struct sbp_target *target) 1641{ 1642 int i; 1643 struct sbp_dev *sdev; 1644 1645 if (target->luns != NULL) { 1646 printf("sbp_detach_target %d\n", target->target_id); 1647 for (i=0; i < target->num_lun; i++) { 1648 sdev = &target->luns[i]; 1649 if (sdev->status == SBP_DEV_RESET || 1650 sdev->status == SBP_DEV_DEAD) 1651 continue; 1652 if (sdev->path) 1653 xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 1654 xpt_free_path(sdev->path); 1655 sdev->path = NULL; 1656 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 1657 } 1658 free(target->luns, M_SBP); 1659 target->luns = NULL; 1660 } 1661 target->fwdev = NULL; 1662} 1663 1664static void 1665sbp_timeout(void *arg) 1666{ 1667 struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 1668 struct sbp_dev *sdev = ocb->sdev; 1669 int s; 1670 1671 sbp_show_sdev_info(sdev, 2); 1672 printf("request timeout ... requeue\n"); 1673 1674 /* XXX need reset? */ 1675 1676 s = splfw(); 1677 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 1678 splx(s); 1679 return; 1680} 1681 1682static void 1683sbp_action1(struct cam_sim *sim, union ccb *ccb) 1684{ 1685 1686 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 1687 struct sbp_target *target = NULL; 1688 struct sbp_dev *sdev = NULL; 1689 1690 /* target:lun -> sdev mapping */ 1691 if (sbp != NULL 1692 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 1693 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 1694 target = &sbp->targets[ccb->ccb_h.target_id]; 1695 if (target->fwdev != NULL 1696 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 1697 && ccb->ccb_h.target_lun < target->num_lun) { 1698 sdev = &target->luns[ccb->ccb_h.target_lun]; 1699 if (sdev->status != SBP_DEV_ATTACHED && 1700 sdev->status != SBP_DEV_PROBE) 1701 sdev = NULL; 1702 } 1703 } 1704 1705SBP_DEBUG(1) 1706 if (sdev == NULL) 1707 printf("invalid target %d lun %d\n", 1708 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1709END_DEBUG 1710 1711 switch (ccb->ccb_h.func_code) { 1712 case XPT_SCSI_IO: 1713 case XPT_RESET_DEV: 1714 case XPT_GET_TRAN_SETTINGS: 1715 case XPT_SET_TRAN_SETTINGS: 1716 case XPT_CALC_GEOMETRY: 1717 if (sdev == NULL) { 1718SBP_DEBUG(1) 1719 printf("%s:%d:%d:func_code 0x%04x: " 1720 "Invalid target (target needed)\n", 1721 device_get_nameunit(sbp->fd.dev), 1722 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1723 ccb->ccb_h.func_code); 1724END_DEBUG 1725 1726 ccb->ccb_h.status = CAM_TID_INVALID; 1727 xpt_done(ccb); 1728 return; 1729 } 1730 break; 1731 case XPT_PATH_INQ: 1732 case XPT_NOOP: 1733 /* The opcodes sometimes aimed at a target (sc is valid), 1734 * sometimes aimed at the SIM (sc is invalid and target is 1735 * CAM_TARGET_WILDCARD) 1736 */ 1737 if (sbp == NULL && 1738 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 1739SBP_DEBUG(0) 1740 printf("%s:%d:%d func_code 0x%04x: " 1741 "Invalid target (no wildcard)\n", 1742 device_get_nameunit(sbp->fd.dev), 1743 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1744 ccb->ccb_h.func_code); 1745END_DEBUG 1746 ccb->ccb_h.status = CAM_TID_INVALID; 1747 xpt_done(ccb); 1748 return; 1749 } 1750 break; 1751 default: 1752 /* XXX Hm, we should check the input parameters */ 1753 break; 1754 } 1755 1756 switch (ccb->ccb_h.func_code) { 1757 case XPT_SCSI_IO: 1758 { 1759 struct ccb_scsiio *csio; 1760 struct sbp_ocb *ocb; 1761 int s, speed; 1762 void *cdb; 1763 1764 csio = &ccb->csio; 1765 1766SBP_DEBUG(1) 1767 printf("%s:%d:%d XPT_SCSI_IO: " 1768 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1769 ", flags: 0x%02x, " 1770 "%db cmd/%db data/%db sense\n", 1771 device_get_nameunit(sbp->fd.dev), 1772 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1773 csio->cdb_io.cdb_bytes[0], 1774 csio->cdb_io.cdb_bytes[1], 1775 csio->cdb_io.cdb_bytes[2], 1776 csio->cdb_io.cdb_bytes[3], 1777 csio->cdb_io.cdb_bytes[4], 1778 csio->cdb_io.cdb_bytes[5], 1779 csio->cdb_io.cdb_bytes[6], 1780 csio->cdb_io.cdb_bytes[7], 1781 csio->cdb_io.cdb_bytes[8], 1782 csio->cdb_io.cdb_bytes[9], 1783 ccb->ccb_h.flags & CAM_DIR_MASK, 1784 csio->cdb_len, csio->dxfer_len, 1785 csio->sense_len); 1786END_DEBUG 1787 if(sdev == NULL){ 1788 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1789 xpt_done(ccb); 1790 return; 1791 } 1792#if 0 1793 /* if we are in probe stage, pass only probe commands */ 1794 if (sdev->status == SBP_DEV_PROBE) { 1795 char *name; 1796 name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 1797 printf("probe stage, periph name: %s\n", name); 1798 if (strcmp(name, "probe") != 0) { 1799 ccb->ccb_h.status = CAM_REQUEUE_REQ; 1800 xpt_done(ccb); 1801 return; 1802 } 1803 } 1804#endif 1805 if ((ocb = sbp_get_ocb(sbp)) == NULL) { 1806 s = splfw(); 1807 sbp->flags |= SBP_RESOURCE_SHORTAGE; 1808 splx(s); 1809 return; 1810 } 1811 ocb->flags = OCB_ACT_CMD; 1812 ocb->sdev = sdev; 1813 ocb->ccb = ccb; 1814 ccb->ccb_h.ccb_sdev_ptr = sdev; 1815 ocb->orb[0] = htonl(1 << 31); 1816 ocb->orb[1] = 0; 1817 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 1818 ocb->orb[3] = htonl(vtophys(ocb->ind_ptr)); 1819 speed = min(target->fwdev->speed, max_speed); 1820 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 1821 | ORB_CMD_MAXP(speed + 7)); 1822 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 1823 ocb->orb[4] |= htonl(ORB_CMD_IN); 1824 } 1825 1826 if (csio->ccb_h.flags & CAM_SCATTER_VALID) 1827 printf("sbp: CAM_SCATTER_VALID\n"); 1828 if (csio->ccb_h.flags & CAM_DATA_PHYS) 1829 printf("sbp: CAM_DATA_PHYS\n"); 1830 1831 if (csio->ccb_h.flags & CAM_CDB_POINTER) 1832 cdb = (void *)csio->cdb_io.cdb_ptr; 1833 else 1834 cdb = (void *)&csio->cdb_io.cdb_bytes; 1835 bcopy(cdb, 1836 (void *)(uintptr_t)(volatile void *)&ocb->orb[5], 1837 csio->cdb_len); 1838/* 1839printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 1840printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 1841*/ 1842 if (ccb->csio.dxfer_len > 0) { 1843 int s; 1844 1845 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 1846 printf("sbp_action1: cannot create dmamap\n"); 1847 break; 1848 } 1849 1850 s = splsoftvm(); 1851 bus_dmamap_load(/*dma tag*/sbp->dmat, 1852 /*dma map*/ocb->dmamap, 1853 ccb->csio.data_ptr, 1854 ccb->csio.dxfer_len, 1855 sbp_execute_ocb, 1856 ocb, 1857 /*flags*/0); 1858 splx(s); 1859 } else 1860 sbp_execute_ocb(ocb, NULL, 0, 0); 1861 break; 1862 } 1863 case XPT_CALC_GEOMETRY: 1864 { 1865 struct ccb_calc_geometry *ccg; 1866 u_int32_t size_mb; 1867 u_int32_t secs_per_cylinder; 1868 int extended = 1; 1869 ccg = &ccb->ccg; 1870 1871 if (ccg->block_size == 0) { 1872 printf("sbp_action1: block_size is 0.\n"); 1873 ccb->ccb_h.status = CAM_REQ_INVALID; 1874 xpt_done(ccb); 1875 break; 1876 } 1877SBP_DEBUG(1) 1878 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 1879 "Volume size = %d\n", 1880 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim), 1881 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1882 ccg->volume_size); 1883END_DEBUG 1884 1885 size_mb = ccg->volume_size 1886 / ((1024L * 1024L) / ccg->block_size); 1887 1888 if (size_mb >= 1024 && extended) { 1889 ccg->heads = 255; 1890 ccg->secs_per_track = 63; 1891 } else { 1892 ccg->heads = 64; 1893 ccg->secs_per_track = 32; 1894 } 1895 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 1896 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 1897 ccb->ccb_h.status = CAM_REQ_CMP; 1898 xpt_done(ccb); 1899 break; 1900 } 1901 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1902 { 1903 1904SBP_DEBUG(1) 1905 printf("%s:%d:XPT_RESET_BUS: \n", 1906 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 1907END_DEBUG 1908 1909 ccb->ccb_h.status = CAM_REQ_INVALID; 1910 xpt_done(ccb); 1911 break; 1912 } 1913 case XPT_PATH_INQ: /* Path routing inquiry */ 1914 { 1915 struct ccb_pathinq *cpi = &ccb->cpi; 1916 1917SBP_DEBUG(1) 1918 printf("%s:%d:%d XPT_PATH_INQ:.\n", 1919 device_get_nameunit(sbp->fd.dev), 1920 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1921END_DEBUG 1922 cpi->version_num = 1; /* XXX??? */ 1923 cpi->hba_inquiry = 0; 1924 cpi->target_sprt = 0; 1925 cpi->hba_misc = 0; 1926 cpi->hba_eng_cnt = 0; 1927 cpi->max_target = SBP_NUM_TARGETS - 1; 1928 cpi->max_lun = SBP_NUM_LUNS - 1; 1929 cpi->initiator_id = SBP_INITIATOR; 1930 cpi->bus_id = sim->bus_id; 1931 cpi->base_transfer_speed = 400 * 1000 / 8; 1932 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1933 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 1934 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 1935 cpi->unit_number = sim->unit_number; 1936 1937 cpi->ccb_h.status = CAM_REQ_CMP; 1938 xpt_done(ccb); 1939 break; 1940 } 1941 case XPT_GET_TRAN_SETTINGS: 1942 { 1943 struct ccb_trans_settings *cts = &ccb->cts; 1944SBP_DEBUG(1) 1945 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 1946 device_get_nameunit(sbp->fd.dev), 1947 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1948END_DEBUG 1949 /* Disable disconnect and tagged queuing */ 1950 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID; 1951 cts->flags = 0; 1952 1953 cts->ccb_h.status = CAM_REQ_CMP; 1954 xpt_done(ccb); 1955 break; 1956 } 1957 case XPT_ABORT: 1958 ccb->ccb_h.status = CAM_UA_ABORT; 1959 xpt_done(ccb); 1960 break; 1961 default: 1962 ccb->ccb_h.status = CAM_REQ_INVALID; 1963 xpt_done(ccb); 1964 break; 1965 } 1966 return; 1967} 1968 1969static void 1970sbp_action(struct cam_sim *sim, union ccb *ccb) 1971{ 1972 int s; 1973 1974 s = splfw(); 1975 sbp_action1(sim, ccb); 1976 splx(s); 1977} 1978 1979static void 1980sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 1981{ 1982 int i; 1983 struct sbp_ocb *ocb; 1984 struct sbp_ocb *prev; 1985 union ccb *ccb; 1986 bus_dma_segment_t *s; 1987 1988 if (error) 1989 printf("sbp_execute_ocb: error=%d\n", error); 1990 1991 ocb = (struct sbp_ocb *)arg; 1992 if (seg == 1) { 1993 /* direct pointer */ 1994 ocb->orb[3] = htonl(segments[0].ds_addr); 1995 ocb->orb[4] |= htonl(segments[0].ds_len); 1996 } else if(seg > 1) { 1997 /* page table */ 1998SBP_DEBUG(1) 1999 printf("sbp_execute_ocb: seg %d", seg); 2000 for (i = 0; i < seg; i++) 2001 printf(", %tx:%zd", segments[i].ds_addr, 2002 segments[i].ds_len); 2003 printf("\n"); 2004END_DEBUG 2005 for (i = 0; i < seg; i++) { 2006 s = &segments[i]; 2007#if 1 /* XXX LSI Logic "< 16 byte" bug might be hit */ 2008 if (s->ds_len < 16) 2009 printf("sbp_execute_ocb: warning, " 2010 "segment length(%zd) is less than 16." 2011 "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2012#endif 2013 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2014 ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2015 } 2016 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2017 } 2018 2019 ccb = ocb->ccb; 2020 prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2021 if (prev) 2022 sbp_doorbell(ocb->sdev); 2023 else 2024 sbp_orb_pointer(ocb->sdev, ocb); 2025} 2026 2027static void 2028sbp_poll(struct cam_sim *sim) 2029{ 2030 /* should call fwohci_intr? */ 2031 return; 2032} 2033static struct sbp_ocb * 2034sbp_dequeue_ocb(struct sbp_dev *sdev, u_int32_t orb_lo) 2035{ 2036 struct sbp_ocb *ocb; 2037 struct sbp_ocb *next; 2038 int s = splfw(), order = 0; 2039 int flags; 2040 2041 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2042 next = STAILQ_NEXT(ocb, ocb); 2043 flags = ocb->flags; 2044SBP_DEBUG(1) 2045 printf("orb: 0x%tx next: 0x%x, flags %x\n", 2046 vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags); 2047END_DEBUG 2048 if (vtophys(&ocb->orb[0]) == orb_lo) { 2049 /* found */ 2050 if (ocb->flags & OCB_RESERVED) 2051 ocb->flags |= OCB_DONE; 2052 else 2053 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2054 if (ocb->ccb != NULL) 2055 untimeout(sbp_timeout, (caddr_t)ocb, 2056 ocb->ccb->ccb_h.timeout_ch); 2057 if (ocb->dmamap != NULL) { 2058 bus_dmamap_destroy(sdev->target->sbp->dmat, 2059 ocb->dmamap); 2060 ocb->dmamap = NULL; 2061 } 2062 break; 2063 } else { 2064 if ((ocb->flags & OCB_RESERVED) && 2065 (ocb->flags & OCB_DONE)) { 2066 /* next orb must be fetched already */ 2067 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2068 sbp_free_ocb(sdev->target->sbp, ocb); 2069 } else 2070 order ++; 2071 } 2072 } 2073 splx(s); 2074SBP_DEBUG(0) 2075 if (ocb && order > 0) { 2076 sbp_show_sdev_info(sdev, 2); 2077 printf("unordered execution order:%d\n", order); 2078 } 2079END_DEBUG 2080 return (ocb); 2081} 2082 2083static struct sbp_ocb * 2084sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2085{ 2086 int s = splfw(); 2087 struct sbp_ocb *prev; 2088 2089SBP_DEBUG(2) 2090 sbp_show_sdev_info(sdev, 2); 2091 printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0])); 2092END_DEBUG 2093 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2094 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2095 2096 if (ocb->ccb != NULL) 2097 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2098 (ocb->ccb->ccb_h.timeout * hz) / 1000); 2099 2100 if (prev != NULL 2101 && ((prev->flags & OCB_ACT_MASK) == OCB_ACT_CMD) 2102 && ((ocb->flags & OCB_ACT_MASK) == OCB_ACT_CMD)) { 2103SBP_DEBUG(1) 2104 printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]), 2105 vtophys(&ocb->orb[0])); 2106END_DEBUG 2107 prev->flags |= OCB_RESERVED; 2108 prev->orb[1] = htonl(vtophys(&ocb->orb[0])); 2109 prev->orb[0] = 0; 2110 } else { 2111 prev = NULL; 2112 } 2113 splx(s); 2114 2115 return prev; 2116} 2117 2118static struct sbp_ocb * 2119sbp_get_ocb(struct sbp_softc *sbp) 2120{ 2121 struct sbp_ocb *ocb; 2122 int s = splfw(); 2123 ocb = STAILQ_FIRST(&sbp->free_ocbs); 2124 if (ocb == NULL) { 2125 printf("ocb shortage!!!\n"); 2126 return NULL; 2127 } 2128 STAILQ_REMOVE(&sbp->free_ocbs, ocb, sbp_ocb, ocb); 2129 splx(s); 2130 ocb->ccb = NULL; 2131 return (ocb); 2132} 2133 2134static void 2135sbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb) 2136{ 2137#if 0 /* XXX make sure that ocb has ccb */ 2138 if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 && 2139 (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 2140 ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2141 sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2142 } 2143#else 2144 if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0) 2145 sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2146#endif 2147 ocb->flags = 0; 2148 ocb->ccb = NULL; 2149 STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb); 2150} 2151 2152static void 2153sbp_abort_ocb(struct sbp_ocb *ocb, int status) 2154{ 2155 struct sbp_dev *sdev; 2156 2157 sdev = ocb->sdev; 2158SBP_DEBUG(0) 2159 sbp_show_sdev_info(sdev, 2); 2160 printf("sbp_abort_ocb 0x%x\n", status); 2161 if (ocb->ccb != NULL) 2162 sbp_print_scsi_cmd(ocb); 2163END_DEBUG 2164 if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) { 2165 if (status != CAM_CMD_TIMEOUT) 2166 untimeout(sbp_timeout, (caddr_t)ocb, 2167 ocb->ccb->ccb_h.timeout_ch); 2168 ocb->ccb->ccb_h.status = status; 2169 xpt_done(ocb->ccb); 2170 } 2171 if (ocb->dmamap != NULL) { 2172 bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap); 2173 ocb->dmamap = NULL; 2174 } 2175 sbp_free_ocb(sdev->target->sbp, ocb); 2176} 2177 2178static void 2179sbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2180{ 2181 int s; 2182 struct sbp_ocb *ocb, *next; 2183 STAILQ_HEAD(, sbp_ocb) temp; 2184 2185 s = splfw(); 2186 2187 bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2188 STAILQ_INIT(&sdev->ocbs); 2189 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2190 next = STAILQ_NEXT(ocb, ocb); 2191 sbp_abort_ocb(ocb, status); 2192 } 2193 2194 splx(s); 2195} 2196 2197static devclass_t sbp_devclass; 2198 2199static device_method_t sbp_methods[] = { 2200 /* device interface */ 2201 DEVMETHOD(device_identify, sbp_identify), 2202 DEVMETHOD(device_probe, sbp_probe), 2203 DEVMETHOD(device_attach, sbp_attach), 2204 DEVMETHOD(device_detach, sbp_detach), 2205 2206 { 0, 0 } 2207}; 2208 2209static driver_t sbp_driver = { 2210 "sbp", 2211 sbp_methods, 2212 sizeof(struct sbp_softc), 2213}; 2214DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2215MODULE_VERSION(sbp, 1); 2216MODULE_DEPEND(sbp, firewire, 1, 1, 1); 2217MODULE_DEPEND(sbp, cam, 1, 1, 1);
|