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