sbp.c revision 169130
1139749Simp/*- 2122526Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa 3122526Ssimokawa * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 4103285Sikob * All rights reserved. 5103285Sikob * 6103285Sikob * Redistribution and use in source and binary forms, with or without 7103285Sikob * modification, are permitted provided that the following conditions 8103285Sikob * are met: 9103285Sikob * 1. Redistributions of source code must retain the above copyright 10103285Sikob * notice, this list of conditions and the following disclaimer. 11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 12103285Sikob * notice, this list of conditions and the following disclaimer in the 13103285Sikob * documentation and/or other materials provided with the distribution. 14103285Sikob * 3. All advertising materials mentioning features or use of this software 15103285Sikob * must display the acknowledgement as bellow: 16103285Sikob * 17103285Sikob * This product includes software developed by K. Kobayashi and H. Shimokawa 18103285Sikob * 19103285Sikob * 4. The name of the author may not be used to endorse or promote products 20103285Sikob * derived from this software without specific prior written permission. 21103285Sikob * 22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25103285Sikob * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32103285Sikob * POSSIBILITY OF SUCH DAMAGE. 33103285Sikob * 34103285Sikob * $FreeBSD: head/sys/dev/firewire/sbp.c 169130 2007-04-30 13:41:40Z simokawa $ 35103285Sikob * 36103285Sikob */ 37103285Sikob 38103285Sikob#include <sys/param.h> 39103285Sikob#include <sys/systm.h> 40103285Sikob#include <sys/module.h> 41103285Sikob#include <sys/bus.h> 42127468Ssimokawa#include <sys/kernel.h> 43103285Sikob#include <sys/sysctl.h> 44103285Sikob#include <machine/bus.h> 45103285Sikob#include <sys/malloc.h> 46127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102 47117126Sscottl#include <sys/lock.h> 48117126Sscottl#include <sys/mutex.h> 49117732Ssimokawa#endif 50103285Sikob 51127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500106 52112136Ssimokawa#include <sys/devicestat.h> /* for struct devstat */ 53112136Ssimokawa#endif 54103285Sikob 55127468Ssimokawa#ifdef __DragonFly__ 56127468Ssimokawa#include <bus/cam/cam.h> 57127468Ssimokawa#include <bus/cam/cam_ccb.h> 58127468Ssimokawa#include <bus/cam/cam_sim.h> 59127468Ssimokawa#include <bus/cam/cam_xpt_sim.h> 60127468Ssimokawa#include <bus/cam/cam_debug.h> 61127468Ssimokawa#include <bus/cam/cam_periph.h> 62127468Ssimokawa#include <bus/cam/scsi/scsi_all.h> 63127468Ssimokawa 64127468Ssimokawa#include <bus/firewire/firewire.h> 65127468Ssimokawa#include <bus/firewire/firewirereg.h> 66127468Ssimokawa#include <bus/firewire/fwdma.h> 67127468Ssimokawa#include <bus/firewire/iec13213.h> 68127468Ssimokawa#include "sbp.h" 69127468Ssimokawa#else 70103285Sikob#include <cam/cam.h> 71103285Sikob#include <cam/cam_ccb.h> 72103285Sikob#include <cam/cam_sim.h> 73103285Sikob#include <cam/cam_xpt_sim.h> 74103285Sikob#include <cam/cam_debug.h> 75103285Sikob#include <cam/cam_periph.h> 76103285Sikob#include <cam/scsi/scsi_all.h> 77103285Sikob 78103285Sikob#include <dev/firewire/firewire.h> 79103285Sikob#include <dev/firewire/firewirereg.h> 80113584Ssimokawa#include <dev/firewire/fwdma.h> 81103285Sikob#include <dev/firewire/iec13213.h> 82120660Ssimokawa#include <dev/firewire/sbp.h> 83127468Ssimokawa#endif 84103285Sikob 85103285Sikob#define ccb_sdev_ptr spriv_ptr0 86103285Sikob#define ccb_sbp_ptr spriv_ptr1 87103285Sikob 88111615Ssimokawa#define SBP_NUM_TARGETS 8 /* MAX 64 */ 89121185Ssimokawa/* 90121185Ssimokawa * Scan_bus doesn't work for more than 8 LUNs 91121185Ssimokawa * because of CAM_SCSI2_MAXLUN in cam_xpt.c 92121185Ssimokawa */ 93121185Ssimokawa#define SBP_NUM_LUNS 64 94167622Ssimokawa#define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */) 95113584Ssimokawa#define SBP_DMA_SIZE PAGE_SIZE 96113584Ssimokawa#define SBP_LOGIN_SIZE sizeof(struct sbp_login_res) 97113584Ssimokawa#define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb)) 98103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS) 99113584Ssimokawa 100111615Ssimokawa/* 101111615Ssimokawa * STATUS FIFO addressing 102111615Ssimokawa * bit 103111615Ssimokawa * ----------------------- 104130532Sdfr * 0- 1( 2): 0 (alignment) 105111615Ssimokawa * 2- 7( 6): target 106111615Ssimokawa * 8-15( 8): lun 107120660Ssimokawa * 16-31( 8): reserved 108111615Ssimokawa * 32-47(16): SBP_BIND_HI 109111615Ssimokawa * 48-64(16): bus_id, node_id 110111615Ssimokawa */ 111103285Sikob#define SBP_BIND_HI 0x1 112120660Ssimokawa#define SBP_DEV2ADDR(t, l) \ 113120660Ssimokawa (((u_int64_t)SBP_BIND_HI << 32) \ 114120660Ssimokawa | (((l) & 0xff) << 8) \ 115120660Ssimokawa | (((t) & 0x3f) << 2)) 116103285Sikob#define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f) 117103285Sikob#define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff) 118120660Ssimokawa#define SBP_INITIATOR 7 119103285Sikob 120103285Sikobstatic char *orb_fun_name[] = { 121120660Ssimokawa ORB_FUN_NAMES 122103285Sikob}; 123103285Sikob 124111203Ssimokawastatic int debug = 0; 125103285Sikobstatic int auto_login = 1; 126124251Ssimokawastatic int max_speed = -1; 127111199Ssimokawastatic int sbp_cold = 1; 128122387Ssimokawastatic int ex_login = 1; 129122387Ssimokawastatic int login_delay = 1000; /* msec */ 130122387Ssimokawastatic int scan_delay = 500; /* msec */ 131127468Ssimokawastatic int use_doorbell = 0; 132127468Ssimokawastatic int sbp_tags = 0; 133103285Sikob 134103285SikobSYSCTL_DECL(_hw_firewire); 135103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem"); 136103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0, 137103285Sikob "SBP debug flag"); 138103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0, 139103285Sikob "SBP perform login automatically"); 140103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0, 141103285Sikob "SBP transfer max speed"); 142121792SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW, 143130677Ssimokawa &ex_login, 0, "SBP enable exclusive login"); 144122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW, 145122387Ssimokawa &login_delay, 0, "SBP login delay in msec"); 146122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW, 147122387Ssimokawa &scan_delay, 0, "SBP scan delay in msec"); 148127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW, 149127468Ssimokawa &use_doorbell, 0, "SBP use doorbell request"); 150127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0, 151127468Ssimokawa "SBP tagged queuing support"); 152103285Sikob 153122387SsimokawaTUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login); 154122387SsimokawaTUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed); 155122387SsimokawaTUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login); 156122387SsimokawaTUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay); 157122387SsimokawaTUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay); 158127468SsimokawaTUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell); 159127468SsimokawaTUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags); 160122387Ssimokawa 161103285Sikob#define NEED_RESPONSE 0 162103285Sikob 163113584Ssimokawa#define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE) 164113584Ssimokawa#ifdef __sparc64__ /* iommu */ 165167622Ssimokawa#define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX) 166113584Ssimokawa#else 167167622Ssimokawa#define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE) 168113584Ssimokawa#endif 169103285Sikobstruct sbp_ocb { 170103285Sikob STAILQ_ENTRY(sbp_ocb) ocb; 171103285Sikob union ccb *ccb; 172113584Ssimokawa bus_addr_t bus_addr; 173129585Sdfr uint32_t orb[8]; 174129585Sdfr#define IND_PTR_OFFSET (8*sizeof(uint32_t)) 175120660Ssimokawa struct ind_ptr ind_ptr[SBP_IND_MAX]; 176103285Sikob struct sbp_dev *sdev; 177113584Ssimokawa int flags; /* XXX should be removed */ 178103285Sikob bus_dmamap_t dmamap; 179103285Sikob}; 180113584Ssimokawa 181103285Sikob#define OCB_ACT_MGM 0 182103285Sikob#define OCB_ACT_CMD 1 183113584Ssimokawa#define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo)) 184103285Sikob 185103285Sikobstruct sbp_dev{ 186103285Sikob#define SBP_DEV_RESET 0 /* accept login */ 187114732Ssimokawa#define SBP_DEV_LOGIN 1 /* to login */ 188110336Ssimokawa#if 0 189103285Sikob#define SBP_DEV_RECONN 2 /* to reconnect */ 190110336Ssimokawa#endif 191103285Sikob#define SBP_DEV_TOATTACH 3 /* to attach */ 192103285Sikob#define SBP_DEV_PROBE 4 /* scan lun */ 193103285Sikob#define SBP_DEV_ATTACHED 5 /* in operation */ 194103285Sikob#define SBP_DEV_DEAD 6 /* unavailable unit */ 195103285Sikob#define SBP_DEV_RETRY 7 /* unavailable unit */ 196129585Sdfr uint8_t status:4, 197114732Ssimokawa timeout:4; 198129585Sdfr uint8_t type; 199129585Sdfr uint16_t lun_id; 200129585Sdfr uint16_t freeze; 201121185Ssimokawa#define ORB_LINK_DEAD (1 << 0) 202121185Ssimokawa#define VALID_LUN (1 << 1) 203121185Ssimokawa#define ORB_POINTER_ACTIVE (1 << 2) 204121185Ssimokawa#define ORB_POINTER_NEED (1 << 3) 205127468Ssimokawa#define ORB_DOORBELL_ACTIVE (1 << 4) 206127468Ssimokawa#define ORB_DOORBELL_NEED (1 << 5) 207127468Ssimokawa#define ORB_SHORTAGE (1 << 6) 208129585Sdfr uint16_t flags; 209103285Sikob struct cam_path *path; 210103285Sikob struct sbp_target *target; 211113584Ssimokawa struct fwdma_alloc dma; 212113584Ssimokawa struct sbp_login_res *login; 213111615Ssimokawa struct callout login_callout; 214113584Ssimokawa struct sbp_ocb *ocb; 215103285Sikob STAILQ_HEAD(, sbp_ocb) ocbs; 216113584Ssimokawa STAILQ_HEAD(, sbp_ocb) free_ocbs; 217127468Ssimokawa struct sbp_ocb *last_ocb; 218103285Sikob char vendor[32]; 219103285Sikob char product[32]; 220103285Sikob char revision[10]; 221103285Sikob}; 222103285Sikob 223103285Sikobstruct sbp_target { 224103285Sikob int target_id; 225103285Sikob int num_lun; 226121185Ssimokawa struct sbp_dev **luns; 227103285Sikob struct sbp_softc *sbp; 228103285Sikob struct fw_device *fwdev; 229129585Sdfr uint32_t mgm_hi, mgm_lo; 230111615Ssimokawa struct sbp_ocb *mgm_ocb_cur; 231111615Ssimokawa STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue; 232111615Ssimokawa struct callout mgm_ocb_timeout; 233111615Ssimokawa struct callout scan_callout; 234113584Ssimokawa STAILQ_HEAD(, fw_xfer) xferlist; 235113584Ssimokawa int n_xfer; 236103285Sikob}; 237103285Sikob 238103285Sikobstruct sbp_softc { 239103285Sikob struct firewire_dev_comm fd; 240103285Sikob struct cam_sim *sim; 241111615Ssimokawa struct cam_path *path; 242103285Sikob struct sbp_target targets[SBP_NUM_TARGETS]; 243103285Sikob struct fw_bind fwb; 244103285Sikob bus_dma_tag_t dmat; 245122387Ssimokawa struct timeval last_busreset; 246124145Ssimokawa#define SIMQ_FREEZED 1 247124145Ssimokawa int flags; 248103285Sikob}; 249122387Ssimokawa 250124169Ssimokawastatic void sbp_post_explore (void *); 251124169Ssimokawastatic void sbp_recv (struct fw_xfer *); 252124169Ssimokawastatic void sbp_mgm_callback (struct fw_xfer *); 253121185Ssimokawa#if 0 254124169Ssimokawastatic void sbp_cmd_callback (struct fw_xfer *); 255121185Ssimokawa#endif 256124169Ssimokawastatic void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *); 257127468Ssimokawastatic void sbp_doorbell(struct sbp_dev *); 258124169Ssimokawastatic void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int); 259124169Ssimokawastatic void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *); 260124169Ssimokawastatic void sbp_abort_ocb (struct sbp_ocb *, int); 261124169Ssimokawastatic void sbp_abort_all_ocbs (struct sbp_dev *, int); 262124169Ssimokawastatic struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int); 263124169Ssimokawastatic struct sbp_ocb * sbp_get_ocb (struct sbp_dev *); 264124169Ssimokawastatic struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *); 265124169Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *); 266121185Ssimokawastatic void sbp_cam_detach_sdev(struct sbp_dev *); 267121185Ssimokawastatic void sbp_free_sdev(struct sbp_dev *); 268124169Ssimokawastatic void sbp_cam_detach_target (struct sbp_target *); 269124169Ssimokawastatic void sbp_free_target (struct sbp_target *); 270124169Ssimokawastatic void sbp_mgm_timeout (void *arg); 271124169Ssimokawastatic void sbp_timeout (void *arg); 272124169Ssimokawastatic void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *); 273103285Sikob 274108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire"); 275103285Sikob 276103285Sikob/* cam related functions */ 277103285Sikobstatic void sbp_action(struct cam_sim *sim, union ccb *ccb); 278103285Sikobstatic void sbp_poll(struct cam_sim *sim); 279111615Ssimokawastatic void sbp_cam_scan_lun(struct cam_periph *, union ccb *); 280111615Ssimokawastatic void sbp_cam_scan_target(void *arg); 281103285Sikob 282103285Sikobstatic char *orb_status0[] = { 283103285Sikob /* 0 */ "No additional information to report", 284103285Sikob /* 1 */ "Request type not supported", 285103285Sikob /* 2 */ "Speed not supported", 286103285Sikob /* 3 */ "Page size not supported", 287103285Sikob /* 4 */ "Access denied", 288103285Sikob /* 5 */ "Logical unit not supported", 289103285Sikob /* 6 */ "Maximum payload too small", 290103285Sikob /* 7 */ "Reserved for future standardization", 291103285Sikob /* 8 */ "Resources unavailable", 292103285Sikob /* 9 */ "Function rejected", 293103285Sikob /* A */ "Login ID not recognized", 294103285Sikob /* B */ "Dummy ORB completed", 295103285Sikob /* C */ "Request aborted", 296103285Sikob /* FF */ "Unspecified error" 297103285Sikob#define MAX_ORB_STATUS0 0xd 298103285Sikob}; 299103285Sikob 300103285Sikobstatic char *orb_status1_object[] = { 301103285Sikob /* 0 */ "Operation request block (ORB)", 302103285Sikob /* 1 */ "Data buffer", 303103285Sikob /* 2 */ "Page table", 304103285Sikob /* 3 */ "Unable to specify" 305103285Sikob}; 306103285Sikob 307103285Sikobstatic char *orb_status1_serial_bus_error[] = { 308103285Sikob /* 0 */ "Missing acknowledge", 309103285Sikob /* 1 */ "Reserved; not to be used", 310103285Sikob /* 2 */ "Time-out error", 311103285Sikob /* 3 */ "Reserved; not to be used", 312103285Sikob /* 4 */ "Busy retry limit exceeded(X)", 313103285Sikob /* 5 */ "Busy retry limit exceeded(A)", 314103285Sikob /* 6 */ "Busy retry limit exceeded(B)", 315103285Sikob /* 7 */ "Reserved for future standardization", 316103285Sikob /* 8 */ "Reserved for future standardization", 317103285Sikob /* 9 */ "Reserved for future standardization", 318103285Sikob /* A */ "Reserved for future standardization", 319103285Sikob /* B */ "Tardy retry limit exceeded", 320103285Sikob /* C */ "Conflict error", 321103285Sikob /* D */ "Data error", 322103285Sikob /* E */ "Type error", 323103285Sikob /* F */ "Address error" 324103285Sikob}; 325103285Sikob 326103285Sikobstatic void 327103285Sikobsbp_identify(driver_t *driver, device_t parent) 328103285Sikob{ 329103285Sikob device_t child; 330103285SikobSBP_DEBUG(0) 331103285Sikob printf("sbp_identify\n"); 332103285SikobEND_DEBUG 333103285Sikob 334103285Sikob child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 335103285Sikob} 336103285Sikob 337103285Sikob/* 338103285Sikob * sbp_probe() 339103285Sikob */ 340103285Sikobstatic int 341103285Sikobsbp_probe(device_t dev) 342103285Sikob{ 343103285Sikob device_t pa; 344103285Sikob 345103285SikobSBP_DEBUG(0) 346103285Sikob printf("sbp_probe\n"); 347103285SikobEND_DEBUG 348103285Sikob 349103285Sikob pa = device_get_parent(dev); 350103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 351103285Sikob return(ENXIO); 352103285Sikob } 353103285Sikob 354120660Ssimokawa device_set_desc(dev, "SBP-2/SCSI over FireWire"); 355111615Ssimokawa 356132432Ssimokawa#if 0 357111615Ssimokawa if (bootverbose) 358111615Ssimokawa debug = bootverbose; 359132432Ssimokawa#endif 360132432Ssimokawa 361103285Sikob return (0); 362103285Sikob} 363103285Sikob 364103285Sikobstatic void 365103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new) 366103285Sikob{ 367103285Sikob struct fw_device *fwdev; 368103285Sikob 369103285Sikob printf("%s:%d:%d ", 370103285Sikob device_get_nameunit(sdev->target->sbp->fd.dev), 371103285Sikob sdev->target->target_id, 372103285Sikob sdev->lun_id 373103285Sikob ); 374103285Sikob if (new == 2) { 375103285Sikob return; 376103285Sikob } 377103285Sikob fwdev = sdev->target->fwdev; 378103285Sikob printf("ordered:%d type:%d EUI:%08x%08x node:%d " 379103285Sikob "speed:%d maxrec:%d", 380108503Ssimokawa (sdev->type & 0x40) >> 6, 381108503Ssimokawa (sdev->type & 0x1f), 382103285Sikob fwdev->eui.hi, 383103285Sikob fwdev->eui.lo, 384103285Sikob fwdev->dst, 385103285Sikob fwdev->speed, 386103285Sikob fwdev->maxrec 387103285Sikob ); 388103285Sikob if (new) 389103285Sikob printf(" new!\n"); 390103285Sikob else 391103285Sikob printf("\n"); 392103285Sikob sbp_show_sdev_info(sdev, 2); 393103285Sikob printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 394103285Sikob} 395103285Sikob 396110184Ssimokawastatic struct { 397110184Ssimokawa int bus; 398110184Ssimokawa int target; 399110184Ssimokawa struct fw_eui64 eui; 400110184Ssimokawa} wired[] = { 401110184Ssimokawa /* Bus Target EUI64 */ 402110184Ssimokawa#if 0 403110184Ssimokawa {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */ 404110184Ssimokawa {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */ 405110184Ssimokawa {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */ 406110184Ssimokawa#endif 407110184Ssimokawa {-1, -1, {0,0}} 408110184Ssimokawa}; 409110184Ssimokawa 410110184Ssimokawastatic int 411110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev) 412110184Ssimokawa{ 413110184Ssimokawa int bus, i, target=-1; 414110184Ssimokawa char w[SBP_NUM_TARGETS]; 415110184Ssimokawa 416110184Ssimokawa bzero(w, sizeof(w)); 417110184Ssimokawa bus = device_get_unit(sbp->fd.dev); 418110184Ssimokawa 419110184Ssimokawa /* XXX wired-down configuration should be gotten from 420110184Ssimokawa tunable or device hint */ 421110184Ssimokawa for (i = 0; wired[i].bus >= 0; i ++) { 422110184Ssimokawa if (wired[i].bus == bus) { 423110184Ssimokawa w[wired[i].target] = 1; 424110184Ssimokawa if (wired[i].eui.hi == fwdev->eui.hi && 425110184Ssimokawa wired[i].eui.lo == fwdev->eui.lo) 426110184Ssimokawa target = wired[i].target; 427110184Ssimokawa } 428110184Ssimokawa } 429110184Ssimokawa if (target >= 0) { 430110184Ssimokawa if(target < SBP_NUM_TARGETS && 431110184Ssimokawa sbp->targets[target].fwdev == NULL) 432110184Ssimokawa return(target); 433110184Ssimokawa device_printf(sbp->fd.dev, 434110184Ssimokawa "target %d is not free for %08x:%08x\n", 435110184Ssimokawa target, fwdev->eui.hi, fwdev->eui.lo); 436110184Ssimokawa target = -1; 437110184Ssimokawa } 438110184Ssimokawa /* non-wired target */ 439110184Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 440110184Ssimokawa if (sbp->targets[i].fwdev == NULL && w[i] == 0) { 441110184Ssimokawa target = i; 442110184Ssimokawa break; 443110184Ssimokawa } 444110184Ssimokawa 445110184Ssimokawa return target; 446110184Ssimokawa} 447110184Ssimokawa 448121185Ssimokawastatic void 449121185Ssimokawasbp_alloc_lun(struct sbp_target *target) 450103285Sikob{ 451108503Ssimokawa struct crom_context cc; 452108503Ssimokawa struct csrreg *reg; 453121185Ssimokawa struct sbp_dev *sdev, **newluns; 454121185Ssimokawa struct sbp_softc *sbp; 455121185Ssimokawa int maxlun, lun, i; 456103285Sikob 457121185Ssimokawa sbp = target->sbp; 458114069Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 459108503Ssimokawa /* XXX shoud parse appropriate unit directories only */ 460108503Ssimokawa maxlun = -1; 461108503Ssimokawa while (cc.depth >= 0) { 462108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 463108503Ssimokawa if (reg == NULL) 464108503Ssimokawa break; 465110839Ssimokawa lun = reg->val & 0xffff; 466108642SsimokawaSBP_DEBUG(0) 467108642Ssimokawa printf("target %d lun %d found\n", target->target_id, lun); 468108642SsimokawaEND_DEBUG 469108503Ssimokawa if (maxlun < lun) 470108503Ssimokawa maxlun = lun; 471108503Ssimokawa crom_next(&cc); 472108503Ssimokawa } 473110839Ssimokawa if (maxlun < 0) 474121185Ssimokawa printf("%s:%d no LUN found\n", 475121185Ssimokawa device_get_nameunit(target->sbp->fd.dev), 476121185Ssimokawa target->target_id); 477121185Ssimokawa 478121185Ssimokawa maxlun ++; 479110839Ssimokawa if (maxlun >= SBP_NUM_LUNS) 480110839Ssimokawa maxlun = SBP_NUM_LUNS; 481121185Ssimokawa 482121185Ssimokawa /* Invalidiate stale devices */ 483121185Ssimokawa for (lun = 0; lun < target->num_lun; lun ++) { 484121185Ssimokawa sdev = target->luns[lun]; 485121185Ssimokawa if (sdev == NULL) 486121185Ssimokawa continue; 487121185Ssimokawa sdev->flags &= ~VALID_LUN; 488121185Ssimokawa if (lun >= maxlun) { 489121185Ssimokawa /* lost device */ 490121185Ssimokawa sbp_cam_detach_sdev(sdev); 491121185Ssimokawa sbp_free_sdev(sdev); 492121185Ssimokawa } 493103285Sikob } 494121185Ssimokawa 495121185Ssimokawa /* Reallocate */ 496121185Ssimokawa if (maxlun != target->num_lun) { 497121185Ssimokawa newluns = (struct sbp_dev **) realloc(target->luns, 498121185Ssimokawa sizeof(struct sbp_dev *) * maxlun, 499121185Ssimokawa M_SBP, M_NOWAIT | M_ZERO); 500121185Ssimokawa 501121185Ssimokawa if (newluns == NULL) { 502127468Ssimokawa printf("%s: realloc failed\n", __func__); 503121185Ssimokawa newluns = target->luns; 504121185Ssimokawa maxlun = target->num_lun; 505121185Ssimokawa } 506121185Ssimokawa 507121185Ssimokawa /* 508121185Ssimokawa * We must zero the extended region for the case 509121185Ssimokawa * realloc() doesn't allocate new buffer. 510121185Ssimokawa */ 511121185Ssimokawa if (maxlun > target->num_lun) 512121185Ssimokawa bzero(&newluns[target->num_lun], 513121185Ssimokawa sizeof(struct sbp_dev *) * 514121185Ssimokawa (maxlun - target->num_lun)); 515121185Ssimokawa 516121185Ssimokawa target->luns = newluns; 517121185Ssimokawa target->num_lun = maxlun; 518121185Ssimokawa } 519121185Ssimokawa 520108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 521108503Ssimokawa while (cc.depth >= 0) { 522121185Ssimokawa int new = 0; 523121185Ssimokawa 524108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 525108503Ssimokawa if (reg == NULL) 526108503Ssimokawa break; 527110839Ssimokawa lun = reg->val & 0xffff; 528110839Ssimokawa if (lun >= SBP_NUM_LUNS) { 529110839Ssimokawa printf("too large lun %d\n", lun); 530121185Ssimokawa goto next; 531110839Ssimokawa } 532121185Ssimokawa 533121185Ssimokawa sdev = target->luns[lun]; 534121185Ssimokawa if (sdev == NULL) { 535121185Ssimokawa sdev = malloc(sizeof(struct sbp_dev), 536121185Ssimokawa M_SBP, M_NOWAIT | M_ZERO); 537121185Ssimokawa if (sdev == NULL) { 538127468Ssimokawa printf("%s: malloc failed\n", __func__); 539121185Ssimokawa goto next; 540121185Ssimokawa } 541121185Ssimokawa target->luns[lun] = sdev; 542121185Ssimokawa sdev->lun_id = lun; 543121185Ssimokawa sdev->target = target; 544121185Ssimokawa STAILQ_INIT(&sdev->ocbs); 545121185Ssimokawa CALLOUT_INIT(&sdev->login_callout); 546121185Ssimokawa sdev->status = SBP_DEV_RESET; 547121185Ssimokawa new = 1; 548121185Ssimokawa } 549121185Ssimokawa sdev->flags |= VALID_LUN; 550119196Ssimokawa sdev->type = (reg->val & 0xff0000) >> 16; 551113584Ssimokawa 552121185Ssimokawa if (new == 0) 553121185Ssimokawa goto next; 554121185Ssimokawa 555113584Ssimokawa fwdma_malloc(sbp->fd.fc, 556129585Sdfr /* alignment */ sizeof(uint32_t), 557113584Ssimokawa SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT); 558113584Ssimokawa if (sdev->dma.v_addr == NULL) { 559113584Ssimokawa printf("%s: dma space allocation failed\n", 560127468Ssimokawa __func__); 561121185Ssimokawa free(sdev, M_SBP); 562121185Ssimokawa target->luns[lun] = NULL; 563121185Ssimokawa goto next; 564113584Ssimokawa } 565113584Ssimokawa sdev->login = (struct sbp_login_res *) sdev->dma.v_addr; 566113584Ssimokawa sdev->ocb = (struct sbp_ocb *) 567113584Ssimokawa ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE); 568113584Ssimokawa bzero((char *)sdev->ocb, 569113584Ssimokawa sizeof (struct sbp_ocb) * SBP_QUEUE_LEN); 570113584Ssimokawa 571113584Ssimokawa STAILQ_INIT(&sdev->free_ocbs); 572113584Ssimokawa for (i = 0; i < SBP_QUEUE_LEN; i++) { 573113584Ssimokawa struct sbp_ocb *ocb; 574113584Ssimokawa ocb = &sdev->ocb[i]; 575113584Ssimokawa ocb->bus_addr = sdev->dma.bus_addr 576113584Ssimokawa + SBP_LOGIN_SIZE 577113584Ssimokawa + sizeof(struct sbp_ocb) * i 578113584Ssimokawa + offsetof(struct sbp_ocb, orb[0]); 579113584Ssimokawa if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 580113584Ssimokawa printf("sbp_attach: cannot create dmamap\n"); 581121185Ssimokawa /* XXX */ 582121185Ssimokawa goto next; 583113584Ssimokawa } 584113584Ssimokawa sbp_free_ocb(sdev, ocb); 585113584Ssimokawa } 586121185Ssimokawanext: 587108503Ssimokawa crom_next(&cc); 588103285Sikob } 589121185Ssimokawa 590121185Ssimokawa for (lun = 0; lun < target->num_lun; lun ++) { 591121185Ssimokawa sdev = target->luns[lun]; 592121185Ssimokawa if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) { 593121185Ssimokawa sbp_cam_detach_sdev(sdev); 594121185Ssimokawa sbp_free_sdev(sdev); 595121185Ssimokawa target->luns[lun] = NULL; 596121185Ssimokawa } 597121185Ssimokawa } 598121185Ssimokawa} 599121185Ssimokawa 600121185Ssimokawastatic struct sbp_target * 601121185Ssimokawasbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 602121185Ssimokawa{ 603121185Ssimokawa int i; 604121185Ssimokawa struct sbp_target *target; 605121185Ssimokawa struct crom_context cc; 606121185Ssimokawa struct csrreg *reg; 607121185Ssimokawa 608121185SsimokawaSBP_DEBUG(1) 609121185Ssimokawa printf("sbp_alloc_target\n"); 610121185SsimokawaEND_DEBUG 611121185Ssimokawa i = sbp_new_target(sbp, fwdev); 612121185Ssimokawa if (i < 0) { 613121185Ssimokawa device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n"); 614121185Ssimokawa return NULL; 615121185Ssimokawa } 616121185Ssimokawa /* new target */ 617121185Ssimokawa target = &sbp->targets[i]; 618121185Ssimokawa target->sbp = sbp; 619121185Ssimokawa target->fwdev = fwdev; 620121185Ssimokawa target->target_id = i; 621121185Ssimokawa /* XXX we may want to reload mgm port after each bus reset */ 622121185Ssimokawa /* XXX there might be multiple management agents */ 623121185Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 624121185Ssimokawa reg = crom_search_key(&cc, CROM_MGM); 625121185Ssimokawa if (reg == NULL || reg->val == 0) { 626121185Ssimokawa printf("NULL management address\n"); 627121185Ssimokawa target->fwdev = NULL; 628121185Ssimokawa return NULL; 629121185Ssimokawa } 630121185Ssimokawa target->mgm_hi = 0xffff; 631121185Ssimokawa target->mgm_lo = 0xf0000000 | (reg->val << 2); 632121185Ssimokawa target->mgm_ocb_cur = NULL; 633121185SsimokawaSBP_DEBUG(1) 634121185Ssimokawa printf("target:%d mgm_port: %x\n", i, target->mgm_lo); 635121185SsimokawaEND_DEBUG 636121185Ssimokawa STAILQ_INIT(&target->xferlist); 637121185Ssimokawa target->n_xfer = 0; 638121185Ssimokawa STAILQ_INIT(&target->mgm_ocb_queue); 639121185Ssimokawa CALLOUT_INIT(&target->mgm_ocb_timeout); 640121185Ssimokawa CALLOUT_INIT(&target->scan_callout); 641121185Ssimokawa 642121185Ssimokawa target->luns = NULL; 643121185Ssimokawa target->num_lun = 0; 644114069Ssimokawa return target; 645103285Sikob} 646103285Sikob 647103285Sikobstatic void 648103285Sikobsbp_probe_lun(struct sbp_dev *sdev) 649103285Sikob{ 650103285Sikob struct fw_device *fwdev; 651114069Ssimokawa struct crom_context c, *cc = &c; 652114069Ssimokawa struct csrreg *reg; 653103285Sikob 654103285Sikob bzero(sdev->vendor, sizeof(sdev->vendor)); 655103285Sikob bzero(sdev->product, sizeof(sdev->product)); 656114069Ssimokawa 657114069Ssimokawa fwdev = sdev->target->fwdev; 658114069Ssimokawa crom_init_context(cc, fwdev->csrrom); 659114069Ssimokawa /* get vendor string */ 660114069Ssimokawa crom_search_key(cc, CSRKEY_VENDOR); 661114069Ssimokawa crom_next(cc); 662114069Ssimokawa crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor)); 663114223Ssimokawa /* skip to the unit directory for SBP-2 */ 664114260Ssimokawa while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) { 665114223Ssimokawa if (reg->val == CSRVAL_T10SBP2) 666114223Ssimokawa break; 667114260Ssimokawa crom_next(cc); 668114260Ssimokawa } 669114069Ssimokawa /* get firmware revision */ 670114069Ssimokawa reg = crom_search_key(cc, CSRKEY_FIRM_VER); 671114069Ssimokawa if (reg != NULL) 672114069Ssimokawa snprintf(sdev->revision, sizeof(sdev->revision), 673114069Ssimokawa "%06x", reg->val); 674114069Ssimokawa /* get product string */ 675114069Ssimokawa crom_search_key(cc, CSRKEY_MODEL); 676114069Ssimokawa crom_next(cc); 677114069Ssimokawa crom_parse_text(cc, sdev->product, sizeof(sdev->product)); 678103285Sikob} 679111615Ssimokawa 680103285Sikobstatic void 681111615Ssimokawasbp_login_callout(void *arg) 682103285Sikob{ 683111615Ssimokawa struct sbp_dev *sdev = (struct sbp_dev *)arg; 684111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL); 685111615Ssimokawa} 686111615Ssimokawa 687122387Ssimokawastatic void 688122387Ssimokawasbp_login(struct sbp_dev *sdev) 689122387Ssimokawa{ 690122387Ssimokawa struct timeval delta; 691122387Ssimokawa struct timeval t; 692122387Ssimokawa int ticks = 0; 693122387Ssimokawa 694122387Ssimokawa microtime(&delta); 695122387Ssimokawa timevalsub(&delta, &sdev->target->sbp->last_busreset); 696122387Ssimokawa t.tv_sec = login_delay / 1000; 697122387Ssimokawa t.tv_usec = (login_delay % 1000) * 1000; 698122387Ssimokawa timevalsub(&t, &delta); 699122387Ssimokawa if (t.tv_sec >= 0 && t.tv_usec > 0) 700122387Ssimokawa ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000; 701122387SsimokawaSBP_DEBUG(0) 702153706Strhodes printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__, 703153706Strhodes (intmax_t)t.tv_sec, t.tv_usec, ticks); 704122387SsimokawaEND_DEBUG 705122387Ssimokawa callout_reset(&sdev->login_callout, ticks, 706122387Ssimokawa sbp_login_callout, (void *)(sdev)); 707122387Ssimokawa} 708122387Ssimokawa 709114069Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \ 710114069Ssimokawa && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2)) 711111615Ssimokawa 712111615Ssimokawastatic void 713111615Ssimokawasbp_probe_target(void *arg) 714111615Ssimokawa{ 715111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 716103285Sikob struct sbp_softc *sbp; 717103285Sikob struct sbp_dev *sdev; 718103285Sikob struct firewire_comm *fc; 719111615Ssimokawa int i, alive; 720103285Sikob 721111615Ssimokawa alive = SBP_FWDEV_ALIVE(target->fwdev); 722103285SikobSBP_DEBUG(1) 723103285Sikob printf("sbp_probe_target %d\n", target->target_id); 724103285Sikob if (!alive) 725103285Sikob printf("not alive\n"); 726103285SikobEND_DEBUG 727103285Sikob 728103285Sikob sbp = target->sbp; 729103285Sikob fc = target->sbp->fd.fc; 730121185Ssimokawa sbp_alloc_lun(target); 731121185Ssimokawa 732111615Ssimokawa /* XXX untimeout mgm_ocb and dequeue */ 733103285Sikob for (i=0; i < target->num_lun; i++) { 734121185Ssimokawa sdev = target->luns[i]; 735121185Ssimokawa if (sdev == NULL) 736121185Ssimokawa continue; 737103285Sikob if (alive && (sdev->status != SBP_DEV_DEAD)) { 738103285Sikob if (sdev->path != NULL) { 739103285Sikob xpt_freeze_devq(sdev->path, 1); 740111615Ssimokawa sdev->freeze ++; 741103285Sikob } 742111615Ssimokawa sbp_probe_lun(sdev); 743111615SsimokawaSBP_DEBUG(0) 744111615Ssimokawa sbp_show_sdev_info(sdev, 745111615Ssimokawa (sdev->status == SBP_DEV_RESET)); 746111615SsimokawaEND_DEBUG 747111615Ssimokawa 748111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 749103285Sikob switch (sdev->status) { 750110336Ssimokawa case SBP_DEV_RESET: 751103285Sikob /* new or revived target */ 752113584Ssimokawa if (auto_login) 753111615Ssimokawa sbp_login(sdev); 754103285Sikob break; 755111615Ssimokawa case SBP_DEV_TOATTACH: 756111615Ssimokawa case SBP_DEV_PROBE: 757111615Ssimokawa case SBP_DEV_ATTACHED: 758110336Ssimokawa case SBP_DEV_RETRY: 759110336Ssimokawa default: 760111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL); 761110336Ssimokawa break; 762103285Sikob } 763103285Sikob } else { 764103285Sikob switch (sdev->status) { 765103285Sikob case SBP_DEV_ATTACHED: 766103285SikobSBP_DEBUG(0) 767103285Sikob /* the device has gone */ 768103285Sikob sbp_show_sdev_info(sdev, 2); 769103285Sikob printf("lost target\n"); 770103285SikobEND_DEBUG 771111615Ssimokawa if (sdev->path) { 772103285Sikob xpt_freeze_devq(sdev->path, 1); 773111615Ssimokawa sdev->freeze ++; 774111615Ssimokawa } 775103285Sikob sdev->status = SBP_DEV_RETRY; 776111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 777103285Sikob break; 778103285Sikob case SBP_DEV_PROBE: 779103285Sikob case SBP_DEV_TOATTACH: 780103285Sikob sdev->status = SBP_DEV_RESET; 781103285Sikob break; 782103285Sikob case SBP_DEV_RETRY: 783103285Sikob case SBP_DEV_RESET: 784103285Sikob case SBP_DEV_DEAD: 785103285Sikob break; 786103285Sikob } 787103285Sikob } 788103285Sikob } 789103285Sikob} 790103285Sikob 791103285Sikobstatic void 792113584Ssimokawasbp_post_busreset(void *arg) 793113584Ssimokawa{ 794113584Ssimokawa struct sbp_softc *sbp; 795113584Ssimokawa 796113584Ssimokawa sbp = (struct sbp_softc *)arg; 797113584SsimokawaSBP_DEBUG(0) 798113584Ssimokawa printf("sbp_post_busreset\n"); 799113584SsimokawaEND_DEBUG 800124145Ssimokawa if ((sbp->sim->flags & SIMQ_FREEZED) == 0) { 801124145Ssimokawa xpt_freeze_simq(sbp->sim, /*count*/1); 802124145Ssimokawa sbp->sim->flags |= SIMQ_FREEZED; 803124145Ssimokawa } 804122387Ssimokawa microtime(&sbp->last_busreset); 805113584Ssimokawa} 806113584Ssimokawa 807113584Ssimokawastatic void 808103285Sikobsbp_post_explore(void *arg) 809103285Sikob{ 810103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)arg; 811103285Sikob struct sbp_target *target; 812103285Sikob struct fw_device *fwdev; 813103285Sikob int i, alive; 814103285Sikob 815111199SsimokawaSBP_DEBUG(0) 816111199Ssimokawa printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold); 817103285SikobEND_DEBUG 818122387Ssimokawa if (sbp_cold > 0) 819122387Ssimokawa sbp_cold --; 820122387Ssimokawa 821122387Ssimokawa#if 0 822122387Ssimokawa /* 823122387Ssimokawa * XXX don't let CAM the bus rest. 824122387Ssimokawa * CAM tries to do something with freezed (DEV_RETRY) devices. 825111615Ssimokawa */ 826111615Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 827103285Sikob#endif 828111615Ssimokawa 829130532Sdfr /* Garbage Collection */ 830103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 831103285Sikob target = &sbp->targets[i]; 832110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) 833110193Ssimokawa if (target->fwdev == NULL || target->fwdev == fwdev) 834110193Ssimokawa break; 835121185Ssimokawa if (fwdev == NULL) { 836103285Sikob /* device has removed in lower driver */ 837110145Ssimokawa sbp_cam_detach_target(target); 838121185Ssimokawa sbp_free_target(target); 839103285Sikob } 840103285Sikob } 841103285Sikob /* traverse device list */ 842110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) { 843103285SikobSBP_DEBUG(0) 844103285Sikob printf("sbp_post_explore: EUI:%08x%08x ", 845103285Sikob fwdev->eui.hi, fwdev->eui.lo); 846114069Ssimokawa if (fwdev->status != FWDEVATTACHED) 847103285Sikob printf("not attached, state=%d.\n", fwdev->status); 848114069Ssimokawa else 849114069Ssimokawa printf("attached\n"); 850103285SikobEND_DEBUG 851111615Ssimokawa alive = SBP_FWDEV_ALIVE(fwdev); 852103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 853103285Sikob target = &sbp->targets[i]; 854103285Sikob if(target->fwdev == fwdev ) { 855103285Sikob /* known target */ 856103285Sikob break; 857103285Sikob } 858103285Sikob } 859103285Sikob if(i == SBP_NUM_TARGETS){ 860103285Sikob if (alive) { 861103285Sikob /* new target */ 862103285Sikob target = sbp_alloc_target(sbp, fwdev); 863103285Sikob if (target == NULL) 864103285Sikob continue; 865103285Sikob } else { 866103285Sikob continue; 867103285Sikob } 868103285Sikob } 869111615Ssimokawa sbp_probe_target((void *)target); 870121185Ssimokawa if (target->num_lun == 0) 871121185Ssimokawa sbp_free_target(target); 872103285Sikob } 873124145Ssimokawa xpt_release_simq(sbp->sim, /*run queue*/TRUE); 874124145Ssimokawa sbp->sim->flags &= ~SIMQ_FREEZED; 875103285Sikob} 876103285Sikob 877103285Sikob#if NEED_RESPONSE 878103285Sikobstatic void 879103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){ 880113584Ssimokawa int s; 881103285Sikob struct sbp_dev *sdev; 882103285Sikob sdev = (struct sbp_dev *)xfer->sc; 883113584SsimokawaSBP_DEBUG(1) 884103285Sikob sbp_show_sdev_info(sdev, 2); 885103285Sikob printf("sbp_loginres_callback\n"); 886103285SikobEND_DEBUG 887113584Ssimokawa /* recycle */ 888113584Ssimokawa s = splfw(); 889113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link); 890113584Ssimokawa splx(s); 891103285Sikob return; 892103285Sikob} 893103285Sikob#endif 894103285Sikob 895113584Ssimokawastatic __inline void 896113584Ssimokawasbp_xfer_free(struct fw_xfer *xfer) 897113584Ssimokawa{ 898113584Ssimokawa struct sbp_dev *sdev; 899113584Ssimokawa int s; 900113584Ssimokawa 901113584Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 902113584Ssimokawa fw_xfer_unload(xfer); 903113584Ssimokawa s = splfw(); 904113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link); 905113584Ssimokawa splx(s); 906113584Ssimokawa} 907113584Ssimokawa 908103285Sikobstatic void 909114732Ssimokawasbp_reset_start_callback(struct fw_xfer *xfer) 910103285Sikob{ 911114732Ssimokawa struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc; 912114732Ssimokawa struct sbp_target *target = sdev->target; 913114732Ssimokawa int i; 914114732Ssimokawa 915114732Ssimokawa if (xfer->resp != 0) { 916114732Ssimokawa sbp_show_sdev_info(sdev, 2); 917114732Ssimokawa printf("sbp_reset_start failed: resp=%d\n", xfer->resp); 918114732Ssimokawa } 919114732Ssimokawa 920114732Ssimokawa for (i = 0; i < target->num_lun; i++) { 921121185Ssimokawa tsdev = target->luns[i]; 922121185Ssimokawa if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN) 923121185Ssimokawa sbp_login(tsdev); 924114732Ssimokawa } 925114732Ssimokawa} 926114732Ssimokawa 927114732Ssimokawastatic void 928114732Ssimokawasbp_reset_start(struct sbp_dev *sdev) 929114732Ssimokawa{ 930114732Ssimokawa struct fw_xfer *xfer; 931114732Ssimokawa struct fw_pkt *fp; 932114732Ssimokawa 933114732SsimokawaSBP_DEBUG(0) 934114732Ssimokawa sbp_show_sdev_info(sdev, 2); 935114732Ssimokawa printf("sbp_reset_start\n"); 936114732SsimokawaEND_DEBUG 937114732Ssimokawa 938114732Ssimokawa xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 939167632Ssimokawa xfer->hand = sbp_reset_start_callback; 940120660Ssimokawa fp = &xfer->send.hdr; 941114732Ssimokawa fp->mode.wreqq.dest_hi = 0xffff; 942114732Ssimokawa fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 943114732Ssimokawa fp->mode.wreqq.data = htonl(0xf); 944114732Ssimokawa fw_asyreq(xfer->fc, -1, xfer); 945114732Ssimokawa} 946114732Ssimokawa 947114732Ssimokawastatic void 948114732Ssimokawasbp_mgm_callback(struct fw_xfer *xfer) 949114732Ssimokawa{ 950103285Sikob struct sbp_dev *sdev; 951114732Ssimokawa int resp; 952114732Ssimokawa 953103285Sikob sdev = (struct sbp_dev *)xfer->sc; 954114732Ssimokawa 955114732SsimokawaSBP_DEBUG(1) 956103285Sikob sbp_show_sdev_info(sdev, 2); 957114732Ssimokawa printf("sbp_mgm_callback\n"); 958103285SikobEND_DEBUG 959114732Ssimokawa resp = xfer->resp; 960113584Ssimokawa sbp_xfer_free(xfer); 961114732Ssimokawa#if 0 962114732Ssimokawa if (resp != 0) { 963114732Ssimokawa sbp_show_sdev_info(sdev, 2); 964114732Ssimokawa printf("management ORB failed(%d) ... RESET_START\n", resp); 965114732Ssimokawa sbp_reset_start(sdev); 966114732Ssimokawa } 967114732Ssimokawa#endif 968103285Sikob return; 969103285Sikob} 970103285Sikob 971111615Ssimokawastatic struct sbp_dev * 972111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun) 973111615Ssimokawa{ 974121185Ssimokawa struct sbp_dev **sdevp; 975111615Ssimokawa int i; 976111615Ssimokawa 977121185Ssimokawa for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun; 978121185Ssimokawa i++, sdevp++) 979121185Ssimokawa if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE) 980121185Ssimokawa return(*sdevp); 981121185Ssimokawa return(NULL); 982111615Ssimokawa} 983111615Ssimokawa 984111615Ssimokawa#define SCAN_PRI 1 985103285Sikobstatic void 986111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb) 987103285Sikob{ 988111615Ssimokawa struct sbp_target *target; 989103285Sikob struct sbp_dev *sdev; 990111615Ssimokawa 991103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 992111615Ssimokawa target = sdev->target; 993110269SsimokawaSBP_DEBUG(0) 994103285Sikob sbp_show_sdev_info(sdev, 2); 995111615Ssimokawa printf("sbp_cam_scan_lun\n"); 996103285SikobEND_DEBUG 997111615Ssimokawa if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 998111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 999111615Ssimokawa } else { 1000111615Ssimokawa sbp_show_sdev_info(sdev, 2); 1001111615Ssimokawa printf("scan failed\n"); 1002111615Ssimokawa } 1003111615Ssimokawa sdev = sbp_next_dev(target, sdev->lun_id + 1); 1004111615Ssimokawa if (sdev == NULL) { 1005111615Ssimokawa free(ccb, M_SBP); 1006111615Ssimokawa return; 1007111615Ssimokawa } 1008111615Ssimokawa /* reuse ccb */ 1009111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 1010111615Ssimokawa ccb->ccb_h.ccb_sdev_ptr = sdev; 1011111615Ssimokawa xpt_action(ccb); 1012111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1013111615Ssimokawa sdev->freeze = 1; 1014103285Sikob} 1015103285Sikob 1016103285Sikobstatic void 1017111615Ssimokawasbp_cam_scan_target(void *arg) 1018103285Sikob{ 1019111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 1020111615Ssimokawa struct sbp_dev *sdev; 1021110798Ssimokawa union ccb *ccb; 1022103285Sikob 1023111615Ssimokawa sdev = sbp_next_dev(target, 0); 1024111615Ssimokawa if (sdev == NULL) { 1025111615Ssimokawa printf("sbp_cam_scan_target: nothing to do for target%d\n", 1026111615Ssimokawa target->target_id); 1027110798Ssimokawa return; 1028110798Ssimokawa } 1029103285SikobSBP_DEBUG(0) 1030103285Sikob sbp_show_sdev_info(sdev, 2); 1031111615Ssimokawa printf("sbp_cam_scan_target\n"); 1032103285SikobEND_DEBUG 1033111615Ssimokawa ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 1034111615Ssimokawa if (ccb == NULL) { 1035111615Ssimokawa printf("sbp_cam_scan_target: malloc failed\n"); 1036111615Ssimokawa return; 1037111615Ssimokawa } 1038111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 1039103285Sikob ccb->ccb_h.func_code = XPT_SCAN_LUN; 1040111615Ssimokawa ccb->ccb_h.cbfcnp = sbp_cam_scan_lun; 1041111615Ssimokawa ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1042103285Sikob ccb->crcn.flags = CAM_FLAG_NONE; 1043103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 1044103285Sikob 1045103285Sikob /* The scan is in progress now. */ 1046111040Ssimokawa xpt_action(ccb); 1047111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1048111615Ssimokawa sdev->freeze = 1; 1049103285Sikob} 1050103285Sikob 1051111615Ssimokawastatic __inline void 1052111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev) 1053111615Ssimokawa{ 1054111040Ssimokawa sdev->status = SBP_DEV_PROBE; 1055122387Ssimokawa callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000, 1056111615Ssimokawa sbp_cam_scan_target, (void *)sdev->target); 1057103285Sikob} 1058103285Sikob 1059103285Sikobstatic void 1060103285Sikobsbp_do_attach(struct fw_xfer *xfer) 1061103285Sikob{ 1062103285Sikob struct sbp_dev *sdev; 1063111615Ssimokawa struct sbp_target *target; 1064111615Ssimokawa struct sbp_softc *sbp; 1065103285Sikob 1066103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1067111615Ssimokawa target = sdev->target; 1068111615Ssimokawa sbp = target->sbp; 1069103285SikobSBP_DEBUG(0) 1070103285Sikob sbp_show_sdev_info(sdev, 2); 1071103285Sikob printf("sbp_do_attach\n"); 1072103285SikobEND_DEBUG 1073113584Ssimokawa sbp_xfer_free(xfer); 1074111199Ssimokawa 1075103285Sikob if (sdev->path == NULL) 1076103285Sikob xpt_create_path(&sdev->path, xpt_periph, 1077111615Ssimokawa cam_sim_path(target->sbp->sim), 1078111615Ssimokawa target->target_id, sdev->lun_id); 1079103285Sikob 1080111199Ssimokawa /* 1081111199Ssimokawa * Let CAM scan the bus if we are in the boot process. 1082111199Ssimokawa * XXX xpt_scan_bus cannot detect LUN larger than 0 1083111199Ssimokawa * if LUN 0 doesn't exists. 1084111199Ssimokawa */ 1085111199Ssimokawa if (sbp_cold > 0) { 1086111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1087111199Ssimokawa return; 1088111199Ssimokawa } 1089111199Ssimokawa 1090111615Ssimokawa sbp_scan_dev(sdev); 1091103285Sikob return; 1092103285Sikob} 1093103285Sikob 1094103285Sikobstatic void 1095103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer) 1096103285Sikob{ 1097103285Sikob struct sbp_dev *sdev; 1098103285Sikob 1099103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1100103285SikobSBP_DEBUG(1) 1101103285Sikob sbp_show_sdev_info(sdev, 2); 1102127468Ssimokawa printf("%s\n", __func__); 1103103285SikobEND_DEBUG 1104114732Ssimokawa if (xfer->resp != 0) { 1105114732Ssimokawa sbp_show_sdev_info(sdev, 2); 1106127468Ssimokawa printf("%s: resp=%d\n", __func__, xfer->resp); 1107114732Ssimokawa } 1108114732Ssimokawa 1109113584Ssimokawa sbp_xfer_free(xfer); 1110111615Ssimokawa if (sdev->path) { 1111111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1112111615Ssimokawa sdev->freeze = 0; 1113111615Ssimokawa } 1114103285Sikob} 1115103285Sikob 1116103285Sikobstatic void 1117110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev) 1118103285Sikob{ 1119103285Sikob struct fw_xfer *xfer; 1120103285Sikob struct fw_pkt *fp; 1121103285Sikob 1122103285SikobSBP_DEBUG(0) 1123103285Sikob sbp_show_sdev_info(sdev, 2); 1124103285Sikob printf("sbp_agent_reset\n"); 1125103285SikobEND_DEBUG 1126103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 1127103285Sikob if (xfer == NULL) 1128103285Sikob return; 1129115788Ssimokawa if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE) 1130167632Ssimokawa xfer->hand = sbp_agent_reset_callback; 1131110336Ssimokawa else 1132167632Ssimokawa xfer->hand = sbp_do_attach; 1133120660Ssimokawa fp = &xfer->send.hdr; 1134103285Sikob fp->mode.wreqq.data = htonl(0xf); 1135103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1136111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_BDR_SENT); 1137103285Sikob} 1138103285Sikob 1139103285Sikobstatic void 1140103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer) 1141103285Sikob{ 1142103285Sikob struct sbp_dev *sdev; 1143103285Sikob 1144103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1145103285SikobSBP_DEBUG(1) 1146103285Sikob sbp_show_sdev_info(sdev, 2); 1147110336Ssimokawa printf("sbp_busy_timeout_callback\n"); 1148103285SikobEND_DEBUG 1149113584Ssimokawa sbp_xfer_free(xfer); 1150110336Ssimokawa sbp_agent_reset(sdev); 1151103285Sikob} 1152103285Sikob 1153103285Sikobstatic void 1154103285Sikobsbp_busy_timeout(struct sbp_dev *sdev) 1155103285Sikob{ 1156103285Sikob struct fw_pkt *fp; 1157103285Sikob struct fw_xfer *xfer; 1158103285SikobSBP_DEBUG(0) 1159103285Sikob sbp_show_sdev_info(sdev, 2); 1160103285Sikob printf("sbp_busy_timeout\n"); 1161103285SikobEND_DEBUG 1162103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1163103285Sikob 1164167632Ssimokawa xfer->hand = sbp_busy_timeout_callback; 1165120660Ssimokawa fp = &xfer->send.hdr; 1166113584Ssimokawa fp->mode.wreqq.dest_hi = 0xffff; 1167113584Ssimokawa fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT; 1168110129Ssimokawa fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 1169103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1170103285Sikob} 1171103285Sikob 1172103285Sikobstatic void 1173121185Ssimokawasbp_orb_pointer_callback(struct fw_xfer *xfer) 1174121185Ssimokawa{ 1175121185Ssimokawa struct sbp_dev *sdev; 1176121185Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 1177121185Ssimokawa 1178121185SsimokawaSBP_DEBUG(1) 1179121185Ssimokawa sbp_show_sdev_info(sdev, 2); 1180127468Ssimokawa printf("%s\n", __func__); 1181121185SsimokawaEND_DEBUG 1182121185Ssimokawa if (xfer->resp != 0) { 1183121185Ssimokawa /* XXX */ 1184127468Ssimokawa printf("%s: xfer->resp = %d\n", __func__, xfer->resp); 1185121185Ssimokawa } 1186121185Ssimokawa sbp_xfer_free(xfer); 1187121185Ssimokawa sdev->flags &= ~ORB_POINTER_ACTIVE; 1188121185Ssimokawa 1189121185Ssimokawa if ((sdev->flags & ORB_POINTER_NEED) != 0) { 1190121185Ssimokawa struct sbp_ocb *ocb; 1191121185Ssimokawa 1192121185Ssimokawa sdev->flags &= ~ORB_POINTER_NEED; 1193121185Ssimokawa ocb = STAILQ_FIRST(&sdev->ocbs); 1194121185Ssimokawa if (ocb != NULL) 1195121185Ssimokawa sbp_orb_pointer(sdev, ocb); 1196121185Ssimokawa } 1197121185Ssimokawa return; 1198121185Ssimokawa} 1199121185Ssimokawa 1200121185Ssimokawastatic void 1201103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1202103285Sikob{ 1203103285Sikob struct fw_xfer *xfer; 1204103285Sikob struct fw_pkt *fp; 1205121185SsimokawaSBP_DEBUG(1) 1206103285Sikob sbp_show_sdev_info(sdev, 2); 1207129585Sdfr printf("%s: 0x%08x\n", __func__, (uint32_t)ocb->bus_addr); 1208103285SikobEND_DEBUG 1209103285Sikob 1210121185Ssimokawa if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) { 1211122421SsimokawaSBP_DEBUG(0) 1212127468Ssimokawa printf("%s: orb pointer active\n", __func__); 1213122421SsimokawaEND_DEBUG 1214121185Ssimokawa sdev->flags |= ORB_POINTER_NEED; 1215121185Ssimokawa return; 1216121185Ssimokawa } 1217121185Ssimokawa 1218121185Ssimokawa sdev->flags |= ORB_POINTER_ACTIVE; 1219103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 1220103285Sikob if (xfer == NULL) 1221103285Sikob return; 1222167632Ssimokawa xfer->hand = sbp_orb_pointer_callback; 1223103285Sikob 1224120660Ssimokawa fp = &xfer->send.hdr; 1225113584Ssimokawa fp->mode.wreqb.len = 8; 1226103285Sikob fp->mode.wreqb.extcode = 0; 1227120660Ssimokawa xfer->send.payload[0] = 1228103285Sikob htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1229129585Sdfr xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr); 1230103285Sikob 1231103285Sikob if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 1232113584Ssimokawa sbp_xfer_free(xfer); 1233103285Sikob ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 1234103285Sikob xpt_done(ocb->ccb); 1235103285Sikob } 1236103285Sikob} 1237103285Sikob 1238103285Sikobstatic void 1239127468Ssimokawasbp_doorbell_callback(struct fw_xfer *xfer) 1240121185Ssimokawa{ 1241121185Ssimokawa struct sbp_dev *sdev; 1242121185Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 1243127468Ssimokawa 1244127468SsimokawaSBP_DEBUG(1) 1245121185Ssimokawa sbp_show_sdev_info(sdev, 2); 1246127468Ssimokawa printf("sbp_doorbell_callback\n"); 1247121185SsimokawaEND_DEBUG 1248121185Ssimokawa if (xfer->resp != 0) { 1249121185Ssimokawa /* XXX */ 1250127468Ssimokawa printf("%s: xfer->resp = %d\n", __func__, xfer->resp); 1251121185Ssimokawa } 1252121185Ssimokawa sbp_xfer_free(xfer); 1253127468Ssimokawa sdev->flags &= ~ORB_DOORBELL_ACTIVE; 1254127468Ssimokawa if ((sdev->flags & ORB_DOORBELL_NEED) != 0) { 1255127468Ssimokawa sdev->flags &= ~ORB_DOORBELL_NEED; 1256127468Ssimokawa sbp_doorbell(sdev); 1257127468Ssimokawa } 1258121185Ssimokawa return; 1259121185Ssimokawa} 1260121185Ssimokawa 1261121185Ssimokawastatic void 1262103285Sikobsbp_doorbell(struct sbp_dev *sdev) 1263103285Sikob{ 1264103285Sikob struct fw_xfer *xfer; 1265103285Sikob struct fw_pkt *fp; 1266103285SikobSBP_DEBUG(1) 1267103285Sikob sbp_show_sdev_info(sdev, 2); 1268103285Sikob printf("sbp_doorbell\n"); 1269103285SikobEND_DEBUG 1270103285Sikob 1271127468Ssimokawa if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) { 1272127468Ssimokawa sdev->flags |= ORB_DOORBELL_NEED; 1273127468Ssimokawa return; 1274127468Ssimokawa } 1275127468Ssimokawa sdev->flags |= ORB_DOORBELL_ACTIVE; 1276103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 1277103285Sikob if (xfer == NULL) 1278103285Sikob return; 1279167632Ssimokawa xfer->hand = sbp_doorbell_callback; 1280127468Ssimokawa fp = &xfer->send.hdr; 1281103285Sikob fp->mode.wreqq.data = htonl(0xf); 1282103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1283103285Sikob} 1284103285Sikob 1285103285Sikobstatic struct fw_xfer * 1286103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1287103285Sikob{ 1288103285Sikob struct fw_xfer *xfer; 1289103285Sikob struct fw_pkt *fp; 1290113584Ssimokawa struct sbp_target *target; 1291113584Ssimokawa int s, new = 0; 1292103285Sikob 1293113584Ssimokawa target = sdev->target; 1294113584Ssimokawa s = splfw(); 1295113584Ssimokawa xfer = STAILQ_FIRST(&target->xferlist); 1296113584Ssimokawa if (xfer == NULL) { 1297113584Ssimokawa if (target->n_xfer > 5 /* XXX */) { 1298113584Ssimokawa printf("sbp: no more xfer for this target\n"); 1299113584Ssimokawa splx(s); 1300113584Ssimokawa return(NULL); 1301113584Ssimokawa } 1302120660Ssimokawa xfer = fw_xfer_alloc_buf(M_SBP, 8, 0); 1303113584Ssimokawa if(xfer == NULL){ 1304113584Ssimokawa printf("sbp: fw_xfer_alloc_buf failed\n"); 1305113584Ssimokawa splx(s); 1306113584Ssimokawa return NULL; 1307113584Ssimokawa } 1308113584Ssimokawa target->n_xfer ++; 1309113584Ssimokawa if (debug) 1310113584Ssimokawa printf("sbp: alloc %d xfer\n", target->n_xfer); 1311113584Ssimokawa new = 1; 1312113584Ssimokawa } else { 1313113584Ssimokawa STAILQ_REMOVE_HEAD(&target->xferlist, link); 1314103285Sikob } 1315113584Ssimokawa splx(s); 1316113584Ssimokawa 1317113584Ssimokawa microtime(&xfer->tv); 1318113584Ssimokawa 1319113584Ssimokawa if (new) { 1320120660Ssimokawa xfer->recv.pay_len = 0; 1321120660Ssimokawa xfer->send.spd = min(sdev->target->fwdev->speed, max_speed); 1322113584Ssimokawa xfer->fc = sdev->target->sbp->fd.fc; 1323103285Sikob } 1324120660Ssimokawa 1325120660Ssimokawa if (tcode == FWTCODE_WREQB) 1326120660Ssimokawa xfer->send.pay_len = 8; 1327120660Ssimokawa else 1328120660Ssimokawa xfer->send.pay_len = 0; 1329120660Ssimokawa 1330103285Sikob xfer->sc = (caddr_t)sdev; 1331120660Ssimokawa fp = &xfer->send.hdr; 1332113584Ssimokawa fp->mode.wreqq.dest_hi = sdev->login->cmd_hi; 1333113584Ssimokawa fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset; 1334103285Sikob fp->mode.wreqq.tlrt = 0; 1335103285Sikob fp->mode.wreqq.tcode = tcode; 1336103285Sikob fp->mode.wreqq.pri = 0; 1337120660Ssimokawa fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst; 1338103285Sikob 1339103285Sikob return xfer; 1340103285Sikob 1341103285Sikob} 1342103285Sikob 1343103285Sikobstatic void 1344111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb) 1345103285Sikob{ 1346103285Sikob struct fw_xfer *xfer; 1347103285Sikob struct fw_pkt *fp; 1348103285Sikob struct sbp_ocb *ocb; 1349111615Ssimokawa struct sbp_target *target; 1350103285Sikob int s, nid; 1351103285Sikob 1352111615Ssimokawa target = sdev->target; 1353111615Ssimokawa nid = target->sbp->fd.fc->nodeid | FWLOCALBUS; 1354111615Ssimokawa 1355111615Ssimokawa s = splfw(); 1356111615Ssimokawa if (func == ORB_FUN_RUNQUEUE) { 1357111615Ssimokawa ocb = STAILQ_FIRST(&target->mgm_ocb_queue); 1358111615Ssimokawa if (target->mgm_ocb_cur != NULL || ocb == NULL) { 1359111615Ssimokawa splx(s); 1360111615Ssimokawa return; 1361111615Ssimokawa } 1362111615Ssimokawa STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb); 1363111615Ssimokawa goto start; 1364111615Ssimokawa } 1365113584Ssimokawa if ((ocb = sbp_get_ocb(sdev)) == NULL) { 1366103285Sikob splx(s); 1367122528Ssimokawa /* XXX */ 1368103285Sikob return; 1369103285Sikob } 1370103285Sikob ocb->flags = OCB_ACT_MGM; 1371103285Sikob ocb->sdev = sdev; 1372103285Sikob 1373120660Ssimokawa bzero((void *)ocb->orb, sizeof(ocb->orb)); 1374103285Sikob ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1375120660Ssimokawa ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id)); 1376103285Sikob 1377107653SsimokawaSBP_DEBUG(0) 1378103285Sikob sbp_show_sdev_info(sdev, 2); 1379103285Sikob printf("%s\n", orb_fun_name[(func>>16)&0xf]); 1380107653SsimokawaEND_DEBUG 1381103285Sikob switch (func) { 1382103285Sikob case ORB_FUN_LGI: 1383120660Ssimokawa ocb->orb[0] = ocb->orb[1] = 0; /* password */ 1384103285Sikob ocb->orb[2] = htonl(nid << 16); 1385113584Ssimokawa ocb->orb[3] = htonl(sdev->dma.bus_addr); 1386121792Ssimokawa ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id); 1387122387Ssimokawa if (ex_login) 1388121792Ssimokawa ocb->orb[4] |= htonl(ORB_EXV); 1389113584Ssimokawa ocb->orb[5] = htonl(SBP_LOGIN_SIZE); 1390113584Ssimokawa fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD); 1391103285Sikob break; 1392110336Ssimokawa case ORB_FUN_ATA: 1393111615Ssimokawa ocb->orb[0] = htonl((0 << 16) | 0); 1394113584Ssimokawa ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff); 1395110336Ssimokawa /* fall through */ 1396103285Sikob case ORB_FUN_RCN: 1397103285Sikob case ORB_FUN_LGO: 1398103285Sikob case ORB_FUN_LUR: 1399103285Sikob case ORB_FUN_RST: 1400103285Sikob case ORB_FUN_ATS: 1401113584Ssimokawa ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id); 1402103285Sikob break; 1403103285Sikob } 1404103285Sikob 1405111615Ssimokawa if (target->mgm_ocb_cur != NULL) { 1406111615Ssimokawa /* there is a standing ORB */ 1407111615Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb); 1408111615Ssimokawa splx(s); 1409111615Ssimokawa return; 1410111615Ssimokawa } 1411111615Ssimokawastart: 1412111615Ssimokawa target->mgm_ocb_cur = ocb; 1413111615Ssimokawa splx(s); 1414111615Ssimokawa 1415111615Ssimokawa callout_reset(&target->mgm_ocb_timeout, 5*hz, 1416114732Ssimokawa sbp_mgm_timeout, (caddr_t)ocb); 1417103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1418103285Sikob if(xfer == NULL){ 1419103285Sikob return; 1420103285Sikob } 1421167632Ssimokawa xfer->hand = sbp_mgm_callback; 1422103285Sikob 1423120660Ssimokawa fp = &xfer->send.hdr; 1424113584Ssimokawa fp->mode.wreqb.dest_hi = sdev->target->mgm_hi; 1425113584Ssimokawa fp->mode.wreqb.dest_lo = sdev->target->mgm_lo; 1426113584Ssimokawa fp->mode.wreqb.len = 8; 1427103285Sikob fp->mode.wreqb.extcode = 0; 1428120660Ssimokawa xfer->send.payload[0] = htonl(nid << 16); 1429120660Ssimokawa xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff); 1430120660SsimokawaSBP_DEBUG(0) 1431120660Ssimokawa sbp_show_sdev_info(sdev, 2); 1432129585Sdfr printf("mgm orb: %08x\n", (uint32_t)ocb->bus_addr); 1433120660SsimokawaEND_DEBUG 1434103285Sikob 1435103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1436103285Sikob} 1437103285Sikob 1438103285Sikobstatic void 1439105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb) 1440103285Sikob{ 1441103285Sikob struct ccb_scsiio *csio; 1442103285Sikob 1443103285Sikob csio = &ocb->ccb->csio; 1444103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1445103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1446103285Sikob ", flags: 0x%02x, " 1447103285Sikob "%db cmd/%db data/%db sense\n", 1448103285Sikob device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1449103285Sikob ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1450103285Sikob csio->cdb_io.cdb_bytes[0], 1451103285Sikob csio->cdb_io.cdb_bytes[1], 1452103285Sikob csio->cdb_io.cdb_bytes[2], 1453103285Sikob csio->cdb_io.cdb_bytes[3], 1454103285Sikob csio->cdb_io.cdb_bytes[4], 1455103285Sikob csio->cdb_io.cdb_bytes[5], 1456103285Sikob csio->cdb_io.cdb_bytes[6], 1457103285Sikob csio->cdb_io.cdb_bytes[7], 1458103285Sikob csio->cdb_io.cdb_bytes[8], 1459103285Sikob csio->cdb_io.cdb_bytes[9], 1460103285Sikob ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1461103285Sikob csio->cdb_len, csio->dxfer_len, 1462103285Sikob csio->sense_len); 1463105792Ssimokawa} 1464105792Ssimokawa 1465105792Ssimokawastatic void 1466105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1467105792Ssimokawa{ 1468105792Ssimokawa struct sbp_cmd_status *sbp_cmd_status; 1469105792Ssimokawa struct scsi_sense_data *sense; 1470105792Ssimokawa 1471105792Ssimokawa sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1472105792Ssimokawa sense = &ocb->ccb->csio.sense_data; 1473105792Ssimokawa 1474105792SsimokawaSBP_DEBUG(0) 1475105792Ssimokawa sbp_print_scsi_cmd(ocb); 1476103285Sikob /* XXX need decode status */ 1477103285Sikob sbp_show_sdev_info(ocb->sdev, 2); 1478113584Ssimokawa printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n", 1479103285Sikob sbp_cmd_status->status, 1480103285Sikob sbp_cmd_status->sfmt, 1481103285Sikob sbp_cmd_status->valid, 1482103285Sikob sbp_cmd_status->s_key, 1483103285Sikob sbp_cmd_status->s_code, 1484103285Sikob sbp_cmd_status->s_qlfr, 1485103285Sikob sbp_status->len 1486103285Sikob ); 1487103285SikobEND_DEBUG 1488103285Sikob 1489110071Ssimokawa switch (sbp_cmd_status->status) { 1490110071Ssimokawa case SCSI_STATUS_CHECK_COND: 1491110071Ssimokawa case SCSI_STATUS_BUSY: 1492110071Ssimokawa case SCSI_STATUS_CMD_TERMINATED: 1493103285Sikob if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1494103285Sikob sense->error_code = SSD_CURRENT_ERROR; 1495103285Sikob }else{ 1496103285Sikob sense->error_code = SSD_DEFERRED_ERROR; 1497103285Sikob } 1498103285Sikob if(sbp_cmd_status->valid) 1499103285Sikob sense->error_code |= SSD_ERRCODE_VALID; 1500103285Sikob sense->flags = sbp_cmd_status->s_key; 1501103285Sikob if(sbp_cmd_status->mark) 1502103285Sikob sense->flags |= SSD_FILEMARK; 1503103285Sikob if(sbp_cmd_status->eom) 1504103285Sikob sense->flags |= SSD_EOM; 1505103285Sikob if(sbp_cmd_status->ill_len) 1506103285Sikob sense->flags |= SSD_ILI; 1507119556Ssimokawa 1508119556Ssimokawa bcopy(&sbp_cmd_status->info, &sense->info[0], 4); 1509119556Ssimokawa 1510103285Sikob if (sbp_status->len <= 1) 1511103285Sikob /* XXX not scsi status. shouldn't be happened */ 1512103285Sikob sense->extra_len = 0; 1513103285Sikob else if (sbp_status->len <= 4) 1514103285Sikob /* add_sense_code(_qual), info, cmd_spec_info */ 1515103285Sikob sense->extra_len = 6; 1516103285Sikob else 1517103285Sikob /* fru, sense_key_spec */ 1518103285Sikob sense->extra_len = 10; 1519119556Ssimokawa 1520119556Ssimokawa bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4); 1521119556Ssimokawa 1522103285Sikob sense->add_sense_code = sbp_cmd_status->s_code; 1523103285Sikob sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1524103285Sikob sense->fru = sbp_cmd_status->fru; 1525103285Sikob 1526119556Ssimokawa bcopy(&sbp_cmd_status->s_keydep[0], 1527119556Ssimokawa &sense->sense_key_spec[0], 3); 1528119556Ssimokawa 1529103285Sikob ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1530103285Sikob ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1531103285Sikob | CAM_AUTOSNS_VALID; 1532103285Sikob/* 1533103285Sikob{ 1534129585Sdfr uint8_t j, *tmp; 1535103285Sikob tmp = sense; 1536103285Sikob for( j = 0 ; j < 32 ; j+=8){ 1537103285Sikob printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1538103285Sikob tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1539103285Sikob tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1540103285Sikob } 1541103285Sikob 1542103285Sikob} 1543103285Sikob*/ 1544110071Ssimokawa break; 1545110071Ssimokawa default: 1546110071Ssimokawa sbp_show_sdev_info(ocb->sdev, 2); 1547110071Ssimokawa printf("sbp_scsi_status: unknown scsi status 0x%x\n", 1548110071Ssimokawa sbp_cmd_status->status); 1549103285Sikob } 1550103285Sikob} 1551103285Sikob 1552103285Sikobstatic void 1553103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb) 1554103285Sikob{ 1555103285Sikob union ccb *ccb; 1556103285Sikob struct sbp_dev *sdev; 1557103285Sikob struct scsi_inquiry_data *inq; 1558103285Sikob 1559103285Sikob ccb = ocb->ccb; 1560103285Sikob sdev = ocb->sdev; 1561103285Sikob 1562103285Sikob if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1563103285Sikob return; 1564103285SikobSBP_DEBUG(1) 1565103285Sikob sbp_show_sdev_info(sdev, 2); 1566103285Sikob printf("sbp_fix_inq_data\n"); 1567103285SikobEND_DEBUG 1568103285Sikob inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1569103285Sikob switch (SID_TYPE(inq)) { 1570103285Sikob case T_DIRECT: 1571118291Ssimokawa#if 0 1572103285Sikob /* 1573103285Sikob * XXX Convert Direct Access device to RBC. 1574108281Ssimokawa * I've never seen FireWire DA devices which support READ_6. 1575103285Sikob */ 1576103285Sikob if (SID_TYPE(inq) == T_DIRECT) 1577103285Sikob inq->device |= T_RBC; /* T_DIRECT == 0 */ 1578103285Sikob#endif 1579103285Sikob /* fall through */ 1580103285Sikob case T_RBC: 1581103285Sikob /* 1582103285Sikob * Override vendor/product/revision information. 1583103285Sikob * Some devices sometimes return strange strings. 1584103285Sikob */ 1585111615Ssimokawa#if 1 1586103285Sikob bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1587103285Sikob bcopy(sdev->product, inq->product, sizeof(inq->product)); 1588103285Sikob bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1589111615Ssimokawa#endif 1590103285Sikob break; 1591103285Sikob } 1592127468Ssimokawa /* 1593127468Ssimokawa * Force to enable/disable tagged queuing. 1594127468Ssimokawa * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page. 1595127468Ssimokawa */ 1596127468Ssimokawa if (sbp_tags > 0) 1597127468Ssimokawa inq->flags |= SID_CmdQue; 1598127468Ssimokawa else if (sbp_tags < 0) 1599127468Ssimokawa inq->flags &= ~SID_CmdQue; 1600127468Ssimokawa 1601103285Sikob} 1602103285Sikob 1603103285Sikobstatic void 1604113584Ssimokawasbp_recv1(struct fw_xfer *xfer) 1605113584Ssimokawa{ 1606103285Sikob struct fw_pkt *rfp; 1607103285Sikob#if NEED_RESPONSE 1608103285Sikob struct fw_pkt *sfp; 1609103285Sikob#endif 1610103285Sikob struct sbp_softc *sbp; 1611103285Sikob struct sbp_dev *sdev; 1612103285Sikob struct sbp_ocb *ocb; 1613103285Sikob struct sbp_login_res *login_res = NULL; 1614103285Sikob struct sbp_status *sbp_status; 1615103285Sikob struct sbp_target *target; 1616111704Ssimokawa int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0; 1617129585Sdfr uint32_t addr; 1618103285Sikob/* 1619129585Sdfr uint32_t *ld; 1620103285Sikob ld = xfer->recv.buf; 1621103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n", 1622103285Sikob xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1623103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1624103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1625103285Sikob*/ 1626113584Ssimokawa sbp = (struct sbp_softc *)xfer->sc; 1627120660Ssimokawa if (xfer->resp != 0){ 1628122529Ssimokawa printf("sbp_recv: xfer->resp = %d\n", xfer->resp); 1629113584Ssimokawa goto done0; 1630103285Sikob } 1631120660Ssimokawa if (xfer->recv.payload == NULL){ 1632120660Ssimokawa printf("sbp_recv: xfer->recv.payload == NULL\n"); 1633113584Ssimokawa goto done0; 1634103285Sikob } 1635120660Ssimokawa rfp = &xfer->recv.hdr; 1636103285Sikob if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1637103285Sikob printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1638113584Ssimokawa goto done0; 1639103285Sikob } 1640120660Ssimokawa sbp_status = (struct sbp_status *)xfer->recv.payload; 1641113584Ssimokawa addr = rfp->mode.wreqb.dest_lo; 1642103285SikobSBP_DEBUG(2) 1643103285Sikob printf("received address 0x%x\n", addr); 1644103285SikobEND_DEBUG 1645110189Ssimokawa t = SBP_ADDR2TRG(addr); 1646110189Ssimokawa if (t >= SBP_NUM_TARGETS) { 1647110189Ssimokawa device_printf(sbp->fd.dev, 1648110189Ssimokawa "sbp_recv1: invalid target %d\n", t); 1649113584Ssimokawa goto done0; 1650110189Ssimokawa } 1651110189Ssimokawa target = &sbp->targets[t]; 1652110189Ssimokawa l = SBP_ADDR2LUN(addr); 1653121185Ssimokawa if (l >= target->num_lun || target->luns[l] == NULL) { 1654110189Ssimokawa device_printf(sbp->fd.dev, 1655110189Ssimokawa "sbp_recv1: invalid lun %d (target=%d)\n", l, t); 1656113584Ssimokawa goto done0; 1657110189Ssimokawa } 1658121185Ssimokawa sdev = target->luns[l]; 1659103285Sikob 1660110189Ssimokawa ocb = NULL; 1661110189Ssimokawa switch (sbp_status->src) { 1662110189Ssimokawa case 0: 1663110189Ssimokawa case 1: 1664111615Ssimokawa /* check mgm_ocb_cur first */ 1665111615Ssimokawa ocb = target->mgm_ocb_cur; 1666111615Ssimokawa if (ocb != NULL) { 1667111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 1668111615Ssimokawa callout_stop(&target->mgm_ocb_timeout); 1669111615Ssimokawa target->mgm_ocb_cur = NULL; 1670111615Ssimokawa break; 1671111615Ssimokawa } 1672111615Ssimokawa } 1673111615Ssimokawa ocb = sbp_dequeue_ocb(sdev, sbp_status); 1674110189Ssimokawa if (ocb == NULL) { 1675110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1676127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1677127468Ssimokawa printf("No ocb(%lx) on the queue\n", 1678127468Ssimokawa#else 1679111819Ssimokawa printf("No ocb(%x) on the queue\n", 1680111858Ssimokawa#endif 1681111819Ssimokawa ntohl(sbp_status->orb_lo)); 1682110189Ssimokawa } 1683110189Ssimokawa break; 1684110189Ssimokawa case 2: 1685110189Ssimokawa /* unsolicit */ 1686110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1687110189Ssimokawa printf("unsolicit status received\n"); 1688110189Ssimokawa break; 1689110189Ssimokawa default: 1690110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1691110189Ssimokawa printf("unknown sbp_status->src\n"); 1692110189Ssimokawa } 1693110189Ssimokawa 1694111615Ssimokawa status_valid0 = (sbp_status->src < 2 1695110189Ssimokawa && sbp_status->resp == ORB_RES_CMPL 1696111615Ssimokawa && sbp_status->dead == 0); 1697111615Ssimokawa status_valid = (status_valid0 && sbp_status->status == 0); 1698103285Sikob 1699121185Ssimokawa if (!status_valid0 || debug > 2){ 1700103285Sikob int status; 1701110129SsimokawaSBP_DEBUG(0) 1702103285Sikob sbp_show_sdev_info(sdev, 2); 1703103285Sikob printf("ORB status src:%x resp:%x dead:%x" 1704127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1705127468Ssimokawa " len:%x stat:%x orb:%x%08lx\n", 1706127468Ssimokawa#else 1707103285Sikob " len:%x stat:%x orb:%x%08x\n", 1708108712Ssimokawa#endif 1709103285Sikob sbp_status->src, sbp_status->resp, sbp_status->dead, 1710103285Sikob sbp_status->len, sbp_status->status, 1711108280Ssimokawa ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1712110129SsimokawaEND_DEBUG 1713103285Sikob sbp_show_sdev_info(sdev, 2); 1714103285Sikob status = sbp_status->status; 1715103285Sikob switch(sbp_status->resp) { 1716103285Sikob case 0: 1717103285Sikob if (status > MAX_ORB_STATUS0) 1718103285Sikob printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1719110189Ssimokawa else 1720110129Ssimokawa printf("%s\n", orb_status0[status]); 1721103285Sikob break; 1722103285Sikob case 1: 1723110336Ssimokawa printf("Obj: %s, Error: %s\n", 1724103285Sikob orb_status1_object[(status>>6) & 3], 1725103285Sikob orb_status1_serial_bus_error[status & 0xf]); 1726103285Sikob break; 1727110129Ssimokawa case 2: 1728110129Ssimokawa printf("Illegal request\n"); 1729110129Ssimokawa break; 1730110129Ssimokawa case 3: 1731110129Ssimokawa printf("Vendor dependent\n"); 1732110129Ssimokawa break; 1733103285Sikob default: 1734110129Ssimokawa printf("unknown respose code %d\n", sbp_status->resp); 1735103285Sikob } 1736103285Sikob } 1737103285Sikob 1738103285Sikob /* we have to reset the fetch agent if it's dead */ 1739103285Sikob if (sbp_status->dead) { 1740111615Ssimokawa if (sdev->path) { 1741103285Sikob xpt_freeze_devq(sdev->path, 1); 1742111615Ssimokawa sdev->freeze ++; 1743111615Ssimokawa } 1744111704Ssimokawa reset_agent = 1; 1745103285Sikob } 1746103285Sikob 1747111704Ssimokawa if (ocb == NULL) 1748111704Ssimokawa goto done; 1749103285Sikob 1750103285Sikob switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1751103285Sikob case ORB_FMT_NOP: 1752103285Sikob break; 1753103285Sikob case ORB_FMT_VED: 1754103285Sikob break; 1755103285Sikob case ORB_FMT_STD: 1756111819Ssimokawa switch(ocb->flags) { 1757103285Sikob case OCB_ACT_MGM: 1758103285Sikob orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1759120842Ssimokawa reset_agent = 0; 1760103285Sikob switch(orb_fun) { 1761103285Sikob case ORB_FUN_LGI: 1762113584Ssimokawa fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD); 1763113584Ssimokawa login_res = sdev->login; 1764103285Sikob login_res->len = ntohs(login_res->len); 1765103285Sikob login_res->id = ntohs(login_res->id); 1766103285Sikob login_res->cmd_hi = ntohs(login_res->cmd_hi); 1767103285Sikob login_res->cmd_lo = ntohl(login_res->cmd_lo); 1768103285Sikob if (status_valid) { 1769103285SikobSBP_DEBUG(0) 1770103285Sikobsbp_show_sdev_info(sdev, 2); 1771103285Sikobprintf("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)); 1772103285SikobEND_DEBUG 1773103285Sikob sbp_busy_timeout(sdev); 1774103285Sikob } else { 1775110336Ssimokawa /* forgot logout? */ 1776110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1777103285Sikob printf("login failed\n"); 1778103285Sikob sdev->status = SBP_DEV_RESET; 1779103285Sikob } 1780103285Sikob break; 1781103285Sikob case ORB_FUN_RCN: 1782113584Ssimokawa login_res = sdev->login; 1783103285Sikob if (status_valid) { 1784103285SikobSBP_DEBUG(0) 1785103285Sikobsbp_show_sdev_info(sdev, 2); 1786103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1787103285SikobEND_DEBUG 1788103285Sikob#if 1 1789111615Ssimokawa if (sdev->status == SBP_DEV_ATTACHED) 1790111615Ssimokawa sbp_scan_dev(sdev); 1791111615Ssimokawa else 1792111615Ssimokawa sbp_agent_reset(sdev); 1793103285Sikob#else 1794110336Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1795111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL); 1796103285Sikob#endif 1797103285Sikob } else { 1798103285Sikob /* reconnection hold time exceed? */ 1799110336SsimokawaSBP_DEBUG(0) 1800110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1801103285Sikob printf("reconnect failed\n"); 1802110336SsimokawaEND_DEBUG 1803111615Ssimokawa sbp_login(sdev); 1804103285Sikob } 1805103285Sikob break; 1806103285Sikob case ORB_FUN_LGO: 1807103285Sikob sdev->status = SBP_DEV_RESET; 1808103285Sikob break; 1809110336Ssimokawa case ORB_FUN_RST: 1810110336Ssimokawa sbp_busy_timeout(sdev); 1811110336Ssimokawa break; 1812103285Sikob case ORB_FUN_LUR: 1813103285Sikob case ORB_FUN_ATA: 1814103285Sikob case ORB_FUN_ATS: 1815110336Ssimokawa sbp_agent_reset(sdev); 1816103285Sikob break; 1817103285Sikob default: 1818110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1819110336Ssimokawa printf("unknown function %d\n", orb_fun); 1820103285Sikob break; 1821103285Sikob } 1822111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 1823103285Sikob break; 1824103285Sikob case OCB_ACT_CMD: 1825114732Ssimokawa sdev->timeout = 0; 1826103285Sikob if(ocb->ccb != NULL){ 1827103285Sikob union ccb *ccb; 1828103285Sikob/* 1829129585Sdfr uint32_t *ld; 1830103285Sikob ld = ocb->ccb->csio.data_ptr; 1831103285Sikob if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1832103285Sikob printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1833103285Sikob else 1834103285Sikob printf("ptr NULL\n"); 1835103285Sikobprintf("len %d\n", sbp_status->len); 1836103285Sikob*/ 1837103285Sikob ccb = ocb->ccb; 1838103285Sikob if(sbp_status->len > 1){ 1839103285Sikob sbp_scsi_status(sbp_status, ocb); 1840103285Sikob }else{ 1841103285Sikob if(sbp_status->resp != ORB_RES_CMPL){ 1842103285Sikob ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1843103285Sikob }else{ 1844103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1845103285Sikob } 1846103285Sikob } 1847103285Sikob /* fix up inq data */ 1848103285Sikob if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1849103285Sikob sbp_fix_inq_data(ocb); 1850103285Sikob xpt_done(ccb); 1851103285Sikob } 1852103285Sikob break; 1853103285Sikob default: 1854103285Sikob break; 1855103285Sikob } 1856103285Sikob } 1857103285Sikob 1858127468Ssimokawa if (!use_doorbell) 1859127468Ssimokawa sbp_free_ocb(sdev, ocb); 1860111704Ssimokawadone: 1861111704Ssimokawa if (reset_agent) 1862111704Ssimokawa sbp_agent_reset(sdev); 1863103285Sikob 1864113584Ssimokawadone0: 1865120660Ssimokawa xfer->recv.pay_len = SBP_RECV_LEN; 1866103285Sikob/* The received packet is usually small enough to be stored within 1867103285Sikob * the buffer. In that case, the controller return ack_complete and 1868103285Sikob * no respose is necessary. 1869103285Sikob * 1870103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as 1871103285Sikob * ack_complete or ack_pending to upper driver. 1872103285Sikob */ 1873103285Sikob#if NEED_RESPONSE 1874103285Sikob xfer->send.off = 0; 1875103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1876103285Sikob sfp->mode.wres.dst = rfp->mode.wreqb.src; 1877113584Ssimokawa xfer->dst = sfp->mode.wres.dst; 1878103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1879167632Ssimokawa xfer->hand = sbp_loginres_callback; 1880103285Sikob 1881103285Sikob sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1882103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1883103285Sikob sfp->mode.wres.rtcode = 0; 1884103285Sikob sfp->mode.wres.pri = 0; 1885103285Sikob 1886103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1887103285Sikob#else 1888113584Ssimokawa /* recycle */ 1889113584Ssimokawa STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link); 1890103285Sikob#endif 1891103285Sikob 1892103285Sikob return; 1893103285Sikob 1894103285Sikob} 1895103285Sikob 1896103285Sikobstatic void 1897103285Sikobsbp_recv(struct fw_xfer *xfer) 1898103285Sikob{ 1899103285Sikob int s; 1900103285Sikob 1901103285Sikob s = splcam(); 1902103285Sikob sbp_recv1(xfer); 1903103285Sikob splx(s); 1904103285Sikob} 1905103285Sikob/* 1906103285Sikob * sbp_attach() 1907103285Sikob */ 1908103285Sikobstatic int 1909103285Sikobsbp_attach(device_t dev) 1910103285Sikob{ 1911103285Sikob struct sbp_softc *sbp; 1912103285Sikob struct cam_devq *devq; 1913103285Sikob int i, s, error; 1914103285Sikob 1915167624Ssimokawa if (DFLTPHYS > SBP_MAXPHYS) 1916167624Ssimokawa device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than " 1917167624Ssimokawa "SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024, 1918167624Ssimokawa SBP_MAXPHYS / 1024); 1919167624Ssimokawa 1920103285SikobSBP_DEBUG(0) 1921111199Ssimokawa printf("sbp_attach (cold=%d)\n", cold); 1922103285SikobEND_DEBUG 1923103285Sikob 1924111199Ssimokawa if (cold) 1925111199Ssimokawa sbp_cold ++; 1926103285Sikob sbp = ((struct sbp_softc *)device_get_softc(dev)); 1927103285Sikob bzero(sbp, sizeof(struct sbp_softc)); 1928103285Sikob sbp->fd.dev = dev; 1929103285Sikob sbp->fd.fc = device_get_ivars(dev); 1930124251Ssimokawa 1931124251Ssimokawa if (max_speed < 0) 1932124251Ssimokawa max_speed = sbp->fd.fc->speed; 1933124251Ssimokawa 1934113584Ssimokawa error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat, 1935113584Ssimokawa /* XXX shoud be 4 for sane backend? */ 1936113584Ssimokawa /*alignment*/1, 1937103285Sikob /*boundary*/0, 1938103285Sikob /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1939103285Sikob /*highaddr*/BUS_SPACE_MAXADDR, 1940103285Sikob /*filter*/NULL, /*filterarg*/NULL, 1941103285Sikob /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1942113474Ssimokawa /*maxsegsz*/SBP_SEG_MAX, 1943103285Sikob /*flags*/BUS_DMA_ALLOCNOW, 1944127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102 1945117126Sscottl /*lockfunc*/busdma_lock_mutex, 1946117228Ssimokawa /*lockarg*/&Giant, 1947117228Ssimokawa#endif 1948117228Ssimokawa &sbp->dmat); 1949103285Sikob if (error != 0) { 1950103285Sikob printf("sbp_attach: Could not allocate DMA tag " 1951103285Sikob "- error %d\n", error); 1952103285Sikob return (ENOMEM); 1953103285Sikob } 1954103285Sikob 1955103285Sikob devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1956103285Sikob if (devq == NULL) 1957103285Sikob return (ENXIO); 1958103285Sikob 1959103285Sikob for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1960103285Sikob sbp->targets[i].fwdev = NULL; 1961103285Sikob sbp->targets[i].luns = NULL; 1962103285Sikob } 1963103285Sikob 1964103285Sikob sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1965103285Sikob device_get_unit(dev), 1966168752Sscottl &Giant, 1967111615Ssimokawa /*untagged*/ 1, 1968122528Ssimokawa /*tagged*/ SBP_QUEUE_LEN - 1, 1969111615Ssimokawa devq); 1970103285Sikob 1971103285Sikob if (sbp->sim == NULL) { 1972103285Sikob cam_simq_free(devq); 1973103285Sikob return (ENXIO); 1974103285Sikob } 1975103285Sikob 1976103285Sikob 1977111615Ssimokawa if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) 1978111615Ssimokawa goto fail; 1979103285Sikob 1980111615Ssimokawa if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim), 1981120660Ssimokawa CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1982120660Ssimokawa xpt_bus_deregister(cam_sim_path(sbp->sim)); 1983111615Ssimokawa goto fail; 1984120660Ssimokawa } 1985111615Ssimokawa 1986103285Sikob /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1987120660Ssimokawa sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0); 1988120660Ssimokawa sbp->fwb.end = sbp->fwb.start + 0xffff; 1989113584Ssimokawa /* pre-allocate xfer */ 1990113584Ssimokawa STAILQ_INIT(&sbp->fwb.xferlist); 1991169130Ssimokawa fw_xferlist_add(&sbp->fwb.xferlist, M_SBP, 1992169130Ssimokawa /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2, 1993169130Ssimokawa sbp->fd.fc, (void *)sbp, sbp_recv); 1994103285Sikob fw_bindadd(sbp->fd.fc, &sbp->fwb); 1995103285Sikob 1996113584Ssimokawa sbp->fd.post_busreset = sbp_post_busreset; 1997103285Sikob sbp->fd.post_explore = sbp_post_explore; 1998103285Sikob 1999111199Ssimokawa if (sbp->fd.fc->status != -1) { 2000111199Ssimokawa s = splfw(); 2001122387Ssimokawa sbp_post_busreset((void *)sbp); 2002111199Ssimokawa sbp_post_explore((void *)sbp); 2003111199Ssimokawa splx(s); 2004111199Ssimokawa } 2005122387Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 2006111199Ssimokawa 2007103285Sikob return (0); 2008111615Ssimokawafail: 2009111615Ssimokawa cam_sim_free(sbp->sim, /*free_devq*/TRUE); 2010111615Ssimokawa return (ENXIO); 2011103285Sikob} 2012103285Sikob 2013103285Sikobstatic int 2014110145Ssimokawasbp_logout_all(struct sbp_softc *sbp) 2015110145Ssimokawa{ 2016110145Ssimokawa struct sbp_target *target; 2017110145Ssimokawa struct sbp_dev *sdev; 2018110145Ssimokawa int i, j; 2019110145Ssimokawa 2020110145SsimokawaSBP_DEBUG(0) 2021110145Ssimokawa printf("sbp_logout_all\n"); 2022110145SsimokawaEND_DEBUG 2023110145Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) { 2024110145Ssimokawa target = &sbp->targets[i]; 2025110145Ssimokawa if (target->luns == NULL) 2026110145Ssimokawa continue; 2027110145Ssimokawa for (j = 0; j < target->num_lun; j++) { 2028121185Ssimokawa sdev = target->luns[j]; 2029121185Ssimokawa if (sdev == NULL) 2030121185Ssimokawa continue; 2031113584Ssimokawa callout_stop(&sdev->login_callout); 2032110336Ssimokawa if (sdev->status >= SBP_DEV_TOATTACH && 2033110336Ssimokawa sdev->status <= SBP_DEV_ATTACHED) 2034111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL); 2035110145Ssimokawa } 2036110145Ssimokawa } 2037113584Ssimokawa 2038110145Ssimokawa return 0; 2039110145Ssimokawa} 2040110145Ssimokawa 2041110145Ssimokawastatic int 2042110145Ssimokawasbp_shutdown(device_t dev) 2043110145Ssimokawa{ 2044110145Ssimokawa struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 2045110145Ssimokawa 2046110145Ssimokawa sbp_logout_all(sbp); 2047110145Ssimokawa return (0); 2048110145Ssimokawa} 2049110145Ssimokawa 2050121185Ssimokawastatic void 2051121185Ssimokawasbp_free_sdev(struct sbp_dev *sdev) 2052121185Ssimokawa{ 2053121185Ssimokawa int i; 2054121185Ssimokawa 2055121185Ssimokawa if (sdev == NULL) 2056121185Ssimokawa return; 2057121185Ssimokawa for (i = 0; i < SBP_QUEUE_LEN; i++) 2058121185Ssimokawa bus_dmamap_destroy(sdev->target->sbp->dmat, 2059121185Ssimokawa sdev->ocb[i].dmamap); 2060121185Ssimokawa fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma); 2061121185Ssimokawa free(sdev, M_SBP); 2062121185Ssimokawa} 2063121185Ssimokawa 2064121185Ssimokawastatic void 2065121185Ssimokawasbp_free_target(struct sbp_target *target) 2066121185Ssimokawa{ 2067121185Ssimokawa struct sbp_softc *sbp; 2068121185Ssimokawa struct fw_xfer *xfer, *next; 2069121185Ssimokawa int i; 2070121185Ssimokawa 2071121185Ssimokawa if (target->luns == NULL) 2072121185Ssimokawa return; 2073121185Ssimokawa callout_stop(&target->mgm_ocb_timeout); 2074121185Ssimokawa sbp = target->sbp; 2075121185Ssimokawa for (i = 0; i < target->num_lun; i++) 2076121185Ssimokawa sbp_free_sdev(target->luns[i]); 2077121185Ssimokawa 2078121185Ssimokawa for (xfer = STAILQ_FIRST(&target->xferlist); 2079121185Ssimokawa xfer != NULL; xfer = next) { 2080121185Ssimokawa next = STAILQ_NEXT(xfer, link); 2081121185Ssimokawa fw_xfer_free_buf(xfer); 2082121185Ssimokawa } 2083121185Ssimokawa STAILQ_INIT(&target->xferlist); 2084121185Ssimokawa free(target->luns, M_SBP); 2085121185Ssimokawa target->num_lun = 0;; 2086121185Ssimokawa target->luns = NULL; 2087121185Ssimokawa target->fwdev = NULL; 2088121185Ssimokawa} 2089121185Ssimokawa 2090110145Ssimokawastatic int 2091103285Sikobsbp_detach(device_t dev) 2092103285Sikob{ 2093103285Sikob struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 2094103285Sikob struct firewire_comm *fc = sbp->fd.fc; 2095121185Ssimokawa int i; 2096103285Sikob 2097103285SikobSBP_DEBUG(0) 2098103285Sikob printf("sbp_detach\n"); 2099103285SikobEND_DEBUG 2100113584Ssimokawa 2101103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i ++) 2102110145Ssimokawa sbp_cam_detach_target(&sbp->targets[i]); 2103120660Ssimokawa xpt_async(AC_LOST_DEVICE, sbp->path, NULL); 2104111615Ssimokawa xpt_free_path(sbp->path); 2105103285Sikob xpt_bus_deregister(cam_sim_path(sbp->sim)); 2106120660Ssimokawa cam_sim_free(sbp->sim, /*free_devq*/ TRUE), 2107110145Ssimokawa 2108110145Ssimokawa sbp_logout_all(sbp); 2109113584Ssimokawa 2110110145Ssimokawa /* XXX wait for logout completion */ 2111167086Sjhb pause("sbpdtc", hz/2); 2112110145Ssimokawa 2113121185Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) 2114121185Ssimokawa sbp_free_target(&sbp->targets[i]); 2115113584Ssimokawa 2116110145Ssimokawa fw_bindremove(fc, &sbp->fwb); 2117169130Ssimokawa fw_xferlist_remove(&sbp->fwb.xferlist); 2118113584Ssimokawa 2119103285Sikob bus_dma_tag_destroy(sbp->dmat); 2120110145Ssimokawa 2121103285Sikob return (0); 2122103285Sikob} 2123103285Sikob 2124103285Sikobstatic void 2125121185Ssimokawasbp_cam_detach_sdev(struct sbp_dev *sdev) 2126121185Ssimokawa{ 2127121185Ssimokawa if (sdev == NULL) 2128121185Ssimokawa return; 2129121185Ssimokawa if (sdev->status == SBP_DEV_DEAD) 2130121185Ssimokawa return; 2131121185Ssimokawa if (sdev->status == SBP_DEV_RESET) 2132121185Ssimokawa return; 2133121185Ssimokawa if (sdev->path) { 2134121185Ssimokawa xpt_release_devq(sdev->path, 2135121185Ssimokawa sdev->freeze, TRUE); 2136121185Ssimokawa sdev->freeze = 0; 2137121185Ssimokawa xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 2138121185Ssimokawa xpt_free_path(sdev->path); 2139121185Ssimokawa sdev->path = NULL; 2140121185Ssimokawa } 2141121185Ssimokawa sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 2142121185Ssimokawa} 2143121185Ssimokawa 2144121185Ssimokawastatic void 2145110145Ssimokawasbp_cam_detach_target(struct sbp_target *target) 2146103285Sikob{ 2147103285Sikob int i; 2148103285Sikob 2149103285Sikob if (target->luns != NULL) { 2150108529SsimokawaSBP_DEBUG(0) 2151103285Sikob printf("sbp_detach_target %d\n", target->target_id); 2152108529SsimokawaEND_DEBUG 2153111615Ssimokawa callout_stop(&target->scan_callout); 2154121185Ssimokawa for (i = 0; i < target->num_lun; i++) 2155121185Ssimokawa sbp_cam_detach_sdev(target->luns[i]); 2156103285Sikob } 2157103285Sikob} 2158103285Sikob 2159103285Sikobstatic void 2160114732Ssimokawasbp_target_reset(struct sbp_dev *sdev, int method) 2161114732Ssimokawa{ 2162114732Ssimokawa int i; 2163114732Ssimokawa struct sbp_target *target = sdev->target; 2164114732Ssimokawa struct sbp_dev *tsdev; 2165114732Ssimokawa 2166114732Ssimokawa for (i = 0; i < target->num_lun; i++) { 2167121185Ssimokawa tsdev = target->luns[i]; 2168121185Ssimokawa if (tsdev == NULL) 2169121185Ssimokawa continue; 2170114732Ssimokawa if (tsdev->status == SBP_DEV_DEAD) 2171114732Ssimokawa continue; 2172114732Ssimokawa if (tsdev->status == SBP_DEV_RESET) 2173114732Ssimokawa continue; 2174114732Ssimokawa xpt_freeze_devq(tsdev->path, 1); 2175114732Ssimokawa tsdev->freeze ++; 2176114732Ssimokawa sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT); 2177114732Ssimokawa if (method == 2) 2178114732Ssimokawa tsdev->status = SBP_DEV_LOGIN; 2179114732Ssimokawa } 2180114732Ssimokawa switch(method) { 2181114732Ssimokawa case 1: 2182114732Ssimokawa printf("target reset\n"); 2183114732Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RST, NULL); 2184114732Ssimokawa break; 2185114732Ssimokawa case 2: 2186114732Ssimokawa printf("reset start\n"); 2187114732Ssimokawa sbp_reset_start(sdev); 2188114732Ssimokawa break; 2189114732Ssimokawa } 2190114732Ssimokawa 2191114732Ssimokawa} 2192114732Ssimokawa 2193114732Ssimokawastatic void 2194114732Ssimokawasbp_mgm_timeout(void *arg) 2195114732Ssimokawa{ 2196114732Ssimokawa struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2197114732Ssimokawa struct sbp_dev *sdev = ocb->sdev; 2198114732Ssimokawa struct sbp_target *target = sdev->target; 2199114732Ssimokawa 2200114732Ssimokawa sbp_show_sdev_info(sdev, 2); 2201120842Ssimokawa printf("request timeout(mgm orb:0x%08x) ... ", 2202129585Sdfr (uint32_t)ocb->bus_addr); 2203114732Ssimokawa target->mgm_ocb_cur = NULL; 2204114732Ssimokawa sbp_free_ocb(sdev, ocb); 2205114732Ssimokawa#if 0 2206114732Ssimokawa /* XXX */ 2207121185Ssimokawa printf("run next request\n"); 2208114732Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 2209114732Ssimokawa#endif 2210121185Ssimokawa#if 1 2211121185Ssimokawa printf("reset start\n"); 2212114732Ssimokawa sbp_reset_start(sdev); 2213114732Ssimokawa#endif 2214114732Ssimokawa} 2215114732Ssimokawa 2216114732Ssimokawastatic void 2217103285Sikobsbp_timeout(void *arg) 2218103285Sikob{ 2219103285Sikob struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2220103285Sikob struct sbp_dev *sdev = ocb->sdev; 2221103285Sikob 2222103285Sikob sbp_show_sdev_info(sdev, 2); 2223120842Ssimokawa printf("request timeout(cmd orb:0x%08x) ... ", 2224129585Sdfr (uint32_t)ocb->bus_addr); 2225103285Sikob 2226114732Ssimokawa sdev->timeout ++; 2227114732Ssimokawa switch(sdev->timeout) { 2228114732Ssimokawa case 1: 2229110336Ssimokawa printf("agent reset\n"); 2230114732Ssimokawa xpt_freeze_devq(sdev->path, 1); 2231114732Ssimokawa sdev->freeze ++; 2232114732Ssimokawa sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 2233110336Ssimokawa sbp_agent_reset(sdev); 2234114732Ssimokawa break; 2235114732Ssimokawa case 2: 2236114732Ssimokawa case 3: 2237114732Ssimokawa sbp_target_reset(sdev, sdev->timeout - 1); 2238114732Ssimokawa break; 2239114732Ssimokawa#if 0 2240114732Ssimokawa default: 2241114732Ssimokawa /* XXX give up */ 2242114732Ssimokawa sbp_cam_detach_target(target); 2243114732Ssimokawa if (target->luns != NULL) 2244114732Ssimokawa free(target->luns, M_SBP); 2245114732Ssimokawa target->num_lun = 0;; 2246114732Ssimokawa target->luns = NULL; 2247114732Ssimokawa target->fwdev = NULL; 2248114732Ssimokawa#endif 2249110336Ssimokawa } 2250103285Sikob} 2251103285Sikob 2252103285Sikobstatic void 2253103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb) 2254103285Sikob{ 2255103285Sikob 2256103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 2257103285Sikob struct sbp_target *target = NULL; 2258103285Sikob struct sbp_dev *sdev = NULL; 2259103285Sikob 2260103285Sikob /* target:lun -> sdev mapping */ 2261103285Sikob if (sbp != NULL 2262103285Sikob && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 2263103285Sikob && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 2264103285Sikob target = &sbp->targets[ccb->ccb_h.target_id]; 2265103285Sikob if (target->fwdev != NULL 2266103285Sikob && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 2267103285Sikob && ccb->ccb_h.target_lun < target->num_lun) { 2268121185Ssimokawa sdev = target->luns[ccb->ccb_h.target_lun]; 2269121185Ssimokawa if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED && 2270103285Sikob sdev->status != SBP_DEV_PROBE) 2271103285Sikob sdev = NULL; 2272103285Sikob } 2273103285Sikob } 2274103285Sikob 2275103285SikobSBP_DEBUG(1) 2276103285Sikob if (sdev == NULL) 2277103285Sikob printf("invalid target %d lun %d\n", 2278103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2279103285SikobEND_DEBUG 2280103285Sikob 2281103285Sikob switch (ccb->ccb_h.func_code) { 2282103285Sikob case XPT_SCSI_IO: 2283103285Sikob case XPT_RESET_DEV: 2284103285Sikob case XPT_GET_TRAN_SETTINGS: 2285103285Sikob case XPT_SET_TRAN_SETTINGS: 2286103285Sikob case XPT_CALC_GEOMETRY: 2287103285Sikob if (sdev == NULL) { 2288103285SikobSBP_DEBUG(1) 2289103285Sikob printf("%s:%d:%d:func_code 0x%04x: " 2290103285Sikob "Invalid target (target needed)\n", 2291103285Sikob device_get_nameunit(sbp->fd.dev), 2292103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2293103285Sikob ccb->ccb_h.func_code); 2294103285SikobEND_DEBUG 2295103285Sikob 2296108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2297103285Sikob xpt_done(ccb); 2298103285Sikob return; 2299103285Sikob } 2300103285Sikob break; 2301103285Sikob case XPT_PATH_INQ: 2302103285Sikob case XPT_NOOP: 2303103285Sikob /* The opcodes sometimes aimed at a target (sc is valid), 2304103285Sikob * sometimes aimed at the SIM (sc is invalid and target is 2305103285Sikob * CAM_TARGET_WILDCARD) 2306103285Sikob */ 2307103285Sikob if (sbp == NULL && 2308103285Sikob ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 2309103285SikobSBP_DEBUG(0) 2310103285Sikob printf("%s:%d:%d func_code 0x%04x: " 2311103285Sikob "Invalid target (no wildcard)\n", 2312103285Sikob device_get_nameunit(sbp->fd.dev), 2313103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2314103285Sikob ccb->ccb_h.func_code); 2315103285SikobEND_DEBUG 2316108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2317103285Sikob xpt_done(ccb); 2318103285Sikob return; 2319103285Sikob } 2320103285Sikob break; 2321103285Sikob default: 2322103285Sikob /* XXX Hm, we should check the input parameters */ 2323103285Sikob break; 2324103285Sikob } 2325103285Sikob 2326103285Sikob switch (ccb->ccb_h.func_code) { 2327103285Sikob case XPT_SCSI_IO: 2328103285Sikob { 2329103285Sikob struct ccb_scsiio *csio; 2330103285Sikob struct sbp_ocb *ocb; 2331113584Ssimokawa int speed; 2332106506Ssimokawa void *cdb; 2333103285Sikob 2334103285Sikob csio = &ccb->csio; 2335103285Sikob 2336121185SsimokawaSBP_DEBUG(2) 2337103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 2338103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 2339103285Sikob ", flags: 0x%02x, " 2340103285Sikob "%db cmd/%db data/%db sense\n", 2341103285Sikob device_get_nameunit(sbp->fd.dev), 2342103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2343103285Sikob csio->cdb_io.cdb_bytes[0], 2344103285Sikob csio->cdb_io.cdb_bytes[1], 2345103285Sikob csio->cdb_io.cdb_bytes[2], 2346103285Sikob csio->cdb_io.cdb_bytes[3], 2347103285Sikob csio->cdb_io.cdb_bytes[4], 2348103285Sikob csio->cdb_io.cdb_bytes[5], 2349103285Sikob csio->cdb_io.cdb_bytes[6], 2350103285Sikob csio->cdb_io.cdb_bytes[7], 2351103285Sikob csio->cdb_io.cdb_bytes[8], 2352103285Sikob csio->cdb_io.cdb_bytes[9], 2353103285Sikob ccb->ccb_h.flags & CAM_DIR_MASK, 2354103285Sikob csio->cdb_len, csio->dxfer_len, 2355103285Sikob csio->sense_len); 2356103285SikobEND_DEBUG 2357103285Sikob if(sdev == NULL){ 2358103285Sikob ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2359103285Sikob xpt_done(ccb); 2360103285Sikob return; 2361103285Sikob } 2362103285Sikob#if 0 2363103285Sikob /* if we are in probe stage, pass only probe commands */ 2364103285Sikob if (sdev->status == SBP_DEV_PROBE) { 2365103285Sikob char *name; 2366103285Sikob name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 2367103285Sikob printf("probe stage, periph name: %s\n", name); 2368103285Sikob if (strcmp(name, "probe") != 0) { 2369103285Sikob ccb->ccb_h.status = CAM_REQUEUE_REQ; 2370103285Sikob xpt_done(ccb); 2371103285Sikob return; 2372103285Sikob } 2373103285Sikob } 2374103285Sikob#endif 2375122528Ssimokawa if ((ocb = sbp_get_ocb(sdev)) == NULL) { 2376122528Ssimokawa ccb->ccb_h.status = CAM_REQUEUE_REQ; 2377127468Ssimokawa if (sdev->freeze == 0) { 2378127468Ssimokawa xpt_freeze_devq(sdev->path, 1); 2379127468Ssimokawa sdev->freeze ++; 2380127468Ssimokawa } 2381122528Ssimokawa xpt_done(ccb); 2382103285Sikob return; 2383122528Ssimokawa } 2384113584Ssimokawa 2385103285Sikob ocb->flags = OCB_ACT_CMD; 2386103285Sikob ocb->sdev = sdev; 2387103285Sikob ocb->ccb = ccb; 2388103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 2389103285Sikob ocb->orb[0] = htonl(1 << 31); 2390103285Sikob ocb->orb[1] = 0; 2391103285Sikob ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 2392113584Ssimokawa ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET); 2393103285Sikob speed = min(target->fwdev->speed, max_speed); 2394103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 2395103285Sikob | ORB_CMD_MAXP(speed + 7)); 2396103285Sikob if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 2397103285Sikob ocb->orb[4] |= htonl(ORB_CMD_IN); 2398103285Sikob } 2399103285Sikob 2400103285Sikob if (csio->ccb_h.flags & CAM_SCATTER_VALID) 2401103285Sikob printf("sbp: CAM_SCATTER_VALID\n"); 2402103285Sikob if (csio->ccb_h.flags & CAM_DATA_PHYS) 2403103285Sikob printf("sbp: CAM_DATA_PHYS\n"); 2404103285Sikob 2405106506Ssimokawa if (csio->ccb_h.flags & CAM_CDB_POINTER) 2406106506Ssimokawa cdb = (void *)csio->cdb_io.cdb_ptr; 2407106506Ssimokawa else 2408106506Ssimokawa cdb = (void *)&csio->cdb_io.cdb_bytes; 2409120660Ssimokawa bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len); 2410103285Sikob/* 2411103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 2412103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 2413103285Sikob*/ 2414103285Sikob if (ccb->csio.dxfer_len > 0) { 2415113584Ssimokawa int s, error; 2416103285Sikob 2417103285Sikob s = splsoftvm(); 2418113584Ssimokawa error = bus_dmamap_load(/*dma tag*/sbp->dmat, 2419103285Sikob /*dma map*/ocb->dmamap, 2420103285Sikob ccb->csio.data_ptr, 2421103285Sikob ccb->csio.dxfer_len, 2422103285Sikob sbp_execute_ocb, 2423103285Sikob ocb, 2424103285Sikob /*flags*/0); 2425103285Sikob splx(s); 2426113584Ssimokawa if (error) 2427113584Ssimokawa printf("sbp: bus_dmamap_load error %d\n", error); 2428103285Sikob } else 2429103285Sikob sbp_execute_ocb(ocb, NULL, 0, 0); 2430103285Sikob break; 2431103285Sikob } 2432103285Sikob case XPT_CALC_GEOMETRY: 2433103285Sikob { 2434103285Sikob struct ccb_calc_geometry *ccg; 2435127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100 2436129585Sdfr uint32_t size_mb; 2437129585Sdfr uint32_t secs_per_cylinder; 2438116429Ssimokawa int extended = 1; 2439116429Ssimokawa#endif 2440116351Snjl 2441103285Sikob ccg = &ccb->ccg; 2442103285Sikob if (ccg->block_size == 0) { 2443103285Sikob printf("sbp_action1: block_size is 0.\n"); 2444103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2445103285Sikob xpt_done(ccb); 2446103285Sikob break; 2447103285Sikob } 2448103285SikobSBP_DEBUG(1) 2449103285Sikob printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2450127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2451127468Ssimokawa "Volume size = %d\n", 2452127468Ssimokawa#else 2453114540Sjake "Volume size = %jd\n", 2454114927Ssimokawa#endif 2455114927Ssimokawa device_get_nameunit(sbp->fd.dev), 2456114927Ssimokawa cam_sim_path(sbp->sim), 2457103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2458127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2459114927Ssimokawa (uintmax_t) 2460114927Ssimokawa#endif 2461114927Ssimokawa ccg->volume_size); 2462103285SikobEND_DEBUG 2463103285Sikob 2464127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100 2465116429Ssimokawa size_mb = ccg->volume_size 2466116429Ssimokawa / ((1024L * 1024L) / ccg->block_size); 2467116429Ssimokawa 2468116429Ssimokawa if (size_mb > 1024 && extended) { 2469116429Ssimokawa ccg->heads = 255; 2470116429Ssimokawa ccg->secs_per_track = 63; 2471116429Ssimokawa } else { 2472116429Ssimokawa ccg->heads = 64; 2473116429Ssimokawa ccg->secs_per_track = 32; 2474116429Ssimokawa } 2475116429Ssimokawa secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2476116429Ssimokawa ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2477116429Ssimokawa ccb->ccb_h.status = CAM_REQ_CMP; 2478116429Ssimokawa#else 2479116351Snjl cam_calc_geometry(ccg, /*extended*/1); 2480116429Ssimokawa#endif 2481103285Sikob xpt_done(ccb); 2482103285Sikob break; 2483103285Sikob } 2484103285Sikob case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2485103285Sikob { 2486103285Sikob 2487103285SikobSBP_DEBUG(1) 2488103285Sikob printf("%s:%d:XPT_RESET_BUS: \n", 2489103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 2490103285SikobEND_DEBUG 2491103285Sikob 2492103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2493103285Sikob xpt_done(ccb); 2494103285Sikob break; 2495103285Sikob } 2496103285Sikob case XPT_PATH_INQ: /* Path routing inquiry */ 2497103285Sikob { 2498103285Sikob struct ccb_pathinq *cpi = &ccb->cpi; 2499103285Sikob 2500103285SikobSBP_DEBUG(1) 2501103285Sikob printf("%s:%d:%d XPT_PATH_INQ:.\n", 2502103285Sikob device_get_nameunit(sbp->fd.dev), 2503103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2504103285SikobEND_DEBUG 2505103285Sikob cpi->version_num = 1; /* XXX??? */ 2506111615Ssimokawa cpi->hba_inquiry = PI_TAG_ABLE; 2507103285Sikob cpi->target_sprt = 0; 2508118105Snjl cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE; 2509103285Sikob cpi->hba_eng_cnt = 0; 2510103285Sikob cpi->max_target = SBP_NUM_TARGETS - 1; 2511103285Sikob cpi->max_lun = SBP_NUM_LUNS - 1; 2512103285Sikob cpi->initiator_id = SBP_INITIATOR; 2513103285Sikob cpi->bus_id = sim->bus_id; 2514103285Sikob cpi->base_transfer_speed = 400 * 1000 / 8; 2515103285Sikob strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2516103285Sikob strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 2517103285Sikob strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 2518103285Sikob cpi->unit_number = sim->unit_number; 2519163816Smjacob cpi->transport = XPORT_SPI; /* XX should have a FireWire */ 2520163816Smjacob cpi->transport_version = 2; 2521163816Smjacob cpi->protocol = PROTO_SCSI; 2522163816Smjacob cpi->protocol_version = SCSI_REV_2; 2523103285Sikob 2524103285Sikob cpi->ccb_h.status = CAM_REQ_CMP; 2525103285Sikob xpt_done(ccb); 2526103285Sikob break; 2527103285Sikob } 2528103285Sikob case XPT_GET_TRAN_SETTINGS: 2529103285Sikob { 2530103285Sikob struct ccb_trans_settings *cts = &ccb->cts; 2531163816Smjacob struct ccb_trans_settings_scsi *scsi = 2532163816Smjacob &cts->proto_specific.scsi; 2533163816Smjacob struct ccb_trans_settings_spi *spi = 2534163816Smjacob &cts->xport_specific.spi; 2535163816Smjacob 2536163816Smjacob cts->protocol = PROTO_SCSI; 2537163816Smjacob cts->protocol_version = SCSI_REV_2; 2538163816Smjacob cts->transport = XPORT_SPI; /* should have a FireWire */ 2539163816Smjacob cts->transport_version = 2; 2540163816Smjacob spi->valid = CTS_SPI_VALID_DISC; 2541163816Smjacob spi->flags = CTS_SPI_FLAGS_DISC_ENB; 2542163816Smjacob scsi->valid = CTS_SCSI_VALID_TQ; 2543163816Smjacob scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 2544103285SikobSBP_DEBUG(1) 2545103285Sikob printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 2546103285Sikob device_get_nameunit(sbp->fd.dev), 2547103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2548103285SikobEND_DEBUG 2549103285Sikob cts->ccb_h.status = CAM_REQ_CMP; 2550103285Sikob xpt_done(ccb); 2551103285Sikob break; 2552103285Sikob } 2553103285Sikob case XPT_ABORT: 2554103285Sikob ccb->ccb_h.status = CAM_UA_ABORT; 2555103285Sikob xpt_done(ccb); 2556103285Sikob break; 2557111615Ssimokawa case XPT_SET_TRAN_SETTINGS: 2558111615Ssimokawa /* XXX */ 2559103285Sikob default: 2560103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2561103285Sikob xpt_done(ccb); 2562103285Sikob break; 2563103285Sikob } 2564103285Sikob return; 2565103285Sikob} 2566103285Sikob 2567103285Sikobstatic void 2568103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb) 2569103285Sikob{ 2570103285Sikob int s; 2571103285Sikob 2572103285Sikob s = splfw(); 2573103285Sikob sbp_action1(sim, ccb); 2574103285Sikob splx(s); 2575103285Sikob} 2576103285Sikob 2577103285Sikobstatic void 2578103285Sikobsbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 2579103285Sikob{ 2580103285Sikob int i; 2581103285Sikob struct sbp_ocb *ocb; 2582103285Sikob struct sbp_ocb *prev; 2583105633Ssimokawa bus_dma_segment_t *s; 2584103285Sikob 2585103285Sikob if (error) 2586103285Sikob printf("sbp_execute_ocb: error=%d\n", error); 2587103285Sikob 2588103285Sikob ocb = (struct sbp_ocb *)arg; 2589113584Ssimokawa 2590121185SsimokawaSBP_DEBUG(2) 2591113584Ssimokawa printf("sbp_execute_ocb: seg %d", seg); 2592113584Ssimokawa for (i = 0; i < seg; i++) 2593127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2594127468Ssimokawa printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len); 2595127468Ssimokawa#else 2596113972Ssimokawa printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr, 2597113972Ssimokawa (uintmax_t)segments[i].ds_len); 2598113584Ssimokawa#endif 2599113584Ssimokawa printf("\n"); 2600113584SsimokawaEND_DEBUG 2601113584Ssimokawa 2602103285Sikob if (seg == 1) { 2603103285Sikob /* direct pointer */ 2604113474Ssimokawa s = &segments[0]; 2605113474Ssimokawa if (s->ds_len > SBP_SEG_MAX) 2606113474Ssimokawa panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2607113474Ssimokawa ocb->orb[3] = htonl(s->ds_addr); 2608113474Ssimokawa ocb->orb[4] |= htonl(s->ds_len); 2609103285Sikob } else if(seg > 1) { 2610103285Sikob /* page table */ 2611103285Sikob for (i = 0; i < seg; i++) { 2612105633Ssimokawa s = &segments[i]; 2613108877SsimokawaSBP_DEBUG(0) 2614108877Ssimokawa /* XXX LSI Logic "< 16 byte" bug might be hit */ 2615105633Ssimokawa if (s->ds_len < 16) 2616105633Ssimokawa printf("sbp_execute_ocb: warning, " 2617127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2618127468Ssimokawa "segment length(%d) is less than 16." 2619127468Ssimokawa#else 2620106543Ssimokawa "segment length(%zd) is less than 16." 2621108877Ssimokawa#endif 2622105633Ssimokawa "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2623108877SsimokawaEND_DEBUG 2624113474Ssimokawa if (s->ds_len > SBP_SEG_MAX) 2625113474Ssimokawa panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2626105633Ssimokawa ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2627105633Ssimokawa ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2628103285Sikob } 2629103285Sikob ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2630103285Sikob } 2631103285Sikob 2632113584Ssimokawa if (seg > 0) 2633113584Ssimokawa bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap, 2634113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2635113584Ssimokawa BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 2636103285Sikob prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2637113584Ssimokawa fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE); 2638127468Ssimokawa if (use_doorbell) { 2639127468Ssimokawa if (prev == NULL) { 2640127468Ssimokawa if (ocb->sdev->last_ocb != NULL) 2641127468Ssimokawa sbp_doorbell(ocb->sdev); 2642127468Ssimokawa else 2643127468Ssimokawa sbp_orb_pointer(ocb->sdev, ocb); 2644127468Ssimokawa } 2645127468Ssimokawa } else { 2646127468Ssimokawa if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) { 2647127468Ssimokawa ocb->sdev->flags &= ~ORB_LINK_DEAD; 2648127468Ssimokawa sbp_orb_pointer(ocb->sdev, ocb); 2649127468Ssimokawa } 2650120842Ssimokawa } 2651103285Sikob} 2652103285Sikob 2653103285Sikobstatic void 2654103285Sikobsbp_poll(struct cam_sim *sim) 2655103285Sikob{ 2656120847Ssimokawa struct sbp_softc *sbp; 2657120847Ssimokawa struct firewire_comm *fc; 2658120847Ssimokawa 2659120847Ssimokawa sbp = (struct sbp_softc *)sim->softc; 2660120847Ssimokawa fc = sbp->fd.fc; 2661120847Ssimokawa 2662120847Ssimokawa fc->poll(fc, 0, -1); 2663120847Ssimokawa 2664103285Sikob return; 2665103285Sikob} 2666120847Ssimokawa 2667103285Sikobstatic struct sbp_ocb * 2668111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status) 2669103285Sikob{ 2670103285Sikob struct sbp_ocb *ocb; 2671103285Sikob struct sbp_ocb *next; 2672103285Sikob int s = splfw(), order = 0; 2673103285Sikob int flags; 2674103285Sikob 2675103285SikobSBP_DEBUG(1) 2676121185Ssimokawa sbp_show_sdev_info(sdev, 2); 2677127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2678127468Ssimokawa printf("%s: 0x%08lx src %d\n", 2679127468Ssimokawa#else 2680121185Ssimokawa printf("%s: 0x%08x src %d\n", 2681108712Ssimokawa#endif 2682127468Ssimokawa __func__, ntohl(sbp_status->orb_lo), sbp_status->src); 2683103285SikobEND_DEBUG 2684121185Ssimokawa for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2685121185Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2686121185Ssimokawa flags = ocb->flags; 2687111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 2688103285Sikob /* found */ 2689111819Ssimokawa STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2690103285Sikob if (ocb->ccb != NULL) 2691103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2692103285Sikob ocb->ccb->ccb_h.timeout_ch); 2693113584Ssimokawa if (ntohl(ocb->orb[4]) & 0xffff) { 2694113584Ssimokawa bus_dmamap_sync(sdev->target->sbp->dmat, 2695113584Ssimokawa ocb->dmamap, 2696113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2697113584Ssimokawa BUS_DMASYNC_POSTREAD : 2698113584Ssimokawa BUS_DMASYNC_POSTWRITE); 2699113584Ssimokawa bus_dmamap_unload(sdev->target->sbp->dmat, 2700113584Ssimokawa ocb->dmamap); 2701103285Sikob } 2702127468Ssimokawa if (!use_doorbell) { 2703127468Ssimokawa if (sbp_status->src == SRC_NO_NEXT) { 2704127468Ssimokawa if (next != NULL) 2705127468Ssimokawa sbp_orb_pointer(sdev, next); 2706127468Ssimokawa else if (order > 0) { 2707127468Ssimokawa /* 2708127468Ssimokawa * Unordered execution 2709127468Ssimokawa * We need to send pointer for 2710127468Ssimokawa * next ORB 2711127468Ssimokawa */ 2712127468Ssimokawa sdev->flags |= ORB_LINK_DEAD; 2713127468Ssimokawa } 2714120842Ssimokawa } 2715127468Ssimokawa } else { 2716127468Ssimokawa /* 2717127468Ssimokawa * XXX this is not correct for unordered 2718127468Ssimokawa * execution. 2719127468Ssimokawa */ 2720127468Ssimokawa if (sdev->last_ocb != NULL) 2721127468Ssimokawa sbp_free_ocb(sdev, sdev->last_ocb); 2722127468Ssimokawa sdev->last_ocb = ocb; 2723127468Ssimokawa if (next != NULL && 2724127468Ssimokawa sbp_status->src == SRC_NO_NEXT) 2725127468Ssimokawa sbp_doorbell(sdev); 2726120842Ssimokawa } 2727103285Sikob break; 2728111819Ssimokawa } else 2729111819Ssimokawa order ++; 2730103285Sikob } 2731103285Sikob splx(s); 2732103285SikobSBP_DEBUG(0) 2733103285Sikob if (ocb && order > 0) { 2734103285Sikob sbp_show_sdev_info(sdev, 2); 2735103285Sikob printf("unordered execution order:%d\n", order); 2736103285Sikob } 2737103285SikobEND_DEBUG 2738103285Sikob return (ocb); 2739103285Sikob} 2740103285Sikob 2741103285Sikobstatic struct sbp_ocb * 2742103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2743103285Sikob{ 2744103285Sikob int s = splfw(); 2745127468Ssimokawa struct sbp_ocb *prev, *prev2; 2746103285Sikob 2747121185SsimokawaSBP_DEBUG(1) 2748103285Sikob sbp_show_sdev_info(sdev, 2); 2749127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2750127468Ssimokawa printf("%s: 0x%08x\n", __func__, ocb->bus_addr); 2751108712Ssimokawa#else 2752127468Ssimokawa printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr); 2753108712Ssimokawa#endif 2754103285SikobEND_DEBUG 2755127468Ssimokawa prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2756103285Sikob STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2757103285Sikob 2758103285Sikob if (ocb->ccb != NULL) 2759103285Sikob ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2760103285Sikob (ocb->ccb->ccb_h.timeout * hz) / 1000); 2761103285Sikob 2762127468Ssimokawa if (use_doorbell && prev == NULL) 2763127468Ssimokawa prev2 = sdev->last_ocb; 2764127468Ssimokawa 2765127468Ssimokawa if (prev2 != NULL) { 2766121185SsimokawaSBP_DEBUG(2) 2767127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2768127468Ssimokawa printf("linking chain 0x%x -> 0x%x\n", 2769127468Ssimokawa prev2->bus_addr, ocb->bus_addr); 2770127468Ssimokawa#else 2771120842Ssimokawa printf("linking chain 0x%jx -> 0x%jx\n", 2772127468Ssimokawa (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr); 2773108712Ssimokawa#endif 2774103285SikobEND_DEBUG 2775127468Ssimokawa prev2->orb[1] = htonl(ocb->bus_addr); 2776127468Ssimokawa prev2->orb[0] = 0; 2777103285Sikob } 2778103285Sikob splx(s); 2779103285Sikob 2780103285Sikob return prev; 2781103285Sikob} 2782103285Sikob 2783103285Sikobstatic struct sbp_ocb * 2784113584Ssimokawasbp_get_ocb(struct sbp_dev *sdev) 2785103285Sikob{ 2786103285Sikob struct sbp_ocb *ocb; 2787103285Sikob int s = splfw(); 2788113584Ssimokawa ocb = STAILQ_FIRST(&sdev->free_ocbs); 2789103285Sikob if (ocb == NULL) { 2790127468Ssimokawa sdev->flags |= ORB_SHORTAGE; 2791103285Sikob printf("ocb shortage!!!\n"); 2792134868Ssimokawa splx(s); 2793103285Sikob return NULL; 2794103285Sikob } 2795113584Ssimokawa STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb); 2796103285Sikob splx(s); 2797103285Sikob ocb->ccb = NULL; 2798103285Sikob return (ocb); 2799103285Sikob} 2800103285Sikob 2801103285Sikobstatic void 2802113584Ssimokawasbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2803103285Sikob{ 2804103285Sikob ocb->flags = 0; 2805103285Sikob ocb->ccb = NULL; 2806113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb); 2807127468Ssimokawa if ((sdev->flags & ORB_SHORTAGE) != 0) { 2808127468Ssimokawa int count; 2809127468Ssimokawa 2810127468Ssimokawa sdev->flags &= ~ORB_SHORTAGE; 2811127468Ssimokawa count = sdev->freeze; 2812127468Ssimokawa sdev->freeze = 0; 2813127468Ssimokawa xpt_release_devq(sdev->path, count, TRUE); 2814127468Ssimokawa } 2815103285Sikob} 2816103285Sikob 2817103285Sikobstatic void 2818103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status) 2819103285Sikob{ 2820103285Sikob struct sbp_dev *sdev; 2821103285Sikob 2822103285Sikob sdev = ocb->sdev; 2823113584SsimokawaSBP_DEBUG(0) 2824103285Sikob sbp_show_sdev_info(sdev, 2); 2825127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2826127468Ssimokawa printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr); 2827127468Ssimokawa#else 2828113972Ssimokawa printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr); 2829111819Ssimokawa#endif 2830113584SsimokawaEND_DEBUG 2831113584SsimokawaSBP_DEBUG(1) 2832105792Ssimokawa if (ocb->ccb != NULL) 2833105792Ssimokawa sbp_print_scsi_cmd(ocb); 2834103285SikobEND_DEBUG 2835113584Ssimokawa if (ntohl(ocb->orb[4]) & 0xffff) { 2836113584Ssimokawa bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap, 2837113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2838113584Ssimokawa BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 2839113584Ssimokawa bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap); 2840113584Ssimokawa } 2841111819Ssimokawa if (ocb->ccb != NULL) { 2842110336Ssimokawa untimeout(sbp_timeout, (caddr_t)ocb, 2843110336Ssimokawa ocb->ccb->ccb_h.timeout_ch); 2844103285Sikob ocb->ccb->ccb_h.status = status; 2845103285Sikob xpt_done(ocb->ccb); 2846103285Sikob } 2847113584Ssimokawa sbp_free_ocb(sdev, ocb); 2848103285Sikob} 2849103285Sikob 2850103285Sikobstatic void 2851103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2852103285Sikob{ 2853103285Sikob int s; 2854105792Ssimokawa struct sbp_ocb *ocb, *next; 2855105792Ssimokawa STAILQ_HEAD(, sbp_ocb) temp; 2856103285Sikob 2857103285Sikob s = splfw(); 2858105792Ssimokawa 2859105792Ssimokawa bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2860105792Ssimokawa STAILQ_INIT(&sdev->ocbs); 2861105792Ssimokawa for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2862105792Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2863103285Sikob sbp_abort_ocb(ocb, status); 2864103285Sikob } 2865127468Ssimokawa if (sdev->last_ocb != NULL) { 2866127468Ssimokawa sbp_free_ocb(sdev, sdev->last_ocb); 2867127468Ssimokawa sdev->last_ocb = NULL; 2868127468Ssimokawa } 2869105792Ssimokawa 2870103285Sikob splx(s); 2871103285Sikob} 2872103285Sikob 2873103285Sikobstatic devclass_t sbp_devclass; 2874103285Sikob 2875103285Sikobstatic device_method_t sbp_methods[] = { 2876103285Sikob /* device interface */ 2877103285Sikob DEVMETHOD(device_identify, sbp_identify), 2878103285Sikob DEVMETHOD(device_probe, sbp_probe), 2879103285Sikob DEVMETHOD(device_attach, sbp_attach), 2880103285Sikob DEVMETHOD(device_detach, sbp_detach), 2881110145Ssimokawa DEVMETHOD(device_shutdown, sbp_shutdown), 2882103285Sikob 2883103285Sikob { 0, 0 } 2884103285Sikob}; 2885103285Sikob 2886103285Sikobstatic driver_t sbp_driver = { 2887103285Sikob "sbp", 2888103285Sikob sbp_methods, 2889103285Sikob sizeof(struct sbp_softc), 2890103285Sikob}; 2891127468Ssimokawa#ifdef __DragonFly__ 2892127468SsimokawaDECLARE_DUMMY_MODULE(sbp); 2893127468Ssimokawa#endif 2894103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2895103285SikobMODULE_VERSION(sbp, 1); 2896103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1); 2897103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1); 2898