sbp.c revision 110579
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 110579 2003-02-09 07:40:27Z 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 u_int16_t len; 173103285Sikob u_int16_t id; 174103285Sikob u_int16_t res0; 175103285Sikob u_int16_t cmd_hi; 176103285Sikob u_int32_t cmd_lo; 177103285Sikob u_int16_t res1; 178103285Sikob u_int16_t recon_hold; 179103285Sikob}; 180103285Sikobstruct sbp_status{ 181103285Sikob u_int8_t len:3, 182103285Sikob dead:1, 183103285Sikob resp:2, 184103285Sikob src:2; 185103285Sikob u_int8_t status:8; 186103285Sikob u_int16_t orb_hi; 187103285Sikob u_int32_t orb_lo; 188103285Sikob u_int32_t data[6]; 189103285Sikob}; 190103285Sikobstruct sbp_cmd_status{ 191103285Sikob#define SBP_SFMT_CURR 0 192103285Sikob#define SBP_SFMT_DEFER 1 193103285Sikob u_int8_t status:6, 194103285Sikob sfmt:2; 195103285Sikob u_int8_t s_key:4, 196103285Sikob ill_len:1, 197103285Sikob eom:1, 198103285Sikob mark:1, 199103285Sikob valid:1; 200103285Sikob u_int8_t s_code; 201103285Sikob u_int8_t s_qlfr; 202103285Sikob u_int32_t info; 203103285Sikob u_int32_t cdb; 204103285Sikob u_int32_t fru:8, 205103285Sikob s_keydep:24; 206103285Sikob u_int32_t vend[2]; 207103285Sikob}; 208103285Sikob 209103285Sikobstruct sbp_dev{ 210103285Sikob#define SBP_DEV_RESET 0 /* accept login */ 211110336Ssimokawa#if 0 212103285Sikob#define SBP_DEV_LOGIN 1 /* to login */ 213103285Sikob#define SBP_DEV_RECONN 2 /* to reconnect */ 214110336Ssimokawa#endif 215103285Sikob#define SBP_DEV_TOATTACH 3 /* to attach */ 216103285Sikob#define SBP_DEV_PROBE 4 /* scan lun */ 217103285Sikob#define SBP_DEV_ATTACHED 5 /* in operation */ 218103285Sikob#define SBP_DEV_DEAD 6 /* unavailable unit */ 219103285Sikob#define SBP_DEV_RETRY 7 /* unavailable unit */ 220110336Ssimokawa u_int8_t status:4, 221110336Ssimokawa#define SBP_DEV_TIMEOUT 1 222110336Ssimokawa flags:4; 223108503Ssimokawa u_int8_t type; 224108503Ssimokawa u_int16_t lun_id; 225103285Sikob struct cam_path *path; 226103285Sikob struct sbp_target *target; 227103285Sikob struct sbp_login_res login; 228103285Sikob STAILQ_HEAD(, sbp_ocb) ocbs; 229103285Sikob char vendor[32]; 230103285Sikob char product[32]; 231103285Sikob char revision[10]; 232103285Sikob}; 233103285Sikob 234103285Sikobstruct sbp_target { 235103285Sikob int target_id; 236103285Sikob int num_lun; 237103285Sikob struct sbp_dev *luns; 238103285Sikob struct sbp_softc *sbp; 239103285Sikob struct fw_device *fwdev; 240103285Sikob u_int32_t mgm_hi, mgm_lo; 241103285Sikob}; 242103285Sikob 243103285Sikobstruct sbp_softc { 244103285Sikob struct firewire_dev_comm fd; 245103285Sikob unsigned char flags; 246103285Sikob struct cam_sim *sim; 247103285Sikob struct sbp_target targets[SBP_NUM_TARGETS]; 248103285Sikob struct fw_bind fwb; 249103285Sikob STAILQ_HEAD(, sbp_ocb) free_ocbs; 250103285Sikob struct sbp_ocb *ocb; 251103285Sikob bus_dma_tag_t dmat; 252103285Sikob}; 253103285Sikobstatic void sbp_post_explore __P((void *)); 254103285Sikobstatic void sbp_recv __P((struct fw_xfer *)); 255103285Sikobstatic void sbp_login_callback __P((struct fw_xfer *)); 256103285Sikobstatic void sbp_cmd_callback __P((struct fw_xfer *)); 257103285Sikobstatic void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *)); 258103285Sikobstatic void sbp_execute_ocb __P((void *, bus_dma_segment_t *, int, int)); 259103285Sikobstatic void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *)); 260103285Sikobstatic void sbp_abort_ocb __P((struct sbp_ocb *, int)); 261103285Sikobstatic void sbp_abort_all_ocbs __P((struct sbp_dev *, int)); 262103285Sikobstatic struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int)); 263103285Sikobstatic struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *)); 264103285Sikobstatic struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *)); 265103285Sikobstatic struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, u_int32_t)); 266110145Ssimokawastatic void sbp_cam_detach_target __P((struct sbp_target *)); 267103285Sikobstatic void sbp_timeout __P((void *arg)); 268110336Ssimokawastatic void sbp_mgm_orb __P((struct sbp_dev *, int, u_int16_t, u_int32_t)); 269103285Sikob 270108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire"); 271103285Sikob 272103285Sikob/* cam related functions */ 273103285Sikobstatic void sbp_action(struct cam_sim *sim, union ccb *ccb); 274103285Sikobstatic void sbp_poll(struct cam_sim *sim); 275103285Sikobstatic void sbp_cam_callback(struct cam_periph *periph, 276103285Sikob union ccb *ccb); 277103285Sikobstatic void sbp_cam_scan_lun(struct sbp_dev *sdev); 278103285Sikob 279103285Sikobstatic char *orb_status0[] = { 280103285Sikob /* 0 */ "No additional information to report", 281103285Sikob /* 1 */ "Request type not supported", 282103285Sikob /* 2 */ "Speed not supported", 283103285Sikob /* 3 */ "Page size not supported", 284103285Sikob /* 4 */ "Access denied", 285103285Sikob /* 5 */ "Logical unit not supported", 286103285Sikob /* 6 */ "Maximum payload too small", 287103285Sikob /* 7 */ "Reserved for future standardization", 288103285Sikob /* 8 */ "Resources unavailable", 289103285Sikob /* 9 */ "Function rejected", 290103285Sikob /* A */ "Login ID not recognized", 291103285Sikob /* B */ "Dummy ORB completed", 292103285Sikob /* C */ "Request aborted", 293103285Sikob /* FF */ "Unspecified error" 294103285Sikob#define MAX_ORB_STATUS0 0xd 295103285Sikob}; 296103285Sikob 297103285Sikobstatic char *orb_status1_object[] = { 298103285Sikob /* 0 */ "Operation request block (ORB)", 299103285Sikob /* 1 */ "Data buffer", 300103285Sikob /* 2 */ "Page table", 301103285Sikob /* 3 */ "Unable to specify" 302103285Sikob}; 303103285Sikob 304103285Sikobstatic char *orb_status1_serial_bus_error[] = { 305103285Sikob /* 0 */ "Missing acknowledge", 306103285Sikob /* 1 */ "Reserved; not to be used", 307103285Sikob /* 2 */ "Time-out error", 308103285Sikob /* 3 */ "Reserved; not to be used", 309103285Sikob /* 4 */ "Busy retry limit exceeded(X)", 310103285Sikob /* 5 */ "Busy retry limit exceeded(A)", 311103285Sikob /* 6 */ "Busy retry limit exceeded(B)", 312103285Sikob /* 7 */ "Reserved for future standardization", 313103285Sikob /* 8 */ "Reserved for future standardization", 314103285Sikob /* 9 */ "Reserved for future standardization", 315103285Sikob /* A */ "Reserved for future standardization", 316103285Sikob /* B */ "Tardy retry limit exceeded", 317103285Sikob /* C */ "Conflict error", 318103285Sikob /* D */ "Data error", 319103285Sikob /* E */ "Type error", 320103285Sikob /* F */ "Address error" 321103285Sikob}; 322103285Sikob 323103285Sikobstatic void 324103285Sikobsbp_identify(driver_t *driver, device_t parent) 325103285Sikob{ 326103285Sikob device_t child; 327103285SikobSBP_DEBUG(0) 328103285Sikob printf("sbp_identify\n"); 329103285SikobEND_DEBUG 330103285Sikob 331103285Sikob child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 332103285Sikob} 333103285Sikob 334103285Sikob/* 335103285Sikob * sbp_probe() 336103285Sikob */ 337103285Sikobstatic int 338103285Sikobsbp_probe(device_t dev) 339103285Sikob{ 340103285Sikob device_t pa; 341103285Sikob 342103285SikobSBP_DEBUG(0) 343103285Sikob printf("sbp_probe\n"); 344103285SikobEND_DEBUG 345103285Sikob 346103285Sikob pa = device_get_parent(dev); 347103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 348103285Sikob return(ENXIO); 349103285Sikob } 350103285Sikob 351103285Sikob device_set_desc(dev, "SBP2/SCSI over firewire"); 352103285Sikob return (0); 353103285Sikob} 354103285Sikob 355103285Sikobstatic void 356103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new) 357103285Sikob{ 358103285Sikob struct fw_device *fwdev; 359103285Sikob 360103285Sikob printf("%s:%d:%d ", 361103285Sikob device_get_nameunit(sdev->target->sbp->fd.dev), 362103285Sikob sdev->target->target_id, 363103285Sikob sdev->lun_id 364103285Sikob ); 365103285Sikob if (new == 2) { 366103285Sikob return; 367103285Sikob } 368103285Sikob fwdev = sdev->target->fwdev; 369103285Sikob printf("ordered:%d type:%d EUI:%08x%08x node:%d " 370103285Sikob "speed:%d maxrec:%d", 371108503Ssimokawa (sdev->type & 0x40) >> 6, 372108503Ssimokawa (sdev->type & 0x1f), 373103285Sikob fwdev->eui.hi, 374103285Sikob fwdev->eui.lo, 375103285Sikob fwdev->dst, 376103285Sikob fwdev->speed, 377103285Sikob fwdev->maxrec 378103285Sikob ); 379103285Sikob if (new) 380103285Sikob printf(" new!\n"); 381103285Sikob else 382103285Sikob printf("\n"); 383103285Sikob sbp_show_sdev_info(sdev, 2); 384103285Sikob printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 385103285Sikob} 386103285Sikob 387110184Ssimokawastatic struct { 388110184Ssimokawa int bus; 389110184Ssimokawa int target; 390110184Ssimokawa struct fw_eui64 eui; 391110184Ssimokawa} wired[] = { 392110184Ssimokawa /* Bus Target EUI64 */ 393110184Ssimokawa#if 0 394110184Ssimokawa {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */ 395110184Ssimokawa {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */ 396110184Ssimokawa {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */ 397110184Ssimokawa#endif 398110184Ssimokawa {-1, -1, {0,0}} 399110184Ssimokawa}; 400110184Ssimokawa 401110184Ssimokawastatic int 402110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev) 403110184Ssimokawa{ 404110184Ssimokawa int bus, i, target=-1; 405110184Ssimokawa char w[SBP_NUM_TARGETS]; 406110184Ssimokawa 407110184Ssimokawa bzero(w, sizeof(w)); 408110184Ssimokawa bus = device_get_unit(sbp->fd.dev); 409110184Ssimokawa 410110184Ssimokawa /* XXX wired-down configuration should be gotten from 411110184Ssimokawa tunable or device hint */ 412110184Ssimokawa for (i = 0; wired[i].bus >= 0; i ++) { 413110184Ssimokawa if (wired[i].bus == bus) { 414110184Ssimokawa w[wired[i].target] = 1; 415110184Ssimokawa if (wired[i].eui.hi == fwdev->eui.hi && 416110184Ssimokawa wired[i].eui.lo == fwdev->eui.lo) 417110184Ssimokawa target = wired[i].target; 418110184Ssimokawa } 419110184Ssimokawa } 420110184Ssimokawa if (target >= 0) { 421110184Ssimokawa if(target < SBP_NUM_TARGETS && 422110184Ssimokawa sbp->targets[target].fwdev == NULL) 423110184Ssimokawa return(target); 424110184Ssimokawa device_printf(sbp->fd.dev, 425110184Ssimokawa "target %d is not free for %08x:%08x\n", 426110184Ssimokawa target, fwdev->eui.hi, fwdev->eui.lo); 427110184Ssimokawa target = -1; 428110184Ssimokawa } 429110184Ssimokawa /* non-wired target */ 430110184Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 431110184Ssimokawa if (sbp->targets[i].fwdev == NULL && w[i] == 0) { 432110184Ssimokawa target = i; 433110184Ssimokawa break; 434110184Ssimokawa } 435110184Ssimokawa 436110184Ssimokawa return target; 437110184Ssimokawa} 438110184Ssimokawa 439103285Sikobstatic struct sbp_target * 440103285Sikobsbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 441103285Sikob{ 442108503Ssimokawa int i, maxlun, lun; 443103285Sikob struct sbp_target *target; 444103285Sikob struct sbp_dev *sdev; 445108503Ssimokawa struct crom_context cc; 446108503Ssimokawa struct csrreg *reg; 447103285Sikob 448103285SikobSBP_DEBUG(1) 449103285Sikob printf("sbp_alloc_target\n"); 450103285SikobEND_DEBUG 451110184Ssimokawa i = sbp_new_target(sbp, fwdev); 452110184Ssimokawa if (i < 0) { 453110184Ssimokawa device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n"); 454103285Sikob return NULL; 455103285Sikob } 456103285Sikob /* new target */ 457103285Sikob target = &sbp->targets[i]; 458103285Sikob target->sbp = sbp; 459103285Sikob target->fwdev = fwdev; 460103285Sikob target->target_id = i; 461103285Sikob if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){ 462103285Sikob /* bad target */ 463103285Sikob printf("NULL management address\n"); 464103285Sikob target->fwdev = NULL; 465103285Sikob return NULL; 466103285Sikob } 467103285Sikob target->mgm_hi = 0xffff; 468103285Sikob target->mgm_lo = 0xf0000000 | target->mgm_lo << 2; 469103285Sikob /* XXX num_lun may be changed. realloc luns? */ 470108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 471108503Ssimokawa /* XXX shoud parse appropriate unit directories only */ 472108503Ssimokawa maxlun = -1; 473108503Ssimokawa while (cc.depth >= 0) { 474108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 475108503Ssimokawa if (reg == NULL) 476108503Ssimokawa break; 477108503Ssimokawa lun = reg->val & 0xff; 478108642SsimokawaSBP_DEBUG(0) 479108642Ssimokawa printf("target %d lun %d found\n", target->target_id, lun); 480108642SsimokawaEND_DEBUG 481108503Ssimokawa if (maxlun < lun) 482108503Ssimokawa maxlun = lun; 483108503Ssimokawa crom_next(&cc); 484108503Ssimokawa } 485108503Ssimokawa target->num_lun = maxlun + 1; 486108503Ssimokawa if (maxlun < 0) { 487108503Ssimokawa printf("no lun found!\n"); 488108503Ssimokawa } 489103285Sikob target->luns = (struct sbp_dev *) malloc( 490103285Sikob sizeof(struct sbp_dev) * target->num_lun, 491103285Sikob M_SBP, M_NOWAIT | M_ZERO); 492103285Sikob for (i = 0; i < target->num_lun; i++) { 493103285Sikob sdev = &target->luns[i]; 494103285Sikob sdev->lun_id = i; 495103285Sikob sdev->target = target; 496103285Sikob STAILQ_INIT(&sdev->ocbs); 497108503Ssimokawa sdev->status = SBP_DEV_DEAD; 498103285Sikob } 499108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 500108503Ssimokawa while (cc.depth >= 0) { 501108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 502108503Ssimokawa if (reg == NULL) 503108503Ssimokawa break; 504108503Ssimokawa lun = reg->val & 0xff; 505108503Ssimokawa target->luns[lun].status = SBP_DEV_RESET; 506108503Ssimokawa target->luns[lun].type = (reg->val & 0x0f00) >> 16; 507108503Ssimokawa crom_next(&cc); 508108503Ssimokawa } 509103285Sikob return target; 510103285Sikob} 511103285Sikob 512103285Sikobstatic void 513103285Sikobsbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len) 514103285Sikob{ 515103285Sikob static char *nullstr = "(null)"; 516103285Sikob int i, clen, found=0; 517103285Sikob struct csrhdr *chdr; 518103285Sikob struct csrreg *creg; 519103285Sikob u_int32_t *src, *dst; 520103285Sikob 521103285Sikob chdr = (struct csrhdr *)&fwdev->csrrom[0]; 522103285Sikob creg = (struct csrreg *)chdr; 523103285Sikob creg += chdr->info_len; 524103285Sikob for( i = chdr->info_len + 4; i <= fwdev->rommax; i+=4){ 525103285Sikob if((creg++)->key == key){ 526103285Sikob found = 1; 527103285Sikob break; 528103285Sikob } 529103285Sikob } 530103285Sikob if (!found) { 531103285Sikob strncpy(buf, nullstr, len); 532103285Sikob return; 533103285Sikob } 534103285Sikob src = (u_int32_t *) creg + creg->val; 535103285Sikob clen = ((*src >> 16) - 2) * 4; 536103285Sikob src += 3; 537103285Sikob dst = (u_int32_t *) buf; 538103285Sikob if (len < clen) 539103285Sikob clen = len; 540103285Sikob for (i = 0; i < clen/4; i++) 541103285Sikob *dst++ = htonl(*src++); 542103285Sikob buf[clen] = 0; 543103285Sikob} 544103285Sikob 545103285Sikobstatic void 546103285Sikobsbp_probe_lun(struct sbp_dev *sdev) 547103285Sikob{ 548103285Sikob struct fw_device *fwdev; 549103285Sikob int rev; 550103285Sikob 551103285Sikob fwdev = sdev->target->fwdev; 552103285Sikob bzero(sdev->vendor, sizeof(sdev->vendor)); 553103285Sikob bzero(sdev->product, sizeof(sdev->product)); 554103285Sikob sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor)); 555103285Sikob sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product)); 556103285Sikob rev = getcsrdata(sdev->target->fwdev, 0x3c); 557103285Sikob snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev); 558103285Sikob} 559103285Sikobstatic void 560103285Sikobsbp_probe_target(struct sbp_target *target, int alive) 561103285Sikob{ 562103285Sikob struct sbp_softc *sbp; 563103285Sikob struct sbp_dev *sdev; 564103285Sikob struct firewire_comm *fc; 565103285Sikob int i; 566103285Sikob 567103285SikobSBP_DEBUG(1) 568103285Sikob printf("sbp_probe_target %d\n", target->target_id); 569103285Sikob if (!alive) 570103285Sikob printf("not alive\n"); 571103285SikobEND_DEBUG 572103285Sikob 573103285Sikob sbp = target->sbp; 574103285Sikob fc = target->sbp->fd.fc; 575103285Sikob for (i=0; i < target->num_lun; i++) { 576103285Sikob sdev = &target->luns[i]; 577103285Sikob if (alive && (sdev->status != SBP_DEV_DEAD)) { 578103285Sikob if (sdev->path != NULL) { 579103285Sikob xpt_freeze_devq(sdev->path, 1); 580103285Sikob } 581103285Sikob sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 582103285Sikob switch (sdev->status) { 583110336Ssimokawa case SBP_DEV_RESET: 584103285Sikob /* new or revived target */ 585103285Sikob sbp_probe_lun(sdev); 586103285Sikob if (auto_login) { 587103285Sikob sdev->status = SBP_DEV_TOATTACH; 588110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGI, 0, 0); 589103285Sikob } 590103285Sikob break; 591110336Ssimokawa case SBP_DEV_RETRY: 592110336Ssimokawa sbp_probe_lun(sdev); 593110336Ssimokawa default: 594110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RCN, 0, 0); 595110336Ssimokawa break; 596103285Sikob } 597107653SsimokawaSBP_DEBUG(0) 598103285Sikob sbp_show_sdev_info(sdev, 599103285Sikob (sdev->status == SBP_DEV_TOATTACH)); 600107653SsimokawaEND_DEBUG 601103285Sikob } else { 602103285Sikob switch (sdev->status) { 603103285Sikob case SBP_DEV_ATTACHED: 604103285SikobSBP_DEBUG(0) 605103285Sikob /* the device has gone */ 606103285Sikob sbp_show_sdev_info(sdev, 2); 607103285Sikob printf("lost target\n"); 608103285SikobEND_DEBUG 609103285Sikob if (sdev->path) 610103285Sikob xpt_freeze_devq(sdev->path, 1); 611103285Sikob sdev->status = SBP_DEV_RETRY; 612103285Sikob sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 613103285Sikob break; 614103285Sikob case SBP_DEV_PROBE: 615103285Sikob case SBP_DEV_TOATTACH: 616103285Sikob sdev->status = SBP_DEV_RESET; 617103285Sikob break; 618103285Sikob case SBP_DEV_RETRY: 619103285Sikob case SBP_DEV_RESET: 620103285Sikob case SBP_DEV_DEAD: 621103285Sikob break; 622103285Sikob } 623103285Sikob } 624103285Sikob } 625103285Sikob} 626103285Sikob 627103285Sikob#if 0 628103285Sikobstatic void 629103285Sikobsbp_release_queue(void *arg) 630103285Sikob{ 631103285Sikob struct sbp_softc *sbp; 632103285Sikob 633103285SikobSBP_DEBUG(0) 634103285Sikob printf("sbp_release_queue\n"); 635103285SikobEND_DEBUG 636103285Sikob sbp = (struct sbp_softc *)arg; 637103285Sikob xpt_release_simq(sbp->sim, 1); 638103285Sikob} 639103285Sikob 640103285Sikobstatic void 641103285Sikobsbp_release_devq(void *arg) 642103285Sikob{ 643103285Sikob struct sbp_dev *sdev; 644103285Sikob int s; 645103285Sikob 646103285Sikob sdev = (struct sbp_dev *)arg; 647103285SikobSBP_DEBUG(0) 648103285Sikob sbp_show_sdev_info(sdev, 2); 649103285Sikob printf("sbp_release_devq\n"); 650103285SikobEND_DEBUG 651103285Sikob s = splcam(); 652103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 653103285Sikob splx(s); 654103285Sikob} 655103285Sikob#endif 656103285Sikob 657103285Sikobstatic void 658103285Sikobsbp_post_explore(void *arg) 659103285Sikob{ 660103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)arg; 661103285Sikob struct sbp_target *target; 662103285Sikob struct fw_device *fwdev; 663103285Sikob int i, alive; 664103285Sikob 665103285SikobSBP_DEBUG(1) 666103285Sikob printf("sbp_post_explore\n"); 667103285SikobEND_DEBUG 668103285Sikob#if 0 669103285Sikob xpt_freeze_simq(sbp->sim, /*count*/ 1); 670103285Sikob#endif 671103285Sikob /* Gabage Collection */ 672103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 673103285Sikob target = &sbp->targets[i]; 674110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) 675110193Ssimokawa if (target->fwdev == NULL || target->fwdev == fwdev) 676110193Ssimokawa break; 677103285Sikob if(fwdev == NULL){ 678103285Sikob /* device has removed in lower driver */ 679110145Ssimokawa sbp_cam_detach_target(target); 680110145Ssimokawa if (target->luns != NULL) 681110145Ssimokawa free(target->luns, M_SBP); 682110145Ssimokawa target->num_lun = 0;; 683110145Ssimokawa target->luns = NULL; 684110145Ssimokawa target->fwdev = NULL; 685103285Sikob } 686103285Sikob } 687103285Sikob /* traverse device list */ 688110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) { 689103285SikobSBP_DEBUG(0) 690103285Sikob printf("sbp_post_explore: EUI:%08x%08x ", 691103285Sikob fwdev->eui.hi, fwdev->eui.lo); 692103285Sikob if (fwdev->status == FWDEVATTACHED) { 693103285Sikob printf("spec=%d key=%d.\n", 694103285Sikob getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10, 695103285Sikob getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 696103285Sikob } else { 697103285Sikob printf("not attached, state=%d.\n", fwdev->status); 698103285Sikob } 699103285SikobEND_DEBUG 700103285Sikob alive = (fwdev->status == FWDEVATTACHED) 701103285Sikob && (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10) 702103285Sikob && (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 703103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 704103285Sikob target = &sbp->targets[i]; 705103285Sikob if(target->fwdev == fwdev ) { 706103285Sikob /* known target */ 707103285Sikob break; 708103285Sikob } 709103285Sikob } 710103285Sikob if(i == SBP_NUM_TARGETS){ 711103285Sikob if (alive) { 712103285Sikob /* new target */ 713103285Sikob target = sbp_alloc_target(sbp, fwdev); 714103285Sikob if (target == NULL) 715103285Sikob continue; 716103285Sikob } else { 717103285Sikob continue; 718103285Sikob } 719103285Sikob } 720103285Sikob sbp_probe_target(target, alive); 721103285Sikob } 722103285Sikob#if 0 723103285Sikob timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000); 724103285Sikob#endif 725103285Sikob} 726103285Sikob 727103285Sikob#if NEED_RESPONSE 728103285Sikobstatic void 729103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){ 730103285SikobSBP_DEBUG(1) 731103285Sikob struct sbp_dev *sdev; 732103285Sikob sdev = (struct sbp_dev *)xfer->sc; 733103285Sikob sbp_show_sdev_info(sdev, 2); 734103285Sikob printf("sbp_loginres_callback\n"); 735103285SikobEND_DEBUG 736103285Sikob fw_xfer_free(xfer); 737103285Sikob return; 738103285Sikob} 739103285Sikob#endif 740103285Sikob 741103285Sikobstatic void 742103285Sikobsbp_login_callback(struct fw_xfer *xfer) 743103285Sikob{ 744103285SikobSBP_DEBUG(1) 745103285Sikob struct sbp_dev *sdev; 746103285Sikob sdev = (struct sbp_dev *)xfer->sc; 747103285Sikob sbp_show_sdev_info(sdev, 2); 748103285Sikob printf("sbp_login_callback\n"); 749103285SikobEND_DEBUG 750103285Sikob fw_xfer_free(xfer); 751103285Sikob return; 752103285Sikob} 753103285Sikob 754103285Sikobstatic void 755103285Sikobsbp_cmd_callback(struct fw_xfer *xfer) 756103285Sikob{ 757103285SikobSBP_DEBUG(2) 758103285Sikob struct sbp_dev *sdev; 759103285Sikob sdev = (struct sbp_dev *)xfer->sc; 760103285Sikob sbp_show_sdev_info(sdev, 2); 761103285Sikob printf("sbp_cmd_callback\n"); 762103285SikobEND_DEBUG 763103285Sikob fw_xfer_free(xfer); 764103285Sikob return; 765103285Sikob} 766103285Sikob 767103285Sikobstatic void 768103285Sikobsbp_cam_callback(struct cam_periph *periph, union ccb *ccb) 769103285Sikob{ 770103285Sikob struct sbp_dev *sdev; 771103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 772110269SsimokawaSBP_DEBUG(0) 773103285Sikob sbp_show_sdev_info(sdev, 2); 774103285Sikob printf("sbp_cam_callback\n"); 775103285SikobEND_DEBUG 776103285Sikob sdev->status = SBP_DEV_ATTACHED; 777103285Sikob free(ccb, M_SBP); 778103285Sikob} 779103285Sikob 780103285Sikobstatic void 781103285Sikobsbp_cam_scan_lun(struct sbp_dev *sdev) 782103285Sikob{ 783109623Salfred union ccb *ccb = malloc(sizeof(union ccb), M_SBP, M_ZERO); 784103285Sikob 785103285SikobSBP_DEBUG(0) 786103285Sikob sbp_show_sdev_info(sdev, 2); 787103285Sikob printf("sbp_cam_scan_lun\n"); 788103285SikobEND_DEBUG 789103285Sikob xpt_setup_ccb(&ccb->ccb_h, sdev->path, 5/*priority (low)*/); 790103285Sikob ccb->ccb_h.func_code = XPT_SCAN_LUN; 791103285Sikob ccb->ccb_h.cbfcnp = sbp_cam_callback; 792103285Sikob ccb->crcn.flags = CAM_FLAG_NONE; 793103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 794103285Sikob xpt_action(ccb); 795103285Sikob 796103285Sikob /* The scan is in progress now. */ 797103285Sikob} 798103285Sikob 799103285Sikob 800103285Sikobstatic void 801103285Sikobsbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb) 802103285Sikob{ 803103285Sikob struct sbp_dev *sdev; 804103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 805110269SsimokawaSBP_DEBUG(0) 806103285Sikob sbp_show_sdev_info(sdev, 2); 807103285Sikob printf("sbp_ping_unit_callback\n"); 808103285SikobEND_DEBUG 809103285Sikob if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 810103285Sikob if (--ccb->ccb_h.retry_count == 0) { 811103285Sikob sbp_show_sdev_info(sdev, 2); 812110336Ssimokawa printf("sbp_ping_unit_callback: " 813110336Ssimokawa "retry count exceeded\n"); 814103285Sikob sdev->status = SBP_DEV_RETRY; 815103285Sikob free(ccb, M_SBP); 816103285Sikob } else { 817103285Sikob /* requeue */ 818103285Sikob xpt_action(ccb); 819103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 820103285Sikob } 821103285Sikob } else { 822103285Sikob free(ccb->csio.data_ptr, M_SBP); 823103285Sikob free(ccb, M_SBP); 824103285Sikob sdev->status = SBP_DEV_ATTACHED; 825103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 826103285Sikob } 827103285Sikob} 828103285Sikob 829103285Sikob/* 830103285Sikob * XXX Some devices need to execute inquiry or read_capacity 831103285Sikob * after bus_rest during busy transfer. 832103285Sikob * Otherwise they return incorrect result for READ(and WRITE?) 833103285Sikob * command without any SBP-II/SCSI error. 834103285Sikob * 835103285Sikob * e.g. Maxtor 3000XT, Yano A-dish. 836103285Sikob */ 837103285Sikobstatic void 838103285Sikobsbp_ping_unit(struct sbp_dev *sdev) 839103285Sikob{ 840103285Sikob union ccb *ccb; 841103285Sikob struct scsi_inquiry_data *inq_buf; 842103285Sikob 843109623Salfred ccb = malloc(sizeof(union ccb), M_SBP, M_ZERO); 844103285Sikob inq_buf = (struct scsi_inquiry_data *) 845109623Salfred malloc(sizeof(*inq_buf), M_SBP, 0); 846103285Sikob 847110269SsimokawaSBP_DEBUG(0) 848103285Sikob sbp_show_sdev_info(sdev, 2); 849103285Sikob printf("sbp_ping_unit\n"); 850103285SikobEND_DEBUG 851103285Sikob 852103285Sikob /* 853103285Sikob * We need to execute this command before any other queued command. 854103285Sikob * Make priority 0 and freeze queue after execution for retry. 855103285Sikob * cam's scan_lun command doesn't provide this feature. 856103285Sikob */ 857103285Sikob xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/); 858103285Sikob scsi_inquiry( 859103285Sikob &ccb->csio, 860103285Sikob /*retries*/ 5, 861103285Sikob sbp_ping_unit_callback, 862103285Sikob MSG_SIMPLE_Q_TAG, 863103285Sikob (u_int8_t *)inq_buf, 864103285Sikob SHORT_INQUIRY_LENGTH, 865103285Sikob /*evpd*/FALSE, 866103285Sikob /*page_code*/0, 867103285Sikob SSD_MIN_SIZE, 868103285Sikob /*timeout*/60000 869103285Sikob ); 870103285Sikob ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 871103285Sikob xpt_action(ccb); 872110336Ssimokawa 873110336Ssimokawa if (sdev->status == SBP_DEV_RETRY) 874110336Ssimokawa /* freezed twice */ 875110336Ssimokawa xpt_release_devq(sdev->path, 1, TRUE); 876103285Sikob} 877103285Sikob 878103285Sikobstatic void 879103285Sikobsbp_do_attach(struct fw_xfer *xfer) 880103285Sikob{ 881103285Sikob struct sbp_dev *sdev; 882103285Sikob 883103285Sikob sdev = (struct sbp_dev *)xfer->sc; 884103285SikobSBP_DEBUG(0) 885103285Sikob sbp_show_sdev_info(sdev, 2); 886103285Sikob printf("sbp_do_attach\n"); 887103285SikobEND_DEBUG 888103285Sikob fw_xfer_free(xfer); 889103285Sikob if (sdev->path == NULL) 890103285Sikob xpt_create_path(&sdev->path, xpt_periph, 891103285Sikob cam_sim_path(sdev->target->sbp->sim), 892103285Sikob sdev->target->target_id, sdev->lun_id); 893103285Sikob 894103285Sikob if (sdev->status == SBP_DEV_RETRY) { 895110336Ssimokawa sbp_ping_unit(sdev); 896103285Sikob sdev->status = SBP_DEV_PROBE; 897103285Sikob } else { 898103285Sikob sdev->status = SBP_DEV_PROBE; 899103285Sikob sbp_cam_scan_lun(sdev); 900103285Sikob } 901103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 902103285Sikob return; 903103285Sikob} 904103285Sikob 905103285Sikobstatic void 906103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer) 907103285Sikob{ 908103285Sikob struct sbp_dev *sdev; 909103285Sikob 910103285Sikob sdev = (struct sbp_dev *)xfer->sc; 911103285SikobSBP_DEBUG(1) 912103285Sikob sbp_show_sdev_info(sdev, 2); 913103285Sikob printf("sbp_cmd_callback\n"); 914103285SikobEND_DEBUG 915103285Sikob fw_xfer_free(xfer); 916103285Sikob sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ); 917103285Sikob if (sdev->path) 918103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 919103285Sikob} 920103285Sikob 921103285Sikobstatic void 922110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev) 923103285Sikob{ 924103285Sikob struct fw_xfer *xfer; 925103285Sikob struct fw_pkt *fp; 926103285Sikob 927103285SikobSBP_DEBUG(0) 928103285Sikob sbp_show_sdev_info(sdev, 2); 929103285Sikob printf("sbp_agent_reset\n"); 930103285SikobEND_DEBUG 931103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 932103285Sikob if (xfer == NULL) 933103285Sikob return; 934110336Ssimokawa if (sdev->status == SBP_DEV_ATTACHED) 935110336Ssimokawa xfer->act.hand = sbp_agent_reset_callback; 936110336Ssimokawa else 937103285Sikob xfer->act.hand = sbp_do_attach; 938103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 939103285Sikob fp->mode.wreqq.data = htonl(0xf); 940103285Sikob fw_asyreq(xfer->fc, -1, xfer); 941103285Sikob} 942103285Sikob 943103285Sikobstatic void 944103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer) 945103285Sikob{ 946103285Sikob struct sbp_dev *sdev; 947103285Sikob 948103285Sikob sdev = (struct sbp_dev *)xfer->sc; 949103285SikobSBP_DEBUG(1) 950103285Sikob sbp_show_sdev_info(sdev, 2); 951110336Ssimokawa printf("sbp_busy_timeout_callback\n"); 952103285SikobEND_DEBUG 953103285Sikob fw_xfer_free(xfer); 954110336Ssimokawa sbp_agent_reset(sdev); 955103285Sikob} 956103285Sikob 957103285Sikobstatic void 958103285Sikobsbp_busy_timeout(struct sbp_dev *sdev) 959103285Sikob{ 960103285Sikob struct fw_pkt *fp; 961103285Sikob struct fw_xfer *xfer; 962103285SikobSBP_DEBUG(0) 963103285Sikob sbp_show_sdev_info(sdev, 2); 964103285Sikob printf("sbp_busy_timeout\n"); 965103285SikobEND_DEBUG 966103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 967103285Sikob 968103285Sikob xfer->act.hand = sbp_busy_timeout_callback; 969103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 970103285Sikob fp->mode.wreqq.dest_hi = htons(0xffff); 971110129Ssimokawa fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT); 972110129Ssimokawa fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 973103285Sikob fw_asyreq(xfer->fc, -1, xfer); 974103285Sikob} 975103285Sikob 976103285Sikob#if 0 977103285Sikobstatic void 978103285Sikobsbp_reset_start(struct sbp_dev *sdev) 979103285Sikob{ 980103285Sikob struct fw_xfer *xfer; 981103285Sikob struct fw_pkt *fp; 982103285Sikob 983103285SikobSBP_DEBUG(0) 984103285Sikob sbp_show_sdev_info(sdev, 2); 985103285Sikob printf("sbp_reset_start\n"); 986103285SikobEND_DEBUG 987103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 988103285Sikob 989103285Sikob xfer->act.hand = sbp_busy_timeout; 990103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 991103285Sikob fp->mode.wreqq.dest_hi = htons(0xffff); 992103285Sikob fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START); 993103285Sikob fp->mode.wreqq.data = htonl(0xf); 994103285Sikob fw_asyreq(xfer->fc, -1, xfer); 995103285Sikob} 996103285Sikob#endif 997103285Sikob 998103285Sikobstatic void 999103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1000103285Sikob{ 1001103285Sikob struct fw_xfer *xfer; 1002103285Sikob struct fw_pkt *fp; 1003103285SikobSBP_DEBUG(2) 1004103285Sikob sbp_show_sdev_info(sdev, 2); 1005103285Sikob printf("sbp_orb_pointer\n"); 1006103285SikobEND_DEBUG 1007103285Sikob 1008103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 1009103285Sikob if (xfer == NULL) 1010103285Sikob return; 1011103285Sikob xfer->act.hand = sbp_cmd_callback; 1012103285Sikob 1013103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1014103285Sikob fp->mode.wreqb.len = htons(8); 1015103285Sikob fp->mode.wreqb.extcode = 0; 1016103285Sikob fp->mode.wreqb.payload[0] = 1017103285Sikob htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1018103285Sikob fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1019103285Sikob 1020103285Sikob if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 1021103285Sikob fw_xfer_free(xfer); 1022103285Sikob ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 1023103285Sikob xpt_done(ocb->ccb); 1024103285Sikob } 1025103285Sikob} 1026103285Sikob 1027103285Sikobstatic void 1028103285Sikobsbp_doorbell(struct sbp_dev *sdev) 1029103285Sikob{ 1030103285Sikob struct fw_xfer *xfer; 1031103285Sikob struct fw_pkt *fp; 1032103285SikobSBP_DEBUG(1) 1033103285Sikob sbp_show_sdev_info(sdev, 2); 1034103285Sikob printf("sbp_doorbell\n"); 1035103285SikobEND_DEBUG 1036103285Sikob 1037103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 1038103285Sikob if (xfer == NULL) 1039103285Sikob return; 1040103285Sikob xfer->act.hand = sbp_cmd_callback; 1041103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1042103285Sikob fp->mode.wreqq.data = htonl(0xf); 1043103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1044103285Sikob} 1045103285Sikob 1046103285Sikobstatic struct fw_xfer * 1047103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1048103285Sikob{ 1049103285Sikob struct fw_xfer *xfer; 1050103285Sikob struct fw_pkt *fp; 1051103285Sikob 1052110269Ssimokawa xfer = fw_xfer_alloc(M_SBP); 1053103285Sikob if(xfer == NULL){ 1054103285Sikob return NULL; 1055103285Sikob } 1056103285Sikob if (tcode == FWTCODE_WREQQ) 1057103285Sikob xfer->send.len = 16; 1058103285Sikob else 1059103285Sikob xfer->send.len = 24; 1060103285Sikob 1061110195Ssimokawa xfer->send.buf = malloc(xfer->send.len, M_FW, M_NOWAIT); 1062103285Sikob if(xfer->send.buf == NULL){ 1063110195Ssimokawa fw_xfer_free(xfer); 1064103285Sikob return NULL; 1065103285Sikob } 1066103285Sikob 1067103285Sikob xfer->send.off = 0; 1068103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1069103285Sikob xfer->sc = (caddr_t)sdev; 1070103285Sikob xfer->fc = sdev->target->sbp->fd.fc; 1071103285Sikob xfer->retry_req = fw_asybusy; 1072103285Sikob 1073103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1074103285Sikob fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi); 1075103285Sikob fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset); 1076103285Sikob fp->mode.wreqq.tlrt = 0; 1077103285Sikob fp->mode.wreqq.tcode = tcode; 1078103285Sikob fp->mode.wreqq.pri = 0; 1079103285Sikob xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst; 1080103285Sikob fp->mode.wreqq.dst = htons(xfer->dst); 1081103285Sikob 1082103285Sikob return xfer; 1083103285Sikob 1084103285Sikob} 1085103285Sikob 1086103285Sikobstatic void 1087110336Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, u_int16_t orb_hi, u_int32_t orb_lo) 1088103285Sikob{ 1089103285Sikob struct fw_xfer *xfer; 1090103285Sikob struct fw_pkt *fp; 1091103285Sikob struct sbp_ocb *ocb; 1092103285Sikob int s, nid; 1093103285Sikob 1094103285Sikob if ((ocb = sbp_get_ocb(sdev->target->sbp)) == NULL) { 1095103285Sikob s = splfw(); 1096103285Sikob sdev->target->sbp->flags |= SBP_RESOURCE_SHORTAGE; 1097103285Sikob splx(s); 1098103285Sikob return; 1099103285Sikob } 1100103285Sikob ocb->flags = OCB_ACT_MGM; 1101103285Sikob ocb->sdev = sdev; 1102103285Sikob ocb->ccb = NULL; 1103103285Sikob 1104103285Sikob nid = sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS; 1105103285Sikob bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb)); 1106103285Sikob ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1107103285Sikob ocb->orb[7] = htonl(SBP_DEV2ADDR( 1108103285Sikob device_get_unit(sdev->target->sbp->fd.dev), 1109103285Sikob sdev->target->target_id, 1110103285Sikob sdev->lun_id)); 1111103285Sikob 1112107653SsimokawaSBP_DEBUG(0) 1113103285Sikob sbp_show_sdev_info(sdev, 2); 1114103285Sikob printf("%s\n", orb_fun_name[(func>>16)&0xf]); 1115107653SsimokawaEND_DEBUG 1116103285Sikob switch (func) { 1117103285Sikob case ORB_FUN_LGI: 1118103285Sikob ocb->orb[2] = htonl(nid << 16); 1119103285Sikob ocb->orb[3] = htonl(vtophys(&sdev->login)); 1120103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id); 1121103285Sikob ocb->orb[5] = htonl(sizeof(struct sbp_login_res)); 1122103285Sikob break; 1123110336Ssimokawa case ORB_FUN_ATA: 1124110336Ssimokawa ocb->orb[0] = htonl((0 << 16) | orb_hi); 1125110336Ssimokawa ocb->orb[1] = htonl(orb_lo); 1126110336Ssimokawa /* fall through */ 1127103285Sikob case ORB_FUN_RCN: 1128103285Sikob case ORB_FUN_LGO: 1129103285Sikob case ORB_FUN_LUR: 1130103285Sikob case ORB_FUN_RST: 1131103285Sikob case ORB_FUN_ATS: 1132103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id); 1133103285Sikob break; 1134103285Sikob } 1135103285Sikob 1136103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1137103285Sikob if(xfer == NULL){ 1138103285Sikob return; 1139103285Sikob } 1140103285Sikob xfer->act.hand = sbp_login_callback; 1141103285Sikob 1142103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1143103285Sikob fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi); 1144103285Sikob fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo); 1145103285Sikob fp->mode.wreqb.len = htons(8); 1146103285Sikob fp->mode.wreqb.extcode = 0; 1147108503Ssimokawa fp->mode.wreqb.payload[0] = htonl(nid << 16); 1148103285Sikob fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1149103285Sikob sbp_enqueue_ocb(sdev, ocb); 1150103285Sikob 1151103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1152103285Sikob} 1153103285Sikob 1154103285Sikobstatic void 1155105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb) 1156103285Sikob{ 1157103285Sikob struct ccb_scsiio *csio; 1158103285Sikob 1159103285Sikob csio = &ocb->ccb->csio; 1160103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1161103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1162103285Sikob ", flags: 0x%02x, " 1163103285Sikob "%db cmd/%db data/%db sense\n", 1164103285Sikob device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1165103285Sikob ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1166103285Sikob csio->cdb_io.cdb_bytes[0], 1167103285Sikob csio->cdb_io.cdb_bytes[1], 1168103285Sikob csio->cdb_io.cdb_bytes[2], 1169103285Sikob csio->cdb_io.cdb_bytes[3], 1170103285Sikob csio->cdb_io.cdb_bytes[4], 1171103285Sikob csio->cdb_io.cdb_bytes[5], 1172103285Sikob csio->cdb_io.cdb_bytes[6], 1173103285Sikob csio->cdb_io.cdb_bytes[7], 1174103285Sikob csio->cdb_io.cdb_bytes[8], 1175103285Sikob csio->cdb_io.cdb_bytes[9], 1176103285Sikob ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1177103285Sikob csio->cdb_len, csio->dxfer_len, 1178103285Sikob csio->sense_len); 1179105792Ssimokawa} 1180105792Ssimokawa 1181105792Ssimokawastatic void 1182105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1183105792Ssimokawa{ 1184105792Ssimokawa struct sbp_cmd_status *sbp_cmd_status; 1185105792Ssimokawa struct scsi_sense_data *sense; 1186105792Ssimokawa 1187105792Ssimokawa sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1188105792Ssimokawa sense = &ocb->ccb->csio.sense_data; 1189105792Ssimokawa 1190105792SsimokawaSBP_DEBUG(0) 1191105792Ssimokawa sbp_print_scsi_cmd(ocb); 1192103285Sikob /* XXX need decode status */ 1193103285Sikob sbp_show_sdev_info(ocb->sdev, 2); 1194103285Sikob printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d", 1195103285Sikob sbp_cmd_status->status, 1196103285Sikob sbp_cmd_status->sfmt, 1197103285Sikob sbp_cmd_status->valid, 1198103285Sikob sbp_cmd_status->s_key, 1199103285Sikob sbp_cmd_status->s_code, 1200103285Sikob sbp_cmd_status->s_qlfr, 1201103285Sikob sbp_status->len 1202103285Sikob ); 1203103285Sikob#if 0 /* XXX */ 1204103285Sikob if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) { 1205103285Sikob printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]); 1206103285Sikob scsi_sense_desc( 1207103285Sikob sbp_cmd_status->s_code, 1208103285Sikob sbp_cmd_status->s_qlfr, 1209103285Sikob ocb->ccb->ccb_h.path->device->inq_data 1210103285Sikob ) 1211103285Sikob } else { 1212103285Sikob printf("\n"); 1213103285Sikob } 1214103285Sikob#else 1215103285Sikob printf("\n"); 1216103285Sikob#endif 1217103285SikobEND_DEBUG 1218103285Sikob 1219110071Ssimokawa switch (sbp_cmd_status->status) { 1220110071Ssimokawa case SCSI_STATUS_CHECK_COND: 1221110071Ssimokawa case SCSI_STATUS_BUSY: 1222110071Ssimokawa case SCSI_STATUS_CMD_TERMINATED: 1223103285Sikob if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1224103285Sikob sense->error_code = SSD_CURRENT_ERROR; 1225103285Sikob }else{ 1226103285Sikob sense->error_code = SSD_DEFERRED_ERROR; 1227103285Sikob } 1228103285Sikob if(sbp_cmd_status->valid) 1229103285Sikob sense->error_code |= SSD_ERRCODE_VALID; 1230103285Sikob sense->flags = sbp_cmd_status->s_key; 1231103285Sikob if(sbp_cmd_status->mark) 1232103285Sikob sense->flags |= SSD_FILEMARK; 1233103285Sikob if(sbp_cmd_status->eom) 1234103285Sikob sense->flags |= SSD_EOM; 1235103285Sikob if(sbp_cmd_status->ill_len) 1236103285Sikob sense->flags |= SSD_ILI; 1237103285Sikob sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff; 1238103285Sikob sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff; 1239103285Sikob sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff; 1240103285Sikob sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff; 1241103285Sikob if (sbp_status->len <= 1) 1242103285Sikob /* XXX not scsi status. shouldn't be happened */ 1243103285Sikob sense->extra_len = 0; 1244103285Sikob else if (sbp_status->len <= 4) 1245103285Sikob /* add_sense_code(_qual), info, cmd_spec_info */ 1246103285Sikob sense->extra_len = 6; 1247103285Sikob else 1248103285Sikob /* fru, sense_key_spec */ 1249103285Sikob sense->extra_len = 10; 1250103285Sikob sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff; 1251103285Sikob sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff; 1252103285Sikob sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff; 1253103285Sikob sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff; 1254103285Sikob sense->add_sense_code = sbp_cmd_status->s_code; 1255103285Sikob sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1256103285Sikob sense->fru = sbp_cmd_status->fru; 1257103285Sikob sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff; 1258103285Sikob sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff; 1259103285Sikob sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff; 1260103285Sikob 1261103285Sikob ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1262103285Sikob ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1263103285Sikob | CAM_AUTOSNS_VALID; 1264103285Sikob/* 1265103285Sikob{ 1266103285Sikob u_int8_t j, *tmp; 1267103285Sikob tmp = sense; 1268103285Sikob for( j = 0 ; j < 32 ; j+=8){ 1269103285Sikob printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1270103285Sikob tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1271103285Sikob tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1272103285Sikob } 1273103285Sikob 1274103285Sikob} 1275103285Sikob*/ 1276110071Ssimokawa break; 1277110071Ssimokawa default: 1278110071Ssimokawa sbp_show_sdev_info(ocb->sdev, 2); 1279110071Ssimokawa printf("sbp_scsi_status: unknown scsi status 0x%x\n", 1280110071Ssimokawa sbp_cmd_status->status); 1281103285Sikob } 1282103285Sikob} 1283103285Sikob 1284103285Sikobstatic void 1285103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb) 1286103285Sikob{ 1287103285Sikob union ccb *ccb; 1288103285Sikob struct sbp_dev *sdev; 1289103285Sikob struct scsi_inquiry_data *inq; 1290103285Sikob 1291103285Sikob ccb = ocb->ccb; 1292103285Sikob sdev = ocb->sdev; 1293103285Sikob 1294103285Sikob if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1295103285Sikob return; 1296103285SikobSBP_DEBUG(1) 1297103285Sikob sbp_show_sdev_info(sdev, 2); 1298103285Sikob printf("sbp_fix_inq_data\n"); 1299103285SikobEND_DEBUG 1300103285Sikob inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1301103285Sikob switch (SID_TYPE(inq)) { 1302103285Sikob case T_DIRECT: 1303103285Sikob /* 1304103285Sikob * XXX Convert Direct Access device to RBC. 1305108281Ssimokawa * I've never seen FireWire DA devices which support READ_6. 1306103285Sikob */ 1307103285Sikob#if 1 1308103285Sikob if (SID_TYPE(inq) == T_DIRECT) 1309103285Sikob inq->device |= T_RBC; /* T_DIRECT == 0 */ 1310103285Sikob#endif 1311103285Sikob /* fall through */ 1312103285Sikob case T_RBC: 1313103285Sikob /* disable tag queuing */ 1314103285Sikob inq->flags &= ~SID_CmdQue; 1315103285Sikob /* 1316103285Sikob * Override vendor/product/revision information. 1317103285Sikob * Some devices sometimes return strange strings. 1318103285Sikob */ 1319103285Sikob bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1320103285Sikob bcopy(sdev->product, inq->product, sizeof(inq->product)); 1321103285Sikob bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1322103285Sikob break; 1323103285Sikob } 1324103285Sikob} 1325103285Sikob 1326103285Sikobstatic void 1327103285Sikobsbp_recv1(struct fw_xfer *xfer){ 1328103285Sikob struct fw_pkt *rfp; 1329103285Sikob#if NEED_RESPONSE 1330103285Sikob struct fw_pkt *sfp; 1331103285Sikob#endif 1332103285Sikob struct sbp_softc *sbp; 1333103285Sikob struct sbp_dev *sdev; 1334103285Sikob struct sbp_ocb *ocb; 1335103285Sikob struct sbp_login_res *login_res = NULL; 1336103285Sikob struct sbp_status *sbp_status; 1337103285Sikob struct sbp_target *target; 1338110189Ssimokawa int orb_fun, status_valid, t, l; 1339103285Sikob u_int32_t addr; 1340103285Sikob/* 1341103285Sikob u_int32_t *ld; 1342103285Sikob ld = xfer->recv.buf; 1343103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n", 1344103285Sikob xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1345103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1346103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1347103285Sikob*/ 1348103285Sikob if(xfer->resp != 0){ 1349103285Sikob printf("sbp_recv: xfer->resp != 0\n"); 1350103285Sikob fw_xfer_free( xfer); 1351103285Sikob return; 1352103285Sikob } 1353103285Sikob if(xfer->recv.buf == NULL){ 1354103285Sikob printf("sbp_recv: xfer->recv.buf == NULL\n"); 1355103285Sikob fw_xfer_free( xfer); 1356103285Sikob return; 1357103285Sikob } 1358103285Sikob sbp = (struct sbp_softc *)xfer->sc; 1359103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1360103285Sikob if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1361103285Sikob printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1362103285Sikob fw_xfer_free( xfer); 1363103285Sikob return; 1364103285Sikob } 1365103285Sikob sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload; 1366103285Sikob addr = ntohl(rfp->mode.wreqb.dest_lo); 1367103285SikobSBP_DEBUG(2) 1368103285Sikob printf("received address 0x%x\n", addr); 1369103285SikobEND_DEBUG 1370110189Ssimokawa t = SBP_ADDR2TRG(addr); 1371110189Ssimokawa if (t >= SBP_NUM_TARGETS) { 1372110189Ssimokawa device_printf(sbp->fd.dev, 1373110189Ssimokawa "sbp_recv1: invalid target %d\n", t); 1374110189Ssimokawa fw_xfer_free(xfer); 1375110189Ssimokawa return; 1376110189Ssimokawa } 1377110189Ssimokawa target = &sbp->targets[t]; 1378110189Ssimokawa l = SBP_ADDR2LUN(addr); 1379110189Ssimokawa if (l >= target->num_lun) { 1380110189Ssimokawa device_printf(sbp->fd.dev, 1381110189Ssimokawa "sbp_recv1: invalid lun %d (target=%d)\n", l, t); 1382110189Ssimokawa fw_xfer_free(xfer); 1383110189Ssimokawa return; 1384110189Ssimokawa } 1385110189Ssimokawa sdev = &target->luns[l]; 1386103285Sikob 1387110189Ssimokawa ocb = NULL; 1388110189Ssimokawa switch (sbp_status->src) { 1389110189Ssimokawa case 0: 1390110189Ssimokawa case 1: 1391110189Ssimokawa ocb = sbp_dequeue_ocb(sdev, ntohl(sbp_status->orb_lo)); 1392110189Ssimokawa if (ocb == NULL) { 1393110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1394110189Ssimokawa printf("No ocb on the queue\n"); 1395110189Ssimokawa } 1396110189Ssimokawa break; 1397110189Ssimokawa case 2: 1398110189Ssimokawa /* unsolicit */ 1399110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1400110189Ssimokawa printf("unsolicit status received\n"); 1401110189Ssimokawa break; 1402110189Ssimokawa default: 1403110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1404110189Ssimokawa printf("unknown sbp_status->src\n"); 1405110189Ssimokawa } 1406110189Ssimokawa 1407110189Ssimokawa status_valid = (sbp_status->src < 2 1408110189Ssimokawa && sbp_status->resp == ORB_RES_CMPL 1409103285Sikob && sbp_status->dead == 0 1410103285Sikob && sbp_status->status == 0); 1411103285Sikob 1412103285Sikob if (!status_valid || debug > 1){ 1413103285Sikob int status; 1414110129SsimokawaSBP_DEBUG(0) 1415103285Sikob sbp_show_sdev_info(sdev, 2); 1416103285Sikob printf("ORB status src:%x resp:%x dead:%x" 1417108712Ssimokawa#if __FreeBSD_version >= 500000 1418103285Sikob " len:%x stat:%x orb:%x%08x\n", 1419108712Ssimokawa#else 1420108712Ssimokawa " len:%x stat:%x orb:%x%08lx\n", 1421108712Ssimokawa#endif 1422103285Sikob sbp_status->src, sbp_status->resp, sbp_status->dead, 1423103285Sikob sbp_status->len, sbp_status->status, 1424108280Ssimokawa ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1425110129SsimokawaEND_DEBUG 1426103285Sikob sbp_show_sdev_info(sdev, 2); 1427103285Sikob status = sbp_status->status; 1428103285Sikob switch(sbp_status->resp) { 1429103285Sikob case 0: 1430103285Sikob if (status > MAX_ORB_STATUS0) 1431103285Sikob printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1432110189Ssimokawa else 1433110129Ssimokawa printf("%s\n", orb_status0[status]); 1434103285Sikob break; 1435103285Sikob case 1: 1436110336Ssimokawa printf("Obj: %s, Error: %s\n", 1437103285Sikob orb_status1_object[(status>>6) & 3], 1438103285Sikob orb_status1_serial_bus_error[status & 0xf]); 1439103285Sikob break; 1440110129Ssimokawa case 2: 1441110129Ssimokawa printf("Illegal request\n"); 1442110129Ssimokawa break; 1443110129Ssimokawa case 3: 1444110129Ssimokawa printf("Vendor dependent\n"); 1445110129Ssimokawa break; 1446103285Sikob default: 1447110129Ssimokawa printf("unknown respose code %d\n", sbp_status->resp); 1448103285Sikob } 1449103285Sikob } 1450103285Sikob 1451103285Sikob /* we have to reset the fetch agent if it's dead */ 1452103285Sikob if (sbp_status->dead) { 1453103285Sikob if (sdev->path) 1454103285Sikob xpt_freeze_devq(sdev->path, 1); 1455110336Ssimokawa sbp_agent_reset(sdev); 1456103285Sikob } 1457103285Sikob 1458103285Sikob if (ocb == NULL) { 1459110189Ssimokawa fw_xfer_free(xfer); 1460103285Sikob return; 1461103285Sikob } 1462103285Sikob 1463110336Ssimokawa sdev->flags &= ~SBP_DEV_TIMEOUT; 1464110336Ssimokawa 1465103285Sikob switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1466103285Sikob case ORB_FMT_NOP: 1467103285Sikob break; 1468103285Sikob case ORB_FMT_VED: 1469103285Sikob break; 1470103285Sikob case ORB_FMT_STD: 1471103285Sikob switch(ocb->flags & OCB_ACT_MASK){ 1472103285Sikob case OCB_ACT_MGM: 1473103285Sikob orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1474103285Sikob switch(orb_fun) { 1475103285Sikob case ORB_FUN_LGI: 1476103285Sikob login_res = &sdev->login; 1477103285Sikob login_res->len = ntohs(login_res->len); 1478103285Sikob login_res->id = ntohs(login_res->id); 1479103285Sikob login_res->cmd_hi = ntohs(login_res->cmd_hi); 1480103285Sikob login_res->cmd_lo = ntohl(login_res->cmd_lo); 1481103285Sikob if (status_valid) { 1482103285SikobSBP_DEBUG(0) 1483103285Sikobsbp_show_sdev_info(sdev, 2); 1484103285Sikobprintf("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)); 1485103285SikobEND_DEBUG 1486103285Sikob#if 1 1487103285Sikob sbp_busy_timeout(sdev); 1488103285Sikob#else 1489110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_ATS, 0, 0); 1490103285Sikob#endif 1491103285Sikob } else { 1492110336Ssimokawa /* forgot logout? */ 1493110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1494103285Sikob printf("login failed\n"); 1495103285Sikob sdev->status = SBP_DEV_RESET; 1496103285Sikob } 1497103285Sikob break; 1498103285Sikob case ORB_FUN_RCN: 1499103285Sikob login_res = &sdev->login; 1500103285Sikob if (status_valid) { 1501103285SikobSBP_DEBUG(0) 1502103285Sikobsbp_show_sdev_info(sdev, 2); 1503103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1504103285SikobEND_DEBUG 1505103285Sikob#if 1 1506103285Sikob sbp_ping_unit(sdev); 1507110336Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1508103285Sikob xpt_release_devq(sdev->path, 1, TRUE); 1509103285Sikob#else 1510110336Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1511110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_ATS, 0, 0); 1512103285Sikob#endif 1513103285Sikob } else { 1514103285Sikob /* reconnection hold time exceed? */ 1515110336SsimokawaSBP_DEBUG(0) 1516110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1517103285Sikob printf("reconnect failed\n"); 1518110336SsimokawaEND_DEBUG 1519110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGI, 0, 0); 1520103285Sikob } 1521103285Sikob break; 1522103285Sikob case ORB_FUN_LGO: 1523103285Sikob sdev->status = SBP_DEV_RESET; 1524103285Sikob break; 1525110336Ssimokawa case ORB_FUN_RST: 1526110336Ssimokawa sbp_busy_timeout(sdev); 1527110336Ssimokawa break; 1528103285Sikob case ORB_FUN_LUR: 1529103285Sikob case ORB_FUN_ATA: 1530103285Sikob case ORB_FUN_ATS: 1531110336Ssimokawa sbp_agent_reset(sdev); 1532103285Sikob break; 1533103285Sikob default: 1534110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1535110336Ssimokawa printf("unknown function %d\n", orb_fun); 1536103285Sikob break; 1537103285Sikob } 1538103285Sikob break; 1539103285Sikob case OCB_ACT_CMD: 1540103285Sikob if(ocb->ccb != NULL){ 1541103285Sikob union ccb *ccb; 1542103285Sikob/* 1543103285Sikob u_int32_t *ld; 1544103285Sikob ld = ocb->ccb->csio.data_ptr; 1545103285Sikob if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1546103285Sikob printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1547103285Sikob else 1548103285Sikob printf("ptr NULL\n"); 1549103285Sikobprintf("len %d\n", sbp_status->len); 1550103285Sikob*/ 1551103285Sikob ccb = ocb->ccb; 1552103285Sikob if(sbp_status->len > 1){ 1553103285Sikob sbp_scsi_status(sbp_status, ocb); 1554103285Sikob }else{ 1555103285Sikob if(sbp_status->resp != ORB_RES_CMPL){ 1556103285Sikob ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1557103285Sikob }else{ 1558103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1559103285Sikob } 1560103285Sikob } 1561103285Sikob /* fix up inq data */ 1562103285Sikob if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1563103285Sikob sbp_fix_inq_data(ocb); 1564103285Sikob xpt_done(ccb); 1565103285Sikob } 1566103285Sikob break; 1567103285Sikob default: 1568103285Sikob break; 1569103285Sikob } 1570103285Sikob } 1571103285Sikob 1572103285Sikob if (!(ocb->flags & OCB_RESERVED)) 1573103285Sikob sbp_free_ocb(sbp, ocb); 1574103285Sikob 1575103285Sikob/* The received packet is usually small enough to be stored within 1576103285Sikob * the buffer. In that case, the controller return ack_complete and 1577103285Sikob * no respose is necessary. 1578103285Sikob * 1579103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as 1580103285Sikob * ack_complete or ack_pending to upper driver. 1581103285Sikob */ 1582103285Sikob#if NEED_RESPONSE 1583103285Sikob xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO); 1584103285Sikob xfer->send.len = 12; 1585103285Sikob xfer->send.off = 0; 1586103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1587103285Sikob sfp->mode.wres.dst = rfp->mode.wreqb.src; 1588103285Sikob xfer->dst = ntohs(sfp->mode.wres.dst); 1589103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1590103285Sikob xfer->act.hand = sbp_loginres_callback; 1591103285Sikob xfer->retry_req = fw_asybusy; 1592103285Sikob 1593103285Sikob sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1594103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1595103285Sikob sfp->mode.wres.rtcode = 0; 1596103285Sikob sfp->mode.wres.pri = 0; 1597103285Sikob 1598103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1599103285Sikob#else 1600103285Sikob fw_xfer_free(xfer); 1601103285Sikob#endif 1602103285Sikob 1603103285Sikob return; 1604103285Sikob 1605103285Sikob} 1606103285Sikob 1607103285Sikobstatic void 1608103285Sikobsbp_recv(struct fw_xfer *xfer) 1609103285Sikob{ 1610103285Sikob int s; 1611103285Sikob 1612103285Sikob s = splcam(); 1613103285Sikob sbp_recv1(xfer); 1614103285Sikob splx(s); 1615103285Sikob} 1616103285Sikob/* 1617103285Sikob * sbp_attach() 1618103285Sikob */ 1619103285Sikobstatic int 1620103285Sikobsbp_attach(device_t dev) 1621103285Sikob{ 1622103285Sikob struct sbp_softc *sbp; 1623103285Sikob struct cam_devq *devq; 1624103285Sikob struct fw_xfer *xfer; 1625103285Sikob int i, s, error; 1626103285Sikob 1627103285SikobSBP_DEBUG(0) 1628103285Sikob printf("sbp_attach\n"); 1629103285SikobEND_DEBUG 1630103285Sikob 1631103285Sikob sbp = ((struct sbp_softc *)device_get_softc(dev)); 1632103285Sikob bzero(sbp, sizeof(struct sbp_softc)); 1633103285Sikob sbp->fd.dev = dev; 1634103285Sikob sbp->fd.fc = device_get_ivars(dev); 1635103285Sikob error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 1636103285Sikob /*boundary*/0, 1637103285Sikob /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1638103285Sikob /*highaddr*/BUS_SPACE_MAXADDR, 1639103285Sikob /*filter*/NULL, /*filterarg*/NULL, 1640103285Sikob /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1641103285Sikob /*maxsegsz*/0x8000, 1642103285Sikob /*flags*/BUS_DMA_ALLOCNOW, 1643103285Sikob &sbp->dmat); 1644103285Sikob if (error != 0) { 1645103285Sikob printf("sbp_attach: Could not allocate DMA tag " 1646103285Sikob "- error %d\n", error); 1647103285Sikob return (ENOMEM); 1648103285Sikob } 1649103285Sikob 1650103285Sikob devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1651103285Sikob if (devq == NULL) 1652103285Sikob return (ENXIO); 1653103285Sikob 1654103285Sikob for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1655103285Sikob sbp->targets[i].fwdev = NULL; 1656103285Sikob sbp->targets[i].luns = NULL; 1657103285Sikob } 1658103285Sikob 1659103285Sikob sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1660103285Sikob device_get_unit(dev), 1661103285Sikob /*untagged*/ SBP_QUEUE_LEN, 1662103285Sikob /*tagged*/0, devq); 1663103285Sikob 1664103285Sikob if (sbp->sim == NULL) { 1665103285Sikob cam_simq_free(devq); 1666103285Sikob return (ENXIO); 1667103285Sikob } 1668103285Sikob 1669103285Sikob sbp->ocb = (struct sbp_ocb *) contigmalloc( 1670103285Sikob sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1671109424Ssimokawa M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 1672103285Sikob bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB); 1673103285Sikob 1674103285Sikob if (sbp->ocb == NULL) { 1675103285Sikob printf("sbp0: ocb alloction failure\n"); 1676103285Sikob return (ENOMEM); 1677103285Sikob } 1678103285Sikob 1679103285Sikob STAILQ_INIT(&sbp->free_ocbs); 1680103285Sikob for (i = 0; i < SBP_NUM_OCB; i++) { 1681103285Sikob sbp_free_ocb(sbp, &sbp->ocb[i]); 1682103285Sikob } 1683103285Sikob 1684103285Sikob if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) { 1685103285Sikob cam_sim_free(sbp->sim, /*free_devq*/TRUE); 1686103285Sikob contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1687103285Sikob M_SBP); 1688103285Sikob return (ENXIO); 1689103285Sikob } 1690103285Sikob 1691110269Ssimokawa xfer = fw_xfer_alloc(M_SBP); 1692103285Sikob xfer->act.hand = sbp_recv; 1693103285Sikob xfer->act_type = FWACT_XFER; 1694103285Sikob#if NEED_RESPONSE 1695103285Sikob xfer->fc = sbp->fd.fc; 1696103285Sikob#endif 1697103285Sikob xfer->sc = (caddr_t)sbp; 1698103285Sikob 1699103285Sikob sbp->fwb.start_hi = SBP_BIND_HI; 1700103285Sikob sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0); 1701103285Sikob /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1702103285Sikob sbp->fwb.addrlen = 0xffff; 1703103285Sikob sbp->fwb.xfer = xfer; 1704103285Sikob fw_bindadd(sbp->fd.fc, &sbp->fwb); 1705103285Sikob 1706103285Sikob sbp->fd.post_explore = sbp_post_explore; 1707103285Sikob s = splfw(); 1708103285Sikob sbp_post_explore((void *)sbp); 1709103285Sikob splx(s); 1710103285Sikob 1711103285Sikob return (0); 1712103285Sikob} 1713103285Sikob 1714103285Sikobstatic int 1715110145Ssimokawasbp_logout_all(struct sbp_softc *sbp) 1716110145Ssimokawa{ 1717110145Ssimokawa struct sbp_target *target; 1718110145Ssimokawa struct sbp_dev *sdev; 1719110145Ssimokawa int i, j; 1720110145Ssimokawa 1721110145SsimokawaSBP_DEBUG(0) 1722110145Ssimokawa printf("sbp_logout_all\n"); 1723110145SsimokawaEND_DEBUG 1724110145Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) { 1725110145Ssimokawa target = &sbp->targets[i]; 1726110145Ssimokawa if (target->luns == NULL) 1727110145Ssimokawa continue; 1728110145Ssimokawa for (j = 0; j < target->num_lun; j++) { 1729110145Ssimokawa sdev = &target->luns[j]; 1730110336Ssimokawa if (sdev->status >= SBP_DEV_TOATTACH && 1731110336Ssimokawa sdev->status <= SBP_DEV_ATTACHED) 1732110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGO, 0, 0); 1733110145Ssimokawa } 1734110145Ssimokawa } 1735110145Ssimokawa return 0; 1736110145Ssimokawa} 1737110145Ssimokawa 1738110145Ssimokawastatic int 1739110145Ssimokawasbp_shutdown(device_t dev) 1740110145Ssimokawa{ 1741110145Ssimokawa struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1742110145Ssimokawa 1743110145Ssimokawa sbp_logout_all(sbp); 1744110145Ssimokawa return (0); 1745110145Ssimokawa} 1746110145Ssimokawa 1747110145Ssimokawastatic int 1748103285Sikobsbp_detach(device_t dev) 1749103285Sikob{ 1750103285Sikob struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1751103285Sikob struct firewire_comm *fc = sbp->fd.fc; 1752103285Sikob int i; 1753103285Sikob 1754103285SikobSBP_DEBUG(0) 1755103285Sikob printf("sbp_detach\n"); 1756103285SikobEND_DEBUG 1757110145Ssimokawa#if 0 1758103285Sikob /* bus reset for logout */ 1759103285Sikob sbp->fd.post_explore = NULL; 1760103285Sikob fc->ibr(fc); 1761110145Ssimokawa#endif 1762103285Sikob 1763103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i ++) 1764110145Ssimokawa sbp_cam_detach_target(&sbp->targets[i]); 1765103285Sikob xpt_bus_deregister(cam_sim_path(sbp->sim)); 1766110145Ssimokawa 1767110145Ssimokawa sbp_logout_all(sbp); 1768110145Ssimokawa /* XXX wait for logout completion */ 1769110145Ssimokawa tsleep(&i, FWPRI, "sbpdtc", hz/2); 1770110145Ssimokawa 1771110145Ssimokawa fw_bindremove(fc, &sbp->fwb); 1772110145Ssimokawa contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1773103285Sikob bus_dma_tag_destroy(sbp->dmat); 1774110145Ssimokawa 1775110145Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 1776110145Ssimokawa if (sbp->targets[i].luns != NULL) 1777110145Ssimokawa free(sbp->targets[i].luns, M_SBP); 1778110145Ssimokawa 1779103285Sikob return (0); 1780103285Sikob} 1781103285Sikob 1782103285Sikobstatic void 1783110145Ssimokawasbp_cam_detach_target(struct sbp_target *target) 1784103285Sikob{ 1785103285Sikob int i; 1786103285Sikob struct sbp_dev *sdev; 1787103285Sikob 1788103285Sikob if (target->luns != NULL) { 1789108529SsimokawaSBP_DEBUG(0) 1790103285Sikob printf("sbp_detach_target %d\n", target->target_id); 1791108529SsimokawaEND_DEBUG 1792110193Ssimokawa for (i = 0; i < target->num_lun; i++) { 1793103285Sikob sdev = &target->luns[i]; 1794103285Sikob if (sdev->status == SBP_DEV_RESET || 1795103285Sikob sdev->status == SBP_DEV_DEAD) 1796103285Sikob continue; 1797110336Ssimokawa if (sdev->path) { 1798103285Sikob xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 1799110336Ssimokawa xpt_free_path(sdev->path); 1800110336Ssimokawa sdev->path = NULL; 1801110336Ssimokawa } 1802103285Sikob sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 1803103285Sikob } 1804103285Sikob } 1805103285Sikob} 1806103285Sikob 1807103285Sikobstatic void 1808103285Sikobsbp_timeout(void *arg) 1809103285Sikob{ 1810103285Sikob struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 1811103285Sikob struct sbp_dev *sdev = ocb->sdev; 1812103285Sikob 1813103285Sikob sbp_show_sdev_info(sdev, 2); 1814110336Ssimokawa printf("request timeout ... "); 1815103285Sikob 1816110336Ssimokawa xpt_freeze_devq(sdev->path, 1); 1817110336Ssimokawa sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 1818110336Ssimokawa if (sdev->flags & SBP_DEV_TIMEOUT) { 1819110269Ssimokawa#if 0 1820110336Ssimokawa struct firewire_comm *fc; 1821110336Ssimokawa 1822110336Ssimokawa printf("bus reset\n"); 1823110336Ssimokawa fc = sdev->target->sbp->fd.fc; 1824110336Ssimokawa fc->ibr(fc); 1825110336Ssimokawa sdev->status == SBP_DEV_RETRY; 1826110269Ssimokawa#else 1827110336Ssimokawa printf("target reset\n"); 1828110336Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RST, 0, 0); 1829110269Ssimokawa#endif 1830110336Ssimokawa sdev->flags &= ~SBP_DEV_TIMEOUT; 1831110336Ssimokawa } else { 1832110336Ssimokawa printf("agent reset\n"); 1833110336Ssimokawa sdev->flags |= SBP_DEV_TIMEOUT; 1834110336Ssimokawa sbp_agent_reset(sdev); 1835110336Ssimokawa } 1836103285Sikob return; 1837103285Sikob} 1838103285Sikob 1839103285Sikobstatic void 1840103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb) 1841103285Sikob{ 1842103285Sikob 1843103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 1844103285Sikob struct sbp_target *target = NULL; 1845103285Sikob struct sbp_dev *sdev = NULL; 1846103285Sikob 1847103285Sikob /* target:lun -> sdev mapping */ 1848103285Sikob if (sbp != NULL 1849103285Sikob && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 1850103285Sikob && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 1851103285Sikob target = &sbp->targets[ccb->ccb_h.target_id]; 1852103285Sikob if (target->fwdev != NULL 1853103285Sikob && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 1854103285Sikob && ccb->ccb_h.target_lun < target->num_lun) { 1855103285Sikob sdev = &target->luns[ccb->ccb_h.target_lun]; 1856103285Sikob if (sdev->status != SBP_DEV_ATTACHED && 1857103285Sikob sdev->status != SBP_DEV_PROBE) 1858103285Sikob sdev = NULL; 1859103285Sikob } 1860103285Sikob } 1861103285Sikob 1862103285SikobSBP_DEBUG(1) 1863103285Sikob if (sdev == NULL) 1864103285Sikob printf("invalid target %d lun %d\n", 1865103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 1866103285SikobEND_DEBUG 1867103285Sikob 1868103285Sikob switch (ccb->ccb_h.func_code) { 1869103285Sikob case XPT_SCSI_IO: 1870103285Sikob case XPT_RESET_DEV: 1871103285Sikob case XPT_GET_TRAN_SETTINGS: 1872103285Sikob case XPT_SET_TRAN_SETTINGS: 1873103285Sikob case XPT_CALC_GEOMETRY: 1874103285Sikob if (sdev == NULL) { 1875103285SikobSBP_DEBUG(1) 1876103285Sikob printf("%s:%d:%d:func_code 0x%04x: " 1877103285Sikob "Invalid target (target needed)\n", 1878103285Sikob device_get_nameunit(sbp->fd.dev), 1879103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1880103285Sikob ccb->ccb_h.func_code); 1881103285SikobEND_DEBUG 1882103285Sikob 1883108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1884103285Sikob xpt_done(ccb); 1885103285Sikob return; 1886103285Sikob } 1887103285Sikob break; 1888103285Sikob case XPT_PATH_INQ: 1889103285Sikob case XPT_NOOP: 1890103285Sikob /* The opcodes sometimes aimed at a target (sc is valid), 1891103285Sikob * sometimes aimed at the SIM (sc is invalid and target is 1892103285Sikob * CAM_TARGET_WILDCARD) 1893103285Sikob */ 1894103285Sikob if (sbp == NULL && 1895103285Sikob ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 1896103285SikobSBP_DEBUG(0) 1897103285Sikob printf("%s:%d:%d func_code 0x%04x: " 1898103285Sikob "Invalid target (no wildcard)\n", 1899103285Sikob device_get_nameunit(sbp->fd.dev), 1900103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1901103285Sikob ccb->ccb_h.func_code); 1902103285SikobEND_DEBUG 1903108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1904103285Sikob xpt_done(ccb); 1905103285Sikob return; 1906103285Sikob } 1907103285Sikob break; 1908103285Sikob default: 1909103285Sikob /* XXX Hm, we should check the input parameters */ 1910103285Sikob break; 1911103285Sikob } 1912103285Sikob 1913103285Sikob switch (ccb->ccb_h.func_code) { 1914103285Sikob case XPT_SCSI_IO: 1915103285Sikob { 1916103285Sikob struct ccb_scsiio *csio; 1917103285Sikob struct sbp_ocb *ocb; 1918103285Sikob int s, speed; 1919106506Ssimokawa void *cdb; 1920103285Sikob 1921103285Sikob csio = &ccb->csio; 1922103285Sikob 1923103285SikobSBP_DEBUG(1) 1924103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1925103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1926103285Sikob ", flags: 0x%02x, " 1927103285Sikob "%db cmd/%db data/%db sense\n", 1928103285Sikob device_get_nameunit(sbp->fd.dev), 1929103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 1930103285Sikob csio->cdb_io.cdb_bytes[0], 1931103285Sikob csio->cdb_io.cdb_bytes[1], 1932103285Sikob csio->cdb_io.cdb_bytes[2], 1933103285Sikob csio->cdb_io.cdb_bytes[3], 1934103285Sikob csio->cdb_io.cdb_bytes[4], 1935103285Sikob csio->cdb_io.cdb_bytes[5], 1936103285Sikob csio->cdb_io.cdb_bytes[6], 1937103285Sikob csio->cdb_io.cdb_bytes[7], 1938103285Sikob csio->cdb_io.cdb_bytes[8], 1939103285Sikob csio->cdb_io.cdb_bytes[9], 1940103285Sikob ccb->ccb_h.flags & CAM_DIR_MASK, 1941103285Sikob csio->cdb_len, csio->dxfer_len, 1942103285Sikob csio->sense_len); 1943103285SikobEND_DEBUG 1944103285Sikob if(sdev == NULL){ 1945103285Sikob ccb->ccb_h.status = CAM_DEV_NOT_THERE; 1946103285Sikob xpt_done(ccb); 1947103285Sikob return; 1948103285Sikob } 1949103285Sikob#if 0 1950103285Sikob /* if we are in probe stage, pass only probe commands */ 1951103285Sikob if (sdev->status == SBP_DEV_PROBE) { 1952103285Sikob char *name; 1953103285Sikob name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 1954103285Sikob printf("probe stage, periph name: %s\n", name); 1955103285Sikob if (strcmp(name, "probe") != 0) { 1956103285Sikob ccb->ccb_h.status = CAM_REQUEUE_REQ; 1957103285Sikob xpt_done(ccb); 1958103285Sikob return; 1959103285Sikob } 1960103285Sikob } 1961103285Sikob#endif 1962103285Sikob if ((ocb = sbp_get_ocb(sbp)) == NULL) { 1963103285Sikob s = splfw(); 1964103285Sikob sbp->flags |= SBP_RESOURCE_SHORTAGE; 1965103285Sikob splx(s); 1966103285Sikob return; 1967103285Sikob } 1968103285Sikob ocb->flags = OCB_ACT_CMD; 1969103285Sikob ocb->sdev = sdev; 1970103285Sikob ocb->ccb = ccb; 1971103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 1972103285Sikob ocb->orb[0] = htonl(1 << 31); 1973103285Sikob ocb->orb[1] = 0; 1974103285Sikob ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 1975103285Sikob ocb->orb[3] = htonl(vtophys(ocb->ind_ptr)); 1976103285Sikob speed = min(target->fwdev->speed, max_speed); 1977103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 1978103285Sikob | ORB_CMD_MAXP(speed + 7)); 1979103285Sikob if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 1980103285Sikob ocb->orb[4] |= htonl(ORB_CMD_IN); 1981103285Sikob } 1982103285Sikob 1983103285Sikob if (csio->ccb_h.flags & CAM_SCATTER_VALID) 1984103285Sikob printf("sbp: CAM_SCATTER_VALID\n"); 1985103285Sikob if (csio->ccb_h.flags & CAM_DATA_PHYS) 1986103285Sikob printf("sbp: CAM_DATA_PHYS\n"); 1987103285Sikob 1988106506Ssimokawa if (csio->ccb_h.flags & CAM_CDB_POINTER) 1989106506Ssimokawa cdb = (void *)csio->cdb_io.cdb_ptr; 1990106506Ssimokawa else 1991106506Ssimokawa cdb = (void *)&csio->cdb_io.cdb_bytes; 1992106506Ssimokawa bcopy(cdb, 1993103285Sikob (void *)(uintptr_t)(volatile void *)&ocb->orb[5], 1994106506Ssimokawa csio->cdb_len); 1995103285Sikob/* 1996103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 1997103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 1998103285Sikob*/ 1999103285Sikob if (ccb->csio.dxfer_len > 0) { 2000103285Sikob int s; 2001103285Sikob 2002103285Sikob if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 2003103285Sikob printf("sbp_action1: cannot create dmamap\n"); 2004103285Sikob break; 2005103285Sikob } 2006103285Sikob 2007103285Sikob s = splsoftvm(); 2008103285Sikob bus_dmamap_load(/*dma tag*/sbp->dmat, 2009103285Sikob /*dma map*/ocb->dmamap, 2010103285Sikob ccb->csio.data_ptr, 2011103285Sikob ccb->csio.dxfer_len, 2012103285Sikob sbp_execute_ocb, 2013103285Sikob ocb, 2014103285Sikob /*flags*/0); 2015103285Sikob splx(s); 2016103285Sikob } else 2017103285Sikob sbp_execute_ocb(ocb, NULL, 0, 0); 2018103285Sikob break; 2019103285Sikob } 2020103285Sikob case XPT_CALC_GEOMETRY: 2021103285Sikob { 2022103285Sikob struct ccb_calc_geometry *ccg; 2023103285Sikob u_int32_t size_mb; 2024103285Sikob u_int32_t secs_per_cylinder; 2025103285Sikob int extended = 1; 2026103285Sikob ccg = &ccb->ccg; 2027103285Sikob 2028103285Sikob if (ccg->block_size == 0) { 2029103285Sikob printf("sbp_action1: block_size is 0.\n"); 2030103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2031103285Sikob xpt_done(ccb); 2032103285Sikob break; 2033103285Sikob } 2034103285SikobSBP_DEBUG(1) 2035103285Sikob printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2036103285Sikob "Volume size = %d\n", 2037103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim), 2038103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2039103285Sikob ccg->volume_size); 2040103285SikobEND_DEBUG 2041103285Sikob 2042103285Sikob size_mb = ccg->volume_size 2043103285Sikob / ((1024L * 1024L) / ccg->block_size); 2044103285Sikob 2045103285Sikob if (size_mb >= 1024 && extended) { 2046103285Sikob ccg->heads = 255; 2047103285Sikob ccg->secs_per_track = 63; 2048103285Sikob } else { 2049103285Sikob ccg->heads = 64; 2050103285Sikob ccg->secs_per_track = 32; 2051103285Sikob } 2052103285Sikob secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2053103285Sikob ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2054103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 2055103285Sikob xpt_done(ccb); 2056103285Sikob break; 2057103285Sikob } 2058103285Sikob case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2059103285Sikob { 2060103285Sikob 2061103285SikobSBP_DEBUG(1) 2062103285Sikob printf("%s:%d:XPT_RESET_BUS: \n", 2063103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 2064103285SikobEND_DEBUG 2065103285Sikob 2066103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2067103285Sikob xpt_done(ccb); 2068103285Sikob break; 2069103285Sikob } 2070103285Sikob case XPT_PATH_INQ: /* Path routing inquiry */ 2071103285Sikob { 2072103285Sikob struct ccb_pathinq *cpi = &ccb->cpi; 2073103285Sikob 2074103285SikobSBP_DEBUG(1) 2075103285Sikob printf("%s:%d:%d XPT_PATH_INQ:.\n", 2076103285Sikob device_get_nameunit(sbp->fd.dev), 2077103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2078103285SikobEND_DEBUG 2079103285Sikob cpi->version_num = 1; /* XXX??? */ 2080103285Sikob cpi->hba_inquiry = 0; 2081103285Sikob cpi->target_sprt = 0; 2082103285Sikob cpi->hba_misc = 0; 2083103285Sikob cpi->hba_eng_cnt = 0; 2084103285Sikob cpi->max_target = SBP_NUM_TARGETS - 1; 2085103285Sikob cpi->max_lun = SBP_NUM_LUNS - 1; 2086103285Sikob cpi->initiator_id = SBP_INITIATOR; 2087103285Sikob cpi->bus_id = sim->bus_id; 2088103285Sikob cpi->base_transfer_speed = 400 * 1000 / 8; 2089103285Sikob strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2090103285Sikob strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 2091103285Sikob strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 2092103285Sikob cpi->unit_number = sim->unit_number; 2093103285Sikob 2094103285Sikob cpi->ccb_h.status = CAM_REQ_CMP; 2095103285Sikob xpt_done(ccb); 2096103285Sikob break; 2097103285Sikob } 2098103285Sikob case XPT_GET_TRAN_SETTINGS: 2099103285Sikob { 2100103285Sikob struct ccb_trans_settings *cts = &ccb->cts; 2101103285SikobSBP_DEBUG(1) 2102103285Sikob printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 2103103285Sikob device_get_nameunit(sbp->fd.dev), 2104103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2105103285SikobEND_DEBUG 2106103285Sikob /* Disable disconnect and tagged queuing */ 2107103285Sikob cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID; 2108103285Sikob cts->flags = 0; 2109103285Sikob 2110103285Sikob cts->ccb_h.status = CAM_REQ_CMP; 2111103285Sikob xpt_done(ccb); 2112103285Sikob break; 2113103285Sikob } 2114103285Sikob case XPT_ABORT: 2115103285Sikob ccb->ccb_h.status = CAM_UA_ABORT; 2116103285Sikob xpt_done(ccb); 2117103285Sikob break; 2118103285Sikob default: 2119103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2120103285Sikob xpt_done(ccb); 2121103285Sikob break; 2122103285Sikob } 2123103285Sikob return; 2124103285Sikob} 2125103285Sikob 2126103285Sikobstatic void 2127103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb) 2128103285Sikob{ 2129103285Sikob int s; 2130103285Sikob 2131103285Sikob s = splfw(); 2132103285Sikob sbp_action1(sim, ccb); 2133103285Sikob splx(s); 2134103285Sikob} 2135103285Sikob 2136103285Sikobstatic void 2137103285Sikobsbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 2138103285Sikob{ 2139103285Sikob int i; 2140103285Sikob struct sbp_ocb *ocb; 2141103285Sikob struct sbp_ocb *prev; 2142103285Sikob union ccb *ccb; 2143105633Ssimokawa bus_dma_segment_t *s; 2144103285Sikob 2145103285Sikob if (error) 2146103285Sikob printf("sbp_execute_ocb: error=%d\n", error); 2147103285Sikob 2148103285Sikob ocb = (struct sbp_ocb *)arg; 2149103285Sikob if (seg == 1) { 2150103285Sikob /* direct pointer */ 2151103285Sikob ocb->orb[3] = htonl(segments[0].ds_addr); 2152103285Sikob ocb->orb[4] |= htonl(segments[0].ds_len); 2153103285Sikob } else if(seg > 1) { 2154103285Sikob /* page table */ 2155103285SikobSBP_DEBUG(1) 2156103285Sikob printf("sbp_execute_ocb: seg %d", seg); 2157103285Sikob for (i = 0; i < seg; i++) 2158108712Ssimokawa#if __FreeBSD_version >= 500000 2159106543Ssimokawa printf(", %tx:%zd", segments[i].ds_addr, 2160108712Ssimokawa#else 2161108877Ssimokawa printf(", %x:%d", segments[i].ds_addr, 2162108712Ssimokawa#endif 2163103285Sikob segments[i].ds_len); 2164103285Sikob printf("\n"); 2165103285SikobEND_DEBUG 2166103285Sikob for (i = 0; i < seg; i++) { 2167105633Ssimokawa s = &segments[i]; 2168108877SsimokawaSBP_DEBUG(0) 2169108877Ssimokawa /* XXX LSI Logic "< 16 byte" bug might be hit */ 2170105633Ssimokawa if (s->ds_len < 16) 2171105633Ssimokawa printf("sbp_execute_ocb: warning, " 2172108877Ssimokawa#if __FreeBSD_version >= 500000 2173106543Ssimokawa "segment length(%zd) is less than 16." 2174108877Ssimokawa#else 2175108877Ssimokawa "segment length(%d) is less than 16." 2176108877Ssimokawa#endif 2177105633Ssimokawa "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2178108877SsimokawaEND_DEBUG 2179105633Ssimokawa ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2180105633Ssimokawa ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2181103285Sikob } 2182103285Sikob ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2183103285Sikob } 2184103285Sikob 2185103285Sikob ccb = ocb->ccb; 2186103285Sikob prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2187103285Sikob if (prev) 2188103285Sikob sbp_doorbell(ocb->sdev); 2189103285Sikob else 2190103285Sikob sbp_orb_pointer(ocb->sdev, ocb); 2191103285Sikob} 2192103285Sikob 2193103285Sikobstatic void 2194103285Sikobsbp_poll(struct cam_sim *sim) 2195103285Sikob{ 2196103285Sikob /* should call fwohci_intr? */ 2197103285Sikob return; 2198103285Sikob} 2199103285Sikobstatic struct sbp_ocb * 2200103285Sikobsbp_dequeue_ocb(struct sbp_dev *sdev, u_int32_t orb_lo) 2201103285Sikob{ 2202103285Sikob struct sbp_ocb *ocb; 2203103285Sikob struct sbp_ocb *next; 2204103285Sikob int s = splfw(), order = 0; 2205103285Sikob int flags; 2206103285Sikob 2207103285Sikob for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2208103285Sikob next = STAILQ_NEXT(ocb, ocb); 2209103285Sikob flags = ocb->flags; 2210103285SikobSBP_DEBUG(1) 2211110336Ssimokawa sbp_show_sdev_info(sdev, 2); 2212108712Ssimokawa#if __FreeBSD_version >= 500000 2213106543Ssimokawa printf("orb: 0x%tx next: 0x%x, flags %x\n", 2214108712Ssimokawa#else 2215108712Ssimokawa printf("orb: 0x%x next: 0x%lx, flags %x\n", 2216108712Ssimokawa#endif 2217103285Sikob vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags); 2218103285SikobEND_DEBUG 2219103285Sikob if (vtophys(&ocb->orb[0]) == orb_lo) { 2220103285Sikob /* found */ 2221103285Sikob if (ocb->flags & OCB_RESERVED) 2222103285Sikob ocb->flags |= OCB_DONE; 2223103285Sikob else 2224103285Sikob STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2225103285Sikob if (ocb->ccb != NULL) 2226103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2227103285Sikob ocb->ccb->ccb_h.timeout_ch); 2228103285Sikob if (ocb->dmamap != NULL) { 2229103285Sikob bus_dmamap_destroy(sdev->target->sbp->dmat, 2230103285Sikob ocb->dmamap); 2231103285Sikob ocb->dmamap = NULL; 2232103285Sikob } 2233103285Sikob break; 2234103285Sikob } else { 2235103285Sikob if ((ocb->flags & OCB_RESERVED) && 2236103285Sikob (ocb->flags & OCB_DONE)) { 2237103285Sikob /* next orb must be fetched already */ 2238103285Sikob STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2239103285Sikob sbp_free_ocb(sdev->target->sbp, ocb); 2240103285Sikob } else 2241103285Sikob order ++; 2242103285Sikob } 2243103285Sikob } 2244103285Sikob splx(s); 2245103285SikobSBP_DEBUG(0) 2246103285Sikob if (ocb && order > 0) { 2247103285Sikob sbp_show_sdev_info(sdev, 2); 2248103285Sikob printf("unordered execution order:%d\n", order); 2249103285Sikob } 2250103285SikobEND_DEBUG 2251103285Sikob return (ocb); 2252103285Sikob} 2253103285Sikob 2254103285Sikobstatic struct sbp_ocb * 2255103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2256103285Sikob{ 2257103285Sikob int s = splfw(); 2258103285Sikob struct sbp_ocb *prev; 2259103285Sikob 2260103285SikobSBP_DEBUG(2) 2261103285Sikob sbp_show_sdev_info(sdev, 2); 2262108712Ssimokawa#if __FreeBSD_version >= 500000 2263106543Ssimokawa printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0])); 2264108712Ssimokawa#else 2265108712Ssimokawa printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0])); 2266108712Ssimokawa#endif 2267103285SikobEND_DEBUG 2268103285Sikob prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2269103285Sikob STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2270103285Sikob 2271103285Sikob if (ocb->ccb != NULL) 2272103285Sikob ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2273103285Sikob (ocb->ccb->ccb_h.timeout * hz) / 1000); 2274103285Sikob 2275110579Ssimokawa if (prev != NULL ) { 2276103285SikobSBP_DEBUG(1) 2277108712Ssimokawa#if __FreeBSD_version >= 500000 2278106543Ssimokawa printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]), 2279108712Ssimokawa#else 2280108712Ssimokawa printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]), 2281108712Ssimokawa#endif 2282103285Sikob vtophys(&ocb->orb[0])); 2283103285SikobEND_DEBUG 2284103285Sikob prev->flags |= OCB_RESERVED; 2285103285Sikob prev->orb[1] = htonl(vtophys(&ocb->orb[0])); 2286103285Sikob prev->orb[0] = 0; 2287103285Sikob } 2288103285Sikob splx(s); 2289103285Sikob 2290103285Sikob return prev; 2291103285Sikob} 2292103285Sikob 2293103285Sikobstatic struct sbp_ocb * 2294103285Sikobsbp_get_ocb(struct sbp_softc *sbp) 2295103285Sikob{ 2296103285Sikob struct sbp_ocb *ocb; 2297103285Sikob int s = splfw(); 2298103285Sikob ocb = STAILQ_FIRST(&sbp->free_ocbs); 2299103285Sikob if (ocb == NULL) { 2300103285Sikob printf("ocb shortage!!!\n"); 2301103285Sikob return NULL; 2302103285Sikob } 2303103285Sikob STAILQ_REMOVE(&sbp->free_ocbs, ocb, sbp_ocb, ocb); 2304103285Sikob splx(s); 2305103285Sikob ocb->ccb = NULL; 2306103285Sikob return (ocb); 2307103285Sikob} 2308103285Sikob 2309103285Sikobstatic void 2310103285Sikobsbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb) 2311103285Sikob{ 2312103285Sikob#if 0 /* XXX make sure that ocb has ccb */ 2313103285Sikob if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 && 2314103285Sikob (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 2315103285Sikob ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2316103285Sikob sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2317103285Sikob } 2318103285Sikob#else 2319103285Sikob if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0) 2320103285Sikob sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2321103285Sikob#endif 2322103285Sikob ocb->flags = 0; 2323103285Sikob ocb->ccb = NULL; 2324103285Sikob STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb); 2325103285Sikob} 2326103285Sikob 2327103285Sikobstatic void 2328103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status) 2329103285Sikob{ 2330103285Sikob struct sbp_dev *sdev; 2331103285Sikob 2332103285Sikob sdev = ocb->sdev; 2333110269SsimokawaSBP_DEBUG(1) 2334103285Sikob sbp_show_sdev_info(sdev, 2); 2335103285Sikob printf("sbp_abort_ocb 0x%x\n", status); 2336105792Ssimokawa if (ocb->ccb != NULL) 2337105792Ssimokawa sbp_print_scsi_cmd(ocb); 2338103285SikobEND_DEBUG 2339103285Sikob if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) { 2340110336Ssimokawa untimeout(sbp_timeout, (caddr_t)ocb, 2341110336Ssimokawa ocb->ccb->ccb_h.timeout_ch); 2342103285Sikob ocb->ccb->ccb_h.status = status; 2343103285Sikob xpt_done(ocb->ccb); 2344103285Sikob } 2345103285Sikob if (ocb->dmamap != NULL) { 2346103285Sikob bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap); 2347103285Sikob ocb->dmamap = NULL; 2348103285Sikob } 2349103285Sikob sbp_free_ocb(sdev->target->sbp, ocb); 2350103285Sikob} 2351103285Sikob 2352103285Sikobstatic void 2353103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2354103285Sikob{ 2355103285Sikob int s; 2356105792Ssimokawa struct sbp_ocb *ocb, *next; 2357105792Ssimokawa STAILQ_HEAD(, sbp_ocb) temp; 2358103285Sikob 2359103285Sikob s = splfw(); 2360105792Ssimokawa 2361105792Ssimokawa bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2362105792Ssimokawa STAILQ_INIT(&sdev->ocbs); 2363105792Ssimokawa for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2364105792Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2365103285Sikob sbp_abort_ocb(ocb, status); 2366103285Sikob } 2367105792Ssimokawa 2368103285Sikob splx(s); 2369103285Sikob} 2370103285Sikob 2371103285Sikobstatic devclass_t sbp_devclass; 2372103285Sikob 2373103285Sikobstatic device_method_t sbp_methods[] = { 2374103285Sikob /* device interface */ 2375103285Sikob DEVMETHOD(device_identify, sbp_identify), 2376103285Sikob DEVMETHOD(device_probe, sbp_probe), 2377103285Sikob DEVMETHOD(device_attach, sbp_attach), 2378103285Sikob DEVMETHOD(device_detach, sbp_detach), 2379110145Ssimokawa DEVMETHOD(device_shutdown, sbp_shutdown), 2380103285Sikob 2381103285Sikob { 0, 0 } 2382103285Sikob}; 2383103285Sikob 2384103285Sikobstatic driver_t sbp_driver = { 2385103285Sikob "sbp", 2386103285Sikob sbp_methods, 2387103285Sikob sizeof(struct sbp_softc), 2388103285Sikob}; 2389103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2390103285SikobMODULE_VERSION(sbp, 1); 2391103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1); 2392103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1); 2393