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