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