1 /* 2 * Copyright (c) 1998,1999,2000,2001 Katsushi Kobayashi and Hidetosh Shimokawa 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the acknowledgement as bellow: 15 * 16 * This product includes software developed by K. Kobayashi and H. Shimokawa 17 * 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $FreeBSD$ 34 * 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/mbuf.h> 42 #include <sys/sysctl.h> 43 #include <machine/bus.h> 44 #include <sys/malloc.h> 45 46 #if __FreeBSD_version < 500106 47 #include <sys/devicestat.h> /* for struct devstat */ 48 #endif 49 50 #include <cam/cam.h> 51 #include <cam/cam_ccb.h> 52 #include <cam/cam_sim.h> 53 #include <cam/cam_xpt_sim.h> 54 #include <cam/cam_debug.h> 55 #include <cam/cam_periph.h> 56 57 #include <cam/scsi/scsi_all.h> 58 #include <cam/scsi/scsi_message.h> 59 #include <cam/scsi/scsi_da.h> 60 61 #include <sys/kernel.h> 62 63 #include <vm/vm.h> 64 #include <vm/pmap.h> 65 66 #include <dev/firewire/firewire.h> 67 #include <dev/firewire/firewirereg.h> 68 #include <dev/firewire/iec13213.h> 69 70 #define ccb_sdev_ptr spriv_ptr0 71 #define ccb_sbp_ptr spriv_ptr1 72 73 #define SBP_NUM_TARGETS 8 /* MAX 64 */ 74 #define SBP_NUM_LUNS 8 /* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */ 75 #define SBP_QUEUE_LEN 4 76 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS) 77 #define SBP_INITIATOR 7 78 79 #define LOGIN_DELAY 2 80 81 /* 82 * STATUS FIFO addressing 83 * bit 84 * ----------------------- 85 * 0- 1( 2): 0 (alingment) 86 * 2- 7( 6): target 87 * 8-15( 8): lun 88 * 16-23( 8): unit 89 * 24-31( 8): reserved 90 * 32-47(16): SBP_BIND_HI 91 * 48-64(16): bus_id, node_id 92 */ 93 #define SBP_BIND_HI 0x1 94 #define SBP_DEV2ADDR(u, t, l) \ 95 ((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2)) 96 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f) 97 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff) 98 99 #define ORB_NOTIFY (1 << 31) 100 #define ORB_FMT_STD (0 << 29) 101 #define ORB_FMT_VED (2 << 29) 102 #define ORB_FMT_NOP (3 << 29) 103 #define ORB_FMT_MSK (3 << 29) 104 #define ORB_EXV (1 << 28) 105 /* */ 106 #define ORB_CMD_IN (1 << 27) 107 /* */ 108 #define ORB_CMD_SPD(x) ((x) << 24) 109 #define ORB_CMD_MAXP(x) ((x) << 20) 110 #define ORB_RCN_TMO(x) ((x) << 20) 111 #define ORB_CMD_PTBL (1 << 19) 112 #define ORB_CMD_PSZ(x) ((x) << 16) 113 114 #define ORB_FUN_LGI (0 << 16) 115 #define ORB_FUN_QLG (1 << 16) 116 #define ORB_FUN_RCN (3 << 16) 117 #define ORB_FUN_LGO (7 << 16) 118 #define ORB_FUN_ATA (0xb << 16) 119 #define ORB_FUN_ATS (0xc << 16) 120 #define ORB_FUN_LUR (0xe << 16) 121 #define ORB_FUN_RST (0xf << 16) 122 #define ORB_FUN_MSK (0xf << 16) 123 #define ORB_FUN_RUNQUEUE 0xffff 124 125 static char *orb_fun_name[] = { 126 /* 0 */ "LOGIN", 127 /* 1 */ "QUERY LOGINS", 128 /* 2 */ "Reserved", 129 /* 3 */ "RECONNECT", 130 /* 4 */ "SET PASSWORD", 131 /* 5 */ "Reserved", 132 /* 6 */ "Reserved", 133 /* 7 */ "LOGOUT", 134 /* 8 */ "Reserved", 135 /* 9 */ "Reserved", 136 /* A */ "Reserved", 137 /* B */ "ABORT TASK", 138 /* C */ "ABORT TASK SET", 139 /* D */ "Reserved", 140 /* E */ "LOGICAL UNIT RESET", 141 /* F */ "TARGET RESET" 142 }; 143 144 #define ORB_RES_CMPL 0 145 #define ORB_RES_FAIL 1 146 #define ORB_RES_ILLE 2 147 #define ORB_RES_VEND 3 148 149 static int debug = 0; 150 static int auto_login = 1; 151 static int max_speed = 2; 152 static int sbp_cold = 1; 153 154 SYSCTL_DECL(_hw_firewire); 155 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem"); 156 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0, 157 "SBP debug flag"); 158 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0, 159 "SBP perform login automatically"); 160 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0, 161 "SBP transfer max speed"); 162 163 #define SBP_DEBUG(x) if (debug > x) { 164 #define END_DEBUG } 165 166 #define NEED_RESPONSE 0 167 168 struct ind_ptr { 169 u_int32_t hi,lo; 170 }; 171 #define SBP_IND_MAX 0x20 172 struct sbp_ocb { 173 STAILQ_ENTRY(sbp_ocb) ocb; 174 union ccb *ccb; 175 volatile u_int32_t orb[8]; 176 volatile struct ind_ptr ind_ptr[SBP_IND_MAX]; 177 struct sbp_dev *sdev; 178 int flags; 179 bus_dmamap_t dmamap; 180 }; 181 #define OCB_ACT_MGM 0 182 #define OCB_ACT_CMD 1 183 #define OCB_MATCH(o,s) (vtophys(&(o)->orb[0]) == ntohl((s)->orb_lo)) 184 185 186 struct sbp_login_res{ 187 u_int16_t len; 188 u_int16_t id; 189 u_int16_t res0; 190 u_int16_t cmd_hi; 191 u_int32_t cmd_lo; 192 u_int16_t res1; 193 u_int16_t recon_hold; 194 }; 195 struct sbp_status{ 196 u_int8_t len:3, 197 dead:1, 198 resp:2, 199 src:2; 200 u_int8_t status:8; 201 u_int16_t orb_hi; 202 u_int32_t orb_lo; 203 u_int32_t data[6]; 204 }; 205 struct sbp_cmd_status{ 206 #define SBP_SFMT_CURR 0 207 #define SBP_SFMT_DEFER 1 208 u_int8_t status:6, 209 sfmt:2; 210 u_int8_t s_key:4, 211 ill_len:1, 212 eom:1, 213 mark:1, 214 valid:1; 215 u_int8_t s_code; 216 u_int8_t s_qlfr; 217 u_int32_t info; 218 u_int32_t cdb; 219 u_int32_t fru:8, 220 s_keydep:24; 221 u_int32_t vend[2]; 222 }; 223 224 struct sbp_dev{ 225 #define SBP_DEV_RESET 0 /* accept login */ 226 #if 0 227 #define SBP_DEV_LOGIN 1 /* to login */ 228 #define SBP_DEV_RECONN 2 /* to reconnect */ 229 #endif 230 #define SBP_DEV_TOATTACH 3 /* to attach */ 231 #define SBP_DEV_PROBE 4 /* scan lun */ 232 #define SBP_DEV_ATTACHED 5 /* in operation */ 233 #define SBP_DEV_DEAD 6 /* unavailable unit */ 234 #define SBP_DEV_RETRY 7 /* unavailable unit */ 235 u_int8_t status:4, 236 #define SBP_DEV_TIMEOUT 1 237 flags:4; 238 u_int8_t type; 239 u_int16_t lun_id; 240 int freeze; 241 struct cam_path *path; 242 struct sbp_target *target; 243 struct sbp_login_res login; 244 struct callout login_callout; 245 STAILQ_HEAD(, sbp_ocb) ocbs; 246 char vendor[32]; 247 char product[32]; 248 char revision[10]; 249 }; 250 251 struct sbp_target { 252 int target_id; 253 int num_lun; 254 struct sbp_dev *luns; 255 struct sbp_softc *sbp; 256 struct fw_device *fwdev; 257 u_int32_t mgm_hi, mgm_lo; 258 struct sbp_ocb *mgm_ocb_cur; 259 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue; 260 struct callout mgm_ocb_timeout; 261 #define SCAN_DELAY 2 262 struct callout scan_callout; 263 }; 264 265 struct sbp_softc { 266 struct firewire_dev_comm fd; 267 struct cam_sim *sim; 268 struct cam_path *path; 269 struct sbp_target targets[SBP_NUM_TARGETS]; 270 struct fw_bind fwb; 271 STAILQ_HEAD(, sbp_ocb) free_ocbs; 272 struct sbp_ocb *ocb; 273 bus_dma_tag_t dmat; 274 #define SBP_RESOURCE_SHORTAGE 0x10 275 unsigned char flags; 276 }; 277 static void sbp_post_explore __P((void *)); 278 static void sbp_recv __P((struct fw_xfer *)); 279 static void sbp_login_callback __P((struct fw_xfer *)); 280 static void sbp_cmd_callback __P((struct fw_xfer *)); 281 static void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *)); 282 static void sbp_execute_ocb __P((void *, bus_dma_segment_t *, int, int)); 283 static void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *)); 284 static void sbp_abort_ocb __P((struct sbp_ocb *, int)); 285 static void sbp_abort_all_ocbs __P((struct sbp_dev *, int)); 286 static struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int)); 287 static struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *)); 288 static struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *)); 289 static struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, struct sbp_status *)); 290 static void sbp_cam_detach_target __P((struct sbp_target *)); 291 static void sbp_timeout __P((void *arg)); 292 static void sbp_mgm_orb __P((struct sbp_dev *, int, struct sbp_ocb *)); 293 #define sbp_login(sdev) \ 294 callout_reset(&(sdev)->login_callout, LOGIN_DELAY * hz, \ 295 sbp_login_callout, (void *)(sdev)); 296 297 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire"); 298 299 /* cam related functions */ 300 static void sbp_action(struct cam_sim *sim, union ccb *ccb); 301 static void sbp_poll(struct cam_sim *sim); 302 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *); 303 static void sbp_cam_scan_target(void *arg); 304 305 static char *orb_status0[] = { 306 /* 0 */ "No additional information to report", 307 /* 1 */ "Request type not supported", 308 /* 2 */ "Speed not supported", 309 /* 3 */ "Page size not supported", 310 /* 4 */ "Access denied", 311 /* 5 */ "Logical unit not supported", 312 /* 6 */ "Maximum payload too small", 313 /* 7 */ "Reserved for future standardization", 314 /* 8 */ "Resources unavailable", 315 /* 9 */ "Function rejected", 316 /* A */ "Login ID not recognized", 317 /* B */ "Dummy ORB completed", 318 /* C */ "Request aborted", 319 /* FF */ "Unspecified error" 320 #define MAX_ORB_STATUS0 0xd 321 }; 322 323 static char *orb_status1_object[] = { 324 /* 0 */ "Operation request block (ORB)", 325 /* 1 */ "Data buffer", 326 /* 2 */ "Page table", 327 /* 3 */ "Unable to specify" 328 }; 329 330 static char *orb_status1_serial_bus_error[] = { 331 /* 0 */ "Missing acknowledge", 332 /* 1 */ "Reserved; not to be used", 333 /* 2 */ "Time-out error", 334 /* 3 */ "Reserved; not to be used", 335 /* 4 */ "Busy retry limit exceeded(X)", 336 /* 5 */ "Busy retry limit exceeded(A)", 337 /* 6 */ "Busy retry limit exceeded(B)", 338 /* 7 */ "Reserved for future standardization", 339 /* 8 */ "Reserved for future standardization", 340 /* 9 */ "Reserved for future standardization", 341 /* A */ "Reserved for future standardization", 342 /* B */ "Tardy retry limit exceeded", 343 /* C */ "Conflict error", 344 /* D */ "Data error", 345 /* E */ "Type error", 346 /* F */ "Address error" 347 }; 348 349 static void 350 sbp_identify(driver_t *driver, device_t parent) 351 { 352 device_t child; 353 SBP_DEBUG(0) 354 printf("sbp_identify\n"); 355 END_DEBUG 356 357 child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 358 } 359 360 /* 361 * sbp_probe() 362 */ 363 static int 364 sbp_probe(device_t dev) 365 { 366 device_t pa; 367 368 SBP_DEBUG(0) 369 printf("sbp_probe\n"); 370 END_DEBUG 371 372 pa = device_get_parent(dev); 373 if(device_get_unit(dev) != device_get_unit(pa)){ 374 return(ENXIO); 375 } 376 377 device_set_desc(dev, "SBP2/SCSI over firewire"); 378 379 if (bootverbose) 380 debug = bootverbose; 381 return (0); 382 } 383 384 static void 385 sbp_show_sdev_info(struct sbp_dev *sdev, int new) 386 { 387 struct fw_device *fwdev; 388 389 printf("%s:%d:%d ", 390 device_get_nameunit(sdev->target->sbp->fd.dev), 391 sdev->target->target_id, 392 sdev->lun_id 393 ); 394 if (new == 2) { 395 return; 396 } 397 fwdev = sdev->target->fwdev; 398 printf("ordered:%d type:%d EUI:%08x%08x node:%d " 399 "speed:%d maxrec:%d", 400 (sdev->type & 0x40) >> 6, 401 (sdev->type & 0x1f), 402 fwdev->eui.hi, 403 fwdev->eui.lo, 404 fwdev->dst, 405 fwdev->speed, 406 fwdev->maxrec 407 ); 408 if (new) 409 printf(" new!\n"); 410 else 411 printf("\n"); 412 sbp_show_sdev_info(sdev, 2); 413 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 414 } 415 416 static struct { 417 int bus; 418 int target; 419 struct fw_eui64 eui; 420 } wired[] = { 421 /* Bus Target EUI64 */ 422 #if 0 423 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */ 424 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */ 425 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */ 426 #endif 427 {-1, -1, {0,0}} 428 }; 429 430 static int 431 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev) 432 { 433 int bus, i, target=-1; 434 char w[SBP_NUM_TARGETS]; 435 436 bzero(w, sizeof(w)); 437 bus = device_get_unit(sbp->fd.dev); 438 439 /* XXX wired-down configuration should be gotten from 440 tunable or device hint */ 441 for (i = 0; wired[i].bus >= 0; i ++) { 442 if (wired[i].bus == bus) { 443 w[wired[i].target] = 1; 444 if (wired[i].eui.hi == fwdev->eui.hi && 445 wired[i].eui.lo == fwdev->eui.lo) 446 target = wired[i].target; 447 } 448 } 449 if (target >= 0) { 450 if(target < SBP_NUM_TARGETS && 451 sbp->targets[target].fwdev == NULL) 452 return(target); 453 device_printf(sbp->fd.dev, 454 "target %d is not free for %08x:%08x\n", 455 target, fwdev->eui.hi, fwdev->eui.lo); 456 target = -1; 457 } 458 /* non-wired target */ 459 for (i = 0; i < SBP_NUM_TARGETS; i ++) 460 if (sbp->targets[i].fwdev == NULL && w[i] == 0) { 461 target = i; 462 break; 463 } 464 465 return target; 466 } 467 468 static struct sbp_target * 469 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 470 { 471 int i, maxlun, lun; 472 struct sbp_target *target; 473 struct sbp_dev *sdev; 474 struct crom_context cc; 475 struct csrreg *reg; 476 477 SBP_DEBUG(1) 478 printf("sbp_alloc_target\n"); 479 END_DEBUG 480 i = sbp_new_target(sbp, fwdev); 481 if (i < 0) { 482 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n"); 483 return NULL; 484 } 485 /* new target */ 486 target = &sbp->targets[i]; 487 target->sbp = sbp; 488 target->fwdev = fwdev; 489 target->target_id = i; 490 /* XXX we may want to reload mgm port after each bus reset */ 491 if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){ 492 /* bad target */ 493 printf("NULL management address\n"); 494 target->fwdev = NULL; 495 return NULL; 496 } 497 target->mgm_hi = 0xffff; 498 target->mgm_lo = 0xf0000000 | target->mgm_lo << 2; 499 target->mgm_ocb_cur = NULL; 500 SBP_DEBUG(1) 501 printf("target:%d mgm_port: %x\n", i, target->mgm_lo); 502 END_DEBUG 503 STAILQ_INIT(&target->mgm_ocb_queue); 504 CALLOUT_INIT(&target->mgm_ocb_timeout); 505 CALLOUT_INIT(&target->scan_callout); 506 507 /* XXX num_lun may be changed. realloc luns? */ 508 crom_init_context(&cc, target->fwdev->csrrom); 509 /* XXX shoud parse appropriate unit directories only */ 510 maxlun = -1; 511 while (cc.depth >= 0) { 512 reg = crom_search_key(&cc, CROM_LUN); 513 if (reg == NULL) 514 break; 515 lun = reg->val & 0xffff; 516 SBP_DEBUG(0) 517 printf("target %d lun %d found\n", target->target_id, lun); 518 END_DEBUG 519 if (maxlun < lun) 520 maxlun = lun; 521 crom_next(&cc); 522 } 523 if (maxlun < 0) 524 printf("no lun found!\n"); 525 if (maxlun >= SBP_NUM_LUNS) 526 maxlun = SBP_NUM_LUNS; 527 target->num_lun = maxlun + 1; 528 target->luns = (struct sbp_dev *) malloc( 529 sizeof(struct sbp_dev) * target->num_lun, 530 M_SBP, M_NOWAIT | M_ZERO); 531 for (i = 0; i < target->num_lun; i++) { 532 sdev = &target->luns[i]; 533 sdev->lun_id = i; 534 sdev->target = target; 535 STAILQ_INIT(&sdev->ocbs); 536 CALLOUT_INIT(&sdev->login_callout); 537 sdev->status = SBP_DEV_DEAD; 538 } 539 crom_init_context(&cc, target->fwdev->csrrom); 540 while (cc.depth >= 0) { 541 reg = crom_search_key(&cc, CROM_LUN); 542 if (reg == NULL) 543 break; 544 lun = reg->val & 0xffff; 545 if (lun >= SBP_NUM_LUNS) { 546 printf("too large lun %d\n", lun); 547 continue; 548 } 549 target->luns[lun].status = SBP_DEV_RESET; 550 target->luns[lun].type = (reg->val & 0xf0000) >> 16; 551 crom_next(&cc); 552 } 553 return target; 554 } 555 556 static void 557 sbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len) 558 { 559 static char *nullstr = "(null)"; 560 int i, clen, found=0; 561 struct csrhdr *chdr; 562 struct csrreg *creg; 563 u_int32_t *src, *dst; 564 565 chdr = (struct csrhdr *)&fwdev->csrrom[0]; 566 /* skip crom header, bus info and root directory */ 567 creg = (struct csrreg *)chdr + chdr->info_len + 2; 568 /* search unitl the one before the last. */ 569 for (i = chdr->info_len + 2; i < fwdev->rommax / 4; i++) { 570 if((creg++)->key == key){ 571 found = 1; 572 break; 573 } 574 } 575 if (!found || creg->key != CROM_TEXTLEAF) { 576 strncpy(buf, nullstr, len); 577 return; 578 } 579 src = (u_int32_t *) creg + creg->val; 580 clen = ((*src >> 16) - 2) * 4; 581 src += 3; 582 dst = (u_int32_t *) buf; 583 if (len < clen) 584 clen = len; 585 for (i = 0; i < clen/4; i++) 586 *dst++ = htonl(*src++); 587 buf[clen] = 0; 588 } 589 590 static void 591 sbp_probe_lun(struct sbp_dev *sdev) 592 { 593 struct fw_device *fwdev; 594 int rev; 595 596 fwdev = sdev->target->fwdev; 597 bzero(sdev->vendor, sizeof(sdev->vendor)); 598 bzero(sdev->product, sizeof(sdev->product)); 599 sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor)); 600 sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product)); 601 rev = getcsrdata(sdev->target->fwdev, 0x3c); 602 snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev); 603 } 604 605 606 static void 607 sbp_login_callout(void *arg) 608 { 609 struct sbp_dev *sdev = (struct sbp_dev *)arg; 610 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL); 611 } 612 613 #define SBP_FWDEV_ALIVE(fwdev) \ 614 ((fwdev->status == FWDEVATTACHED) \ 615 && (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10) \ 616 && (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2)) 617 618 static void 619 sbp_probe_target(void *arg) 620 { 621 struct sbp_target *target = (struct sbp_target *)arg; 622 struct sbp_softc *sbp; 623 struct sbp_dev *sdev; 624 struct firewire_comm *fc; 625 int i, alive; 626 627 alive = SBP_FWDEV_ALIVE(target->fwdev); 628 SBP_DEBUG(1) 629 printf("sbp_probe_target %d\n", target->target_id); 630 if (!alive) 631 printf("not alive\n"); 632 END_DEBUG 633 634 sbp = target->sbp; 635 fc = target->sbp->fd.fc; 636 /* XXX untimeout mgm_ocb and dequeue */ 637 for (i=0; i < target->num_lun; i++) { 638 sdev = &target->luns[i]; 639 if (alive && (sdev->status != SBP_DEV_DEAD)) { 640 if (sdev->path != NULL) { 641 xpt_freeze_devq(sdev->path, 1); 642 sdev->freeze ++; 643 } 644 sbp_probe_lun(sdev); 645 SBP_DEBUG(0) 646 sbp_show_sdev_info(sdev, 647 #if 0 648 (sdev->status == SBP_DEV_TOATTACH)); 649 #else 650 (sdev->status == SBP_DEV_RESET)); 651 #endif 652 END_DEBUG 653 654 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 655 switch (sdev->status) { 656 case SBP_DEV_RESET: 657 /* new or revived target */ 658 if (auto_login) { 659 #if 0 660 sdev->status = SBP_DEV_TOATTACH; 661 #endif 662 sbp_login(sdev); 663 } 664 break; 665 case SBP_DEV_TOATTACH: 666 case SBP_DEV_PROBE: 667 case SBP_DEV_ATTACHED: 668 case SBP_DEV_RETRY: 669 default: 670 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL); 671 break; 672 } 673 } else { 674 switch (sdev->status) { 675 case SBP_DEV_ATTACHED: 676 SBP_DEBUG(0) 677 /* the device has gone */ 678 sbp_show_sdev_info(sdev, 2); 679 printf("lost target\n"); 680 END_DEBUG 681 if (sdev->path) { 682 xpt_freeze_devq(sdev->path, 1); 683 sdev->freeze ++; 684 } 685 sdev->status = SBP_DEV_RETRY; 686 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 687 break; 688 case SBP_DEV_PROBE: 689 case SBP_DEV_TOATTACH: 690 sdev->status = SBP_DEV_RESET; 691 break; 692 case SBP_DEV_RETRY: 693 case SBP_DEV_RESET: 694 case SBP_DEV_DEAD: 695 break; 696 } 697 } 698 } 699 } 700 701 static void 702 sbp_post_explore(void *arg) 703 { 704 struct sbp_softc *sbp = (struct sbp_softc *)arg; 705 struct sbp_target *target; 706 struct fw_device *fwdev; 707 int i, alive; 708 709 SBP_DEBUG(0) 710 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold); 711 END_DEBUG 712 #if 0 /* 713 * XXX don't let CAM the bus rest. CAM tries to do something with 714 * freezed (DEV_RETRY) devices 715 */ 716 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 717 #endif 718 if (sbp_cold > 0) 719 sbp_cold --; 720 721 /* Gabage Collection */ 722 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 723 target = &sbp->targets[i]; 724 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) 725 if (target->fwdev == NULL || target->fwdev == fwdev) 726 break; 727 if(fwdev == NULL){ 728 /* device has removed in lower driver */ 729 sbp_cam_detach_target(target); 730 if (target->luns != NULL) 731 free(target->luns, M_SBP); 732 target->num_lun = 0;; 733 target->luns = NULL; 734 target->fwdev = NULL; 735 } 736 } 737 /* traverse device list */ 738 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) { 739 SBP_DEBUG(0) 740 printf("sbp_post_explore: EUI:%08x%08x ", 741 fwdev->eui.hi, fwdev->eui.lo); 742 if (fwdev->status == FWDEVATTACHED) { 743 printf("spec=%d key=%d.\n", 744 getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10, 745 getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 746 } else { 747 printf("not attached, state=%d.\n", fwdev->status); 748 } 749 END_DEBUG 750 alive = SBP_FWDEV_ALIVE(fwdev); 751 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 752 target = &sbp->targets[i]; 753 if(target->fwdev == fwdev ) { 754 /* known target */ 755 break; 756 } 757 } 758 if(i == SBP_NUM_TARGETS){ 759 if (alive) { 760 /* new target */ 761 target = sbp_alloc_target(sbp, fwdev); 762 if (target == NULL) 763 continue; 764 } else { 765 continue; 766 } 767 } 768 sbp_probe_target((void *)target); 769 } 770 #if 0 771 timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000); 772 #endif 773 } 774 775 #if NEED_RESPONSE 776 static void 777 sbp_loginres_callback(struct fw_xfer *xfer){ 778 SBP_DEBUG(1) 779 struct sbp_dev *sdev; 780 sdev = (struct sbp_dev *)xfer->sc; 781 sbp_show_sdev_info(sdev, 2); 782 printf("sbp_loginres_callback\n"); 783 END_DEBUG 784 fw_xfer_free(xfer); 785 return; 786 } 787 #endif 788 789 static void 790 sbp_login_callback(struct fw_xfer *xfer) 791 { 792 SBP_DEBUG(1) 793 struct sbp_dev *sdev; 794 sdev = (struct sbp_dev *)xfer->sc; 795 sbp_show_sdev_info(sdev, 2); 796 printf("sbp_login_callback\n"); 797 END_DEBUG 798 fw_xfer_free(xfer); 799 return; 800 } 801 802 static void 803 sbp_cmd_callback(struct fw_xfer *xfer) 804 { 805 SBP_DEBUG(2) 806 struct sbp_dev *sdev; 807 sdev = (struct sbp_dev *)xfer->sc; 808 sbp_show_sdev_info(sdev, 2); 809 printf("sbp_cmd_callback\n"); 810 END_DEBUG 811 fw_xfer_free(xfer); 812 return; 813 } 814 815 static struct sbp_dev * 816 sbp_next_dev(struct sbp_target *target, int lun) 817 { 818 struct sbp_dev *sdev; 819 int i; 820 821 for (i = lun, sdev = &target->luns[lun]; 822 i < target->num_lun; i++, sdev++) { 823 if (sdev->status == SBP_DEV_PROBE) 824 break; 825 } 826 if (i >= target->num_lun) 827 return(NULL); 828 return(sdev); 829 } 830 831 #define SCAN_PRI 1 832 static void 833 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb) 834 { 835 struct sbp_target *target; 836 struct sbp_dev *sdev; 837 838 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 839 target = sdev->target; 840 SBP_DEBUG(0) 841 sbp_show_sdev_info(sdev, 2); 842 printf("sbp_cam_scan_lun\n"); 843 END_DEBUG 844 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 845 sdev->status = SBP_DEV_ATTACHED; 846 } else { 847 sbp_show_sdev_info(sdev, 2); 848 printf("scan failed\n"); 849 } 850 sdev = sbp_next_dev(target, sdev->lun_id + 1); 851 if (sdev == NULL) { 852 free(ccb, M_SBP); 853 return; 854 } 855 /* reuse ccb */ 856 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 857 ccb->ccb_h.ccb_sdev_ptr = sdev; 858 xpt_action(ccb); 859 xpt_release_devq(sdev->path, sdev->freeze, TRUE); 860 sdev->freeze = 1; 861 } 862 863 static void 864 sbp_cam_scan_target(void *arg) 865 { 866 struct sbp_target *target = (struct sbp_target *)arg; 867 struct sbp_dev *sdev; 868 union ccb *ccb; 869 870 sdev = sbp_next_dev(target, 0); 871 if (sdev == NULL) { 872 printf("sbp_cam_scan_target: nothing to do for target%d\n", 873 target->target_id); 874 return; 875 } 876 SBP_DEBUG(0) 877 sbp_show_sdev_info(sdev, 2); 878 printf("sbp_cam_scan_target\n"); 879 END_DEBUG 880 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 881 if (ccb == NULL) { 882 printf("sbp_cam_scan_target: malloc failed\n"); 883 return; 884 } 885 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 886 ccb->ccb_h.func_code = XPT_SCAN_LUN; 887 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun; 888 ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 889 ccb->crcn.flags = CAM_FLAG_NONE; 890 ccb->ccb_h.ccb_sdev_ptr = sdev; 891 892 /* The scan is in progress now. */ 893 xpt_action(ccb); 894 xpt_release_devq(sdev->path, sdev->freeze, TRUE); 895 sdev->freeze = 1; 896 } 897 898 899 #if 0 900 static void 901 sbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb) 902 { 903 struct sbp_dev *sdev; 904 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 905 SBP_DEBUG(0) 906 sbp_show_sdev_info(sdev, 2); 907 printf("sbp_ping_unit_callback\n"); 908 END_DEBUG 909 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 910 if (--ccb->ccb_h.retry_count == 0) { 911 sbp_show_sdev_info(sdev, 2); 912 printf("sbp_ping_unit_callback: " 913 "retry count exceeded\n"); 914 sdev->status = SBP_DEV_RETRY; 915 free(ccb, M_SBP); 916 } else { 917 /* requeue */ 918 xpt_action(ccb); 919 xpt_release_devq(sdev->path, sdev->freeze, TRUE); 920 sdev->freeze = 1; /* we will freeze */ 921 } 922 } else { 923 free(ccb->csio.data_ptr, M_SBP); 924 free(ccb, M_SBP); 925 sdev->status = SBP_DEV_ATTACHED; 926 xpt_release_devq(sdev->path, sdev->freeze, TRUE); 927 sdev->freeze = 0; 928 } 929 } 930 931 /* 932 * XXX Some devices need to execute inquiry or read_capacity 933 * after bus_rest during busy transfer. 934 * Otherwise they return incorrect result for READ(and WRITE?) 935 * command without any SBP-II/SCSI error. 936 * 937 * e.g. Maxtor 3000XT, Yano A-dish. 938 */ 939 static void 940 sbp_ping_unit(struct sbp_dev *sdev) 941 { 942 union ccb *ccb; 943 struct scsi_inquiry_data *inq_buf; 944 945 946 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 947 if (ccb == NULL) { 948 printf("sbp_ping_unit: malloc failed\n"); 949 return; 950 } 951 952 inq_buf = (struct scsi_inquiry_data *) 953 malloc(sizeof(*inq_buf), M_SBP, M_NOWAIT); 954 if (inq_buf == NULL) { 955 free(ccb, M_SBP); 956 printf("sbp_ping_unit: malloc failed\n"); 957 return; 958 } 959 960 SBP_DEBUG(0) 961 sbp_show_sdev_info(sdev, 2); 962 printf("sbp_ping_unit\n"); 963 END_DEBUG 964 965 /* 966 * We need to execute this command before any other queued command. 967 * Make priority 0 and freeze the queue after execution for retry. 968 * cam's scan_lun command doesn't provide this feature. 969 */ 970 xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/); 971 scsi_inquiry( 972 &ccb->csio, 973 /*retries*/ 5, 974 sbp_ping_unit_callback, 975 MSG_SIMPLE_Q_TAG, 976 (u_int8_t *)inq_buf, 977 SHORT_INQUIRY_LENGTH, 978 /*evpd*/FALSE, 979 /*page_code*/0, 980 SSD_MIN_SIZE, 981 /*timeout*/60000 982 ); 983 ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 984 ccb->ccb_h.ccb_sdev_ptr = sdev; 985 xpt_action(ccb); 986 if (sdev->status != SBP_DEV_ATTACHED) 987 sdev->status = SBP_DEV_PROBE; 988 xpt_release_devq(sdev->path, sdev->freeze, TRUE); 989 sdev->freeze = 1; /* We will freeze the queue */ 990 } 991 #endif 992 993 static __inline void 994 sbp_scan_dev(struct sbp_dev *sdev) 995 { 996 sdev->status = SBP_DEV_PROBE; 997 callout_reset(&sdev->target->scan_callout, SCAN_DELAY * hz, 998 sbp_cam_scan_target, (void *)sdev->target); 999 } 1000 1001 static void 1002 sbp_do_attach(struct fw_xfer *xfer) 1003 { 1004 struct sbp_dev *sdev; 1005 struct sbp_target *target; 1006 struct sbp_softc *sbp; 1007 1008 sdev = (struct sbp_dev *)xfer->sc; 1009 target = sdev->target; 1010 sbp = target->sbp; 1011 SBP_DEBUG(0) 1012 sbp_show_sdev_info(sdev, 2); 1013 printf("sbp_do_attach\n"); 1014 END_DEBUG 1015 fw_xfer_free(xfer); 1016 1017 if (sdev->path == NULL) 1018 xpt_create_path(&sdev->path, xpt_periph, 1019 cam_sim_path(target->sbp->sim), 1020 target->target_id, sdev->lun_id); 1021 1022 /* 1023 * Let CAM scan the bus if we are in the boot process. 1024 * XXX xpt_scan_bus cannot detect LUN larger than 0 1025 * if LUN 0 doesn't exists. 1026 */ 1027 if (sbp_cold > 0) { 1028 sdev->status = SBP_DEV_ATTACHED; 1029 return; 1030 } 1031 1032 sbp_scan_dev(sdev); 1033 return; 1034 } 1035 1036 static void 1037 sbp_agent_reset_callback(struct fw_xfer *xfer) 1038 { 1039 struct sbp_dev *sdev; 1040 1041 sdev = (struct sbp_dev *)xfer->sc; 1042 SBP_DEBUG(1) 1043 sbp_show_sdev_info(sdev, 2); 1044 printf("sbp_cmd_callback\n"); 1045 END_DEBUG 1046 fw_xfer_free(xfer); 1047 if (sdev->path) { 1048 xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1049 sdev->freeze = 0; 1050 } 1051 } 1052 1053 static void 1054 sbp_agent_reset(struct sbp_dev *sdev) 1055 { 1056 struct fw_xfer *xfer; 1057 struct fw_pkt *fp; 1058 1059 SBP_DEBUG(0) 1060 sbp_show_sdev_info(sdev, 2); 1061 printf("sbp_agent_reset\n"); 1062 END_DEBUG 1063 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 1064 if (xfer == NULL) 1065 return; 1066 if (sdev->status == SBP_DEV_ATTACHED) 1067 xfer->act.hand = sbp_agent_reset_callback; 1068 else 1069 xfer->act.hand = sbp_do_attach; 1070 fp = (struct fw_pkt *)xfer->send.buf; 1071 fp->mode.wreqq.data = htonl(0xf); 1072 fw_asyreq(xfer->fc, -1, xfer); 1073 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT); 1074 } 1075 1076 static void 1077 sbp_busy_timeout_callback(struct fw_xfer *xfer) 1078 { 1079 struct sbp_dev *sdev; 1080 1081 sdev = (struct sbp_dev *)xfer->sc; 1082 SBP_DEBUG(1) 1083 sbp_show_sdev_info(sdev, 2); 1084 printf("sbp_busy_timeout_callback\n"); 1085 END_DEBUG 1086 fw_xfer_free(xfer); 1087 sbp_agent_reset(sdev); 1088 } 1089 1090 static void 1091 sbp_busy_timeout(struct sbp_dev *sdev) 1092 { 1093 struct fw_pkt *fp; 1094 struct fw_xfer *xfer; 1095 SBP_DEBUG(0) 1096 sbp_show_sdev_info(sdev, 2); 1097 printf("sbp_busy_timeout\n"); 1098 END_DEBUG 1099 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1100 1101 xfer->act.hand = sbp_busy_timeout_callback; 1102 fp = (struct fw_pkt *)xfer->send.buf; 1103 fp->mode.wreqq.dest_hi = htons(0xffff); 1104 fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT); 1105 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 1106 fw_asyreq(xfer->fc, -1, xfer); 1107 } 1108 1109 #if 0 1110 static void 1111 sbp_reset_start(struct sbp_dev *sdev) 1112 { 1113 struct fw_xfer *xfer; 1114 struct fw_pkt *fp; 1115 1116 SBP_DEBUG(0) 1117 sbp_show_sdev_info(sdev, 2); 1118 printf("sbp_reset_start\n"); 1119 END_DEBUG 1120 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1121 1122 xfer->act.hand = sbp_busy_timeout; 1123 fp = (struct fw_pkt *)xfer->send.buf; 1124 fp->mode.wreqq.dest_hi = htons(0xffff); 1125 fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START); 1126 fp->mode.wreqq.data = htonl(0xf); 1127 fw_asyreq(xfer->fc, -1, xfer); 1128 } 1129 #endif 1130 1131 static void 1132 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1133 { 1134 struct fw_xfer *xfer; 1135 struct fw_pkt *fp; 1136 SBP_DEBUG(2) 1137 sbp_show_sdev_info(sdev, 2); 1138 printf("sbp_orb_pointer\n"); 1139 END_DEBUG 1140 1141 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 1142 if (xfer == NULL) 1143 return; 1144 xfer->act.hand = sbp_cmd_callback; 1145 1146 fp = (struct fw_pkt *)xfer->send.buf; 1147 fp->mode.wreqb.len = htons(8); 1148 fp->mode.wreqb.extcode = 0; 1149 fp->mode.wreqb.payload[0] = 1150 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1151 fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1152 1153 if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 1154 fw_xfer_free(xfer); 1155 ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 1156 xpt_done(ocb->ccb); 1157 } 1158 } 1159 1160 #if 0 1161 static void 1162 sbp_doorbell(struct sbp_dev *sdev) 1163 { 1164 struct fw_xfer *xfer; 1165 struct fw_pkt *fp; 1166 SBP_DEBUG(1) 1167 sbp_show_sdev_info(sdev, 2); 1168 printf("sbp_doorbell\n"); 1169 END_DEBUG 1170 1171 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 1172 if (xfer == NULL) 1173 return; 1174 xfer->act.hand = sbp_cmd_callback; 1175 fp = (struct fw_pkt *)xfer->send.buf; 1176 fp->mode.wreqq.data = htonl(0xf); 1177 fw_asyreq(xfer->fc, -1, xfer); 1178 } 1179 #endif 1180 1181 static struct fw_xfer * 1182 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1183 { 1184 struct fw_xfer *xfer; 1185 struct fw_pkt *fp; 1186 1187 xfer = fw_xfer_alloc(M_SBP); 1188 if(xfer == NULL){ 1189 return NULL; 1190 } 1191 if (tcode == FWTCODE_WREQQ) 1192 xfer->send.len = 16; 1193 else 1194 xfer->send.len = 24; 1195 1196 xfer->send.buf = malloc(xfer->send.len, M_FW, M_NOWAIT); 1197 if(xfer->send.buf == NULL){ 1198 fw_xfer_free(xfer); 1199 return NULL; 1200 } 1201 1202 xfer->send.off = 0; 1203 xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1204 xfer->sc = (caddr_t)sdev; 1205 xfer->fc = sdev->target->sbp->fd.fc; 1206 xfer->retry_req = fw_asybusy; 1207 1208 fp = (struct fw_pkt *)xfer->send.buf; 1209 fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi); 1210 fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset); 1211 fp->mode.wreqq.tlrt = 0; 1212 fp->mode.wreqq.tcode = tcode; 1213 fp->mode.wreqq.pri = 0; 1214 xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst; 1215 fp->mode.wreqq.dst = htons(xfer->dst); 1216 1217 return xfer; 1218 1219 } 1220 1221 static void 1222 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb) 1223 { 1224 struct fw_xfer *xfer; 1225 struct fw_pkt *fp; 1226 struct sbp_ocb *ocb; 1227 struct sbp_target *target; 1228 int s, nid; 1229 1230 target = sdev->target; 1231 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS; 1232 1233 s = splfw(); 1234 if (func == ORB_FUN_RUNQUEUE) { 1235 ocb = STAILQ_FIRST(&target->mgm_ocb_queue); 1236 if (target->mgm_ocb_cur != NULL || ocb == NULL) { 1237 splx(s); 1238 return; 1239 } 1240 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb); 1241 goto start; 1242 } 1243 if ((ocb = sbp_get_ocb(target->sbp)) == NULL) { 1244 target->sbp->flags |= SBP_RESOURCE_SHORTAGE; 1245 splx(s); 1246 return; 1247 } 1248 ocb->flags = OCB_ACT_MGM; 1249 ocb->sdev = sdev; 1250 1251 bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb)); 1252 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1253 ocb->orb[7] = htonl(SBP_DEV2ADDR( 1254 device_get_unit(target->sbp->fd.dev), 1255 target->target_id, 1256 sdev->lun_id)); 1257 1258 SBP_DEBUG(0) 1259 sbp_show_sdev_info(sdev, 2); 1260 printf("%s\n", orb_fun_name[(func>>16)&0xf]); 1261 END_DEBUG 1262 switch (func) { 1263 case ORB_FUN_LGI: 1264 ocb->orb[2] = htonl(nid << 16); 1265 ocb->orb[3] = htonl(vtophys(&sdev->login)); 1266 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id); 1267 ocb->orb[5] = htonl(sizeof(struct sbp_login_res)); 1268 break; 1269 case ORB_FUN_ATA: 1270 ocb->orb[0] = htonl((0 << 16) | 0); 1271 ocb->orb[1] = htonl(vtophys(&aocb->orb[0])); 1272 /* fall through */ 1273 case ORB_FUN_RCN: 1274 case ORB_FUN_LGO: 1275 case ORB_FUN_LUR: 1276 case ORB_FUN_RST: 1277 case ORB_FUN_ATS: 1278 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id); 1279 break; 1280 } 1281 1282 if (target->mgm_ocb_cur != NULL) { 1283 /* there is a standing ORB */ 1284 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb); 1285 splx(s); 1286 return; 1287 } 1288 start: 1289 target->mgm_ocb_cur = ocb; 1290 splx(s); 1291 1292 callout_reset(&target->mgm_ocb_timeout, 5*hz, 1293 sbp_timeout, (caddr_t)ocb); 1294 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1295 if(xfer == NULL){ 1296 return; 1297 } 1298 xfer->act.hand = sbp_login_callback; 1299 1300 fp = (struct fw_pkt *)xfer->send.buf; 1301 fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi); 1302 fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo); 1303 fp->mode.wreqb.len = htons(8); 1304 fp->mode.wreqb.extcode = 0; 1305 fp->mode.wreqb.payload[0] = htonl(nid << 16); 1306 fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1307 1308 fw_asyreq(xfer->fc, -1, xfer); 1309 } 1310 1311 static void 1312 sbp_print_scsi_cmd(struct sbp_ocb *ocb) 1313 { 1314 struct ccb_scsiio *csio; 1315 1316 csio = &ocb->ccb->csio; 1317 printf("%s:%d:%d XPT_SCSI_IO: " 1318 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1319 ", flags: 0x%02x, " 1320 "%db cmd/%db data/%db sense\n", 1321 device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1322 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1323 csio->cdb_io.cdb_bytes[0], 1324 csio->cdb_io.cdb_bytes[1], 1325 csio->cdb_io.cdb_bytes[2], 1326 csio->cdb_io.cdb_bytes[3], 1327 csio->cdb_io.cdb_bytes[4], 1328 csio->cdb_io.cdb_bytes[5], 1329 csio->cdb_io.cdb_bytes[6], 1330 csio->cdb_io.cdb_bytes[7], 1331 csio->cdb_io.cdb_bytes[8], 1332 csio->cdb_io.cdb_bytes[9], 1333 ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1334 csio->cdb_len, csio->dxfer_len, 1335 csio->sense_len); 1336 } 1337 1338 static void 1339 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1340 { 1341 struct sbp_cmd_status *sbp_cmd_status; 1342 struct scsi_sense_data *sense; 1343 1344 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1345 sense = &ocb->ccb->csio.sense_data; 1346 1347 SBP_DEBUG(0) 1348 sbp_print_scsi_cmd(ocb); 1349 /* XXX need decode status */ 1350 sbp_show_sdev_info(ocb->sdev, 2); 1351 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d", 1352 sbp_cmd_status->status, 1353 sbp_cmd_status->sfmt, 1354 sbp_cmd_status->valid, 1355 sbp_cmd_status->s_key, 1356 sbp_cmd_status->s_code, 1357 sbp_cmd_status->s_qlfr, 1358 sbp_status->len 1359 ); 1360 #if 0 /* XXX */ 1361 if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) { 1362 printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]); 1363 scsi_sense_desc( 1364 sbp_cmd_status->s_code, 1365 sbp_cmd_status->s_qlfr, 1366 ocb->ccb->ccb_h.path->device->inq_data 1367 ) 1368 } else { 1369 printf("\n"); 1370 } 1371 #else 1372 printf("\n"); 1373 #endif 1374 END_DEBUG 1375 1376 switch (sbp_cmd_status->status) { 1377 case SCSI_STATUS_CHECK_COND: 1378 case SCSI_STATUS_BUSY: 1379 case SCSI_STATUS_CMD_TERMINATED: 1380 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1381 sense->error_code = SSD_CURRENT_ERROR; 1382 }else{ 1383 sense->error_code = SSD_DEFERRED_ERROR; 1384 } 1385 if(sbp_cmd_status->valid) 1386 sense->error_code |= SSD_ERRCODE_VALID; 1387 sense->flags = sbp_cmd_status->s_key; 1388 if(sbp_cmd_status->mark) 1389 sense->flags |= SSD_FILEMARK; 1390 if(sbp_cmd_status->eom) 1391 sense->flags |= SSD_EOM; 1392 if(sbp_cmd_status->ill_len) 1393 sense->flags |= SSD_ILI; 1394 sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff; 1395 sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff; 1396 sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff; 1397 sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff; 1398 if (sbp_status->len <= 1) 1399 /* XXX not scsi status. shouldn't be happened */ 1400 sense->extra_len = 0; 1401 else if (sbp_status->len <= 4) 1402 /* add_sense_code(_qual), info, cmd_spec_info */ 1403 sense->extra_len = 6; 1404 else 1405 /* fru, sense_key_spec */ 1406 sense->extra_len = 10; 1407 sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff; 1408 sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff; 1409 sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff; 1410 sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff; 1411 sense->add_sense_code = sbp_cmd_status->s_code; 1412 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1413 sense->fru = sbp_cmd_status->fru; 1414 sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff; 1415 sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff; 1416 sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff; 1417 1418 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1419 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1420 | CAM_AUTOSNS_VALID; 1421 /* 1422 { 1423 u_int8_t j, *tmp; 1424 tmp = sense; 1425 for( j = 0 ; j < 32 ; j+=8){ 1426 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1427 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1428 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1429 } 1430 1431 } 1432 */ 1433 break; 1434 default: 1435 sbp_show_sdev_info(ocb->sdev, 2); 1436 printf("sbp_scsi_status: unknown scsi status 0x%x\n", 1437 sbp_cmd_status->status); 1438 } 1439 } 1440 1441 static void 1442 sbp_fix_inq_data(struct sbp_ocb *ocb) 1443 { 1444 union ccb *ccb; 1445 struct sbp_dev *sdev; 1446 struct scsi_inquiry_data *inq; 1447 1448 ccb = ocb->ccb; 1449 sdev = ocb->sdev; 1450 1451 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1452 return; 1453 SBP_DEBUG(1) 1454 sbp_show_sdev_info(sdev, 2); 1455 printf("sbp_fix_inq_data\n"); 1456 END_DEBUG 1457 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1458 switch (SID_TYPE(inq)) { 1459 case T_DIRECT: 1460 /* 1461 * XXX Convert Direct Access device to RBC. 1462 * I've never seen FireWire DA devices which support READ_6. 1463 */ 1464 #if 1 1465 if (SID_TYPE(inq) == T_DIRECT) 1466 inq->device |= T_RBC; /* T_DIRECT == 0 */ 1467 #endif 1468 /* fall through */ 1469 case T_RBC: 1470 /* enable tag queuing */ 1471 #if 1 1472 inq->flags |= SID_CmdQue; 1473 #endif 1474 /* 1475 * Override vendor/product/revision information. 1476 * Some devices sometimes return strange strings. 1477 */ 1478 #if 1 1479 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1480 bcopy(sdev->product, inq->product, sizeof(inq->product)); 1481 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1482 #endif 1483 break; 1484 } 1485 } 1486 1487 static void 1488 sbp_recv1(struct fw_xfer *xfer){ 1489 struct fw_pkt *rfp; 1490 #if NEED_RESPONSE 1491 struct fw_pkt *sfp; 1492 #endif 1493 struct sbp_softc *sbp; 1494 struct sbp_dev *sdev; 1495 struct sbp_ocb *ocb; 1496 struct sbp_login_res *login_res = NULL; 1497 struct sbp_status *sbp_status; 1498 struct sbp_target *target; 1499 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0; 1500 u_int32_t addr; 1501 /* 1502 u_int32_t *ld; 1503 ld = xfer->recv.buf; 1504 printf("sbp %x %d %d %08x %08x %08x %08x\n", 1505 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1506 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1507 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1508 */ 1509 if(xfer->resp != 0){ 1510 printf("sbp_recv: xfer->resp != 0\n"); 1511 fw_xfer_free( xfer); 1512 return; 1513 } 1514 if(xfer->recv.buf == NULL){ 1515 printf("sbp_recv: xfer->recv.buf == NULL\n"); 1516 fw_xfer_free( xfer); 1517 return; 1518 } 1519 sbp = (struct sbp_softc *)xfer->sc; 1520 rfp = (struct fw_pkt *)xfer->recv.buf; 1521 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1522 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1523 fw_xfer_free( xfer); 1524 return; 1525 } 1526 sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload; 1527 addr = ntohl(rfp->mode.wreqb.dest_lo); 1528 SBP_DEBUG(2) 1529 printf("received address 0x%x\n", addr); 1530 END_DEBUG 1531 t = SBP_ADDR2TRG(addr); 1532 if (t >= SBP_NUM_TARGETS) { 1533 device_printf(sbp->fd.dev, 1534 "sbp_recv1: invalid target %d\n", t); 1535 fw_xfer_free(xfer); 1536 return; 1537 } 1538 target = &sbp->targets[t]; 1539 l = SBP_ADDR2LUN(addr); 1540 if (l >= target->num_lun) { 1541 device_printf(sbp->fd.dev, 1542 "sbp_recv1: invalid lun %d (target=%d)\n", l, t); 1543 fw_xfer_free(xfer); 1544 return; 1545 } 1546 sdev = &target->luns[l]; 1547 1548 ocb = NULL; 1549 switch (sbp_status->src) { 1550 case 0: 1551 case 1: 1552 /* check mgm_ocb_cur first */ 1553 ocb = target->mgm_ocb_cur; 1554 if (ocb != NULL) { 1555 if (OCB_MATCH(ocb, sbp_status)) { 1556 callout_stop(&target->mgm_ocb_timeout); 1557 target->mgm_ocb_cur = NULL; 1558 break; 1559 } 1560 } 1561 ocb = sbp_dequeue_ocb(sdev, sbp_status); 1562 if (ocb == NULL) { 1563 sbp_show_sdev_info(sdev, 2); 1564 #if __FreeBSD_version >= 500000 1565 printf("No ocb(%x) on the queue\n", 1566 #else 1567 printf("No ocb(%lx) on the queue\n", 1568 #endif 1569 ntohl(sbp_status->orb_lo)); 1570 } 1571 break; 1572 case 2: 1573 /* unsolicit */ 1574 sbp_show_sdev_info(sdev, 2); 1575 printf("unsolicit status received\n"); 1576 break; 1577 default: 1578 sbp_show_sdev_info(sdev, 2); 1579 printf("unknown sbp_status->src\n"); 1580 } 1581 1582 status_valid0 = (sbp_status->src < 2 1583 && sbp_status->resp == ORB_RES_CMPL 1584 && sbp_status->dead == 0); 1585 status_valid = (status_valid0 && sbp_status->status == 0); 1586 1587 if (!status_valid0 || debug > 1){ 1588 int status; 1589 SBP_DEBUG(0) 1590 sbp_show_sdev_info(sdev, 2); 1591 printf("ORB status src:%x resp:%x dead:%x" 1592 #if __FreeBSD_version >= 500000 1593 " len:%x stat:%x orb:%x%08x\n", 1594 #else 1595 " len:%x stat:%x orb:%x%08lx\n", 1596 #endif 1597 sbp_status->src, sbp_status->resp, sbp_status->dead, 1598 sbp_status->len, sbp_status->status, 1599 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1600 END_DEBUG 1601 sbp_show_sdev_info(sdev, 2); 1602 status = sbp_status->status; 1603 switch(sbp_status->resp) { 1604 case 0: 1605 if (status > MAX_ORB_STATUS0) 1606 printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1607 else 1608 printf("%s\n", orb_status0[status]); 1609 break; 1610 case 1: 1611 printf("Obj: %s, Error: %s\n", 1612 orb_status1_object[(status>>6) & 3], 1613 orb_status1_serial_bus_error[status & 0xf]); 1614 break; 1615 case 2: 1616 printf("Illegal request\n"); 1617 break; 1618 case 3: 1619 printf("Vendor dependent\n"); 1620 break; 1621 default: 1622 printf("unknown respose code %d\n", sbp_status->resp); 1623 } 1624 } 1625 1626 /* we have to reset the fetch agent if it's dead */ 1627 if (sbp_status->dead) { 1628 if (sdev->path) { 1629 xpt_freeze_devq(sdev->path, 1); 1630 sdev->freeze ++; 1631 } 1632 reset_agent = 1; 1633 } 1634 1635 if (ocb == NULL) 1636 goto done; 1637 1638 sdev->flags &= ~SBP_DEV_TIMEOUT; 1639 1640 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1641 case ORB_FMT_NOP: 1642 break; 1643 case ORB_FMT_VED: 1644 break; 1645 case ORB_FMT_STD: 1646 switch(ocb->flags) { 1647 case OCB_ACT_MGM: 1648 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1649 switch(orb_fun) { 1650 case ORB_FUN_LGI: 1651 login_res = &sdev->login; 1652 login_res->len = ntohs(login_res->len); 1653 login_res->id = ntohs(login_res->id); 1654 login_res->cmd_hi = ntohs(login_res->cmd_hi); 1655 login_res->cmd_lo = ntohl(login_res->cmd_lo); 1656 if (status_valid) { 1657 SBP_DEBUG(0) 1658 sbp_show_sdev_info(sdev, 2); 1659 printf("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)); 1660 END_DEBUG 1661 #if 0 1662 sdev->status = SBP_DEV_TOATTACH; 1663 #endif 1664 #if 1 1665 sbp_busy_timeout(sdev); 1666 #else 1667 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL); 1668 #endif 1669 } else { 1670 /* forgot logout? */ 1671 sbp_show_sdev_info(sdev, 2); 1672 printf("login failed\n"); 1673 sdev->status = SBP_DEV_RESET; 1674 } 1675 break; 1676 case ORB_FUN_RCN: 1677 login_res = &sdev->login; 1678 if (status_valid) { 1679 SBP_DEBUG(0) 1680 sbp_show_sdev_info(sdev, 2); 1681 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1682 END_DEBUG 1683 #if 1 1684 if (sdev->status == SBP_DEV_ATTACHED) 1685 sbp_scan_dev(sdev); 1686 else 1687 sbp_agent_reset(sdev); 1688 #else 1689 sdev->status = SBP_DEV_ATTACHED; 1690 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL); 1691 #endif 1692 } else { 1693 /* reconnection hold time exceed? */ 1694 SBP_DEBUG(0) 1695 sbp_show_sdev_info(sdev, 2); 1696 printf("reconnect failed\n"); 1697 END_DEBUG 1698 sbp_login(sdev); 1699 } 1700 break; 1701 case ORB_FUN_LGO: 1702 sdev->status = SBP_DEV_RESET; 1703 break; 1704 case ORB_FUN_RST: 1705 sbp_busy_timeout(sdev); 1706 break; 1707 case ORB_FUN_LUR: 1708 case ORB_FUN_ATA: 1709 case ORB_FUN_ATS: 1710 sbp_agent_reset(sdev); 1711 break; 1712 default: 1713 sbp_show_sdev_info(sdev, 2); 1714 printf("unknown function %d\n", orb_fun); 1715 break; 1716 } 1717 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 1718 break; 1719 case OCB_ACT_CMD: 1720 if(ocb->ccb != NULL){ 1721 union ccb *ccb; 1722 /* 1723 u_int32_t *ld; 1724 ld = ocb->ccb->csio.data_ptr; 1725 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1726 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1727 else 1728 printf("ptr NULL\n"); 1729 printf("len %d\n", sbp_status->len); 1730 */ 1731 ccb = ocb->ccb; 1732 if(sbp_status->len > 1){ 1733 sbp_scsi_status(sbp_status, ocb); 1734 }else{ 1735 if(sbp_status->resp != ORB_RES_CMPL){ 1736 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1737 }else{ 1738 ccb->ccb_h.status = CAM_REQ_CMP; 1739 } 1740 } 1741 /* fix up inq data */ 1742 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1743 sbp_fix_inq_data(ocb); 1744 xpt_done(ccb); 1745 } 1746 break; 1747 default: 1748 break; 1749 } 1750 } 1751 1752 sbp_free_ocb(sbp, ocb); 1753 done: 1754 if (reset_agent) 1755 sbp_agent_reset(sdev); 1756 1757 /* The received packet is usually small enough to be stored within 1758 * the buffer. In that case, the controller return ack_complete and 1759 * no respose is necessary. 1760 * 1761 * XXX fwohci.c and firewire.c should inform event_code such as 1762 * ack_complete or ack_pending to upper driver. 1763 */ 1764 #if NEED_RESPONSE 1765 xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO); 1766 xfer->send.len = 12; 1767 xfer->send.off = 0; 1768 sfp = (struct fw_pkt *)xfer->send.buf; 1769 sfp->mode.wres.dst = rfp->mode.wreqb.src; 1770 xfer->dst = ntohs(sfp->mode.wres.dst); 1771 xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1772 xfer->act.hand = sbp_loginres_callback; 1773 xfer->retry_req = fw_asybusy; 1774 1775 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1776 sfp->mode.wres.tcode = FWTCODE_WRES; 1777 sfp->mode.wres.rtcode = 0; 1778 sfp->mode.wres.pri = 0; 1779 1780 fw_asyreq(xfer->fc, -1, xfer); 1781 #else 1782 fw_xfer_free(xfer); 1783 #endif 1784 1785 return; 1786 1787 } 1788 1789 static void 1790 sbp_recv(struct fw_xfer *xfer) 1791 { 1792 int s; 1793 1794 s = splcam(); 1795 sbp_recv1(xfer); 1796 splx(s); 1797 } 1798 /* 1799 * sbp_attach() 1800 */ 1801 static int 1802 sbp_attach(device_t dev) 1803 { 1804 struct sbp_softc *sbp; 1805 struct cam_devq *devq; 1806 struct fw_xfer *xfer; 1807 int i, s, error; 1808 1809 SBP_DEBUG(0) 1810 printf("sbp_attach (cold=%d)\n", cold); 1811 END_DEBUG 1812 1813 if (cold) 1814 sbp_cold ++; 1815 sbp = ((struct sbp_softc *)device_get_softc(dev)); 1816 bzero(sbp, sizeof(struct sbp_softc)); 1817 sbp->fd.dev = dev; 1818 sbp->fd.fc = device_get_ivars(dev); 1819 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 1820 /*boundary*/0, 1821 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1822 /*highaddr*/BUS_SPACE_MAXADDR, 1823 /*filter*/NULL, /*filterarg*/NULL, 1824 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1825 /*maxsegsz*/0x8000, 1826 /*flags*/BUS_DMA_ALLOCNOW, 1827 &sbp->dmat); 1828 if (error != 0) { 1829 printf("sbp_attach: Could not allocate DMA tag " 1830 "- error %d\n", error); 1831 return (ENOMEM); 1832 } 1833 1834 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1835 if (devq == NULL) 1836 return (ENXIO); 1837 1838 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1839 sbp->targets[i].fwdev = NULL; 1840 sbp->targets[i].luns = NULL; 1841 } 1842 1843 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1844 device_get_unit(dev), 1845 /*untagged*/ 1, 1846 /*tagged*/ SBP_QUEUE_LEN, 1847 devq); 1848 1849 if (sbp->sim == NULL) { 1850 cam_simq_free(devq); 1851 return (ENXIO); 1852 } 1853 1854 sbp->ocb = (struct sbp_ocb *) contigmalloc( 1855 sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1856 M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 1857 bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB); 1858 1859 if (sbp->ocb == NULL) { 1860 printf("sbp0: ocb alloction failure\n"); 1861 return (ENOMEM); 1862 } 1863 1864 STAILQ_INIT(&sbp->free_ocbs); 1865 for (i = 0; i < SBP_NUM_OCB; i++) { 1866 sbp_free_ocb(sbp, &sbp->ocb[i]); 1867 } 1868 1869 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) 1870 goto fail; 1871 1872 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim), 1873 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) 1874 goto fail; 1875 1876 xfer = fw_xfer_alloc(M_SBP); 1877 xfer->act.hand = sbp_recv; 1878 xfer->act_type = FWACT_XFER; 1879 #if NEED_RESPONSE 1880 xfer->fc = sbp->fd.fc; 1881 #endif 1882 xfer->sc = (caddr_t)sbp; 1883 1884 sbp->fwb.start_hi = SBP_BIND_HI; 1885 sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0); 1886 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1887 sbp->fwb.addrlen = 0xffff; 1888 sbp->fwb.xfer = xfer; 1889 fw_bindadd(sbp->fd.fc, &sbp->fwb); 1890 1891 sbp->fd.post_explore = sbp_post_explore; 1892 1893 if (sbp->fd.fc->status != -1) { 1894 s = splfw(); 1895 sbp_post_explore((void *)sbp); 1896 splx(s); 1897 } 1898 1899 return (0); 1900 fail: 1901 cam_sim_free(sbp->sim, /*free_devq*/TRUE); 1902 contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1903 return (ENXIO); 1904 } 1905 1906 static int 1907 sbp_logout_all(struct sbp_softc *sbp) 1908 { 1909 struct sbp_target *target; 1910 struct sbp_dev *sdev; 1911 int i, j; 1912 1913 SBP_DEBUG(0) 1914 printf("sbp_logout_all\n"); 1915 END_DEBUG 1916 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) { 1917 target = &sbp->targets[i]; 1918 if (target->luns == NULL) 1919 continue; 1920 for (j = 0; j < target->num_lun; j++) { 1921 sdev = &target->luns[j]; 1922 if (sdev->status >= SBP_DEV_TOATTACH && 1923 sdev->status <= SBP_DEV_ATTACHED) 1924 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL); 1925 } 1926 } 1927 return 0; 1928 } 1929 1930 static int 1931 sbp_shutdown(device_t dev) 1932 { 1933 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1934 1935 sbp_logout_all(sbp); 1936 return (0); 1937 } 1938 1939 static int 1940 sbp_detach(device_t dev) 1941 { 1942 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1943 struct firewire_comm *fc = sbp->fd.fc; 1944 int i; 1945 1946 SBP_DEBUG(0) 1947 printf("sbp_detach\n"); 1948 END_DEBUG 1949 #if 0 1950 /* bus reset for logout */ 1951 sbp->fd.post_explore = NULL; 1952 fc->ibr(fc); 1953 #endif 1954 1955 for (i = 0; i < SBP_NUM_TARGETS; i ++) 1956 sbp_cam_detach_target(&sbp->targets[i]); 1957 xpt_free_path(sbp->path); 1958 xpt_bus_deregister(cam_sim_path(sbp->sim)); 1959 1960 sbp_logout_all(sbp); 1961 /* XXX wait for logout completion */ 1962 tsleep(&i, FWPRI, "sbpdtc", hz/2); 1963 1964 fw_bindremove(fc, &sbp->fwb); 1965 contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1966 bus_dma_tag_destroy(sbp->dmat); 1967 1968 for (i = 0; i < SBP_NUM_TARGETS; i ++) 1969 if (sbp->targets[i].luns != NULL) 1970 free(sbp->targets[i].luns, M_SBP); 1971 1972 return (0); 1973 } 1974 1975 static void 1976 sbp_cam_detach_target(struct sbp_target *target) 1977 { 1978 int i; 1979 struct sbp_dev *sdev; 1980 1981 if (target->luns != NULL) { 1982 SBP_DEBUG(0) 1983 printf("sbp_detach_target %d\n", target->target_id); 1984 END_DEBUG 1985 callout_stop(&target->scan_callout); 1986 callout_stop(&target->mgm_ocb_timeout); 1987 for (i = 0; i < target->num_lun; i++) { 1988 sdev = &target->luns[i]; 1989 callout_stop(&sdev->login_callout); 1990 if (sdev->status == SBP_DEV_RESET || 1991 sdev->status == SBP_DEV_DEAD) 1992 continue; 1993 if (sdev->path) { 1994 xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 1995 xpt_free_path(sdev->path); 1996 sdev->path = NULL; 1997 } 1998 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 1999 } 2000 } 2001 } 2002 2003 static void 2004 sbp_timeout(void *arg) 2005 { 2006 struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2007 struct sbp_dev *sdev = ocb->sdev; 2008 2009 sbp_show_sdev_info(sdev, 2); 2010 printf("request timeout ... "); 2011 2012 if (ocb->flags == OCB_ACT_MGM) { 2013 printf("management ORB\n"); 2014 /* XXX just ignore for now */ 2015 sdev->target->mgm_ocb_cur = NULL; 2016 sbp_free_ocb(sdev->target->sbp, ocb); 2017 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 2018 return; 2019 } 2020 2021 xpt_freeze_devq(sdev->path, 1); 2022 sdev->freeze ++; 2023 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 2024 if (sdev->flags & SBP_DEV_TIMEOUT) { 2025 #if 0 2026 struct firewire_comm *fc; 2027 2028 printf("bus reset\n"); 2029 fc = sdev->target->sbp->fd.fc; 2030 fc->ibr(fc); 2031 sdev->status == SBP_DEV_RETRY; 2032 #else 2033 printf("target reset\n"); 2034 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL); 2035 #endif 2036 sdev->flags &= ~SBP_DEV_TIMEOUT; 2037 } else { 2038 printf("agent reset\n"); 2039 sdev->flags |= SBP_DEV_TIMEOUT; 2040 sbp_agent_reset(sdev); 2041 } 2042 return; 2043 } 2044 2045 static void 2046 sbp_action1(struct cam_sim *sim, union ccb *ccb) 2047 { 2048 2049 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 2050 struct sbp_target *target = NULL; 2051 struct sbp_dev *sdev = NULL; 2052 2053 /* target:lun -> sdev mapping */ 2054 if (sbp != NULL 2055 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 2056 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 2057 target = &sbp->targets[ccb->ccb_h.target_id]; 2058 if (target->fwdev != NULL 2059 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 2060 && ccb->ccb_h.target_lun < target->num_lun) { 2061 sdev = &target->luns[ccb->ccb_h.target_lun]; 2062 if (sdev->status != SBP_DEV_ATTACHED && 2063 sdev->status != SBP_DEV_PROBE) 2064 sdev = NULL; 2065 } 2066 } 2067 2068 SBP_DEBUG(1) 2069 if (sdev == NULL) 2070 printf("invalid target %d lun %d\n", 2071 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2072 END_DEBUG 2073 2074 switch (ccb->ccb_h.func_code) { 2075 case XPT_SCSI_IO: 2076 case XPT_RESET_DEV: 2077 case XPT_GET_TRAN_SETTINGS: 2078 case XPT_SET_TRAN_SETTINGS: 2079 case XPT_CALC_GEOMETRY: 2080 if (sdev == NULL) { 2081 SBP_DEBUG(1) 2082 printf("%s:%d:%d:func_code 0x%04x: " 2083 "Invalid target (target needed)\n", 2084 device_get_nameunit(sbp->fd.dev), 2085 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2086 ccb->ccb_h.func_code); 2087 END_DEBUG 2088 2089 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2090 xpt_done(ccb); 2091 return; 2092 } 2093 break; 2094 case XPT_PATH_INQ: 2095 case XPT_NOOP: 2096 /* The opcodes sometimes aimed at a target (sc is valid), 2097 * sometimes aimed at the SIM (sc is invalid and target is 2098 * CAM_TARGET_WILDCARD) 2099 */ 2100 if (sbp == NULL && 2101 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 2102 SBP_DEBUG(0) 2103 printf("%s:%d:%d func_code 0x%04x: " 2104 "Invalid target (no wildcard)\n", 2105 device_get_nameunit(sbp->fd.dev), 2106 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2107 ccb->ccb_h.func_code); 2108 END_DEBUG 2109 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2110 xpt_done(ccb); 2111 return; 2112 } 2113 break; 2114 default: 2115 /* XXX Hm, we should check the input parameters */ 2116 break; 2117 } 2118 2119 switch (ccb->ccb_h.func_code) { 2120 case XPT_SCSI_IO: 2121 { 2122 struct ccb_scsiio *csio; 2123 struct sbp_ocb *ocb; 2124 int s, speed; 2125 void *cdb; 2126 2127 csio = &ccb->csio; 2128 2129 SBP_DEBUG(1) 2130 printf("%s:%d:%d XPT_SCSI_IO: " 2131 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 2132 ", flags: 0x%02x, " 2133 "%db cmd/%db data/%db sense\n", 2134 device_get_nameunit(sbp->fd.dev), 2135 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2136 csio->cdb_io.cdb_bytes[0], 2137 csio->cdb_io.cdb_bytes[1], 2138 csio->cdb_io.cdb_bytes[2], 2139 csio->cdb_io.cdb_bytes[3], 2140 csio->cdb_io.cdb_bytes[4], 2141 csio->cdb_io.cdb_bytes[5], 2142 csio->cdb_io.cdb_bytes[6], 2143 csio->cdb_io.cdb_bytes[7], 2144 csio->cdb_io.cdb_bytes[8], 2145 csio->cdb_io.cdb_bytes[9], 2146 ccb->ccb_h.flags & CAM_DIR_MASK, 2147 csio->cdb_len, csio->dxfer_len, 2148 csio->sense_len); 2149 END_DEBUG 2150 if(sdev == NULL){ 2151 ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2152 xpt_done(ccb); 2153 return; 2154 } 2155 #if 0 2156 /* if we are in probe stage, pass only probe commands */ 2157 if (sdev->status == SBP_DEV_PROBE) { 2158 char *name; 2159 name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 2160 printf("probe stage, periph name: %s\n", name); 2161 if (strcmp(name, "probe") != 0) { 2162 ccb->ccb_h.status = CAM_REQUEUE_REQ; 2163 xpt_done(ccb); 2164 return; 2165 } 2166 } 2167 #endif 2168 if ((ocb = sbp_get_ocb(sbp)) == NULL) { 2169 s = splfw(); 2170 sbp->flags |= SBP_RESOURCE_SHORTAGE; 2171 splx(s); 2172 return; 2173 } 2174 ocb->flags = OCB_ACT_CMD; 2175 ocb->sdev = sdev; 2176 ocb->ccb = ccb; 2177 ccb->ccb_h.ccb_sdev_ptr = sdev; 2178 ocb->orb[0] = htonl(1 << 31); 2179 ocb->orb[1] = 0; 2180 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 2181 ocb->orb[3] = htonl(vtophys(ocb->ind_ptr)); 2182 speed = min(target->fwdev->speed, max_speed); 2183 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 2184 | ORB_CMD_MAXP(speed + 7)); 2185 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 2186 ocb->orb[4] |= htonl(ORB_CMD_IN); 2187 } 2188 2189 if (csio->ccb_h.flags & CAM_SCATTER_VALID) 2190 printf("sbp: CAM_SCATTER_VALID\n"); 2191 if (csio->ccb_h.flags & CAM_DATA_PHYS) 2192 printf("sbp: CAM_DATA_PHYS\n"); 2193 2194 if (csio->ccb_h.flags & CAM_CDB_POINTER) 2195 cdb = (void *)csio->cdb_io.cdb_ptr; 2196 else 2197 cdb = (void *)&csio->cdb_io.cdb_bytes; 2198 bcopy(cdb, 2199 (void *)(uintptr_t)(volatile void *)&ocb->orb[5], 2200 csio->cdb_len); 2201 /* 2202 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 2203 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 2204 */ 2205 if (ccb->csio.dxfer_len > 0) { 2206 int s; 2207 2208 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 2209 printf("sbp_action1: cannot create dmamap\n"); 2210 break; 2211 } 2212 2213 s = splsoftvm(); 2214 bus_dmamap_load(/*dma tag*/sbp->dmat, 2215 /*dma map*/ocb->dmamap, 2216 ccb->csio.data_ptr, 2217 ccb->csio.dxfer_len, 2218 sbp_execute_ocb, 2219 ocb, 2220 /*flags*/0); 2221 splx(s); 2222 } else 2223 sbp_execute_ocb(ocb, NULL, 0, 0); 2224 break; 2225 } 2226 case XPT_CALC_GEOMETRY: 2227 { 2228 struct ccb_calc_geometry *ccg; 2229 u_int32_t size_mb; 2230 u_int32_t secs_per_cylinder; 2231 int extended = 1; 2232 ccg = &ccb->ccg; 2233 2234 if (ccg->block_size == 0) { 2235 printf("sbp_action1: block_size is 0.\n"); 2236 ccb->ccb_h.status = CAM_REQ_INVALID; 2237 xpt_done(ccb); 2238 break; 2239 } 2240 SBP_DEBUG(1) 2241 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2242 "Volume size = %d\n", 2243 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim), 2244 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2245 ccg->volume_size); 2246 END_DEBUG 2247 2248 size_mb = ccg->volume_size 2249 / ((1024L * 1024L) / ccg->block_size); 2250 2251 if (size_mb >= 1024 && extended) { 2252 ccg->heads = 255; 2253 ccg->secs_per_track = 63; 2254 } else { 2255 ccg->heads = 64; 2256 ccg->secs_per_track = 32; 2257 } 2258 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2259 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2260 ccb->ccb_h.status = CAM_REQ_CMP; 2261 xpt_done(ccb); 2262 break; 2263 } 2264 case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2265 { 2266 2267 SBP_DEBUG(1) 2268 printf("%s:%d:XPT_RESET_BUS: \n", 2269 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 2270 END_DEBUG 2271 2272 ccb->ccb_h.status = CAM_REQ_INVALID; 2273 xpt_done(ccb); 2274 break; 2275 } 2276 case XPT_PATH_INQ: /* Path routing inquiry */ 2277 { 2278 struct ccb_pathinq *cpi = &ccb->cpi; 2279 2280 SBP_DEBUG(1) 2281 printf("%s:%d:%d XPT_PATH_INQ:.\n", 2282 device_get_nameunit(sbp->fd.dev), 2283 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2284 END_DEBUG 2285 cpi->version_num = 1; /* XXX??? */ 2286 cpi->hba_inquiry = PI_TAG_ABLE; 2287 cpi->target_sprt = 0; 2288 cpi->hba_misc = PIM_NOBUSRESET; 2289 cpi->hba_eng_cnt = 0; 2290 cpi->max_target = SBP_NUM_TARGETS - 1; 2291 cpi->max_lun = SBP_NUM_LUNS - 1; 2292 cpi->initiator_id = SBP_INITIATOR; 2293 cpi->bus_id = sim->bus_id; 2294 cpi->base_transfer_speed = 400 * 1000 / 8; 2295 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2296 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 2297 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 2298 cpi->unit_number = sim->unit_number; 2299 2300 cpi->ccb_h.status = CAM_REQ_CMP; 2301 xpt_done(ccb); 2302 break; 2303 } 2304 case XPT_GET_TRAN_SETTINGS: 2305 { 2306 struct ccb_trans_settings *cts = &ccb->cts; 2307 SBP_DEBUG(1) 2308 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 2309 device_get_nameunit(sbp->fd.dev), 2310 ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2311 END_DEBUG 2312 /* Enable disconnect and tagged queuing */ 2313 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID; 2314 cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB; 2315 2316 cts->ccb_h.status = CAM_REQ_CMP; 2317 xpt_done(ccb); 2318 break; 2319 } 2320 case XPT_ABORT: 2321 ccb->ccb_h.status = CAM_UA_ABORT; 2322 xpt_done(ccb); 2323 break; 2324 case XPT_SET_TRAN_SETTINGS: 2325 /* XXX */ 2326 default: 2327 ccb->ccb_h.status = CAM_REQ_INVALID; 2328 xpt_done(ccb); 2329 break; 2330 } 2331 return; 2332 } 2333 2334 static void 2335 sbp_action(struct cam_sim *sim, union ccb *ccb) 2336 { 2337 int s; 2338 2339 s = splfw(); 2340 sbp_action1(sim, ccb); 2341 splx(s); 2342 } 2343 2344 static void 2345 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 2346 { 2347 int i; 2348 struct sbp_ocb *ocb; 2349 struct sbp_ocb *prev; 2350 union ccb *ccb; 2351 bus_dma_segment_t *s; 2352 2353 if (error) 2354 printf("sbp_execute_ocb: error=%d\n", error); 2355 2356 ocb = (struct sbp_ocb *)arg; 2357 if (seg == 1) { 2358 /* direct pointer */ 2359 ocb->orb[3] = htonl(segments[0].ds_addr); 2360 ocb->orb[4] |= htonl(segments[0].ds_len); 2361 } else if(seg > 1) { 2362 /* page table */ 2363 SBP_DEBUG(1) 2364 printf("sbp_execute_ocb: seg %d", seg); 2365 for (i = 0; i < seg; i++) 2366 #if __FreeBSD_version >= 500000 2367 printf(", %tx:%zd", segments[i].ds_addr, 2368 #else 2369 printf(", %x:%d", segments[i].ds_addr, 2370 #endif 2371 segments[i].ds_len); 2372 printf("\n"); 2373 END_DEBUG 2374 for (i = 0; i < seg; i++) { 2375 s = &segments[i]; 2376 SBP_DEBUG(0) 2377 /* XXX LSI Logic "< 16 byte" bug might be hit */ 2378 if (s->ds_len < 16) 2379 printf("sbp_execute_ocb: warning, " 2380 #if __FreeBSD_version >= 500000 2381 "segment length(%zd) is less than 16." 2382 #else 2383 "segment length(%d) is less than 16." 2384 #endif 2385 "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2386 END_DEBUG 2387 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2388 ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2389 } 2390 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2391 } 2392 2393 ccb = ocb->ccb; 2394 prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2395 if (prev == NULL) 2396 sbp_orb_pointer(ocb->sdev, ocb); 2397 } 2398 2399 static void 2400 sbp_poll(struct cam_sim *sim) 2401 { 2402 /* should call fwohci_intr? */ 2403 return; 2404 } 2405 static struct sbp_ocb * 2406 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status) 2407 { 2408 struct sbp_ocb *ocb; 2409 struct sbp_ocb *next; 2410 int s = splfw(), order = 0; 2411 int flags; 2412 2413 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2414 next = STAILQ_NEXT(ocb, ocb); 2415 flags = ocb->flags; 2416 SBP_DEBUG(1) 2417 sbp_show_sdev_info(sdev, 2); 2418 #if __FreeBSD_version >= 500000 2419 printf("orb: 0x%tx next: 0x%x, flags %x\n", 2420 #else 2421 printf("orb: 0x%x next: 0x%lx, flags %x\n", 2422 #endif 2423 vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags); 2424 END_DEBUG 2425 if (OCB_MATCH(ocb, sbp_status)) { 2426 /* found */ 2427 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2428 if (ocb->ccb != NULL) 2429 untimeout(sbp_timeout, (caddr_t)ocb, 2430 ocb->ccb->ccb_h.timeout_ch); 2431 if (ocb->dmamap != NULL) { 2432 bus_dmamap_destroy(sdev->target->sbp->dmat, 2433 ocb->dmamap); 2434 ocb->dmamap = NULL; 2435 } 2436 if (next != NULL && sbp_status->src == 1) 2437 sbp_orb_pointer(sdev, next); 2438 break; 2439 } else 2440 order ++; 2441 } 2442 splx(s); 2443 SBP_DEBUG(0) 2444 if (ocb && order > 0) { 2445 sbp_show_sdev_info(sdev, 2); 2446 printf("unordered execution order:%d\n", order); 2447 } 2448 END_DEBUG 2449 return (ocb); 2450 } 2451 2452 static struct sbp_ocb * 2453 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2454 { 2455 int s = splfw(); 2456 struct sbp_ocb *prev; 2457 2458 SBP_DEBUG(2) 2459 sbp_show_sdev_info(sdev, 2); 2460 #if __FreeBSD_version >= 500000 2461 printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0])); 2462 #else 2463 printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0])); 2464 #endif 2465 END_DEBUG 2466 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2467 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2468 2469 if (ocb->ccb != NULL) 2470 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2471 (ocb->ccb->ccb_h.timeout * hz) / 1000); 2472 2473 if (prev != NULL ) { 2474 SBP_DEBUG(1) 2475 #if __FreeBSD_version >= 500000 2476 printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]), 2477 #else 2478 printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]), 2479 #endif 2480 vtophys(&ocb->orb[0])); 2481 END_DEBUG 2482 prev->orb[1] = htonl(vtophys(&ocb->orb[0])); 2483 prev->orb[0] = 0; 2484 } 2485 splx(s); 2486 2487 return prev; 2488 } 2489 2490 static struct sbp_ocb * 2491 sbp_get_ocb(struct sbp_softc *sbp) 2492 { 2493 struct sbp_ocb *ocb; 2494 int s = splfw(); 2495 ocb = STAILQ_FIRST(&sbp->free_ocbs); 2496 if (ocb == NULL) { 2497 printf("ocb shortage!!!\n"); 2498 return NULL; 2499 } 2500 STAILQ_REMOVE_HEAD(&sbp->free_ocbs, ocb); 2501 splx(s); 2502 ocb->ccb = NULL; 2503 return (ocb); 2504 } 2505 2506 static void 2507 sbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb) 2508 { 2509 #if 0 /* XXX make sure that ocb has ccb */ 2510 if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 && 2511 (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 2512 ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2513 sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2514 } 2515 #else 2516 if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0) 2517 sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2518 #endif 2519 ocb->flags = 0; 2520 ocb->ccb = NULL; 2521 STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb); 2522 } 2523 2524 static void 2525 sbp_abort_ocb(struct sbp_ocb *ocb, int status) 2526 { 2527 struct sbp_dev *sdev; 2528 2529 sdev = ocb->sdev; 2530 SBP_DEBUG(1) 2531 sbp_show_sdev_info(sdev, 2); 2532 #if __FreeBSD_version >= 500000 2533 printf("sbp_abort_ocb 0x%tx\n", 2534 #else 2535 printf("sbp_abort_ocb 0x%x\n", 2536 #endif 2537 vtophys(&ocb->orb[0])); 2538 if (ocb->ccb != NULL) 2539 sbp_print_scsi_cmd(ocb); 2540 END_DEBUG 2541 if (ocb->ccb != NULL) { 2542 untimeout(sbp_timeout, (caddr_t)ocb, 2543 ocb->ccb->ccb_h.timeout_ch); 2544 ocb->ccb->ccb_h.status = status; 2545 xpt_done(ocb->ccb); 2546 } 2547 if (ocb->dmamap != NULL) { 2548 bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap); 2549 ocb->dmamap = NULL; 2550 } 2551 sbp_free_ocb(sdev->target->sbp, ocb); 2552 } 2553 2554 static void 2555 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2556 { 2557 int s; 2558 struct sbp_ocb *ocb, *next; 2559 STAILQ_HEAD(, sbp_ocb) temp; 2560 2561 s = splfw(); 2562 2563 bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2564 STAILQ_INIT(&sdev->ocbs); 2565 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2566 next = STAILQ_NEXT(ocb, ocb); 2567 sbp_abort_ocb(ocb, status); 2568 } 2569 2570 splx(s); 2571 } 2572 2573 static devclass_t sbp_devclass; 2574 2575 static device_method_t sbp_methods[] = { 2576 /* device interface */ 2577 DEVMETHOD(device_identify, sbp_identify), 2578 DEVMETHOD(device_probe, sbp_probe), 2579 DEVMETHOD(device_attach, sbp_attach), 2580 DEVMETHOD(device_detach, sbp_detach), 2581 DEVMETHOD(device_shutdown, sbp_shutdown), 2582 2583 { 0, 0 } 2584 }; 2585 2586 static driver_t sbp_driver = { 2587 "sbp", 2588 sbp_methods, 2589 sizeof(struct sbp_softc), 2590 }; 2591 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2592 MODULE_VERSION(sbp, 1); 2593 MODULE_DEPEND(sbp, firewire, 1, 1, 1); 2594 MODULE_DEPEND(sbp, cam, 1, 1, 1); 2595