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