sbp.c revision 108280
1103285Sikob/* 2103285Sikob * Copyright (c) 1998,1999,2000,2001 Katsushi Kobayashi and Hidetosh Shimokawa 3103285Sikob * All rights reserved. 4103285Sikob * 5103285Sikob * Redistribution and use in source and binary forms, with or without 6103285Sikob * modification, are permitted provided that the following conditions 7103285Sikob * are met: 8103285Sikob * 1. Redistributions of source code must retain the above copyright 9103285Sikob * notice, this list of conditions and the following disclaimer. 10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 11103285Sikob * notice, this list of conditions and the following disclaimer in the 12103285Sikob * documentation and/or other materials provided with the distribution. 13103285Sikob * 3. All advertising materials mentioning features or use of this software 14103285Sikob * must display the acknowledgement as bellow: 15103285Sikob * 16103285Sikob * This product includes software developed by K. Kobayashi and H. Shimokawa 17103285Sikob * 18103285Sikob * 4. The name of the author may not be used to endorse or promote products 19103285Sikob * derived from this software without specific prior written permission. 20103285Sikob * 21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24103285Sikob * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31103285Sikob * POSSIBILITY OF SUCH DAMAGE. 32103285Sikob * 33103285Sikob * $FreeBSD: head/sys/dev/firewire/sbp.c 108280 2002-12-26 06:41:37Z simokawa $ 34103285Sikob * 35103285Sikob */ 36103285Sikob 37103285Sikob#include <sys/param.h> 38103285Sikob#include <sys/systm.h> 39103285Sikob#include <sys/module.h> 40103285Sikob#include <sys/bus.h> 41103285Sikob#include <sys/mbuf.h> 42103285Sikob#include <sys/sysctl.h> 43103285Sikob#include <machine/bus.h> 44103285Sikob#include <sys/malloc.h> 45103285Sikob#include <sys/devicestat.h> /* for struct devstat */ 46103285Sikob 47103285Sikob 48103285Sikob#include <cam/cam.h> 49103285Sikob#include <cam/cam_ccb.h> 50103285Sikob#include <cam/cam_sim.h> 51103285Sikob#include <cam/cam_xpt_sim.h> 52103285Sikob#include <cam/cam_debug.h> 53103285Sikob#include <cam/cam_periph.h> 54103285Sikob 55103285Sikob#include <cam/scsi/scsi_all.h> 56103285Sikob#include <cam/scsi/scsi_message.h> 57103285Sikob#include <cam/scsi/scsi_da.h> 58103285Sikob 59103285Sikob#include <sys/kernel.h> 60103285Sikob 61103285Sikob#include <vm/vm.h> 62103285Sikob#include <vm/pmap.h> 63103285Sikob 64103285Sikob#include <dev/firewire/firewire.h> 65103285Sikob#include <dev/firewire/firewirereg.h> 66103285Sikob#include <dev/firewire/iec13213.h> 67103285Sikob 68103285Sikob#define ccb_sdev_ptr spriv_ptr0 69103285Sikob#define ccb_sbp_ptr spriv_ptr1 70103285Sikob 71103285Sikob#define SBP_NUM_TARGETS 8 72103285Sikob#define SBP_NUM_LUNS 8 /* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */ 73103285Sikob#define SBP_QUEUE_LEN 4 74103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS) 75103285Sikob#define SBP_INITIATOR 7 76103285Sikob#define SBP_ESELECT_TIMEOUT 1 77103285Sikob#define SBP_BIND_HI 0x1 78103285Sikob#define SBP_DEV2ADDR(u, t, l) \ 79103285Sikob ((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2)) 80103285Sikob#define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f) 81103285Sikob#define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff) 82103285Sikob 83103285Sikob#define ORB_NOTIFY (1 << 31) 84103285Sikob#define ORB_FMT_STD (0 << 29) 85103285Sikob#define ORB_FMT_VED (2 << 29) 86103285Sikob#define ORB_FMT_NOP (3 << 29) 87103285Sikob#define ORB_FMT_MSK (3 << 29) 88103285Sikob#define ORB_EXV (1 << 28) 89103285Sikob/* */ 90103285Sikob#define ORB_CMD_IN (1 << 27) 91103285Sikob/* */ 92103285Sikob#define ORB_CMD_SPD(x) ((x) << 24) 93103285Sikob#define ORB_CMD_MAXP(x) ((x) << 20) 94103285Sikob#define ORB_RCN_TMO(x) ((x) << 20) 95103285Sikob#define ORB_CMD_PTBL (1 << 19) 96103285Sikob#define ORB_CMD_PSZ(x) ((x) << 16) 97103285Sikob 98103285Sikob#define ORB_FUN_LGI (0 << 16) 99103285Sikob#define ORB_FUN_QLG (1 << 16) 100103285Sikob#define ORB_FUN_RCN (3 << 16) 101103285Sikob#define ORB_FUN_LGO (7 << 16) 102103285Sikob#define ORB_FUN_ATA (0xb << 16) 103103285Sikob#define ORB_FUN_ATS (0xc << 16) 104103285Sikob#define ORB_FUN_LUR (0xe << 16) 105103285Sikob#define ORB_FUN_RST (0xf << 16) 106103285Sikob#define ORB_FUN_MSK (0xf << 16) 107103285Sikob 108103285Sikobstatic char *orb_fun_name[] = { 109103285Sikob /* 0 */ "LOGIN", 110103285Sikob /* 1 */ "QUERY LOGINS", 111103285Sikob /* 2 */ "Reserved", 112103285Sikob /* 3 */ "RECONNECT", 113103285Sikob /* 4 */ "SET PASSWORD", 114103285Sikob /* 5 */ "Reserved", 115103285Sikob /* 6 */ "Reserved", 116103285Sikob /* 7 */ "LOGOUT", 117103285Sikob /* 8 */ "Reserved", 118103285Sikob /* 9 */ "Reserved", 119103285Sikob /* A */ "Reserved", 120103285Sikob /* B */ "ABORT TASK", 121103285Sikob /* C */ "ABORT TASK SET", 122103285Sikob /* D */ "Reserved", 123103285Sikob /* E */ "LOGICAL UNIT RESET", 124103285Sikob /* F */ "TARGET RESET" 125103285Sikob}; 126103285Sikob 127103285Sikob#define ORB_RES_CMPL 0 128103285Sikob#define ORB_RES_FAIL 1 129103285Sikob#define ORB_RES_ILLE 2 130103285Sikob#define ORB_RES_VEND 3 131103285Sikob 132107653Ssimokawastatic int debug = 0; 133103285Sikobstatic int auto_login = 1; 134103285Sikobstatic int max_speed = 2; 135103285Sikob 136103285SikobSYSCTL_DECL(_hw_firewire); 137103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem"); 138103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0, 139103285Sikob "SBP debug flag"); 140103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0, 141103285Sikob "SBP perform login automatically"); 142103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0, 143103285Sikob "SBP transfer max speed"); 144103285Sikob 145103285Sikob#define SBP_DEBUG(x) if (debug > x) { 146103285Sikob#define END_DEBUG } 147103285Sikob 148103285Sikob#define NEED_RESPONSE 0 149103285Sikob 150103285Sikobstruct ind_ptr { 151103285Sikob u_int32_t hi,lo; 152103285Sikob}; 153103285Sikob#define SBP_IND_MAX 0x20 154103285Sikobstruct sbp_ocb { 155103285Sikob STAILQ_ENTRY(sbp_ocb) ocb; 156103285Sikob union ccb *ccb; 157103285Sikob volatile u_int32_t orb[8]; 158103285Sikob volatile struct ind_ptr ind_ptr[SBP_IND_MAX]; 159103285Sikob struct sbp_dev *sdev; 160103285Sikob int flags; 161103285Sikob bus_dmamap_t dmamap; 162103285Sikob}; 163103285Sikob#define OCB_ACT_MGM 0 164103285Sikob#define OCB_ACT_CMD 1 165103285Sikob#define OCB_ACT_MASK 3 166103285Sikob#define OCB_RESERVED 0x10 167103285Sikob#define OCB_DONE 0x20 168103285Sikob 169103285Sikob#define SBP_RESOURCE_SHORTAGE 0x10 170103285Sikob 171103285Sikobstruct sbp_login_res{ 172103285Sikob#if FW_ENDIANSWAP == 0 && BYTE_ORDER == LITTLE_ENDIAN 173103285Sikob u_int16_t len; 174103285Sikob u_int16_t id; 175103285Sikob u_int16_t res0; 176103285Sikob u_int16_t cmd_hi; 177103285Sikob u_int32_t cmd_lo; 178103285Sikob u_int16_t res1; 179103285Sikob u_int16_t recon_hold; 180103285Sikob#else 181103285Sikob u_int16_t id; 182103285Sikob u_int16_t len; 183103285Sikob u_int16_t cmd_hi; 184103285Sikob u_int16_t res0; 185103285Sikob u_int32_t cmd_lo; 186103285Sikob u_int16_t recon_hold; 187103285Sikob u_int16_t res1; 188103285Sikob#endif 189103285Sikob}; 190103285Sikobstruct sbp_status{ 191103285Sikob#if FW_ENDIANSWAP == 0 && BYTE_ORDER == LITTLE_ENDIAN 192103285Sikob u_int8_t len:3, 193103285Sikob dead:1, 194103285Sikob resp:2, 195103285Sikob src:2; 196103285Sikob u_int8_t status:8; 197103285Sikob u_int16_t orb_hi; 198103285Sikob u_int32_t orb_lo; 199103285Sikob u_int32_t data[6]; 200103285Sikob#else 201103285Sikob u_int16_t orb_hi; 202103285Sikob u_int8_t status:8; 203103285Sikob u_int8_t len:3, 204103285Sikob dead:1, 205103285Sikob resp:2, 206103285Sikob src:2; 207103285Sikob u_int32_t orb_lo; 208103285Sikob u_int32_t data[6]; 209103285Sikob#endif 210103285Sikob}; 211103285Sikobstruct sbp_cmd_status{ 212103285Sikob#define SBP_SFMT_CURR 0 213103285Sikob#define SBP_SFMT_DEFER 1 214103285Sikob#if FW_ENDIANSWAP == 0 && BYTE_ORDER == LITTLE_ENDIAN 215103285Sikob u_int8_t status:6, 216103285Sikob sfmt:2; 217103285Sikob u_int8_t s_key:4, 218103285Sikob ill_len:1, 219103285Sikob eom:1, 220103285Sikob mark:1, 221103285Sikob valid:1; 222103285Sikob u_int8_t s_code; 223103285Sikob u_int8_t s_qlfr; 224103285Sikob u_int32_t info; 225103285Sikob u_int32_t cdb; 226103285Sikob u_int32_t fru:8, 227103285Sikob s_keydep:24; 228103285Sikob u_int32_t vend[2]; 229103285Sikob#else 230103285Sikob u_int8_t s_qlfr; 231103285Sikob u_int8_t s_code; 232103285Sikob u_int8_t s_key:4, 233103285Sikob ill_len:1, 234103285Sikob eom:1, 235103285Sikob mark:1, 236103285Sikob valid:1; 237103285Sikob u_int8_t status:6, 238103285Sikob sfmt:2; 239103285Sikob u_int32_t info; 240103285Sikob u_int32_t cdb; 241103285Sikob u_int32_t s_keydep:24, 242103285Sikob fru:8; 243103285Sikob u_int32_t vend[2]; 244103285Sikob#endif 245103285Sikob}; 246103285Sikob 247103285Sikobstruct sbp_dev{ 248103285Sikob#define SBP_DEV_RESET 0 /* accept login */ 249103285Sikob#define SBP_DEV_LOGIN 1 /* to login */ 250103285Sikob#define SBP_DEV_RECONN 2 /* to reconnect */ 251103285Sikob#define SBP_DEV_TOATTACH 3 /* to attach */ 252103285Sikob#define SBP_DEV_PROBE 4 /* scan lun */ 253103285Sikob#define SBP_DEV_ATTACHED 5 /* in operation */ 254103285Sikob#define SBP_DEV_DEAD 6 /* unavailable unit */ 255103285Sikob#define SBP_DEV_RETRY 7 /* unavailable unit */ 256103285Sikob int status; 257103285Sikob int lun_id; 258103285Sikob struct cam_path *path; 259103285Sikob struct sbp_target *target; 260103285Sikob struct sbp_login_res login; 261103285Sikob STAILQ_HEAD(, sbp_ocb) ocbs; 262103285Sikob char vendor[32]; 263103285Sikob char product[32]; 264103285Sikob char revision[10]; 265103285Sikob}; 266103285Sikob 267103285Sikobstruct sbp_target { 268103285Sikob int target_id; 269103285Sikob int num_lun; 270103285Sikob struct sbp_dev *luns; 271103285Sikob struct sbp_softc *sbp; 272103285Sikob struct fw_device *fwdev; 273103285Sikob u_int32_t mgm_hi, mgm_lo; 274103285Sikob}; 275103285Sikob 276103285Sikobstruct sbp_softc { 277103285Sikob struct firewire_dev_comm fd; 278103285Sikob unsigned char flags; 279103285Sikob struct cam_sim *sim; 280103285Sikob struct sbp_target targets[SBP_NUM_TARGETS]; 281103285Sikob struct fw_bind fwb; 282103285Sikob STAILQ_HEAD(, sbp_ocb) free_ocbs; 283103285Sikob struct sbp_ocb *ocb; 284103285Sikob bus_dma_tag_t dmat; 285103285Sikob}; 286103285Sikobstatic void sbp_post_explore __P((void *)); 287103285Sikobstatic void sbp_recv __P((struct fw_xfer *)); 288103285Sikobstatic void sbp_login_callback __P((struct fw_xfer *)); 289103285Sikobstatic void sbp_cmd_callback __P((struct fw_xfer *)); 290103285Sikobstatic void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *)); 291103285Sikobstatic void sbp_execute_ocb __P((void *, bus_dma_segment_t *, int, int)); 292103285Sikobstatic void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *)); 293103285Sikobstatic void sbp_abort_ocb __P((struct sbp_ocb *, int)); 294103285Sikobstatic void sbp_abort_all_ocbs __P((struct sbp_dev *, int)); 295103285Sikobstatic struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int)); 296103285Sikobstatic struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *)); 297103285Sikobstatic struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *)); 298103285Sikobstatic struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, u_int32_t)); 299103285Sikobstatic void sbp_detach_target __P((struct sbp_target *)); 300103285Sikobstatic void sbp_timeout __P((void *arg)); 301103285Sikobstatic void sbp_mgm_orb __P((struct sbp_dev *, int)); 302103285Sikob 303103285SikobMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/Firewire"); 304103285Sikob 305103285Sikob/* cam related functions */ 306103285Sikobstatic void sbp_action(struct cam_sim *sim, union ccb *ccb); 307103285Sikobstatic void sbp_poll(struct cam_sim *sim); 308103285Sikobstatic void sbp_cam_callback(struct cam_periph *periph, 309103285Sikob union ccb *ccb); 310103285Sikobstatic void sbp_cam_scan_lun(struct sbp_dev *sdev); 311103285Sikob 312103285Sikobstatic char *orb_status0[] = { 313103285Sikob /* 0 */ "No additional information to report", 314103285Sikob /* 1 */ "Request type not supported", 315103285Sikob /* 2 */ "Speed not supported", 316103285Sikob /* 3 */ "Page size not supported", 317103285Sikob /* 4 */ "Access denied", 318103285Sikob /* 5 */ "Logical unit not supported", 319103285Sikob /* 6 */ "Maximum payload too small", 320103285Sikob /* 7 */ "Reserved for future standardization", 321103285Sikob /* 8 */ "Resources unavailable", 322103285Sikob /* 9 */ "Function rejected", 323103285Sikob /* A */ "Login ID not recognized", 324103285Sikob /* B */ "Dummy ORB completed", 325103285Sikob /* C */ "Request aborted", 326103285Sikob /* FF */ "Unspecified error" 327103285Sikob#define MAX_ORB_STATUS0 0xd 328103285Sikob}; 329103285Sikob 330103285Sikobstatic char *orb_status1_object[] = { 331103285Sikob /* 0 */ "Operation request block (ORB)", 332103285Sikob /* 1 */ "Data buffer", 333103285Sikob /* 2 */ "Page table", 334103285Sikob /* 3 */ "Unable to specify" 335103285Sikob}; 336103285Sikob 337103285Sikobstatic char *orb_status1_serial_bus_error[] = { 338103285Sikob /* 0 */ "Missing acknowledge", 339103285Sikob /* 1 */ "Reserved; not to be used", 340103285Sikob /* 2 */ "Time-out error", 341103285Sikob /* 3 */ "Reserved; not to be used", 342103285Sikob /* 4 */ "Busy retry limit exceeded(X)", 343103285Sikob /* 5 */ "Busy retry limit exceeded(A)", 344103285Sikob /* 6 */ "Busy retry limit exceeded(B)", 345103285Sikob /* 7 */ "Reserved for future standardization", 346103285Sikob /* 8 */ "Reserved for future standardization", 347103285Sikob /* 9 */ "Reserved for future standardization", 348103285Sikob /* A */ "Reserved for future standardization", 349103285Sikob /* B */ "Tardy retry limit exceeded", 350103285Sikob /* C */ "Conflict error", 351103285Sikob /* D */ "Data error", 352103285Sikob /* E */ "Type error", 353103285Sikob /* F */ "Address error" 354103285Sikob}; 355103285Sikob 356103285Sikobstatic void 357103285Sikobsbp_identify(driver_t *driver, device_t parent) 358103285Sikob{ 359103285Sikob device_t child; 360103285SikobSBP_DEBUG(0) 361103285Sikob printf("sbp_identify\n"); 362103285SikobEND_DEBUG 363103285Sikob 364103285Sikob child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 365103285Sikob} 366103285Sikob 367103285Sikob/* 368103285Sikob * sbp_probe() 369103285Sikob */ 370103285Sikobstatic int 371103285Sikobsbp_probe(device_t dev) 372103285Sikob{ 373103285Sikob device_t pa; 374103285Sikob 375103285SikobSBP_DEBUG(0) 376103285Sikob printf("sbp_probe\n"); 377103285SikobEND_DEBUG 378103285Sikob 379103285Sikob pa = device_get_parent(dev); 380103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 381103285Sikob return(ENXIO); 382103285Sikob } 383103285Sikob 384103285Sikob device_set_desc(dev, "SBP2/SCSI over firewire"); 385103285Sikob return (0); 386103285Sikob} 387103285Sikob 388103285Sikobstatic void 389103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new) 390103285Sikob{ 391103285Sikob int lun; 392103285Sikob struct fw_device *fwdev; 393103285Sikob 394103285Sikob printf("%s:%d:%d ", 395103285Sikob device_get_nameunit(sdev->target->sbp->fd.dev), 396103285Sikob sdev->target->target_id, 397103285Sikob sdev->lun_id 398103285Sikob ); 399103285Sikob if (new == 2) { 400103285Sikob return; 401103285Sikob } 402103285Sikob fwdev = sdev->target->fwdev; 403103285Sikob lun = getcsrdata(fwdev, 0x14); 404103285Sikob printf("ordered:%d type:%d EUI:%08x%08x node:%d " 405103285Sikob "speed:%d maxrec:%d", 406103285Sikob (lun & 0x00400000) >> 22, 407103285Sikob (lun & 0x001f0000) >> 16, 408103285Sikob fwdev->eui.hi, 409103285Sikob fwdev->eui.lo, 410103285Sikob fwdev->dst, 411103285Sikob fwdev->speed, 412103285Sikob fwdev->maxrec 413103285Sikob ); 414103285Sikob if (new) 415103285Sikob printf(" new!\n"); 416103285Sikob else 417103285Sikob printf("\n"); 418103285Sikob sbp_show_sdev_info(sdev, 2); 419103285Sikob printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 420103285Sikob} 421103285Sikob 422103285Sikobstatic struct sbp_target * 423103285Sikobsbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 424103285Sikob{ 425103285Sikob int i, lun; 426103285Sikob struct sbp_target *target; 427103285Sikob struct sbp_dev *sdev; 428103285Sikob 429103285SikobSBP_DEBUG(1) 430103285Sikob printf("sbp_alloc_target\n"); 431103285SikobEND_DEBUG 432103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i++) 433103285Sikob if(sbp->targets[i].fwdev == NULL) break; 434103285Sikob if (i == SBP_NUM_TARGETS) { 435103285Sikob printf("increase SBP_NUM_TARGETS!\n"); 436103285Sikob return NULL; 437103285Sikob } 438103285Sikob /* new target */ 439103285Sikob target = &sbp->targets[i]; 440103285Sikob target->sbp = sbp; 441103285Sikob target->fwdev = fwdev; 442103285Sikob target->target_id = i; 443103285Sikob if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){ 444103285Sikob /* bad target */ 445103285Sikob printf("NULL management address\n"); 446103285Sikob target->fwdev = NULL; 447103285Sikob return NULL; 448103285Sikob } 449103285Sikob target->mgm_hi = 0xffff; 450103285Sikob target->mgm_lo = 0xf0000000 | target->mgm_lo << 2; 451103285Sikob /* XXX should probe all luns */ 452103285Sikob /* XXX num_lun may be changed. realloc luns? */ 453103285Sikob lun = getcsrdata(target->fwdev, 0x14) & 0xff; 454103285Sikob target->num_lun = lun + 1; 455103285Sikob target->luns = (struct sbp_dev *) malloc( 456103285Sikob sizeof(struct sbp_dev) * target->num_lun, 457103285Sikob M_SBP, M_NOWAIT | M_ZERO); 458103285Sikob for (i = 0; i < target->num_lun; i++) { 459103285Sikob sdev = &target->luns[i]; 460103285Sikob sdev->lun_id = i; 461103285Sikob sdev->target = target; 462103285Sikob STAILQ_INIT(&sdev->ocbs); 463103285Sikob if (i == lun) 464103285Sikob sdev->status = SBP_DEV_RESET; 465103285Sikob else 466103285Sikob sdev->status = SBP_DEV_DEAD; 467103285Sikob } 468103285Sikob return target; 469103285Sikob} 470103285Sikob 471103285Sikobstatic void 472103285Sikobsbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len) 473103285Sikob{ 474103285Sikob static char *nullstr = "(null)"; 475103285Sikob int i, clen, found=0; 476103285Sikob struct csrhdr *chdr; 477103285Sikob struct csrreg *creg; 478103285Sikob u_int32_t *src, *dst; 479103285Sikob 480103285Sikob chdr = (struct csrhdr *)&fwdev->csrrom[0]; 481103285Sikob creg = (struct csrreg *)chdr; 482103285Sikob creg += chdr->info_len; 483103285Sikob for( i = chdr->info_len + 4; i <= fwdev->rommax; i+=4){ 484103285Sikob if((creg++)->key == key){ 485103285Sikob found = 1; 486103285Sikob break; 487103285Sikob } 488103285Sikob } 489103285Sikob if (!found) { 490103285Sikob strncpy(buf, nullstr, len); 491103285Sikob return; 492103285Sikob } 493103285Sikob src = (u_int32_t *) creg + creg->val; 494103285Sikob clen = ((*src >> 16) - 2) * 4; 495103285Sikob src += 3; 496103285Sikob dst = (u_int32_t *) buf; 497103285Sikob if (len < clen) 498103285Sikob clen = len; 499103285Sikob for (i = 0; i < clen/4; i++) 500103285Sikob *dst++ = htonl(*src++); 501103285Sikob buf[clen] = 0; 502103285Sikob} 503103285Sikob 504103285Sikobstatic void 505103285Sikobsbp_probe_lun(struct sbp_dev *sdev) 506103285Sikob{ 507103285Sikob struct fw_device *fwdev; 508103285Sikob int rev; 509103285Sikob 510103285Sikob fwdev = sdev->target->fwdev; 511103285Sikob bzero(sdev->vendor, sizeof(sdev->vendor)); 512103285Sikob bzero(sdev->product, sizeof(sdev->product)); 513103285Sikob sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor)); 514103285Sikob sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product)); 515103285Sikob rev = getcsrdata(sdev->target->fwdev, 0x3c); 516103285Sikob snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev); 517103285Sikob} 518103285Sikobstatic void 519103285Sikobsbp_probe_target(struct sbp_target *target, int alive) 520103285Sikob{ 521103285Sikob struct sbp_softc *sbp; 522103285Sikob struct sbp_dev *sdev; 523103285Sikob struct firewire_comm *fc; 524103285Sikob int i; 525103285Sikob 526103285SikobSBP_DEBUG(1) 527103285Sikob printf("sbp_probe_target %d\n", target->target_id); 528103285Sikob if (!alive) 529103285Sikob printf("not alive\n"); 530103285SikobEND_DEBUG 531103285Sikob 532103285Sikob sbp = target->sbp; 533103285Sikob fc = target->sbp->fd.fc; 534103285Sikob for (i=0; i < target->num_lun; i++) { 535103285Sikob sdev = &target->luns[i]; 536103285Sikob if (alive && (sdev->status != SBP_DEV_DEAD)) { 537103285Sikob if (sdev->path != NULL) { 538103285Sikob xpt_freeze_devq(sdev->path, 1); 539103285Sikob } 540103285Sikob sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 541103285Sikob switch (sdev->status) { 542103285Sikob case SBP_DEV_ATTACHED: 543103285Sikob sbp_mgm_orb(sdev, ORB_FUN_RCN); 544103285Sikob break; 545103285Sikob case SBP_DEV_RETRY: 546103285Sikob sbp_probe_lun(sdev); 547103285Sikob sbp_mgm_orb(sdev, ORB_FUN_LGI); 548103285Sikob break; 549103285Sikob default: 550103285Sikob /* new or revived target */ 551103285Sikob sbp_probe_lun(sdev); 552103285Sikob if (auto_login) { 553103285Sikob sdev->status = SBP_DEV_TOATTACH; 554103285Sikob sbp_mgm_orb(sdev, ORB_FUN_LGI); 555103285Sikob } 556103285Sikob break; 557103285Sikob } 558107653SsimokawaSBP_DEBUG(0) 559103285Sikob sbp_show_sdev_info(sdev, 560103285Sikob (sdev->status == SBP_DEV_TOATTACH)); 561107653SsimokawaEND_DEBUG 562103285Sikob } else { 563103285Sikob switch (sdev->status) { 564103285Sikob case SBP_DEV_ATTACHED: 565103285SikobSBP_DEBUG(0) 566103285Sikob /* the device has gone */ 567103285Sikob sbp_show_sdev_info(sdev, 2); 568103285Sikob printf("lost target\n"); 569103285SikobEND_DEBUG 570103285Sikob if (sdev->path) 571103285Sikob xpt_freeze_devq(sdev->path, 1); 572103285Sikob sdev->status = SBP_DEV_RETRY; 573103285Sikob sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 574103285Sikob break; 575103285Sikob case SBP_DEV_PROBE: 576103285Sikob case SBP_DEV_TOATTACH: 577103285Sikob sdev->status = SBP_DEV_RESET; 578103285Sikob break; 579103285Sikob case SBP_DEV_RETRY: 580103285Sikob case SBP_DEV_RESET: 581103285Sikob case SBP_DEV_DEAD: 582103285Sikob break; 583103285Sikob } 584103285Sikob } 585103285Sikob } 586103285Sikob} 587103285Sikob 588103285Sikob#if 0 589103285Sikobstatic void 590103285Sikobsbp_release_queue(void *arg) 591103285Sikob{ 592103285Sikob struct sbp_softc *sbp; 593103285Sikob 594103285SikobSBP_DEBUG(0) 595103285Sikob printf("sbp_release_queue\n"); 596103285SikobEND_DEBUG 597103285Sikob sbp = (struct sbp_softc *)arg; 598103285Sikob xpt_release_simq(sbp->sim, 1); 599103285Sikob} 600103285Sikob 601103285Sikobstatic void 602103285Sikobsbp_release_devq(void *arg) 603103285Sikob{ 604103285Sikob struct sbp_dev *sdev; 605103285Sikob int s; 606103285Sikob 607103285Sikob sdev = (struct sbp_dev *)arg; 608103285SikobSBP_DEBUG(0) 609103285Sikob sbp_show_sdev_info(sdev, 2); 610103285Sikob printf("sbp_release_devq\n"); 611103285SikobEND_DEBUG 612103285Sikob s = splcam(); 613103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 614103285Sikob splx(s); 615103285Sikob} 616103285Sikob#endif 617103285Sikob 618103285Sikobstatic void 619103285Sikobsbp_post_explore(void *arg) 620103285Sikob{ 621103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)arg; 622103285Sikob struct sbp_target *target; 623103285Sikob struct fw_device *fwdev; 624103285Sikob int i, alive; 625103285Sikob 626103285SikobSBP_DEBUG(1) 627103285Sikob printf("sbp_post_explore\n"); 628103285SikobEND_DEBUG 629103285Sikob#if 0 630103285Sikob xpt_freeze_simq(sbp->sim, /*count*/ 1); 631103285Sikob#endif 632103285Sikob /* Gabage Collection */ 633103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 634103285Sikob target = &sbp->targets[i]; 635103285Sikob for( fwdev = TAILQ_FIRST(&sbp->fd.fc->devices); 636103285Sikob fwdev != NULL; fwdev = TAILQ_NEXT(fwdev, link)){ 637103285Sikob if(target->fwdev == NULL) break; 638103285Sikob if(target->fwdev == fwdev) break; 639103285Sikob } 640103285Sikob if(fwdev == NULL){ 641103285Sikob /* device has removed in lower driver */ 642103285Sikob sbp_detach_target(target); 643103285Sikob } 644103285Sikob } 645103285Sikob /* traverse device list */ 646103285Sikob for( fwdev = TAILQ_FIRST(&sbp->fd.fc->devices); 647103285Sikob fwdev != NULL; fwdev = TAILQ_NEXT(fwdev, link)){ 648103285SikobSBP_DEBUG(0) 649103285Sikob printf("sbp_post_explore: EUI:%08x%08x ", 650103285Sikob fwdev->eui.hi, fwdev->eui.lo); 651103285Sikob if (fwdev->status == FWDEVATTACHED) { 652103285Sikob printf("spec=%d key=%d.\n", 653103285Sikob getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10, 654103285Sikob getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 655103285Sikob } else { 656103285Sikob printf("not attached, state=%d.\n", fwdev->status); 657103285Sikob } 658103285SikobEND_DEBUG 659103285Sikob alive = (fwdev->status == FWDEVATTACHED) 660103285Sikob && (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10) 661103285Sikob && (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 662103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 663103285Sikob target = &sbp->targets[i]; 664103285Sikob if(target->fwdev == fwdev ) { 665103285Sikob /* known target */ 666103285Sikob break; 667103285Sikob } 668103285Sikob } 669103285Sikob if(i == SBP_NUM_TARGETS){ 670103285Sikob if (alive) { 671103285Sikob /* new target */ 672103285Sikob target = sbp_alloc_target(sbp, fwdev); 673103285Sikob if (target == NULL) 674103285Sikob continue; 675103285Sikob } else { 676103285Sikob continue; 677103285Sikob } 678103285Sikob } 679103285Sikob sbp_probe_target(target, alive); 680103285Sikob } 681103285Sikob#if 0 682103285Sikob timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000); 683103285Sikob#endif 684103285Sikob} 685103285Sikob 686103285Sikob#if NEED_RESPONSE 687103285Sikobstatic void 688103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){ 689103285SikobSBP_DEBUG(1) 690103285Sikob struct sbp_dev *sdev; 691103285Sikob sdev = (struct sbp_dev *)xfer->sc; 692103285Sikob sbp_show_sdev_info(sdev, 2); 693103285Sikob printf("sbp_loginres_callback\n"); 694103285SikobEND_DEBUG 695103285Sikob fw_xfer_free(xfer); 696103285Sikob return; 697103285Sikob} 698103285Sikob#endif 699103285Sikob 700103285Sikobstatic void 701103285Sikobsbp_login_callback(struct fw_xfer *xfer) 702103285Sikob{ 703103285SikobSBP_DEBUG(1) 704103285Sikob struct sbp_dev *sdev; 705103285Sikob sdev = (struct sbp_dev *)xfer->sc; 706103285Sikob sbp_show_sdev_info(sdev, 2); 707103285Sikob printf("sbp_login_callback\n"); 708103285SikobEND_DEBUG 709103285Sikob fw_xfer_free(xfer); 710103285Sikob return; 711103285Sikob} 712103285Sikob 713103285Sikobstatic void 714103285Sikobsbp_cmd_callback(struct fw_xfer *xfer) 715103285Sikob{ 716103285SikobSBP_DEBUG(2) 717103285Sikob struct sbp_dev *sdev; 718103285Sikob sdev = (struct sbp_dev *)xfer->sc; 719103285Sikob sbp_show_sdev_info(sdev, 2); 720103285Sikob printf("sbp_cmd_callback\n"); 721103285SikobEND_DEBUG 722103285Sikob fw_xfer_free(xfer); 723103285Sikob return; 724103285Sikob} 725103285Sikob 726103285Sikobstatic void 727103285Sikobsbp_cam_callback(struct cam_periph *periph, union ccb *ccb) 728103285Sikob{ 729103285Sikob struct sbp_dev *sdev; 730103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 731103285SikobSBP_DEBUG(1) 732103285Sikob sbp_show_sdev_info(sdev, 2); 733103285Sikob printf("sbp_cam_callback\n"); 734103285SikobEND_DEBUG 735103285Sikob sdev->status = SBP_DEV_ATTACHED; 736103285Sikob free(ccb, M_SBP); 737103285Sikob} 738103285Sikob 739103285Sikobstatic void 740103285Sikobsbp_cam_scan_lun(struct sbp_dev *sdev) 741103285Sikob{ 742103285Sikob union ccb *ccb = malloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO); 743103285Sikob 744103285SikobSBP_DEBUG(0) 745103285Sikob sbp_show_sdev_info(sdev, 2); 746103285Sikob printf("sbp_cam_scan_lun\n"); 747103285SikobEND_DEBUG 748103285Sikob xpt_setup_ccb(&ccb->ccb_h, sdev->path, 5/*priority (low)*/); 749103285Sikob ccb->ccb_h.func_code = XPT_SCAN_LUN; 750103285Sikob ccb->ccb_h.cbfcnp = sbp_cam_callback; 751103285Sikob ccb->crcn.flags = CAM_FLAG_NONE; 752103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 753103285Sikob xpt_action(ccb); 754103285Sikob 755103285Sikob /* The scan is in progress now. */ 756103285Sikob} 757103285Sikob 758103285Sikob 759103285Sikobstatic void 760103285Sikobsbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb) 761103285Sikob{ 762103285Sikob struct sbp_dev *sdev; 763103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 764103285SikobSBP_DEBUG(1) 765103285Sikob sbp_show_sdev_info(sdev, 2); 766103285Sikob printf("sbp_ping_unit_callback\n"); 767103285SikobEND_DEBUG 768103285Sikob if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 769103285Sikob if (--ccb->ccb_h.retry_count == 0) { 770103285Sikob sbp_show_sdev_info(sdev, 2); 771103285Sikob printf("sbp_tur_callback: retry count exceeded\n"); 772103285Sikob sdev->status = SBP_DEV_RETRY; 773103285Sikob free(ccb, M_SBP); 774103285Sikob } else { 775103285Sikob /* requeue */ 776103285Sikob xpt_action(ccb); 777103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 778103285Sikob } 779103285Sikob } else { 780103285Sikob free(ccb->csio.data_ptr, M_SBP); 781103285Sikob free(ccb, M_SBP); 782103285Sikob sdev->status = SBP_DEV_ATTACHED; 783103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 784103285Sikob } 785103285Sikob} 786103285Sikob 787103285Sikob/* 788103285Sikob * XXX Some devices need to execute inquiry or read_capacity 789103285Sikob * after bus_rest during busy transfer. 790103285Sikob * Otherwise they return incorrect result for READ(and WRITE?) 791103285Sikob * command without any SBP-II/SCSI error. 792103285Sikob * 793103285Sikob * e.g. Maxtor 3000XT, Yano A-dish. 794103285Sikob */ 795103285Sikobstatic void 796103285Sikobsbp_ping_unit(struct sbp_dev *sdev) 797103285Sikob{ 798103285Sikob union ccb *ccb; 799103285Sikob struct scsi_inquiry_data *inq_buf; 800103285Sikob 801103285Sikob ccb = malloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO); 802103285Sikob inq_buf = (struct scsi_inquiry_data *) 803103285Sikob malloc(sizeof(*inq_buf), M_SBP, M_WAITOK); 804103285Sikob 805103285SikobSBP_DEBUG(1) 806103285Sikob sbp_show_sdev_info(sdev, 2); 807103285Sikob printf("sbp_ping_unit\n"); 808103285SikobEND_DEBUG 809103285Sikob 810103285Sikob /* 811103285Sikob * We need to execute this command before any other queued command. 812103285Sikob * Make priority 0 and freeze queue after execution for retry. 813103285Sikob * cam's scan_lun command doesn't provide this feature. 814103285Sikob */ 815103285Sikob xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/); 816103285Sikob scsi_inquiry( 817103285Sikob &ccb->csio, 818103285Sikob /*retries*/ 5, 819103285Sikob sbp_ping_unit_callback, 820103285Sikob MSG_SIMPLE_Q_TAG, 821103285Sikob (u_int8_t *)inq_buf, 822103285Sikob SHORT_INQUIRY_LENGTH, 823103285Sikob /*evpd*/FALSE, 824103285Sikob /*page_code*/0, 825103285Sikob SSD_MIN_SIZE, 826103285Sikob /*timeout*/60000 827103285Sikob ); 828103285Sikob ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 829103285Sikob xpt_action(ccb); 830103285Sikob} 831103285Sikob 832103285Sikobstatic void 833103285Sikobsbp_do_attach(struct fw_xfer *xfer) 834103285Sikob{ 835103285Sikob struct sbp_dev *sdev; 836103285Sikob 837103285Sikob sdev = (struct sbp_dev *)xfer->sc; 838103285SikobSBP_DEBUG(0) 839103285Sikob sbp_show_sdev_info(sdev, 2); 840103285Sikob printf("sbp_do_attach\n"); 841103285SikobEND_DEBUG 842103285Sikob fw_xfer_free(xfer); 843103285Sikob if (sdev->path == NULL) 844103285Sikob xpt_create_path(&sdev->path, xpt_periph, 845103285Sikob cam_sim_path(sdev->target->sbp->sim), 846103285Sikob sdev->target->target_id, sdev->lun_id); 847103285Sikob 848103285Sikob if (sdev->status == SBP_DEV_RETRY) { 849103285Sikob sdev->status = SBP_DEV_PROBE; 850103285Sikob sbp_ping_unit(sdev); 851103285Sikob /* freezed twice */ 852103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 853103285Sikob } else { 854103285Sikob sdev->status = SBP_DEV_PROBE; 855103285Sikob sbp_cam_scan_lun(sdev); 856103285Sikob } 857103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 858103285Sikob return; 859103285Sikob} 860103285Sikob 861103285Sikobstatic void 862103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer) 863103285Sikob{ 864103285Sikob struct sbp_dev *sdev; 865103285Sikob 866103285Sikob sdev = (struct sbp_dev *)xfer->sc; 867103285SikobSBP_DEBUG(1) 868103285Sikob sbp_show_sdev_info(sdev, 2); 869103285Sikob printf("sbp_cmd_callback\n"); 870103285SikobEND_DEBUG 871103285Sikob fw_xfer_free(xfer); 872103285Sikob sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 873103285Sikob if (sdev->path) 874103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 875103285Sikob} 876103285Sikob 877103285Sikobstatic void 878103285Sikobsbp_agent_reset(struct sbp_dev *sdev, int attach) 879103285Sikob{ 880103285Sikob struct fw_xfer *xfer; 881103285Sikob struct fw_pkt *fp; 882103285Sikob 883103285SikobSBP_DEBUG(0) 884103285Sikob sbp_show_sdev_info(sdev, 2); 885103285Sikob printf("sbp_agent_reset\n"); 886103285SikobEND_DEBUG 887103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 888103285Sikob if (xfer == NULL) 889103285Sikob return; 890103285Sikob if (attach) 891103285Sikob xfer->act.hand = sbp_do_attach; 892103285Sikob else 893103285Sikob xfer->act.hand = sbp_agent_reset_callback; 894103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 895103285Sikob fp->mode.wreqq.data = htonl(0xf); 896103285Sikob fw_asyreq(xfer->fc, -1, xfer); 897103285Sikob} 898103285Sikob 899103285Sikobstatic void 900103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer) 901103285Sikob{ 902103285Sikob struct sbp_dev *sdev; 903103285Sikob 904103285Sikob sdev = (struct sbp_dev *)xfer->sc; 905103285SikobSBP_DEBUG(1) 906103285Sikob sbp_show_sdev_info(sdev, 2); 907103285Sikob printf("sbp_but_timeout_callback\n"); 908103285SikobEND_DEBUG 909103285Sikob fw_xfer_free(xfer); 910103285Sikob sbp_agent_reset(sdev, 1); 911103285Sikob} 912103285Sikob 913103285Sikobstatic void 914103285Sikobsbp_busy_timeout(struct sbp_dev *sdev) 915103285Sikob{ 916103285Sikob struct fw_pkt *fp; 917103285Sikob struct fw_xfer *xfer; 918103285SikobSBP_DEBUG(0) 919103285Sikob sbp_show_sdev_info(sdev, 2); 920103285Sikob printf("sbp_busy_timeout\n"); 921103285SikobEND_DEBUG 922103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 923103285Sikob 924103285Sikob xfer->act.hand = sbp_busy_timeout_callback; 925103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 926103285Sikob fp->mode.wreqq.dest_hi = htons(0xffff); 927103285Sikob fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUS_TIME); 928103285Sikob fp->mode.wreqq.data = htonl(0xf); 929103285Sikob fw_asyreq(xfer->fc, -1, xfer); 930103285Sikob} 931103285Sikob 932103285Sikob#if 0 933103285Sikobstatic void 934103285Sikobsbp_reset_start(struct sbp_dev *sdev) 935103285Sikob{ 936103285Sikob struct fw_xfer *xfer; 937103285Sikob struct fw_pkt *fp; 938103285Sikob 939103285SikobSBP_DEBUG(0) 940103285Sikob sbp_show_sdev_info(sdev, 2); 941103285Sikob printf("sbp_reset_start\n"); 942103285SikobEND_DEBUG 943103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 944103285Sikob 945103285Sikob xfer->act.hand = sbp_busy_timeout; 946103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 947103285Sikob fp->mode.wreqq.dest_hi = htons(0xffff); 948103285Sikob fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START); 949103285Sikob fp->mode.wreqq.data = htonl(0xf); 950103285Sikob fw_asyreq(xfer->fc, -1, xfer); 951103285Sikob} 952103285Sikob#endif 953103285Sikob 954103285Sikobstatic void 955103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 956103285Sikob{ 957103285Sikob struct fw_xfer *xfer; 958103285Sikob struct fw_pkt *fp; 959103285SikobSBP_DEBUG(2) 960103285Sikob sbp_show_sdev_info(sdev, 2); 961103285Sikob printf("sbp_orb_pointer\n"); 962103285SikobEND_DEBUG 963103285Sikob 964103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 965103285Sikob if (xfer == NULL) 966103285Sikob return; 967103285Sikob xfer->act.hand = sbp_cmd_callback; 968103285Sikob 969103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 970103285Sikob fp->mode.wreqb.len = htons(8); 971103285Sikob fp->mode.wreqb.extcode = 0; 972103285Sikob fp->mode.wreqb.payload[0] = 973103285Sikob htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 974103285Sikob fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 975103285Sikob 976103285Sikob if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 977103285Sikob fw_xfer_free(xfer); 978103285Sikob ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 979103285Sikob xpt_done(ocb->ccb); 980103285Sikob } 981103285Sikob} 982103285Sikob 983103285Sikobstatic void 984103285Sikobsbp_doorbell(struct sbp_dev *sdev) 985103285Sikob{ 986103285Sikob struct fw_xfer *xfer; 987103285Sikob struct fw_pkt *fp; 988103285SikobSBP_DEBUG(1) 989103285Sikob sbp_show_sdev_info(sdev, 2); 990103285Sikob printf("sbp_doorbell\n"); 991103285SikobEND_DEBUG 992103285Sikob 993103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 994103285Sikob if (xfer == NULL) 995103285Sikob return; 996103285Sikob xfer->act.hand = sbp_cmd_callback; 997103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 998103285Sikob fp->mode.wreqq.data = htonl(0xf); 999103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1000103285Sikob} 1001103285Sikob 1002103285Sikobstatic struct fw_xfer * 1003103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1004103285Sikob{ 1005103285Sikob struct fw_xfer *xfer; 1006103285Sikob struct fw_pkt *fp; 1007103285Sikob 1008103285Sikob xfer = fw_xfer_alloc(); 1009103285Sikob if(xfer == NULL){ 1010103285Sikob return NULL; 1011103285Sikob } 1012103285Sikob if (tcode == FWTCODE_WREQQ) 1013103285Sikob xfer->send.len = 16; 1014103285Sikob else 1015103285Sikob xfer->send.len = 24; 1016103285Sikob 1017103285Sikob xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT); 1018103285Sikob if(xfer->send.buf == NULL){ 1019103285Sikob fw_xfer_free( xfer); 1020103285Sikob return NULL; 1021103285Sikob } 1022103285Sikob 1023103285Sikob xfer->send.off = 0; 1024103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1025103285Sikob xfer->sc = (caddr_t)sdev; 1026103285Sikob xfer->fc = sdev->target->sbp->fd.fc; 1027103285Sikob xfer->retry_req = fw_asybusy; 1028103285Sikob 1029103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1030103285Sikob fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi); 1031103285Sikob fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset); 1032103285Sikob fp->mode.wreqq.tlrt = 0; 1033103285Sikob fp->mode.wreqq.tcode = tcode; 1034103285Sikob fp->mode.wreqq.pri = 0; 1035103285Sikob xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst; 1036103285Sikob fp->mode.wreqq.dst = htons(xfer->dst); 1037103285Sikob 1038103285Sikob return xfer; 1039103285Sikob 1040103285Sikob} 1041103285Sikob 1042103285Sikobstatic void 1043103285Sikobsbp_mgm_orb(struct sbp_dev *sdev, int func) 1044103285Sikob{ 1045103285Sikob struct fw_xfer *xfer; 1046103285Sikob struct fw_pkt *fp; 1047103285Sikob struct sbp_ocb *ocb; 1048103285Sikob int s, nid; 1049103285Sikob 1050103285Sikob if ((ocb = sbp_get_ocb(sdev->target->sbp)) == NULL) { 1051103285Sikob s = splfw(); 1052103285Sikob sdev->target->sbp->flags |= SBP_RESOURCE_SHORTAGE; 1053103285Sikob splx(s); 1054103285Sikob return; 1055103285Sikob } 1056103285Sikob ocb->flags = OCB_ACT_MGM; 1057103285Sikob ocb->sdev = sdev; 1058103285Sikob ocb->ccb = NULL; 1059103285Sikob 1060103285Sikob nid = sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS; 1061103285Sikob bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb)); 1062103285Sikob ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1063103285Sikob ocb->orb[7] = htonl(SBP_DEV2ADDR( 1064103285Sikob device_get_unit(sdev->target->sbp->fd.dev), 1065103285Sikob sdev->target->target_id, 1066103285Sikob sdev->lun_id)); 1067103285Sikob 1068107653SsimokawaSBP_DEBUG(0) 1069103285Sikob sbp_show_sdev_info(sdev, 2); 1070103285Sikob printf("%s\n", orb_fun_name[(func>>16)&0xf]); 1071107653SsimokawaEND_DEBUG 1072103285Sikob switch (func) { 1073103285Sikob case ORB_FUN_LGI: 1074103285Sikob ocb->orb[2] = htonl(nid << 16); 1075103285Sikob ocb->orb[3] = htonl(vtophys(&sdev->login)); 1076103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id); 1077103285Sikob ocb->orb[5] = htonl(sizeof(struct sbp_login_res)); 1078103285Sikob break; 1079103285Sikob case ORB_FUN_RCN: 1080103285Sikob case ORB_FUN_LGO: 1081103285Sikob case ORB_FUN_LUR: 1082103285Sikob case ORB_FUN_RST: 1083103285Sikob case ORB_FUN_ATA: 1084103285Sikob case ORB_FUN_ATS: 1085103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id); 1086103285Sikob break; 1087103285Sikob } 1088103285Sikob 1089103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1090103285Sikob if(xfer == NULL){ 1091103285Sikob return; 1092103285Sikob } 1093103285Sikob xfer->act.hand = sbp_login_callback; 1094103285Sikob 1095103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1096103285Sikob fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi); 1097103285Sikob fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo); 1098103285Sikob fp->mode.wreqb.len = htons(8); 1099103285Sikob fp->mode.wreqb.extcode = 0; 1100103285Sikob fp->mode.wreqb.payload[0] = htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1101103285Sikob fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1102103285Sikob sbp_enqueue_ocb(sdev, ocb); 1103103285Sikob 1104103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1105103285Sikob} 1106103285Sikob 1107103285Sikobstatic void 1108105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb) 1109103285Sikob{ 1110103285Sikob struct ccb_scsiio *csio; 1111103285Sikob 1112103285Sikob csio = &ocb->ccb->csio; 1113103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1114103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1115103285Sikob ", flags: 0x%02x, " 1116103285Sikob "%db cmd/%db data/%db sense\n", 1117103285Sikob device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1118103285Sikob ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1119103285Sikob csio->cdb_io.cdb_bytes[0], 1120103285Sikob csio->cdb_io.cdb_bytes[1], 1121103285Sikob csio->cdb_io.cdb_bytes[2], 1122103285Sikob csio->cdb_io.cdb_bytes[3], 1123103285Sikob csio->cdb_io.cdb_bytes[4], 1124103285Sikob csio->cdb_io.cdb_bytes[5], 1125103285Sikob csio->cdb_io.cdb_bytes[6], 1126103285Sikob csio->cdb_io.cdb_bytes[7], 1127103285Sikob csio->cdb_io.cdb_bytes[8], 1128103285Sikob csio->cdb_io.cdb_bytes[9], 1129103285Sikob ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1130103285Sikob csio->cdb_len, csio->dxfer_len, 1131103285Sikob csio->sense_len); 1132105792Ssimokawa} 1133105792Ssimokawa 1134105792Ssimokawastatic void 1135105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1136105792Ssimokawa{ 1137105792Ssimokawa struct sbp_cmd_status *sbp_cmd_status; 1138105792Ssimokawa struct scsi_sense_data *sense; 1139105792Ssimokawa 1140105792Ssimokawa sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1141105792Ssimokawa sense = &ocb->ccb->csio.sense_data; 1142105792Ssimokawa 1143105792SsimokawaSBP_DEBUG(0) 1144105792Ssimokawa sbp_print_scsi_cmd(ocb); 1145103285Sikob /* XXX need decode status */ 1146103285Sikob sbp_show_sdev_info(ocb->sdev, 2); 1147103285Sikob printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d", 1148103285Sikob sbp_cmd_status->status, 1149103285Sikob sbp_cmd_status->sfmt, 1150103285Sikob sbp_cmd_status->valid, 1151103285Sikob sbp_cmd_status->s_key, 1152103285Sikob sbp_cmd_status->s_code, 1153103285Sikob sbp_cmd_status->s_qlfr, 1154103285Sikob sbp_status->len 1155103285Sikob ); 1156103285Sikob#if 0 /* XXX */ 1157103285Sikob if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) { 1158103285Sikob printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]); 1159103285Sikob scsi_sense_desc( 1160103285Sikob sbp_cmd_status->s_code, 1161103285Sikob sbp_cmd_status->s_qlfr, 1162103285Sikob ocb->ccb->ccb_h.path->device->inq_data 1163103285Sikob ) 1164103285Sikob } else { 1165103285Sikob printf("\n"); 1166103285Sikob } 1167103285Sikob#else 1168103285Sikob printf("\n"); 1169103285Sikob#endif 1170103285SikobEND_DEBUG 1171103285Sikob 1172103285Sikob 1173103285Sikob if(sbp_cmd_status->status == SCSI_STATUS_CHECK_COND || 1174103285Sikob sbp_cmd_status->status == SCSI_STATUS_CMD_TERMINATED){ 1175103285Sikob if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1176103285Sikob sense->error_code = SSD_CURRENT_ERROR; 1177103285Sikob }else{ 1178103285Sikob sense->error_code = SSD_DEFERRED_ERROR; 1179103285Sikob } 1180103285Sikob if(sbp_cmd_status->valid) 1181103285Sikob sense->error_code |= SSD_ERRCODE_VALID; 1182103285Sikob sense->flags = sbp_cmd_status->s_key; 1183103285Sikob if(sbp_cmd_status->mark) 1184103285Sikob sense->flags |= SSD_FILEMARK; 1185103285Sikob if(sbp_cmd_status->eom) 1186103285Sikob sense->flags |= SSD_EOM; 1187103285Sikob if(sbp_cmd_status->ill_len) 1188103285Sikob sense->flags |= SSD_ILI; 1189103285Sikob sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff; 1190103285Sikob sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff; 1191103285Sikob sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff; 1192103285Sikob sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff; 1193103285Sikob if (sbp_status->len <= 1) 1194103285Sikob /* XXX not scsi status. shouldn't be happened */ 1195103285Sikob sense->extra_len = 0; 1196103285Sikob else if (sbp_status->len <= 4) 1197103285Sikob /* add_sense_code(_qual), info, cmd_spec_info */ 1198103285Sikob sense->extra_len = 6; 1199103285Sikob else 1200103285Sikob /* fru, sense_key_spec */ 1201103285Sikob sense->extra_len = 10; 1202103285Sikob sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff; 1203103285Sikob sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff; 1204103285Sikob sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff; 1205103285Sikob sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff; 1206103285Sikob sense->add_sense_code = sbp_cmd_status->s_code; 1207103285Sikob sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1208103285Sikob sense->fru = sbp_cmd_status->fru; 1209103285Sikob sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff; 1210103285Sikob sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff; 1211103285Sikob sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff; 1212103285Sikob 1213103285Sikob ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1214103285Sikob ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1215103285Sikob | CAM_AUTOSNS_VALID; 1216103285Sikob/* 1217103285Sikob{ 1218103285Sikob u_int8_t j, *tmp; 1219103285Sikob tmp = sense; 1220103285Sikob for( j = 0 ; j < 32 ; j+=8){ 1221103285Sikob printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1222103285Sikob tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1223103285Sikob tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1224103285Sikob } 1225103285Sikob 1226103285Sikob} 1227103285Sikob*/ 1228103285Sikob } else { 1229103285Sikob printf("sbp_scsi_status: unknown scsi status\n"); 1230103285Sikob } 1231103285Sikob} 1232103285Sikob 1233103285Sikobstatic void 1234103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb) 1235103285Sikob{ 1236103285Sikob union ccb *ccb; 1237103285Sikob struct sbp_dev *sdev; 1238103285Sikob struct scsi_inquiry_data *inq; 1239103285Sikob 1240103285Sikob ccb = ocb->ccb; 1241103285Sikob sdev = ocb->sdev; 1242103285Sikob 1243103285Sikob if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1244103285Sikob return; 1245103285SikobSBP_DEBUG(1) 1246103285Sikob sbp_show_sdev_info(sdev, 2); 1247103285Sikob printf("sbp_fix_inq_data\n"); 1248103285SikobEND_DEBUG 1249103285Sikob inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1250103285Sikob switch (SID_TYPE(inq)) { 1251103285Sikob case T_DIRECT: 1252103285Sikob /* 1253103285Sikob * XXX Convert Direct Access device to RBC. 1254103285Sikob * I've never seen Firewire DA devices which support READ_6. 1255103285Sikob */ 1256103285Sikob#if 1 1257103285Sikob if (SID_TYPE(inq) == T_DIRECT) 1258103285Sikob inq->device |= T_RBC; /* T_DIRECT == 0 */ 1259103285Sikob#endif 1260103285Sikob /* fall through */ 1261103285Sikob case T_RBC: 1262103285Sikob /* disable tag queuing */ 1263103285Sikob inq->flags &= ~SID_CmdQue; 1264103285Sikob /* 1265103285Sikob * Override vendor/product/revision information. 1266103285Sikob * Some devices sometimes return strange strings. 1267103285Sikob */ 1268103285Sikob bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1269103285Sikob bcopy(sdev->product, inq->product, sizeof(inq->product)); 1270103285Sikob bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1271103285Sikob break; 1272103285Sikob } 1273103285Sikob} 1274103285Sikob 1275103285Sikobstatic void 1276103285Sikobsbp_recv1(struct fw_xfer *xfer){ 1277103285Sikob struct fw_pkt *rfp; 1278103285Sikob#if NEED_RESPONSE 1279103285Sikob struct fw_pkt *sfp; 1280103285Sikob#endif 1281103285Sikob struct sbp_softc *sbp; 1282103285Sikob struct sbp_dev *sdev; 1283103285Sikob struct sbp_ocb *ocb; 1284103285Sikob struct sbp_login_res *login_res = NULL; 1285103285Sikob struct sbp_status *sbp_status; 1286103285Sikob struct sbp_target *target; 1287103285Sikob int orb_fun, status_valid; 1288103285Sikob u_int32_t addr; 1289103285Sikob/* 1290103285Sikob u_int32_t *ld; 1291103285Sikob ld = xfer->recv.buf; 1292103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n", 1293103285Sikob xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1294103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1295103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1296103285Sikob*/ 1297103285Sikob if(xfer->resp != 0){ 1298103285Sikob printf("sbp_recv: xfer->resp != 0\n"); 1299103285Sikob fw_xfer_free( xfer); 1300103285Sikob return; 1301103285Sikob } 1302103285Sikob if(xfer->recv.buf == NULL){ 1303103285Sikob printf("sbp_recv: xfer->recv.buf == NULL\n"); 1304103285Sikob fw_xfer_free( xfer); 1305103285Sikob return; 1306103285Sikob } 1307103285Sikob sbp = (struct sbp_softc *)xfer->sc; 1308103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1309103285Sikob if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1310103285Sikob printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1311103285Sikob fw_xfer_free( xfer); 1312103285Sikob return; 1313103285Sikob } 1314103285Sikob sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload; 1315103285Sikob addr = ntohl(rfp->mode.wreqb.dest_lo); 1316103285SikobSBP_DEBUG(2) 1317103285Sikob printf("received address 0x%x\n", addr); 1318103285SikobEND_DEBUG 1319103285Sikob target = &sbp->targets[SBP_ADDR2TRG(addr)]; 1320103285Sikob sdev = &target->luns[SBP_ADDR2LUN(addr)]; 1321103285Sikob 1322103285Sikob status_valid = (sbp_status->resp == ORB_RES_CMPL 1323103285Sikob && sbp_status->dead == 0 1324103285Sikob && sbp_status->status == 0); 1325103285Sikob 1326103285SikobSBP_DEBUG(0) 1327103285Sikob if (!status_valid || debug > 1){ 1328103285Sikob int status; 1329103285Sikob 1330103285Sikob sbp_show_sdev_info(sdev, 2); 1331103285Sikob printf("ORB status src:%x resp:%x dead:%x" 1332103285Sikob " len:%x stat:%x orb:%x%08x\n", 1333103285Sikob sbp_status->src, sbp_status->resp, sbp_status->dead, 1334103285Sikob sbp_status->len, sbp_status->status, 1335108280Ssimokawa ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1336103285Sikob sbp_show_sdev_info(sdev, 2); 1337103285Sikob status = sbp_status->status; 1338103285Sikob switch(sbp_status->resp) { 1339103285Sikob case 0: 1340103285Sikob if (status > MAX_ORB_STATUS0) 1341103285Sikob printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1342103285Sikob else 1343103285Sikob printf("%s\n", orb_status0[status]); 1344103285Sikob break; 1345103285Sikob case 1: 1346103285Sikob printf("Object: %s, Serial Bus Error: %s\n", 1347103285Sikob orb_status1_object[(status>>6) & 3], 1348103285Sikob orb_status1_serial_bus_error[status & 0xf]); 1349103285Sikob break; 1350103285Sikob default: 1351103285Sikob printf("unknown respose code\n"); 1352103285Sikob } 1353103285Sikob } 1354103285SikobEND_DEBUG 1355103285Sikob ocb = sbp_dequeue_ocb(sdev, ntohl(sbp_status->orb_lo)); 1356103285Sikob 1357103285Sikob /* we have to reset the fetch agent if it's dead */ 1358103285Sikob if (sbp_status->dead) { 1359103285Sikob if (sdev->path) 1360103285Sikob xpt_freeze_devq(sdev->path, 1); 1361103285Sikob sbp_agent_reset(sdev, 0); 1362103285Sikob } 1363103285Sikob 1364103285Sikob 1365103285Sikob if (ocb == NULL) { 1366103285Sikob printf("No ocb on the queue for target %d.\n", sdev->target->target_id); 1367103285Sikob fw_xfer_free( xfer); 1368103285Sikob return; 1369103285Sikob } 1370103285Sikob 1371103285Sikob switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1372103285Sikob case ORB_FMT_NOP: 1373103285Sikob break; 1374103285Sikob case ORB_FMT_VED: 1375103285Sikob break; 1376103285Sikob case ORB_FMT_STD: 1377103285Sikob switch(ocb->flags & OCB_ACT_MASK){ 1378103285Sikob case OCB_ACT_MGM: 1379103285Sikob orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1380103285Sikob switch(orb_fun) { 1381103285Sikob case ORB_FUN_LGI: 1382103285Sikob login_res = &sdev->login; 1383103285Sikob login_res->len = ntohs(login_res->len); 1384103285Sikob login_res->id = ntohs(login_res->id); 1385103285Sikob login_res->cmd_hi = ntohs(login_res->cmd_hi); 1386103285Sikob login_res->cmd_lo = ntohl(login_res->cmd_lo); 1387103285Sikob if (status_valid) { 1388103285SikobSBP_DEBUG(0) 1389103285Sikobsbp_show_sdev_info(sdev, 2); 1390103285Sikobprintf("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)); 1391103285SikobEND_DEBUG 1392103285Sikob#if 1 1393103285Sikob sbp_busy_timeout(sdev); 1394103285Sikob#else 1395103285Sikob sbp_mgm_orb(sdev, ORB_FUN_ATS); 1396103285Sikob#endif 1397103285Sikob } else { 1398103285Sikob /* forgot logout ? */ 1399103285Sikob printf("login failed\n"); 1400103285Sikob sdev->status = SBP_DEV_RESET; 1401103285Sikob } 1402103285Sikob break; 1403103285Sikob case ORB_FUN_RCN: 1404103285Sikob login_res = &sdev->login; 1405103285Sikob if (status_valid) { 1406103285Sikob sdev->status = SBP_DEV_ATTACHED; 1407103285SikobSBP_DEBUG(0) 1408103285Sikobsbp_show_sdev_info(sdev, 2); 1409103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1410103285SikobEND_DEBUG 1411103285Sikob#if 1 1412103285Sikob sbp_ping_unit(sdev); 1413103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 1414103285Sikob#else 1415103285Sikob sbp_mgm_orb(sdev, ORB_FUN_ATS); 1416103285Sikob#endif 1417103285Sikob } else { 1418103285Sikob /* reconnection hold time exceed? */ 1419103285Sikob printf("reconnect failed\n"); 1420103285Sikob sbp_mgm_orb(sdev, ORB_FUN_LGI); 1421103285Sikob } 1422103285Sikob break; 1423103285Sikob case ORB_FUN_LGO: 1424103285Sikob sdev->status = SBP_DEV_RESET; 1425103285Sikob break; 1426103285Sikob case ORB_FUN_LUR: 1427103285Sikob case ORB_FUN_RST: 1428103285Sikob case ORB_FUN_ATA: 1429103285Sikob case ORB_FUN_ATS: 1430103285Sikob if (sdev->status == SBP_DEV_ATTACHED) { 1431103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 1432103285Sikob } else { 1433103285Sikob sbp_busy_timeout(sdev); 1434103285Sikob } 1435103285Sikob break; 1436103285Sikob default: 1437103285Sikob break; 1438103285Sikob } 1439103285Sikob break; 1440103285Sikob case OCB_ACT_CMD: 1441103285Sikob if(ocb->ccb != NULL){ 1442103285Sikob union ccb *ccb; 1443103285Sikob/* 1444103285Sikob u_int32_t *ld; 1445103285Sikob ld = ocb->ccb->csio.data_ptr; 1446103285Sikob if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1447103285Sikob printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1448103285Sikob else 1449103285Sikob printf("ptr NULL\n"); 1450103285Sikobprintf("len %d\n", sbp_status->len); 1451103285Sikob*/ 1452103285Sikob ccb = ocb->ccb; 1453103285Sikob if(sbp_status->len > 1){ 1454103285Sikob sbp_scsi_status(sbp_status, ocb); 1455103285Sikob }else{ 1456103285Sikob if(sbp_status->resp != ORB_RES_CMPL){ 1457103285Sikob ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1458103285Sikob }else{ 1459103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1460103285Sikob } 1461103285Sikob } 1462103285Sikob /* fix up inq data */ 1463103285Sikob if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1464103285Sikob sbp_fix_inq_data(ocb); 1465103285Sikob xpt_done(ccb); 1466103285Sikob } 1467103285Sikob break; 1468103285Sikob default: 1469103285Sikob break; 1470103285Sikob } 1471103285Sikob } 1472103285Sikob 1473103285Sikob if (!(ocb->flags & OCB_RESERVED)) 1474103285Sikob sbp_free_ocb(sbp, ocb); 1475103285Sikob 1476103285Sikob/* The received packet is usually small enough to be stored within 1477103285Sikob * the buffer. In that case, the controller return ack_complete and 1478103285Sikob * no respose is necessary. 1479103285Sikob * 1480103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as 1481103285Sikob * ack_complete or ack_pending to upper driver. 1482103285Sikob */ 1483103285Sikob#if NEED_RESPONSE 1484103285Sikob xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO); 1485103285Sikob xfer->send.len = 12; 1486103285Sikob xfer->send.off = 0; 1487103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1488103285Sikob sfp->mode.wres.dst = rfp->mode.wreqb.src; 1489103285Sikob xfer->dst = ntohs(sfp->mode.wres.dst); 1490103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1491103285Sikob xfer->act.hand = sbp_loginres_callback; 1492103285Sikob xfer->retry_req = fw_asybusy; 1493103285Sikob 1494103285Sikob sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1495103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1496103285Sikob sfp->mode.wres.rtcode = 0; 1497103285Sikob sfp->mode.wres.pri = 0; 1498103285Sikob 1499103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1500103285Sikob#else 1501103285Sikob fw_xfer_free(xfer); 1502103285Sikob#endif 1503103285Sikob 1504103285Sikob return; 1505103285Sikob 1506103285Sikob} 1507103285Sikob 1508103285Sikobstatic void 1509103285Sikobsbp_recv(struct fw_xfer *xfer) 1510103285Sikob{ 1511103285Sikob int s; 1512103285Sikob 1513103285Sikob s = splcam(); 1514103285Sikob sbp_recv1(xfer); 1515103285Sikob splx(s); 1516103285Sikob} 1517103285Sikob/* 1518103285Sikob * sbp_attach() 1519103285Sikob */ 1520103285Sikobstatic int 1521103285Sikobsbp_attach(device_t dev) 1522103285Sikob{ 1523103285Sikob struct sbp_softc *sbp; 1524103285Sikob struct cam_devq *devq; 1525103285Sikob struct fw_xfer *xfer; 1526103285Sikob int i, s, error; 1527103285Sikob 1528103285SikobSBP_DEBUG(0) 1529103285Sikob printf("sbp_attach\n"); 1530103285SikobEND_DEBUG 1531103285Sikob 1532103285Sikob sbp = ((struct sbp_softc *)device_get_softc(dev)); 1533103285Sikob bzero(sbp, sizeof(struct sbp_softc)); 1534103285Sikob sbp->fd.dev = dev; 1535103285Sikob sbp->fd.fc = device_get_ivars(dev); 1536103285Sikob error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 1537103285Sikob /*boundary*/0, 1538103285Sikob /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1539103285Sikob /*highaddr*/BUS_SPACE_MAXADDR, 1540103285Sikob /*filter*/NULL, /*filterarg*/NULL, 1541103285Sikob /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1542103285Sikob /*maxsegsz*/0x8000, 1543103285Sikob /*flags*/BUS_DMA_ALLOCNOW, 1544103285Sikob &sbp->dmat); 1545103285Sikob if (error != 0) { 1546103285Sikob printf("sbp_attach: Could not allocate DMA tag " 1547103285Sikob "- error %d\n", error); 1548103285Sikob return (ENOMEM); 1549103285Sikob } 1550103285Sikob 1551103285Sikob devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1552103285Sikob if (devq == NULL) 1553103285Sikob return (ENXIO); 1554103285Sikob 1555103285Sikob for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1556103285Sikob sbp->targets[i].fwdev = NULL; 1557103285Sikob sbp->targets[i].luns = NULL; 1558103285Sikob } 1559103285Sikob 1560103285Sikob sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1561103285Sikob device_get_unit(dev), 1562103285Sikob /*untagged*/ SBP_QUEUE_LEN, 1563103285Sikob /*tagged*/0, devq); 1564103285Sikob 1565103285Sikob if (sbp->sim == NULL) { 1566103285Sikob cam_simq_free(devq); 1567103285Sikob return (ENXIO); 1568103285Sikob } 1569103285Sikob 1570103285Sikob sbp->ocb = (struct sbp_ocb *) contigmalloc( 1571103285Sikob sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1572103285Sikob M_SBP, M_DONTWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 1573103285Sikob bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB); 1574103285Sikob 1575103285Sikob if (sbp->ocb == NULL) { 1576103285Sikob printf("sbp0: ocb alloction failure\n"); 1577103285Sikob return (ENOMEM); 1578103285Sikob } 1579103285Sikob 1580103285Sikob STAILQ_INIT(&sbp->free_ocbs); 1581103285Sikob for (i = 0; i < SBP_NUM_OCB; i++) { 1582103285Sikob sbp_free_ocb(sbp, &sbp->ocb[i]); 1583103285Sikob } 1584103285Sikob 1585103285Sikob if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) { 1586103285Sikob cam_sim_free(sbp->sim, /*free_devq*/TRUE); 1587103285Sikob contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1588103285Sikob M_SBP); 1589103285Sikob return (ENXIO); 1590103285Sikob } 1591103285Sikob 1592103285Sikob xfer = fw_xfer_alloc(); 1593103285Sikob xfer->act.hand = sbp_recv; 1594103285Sikob xfer->act_type = FWACT_XFER; 1595103285Sikob#if NEED_RESPONSE 1596103285Sikob xfer->fc = sbp->fd.fc; 1597103285Sikob#endif 1598103285Sikob xfer->sc = (caddr_t)sbp; 1599103285Sikob 1600103285Sikob sbp->fwb.start_hi = SBP_BIND_HI; 1601103285Sikob sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0); 1602103285Sikob /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1603103285Sikob sbp->fwb.addrlen = 0xffff; 1604103285Sikob sbp->fwb.xfer = xfer; 1605103285Sikob fw_bindadd(sbp->fd.fc, &sbp->fwb); 1606103285Sikob 1607103285Sikob sbp->fd.post_explore = sbp_post_explore; 1608103285Sikob s = splfw(); 1609103285Sikob sbp_post_explore((void *)sbp); 1610103285Sikob splx(s); 1611103285Sikob 1612103285Sikob return (0); 1613103285Sikob} 1614103285Sikob 1615103285Sikobstatic int 1616103285Sikobsbp_detach(device_t dev) 1617103285Sikob{ 1618103285Sikob struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1619103285Sikob struct firewire_comm *fc = sbp->fd.fc; 1620103285Sikob int i; 1621103285Sikob 1622103285SikobSBP_DEBUG(0) 1623103285Sikob printf("sbp_detach\n"); 1624103285SikobEND_DEBUG 1625103285Sikob 1626103285Sikob /* bus reset for logout */ 1627103285Sikob sbp->fd.post_explore = NULL; 1628103285Sikob fc->ibr(fc); 1629103285Sikob 1630103285Sikob contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1631103285Sikob fw_bindremove(fc, &sbp->fwb); 1632103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i ++) 1633103285Sikob sbp_detach_target(&sbp->targets[i]); 1634103285Sikob xpt_bus_deregister(cam_sim_path(sbp->sim)); 1635103285Sikob bus_dma_tag_destroy(sbp->dmat); 1636103285Sikob return (0); 1637103285Sikob} 1638103285Sikob 1639103285Sikobstatic void 1640103285Sikobsbp_detach_target(struct sbp_target *target) 1641103285Sikob{ 1642103285Sikob int i; 1643103285Sikob struct sbp_dev *sdev; 1644103285Sikob 1645103285Sikob if (target->luns != NULL) { 1646103285Sikob printf("sbp_detach_target %d\n", target->target_id); 1647103285Sikob for (i=0; i < target->num_lun; i++) { 1648103285Sikob sdev = &target->luns[i]; 1649103285Sikob if (sdev->status == SBP_DEV_RESET || 1650103285Sikob sdev->status == SBP_DEV_DEAD) 1651103285Sikob continue; 1652103285Sikob if (sdev->path) 1653103285Sikob xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 1654103285Sikob xpt_free_path(sdev->path); 1655103285Sikob sdev->path = NULL; 1656103285Sikob sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 1657103285Sikob } 1658103285Sikob free(target->luns, M_SBP); 1659103285Sikob target->luns = NULL; 1660103285Sikob } 1661103285Sikob target->fwdev = NULL; 1662103285Sikob} 1663103285Sikob 1664103285Sikobstatic void 1665103285Sikobsbp_timeout(void *arg) 1666103285Sikob{ 1667103285Sikob struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 1668103285Sikob struct sbp_dev *sdev = ocb->sdev; 1669103285Sikob int s; 1670103285Sikob 1671103285Sikob sbp_show_sdev_info(sdev, 2); 1672103285Sikob printf("request timeout ... requeue\n"); 1673103285Sikob 1674103285Sikob /* XXX need reset? */ 1675103285Sikob 1676103285Sikob s = splfw(); 1677105633Ssimokawa sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 1678103285Sikob splx(s); 1679103285Sikob return; 1680103285Sikob} 1681103285Sikob 1682103285Sikobstatic void 1683103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb) 1684103285Sikob{ 1685103285Sikob 1686103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 1687103285Sikob struct sbp_target *target = NULL; 1688103285Sikob struct sbp_dev *sdev = NULL; 1689103285Sikob 1690103285Sikob /* target:lun -> sdev mapping */ 1691103285Sikob if (sbp != NULL 1692103285Sikob && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 1693103285Sikob && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 1694103285Sikob target = &sbp->targets[ccb->ccb_h.target_id]; 1695103285Sikob if (target->fwdev != NULL 1696103285Sikob && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 1697103285Sikob && ccb->ccb_h.target_lun < target->num_lun) { 1698103285Sikob sdev = &target->luns[ccb->ccb_h.target_lun]; 1699103285Sikob if (sdev->status != SBP_DEV_ATTACHED && 1700103285Sikob sdev->status != SBP_DEV_PROBE) 1701103285Sikob sdev = NULL; 1702103285Sikob } 1703103285Sikob } 1704103285Sikob 1705103285SikobSBP_DEBUG(1) 1706103285Sikob if (sdev == NULL) 1707103285Sikob printf("invalid target %d lun %d\n", 1708103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1709103285SikobEND_DEBUG 1710103285Sikob 1711103285Sikob switch (ccb->ccb_h.func_code) { 1712103285Sikob case XPT_SCSI_IO: 1713103285Sikob case XPT_RESET_DEV: 1714103285Sikob case XPT_GET_TRAN_SETTINGS: 1715103285Sikob case XPT_SET_TRAN_SETTINGS: 1716103285Sikob case XPT_CALC_GEOMETRY: 1717103285Sikob if (sdev == NULL) { 1718103285SikobSBP_DEBUG(1) 1719103285Sikob printf("%s:%d:%d:func_code 0x%04x: " 1720103285Sikob "Invalid target (target needed)\n", 1721103285Sikob device_get_nameunit(sbp->fd.dev), 1722103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1723103285Sikob ccb->ccb_h.func_code); 1724103285SikobEND_DEBUG 1725103285Sikob 1726108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1727103285Sikob xpt_done(ccb); 1728103285Sikob return; 1729103285Sikob } 1730103285Sikob break; 1731103285Sikob case XPT_PATH_INQ: 1732103285Sikob case XPT_NOOP: 1733103285Sikob /* The opcodes sometimes aimed at a target (sc is valid), 1734103285Sikob * sometimes aimed at the SIM (sc is invalid and target is 1735103285Sikob * CAM_TARGET_WILDCARD) 1736103285Sikob */ 1737103285Sikob if (sbp == NULL && 1738103285Sikob ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 1739103285SikobSBP_DEBUG(0) 1740103285Sikob printf("%s:%d:%d func_code 0x%04x: " 1741103285Sikob "Invalid target (no wildcard)\n", 1742103285Sikob device_get_nameunit(sbp->fd.dev), 1743103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1744103285Sikob ccb->ccb_h.func_code); 1745103285SikobEND_DEBUG 1746108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1747103285Sikob xpt_done(ccb); 1748103285Sikob return; 1749103285Sikob } 1750103285Sikob break; 1751103285Sikob default: 1752103285Sikob /* XXX Hm, we should check the input parameters */ 1753103285Sikob break; 1754103285Sikob } 1755103285Sikob 1756103285Sikob switch (ccb->ccb_h.func_code) { 1757103285Sikob case XPT_SCSI_IO: 1758103285Sikob { 1759103285Sikob struct ccb_scsiio *csio; 1760103285Sikob struct sbp_ocb *ocb; 1761103285Sikob int s, speed; 1762106506Ssimokawa void *cdb; 1763103285Sikob 1764103285Sikob csio = &ccb->csio; 1765103285Sikob 1766103285SikobSBP_DEBUG(1) 1767103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1768103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1769103285Sikob ", flags: 0x%02x, " 1770103285Sikob "%db cmd/%db data/%db sense\n", 1771103285Sikob device_get_nameunit(sbp->fd.dev), 1772103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1773103285Sikob csio->cdb_io.cdb_bytes[0], 1774103285Sikob csio->cdb_io.cdb_bytes[1], 1775103285Sikob csio->cdb_io.cdb_bytes[2], 1776103285Sikob csio->cdb_io.cdb_bytes[3], 1777103285Sikob csio->cdb_io.cdb_bytes[4], 1778103285Sikob csio->cdb_io.cdb_bytes[5], 1779103285Sikob csio->cdb_io.cdb_bytes[6], 1780103285Sikob csio->cdb_io.cdb_bytes[7], 1781103285Sikob csio->cdb_io.cdb_bytes[8], 1782103285Sikob csio->cdb_io.cdb_bytes[9], 1783103285Sikob ccb->ccb_h.flags & CAM_DIR_MASK, 1784103285Sikob csio->cdb_len, csio->dxfer_len, 1785103285Sikob csio->sense_len); 1786103285SikobEND_DEBUG 1787103285Sikob if(sdev == NULL){ 1788103285Sikob ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1789103285Sikob xpt_done(ccb); 1790103285Sikob return; 1791103285Sikob } 1792103285Sikob#if 0 1793103285Sikob /* if we are in probe stage, pass only probe commands */ 1794103285Sikob if (sdev->status == SBP_DEV_PROBE) { 1795103285Sikob char *name; 1796103285Sikob name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 1797103285Sikob printf("probe stage, periph name: %s\n", name); 1798103285Sikob if (strcmp(name, "probe") != 0) { 1799103285Sikob ccb->ccb_h.status = CAM_REQUEUE_REQ; 1800103285Sikob xpt_done(ccb); 1801103285Sikob return; 1802103285Sikob } 1803103285Sikob } 1804103285Sikob#endif 1805103285Sikob if ((ocb = sbp_get_ocb(sbp)) == NULL) { 1806103285Sikob s = splfw(); 1807103285Sikob sbp->flags |= SBP_RESOURCE_SHORTAGE; 1808103285Sikob splx(s); 1809103285Sikob return; 1810103285Sikob } 1811103285Sikob ocb->flags = OCB_ACT_CMD; 1812103285Sikob ocb->sdev = sdev; 1813103285Sikob ocb->ccb = ccb; 1814103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 1815103285Sikob ocb->orb[0] = htonl(1 << 31); 1816103285Sikob ocb->orb[1] = 0; 1817103285Sikob ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 1818103285Sikob ocb->orb[3] = htonl(vtophys(ocb->ind_ptr)); 1819103285Sikob speed = min(target->fwdev->speed, max_speed); 1820103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 1821103285Sikob | ORB_CMD_MAXP(speed + 7)); 1822103285Sikob if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 1823103285Sikob ocb->orb[4] |= htonl(ORB_CMD_IN); 1824103285Sikob } 1825103285Sikob 1826103285Sikob if (csio->ccb_h.flags & CAM_SCATTER_VALID) 1827103285Sikob printf("sbp: CAM_SCATTER_VALID\n"); 1828103285Sikob if (csio->ccb_h.flags & CAM_DATA_PHYS) 1829103285Sikob printf("sbp: CAM_DATA_PHYS\n"); 1830103285Sikob 1831106506Ssimokawa if (csio->ccb_h.flags & CAM_CDB_POINTER) 1832106506Ssimokawa cdb = (void *)csio->cdb_io.cdb_ptr; 1833106506Ssimokawa else 1834106506Ssimokawa cdb = (void *)&csio->cdb_io.cdb_bytes; 1835106506Ssimokawa bcopy(cdb, 1836103285Sikob (void *)(uintptr_t)(volatile void *)&ocb->orb[5], 1837106506Ssimokawa csio->cdb_len); 1838103285Sikob/* 1839103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 1840103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 1841103285Sikob*/ 1842103285Sikob if (ccb->csio.dxfer_len > 0) { 1843103285Sikob int s; 1844103285Sikob 1845103285Sikob if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 1846103285Sikob printf("sbp_action1: cannot create dmamap\n"); 1847103285Sikob break; 1848103285Sikob } 1849103285Sikob 1850103285Sikob s = splsoftvm(); 1851103285Sikob bus_dmamap_load(/*dma tag*/sbp->dmat, 1852103285Sikob /*dma map*/ocb->dmamap, 1853103285Sikob ccb->csio.data_ptr, 1854103285Sikob ccb->csio.dxfer_len, 1855103285Sikob sbp_execute_ocb, 1856103285Sikob ocb, 1857103285Sikob /*flags*/0); 1858103285Sikob splx(s); 1859103285Sikob } else 1860103285Sikob sbp_execute_ocb(ocb, NULL, 0, 0); 1861103285Sikob break; 1862103285Sikob } 1863103285Sikob case XPT_CALC_GEOMETRY: 1864103285Sikob { 1865103285Sikob struct ccb_calc_geometry *ccg; 1866103285Sikob u_int32_t size_mb; 1867103285Sikob u_int32_t secs_per_cylinder; 1868103285Sikob int extended = 1; 1869103285Sikob ccg = &ccb->ccg; 1870103285Sikob 1871103285Sikob if (ccg->block_size == 0) { 1872103285Sikob printf("sbp_action1: block_size is 0.\n"); 1873103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 1874103285Sikob xpt_done(ccb); 1875103285Sikob break; 1876103285Sikob } 1877103285SikobSBP_DEBUG(1) 1878103285Sikob printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 1879103285Sikob "Volume size = %d\n", 1880103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim), 1881103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1882103285Sikob ccg->volume_size); 1883103285SikobEND_DEBUG 1884103285Sikob 1885103285Sikob size_mb = ccg->volume_size 1886103285Sikob / ((1024L * 1024L) / ccg->block_size); 1887103285Sikob 1888103285Sikob if (size_mb >= 1024 && extended) { 1889103285Sikob ccg->heads = 255; 1890103285Sikob ccg->secs_per_track = 63; 1891103285Sikob } else { 1892103285Sikob ccg->heads = 64; 1893103285Sikob ccg->secs_per_track = 32; 1894103285Sikob } 1895103285Sikob secs_per_cylinder = ccg->heads * ccg->secs_per_track; 1896103285Sikob ccg->cylinders = ccg->volume_size / secs_per_cylinder; 1897103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1898103285Sikob xpt_done(ccb); 1899103285Sikob break; 1900103285Sikob } 1901103285Sikob case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 1902103285Sikob { 1903103285Sikob 1904103285SikobSBP_DEBUG(1) 1905103285Sikob printf("%s:%d:XPT_RESET_BUS: \n", 1906103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 1907103285SikobEND_DEBUG 1908103285Sikob 1909103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 1910103285Sikob xpt_done(ccb); 1911103285Sikob break; 1912103285Sikob } 1913103285Sikob case XPT_PATH_INQ: /* Path routing inquiry */ 1914103285Sikob { 1915103285Sikob struct ccb_pathinq *cpi = &ccb->cpi; 1916103285Sikob 1917103285SikobSBP_DEBUG(1) 1918103285Sikob printf("%s:%d:%d XPT_PATH_INQ:.\n", 1919103285Sikob device_get_nameunit(sbp->fd.dev), 1920103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1921103285SikobEND_DEBUG 1922103285Sikob cpi->version_num = 1; /* XXX??? */ 1923103285Sikob cpi->hba_inquiry = 0; 1924103285Sikob cpi->target_sprt = 0; 1925103285Sikob cpi->hba_misc = 0; 1926103285Sikob cpi->hba_eng_cnt = 0; 1927103285Sikob cpi->max_target = SBP_NUM_TARGETS - 1; 1928103285Sikob cpi->max_lun = SBP_NUM_LUNS - 1; 1929103285Sikob cpi->initiator_id = SBP_INITIATOR; 1930103285Sikob cpi->bus_id = sim->bus_id; 1931103285Sikob cpi->base_transfer_speed = 400 * 1000 / 8; 1932103285Sikob strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 1933103285Sikob strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 1934103285Sikob strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 1935103285Sikob cpi->unit_number = sim->unit_number; 1936103285Sikob 1937103285Sikob cpi->ccb_h.status = CAM_REQ_CMP; 1938103285Sikob xpt_done(ccb); 1939103285Sikob break; 1940103285Sikob } 1941103285Sikob case XPT_GET_TRAN_SETTINGS: 1942103285Sikob { 1943103285Sikob struct ccb_trans_settings *cts = &ccb->cts; 1944103285SikobSBP_DEBUG(1) 1945103285Sikob printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 1946103285Sikob device_get_nameunit(sbp->fd.dev), 1947103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1948103285SikobEND_DEBUG 1949103285Sikob /* Disable disconnect and tagged queuing */ 1950103285Sikob cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID; 1951103285Sikob cts->flags = 0; 1952103285Sikob 1953103285Sikob cts->ccb_h.status = CAM_REQ_CMP; 1954103285Sikob xpt_done(ccb); 1955103285Sikob break; 1956103285Sikob } 1957103285Sikob case XPT_ABORT: 1958103285Sikob ccb->ccb_h.status = CAM_UA_ABORT; 1959103285Sikob xpt_done(ccb); 1960103285Sikob break; 1961103285Sikob default: 1962103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 1963103285Sikob xpt_done(ccb); 1964103285Sikob break; 1965103285Sikob } 1966103285Sikob return; 1967103285Sikob} 1968103285Sikob 1969103285Sikobstatic void 1970103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb) 1971103285Sikob{ 1972103285Sikob int s; 1973103285Sikob 1974103285Sikob s = splfw(); 1975103285Sikob sbp_action1(sim, ccb); 1976103285Sikob splx(s); 1977103285Sikob} 1978103285Sikob 1979103285Sikobstatic void 1980103285Sikobsbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 1981103285Sikob{ 1982103285Sikob int i; 1983103285Sikob struct sbp_ocb *ocb; 1984103285Sikob struct sbp_ocb *prev; 1985103285Sikob union ccb *ccb; 1986105633Ssimokawa bus_dma_segment_t *s; 1987103285Sikob 1988103285Sikob if (error) 1989103285Sikob printf("sbp_execute_ocb: error=%d\n", error); 1990103285Sikob 1991103285Sikob ocb = (struct sbp_ocb *)arg; 1992103285Sikob if (seg == 1) { 1993103285Sikob /* direct pointer */ 1994103285Sikob ocb->orb[3] = htonl(segments[0].ds_addr); 1995103285Sikob ocb->orb[4] |= htonl(segments[0].ds_len); 1996103285Sikob } else if(seg > 1) { 1997103285Sikob /* page table */ 1998103285SikobSBP_DEBUG(1) 1999103285Sikob printf("sbp_execute_ocb: seg %d", seg); 2000103285Sikob for (i = 0; i < seg; i++) 2001106543Ssimokawa printf(", %tx:%zd", segments[i].ds_addr, 2002103285Sikob segments[i].ds_len); 2003103285Sikob printf("\n"); 2004103285SikobEND_DEBUG 2005103285Sikob for (i = 0; i < seg; i++) { 2006105633Ssimokawa s = &segments[i]; 2007105633Ssimokawa#if 1 /* XXX LSI Logic "< 16 byte" bug might be hit */ 2008105633Ssimokawa if (s->ds_len < 16) 2009105633Ssimokawa printf("sbp_execute_ocb: warning, " 2010106543Ssimokawa "segment length(%zd) is less than 16." 2011105633Ssimokawa "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2012105633Ssimokawa#endif 2013105633Ssimokawa ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2014105633Ssimokawa ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2015103285Sikob } 2016103285Sikob ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2017103285Sikob } 2018103285Sikob 2019103285Sikob ccb = ocb->ccb; 2020103285Sikob prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2021103285Sikob if (prev) 2022103285Sikob sbp_doorbell(ocb->sdev); 2023103285Sikob else 2024103285Sikob sbp_orb_pointer(ocb->sdev, ocb); 2025103285Sikob} 2026103285Sikob 2027103285Sikobstatic void 2028103285Sikobsbp_poll(struct cam_sim *sim) 2029103285Sikob{ 2030103285Sikob /* should call fwohci_intr? */ 2031103285Sikob return; 2032103285Sikob} 2033103285Sikobstatic struct sbp_ocb * 2034103285Sikobsbp_dequeue_ocb(struct sbp_dev *sdev, u_int32_t orb_lo) 2035103285Sikob{ 2036103285Sikob struct sbp_ocb *ocb; 2037103285Sikob struct sbp_ocb *next; 2038103285Sikob int s = splfw(), order = 0; 2039103285Sikob int flags; 2040103285Sikob 2041103285Sikob for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2042103285Sikob next = STAILQ_NEXT(ocb, ocb); 2043103285Sikob flags = ocb->flags; 2044103285SikobSBP_DEBUG(1) 2045106543Ssimokawa printf("orb: 0x%tx next: 0x%x, flags %x\n", 2046103285Sikob vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags); 2047103285SikobEND_DEBUG 2048103285Sikob if (vtophys(&ocb->orb[0]) == orb_lo) { 2049103285Sikob /* found */ 2050103285Sikob if (ocb->flags & OCB_RESERVED) 2051103285Sikob ocb->flags |= OCB_DONE; 2052103285Sikob else 2053103285Sikob STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2054103285Sikob if (ocb->ccb != NULL) 2055103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2056103285Sikob ocb->ccb->ccb_h.timeout_ch); 2057103285Sikob if (ocb->dmamap != NULL) { 2058103285Sikob bus_dmamap_destroy(sdev->target->sbp->dmat, 2059103285Sikob ocb->dmamap); 2060103285Sikob ocb->dmamap = NULL; 2061103285Sikob } 2062103285Sikob break; 2063103285Sikob } else { 2064103285Sikob if ((ocb->flags & OCB_RESERVED) && 2065103285Sikob (ocb->flags & OCB_DONE)) { 2066103285Sikob /* next orb must be fetched already */ 2067103285Sikob STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2068103285Sikob sbp_free_ocb(sdev->target->sbp, ocb); 2069103285Sikob } else 2070103285Sikob order ++; 2071103285Sikob } 2072103285Sikob } 2073103285Sikob splx(s); 2074103285SikobSBP_DEBUG(0) 2075103285Sikob if (ocb && order > 0) { 2076103285Sikob sbp_show_sdev_info(sdev, 2); 2077103285Sikob printf("unordered execution order:%d\n", order); 2078103285Sikob } 2079103285SikobEND_DEBUG 2080103285Sikob return (ocb); 2081103285Sikob} 2082103285Sikob 2083103285Sikobstatic struct sbp_ocb * 2084103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2085103285Sikob{ 2086103285Sikob int s = splfw(); 2087103285Sikob struct sbp_ocb *prev; 2088103285Sikob 2089103285SikobSBP_DEBUG(2) 2090103285Sikob sbp_show_sdev_info(sdev, 2); 2091106543Ssimokawa printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0])); 2092103285SikobEND_DEBUG 2093103285Sikob prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2094103285Sikob STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2095103285Sikob 2096103285Sikob if (ocb->ccb != NULL) 2097103285Sikob ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2098103285Sikob (ocb->ccb->ccb_h.timeout * hz) / 1000); 2099103285Sikob 2100103285Sikob if (prev != NULL 2101103285Sikob && ((prev->flags & OCB_ACT_MASK) == OCB_ACT_CMD) 2102103285Sikob && ((ocb->flags & OCB_ACT_MASK) == OCB_ACT_CMD)) { 2103103285SikobSBP_DEBUG(1) 2104106543Ssimokawa printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]), 2105103285Sikob vtophys(&ocb->orb[0])); 2106103285SikobEND_DEBUG 2107103285Sikob prev->flags |= OCB_RESERVED; 2108103285Sikob prev->orb[1] = htonl(vtophys(&ocb->orb[0])); 2109103285Sikob prev->orb[0] = 0; 2110103285Sikob } else { 2111103285Sikob prev = NULL; 2112103285Sikob } 2113103285Sikob splx(s); 2114103285Sikob 2115103285Sikob return prev; 2116103285Sikob} 2117103285Sikob 2118103285Sikobstatic struct sbp_ocb * 2119103285Sikobsbp_get_ocb(struct sbp_softc *sbp) 2120103285Sikob{ 2121103285Sikob struct sbp_ocb *ocb; 2122103285Sikob int s = splfw(); 2123103285Sikob ocb = STAILQ_FIRST(&sbp->free_ocbs); 2124103285Sikob if (ocb == NULL) { 2125103285Sikob printf("ocb shortage!!!\n"); 2126103285Sikob return NULL; 2127103285Sikob } 2128103285Sikob STAILQ_REMOVE(&sbp->free_ocbs, ocb, sbp_ocb, ocb); 2129103285Sikob splx(s); 2130103285Sikob ocb->ccb = NULL; 2131103285Sikob return (ocb); 2132103285Sikob} 2133103285Sikob 2134103285Sikobstatic void 2135103285Sikobsbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb) 2136103285Sikob{ 2137103285Sikob#if 0 /* XXX make sure that ocb has ccb */ 2138103285Sikob if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 && 2139103285Sikob (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 2140103285Sikob ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2141103285Sikob sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2142103285Sikob } 2143103285Sikob#else 2144103285Sikob if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0) 2145103285Sikob sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2146103285Sikob#endif 2147103285Sikob ocb->flags = 0; 2148103285Sikob ocb->ccb = NULL; 2149103285Sikob STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb); 2150103285Sikob} 2151103285Sikob 2152103285Sikobstatic void 2153103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status) 2154103285Sikob{ 2155103285Sikob struct sbp_dev *sdev; 2156103285Sikob 2157103285Sikob sdev = ocb->sdev; 2158103285SikobSBP_DEBUG(0) 2159103285Sikob sbp_show_sdev_info(sdev, 2); 2160103285Sikob printf("sbp_abort_ocb 0x%x\n", status); 2161105792Ssimokawa if (ocb->ccb != NULL) 2162105792Ssimokawa sbp_print_scsi_cmd(ocb); 2163103285SikobEND_DEBUG 2164103285Sikob if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) { 2165103285Sikob if (status != CAM_CMD_TIMEOUT) 2166103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2167103285Sikob ocb->ccb->ccb_h.timeout_ch); 2168103285Sikob ocb->ccb->ccb_h.status = status; 2169103285Sikob xpt_done(ocb->ccb); 2170103285Sikob } 2171103285Sikob if (ocb->dmamap != NULL) { 2172103285Sikob bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap); 2173103285Sikob ocb->dmamap = NULL; 2174103285Sikob } 2175103285Sikob sbp_free_ocb(sdev->target->sbp, ocb); 2176103285Sikob} 2177103285Sikob 2178103285Sikobstatic void 2179103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2180103285Sikob{ 2181103285Sikob int s; 2182105792Ssimokawa struct sbp_ocb *ocb, *next; 2183105792Ssimokawa STAILQ_HEAD(, sbp_ocb) temp; 2184103285Sikob 2185103285Sikob s = splfw(); 2186105792Ssimokawa 2187105792Ssimokawa bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2188105792Ssimokawa STAILQ_INIT(&sdev->ocbs); 2189105792Ssimokawa for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2190105792Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2191103285Sikob sbp_abort_ocb(ocb, status); 2192103285Sikob } 2193105792Ssimokawa 2194103285Sikob splx(s); 2195103285Sikob} 2196103285Sikob 2197103285Sikobstatic devclass_t sbp_devclass; 2198103285Sikob 2199103285Sikobstatic device_method_t sbp_methods[] = { 2200103285Sikob /* device interface */ 2201103285Sikob DEVMETHOD(device_identify, sbp_identify), 2202103285Sikob DEVMETHOD(device_probe, sbp_probe), 2203103285Sikob DEVMETHOD(device_attach, sbp_attach), 2204103285Sikob DEVMETHOD(device_detach, sbp_detach), 2205103285Sikob 2206103285Sikob { 0, 0 } 2207103285Sikob}; 2208103285Sikob 2209103285Sikobstatic driver_t sbp_driver = { 2210103285Sikob "sbp", 2211103285Sikob sbp_methods, 2212103285Sikob sizeof(struct sbp_softc), 2213103285Sikob}; 2214103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2215103285SikobMODULE_VERSION(sbp, 1); 2216103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1); 2217103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1); 2218