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