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